5 * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6 * Philip Kelleher <pjk1939@linux.vnet.ibm.com>
8 * (C) Copyright 2013 IBM Corporation
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.
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.
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
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>
31 #include <linux/hdreg.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/bio.h>
38 #include "rsxx_priv.h"
40 static unsigned int blkdev_minors
= 64;
41 module_param(blkdev_minors
, uint
, 0444);
42 MODULE_PARM_DESC(blkdev_minors
, "Number of minors(partitions)");
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
49 static unsigned int blkdev_max_hw_sectors
= 1024;
50 module_param(blkdev_max_hw_sectors
, uint
, 0444);
51 MODULE_PARM_DESC(blkdev_max_hw_sectors
, "Max hw sectors for a single BIO");
53 static unsigned int enable_blkdev
= 1;
54 module_param(enable_blkdev
, uint
, 0444);
55 MODULE_PARM_DESC(enable_blkdev
, "Enable block device interfaces");
58 struct rsxx_bio_meta
{
60 atomic_t pending_dmas
;
62 unsigned long start_time
;
65 static struct kmem_cache
*bio_meta_pool
;
67 /*----------------- Block Device Operations -----------------*/
68 static int rsxx_blkdev_ioctl(struct block_device
*bdev
,
73 struct rsxx_cardinfo
*card
= bdev
->bd_disk
->private_data
;
77 return rsxx_reg_access(card
, (void __user
*)arg
, 1);
79 return rsxx_reg_access(card
, (void __user
*)arg
, 0);
85 static int rsxx_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
87 struct rsxx_cardinfo
*card
= bdev
->bd_disk
->private_data
;
88 u64 blocks
= card
->size8
>> 9;
91 * get geometry: Fake it. I haven't found any drivers that set
92 * geo->start, so we won't either.
97 do_div(blocks
, (geo
->heads
* geo
->sectors
));
98 geo
->cylinders
= blocks
;
107 static const struct block_device_operations rsxx_fops
= {
108 .owner
= THIS_MODULE
,
109 .getgeo
= rsxx_getgeo
,
110 .ioctl
= rsxx_blkdev_ioctl
,
113 static void disk_stats_start(struct rsxx_cardinfo
*card
, struct bio
*bio
)
115 struct hd_struct
*part0
= &card
->gendisk
->part0
;
116 int rw
= bio_data_dir(bio
);
119 cpu
= part_stat_lock();
121 part_round_stats(cpu
, part0
);
122 part_inc_in_flight(part0
, rw
);
127 static void disk_stats_complete(struct rsxx_cardinfo
*card
,
129 unsigned long start_time
)
131 struct hd_struct
*part0
= &card
->gendisk
->part0
;
132 unsigned long duration
= jiffies
- start_time
;
133 int rw
= bio_data_dir(bio
);
136 cpu
= part_stat_lock();
138 part_stat_add(cpu
, part0
, sectors
[rw
], bio_sectors(bio
));
139 part_stat_inc(cpu
, part0
, ios
[rw
]);
140 part_stat_add(cpu
, part0
, ticks
[rw
], duration
);
142 part_round_stats(cpu
, part0
);
143 part_dec_in_flight(part0
, rw
);
148 static void bio_dma_done_cb(struct rsxx_cardinfo
*card
,
152 struct rsxx_bio_meta
*meta
= cb_data
;
155 atomic_set(&meta
->error
, 1);
157 if (atomic_dec_and_test(&meta
->pending_dmas
)) {
158 if (!card
->eeh_state
&& card
->gendisk
)
159 disk_stats_complete(card
, meta
->bio
, meta
->start_time
);
161 bio_endio(meta
->bio
, atomic_read(&meta
->error
) ? -EIO
: 0);
162 kmem_cache_free(bio_meta_pool
, meta
);
166 static void rsxx_make_request(struct request_queue
*q
, struct bio
*bio
)
168 struct rsxx_cardinfo
*card
= q
->queuedata
;
169 struct rsxx_bio_meta
*bio_meta
;
177 if (bio
->bi_sector
+ (bio
->bi_size
>> 9) > get_capacity(card
->gendisk
))
180 if (unlikely(card
->halt
)) {
185 if (unlikely(card
->dma_fault
)) {
190 if (bio
->bi_size
== 0) {
191 dev_err(CARD_TO_DEV(card
), "size zero BIO!\n");
195 bio_meta
= kmem_cache_alloc(bio_meta_pool
, GFP_KERNEL
);
202 atomic_set(&bio_meta
->error
, 0);
203 atomic_set(&bio_meta
->pending_dmas
, 0);
204 bio_meta
->start_time
= jiffies
;
206 if (!unlikely(card
->halt
))
207 disk_stats_start(card
, bio
);
209 dev_dbg(CARD_TO_DEV(card
), "BIO[%c]: meta: %p addr8: x%llx size: %d\n",
210 bio_data_dir(bio
) ? 'W' : 'R', bio_meta
,
211 (u64
)bio
->bi_sector
<< 9, bio
->bi_size
);
213 st
= rsxx_dma_queue_bio(card
, bio
, &bio_meta
->pending_dmas
,
214 bio_dma_done_cb
, bio_meta
);
221 kmem_cache_free(bio_meta_pool
, bio_meta
);
226 /*----------------- Device Setup -------------------*/
227 static bool rsxx_discard_supported(struct rsxx_cardinfo
*card
)
229 unsigned char pci_rev
;
231 pci_read_config_byte(card
->dev
, PCI_REVISION_ID
, &pci_rev
);
233 return (pci_rev
>= RSXX_DISCARD_SUPPORT
);
236 int rsxx_attach_dev(struct rsxx_cardinfo
*card
)
238 mutex_lock(&card
->dev_lock
);
240 /* The block device requires the stripe size from the config. */
242 if (card
->config_valid
)
243 set_capacity(card
->gendisk
, card
->size8
>> 9);
245 set_capacity(card
->gendisk
, 0);
246 add_disk(card
->gendisk
);
248 card
->bdev_attached
= 1;
251 mutex_unlock(&card
->dev_lock
);
256 void rsxx_detach_dev(struct rsxx_cardinfo
*card
)
258 mutex_lock(&card
->dev_lock
);
260 if (card
->bdev_attached
) {
261 del_gendisk(card
->gendisk
);
262 card
->bdev_attached
= 0;
265 mutex_unlock(&card
->dev_lock
);
268 int rsxx_setup_dev(struct rsxx_cardinfo
*card
)
270 unsigned short blk_size
;
272 mutex_init(&card
->dev_lock
);
277 card
->major
= register_blkdev(0, DRIVER_NAME
);
278 if (card
->major
< 0) {
279 dev_err(CARD_TO_DEV(card
), "Failed to get major number\n");
283 card
->queue
= blk_alloc_queue(GFP_KERNEL
);
285 dev_err(CARD_TO_DEV(card
), "Failed queue alloc\n");
286 unregister_blkdev(card
->major
, DRIVER_NAME
);
290 card
->gendisk
= alloc_disk(blkdev_minors
);
291 if (!card
->gendisk
) {
292 dev_err(CARD_TO_DEV(card
), "Failed disk alloc\n");
293 blk_cleanup_queue(card
->queue
);
294 unregister_blkdev(card
->major
, DRIVER_NAME
);
298 if (card
->config_valid
) {
299 blk_size
= card
->config
.data
.block_size
;
300 blk_queue_dma_alignment(card
->queue
, blk_size
- 1);
301 blk_queue_logical_block_size(card
->queue
, blk_size
);
304 blk_queue_make_request(card
->queue
, rsxx_make_request
);
305 blk_queue_bounce_limit(card
->queue
, BLK_BOUNCE_ANY
);
306 blk_queue_max_hw_sectors(card
->queue
, blkdev_max_hw_sectors
);
307 blk_queue_physical_block_size(card
->queue
, RSXX_HW_BLK_SIZE
);
309 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, card
->queue
);
310 if (rsxx_discard_supported(card
)) {
311 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, card
->queue
);
312 blk_queue_max_discard_sectors(card
->queue
,
313 RSXX_HW_BLK_SIZE
>> 9);
314 card
->queue
->limits
.discard_granularity
= RSXX_HW_BLK_SIZE
;
315 card
->queue
->limits
.discard_alignment
= RSXX_HW_BLK_SIZE
;
316 card
->queue
->limits
.discard_zeroes_data
= 1;
319 card
->queue
->queuedata
= card
;
321 snprintf(card
->gendisk
->disk_name
, sizeof(card
->gendisk
->disk_name
),
322 "rsxx%d", card
->disk_id
);
323 card
->gendisk
->driverfs_dev
= &card
->dev
->dev
;
324 card
->gendisk
->major
= card
->major
;
325 card
->gendisk
->first_minor
= 0;
326 card
->gendisk
->fops
= &rsxx_fops
;
327 card
->gendisk
->private_data
= card
;
328 card
->gendisk
->queue
= card
->queue
;
333 void rsxx_destroy_dev(struct rsxx_cardinfo
*card
)
338 put_disk(card
->gendisk
);
339 card
->gendisk
= NULL
;
341 blk_cleanup_queue(card
->queue
);
342 card
->queue
->queuedata
= NULL
;
343 unregister_blkdev(card
->major
, DRIVER_NAME
);
346 int rsxx_dev_init(void)
348 bio_meta_pool
= KMEM_CACHE(rsxx_bio_meta
, SLAB_HWCACHE_ALIGN
);
355 void rsxx_dev_cleanup(void)
357 kmem_cache_destroy(bio_meta_pool
);