blob: 63b9d2ffa8ee406628da48d60c841b6a8a65bb98 [file] [log] [blame]
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +01001/*
2* Filename: dev.c
3*
4*
5* Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6* Philip Kelleher <pjk1939@linux.vnet.ibm.com>
7*
8* (C) Copyright 2013 IBM Corporation
9*
10* This program is free software; you can redistribute it and/or
11* modify it under the terms of the GNU General Public License as
12* published by the Free Software Foundation; either version 2 of the
13* License, or (at your option) any later version.
14*
15* This program is distributed in the hope that it will be useful, but
16* WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18* General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with this program; if not, write to the Free Software Foundation,
22* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#include <linux/kernel.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/pci.h>
29#include <linux/slab.h>
30
31#include <linux/hdreg.h>
32#include <linux/genhd.h>
33#include <linux/blkdev.h>
34#include <linux/bio.h>
35
36#include <linux/fs.h>
37
38#include "rsxx_priv.h"
39
40static unsigned int blkdev_minors = 64;
41module_param(blkdev_minors, uint, 0444);
42MODULE_PARM_DESC(blkdev_minors, "Number of minors(partitions)");
43
44/*
45 * For now I'm making this tweakable in case any applications hit this limit.
46 * If you see a "bio too big" error in the log you will need to raise this
47 * value.
48 */
49static unsigned int blkdev_max_hw_sectors = 1024;
50module_param(blkdev_max_hw_sectors, uint, 0444);
51MODULE_PARM_DESC(blkdev_max_hw_sectors, "Max hw sectors for a single BIO");
52
53static unsigned int enable_blkdev = 1;
54module_param(enable_blkdev , uint, 0444);
55MODULE_PARM_DESC(enable_blkdev, "Enable block device interfaces");
56
57
58struct rsxx_bio_meta {
59 struct bio *bio;
60 atomic_t pending_dmas;
61 atomic_t error;
62 unsigned long start_time;
63};
64
65static struct kmem_cache *bio_meta_pool;
66
67/*----------------- Block Device Operations -----------------*/
68static int rsxx_blkdev_ioctl(struct block_device *bdev,
69 fmode_t mode,
70 unsigned int cmd,
71 unsigned long arg)
72{
73 struct rsxx_cardinfo *card = bdev->bd_disk->private_data;
74
75 switch (cmd) {
76 case RSXX_GETREG:
77 return rsxx_reg_access(card, (void __user *)arg, 1);
78 case RSXX_SETREG:
79 return rsxx_reg_access(card, (void __user *)arg, 0);
80 }
81
82 return -ENOTTY;
83}
84
85static int rsxx_getgeo(struct block_device *bdev, struct hd_geometry *geo)
86{
87 struct rsxx_cardinfo *card = bdev->bd_disk->private_data;
88 u64 blocks = card->size8 >> 9;
89
90 /*
91 * get geometry: Fake it. I haven't found any drivers that set
92 * geo->start, so we won't either.
93 */
94 if (card->size8) {
95 geo->heads = 64;
96 geo->sectors = 16;
97 do_div(blocks, (geo->heads * geo->sectors));
98 geo->cylinders = blocks;
99 } else {
100 geo->heads = 0;
101 geo->sectors = 0;
102 geo->cylinders = 0;
103 }
104 return 0;
105}
106
107static const struct block_device_operations rsxx_fops = {
108 .owner = THIS_MODULE,
109 .getgeo = rsxx_getgeo,
110 .ioctl = rsxx_blkdev_ioctl,
111};
112
113static void disk_stats_start(struct rsxx_cardinfo *card, struct bio *bio)
114{
Gu Zhengfa573f72014-11-24 11:05:27 +0800115 generic_start_io_acct(bio_data_dir(bio), bio_sectors(bio),
116 &card->gendisk->part0);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100117}
118
119static void disk_stats_complete(struct rsxx_cardinfo *card,
120 struct bio *bio,
121 unsigned long start_time)
122{
Gu Zhengfa573f72014-11-24 11:05:27 +0800123 generic_end_io_acct(bio_data_dir(bio), &card->gendisk->part0,
124 start_time);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100125}
126
127static void bio_dma_done_cb(struct rsxx_cardinfo *card,
128 void *cb_data,
129 unsigned int error)
130{
Philip J Kelleherc206c702013-02-18 21:35:59 +0100131 struct rsxx_bio_meta *meta = cb_data;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100132
133 if (error)
134 atomic_set(&meta->error, 1);
135
136 if (atomic_dec_and_test(&meta->pending_dmas)) {
Philip J Kelleher0ab47432013-06-18 14:36:26 -0500137 if (!card->eeh_state && card->gendisk)
138 disk_stats_complete(card, meta->bio, meta->start_time);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100139
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200140 if (atomic_read(&meta->error))
141 bio_io_error(meta->bio);
142 else
143 bio_endio(meta->bio);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100144 kmem_cache_free(bio_meta_pool, meta);
145 }
146}
147
148static void rsxx_make_request(struct request_queue *q, struct bio *bio)
149{
150 struct rsxx_cardinfo *card = q->queuedata;
151 struct rsxx_bio_meta *bio_meta;
152 int st = -EINVAL;
153
154 might_sleep();
155
Philip J Kelleher66bc6002013-06-18 14:46:04 -0500156 if (!card)
157 goto req_err;
158
Kent Overstreet4f024f32013-10-11 15:44:27 -0700159 if (bio_end_sector(bio) > get_capacity(card->gendisk))
Philip J Kelleher3eb8dca2013-06-18 14:48:38 -0500160 goto req_err;
161
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100162 if (unlikely(card->halt)) {
163 st = -EFAULT;
164 goto req_err;
165 }
166
167 if (unlikely(card->dma_fault)) {
168 st = (-EFAULT);
169 goto req_err;
170 }
171
Kent Overstreet4f024f32013-10-11 15:44:27 -0700172 if (bio->bi_iter.bi_size == 0) {
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100173 dev_err(CARD_TO_DEV(card), "size zero BIO!\n");
174 goto req_err;
175 }
176
177 bio_meta = kmem_cache_alloc(bio_meta_pool, GFP_KERNEL);
178 if (!bio_meta) {
179 st = -ENOMEM;
180 goto req_err;
181 }
182
183 bio_meta->bio = bio;
184 atomic_set(&bio_meta->error, 0);
185 atomic_set(&bio_meta->pending_dmas, 0);
186 bio_meta->start_time = jiffies;
187
Philip J Kelleher0ab47432013-06-18 14:36:26 -0500188 if (!unlikely(card->halt))
189 disk_stats_start(card, bio);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100190
191 dev_dbg(CARD_TO_DEV(card), "BIO[%c]: meta: %p addr8: x%llx size: %d\n",
192 bio_data_dir(bio) ? 'W' : 'R', bio_meta,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700193 (u64)bio->bi_iter.bi_sector << 9, bio->bi_iter.bi_size);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100194
195 st = rsxx_dma_queue_bio(card, bio, &bio_meta->pending_dmas,
196 bio_dma_done_cb, bio_meta);
197 if (st)
198 goto queue_err;
199
200 return;
201
202queue_err:
203 kmem_cache_free(bio_meta_pool, bio_meta);
204req_err:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200205 if (st)
206 bio->bi_error = st;
207 bio_endio(bio);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100208}
209
210/*----------------- Device Setup -------------------*/
211static bool rsxx_discard_supported(struct rsxx_cardinfo *card)
212{
213 unsigned char pci_rev;
214
215 pci_read_config_byte(card->dev, PCI_REVISION_ID, &pci_rev);
216
217 return (pci_rev >= RSXX_DISCARD_SUPPORT);
218}
219
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100220int rsxx_attach_dev(struct rsxx_cardinfo *card)
221{
222 mutex_lock(&card->dev_lock);
223
224 /* The block device requires the stripe size from the config. */
225 if (enable_blkdev) {
226 if (card->config_valid)
227 set_capacity(card->gendisk, card->size8 >> 9);
228 else
229 set_capacity(card->gendisk, 0);
230 add_disk(card->gendisk);
231
232 card->bdev_attached = 1;
233 }
234
235 mutex_unlock(&card->dev_lock);
236
237 return 0;
238}
239
240void rsxx_detach_dev(struct rsxx_cardinfo *card)
241{
242 mutex_lock(&card->dev_lock);
243
244 if (card->bdev_attached) {
245 del_gendisk(card->gendisk);
246 card->bdev_attached = 0;
247 }
248
249 mutex_unlock(&card->dev_lock);
250}
251
252int rsxx_setup_dev(struct rsxx_cardinfo *card)
253{
254 unsigned short blk_size;
255
256 mutex_init(&card->dev_lock);
257
258 if (!enable_blkdev)
259 return 0;
260
261 card->major = register_blkdev(0, DRIVER_NAME);
262 if (card->major < 0) {
263 dev_err(CARD_TO_DEV(card), "Failed to get major number\n");
264 return -ENOMEM;
265 }
266
267 card->queue = blk_alloc_queue(GFP_KERNEL);
268 if (!card->queue) {
269 dev_err(CARD_TO_DEV(card), "Failed queue alloc\n");
270 unregister_blkdev(card->major, DRIVER_NAME);
271 return -ENOMEM;
272 }
273
274 card->gendisk = alloc_disk(blkdev_minors);
275 if (!card->gendisk) {
276 dev_err(CARD_TO_DEV(card), "Failed disk alloc\n");
277 blk_cleanup_queue(card->queue);
278 unregister_blkdev(card->major, DRIVER_NAME);
279 return -ENOMEM;
280 }
281
Philip J Kelleher8c49a772013-10-18 17:12:35 -0500282 if (card->config_valid) {
283 blk_size = card->config.data.block_size;
284 blk_queue_dma_alignment(card->queue, blk_size - 1);
285 blk_queue_logical_block_size(card->queue, blk_size);
286 }
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100287
288 blk_queue_make_request(card->queue, rsxx_make_request);
289 blk_queue_bounce_limit(card->queue, BLK_BOUNCE_ANY);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100290 blk_queue_max_hw_sectors(card->queue, blkdev_max_hw_sectors);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100291 blk_queue_physical_block_size(card->queue, RSXX_HW_BLK_SIZE);
292
293 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, card->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600294 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, card->queue);
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100295 if (rsxx_discard_supported(card)) {
296 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, card->queue);
297 blk_queue_max_discard_sectors(card->queue,
298 RSXX_HW_BLK_SIZE >> 9);
299 card->queue->limits.discard_granularity = RSXX_HW_BLK_SIZE;
300 card->queue->limits.discard_alignment = RSXX_HW_BLK_SIZE;
301 card->queue->limits.discard_zeroes_data = 1;
302 }
303
304 card->queue->queuedata = card;
305
306 snprintf(card->gendisk->disk_name, sizeof(card->gendisk->disk_name),
307 "rsxx%d", card->disk_id);
308 card->gendisk->driverfs_dev = &card->dev->dev;
309 card->gendisk->major = card->major;
310 card->gendisk->first_minor = 0;
311 card->gendisk->fops = &rsxx_fops;
312 card->gendisk->private_data = card;
313 card->gendisk->queue = card->queue;
314
315 return 0;
316}
317
318void rsxx_destroy_dev(struct rsxx_cardinfo *card)
319{
320 if (!enable_blkdev)
321 return;
322
323 put_disk(card->gendisk);
324 card->gendisk = NULL;
325
326 blk_cleanup_queue(card->queue);
Philip J Kelleher66bc6002013-06-18 14:46:04 -0500327 card->queue->queuedata = NULL;
josh.h.morris@us.ibm.com8722ff82013-02-05 14:15:02 +0100328 unregister_blkdev(card->major, DRIVER_NAME);
329}
330
331int rsxx_dev_init(void)
332{
333 bio_meta_pool = KMEM_CACHE(rsxx_bio_meta, SLAB_HWCACHE_ALIGN);
334 if (!bio_meta_pool)
335 return -ENOMEM;
336
337 return 0;
338}
339
340void rsxx_dev_cleanup(void)
341{
342 kmem_cache_destroy(bio_meta_pool);
343}
344
345