blob: 3ba07ab30c84f5478beb58c192d66d576a2f22ad [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +01002/*
3* Filename: dev.c
4*
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +01005* Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6* Philip Kelleher <pjk1939@linux.vnet.ibm.com>
7*
8* (C) Copyright 2013 IBM Corporation
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +01009*/
10
11#include <linux/kernel.h>
12#include <linux/interrupt.h>
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/slab.h>
16
17#include <linux/hdreg.h>
18#include <linux/genhd.h>
19#include <linux/blkdev.h>
20#include <linux/bio.h>
21
22#include <linux/fs.h>
23
24#include "rsxx_priv.h"
25
26static unsigned int blkdev_minors = 64;
27module_param(blkdev_minors, uint, 0444);
28MODULE_PARM_DESC(blkdev_minors, "Number of minors(partitions)");
29
30/*
31 * For now I'm making this tweakable in case any applications hit this limit.
32 * If you see a "bio too big" error in the log you will need to raise this
33 * value.
34 */
35static unsigned int blkdev_max_hw_sectors = 1024;
36module_param(blkdev_max_hw_sectors, uint, 0444);
37MODULE_PARM_DESC(blkdev_max_hw_sectors, "Max hw sectors for a single BIO");
38
39static unsigned int enable_blkdev = 1;
40module_param(enable_blkdev , uint, 0444);
41MODULE_PARM_DESC(enable_blkdev, "Enable block device interfaces");
42
43
44struct rsxx_bio_meta {
45 struct bio *bio;
46 atomic_t pending_dmas;
47 atomic_t error;
48 unsigned long start_time;
49};
50
51static struct kmem_cache *bio_meta_pool;
52
53/*----------------- Block Device Operations -----------------*/
54static int rsxx_blkdev_ioctl(struct block_device *bdev,
55 fmode_t mode,
56 unsigned int cmd,
57 unsigned long arg)
58{
59 struct rsxx_cardinfo *card = bdev->bd_disk->private_data;
60
61 switch (cmd) {
62 case RSXX_GETREG:
63 return rsxx_reg_access(card, (void __user *)arg, 1);
64 case RSXX_SETREG:
65 return rsxx_reg_access(card, (void __user *)arg, 0);
66 }
67
68 return -ENOTTY;
69}
70
71static int rsxx_getgeo(struct block_device *bdev, struct hd_geometry *geo)
72{
73 struct rsxx_cardinfo *card = bdev->bd_disk->private_data;
74 u64 blocks = card->size8 >> 9;
75
76 /*
77 * get geometry: Fake it. I haven't found any drivers that set
78 * geo->start, so we won't either.
79 */
80 if (card->size8) {
81 geo->heads = 64;
82 geo->sectors = 16;
83 do_div(blocks, (geo->heads * geo->sectors));
84 geo->cylinders = blocks;
85 } else {
86 geo->heads = 0;
87 geo->sectors = 0;
88 geo->cylinders = 0;
89 }
90 return 0;
91}
92
93static const struct block_device_operations rsxx_fops = {
94 .owner = THIS_MODULE,
95 .getgeo = rsxx_getgeo,
96 .ioctl = rsxx_blkdev_ioctl,
97};
98
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +010099static void bio_dma_done_cb(struct rsxx_cardinfo *card,
100 void *cb_data,
101 unsigned int error)
102{
Philip J Kelleherc206c702013-02-18 21:35:59 +0100103 struct rsxx_bio_meta *meta = cb_data;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100104
105 if (error)
106 atomic_set(&meta->error, 1);
107
108 if (atomic_dec_and_test(&meta->pending_dmas)) {
Philip J Kelleher0ab47432013-06-18 14:36:26 -0500109 if (!card->eeh_state && card->gendisk)
Christoph Hellwig421716b2020-05-27 07:24:06 +0200110 bio_end_io_acct(meta->bio, meta->start_time);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100111
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200112 if (atomic_read(&meta->error))
113 bio_io_error(meta->bio);
114 else
115 bio_endio(meta->bio);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100116 kmem_cache_free(bio_meta_pool, meta);
117 }
118}
119
Jens Axboedece1632015-11-05 10:41:16 -0700120static blk_qc_t rsxx_make_request(struct request_queue *q, struct bio *bio)
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100121{
122 struct rsxx_cardinfo *card = q->queuedata;
123 struct rsxx_bio_meta *bio_meta;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200124 blk_status_t st = BLK_STS_IOERR;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100125
NeilBrownaf67c312017-06-18 14:38:57 +1000126 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700127
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100128 might_sleep();
129
Philip J Kelleher66bc6002013-06-18 14:46:04 -0500130 if (!card)
131 goto req_err;
132
Kent Overstreet4f024f32013-10-11 15:44:27 -0700133 if (bio_end_sector(bio) > get_capacity(card->gendisk))
Philip J Kelleher3eb8dca2013-06-18 14:48:38 -0500134 goto req_err;
135
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200136 if (unlikely(card->halt))
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100137 goto req_err;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100138
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200139 if (unlikely(card->dma_fault))
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100140 goto req_err;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100141
Kent Overstreet4f024f32013-10-11 15:44:27 -0700142 if (bio->bi_iter.bi_size == 0) {
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100143 dev_err(CARD_TO_DEV(card), "size zero BIO!\n");
144 goto req_err;
145 }
146
147 bio_meta = kmem_cache_alloc(bio_meta_pool, GFP_KERNEL);
148 if (!bio_meta) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200149 st = BLK_STS_RESOURCE;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100150 goto req_err;
151 }
152
153 bio_meta->bio = bio;
154 atomic_set(&bio_meta->error, 0);
155 atomic_set(&bio_meta->pending_dmas, 0);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100156
Philip J Kelleher0ab47432013-06-18 14:36:26 -0500157 if (!unlikely(card->halt))
Christoph Hellwig421716b2020-05-27 07:24:06 +0200158 bio_meta->start_time = bio_start_io_acct(bio);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100159
160 dev_dbg(CARD_TO_DEV(card), "BIO[%c]: meta: %p addr8: x%llx size: %d\n",
161 bio_data_dir(bio) ? 'W' : 'R', bio_meta,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700162 (u64)bio->bi_iter.bi_sector << 9, bio->bi_iter.bi_size);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100163
164 st = rsxx_dma_queue_bio(card, bio, &bio_meta->pending_dmas,
165 bio_dma_done_cb, bio_meta);
166 if (st)
167 goto queue_err;
168
Jens Axboedece1632015-11-05 10:41:16 -0700169 return BLK_QC_T_NONE;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100170
171queue_err:
172 kmem_cache_free(bio_meta_pool, bio_meta);
173req_err:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200174 if (st)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200175 bio->bi_status = st;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200176 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700177 return BLK_QC_T_NONE;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100178}
179
180/*----------------- Device Setup -------------------*/
181static bool rsxx_discard_supported(struct rsxx_cardinfo *card)
182{
183 unsigned char pci_rev;
184
185 pci_read_config_byte(card->dev, PCI_REVISION_ID, &pci_rev);
186
187 return (pci_rev >= RSXX_DISCARD_SUPPORT);
188}
189
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100190int rsxx_attach_dev(struct rsxx_cardinfo *card)
191{
192 mutex_lock(&card->dev_lock);
193
194 /* The block device requires the stripe size from the config. */
195 if (enable_blkdev) {
196 if (card->config_valid)
197 set_capacity(card->gendisk, card->size8 >> 9);
198 else
199 set_capacity(card->gendisk, 0);
Hannes Reineckefef912b2018-09-28 08:17:19 +0200200 device_add_disk(CARD_TO_DEV(card), card->gendisk, NULL);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100201 card->bdev_attached = 1;
202 }
203
204 mutex_unlock(&card->dev_lock);
205
206 return 0;
207}
208
209void rsxx_detach_dev(struct rsxx_cardinfo *card)
210{
211 mutex_lock(&card->dev_lock);
212
213 if (card->bdev_attached) {
214 del_gendisk(card->gendisk);
215 card->bdev_attached = 0;
216 }
217
218 mutex_unlock(&card->dev_lock);
219}
220
221int rsxx_setup_dev(struct rsxx_cardinfo *card)
222{
223 unsigned short blk_size;
224
225 mutex_init(&card->dev_lock);
226
227 if (!enable_blkdev)
228 return 0;
229
230 card->major = register_blkdev(0, DRIVER_NAME);
231 if (card->major < 0) {
232 dev_err(CARD_TO_DEV(card), "Failed to get major number\n");
233 return -ENOMEM;
234 }
235
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100236 card->queue = blk_alloc_queue(rsxx_make_request, NUMA_NO_NODE);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100237 if (!card->queue) {
238 dev_err(CARD_TO_DEV(card), "Failed queue alloc\n");
239 unregister_blkdev(card->major, DRIVER_NAME);
240 return -ENOMEM;
241 }
242
243 card->gendisk = alloc_disk(blkdev_minors);
244 if (!card->gendisk) {
245 dev_err(CARD_TO_DEV(card), "Failed disk alloc\n");
246 blk_cleanup_queue(card->queue);
247 unregister_blkdev(card->major, DRIVER_NAME);
248 return -ENOMEM;
249 }
250
Philip J Kelleher8c49a772013-10-18 17:12:35 -0500251 if (card->config_valid) {
252 blk_size = card->config.data.block_size;
253 blk_queue_dma_alignment(card->queue, blk_size - 1);
254 blk_queue_logical_block_size(card->queue, blk_size);
255 }
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100256
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100257 blk_queue_max_hw_sectors(card->queue, blkdev_max_hw_sectors);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100258 blk_queue_physical_block_size(card->queue, RSXX_HW_BLK_SIZE);
259
Bart Van Assche8b904b52018-03-07 17:10:10 -0800260 blk_queue_flag_set(QUEUE_FLAG_NONROT, card->queue);
261 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, card->queue);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100262 if (rsxx_discard_supported(card)) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800263 blk_queue_flag_set(QUEUE_FLAG_DISCARD, card->queue);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100264 blk_queue_max_discard_sectors(card->queue,
265 RSXX_HW_BLK_SIZE >> 9);
266 card->queue->limits.discard_granularity = RSXX_HW_BLK_SIZE;
267 card->queue->limits.discard_alignment = RSXX_HW_BLK_SIZE;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100268 }
269
270 card->queue->queuedata = card;
271
272 snprintf(card->gendisk->disk_name, sizeof(card->gendisk->disk_name),
273 "rsxx%d", card->disk_id);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100274 card->gendisk->major = card->major;
275 card->gendisk->first_minor = 0;
276 card->gendisk->fops = &rsxx_fops;
277 card->gendisk->private_data = card;
278 card->gendisk->queue = card->queue;
279
280 return 0;
281}
282
283void rsxx_destroy_dev(struct rsxx_cardinfo *card)
284{
285 if (!enable_blkdev)
286 return;
287
288 put_disk(card->gendisk);
289 card->gendisk = NULL;
290
291 blk_cleanup_queue(card->queue);
Philip J Kelleher66bc6002013-06-18 14:46:04 -0500292 card->queue->queuedata = NULL;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100293 unregister_blkdev(card->major, DRIVER_NAME);
294}
295
296int rsxx_dev_init(void)
297{
298 bio_meta_pool = KMEM_CACHE(rsxx_bio_meta, SLAB_HWCACHE_ALIGN);
299 if (!bio_meta_pool)
300 return -ENOMEM;
301
302 return 0;
303}
304
305void rsxx_dev_cleanup(void)
306{
307 kmem_cache_destroy(bio_meta_pool);
308}
309
310