nvme-core.c 51 KB

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