nvme.c 44 KB

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