nvme.c 43 KB

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