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