nvme.c 44 KB

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