blob: 6f45a53f7cbf5204e62f04c946d8a6420b6d75f6 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
David S. Miller667ef3c2007-07-16 04:03:56 -07002/* sunvdc.c: Sun LDOM Virtual Disk Client.
3 *
David S. Miller3d452e52008-09-01 01:48:52 -07004 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller667ef3c2007-07-16 04:03:56 -07005 */
6
7#include <linux/module.h>
8#include <linux/kernel.h>
9#include <linux/types.h>
Jens Axboefa182a12018-10-15 08:54:23 -060010#include <linux/blk-mq.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070011#include <linux/hdreg.h>
12#include <linux/genhd.h>
Allen Pais9bce2182014-09-19 09:42:14 -040013#include <linux/cdrom.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070014#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>
David Millerd91c5e82007-10-24 08:46:01 +020020#include <linux/scatterlist.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070021
22#include <asm/vio.h>
23#include <asm/ldc.h>
24
25#define DRV_MODULE_NAME "sunvdc"
26#define PFX DRV_MODULE_NAME ": "
Dwight Engen76e74bb2014-12-11 12:26:17 -050027#define DRV_MODULE_VERSION "1.2"
28#define DRV_MODULE_RELDATE "November 24, 2014"
David S. Miller667ef3c2007-07-16 04:03:56 -070029
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -080030static char version[] =
David S. Miller667ef3c2007-07-16 04:03:56 -070031 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
32MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
33MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
34MODULE_LICENSE("GPL");
35MODULE_VERSION(DRV_MODULE_VERSION);
36
Dwight Engend0aedcd2014-09-19 09:43:02 -040037#define VDC_TX_RING_SIZE 512
Liam R. Howlettf41e5462017-02-01 14:09:26 -050038#define VDC_DEFAULT_BLK_SIZE 512
David S. Miller667ef3c2007-07-16 04:03:56 -070039
Kees Cookff5d1a42018-10-08 08:46:51 -070040#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
David S. Miller667ef3c2007-07-16 04:03:56 -070044#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
Young Xiaoa11f6ca2018-11-28 12:36:39 +000049#define VDC_MAX_RETRIES 10
50
Dwight Engen76e74bb2014-12-11 12:26:17 -050051static struct workqueue_struct *sunvdc_wq;
52
David S. Miller667ef3c2007-07-16 04:03:56 -070053struct vdc_req_entry {
54 struct request *req;
55};
56
57struct vdc_port {
58 struct vio_driver_state vio;
59
David S. Miller667ef3c2007-07-16 04:03:56 -070060 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;
Jens Axboefa182a12018-10-15 08:54:23 -060072 u32 drain;
David S. Miller667ef3c2007-07-16 04:03:56 -070073
Dwight Engen76e74bb2014-12-11 12:26:17 -050074 u64 ldc_timeout;
Jens Axboefa182a12018-10-15 08:54:23 -060075 struct delayed_work ldc_reset_timer_work;
Dwight Engen76e74bb2014-12-11 12:26:17 -050076 struct work_struct ldc_reset_work;
77
David S. Miller667ef3c2007-07-16 04:03:56 -070078 /* 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;
Allen Pais9bce2182014-09-19 09:42:14 -040084 u8 vdisk_mtype;
Liam R. Howlettf41e5462017-02-01 14:09:26 -050085 u32 vdisk_phys_blksz;
David S. Miller667ef3c2007-07-16 04:03:56 -070086
Jens Axboefa182a12018-10-15 08:54:23 -060087 struct blk_mq_tag_set tag_set;
88
David S. Miller667ef3c2007-07-16 04:03:56 -070089 char disk_name[32];
David S. Miller667ef3c2007-07-16 04:03:56 -070090};
91
Dwight Engen76e74bb2014-12-11 12:26:17 -050092static void vdc_ldc_reset(struct vdc_port *port);
93static void vdc_ldc_reset_work(struct work_struct *work);
Jens Axboefa182a12018-10-15 08:54:23 -060094static void vdc_ldc_reset_timer_work(struct work_struct *work);
Dwight Engen76e74bb2014-12-11 12:26:17 -050095
David S. Miller667ef3c2007-07-16 04:03:56 -070096static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
97{
98 return container_of(vio, struct vdc_port, vio);
99}
100
David S. Miller667ef3c2007-07-16 04:03:56 -0700101/* Ordered from largest major to lowest */
102static struct vio_version vdc_versions[] = {
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500103 { .major = 1, .minor = 2 },
Allen Pais9bce2182014-09-19 09:42:14 -0400104 { .major = 1, .minor = 1 },
David S. Miller667ef3c2007-07-16 04:03:56 -0700105 { .major = 1, .minor = 0 },
106};
107
Allen Pais9bce2182014-09-19 09:42:14 -0400108static 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700114#define VDCBLK_NAME "vdisk"
115static int vdc_major;
116#define PARTITION_SHIFT 3
117
118static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
119{
120 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
121}
122
123static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
124{
125 struct gendisk *disk = bdev->bd_disk;
Allen Paisde5b73f2014-09-19 09:42:26 -0400126 sector_t nsect = get_capacity(disk);
127 sector_t cylinders = nsect;
David S. Miller667ef3c2007-07-16 04:03:56 -0700128
Allen Paisde5b73f2014-09-19 09:42:26 -0400129 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;
David S. Miller667ef3c2007-07-16 04:03:56 -0700135
136 return 0;
137}
138
Allen Pais9bce2182014-09-19 09:42:14 -0400139/* 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 */
143static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
144 unsigned command, unsigned long argument)
145{
146 int i;
147 struct gendisk *disk;
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 disk = bdev->bd_disk;
159
160 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
161 return 0;
162 return -EINVAL;
163
164 default:
165 pr_debug(PFX "ioctl %08x not supported\n", command);
166 return -EINVAL;
167 }
168}
169
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700170static const struct block_device_operations vdc_fops = {
David S. Miller667ef3c2007-07-16 04:03:56 -0700171 .owner = THIS_MODULE,
172 .getgeo = vdc_getgeo,
Allen Pais9bce2182014-09-19 09:42:14 -0400173 .ioctl = vdc_ioctl,
Arnd Bergmann9452b1a2019-11-28 15:48:10 +0100174 .compat_ioctl = blkdev_compat_ptr_ioctl,
David S. Miller667ef3c2007-07-16 04:03:56 -0700175};
176
Dwight Engen76e74bb2014-12-11 12:26:17 -0500177static void vdc_blk_queue_start(struct vdc_port *port)
178{
179 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
180
181 /* restart blk queue when ring is half emptied. also called after
182 * handshake completes, so check for initial handshake before we've
183 * allocated a disk.
184 */
Jens Axboefa182a12018-10-15 08:54:23 -0600185 if (port->disk && vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50)
Ming Lei43bfeb42019-01-03 09:19:48 +0800186 blk_mq_start_stopped_hw_queues(port->disk->queue, true);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500187}
188
David S. Miller667ef3c2007-07-16 04:03:56 -0700189static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
190{
191 if (vio->cmp &&
192 (waiting_for == -1 ||
193 vio->cmp->waiting_for == waiting_for)) {
194 vio->cmp->err = err;
195 complete(&vio->cmp->com);
196 vio->cmp = NULL;
197 }
198}
199
200static void vdc_handshake_complete(struct vio_driver_state *vio)
201{
Dwight Engen76e74bb2014-12-11 12:26:17 -0500202 struct vdc_port *port = to_vdc_port(vio);
203
Jens Axboefa182a12018-10-15 08:54:23 -0600204 cancel_delayed_work(&port->ldc_reset_timer_work);
David S. Miller667ef3c2007-07-16 04:03:56 -0700205 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500206 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700207}
208
209static int vdc_handle_unknown(struct vdc_port *port, void *arg)
210{
211 struct vio_msg_tag *pkt = arg;
212
213 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
214 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
215 printk(KERN_ERR PFX "Resetting connection.\n");
216
217 ldc_disconnect(port->vio.lp);
218
219 return -ECONNRESET;
220}
221
222static int vdc_send_attr(struct vio_driver_state *vio)
223{
224 struct vdc_port *port = to_vdc_port(vio);
225 struct vio_disk_attr_info pkt;
226
227 memset(&pkt, 0, sizeof(pkt));
228
229 pkt.tag.type = VIO_TYPE_CTRL;
230 pkt.tag.stype = VIO_SUBTYPE_INFO;
231 pkt.tag.stype_env = VIO_ATTR_INFO;
232 pkt.tag.sid = vio_send_sid(vio);
233
234 pkt.xfer_mode = VIO_DRING_MODE;
235 pkt.vdisk_block_size = port->vdisk_block_size;
236 pkt.max_xfer_size = port->max_xfer_size;
237
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800238 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700239 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
240
241 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
242}
243
244static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
245{
246 struct vdc_port *port = to_vdc_port(vio);
247 struct vio_disk_attr_info *pkt = arg;
248
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800249 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
Allen Pais9bce2182014-09-19 09:42:14 -0400250 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700251 pkt->tag.stype, pkt->operations,
Allen Pais9bce2182014-09-19 09:42:14 -0400252 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
David S. Miller667ef3c2007-07-16 04:03:56 -0700253 pkt->xfer_mode, pkt->vdisk_block_size,
254 pkt->max_xfer_size);
255
256 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
257 switch (pkt->vdisk_type) {
258 case VD_DISK_TYPE_DISK:
259 case VD_DISK_TYPE_SLICE:
260 break;
261
262 default:
263 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
264 vio->name, pkt->vdisk_type);
265 return -ECONNRESET;
266 }
267
268 if (pkt->vdisk_block_size > port->vdisk_block_size) {
269 printk(KERN_ERR PFX "%s: BLOCK size increased "
270 "%u --> %u\n",
271 vio->name,
272 port->vdisk_block_size, pkt->vdisk_block_size);
273 return -ECONNRESET;
274 }
275
276 port->operations = pkt->operations;
David S. Miller667ef3c2007-07-16 04:03:56 -0700277 port->vdisk_type = pkt->vdisk_type;
Allen Pais9bce2182014-09-19 09:42:14 -0400278 if (vdc_version_supported(port, 1, 1)) {
279 port->vdisk_size = pkt->vdisk_size;
280 port->vdisk_mtype = pkt->vdisk_mtype;
281 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700282 if (pkt->max_xfer_size < port->max_xfer_size)
283 port->max_xfer_size = pkt->max_xfer_size;
284 port->vdisk_block_size = pkt->vdisk_block_size;
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500285
286 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
287 if (vdc_version_supported(port, 1, 2))
288 port->vdisk_phys_blksz = pkt->phys_block_size;
289
David S. Miller667ef3c2007-07-16 04:03:56 -0700290 return 0;
291 } else {
292 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
293
294 return -ECONNRESET;
295 }
296}
297
298static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
299{
300 int err = desc->status;
301
302 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
303}
304
David S. Miller667ef3c2007-07-16 04:03:56 -0700305static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
306 unsigned int index)
307{
308 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
309 struct vdc_req_entry *rqe = &port->rq_arr[index];
310 struct request *req;
311
312 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
313 return;
314
315 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
316 desc->hdr.state = VIO_DESC_FREE;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500317 dr->cons = vio_dring_next(dr, index);
David S. Miller667ef3c2007-07-16 04:03:56 -0700318
319 req = rqe->req;
320 if (req == NULL) {
321 vdc_end_special(port, desc);
322 return;
323 }
324
325 rqe->req = NULL;
326
Jens Axboefa182a12018-10-15 08:54:23 -0600327 blk_mq_end_request(req, desc->status ? BLK_STS_IOERR : 0);
David S. Miller667ef3c2007-07-16 04:03:56 -0700328
Dwight Engen76e74bb2014-12-11 12:26:17 -0500329 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700330}
331
332static int vdc_ack(struct vdc_port *port, void *msgbuf)
333{
334 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
335 struct vio_dring_data *pkt = msgbuf;
336
337 if (unlikely(pkt->dring_ident != dr->ident ||
338 pkt->start_idx != pkt->end_idx ||
339 pkt->start_idx >= VDC_TX_RING_SIZE))
340 return 0;
341
342 vdc_end_one(port, dr, pkt->start_idx);
343
344 return 0;
345}
346
347static int vdc_nack(struct vdc_port *port, void *msgbuf)
348{
349 /* XXX Implement me XXX */
350 return 0;
351}
352
353static void vdc_event(void *arg, int event)
354{
355 struct vdc_port *port = arg;
356 struct vio_driver_state *vio = &port->vio;
357 unsigned long flags;
358 int err;
359
360 spin_lock_irqsave(&vio->lock, flags);
361
Dwight Engen76e74bb2014-12-11 12:26:17 -0500362 if (unlikely(event == LDC_EVENT_RESET)) {
David S. Miller667ef3c2007-07-16 04:03:56 -0700363 vio_link_state_change(vio, event);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500364 queue_work(sunvdc_wq, &port->ldc_reset_work);
365 goto out;
366 }
367
368 if (unlikely(event == LDC_EVENT_UP)) {
369 vio_link_state_change(vio, event);
370 goto out;
David S. Miller667ef3c2007-07-16 04:03:56 -0700371 }
372
373 if (unlikely(event != LDC_EVENT_DATA_READY)) {
Dwight Engen76e74bb2014-12-11 12:26:17 -0500374 pr_warn(PFX "Unexpected LDC event %d\n", event);
375 goto out;
David S. Miller667ef3c2007-07-16 04:03:56 -0700376 }
377
378 err = 0;
379 while (1) {
380 union {
381 struct vio_msg_tag tag;
382 u64 raw[8];
383 } msgbuf;
384
385 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
386 if (unlikely(err < 0)) {
387 if (err == -ECONNRESET)
388 vio_conn_reset(vio);
389 break;
390 }
391 if (err == 0)
392 break;
393 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
394 msgbuf.tag.type,
395 msgbuf.tag.stype,
396 msgbuf.tag.stype_env,
397 msgbuf.tag.sid);
398 err = vio_validate_sid(vio, &msgbuf.tag);
399 if (err < 0)
400 break;
401
402 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
403 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
404 err = vdc_ack(port, &msgbuf);
405 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
406 err = vdc_nack(port, &msgbuf);
407 else
408 err = vdc_handle_unknown(port, &msgbuf);
409 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
410 err = vio_control_pkt_engine(vio, &msgbuf);
411 } else {
412 err = vdc_handle_unknown(port, &msgbuf);
413 }
414 if (err < 0)
415 break;
416 }
417 if (err < 0)
418 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500419out:
David S. Miller667ef3c2007-07-16 04:03:56 -0700420 spin_unlock_irqrestore(&vio->lock, flags);
421}
422
423static int __vdc_tx_trigger(struct vdc_port *port)
424{
425 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
426 struct vio_dring_data hdr = {
427 .tag = {
428 .type = VIO_TYPE_DATA,
429 .stype = VIO_SUBTYPE_INFO,
430 .stype_env = VIO_DRING_DATA,
431 .sid = vio_send_sid(&port->vio),
432 },
433 .dring_ident = dr->ident,
434 .start_idx = dr->prod,
435 .end_idx = dr->prod,
436 };
437 int err, delay;
Young Xiaoa11f6ca2018-11-28 12:36:39 +0000438 int retries = 0;
David S. Miller667ef3c2007-07-16 04:03:56 -0700439
440 hdr.seq = dr->snd_nxt;
441 delay = 1;
442 do {
443 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
444 if (err > 0) {
445 dr->snd_nxt++;
446 break;
447 }
448 udelay(delay);
449 if ((delay <<= 1) > 128)
450 delay = 128;
Young Xiaoa11f6ca2018-11-28 12:36:39 +0000451 if (retries++ > VDC_MAX_RETRIES)
452 break;
David S. Miller667ef3c2007-07-16 04:03:56 -0700453 } while (err == -EAGAIN);
454
Dwight Engen76e74bb2014-12-11 12:26:17 -0500455 if (err == -ENOTCONN)
456 vdc_ldc_reset(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700457 return err;
458}
459
460static int __send_request(struct request *req)
461{
462 struct vdc_port *port = req->rq_disk->private_data;
463 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
Kees Cookff5d1a42018-10-08 08:46:51 -0700464 struct scatterlist sg[MAX_RING_COOKIES];
David S. Miller667ef3c2007-07-16 04:03:56 -0700465 struct vdc_req_entry *rqe;
466 struct vio_disk_desc *desc;
467 unsigned int map_perm;
468 int nsg, err, i;
469 u64 len;
470 u8 op;
471
Kees Cookff5d1a42018-10-08 08:46:51 -0700472 if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
473 return -EINVAL;
474
David S. Miller667ef3c2007-07-16 04:03:56 -0700475 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
476
477 if (rq_data_dir(req) == READ) {
478 map_perm |= LDC_MAP_W;
479 op = VD_OP_BREAD;
480 } else {
481 map_perm |= LDC_MAP_R;
482 op = VD_OP_BWRITE;
483 }
484
Jens Axboe45711f12007-10-22 21:19:53 +0200485 sg_init_table(sg, port->ring_cookies);
David S. Miller667ef3c2007-07-16 04:03:56 -0700486 nsg = blk_rq_map_sg(req->q, req, sg);
487
488 len = 0;
489 for (i = 0; i < nsg; i++)
490 len += sg[i].length;
491
David S. Miller667ef3c2007-07-16 04:03:56 -0700492 desc = vio_dring_cur(dr);
493
494 err = ldc_map_sg(port->vio.lp, sg, nsg,
495 desc->cookies, port->ring_cookies,
496 map_perm);
497 if (err < 0) {
498 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
499 return err;
500 }
501
502 rqe = &port->rq_arr[dr->prod];
503 rqe->req = req;
504
505 desc->hdr.ack = VIO_ACK_ENABLE;
506 desc->req_id = port->req_id;
507 desc->operation = op;
508 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
David S. Miller1bd4b282007-08-24 22:05:44 -0700509 desc->slice = 0xff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700510 } else {
511 desc->slice = 0;
512 }
513 desc->status = ~0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900514 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
David S. Miller667ef3c2007-07-16 04:03:56 -0700515 desc->size = len;
516 desc->ncookies = err;
517
518 /* This has to be a non-SMP write barrier because we are writing
519 * to memory which is shared with the peer LDOM.
520 */
521 wmb();
522 desc->hdr.state = VIO_DESC_READY;
523
524 err = __vdc_tx_trigger(port);
525 if (err < 0) {
526 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
527 } else {
528 port->req_id++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500529 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700530 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700531
532 return err;
533}
534
Jens Axboefa182a12018-10-15 08:54:23 -0600535static blk_status_t vdc_queue_rq(struct blk_mq_hw_ctx *hctx,
536 const struct blk_mq_queue_data *bd)
David S. Miller667ef3c2007-07-16 04:03:56 -0700537{
Jens Axboefa182a12018-10-15 08:54:23 -0600538 struct vdc_port *port = hctx->queue->queuedata;
539 struct vio_dring_state *dr;
540 unsigned long flags;
David S. Miller667ef3c2007-07-16 04:03:56 -0700541
Jens Axboefa182a12018-10-15 08:54:23 -0600542 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
Dwight Engend0aedcd2014-09-19 09:43:02 -0400543
Jens Axboefa182a12018-10-15 08:54:23 -0600544 blk_mq_start_request(bd->rq);
Dwight Engend0aedcd2014-09-19 09:43:02 -0400545
Jens Axboefa182a12018-10-15 08:54:23 -0600546 spin_lock_irqsave(&port->vio.lock, flags);
Dwight Engend0aedcd2014-09-19 09:43:02 -0400547
Jens Axboefa182a12018-10-15 08:54:23 -0600548 /*
549 * Doing drain, just end the request in error
550 */
551 if (unlikely(port->drain)) {
552 spin_unlock_irqrestore(&port->vio.lock, flags);
553 return BLK_STS_IOERR;
David S. Miller667ef3c2007-07-16 04:03:56 -0700554 }
Jens Axboefa182a12018-10-15 08:54:23 -0600555
556 if (unlikely(vdc_tx_dring_avail(dr) < 1)) {
557 spin_unlock_irqrestore(&port->vio.lock, flags);
558 blk_mq_stop_hw_queue(hctx);
559 return BLK_STS_DEV_RESOURCE;
560 }
561
562 if (__send_request(bd->rq) < 0) {
563 spin_unlock_irqrestore(&port->vio.lock, flags);
564 return BLK_STS_IOERR;
565 }
566
567 spin_unlock_irqrestore(&port->vio.lock, flags);
568 return BLK_STS_OK;
David S. Miller667ef3c2007-07-16 04:03:56 -0700569}
570
571static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
572{
573 struct vio_dring_state *dr;
574 struct vio_completion comp;
575 struct vio_disk_desc *desc;
576 unsigned int map_perm;
577 unsigned long flags;
578 int op_len, err;
579 void *req_buf;
580
David S. Millerf4d96052013-02-14 11:49:01 -0800581 if (!(((u64)1 << (u64)op) & port->operations))
David S. Miller667ef3c2007-07-16 04:03:56 -0700582 return -EOPNOTSUPP;
583
584 switch (op) {
585 case VD_OP_BREAD:
586 case VD_OP_BWRITE:
587 default:
588 return -EINVAL;
589
590 case VD_OP_FLUSH:
591 op_len = 0;
592 map_perm = 0;
593 break;
594
595 case VD_OP_GET_WCE:
596 op_len = sizeof(u32);
597 map_perm = LDC_MAP_W;
598 break;
599
600 case VD_OP_SET_WCE:
601 op_len = sizeof(u32);
602 map_perm = LDC_MAP_R;
603 break;
604
605 case VD_OP_GET_VTOC:
606 op_len = sizeof(struct vio_disk_vtoc);
607 map_perm = LDC_MAP_W;
608 break;
609
610 case VD_OP_SET_VTOC:
611 op_len = sizeof(struct vio_disk_vtoc);
612 map_perm = LDC_MAP_R;
613 break;
614
615 case VD_OP_GET_DISKGEOM:
616 op_len = sizeof(struct vio_disk_geom);
617 map_perm = LDC_MAP_W;
618 break;
619
620 case VD_OP_SET_DISKGEOM:
621 op_len = sizeof(struct vio_disk_geom);
622 map_perm = LDC_MAP_R;
623 break;
624
625 case VD_OP_SCSICMD:
626 op_len = 16;
627 map_perm = LDC_MAP_RW;
628 break;
629
630 case VD_OP_GET_DEVID:
631 op_len = sizeof(struct vio_disk_devid);
632 map_perm = LDC_MAP_W;
633 break;
634
635 case VD_OP_GET_EFI:
636 case VD_OP_SET_EFI:
637 return -EOPNOTSUPP;
zhengbinc15cc232019-11-28 11:09:31 +0800638 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700639
640 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
641
642 op_len = (op_len + 7) & ~7;
643 req_buf = kzalloc(op_len, GFP_KERNEL);
644 if (!req_buf)
645 return -ENOMEM;
646
647 if (len > op_len)
648 len = op_len;
649
650 if (map_perm & LDC_MAP_R)
651 memcpy(req_buf, buf, len);
652
653 spin_lock_irqsave(&port->vio.lock, flags);
654
655 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
656
657 /* XXX If we want to use this code generically we have to
658 * XXX handle TX ring exhaustion etc.
659 */
660 desc = vio_dring_cur(dr);
661
662 err = ldc_map_single(port->vio.lp, req_buf, op_len,
663 desc->cookies, port->ring_cookies,
664 map_perm);
665 if (err < 0) {
666 spin_unlock_irqrestore(&port->vio.lock, flags);
667 kfree(req_buf);
668 return err;
669 }
670
671 init_completion(&comp.com);
672 comp.waiting_for = WAITING_FOR_GEN_CMD;
673 port->vio.cmp = &comp;
674
675 desc->hdr.ack = VIO_ACK_ENABLE;
676 desc->req_id = port->req_id;
677 desc->operation = op;
678 desc->slice = 0;
679 desc->status = ~0;
680 desc->offset = 0;
681 desc->size = op_len;
682 desc->ncookies = err;
683
684 /* This has to be a non-SMP write barrier because we are writing
685 * to memory which is shared with the peer LDOM.
686 */
687 wmb();
688 desc->hdr.state = VIO_DESC_READY;
689
690 err = __vdc_tx_trigger(port);
691 if (err >= 0) {
692 port->req_id++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500693 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700694 spin_unlock_irqrestore(&port->vio.lock, flags);
695
696 wait_for_completion(&comp.com);
697 err = comp.err;
698 } else {
699 port->vio.cmp = NULL;
700 spin_unlock_irqrestore(&port->vio.lock, flags);
701 }
702
703 if (map_perm & LDC_MAP_W)
704 memcpy(buf, req_buf, len);
705
706 kfree(req_buf);
707
708 return err;
709}
710
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800711static int vdc_alloc_tx_ring(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700712{
713 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
714 unsigned long len, entry_size;
715 int ncookies;
716 void *dring;
717
718 entry_size = sizeof(struct vio_disk_desc) +
719 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
720 len = (VDC_TX_RING_SIZE * entry_size);
721
722 ncookies = VIO_MAX_RING_COOKIES;
723 dring = ldc_alloc_exp_dring(port->vio.lp, len,
724 dr->cookies, &ncookies,
725 (LDC_MAP_SHADOW |
726 LDC_MAP_DIRECT |
727 LDC_MAP_RW));
728 if (IS_ERR(dring))
729 return PTR_ERR(dring);
730
731 dr->base = dring;
732 dr->entry_size = entry_size;
733 dr->num_entries = VDC_TX_RING_SIZE;
734 dr->prod = dr->cons = 0;
735 dr->pending = VDC_TX_RING_SIZE;
736 dr->ncookies = ncookies;
737
738 return 0;
739}
740
741static void vdc_free_tx_ring(struct vdc_port *port)
742{
743 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
744
745 if (dr->base) {
746 ldc_free_exp_dring(port->vio.lp, dr->base,
747 (dr->entry_size * dr->num_entries),
748 dr->cookies, dr->ncookies);
749 dr->base = NULL;
750 dr->entry_size = 0;
751 dr->num_entries = 0;
752 dr->pending = 0;
753 dr->ncookies = 0;
754 }
755}
756
Dwight Engen76e74bb2014-12-11 12:26:17 -0500757static int vdc_port_up(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700758{
759 struct vio_completion comp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700760
761 init_completion(&comp.com);
762 comp.err = 0;
763 comp.waiting_for = WAITING_FOR_LINK_UP;
764 port->vio.cmp = &comp;
765
766 vio_port_up(&port->vio);
David S. Miller667ef3c2007-07-16 04:03:56 -0700767 wait_for_completion(&comp.com);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500768 return comp.err;
769}
770
771static void vdc_port_down(struct vdc_port *port)
772{
773 ldc_disconnect(port->vio.lp);
774 ldc_unbind(port->vio.lp);
775 vdc_free_tx_ring(port);
776 vio_ldc_free(&port->vio);
777}
778
Jens Axboefa182a12018-10-15 08:54:23 -0600779static const struct blk_mq_ops vdc_mq_ops = {
780 .queue_rq = vdc_queue_rq,
781};
782
Dwight Engen76e74bb2014-12-11 12:26:17 -0500783static int probe_disk(struct vdc_port *port)
784{
785 struct request_queue *q;
786 struct gendisk *g;
787 int err;
788
789 err = vdc_port_up(port);
790 if (err)
791 return err;
David S. Miller667ef3c2007-07-16 04:03:56 -0700792
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500793 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
794 * disk is reserved by another system.
795 */
796 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
797 return -ENODEV;
798
Allen Pais9bce2182014-09-19 09:42:14 -0400799 if (vdc_version_supported(port, 1, 1)) {
800 /* vdisk_size should be set during the handshake, if it wasn't
801 * then the underlying disk is reserved by another system
802 */
803 if (port->vdisk_size == -1)
804 return -ENODEV;
805 } else {
Allen Paisde5b73f2014-09-19 09:42:26 -0400806 struct vio_disk_geom geom;
807
Allen Pais9bce2182014-09-19 09:42:14 -0400808 err = generic_request(port, VD_OP_GET_DISKGEOM,
Allen Paisde5b73f2014-09-19 09:42:26 -0400809 &geom, sizeof(geom));
Allen Pais9bce2182014-09-19 09:42:14 -0400810 if (err < 0) {
811 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
812 "error %d\n", err);
813 return err;
814 }
Allen Paisde5b73f2014-09-19 09:42:26 -0400815 port->vdisk_size = ((u64)geom.num_cyl *
816 (u64)geom.num_hd *
817 (u64)geom.num_sec);
David S. Miller667ef3c2007-07-16 04:03:56 -0700818 }
819
Christoph Hellwigafea05a2021-06-02 09:53:28 +0300820 err = blk_mq_alloc_sq_tag_set(&port->tag_set, &vdc_mq_ops,
821 VDC_TX_RING_SIZE, BLK_MQ_F_SHOULD_MERGE);
822 if (err)
823 return err;
824
825 g = blk_mq_alloc_disk(&port->tag_set, port);
826 if (IS_ERR(g)) {
David S. Miller667ef3c2007-07-16 04:03:56 -0700827 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
828 port->vio.name);
Luis Chamberlainf583eae2021-11-03 16:04:31 -0700829 err = PTR_ERR(g);
830 goto out_free_tag;
David S. Miller667ef3c2007-07-16 04:03:56 -0700831 }
832
833 port->disk = g;
Christoph Hellwigafea05a2021-06-02 09:53:28 +0300834 q = g->queue;
David S. Miller667ef3c2007-07-16 04:03:56 -0700835
Dwight Engen5eed69f2014-09-19 09:42:53 -0400836 /* Each segment in a request is up to an aligned page in size. */
837 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
838 blk_queue_max_segment_size(q, PAGE_SIZE);
839
Martin K. Petersen8a783622010-02-26 00:20:39 -0500840 blk_queue_max_segments(q, port->ring_cookies);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500841 blk_queue_max_hw_sectors(q, port->max_xfer_size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700842 g->major = vdc_major;
David S. Miller91ba3c22007-07-18 15:15:45 -0700843 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
Christoph Hellwigafea05a2021-06-02 09:53:28 +0300844 g->minors = 1 << PARTITION_SHIFT;
David S. Miller667ef3c2007-07-16 04:03:56 -0700845 strcpy(g->disk_name, port->disk_name);
846
847 g->fops = &vdc_fops;
848 g->queue = q;
849 g->private_data = port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700850
851 set_capacity(g, port->vdisk_size);
852
Allen Pais9bce2182014-09-19 09:42:14 -0400853 if (vdc_version_supported(port, 1, 1)) {
854 switch (port->vdisk_mtype) {
855 case VD_MEDIA_TYPE_CD:
856 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
857 g->flags |= GENHD_FL_CD;
858 g->flags |= GENHD_FL_REMOVABLE;
859 set_disk_ro(g, 1);
860 break;
861
862 case VD_MEDIA_TYPE_DVD:
863 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
864 g->flags |= GENHD_FL_CD;
865 g->flags |= GENHD_FL_REMOVABLE;
866 set_disk_ro(g, 1);
867 break;
868
869 case VD_MEDIA_TYPE_FIXED:
870 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
871 break;
872 }
873 }
874
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500875 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
876
Allen Pais9bce2182014-09-19 09:42:14 -0400877 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700878 g->disk_name,
Allen Pais9bce2182014-09-19 09:42:14 -0400879 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
880 port->vio.ver.major, port->vio.ver.minor);
David S. Miller667ef3c2007-07-16 04:03:56 -0700881
Luis Chamberlainf583eae2021-11-03 16:04:31 -0700882 err = device_add_disk(&port->vio.vdev->dev, g, NULL);
883 if (err)
884 goto out_cleanup_disk;
David S. Miller667ef3c2007-07-16 04:03:56 -0700885
886 return 0;
Luis Chamberlainf583eae2021-11-03 16:04:31 -0700887
888out_cleanup_disk:
889 blk_cleanup_disk(g);
890out_free_tag:
891 blk_mq_free_tag_set(&port->tag_set);
892 return err;
David S. Miller667ef3c2007-07-16 04:03:56 -0700893}
894
895static struct ldc_channel_config vdc_ldc_cfg = {
896 .event = vdc_event,
897 .mtu = 64,
898 .mode = LDC_MODE_UNRELIABLE,
899};
900
901static struct vio_driver_ops vdc_vio_ops = {
902 .send_attr = vdc_send_attr,
903 .handle_attr = vdc_handle_attr,
904 .handshake_complete = vdc_handshake_complete,
905};
906
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800907static void print_version(void)
David S. Miller80dc35d2007-07-17 21:46:00 -0700908{
909 static int version_printed;
910
911 if (version_printed++ == 0)
912 printk(KERN_INFO "%s", version);
913}
914
Jim Quigley3ee70592017-07-21 09:20:15 -0400915struct vdc_check_port_data {
916 int dev_no;
917 char *type;
918};
919
920static int vdc_device_probed(struct device *dev, void *arg)
921{
922 struct vio_dev *vdev = to_vio_dev(dev);
923 struct vdc_check_port_data *port_data;
924
925 port_data = (struct vdc_check_port_data *)arg;
926
927 if ((vdev->dev_no == port_data->dev_no) &&
928 (!(strcmp((char *)&vdev->type, port_data->type))) &&
929 dev_get_drvdata(dev)) {
930 /* This device has already been configured
931 * by vdc_port_probe()
932 */
933 return 1;
934 } else {
935 return 0;
936 }
937}
938
939/* Determine whether the VIO device is part of an mpgroup
940 * by locating all the virtual-device-port nodes associated
941 * with the parent virtual-device node for the VIO device
942 * and checking whether any of these nodes are vdc-ports
943 * which have already been configured.
944 *
945 * Returns true if this device is part of an mpgroup and has
946 * already been probed.
947 */
948static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
949{
950 struct vdc_check_port_data port_data;
951 struct device *dev;
952
953 port_data.dev_no = vdev->dev_no;
954 port_data.type = (char *)&vdev->type;
955
956 dev = device_find_child(vdev->dev.parent, &port_data,
957 vdc_device_probed);
958
959 if (dev)
960 return true;
961
962 return false;
963}
964
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800965static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller667ef3c2007-07-16 04:03:56 -0700966{
David S. Miller43fdf272007-07-12 13:47:50 -0700967 struct mdesc_handle *hp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700968 struct vdc_port *port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700969 int err;
Dwight Engen76e74bb2014-12-11 12:26:17 -0500970 const u64 *ldc_timeout;
David S. Miller667ef3c2007-07-16 04:03:56 -0700971
David S. Miller80dc35d2007-07-17 21:46:00 -0700972 print_version();
David S. Miller667ef3c2007-07-16 04:03:56 -0700973
David S. Miller43fdf272007-07-12 13:47:50 -0700974 hp = mdesc_grab();
David S. Miller667ef3c2007-07-16 04:03:56 -0700975
David S. Miller43fdf272007-07-12 13:47:50 -0700976 err = -ENODEV;
David S. Miller91ba3c22007-07-18 15:15:45 -0700977 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800978 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
David S. Miller91ba3c22007-07-18 15:15:45 -0700979 vdev->dev_no);
David S. Miller43fdf272007-07-12 13:47:50 -0700980 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700981 }
982
Jim Quigley3ee70592017-07-21 09:20:15 -0400983 /* Check if this device is part of an mpgroup */
984 if (vdc_port_mpgroup_check(vdev)) {
985 printk(KERN_WARNING
986 "VIO: Ignoring extra vdisk port %s",
987 dev_name(&vdev->dev));
988 goto err_out_release_mdesc;
989 }
990
David S. Miller667ef3c2007-07-16 04:03:56 -0700991 port = kzalloc(sizeof(*port), GFP_KERNEL);
992 if (!port) {
Zhen Lei6597efa2021-06-09 20:23:27 +0800993 err = -ENOMEM;
David S. Miller43fdf272007-07-12 13:47:50 -0700994 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700995 }
996
David S. Miller91ba3c22007-07-18 15:15:45 -0700997 if (vdev->dev_no >= 26)
David S. Miller667ef3c2007-07-16 04:03:56 -0700998 snprintf(port->disk_name, sizeof(port->disk_name),
999 VDCBLK_NAME "%c%c",
David S. Miller91ba3c22007-07-18 15:15:45 -07001000 'a' + ((int)vdev->dev_no / 26) - 1,
1001 'a' + ((int)vdev->dev_no % 26));
David S. Miller667ef3c2007-07-16 04:03:56 -07001002 else
1003 snprintf(port->disk_name, sizeof(port->disk_name),
David S. Miller91ba3c22007-07-18 15:15:45 -07001004 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
Allen Pais9bce2182014-09-19 09:42:14 -04001005 port->vdisk_size = -1;
David S. Miller667ef3c2007-07-16 04:03:56 -07001006
Dwight Engen76e74bb2014-12-11 12:26:17 -05001007 /* Actual wall time may be double due to do_generic_file_read() doing
1008 * a readahead I/O first, and once that fails it will try to read a
1009 * single page.
1010 */
1011 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
1012 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
Jens Axboefa182a12018-10-15 08:54:23 -06001013 INIT_DELAYED_WORK(&port->ldc_reset_timer_work, vdc_ldc_reset_timer_work);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001014 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
1015
David S. Miller43fdf272007-07-12 13:47:50 -07001016 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
David S. Miller667ef3c2007-07-16 04:03:56 -07001017 vdc_versions, ARRAY_SIZE(vdc_versions),
1018 &vdc_vio_ops, port->disk_name);
1019 if (err)
1020 goto err_out_free_port;
1021
Liam R. Howlettf41e5462017-02-01 14:09:26 -05001022 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
Kees Cookff5d1a42018-10-08 08:46:51 -07001023 port->max_xfer_size = MAX_XFER_SIZE;
1024 port->ring_cookies = MAX_RING_COOKIES;
David S. Miller667ef3c2007-07-16 04:03:56 -07001025
1026 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1027 if (err)
1028 goto err_out_free_port;
1029
1030 err = vdc_alloc_tx_ring(port);
1031 if (err)
1032 goto err_out_free_ldc;
1033
1034 err = probe_disk(port);
1035 if (err)
1036 goto err_out_free_tx_ring;
1037
Jim Quigley3ee70592017-07-21 09:20:15 -04001038 /* Note that the device driver_data is used to determine
1039 * whether the port has been probed.
1040 */
David S. Miller667ef3c2007-07-16 04:03:56 -07001041 dev_set_drvdata(&vdev->dev, port);
1042
David S. Miller43fdf272007-07-12 13:47:50 -07001043 mdesc_release(hp);
1044
David S. Miller667ef3c2007-07-16 04:03:56 -07001045 return 0;
1046
1047err_out_free_tx_ring:
1048 vdc_free_tx_ring(port);
1049
1050err_out_free_ldc:
1051 vio_ldc_free(&port->vio);
1052
1053err_out_free_port:
1054 kfree(port);
1055
David S. Miller43fdf272007-07-12 13:47:50 -07001056err_out_release_mdesc:
1057 mdesc_release(hp);
David S. Miller667ef3c2007-07-16 04:03:56 -07001058 return err;
1059}
1060
Uwe Kleine-König15535732021-05-05 22:14:49 +02001061static void vdc_port_remove(struct vio_dev *vdev)
David S. Miller667ef3c2007-07-16 04:03:56 -07001062{
1063 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1064
1065 if (port) {
Jens Axboefa182a12018-10-15 08:54:23 -06001066 blk_mq_stop_hw_queues(port->disk->queue);
Dwight Engen31f48882014-12-11 12:25:42 -05001067
Dwight Engen76e74bb2014-12-11 12:26:17 -05001068 flush_work(&port->ldc_reset_work);
Jens Axboefa182a12018-10-15 08:54:23 -06001069 cancel_delayed_work_sync(&port->ldc_reset_timer_work);
David S. Miller667ef3c2007-07-16 04:03:56 -07001070 del_timer_sync(&port->vio.timer);
1071
Dwight Engen31f48882014-12-11 12:25:42 -05001072 del_gendisk(port->disk);
Christoph Hellwigafea05a2021-06-02 09:53:28 +03001073 blk_cleanup_disk(port->disk);
1074 blk_mq_free_tag_set(&port->tag_set);
Dwight Engen31f48882014-12-11 12:25:42 -05001075
David S. Miller667ef3c2007-07-16 04:03:56 -07001076 vdc_free_tx_ring(port);
1077 vio_ldc_free(&port->vio);
1078
1079 dev_set_drvdata(&vdev->dev, NULL);
1080
1081 kfree(port);
1082 }
David S. Miller667ef3c2007-07-16 04:03:56 -07001083}
1084
Dwight Engen76e74bb2014-12-11 12:26:17 -05001085static void vdc_requeue_inflight(struct vdc_port *port)
1086{
1087 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1088 u32 idx;
1089
1090 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1091 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1092 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1093 struct request *req;
1094
1095 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1096 desc->hdr.state = VIO_DESC_FREE;
1097 dr->cons = vio_dring_next(dr, idx);
1098
1099 req = rqe->req;
1100 if (req == NULL) {
1101 vdc_end_special(port, desc);
1102 continue;
1103 }
1104
1105 rqe->req = NULL;
Jens Axboefa182a12018-10-15 08:54:23 -06001106 blk_mq_requeue_request(req, false);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001107 }
1108}
1109
1110static void vdc_queue_drain(struct vdc_port *port)
1111{
Jens Axboefa182a12018-10-15 08:54:23 -06001112 struct request_queue *q = port->disk->queue;
Dwight Engen76e74bb2014-12-11 12:26:17 -05001113
Jens Axboefa182a12018-10-15 08:54:23 -06001114 /*
1115 * Mark the queue as draining, then freeze/quiesce to ensure
1116 * that all existing requests are seen in ->queue_rq() and killed
1117 */
1118 port->drain = 1;
1119 spin_unlock_irq(&port->vio.lock);
1120
1121 blk_mq_freeze_queue(q);
1122 blk_mq_quiesce_queue(q);
1123
1124 spin_lock_irq(&port->vio.lock);
1125 port->drain = 0;
1126 blk_mq_unquiesce_queue(q);
1127 blk_mq_unfreeze_queue(q);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001128}
1129
Jens Axboefa182a12018-10-15 08:54:23 -06001130static void vdc_ldc_reset_timer_work(struct work_struct *work)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001131{
Jens Axboefa182a12018-10-15 08:54:23 -06001132 struct vdc_port *port;
1133 struct vio_driver_state *vio;
Dwight Engen76e74bb2014-12-11 12:26:17 -05001134
Jens Axboefa182a12018-10-15 08:54:23 -06001135 port = container_of(work, struct vdc_port, ldc_reset_timer_work.work);
1136 vio = &port->vio;
1137
1138 spin_lock_irq(&vio->lock);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001139 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1140 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1141 port->disk_name, port->ldc_timeout);
1142 vdc_queue_drain(port);
1143 vdc_blk_queue_start(port);
1144 }
Jens Axboefa182a12018-10-15 08:54:23 -06001145 spin_unlock_irq(&vio->lock);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001146}
1147
1148static void vdc_ldc_reset_work(struct work_struct *work)
1149{
1150 struct vdc_port *port;
1151 struct vio_driver_state *vio;
1152 unsigned long flags;
1153
1154 port = container_of(work, struct vdc_port, ldc_reset_work);
1155 vio = &port->vio;
1156
1157 spin_lock_irqsave(&vio->lock, flags);
1158 vdc_ldc_reset(port);
1159 spin_unlock_irqrestore(&vio->lock, flags);
1160}
1161
1162static void vdc_ldc_reset(struct vdc_port *port)
1163{
1164 int err;
1165
1166 assert_spin_locked(&port->vio.lock);
1167
1168 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
Jens Axboefa182a12018-10-15 08:54:23 -06001169 blk_mq_stop_hw_queues(port->disk->queue);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001170 vdc_requeue_inflight(port);
1171 vdc_port_down(port);
1172
1173 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1174 if (err) {
1175 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1176 return;
1177 }
1178
1179 err = vdc_alloc_tx_ring(port);
1180 if (err) {
1181 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1182 goto err_free_ldc;
1183 }
1184
1185 if (port->ldc_timeout)
Jens Axboefa182a12018-10-15 08:54:23 -06001186 mod_delayed_work(system_wq, &port->ldc_reset_timer_work,
Dwight Engen76e74bb2014-12-11 12:26:17 -05001187 round_jiffies(jiffies + HZ * port->ldc_timeout));
1188 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1189 return;
1190
1191err_free_ldc:
1192 vio_ldc_free(&port->vio);
1193}
1194
David S. Miller3d452e52008-09-01 01:48:52 -07001195static const struct vio_device_id vdc_port_match[] = {
David S. Miller667ef3c2007-07-16 04:03:56 -07001196 {
1197 .type = "vdc-port",
1198 },
1199 {},
1200};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -07001201MODULE_DEVICE_TABLE(vio, vdc_port_match);
David S. Miller667ef3c2007-07-16 04:03:56 -07001202
1203static struct vio_driver vdc_port_driver = {
1204 .id_table = vdc_port_match,
1205 .probe = vdc_port_probe,
1206 .remove = vdc_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001207 .name = "vdc_port",
David S. Miller667ef3c2007-07-16 04:03:56 -07001208};
1209
David S. Miller667ef3c2007-07-16 04:03:56 -07001210static int __init vdc_init(void)
1211{
1212 int err;
1213
Dwight Engen76e74bb2014-12-11 12:26:17 -05001214 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1215 if (!sunvdc_wq)
1216 return -ENOMEM;
1217
David S. Miller667ef3c2007-07-16 04:03:56 -07001218 err = register_blkdev(0, VDCBLK_NAME);
1219 if (err < 0)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001220 goto out_free_wq;
David S. Miller667ef3c2007-07-16 04:03:56 -07001221
1222 vdc_major = err;
David S. Miller667ef3c2007-07-16 04:03:56 -07001223
1224 err = vio_register_driver(&vdc_port_driver);
1225 if (err)
David S. Miller80dc35d2007-07-17 21:46:00 -07001226 goto out_unregister_blkdev;
David S. Miller667ef3c2007-07-16 04:03:56 -07001227
1228 return 0;
1229
David S. Miller667ef3c2007-07-16 04:03:56 -07001230out_unregister_blkdev:
1231 unregister_blkdev(vdc_major, VDCBLK_NAME);
1232 vdc_major = 0;
1233
Dwight Engen76e74bb2014-12-11 12:26:17 -05001234out_free_wq:
1235 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001236 return err;
1237}
1238
1239static void __exit vdc_exit(void)
1240{
1241 vio_unregister_driver(&vdc_port_driver);
David S. Miller667ef3c2007-07-16 04:03:56 -07001242 unregister_blkdev(vdc_major, VDCBLK_NAME);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001243 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001244}
1245
1246module_init(vdc_init);
1247module_exit(vdc_exit);