nvme-core.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304
  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. if (!nvmeq) {
  686. put_nvmeq(NULL);
  687. bio_endio(bio, -EIO);
  688. return;
  689. }
  690. spin_lock_irq(&nvmeq->q_lock);
  691. if (!nvmeq->q_suspended && bio_list_empty(&nvmeq->sq_cong))
  692. result = nvme_submit_bio_queue(nvmeq, ns, bio);
  693. if (unlikely(result)) {
  694. if (bio_list_empty(&nvmeq->sq_cong))
  695. add_wait_queue(&nvmeq->sq_full, &nvmeq->sq_cong_wait);
  696. bio_list_add(&nvmeq->sq_cong, bio);
  697. }
  698. nvme_process_cq(nvmeq);
  699. spin_unlock_irq(&nvmeq->q_lock);
  700. put_nvmeq(nvmeq);
  701. }
  702. static irqreturn_t nvme_irq(int irq, void *data)
  703. {
  704. irqreturn_t result;
  705. struct nvme_queue *nvmeq = data;
  706. spin_lock(&nvmeq->q_lock);
  707. nvme_process_cq(nvmeq);
  708. result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
  709. nvmeq->cqe_seen = 0;
  710. spin_unlock(&nvmeq->q_lock);
  711. return result;
  712. }
  713. static irqreturn_t nvme_irq_check(int irq, void *data)
  714. {
  715. struct nvme_queue *nvmeq = data;
  716. struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
  717. if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
  718. return IRQ_NONE;
  719. return IRQ_WAKE_THREAD;
  720. }
  721. static void nvme_abort_command(struct nvme_queue *nvmeq, int cmdid)
  722. {
  723. spin_lock_irq(&nvmeq->q_lock);
  724. cancel_cmdid(nvmeq, cmdid, NULL);
  725. spin_unlock_irq(&nvmeq->q_lock);
  726. }
  727. struct sync_cmd_info {
  728. struct task_struct *task;
  729. u32 result;
  730. int status;
  731. };
  732. static void sync_completion(struct nvme_dev *dev, void *ctx,
  733. struct nvme_completion *cqe)
  734. {
  735. struct sync_cmd_info *cmdinfo = ctx;
  736. cmdinfo->result = le32_to_cpup(&cqe->result);
  737. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  738. wake_up_process(cmdinfo->task);
  739. }
  740. /*
  741. * Returns 0 on success. If the result is negative, it's a Linux error code;
  742. * if the result is positive, it's an NVM Express status code
  743. */
  744. int nvme_submit_sync_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
  745. u32 *result, unsigned timeout)
  746. {
  747. int cmdid;
  748. struct sync_cmd_info cmdinfo;
  749. cmdinfo.task = current;
  750. cmdinfo.status = -EINTR;
  751. cmdid = alloc_cmdid_killable(nvmeq, &cmdinfo, sync_completion,
  752. timeout);
  753. if (cmdid < 0)
  754. return cmdid;
  755. cmd->common.command_id = cmdid;
  756. set_current_state(TASK_KILLABLE);
  757. nvme_submit_cmd(nvmeq, cmd);
  758. schedule_timeout(timeout);
  759. if (cmdinfo.status == -EINTR) {
  760. nvme_abort_command(nvmeq, cmdid);
  761. return -EINTR;
  762. }
  763. if (result)
  764. *result = cmdinfo.result;
  765. return cmdinfo.status;
  766. }
  767. int nvme_submit_admin_cmd(struct nvme_dev *dev, struct nvme_command *cmd,
  768. u32 *result)
  769. {
  770. return nvme_submit_sync_cmd(dev->queues[0], cmd, result, ADMIN_TIMEOUT);
  771. }
  772. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  773. {
  774. int status;
  775. struct nvme_command c;
  776. memset(&c, 0, sizeof(c));
  777. c.delete_queue.opcode = opcode;
  778. c.delete_queue.qid = cpu_to_le16(id);
  779. status = nvme_submit_admin_cmd(dev, &c, NULL);
  780. if (status)
  781. return -EIO;
  782. return 0;
  783. }
  784. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  785. struct nvme_queue *nvmeq)
  786. {
  787. int status;
  788. struct nvme_command c;
  789. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  790. memset(&c, 0, sizeof(c));
  791. c.create_cq.opcode = nvme_admin_create_cq;
  792. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  793. c.create_cq.cqid = cpu_to_le16(qid);
  794. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  795. c.create_cq.cq_flags = cpu_to_le16(flags);
  796. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  797. status = nvme_submit_admin_cmd(dev, &c, NULL);
  798. if (status)
  799. return -EIO;
  800. return 0;
  801. }
  802. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  803. struct nvme_queue *nvmeq)
  804. {
  805. int status;
  806. struct nvme_command c;
  807. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  808. memset(&c, 0, sizeof(c));
  809. c.create_sq.opcode = nvme_admin_create_sq;
  810. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  811. c.create_sq.sqid = cpu_to_le16(qid);
  812. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  813. c.create_sq.sq_flags = cpu_to_le16(flags);
  814. c.create_sq.cqid = cpu_to_le16(qid);
  815. status = nvme_submit_admin_cmd(dev, &c, NULL);
  816. if (status)
  817. return -EIO;
  818. return 0;
  819. }
  820. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  821. {
  822. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  823. }
  824. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  825. {
  826. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  827. }
  828. int nvme_identify(struct nvme_dev *dev, unsigned nsid, unsigned cns,
  829. dma_addr_t dma_addr)
  830. {
  831. struct nvme_command c;
  832. memset(&c, 0, sizeof(c));
  833. c.identify.opcode = nvme_admin_identify;
  834. c.identify.nsid = cpu_to_le32(nsid);
  835. c.identify.prp1 = cpu_to_le64(dma_addr);
  836. c.identify.cns = cpu_to_le32(cns);
  837. return nvme_submit_admin_cmd(dev, &c, NULL);
  838. }
  839. int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
  840. dma_addr_t dma_addr, u32 *result)
  841. {
  842. struct nvme_command c;
  843. memset(&c, 0, sizeof(c));
  844. c.features.opcode = nvme_admin_get_features;
  845. c.features.nsid = cpu_to_le32(nsid);
  846. c.features.prp1 = cpu_to_le64(dma_addr);
  847. c.features.fid = cpu_to_le32(fid);
  848. return nvme_submit_admin_cmd(dev, &c, result);
  849. }
  850. int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
  851. dma_addr_t dma_addr, u32 *result)
  852. {
  853. struct nvme_command c;
  854. memset(&c, 0, sizeof(c));
  855. c.features.opcode = nvme_admin_set_features;
  856. c.features.prp1 = cpu_to_le64(dma_addr);
  857. c.features.fid = cpu_to_le32(fid);
  858. c.features.dword11 = cpu_to_le32(dword11);
  859. return nvme_submit_admin_cmd(dev, &c, result);
  860. }
  861. /**
  862. * nvme_cancel_ios - Cancel outstanding I/Os
  863. * @queue: The queue to cancel I/Os on
  864. * @timeout: True to only cancel I/Os which have timed out
  865. */
  866. static void nvme_cancel_ios(struct nvme_queue *nvmeq, bool timeout)
  867. {
  868. int depth = nvmeq->q_depth - 1;
  869. struct nvme_cmd_info *info = nvme_cmd_info(nvmeq);
  870. unsigned long now = jiffies;
  871. int cmdid;
  872. for_each_set_bit(cmdid, nvmeq->cmdid_data, depth) {
  873. void *ctx;
  874. nvme_completion_fn fn;
  875. static struct nvme_completion cqe = {
  876. .status = cpu_to_le16(NVME_SC_ABORT_REQ << 1),
  877. };
  878. if (timeout && !time_after(now, info[cmdid].timeout))
  879. continue;
  880. if (info[cmdid].ctx == CMD_CTX_CANCELLED)
  881. continue;
  882. dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d\n", cmdid);
  883. ctx = cancel_cmdid(nvmeq, cmdid, &fn);
  884. fn(nvmeq->dev, ctx, &cqe);
  885. }
  886. }
  887. static void nvme_free_queue(struct nvme_queue *nvmeq)
  888. {
  889. spin_lock_irq(&nvmeq->q_lock);
  890. while (bio_list_peek(&nvmeq->sq_cong)) {
  891. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  892. bio_endio(bio, -EIO);
  893. }
  894. spin_unlock_irq(&nvmeq->q_lock);
  895. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  896. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  897. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  898. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  899. kfree(nvmeq);
  900. }
  901. static void nvme_free_queues(struct nvme_dev *dev)
  902. {
  903. int i;
  904. for (i = dev->queue_count - 1; i >= 0; i--) {
  905. nvme_free_queue(dev->queues[i]);
  906. dev->queue_count--;
  907. dev->queues[i] = NULL;
  908. }
  909. }
  910. static void nvme_disable_queue(struct nvme_dev *dev, int qid)
  911. {
  912. struct nvme_queue *nvmeq = dev->queues[qid];
  913. int vector = dev->entry[nvmeq->cq_vector].vector;
  914. spin_lock_irq(&nvmeq->q_lock);
  915. if (nvmeq->q_suspended) {
  916. spin_unlock_irq(&nvmeq->q_lock);
  917. return;
  918. }
  919. nvmeq->q_suspended = 1;
  920. spin_unlock_irq(&nvmeq->q_lock);
  921. irq_set_affinity_hint(vector, NULL);
  922. free_irq(vector, nvmeq);
  923. /* Don't tell the adapter to delete the admin queue */
  924. if (qid) {
  925. adapter_delete_sq(dev, qid);
  926. adapter_delete_cq(dev, qid);
  927. }
  928. spin_lock_irq(&nvmeq->q_lock);
  929. nvme_process_cq(nvmeq);
  930. nvme_cancel_ios(nvmeq, false);
  931. spin_unlock_irq(&nvmeq->q_lock);
  932. }
  933. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  934. int depth, int vector)
  935. {
  936. struct device *dmadev = &dev->pci_dev->dev;
  937. unsigned extra = nvme_queue_extra(depth);
  938. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq) + extra, GFP_KERNEL);
  939. if (!nvmeq)
  940. return NULL;
  941. nvmeq->cqes = dma_alloc_coherent(dmadev, CQ_SIZE(depth),
  942. &nvmeq->cq_dma_addr, GFP_KERNEL);
  943. if (!nvmeq->cqes)
  944. goto free_nvmeq;
  945. memset((void *)nvmeq->cqes, 0, CQ_SIZE(depth));
  946. nvmeq->sq_cmds = dma_alloc_coherent(dmadev, SQ_SIZE(depth),
  947. &nvmeq->sq_dma_addr, GFP_KERNEL);
  948. if (!nvmeq->sq_cmds)
  949. goto free_cqdma;
  950. nvmeq->q_dmadev = dmadev;
  951. nvmeq->dev = dev;
  952. spin_lock_init(&nvmeq->q_lock);
  953. nvmeq->cq_head = 0;
  954. nvmeq->cq_phase = 1;
  955. init_waitqueue_head(&nvmeq->sq_full);
  956. init_waitqueue_entry(&nvmeq->sq_cong_wait, nvme_thread);
  957. bio_list_init(&nvmeq->sq_cong);
  958. nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
  959. nvmeq->q_depth = depth;
  960. nvmeq->cq_vector = vector;
  961. nvmeq->q_suspended = 1;
  962. dev->queue_count++;
  963. return nvmeq;
  964. free_cqdma:
  965. dma_free_coherent(dmadev, CQ_SIZE(depth), (void *)nvmeq->cqes,
  966. nvmeq->cq_dma_addr);
  967. free_nvmeq:
  968. kfree(nvmeq);
  969. return NULL;
  970. }
  971. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  972. const char *name)
  973. {
  974. if (use_threaded_interrupts)
  975. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  976. nvme_irq_check, nvme_irq,
  977. IRQF_DISABLED | IRQF_SHARED,
  978. name, nvmeq);
  979. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  980. IRQF_DISABLED | IRQF_SHARED, name, nvmeq);
  981. }
  982. static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
  983. {
  984. struct nvme_dev *dev = nvmeq->dev;
  985. unsigned extra = nvme_queue_extra(nvmeq->q_depth);
  986. nvmeq->sq_tail = 0;
  987. nvmeq->cq_head = 0;
  988. nvmeq->cq_phase = 1;
  989. nvmeq->q_db = &dev->dbs[qid << (dev->db_stride + 1)];
  990. memset(nvmeq->cmdid_data, 0, extra);
  991. memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
  992. nvme_cancel_ios(nvmeq, false);
  993. nvmeq->q_suspended = 0;
  994. }
  995. static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
  996. {
  997. struct nvme_dev *dev = nvmeq->dev;
  998. int result;
  999. result = adapter_alloc_cq(dev, qid, nvmeq);
  1000. if (result < 0)
  1001. return result;
  1002. result = adapter_alloc_sq(dev, qid, nvmeq);
  1003. if (result < 0)
  1004. goto release_cq;
  1005. result = queue_request_irq(dev, nvmeq, "nvme");
  1006. if (result < 0)
  1007. goto release_sq;
  1008. spin_lock(&nvmeq->q_lock);
  1009. nvme_init_queue(nvmeq, qid);
  1010. spin_unlock(&nvmeq->q_lock);
  1011. return result;
  1012. release_sq:
  1013. adapter_delete_sq(dev, qid);
  1014. release_cq:
  1015. adapter_delete_cq(dev, qid);
  1016. return result;
  1017. }
  1018. static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
  1019. {
  1020. unsigned long timeout;
  1021. u32 bit = enabled ? NVME_CSTS_RDY : 0;
  1022. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  1023. while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
  1024. msleep(100);
  1025. if (fatal_signal_pending(current))
  1026. return -EINTR;
  1027. if (time_after(jiffies, timeout)) {
  1028. dev_err(&dev->pci_dev->dev,
  1029. "Device not ready; aborting initialisation\n");
  1030. return -ENODEV;
  1031. }
  1032. }
  1033. return 0;
  1034. }
  1035. /*
  1036. * If the device has been passed off to us in an enabled state, just clear
  1037. * the enabled bit. The spec says we should set the 'shutdown notification
  1038. * bits', but doing so may cause the device to complete commands to the
  1039. * admin queue ... and we don't know what memory that might be pointing at!
  1040. */
  1041. static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
  1042. {
  1043. u32 cc = readl(&dev->bar->cc);
  1044. if (cc & NVME_CC_ENABLE)
  1045. writel(cc & ~NVME_CC_ENABLE, &dev->bar->cc);
  1046. return nvme_wait_ready(dev, cap, false);
  1047. }
  1048. static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
  1049. {
  1050. return nvme_wait_ready(dev, cap, true);
  1051. }
  1052. static int nvme_shutdown_ctrl(struct nvme_dev *dev)
  1053. {
  1054. unsigned long timeout;
  1055. u32 cc;
  1056. cc = (readl(&dev->bar->cc) & ~NVME_CC_SHN_MASK) | NVME_CC_SHN_NORMAL;
  1057. writel(cc, &dev->bar->cc);
  1058. timeout = 2 * HZ + jiffies;
  1059. while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
  1060. NVME_CSTS_SHST_CMPLT) {
  1061. msleep(100);
  1062. if (fatal_signal_pending(current))
  1063. return -EINTR;
  1064. if (time_after(jiffies, timeout)) {
  1065. dev_err(&dev->pci_dev->dev,
  1066. "Device shutdown incomplete; abort shutdown\n");
  1067. return -ENODEV;
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. static int nvme_configure_admin_queue(struct nvme_dev *dev)
  1073. {
  1074. int result;
  1075. u32 aqa;
  1076. u64 cap = readq(&dev->bar->cap);
  1077. struct nvme_queue *nvmeq;
  1078. result = nvme_disable_ctrl(dev, cap);
  1079. if (result < 0)
  1080. return result;
  1081. nvmeq = dev->queues[0];
  1082. if (!nvmeq) {
  1083. nvmeq = nvme_alloc_queue(dev, 0, 64, 0);
  1084. if (!nvmeq)
  1085. return -ENOMEM;
  1086. dev->queues[0] = nvmeq;
  1087. }
  1088. aqa = nvmeq->q_depth - 1;
  1089. aqa |= aqa << 16;
  1090. dev->ctrl_config = NVME_CC_ENABLE | NVME_CC_CSS_NVM;
  1091. dev->ctrl_config |= (PAGE_SHIFT - 12) << NVME_CC_MPS_SHIFT;
  1092. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  1093. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  1094. writel(aqa, &dev->bar->aqa);
  1095. writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  1096. writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  1097. writel(dev->ctrl_config, &dev->bar->cc);
  1098. result = nvme_enable_ctrl(dev, cap);
  1099. if (result)
  1100. return result;
  1101. result = queue_request_irq(dev, nvmeq, "nvme admin");
  1102. if (result)
  1103. return result;
  1104. spin_lock(&nvmeq->q_lock);
  1105. nvme_init_queue(nvmeq, 0);
  1106. spin_unlock(&nvmeq->q_lock);
  1107. return result;
  1108. }
  1109. struct nvme_iod *nvme_map_user_pages(struct nvme_dev *dev, int write,
  1110. unsigned long addr, unsigned length)
  1111. {
  1112. int i, err, count, nents, offset;
  1113. struct scatterlist *sg;
  1114. struct page **pages;
  1115. struct nvme_iod *iod;
  1116. if (addr & 3)
  1117. return ERR_PTR(-EINVAL);
  1118. if (!length || length > INT_MAX - PAGE_SIZE)
  1119. return ERR_PTR(-EINVAL);
  1120. offset = offset_in_page(addr);
  1121. count = DIV_ROUND_UP(offset + length, PAGE_SIZE);
  1122. pages = kcalloc(count, sizeof(*pages), GFP_KERNEL);
  1123. if (!pages)
  1124. return ERR_PTR(-ENOMEM);
  1125. err = get_user_pages_fast(addr, count, 1, pages);
  1126. if (err < count) {
  1127. count = err;
  1128. err = -EFAULT;
  1129. goto put_pages;
  1130. }
  1131. iod = nvme_alloc_iod(count, length, GFP_KERNEL);
  1132. sg = iod->sg;
  1133. sg_init_table(sg, count);
  1134. for (i = 0; i < count; i++) {
  1135. sg_set_page(&sg[i], pages[i],
  1136. min_t(unsigned, length, PAGE_SIZE - offset),
  1137. offset);
  1138. length -= (PAGE_SIZE - offset);
  1139. offset = 0;
  1140. }
  1141. sg_mark_end(&sg[i - 1]);
  1142. iod->nents = count;
  1143. err = -ENOMEM;
  1144. nents = dma_map_sg(&dev->pci_dev->dev, sg, count,
  1145. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1146. if (!nents)
  1147. goto free_iod;
  1148. kfree(pages);
  1149. return iod;
  1150. free_iod:
  1151. kfree(iod);
  1152. put_pages:
  1153. for (i = 0; i < count; i++)
  1154. put_page(pages[i]);
  1155. kfree(pages);
  1156. return ERR_PTR(err);
  1157. }
  1158. void nvme_unmap_user_pages(struct nvme_dev *dev, int write,
  1159. struct nvme_iod *iod)
  1160. {
  1161. int i;
  1162. dma_unmap_sg(&dev->pci_dev->dev, iod->sg, iod->nents,
  1163. write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1164. for (i = 0; i < iod->nents; i++)
  1165. put_page(sg_page(&iod->sg[i]));
  1166. }
  1167. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  1168. {
  1169. struct nvme_dev *dev = ns->dev;
  1170. struct nvme_queue *nvmeq;
  1171. struct nvme_user_io io;
  1172. struct nvme_command c;
  1173. unsigned length, meta_len;
  1174. int status, i;
  1175. struct nvme_iod *iod, *meta_iod = NULL;
  1176. dma_addr_t meta_dma_addr;
  1177. void *meta, *uninitialized_var(meta_mem);
  1178. if (copy_from_user(&io, uio, sizeof(io)))
  1179. return -EFAULT;
  1180. length = (io.nblocks + 1) << ns->lba_shift;
  1181. meta_len = (io.nblocks + 1) * ns->ms;
  1182. if (meta_len && ((io.metadata & 3) || !io.metadata))
  1183. return -EINVAL;
  1184. switch (io.opcode) {
  1185. case nvme_cmd_write:
  1186. case nvme_cmd_read:
  1187. case nvme_cmd_compare:
  1188. iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
  1189. break;
  1190. default:
  1191. return -EINVAL;
  1192. }
  1193. if (IS_ERR(iod))
  1194. return PTR_ERR(iod);
  1195. memset(&c, 0, sizeof(c));
  1196. c.rw.opcode = io.opcode;
  1197. c.rw.flags = io.flags;
  1198. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1199. c.rw.slba = cpu_to_le64(io.slba);
  1200. c.rw.length = cpu_to_le16(io.nblocks);
  1201. c.rw.control = cpu_to_le16(io.control);
  1202. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  1203. c.rw.reftag = cpu_to_le32(io.reftag);
  1204. c.rw.apptag = cpu_to_le16(io.apptag);
  1205. c.rw.appmask = cpu_to_le16(io.appmask);
  1206. if (meta_len) {
  1207. meta_iod = nvme_map_user_pages(dev, io.opcode & 1, io.metadata,
  1208. meta_len);
  1209. if (IS_ERR(meta_iod)) {
  1210. status = PTR_ERR(meta_iod);
  1211. meta_iod = NULL;
  1212. goto unmap;
  1213. }
  1214. meta_mem = dma_alloc_coherent(&dev->pci_dev->dev, meta_len,
  1215. &meta_dma_addr, GFP_KERNEL);
  1216. if (!meta_mem) {
  1217. status = -ENOMEM;
  1218. goto unmap;
  1219. }
  1220. if (io.opcode & 1) {
  1221. int meta_offset = 0;
  1222. for (i = 0; i < meta_iod->nents; i++) {
  1223. meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
  1224. meta_iod->sg[i].offset;
  1225. memcpy(meta_mem + meta_offset, meta,
  1226. meta_iod->sg[i].length);
  1227. kunmap_atomic(meta);
  1228. meta_offset += meta_iod->sg[i].length;
  1229. }
  1230. }
  1231. c.rw.metadata = cpu_to_le64(meta_dma_addr);
  1232. }
  1233. length = nvme_setup_prps(dev, &c.common, iod, length, GFP_KERNEL);
  1234. nvmeq = get_nvmeq(dev);
  1235. /*
  1236. * Since nvme_submit_sync_cmd sleeps, we can't keep preemption
  1237. * disabled. We may be preempted at any point, and be rescheduled
  1238. * to a different CPU. That will cause cacheline bouncing, but no
  1239. * additional races since q_lock already protects against other CPUs.
  1240. */
  1241. put_nvmeq(nvmeq);
  1242. if (length != (io.nblocks + 1) << ns->lba_shift)
  1243. status = -ENOMEM;
  1244. else if (!nvmeq || nvmeq->q_suspended)
  1245. status = -EBUSY;
  1246. else
  1247. status = nvme_submit_sync_cmd(nvmeq, &c, NULL, NVME_IO_TIMEOUT);
  1248. if (meta_len) {
  1249. if (status == NVME_SC_SUCCESS && !(io.opcode & 1)) {
  1250. int meta_offset = 0;
  1251. for (i = 0; i < meta_iod->nents; i++) {
  1252. meta = kmap_atomic(sg_page(&meta_iod->sg[i])) +
  1253. meta_iod->sg[i].offset;
  1254. memcpy(meta, meta_mem + meta_offset,
  1255. meta_iod->sg[i].length);
  1256. kunmap_atomic(meta);
  1257. meta_offset += meta_iod->sg[i].length;
  1258. }
  1259. }
  1260. dma_free_coherent(&dev->pci_dev->dev, meta_len, meta_mem,
  1261. meta_dma_addr);
  1262. }
  1263. unmap:
  1264. nvme_unmap_user_pages(dev, io.opcode & 1, iod);
  1265. nvme_free_iod(dev, iod);
  1266. if (meta_iod) {
  1267. nvme_unmap_user_pages(dev, io.opcode & 1, meta_iod);
  1268. nvme_free_iod(dev, meta_iod);
  1269. }
  1270. return status;
  1271. }
  1272. static int nvme_user_admin_cmd(struct nvme_dev *dev,
  1273. struct nvme_admin_cmd __user *ucmd)
  1274. {
  1275. struct nvme_admin_cmd cmd;
  1276. struct nvme_command c;
  1277. int status, length;
  1278. struct nvme_iod *uninitialized_var(iod);
  1279. unsigned timeout;
  1280. if (!capable(CAP_SYS_ADMIN))
  1281. return -EACCES;
  1282. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1283. return -EFAULT;
  1284. memset(&c, 0, sizeof(c));
  1285. c.common.opcode = cmd.opcode;
  1286. c.common.flags = cmd.flags;
  1287. c.common.nsid = cpu_to_le32(cmd.nsid);
  1288. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1289. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1290. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1291. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1292. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1293. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1294. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1295. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1296. length = cmd.data_len;
  1297. if (cmd.data_len) {
  1298. iod = nvme_map_user_pages(dev, cmd.opcode & 1, cmd.addr,
  1299. length);
  1300. if (IS_ERR(iod))
  1301. return PTR_ERR(iod);
  1302. length = nvme_setup_prps(dev, &c.common, iod, length,
  1303. GFP_KERNEL);
  1304. }
  1305. timeout = cmd.timeout_ms ? msecs_to_jiffies(cmd.timeout_ms) :
  1306. ADMIN_TIMEOUT;
  1307. if (length != cmd.data_len)
  1308. status = -ENOMEM;
  1309. else
  1310. status = nvme_submit_sync_cmd(dev->queues[0], &c, &cmd.result,
  1311. timeout);
  1312. if (cmd.data_len) {
  1313. nvme_unmap_user_pages(dev, cmd.opcode & 1, iod);
  1314. nvme_free_iod(dev, iod);
  1315. }
  1316. if ((status >= 0) && copy_to_user(&ucmd->result, &cmd.result,
  1317. sizeof(cmd.result)))
  1318. status = -EFAULT;
  1319. return status;
  1320. }
  1321. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1322. unsigned long arg)
  1323. {
  1324. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1325. switch (cmd) {
  1326. case NVME_IOCTL_ID:
  1327. force_successful_syscall_return();
  1328. return ns->ns_id;
  1329. case NVME_IOCTL_ADMIN_CMD:
  1330. return nvme_user_admin_cmd(ns->dev, (void __user *)arg);
  1331. case NVME_IOCTL_SUBMIT_IO:
  1332. return nvme_submit_io(ns, (void __user *)arg);
  1333. case SG_GET_VERSION_NUM:
  1334. return nvme_sg_get_version_num((void __user *)arg);
  1335. case SG_IO:
  1336. return nvme_sg_io(ns, (void __user *)arg);
  1337. default:
  1338. return -ENOTTY;
  1339. }
  1340. }
  1341. static const struct block_device_operations nvme_fops = {
  1342. .owner = THIS_MODULE,
  1343. .ioctl = nvme_ioctl,
  1344. .compat_ioctl = nvme_ioctl,
  1345. };
  1346. static void nvme_resubmit_bios(struct nvme_queue *nvmeq)
  1347. {
  1348. while (bio_list_peek(&nvmeq->sq_cong)) {
  1349. struct bio *bio = bio_list_pop(&nvmeq->sq_cong);
  1350. struct nvme_ns *ns = bio->bi_bdev->bd_disk->private_data;
  1351. if (bio_list_empty(&nvmeq->sq_cong))
  1352. remove_wait_queue(&nvmeq->sq_full,
  1353. &nvmeq->sq_cong_wait);
  1354. if (nvme_submit_bio_queue(nvmeq, ns, bio)) {
  1355. if (bio_list_empty(&nvmeq->sq_cong))
  1356. add_wait_queue(&nvmeq->sq_full,
  1357. &nvmeq->sq_cong_wait);
  1358. bio_list_add_head(&nvmeq->sq_cong, bio);
  1359. break;
  1360. }
  1361. }
  1362. }
  1363. static int nvme_kthread(void *data)
  1364. {
  1365. struct nvme_dev *dev;
  1366. while (!kthread_should_stop()) {
  1367. set_current_state(TASK_INTERRUPTIBLE);
  1368. spin_lock(&dev_list_lock);
  1369. list_for_each_entry(dev, &dev_list, node) {
  1370. int i;
  1371. for (i = 0; i < dev->queue_count; i++) {
  1372. struct nvme_queue *nvmeq = dev->queues[i];
  1373. if (!nvmeq)
  1374. continue;
  1375. spin_lock_irq(&nvmeq->q_lock);
  1376. if (nvmeq->q_suspended)
  1377. goto unlock;
  1378. nvme_process_cq(nvmeq);
  1379. nvme_cancel_ios(nvmeq, true);
  1380. nvme_resubmit_bios(nvmeq);
  1381. unlock:
  1382. spin_unlock_irq(&nvmeq->q_lock);
  1383. }
  1384. }
  1385. spin_unlock(&dev_list_lock);
  1386. schedule_timeout(round_jiffies_relative(HZ));
  1387. }
  1388. return 0;
  1389. }
  1390. static DEFINE_IDA(nvme_index_ida);
  1391. static int nvme_get_ns_idx(void)
  1392. {
  1393. int index, error;
  1394. do {
  1395. if (!ida_pre_get(&nvme_index_ida, GFP_KERNEL))
  1396. return -1;
  1397. spin_lock(&dev_list_lock);
  1398. error = ida_get_new(&nvme_index_ida, &index);
  1399. spin_unlock(&dev_list_lock);
  1400. } while (error == -EAGAIN);
  1401. if (error)
  1402. index = -1;
  1403. return index;
  1404. }
  1405. static void nvme_put_ns_idx(int index)
  1406. {
  1407. spin_lock(&dev_list_lock);
  1408. ida_remove(&nvme_index_ida, index);
  1409. spin_unlock(&dev_list_lock);
  1410. }
  1411. static void nvme_config_discard(struct nvme_ns *ns)
  1412. {
  1413. u32 logical_block_size = queue_logical_block_size(ns->queue);
  1414. ns->queue->limits.discard_zeroes_data = 0;
  1415. ns->queue->limits.discard_alignment = logical_block_size;
  1416. ns->queue->limits.discard_granularity = logical_block_size;
  1417. ns->queue->limits.max_discard_sectors = 0xffffffff;
  1418. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  1419. }
  1420. static struct nvme_ns *nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid,
  1421. struct nvme_id_ns *id, struct nvme_lba_range_type *rt)
  1422. {
  1423. struct nvme_ns *ns;
  1424. struct gendisk *disk;
  1425. int lbaf;
  1426. if (rt->attributes & NVME_LBART_ATTRIB_HIDE)
  1427. return NULL;
  1428. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  1429. if (!ns)
  1430. return NULL;
  1431. ns->queue = blk_alloc_queue(GFP_KERNEL);
  1432. if (!ns->queue)
  1433. goto out_free_ns;
  1434. ns->queue->queue_flags = QUEUE_FLAG_DEFAULT;
  1435. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1436. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1437. blk_queue_make_request(ns->queue, nvme_make_request);
  1438. ns->dev = dev;
  1439. ns->queue->queuedata = ns;
  1440. disk = alloc_disk(NVME_MINORS);
  1441. if (!disk)
  1442. goto out_free_queue;
  1443. ns->ns_id = nsid;
  1444. ns->disk = disk;
  1445. lbaf = id->flbas & 0xf;
  1446. ns->lba_shift = id->lbaf[lbaf].ds;
  1447. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  1448. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1449. if (dev->max_hw_sectors)
  1450. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1451. disk->major = nvme_major;
  1452. disk->minors = NVME_MINORS;
  1453. disk->first_minor = NVME_MINORS * nvme_get_ns_idx();
  1454. disk->fops = &nvme_fops;
  1455. disk->private_data = ns;
  1456. disk->queue = ns->queue;
  1457. disk->driverfs_dev = &dev->pci_dev->dev;
  1458. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1459. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1460. if (dev->oncs & NVME_CTRL_ONCS_DSM)
  1461. nvme_config_discard(ns);
  1462. return ns;
  1463. out_free_queue:
  1464. blk_cleanup_queue(ns->queue);
  1465. out_free_ns:
  1466. kfree(ns);
  1467. return NULL;
  1468. }
  1469. static void nvme_ns_free(struct nvme_ns *ns)
  1470. {
  1471. int index = ns->disk->first_minor / NVME_MINORS;
  1472. put_disk(ns->disk);
  1473. nvme_put_ns_idx(index);
  1474. blk_cleanup_queue(ns->queue);
  1475. kfree(ns);
  1476. }
  1477. static int set_queue_count(struct nvme_dev *dev, int count)
  1478. {
  1479. int status;
  1480. u32 result;
  1481. u32 q_count = (count - 1) | ((count - 1) << 16);
  1482. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  1483. &result);
  1484. if (status)
  1485. return status < 0 ? -EIO : -EBUSY;
  1486. return min(result & 0xffff, result >> 16) + 1;
  1487. }
  1488. static int nvme_setup_io_queues(struct nvme_dev *dev)
  1489. {
  1490. struct pci_dev *pdev = dev->pci_dev;
  1491. int result, cpu, i, vecs, nr_io_queues, db_bar_size, q_depth;
  1492. nr_io_queues = num_online_cpus();
  1493. result = set_queue_count(dev, nr_io_queues);
  1494. if (result < 0)
  1495. return result;
  1496. if (result < nr_io_queues)
  1497. nr_io_queues = result;
  1498. /* Deregister the admin queue's interrupt */
  1499. free_irq(dev->entry[0].vector, dev->queues[0]);
  1500. db_bar_size = 4096 + ((nr_io_queues + 1) << (dev->db_stride + 3));
  1501. if (db_bar_size > 8192) {
  1502. iounmap(dev->bar);
  1503. dev->bar = ioremap(pci_resource_start(pdev, 0), db_bar_size);
  1504. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1505. dev->queues[0]->q_db = dev->dbs;
  1506. }
  1507. vecs = nr_io_queues;
  1508. for (i = 0; i < vecs; i++)
  1509. dev->entry[i].entry = i;
  1510. for (;;) {
  1511. result = pci_enable_msix(pdev, dev->entry, vecs);
  1512. if (result <= 0)
  1513. break;
  1514. vecs = result;
  1515. }
  1516. if (result < 0) {
  1517. vecs = nr_io_queues;
  1518. if (vecs > 32)
  1519. vecs = 32;
  1520. for (;;) {
  1521. result = pci_enable_msi_block(pdev, vecs);
  1522. if (result == 0) {
  1523. for (i = 0; i < vecs; i++)
  1524. dev->entry[i].vector = i + pdev->irq;
  1525. break;
  1526. } else if (result < 0) {
  1527. vecs = 1;
  1528. break;
  1529. }
  1530. vecs = result;
  1531. }
  1532. }
  1533. /*
  1534. * Should investigate if there's a performance win from allocating
  1535. * more queues than interrupt vectors; it might allow the submission
  1536. * path to scale better, even if the receive path is limited by the
  1537. * number of interrupts.
  1538. */
  1539. nr_io_queues = vecs;
  1540. result = queue_request_irq(dev, dev->queues[0], "nvme admin");
  1541. if (result)
  1542. goto free_queues;
  1543. /* Free previously allocated queues that are no longer usable */
  1544. spin_lock(&dev_list_lock);
  1545. for (i = dev->queue_count - 1; i > nr_io_queues; i--) {
  1546. struct nvme_queue *nvmeq = dev->queues[i];
  1547. spin_lock(&nvmeq->q_lock);
  1548. nvme_cancel_ios(nvmeq, false);
  1549. spin_unlock(&nvmeq->q_lock);
  1550. nvme_free_queue(nvmeq);
  1551. dev->queue_count--;
  1552. dev->queues[i] = NULL;
  1553. }
  1554. spin_unlock(&dev_list_lock);
  1555. cpu = cpumask_first(cpu_online_mask);
  1556. for (i = 0; i < nr_io_queues; i++) {
  1557. irq_set_affinity_hint(dev->entry[i].vector, get_cpu_mask(cpu));
  1558. cpu = cpumask_next(cpu, cpu_online_mask);
  1559. }
  1560. q_depth = min_t(int, NVME_CAP_MQES(readq(&dev->bar->cap)) + 1,
  1561. NVME_Q_DEPTH);
  1562. for (i = dev->queue_count - 1; i < nr_io_queues; i++) {
  1563. dev->queues[i + 1] = nvme_alloc_queue(dev, i + 1, q_depth, i);
  1564. if (!dev->queues[i + 1]) {
  1565. result = -ENOMEM;
  1566. goto free_queues;
  1567. }
  1568. }
  1569. for (; i < num_possible_cpus(); i++) {
  1570. int target = i % rounddown_pow_of_two(dev->queue_count - 1);
  1571. dev->queues[i + 1] = dev->queues[target + 1];
  1572. }
  1573. for (i = 1; i < dev->queue_count; i++) {
  1574. result = nvme_create_queue(dev->queues[i], i);
  1575. if (result) {
  1576. for (--i; i > 0; i--)
  1577. nvme_disable_queue(dev, i);
  1578. goto free_queues;
  1579. }
  1580. }
  1581. return 0;
  1582. free_queues:
  1583. nvme_free_queues(dev);
  1584. return result;
  1585. }
  1586. /*
  1587. * Return: error value if an error occurred setting up the queues or calling
  1588. * Identify Device. 0 if these succeeded, even if adding some of the
  1589. * namespaces failed. At the moment, these failures are silent. TBD which
  1590. * failures should be reported.
  1591. */
  1592. static int nvme_dev_add(struct nvme_dev *dev)
  1593. {
  1594. int res;
  1595. unsigned nn, i;
  1596. struct nvme_ns *ns;
  1597. struct nvme_id_ctrl *ctrl;
  1598. struct nvme_id_ns *id_ns;
  1599. void *mem;
  1600. dma_addr_t dma_addr;
  1601. int shift = NVME_CAP_MPSMIN(readq(&dev->bar->cap)) + 12;
  1602. mem = dma_alloc_coherent(&dev->pci_dev->dev, 8192, &dma_addr,
  1603. GFP_KERNEL);
  1604. if (!mem)
  1605. return -ENOMEM;
  1606. res = nvme_identify(dev, 0, 1, dma_addr);
  1607. if (res) {
  1608. res = -EIO;
  1609. goto out;
  1610. }
  1611. ctrl = mem;
  1612. nn = le32_to_cpup(&ctrl->nn);
  1613. dev->oncs = le16_to_cpup(&ctrl->oncs);
  1614. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  1615. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  1616. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  1617. if (ctrl->mdts)
  1618. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  1619. if ((dev->pci_dev->vendor == PCI_VENDOR_ID_INTEL) &&
  1620. (dev->pci_dev->device == 0x0953) && ctrl->vs[3])
  1621. dev->stripe_size = 1 << (ctrl->vs[3] + shift);
  1622. id_ns = mem;
  1623. for (i = 1; i <= nn; i++) {
  1624. res = nvme_identify(dev, i, 0, dma_addr);
  1625. if (res)
  1626. continue;
  1627. if (id_ns->ncap == 0)
  1628. continue;
  1629. res = nvme_get_features(dev, NVME_FEAT_LBA_RANGE, i,
  1630. dma_addr + 4096, NULL);
  1631. if (res)
  1632. memset(mem + 4096, 0, 4096);
  1633. ns = nvme_alloc_ns(dev, i, mem, mem + 4096);
  1634. if (ns)
  1635. list_add_tail(&ns->list, &dev->namespaces);
  1636. }
  1637. list_for_each_entry(ns, &dev->namespaces, list)
  1638. add_disk(ns->disk);
  1639. res = 0;
  1640. out:
  1641. dma_free_coherent(&dev->pci_dev->dev, 8192, mem, dma_addr);
  1642. return res;
  1643. }
  1644. static int nvme_dev_map(struct nvme_dev *dev)
  1645. {
  1646. int bars, result = -ENOMEM;
  1647. struct pci_dev *pdev = dev->pci_dev;
  1648. if (pci_enable_device_mem(pdev))
  1649. return result;
  1650. dev->entry[0].vector = pdev->irq;
  1651. pci_set_master(pdev);
  1652. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1653. if (pci_request_selected_regions(pdev, bars, "nvme"))
  1654. goto disable_pci;
  1655. if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)))
  1656. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1657. else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)))
  1658. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  1659. else
  1660. goto disable_pci;
  1661. pci_set_drvdata(pdev, dev);
  1662. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1663. if (!dev->bar)
  1664. goto disable;
  1665. dev->db_stride = NVME_CAP_STRIDE(readq(&dev->bar->cap));
  1666. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  1667. return 0;
  1668. disable:
  1669. pci_release_regions(pdev);
  1670. disable_pci:
  1671. pci_disable_device(pdev);
  1672. return result;
  1673. }
  1674. static void nvme_dev_unmap(struct nvme_dev *dev)
  1675. {
  1676. if (dev->pci_dev->msi_enabled)
  1677. pci_disable_msi(dev->pci_dev);
  1678. else if (dev->pci_dev->msix_enabled)
  1679. pci_disable_msix(dev->pci_dev);
  1680. if (dev->bar) {
  1681. iounmap(dev->bar);
  1682. dev->bar = NULL;
  1683. }
  1684. pci_release_regions(dev->pci_dev);
  1685. if (pci_is_enabled(dev->pci_dev))
  1686. pci_disable_device(dev->pci_dev);
  1687. }
  1688. static void nvme_dev_shutdown(struct nvme_dev *dev)
  1689. {
  1690. int i;
  1691. for (i = dev->queue_count - 1; i >= 0; i--)
  1692. nvme_disable_queue(dev, i);
  1693. spin_lock(&dev_list_lock);
  1694. list_del_init(&dev->node);
  1695. spin_unlock(&dev_list_lock);
  1696. if (dev->bar)
  1697. nvme_shutdown_ctrl(dev);
  1698. nvme_dev_unmap(dev);
  1699. }
  1700. static void nvme_dev_remove(struct nvme_dev *dev)
  1701. {
  1702. struct nvme_ns *ns, *next;
  1703. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  1704. list_del(&ns->list);
  1705. del_gendisk(ns->disk);
  1706. nvme_ns_free(ns);
  1707. }
  1708. }
  1709. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1710. {
  1711. struct device *dmadev = &dev->pci_dev->dev;
  1712. dev->prp_page_pool = dma_pool_create("prp list page", dmadev,
  1713. PAGE_SIZE, PAGE_SIZE, 0);
  1714. if (!dev->prp_page_pool)
  1715. return -ENOMEM;
  1716. /* Optimisation for I/Os between 4k and 128k */
  1717. dev->prp_small_pool = dma_pool_create("prp list 256", dmadev,
  1718. 256, 256, 0);
  1719. if (!dev->prp_small_pool) {
  1720. dma_pool_destroy(dev->prp_page_pool);
  1721. return -ENOMEM;
  1722. }
  1723. return 0;
  1724. }
  1725. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1726. {
  1727. dma_pool_destroy(dev->prp_page_pool);
  1728. dma_pool_destroy(dev->prp_small_pool);
  1729. }
  1730. static DEFINE_IDA(nvme_instance_ida);
  1731. static int nvme_set_instance(struct nvme_dev *dev)
  1732. {
  1733. int instance, error;
  1734. do {
  1735. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  1736. return -ENODEV;
  1737. spin_lock(&dev_list_lock);
  1738. error = ida_get_new(&nvme_instance_ida, &instance);
  1739. spin_unlock(&dev_list_lock);
  1740. } while (error == -EAGAIN);
  1741. if (error)
  1742. return -ENODEV;
  1743. dev->instance = instance;
  1744. return 0;
  1745. }
  1746. static void nvme_release_instance(struct nvme_dev *dev)
  1747. {
  1748. spin_lock(&dev_list_lock);
  1749. ida_remove(&nvme_instance_ida, dev->instance);
  1750. spin_unlock(&dev_list_lock);
  1751. }
  1752. static void nvme_free_dev(struct kref *kref)
  1753. {
  1754. struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
  1755. nvme_dev_remove(dev);
  1756. nvme_dev_shutdown(dev);
  1757. nvme_free_queues(dev);
  1758. nvme_release_instance(dev);
  1759. nvme_release_prp_pools(dev);
  1760. kfree(dev->queues);
  1761. kfree(dev->entry);
  1762. kfree(dev);
  1763. }
  1764. static int nvme_dev_open(struct inode *inode, struct file *f)
  1765. {
  1766. struct nvme_dev *dev = container_of(f->private_data, struct nvme_dev,
  1767. miscdev);
  1768. kref_get(&dev->kref);
  1769. f->private_data = dev;
  1770. return 0;
  1771. }
  1772. static int nvme_dev_release(struct inode *inode, struct file *f)
  1773. {
  1774. struct nvme_dev *dev = f->private_data;
  1775. kref_put(&dev->kref, nvme_free_dev);
  1776. return 0;
  1777. }
  1778. static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  1779. {
  1780. struct nvme_dev *dev = f->private_data;
  1781. switch (cmd) {
  1782. case NVME_IOCTL_ADMIN_CMD:
  1783. return nvme_user_admin_cmd(dev, (void __user *)arg);
  1784. default:
  1785. return -ENOTTY;
  1786. }
  1787. }
  1788. static const struct file_operations nvme_dev_fops = {
  1789. .owner = THIS_MODULE,
  1790. .open = nvme_dev_open,
  1791. .release = nvme_dev_release,
  1792. .unlocked_ioctl = nvme_dev_ioctl,
  1793. .compat_ioctl = nvme_dev_ioctl,
  1794. };
  1795. static int nvme_dev_start(struct nvme_dev *dev)
  1796. {
  1797. int result;
  1798. result = nvme_dev_map(dev);
  1799. if (result)
  1800. return result;
  1801. result = nvme_configure_admin_queue(dev);
  1802. if (result)
  1803. goto unmap;
  1804. spin_lock(&dev_list_lock);
  1805. list_add(&dev->node, &dev_list);
  1806. spin_unlock(&dev_list_lock);
  1807. result = nvme_setup_io_queues(dev);
  1808. if (result)
  1809. goto disable;
  1810. return 0;
  1811. disable:
  1812. spin_lock(&dev_list_lock);
  1813. list_del_init(&dev->node);
  1814. spin_unlock(&dev_list_lock);
  1815. unmap:
  1816. nvme_dev_unmap(dev);
  1817. return result;
  1818. }
  1819. static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1820. {
  1821. int result = -ENOMEM;
  1822. struct nvme_dev *dev;
  1823. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1824. if (!dev)
  1825. return -ENOMEM;
  1826. dev->entry = kcalloc(num_possible_cpus(), sizeof(*dev->entry),
  1827. GFP_KERNEL);
  1828. if (!dev->entry)
  1829. goto free;
  1830. dev->queues = kcalloc(num_possible_cpus() + 1, sizeof(void *),
  1831. GFP_KERNEL);
  1832. if (!dev->queues)
  1833. goto free;
  1834. INIT_LIST_HEAD(&dev->namespaces);
  1835. dev->pci_dev = pdev;
  1836. result = nvme_set_instance(dev);
  1837. if (result)
  1838. goto free;
  1839. result = nvme_setup_prp_pools(dev);
  1840. if (result)
  1841. goto release;
  1842. result = nvme_dev_start(dev);
  1843. if (result)
  1844. goto release_pools;
  1845. result = nvme_dev_add(dev);
  1846. if (result && result != -EBUSY)
  1847. goto shutdown;
  1848. scnprintf(dev->name, sizeof(dev->name), "nvme%d", dev->instance);
  1849. dev->miscdev.minor = MISC_DYNAMIC_MINOR;
  1850. dev->miscdev.parent = &pdev->dev;
  1851. dev->miscdev.name = dev->name;
  1852. dev->miscdev.fops = &nvme_dev_fops;
  1853. result = misc_register(&dev->miscdev);
  1854. if (result)
  1855. goto remove;
  1856. kref_init(&dev->kref);
  1857. return 0;
  1858. remove:
  1859. nvme_dev_remove(dev);
  1860. shutdown:
  1861. nvme_dev_shutdown(dev);
  1862. release_pools:
  1863. nvme_free_queues(dev);
  1864. nvme_release_prp_pools(dev);
  1865. release:
  1866. nvme_release_instance(dev);
  1867. free:
  1868. kfree(dev->queues);
  1869. kfree(dev->entry);
  1870. kfree(dev);
  1871. return result;
  1872. }
  1873. static void nvme_remove(struct pci_dev *pdev)
  1874. {
  1875. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1876. misc_deregister(&dev->miscdev);
  1877. kref_put(&dev->kref, nvme_free_dev);
  1878. }
  1879. /* These functions are yet to be implemented */
  1880. #define nvme_error_detected NULL
  1881. #define nvme_dump_registers NULL
  1882. #define nvme_link_reset NULL
  1883. #define nvme_slot_reset NULL
  1884. #define nvme_error_resume NULL
  1885. static int nvme_suspend(struct device *dev)
  1886. {
  1887. struct pci_dev *pdev = to_pci_dev(dev);
  1888. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  1889. nvme_dev_shutdown(ndev);
  1890. return 0;
  1891. }
  1892. static int nvme_resume(struct device *dev)
  1893. {
  1894. struct pci_dev *pdev = to_pci_dev(dev);
  1895. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  1896. int ret;
  1897. ret = nvme_dev_start(ndev);
  1898. /* XXX: should remove gendisks if resume fails */
  1899. if (ret)
  1900. nvme_free_queues(ndev);
  1901. return ret;
  1902. }
  1903. static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  1904. static const struct pci_error_handlers nvme_err_handler = {
  1905. .error_detected = nvme_error_detected,
  1906. .mmio_enabled = nvme_dump_registers,
  1907. .link_reset = nvme_link_reset,
  1908. .slot_reset = nvme_slot_reset,
  1909. .resume = nvme_error_resume,
  1910. };
  1911. /* Move to pci_ids.h later */
  1912. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  1913. static DEFINE_PCI_DEVICE_TABLE(nvme_id_table) = {
  1914. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  1915. { 0, }
  1916. };
  1917. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  1918. static struct pci_driver nvme_driver = {
  1919. .name = "nvme",
  1920. .id_table = nvme_id_table,
  1921. .probe = nvme_probe,
  1922. .remove = nvme_remove,
  1923. .driver = {
  1924. .pm = &nvme_dev_pm_ops,
  1925. },
  1926. .err_handler = &nvme_err_handler,
  1927. };
  1928. static int __init nvme_init(void)
  1929. {
  1930. int result;
  1931. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  1932. if (IS_ERR(nvme_thread))
  1933. return PTR_ERR(nvme_thread);
  1934. result = register_blkdev(nvme_major, "nvme");
  1935. if (result < 0)
  1936. goto kill_kthread;
  1937. else if (result > 0)
  1938. nvme_major = result;
  1939. result = pci_register_driver(&nvme_driver);
  1940. if (result)
  1941. goto unregister_blkdev;
  1942. return 0;
  1943. unregister_blkdev:
  1944. unregister_blkdev(nvme_major, "nvme");
  1945. kill_kthread:
  1946. kthread_stop(nvme_thread);
  1947. return result;
  1948. }
  1949. static void __exit nvme_exit(void)
  1950. {
  1951. pci_unregister_driver(&nvme_driver);
  1952. unregister_blkdev(nvme_major, "nvme");
  1953. kthread_stop(nvme_thread);
  1954. }
  1955. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  1956. MODULE_LICENSE("GPL");
  1957. MODULE_VERSION("0.8");
  1958. module_init(nvme_init);
  1959. module_exit(nvme_exit);