blob: 5d7024057540aa154854686c3b48337d4226db5b [file] [log] [blame]
David S. Miller667ef3c2007-07-16 04:03:56 -07001/* sunvdc.c: Sun LDOM Virtual Disk Client.
2 *
David S. Miller3d452e52008-09-01 01:48:52 -07003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller667ef3c2007-07-16 04:03:56 -07004 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/blkdev.h>
10#include <linux/hdreg.h>
11#include <linux/genhd.h>
Allen Pais9bce2182014-09-19 09:42:14 -040012#include <linux/cdrom.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070013#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/completion.h>
16#include <linux/delay.h>
17#include <linux/init.h>
18#include <linux/list.h>
David Millerd91c5e82007-10-24 08:46:01 +020019#include <linux/scatterlist.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070020
21#include <asm/vio.h>
22#include <asm/ldc.h>
23
24#define DRV_MODULE_NAME "sunvdc"
25#define PFX DRV_MODULE_NAME ": "
Dwight Engen76e74bb2014-12-11 12:26:17 -050026#define DRV_MODULE_VERSION "1.2"
27#define DRV_MODULE_RELDATE "November 24, 2014"
David S. Miller667ef3c2007-07-16 04:03:56 -070028
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -080029static char version[] =
David S. Miller667ef3c2007-07-16 04:03:56 -070030 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33MODULE_LICENSE("GPL");
34MODULE_VERSION(DRV_MODULE_VERSION);
35
Dwight Engend0aedcd2014-09-19 09:43:02 -040036#define VDC_TX_RING_SIZE 512
Liam R. Howlettf41e5462017-02-01 14:09:26 -050037#define VDC_DEFAULT_BLK_SIZE 512
David S. Miller667ef3c2007-07-16 04:03:56 -070038
Kees Cookff5d1a42018-10-08 08:46:51 -070039#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
David S. Miller667ef3c2007-07-16 04:03:56 -070043#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
Young Xiao3fbba4e2018-11-28 12:36:39 +000048#define VDC_MAX_RETRIES 10
49
Dwight Engen76e74bb2014-12-11 12:26:17 -050050static struct workqueue_struct *sunvdc_wq;
51
David S. Miller667ef3c2007-07-16 04:03:56 -070052struct vdc_req_entry {
53 struct request *req;
54};
55
56struct vdc_port {
57 struct vio_driver_state vio;
58
David S. Miller667ef3c2007-07-16 04:03:56 -070059 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
Dwight Engen76e74bb2014-12-11 12:26:17 -050072 u64 ldc_timeout;
73 struct timer_list ldc_reset_timer;
74 struct work_struct ldc_reset_work;
75
David S. Miller667ef3c2007-07-16 04:03:56 -070076 /* The server fills these in for us in the disk attribute
77 * ACK packet.
78 */
79 u64 operations;
80 u32 vdisk_size;
81 u8 vdisk_type;
Allen Pais9bce2182014-09-19 09:42:14 -040082 u8 vdisk_mtype;
Liam R. Howlettf41e5462017-02-01 14:09:26 -050083 u32 vdisk_phys_blksz;
David S. Miller667ef3c2007-07-16 04:03:56 -070084
85 char disk_name[32];
David S. Miller667ef3c2007-07-16 04:03:56 -070086};
87
Dwight Engen76e74bb2014-12-11 12:26:17 -050088static void vdc_ldc_reset(struct vdc_port *port);
89static void vdc_ldc_reset_work(struct work_struct *work);
Kees Cooke99e88a2017-10-16 14:43:17 -070090static void vdc_ldc_reset_timer(struct timer_list *t);
Dwight Engen76e74bb2014-12-11 12:26:17 -050091
David S. Miller667ef3c2007-07-16 04:03:56 -070092static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
93{
94 return container_of(vio, struct vdc_port, vio);
95}
96
David S. Miller667ef3c2007-07-16 04:03:56 -070097/* Ordered from largest major to lowest */
98static struct vio_version vdc_versions[] = {
Liam R. Howlettf41e5462017-02-01 14:09:26 -050099 { .major = 1, .minor = 2 },
Allen Pais9bce2182014-09-19 09:42:14 -0400100 { .major = 1, .minor = 1 },
David S. Miller667ef3c2007-07-16 04:03:56 -0700101 { .major = 1, .minor = 0 },
102};
103
Allen Pais9bce2182014-09-19 09:42:14 -0400104static inline int vdc_version_supported(struct vdc_port *port,
105 u16 major, u16 minor)
106{
107 return port->vio.ver.major == major && port->vio.ver.minor >= minor;
108}
109
David S. Miller667ef3c2007-07-16 04:03:56 -0700110#define VDCBLK_NAME "vdisk"
111static int vdc_major;
112#define PARTITION_SHIFT 3
113
114static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
115{
116 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
117}
118
119static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
120{
121 struct gendisk *disk = bdev->bd_disk;
Allen Paisde5b73f2014-09-19 09:42:26 -0400122 sector_t nsect = get_capacity(disk);
123 sector_t cylinders = nsect;
David S. Miller667ef3c2007-07-16 04:03:56 -0700124
Allen Paisde5b73f2014-09-19 09:42:26 -0400125 geo->heads = 0xff;
126 geo->sectors = 0x3f;
127 sector_div(cylinders, geo->heads * geo->sectors);
128 geo->cylinders = cylinders;
129 if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
130 geo->cylinders = 0xffff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700131
132 return 0;
133}
134
Allen Pais9bce2182014-09-19 09:42:14 -0400135/* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
136 * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
137 * Needed to be able to install inside an ldom from an iso image.
138 */
139static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
140 unsigned command, unsigned long argument)
141{
142 int i;
143 struct gendisk *disk;
144
145 switch (command) {
146 case CDROMMULTISESSION:
147 pr_debug(PFX "Multisession CDs not supported\n");
148 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
149 if (put_user(0, (char __user *)(argument + i)))
150 return -EFAULT;
151 return 0;
152
153 case CDROM_GET_CAPABILITY:
154 disk = bdev->bd_disk;
155
156 if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
157 return 0;
158 return -EINVAL;
159
160 default:
161 pr_debug(PFX "ioctl %08x not supported\n", command);
162 return -EINVAL;
163 }
164}
165
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700166static const struct block_device_operations vdc_fops = {
David S. Miller667ef3c2007-07-16 04:03:56 -0700167 .owner = THIS_MODULE,
168 .getgeo = vdc_getgeo,
Allen Pais9bce2182014-09-19 09:42:14 -0400169 .ioctl = vdc_ioctl,
David S. Miller667ef3c2007-07-16 04:03:56 -0700170};
171
Dwight Engen76e74bb2014-12-11 12:26:17 -0500172static void vdc_blk_queue_start(struct vdc_port *port)
173{
174 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
175
176 /* restart blk queue when ring is half emptied. also called after
177 * handshake completes, so check for initial handshake before we've
178 * allocated a disk.
179 */
180 if (port->disk && blk_queue_stopped(port->disk->queue) &&
181 vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
182 blk_start_queue(port->disk->queue);
183 }
184
185}
186
David S. Miller667ef3c2007-07-16 04:03:56 -0700187static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
188{
189 if (vio->cmp &&
190 (waiting_for == -1 ||
191 vio->cmp->waiting_for == waiting_for)) {
192 vio->cmp->err = err;
193 complete(&vio->cmp->com);
194 vio->cmp = NULL;
195 }
196}
197
198static void vdc_handshake_complete(struct vio_driver_state *vio)
199{
Dwight Engen76e74bb2014-12-11 12:26:17 -0500200 struct vdc_port *port = to_vdc_port(vio);
201
202 del_timer(&port->ldc_reset_timer);
David S. Miller667ef3c2007-07-16 04:03:56 -0700203 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500204 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700205}
206
207static int vdc_handle_unknown(struct vdc_port *port, void *arg)
208{
209 struct vio_msg_tag *pkt = arg;
210
211 printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
212 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
213 printk(KERN_ERR PFX "Resetting connection.\n");
214
215 ldc_disconnect(port->vio.lp);
216
217 return -ECONNRESET;
218}
219
220static int vdc_send_attr(struct vio_driver_state *vio)
221{
222 struct vdc_port *port = to_vdc_port(vio);
223 struct vio_disk_attr_info pkt;
224
225 memset(&pkt, 0, sizeof(pkt));
226
227 pkt.tag.type = VIO_TYPE_CTRL;
228 pkt.tag.stype = VIO_SUBTYPE_INFO;
229 pkt.tag.stype_env = VIO_ATTR_INFO;
230 pkt.tag.sid = vio_send_sid(vio);
231
232 pkt.xfer_mode = VIO_DRING_MODE;
233 pkt.vdisk_block_size = port->vdisk_block_size;
234 pkt.max_xfer_size = port->max_xfer_size;
235
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800236 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700237 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
238
239 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
240}
241
242static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
243{
244 struct vdc_port *port = to_vdc_port(vio);
245 struct vio_disk_attr_info *pkt = arg;
246
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800247 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
Allen Pais9bce2182014-09-19 09:42:14 -0400248 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700249 pkt->tag.stype, pkt->operations,
Allen Pais9bce2182014-09-19 09:42:14 -0400250 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
David S. Miller667ef3c2007-07-16 04:03:56 -0700251 pkt->xfer_mode, pkt->vdisk_block_size,
252 pkt->max_xfer_size);
253
254 if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
255 switch (pkt->vdisk_type) {
256 case VD_DISK_TYPE_DISK:
257 case VD_DISK_TYPE_SLICE:
258 break;
259
260 default:
261 printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
262 vio->name, pkt->vdisk_type);
263 return -ECONNRESET;
264 }
265
266 if (pkt->vdisk_block_size > port->vdisk_block_size) {
267 printk(KERN_ERR PFX "%s: BLOCK size increased "
268 "%u --> %u\n",
269 vio->name,
270 port->vdisk_block_size, pkt->vdisk_block_size);
271 return -ECONNRESET;
272 }
273
274 port->operations = pkt->operations;
David S. Miller667ef3c2007-07-16 04:03:56 -0700275 port->vdisk_type = pkt->vdisk_type;
Allen Pais9bce2182014-09-19 09:42:14 -0400276 if (vdc_version_supported(port, 1, 1)) {
277 port->vdisk_size = pkt->vdisk_size;
278 port->vdisk_mtype = pkt->vdisk_mtype;
279 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700280 if (pkt->max_xfer_size < port->max_xfer_size)
281 port->max_xfer_size = pkt->max_xfer_size;
282 port->vdisk_block_size = pkt->vdisk_block_size;
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500283
284 port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
285 if (vdc_version_supported(port, 1, 2))
286 port->vdisk_phys_blksz = pkt->phys_block_size;
287
David S. Miller667ef3c2007-07-16 04:03:56 -0700288 return 0;
289 } else {
290 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
291
292 return -ECONNRESET;
293 }
294}
295
296static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
297{
298 int err = desc->status;
299
300 vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
301}
302
David S. Miller667ef3c2007-07-16 04:03:56 -0700303static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
304 unsigned int index)
305{
306 struct vio_disk_desc *desc = vio_dring_entry(dr, index);
307 struct vdc_req_entry *rqe = &port->rq_arr[index];
308 struct request *req;
309
310 if (unlikely(desc->hdr.state != VIO_DESC_DONE))
311 return;
312
313 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
314 desc->hdr.state = VIO_DESC_FREE;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500315 dr->cons = vio_dring_next(dr, index);
David S. Miller667ef3c2007-07-16 04:03:56 -0700316
317 req = rqe->req;
318 if (req == NULL) {
319 vdc_end_special(port, desc);
320 return;
321 }
322
323 rqe->req = NULL;
324
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200325 __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700326
Dwight Engen76e74bb2014-12-11 12:26:17 -0500327 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700328}
329
330static int vdc_ack(struct vdc_port *port, void *msgbuf)
331{
332 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
333 struct vio_dring_data *pkt = msgbuf;
334
335 if (unlikely(pkt->dring_ident != dr->ident ||
336 pkt->start_idx != pkt->end_idx ||
337 pkt->start_idx >= VDC_TX_RING_SIZE))
338 return 0;
339
340 vdc_end_one(port, dr, pkt->start_idx);
341
342 return 0;
343}
344
345static int vdc_nack(struct vdc_port *port, void *msgbuf)
346{
347 /* XXX Implement me XXX */
348 return 0;
349}
350
351static void vdc_event(void *arg, int event)
352{
353 struct vdc_port *port = arg;
354 struct vio_driver_state *vio = &port->vio;
355 unsigned long flags;
356 int err;
357
358 spin_lock_irqsave(&vio->lock, flags);
359
Dwight Engen76e74bb2014-12-11 12:26:17 -0500360 if (unlikely(event == LDC_EVENT_RESET)) {
David S. Miller667ef3c2007-07-16 04:03:56 -0700361 vio_link_state_change(vio, event);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500362 queue_work(sunvdc_wq, &port->ldc_reset_work);
363 goto out;
364 }
365
366 if (unlikely(event == LDC_EVENT_UP)) {
367 vio_link_state_change(vio, event);
368 goto out;
David S. Miller667ef3c2007-07-16 04:03:56 -0700369 }
370
371 if (unlikely(event != LDC_EVENT_DATA_READY)) {
Dwight Engen76e74bb2014-12-11 12:26:17 -0500372 pr_warn(PFX "Unexpected LDC event %d\n", event);
373 goto out;
David S. Miller667ef3c2007-07-16 04:03:56 -0700374 }
375
376 err = 0;
377 while (1) {
378 union {
379 struct vio_msg_tag tag;
380 u64 raw[8];
381 } msgbuf;
382
383 err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
384 if (unlikely(err < 0)) {
385 if (err == -ECONNRESET)
386 vio_conn_reset(vio);
387 break;
388 }
389 if (err == 0)
390 break;
391 viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
392 msgbuf.tag.type,
393 msgbuf.tag.stype,
394 msgbuf.tag.stype_env,
395 msgbuf.tag.sid);
396 err = vio_validate_sid(vio, &msgbuf.tag);
397 if (err < 0)
398 break;
399
400 if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
401 if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
402 err = vdc_ack(port, &msgbuf);
403 else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
404 err = vdc_nack(port, &msgbuf);
405 else
406 err = vdc_handle_unknown(port, &msgbuf);
407 } else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
408 err = vio_control_pkt_engine(vio, &msgbuf);
409 } else {
410 err = vdc_handle_unknown(port, &msgbuf);
411 }
412 if (err < 0)
413 break;
414 }
415 if (err < 0)
416 vdc_finish(&port->vio, err, WAITING_FOR_ANY);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500417out:
David S. Miller667ef3c2007-07-16 04:03:56 -0700418 spin_unlock_irqrestore(&vio->lock, flags);
419}
420
421static int __vdc_tx_trigger(struct vdc_port *port)
422{
423 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
424 struct vio_dring_data hdr = {
425 .tag = {
426 .type = VIO_TYPE_DATA,
427 .stype = VIO_SUBTYPE_INFO,
428 .stype_env = VIO_DRING_DATA,
429 .sid = vio_send_sid(&port->vio),
430 },
431 .dring_ident = dr->ident,
432 .start_idx = dr->prod,
433 .end_idx = dr->prod,
434 };
435 int err, delay;
Young Xiao3fbba4e2018-11-28 12:36:39 +0000436 int retries = 0;
David S. Miller667ef3c2007-07-16 04:03:56 -0700437
438 hdr.seq = dr->snd_nxt;
439 delay = 1;
440 do {
441 err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
442 if (err > 0) {
443 dr->snd_nxt++;
444 break;
445 }
446 udelay(delay);
447 if ((delay <<= 1) > 128)
448 delay = 128;
Young Xiao3fbba4e2018-11-28 12:36:39 +0000449 if (retries++ > VDC_MAX_RETRIES)
450 break;
David S. Miller667ef3c2007-07-16 04:03:56 -0700451 } while (err == -EAGAIN);
452
Dwight Engen76e74bb2014-12-11 12:26:17 -0500453 if (err == -ENOTCONN)
454 vdc_ldc_reset(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700455 return err;
456}
457
458static int __send_request(struct request *req)
459{
460 struct vdc_port *port = req->rq_disk->private_data;
461 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
Kees Cookff5d1a42018-10-08 08:46:51 -0700462 struct scatterlist sg[MAX_RING_COOKIES];
David S. Miller667ef3c2007-07-16 04:03:56 -0700463 struct vdc_req_entry *rqe;
464 struct vio_disk_desc *desc;
465 unsigned int map_perm;
466 int nsg, err, i;
467 u64 len;
468 u8 op;
469
Kees Cookff5d1a42018-10-08 08:46:51 -0700470 if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
471 return -EINVAL;
472
David S. Miller667ef3c2007-07-16 04:03:56 -0700473 map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
474
475 if (rq_data_dir(req) == READ) {
476 map_perm |= LDC_MAP_W;
477 op = VD_OP_BREAD;
478 } else {
479 map_perm |= LDC_MAP_R;
480 op = VD_OP_BWRITE;
481 }
482
Jens Axboe45711f12007-10-22 21:19:53 +0200483 sg_init_table(sg, port->ring_cookies);
David S. Miller667ef3c2007-07-16 04:03:56 -0700484 nsg = blk_rq_map_sg(req->q, req, sg);
485
486 len = 0;
487 for (i = 0; i < nsg; i++)
488 len += sg[i].length;
489
David S. Miller667ef3c2007-07-16 04:03:56 -0700490 desc = vio_dring_cur(dr);
491
492 err = ldc_map_sg(port->vio.lp, sg, nsg,
493 desc->cookies, port->ring_cookies,
494 map_perm);
495 if (err < 0) {
496 printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
497 return err;
498 }
499
500 rqe = &port->rq_arr[dr->prod];
501 rqe->req = req;
502
503 desc->hdr.ack = VIO_ACK_ENABLE;
504 desc->req_id = port->req_id;
505 desc->operation = op;
506 if (port->vdisk_type == VD_DISK_TYPE_DISK) {
David S. Miller1bd4b282007-08-24 22:05:44 -0700507 desc->slice = 0xff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700508 } else {
509 desc->slice = 0;
510 }
511 desc->status = ~0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900512 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
David S. Miller667ef3c2007-07-16 04:03:56 -0700513 desc->size = len;
514 desc->ncookies = err;
515
516 /* This has to be a non-SMP write barrier because we are writing
517 * to memory which is shared with the peer LDOM.
518 */
519 wmb();
520 desc->hdr.state = VIO_DESC_READY;
521
522 err = __vdc_tx_trigger(port);
523 if (err < 0) {
524 printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
525 } else {
526 port->req_id++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500527 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700528 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700529
530 return err;
531}
532
Dwight Engend0aedcd2014-09-19 09:43:02 -0400533static void do_vdc_request(struct request_queue *rq)
David S. Miller667ef3c2007-07-16 04:03:56 -0700534{
Dwight Engend0aedcd2014-09-19 09:43:02 -0400535 struct request *req;
David S. Miller667ef3c2007-07-16 04:03:56 -0700536
Dwight Engend0aedcd2014-09-19 09:43:02 -0400537 while ((req = blk_peek_request(rq)) != NULL) {
538 struct vdc_port *port;
539 struct vio_dring_state *dr;
540
541 port = req->rq_disk->private_data;
542 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
543 if (unlikely(vdc_tx_dring_avail(dr) < 1))
544 goto wait;
545
546 blk_start_request(req);
547
548 if (__send_request(req) < 0) {
549 blk_requeue_request(rq, req);
550wait:
551 /* Avoid pointless unplugs. */
552 blk_stop_queue(rq);
David S. Miller667ef3c2007-07-16 04:03:56 -0700553 break;
Dwight Engend0aedcd2014-09-19 09:43:02 -0400554 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700555 }
556}
557
558static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
559{
560 struct vio_dring_state *dr;
561 struct vio_completion comp;
562 struct vio_disk_desc *desc;
563 unsigned int map_perm;
564 unsigned long flags;
565 int op_len, err;
566 void *req_buf;
567
David S. Millerf4d96052013-02-14 11:49:01 -0800568 if (!(((u64)1 << (u64)op) & port->operations))
David S. Miller667ef3c2007-07-16 04:03:56 -0700569 return -EOPNOTSUPP;
570
571 switch (op) {
572 case VD_OP_BREAD:
573 case VD_OP_BWRITE:
574 default:
575 return -EINVAL;
576
577 case VD_OP_FLUSH:
578 op_len = 0;
579 map_perm = 0;
580 break;
581
582 case VD_OP_GET_WCE:
583 op_len = sizeof(u32);
584 map_perm = LDC_MAP_W;
585 break;
586
587 case VD_OP_SET_WCE:
588 op_len = sizeof(u32);
589 map_perm = LDC_MAP_R;
590 break;
591
592 case VD_OP_GET_VTOC:
593 op_len = sizeof(struct vio_disk_vtoc);
594 map_perm = LDC_MAP_W;
595 break;
596
597 case VD_OP_SET_VTOC:
598 op_len = sizeof(struct vio_disk_vtoc);
599 map_perm = LDC_MAP_R;
600 break;
601
602 case VD_OP_GET_DISKGEOM:
603 op_len = sizeof(struct vio_disk_geom);
604 map_perm = LDC_MAP_W;
605 break;
606
607 case VD_OP_SET_DISKGEOM:
608 op_len = sizeof(struct vio_disk_geom);
609 map_perm = LDC_MAP_R;
610 break;
611
612 case VD_OP_SCSICMD:
613 op_len = 16;
614 map_perm = LDC_MAP_RW;
615 break;
616
617 case VD_OP_GET_DEVID:
618 op_len = sizeof(struct vio_disk_devid);
619 map_perm = LDC_MAP_W;
620 break;
621
622 case VD_OP_GET_EFI:
623 case VD_OP_SET_EFI:
624 return -EOPNOTSUPP;
625 break;
626 };
627
628 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
629
630 op_len = (op_len + 7) & ~7;
631 req_buf = kzalloc(op_len, GFP_KERNEL);
632 if (!req_buf)
633 return -ENOMEM;
634
635 if (len > op_len)
636 len = op_len;
637
638 if (map_perm & LDC_MAP_R)
639 memcpy(req_buf, buf, len);
640
641 spin_lock_irqsave(&port->vio.lock, flags);
642
643 dr = &port->vio.drings[VIO_DRIVER_TX_RING];
644
645 /* XXX If we want to use this code generically we have to
646 * XXX handle TX ring exhaustion etc.
647 */
648 desc = vio_dring_cur(dr);
649
650 err = ldc_map_single(port->vio.lp, req_buf, op_len,
651 desc->cookies, port->ring_cookies,
652 map_perm);
653 if (err < 0) {
654 spin_unlock_irqrestore(&port->vio.lock, flags);
655 kfree(req_buf);
656 return err;
657 }
658
659 init_completion(&comp.com);
660 comp.waiting_for = WAITING_FOR_GEN_CMD;
661 port->vio.cmp = &comp;
662
663 desc->hdr.ack = VIO_ACK_ENABLE;
664 desc->req_id = port->req_id;
665 desc->operation = op;
666 desc->slice = 0;
667 desc->status = ~0;
668 desc->offset = 0;
669 desc->size = op_len;
670 desc->ncookies = err;
671
672 /* This has to be a non-SMP write barrier because we are writing
673 * to memory which is shared with the peer LDOM.
674 */
675 wmb();
676 desc->hdr.state = VIO_DESC_READY;
677
678 err = __vdc_tx_trigger(port);
679 if (err >= 0) {
680 port->req_id++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500681 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700682 spin_unlock_irqrestore(&port->vio.lock, flags);
683
684 wait_for_completion(&comp.com);
685 err = comp.err;
686 } else {
687 port->vio.cmp = NULL;
688 spin_unlock_irqrestore(&port->vio.lock, flags);
689 }
690
691 if (map_perm & LDC_MAP_W)
692 memcpy(buf, req_buf, len);
693
694 kfree(req_buf);
695
696 return err;
697}
698
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800699static int vdc_alloc_tx_ring(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700700{
701 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
702 unsigned long len, entry_size;
703 int ncookies;
704 void *dring;
705
706 entry_size = sizeof(struct vio_disk_desc) +
707 (sizeof(struct ldc_trans_cookie) * port->ring_cookies);
708 len = (VDC_TX_RING_SIZE * entry_size);
709
710 ncookies = VIO_MAX_RING_COOKIES;
711 dring = ldc_alloc_exp_dring(port->vio.lp, len,
712 dr->cookies, &ncookies,
713 (LDC_MAP_SHADOW |
714 LDC_MAP_DIRECT |
715 LDC_MAP_RW));
716 if (IS_ERR(dring))
717 return PTR_ERR(dring);
718
719 dr->base = dring;
720 dr->entry_size = entry_size;
721 dr->num_entries = VDC_TX_RING_SIZE;
722 dr->prod = dr->cons = 0;
723 dr->pending = VDC_TX_RING_SIZE;
724 dr->ncookies = ncookies;
725
726 return 0;
727}
728
729static void vdc_free_tx_ring(struct vdc_port *port)
730{
731 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
732
733 if (dr->base) {
734 ldc_free_exp_dring(port->vio.lp, dr->base,
735 (dr->entry_size * dr->num_entries),
736 dr->cookies, dr->ncookies);
737 dr->base = NULL;
738 dr->entry_size = 0;
739 dr->num_entries = 0;
740 dr->pending = 0;
741 dr->ncookies = 0;
742 }
743}
744
Dwight Engen76e74bb2014-12-11 12:26:17 -0500745static int vdc_port_up(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700746{
747 struct vio_completion comp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700748
749 init_completion(&comp.com);
750 comp.err = 0;
751 comp.waiting_for = WAITING_FOR_LINK_UP;
752 port->vio.cmp = &comp;
753
754 vio_port_up(&port->vio);
David S. Miller667ef3c2007-07-16 04:03:56 -0700755 wait_for_completion(&comp.com);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500756 return comp.err;
757}
758
759static void vdc_port_down(struct vdc_port *port)
760{
761 ldc_disconnect(port->vio.lp);
762 ldc_unbind(port->vio.lp);
763 vdc_free_tx_ring(port);
764 vio_ldc_free(&port->vio);
765}
766
767static int probe_disk(struct vdc_port *port)
768{
769 struct request_queue *q;
770 struct gendisk *g;
771 int err;
772
773 err = vdc_port_up(port);
774 if (err)
775 return err;
David S. Miller667ef3c2007-07-16 04:03:56 -0700776
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500777 /* Using version 1.2 means vdisk_phys_blksz should be set unless the
778 * disk is reserved by another system.
779 */
780 if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
781 return -ENODEV;
782
Allen Pais9bce2182014-09-19 09:42:14 -0400783 if (vdc_version_supported(port, 1, 1)) {
784 /* vdisk_size should be set during the handshake, if it wasn't
785 * then the underlying disk is reserved by another system
786 */
787 if (port->vdisk_size == -1)
788 return -ENODEV;
789 } else {
Allen Paisde5b73f2014-09-19 09:42:26 -0400790 struct vio_disk_geom geom;
791
Allen Pais9bce2182014-09-19 09:42:14 -0400792 err = generic_request(port, VD_OP_GET_DISKGEOM,
Allen Paisde5b73f2014-09-19 09:42:26 -0400793 &geom, sizeof(geom));
Allen Pais9bce2182014-09-19 09:42:14 -0400794 if (err < 0) {
795 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
796 "error %d\n", err);
797 return err;
798 }
Allen Paisde5b73f2014-09-19 09:42:26 -0400799 port->vdisk_size = ((u64)geom.num_cyl *
800 (u64)geom.num_hd *
801 (u64)geom.num_sec);
David S. Miller667ef3c2007-07-16 04:03:56 -0700802 }
803
David S. Miller667ef3c2007-07-16 04:03:56 -0700804 q = blk_init_queue(do_vdc_request, &port->vio.lock);
805 if (!q) {
806 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
807 port->vio.name);
808 return -ENOMEM;
809 }
810 g = alloc_disk(1 << PARTITION_SHIFT);
811 if (!g) {
812 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
813 port->vio.name);
814 blk_cleanup_queue(q);
815 return -ENOMEM;
816 }
817
818 port->disk = g;
819
Dwight Engen5eed69f2014-09-19 09:42:53 -0400820 /* Each segment in a request is up to an aligned page in size. */
821 blk_queue_segment_boundary(q, PAGE_SIZE - 1);
822 blk_queue_max_segment_size(q, PAGE_SIZE);
823
Martin K. Petersen8a783622010-02-26 00:20:39 -0500824 blk_queue_max_segments(q, port->ring_cookies);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500825 blk_queue_max_hw_sectors(q, port->max_xfer_size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700826 g->major = vdc_major;
David S. Miller91ba3c22007-07-18 15:15:45 -0700827 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
David S. Miller667ef3c2007-07-16 04:03:56 -0700828 strcpy(g->disk_name, port->disk_name);
829
830 g->fops = &vdc_fops;
831 g->queue = q;
832 g->private_data = port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700833
834 set_capacity(g, port->vdisk_size);
835
Allen Pais9bce2182014-09-19 09:42:14 -0400836 if (vdc_version_supported(port, 1, 1)) {
837 switch (port->vdisk_mtype) {
838 case VD_MEDIA_TYPE_CD:
839 pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
840 g->flags |= GENHD_FL_CD;
841 g->flags |= GENHD_FL_REMOVABLE;
842 set_disk_ro(g, 1);
843 break;
844
845 case VD_MEDIA_TYPE_DVD:
846 pr_info(PFX "Virtual DVD %s\n", port->disk_name);
847 g->flags |= GENHD_FL_CD;
848 g->flags |= GENHD_FL_REMOVABLE;
849 set_disk_ro(g, 1);
850 break;
851
852 case VD_MEDIA_TYPE_FIXED:
853 pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
854 break;
855 }
856 }
857
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500858 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
859
Allen Pais9bce2182014-09-19 09:42:14 -0400860 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700861 g->disk_name,
Allen Pais9bce2182014-09-19 09:42:14 -0400862 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
863 port->vio.ver.major, port->vio.ver.minor);
David S. Miller667ef3c2007-07-16 04:03:56 -0700864
Dan Williams0d52c7562016-06-15 19:44:20 -0700865 device_add_disk(&port->vio.vdev->dev, g);
David S. Miller667ef3c2007-07-16 04:03:56 -0700866
867 return 0;
868}
869
870static struct ldc_channel_config vdc_ldc_cfg = {
871 .event = vdc_event,
872 .mtu = 64,
873 .mode = LDC_MODE_UNRELIABLE,
874};
875
876static struct vio_driver_ops vdc_vio_ops = {
877 .send_attr = vdc_send_attr,
878 .handle_attr = vdc_handle_attr,
879 .handshake_complete = vdc_handshake_complete,
880};
881
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800882static void print_version(void)
David S. Miller80dc35d2007-07-17 21:46:00 -0700883{
884 static int version_printed;
885
886 if (version_printed++ == 0)
887 printk(KERN_INFO "%s", version);
888}
889
Jim Quigley3ee70592017-07-21 09:20:15 -0400890struct vdc_check_port_data {
891 int dev_no;
892 char *type;
893};
894
895static int vdc_device_probed(struct device *dev, void *arg)
896{
897 struct vio_dev *vdev = to_vio_dev(dev);
898 struct vdc_check_port_data *port_data;
899
900 port_data = (struct vdc_check_port_data *)arg;
901
902 if ((vdev->dev_no == port_data->dev_no) &&
903 (!(strcmp((char *)&vdev->type, port_data->type))) &&
904 dev_get_drvdata(dev)) {
905 /* This device has already been configured
906 * by vdc_port_probe()
907 */
908 return 1;
909 } else {
910 return 0;
911 }
912}
913
914/* Determine whether the VIO device is part of an mpgroup
915 * by locating all the virtual-device-port nodes associated
916 * with the parent virtual-device node for the VIO device
917 * and checking whether any of these nodes are vdc-ports
918 * which have already been configured.
919 *
920 * Returns true if this device is part of an mpgroup and has
921 * already been probed.
922 */
923static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
924{
925 struct vdc_check_port_data port_data;
926 struct device *dev;
927
928 port_data.dev_no = vdev->dev_no;
929 port_data.type = (char *)&vdev->type;
930
931 dev = device_find_child(vdev->dev.parent, &port_data,
932 vdc_device_probed);
933
934 if (dev)
935 return true;
936
937 return false;
938}
939
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800940static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller667ef3c2007-07-16 04:03:56 -0700941{
David S. Miller43fdf272007-07-12 13:47:50 -0700942 struct mdesc_handle *hp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700943 struct vdc_port *port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700944 int err;
Dwight Engen76e74bb2014-12-11 12:26:17 -0500945 const u64 *ldc_timeout;
David S. Miller667ef3c2007-07-16 04:03:56 -0700946
David S. Miller80dc35d2007-07-17 21:46:00 -0700947 print_version();
David S. Miller667ef3c2007-07-16 04:03:56 -0700948
David S. Miller43fdf272007-07-12 13:47:50 -0700949 hp = mdesc_grab();
David S. Miller667ef3c2007-07-16 04:03:56 -0700950
David S. Miller43fdf272007-07-12 13:47:50 -0700951 err = -ENODEV;
David S. Miller91ba3c22007-07-18 15:15:45 -0700952 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800953 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
David S. Miller91ba3c22007-07-18 15:15:45 -0700954 vdev->dev_no);
David S. Miller43fdf272007-07-12 13:47:50 -0700955 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700956 }
957
Jim Quigley3ee70592017-07-21 09:20:15 -0400958 /* Check if this device is part of an mpgroup */
959 if (vdc_port_mpgroup_check(vdev)) {
960 printk(KERN_WARNING
961 "VIO: Ignoring extra vdisk port %s",
962 dev_name(&vdev->dev));
963 goto err_out_release_mdesc;
964 }
965
David S. Miller667ef3c2007-07-16 04:03:56 -0700966 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700967 err = -ENOMEM;
David S. Miller667ef3c2007-07-16 04:03:56 -0700968 if (!port) {
969 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
David S. Miller43fdf272007-07-12 13:47:50 -0700970 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700971 }
972
David S. Miller91ba3c22007-07-18 15:15:45 -0700973 if (vdev->dev_no >= 26)
David S. Miller667ef3c2007-07-16 04:03:56 -0700974 snprintf(port->disk_name, sizeof(port->disk_name),
975 VDCBLK_NAME "%c%c",
David S. Miller91ba3c22007-07-18 15:15:45 -0700976 'a' + ((int)vdev->dev_no / 26) - 1,
977 'a' + ((int)vdev->dev_no % 26));
David S. Miller667ef3c2007-07-16 04:03:56 -0700978 else
979 snprintf(port->disk_name, sizeof(port->disk_name),
David S. Miller91ba3c22007-07-18 15:15:45 -0700980 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
Allen Pais9bce2182014-09-19 09:42:14 -0400981 port->vdisk_size = -1;
David S. Miller667ef3c2007-07-16 04:03:56 -0700982
Dwight Engen76e74bb2014-12-11 12:26:17 -0500983 /* Actual wall time may be double due to do_generic_file_read() doing
984 * a readahead I/O first, and once that fails it will try to read a
985 * single page.
986 */
987 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
988 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
Kees Cooke99e88a2017-10-16 14:43:17 -0700989 timer_setup(&port->ldc_reset_timer, vdc_ldc_reset_timer, 0);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500990 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
991
David S. Miller43fdf272007-07-12 13:47:50 -0700992 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
David S. Miller667ef3c2007-07-16 04:03:56 -0700993 vdc_versions, ARRAY_SIZE(vdc_versions),
994 &vdc_vio_ops, port->disk_name);
995 if (err)
996 goto err_out_free_port;
997
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500998 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
Kees Cookff5d1a42018-10-08 08:46:51 -0700999 port->max_xfer_size = MAX_XFER_SIZE;
1000 port->ring_cookies = MAX_RING_COOKIES;
David S. Miller667ef3c2007-07-16 04:03:56 -07001001
1002 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1003 if (err)
1004 goto err_out_free_port;
1005
1006 err = vdc_alloc_tx_ring(port);
1007 if (err)
1008 goto err_out_free_ldc;
1009
1010 err = probe_disk(port);
1011 if (err)
1012 goto err_out_free_tx_ring;
1013
Jim Quigley3ee70592017-07-21 09:20:15 -04001014 /* Note that the device driver_data is used to determine
1015 * whether the port has been probed.
1016 */
David S. Miller667ef3c2007-07-16 04:03:56 -07001017 dev_set_drvdata(&vdev->dev, port);
1018
David S. Miller43fdf272007-07-12 13:47:50 -07001019 mdesc_release(hp);
1020
David S. Miller667ef3c2007-07-16 04:03:56 -07001021 return 0;
1022
1023err_out_free_tx_ring:
1024 vdc_free_tx_ring(port);
1025
1026err_out_free_ldc:
1027 vio_ldc_free(&port->vio);
1028
1029err_out_free_port:
1030 kfree(port);
1031
David S. Miller43fdf272007-07-12 13:47:50 -07001032err_out_release_mdesc:
1033 mdesc_release(hp);
David S. Miller667ef3c2007-07-16 04:03:56 -07001034 return err;
1035}
1036
1037static int vdc_port_remove(struct vio_dev *vdev)
1038{
1039 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1040
1041 if (port) {
Dwight Engen31f48882014-12-11 12:25:42 -05001042 unsigned long flags;
1043
1044 spin_lock_irqsave(&port->vio.lock, flags);
1045 blk_stop_queue(port->disk->queue);
1046 spin_unlock_irqrestore(&port->vio.lock, flags);
1047
Dwight Engen76e74bb2014-12-11 12:26:17 -05001048 flush_work(&port->ldc_reset_work);
1049 del_timer_sync(&port->ldc_reset_timer);
David S. Miller667ef3c2007-07-16 04:03:56 -07001050 del_timer_sync(&port->vio.timer);
1051
Dwight Engen31f48882014-12-11 12:25:42 -05001052 del_gendisk(port->disk);
1053 blk_cleanup_queue(port->disk->queue);
1054 put_disk(port->disk);
1055 port->disk = NULL;
1056
David S. Miller667ef3c2007-07-16 04:03:56 -07001057 vdc_free_tx_ring(port);
1058 vio_ldc_free(&port->vio);
1059
1060 dev_set_drvdata(&vdev->dev, NULL);
1061
1062 kfree(port);
1063 }
1064 return 0;
1065}
1066
Dwight Engen76e74bb2014-12-11 12:26:17 -05001067static void vdc_requeue_inflight(struct vdc_port *port)
1068{
1069 struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1070 u32 idx;
1071
1072 for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1073 struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1074 struct vdc_req_entry *rqe = &port->rq_arr[idx];
1075 struct request *req;
1076
1077 ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1078 desc->hdr.state = VIO_DESC_FREE;
1079 dr->cons = vio_dring_next(dr, idx);
1080
1081 req = rqe->req;
1082 if (req == NULL) {
1083 vdc_end_special(port, desc);
1084 continue;
1085 }
1086
1087 rqe->req = NULL;
1088 blk_requeue_request(port->disk->queue, req);
1089 }
1090}
1091
1092static void vdc_queue_drain(struct vdc_port *port)
1093{
1094 struct request *req;
1095
1096 while ((req = blk_fetch_request(port->disk->queue)) != NULL)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001097 __blk_end_request_all(req, BLK_STS_IOERR);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001098}
1099
Kees Cooke99e88a2017-10-16 14:43:17 -07001100static void vdc_ldc_reset_timer(struct timer_list *t)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001101{
Kees Cooke99e88a2017-10-16 14:43:17 -07001102 struct vdc_port *port = from_timer(port, t, ldc_reset_timer);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001103 struct vio_driver_state *vio = &port->vio;
1104 unsigned long flags;
1105
1106 spin_lock_irqsave(&vio->lock, flags);
1107 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1108 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1109 port->disk_name, port->ldc_timeout);
1110 vdc_queue_drain(port);
1111 vdc_blk_queue_start(port);
1112 }
1113 spin_unlock_irqrestore(&vio->lock, flags);
1114}
1115
1116static void vdc_ldc_reset_work(struct work_struct *work)
1117{
1118 struct vdc_port *port;
1119 struct vio_driver_state *vio;
1120 unsigned long flags;
1121
1122 port = container_of(work, struct vdc_port, ldc_reset_work);
1123 vio = &port->vio;
1124
1125 spin_lock_irqsave(&vio->lock, flags);
1126 vdc_ldc_reset(port);
1127 spin_unlock_irqrestore(&vio->lock, flags);
1128}
1129
1130static void vdc_ldc_reset(struct vdc_port *port)
1131{
1132 int err;
1133
1134 assert_spin_locked(&port->vio.lock);
1135
1136 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1137 blk_stop_queue(port->disk->queue);
1138 vdc_requeue_inflight(port);
1139 vdc_port_down(port);
1140
1141 err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1142 if (err) {
1143 pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1144 return;
1145 }
1146
1147 err = vdc_alloc_tx_ring(port);
1148 if (err) {
1149 pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1150 goto err_free_ldc;
1151 }
1152
1153 if (port->ldc_timeout)
1154 mod_timer(&port->ldc_reset_timer,
1155 round_jiffies(jiffies + HZ * port->ldc_timeout));
1156 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1157 return;
1158
1159err_free_ldc:
1160 vio_ldc_free(&port->vio);
1161}
1162
David S. Miller3d452e52008-09-01 01:48:52 -07001163static const struct vio_device_id vdc_port_match[] = {
David S. Miller667ef3c2007-07-16 04:03:56 -07001164 {
1165 .type = "vdc-port",
1166 },
1167 {},
1168};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -07001169MODULE_DEVICE_TABLE(vio, vdc_port_match);
David S. Miller667ef3c2007-07-16 04:03:56 -07001170
1171static struct vio_driver vdc_port_driver = {
1172 .id_table = vdc_port_match,
1173 .probe = vdc_port_probe,
1174 .remove = vdc_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001175 .name = "vdc_port",
David S. Miller667ef3c2007-07-16 04:03:56 -07001176};
1177
David S. Miller667ef3c2007-07-16 04:03:56 -07001178static int __init vdc_init(void)
1179{
1180 int err;
1181
Dwight Engen76e74bb2014-12-11 12:26:17 -05001182 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1183 if (!sunvdc_wq)
1184 return -ENOMEM;
1185
David S. Miller667ef3c2007-07-16 04:03:56 -07001186 err = register_blkdev(0, VDCBLK_NAME);
1187 if (err < 0)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001188 goto out_free_wq;
David S. Miller667ef3c2007-07-16 04:03:56 -07001189
1190 vdc_major = err;
David S. Miller667ef3c2007-07-16 04:03:56 -07001191
1192 err = vio_register_driver(&vdc_port_driver);
1193 if (err)
David S. Miller80dc35d2007-07-17 21:46:00 -07001194 goto out_unregister_blkdev;
David S. Miller667ef3c2007-07-16 04:03:56 -07001195
1196 return 0;
1197
David S. Miller667ef3c2007-07-16 04:03:56 -07001198out_unregister_blkdev:
1199 unregister_blkdev(vdc_major, VDCBLK_NAME);
1200 vdc_major = 0;
1201
Dwight Engen76e74bb2014-12-11 12:26:17 -05001202out_free_wq:
1203 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001204 return err;
1205}
1206
1207static void __exit vdc_exit(void)
1208{
1209 vio_unregister_driver(&vdc_port_driver);
David S. Miller667ef3c2007-07-16 04:03:56 -07001210 unregister_blkdev(vdc_major, VDCBLK_NAME);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001211 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001212}
1213
1214module_init(vdc_init);
1215module_exit(vdc_exit);