nvme.c 43 KB

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