Loading...
1/* sunvdc.c: Sun LDOM Virtual Disk Client.
2 *
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/blkdev.h>
10#include <linux/hdreg.h>
11#include <linux/genhd.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/completion.h>
15#include <linux/delay.h>
16#include <linux/init.h>
17#include <linux/list.h>
18#include <linux/scatterlist.h>
19
20#include <asm/vio.h>
21#include <asm/ldc.h>
22
23#define DRV_MODULE_NAME "sunvdc"
24#define PFX DRV_MODULE_NAME ": "
25#define DRV_MODULE_VERSION "1.0"
26#define DRV_MODULE_RELDATE "June 25, 2007"
27
28static char version[] __devinitdata =
29 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
30MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
31MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
32MODULE_LICENSE("GPL");
33MODULE_VERSION(DRV_MODULE_VERSION);
34
35#define VDC_TX_RING_SIZE 256
36
37#define WAITING_FOR_LINK_UP 0x01
38#define WAITING_FOR_TX_SPACE 0x02
39#define WAITING_FOR_GEN_CMD 0x04
40#define WAITING_FOR_ANY -1
41
42struct vdc_req_entry {
43 struct request *req;
44};
45
46struct vdc_port {
47 struct vio_driver_state vio;
48
49 struct gendisk *disk;
50
51 struct vdc_completion *cmp;
52
53 u64 req_id;
54 u64 seq;
55 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
56
57 unsigned long ring_cookies;
58
59 u64 max_xfer_size;
60 u32 vdisk_block_size;
61
62 /* The server fills these in for us in the disk attribute
63 * ACK packet.
64 */
65 u64 operations;
66 u32 vdisk_size;
67 u8 vdisk_type;
68
69 char disk_name[32];
70
71 struct vio_disk_geom geom;
72 struct vio_disk_vtoc label;
73};
74
75static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
76{
77 return container_of(vio, struct vdc_port, vio);
78}
79
80/* Ordered from largest major to lowest */
81static struct vio_version vdc_versions[] = {
82 { .major = 1, .minor = 0 },
83};
84
85#define VDCBLK_NAME "vdisk"
86static int vdc_major;
87#define PARTITION_SHIFT 3
88
89static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
90{
91 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
92}
93
94static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
95{
96 struct gendisk *disk = bdev->bd_disk;
97 struct vdc_port *port = disk->private_data;
98
99 geo->heads = (u8) port->geom.num_hd;
100 geo->sectors = (u8) port->geom.num_sec;
101 geo->cylinders = port->geom.num_cyl;
102
103 return 0;
104}
105
106static const struct block_device_operations vdc_fops = {
107 .owner = THIS_MODULE,
108 .getgeo = vdc_getgeo,
109};
110
111static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
112{
113 if (vio->cmp &&
114 (waiting_for == -1 ||
115 vio->cmp->waiting_for == waiting_for)) {
116 vio->cmp->err = err;
117 complete(&vio->cmp->com);
118 vio->cmp = NULL;
119 }
120}
121
122static void vdc_handshake_complete(struct vio_driver_state *vio)
123{
124 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
125}
126
127static int vdc_handle_unknown(struct vdc_port *port, void *arg)
128{
129 struct vio_msg_tag *pkt = arg;
130
131 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
132 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
133 printk(KERN_ERR PFX "Resetting connection.\n");
134
135 ldc_disconnect(port->vio.lp);
136
137 return -ECONNRESET;
138}
139
140static int vdc_send_attr(struct vio_driver_state *vio)
141{
142 struct vdc_port *port = to_vdc_port(vio);
143 struct vio_disk_attr_info pkt;
144
145 memset(&pkt, 0, sizeof(pkt));
146
147 pkt.tag.type = VIO_TYPE_CTRL;
148 pkt.tag.stype = VIO_SUBTYPE_INFO;
149 pkt.tag.stype_env = VIO_ATTR_INFO;
150 pkt.tag.sid = vio_send_sid(vio);
151
152 pkt.xfer_mode = VIO_DRING_MODE;
153 pkt.vdisk_block_size = port->vdisk_block_size;
154 pkt.max_xfer_size = port->max_xfer_size;
155
156 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
157 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
158
159 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
160}
161
162static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
163{
164 struct vdc_port *port = to_vdc_port(vio);
165 struct vio_disk_attr_info *pkt = arg;
166
167 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
168 "xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
169 pkt->tag.stype, pkt->operations,
170 pkt->vdisk_size, pkt->vdisk_type,
171 pkt->xfer_mode, pkt->vdisk_block_size,
172 pkt->max_xfer_size);
173
174 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
175 switch (pkt->vdisk_type) {
176 case VD_DISK_TYPE_DISK:
177 case VD_DISK_TYPE_SLICE:
178 break;
179
180 default:
181 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
182 vio->name, pkt->vdisk_type);
183 return -ECONNRESET;
184 }
185
186 if (pkt->vdisk_block_size > port->vdisk_block_size) {
187 printk(KERN_ERR PFX "%s: BLOCK size increased "
188 "%u --> %u\n",
189 vio->name,
190 port->vdisk_block_size, pkt->vdisk_block_size);
191 return -ECONNRESET;
192 }
193
194 port->operations = pkt->operations;
195 port->vdisk_size = pkt->vdisk_size;
196 port->vdisk_type = pkt->vdisk_type;
197 if (pkt->max_xfer_size < port->max_xfer_size)
198 port->max_xfer_size = pkt->max_xfer_size;
199 port->vdisk_block_size = pkt->vdisk_block_size;
200 return 0;
201 } else {
202 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
203
204 return -ECONNRESET;
205 }
206}
207
208static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
209{
210 int err = desc->status;
211
212 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
213}
214
215static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
216 unsigned int index)
217{
218 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
219 struct vdc_req_entry *rqe = &port->rq_arr[index];
220 struct request *req;
221
222 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
223 return;
224
225 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
226 desc->hdr.state = VIO_DESC_FREE;
227 dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
228
229 req = rqe->req;
230 if (req == NULL) {
231 vdc_end_special(port, desc);
232 return;
233 }
234
235 rqe->req = NULL;
236
237 __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
238
239 if (blk_queue_stopped(port->disk->queue))
240 blk_start_queue(port->disk->queue);
241}
242
243static int vdc_ack(struct vdc_port *port, void *msgbuf)
244{
245 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
246 struct vio_dring_data *pkt = msgbuf;
247
248 if (unlikely(pkt->dring_ident != dr->ident ||
249 pkt->start_idx != pkt->end_idx ||
250 pkt->start_idx >= VDC_TX_RING_SIZE))
251 return 0;
252
253 vdc_end_one(port, dr, pkt->start_idx);
254
255 return 0;
256}
257
258static int vdc_nack(struct vdc_port *port, void *msgbuf)
259{
260 /* XXX Implement me XXX */
261 return 0;
262}
263
264static void vdc_event(void *arg, int event)
265{
266 struct vdc_port *port = arg;
267 struct vio_driver_state *vio = &port->vio;
268 unsigned long flags;
269 int err;
270
271 spin_lock_irqsave(&vio->lock, flags);
272
273 if (unlikely(event == LDC_EVENT_RESET ||
274 event == LDC_EVENT_UP)) {
275 vio_link_state_change(vio, event);
276 spin_unlock_irqrestore(&vio->lock, flags);
277 return;
278 }
279
280 if (unlikely(event != LDC_EVENT_DATA_READY)) {
281 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
282 spin_unlock_irqrestore(&vio->lock, flags);
283 return;
284 }
285
286 err = 0;
287 while (1) {
288 union {
289 struct vio_msg_tag tag;
290 u64 raw[8];
291 } msgbuf;
292
293 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
294 if (unlikely(err < 0)) {
295 if (err == -ECONNRESET)
296 vio_conn_reset(vio);
297 break;
298 }
299 if (err == 0)
300 break;
301 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
302 msgbuf.tag.type,
303 msgbuf.tag.stype,
304 msgbuf.tag.stype_env,
305 msgbuf.tag.sid);
306 err = vio_validate_sid(vio, &msgbuf.tag);
307 if (err < 0)
308 break;
309
310 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
311 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
312 err = vdc_ack(port, &msgbuf);
313 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
314 err = vdc_nack(port, &msgbuf);
315 else
316 err = vdc_handle_unknown(port, &msgbuf);
317 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
318 err = vio_control_pkt_engine(vio, &msgbuf);
319 } else {
320 err = vdc_handle_unknown(port, &msgbuf);
321 }
322 if (err < 0)
323 break;
324 }
325 if (err < 0)
326 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
327 spin_unlock_irqrestore(&vio->lock, flags);
328}
329
330static int __vdc_tx_trigger(struct vdc_port *port)
331{
332 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
333 struct vio_dring_data hdr = {
334 .tag = {
335 .type = VIO_TYPE_DATA,
336 .stype = VIO_SUBTYPE_INFO,
337 .stype_env = VIO_DRING_DATA,
338 .sid = vio_send_sid(&port->vio),
339 },
340 .dring_ident = dr->ident,
341 .start_idx = dr->prod,
342 .end_idx = dr->prod,
343 };
344 int err, delay;
345
346 hdr.seq = dr->snd_nxt;
347 delay = 1;
348 do {
349 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
350 if (err > 0) {
351 dr->snd_nxt++;
352 break;
353 }
354 udelay(delay);
355 if ((delay <<= 1) > 128)
356 delay = 128;
357 } while (err == -EAGAIN);
358
359 return err;
360}
361
362static int __send_request(struct request *req)
363{
364 struct vdc_port *port = req->rq_disk->private_data;
365 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
366 struct scatterlist sg[port->ring_cookies];
367 struct vdc_req_entry *rqe;
368 struct vio_disk_desc *desc;
369 unsigned int map_perm;
370 int nsg, err, i;
371 u64 len;
372 u8 op;
373
374 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
375
376 if (rq_data_dir(req) == READ) {
377 map_perm |= LDC_MAP_W;
378 op = VD_OP_BREAD;
379 } else {
380 map_perm |= LDC_MAP_R;
381 op = VD_OP_BWRITE;
382 }
383
384 sg_init_table(sg, port->ring_cookies);
385 nsg = blk_rq_map_sg(req->q, req, sg);
386
387 len = 0;
388 for (i = 0; i < nsg; i++)
389 len += sg[i].length;
390
391 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
392 blk_stop_queue(port->disk->queue);
393 err = -ENOMEM;
394 goto out;
395 }
396
397 desc = vio_dring_cur(dr);
398
399 err = ldc_map_sg(port->vio.lp, sg, nsg,
400 desc->cookies, port->ring_cookies,
401 map_perm);
402 if (err < 0) {
403 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
404 return err;
405 }
406
407 rqe = &port->rq_arr[dr->prod];
408 rqe->req = req;
409
410 desc->hdr.ack = VIO_ACK_ENABLE;
411 desc->req_id = port->req_id;
412 desc->operation = op;
413 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
414 desc->slice = 0xff;
415 } else {
416 desc->slice = 0;
417 }
418 desc->status = ~0;
419 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
420 desc->size = len;
421 desc->ncookies = err;
422
423 /* This has to be a non-SMP write barrier because we are writing
424 * to memory which is shared with the peer LDOM.
425 */
426 wmb();
427 desc->hdr.state = VIO_DESC_READY;
428
429 err = __vdc_tx_trigger(port);
430 if (err < 0) {
431 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
432 } else {
433 port->req_id++;
434 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
435 }
436out:
437
438 return err;
439}
440
441static void do_vdc_request(struct request_queue *q)
442{
443 while (1) {
444 struct request *req = blk_fetch_request(q);
445
446 if (!req)
447 break;
448
449 if (__send_request(req) < 0)
450 __blk_end_request_all(req, -EIO);
451 }
452}
453
454static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
455{
456 struct vio_dring_state *dr;
457 struct vio_completion comp;
458 struct vio_disk_desc *desc;
459 unsigned int map_perm;
460 unsigned long flags;
461 int op_len, err;
462 void *req_buf;
463
464 if (!(((u64)1 << ((u64)op - 1)) & port->operations))
465 return -EOPNOTSUPP;
466
467 switch (op) {
468 case VD_OP_BREAD:
469 case VD_OP_BWRITE:
470 default:
471 return -EINVAL;
472
473 case VD_OP_FLUSH:
474 op_len = 0;
475 map_perm = 0;
476 break;
477
478 case VD_OP_GET_WCE:
479 op_len = sizeof(u32);
480 map_perm = LDC_MAP_W;
481 break;
482
483 case VD_OP_SET_WCE:
484 op_len = sizeof(u32);
485 map_perm = LDC_MAP_R;
486 break;
487
488 case VD_OP_GET_VTOC:
489 op_len = sizeof(struct vio_disk_vtoc);
490 map_perm = LDC_MAP_W;
491 break;
492
493 case VD_OP_SET_VTOC:
494 op_len = sizeof(struct vio_disk_vtoc);
495 map_perm = LDC_MAP_R;
496 break;
497
498 case VD_OP_GET_DISKGEOM:
499 op_len = sizeof(struct vio_disk_geom);
500 map_perm = LDC_MAP_W;
501 break;
502
503 case VD_OP_SET_DISKGEOM:
504 op_len = sizeof(struct vio_disk_geom);
505 map_perm = LDC_MAP_R;
506 break;
507
508 case VD_OP_SCSICMD:
509 op_len = 16;
510 map_perm = LDC_MAP_RW;
511 break;
512
513 case VD_OP_GET_DEVID:
514 op_len = sizeof(struct vio_disk_devid);
515 map_perm = LDC_MAP_W;
516 break;
517
518 case VD_OP_GET_EFI:
519 case VD_OP_SET_EFI:
520 return -EOPNOTSUPP;
521 break;
522 };
523
524 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
525
526 op_len = (op_len + 7) & ~7;
527 req_buf = kzalloc(op_len, GFP_KERNEL);
528 if (!req_buf)
529 return -ENOMEM;
530
531 if (len > op_len)
532 len = op_len;
533
534 if (map_perm & LDC_MAP_R)
535 memcpy(req_buf, buf, len);
536
537 spin_lock_irqsave(&port->vio.lock, flags);
538
539 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
540
541 /* XXX If we want to use this code generically we have to
542 * XXX handle TX ring exhaustion etc.
543 */
544 desc = vio_dring_cur(dr);
545
546 err = ldc_map_single(port->vio.lp, req_buf, op_len,
547 desc->cookies, port->ring_cookies,
548 map_perm);
549 if (err < 0) {
550 spin_unlock_irqrestore(&port->vio.lock, flags);
551 kfree(req_buf);
552 return err;
553 }
554
555 init_completion(&comp.com);
556 comp.waiting_for = WAITING_FOR_GEN_CMD;
557 port->vio.cmp = ∁
558
559 desc->hdr.ack = VIO_ACK_ENABLE;
560 desc->req_id = port->req_id;
561 desc->operation = op;
562 desc->slice = 0;
563 desc->status = ~0;
564 desc->offset = 0;
565 desc->size = op_len;
566 desc->ncookies = err;
567
568 /* This has to be a non-SMP write barrier because we are writing
569 * to memory which is shared with the peer LDOM.
570 */
571 wmb();
572 desc->hdr.state = VIO_DESC_READY;
573
574 err = __vdc_tx_trigger(port);
575 if (err >= 0) {
576 port->req_id++;
577 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
578 spin_unlock_irqrestore(&port->vio.lock, flags);
579
580 wait_for_completion(&comp.com);
581 err = comp.err;
582 } else {
583 port->vio.cmp = NULL;
584 spin_unlock_irqrestore(&port->vio.lock, flags);
585 }
586
587 if (map_perm & LDC_MAP_W)
588 memcpy(buf, req_buf, len);
589
590 kfree(req_buf);
591
592 return err;
593}
594
595static int __devinit vdc_alloc_tx_ring(struct vdc_port *port)
596{
597 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
598 unsigned long len, entry_size;
599 int ncookies;
600 void *dring;
601
602 entry_size = sizeof(struct vio_disk_desc) +
603 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
604 len = (VDC_TX_RING_SIZE * entry_size);
605
606 ncookies = VIO_MAX_RING_COOKIES;
607 dring = ldc_alloc_exp_dring(port->vio.lp, len,
608 dr->cookies, &ncookies,
609 (LDC_MAP_SHADOW |
610 LDC_MAP_DIRECT |
611 LDC_MAP_RW));
612 if (IS_ERR(dring))
613 return PTR_ERR(dring);
614
615 dr->base = dring;
616 dr->entry_size = entry_size;
617 dr->num_entries = VDC_TX_RING_SIZE;
618 dr->prod = dr->cons = 0;
619 dr->pending = VDC_TX_RING_SIZE;
620 dr->ncookies = ncookies;
621
622 return 0;
623}
624
625static void vdc_free_tx_ring(struct vdc_port *port)
626{
627 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
628
629 if (dr->base) {
630 ldc_free_exp_dring(port->vio.lp, dr->base,
631 (dr->entry_size * dr->num_entries),
632 dr->cookies, dr->ncookies);
633 dr->base = NULL;
634 dr->entry_size = 0;
635 dr->num_entries = 0;
636 dr->pending = 0;
637 dr->ncookies = 0;
638 }
639}
640
641static int probe_disk(struct vdc_port *port)
642{
643 struct vio_completion comp;
644 struct request_queue *q;
645 struct gendisk *g;
646 int err;
647
648 init_completion(&comp.com);
649 comp.err = 0;
650 comp.waiting_for = WAITING_FOR_LINK_UP;
651 port->vio.cmp = ∁
652
653 vio_port_up(&port->vio);
654
655 wait_for_completion(&comp.com);
656 if (comp.err)
657 return comp.err;
658
659 err = generic_request(port, VD_OP_GET_VTOC,
660 &port->label, sizeof(port->label));
661 if (err < 0) {
662 printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
663 return err;
664 }
665
666 err = generic_request(port, VD_OP_GET_DISKGEOM,
667 &port->geom, sizeof(port->geom));
668 if (err < 0) {
669 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
670 "error %d\n", err);
671 return err;
672 }
673
674 port->vdisk_size = ((u64)port->geom.num_cyl *
675 (u64)port->geom.num_hd *
676 (u64)port->geom.num_sec);
677
678 q = blk_init_queue(do_vdc_request, &port->vio.lock);
679 if (!q) {
680 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
681 port->vio.name);
682 return -ENOMEM;
683 }
684 g = alloc_disk(1 << PARTITION_SHIFT);
685 if (!g) {
686 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
687 port->vio.name);
688 blk_cleanup_queue(q);
689 return -ENOMEM;
690 }
691
692 port->disk = g;
693
694 blk_queue_max_segments(q, port->ring_cookies);
695 blk_queue_max_hw_sectors(q, port->max_xfer_size);
696 g->major = vdc_major;
697 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
698 strcpy(g->disk_name, port->disk_name);
699
700 g->fops = &vdc_fops;
701 g->queue = q;
702 g->private_data = port;
703 g->driverfs_dev = &port->vio.vdev->dev;
704
705 set_capacity(g, port->vdisk_size);
706
707 printk(KERN_INFO PFX "%s: %u sectors (%u MB)\n",
708 g->disk_name,
709 port->vdisk_size, (port->vdisk_size >> (20 - 9)));
710
711 add_disk(g);
712
713 return 0;
714}
715
716static struct ldc_channel_config vdc_ldc_cfg = {
717 .event = vdc_event,
718 .mtu = 64,
719 .mode = LDC_MODE_UNRELIABLE,
720};
721
722static struct vio_driver_ops vdc_vio_ops = {
723 .send_attr = vdc_send_attr,
724 .handle_attr = vdc_handle_attr,
725 .handshake_complete = vdc_handshake_complete,
726};
727
728static void __devinit print_version(void)
729{
730 static int version_printed;
731
732 if (version_printed++ == 0)
733 printk(KERN_INFO "%s", version);
734}
735
736static int __devinit vdc_port_probe(struct vio_dev *vdev,
737 const struct vio_device_id *id)
738{
739 struct mdesc_handle *hp;
740 struct vdc_port *port;
741 int err;
742
743 print_version();
744
745 hp = mdesc_grab();
746
747 err = -ENODEV;
748 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
749 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
750 vdev->dev_no);
751 goto err_out_release_mdesc;
752 }
753
754 port = kzalloc(sizeof(*port), GFP_KERNEL);
755 err = -ENOMEM;
756 if (!port) {
757 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
758 goto err_out_release_mdesc;
759 }
760
761 if (vdev->dev_no >= 26)
762 snprintf(port->disk_name, sizeof(port->disk_name),
763 VDCBLK_NAME "%c%c",
764 'a' + ((int)vdev->dev_no / 26) - 1,
765 'a' + ((int)vdev->dev_no % 26));
766 else
767 snprintf(port->disk_name, sizeof(port->disk_name),
768 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
769
770 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
771 vdc_versions, ARRAY_SIZE(vdc_versions),
772 &vdc_vio_ops, port->disk_name);
773 if (err)
774 goto err_out_free_port;
775
776 port->vdisk_block_size = 512;
777 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
778 port->ring_cookies = ((port->max_xfer_size *
779 port->vdisk_block_size) / PAGE_SIZE) + 2;
780
781 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
782 if (err)
783 goto err_out_free_port;
784
785 err = vdc_alloc_tx_ring(port);
786 if (err)
787 goto err_out_free_ldc;
788
789 err = probe_disk(port);
790 if (err)
791 goto err_out_free_tx_ring;
792
793 dev_set_drvdata(&vdev->dev, port);
794
795 mdesc_release(hp);
796
797 return 0;
798
799err_out_free_tx_ring:
800 vdc_free_tx_ring(port);
801
802err_out_free_ldc:
803 vio_ldc_free(&port->vio);
804
805err_out_free_port:
806 kfree(port);
807
808err_out_release_mdesc:
809 mdesc_release(hp);
810 return err;
811}
812
813static int vdc_port_remove(struct vio_dev *vdev)
814{
815 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
816
817 if (port) {
818 del_timer_sync(&port->vio.timer);
819
820 vdc_free_tx_ring(port);
821 vio_ldc_free(&port->vio);
822
823 dev_set_drvdata(&vdev->dev, NULL);
824
825 kfree(port);
826 }
827 return 0;
828}
829
830static const struct vio_device_id vdc_port_match[] = {
831 {
832 .type = "vdc-port",
833 },
834 {},
835};
836MODULE_DEVICE_TABLE(vio, vdc_port_match);
837
838static struct vio_driver vdc_port_driver = {
839 .id_table = vdc_port_match,
840 .probe = vdc_port_probe,
841 .remove = vdc_port_remove,
842 .driver = {
843 .name = "vdc_port",
844 .owner = THIS_MODULE,
845 }
846};
847
848static int __init vdc_init(void)
849{
850 int err;
851
852 err = register_blkdev(0, VDCBLK_NAME);
853 if (err < 0)
854 goto out_err;
855
856 vdc_major = err;
857
858 err = vio_register_driver(&vdc_port_driver);
859 if (err)
860 goto out_unregister_blkdev;
861
862 return 0;
863
864out_unregister_blkdev:
865 unregister_blkdev(vdc_major, VDCBLK_NAME);
866 vdc_major = 0;
867
868out_err:
869 return err;
870}
871
872static void __exit vdc_exit(void)
873{
874 vio_unregister_driver(&vdc_port_driver);
875 unregister_blkdev(vdc_major, VDCBLK_NAME);
876}
877
878module_init(vdc_init);
879module_exit(vdc_exit);
1/* sunvdc.c: Sun LDOM Virtual Disk Client.
2 *
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/blkdev.h>
10#include <linux/hdreg.h>
11#include <linux/genhd.h>
12#include <linux/cdrom.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/completion.h>
16#include <linux/delay.h>
17#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/scatterlist.h>
20
21#include <asm/vio.h>
22#include <asm/ldc.h>
23
24#define DRV_MODULE_NAME "sunvdc"
25#define PFX DRV_MODULE_NAME ": "
26#define DRV_MODULE_VERSION "1.2"
27#define DRV_MODULE_RELDATE "November 24, 2014"
28
29static char version[] =
30 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33MODULE_LICENSE("GPL");
34MODULE_VERSION(DRV_MODULE_VERSION);
35
36#define VDC_TX_RING_SIZE 512
37#define VDC_DEFAULT_BLK_SIZE 512
38
39#define WAITING_FOR_LINK_UP 0x01
40#define WAITING_FOR_TX_SPACE 0x02
41#define WAITING_FOR_GEN_CMD 0x04
42#define WAITING_FOR_ANY -1
43
44static struct workqueue_struct *sunvdc_wq;
45
46struct vdc_req_entry {
47 struct request *req;
48};
49
50struct vdc_port {
51 struct vio_driver_state vio;
52
53 struct gendisk *disk;
54
55 struct vdc_completion *cmp;
56
57 u64 req_id;
58 u64 seq;
59 struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE];
60
61 unsigned long ring_cookies;
62
63 u64 max_xfer_size;
64 u32 vdisk_block_size;
65
66 u64 ldc_timeout;
67 struct timer_list ldc_reset_timer;
68 struct work_struct ldc_reset_work;
69
70 /* The server fills these in for us in the disk attribute
71 * ACK packet.
72 */
73 u64 operations;
74 u32 vdisk_size;
75 u8 vdisk_type;
76 u8 vdisk_mtype;
77 u32 vdisk_phys_blksz;
78
79 char disk_name[32];
80};
81
82static void vdc_ldc_reset(struct vdc_port *port);
83static void vdc_ldc_reset_work(struct work_struct *work);
84static void vdc_ldc_reset_timer(struct timer_list *t);
85
86static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
87{
88 return container_of(vio, struct vdc_port, vio);
89}
90
91/* Ordered from largest major to lowest */
92static struct vio_version vdc_versions[] = {
93 { .major = 1, .minor = 2 },
94 { .major = 1, .minor = 1 },
95 { .major = 1, .minor = 0 },
96};
97
98static inline int vdc_version_supported(struct vdc_port *port,
99 u16 major, u16 minor)
100{
101 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
102}
103
104#define VDCBLK_NAME "vdisk"
105static int vdc_major;
106#define PARTITION_SHIFT 3
107
108static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
109{
110 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
111}
112
113static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
114{
115 struct gendisk *disk = bdev->bd_disk;
116 sector_t nsect = get_capacity(disk);
117 sector_t cylinders = nsect;
118
119 geo->heads = 0xff;
120 geo->sectors = 0x3f;
121 sector_div(cylinders, geo->heads * geo->sectors);
122 geo->cylinders = cylinders;
123 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
124 geo->cylinders = 0xffff;
125
126 return 0;
127}
128
129/* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
130 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
131 * Needed to be able to install inside an ldom from an iso image.
132 */
133static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
134 unsigned command, unsigned long argument)
135{
136 int i;
137 struct gendisk *disk;
138
139 switch (command) {
140 case CDROMMULTISESSION:
141 pr_debug(PFX "Multisession CDs not supported\n");
142 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
143 if (put_user(0, (char __user *)(argument + i)))
144 return -EFAULT;
145 return 0;
146
147 case CDROM_GET_CAPABILITY:
148 disk = bdev->bd_disk;
149
150 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
151 return 0;
152 return -EINVAL;
153
154 default:
155 pr_debug(PFX "ioctl %08x not supported\n", command);
156 return -EINVAL;
157 }
158}
159
160static const struct block_device_operations vdc_fops = {
161 .owner = THIS_MODULE,
162 .getgeo = vdc_getgeo,
163 .ioctl = vdc_ioctl,
164};
165
166static void vdc_blk_queue_start(struct vdc_port *port)
167{
168 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
169
170 /* restart blk queue when ring is half emptied. also called after
171 * handshake completes, so check for initial handshake before we've
172 * allocated a disk.
173 */
174 if (port->disk && blk_queue_stopped(port->disk->queue) &&
175 vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
176 blk_start_queue(port->disk->queue);
177 }
178
179}
180
181static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
182{
183 if (vio->cmp &&
184 (waiting_for == -1 ||
185 vio->cmp->waiting_for == waiting_for)) {
186 vio->cmp->err = err;
187 complete(&vio->cmp->com);
188 vio->cmp = NULL;
189 }
190}
191
192static void vdc_handshake_complete(struct vio_driver_state *vio)
193{
194 struct vdc_port *port = to_vdc_port(vio);
195
196 del_timer(&port->ldc_reset_timer);
197 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
198 vdc_blk_queue_start(port);
199}
200
201static int vdc_handle_unknown(struct vdc_port *port, void *arg)
202{
203 struct vio_msg_tag *pkt = arg;
204
205 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
206 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
207 printk(KERN_ERR PFX "Resetting connection.\n");
208
209 ldc_disconnect(port->vio.lp);
210
211 return -ECONNRESET;
212}
213
214static int vdc_send_attr(struct vio_driver_state *vio)
215{
216 struct vdc_port *port = to_vdc_port(vio);
217 struct vio_disk_attr_info pkt;
218
219 memset(&pkt, 0, sizeof(pkt));
220
221 pkt.tag.type = VIO_TYPE_CTRL;
222 pkt.tag.stype = VIO_SUBTYPE_INFO;
223 pkt.tag.stype_env = VIO_ATTR_INFO;
224 pkt.tag.sid = vio_send_sid(vio);
225
226 pkt.xfer_mode = VIO_DRING_MODE;
227 pkt.vdisk_block_size = port->vdisk_block_size;
228 pkt.max_xfer_size = port->max_xfer_size;
229
230 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
231 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
232
233 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
234}
235
236static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
237{
238 struct vdc_port *port = to_vdc_port(vio);
239 struct vio_disk_attr_info *pkt = arg;
240
241 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
242 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
243 pkt->tag.stype, pkt->operations,
244 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
245 pkt->xfer_mode, pkt->vdisk_block_size,
246 pkt->max_xfer_size);
247
248 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
249 switch (pkt->vdisk_type) {
250 case VD_DISK_TYPE_DISK:
251 case VD_DISK_TYPE_SLICE:
252 break;
253
254 default:
255 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
256 vio->name, pkt->vdisk_type);
257 return -ECONNRESET;
258 }
259
260 if (pkt->vdisk_block_size > port->vdisk_block_size) {
261 printk(KERN_ERR PFX "%s: BLOCK size increased "
262 "%u --> %u\n",
263 vio->name,
264 port->vdisk_block_size, pkt->vdisk_block_size);
265 return -ECONNRESET;
266 }
267
268 port->operations = pkt->operations;
269 port->vdisk_type = pkt->vdisk_type;
270 if (vdc_version_supported(port, 1, 1)) {
271 port->vdisk_size = pkt->vdisk_size;
272 port->vdisk_mtype = pkt->vdisk_mtype;
273 }
274 if (pkt->max_xfer_size < port->max_xfer_size)
275 port->max_xfer_size = pkt->max_xfer_size;
276 port->vdisk_block_size = pkt->vdisk_block_size;
277
278 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
279 if (vdc_version_supported(port, 1, 2))
280 port->vdisk_phys_blksz = pkt->phys_block_size;
281
282 return 0;
283 } else {
284 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
285
286 return -ECONNRESET;
287 }
288}
289
290static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
291{
292 int err = desc->status;
293
294 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
295}
296
297static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
298 unsigned int index)
299{
300 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
301 struct vdc_req_entry *rqe = &port->rq_arr[index];
302 struct request *req;
303
304 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
305 return;
306
307 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
308 desc->hdr.state = VIO_DESC_FREE;
309 dr->cons = vio_dring_next(dr, index);
310
311 req = rqe->req;
312 if (req == NULL) {
313 vdc_end_special(port, desc);
314 return;
315 }
316
317 rqe->req = NULL;
318
319 __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
320
321 vdc_blk_queue_start(port);
322}
323
324static int vdc_ack(struct vdc_port *port, void *msgbuf)
325{
326 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
327 struct vio_dring_data *pkt = msgbuf;
328
329 if (unlikely(pkt->dring_ident != dr->ident ||
330 pkt->start_idx != pkt->end_idx ||
331 pkt->start_idx >= VDC_TX_RING_SIZE))
332 return 0;
333
334 vdc_end_one(port, dr, pkt->start_idx);
335
336 return 0;
337}
338
339static int vdc_nack(struct vdc_port *port, void *msgbuf)
340{
341 /* XXX Implement me XXX */
342 return 0;
343}
344
345static void vdc_event(void *arg, int event)
346{
347 struct vdc_port *port = arg;
348 struct vio_driver_state *vio = &port->vio;
349 unsigned long flags;
350 int err;
351
352 spin_lock_irqsave(&vio->lock, flags);
353
354 if (unlikely(event == LDC_EVENT_RESET)) {
355 vio_link_state_change(vio, event);
356 queue_work(sunvdc_wq, &port->ldc_reset_work);
357 goto out;
358 }
359
360 if (unlikely(event == LDC_EVENT_UP)) {
361 vio_link_state_change(vio, event);
362 goto out;
363 }
364
365 if (unlikely(event != LDC_EVENT_DATA_READY)) {
366 pr_warn(PFX "Unexpected LDC event %d\n", event);
367 goto out;
368 }
369
370 err = 0;
371 while (1) {
372 union {
373 struct vio_msg_tag tag;
374 u64 raw[8];
375 } msgbuf;
376
377 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
378 if (unlikely(err < 0)) {
379 if (err == -ECONNRESET)
380 vio_conn_reset(vio);
381 break;
382 }
383 if (err == 0)
384 break;
385 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
386 msgbuf.tag.type,
387 msgbuf.tag.stype,
388 msgbuf.tag.stype_env,
389 msgbuf.tag.sid);
390 err = vio_validate_sid(vio, &msgbuf.tag);
391 if (err < 0)
392 break;
393
394 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
395 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
396 err = vdc_ack(port, &msgbuf);
397 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
398 err = vdc_nack(port, &msgbuf);
399 else
400 err = vdc_handle_unknown(port, &msgbuf);
401 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
402 err = vio_control_pkt_engine(vio, &msgbuf);
403 } else {
404 err = vdc_handle_unknown(port, &msgbuf);
405 }
406 if (err < 0)
407 break;
408 }
409 if (err < 0)
410 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
411out:
412 spin_unlock_irqrestore(&vio->lock, flags);
413}
414
415static int __vdc_tx_trigger(struct vdc_port *port)
416{
417 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
418 struct vio_dring_data hdr = {
419 .tag = {
420 .type = VIO_TYPE_DATA,
421 .stype = VIO_SUBTYPE_INFO,
422 .stype_env = VIO_DRING_DATA,
423 .sid = vio_send_sid(&port->vio),
424 },
425 .dring_ident = dr->ident,
426 .start_idx = dr->prod,
427 .end_idx = dr->prod,
428 };
429 int err, delay;
430
431 hdr.seq = dr->snd_nxt;
432 delay = 1;
433 do {
434 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
435 if (err > 0) {
436 dr->snd_nxt++;
437 break;
438 }
439 udelay(delay);
440 if ((delay <<= 1) > 128)
441 delay = 128;
442 } while (err == -EAGAIN);
443
444 if (err == -ENOTCONN)
445 vdc_ldc_reset(port);
446 return err;
447}
448
449static int __send_request(struct request *req)
450{
451 struct vdc_port *port = req->rq_disk->private_data;
452 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
453 struct scatterlist sg[port->ring_cookies];
454 struct vdc_req_entry *rqe;
455 struct vio_disk_desc *desc;
456 unsigned int map_perm;
457 int nsg, err, i;
458 u64 len;
459 u8 op;
460
461 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
462
463 if (rq_data_dir(req) == READ) {
464 map_perm |= LDC_MAP_W;
465 op = VD_OP_BREAD;
466 } else {
467 map_perm |= LDC_MAP_R;
468 op = VD_OP_BWRITE;
469 }
470
471 sg_init_table(sg, port->ring_cookies);
472 nsg = blk_rq_map_sg(req->q, req, sg);
473
474 len = 0;
475 for (i = 0; i < nsg; i++)
476 len += sg[i].length;
477
478 desc = vio_dring_cur(dr);
479
480 err = ldc_map_sg(port->vio.lp, sg, nsg,
481 desc->cookies, port->ring_cookies,
482 map_perm);
483 if (err < 0) {
484 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
485 return err;
486 }
487
488 rqe = &port->rq_arr[dr->prod];
489 rqe->req = req;
490
491 desc->hdr.ack = VIO_ACK_ENABLE;
492 desc->req_id = port->req_id;
493 desc->operation = op;
494 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
495 desc->slice = 0xff;
496 } else {
497 desc->slice = 0;
498 }
499 desc->status = ~0;
500 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
501 desc->size = len;
502 desc->ncookies = err;
503
504 /* This has to be a non-SMP write barrier because we are writing
505 * to memory which is shared with the peer LDOM.
506 */
507 wmb();
508 desc->hdr.state = VIO_DESC_READY;
509
510 err = __vdc_tx_trigger(port);
511 if (err < 0) {
512 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
513 } else {
514 port->req_id++;
515 dr->prod = vio_dring_next(dr, dr->prod);
516 }
517
518 return err;
519}
520
521static void do_vdc_request(struct request_queue *rq)
522{
523 struct request *req;
524
525 while ((req = blk_peek_request(rq)) != NULL) {
526 struct vdc_port *port;
527 struct vio_dring_state *dr;
528
529 port = req->rq_disk->private_data;
530 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
531 if (unlikely(vdc_tx_dring_avail(dr) < 1))
532 goto wait;
533
534 blk_start_request(req);
535
536 if (__send_request(req) < 0) {
537 blk_requeue_request(rq, req);
538wait:
539 /* Avoid pointless unplugs. */
540 blk_stop_queue(rq);
541 break;
542 }
543 }
544}
545
546static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
547{
548 struct vio_dring_state *dr;
549 struct vio_completion comp;
550 struct vio_disk_desc *desc;
551 unsigned int map_perm;
552 unsigned long flags;
553 int op_len, err;
554 void *req_buf;
555
556 if (!(((u64)1 << (u64)op) & port->operations))
557 return -EOPNOTSUPP;
558
559 switch (op) {
560 case VD_OP_BREAD:
561 case VD_OP_BWRITE:
562 default:
563 return -EINVAL;
564
565 case VD_OP_FLUSH:
566 op_len = 0;
567 map_perm = 0;
568 break;
569
570 case VD_OP_GET_WCE:
571 op_len = sizeof(u32);
572 map_perm = LDC_MAP_W;
573 break;
574
575 case VD_OP_SET_WCE:
576 op_len = sizeof(u32);
577 map_perm = LDC_MAP_R;
578 break;
579
580 case VD_OP_GET_VTOC:
581 op_len = sizeof(struct vio_disk_vtoc);
582 map_perm = LDC_MAP_W;
583 break;
584
585 case VD_OP_SET_VTOC:
586 op_len = sizeof(struct vio_disk_vtoc);
587 map_perm = LDC_MAP_R;
588 break;
589
590 case VD_OP_GET_DISKGEOM:
591 op_len = sizeof(struct vio_disk_geom);
592 map_perm = LDC_MAP_W;
593 break;
594
595 case VD_OP_SET_DISKGEOM:
596 op_len = sizeof(struct vio_disk_geom);
597 map_perm = LDC_MAP_R;
598 break;
599
600 case VD_OP_SCSICMD:
601 op_len = 16;
602 map_perm = LDC_MAP_RW;
603 break;
604
605 case VD_OP_GET_DEVID:
606 op_len = sizeof(struct vio_disk_devid);
607 map_perm = LDC_MAP_W;
608 break;
609
610 case VD_OP_GET_EFI:
611 case VD_OP_SET_EFI:
612 return -EOPNOTSUPP;
613 break;
614 };
615
616 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
617
618 op_len = (op_len + 7) & ~7;
619 req_buf = kzalloc(op_len, GFP_KERNEL);
620 if (!req_buf)
621 return -ENOMEM;
622
623 if (len > op_len)
624 len = op_len;
625
626 if (map_perm & LDC_MAP_R)
627 memcpy(req_buf, buf, len);
628
629 spin_lock_irqsave(&port->vio.lock, flags);
630
631 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
632
633 /* XXX If we want to use this code generically we have to
634 * XXX handle TX ring exhaustion etc.
635 */
636 desc = vio_dring_cur(dr);
637
638 err = ldc_map_single(port->vio.lp, req_buf, op_len,
639 desc->cookies, port->ring_cookies,
640 map_perm);
641 if (err < 0) {
642 spin_unlock_irqrestore(&port->vio.lock, flags);
643 kfree(req_buf);
644 return err;
645 }
646
647 init_completion(&comp.com);
648 comp.waiting_for = WAITING_FOR_GEN_CMD;
649 port->vio.cmp = ∁
650
651 desc->hdr.ack = VIO_ACK_ENABLE;
652 desc->req_id = port->req_id;
653 desc->operation = op;
654 desc->slice = 0;
655 desc->status = ~0;
656 desc->offset = 0;
657 desc->size = op_len;
658 desc->ncookies = err;
659
660 /* This has to be a non-SMP write barrier because we are writing
661 * to memory which is shared with the peer LDOM.
662 */
663 wmb();
664 desc->hdr.state = VIO_DESC_READY;
665
666 err = __vdc_tx_trigger(port);
667 if (err >= 0) {
668 port->req_id++;
669 dr->prod = vio_dring_next(dr, dr->prod);
670 spin_unlock_irqrestore(&port->vio.lock, flags);
671
672 wait_for_completion(&comp.com);
673 err = comp.err;
674 } else {
675 port->vio.cmp = NULL;
676 spin_unlock_irqrestore(&port->vio.lock, flags);
677 }
678
679 if (map_perm & LDC_MAP_W)
680 memcpy(buf, req_buf, len);
681
682 kfree(req_buf);
683
684 return err;
685}
686
687static int vdc_alloc_tx_ring(struct vdc_port *port)
688{
689 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
690 unsigned long len, entry_size;
691 int ncookies;
692 void *dring;
693
694 entry_size = sizeof(struct vio_disk_desc) +
695 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
696 len = (VDC_TX_RING_SIZE * entry_size);
697
698 ncookies = VIO_MAX_RING_COOKIES;
699 dring = ldc_alloc_exp_dring(port->vio.lp, len,
700 dr->cookies, &ncookies,
701 (LDC_MAP_SHADOW |
702 LDC_MAP_DIRECT |
703 LDC_MAP_RW));
704 if (IS_ERR(dring))
705 return PTR_ERR(dring);
706
707 dr->base = dring;
708 dr->entry_size = entry_size;
709 dr->num_entries = VDC_TX_RING_SIZE;
710 dr->prod = dr->cons = 0;
711 dr->pending = VDC_TX_RING_SIZE;
712 dr->ncookies = ncookies;
713
714 return 0;
715}
716
717static void vdc_free_tx_ring(struct vdc_port *port)
718{
719 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
720
721 if (dr->base) {
722 ldc_free_exp_dring(port->vio.lp, dr->base,
723 (dr->entry_size * dr->num_entries),
724 dr->cookies, dr->ncookies);
725 dr->base = NULL;
726 dr->entry_size = 0;
727 dr->num_entries = 0;
728 dr->pending = 0;
729 dr->ncookies = 0;
730 }
731}
732
733static int vdc_port_up(struct vdc_port *port)
734{
735 struct vio_completion comp;
736
737 init_completion(&comp.com);
738 comp.err = 0;
739 comp.waiting_for = WAITING_FOR_LINK_UP;
740 port->vio.cmp = ∁
741
742 vio_port_up(&port->vio);
743 wait_for_completion(&comp.com);
744 return comp.err;
745}
746
747static void vdc_port_down(struct vdc_port *port)
748{
749 ldc_disconnect(port->vio.lp);
750 ldc_unbind(port->vio.lp);
751 vdc_free_tx_ring(port);
752 vio_ldc_free(&port->vio);
753}
754
755static int probe_disk(struct vdc_port *port)
756{
757 struct request_queue *q;
758 struct gendisk *g;
759 int err;
760
761 err = vdc_port_up(port);
762 if (err)
763 return err;
764
765 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
766 * disk is reserved by another system.
767 */
768 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
769 return -ENODEV;
770
771 if (vdc_version_supported(port, 1, 1)) {
772 /* vdisk_size should be set during the handshake, if it wasn't
773 * then the underlying disk is reserved by another system
774 */
775 if (port->vdisk_size == -1)
776 return -ENODEV;
777 } else {
778 struct vio_disk_geom geom;
779
780 err = generic_request(port, VD_OP_GET_DISKGEOM,
781 &geom, sizeof(geom));
782 if (err < 0) {
783 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
784 "error %d\n", err);
785 return err;
786 }
787 port->vdisk_size = ((u64)geom.num_cyl *
788 (u64)geom.num_hd *
789 (u64)geom.num_sec);
790 }
791
792 q = blk_init_queue(do_vdc_request, &port->vio.lock);
793 if (!q) {
794 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
795 port->vio.name);
796 return -ENOMEM;
797 }
798 g = alloc_disk(1 << PARTITION_SHIFT);
799 if (!g) {
800 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
801 port->vio.name);
802 blk_cleanup_queue(q);
803 return -ENOMEM;
804 }
805
806 port->disk = g;
807
808 /* Each segment in a request is up to an aligned page in size. */
809 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
810 blk_queue_max_segment_size(q, PAGE_SIZE);
811
812 blk_queue_max_segments(q, port->ring_cookies);
813 blk_queue_max_hw_sectors(q, port->max_xfer_size);
814 g->major = vdc_major;
815 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
816 strcpy(g->disk_name, port->disk_name);
817
818 g->fops = &vdc_fops;
819 g->queue = q;
820 g->private_data = port;
821
822 set_capacity(g, port->vdisk_size);
823
824 if (vdc_version_supported(port, 1, 1)) {
825 switch (port->vdisk_mtype) {
826 case VD_MEDIA_TYPE_CD:
827 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
828 g->flags |= GENHD_FL_CD;
829 g->flags |= GENHD_FL_REMOVABLE;
830 set_disk_ro(g, 1);
831 break;
832
833 case VD_MEDIA_TYPE_DVD:
834 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
835 g->flags |= GENHD_FL_CD;
836 g->flags |= GENHD_FL_REMOVABLE;
837 set_disk_ro(g, 1);
838 break;
839
840 case VD_MEDIA_TYPE_FIXED:
841 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
842 break;
843 }
844 }
845
846 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
847
848 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
849 g->disk_name,
850 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
851 port->vio.ver.major, port->vio.ver.minor);
852
853 device_add_disk(&port->vio.vdev->dev, g);
854
855 return 0;
856}
857
858static struct ldc_channel_config vdc_ldc_cfg = {
859 .event = vdc_event,
860 .mtu = 64,
861 .mode = LDC_MODE_UNRELIABLE,
862};
863
864static struct vio_driver_ops vdc_vio_ops = {
865 .send_attr = vdc_send_attr,
866 .handle_attr = vdc_handle_attr,
867 .handshake_complete = vdc_handshake_complete,
868};
869
870static void print_version(void)
871{
872 static int version_printed;
873
874 if (version_printed++ == 0)
875 printk(KERN_INFO "%s", version);
876}
877
878struct vdc_check_port_data {
879 int dev_no;
880 char *type;
881};
882
883static int vdc_device_probed(struct device *dev, void *arg)
884{
885 struct vio_dev *vdev = to_vio_dev(dev);
886 struct vdc_check_port_data *port_data;
887
888 port_data = (struct vdc_check_port_data *)arg;
889
890 if ((vdev->dev_no == port_data->dev_no) &&
891 (!(strcmp((char *)&vdev->type, port_data->type))) &&
892 dev_get_drvdata(dev)) {
893 /* This device has already been configured
894 * by vdc_port_probe()
895 */
896 return 1;
897 } else {
898 return 0;
899 }
900}
901
902/* Determine whether the VIO device is part of an mpgroup
903 * by locating all the virtual-device-port nodes associated
904 * with the parent virtual-device node for the VIO device
905 * and checking whether any of these nodes are vdc-ports
906 * which have already been configured.
907 *
908 * Returns true if this device is part of an mpgroup and has
909 * already been probed.
910 */
911static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
912{
913 struct vdc_check_port_data port_data;
914 struct device *dev;
915
916 port_data.dev_no = vdev->dev_no;
917 port_data.type = (char *)&vdev->type;
918
919 dev = device_find_child(vdev->dev.parent, &port_data,
920 vdc_device_probed);
921
922 if (dev)
923 return true;
924
925 return false;
926}
927
928static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
929{
930 struct mdesc_handle *hp;
931 struct vdc_port *port;
932 int err;
933 const u64 *ldc_timeout;
934
935 print_version();
936
937 hp = mdesc_grab();
938
939 err = -ENODEV;
940 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
941 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
942 vdev->dev_no);
943 goto err_out_release_mdesc;
944 }
945
946 /* Check if this device is part of an mpgroup */
947 if (vdc_port_mpgroup_check(vdev)) {
948 printk(KERN_WARNING
949 "VIO: Ignoring extra vdisk port %s",
950 dev_name(&vdev->dev));
951 goto err_out_release_mdesc;
952 }
953
954 port = kzalloc(sizeof(*port), GFP_KERNEL);
955 err = -ENOMEM;
956 if (!port) {
957 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
958 goto err_out_release_mdesc;
959 }
960
961 if (vdev->dev_no >= 26)
962 snprintf(port->disk_name, sizeof(port->disk_name),
963 VDCBLK_NAME "%c%c",
964 'a' + ((int)vdev->dev_no / 26) - 1,
965 'a' + ((int)vdev->dev_no % 26));
966 else
967 snprintf(port->disk_name, sizeof(port->disk_name),
968 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
969 port->vdisk_size = -1;
970
971 /* Actual wall time may be double due to do_generic_file_read() doing
972 * a readahead I/O first, and once that fails it will try to read a
973 * single page.
974 */
975 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
976 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
977 timer_setup(&port->ldc_reset_timer, vdc_ldc_reset_timer, 0);
978 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
979
980 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
981 vdc_versions, ARRAY_SIZE(vdc_versions),
982 &vdc_vio_ops, port->disk_name);
983 if (err)
984 goto err_out_free_port;
985
986 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
987 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
988 port->ring_cookies = ((port->max_xfer_size *
989 port->vdisk_block_size) / PAGE_SIZE) + 2;
990
991 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
992 if (err)
993 goto err_out_free_port;
994
995 err = vdc_alloc_tx_ring(port);
996 if (err)
997 goto err_out_free_ldc;
998
999 err = probe_disk(port);
1000 if (err)
1001 goto err_out_free_tx_ring;
1002
1003 /* Note that the device driver_data is used to determine
1004 * whether the port has been probed.
1005 */
1006 dev_set_drvdata(&vdev->dev, port);
1007
1008 mdesc_release(hp);
1009
1010 return 0;
1011
1012err_out_free_tx_ring:
1013 vdc_free_tx_ring(port);
1014
1015err_out_free_ldc:
1016 vio_ldc_free(&port->vio);
1017
1018err_out_free_port:
1019 kfree(port);
1020
1021err_out_release_mdesc:
1022 mdesc_release(hp);
1023 return err;
1024}
1025
1026static int vdc_port_remove(struct vio_dev *vdev)
1027{
1028 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1029
1030 if (port) {
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&port->vio.lock, flags);
1034 blk_stop_queue(port->disk->queue);
1035 spin_unlock_irqrestore(&port->vio.lock, flags);
1036
1037 flush_work(&port->ldc_reset_work);
1038 del_timer_sync(&port->ldc_reset_timer);
1039 del_timer_sync(&port->vio.timer);
1040
1041 del_gendisk(port->disk);
1042 blk_cleanup_queue(port->disk->queue);
1043 put_disk(port->disk);
1044 port->disk = NULL;
1045
1046 vdc_free_tx_ring(port);
1047 vio_ldc_free(&port->vio);
1048
1049 dev_set_drvdata(&vdev->dev, NULL);
1050
1051 kfree(port);
1052 }
1053 return 0;
1054}
1055
1056static void vdc_requeue_inflight(struct vdc_port *port)
1057{
1058 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1059 u32 idx;
1060
1061 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1062 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1063 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1064 struct request *req;
1065
1066 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1067 desc->hdr.state = VIO_DESC_FREE;
1068 dr->cons = vio_dring_next(dr, idx);
1069
1070 req = rqe->req;
1071 if (req == NULL) {
1072 vdc_end_special(port, desc);
1073 continue;
1074 }
1075
1076 rqe->req = NULL;
1077 blk_requeue_request(port->disk->queue, req);
1078 }
1079}
1080
1081static void vdc_queue_drain(struct vdc_port *port)
1082{
1083 struct request *req;
1084
1085 while ((req = blk_fetch_request(port->disk->queue)) != NULL)
1086 __blk_end_request_all(req, BLK_STS_IOERR);
1087}
1088
1089static void vdc_ldc_reset_timer(struct timer_list *t)
1090{
1091 struct vdc_port *port = from_timer(port, t, ldc_reset_timer);
1092 struct vio_driver_state *vio = &port->vio;
1093 unsigned long flags;
1094
1095 spin_lock_irqsave(&vio->lock, flags);
1096 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1097 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1098 port->disk_name, port->ldc_timeout);
1099 vdc_queue_drain(port);
1100 vdc_blk_queue_start(port);
1101 }
1102 spin_unlock_irqrestore(&vio->lock, flags);
1103}
1104
1105static void vdc_ldc_reset_work(struct work_struct *work)
1106{
1107 struct vdc_port *port;
1108 struct vio_driver_state *vio;
1109 unsigned long flags;
1110
1111 port = container_of(work, struct vdc_port, ldc_reset_work);
1112 vio = &port->vio;
1113
1114 spin_lock_irqsave(&vio->lock, flags);
1115 vdc_ldc_reset(port);
1116 spin_unlock_irqrestore(&vio->lock, flags);
1117}
1118
1119static void vdc_ldc_reset(struct vdc_port *port)
1120{
1121 int err;
1122
1123 assert_spin_locked(&port->vio.lock);
1124
1125 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1126 blk_stop_queue(port->disk->queue);
1127 vdc_requeue_inflight(port);
1128 vdc_port_down(port);
1129
1130 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1131 if (err) {
1132 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1133 return;
1134 }
1135
1136 err = vdc_alloc_tx_ring(port);
1137 if (err) {
1138 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1139 goto err_free_ldc;
1140 }
1141
1142 if (port->ldc_timeout)
1143 mod_timer(&port->ldc_reset_timer,
1144 round_jiffies(jiffies + HZ * port->ldc_timeout));
1145 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1146 return;
1147
1148err_free_ldc:
1149 vio_ldc_free(&port->vio);
1150}
1151
1152static const struct vio_device_id vdc_port_match[] = {
1153 {
1154 .type = "vdc-port",
1155 },
1156 {},
1157};
1158MODULE_DEVICE_TABLE(vio, vdc_port_match);
1159
1160static struct vio_driver vdc_port_driver = {
1161 .id_table = vdc_port_match,
1162 .probe = vdc_port_probe,
1163 .remove = vdc_port_remove,
1164 .name = "vdc_port",
1165};
1166
1167static int __init vdc_init(void)
1168{
1169 int err;
1170
1171 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1172 if (!sunvdc_wq)
1173 return -ENOMEM;
1174
1175 err = register_blkdev(0, VDCBLK_NAME);
1176 if (err < 0)
1177 goto out_free_wq;
1178
1179 vdc_major = err;
1180
1181 err = vio_register_driver(&vdc_port_driver);
1182 if (err)
1183 goto out_unregister_blkdev;
1184
1185 return 0;
1186
1187out_unregister_blkdev:
1188 unregister_blkdev(vdc_major, VDCBLK_NAME);
1189 vdc_major = 0;
1190
1191out_free_wq:
1192 destroy_workqueue(sunvdc_wq);
1193 return err;
1194}
1195
1196static void __exit vdc_exit(void)
1197{
1198 vio_unregister_driver(&vdc_port_driver);
1199 unregister_blkdev(vdc_major, VDCBLK_NAME);
1200 destroy_workqueue(sunvdc_wq);
1201}
1202
1203module_init(vdc_init);
1204module_exit(vdc_exit);