hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/memstick/core/ms_block.c
....@@ -1,21 +1,17 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * ms_block.c - Sony MemoryStick (legacy) storage support
34
45 * Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com>
56 *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
9
- *
107 * Minor portions of the driver were copied from mspro_block.c which is
118 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
12
- *
139 */
1410 #define DRIVER_NAME "ms_block"
1511 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
1612
1713 #include <linux/module.h>
18
-#include <linux/blkdev.h>
14
+#include <linux/blk-mq.h>
1915 #include <linux/memstick.h>
2016 #include <linux/idr.h>
2117 #include <linux/hdreg.h>
....@@ -375,7 +371,7 @@
375371 serial mode), then just fall through */
376372 if (msb_read_int_reg(msb, -1))
377373 return 0;
378
- /* fallthrough */
374
+ fallthrough;
379375
380376 case MSB_RP_RECEIVE_INT_REQ_RESULT:
381377 intreg = mrq->data[0];
....@@ -407,7 +403,7 @@
407403 case MSB_RP_RECEIVE_STATUS_REG:
408404 msb->regs.status = *(struct ms_status_register *)mrq->data;
409405 msb->state = MSB_RP_SEND_OOB_READ;
410
- /* fallthrough */
406
+ fallthrough;
411407
412408 case MSB_RP_SEND_OOB_READ:
413409 if (!msb_read_regs(msb,
....@@ -422,7 +418,7 @@
422418 msb->regs.extra_data =
423419 *(struct ms_extra_data_register *) mrq->data;
424420 msb->state = MSB_RP_SEND_READ_DATA;
425
- /* fallthrough */
421
+ fallthrough;
426422
427423 case MSB_RP_SEND_READ_DATA:
428424 /* Skip that state if we only read the oob */
....@@ -522,7 +518,7 @@
522518 msb->state = MSB_WB_RECEIVE_INT_REQ;
523519 if (msb_read_int_reg(msb, -1))
524520 return 0;
525
- /* fallthrough */
521
+ fallthrough;
526522
527523 case MSB_WB_RECEIVE_INT_REQ:
528524 intreg = mrq->data[0];
....@@ -553,7 +549,7 @@
553549
554550 msb->int_polling = false;
555551 msb->state = MSB_WB_SEND_WRITE_DATA;
556
- /* fallthrough */
552
+ fallthrough;
557553
558554 case MSB_WB_SEND_WRITE_DATA:
559555 sg_init_table(sg, ARRAY_SIZE(sg));
....@@ -632,7 +628,7 @@
632628 msb->state = MSB_SC_RECEIVE_INT_REQ;
633629 if (msb_read_int_reg(msb, -1))
634630 return 0;
635
- /* fallthrough */
631
+ fallthrough;
636632
637633 case MSB_SC_RECEIVE_INT_REQ:
638634 intreg = mrq->data[0];
....@@ -1091,7 +1087,7 @@
10911087
10921088 pos %= msb->free_block_count[zone];
10931089
1094
- dbg_verbose("have %d choices for a free block, selected randomally: %d",
1090
+ dbg_verbose("have %d choices for a free block, selected randomly: %d",
10951091 msb->free_block_count[zone], pos);
10961092
10971093 pba = find_next_zero_bit(msb->used_blocks_bitmap,
....@@ -1227,7 +1223,7 @@
12271223 }
12281224
12291225 if (be16_to_cpu(page->header.block_id) != MS_BLOCK_BOOT_ID) {
1230
- dbg("the pba at %d doesn' contain boot block ID", pba);
1226
+ dbg("the pba at %d doesn't contain boot block ID", pba);
12311227 continue;
12321228 }
12331229
....@@ -1339,17 +1335,17 @@
13391335 msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
13401336 msb->logical_block_count = msb->zone_count * 496 - 2;
13411337
1342
- msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
1343
- msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
1338
+ msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
1339
+ msb->erased_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
13441340 msb->lba_to_pba_table =
13451341 kmalloc_array(msb->logical_block_count, sizeof(u16),
13461342 GFP_KERNEL);
13471343
13481344 if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
13491345 !msb->erased_blocks_bitmap) {
1350
- kfree(msb->used_blocks_bitmap);
1346
+ bitmap_free(msb->used_blocks_bitmap);
1347
+ bitmap_free(msb->erased_blocks_bitmap);
13511348 kfree(msb->lba_to_pba_table);
1352
- kfree(msb->erased_blocks_bitmap);
13531349 return -ENOMEM;
13541350 }
13551351
....@@ -1873,69 +1869,65 @@
18731869 struct msb_data *msb = container_of(work, struct msb_data, io_work);
18741870 int page, error, len;
18751871 sector_t lba;
1876
- unsigned long flags;
18771872 struct scatterlist *sg = msb->prealloc_sg;
1873
+ struct request *req;
18781874
18791875 dbg_verbose("IO: work started");
18801876
18811877 while (1) {
1882
- spin_lock_irqsave(&msb->q_lock, flags);
1878
+ spin_lock_irq(&msb->q_lock);
18831879
18841880 if (msb->need_flush_cache) {
18851881 msb->need_flush_cache = false;
1886
- spin_unlock_irqrestore(&msb->q_lock, flags);
1882
+ spin_unlock_irq(&msb->q_lock);
18871883 msb_cache_flush(msb);
18881884 continue;
18891885 }
18901886
1891
- if (!msb->req) {
1892
- msb->req = blk_fetch_request(msb->queue);
1893
- if (!msb->req) {
1894
- dbg_verbose("IO: no more requests exiting");
1895
- spin_unlock_irqrestore(&msb->q_lock, flags);
1896
- return;
1897
- }
1887
+ req = msb->req;
1888
+ if (!req) {
1889
+ dbg_verbose("IO: no more requests exiting");
1890
+ spin_unlock_irq(&msb->q_lock);
1891
+ return;
18981892 }
18991893
1900
- spin_unlock_irqrestore(&msb->q_lock, flags);
1901
-
1902
- /* If card was removed meanwhile */
1903
- if (!msb->req)
1904
- return;
1894
+ spin_unlock_irq(&msb->q_lock);
19051895
19061896 /* process the request */
19071897 dbg_verbose("IO: processing new request");
1908
- blk_rq_map_sg(msb->queue, msb->req, sg);
1898
+ blk_rq_map_sg(msb->queue, req, sg);
19091899
1910
- lba = blk_rq_pos(msb->req);
1900
+ lba = blk_rq_pos(req);
19111901
19121902 sector_div(lba, msb->page_size / 512);
19131903 page = sector_div(lba, msb->pages_in_block);
19141904
19151905 if (rq_data_dir(msb->req) == READ)
19161906 error = msb_do_read_request(msb, lba, page, sg,
1917
- blk_rq_bytes(msb->req), &len);
1907
+ blk_rq_bytes(req), &len);
19181908 else
19191909 error = msb_do_write_request(msb, lba, page, sg,
1920
- blk_rq_bytes(msb->req), &len);
1910
+ blk_rq_bytes(req), &len);
19211911
1922
- spin_lock_irqsave(&msb->q_lock, flags);
1923
-
1924
- if (len)
1925
- if (!__blk_end_request(msb->req, BLK_STS_OK, len))
1926
- msb->req = NULL;
1912
+ if (len && !blk_update_request(req, BLK_STS_OK, len)) {
1913
+ __blk_mq_end_request(req, BLK_STS_OK);
1914
+ spin_lock_irq(&msb->q_lock);
1915
+ msb->req = NULL;
1916
+ spin_unlock_irq(&msb->q_lock);
1917
+ }
19271918
19281919 if (error && msb->req) {
19291920 blk_status_t ret = errno_to_blk_status(error);
1921
+
19301922 dbg_verbose("IO: ending one sector of the request with error");
1931
- if (!__blk_end_request(msb->req, ret, msb->page_size))
1932
- msb->req = NULL;
1923
+ blk_mq_end_request(req, ret);
1924
+ spin_lock_irq(&msb->q_lock);
1925
+ msb->req = NULL;
1926
+ spin_unlock_irq(&msb->q_lock);
19331927 }
19341928
19351929 if (msb->req)
19361930 dbg_verbose("IO: request still pending");
1937
-
1938
- spin_unlock_irqrestore(&msb->q_lock, flags);
19391931 }
19401932 }
19411933
....@@ -1961,7 +1953,8 @@
19611953 static void msb_data_clear(struct msb_data *msb)
19621954 {
19631955 kfree(msb->boot_page);
1964
- kfree(msb->used_blocks_bitmap);
1956
+ bitmap_free(msb->used_blocks_bitmap);
1957
+ bitmap_free(msb->erased_blocks_bitmap);
19651958 kfree(msb->lba_to_pba_table);
19661959 kfree(msb->cache);
19671960 msb->card = NULL;
....@@ -2002,29 +1995,40 @@
20021995 return 0;
20031996 }
20041997
2005
-static void msb_submit_req(struct request_queue *q)
1998
+static blk_status_t msb_queue_rq(struct blk_mq_hw_ctx *hctx,
1999
+ const struct blk_mq_queue_data *bd)
20062000 {
2007
- struct memstick_dev *card = q->queuedata;
2001
+ struct memstick_dev *card = hctx->queue->queuedata;
20082002 struct msb_data *msb = memstick_get_drvdata(card);
2009
- struct request *req = NULL;
2003
+ struct request *req = bd->rq;
20102004
20112005 dbg_verbose("Submit request");
2006
+
2007
+ spin_lock_irq(&msb->q_lock);
20122008
20132009 if (msb->card_dead) {
20142010 dbg("Refusing requests on removed card");
20152011
20162012 WARN_ON(!msb->io_queue_stopped);
20172013
2018
- while ((req = blk_fetch_request(q)) != NULL)
2019
- __blk_end_request_all(req, BLK_STS_IOERR);
2020
- return;
2014
+ spin_unlock_irq(&msb->q_lock);
2015
+ blk_mq_start_request(req);
2016
+ return BLK_STS_IOERR;
20212017 }
20222018
2023
- if (msb->req)
2024
- return;
2019
+ if (msb->req) {
2020
+ spin_unlock_irq(&msb->q_lock);
2021
+ return BLK_STS_DEV_RESOURCE;
2022
+ }
2023
+
2024
+ blk_mq_start_request(req);
2025
+ msb->req = req;
20252026
20262027 if (!msb->io_queue_stopped)
20272028 queue_work(msb->io_queue, &msb->io_work);
2029
+
2030
+ spin_unlock_irq(&msb->q_lock);
2031
+ return BLK_STS_OK;
20282032 }
20292033
20302034 static int msb_check_card(struct memstick_dev *card)
....@@ -2040,21 +2044,20 @@
20402044
20412045 dbg("Stopping all msblock IO");
20422046
2047
+ blk_mq_stop_hw_queues(msb->queue);
20432048 spin_lock_irqsave(&msb->q_lock, flags);
2044
- blk_stop_queue(msb->queue);
20452049 msb->io_queue_stopped = true;
20462050 spin_unlock_irqrestore(&msb->q_lock, flags);
20472051
20482052 del_timer_sync(&msb->cache_flush_timer);
20492053 flush_workqueue(msb->io_queue);
20502054
2055
+ spin_lock_irqsave(&msb->q_lock, flags);
20512056 if (msb->req) {
2052
- spin_lock_irqsave(&msb->q_lock, flags);
2053
- blk_requeue_request(msb->queue, msb->req);
2057
+ blk_mq_requeue_request(msb->req, false);
20542058 msb->req = NULL;
2055
- spin_unlock_irqrestore(&msb->q_lock, flags);
20562059 }
2057
-
2060
+ spin_unlock_irqrestore(&msb->q_lock, flags);
20582061 }
20592062
20602063 static void msb_start(struct memstick_dev *card)
....@@ -2077,9 +2080,7 @@
20772080 msb->need_flush_cache = true;
20782081 msb->io_queue_stopped = false;
20792082
2080
- spin_lock_irqsave(&msb->q_lock, flags);
2081
- blk_start_queue(msb->queue);
2082
- spin_unlock_irqrestore(&msb->q_lock, flags);
2083
+ blk_mq_start_hw_queues(msb->queue);
20832084
20842085 queue_work(msb->io_queue, &msb->io_work);
20852086
....@@ -2090,6 +2091,10 @@
20902091 .release = msb_bd_release,
20912092 .getgeo = msb_bd_getgeo,
20922093 .owner = THIS_MODULE
2094
+};
2095
+
2096
+static const struct blk_mq_ops msb_mq_ops = {
2097
+ .queue_rq = msb_queue_rq,
20932098 };
20942099
20952100 /* Registers the block device */
....@@ -2112,9 +2117,11 @@
21122117 goto out_release_id;
21132118 }
21142119
2115
- msb->queue = blk_init_queue(msb_submit_req, &msb->q_lock);
2116
- if (!msb->queue) {
2117
- rc = -ENOMEM;
2120
+ msb->queue = blk_mq_init_sq_queue(&msb->tag_set, &msb_mq_ops, 2,
2121
+ BLK_MQ_F_SHOULD_MERGE);
2122
+ if (IS_ERR(msb->queue)) {
2123
+ rc = PTR_ERR(msb->queue);
2124
+ msb->queue = NULL;
21182125 goto out_put_disk;
21192126 }
21202127
....@@ -2146,7 +2153,7 @@
21462153 set_disk_ro(msb->disk, 1);
21472154
21482155 msb_start(card);
2149
- device_add_disk(&card->dev, msb->disk);
2156
+ device_add_disk(&card->dev, msb->disk, NULL);
21502157 dbg("Disk added");
21512158 return 0;
21522159
....@@ -2202,12 +2209,13 @@
22022209 /* Take care of unhandled + new requests from now on */
22032210 spin_lock_irqsave(&msb->q_lock, flags);
22042211 msb->card_dead = true;
2205
- blk_start_queue(msb->queue);
22062212 spin_unlock_irqrestore(&msb->q_lock, flags);
2213
+ blk_mq_start_hw_queues(msb->queue);
22072214
22082215 /* Remove the disk */
22092216 del_gendisk(msb->disk);
22102217 blk_cleanup_queue(msb->queue);
2218
+ blk_mq_free_tag_set(&msb->tag_set);
22112219 msb->queue = NULL;
22122220
22132221 mutex_lock(&msb_disk_lock);