nvme-core.c 48 KB

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