hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/drivers/block/sunvdc.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* sunvdc.c: Sun LDOM Virtual Disk Client.
23 *
34 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
....@@ -6,7 +7,7 @@
67 #include <linux/module.h>
78 #include <linux/kernel.h>
89 #include <linux/types.h>
9
-#include <linux/blkdev.h>
10
+#include <linux/blk-mq.h>
1011 #include <linux/hdreg.h>
1112 #include <linux/genhd.h>
1213 #include <linux/cdrom.h>
....@@ -68,9 +69,10 @@
6869
6970 u64 max_xfer_size;
7071 u32 vdisk_block_size;
72
+ u32 drain;
7173
7274 u64 ldc_timeout;
73
- struct timer_list ldc_reset_timer;
75
+ struct delayed_work ldc_reset_timer_work;
7476 struct work_struct ldc_reset_work;
7577
7678 /* The server fills these in for us in the disk attribute
....@@ -82,12 +84,14 @@
8284 u8 vdisk_mtype;
8385 u32 vdisk_phys_blksz;
8486
87
+ struct blk_mq_tag_set tag_set;
88
+
8589 char disk_name[32];
8690 };
8791
8892 static void vdc_ldc_reset(struct vdc_port *port);
8993 static void vdc_ldc_reset_work(struct work_struct *work);
90
-static void vdc_ldc_reset_timer(struct timer_list *t);
94
+static void vdc_ldc_reset_timer_work(struct work_struct *work);
9195
9296 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
9397 {
....@@ -167,6 +171,7 @@
167171 .owner = THIS_MODULE,
168172 .getgeo = vdc_getgeo,
169173 .ioctl = vdc_ioctl,
174
+ .compat_ioctl = blkdev_compat_ptr_ioctl,
170175 };
171176
172177 static void vdc_blk_queue_start(struct vdc_port *port)
....@@ -177,11 +182,8 @@
177182 * handshake completes, so check for initial handshake before we've
178183 * allocated a disk.
179184 */
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
+ if (port->disk && vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50)
186
+ blk_mq_start_stopped_hw_queues(port->disk->queue, true);
185187 }
186188
187189 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
....@@ -199,7 +201,7 @@
199201 {
200202 struct vdc_port *port = to_vdc_port(vio);
201203
202
- del_timer(&port->ldc_reset_timer);
204
+ cancel_delayed_work(&port->ldc_reset_timer_work);
203205 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
204206 vdc_blk_queue_start(port);
205207 }
....@@ -322,7 +324,7 @@
322324
323325 rqe->req = NULL;
324326
325
- __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
327
+ blk_mq_end_request(req, desc->status ? BLK_STS_IOERR : 0);
326328
327329 vdc_blk_queue_start(port);
328330 }
....@@ -530,29 +532,40 @@
530532 return err;
531533 }
532534
533
-static void do_vdc_request(struct request_queue *rq)
535
+static blk_status_t vdc_queue_rq(struct blk_mq_hw_ctx *hctx,
536
+ const struct blk_mq_queue_data *bd)
534537 {
535
- struct request *req;
538
+ struct vdc_port *port = hctx->queue->queuedata;
539
+ struct vio_dring_state *dr;
540
+ unsigned long flags;
536541
537
- while ((req = blk_peek_request(rq)) != NULL) {
538
- struct vdc_port *port;
539
- struct vio_dring_state *dr;
542
+ dr = &port->vio.drings[VIO_DRIVER_TX_RING];
540543
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;
544
+ blk_mq_start_request(bd->rq);
545545
546
- blk_start_request(req);
546
+ spin_lock_irqsave(&port->vio.lock, flags);
547547
548
- if (__send_request(req) < 0) {
549
- blk_requeue_request(rq, req);
550
-wait:
551
- /* Avoid pointless unplugs. */
552
- blk_stop_queue(rq);
553
- break;
554
- }
548
+ /*
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;
555554 }
555
+
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;
556569 }
557570
558571 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
....@@ -622,8 +635,7 @@
622635 case VD_OP_GET_EFI:
623636 case VD_OP_SET_EFI:
624637 return -EOPNOTSUPP;
625
- break;
626
- };
638
+ }
627639
628640 map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
629641
....@@ -764,6 +776,31 @@
764776 vio_ldc_free(&port->vio);
765777 }
766778
779
+static const struct blk_mq_ops vdc_mq_ops = {
780
+ .queue_rq = vdc_queue_rq,
781
+};
782
+
783
+static void cleanup_queue(struct request_queue *q)
784
+{
785
+ struct vdc_port *port = q->queuedata;
786
+
787
+ blk_cleanup_queue(q);
788
+ blk_mq_free_tag_set(&port->tag_set);
789
+}
790
+
791
+static struct request_queue *init_queue(struct vdc_port *port)
792
+{
793
+ struct request_queue *q;
794
+
795
+ q = blk_mq_init_sq_queue(&port->tag_set, &vdc_mq_ops, VDC_TX_RING_SIZE,
796
+ BLK_MQ_F_SHOULD_MERGE);
797
+ if (IS_ERR(q))
798
+ return q;
799
+
800
+ q->queuedata = port;
801
+ return q;
802
+}
803
+
767804 static int probe_disk(struct vdc_port *port)
768805 {
769806 struct request_queue *q;
....@@ -801,17 +838,17 @@
801838 (u64)geom.num_sec);
802839 }
803840
804
- q = blk_init_queue(do_vdc_request, &port->vio.lock);
805
- if (!q) {
841
+ q = init_queue(port);
842
+ if (IS_ERR(q)) {
806843 printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
807844 port->vio.name);
808
- return -ENOMEM;
845
+ return PTR_ERR(q);
809846 }
810847 g = alloc_disk(1 << PARTITION_SHIFT);
811848 if (!g) {
812849 printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
813850 port->vio.name);
814
- blk_cleanup_queue(q);
851
+ cleanup_queue(q);
815852 return -ENOMEM;
816853 }
817854
....@@ -862,7 +899,7 @@
862899 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
863900 port->vio.ver.major, port->vio.ver.minor);
864901
865
- device_add_disk(&port->vio.vdev->dev, g);
902
+ device_add_disk(&port->vio.vdev->dev, g, NULL);
866903
867904 return 0;
868905 }
....@@ -947,6 +984,8 @@
947984 print_version();
948985
949986 hp = mdesc_grab();
987
+ if (!hp)
988
+ return -ENODEV;
950989
951990 err = -ENODEV;
952991 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
....@@ -986,7 +1025,7 @@
9861025 */
9871026 ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
9881027 port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
989
- timer_setup(&port->ldc_reset_timer, vdc_ldc_reset_timer, 0);
1028
+ INIT_DELAYED_WORK(&port->ldc_reset_timer_work, vdc_ldc_reset_timer_work);
9901029 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
9911030
9921031 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
....@@ -1039,18 +1078,14 @@
10391078 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
10401079
10411080 if (port) {
1042
- 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);
1081
+ blk_mq_stop_hw_queues(port->disk->queue);
10471082
10481083 flush_work(&port->ldc_reset_work);
1049
- del_timer_sync(&port->ldc_reset_timer);
1084
+ cancel_delayed_work_sync(&port->ldc_reset_timer_work);
10501085 del_timer_sync(&port->vio.timer);
10511086
10521087 del_gendisk(port->disk);
1053
- blk_cleanup_queue(port->disk->queue);
1088
+ cleanup_queue(port->disk->queue);
10541089 put_disk(port->disk);
10551090 port->disk = NULL;
10561091
....@@ -1085,32 +1120,46 @@
10851120 }
10861121
10871122 rqe->req = NULL;
1088
- blk_requeue_request(port->disk->queue, req);
1123
+ blk_mq_requeue_request(req, false);
10891124 }
10901125 }
10911126
10921127 static void vdc_queue_drain(struct vdc_port *port)
10931128 {
1094
- struct request *req;
1129
+ struct request_queue *q = port->disk->queue;
10951130
1096
- while ((req = blk_fetch_request(port->disk->queue)) != NULL)
1097
- __blk_end_request_all(req, BLK_STS_IOERR);
1131
+ /*
1132
+ * Mark the queue as draining, then freeze/quiesce to ensure
1133
+ * that all existing requests are seen in ->queue_rq() and killed
1134
+ */
1135
+ port->drain = 1;
1136
+ spin_unlock_irq(&port->vio.lock);
1137
+
1138
+ blk_mq_freeze_queue(q);
1139
+ blk_mq_quiesce_queue(q);
1140
+
1141
+ spin_lock_irq(&port->vio.lock);
1142
+ port->drain = 0;
1143
+ blk_mq_unquiesce_queue(q);
1144
+ blk_mq_unfreeze_queue(q);
10981145 }
10991146
1100
-static void vdc_ldc_reset_timer(struct timer_list *t)
1147
+static void vdc_ldc_reset_timer_work(struct work_struct *work)
11011148 {
1102
- struct vdc_port *port = from_timer(port, t, ldc_reset_timer);
1103
- struct vio_driver_state *vio = &port->vio;
1104
- unsigned long flags;
1149
+ struct vdc_port *port;
1150
+ struct vio_driver_state *vio;
11051151
1106
- spin_lock_irqsave(&vio->lock, flags);
1152
+ port = container_of(work, struct vdc_port, ldc_reset_timer_work.work);
1153
+ vio = &port->vio;
1154
+
1155
+ spin_lock_irq(&vio->lock);
11071156 if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
11081157 pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
11091158 port->disk_name, port->ldc_timeout);
11101159 vdc_queue_drain(port);
11111160 vdc_blk_queue_start(port);
11121161 }
1113
- spin_unlock_irqrestore(&vio->lock, flags);
1162
+ spin_unlock_irq(&vio->lock);
11141163 }
11151164
11161165 static void vdc_ldc_reset_work(struct work_struct *work)
....@@ -1134,7 +1183,7 @@
11341183 assert_spin_locked(&port->vio.lock);
11351184
11361185 pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1137
- blk_stop_queue(port->disk->queue);
1186
+ blk_mq_stop_hw_queues(port->disk->queue);
11381187 vdc_requeue_inflight(port);
11391188 vdc_port_down(port);
11401189
....@@ -1151,7 +1200,7 @@
11511200 }
11521201
11531202 if (port->ldc_timeout)
1154
- mod_timer(&port->ldc_reset_timer,
1203
+ mod_delayed_work(system_wq, &port->ldc_reset_timer_work,
11551204 round_jiffies(jiffies + HZ * port->ldc_timeout));
11561205 mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
11571206 return;