dev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #include <linux/kernel.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/slab.h>
  29. #include <linux/hdreg.h>
  30. #include <linux/genhd.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/bio.h>
  33. #include <linux/fs.h>
  34. #include "rsxx_priv.h"
  35. static unsigned int blkdev_minors = 64;
  36. module_param(blkdev_minors, uint, 0444);
  37. MODULE_PARM_DESC(blkdev_minors, "Number of minors(partitions)");
  38. /*
  39. * For now I'm making this tweakable in case any applications hit this limit.
  40. * If you see a "bio too big" error in the log you will need to raise this
  41. * value.
  42. */
  43. static unsigned int blkdev_max_hw_sectors = 1024;
  44. module_param(blkdev_max_hw_sectors, uint, 0444);
  45. MODULE_PARM_DESC(blkdev_max_hw_sectors, "Max hw sectors for a single BIO");
  46. static unsigned int enable_blkdev = 1;
  47. module_param(enable_blkdev , uint, 0444);
  48. MODULE_PARM_DESC(enable_blkdev, "Enable block device interfaces");
  49. struct rsxx_bio_meta {
  50. struct bio *bio;
  51. atomic_t pending_dmas;
  52. atomic_t error;
  53. unsigned long start_time;
  54. };
  55. static struct kmem_cache *bio_meta_pool;
  56. /*----------------- Block Device Operations -----------------*/
  57. static int rsxx_blkdev_ioctl(struct block_device *bdev,
  58. fmode_t mode,
  59. unsigned int cmd,
  60. unsigned long arg)
  61. {
  62. struct rsxx_cardinfo *card = bdev->bd_disk->private_data;
  63. switch (cmd) {
  64. case RSXX_GETREG:
  65. return rsxx_reg_access(card, (void __user *)arg, 1);
  66. case RSXX_SETREG:
  67. return rsxx_reg_access(card, (void __user *)arg, 0);
  68. }
  69. return -ENOTTY;
  70. }
  71. static 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. * get geometry: Fake it. I haven't found any drivers that set
  77. * geo->start, so we won't either.
  78. */
  79. if (card->size8) {
  80. geo->heads = 64;
  81. geo->sectors = 16;
  82. do_div(blocks, (geo->heads * geo->sectors));
  83. geo->cylinders = blocks;
  84. } else {
  85. geo->heads = 0;
  86. geo->sectors = 0;
  87. geo->cylinders = 0;
  88. }
  89. return 0;
  90. }
  91. static const struct block_device_operations rsxx_fops = {
  92. .owner = THIS_MODULE,
  93. .getgeo = rsxx_getgeo,
  94. .ioctl = rsxx_blkdev_ioctl,
  95. };
  96. static void disk_stats_start(struct rsxx_cardinfo *card, struct bio *bio)
  97. {
  98. generic_start_io_acct(bio_data_dir(bio), bio_sectors(bio),
  99. &card->gendisk->part0);
  100. }
  101. static void disk_stats_complete(struct rsxx_cardinfo *card,
  102. struct bio *bio,
  103. unsigned long start_time)
  104. {
  105. generic_end_io_acct(bio_data_dir(bio), &card->gendisk->part0,
  106. start_time);
  107. }
  108. static void bio_dma_done_cb(struct rsxx_cardinfo *card,
  109. void *cb_data,
  110. unsigned int error)
  111. {
  112. struct rsxx_bio_meta *meta = cb_data;
  113. if (error)
  114. atomic_set(&meta->error, 1);
  115. if (atomic_dec_and_test(&meta->pending_dmas)) {
  116. if (!card->eeh_state && card->gendisk)
  117. disk_stats_complete(card, meta->bio, meta->start_time);
  118. if (atomic_read(&meta->error))
  119. bio_io_error(meta->bio);
  120. else
  121. bio_endio(meta->bio);
  122. kmem_cache_free(bio_meta_pool, meta);
  123. }
  124. }
  125. static blk_qc_t rsxx_make_request(struct request_queue *q, struct bio *bio)
  126. {
  127. struct rsxx_cardinfo *card = q->queuedata;
  128. struct rsxx_bio_meta *bio_meta;
  129. int st = -EINVAL;
  130. blk_queue_split(q, &bio, q->bio_split);
  131. might_sleep();
  132. if (!card)
  133. goto req_err;
  134. if (bio_end_sector(bio) > get_capacity(card->gendisk))
  135. goto req_err;
  136. if (unlikely(card->halt)) {
  137. st = -EFAULT;
  138. goto req_err;
  139. }
  140. if (unlikely(card->dma_fault)) {
  141. st = (-EFAULT);
  142. goto req_err;
  143. }
  144. if (bio->bi_iter.bi_size == 0) {
  145. dev_err(CARD_TO_DEV(card), "size zero BIO!\n");
  146. goto req_err;
  147. }
  148. bio_meta = kmem_cache_alloc(bio_meta_pool, GFP_KERNEL);
  149. if (!bio_meta) {
  150. st = -ENOMEM;
  151. goto req_err;
  152. }
  153. bio_meta->bio = bio;
  154. atomic_set(&bio_meta->error, 0);
  155. atomic_set(&bio_meta->pending_dmas, 0);
  156. bio_meta->start_time = jiffies;
  157. if (!unlikely(card->halt))
  158. disk_stats_start(card, bio);
  159. dev_dbg(CARD_TO_DEV(card), "BIO[%c]: meta: %p addr8: x%llx size: %d\n",
  160. bio_data_dir(bio) ? 'W' : 'R', bio_meta,
  161. (u64)bio->bi_iter.bi_sector << 9, bio->bi_iter.bi_size);
  162. st = rsxx_dma_queue_bio(card, bio, &bio_meta->pending_dmas,
  163. bio_dma_done_cb, bio_meta);
  164. if (st)
  165. goto queue_err;
  166. return BLK_QC_T_NONE;
  167. queue_err:
  168. kmem_cache_free(bio_meta_pool, bio_meta);
  169. req_err:
  170. if (st)
  171. bio->bi_error = st;
  172. bio_endio(bio);
  173. return BLK_QC_T_NONE;
  174. }
  175. /*----------------- Device Setup -------------------*/
  176. static bool rsxx_discard_supported(struct rsxx_cardinfo *card)
  177. {
  178. unsigned char pci_rev;
  179. pci_read_config_byte(card->dev, PCI_REVISION_ID, &pci_rev);
  180. return (pci_rev >= RSXX_DISCARD_SUPPORT);
  181. }
  182. int rsxx_attach_dev(struct rsxx_cardinfo *card)
  183. {
  184. mutex_lock(&card->dev_lock);
  185. /* The block device requires the stripe size from the config. */
  186. if (enable_blkdev) {
  187. if (card->config_valid)
  188. set_capacity(card->gendisk, card->size8 >> 9);
  189. else
  190. set_capacity(card->gendisk, 0);
  191. device_add_disk(CARD_TO_DEV(card), card->gendisk);
  192. card->bdev_attached = 1;
  193. }
  194. mutex_unlock(&card->dev_lock);
  195. return 0;
  196. }
  197. void rsxx_detach_dev(struct rsxx_cardinfo *card)
  198. {
  199. mutex_lock(&card->dev_lock);
  200. if (card->bdev_attached) {
  201. del_gendisk(card->gendisk);
  202. card->bdev_attached = 0;
  203. }
  204. mutex_unlock(&card->dev_lock);
  205. }
  206. int rsxx_setup_dev(struct rsxx_cardinfo *card)
  207. {
  208. unsigned short blk_size;
  209. mutex_init(&card->dev_lock);
  210. if (!enable_blkdev)
  211. return 0;
  212. card->major = register_blkdev(0, DRIVER_NAME);
  213. if (card->major < 0) {
  214. dev_err(CARD_TO_DEV(card), "Failed to get major number\n");
  215. return -ENOMEM;
  216. }
  217. card->queue = blk_alloc_queue(GFP_KERNEL);
  218. if (!card->queue) {
  219. dev_err(CARD_TO_DEV(card), "Failed queue alloc\n");
  220. unregister_blkdev(card->major, DRIVER_NAME);
  221. return -ENOMEM;
  222. }
  223. card->gendisk = alloc_disk(blkdev_minors);
  224. if (!card->gendisk) {
  225. dev_err(CARD_TO_DEV(card), "Failed disk alloc\n");
  226. blk_cleanup_queue(card->queue);
  227. unregister_blkdev(card->major, DRIVER_NAME);
  228. return -ENOMEM;
  229. }
  230. if (card->config_valid) {
  231. blk_size = card->config.data.block_size;
  232. blk_queue_dma_alignment(card->queue, blk_size - 1);
  233. blk_queue_logical_block_size(card->queue, blk_size);
  234. }
  235. blk_queue_make_request(card->queue, rsxx_make_request);
  236. blk_queue_bounce_limit(card->queue, BLK_BOUNCE_ANY);
  237. blk_queue_max_hw_sectors(card->queue, blkdev_max_hw_sectors);
  238. blk_queue_physical_block_size(card->queue, RSXX_HW_BLK_SIZE);
  239. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, card->queue);
  240. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, card->queue);
  241. if (rsxx_discard_supported(card)) {
  242. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, card->queue);
  243. blk_queue_max_discard_sectors(card->queue,
  244. RSXX_HW_BLK_SIZE >> 9);
  245. card->queue->limits.discard_granularity = RSXX_HW_BLK_SIZE;
  246. card->queue->limits.discard_alignment = RSXX_HW_BLK_SIZE;
  247. card->queue->limits.discard_zeroes_data = 1;
  248. }
  249. card->queue->queuedata = card;
  250. snprintf(card->gendisk->disk_name, sizeof(card->gendisk->disk_name),
  251. "rsxx%d", card->disk_id);
  252. card->gendisk->major = card->major;
  253. card->gendisk->first_minor = 0;
  254. card->gendisk->fops = &rsxx_fops;
  255. card->gendisk->private_data = card;
  256. card->gendisk->queue = card->queue;
  257. return 0;
  258. }
  259. void rsxx_destroy_dev(struct rsxx_cardinfo *card)
  260. {
  261. if (!enable_blkdev)
  262. return;
  263. put_disk(card->gendisk);
  264. card->gendisk = NULL;
  265. blk_cleanup_queue(card->queue);
  266. card->queue->queuedata = NULL;
  267. unregister_blkdev(card->major, DRIVER_NAME);
  268. }
  269. int rsxx_dev_init(void)
  270. {
  271. bio_meta_pool = KMEM_CACHE(rsxx_bio_meta, SLAB_HWCACHE_ALIGN);
  272. if (!bio_meta_pool)
  273. return -ENOMEM;
  274. return 0;
  275. }
  276. void rsxx_dev_cleanup(void)
  277. {
  278. kmem_cache_destroy(bio_meta_pool);
  279. }