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