nvme-core.c 51 KB

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