blob: 1a9360da1f546f4acbc5df1ca337572f65993616 [file] [log] [blame]
David S. Miller667ef3c2007-07-16 04:03:56 -07001/* sunvdc.c: Sun LDOM Virtual Disk Client.
2 *
David S. Miller3d452e52008-09-01 01:48:52 -07003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller667ef3c2007-07-16 04:03:56 -07004 */
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>
Allen Pais9bce2182014-09-19 09:42:14 -040012#include <linux/cdrom.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070013#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>
David Millerd91c5e82007-10-24 08:46:01 +020019#include <linux/scatterlist.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070020
21#include <asm/vio.h>
22#include <asm/ldc.h>
23
24#define DRV_MODULE_NAME "sunvdc"
25#define PFX DRV_MODULE_NAME ": "
Allen Pais9bce2182014-09-19 09:42:14 -040026#define DRV_MODULE_VERSION "1.1"
27#define DRV_MODULE_RELDATE "February 13, 2013"
David S. Miller667ef3c2007-07-16 04:03:56 -070028
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -080029static char version[] =
David S. Miller667ef3c2007-07-16 04:03:56 -070030 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33MODULE_LICENSE("GPL");
34MODULE_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
43struct vdc_req_entry {
44 struct request *req;
45};
46
47struct vdc_port {
48 struct vio_driver_state vio;
49
David S. Miller667ef3c2007-07-16 04:03:56 -070050 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;
Allen Pais9bce2182014-09-19 09:42:14 -040069 u8 vdisk_mtype;
David S. Miller667ef3c2007-07-16 04:03:56 -070070
71 char disk_name[32];
72
David S. Miller667ef3c2007-07-16 04:03:56 -070073 struct vio_disk_vtoc label;
David S. Miller667ef3c2007-07-16 04:03:56 -070074};
75
76static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
77{
78 return container_of(vio, struct vdc_port, vio);
79}
80
David S. Miller667ef3c2007-07-16 04:03:56 -070081/* Ordered from largest major to lowest */
82static struct vio_version vdc_versions[] = {
Allen Pais9bce2182014-09-19 09:42:14 -040083 { .major = 1, .minor = 1 },
David S. Miller667ef3c2007-07-16 04:03:56 -070084 { .major = 1, .minor = 0 },
85};
86
Allen Pais9bce2182014-09-19 09:42:14 -040087static inline int vdc_version_supported(struct vdc_port *port,
88 u16 major, u16 minor)
89{
90 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
91}
92
David S. Miller667ef3c2007-07-16 04:03:56 -070093#define VDCBLK_NAME "vdisk"
94static int vdc_major;
95#define PARTITION_SHIFT 3
96
97static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
98{
99 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
100}
101
102static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
103{
104 struct gendisk *disk = bdev->bd_disk;
Allen Paisde5b73f2014-09-19 09:42:26 -0400105 sector_t nsect = get_capacity(disk);
106 sector_t cylinders = nsect;
David S. Miller667ef3c2007-07-16 04:03:56 -0700107
Allen Paisde5b73f2014-09-19 09:42:26 -0400108 geo->heads = 0xff;
109 geo->sectors = 0x3f;
110 sector_div(cylinders, geo->heads * geo->sectors);
111 geo->cylinders = cylinders;
112 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
113 geo->cylinders = 0xffff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700114
115 return 0;
116}
117
Allen Pais9bce2182014-09-19 09:42:14 -0400118/* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
119 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
120 * Needed to be able to install inside an ldom from an iso image.
121 */
122static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
123 unsigned command, unsigned long argument)
124{
125 int i;
126 struct gendisk *disk;
127
128 switch (command) {
129 case CDROMMULTISESSION:
130 pr_debug(PFX "Multisession CDs not supported\n");
131 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
132 if (put_user(0, (char __user *)(argument + i)))
133 return -EFAULT;
134 return 0;
135
136 case CDROM_GET_CAPABILITY:
137 disk = bdev->bd_disk;
138
139 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
140 return 0;
141 return -EINVAL;
142
143 default:
144 pr_debug(PFX "ioctl %08x not supported\n", command);
145 return -EINVAL;
146 }
147}
148
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700149static const struct block_device_operations vdc_fops = {
David S. Miller667ef3c2007-07-16 04:03:56 -0700150 .owner = THIS_MODULE,
151 .getgeo = vdc_getgeo,
Allen Pais9bce2182014-09-19 09:42:14 -0400152 .ioctl = vdc_ioctl,
David S. Miller667ef3c2007-07-16 04:03:56 -0700153};
154
155static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
156{
157 if (vio->cmp &&
158 (waiting_for == -1 ||
159 vio->cmp->waiting_for == waiting_for)) {
160 vio->cmp->err = err;
161 complete(&vio->cmp->com);
162 vio->cmp = NULL;
163 }
164}
165
166static void vdc_handshake_complete(struct vio_driver_state *vio)
167{
168 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
169}
170
171static int vdc_handle_unknown(struct vdc_port *port, void *arg)
172{
173 struct vio_msg_tag *pkt = arg;
174
175 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
176 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
177 printk(KERN_ERR PFX "Resetting connection.\n");
178
179 ldc_disconnect(port->vio.lp);
180
181 return -ECONNRESET;
182}
183
184static int vdc_send_attr(struct vio_driver_state *vio)
185{
186 struct vdc_port *port = to_vdc_port(vio);
187 struct vio_disk_attr_info pkt;
188
189 memset(&pkt, 0, sizeof(pkt));
190
191 pkt.tag.type = VIO_TYPE_CTRL;
192 pkt.tag.stype = VIO_SUBTYPE_INFO;
193 pkt.tag.stype_env = VIO_ATTR_INFO;
194 pkt.tag.sid = vio_send_sid(vio);
195
196 pkt.xfer_mode = VIO_DRING_MODE;
197 pkt.vdisk_block_size = port->vdisk_block_size;
198 pkt.max_xfer_size = port->max_xfer_size;
199
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800200 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700201 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
202
203 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
204}
205
206static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
207{
208 struct vdc_port *port = to_vdc_port(vio);
209 struct vio_disk_attr_info *pkt = arg;
210
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800211 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
Allen Pais9bce2182014-09-19 09:42:14 -0400212 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700213 pkt->tag.stype, pkt->operations,
Allen Pais9bce2182014-09-19 09:42:14 -0400214 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
David S. Miller667ef3c2007-07-16 04:03:56 -0700215 pkt->xfer_mode, pkt->vdisk_block_size,
216 pkt->max_xfer_size);
217
218 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
219 switch (pkt->vdisk_type) {
220 case VD_DISK_TYPE_DISK:
221 case VD_DISK_TYPE_SLICE:
222 break;
223
224 default:
225 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
226 vio->name, pkt->vdisk_type);
227 return -ECONNRESET;
228 }
229
230 if (pkt->vdisk_block_size > port->vdisk_block_size) {
231 printk(KERN_ERR PFX "%s: BLOCK size increased "
232 "%u --> %u\n",
233 vio->name,
234 port->vdisk_block_size, pkt->vdisk_block_size);
235 return -ECONNRESET;
236 }
237
238 port->operations = pkt->operations;
David S. Miller667ef3c2007-07-16 04:03:56 -0700239 port->vdisk_type = pkt->vdisk_type;
Allen Pais9bce2182014-09-19 09:42:14 -0400240 if (vdc_version_supported(port, 1, 1)) {
241 port->vdisk_size = pkt->vdisk_size;
242 port->vdisk_mtype = pkt->vdisk_mtype;
243 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700244 if (pkt->max_xfer_size < port->max_xfer_size)
245 port->max_xfer_size = pkt->max_xfer_size;
246 port->vdisk_block_size = pkt->vdisk_block_size;
247 return 0;
248 } else {
249 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
250
251 return -ECONNRESET;
252 }
253}
254
255static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
256{
257 int err = desc->status;
258
259 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
260}
261
David S. Miller667ef3c2007-07-16 04:03:56 -0700262static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
263 unsigned int index)
264{
265 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
266 struct vdc_req_entry *rqe = &port->rq_arr[index];
267 struct request *req;
268
269 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
270 return;
271
272 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
273 desc->hdr.state = VIO_DESC_FREE;
274 dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1);
275
276 req = rqe->req;
277 if (req == NULL) {
278 vdc_end_special(port, desc);
279 return;
280 }
281
282 rqe->req = NULL;
283
Tejun Heo044208502009-04-28 13:06:08 +0900284 __blk_end_request(req, (desc->status ? -EIO : 0), desc->size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700285
286 if (blk_queue_stopped(port->disk->queue))
287 blk_start_queue(port->disk->queue);
288}
289
290static int vdc_ack(struct vdc_port *port, void *msgbuf)
291{
292 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
293 struct vio_dring_data *pkt = msgbuf;
294
295 if (unlikely(pkt->dring_ident != dr->ident ||
296 pkt->start_idx != pkt->end_idx ||
297 pkt->start_idx >= VDC_TX_RING_SIZE))
298 return 0;
299
300 vdc_end_one(port, dr, pkt->start_idx);
301
302 return 0;
303}
304
305static int vdc_nack(struct vdc_port *port, void *msgbuf)
306{
307 /* XXX Implement me XXX */
308 return 0;
309}
310
311static void vdc_event(void *arg, int event)
312{
313 struct vdc_port *port = arg;
314 struct vio_driver_state *vio = &port->vio;
315 unsigned long flags;
316 int err;
317
318 spin_lock_irqsave(&vio->lock, flags);
319
320 if (unlikely(event == LDC_EVENT_RESET ||
321 event == LDC_EVENT_UP)) {
322 vio_link_state_change(vio, event);
323 spin_unlock_irqrestore(&vio->lock, flags);
324 return;
325 }
326
327 if (unlikely(event != LDC_EVENT_DATA_READY)) {
328 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
329 spin_unlock_irqrestore(&vio->lock, flags);
330 return;
331 }
332
333 err = 0;
334 while (1) {
335 union {
336 struct vio_msg_tag tag;
337 u64 raw[8];
338 } msgbuf;
339
340 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
341 if (unlikely(err < 0)) {
342 if (err == -ECONNRESET)
343 vio_conn_reset(vio);
344 break;
345 }
346 if (err == 0)
347 break;
348 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
349 msgbuf.tag.type,
350 msgbuf.tag.stype,
351 msgbuf.tag.stype_env,
352 msgbuf.tag.sid);
353 err = vio_validate_sid(vio, &msgbuf.tag);
354 if (err < 0)
355 break;
356
357 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
358 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
359 err = vdc_ack(port, &msgbuf);
360 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
361 err = vdc_nack(port, &msgbuf);
362 else
363 err = vdc_handle_unknown(port, &msgbuf);
364 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
365 err = vio_control_pkt_engine(vio, &msgbuf);
366 } else {
367 err = vdc_handle_unknown(port, &msgbuf);
368 }
369 if (err < 0)
370 break;
371 }
372 if (err < 0)
373 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
374 spin_unlock_irqrestore(&vio->lock, flags);
375}
376
377static int __vdc_tx_trigger(struct vdc_port *port)
378{
379 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
380 struct vio_dring_data hdr = {
381 .tag = {
382 .type = VIO_TYPE_DATA,
383 .stype = VIO_SUBTYPE_INFO,
384 .stype_env = VIO_DRING_DATA,
385 .sid = vio_send_sid(&port->vio),
386 },
387 .dring_ident = dr->ident,
388 .start_idx = dr->prod,
389 .end_idx = dr->prod,
390 };
391 int err, delay;
392
393 hdr.seq = dr->snd_nxt;
394 delay = 1;
395 do {
396 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
397 if (err > 0) {
398 dr->snd_nxt++;
399 break;
400 }
401 udelay(delay);
402 if ((delay <<= 1) > 128)
403 delay = 128;
404 } while (err == -EAGAIN);
405
406 return err;
407}
408
409static int __send_request(struct request *req)
410{
411 struct vdc_port *port = req->rq_disk->private_data;
412 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
413 struct scatterlist sg[port->ring_cookies];
414 struct vdc_req_entry *rqe;
415 struct vio_disk_desc *desc;
416 unsigned int map_perm;
417 int nsg, err, i;
418 u64 len;
419 u8 op;
420
421 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
422
423 if (rq_data_dir(req) == READ) {
424 map_perm |= LDC_MAP_W;
425 op = VD_OP_BREAD;
426 } else {
427 map_perm |= LDC_MAP_R;
428 op = VD_OP_BWRITE;
429 }
430
Jens Axboe45711f12007-10-22 21:19:53 +0200431 sg_init_table(sg, port->ring_cookies);
David S. Miller667ef3c2007-07-16 04:03:56 -0700432 nsg = blk_rq_map_sg(req->q, req, sg);
433
434 len = 0;
435 for (i = 0; i < nsg; i++)
436 len += sg[i].length;
437
438 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
439 blk_stop_queue(port->disk->queue);
440 err = -ENOMEM;
441 goto out;
442 }
443
444 desc = vio_dring_cur(dr);
445
446 err = ldc_map_sg(port->vio.lp, sg, nsg,
447 desc->cookies, port->ring_cookies,
448 map_perm);
449 if (err < 0) {
450 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
451 return err;
452 }
453
454 rqe = &port->rq_arr[dr->prod];
455 rqe->req = req;
456
457 desc->hdr.ack = VIO_ACK_ENABLE;
458 desc->req_id = port->req_id;
459 desc->operation = op;
460 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
David S. Miller1bd4b282007-08-24 22:05:44 -0700461 desc->slice = 0xff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700462 } else {
463 desc->slice = 0;
464 }
465 desc->status = ~0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900466 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
David S. Miller667ef3c2007-07-16 04:03:56 -0700467 desc->size = len;
468 desc->ncookies = err;
469
470 /* This has to be a non-SMP write barrier because we are writing
471 * to memory which is shared with the peer LDOM.
472 */
473 wmb();
474 desc->hdr.state = VIO_DESC_READY;
475
476 err = __vdc_tx_trigger(port);
477 if (err < 0) {
478 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
479 } else {
480 port->req_id++;
481 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
482 }
483out:
484
485 return err;
486}
487
Jens Axboe165125e2007-07-24 09:28:11 +0200488static void do_vdc_request(struct request_queue *q)
David S. Miller667ef3c2007-07-16 04:03:56 -0700489{
490 while (1) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900491 struct request *req = blk_fetch_request(q);
David S. Miller667ef3c2007-07-16 04:03:56 -0700492
493 if (!req)
494 break;
495
David S. Miller667ef3c2007-07-16 04:03:56 -0700496 if (__send_request(req) < 0)
Tejun Heo044208502009-04-28 13:06:08 +0900497 __blk_end_request_all(req, -EIO);
David S. Miller667ef3c2007-07-16 04:03:56 -0700498 }
499}
500
501static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
502{
503 struct vio_dring_state *dr;
504 struct vio_completion comp;
505 struct vio_disk_desc *desc;
506 unsigned int map_perm;
507 unsigned long flags;
508 int op_len, err;
509 void *req_buf;
510
David S. Millerf4d96052013-02-14 11:49:01 -0800511 if (!(((u64)1 << (u64)op) & port->operations))
David S. Miller667ef3c2007-07-16 04:03:56 -0700512 return -EOPNOTSUPP;
513
514 switch (op) {
515 case VD_OP_BREAD:
516 case VD_OP_BWRITE:
517 default:
518 return -EINVAL;
519
520 case VD_OP_FLUSH:
521 op_len = 0;
522 map_perm = 0;
523 break;
524
525 case VD_OP_GET_WCE:
526 op_len = sizeof(u32);
527 map_perm = LDC_MAP_W;
528 break;
529
530 case VD_OP_SET_WCE:
531 op_len = sizeof(u32);
532 map_perm = LDC_MAP_R;
533 break;
534
535 case VD_OP_GET_VTOC:
536 op_len = sizeof(struct vio_disk_vtoc);
537 map_perm = LDC_MAP_W;
538 break;
539
540 case VD_OP_SET_VTOC:
541 op_len = sizeof(struct vio_disk_vtoc);
542 map_perm = LDC_MAP_R;
543 break;
544
545 case VD_OP_GET_DISKGEOM:
546 op_len = sizeof(struct vio_disk_geom);
547 map_perm = LDC_MAP_W;
548 break;
549
550 case VD_OP_SET_DISKGEOM:
551 op_len = sizeof(struct vio_disk_geom);
552 map_perm = LDC_MAP_R;
553 break;
554
555 case VD_OP_SCSICMD:
556 op_len = 16;
557 map_perm = LDC_MAP_RW;
558 break;
559
560 case VD_OP_GET_DEVID:
561 op_len = sizeof(struct vio_disk_devid);
562 map_perm = LDC_MAP_W;
563 break;
564
565 case VD_OP_GET_EFI:
566 case VD_OP_SET_EFI:
567 return -EOPNOTSUPP;
568 break;
569 };
570
571 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
572
573 op_len = (op_len + 7) & ~7;
574 req_buf = kzalloc(op_len, GFP_KERNEL);
575 if (!req_buf)
576 return -ENOMEM;
577
578 if (len > op_len)
579 len = op_len;
580
581 if (map_perm & LDC_MAP_R)
582 memcpy(req_buf, buf, len);
583
584 spin_lock_irqsave(&port->vio.lock, flags);
585
586 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
587
588 /* XXX If we want to use this code generically we have to
589 * XXX handle TX ring exhaustion etc.
590 */
591 desc = vio_dring_cur(dr);
592
593 err = ldc_map_single(port->vio.lp, req_buf, op_len,
594 desc->cookies, port->ring_cookies,
595 map_perm);
596 if (err < 0) {
597 spin_unlock_irqrestore(&port->vio.lock, flags);
598 kfree(req_buf);
599 return err;
600 }
601
602 init_completion(&comp.com);
603 comp.waiting_for = WAITING_FOR_GEN_CMD;
604 port->vio.cmp = &comp;
605
606 desc->hdr.ack = VIO_ACK_ENABLE;
607 desc->req_id = port->req_id;
608 desc->operation = op;
609 desc->slice = 0;
610 desc->status = ~0;
611 desc->offset = 0;
612 desc->size = op_len;
613 desc->ncookies = err;
614
615 /* This has to be a non-SMP write barrier because we are writing
616 * to memory which is shared with the peer LDOM.
617 */
618 wmb();
619 desc->hdr.state = VIO_DESC_READY;
620
621 err = __vdc_tx_trigger(port);
622 if (err >= 0) {
623 port->req_id++;
624 dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1);
625 spin_unlock_irqrestore(&port->vio.lock, flags);
626
627 wait_for_completion(&comp.com);
628 err = comp.err;
629 } else {
630 port->vio.cmp = NULL;
631 spin_unlock_irqrestore(&port->vio.lock, flags);
632 }
633
634 if (map_perm & LDC_MAP_W)
635 memcpy(buf, req_buf, len);
636
637 kfree(req_buf);
638
639 return err;
640}
641
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800642static int vdc_alloc_tx_ring(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700643{
644 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
645 unsigned long len, entry_size;
646 int ncookies;
647 void *dring;
648
649 entry_size = sizeof(struct vio_disk_desc) +
650 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
651 len = (VDC_TX_RING_SIZE * entry_size);
652
653 ncookies = VIO_MAX_RING_COOKIES;
654 dring = ldc_alloc_exp_dring(port->vio.lp, len,
655 dr->cookies, &ncookies,
656 (LDC_MAP_SHADOW |
657 LDC_MAP_DIRECT |
658 LDC_MAP_RW));
659 if (IS_ERR(dring))
660 return PTR_ERR(dring);
661
662 dr->base = dring;
663 dr->entry_size = entry_size;
664 dr->num_entries = VDC_TX_RING_SIZE;
665 dr->prod = dr->cons = 0;
666 dr->pending = VDC_TX_RING_SIZE;
667 dr->ncookies = ncookies;
668
669 return 0;
670}
671
672static void vdc_free_tx_ring(struct vdc_port *port)
673{
674 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
675
676 if (dr->base) {
677 ldc_free_exp_dring(port->vio.lp, dr->base,
678 (dr->entry_size * dr->num_entries),
679 dr->cookies, dr->ncookies);
680 dr->base = NULL;
681 dr->entry_size = 0;
682 dr->num_entries = 0;
683 dr->pending = 0;
684 dr->ncookies = 0;
685 }
686}
687
688static int probe_disk(struct vdc_port *port)
689{
690 struct vio_completion comp;
691 struct request_queue *q;
692 struct gendisk *g;
693 int err;
694
695 init_completion(&comp.com);
696 comp.err = 0;
697 comp.waiting_for = WAITING_FOR_LINK_UP;
698 port->vio.cmp = &comp;
699
700 vio_port_up(&port->vio);
701
702 wait_for_completion(&comp.com);
703 if (comp.err)
704 return comp.err;
705
706 err = generic_request(port, VD_OP_GET_VTOC,
707 &port->label, sizeof(port->label));
708 if (err < 0) {
709 printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err);
710 return err;
711 }
712
Allen Pais9bce2182014-09-19 09:42:14 -0400713 if (vdc_version_supported(port, 1, 1)) {
714 /* vdisk_size should be set during the handshake, if it wasn't
715 * then the underlying disk is reserved by another system
716 */
717 if (port->vdisk_size == -1)
718 return -ENODEV;
719 } else {
Allen Paisde5b73f2014-09-19 09:42:26 -0400720 struct vio_disk_geom geom;
721
Allen Pais9bce2182014-09-19 09:42:14 -0400722 err = generic_request(port, VD_OP_GET_DISKGEOM,
Allen Paisde5b73f2014-09-19 09:42:26 -0400723 &geom, sizeof(geom));
Allen Pais9bce2182014-09-19 09:42:14 -0400724 if (err < 0) {
725 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
726 "error %d\n", err);
727 return err;
728 }
Allen Paisde5b73f2014-09-19 09:42:26 -0400729 port->vdisk_size = ((u64)geom.num_cyl *
730 (u64)geom.num_hd *
731 (u64)geom.num_sec);
David S. Miller667ef3c2007-07-16 04:03:56 -0700732 }
733
David S. Miller667ef3c2007-07-16 04:03:56 -0700734 q = blk_init_queue(do_vdc_request, &port->vio.lock);
735 if (!q) {
736 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
737 port->vio.name);
738 return -ENOMEM;
739 }
740 g = alloc_disk(1 << PARTITION_SHIFT);
741 if (!g) {
742 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
743 port->vio.name);
744 blk_cleanup_queue(q);
745 return -ENOMEM;
746 }
747
748 port->disk = g;
749
Dwight Engen5eed69f2014-09-19 09:42:53 -0400750 /* Each segment in a request is up to an aligned page in size. */
751 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
752 blk_queue_max_segment_size(q, PAGE_SIZE);
753
Martin K. Petersen8a783622010-02-26 00:20:39 -0500754 blk_queue_max_segments(q, port->ring_cookies);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500755 blk_queue_max_hw_sectors(q, port->max_xfer_size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700756 g->major = vdc_major;
David S. Miller91ba3c22007-07-18 15:15:45 -0700757 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
David S. Miller667ef3c2007-07-16 04:03:56 -0700758 strcpy(g->disk_name, port->disk_name);
759
760 g->fops = &vdc_fops;
761 g->queue = q;
762 g->private_data = port;
763 g->driverfs_dev = &port->vio.vdev->dev;
764
765 set_capacity(g, port->vdisk_size);
766
Allen Pais9bce2182014-09-19 09:42:14 -0400767 if (vdc_version_supported(port, 1, 1)) {
768 switch (port->vdisk_mtype) {
769 case VD_MEDIA_TYPE_CD:
770 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
771 g->flags |= GENHD_FL_CD;
772 g->flags |= GENHD_FL_REMOVABLE;
773 set_disk_ro(g, 1);
774 break;
775
776 case VD_MEDIA_TYPE_DVD:
777 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
778 g->flags |= GENHD_FL_CD;
779 g->flags |= GENHD_FL_REMOVABLE;
780 set_disk_ro(g, 1);
781 break;
782
783 case VD_MEDIA_TYPE_FIXED:
784 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
785 break;
786 }
787 }
788
789 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700790 g->disk_name,
Allen Pais9bce2182014-09-19 09:42:14 -0400791 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
792 port->vio.ver.major, port->vio.ver.minor);
David S. Miller667ef3c2007-07-16 04:03:56 -0700793
794 add_disk(g);
795
796 return 0;
797}
798
799static struct ldc_channel_config vdc_ldc_cfg = {
800 .event = vdc_event,
801 .mtu = 64,
802 .mode = LDC_MODE_UNRELIABLE,
803};
804
805static struct vio_driver_ops vdc_vio_ops = {
806 .send_attr = vdc_send_attr,
807 .handle_attr = vdc_handle_attr,
808 .handshake_complete = vdc_handshake_complete,
809};
810
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800811static void print_version(void)
David S. Miller80dc35d2007-07-17 21:46:00 -0700812{
813 static int version_printed;
814
815 if (version_printed++ == 0)
816 printk(KERN_INFO "%s", version);
817}
818
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800819static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller667ef3c2007-07-16 04:03:56 -0700820{
David S. Miller43fdf272007-07-12 13:47:50 -0700821 struct mdesc_handle *hp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700822 struct vdc_port *port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700823 int err;
824
David S. Miller80dc35d2007-07-17 21:46:00 -0700825 print_version();
David S. Miller667ef3c2007-07-16 04:03:56 -0700826
David S. Miller43fdf272007-07-12 13:47:50 -0700827 hp = mdesc_grab();
David S. Miller667ef3c2007-07-16 04:03:56 -0700828
David S. Miller43fdf272007-07-12 13:47:50 -0700829 err = -ENODEV;
David S. Miller91ba3c22007-07-18 15:15:45 -0700830 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800831 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
David S. Miller91ba3c22007-07-18 15:15:45 -0700832 vdev->dev_no);
David S. Miller43fdf272007-07-12 13:47:50 -0700833 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700834 }
835
836 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700837 err = -ENOMEM;
David S. Miller667ef3c2007-07-16 04:03:56 -0700838 if (!port) {
839 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
David S. Miller43fdf272007-07-12 13:47:50 -0700840 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700841 }
842
David S. Miller91ba3c22007-07-18 15:15:45 -0700843 if (vdev->dev_no >= 26)
David S. Miller667ef3c2007-07-16 04:03:56 -0700844 snprintf(port->disk_name, sizeof(port->disk_name),
845 VDCBLK_NAME "%c%c",
David S. Miller91ba3c22007-07-18 15:15:45 -0700846 'a' + ((int)vdev->dev_no / 26) - 1,
847 'a' + ((int)vdev->dev_no % 26));
David S. Miller667ef3c2007-07-16 04:03:56 -0700848 else
849 snprintf(port->disk_name, sizeof(port->disk_name),
David S. Miller91ba3c22007-07-18 15:15:45 -0700850 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
Allen Pais9bce2182014-09-19 09:42:14 -0400851 port->vdisk_size = -1;
David S. Miller667ef3c2007-07-16 04:03:56 -0700852
David S. Miller43fdf272007-07-12 13:47:50 -0700853 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
David S. Miller667ef3c2007-07-16 04:03:56 -0700854 vdc_versions, ARRAY_SIZE(vdc_versions),
855 &vdc_vio_ops, port->disk_name);
856 if (err)
857 goto err_out_free_port;
858
859 port->vdisk_block_size = 512;
860 port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size);
861 port->ring_cookies = ((port->max_xfer_size *
862 port->vdisk_block_size) / PAGE_SIZE) + 2;
863
864 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
865 if (err)
866 goto err_out_free_port;
867
868 err = vdc_alloc_tx_ring(port);
869 if (err)
870 goto err_out_free_ldc;
871
872 err = probe_disk(port);
873 if (err)
874 goto err_out_free_tx_ring;
875
David S. Miller667ef3c2007-07-16 04:03:56 -0700876 dev_set_drvdata(&vdev->dev, port);
877
David S. Miller43fdf272007-07-12 13:47:50 -0700878 mdesc_release(hp);
879
David S. Miller667ef3c2007-07-16 04:03:56 -0700880 return 0;
881
882err_out_free_tx_ring:
883 vdc_free_tx_ring(port);
884
885err_out_free_ldc:
886 vio_ldc_free(&port->vio);
887
888err_out_free_port:
889 kfree(port);
890
David S. Miller43fdf272007-07-12 13:47:50 -0700891err_out_release_mdesc:
892 mdesc_release(hp);
David S. Miller667ef3c2007-07-16 04:03:56 -0700893 return err;
894}
895
896static int vdc_port_remove(struct vio_dev *vdev)
897{
898 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
899
900 if (port) {
901 del_timer_sync(&port->vio.timer);
902
903 vdc_free_tx_ring(port);
904 vio_ldc_free(&port->vio);
905
906 dev_set_drvdata(&vdev->dev, NULL);
907
908 kfree(port);
909 }
910 return 0;
911}
912
David S. Miller3d452e52008-09-01 01:48:52 -0700913static const struct vio_device_id vdc_port_match[] = {
David S. Miller667ef3c2007-07-16 04:03:56 -0700914 {
915 .type = "vdc-port",
916 },
917 {},
918};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -0700919MODULE_DEVICE_TABLE(vio, vdc_port_match);
David S. Miller667ef3c2007-07-16 04:03:56 -0700920
921static struct vio_driver vdc_port_driver = {
922 .id_table = vdc_port_match,
923 .probe = vdc_port_probe,
924 .remove = vdc_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +0000925 .name = "vdc_port",
David S. Miller667ef3c2007-07-16 04:03:56 -0700926};
927
David S. Miller667ef3c2007-07-16 04:03:56 -0700928static int __init vdc_init(void)
929{
930 int err;
931
932 err = register_blkdev(0, VDCBLK_NAME);
933 if (err < 0)
934 goto out_err;
935
936 vdc_major = err;
David S. Miller667ef3c2007-07-16 04:03:56 -0700937
938 err = vio_register_driver(&vdc_port_driver);
939 if (err)
David S. Miller80dc35d2007-07-17 21:46:00 -0700940 goto out_unregister_blkdev;
David S. Miller667ef3c2007-07-16 04:03:56 -0700941
942 return 0;
943
David S. Miller667ef3c2007-07-16 04:03:56 -0700944out_unregister_blkdev:
945 unregister_blkdev(vdc_major, VDCBLK_NAME);
946 vdc_major = 0;
947
948out_err:
949 return err;
950}
951
952static void __exit vdc_exit(void)
953{
954 vio_unregister_driver(&vdc_port_driver);
David S. Miller667ef3c2007-07-16 04:03:56 -0700955 unregister_blkdev(vdc_major, VDCBLK_NAME);
956}
957
958module_init(vdc_init);
959module_exit(vdc_exit);