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