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