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