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