sunvdc: add cdrom and v1.1 protocol support
[firefly-linux-kernel-4.4.55.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.1"
27 #define DRV_MODULE_RELDATE      "February 13, 2013"
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        256
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 struct vdc_req_entry {
44         struct request          *req;
45 };
46
47 struct vdc_port {
48         struct vio_driver_state vio;
49
50         struct gendisk          *disk;
51
52         struct vdc_completion   *cmp;
53
54         u64                     req_id;
55         u64                     seq;
56         struct vdc_req_entry    rq_arr[VDC_TX_RING_SIZE];
57
58         unsigned long           ring_cookies;
59
60         u64                     max_xfer_size;
61         u32                     vdisk_block_size;
62
63         /* The server fills these in for us in the disk attribute
64          * ACK packet.
65          */
66         u64                     operations;
67         u32                     vdisk_size;
68         u8                      vdisk_type;
69         u8                      vdisk_mtype;
70
71         char                    disk_name[32];
72
73         struct vio_disk_geom    geom;
74         struct vio_disk_vtoc    label;
75 };
76
77 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
78 {
79         return container_of(vio, struct vdc_port, vio);
80 }
81
82 /* Ordered from largest major to lowest */
83 static struct vio_version vdc_versions[] = {
84         { .major = 1, .minor = 1 },
85         { .major = 1, .minor = 0 },
86 };
87
88 static inline int vdc_version_supported(struct vdc_port *port,
89                                         u16 major, u16 minor)
90 {
91         return port->vio.ver.major == major && port->vio.ver.minor >= minor;
92 }
93
94 #define VDCBLK_NAME     "vdisk"
95 static int vdc_major;
96 #define PARTITION_SHIFT 3
97
98 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
99 {
100         return vio_dring_avail(dr, VDC_TX_RING_SIZE);
101 }
102
103 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
104 {
105         struct gendisk *disk = bdev->bd_disk;
106         struct vdc_port *port = disk->private_data;
107
108         geo->heads = (u8) port->geom.num_hd;
109         geo->sectors = (u8) port->geom.num_sec;
110         geo->cylinders = port->geom.num_cyl;
111
112         return 0;
113 }
114
115 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
116  * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
117  * Needed to be able to install inside an ldom from an iso image.
118  */
119 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
120                      unsigned command, unsigned long argument)
121 {
122         int i;
123         struct gendisk *disk;
124
125         switch (command) {
126         case CDROMMULTISESSION:
127                 pr_debug(PFX "Multisession CDs not supported\n");
128                 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
129                         if (put_user(0, (char __user *)(argument + i)))
130                                 return -EFAULT;
131                 return 0;
132
133         case CDROM_GET_CAPABILITY:
134                 disk = bdev->bd_disk;
135
136                 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
137                         return 0;
138                 return -EINVAL;
139
140         default:
141                 pr_debug(PFX "ioctl %08x not supported\n", command);
142                 return -EINVAL;
143         }
144 }
145
146 static const struct block_device_operations vdc_fops = {
147         .owner          = THIS_MODULE,
148         .getgeo         = vdc_getgeo,
149         .ioctl          = vdc_ioctl,
150 };
151
152 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
153 {
154         if (vio->cmp &&
155             (waiting_for == -1 ||
156              vio->cmp->waiting_for == waiting_for)) {
157                 vio->cmp->err = err;
158                 complete(&vio->cmp->com);
159                 vio->cmp = NULL;
160         }
161 }
162
163 static void vdc_handshake_complete(struct vio_driver_state *vio)
164 {
165         vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
166 }
167
168 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
169 {
170         struct vio_msg_tag *pkt = arg;
171
172         printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
173                pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
174         printk(KERN_ERR PFX "Resetting connection.\n");
175
176         ldc_disconnect(port->vio.lp);
177
178         return -ECONNRESET;
179 }
180
181 static int vdc_send_attr(struct vio_driver_state *vio)
182 {
183         struct vdc_port *port = to_vdc_port(vio);
184         struct vio_disk_attr_info pkt;
185
186         memset(&pkt, 0, sizeof(pkt));
187
188         pkt.tag.type = VIO_TYPE_CTRL;
189         pkt.tag.stype = VIO_SUBTYPE_INFO;
190         pkt.tag.stype_env = VIO_ATTR_INFO;
191         pkt.tag.sid = vio_send_sid(vio);
192
193         pkt.xfer_mode = VIO_DRING_MODE;
194         pkt.vdisk_block_size = port->vdisk_block_size;
195         pkt.max_xfer_size = port->max_xfer_size;
196
197         viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
198                pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
199
200         return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
201 }
202
203 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
204 {
205         struct vdc_port *port = to_vdc_port(vio);
206         struct vio_disk_attr_info *pkt = arg;
207
208         viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
209                "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
210                pkt->tag.stype, pkt->operations,
211                pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
212                pkt->xfer_mode, pkt->vdisk_block_size,
213                pkt->max_xfer_size);
214
215         if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
216                 switch (pkt->vdisk_type) {
217                 case VD_DISK_TYPE_DISK:
218                 case VD_DISK_TYPE_SLICE:
219                         break;
220
221                 default:
222                         printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
223                                vio->name, pkt->vdisk_type);
224                         return -ECONNRESET;
225                 }
226
227                 if (pkt->vdisk_block_size > port->vdisk_block_size) {
228                         printk(KERN_ERR PFX "%s: BLOCK size increased "
229                                "%u --> %u\n",
230                                vio->name,
231                                port->vdisk_block_size, pkt->vdisk_block_size);
232                         return -ECONNRESET;
233                 }
234
235                 port->operations = pkt->operations;
236                 port->vdisk_type = pkt->vdisk_type;
237                 if (vdc_version_supported(port, 1, 1)) {
238                         port->vdisk_size = pkt->vdisk_size;
239                         port->vdisk_mtype = pkt->vdisk_mtype;
240                 }
241                 if (pkt->max_xfer_size < port->max_xfer_size)
242                         port->max_xfer_size = pkt->max_xfer_size;
243                 port->vdisk_block_size = pkt->vdisk_block_size;
244                 return 0;
245         } else {
246                 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
247
248                 return -ECONNRESET;
249         }
250 }
251
252 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
253 {
254         int err = desc->status;
255
256         vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
257 }
258
259 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
260                         unsigned int index)
261 {
262         struct vio_disk_desc *desc = vio_dring_entry(dr, index);
263         struct vdc_req_entry *rqe = &port->rq_arr[index];
264         struct request *req;
265
266         if (unlikely(desc->hdr.state != VIO_DESC_DONE))
267                 return;
268
269         ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
270         desc->hdr.state = VIO_DESC_FREE;
271         dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
272
273         req = rqe->req;
274         if (req == NULL) {
275                 vdc_end_special(port, desc);
276                 return;
277         }
278
279         rqe->req = NULL;
280
281         __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
282
283         if (blk_queue_stopped(port->disk->queue))
284                 blk_start_queue(port->disk->queue);
285 }
286
287 static int vdc_ack(struct vdc_port *port, void *msgbuf)
288 {
289         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
290         struct vio_dring_data *pkt = msgbuf;
291
292         if (unlikely(pkt->dring_ident != dr->ident ||
293                      pkt->start_idx != pkt->end_idx ||
294                      pkt->start_idx >= VDC_TX_RING_SIZE))
295                 return 0;
296
297         vdc_end_one(port, dr, pkt->start_idx);
298
299         return 0;
300 }
301
302 static int vdc_nack(struct vdc_port *port, void *msgbuf)
303 {
304         /* XXX Implement me XXX */
305         return 0;
306 }
307
308 static void vdc_event(void *arg, int event)
309 {
310         struct vdc_port *port = arg;
311         struct vio_driver_state *vio = &port->vio;
312         unsigned long flags;
313         int err;
314
315         spin_lock_irqsave(&vio->lock, flags);
316
317         if (unlikely(event == LDC_EVENT_RESET ||
318                      event == LDC_EVENT_UP)) {
319                 vio_link_state_change(vio, event);
320                 spin_unlock_irqrestore(&vio->lock, flags);
321                 return;
322         }
323
324         if (unlikely(event != LDC_EVENT_DATA_READY)) {
325                 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
326                 spin_unlock_irqrestore(&vio->lock, flags);
327                 return;
328         }
329
330         err = 0;
331         while (1) {
332                 union {
333                         struct vio_msg_tag tag;
334                         u64 raw[8];
335                 } msgbuf;
336
337                 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
338                 if (unlikely(err < 0)) {
339                         if (err == -ECONNRESET)
340                                 vio_conn_reset(vio);
341                         break;
342                 }
343                 if (err == 0)
344                         break;
345                 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
346                        msgbuf.tag.type,
347                        msgbuf.tag.stype,
348                        msgbuf.tag.stype_env,
349                        msgbuf.tag.sid);
350                 err = vio_validate_sid(vio, &msgbuf.tag);
351                 if (err < 0)
352                         break;
353
354                 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
355                         if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
356                                 err = vdc_ack(port, &msgbuf);
357                         else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
358                                 err = vdc_nack(port, &msgbuf);
359                         else
360                                 err = vdc_handle_unknown(port, &msgbuf);
361                 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
362                         err = vio_control_pkt_engine(vio, &msgbuf);
363                 } else {
364                         err = vdc_handle_unknown(port, &msgbuf);
365                 }
366                 if (err < 0)
367                         break;
368         }
369         if (err < 0)
370                 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
371         spin_unlock_irqrestore(&vio->lock, flags);
372 }
373
374 static int __vdc_tx_trigger(struct vdc_port *port)
375 {
376         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
377         struct vio_dring_data hdr = {
378                 .tag = {
379                         .type           = VIO_TYPE_DATA,
380                         .stype          = VIO_SUBTYPE_INFO,
381                         .stype_env      = VIO_DRING_DATA,
382                         .sid            = vio_send_sid(&port->vio),
383                 },
384                 .dring_ident            = dr->ident,
385                 .start_idx              = dr->prod,
386                 .end_idx                = dr->prod,
387         };
388         int err, delay;
389
390         hdr.seq = dr->snd_nxt;
391         delay = 1;
392         do {
393                 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
394                 if (err > 0) {
395                         dr->snd_nxt++;
396                         break;
397                 }
398                 udelay(delay);
399                 if ((delay <<= 1) > 128)
400                         delay = 128;
401         } while (err == -EAGAIN);
402
403         return err;
404 }
405
406 static int __send_request(struct request *req)
407 {
408         struct vdc_port *port = req->rq_disk->private_data;
409         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
410         struct scatterlist sg[port->ring_cookies];
411         struct vdc_req_entry *rqe;
412         struct vio_disk_desc *desc;
413         unsigned int map_perm;
414         int nsg, err, i;
415         u64 len;
416         u8 op;
417
418         map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
419
420         if (rq_data_dir(req) == READ) {
421                 map_perm |= LDC_MAP_W;
422                 op = VD_OP_BREAD;
423         } else {
424                 map_perm |= LDC_MAP_R;
425                 op = VD_OP_BWRITE;
426         }
427
428         sg_init_table(sg, port->ring_cookies);
429         nsg = blk_rq_map_sg(req->q, req, sg);
430
431         len = 0;
432         for (i = 0; i < nsg; i++)
433                 len += sg[i].length;
434
435         if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
436                 blk_stop_queue(port->disk->queue);
437                 err = -ENOMEM;
438                 goto out;
439         }
440
441         desc = vio_dring_cur(dr);
442
443         err = ldc_map_sg(port->vio.lp, sg, nsg,
444                          desc->cookies, port->ring_cookies,
445                          map_perm);
446         if (err < 0) {
447                 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
448                 return err;
449         }
450
451         rqe = &port->rq_arr[dr->prod];
452         rqe->req = req;
453
454         desc->hdr.ack = VIO_ACK_ENABLE;
455         desc->req_id = port->req_id;
456         desc->operation = op;
457         if (port->vdisk_type == VD_DISK_TYPE_DISK) {
458                 desc->slice = 0xff;
459         } else {
460                 desc->slice = 0;
461         }
462         desc->status = ~0;
463         desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
464         desc->size = len;
465         desc->ncookies = err;
466
467         /* This has to be a non-SMP write barrier because we are writing
468          * to memory which is shared with the peer LDOM.
469          */
470         wmb();
471         desc->hdr.state = VIO_DESC_READY;
472
473         err = __vdc_tx_trigger(port);
474         if (err < 0) {
475                 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
476         } else {
477                 port->req_id++;
478                 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
479         }
480 out:
481
482         return err;
483 }
484
485 static void do_vdc_request(struct request_queue *q)
486 {
487         while (1) {
488                 struct request *req = blk_fetch_request(q);
489
490                 if (!req)
491                         break;
492
493                 if (__send_request(req) < 0)
494                         __blk_end_request_all(req, -EIO);
495         }
496 }
497
498 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
499 {
500         struct vio_dring_state *dr;
501         struct vio_completion comp;
502         struct vio_disk_desc *desc;
503         unsigned int map_perm;
504         unsigned long flags;
505         int op_len, err;
506         void *req_buf;
507
508         if (!(((u64)1 << (u64)op) & port->operations))
509                 return -EOPNOTSUPP;
510
511         switch (op) {
512         case VD_OP_BREAD:
513         case VD_OP_BWRITE:
514         default:
515                 return -EINVAL;
516
517         case VD_OP_FLUSH:
518                 op_len = 0;
519                 map_perm = 0;
520                 break;
521
522         case VD_OP_GET_WCE:
523                 op_len = sizeof(u32);
524                 map_perm = LDC_MAP_W;
525                 break;
526
527         case VD_OP_SET_WCE:
528                 op_len = sizeof(u32);
529                 map_perm = LDC_MAP_R;
530                 break;
531
532         case VD_OP_GET_VTOC:
533                 op_len = sizeof(struct vio_disk_vtoc);
534                 map_perm = LDC_MAP_W;
535                 break;
536
537         case VD_OP_SET_VTOC:
538                 op_len = sizeof(struct vio_disk_vtoc);
539                 map_perm = LDC_MAP_R;
540                 break;
541
542         case VD_OP_GET_DISKGEOM:
543                 op_len = sizeof(struct vio_disk_geom);
544                 map_perm = LDC_MAP_W;
545                 break;
546
547         case VD_OP_SET_DISKGEOM:
548                 op_len = sizeof(struct vio_disk_geom);
549                 map_perm = LDC_MAP_R;
550                 break;
551
552         case VD_OP_SCSICMD:
553                 op_len = 16;
554                 map_perm = LDC_MAP_RW;
555                 break;
556
557         case VD_OP_GET_DEVID:
558                 op_len = sizeof(struct vio_disk_devid);
559                 map_perm = LDC_MAP_W;
560                 break;
561
562         case VD_OP_GET_EFI:
563         case VD_OP_SET_EFI:
564                 return -EOPNOTSUPP;
565                 break;
566         };
567
568         map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
569
570         op_len = (op_len + 7) & ~7;
571         req_buf = kzalloc(op_len, GFP_KERNEL);
572         if (!req_buf)
573                 return -ENOMEM;
574
575         if (len > op_len)
576                 len = op_len;
577
578         if (map_perm & LDC_MAP_R)
579                 memcpy(req_buf, buf, len);
580
581         spin_lock_irqsave(&port->vio.lock, flags);
582
583         dr = &port->vio.drings[VIO_DRIVER_TX_RING];
584
585         /* XXX If we want to use this code generically we have to
586          * XXX handle TX ring exhaustion etc.
587          */
588         desc = vio_dring_cur(dr);
589
590         err = ldc_map_single(port->vio.lp, req_buf, op_len,
591                              desc->cookies, port->ring_cookies,
592                              map_perm);
593         if (err < 0) {
594                 spin_unlock_irqrestore(&port->vio.lock, flags);
595                 kfree(req_buf);
596                 return err;
597         }
598
599         init_completion(&comp.com);
600         comp.waiting_for = WAITING_FOR_GEN_CMD;
601         port->vio.cmp = &comp;
602
603         desc->hdr.ack = VIO_ACK_ENABLE;
604         desc->req_id = port->req_id;
605         desc->operation = op;
606         desc->slice = 0;
607         desc->status = ~0;
608         desc->offset = 0;
609         desc->size = op_len;
610         desc->ncookies = err;
611
612         /* This has to be a non-SMP write barrier because we are writing
613          * to memory which is shared with the peer LDOM.
614          */
615         wmb();
616         desc->hdr.state = VIO_DESC_READY;
617
618         err = __vdc_tx_trigger(port);
619         if (err >= 0) {
620                 port->req_id++;
621                 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
622                 spin_unlock_irqrestore(&port->vio.lock, flags);
623
624                 wait_for_completion(&comp.com);
625                 err = comp.err;
626         } else {
627                 port->vio.cmp = NULL;
628                 spin_unlock_irqrestore(&port->vio.lock, flags);
629         }
630
631         if (map_perm & LDC_MAP_W)
632                 memcpy(buf, req_buf, len);
633
634         kfree(req_buf);
635
636         return err;
637 }
638
639 static int vdc_alloc_tx_ring(struct vdc_port *port)
640 {
641         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
642         unsigned long len, entry_size;
643         int ncookies;
644         void *dring;
645
646         entry_size = sizeof(struct vio_disk_desc) +
647                 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
648         len = (VDC_TX_RING_SIZE * entry_size);
649
650         ncookies = VIO_MAX_RING_COOKIES;
651         dring = ldc_alloc_exp_dring(port->vio.lp, len,
652                                     dr->cookies, &ncookies,
653                                     (LDC_MAP_SHADOW |
654                                      LDC_MAP_DIRECT |
655                                      LDC_MAP_RW));
656         if (IS_ERR(dring))
657                 return PTR_ERR(dring);
658
659         dr->base = dring;
660         dr->entry_size = entry_size;
661         dr->num_entries = VDC_TX_RING_SIZE;
662         dr->prod = dr->cons = 0;
663         dr->pending = VDC_TX_RING_SIZE;
664         dr->ncookies = ncookies;
665
666         return 0;
667 }
668
669 static void vdc_free_tx_ring(struct vdc_port *port)
670 {
671         struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
672
673         if (dr->base) {
674                 ldc_free_exp_dring(port->vio.lp, dr->base,
675                                    (dr->entry_size * dr->num_entries),
676                                    dr->cookies, dr->ncookies);
677                 dr->base = NULL;
678                 dr->entry_size = 0;
679                 dr->num_entries = 0;
680                 dr->pending = 0;
681                 dr->ncookies = 0;
682         }
683 }
684
685 static int probe_disk(struct vdc_port *port)
686 {
687         struct vio_completion comp;
688         struct request_queue *q;
689         struct gendisk *g;
690         int err;
691
692         init_completion(&comp.com);
693         comp.err = 0;
694         comp.waiting_for = WAITING_FOR_LINK_UP;
695         port->vio.cmp = &comp;
696
697         vio_port_up(&port->vio);
698
699         wait_for_completion(&comp.com);
700         if (comp.err)
701                 return comp.err;
702
703         err = generic_request(port, VD_OP_GET_VTOC,
704                               &port->label, sizeof(port->label));
705         if (err < 0) {
706                 printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
707                 return err;
708         }
709
710         if (vdc_version_supported(port, 1, 1)) {
711                 /* vdisk_size should be set during the handshake, if it wasn't
712                  * then the underlying disk is reserved by another system
713                  */
714                 if (port->vdisk_size == -1)
715                         return -ENODEV;
716         } else {
717                 err = generic_request(port, VD_OP_GET_DISKGEOM,
718                                       &port->geom, sizeof(port->geom));
719                 if (err < 0) {
720                         printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
721                                "error %d\n", err);
722                         return err;
723                 }
724                 port->vdisk_size = ((u64)port->geom.num_cyl *
725                                     (u64)port->geom.num_hd *
726                                     (u64)port->geom.num_sec);
727         }
728
729         q = blk_init_queue(do_vdc_request, &port->vio.lock);
730         if (!q) {
731                 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
732                        port->vio.name);
733                 return -ENOMEM;
734         }
735         g = alloc_disk(1 << PARTITION_SHIFT);
736         if (!g) {
737                 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
738                        port->vio.name);
739                 blk_cleanup_queue(q);
740                 return -ENOMEM;
741         }
742
743         port->disk = g;
744
745         blk_queue_max_segments(q, port->ring_cookies);
746         blk_queue_max_hw_sectors(q, port->max_xfer_size);
747         g->major = vdc_major;
748         g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
749         strcpy(g->disk_name, port->disk_name);
750
751         g->fops = &vdc_fops;
752         g->queue = q;
753         g->private_data = port;
754         g->driverfs_dev = &port->vio.vdev->dev;
755
756         set_capacity(g, port->vdisk_size);
757
758         if (vdc_version_supported(port, 1, 1)) {
759                 switch (port->vdisk_mtype) {
760                 case VD_MEDIA_TYPE_CD:
761                         pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
762                         g->flags |= GENHD_FL_CD;
763                         g->flags |= GENHD_FL_REMOVABLE;
764                         set_disk_ro(g, 1);
765                         break;
766
767                 case VD_MEDIA_TYPE_DVD:
768                         pr_info(PFX "Virtual DVD %s\n", port->disk_name);
769                         g->flags |= GENHD_FL_CD;
770                         g->flags |= GENHD_FL_REMOVABLE;
771                         set_disk_ro(g, 1);
772                         break;
773
774                 case VD_MEDIA_TYPE_FIXED:
775                         pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
776                         break;
777                 }
778         }
779
780         pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
781                g->disk_name,
782                port->vdisk_size, (port->vdisk_size >> (20 - 9)),
783                port->vio.ver.major, port->vio.ver.minor);
784
785         add_disk(g);
786
787         return 0;
788 }
789
790 static struct ldc_channel_config vdc_ldc_cfg = {
791         .event          = vdc_event,
792         .mtu            = 64,
793         .mode           = LDC_MODE_UNRELIABLE,
794 };
795
796 static struct vio_driver_ops vdc_vio_ops = {
797         .send_attr              = vdc_send_attr,
798         .handle_attr            = vdc_handle_attr,
799         .handshake_complete     = vdc_handshake_complete,
800 };
801
802 static void print_version(void)
803 {
804         static int version_printed;
805
806         if (version_printed++ == 0)
807                 printk(KERN_INFO "%s", version);
808 }
809
810 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
811 {
812         struct mdesc_handle *hp;
813         struct vdc_port *port;
814         int err;
815
816         print_version();
817
818         hp = mdesc_grab();
819
820         err = -ENODEV;
821         if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
822                 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
823                        vdev->dev_no);
824                 goto err_out_release_mdesc;
825         }
826
827         port = kzalloc(sizeof(*port), GFP_KERNEL);
828         err = -ENOMEM;
829         if (!port) {
830                 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
831                 goto err_out_release_mdesc;
832         }
833
834         if (vdev->dev_no >= 26)
835                 snprintf(port->disk_name, sizeof(port->disk_name),
836                          VDCBLK_NAME "%c%c",
837                          'a' + ((int)vdev->dev_no / 26) - 1,
838                          'a' + ((int)vdev->dev_no % 26));
839         else
840                 snprintf(port->disk_name, sizeof(port->disk_name),
841                          VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
842         port->vdisk_size = -1;
843
844         err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
845                               vdc_versions, ARRAY_SIZE(vdc_versions),
846                               &vdc_vio_ops, port->disk_name);
847         if (err)
848                 goto err_out_free_port;
849
850         port->vdisk_block_size = 512;
851         port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
852         port->ring_cookies = ((port->max_xfer_size *
853                                port->vdisk_block_size) / PAGE_SIZE) + 2;
854
855         err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
856         if (err)
857                 goto err_out_free_port;
858
859         err = vdc_alloc_tx_ring(port);
860         if (err)
861                 goto err_out_free_ldc;
862
863         err = probe_disk(port);
864         if (err)
865                 goto err_out_free_tx_ring;
866
867         dev_set_drvdata(&vdev->dev, port);
868
869         mdesc_release(hp);
870
871         return 0;
872
873 err_out_free_tx_ring:
874         vdc_free_tx_ring(port);
875
876 err_out_free_ldc:
877         vio_ldc_free(&port->vio);
878
879 err_out_free_port:
880         kfree(port);
881
882 err_out_release_mdesc:
883         mdesc_release(hp);
884         return err;
885 }
886
887 static int vdc_port_remove(struct vio_dev *vdev)
888 {
889         struct vdc_port *port = dev_get_drvdata(&vdev->dev);
890
891         if (port) {
892                 del_timer_sync(&port->vio.timer);
893
894                 vdc_free_tx_ring(port);
895                 vio_ldc_free(&port->vio);
896
897                 dev_set_drvdata(&vdev->dev, NULL);
898
899                 kfree(port);
900         }
901         return 0;
902 }
903
904 static const struct vio_device_id vdc_port_match[] = {
905         {
906                 .type = "vdc-port",
907         },
908         {},
909 };
910 MODULE_DEVICE_TABLE(vio, vdc_port_match);
911
912 static struct vio_driver vdc_port_driver = {
913         .id_table       = vdc_port_match,
914         .probe          = vdc_port_probe,
915         .remove         = vdc_port_remove,
916         .name           = "vdc_port",
917 };
918
919 static int __init vdc_init(void)
920 {
921         int err;
922
923         err = register_blkdev(0, VDCBLK_NAME);
924         if (err < 0)
925                 goto out_err;
926
927         vdc_major = err;
928
929         err = vio_register_driver(&vdc_port_driver);
930         if (err)
931                 goto out_unregister_blkdev;
932
933         return 0;
934
935 out_unregister_blkdev:
936         unregister_blkdev(vdc_major, VDCBLK_NAME);
937         vdc_major = 0;
938
939 out_err:
940         return err;
941 }
942
943 static void __exit vdc_exit(void)
944 {
945         vio_unregister_driver(&vdc_port_driver);
946         unregister_blkdev(vdc_major, VDCBLK_NAME);
947 }
948
949 module_init(vdc_init);
950 module_exit(vdc_exit);