josh.h.morris@us.ibm.com | 8722ff8 | 2013-02-05 14:15:02 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 40 | static unsigned int blkdev_minors = 64; |
| 41 | module_param(blkdev_minors, uint, 0444); |
| 42 | MODULE_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 | */ |
| 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"); |
| 52 | |
| 53 | static unsigned int enable_blkdev = 1; |
| 54 | module_param(enable_blkdev , uint, 0444); |
| 55 | MODULE_PARM_DESC(enable_blkdev, "Enable block device interfaces"); |
| 56 | |
| 57 | |
| 58 | struct rsxx_bio_meta { |
| 59 | struct bio *bio; |
| 60 | atomic_t pending_dmas; |
| 61 | atomic_t error; |
| 62 | unsigned long start_time; |
| 63 | }; |
| 64 | |
| 65 | static struct kmem_cache *bio_meta_pool; |
| 66 | |
| 67 | /*----------------- Block Device Operations -----------------*/ |
| 68 | static 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 | |
| 85 | static 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 | |
| 107 | static const struct block_device_operations rsxx_fops = { |
| 108 | .owner = THIS_MODULE, |
| 109 | .getgeo = rsxx_getgeo, |
| 110 | .ioctl = rsxx_blkdev_ioctl, |
| 111 | }; |
| 112 | |
| 113 | static void disk_stats_start(struct rsxx_cardinfo *card, struct bio *bio) |
| 114 | { |
| 115 | struct hd_struct *part0 = &card->gendisk->part0; |
| 116 | int rw = bio_data_dir(bio); |
| 117 | int cpu; |
| 118 | |
| 119 | cpu = part_stat_lock(); |
| 120 | |
| 121 | part_round_stats(cpu, part0); |
| 122 | part_inc_in_flight(part0, rw); |
| 123 | |
| 124 | part_stat_unlock(); |
| 125 | } |
| 126 | |
| 127 | static void disk_stats_complete(struct rsxx_cardinfo *card, |
| 128 | struct bio *bio, |
| 129 | unsigned long start_time) |
| 130 | { |
| 131 | struct hd_struct *part0 = &card->gendisk->part0; |
| 132 | unsigned long duration = jiffies - start_time; |
| 133 | int rw = bio_data_dir(bio); |
| 134 | int cpu; |
| 135 | |
| 136 | cpu = part_stat_lock(); |
| 137 | |
| 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); |
| 141 | |
| 142 | part_round_stats(cpu, part0); |
| 143 | part_dec_in_flight(part0, rw); |
| 144 | |
| 145 | part_stat_unlock(); |
| 146 | } |
| 147 | |
| 148 | static void bio_dma_done_cb(struct rsxx_cardinfo *card, |
| 149 | void *cb_data, |
| 150 | unsigned int error) |
| 151 | { |
Philip J Kelleher | c206c70 | 2013-02-18 21:35:59 +0100 | [diff] [blame^] | 152 | struct rsxx_bio_meta *meta = cb_data; |
josh.h.morris@us.ibm.com | 8722ff8 | 2013-02-05 14:15:02 +0100 | [diff] [blame] | 153 | |
| 154 | if (error) |
| 155 | atomic_set(&meta->error, 1); |
| 156 | |
| 157 | if (atomic_dec_and_test(&meta->pending_dmas)) { |
| 158 | disk_stats_complete(card, meta->bio, meta->start_time); |
| 159 | |
| 160 | bio_endio(meta->bio, atomic_read(&meta->error) ? -EIO : 0); |
| 161 | kmem_cache_free(bio_meta_pool, meta); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void rsxx_make_request(struct request_queue *q, struct bio *bio) |
| 166 | { |
| 167 | struct rsxx_cardinfo *card = q->queuedata; |
| 168 | struct rsxx_bio_meta *bio_meta; |
| 169 | int st = -EINVAL; |
| 170 | |
| 171 | might_sleep(); |
| 172 | |
| 173 | if (unlikely(card->halt)) { |
| 174 | st = -EFAULT; |
| 175 | goto req_err; |
| 176 | } |
| 177 | |
| 178 | if (unlikely(card->dma_fault)) { |
| 179 | st = (-EFAULT); |
| 180 | goto req_err; |
| 181 | } |
| 182 | |
| 183 | if (bio->bi_size == 0) { |
| 184 | dev_err(CARD_TO_DEV(card), "size zero BIO!\n"); |
| 185 | goto req_err; |
| 186 | } |
| 187 | |
| 188 | bio_meta = kmem_cache_alloc(bio_meta_pool, GFP_KERNEL); |
| 189 | if (!bio_meta) { |
| 190 | st = -ENOMEM; |
| 191 | goto req_err; |
| 192 | } |
| 193 | |
| 194 | bio_meta->bio = bio; |
| 195 | atomic_set(&bio_meta->error, 0); |
| 196 | atomic_set(&bio_meta->pending_dmas, 0); |
| 197 | bio_meta->start_time = jiffies; |
| 198 | |
| 199 | disk_stats_start(card, bio); |
| 200 | |
| 201 | dev_dbg(CARD_TO_DEV(card), "BIO[%c]: meta: %p addr8: x%llx size: %d\n", |
| 202 | bio_data_dir(bio) ? 'W' : 'R', bio_meta, |
| 203 | (u64)bio->bi_sector << 9, bio->bi_size); |
| 204 | |
| 205 | st = rsxx_dma_queue_bio(card, bio, &bio_meta->pending_dmas, |
| 206 | bio_dma_done_cb, bio_meta); |
| 207 | if (st) |
| 208 | goto queue_err; |
| 209 | |
| 210 | return; |
| 211 | |
| 212 | queue_err: |
| 213 | kmem_cache_free(bio_meta_pool, bio_meta); |
| 214 | req_err: |
| 215 | bio_endio(bio, st); |
| 216 | } |
| 217 | |
| 218 | /*----------------- Device Setup -------------------*/ |
| 219 | static bool rsxx_discard_supported(struct rsxx_cardinfo *card) |
| 220 | { |
| 221 | unsigned char pci_rev; |
| 222 | |
| 223 | pci_read_config_byte(card->dev, PCI_REVISION_ID, &pci_rev); |
| 224 | |
| 225 | return (pci_rev >= RSXX_DISCARD_SUPPORT); |
| 226 | } |
| 227 | |
| 228 | static unsigned short rsxx_get_logical_block_size( |
| 229 | struct rsxx_cardinfo *card) |
| 230 | { |
| 231 | u32 capabilities = 0; |
| 232 | int st; |
| 233 | |
| 234 | st = rsxx_get_card_capabilities(card, &capabilities); |
| 235 | if (st) |
| 236 | dev_warn(CARD_TO_DEV(card), |
| 237 | "Failed reading card capabilities register\n"); |
| 238 | |
| 239 | /* Earlier firmware did not have support for 512 byte accesses */ |
| 240 | if (capabilities & CARD_CAP_SUBPAGE_WRITES) |
| 241 | return 512; |
| 242 | else |
| 243 | return RSXX_HW_BLK_SIZE; |
| 244 | } |
| 245 | |
| 246 | int rsxx_attach_dev(struct rsxx_cardinfo *card) |
| 247 | { |
| 248 | mutex_lock(&card->dev_lock); |
| 249 | |
| 250 | /* The block device requires the stripe size from the config. */ |
| 251 | if (enable_blkdev) { |
| 252 | if (card->config_valid) |
| 253 | set_capacity(card->gendisk, card->size8 >> 9); |
| 254 | else |
| 255 | set_capacity(card->gendisk, 0); |
| 256 | add_disk(card->gendisk); |
| 257 | |
| 258 | card->bdev_attached = 1; |
| 259 | } |
| 260 | |
| 261 | mutex_unlock(&card->dev_lock); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | void rsxx_detach_dev(struct rsxx_cardinfo *card) |
| 267 | { |
| 268 | mutex_lock(&card->dev_lock); |
| 269 | |
| 270 | if (card->bdev_attached) { |
| 271 | del_gendisk(card->gendisk); |
| 272 | card->bdev_attached = 0; |
| 273 | } |
| 274 | |
| 275 | mutex_unlock(&card->dev_lock); |
| 276 | } |
| 277 | |
| 278 | int rsxx_setup_dev(struct rsxx_cardinfo *card) |
| 279 | { |
| 280 | unsigned short blk_size; |
| 281 | |
| 282 | mutex_init(&card->dev_lock); |
| 283 | |
| 284 | if (!enable_blkdev) |
| 285 | return 0; |
| 286 | |
| 287 | card->major = register_blkdev(0, DRIVER_NAME); |
| 288 | if (card->major < 0) { |
| 289 | dev_err(CARD_TO_DEV(card), "Failed to get major number\n"); |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
| 293 | card->queue = blk_alloc_queue(GFP_KERNEL); |
| 294 | if (!card->queue) { |
| 295 | dev_err(CARD_TO_DEV(card), "Failed queue alloc\n"); |
| 296 | unregister_blkdev(card->major, DRIVER_NAME); |
| 297 | return -ENOMEM; |
| 298 | } |
| 299 | |
| 300 | card->gendisk = alloc_disk(blkdev_minors); |
| 301 | if (!card->gendisk) { |
| 302 | dev_err(CARD_TO_DEV(card), "Failed disk alloc\n"); |
| 303 | blk_cleanup_queue(card->queue); |
| 304 | unregister_blkdev(card->major, DRIVER_NAME); |
| 305 | return -ENOMEM; |
| 306 | } |
| 307 | |
| 308 | blk_size = rsxx_get_logical_block_size(card); |
| 309 | |
| 310 | blk_queue_make_request(card->queue, rsxx_make_request); |
| 311 | blk_queue_bounce_limit(card->queue, BLK_BOUNCE_ANY); |
| 312 | blk_queue_dma_alignment(card->queue, blk_size - 1); |
| 313 | blk_queue_max_hw_sectors(card->queue, blkdev_max_hw_sectors); |
| 314 | blk_queue_logical_block_size(card->queue, blk_size); |
| 315 | blk_queue_physical_block_size(card->queue, RSXX_HW_BLK_SIZE); |
| 316 | |
| 317 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, card->queue); |
| 318 | if (rsxx_discard_supported(card)) { |
| 319 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, card->queue); |
| 320 | blk_queue_max_discard_sectors(card->queue, |
| 321 | RSXX_HW_BLK_SIZE >> 9); |
| 322 | card->queue->limits.discard_granularity = RSXX_HW_BLK_SIZE; |
| 323 | card->queue->limits.discard_alignment = RSXX_HW_BLK_SIZE; |
| 324 | card->queue->limits.discard_zeroes_data = 1; |
| 325 | } |
| 326 | |
| 327 | card->queue->queuedata = card; |
| 328 | |
| 329 | snprintf(card->gendisk->disk_name, sizeof(card->gendisk->disk_name), |
| 330 | "rsxx%d", card->disk_id); |
| 331 | card->gendisk->driverfs_dev = &card->dev->dev; |
| 332 | card->gendisk->major = card->major; |
| 333 | card->gendisk->first_minor = 0; |
| 334 | card->gendisk->fops = &rsxx_fops; |
| 335 | card->gendisk->private_data = card; |
| 336 | card->gendisk->queue = card->queue; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | void rsxx_destroy_dev(struct rsxx_cardinfo *card) |
| 342 | { |
| 343 | if (!enable_blkdev) |
| 344 | return; |
| 345 | |
| 346 | put_disk(card->gendisk); |
| 347 | card->gendisk = NULL; |
| 348 | |
| 349 | blk_cleanup_queue(card->queue); |
| 350 | unregister_blkdev(card->major, DRIVER_NAME); |
| 351 | } |
| 352 | |
| 353 | int rsxx_dev_init(void) |
| 354 | { |
| 355 | bio_meta_pool = KMEM_CACHE(rsxx_bio_meta, SLAB_HWCACHE_ALIGN); |
| 356 | if (!bio_meta_pool) |
| 357 | return -ENOMEM; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | void rsxx_dev_cleanup(void) |
| 363 | { |
| 364 | kmem_cache_destroy(bio_meta_pool); |
| 365 | } |
| 366 | |
| 367 | |