nvme.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. /*
  2. * NVM Express device driver
  3. * Copyright (c) 2011, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/nvme.h>
  19. #include <linux/bio.h>
  20. #include <linux/bitops.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/genhd.h>
  26. #include <linux/idr.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/kthread.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/pci.h>
  37. #include <linux/poison.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <linux/version.h>
  42. #define NVME_Q_DEPTH 1024
  43. #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
  44. #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
  45. #define NVME_MINORS 64
  46. #define NVME_IO_TIMEOUT (5 * HZ)
  47. #define ADMIN_TIMEOUT (60 * HZ)
  48. static int nvme_major;
  49. module_param(nvme_major, int, 0);
  50. static int use_threaded_interrupts;
  51. module_param(use_threaded_interrupts, int, 0);
  52. static DEFINE_SPINLOCK(dev_list_lock);
  53. static LIST_HEAD(dev_list);
  54. static struct task_struct *nvme_thread;
  55. /*
  56. * Represents an NVM Express device. Each nvme_dev is a PCI function.
  57. */
  58. struct nvme_dev {
  59. struct list_head node;
  60. struct nvme_queue **queues;
  61. u32 __iomem *dbs;
  62. struct pci_dev *pci_dev;
  63. struct dma_pool *prp_page_pool;
  64. struct dma_pool *prp_small_pool;
  65. int instance;
  66. int queue_count;
  67. int db_stride;
  68. u32 ctrl_config;
  69. struct msix_entry *entry;
  70. struct nvme_bar __iomem *bar;
  71. struct list_head namespaces;
  72. char serial[20];
  73. char model[40];
  74. char firmware_rev[8];
  75. u32 max_hw_sectors;
  76. };
  77. /*
  78. * An NVM Express namespace is equivalent to a SCSI LUN
  79. */
  80. struct nvme_ns {
  81. struct list_head list;
  82. struct nvme_dev *dev;
  83. struct request_queue *queue;
  84. struct gendisk *disk;
  85. int ns_id;
  86. int lba_shift;
  87. };
  88. /*
  89. * An NVM Express queue. Each device has at least two (one for admin
  90. * commands and one for I/O commands).
  91. */
  92. struct nvme_queue {
  93. struct device *q_dmadev;
  94. struct nvme_dev *dev;
  95. spinlock_t q_lock;
  96. struct nvme_command *sq_cmds;
  97. volatile struct nvme_completion *cqes;
  98. dma_addr_t sq_dma_addr;
  99. dma_addr_t cq_dma_addr;
  100. wait_queue_head_t sq_full;
  101. wait_queue_t sq_cong_wait;
  102. struct bio_list sq_cong;
  103. u32 __iomem *q_db;
  104. u16 q_depth;
  105. u16 cq_vector;
  106. u16 sq_head;
  107. u16 sq_tail;
  108. u16 cq_head;
  109. u16 cq_phase;
  110. unsigned long cmdid_data[];
  111. };
  112. /*
  113. * Check we didin't inadvertently grow the command struct
  114. */
  115. static inline void _nvme_check_size(void)
  116. {
  117. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  118. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  119. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  120. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  121. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  122. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  123. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
  124. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
  125. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  126. }
  127. typedef void (*nvme_completion_fn)(struct nvme_dev *, void *,
  128. struct nvme_completion *);
  129. struct nvme_cmd_info {
  130. nvme_completion_fn fn;
  131. void *ctx;
  132. unsigned long timeout;
  133. };
  134. static struct nvme_cmd_info *nvme_cmd_info(struct nvme_queue *nvmeq)
  135. {
  136. return (void *)&nvmeq->cmdid_data[BITS_TO_LONGS(nvmeq->q_depth)];
  137. }
  138. /**
  139. * alloc_cmdid() - Allocate a Command ID
  140. * @nvmeq: The queue that will be used for this command
  141. * @ctx: A pointer that will be passed to the handler
  142. * @handler: The function to call on completion
  143. *
  144. * Allocate a Command ID for a queue. The data passed in will
  145. * be passed to the completion handler. This is implemented by using
  146. * the bottom two bits of the ctx pointer to store the handler ID.
  147. * Passing in a pointer that's not 4-byte aligned will cause a BUG.
  148. * We can change this if it becomes a problem.
  149. *
  150. * May be called with local interrupts disabled and the q_lock held,
  151. * or with interrupts enabled and no locks held.
  152. */
  153. static int alloc_cmdid(struct nvme_queue *nvmeq, void *ctx,
  154. nvme_completion_fn handler, unsigned timeout)
  155. {
  156. int depth = nvmeq->q_depth - 1;
  157. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  158. int cmdid;
  159. do {
  160. cmdid = find_first_zero_bit(nvmeq->cmdid_data, depth);
  161. if (cmdid >= depth)
  162. return -EBUSY;
  163. } while (test_and_set_bit(cmdid, nvmeq->cmdid_data));
  164. info[cmdid].fn = handler;
  165. info[cmdid].ctx = ctx;
  166. info[cmdid].timeout = jiffies + timeout;
  167. return cmdid;
  168. }
  169. static int alloc_cmdid_killable(struct nvme_queue *nvmeq, void *ctx,
  170. nvme_completion_fn handler, unsigned timeout)
  171. {
  172. int cmdid;
  173. wait_event_killable(nvmeq->sq_full,
  174. (cmdid = alloc_cmdid(nvmeq, ctx, handler, timeout)) >= 0);
  175. return (cmdid < 0) ? -EINTR : cmdid;
  176. }
  177. /* Special values must be less than 0x1000 */
  178. #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
  179. #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
  180. #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
  181. #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
  182. #define CMD_CTX_FLUSH (0x318 + CMD_CTX_BASE)
  183. static void special_completion(struct nvme_dev *dev, void *ctx,
  184. struct nvme_completion *cqe)
  185. {
  186. if (ctx == CMD_CTX_CANCELLED)
  187. return;
  188. if (ctx == CMD_CTX_FLUSH)
  189. return;
  190. if (ctx == CMD_CTX_COMPLETED) {
  191. dev_warn(&dev->pci_dev->dev,
  192. "completed id %d twice on queue %d\n",
  193. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  194. return;
  195. }
  196. if (ctx == CMD_CTX_INVALID) {
  197. dev_warn(&dev->pci_dev->dev,
  198. "invalid id %d completed on queue %d\n",
  199. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  200. return;
  201. }
  202. dev_warn(&dev->pci_dev->dev, "Unknown special completion %p\n", ctx);
  203. }
  204. /*
  205. * Called with local interrupts disabled and the q_lock held. May not sleep.
  206. */
  207. static void *free_cmdid(struct nvme_queue *nvmeq, int cmdid,
  208. nvme_completion_fn *fn)
  209. {
  210. void *ctx;
  211. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  212. if (cmdid >= nvmeq->q_depth) {
  213. *fn = special_completion;
  214. return CMD_CTX_INVALID;
  215. }
  216. *fn = info[cmdid].fn;
  217. ctx = info[cmdid].ctx;
  218. info[cmdid].fn = special_completion;
  219. info[cmdid].ctx = CMD_CTX_COMPLETED;
  220. clear_bit(cmdid, nvmeq->cmdid_data);
  221. wake_up(&nvmeq->sq_full);
  222. return ctx;
  223. }
  224. static void *cancel_cmdid(struct nvme_queue *nvmeq, int cmdid,
  225. nvme_completion_fn *fn)
  226. {
  227. void *ctx;
  228. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  229. if (fn)
  230. *fn = info[cmdid].fn;
  231. ctx = info[cmdid].ctx;
  232. info[cmdid].fn = special_completion;
  233. info[cmdid].ctx = CMD_CTX_CANCELLED;
  234. return ctx;
  235. }
  236. static struct nvme_queue *get_nvmeq(struct nvme_dev *dev)
  237. {
  238. return dev->queues[get_cpu() + 1];
  239. }
  240. static void put_nvmeq(struct nvme_queue *nvmeq)
  241. {
  242. put_cpu();
  243. }
  244. /**
  245. * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  246. * @nvmeq: The queue to use
  247. * @cmd: The command to send
  248. *
  249. * Safe to use from interrupt context
  250. */
  251. static int nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
  252. {
  253. unsigned long flags;
  254. u16 tail;
  255. spin_lock_irqsave(&nvmeq->q_lock, flags);
  256. tail = nvmeq->sq_tail;
  257. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  258. if (++tail == nvmeq->q_depth)
  259. tail = 0;
  260. writel(tail, nvmeq->q_db);
  261. nvmeq->sq_tail = tail;
  262. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  263. return 0;
  264. }
  265. /*
  266. * The nvme_iod describes the data in an I/O, including the list of PRP
  267. * entries. You can't see it in this data structure because C doesn't let
  268. * me express that. Use nvme_alloc_iod to ensure there's enough space
  269. * allocated to store the PRP list.
  270. */
  271. struct nvme_iod {
  272. void *private; /* For the use of the submitter of the I/O */
  273. int npages; /* In the PRP list. 0 means small pool in use */
  274. int offset; /* Of PRP list */
  275. int nents; /* Used in scatterlist */
  276. int length; /* Of data, in bytes */
  277. dma_addr_t first_dma;
  278. struct scatterlist sg[0];
  279. };
  280. static __le64 **iod_list(struct nvme_iod *iod)
  281. {
  282. return ((void *)iod) + iod->offset;
  283. }
  284. /*
  285. * Will slightly overestimate the number of pages needed. This is OK
  286. * as it only leads to a small amount of wasted memory for the lifetime of
  287. * the I/O.
  288. */
  289. static int nvme_npages(unsigned size)
  290. {
  291. unsigned nprps = DIV_ROUND_UP(size + PAGE_SIZE, PAGE_SIZE);
  292. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  293. }
  294. static struct nvme_iod *
  295. nvme_alloc_iod(unsigned nseg, unsigned nbytes, gfp_t gfp)
  296. {
  297. struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
  298. sizeof(__le64 *) * nvme_npages(nbytes) +
  299. sizeof(struct scatterlist) * nseg, gfp);
  300. if (iod) {
  301. iod->offset = offsetof(struct nvme_iod, sg[nseg]);
  302. iod->npages = -1;
  303. iod->length = nbytes;
  304. }
  305. return iod;
  306. }
  307. static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
  308. {
  309. const int last_prp = PAGE_SIZE / 8 - 1;
  310. int i;
  311. __le64 **list = iod_list(iod);
  312. dma_addr_t prp_dma = iod->first_dma;
  313. if (iod->npages == 0)
  314. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  315. for (i = 0; i < iod->npages; i++) {
  316. __le64 *prp_list = list[i];
  317. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  318. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  319. prp_dma = next_prp_dma;
  320. }
  321. kfree(iod);
  322. }
  323. static void requeue_bio(struct nvme_dev *dev, struct bio *bio)
  324. {
  325. struct nvme_queue *nvmeq = get_nvmeq(dev);
  326. if (bio_list_empty(&nvmeq->sq_cong))
  327. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  328. bio_list_add(&nvmeq->sq_cong, bio);
  329. put_nvmeq(nvmeq);
  330. wake_up_process(nvme_thread);
  331. }
  332. static void bio_completion(struct nvme_dev *dev, void *ctx,
  333. struct nvme_completion *cqe)
  334. {
  335. struct nvme_iod *iod = ctx;
  336. struct bio *bio = iod->private;
  337. u16 status = le16_to_cpup(&cqe->status) >> 1;
  338. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  339. bio_data_dir(bio) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  340. nvme_free_iod(dev, iod);
  341. if (status) {
  342. bio_endio(bio, -EIO);
  343. } else if (bio->bi_vcnt > bio->bi_idx) {
  344. requeue_bio(dev, bio);
  345. } else {
  346. bio_endio(bio, 0);
  347. }
  348. }
  349. /* length is in bytes. gfp flags indicates whether we may sleep. */
  350. static int nvme_setup_prps(struct nvme_dev *dev,
  351. struct nvme_common_command *cmd, struct nvme_iod *iod,
  352. int total_len, gfp_t gfp)
  353. {
  354. struct dma_pool *pool;
  355. int length = total_len;
  356. struct scatterlist *sg = iod->sg;
  357. int dma_len = sg_dma_len(sg);
  358. u64 dma_addr = sg_dma_address(sg);
  359. int offset = offset_in_page(dma_addr);
  360. __le64 *prp_list;
  361. __le64 **list = iod_list(iod);
  362. dma_addr_t prp_dma;
  363. int nprps, i;
  364. cmd->prp1 = cpu_to_le64(dma_addr);
  365. length -= (PAGE_SIZE - offset);
  366. if (length <= 0)
  367. return total_len;
  368. dma_len -= (PAGE_SIZE - offset);
  369. if (dma_len) {
  370. dma_addr += (PAGE_SIZE - offset);
  371. } else {
  372. sg = sg_next(sg);
  373. dma_addr = sg_dma_address(sg);
  374. dma_len = sg_dma_len(sg);
  375. }
  376. if (length <= PAGE_SIZE) {
  377. cmd->prp2 = cpu_to_le64(dma_addr);
  378. return total_len;
  379. }
  380. nprps = DIV_ROUND_UP(length, PAGE_SIZE);
  381. if (nprps <= (256 / 8)) {
  382. pool = dev->prp_small_pool;
  383. iod->npages = 0;
  384. } else {
  385. pool = dev->prp_page_pool;
  386. iod->npages = 1;
  387. }
  388. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  389. if (!prp_list) {
  390. cmd->prp2 = cpu_to_le64(dma_addr);
  391. iod->npages = -1;
  392. return (total_len - length) + PAGE_SIZE;
  393. }
  394. list[0] = prp_list;
  395. iod->first_dma = prp_dma;
  396. cmd->prp2 = cpu_to_le64(prp_dma);
  397. i = 0;
  398. for (;;) {
  399. if (i == PAGE_SIZE / 8) {
  400. __le64 *old_prp_list = prp_list;
  401. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  402. if (!prp_list)
  403. return total_len - length;
  404. list[iod->npages++] = prp_list;
  405. prp_list[0] = old_prp_list[i - 1];
  406. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  407. i = 1;
  408. }
  409. prp_list[i++] = cpu_to_le64(dma_addr);
  410. dma_len -= PAGE_SIZE;
  411. dma_addr += PAGE_SIZE;
  412. length -= PAGE_SIZE;
  413. if (length <= 0)
  414. break;
  415. if (dma_len > 0)
  416. continue;
  417. BUG_ON(dma_len < 0);
  418. sg = sg_next(sg);
  419. dma_addr = sg_dma_address(sg);
  420. dma_len = sg_dma_len(sg);
  421. }
  422. return total_len;
  423. }
  424. /* NVMe scatterlists require no holes in the virtual address */
  425. #define BIOVEC_NOT_VIRT_MERGEABLE(vec1, vec2) ((vec2)->bv_offset || \
  426. (((vec1)->bv_offset + (vec1)->bv_len) % PAGE_SIZE))
  427. static int nvme_map_bio(struct device *dev, struct nvme_iod *iod,
  428. struct bio *bio, enum dma_data_direction dma_dir, int psegs)
  429. {
  430. struct bio_vec *bvec, *bvprv = NULL;
  431. struct scatterlist *sg = NULL;
  432. int i, old_idx, length = 0, nsegs = 0;
  433. sg_init_table(iod->sg, psegs);
  434. old_idx = bio->bi_idx;
  435. bio_for_each_segment(bvec, bio, i) {
  436. if (bvprv && BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) {
  437. sg->length += bvec->bv_len;
  438. } else {
  439. if (bvprv && BIOVEC_NOT_VIRT_MERGEABLE(bvprv, bvec))
  440. break;
  441. sg = sg ? sg + 1 : iod->sg;
  442. sg_set_page(sg, bvec->bv_page, bvec->bv_len,
  443. bvec->bv_offset);
  444. nsegs++;
  445. }
  446. length += bvec->bv_len;
  447. bvprv = bvec;
  448. }
  449. bio->bi_idx = i;
  450. iod->nents = nsegs;
  451. sg_mark_end(sg);
  452. if (dma_map_sg(dev, iod->sg, iod->nents, dma_dir) == 0) {
  453. bio->bi_idx = old_idx;
  454. return -ENOMEM;
  455. }
  456. return length;
  457. }
  458. static int nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  459. int cmdid)
  460. {
  461. struct nvme_command *cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  462. memset(cmnd, 0, sizeof(*cmnd));
  463. cmnd->common.opcode = nvme_cmd_flush;
  464. cmnd->common.command_id = cmdid;
  465. cmnd->common.nsid = cpu_to_le32(ns->ns_id);
  466. if (++nvmeq->sq_tail == nvmeq->q_depth)
  467. nvmeq->sq_tail = 0;
  468. writel(nvmeq->sq_tail, nvmeq->q_db);
  469. return 0;
  470. }
  471. static int nvme_submit_flush_data(struct nvme_queue *nvmeq, struct nvme_ns *ns)
  472. {
  473. int cmdid = alloc_cmdid(nvmeq, (void *)CMD_CTX_FLUSH,
  474. special_completion, NVME_IO_TIMEOUT);
  475. if (unlikely(cmdid < 0))
  476. return cmdid;
  477. return nvme_submit_flush(nvmeq, ns, cmdid);
  478. }
  479. /*
  480. * Called with local interrupts disabled and the q_lock held. May not sleep.
  481. */
  482. static int nvme_submit_bio_queue(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  483. struct bio *bio)
  484. {
  485. struct nvme_command *cmnd;
  486. struct nvme_iod *iod;
  487. enum dma_data_direction dma_dir;
  488. int cmdid, length, result = -ENOMEM;
  489. u16 control;
  490. u32 dsmgmt;
  491. int psegs = bio_phys_segments(ns->queue, bio);
  492. if ((bio->bi_rw & REQ_FLUSH) && psegs) {
  493. result = nvme_submit_flush_data(nvmeq, ns);
  494. if (result)
  495. return result;
  496. }
  497. iod = nvme_alloc_iod(psegs, bio->bi_size, GFP_ATOMIC);
  498. if (!iod)
  499. goto nomem;
  500. iod->private = bio;
  501. result = -EBUSY;
  502. cmdid = alloc_cmdid(nvmeq, iod, bio_completion, NVME_IO_TIMEOUT);
  503. if (unlikely(cmdid < 0))
  504. goto free_iod;
  505. if ((bio->bi_rw & REQ_FLUSH) && !psegs)
  506. return nvme_submit_flush(nvmeq, ns, cmdid);
  507. control = 0;
  508. if (bio->bi_rw & REQ_FUA)
  509. control |= NVME_RW_FUA;
  510. if (bio->bi_rw & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  511. control |= NVME_RW_LR;
  512. dsmgmt = 0;
  513. if (bio->bi_rw & REQ_RAHEAD)
  514. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  515. cmnd = &nvmeq->sq_cmds[nvmeq->sq_tail];
  516. memset(cmnd, 0, sizeof(*cmnd));
  517. if (bio_data_dir(bio)) {
  518. cmnd->rw.opcode = nvme_cmd_write;
  519. dma_dir = DMA_TO_DEVICE;
  520. } else {
  521. cmnd->rw.opcode = nvme_cmd_read;
  522. dma_dir = DMA_FROM_DEVICE;
  523. }
  524. result = nvme_map_bio(nvmeq->q_dmadev, iod, bio, dma_dir, psegs);
  525. if (result < 0)
  526. goto free_iod;
  527. length = result;
  528. cmnd->rw.command_id = cmdid;
  529. cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
  530. length = nvme_setup_prps(nvmeq->dev, &cmnd->common, iod, length,
  531. GFP_ATOMIC);
  532. cmnd->rw.slba = cpu_to_le64(bio->bi_sector >> (ns->lba_shift - 9));
  533. cmnd->rw.length = cpu_to_le16((length >> ns->lba_shift) - 1);
  534. cmnd->rw.control = cpu_to_le16(control);
  535. cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
  536. bio->bi_sector += length >> 9;
  537. if (++nvmeq->sq_tail == nvmeq->q_depth)
  538. nvmeq->sq_tail = 0;
  539. writel(nvmeq->sq_tail, nvmeq->q_db);
  540. return 0;
  541. free_iod:
  542. nvme_free_iod(nvmeq->dev, iod);
  543. nomem:
  544. return result;
  545. }
  546. /*
  547. * NB: return value of non-zero would mean that we were a stacking driver.
  548. * make_request must always succeed.
  549. */
  550. static int nvme_make_request(struct request_queue *q, struct bio *bio)
  551. {
  552. struct nvme_ns *ns = q->queuedata;
  553. struct nvme_queue *nvmeq = get_nvmeq(ns->dev);
  554. int result = -EBUSY;
  555. spin_lock_irq(&nvmeq->q_lock);
  556. if (bio_list_empty(&nvmeq->sq_cong))
  557. result = nvme_submit_bio_queue(nvmeq, ns, bio);
  558. if (unlikely(result)) {
  559. if (bio_list_empty(&nvmeq->sq_cong))
  560. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  561. bio_list_add(&nvmeq->sq_cong, bio);
  562. }
  563. spin_unlock_irq(&nvmeq->q_lock);
  564. put_nvmeq(nvmeq);
  565. return 0;
  566. }
  567. static irqreturn_t nvme_process_cq(struct nvme_queue *nvmeq)
  568. {
  569. u16 head, phase;
  570. head = nvmeq->cq_head;
  571. phase = nvmeq->cq_phase;
  572. for (;;) {
  573. void *ctx;
  574. nvme_completion_fn fn;
  575. struct nvme_completion cqe = nvmeq->cqes[head];
  576. if ((le16_to_cpu(cqe.status) & 1) != phase)
  577. break;
  578. nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
  579. if (++head == nvmeq->q_depth) {
  580. head = 0;
  581. phase = !phase;
  582. }
  583. ctx = free_cmdid(nvmeq, cqe.command_id, &fn);
  584. fn(nvmeq->dev, ctx, &cqe);
  585. }
  586. /* If the controller ignores the cq head doorbell and continuously
  587. * writes to the queue, it is theoretically possible to wrap around
  588. * the queue twice and mistakenly return IRQ_NONE. Linux only
  589. * requires that 0.1% of your interrupts are handled, so this isn't
  590. * a big problem.
  591. */
  592. if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
  593. return IRQ_NONE;
  594. writel(head, nvmeq->q_db + (1 << nvmeq->dev->db_stride));
  595. nvmeq->cq_head = head;
  596. nvmeq->cq_phase = phase;
  597. return IRQ_HANDLED;
  598. }
  599. static irqreturn_t nvme_irq(int irq, void *data)
  600. {
  601. irqreturn_t result;
  602. struct nvme_queue *nvmeq = data;
  603. spin_lock(&nvmeq->q_lock);
  604. result = nvme_process_cq(nvmeq);
  605. spin_unlock(&nvmeq->q_lock);
  606. return result;
  607. }
  608. static irqreturn_t nvme_irq_check(int irq, void *data)
  609. {
  610. struct nvme_queue *nvmeq = data;
  611. struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
  612. if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
  613. return IRQ_NONE;
  614. return IRQ_WAKE_THREAD;
  615. }
  616. static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
  617. {
  618. spin_lock_irq(&nvmeq->q_lock);
  619. cancel_cmdid(nvmeq, cmdid, NULL);
  620. spin_unlock_irq(&nvmeq->q_lock);
  621. }
  622. struct sync_cmd_info {
  623. struct task_struct *task;
  624. u32 result;
  625. int status;
  626. };
  627. static void sync_completion(struct nvme_dev *dev, void *ctx,
  628. struct nvme_completion *cqe)
  629. {
  630. struct sync_cmd_info *cmdinfo = ctx;
  631. cmdinfo->result = le32_to_cpup(&cqe->result);
  632. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  633. wake_up_process(cmdinfo->task);
  634. }
  635. /*
  636. * Returns 0 on success. If the result is negative, it's a Linux error code;
  637. * if the result is positive, it's an NVM Express status code
  638. */
  639. static int nvme_submit_sync_cmd(struct nvme_queue *nvmeq,
  640. struct nvme_command *cmd, u32 *result, unsigned timeout)
  641. {
  642. int cmdid;
  643. struct sync_cmd_info cmdinfo;
  644. cmdinfo.task = current;
  645. cmdinfo.status = -EINTR;
  646. cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
  647. timeout);
  648. if (cmdid < 0)
  649. return cmdid;
  650. cmd->common.command_id = cmdid;
  651. set_current_state(TASK_KILLABLE);
  652. nvme_submit_cmd(nvmeq, cmd);
  653. schedule();
  654. if (cmdinfo.status == -EINTR) {
  655. nvme_abort_command(nvmeq, cmdid);
  656. return -EINTR;
  657. }
  658. if (result)
  659. *result = cmdinfo.result;
  660. return cmdinfo.status;
  661. }
  662. static int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
  663. u32 *result)
  664. {
  665. return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
  666. }
  667. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  668. {
  669. int status;
  670. struct nvme_command c;
  671. memset(&c, 0, sizeof(c));
  672. c.delete_queue.opcode = opcode;
  673. c.delete_queue.qid = cpu_to_le16(id);
  674. status = nvme_submit_admin_cmd(dev, &c, NULL);
  675. if (status)
  676. return -EIO;
  677. return 0;
  678. }
  679. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  680. struct nvme_queue *nvmeq)
  681. {
  682. int status;
  683. struct nvme_command c;
  684. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  685. memset(&c, 0, sizeof(c));
  686. c.create_cq.opcode = nvme_admin_create_cq;
  687. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  688. c.create_cq.cqid = cpu_to_le16(qid);
  689. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  690. c.create_cq.cq_flags = cpu_to_le16(flags);
  691. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  692. status = nvme_submit_admin_cmd(dev, &c, NULL);
  693. if (status)
  694. return -EIO;
  695. return 0;
  696. }
  697. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  698. struct nvme_queue *nvmeq)
  699. {
  700. int status;
  701. struct nvme_command c;
  702. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  703. memset(&c, 0, sizeof(c));
  704. c.create_sq.opcode = nvme_admin_create_sq;
  705. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  706. c.create_sq.sqid = cpu_to_le16(qid);
  707. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  708. c.create_sq.sq_flags = cpu_to_le16(flags);
  709. c.create_sq.cqid = cpu_to_le16(qid);
  710. status = nvme_submit_admin_cmd(dev, &c, NULL);
  711. if (status)
  712. return -EIO;
  713. return 0;
  714. }
  715. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  716. {
  717. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  718. }
  719. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  720. {
  721. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  722. }
  723. static int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
  724. dma_addr_t dma_addr)
  725. {
  726. struct nvme_command c;
  727. memset(&c, 0, sizeof(c));
  728. c.identify.opcode = nvme_admin_identify;
  729. c.identify.nsid = cpu_to_le32(nsid);
  730. c.identify.prp1 = cpu_to_le64(dma_addr);
  731. c.identify.cns = cpu_to_le32(cns);
  732. return nvme_submit_admin_cmd(dev, &c, NULL);
  733. }
  734. static int nvme_get_features(struct nvme_dev *dev, unsigned fid,
  735. unsigned nsid, dma_addr_t dma_addr)
  736. {
  737. struct nvme_command c;
  738. memset(&c, 0, sizeof(c));
  739. c.features.opcode = nvme_admin_get_features;
  740. c.features.nsid = cpu_to_le32(nsid);
  741. c.features.prp1 = cpu_to_le64(dma_addr);
  742. c.features.fid = cpu_to_le32(fid);
  743. return nvme_submit_admin_cmd(dev, &c, NULL);
  744. }
  745. static int nvme_set_features(struct nvme_dev *dev, unsigned fid,
  746. unsigned dword11, dma_addr_t dma_addr, u32 *result)
  747. {
  748. struct nvme_command c;
  749. memset(&c, 0, sizeof(c));
  750. c.features.opcode = nvme_admin_set_features;
  751. c.features.prp1 = cpu_to_le64(dma_addr);
  752. c.features.fid = cpu_to_le32(fid);
  753. c.features.dword11 = cpu_to_le32(dword11);
  754. return nvme_submit_admin_cmd(dev, &c, result);
  755. }
  756. /**
  757. * nvme_cancel_ios - Cancel outstanding I/Os
  758. * @queue: The queue to cancel I/Os on
  759. * @timeout: True to only cancel I/Os which have timed out
  760. */
  761. static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
  762. {
  763. int depth = nvmeq->q_depth - 1;
  764. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  765. unsigned long now = jiffies;
  766. int cmdid;
  767. for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
  768. void *ctx;
  769. nvme_completion_fn fn;
  770. static struct nvme_completion cqe = {
  771. .status = cpu_to_le16(NVME_SC_ABORT_REQ) << 1,
  772. };
  773. if (timeout && !time_after(now, info[cmdid].timeout))
  774. continue;
  775. dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
  776. ctx = cancel_cmdid(nvmeq, cmdid, &fn);
  777. fn(nvmeq->dev, ctx, &cqe);
  778. }
  779. }
  780. static void nvme_free_queue_mem(struct nvme_queue *nvmeq)
  781. {
  782. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  783. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  784. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  785. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  786. kfree(nvmeq);
  787. }
  788. static void nvme_free_queue(struct nvme_dev *dev, int qid)
  789. {
  790. struct nvme_queue *nvmeq = dev->queues[qid];
  791. int vector = dev->entry[nvmeq->cq_vector].vector;
  792. spin_lock_irq(&nvmeq->q_lock);
  793. nvme_cancel_ios(nvmeq, false);
  794. spin_unlock_irq(&nvmeq->q_lock);
  795. irq_set_affinity_hint(vector, NULL);
  796. free_irq(vector, nvmeq);
  797. /* Don't tell the adapter to delete the admin queue */
  798. if (qid) {
  799. adapter_delete_sq(dev, qid);
  800. adapter_delete_cq(dev, qid);
  801. }
  802. nvme_free_queue_mem(nvmeq);
  803. }
  804. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  805. int depth, int vector)
  806. {
  807. struct device *dmadev = &dev->pci_dev->dev;
  808. unsigned extra = DIV_ROUND_UP(depth, 8) + (depth *
  809. sizeof(struct nvme_cmd_info));
  810. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
  811. if (!nvmeq)
  812. return NULL;
  813. nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
  814. &nvmeq->cq_dma_addr, GFP_KERNEL);
  815. if (!nvmeq->cqes)
  816. goto free_nvmeq;
  817. memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
  818. nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
  819. &nvmeq->sq_dma_addr, GFP_KERNEL);
  820. if (!nvmeq->sq_cmds)
  821. goto free_cqdma;
  822. nvmeq->q_dmadev = dmadev;
  823. nvmeq->dev = dev;
  824. spin_lock_init(&nvmeq->q_lock);
  825. nvmeq->cq_head = 0;
  826. nvmeq->cq_phase = 1;
  827. init_waitqueue_head(&nvmeq->sq_full);
  828. init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
  829. bio_list_init(&nvmeq->sq_cong);
  830. nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
  831. nvmeq->q_depth = depth;
  832. nvmeq->cq_vector = vector;
  833. return nvmeq;
  834. free_cqdma:
  835. dma_free_coherent(dmadev, CQ_SIZE(nvmeq->q_depth), (void *)nvmeq->cqes,
  836. nvmeq->cq_dma_addr);
  837. free_nvmeq:
  838. kfree(nvmeq);
  839. return NULL;
  840. }
  841. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  842. const char *name)
  843. {
  844. if (use_threaded_interrupts)
  845. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  846. nvme_irq_check, nvme_irq,
  847. IRQF_DISABLED | IRQF_SHARED,
  848. name, nvmeq);
  849. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  850. IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
  851. }
  852. static __devinit struct nvme_queue *nvme_create_queue(struct nvme_dev *dev,
  853. int qid, int cq_size, int vector)
  854. {
  855. int result;
  856. struct nvme_queue *nvmeq = nvme_alloc_queue(dev, qid, cq_size, vector);
  857. if (!nvmeq)
  858. return ERR_PTR(-ENOMEM);
  859. result = adapter_alloc_cq(dev, qid, nvmeq);
  860. if (result < 0)
  861. goto free_nvmeq;
  862. result = adapter_alloc_sq(dev, qid, nvmeq);
  863. if (result < 0)
  864. goto release_cq;
  865. result = queue_request_irq(dev, nvmeq, "nvme");
  866. if (result < 0)
  867. goto release_sq;
  868. return nvmeq;
  869. release_sq:
  870. adapter_delete_sq(dev, qid);
  871. release_cq:
  872. adapter_delete_cq(dev, qid);
  873. free_nvmeq:
  874. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  875. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  876. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  877. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  878. kfree(nvmeq);
  879. return ERR_PTR(result);
  880. }
  881. static int __devinit nvme_configure_admin_queue(struct nvme_dev *dev)
  882. {
  883. int result = 0;
  884. u32 aqa;
  885. u64 cap;
  886. unsigned long timeout;
  887. struct nvme_queue *nvmeq;
  888. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  889. nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
  890. if (!nvmeq)
  891. return -ENOMEM;
  892. aqa = nvmeq->q_depth - 1;
  893. aqa |= aqa << 16;
  894. dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
  895. dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
  896. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  897. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  898. writel(0, &dev->bar->cc);
  899. writel(aqa, &dev->bar->aqa);
  900. writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  901. writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  902. writel(dev->ctrl_config, &dev->bar->cc);
  903. cap = readq(&dev->bar->cap);
  904. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  905. dev->db_stride = NVME_CAP_STRIDE(cap);
  906. while (!result && !(readl(&dev->bar->csts) & NVME_CSTS_RDY)) {
  907. msleep(100);
  908. if (fatal_signal_pending(current))
  909. result = -EINTR;
  910. if (time_after(jiffies, timeout)) {
  911. dev_err(&dev->pci_dev->dev,
  912. "Device not ready; aborting initialisation\n");
  913. result = -ENODEV;
  914. }
  915. }
  916. if (result) {
  917. nvme_free_queue_mem(nvmeq);
  918. return result;
  919. }
  920. result = queue_request_irq(dev, nvmeq, "nvme admin");
  921. dev->queues[0] = nvmeq;
  922. return result;
  923. }
  924. static struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
  925. unsigned long addr, unsigned length)
  926. {
  927. int i, err, count, nents, offset;
  928. struct scatterlist *sg;
  929. struct page **pages;
  930. struct nvme_iod *iod;
  931. if (addr & 3)
  932. return ERR_PTR(-EINVAL);
  933. if (!length)
  934. return ERR_PTR(-EINVAL);
  935. offset = offset_in_page(addr);
  936. count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  937. pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
  938. if (!pages)
  939. return ERR_PTR(-ENOMEM);
  940. err = get_user_pages_fast(addr, count, 1, pages);
  941. if (err < count) {
  942. count = err;
  943. err = -EFAULT;
  944. goto put_pages;
  945. }
  946. iod = nvme_alloc_iod(count, length, GFP_KERNEL);
  947. sg = iod->sg;
  948. sg_init_table(sg, count);
  949. for (i = 0; i < count; i++) {
  950. sg_set_page(&sg[i], pages[i],
  951. min_t(int, length, PAGE_SIZE - offset), offset);
  952. length -= (PAGE_SIZE - offset);
  953. offset = 0;
  954. }
  955. sg_mark_end(&sg[i - 1]);
  956. iod->nents = count;
  957. err = -ENOMEM;
  958. nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
  959. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  960. if (!nents)
  961. goto free_iod;
  962. kfree(pages);
  963. return iod;
  964. free_iod:
  965. kfree(iod);
  966. put_pages:
  967. for (i = 0; i < count; i++)
  968. put_page(pages[i]);
  969. kfree(pages);
  970. return ERR_PTR(err);
  971. }
  972. static void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
  973. struct nvme_iod *iod)
  974. {
  975. int i;
  976. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  977. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  978. for (i = 0; i < iod->nents; i++)
  979. put_page(sg_page(&iod->sg[i]));
  980. }
  981. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  982. {
  983. struct nvme_dev *dev = ns->dev;
  984. struct nvme_queue *nvmeq;
  985. struct nvme_user_io io;
  986. struct nvme_command c;
  987. unsigned length;
  988. int status;
  989. struct nvme_iod *iod;
  990. if (copy_from_user(&io, uio, sizeof(io)))
  991. return -EFAULT;
  992. length = (io.nblocks + 1) << ns->lba_shift;
  993. switch (io.opcode) {
  994. case nvme_cmd_write:
  995. case nvme_cmd_read:
  996. case nvme_cmd_compare:
  997. iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
  998. break;
  999. default:
  1000. return -EINVAL;
  1001. }
  1002. if (IS_ERR(iod))
  1003. return PTR_ERR(iod);
  1004. memset(&c, 0, sizeof(c));
  1005. c.rw.opcode = io.opcode;
  1006. c.rw.flags = io.flags;
  1007. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1008. c.rw.slba = cpu_to_le64(io.slba);
  1009. c.rw.length = cpu_to_le16(io.nblocks);
  1010. c.rw.control = cpu_to_le16(io.control);
  1011. c.rw.dsmgmt = cpu_to_le16(io.dsmgmt);
  1012. c.rw.reftag = io.reftag;
  1013. c.rw.apptag = io.apptag;
  1014. c.rw.appmask = io.appmask;
  1015. /* XXX: metadata */
  1016. length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
  1017. nvmeq = get_nvmeq(dev);
  1018. /*
  1019. * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
  1020. * disabled. We may be preempted at any point, and be rescheduled
  1021. * to a different CPU. That will cause cacheline bouncing, but no
  1022. * additional races since q_lock already protects against other CPUs.
  1023. */
  1024. put_nvmeq(nvmeq);
  1025. if (length != (io.nblocks + 1) << ns->lba_shift)
  1026. status = -ENOMEM;
  1027. else
  1028. status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
  1029. nvme_unmap_user_pages(dev, io.opcode & 1, iod);
  1030. nvme_free_iod(dev, iod);
  1031. return status;
  1032. }
  1033. static int nvme_user_admin_cmd(struct nvme_dev *dev,
  1034. struct nvme_admin_cmd __user *ucmd)
  1035. {
  1036. struct nvme_admin_cmd cmd;
  1037. struct nvme_command c;
  1038. int status, length;
  1039. struct nvme_iod *uninitialized_var(iod);
  1040. if (!capable(CAP_SYS_ADMIN))
  1041. return -EACCES;
  1042. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1043. return -EFAULT;
  1044. memset(&c, 0, sizeof(c));
  1045. c.common.opcode = cmd.opcode;
  1046. c.common.flags = cmd.flags;
  1047. c.common.nsid = cpu_to_le32(cmd.nsid);
  1048. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1049. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1050. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1051. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1052. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1053. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1054. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1055. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1056. length = cmd.data_len;
  1057. if (cmd.data_len) {
  1058. iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
  1059. length);
  1060. if (IS_ERR(iod))
  1061. return PTR_ERR(iod);
  1062. length = nvme_setup_prps(dev, &c.common, iod, length,
  1063. GFP_KERNEL);
  1064. }
  1065. if (length != cmd.data_len)
  1066. status = -ENOMEM;
  1067. else
  1068. status = nvme_submit_admin_cmd(dev, &c, NULL);
  1069. if (cmd.data_len) {
  1070. nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
  1071. nvme_free_iod(dev, iod);
  1072. }
  1073. return status;
  1074. }
  1075. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1076. unsigned long arg)
  1077. {
  1078. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1079. switch (cmd) {
  1080. case NVME_IOCTL_ID:
  1081. return ns->ns_id;
  1082. case NVME_IOCTL_ADMIN_CMD:
  1083. return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
  1084. case NVME_IOCTL_SUBMIT_IO:
  1085. return nvme_submit_io(ns, (void __user *)arg);
  1086. default:
  1087. return -ENOTTY;
  1088. }
  1089. }
  1090. static const struct block_device_operations nvme_fops = {
  1091. .owner = THIS_MODULE,
  1092. .ioctl = nvme_ioctl,
  1093. .compat_ioctl = nvme_ioctl,
  1094. };
  1095. static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
  1096. {
  1097. while (bio_list_peek(&nvmeq->sq_cong)) {
  1098. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1099. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  1100. if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
  1101. bio_list_add_head(&nvmeq->sq_cong, bio);
  1102. break;
  1103. }
  1104. if (bio_list_empty(&nvmeq->sq_cong))
  1105. remove_wait_queue(&nvmeq->sq_full,
  1106. &nvmeq->sq_cong_wait);
  1107. }
  1108. }
  1109. static int nvme_kthread(void *data)
  1110. {
  1111. struct nvme_dev *dev;
  1112. while (!kthread_should_stop()) {
  1113. __set_current_state(TASK_RUNNING);
  1114. spin_lock(&dev_list_lock);
  1115. list_for_each_entry(dev, &dev_list, node) {
  1116. int i;
  1117. for (i = 0; i < dev->queue_count; i++) {
  1118. struct nvme_queue *nvmeq = dev->queues[i];
  1119. if (!nvmeq)
  1120. continue;
  1121. spin_lock_irq(&nvmeq->q_lock);
  1122. if (nvme_process_cq(nvmeq))
  1123. printk("process_cq did something\n");
  1124. nvme_cancel_ios(nvmeq, true);
  1125. nvme_resubmit_bios(nvmeq);
  1126. spin_unlock_irq(&nvmeq->q_lock);
  1127. }
  1128. }
  1129. spin_unlock(&dev_list_lock);
  1130. set_current_state(TASK_INTERRUPTIBLE);
  1131. schedule_timeout(HZ);
  1132. }
  1133. return 0;
  1134. }
  1135. static DEFINE_IDA(nvme_index_ida);
  1136. static int nvme_get_ns_idx(void)
  1137. {
  1138. int index, error;
  1139. do {
  1140. if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
  1141. return -1;
  1142. spin_lock(&dev_list_lock);
  1143. error = ida_get_new(&nvme_index_ida, &index);
  1144. spin_unlock(&dev_list_lock);
  1145. } while (error == -EAGAIN);
  1146. if (error)
  1147. index = -1;
  1148. return index;
  1149. }
  1150. static void nvme_put_ns_idx(int index)
  1151. {
  1152. spin_lock(&dev_list_lock);
  1153. ida_remove(&nvme_index_ida, index);
  1154. spin_unlock(&dev_list_lock);
  1155. }
  1156. static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, int nsid,
  1157. struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
  1158. {
  1159. struct nvme_ns *ns;
  1160. struct gendisk *disk;
  1161. int lbaf;
  1162. if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
  1163. return NULL;
  1164. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  1165. if (!ns)
  1166. return NULL;
  1167. ns->queue = blk_alloc_queue(GFP_KERNEL);
  1168. if (!ns->queue)
  1169. goto out_free_ns;
  1170. ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
  1171. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1172. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1173. /* queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue); */
  1174. blk_queue_make_request(ns->queue, nvme_make_request);
  1175. ns->dev = dev;
  1176. ns->queue->queuedata = ns;
  1177. disk = alloc_disk(NVME_MINORS);
  1178. if (!disk)
  1179. goto out_free_queue;
  1180. ns->ns_id = nsid;
  1181. ns->disk = disk;
  1182. lbaf = id->flbas & 0xf;
  1183. ns->lba_shift = id->lbaf[lbaf].ds;
  1184. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1185. if (dev->max_hw_sectors)
  1186. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1187. disk->major = nvme_major;
  1188. disk->minors = NVME_MINORS;
  1189. disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
  1190. disk->fops = &nvme_fops;
  1191. disk->private_data = ns;
  1192. disk->queue = ns->queue;
  1193. disk->driverfs_dev = &dev->pci_dev->dev;
  1194. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1195. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1196. return ns;
  1197. out_free_queue:
  1198. blk_cleanup_queue(ns->queue);
  1199. out_free_ns:
  1200. kfree(ns);
  1201. return NULL;
  1202. }
  1203. static void nvme_ns_free(struct nvme_ns *ns)
  1204. {
  1205. int index = ns->disk->first_minor / NVME_MINORS;
  1206. put_disk(ns->disk);
  1207. nvme_put_ns_idx(index);
  1208. blk_cleanup_queue(ns->queue);
  1209. kfree(ns);
  1210. }
  1211. static int set_queue_count(struct nvme_dev *dev, int count)
  1212. {
  1213. int status;
  1214. u32 result;
  1215. u32 q_count = (count - 1) | ((count - 1) << 16);
  1216. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  1217. &result);
  1218. if (status)
  1219. return -EIO;
  1220. return min(result & 0xffff, result >> 16) + 1;
  1221. }
  1222. static int __devinit nvme_setup_io_queues(struct nvme_dev *dev)
  1223. {
  1224. int result, cpu, i, nr_io_queues, db_bar_size, q_depth;
  1225. nr_io_queues = num_online_cpus();
  1226. result = set_queue_count(dev, nr_io_queues);
  1227. if (result < 0)
  1228. return result;
  1229. if (result < nr_io_queues)
  1230. nr_io_queues = result;
  1231. /* Deregister the admin queue's interrupt */
  1232. free_irq(dev->entry[0].vector, dev->queues[0]);
  1233. db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
  1234. if (db_bar_size > 8192) {
  1235. iounmap(dev->bar);
  1236. dev->bar = ioremap(pci_resource_start(dev->pci_dev, 0),
  1237. db_bar_size);
  1238. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1239. dev->queues[0]->q_db = dev->dbs;
  1240. }
  1241. for (i = 0; i < nr_io_queues; i++)
  1242. dev->entry[i].entry = i;
  1243. for (;;) {
  1244. result = pci_enable_msix(dev->pci_dev, dev->entry,
  1245. nr_io_queues);
  1246. if (result == 0) {
  1247. break;
  1248. } else if (result > 0) {
  1249. nr_io_queues = result;
  1250. continue;
  1251. } else {
  1252. nr_io_queues = 1;
  1253. break;
  1254. }
  1255. }
  1256. result = queue_request_irq(dev, dev->queues[0], "nvme admin");
  1257. /* XXX: handle failure here */
  1258. cpu = cpumask_first(cpu_online_mask);
  1259. for (i = 0; i < nr_io_queues; i++) {
  1260. irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
  1261. cpu = cpumask_next(cpu, cpu_online_mask);
  1262. }
  1263. q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
  1264. NVME_Q_DEPTH);
  1265. for (i = 0; i < nr_io_queues; i++) {
  1266. dev->queues[i + 1] = nvme_create_queue(dev, i + 1, q_depth, i);
  1267. if (IS_ERR(dev->queues[i + 1]))
  1268. return PTR_ERR(dev->queues[i + 1]);
  1269. dev->queue_count++;
  1270. }
  1271. for (; i < num_possible_cpus(); i++) {
  1272. int target = i % rounddown_pow_of_two(dev->queue_count - 1);
  1273. dev->queues[i + 1] = dev->queues[target + 1];
  1274. }
  1275. return 0;
  1276. }
  1277. static void nvme_free_queues(struct nvme_dev *dev)
  1278. {
  1279. int i;
  1280. for (i = dev->queue_count - 1; i >= 0; i--)
  1281. nvme_free_queue(dev, i);
  1282. }
  1283. static int __devinit nvme_dev_add(struct nvme_dev *dev)
  1284. {
  1285. int res, nn, i;
  1286. struct nvme_ns *ns, *next;
  1287. struct nvme_id_ctrl *ctrl;
  1288. struct nvme_id_ns *id_ns;
  1289. void *mem;
  1290. dma_addr_t dma_addr;
  1291. res = nvme_setup_io_queues(dev);
  1292. if (res)
  1293. return res;
  1294. mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
  1295. GFP_KERNEL);
  1296. res = nvme_identify(dev, 0, 1, dma_addr);
  1297. if (res) {
  1298. res = -EIO;
  1299. goto out_free;
  1300. }
  1301. ctrl = mem;
  1302. nn = le32_to_cpup(&ctrl->nn);
  1303. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  1304. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  1305. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  1306. if (ctrl->mdts) {
  1307. int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
  1308. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  1309. }
  1310. id_ns = mem;
  1311. for (i = 1; i <= nn; i++) {
  1312. res = nvme_identify(dev, i, 0, dma_addr);
  1313. if (res)
  1314. continue;
  1315. if (id_ns->ncap == 0)
  1316. continue;
  1317. res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
  1318. dma_addr + 4096);
  1319. if (res)
  1320. continue;
  1321. ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
  1322. if (ns)
  1323. list_add_tail(&ns->list, &dev->namespaces);
  1324. }
  1325. list_for_each_entry(ns, &dev->namespaces, list)
  1326. add_disk(ns->disk);
  1327. goto out;
  1328. out_free:
  1329. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1330. list_del(&ns->list);
  1331. nvme_ns_free(ns);
  1332. }
  1333. out:
  1334. dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
  1335. return res;
  1336. }
  1337. static int nvme_dev_remove(struct nvme_dev *dev)
  1338. {
  1339. struct nvme_ns *ns, *next;
  1340. spin_lock(&dev_list_lock);
  1341. list_del(&dev->node);
  1342. spin_unlock(&dev_list_lock);
  1343. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1344. list_del(&ns->list);
  1345. del_gendisk(ns->disk);
  1346. nvme_ns_free(ns);
  1347. }
  1348. nvme_free_queues(dev);
  1349. return 0;
  1350. }
  1351. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1352. {
  1353. struct device *dmadev = &dev->pci_dev->dev;
  1354. dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
  1355. PAGE_SIZE, PAGE_SIZE, 0);
  1356. if (!dev->prp_page_pool)
  1357. return -ENOMEM;
  1358. /* Optimisation for I/Os between 4k and 128k */
  1359. dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
  1360. 256, 256, 0);
  1361. if (!dev->prp_small_pool) {
  1362. dma_pool_destroy(dev->prp_page_pool);
  1363. return -ENOMEM;
  1364. }
  1365. return 0;
  1366. }
  1367. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1368. {
  1369. dma_pool_destroy(dev->prp_page_pool);
  1370. dma_pool_destroy(dev->prp_small_pool);
  1371. }
  1372. static DEFINE_IDA(nvme_instance_ida);
  1373. static int nvme_set_instance(struct nvme_dev *dev)
  1374. {
  1375. int instance, error;
  1376. do {
  1377. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1378. return -ENODEV;
  1379. spin_lock(&dev_list_lock);
  1380. error = ida_get_new(&nvme_instance_ida, &instance);
  1381. spin_unlock(&dev_list_lock);
  1382. } while (error == -EAGAIN);
  1383. if (error)
  1384. return -ENODEV;
  1385. dev->instance = instance;
  1386. return 0;
  1387. }
  1388. static void nvme_release_instance(struct nvme_dev *dev)
  1389. {
  1390. spin_lock(&dev_list_lock);
  1391. ida_remove(&nvme_instance_ida, dev->instance);
  1392. spin_unlock(&dev_list_lock);
  1393. }
  1394. static int __devinit nvme_probe(struct pci_dev *pdev,
  1395. const struct pci_device_id *id)
  1396. {
  1397. int bars, result = -ENOMEM;
  1398. struct nvme_dev *dev;
  1399. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1400. if (!dev)
  1401. return -ENOMEM;
  1402. dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
  1403. GFP_KERNEL);
  1404. if (!dev->entry)
  1405. goto free;
  1406. dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
  1407. GFP_KERNEL);
  1408. if (!dev->queues)
  1409. goto free;
  1410. if (pci_enable_device_mem(pdev))
  1411. goto free;
  1412. pci_set_master(pdev);
  1413. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1414. if (pci_request_selected_regions(pdev, bars, "nvme"))
  1415. goto disable;
  1416. INIT_LIST_HEAD(&dev->namespaces);
  1417. dev->pci_dev = pdev;
  1418. pci_set_drvdata(pdev, dev);
  1419. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  1420. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1421. result = nvme_set_instance(dev);
  1422. if (result)
  1423. goto disable;
  1424. dev->entry[0].vector = pdev->irq;
  1425. result = nvme_setup_prp_pools(dev);
  1426. if (result)
  1427. goto disable_msix;
  1428. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1429. if (!dev->bar) {
  1430. result = -ENOMEM;
  1431. goto disable_msix;
  1432. }
  1433. result = nvme_configure_admin_queue(dev);
  1434. if (result)
  1435. goto unmap;
  1436. dev->queue_count++;
  1437. spin_lock(&dev_list_lock);
  1438. list_add(&dev->node, &dev_list);
  1439. spin_unlock(&dev_list_lock);
  1440. result = nvme_dev_add(dev);
  1441. if (result)
  1442. goto delete;
  1443. return 0;
  1444. delete:
  1445. spin_lock(&dev_list_lock);
  1446. list_del(&dev->node);
  1447. spin_unlock(&dev_list_lock);
  1448. nvme_free_queues(dev);
  1449. unmap:
  1450. iounmap(dev->bar);
  1451. disable_msix:
  1452. pci_disable_msix(pdev);
  1453. nvme_release_instance(dev);
  1454. nvme_release_prp_pools(dev);
  1455. disable:
  1456. pci_disable_device(pdev);
  1457. pci_release_regions(pdev);
  1458. free:
  1459. kfree(dev->queues);
  1460. kfree(dev->entry);
  1461. kfree(dev);
  1462. return result;
  1463. }
  1464. static void __devexit nvme_remove(struct pci_dev *pdev)
  1465. {
  1466. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1467. nvme_dev_remove(dev);
  1468. pci_disable_msix(pdev);
  1469. iounmap(dev->bar);
  1470. nvme_release_instance(dev);
  1471. nvme_release_prp_pools(dev);
  1472. pci_disable_device(pdev);
  1473. pci_release_regions(pdev);
  1474. kfree(dev->queues);
  1475. kfree(dev->entry);
  1476. kfree(dev);
  1477. }
  1478. /* These functions are yet to be implemented */
  1479. #define nvme_error_detected NULL
  1480. #define nvme_dump_registers NULL
  1481. #define nvme_link_reset NULL
  1482. #define nvme_slot_reset NULL
  1483. #define nvme_error_resume NULL
  1484. #define nvme_suspend NULL
  1485. #define nvme_resume NULL
  1486. static struct pci_error_handlers nvme_err_handler = {
  1487. .error_detected = nvme_error_detected,
  1488. .mmio_enabled = nvme_dump_registers,
  1489. .link_reset = nvme_link_reset,
  1490. .slot_reset = nvme_slot_reset,
  1491. .resume = nvme_error_resume,
  1492. };
  1493. /* Move to pci_ids.h later */
  1494. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  1495. static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
  1496. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  1497. { 0, }
  1498. };
  1499. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  1500. static struct pci_driver nvme_driver = {
  1501. .name = "nvme",
  1502. .id_table = nvme_id_table,
  1503. .probe = nvme_probe,
  1504. .remove = __devexit_p(nvme_remove),
  1505. .suspend = nvme_suspend,
  1506. .resume = nvme_resume,
  1507. .err_handler = &nvme_err_handler,
  1508. };
  1509. static int __init nvme_init(void)
  1510. {
  1511. int result;
  1512. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  1513. if (IS_ERR(nvme_thread))
  1514. return PTR_ERR(nvme_thread);
  1515. result = register_blkdev(nvme_major, "nvme");
  1516. if (result < 0)
  1517. goto kill_kthread;
  1518. else if (result > 0)
  1519. nvme_major = result;
  1520. result = pci_register_driver(&nvme_driver);
  1521. if (result)
  1522. goto unregister_blkdev;
  1523. return 0;
  1524. unregister_blkdev:
  1525. unregister_blkdev(nvme_major, "nvme");
  1526. kill_kthread:
  1527. kthread_stop(nvme_thread);
  1528. return result;
  1529. }
  1530. static void __exit nvme_exit(void)
  1531. {
  1532. pci_unregister_driver(&nvme_driver);
  1533. unregister_blkdev(nvme_major, "nvme");
  1534. kthread_stop(nvme_thread);
  1535. }
  1536. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  1537. MODULE_LICENSE("GPL");
  1538. MODULE_VERSION("0.8");
  1539. module_init(nvme_init);
  1540. module_exit(nvme_exit);