nvme-core.c 46 KB

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