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