nvme-core.c 55 KB

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