nvme-core.c 55 KB

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