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