pci.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. /*
  2. * NVM Express device driver
  3. * Copyright (c) 2011-2014, 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. #include <linux/aer.h>
  15. #include <linux/bitops.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/blk-mq.h>
  18. #include <linux/blk-mq-pci.h>
  19. #include <linux/cpu.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/genhd.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/idr.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/kdev_t.h>
  30. #include <linux/kernel.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/mutex.h>
  35. #include <linux/pci.h>
  36. #include <linux/poison.h>
  37. #include <linux/ptrace.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/t10-pi.h>
  41. #include <linux/timer.h>
  42. #include <linux/types.h>
  43. #include <linux/io-64-nonatomic-lo-hi.h>
  44. #include <asm/unaligned.h>
  45. #include "nvme.h"
  46. #define NVME_Q_DEPTH 1024
  47. #define NVME_AQ_DEPTH 256
  48. #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
  49. #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
  50. /*
  51. * We handle AEN commands ourselves and don't even let the
  52. * block layer know about them.
  53. */
  54. #define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AERS)
  55. static int use_threaded_interrupts;
  56. module_param(use_threaded_interrupts, int, 0);
  57. static bool use_cmb_sqes = true;
  58. module_param(use_cmb_sqes, bool, 0644);
  59. MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
  60. static struct workqueue_struct *nvme_workq;
  61. struct nvme_dev;
  62. struct nvme_queue;
  63. static int nvme_reset(struct nvme_dev *dev);
  64. static void nvme_process_cq(struct nvme_queue *nvmeq);
  65. static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
  66. /*
  67. * Represents an NVM Express device. Each nvme_dev is a PCI function.
  68. */
  69. struct nvme_dev {
  70. struct nvme_queue **queues;
  71. struct blk_mq_tag_set tagset;
  72. struct blk_mq_tag_set admin_tagset;
  73. u32 __iomem *dbs;
  74. struct device *dev;
  75. struct dma_pool *prp_page_pool;
  76. struct dma_pool *prp_small_pool;
  77. unsigned queue_count;
  78. unsigned online_queues;
  79. unsigned max_qid;
  80. int q_depth;
  81. u32 db_stride;
  82. void __iomem *bar;
  83. struct work_struct reset_work;
  84. struct work_struct remove_work;
  85. struct timer_list watchdog_timer;
  86. struct mutex shutdown_lock;
  87. bool subsystem;
  88. void __iomem *cmb;
  89. pci_bus_addr_t cmb_bus_addr;
  90. u64 cmb_size;
  91. u32 cmbsz;
  92. u32 cmbloc;
  93. struct nvme_ctrl ctrl;
  94. struct completion ioq_wait;
  95. };
  96. static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
  97. {
  98. return container_of(ctrl, struct nvme_dev, ctrl);
  99. }
  100. /*
  101. * An NVM Express queue. Each device has at least two (one for admin
  102. * commands and one for I/O commands).
  103. */
  104. struct nvme_queue {
  105. struct device *q_dmadev;
  106. struct nvme_dev *dev;
  107. char irqname[24]; /* nvme4294967295-65535\0 */
  108. spinlock_t q_lock;
  109. struct nvme_command *sq_cmds;
  110. struct nvme_command __iomem *sq_cmds_io;
  111. volatile struct nvme_completion *cqes;
  112. struct blk_mq_tags **tags;
  113. dma_addr_t sq_dma_addr;
  114. dma_addr_t cq_dma_addr;
  115. u32 __iomem *q_db;
  116. u16 q_depth;
  117. s16 cq_vector;
  118. u16 sq_tail;
  119. u16 cq_head;
  120. u16 qid;
  121. u8 cq_phase;
  122. u8 cqe_seen;
  123. };
  124. /*
  125. * The nvme_iod describes the data in an I/O, including the list of PRP
  126. * entries. You can't see it in this data structure because C doesn't let
  127. * me express that. Use nvme_init_iod to ensure there's enough space
  128. * allocated to store the PRP list.
  129. */
  130. struct nvme_iod {
  131. struct nvme_queue *nvmeq;
  132. int aborted;
  133. int npages; /* In the PRP list. 0 means small pool in use */
  134. int nents; /* Used in scatterlist */
  135. int length; /* Of data, in bytes */
  136. dma_addr_t first_dma;
  137. struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
  138. struct scatterlist *sg;
  139. struct scatterlist inline_sg[0];
  140. };
  141. /*
  142. * Check we didin't inadvertently grow the command struct
  143. */
  144. static inline void _nvme_check_size(void)
  145. {
  146. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  147. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  148. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  149. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  150. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  151. BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
  152. BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
  153. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  154. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
  155. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
  156. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  157. BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
  158. }
  159. /*
  160. * Max size of iod being embedded in the request payload
  161. */
  162. #define NVME_INT_PAGES 2
  163. #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
  164. /*
  165. * Will slightly overestimate the number of pages needed. This is OK
  166. * as it only leads to a small amount of wasted memory for the lifetime of
  167. * the I/O.
  168. */
  169. static int nvme_npages(unsigned size, struct nvme_dev *dev)
  170. {
  171. unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
  172. dev->ctrl.page_size);
  173. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  174. }
  175. static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
  176. unsigned int size, unsigned int nseg)
  177. {
  178. return sizeof(__le64 *) * nvme_npages(size, dev) +
  179. sizeof(struct scatterlist) * nseg;
  180. }
  181. static unsigned int nvme_cmd_size(struct nvme_dev *dev)
  182. {
  183. return sizeof(struct nvme_iod) +
  184. nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
  185. }
  186. static int nvmeq_irq(struct nvme_queue *nvmeq)
  187. {
  188. return pci_irq_vector(to_pci_dev(nvmeq->dev->dev), nvmeq->cq_vector);
  189. }
  190. static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  191. unsigned int hctx_idx)
  192. {
  193. struct nvme_dev *dev = data;
  194. struct nvme_queue *nvmeq = dev->queues[0];
  195. WARN_ON(hctx_idx != 0);
  196. WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
  197. WARN_ON(nvmeq->tags);
  198. hctx->driver_data = nvmeq;
  199. nvmeq->tags = &dev->admin_tagset.tags[0];
  200. return 0;
  201. }
  202. static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  203. {
  204. struct nvme_queue *nvmeq = hctx->driver_data;
  205. nvmeq->tags = NULL;
  206. }
  207. static int nvme_admin_init_request(void *data, struct request *req,
  208. unsigned int hctx_idx, unsigned int rq_idx,
  209. unsigned int numa_node)
  210. {
  211. struct nvme_dev *dev = data;
  212. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  213. struct nvme_queue *nvmeq = dev->queues[0];
  214. BUG_ON(!nvmeq);
  215. iod->nvmeq = nvmeq;
  216. return 0;
  217. }
  218. static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  219. unsigned int hctx_idx)
  220. {
  221. struct nvme_dev *dev = data;
  222. struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
  223. if (!nvmeq->tags)
  224. nvmeq->tags = &dev->tagset.tags[hctx_idx];
  225. WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
  226. hctx->driver_data = nvmeq;
  227. return 0;
  228. }
  229. static int nvme_init_request(void *data, struct request *req,
  230. unsigned int hctx_idx, unsigned int rq_idx,
  231. unsigned int numa_node)
  232. {
  233. struct nvme_dev *dev = data;
  234. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  235. struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
  236. BUG_ON(!nvmeq);
  237. iod->nvmeq = nvmeq;
  238. return 0;
  239. }
  240. static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
  241. {
  242. struct nvme_dev *dev = set->driver_data;
  243. return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
  244. }
  245. /**
  246. * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  247. * @nvmeq: The queue to use
  248. * @cmd: The command to send
  249. *
  250. * Safe to use from interrupt context
  251. */
  252. static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
  253. struct nvme_command *cmd)
  254. {
  255. u16 tail = nvmeq->sq_tail;
  256. if (nvmeq->sq_cmds_io)
  257. memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
  258. else
  259. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  260. if (++tail == nvmeq->q_depth)
  261. tail = 0;
  262. writel(tail, nvmeq->q_db);
  263. nvmeq->sq_tail = tail;
  264. }
  265. static __le64 **iod_list(struct request *req)
  266. {
  267. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  268. return (__le64 **)(iod->sg + req->nr_phys_segments);
  269. }
  270. static int nvme_init_iod(struct request *rq, unsigned size,
  271. struct nvme_dev *dev)
  272. {
  273. struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
  274. int nseg = rq->nr_phys_segments;
  275. if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
  276. iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
  277. if (!iod->sg)
  278. return BLK_MQ_RQ_QUEUE_BUSY;
  279. } else {
  280. iod->sg = iod->inline_sg;
  281. }
  282. iod->aborted = 0;
  283. iod->npages = -1;
  284. iod->nents = 0;
  285. iod->length = size;
  286. if (!(rq->cmd_flags & REQ_DONTPREP)) {
  287. rq->retries = 0;
  288. rq->cmd_flags |= REQ_DONTPREP;
  289. }
  290. return 0;
  291. }
  292. static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
  293. {
  294. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  295. const int last_prp = dev->ctrl.page_size / 8 - 1;
  296. int i;
  297. __le64 **list = iod_list(req);
  298. dma_addr_t prp_dma = iod->first_dma;
  299. nvme_cleanup_cmd(req);
  300. if (iod->npages == 0)
  301. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  302. for (i = 0; i < iod->npages; i++) {
  303. __le64 *prp_list = list[i];
  304. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  305. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  306. prp_dma = next_prp_dma;
  307. }
  308. if (iod->sg != iod->inline_sg)
  309. kfree(iod->sg);
  310. }
  311. #ifdef CONFIG_BLK_DEV_INTEGRITY
  312. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  313. {
  314. if (be32_to_cpu(pi->ref_tag) == v)
  315. pi->ref_tag = cpu_to_be32(p);
  316. }
  317. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  318. {
  319. if (be32_to_cpu(pi->ref_tag) == p)
  320. pi->ref_tag = cpu_to_be32(v);
  321. }
  322. /**
  323. * nvme_dif_remap - remaps ref tags to bip seed and physical lba
  324. *
  325. * The virtual start sector is the one that was originally submitted by the
  326. * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
  327. * start sector may be different. Remap protection information to match the
  328. * physical LBA on writes, and back to the original seed on reads.
  329. *
  330. * Type 0 and 3 do not have a ref tag, so no remapping required.
  331. */
  332. static void nvme_dif_remap(struct request *req,
  333. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  334. {
  335. struct nvme_ns *ns = req->rq_disk->private_data;
  336. struct bio_integrity_payload *bip;
  337. struct t10_pi_tuple *pi;
  338. void *p, *pmap;
  339. u32 i, nlb, ts, phys, virt;
  340. if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
  341. return;
  342. bip = bio_integrity(req->bio);
  343. if (!bip)
  344. return;
  345. pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
  346. p = pmap;
  347. virt = bip_get_seed(bip);
  348. phys = nvme_block_nr(ns, blk_rq_pos(req));
  349. nlb = (blk_rq_bytes(req) >> ns->lba_shift);
  350. ts = ns->disk->queue->integrity.tuple_size;
  351. for (i = 0; i < nlb; i++, virt++, phys++) {
  352. pi = (struct t10_pi_tuple *)p;
  353. dif_swap(phys, virt, pi);
  354. p += ts;
  355. }
  356. kunmap_atomic(pmap);
  357. }
  358. #else /* CONFIG_BLK_DEV_INTEGRITY */
  359. static void nvme_dif_remap(struct request *req,
  360. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  361. {
  362. }
  363. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  364. {
  365. }
  366. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  367. {
  368. }
  369. #endif
  370. static bool nvme_setup_prps(struct nvme_dev *dev, struct request *req,
  371. int total_len)
  372. {
  373. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  374. struct dma_pool *pool;
  375. int length = total_len;
  376. struct scatterlist *sg = iod->sg;
  377. int dma_len = sg_dma_len(sg);
  378. u64 dma_addr = sg_dma_address(sg);
  379. u32 page_size = dev->ctrl.page_size;
  380. int offset = dma_addr & (page_size - 1);
  381. __le64 *prp_list;
  382. __le64 **list = iod_list(req);
  383. dma_addr_t prp_dma;
  384. int nprps, i;
  385. length -= (page_size - offset);
  386. if (length <= 0)
  387. return true;
  388. dma_len -= (page_size - offset);
  389. if (dma_len) {
  390. dma_addr += (page_size - offset);
  391. } else {
  392. sg = sg_next(sg);
  393. dma_addr = sg_dma_address(sg);
  394. dma_len = sg_dma_len(sg);
  395. }
  396. if (length <= page_size) {
  397. iod->first_dma = dma_addr;
  398. return true;
  399. }
  400. nprps = DIV_ROUND_UP(length, page_size);
  401. if (nprps <= (256 / 8)) {
  402. pool = dev->prp_small_pool;
  403. iod->npages = 0;
  404. } else {
  405. pool = dev->prp_page_pool;
  406. iod->npages = 1;
  407. }
  408. prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
  409. if (!prp_list) {
  410. iod->first_dma = dma_addr;
  411. iod->npages = -1;
  412. return false;
  413. }
  414. list[0] = prp_list;
  415. iod->first_dma = prp_dma;
  416. i = 0;
  417. for (;;) {
  418. if (i == page_size >> 3) {
  419. __le64 *old_prp_list = prp_list;
  420. prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
  421. if (!prp_list)
  422. return false;
  423. list[iod->npages++] = prp_list;
  424. prp_list[0] = old_prp_list[i - 1];
  425. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  426. i = 1;
  427. }
  428. prp_list[i++] = cpu_to_le64(dma_addr);
  429. dma_len -= page_size;
  430. dma_addr += page_size;
  431. length -= page_size;
  432. if (length <= 0)
  433. break;
  434. if (dma_len > 0)
  435. continue;
  436. BUG_ON(dma_len < 0);
  437. sg = sg_next(sg);
  438. dma_addr = sg_dma_address(sg);
  439. dma_len = sg_dma_len(sg);
  440. }
  441. return true;
  442. }
  443. static int nvme_map_data(struct nvme_dev *dev, struct request *req,
  444. unsigned size, struct nvme_command *cmnd)
  445. {
  446. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  447. struct request_queue *q = req->q;
  448. enum dma_data_direction dma_dir = rq_data_dir(req) ?
  449. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  450. int ret = BLK_MQ_RQ_QUEUE_ERROR;
  451. sg_init_table(iod->sg, req->nr_phys_segments);
  452. iod->nents = blk_rq_map_sg(q, req, iod->sg);
  453. if (!iod->nents)
  454. goto out;
  455. ret = BLK_MQ_RQ_QUEUE_BUSY;
  456. if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
  457. DMA_ATTR_NO_WARN))
  458. goto out;
  459. if (!nvme_setup_prps(dev, req, size))
  460. goto out_unmap;
  461. ret = BLK_MQ_RQ_QUEUE_ERROR;
  462. if (blk_integrity_rq(req)) {
  463. if (blk_rq_count_integrity_sg(q, req->bio) != 1)
  464. goto out_unmap;
  465. sg_init_table(&iod->meta_sg, 1);
  466. if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
  467. goto out_unmap;
  468. if (rq_data_dir(req))
  469. nvme_dif_remap(req, nvme_dif_prep);
  470. if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
  471. goto out_unmap;
  472. }
  473. cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  474. cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
  475. if (blk_integrity_rq(req))
  476. cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
  477. return BLK_MQ_RQ_QUEUE_OK;
  478. out_unmap:
  479. dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
  480. out:
  481. return ret;
  482. }
  483. static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
  484. {
  485. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  486. enum dma_data_direction dma_dir = rq_data_dir(req) ?
  487. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  488. if (iod->nents) {
  489. dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
  490. if (blk_integrity_rq(req)) {
  491. if (!rq_data_dir(req))
  492. nvme_dif_remap(req, nvme_dif_complete);
  493. dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
  494. }
  495. }
  496. nvme_free_iod(dev, req);
  497. }
  498. /*
  499. * NOTE: ns is NULL when called on the admin queue.
  500. */
  501. static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
  502. const struct blk_mq_queue_data *bd)
  503. {
  504. struct nvme_ns *ns = hctx->queue->queuedata;
  505. struct nvme_queue *nvmeq = hctx->driver_data;
  506. struct nvme_dev *dev = nvmeq->dev;
  507. struct request *req = bd->rq;
  508. struct nvme_command cmnd;
  509. unsigned map_len;
  510. int ret = BLK_MQ_RQ_QUEUE_OK;
  511. /*
  512. * If formated with metadata, require the block layer provide a buffer
  513. * unless this namespace is formated such that the metadata can be
  514. * stripped/generated by the controller with PRACT=1.
  515. */
  516. if (ns && ns->ms && !blk_integrity_rq(req)) {
  517. if (!(ns->pi_type && ns->ms == 8) &&
  518. req->cmd_type != REQ_TYPE_DRV_PRIV) {
  519. blk_mq_end_request(req, -EFAULT);
  520. return BLK_MQ_RQ_QUEUE_OK;
  521. }
  522. }
  523. map_len = nvme_map_len(req);
  524. ret = nvme_init_iod(req, map_len, dev);
  525. if (ret)
  526. return ret;
  527. ret = nvme_setup_cmd(ns, req, &cmnd);
  528. if (ret)
  529. goto out;
  530. if (req->nr_phys_segments)
  531. ret = nvme_map_data(dev, req, map_len, &cmnd);
  532. if (ret)
  533. goto out;
  534. cmnd.common.command_id = req->tag;
  535. blk_mq_start_request(req);
  536. spin_lock_irq(&nvmeq->q_lock);
  537. if (unlikely(nvmeq->cq_vector < 0)) {
  538. if (ns && !test_bit(NVME_NS_DEAD, &ns->flags))
  539. ret = BLK_MQ_RQ_QUEUE_BUSY;
  540. else
  541. ret = BLK_MQ_RQ_QUEUE_ERROR;
  542. spin_unlock_irq(&nvmeq->q_lock);
  543. goto out;
  544. }
  545. __nvme_submit_cmd(nvmeq, &cmnd);
  546. nvme_process_cq(nvmeq);
  547. spin_unlock_irq(&nvmeq->q_lock);
  548. return BLK_MQ_RQ_QUEUE_OK;
  549. out:
  550. nvme_free_iod(dev, req);
  551. return ret;
  552. }
  553. static void nvme_complete_rq(struct request *req)
  554. {
  555. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  556. struct nvme_dev *dev = iod->nvmeq->dev;
  557. int error = 0;
  558. nvme_unmap_data(dev, req);
  559. if (unlikely(req->errors)) {
  560. if (nvme_req_needs_retry(req, req->errors)) {
  561. req->retries++;
  562. nvme_requeue_req(req);
  563. return;
  564. }
  565. if (req->cmd_type == REQ_TYPE_DRV_PRIV)
  566. error = req->errors;
  567. else
  568. error = nvme_error_status(req->errors);
  569. }
  570. if (unlikely(iod->aborted)) {
  571. dev_warn(dev->ctrl.device,
  572. "completing aborted command with status: %04x\n",
  573. req->errors);
  574. }
  575. blk_mq_end_request(req, error);
  576. }
  577. /* We read the CQE phase first to check if the rest of the entry is valid */
  578. static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
  579. u16 phase)
  580. {
  581. return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
  582. }
  583. static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
  584. {
  585. u16 head, phase;
  586. head = nvmeq->cq_head;
  587. phase = nvmeq->cq_phase;
  588. while (nvme_cqe_valid(nvmeq, head, phase)) {
  589. struct nvme_completion cqe = nvmeq->cqes[head];
  590. struct request *req;
  591. if (++head == nvmeq->q_depth) {
  592. head = 0;
  593. phase = !phase;
  594. }
  595. if (tag && *tag == cqe.command_id)
  596. *tag = -1;
  597. if (unlikely(cqe.command_id >= nvmeq->q_depth)) {
  598. dev_warn(nvmeq->dev->ctrl.device,
  599. "invalid id %d completed on queue %d\n",
  600. cqe.command_id, le16_to_cpu(cqe.sq_id));
  601. continue;
  602. }
  603. /*
  604. * AEN requests are special as they don't time out and can
  605. * survive any kind of queue freeze and often don't respond to
  606. * aborts. We don't even bother to allocate a struct request
  607. * for them but rather special case them here.
  608. */
  609. if (unlikely(nvmeq->qid == 0 &&
  610. cqe.command_id >= NVME_AQ_BLKMQ_DEPTH)) {
  611. nvme_complete_async_event(&nvmeq->dev->ctrl, &cqe);
  612. continue;
  613. }
  614. req = blk_mq_tag_to_rq(*nvmeq->tags, cqe.command_id);
  615. if (req->cmd_type == REQ_TYPE_DRV_PRIV && req->special)
  616. memcpy(req->special, &cqe, sizeof(cqe));
  617. blk_mq_complete_request(req, le16_to_cpu(cqe.status) >> 1);
  618. }
  619. /* If the controller ignores the cq head doorbell and continuously
  620. * writes to the queue, it is theoretically possible to wrap around
  621. * the queue twice and mistakenly return IRQ_NONE. Linux only
  622. * requires that 0.1% of your interrupts are handled, so this isn't
  623. * a big problem.
  624. */
  625. if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
  626. return;
  627. if (likely(nvmeq->cq_vector >= 0))
  628. writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
  629. nvmeq->cq_head = head;
  630. nvmeq->cq_phase = phase;
  631. nvmeq->cqe_seen = 1;
  632. }
  633. static void nvme_process_cq(struct nvme_queue *nvmeq)
  634. {
  635. __nvme_process_cq(nvmeq, NULL);
  636. }
  637. static irqreturn_t nvme_irq(int irq, void *data)
  638. {
  639. irqreturn_t result;
  640. struct nvme_queue *nvmeq = data;
  641. spin_lock(&nvmeq->q_lock);
  642. nvme_process_cq(nvmeq);
  643. result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
  644. nvmeq->cqe_seen = 0;
  645. spin_unlock(&nvmeq->q_lock);
  646. return result;
  647. }
  648. static irqreturn_t nvme_irq_check(int irq, void *data)
  649. {
  650. struct nvme_queue *nvmeq = data;
  651. if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
  652. return IRQ_WAKE_THREAD;
  653. return IRQ_NONE;
  654. }
  655. static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
  656. {
  657. struct nvme_queue *nvmeq = hctx->driver_data;
  658. if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
  659. spin_lock_irq(&nvmeq->q_lock);
  660. __nvme_process_cq(nvmeq, &tag);
  661. spin_unlock_irq(&nvmeq->q_lock);
  662. if (tag == -1)
  663. return 1;
  664. }
  665. return 0;
  666. }
  667. static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
  668. {
  669. struct nvme_dev *dev = to_nvme_dev(ctrl);
  670. struct nvme_queue *nvmeq = dev->queues[0];
  671. struct nvme_command c;
  672. memset(&c, 0, sizeof(c));
  673. c.common.opcode = nvme_admin_async_event;
  674. c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
  675. spin_lock_irq(&nvmeq->q_lock);
  676. __nvme_submit_cmd(nvmeq, &c);
  677. spin_unlock_irq(&nvmeq->q_lock);
  678. }
  679. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  680. {
  681. struct nvme_command c;
  682. memset(&c, 0, sizeof(c));
  683. c.delete_queue.opcode = opcode;
  684. c.delete_queue.qid = cpu_to_le16(id);
  685. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  686. }
  687. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  688. struct nvme_queue *nvmeq)
  689. {
  690. struct nvme_command c;
  691. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  692. /*
  693. * Note: we (ab)use the fact the the prp fields survive if no data
  694. * is attached to the request.
  695. */
  696. memset(&c, 0, sizeof(c));
  697. c.create_cq.opcode = nvme_admin_create_cq;
  698. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  699. c.create_cq.cqid = cpu_to_le16(qid);
  700. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  701. c.create_cq.cq_flags = cpu_to_le16(flags);
  702. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  703. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  704. }
  705. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  706. struct nvme_queue *nvmeq)
  707. {
  708. struct nvme_command c;
  709. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  710. /*
  711. * Note: we (ab)use the fact the the prp fields survive if no data
  712. * is attached to the request.
  713. */
  714. memset(&c, 0, sizeof(c));
  715. c.create_sq.opcode = nvme_admin_create_sq;
  716. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  717. c.create_sq.sqid = cpu_to_le16(qid);
  718. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  719. c.create_sq.sq_flags = cpu_to_le16(flags);
  720. c.create_sq.cqid = cpu_to_le16(qid);
  721. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  722. }
  723. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  724. {
  725. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  726. }
  727. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  728. {
  729. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  730. }
  731. static void abort_endio(struct request *req, int error)
  732. {
  733. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  734. struct nvme_queue *nvmeq = iod->nvmeq;
  735. u16 status = req->errors;
  736. dev_warn(nvmeq->dev->ctrl.device, "Abort status: 0x%x", status);
  737. atomic_inc(&nvmeq->dev->ctrl.abort_limit);
  738. blk_mq_free_request(req);
  739. }
  740. static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
  741. {
  742. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  743. struct nvme_queue *nvmeq = iod->nvmeq;
  744. struct nvme_dev *dev = nvmeq->dev;
  745. struct request *abort_req;
  746. struct nvme_command cmd;
  747. /*
  748. * Shutdown immediately if controller times out while starting. The
  749. * reset work will see the pci device disabled when it gets the forced
  750. * cancellation error. All outstanding requests are completed on
  751. * shutdown, so we return BLK_EH_HANDLED.
  752. */
  753. if (dev->ctrl.state == NVME_CTRL_RESETTING) {
  754. dev_warn(dev->ctrl.device,
  755. "I/O %d QID %d timeout, disable controller\n",
  756. req->tag, nvmeq->qid);
  757. nvme_dev_disable(dev, false);
  758. req->errors = NVME_SC_CANCELLED;
  759. return BLK_EH_HANDLED;
  760. }
  761. /*
  762. * Shutdown the controller immediately and schedule a reset if the
  763. * command was already aborted once before and still hasn't been
  764. * returned to the driver, or if this is the admin queue.
  765. */
  766. if (!nvmeq->qid || iod->aborted) {
  767. dev_warn(dev->ctrl.device,
  768. "I/O %d QID %d timeout, reset controller\n",
  769. req->tag, nvmeq->qid);
  770. nvme_dev_disable(dev, false);
  771. nvme_reset(dev);
  772. /*
  773. * Mark the request as handled, since the inline shutdown
  774. * forces all outstanding requests to complete.
  775. */
  776. req->errors = NVME_SC_CANCELLED;
  777. return BLK_EH_HANDLED;
  778. }
  779. iod->aborted = 1;
  780. if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
  781. atomic_inc(&dev->ctrl.abort_limit);
  782. return BLK_EH_RESET_TIMER;
  783. }
  784. memset(&cmd, 0, sizeof(cmd));
  785. cmd.abort.opcode = nvme_admin_abort_cmd;
  786. cmd.abort.cid = req->tag;
  787. cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
  788. dev_warn(nvmeq->dev->ctrl.device,
  789. "I/O %d QID %d timeout, aborting\n",
  790. req->tag, nvmeq->qid);
  791. abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
  792. BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
  793. if (IS_ERR(abort_req)) {
  794. atomic_inc(&dev->ctrl.abort_limit);
  795. return BLK_EH_RESET_TIMER;
  796. }
  797. abort_req->timeout = ADMIN_TIMEOUT;
  798. abort_req->end_io_data = NULL;
  799. blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
  800. /*
  801. * The aborted req will be completed on receiving the abort req.
  802. * We enable the timer again. If hit twice, it'll cause a device reset,
  803. * as the device then is in a faulty state.
  804. */
  805. return BLK_EH_RESET_TIMER;
  806. }
  807. static void nvme_free_queue(struct nvme_queue *nvmeq)
  808. {
  809. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  810. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  811. if (nvmeq->sq_cmds)
  812. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  813. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  814. kfree(nvmeq);
  815. }
  816. static void nvme_free_queues(struct nvme_dev *dev, int lowest)
  817. {
  818. int i;
  819. for (i = dev->queue_count - 1; i >= lowest; i--) {
  820. struct nvme_queue *nvmeq = dev->queues[i];
  821. dev->queue_count--;
  822. dev->queues[i] = NULL;
  823. nvme_free_queue(nvmeq);
  824. }
  825. }
  826. /**
  827. * nvme_suspend_queue - put queue into suspended state
  828. * @nvmeq - queue to suspend
  829. */
  830. static int nvme_suspend_queue(struct nvme_queue *nvmeq)
  831. {
  832. int vector;
  833. spin_lock_irq(&nvmeq->q_lock);
  834. if (nvmeq->cq_vector == -1) {
  835. spin_unlock_irq(&nvmeq->q_lock);
  836. return 1;
  837. }
  838. vector = nvmeq_irq(nvmeq);
  839. nvmeq->dev->online_queues--;
  840. nvmeq->cq_vector = -1;
  841. spin_unlock_irq(&nvmeq->q_lock);
  842. if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
  843. blk_mq_stop_hw_queues(nvmeq->dev->ctrl.admin_q);
  844. free_irq(vector, nvmeq);
  845. return 0;
  846. }
  847. static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
  848. {
  849. struct nvme_queue *nvmeq = dev->queues[0];
  850. if (!nvmeq)
  851. return;
  852. if (nvme_suspend_queue(nvmeq))
  853. return;
  854. if (shutdown)
  855. nvme_shutdown_ctrl(&dev->ctrl);
  856. else
  857. nvme_disable_ctrl(&dev->ctrl, lo_hi_readq(
  858. dev->bar + NVME_REG_CAP));
  859. spin_lock_irq(&nvmeq->q_lock);
  860. nvme_process_cq(nvmeq);
  861. spin_unlock_irq(&nvmeq->q_lock);
  862. }
  863. static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
  864. int entry_size)
  865. {
  866. int q_depth = dev->q_depth;
  867. unsigned q_size_aligned = roundup(q_depth * entry_size,
  868. dev->ctrl.page_size);
  869. if (q_size_aligned * nr_io_queues > dev->cmb_size) {
  870. u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
  871. mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
  872. q_depth = div_u64(mem_per_q, entry_size);
  873. /*
  874. * Ensure the reduced q_depth is above some threshold where it
  875. * would be better to map queues in system memory with the
  876. * original depth
  877. */
  878. if (q_depth < 64)
  879. return -ENOMEM;
  880. }
  881. return q_depth;
  882. }
  883. static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  884. int qid, int depth)
  885. {
  886. /* CMB SQEs will be mapped before creation */
  887. if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz))
  888. return 0;
  889. nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
  890. &nvmeq->sq_dma_addr, GFP_KERNEL);
  891. if (!nvmeq->sq_cmds)
  892. return -ENOMEM;
  893. return 0;
  894. }
  895. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  896. int depth)
  897. {
  898. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
  899. if (!nvmeq)
  900. return NULL;
  901. nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
  902. &nvmeq->cq_dma_addr, GFP_KERNEL);
  903. if (!nvmeq->cqes)
  904. goto free_nvmeq;
  905. if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
  906. goto free_cqdma;
  907. nvmeq->q_dmadev = dev->dev;
  908. nvmeq->dev = dev;
  909. snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
  910. dev->ctrl.instance, qid);
  911. spin_lock_init(&nvmeq->q_lock);
  912. nvmeq->cq_head = 0;
  913. nvmeq->cq_phase = 1;
  914. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  915. nvmeq->q_depth = depth;
  916. nvmeq->qid = qid;
  917. nvmeq->cq_vector = -1;
  918. dev->queues[qid] = nvmeq;
  919. dev->queue_count++;
  920. return nvmeq;
  921. free_cqdma:
  922. dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
  923. nvmeq->cq_dma_addr);
  924. free_nvmeq:
  925. kfree(nvmeq);
  926. return NULL;
  927. }
  928. static int queue_request_irq(struct nvme_queue *nvmeq)
  929. {
  930. if (use_threaded_interrupts)
  931. return request_threaded_irq(nvmeq_irq(nvmeq), nvme_irq_check,
  932. nvme_irq, IRQF_SHARED, nvmeq->irqname, nvmeq);
  933. else
  934. return request_irq(nvmeq_irq(nvmeq), nvme_irq, IRQF_SHARED,
  935. nvmeq->irqname, nvmeq);
  936. }
  937. static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
  938. {
  939. struct nvme_dev *dev = nvmeq->dev;
  940. spin_lock_irq(&nvmeq->q_lock);
  941. nvmeq->sq_tail = 0;
  942. nvmeq->cq_head = 0;
  943. nvmeq->cq_phase = 1;
  944. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  945. memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
  946. dev->online_queues++;
  947. spin_unlock_irq(&nvmeq->q_lock);
  948. }
  949. static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
  950. {
  951. struct nvme_dev *dev = nvmeq->dev;
  952. int result;
  953. if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
  954. unsigned offset = (qid - 1) * roundup(SQ_SIZE(nvmeq->q_depth),
  955. dev->ctrl.page_size);
  956. nvmeq->sq_dma_addr = dev->cmb_bus_addr + offset;
  957. nvmeq->sq_cmds_io = dev->cmb + offset;
  958. }
  959. nvmeq->cq_vector = qid - 1;
  960. result = adapter_alloc_cq(dev, qid, nvmeq);
  961. if (result < 0)
  962. goto release_vector;
  963. result = adapter_alloc_sq(dev, qid, nvmeq);
  964. if (result < 0)
  965. goto release_cq;
  966. nvme_init_queue(nvmeq, qid);
  967. result = queue_request_irq(nvmeq);
  968. if (result < 0)
  969. goto release_sq;
  970. return result;
  971. release_sq:
  972. dev->online_queues--;
  973. adapter_delete_sq(dev, qid);
  974. release_cq:
  975. adapter_delete_cq(dev, qid);
  976. release_vector:
  977. nvmeq->cq_vector = -1;
  978. return result;
  979. }
  980. static struct blk_mq_ops nvme_mq_admin_ops = {
  981. .queue_rq = nvme_queue_rq,
  982. .complete = nvme_complete_rq,
  983. .init_hctx = nvme_admin_init_hctx,
  984. .exit_hctx = nvme_admin_exit_hctx,
  985. .init_request = nvme_admin_init_request,
  986. .timeout = nvme_timeout,
  987. };
  988. static struct blk_mq_ops nvme_mq_ops = {
  989. .queue_rq = nvme_queue_rq,
  990. .complete = nvme_complete_rq,
  991. .init_hctx = nvme_init_hctx,
  992. .init_request = nvme_init_request,
  993. .map_queues = nvme_pci_map_queues,
  994. .timeout = nvme_timeout,
  995. .poll = nvme_poll,
  996. };
  997. static void nvme_dev_remove_admin(struct nvme_dev *dev)
  998. {
  999. if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
  1000. /*
  1001. * If the controller was reset during removal, it's possible
  1002. * user requests may be waiting on a stopped queue. Start the
  1003. * queue to flush these to completion.
  1004. */
  1005. blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
  1006. blk_cleanup_queue(dev->ctrl.admin_q);
  1007. blk_mq_free_tag_set(&dev->admin_tagset);
  1008. }
  1009. }
  1010. static int nvme_alloc_admin_tags(struct nvme_dev *dev)
  1011. {
  1012. if (!dev->ctrl.admin_q) {
  1013. dev->admin_tagset.ops = &nvme_mq_admin_ops;
  1014. dev->admin_tagset.nr_hw_queues = 1;
  1015. /*
  1016. * Subtract one to leave an empty queue entry for 'Full Queue'
  1017. * condition. See NVM-Express 1.2 specification, section 4.1.2.
  1018. */
  1019. dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
  1020. dev->admin_tagset.timeout = ADMIN_TIMEOUT;
  1021. dev->admin_tagset.numa_node = dev_to_node(dev->dev);
  1022. dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
  1023. dev->admin_tagset.driver_data = dev;
  1024. if (blk_mq_alloc_tag_set(&dev->admin_tagset))
  1025. return -ENOMEM;
  1026. dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
  1027. if (IS_ERR(dev->ctrl.admin_q)) {
  1028. blk_mq_free_tag_set(&dev->admin_tagset);
  1029. return -ENOMEM;
  1030. }
  1031. if (!blk_get_queue(dev->ctrl.admin_q)) {
  1032. nvme_dev_remove_admin(dev);
  1033. dev->ctrl.admin_q = NULL;
  1034. return -ENODEV;
  1035. }
  1036. } else
  1037. blk_mq_start_stopped_hw_queues(dev->ctrl.admin_q, true);
  1038. return 0;
  1039. }
  1040. static int nvme_configure_admin_queue(struct nvme_dev *dev)
  1041. {
  1042. int result;
  1043. u32 aqa;
  1044. u64 cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
  1045. struct nvme_queue *nvmeq;
  1046. dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
  1047. NVME_CAP_NSSRC(cap) : 0;
  1048. if (dev->subsystem &&
  1049. (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
  1050. writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
  1051. result = nvme_disable_ctrl(&dev->ctrl, cap);
  1052. if (result < 0)
  1053. return result;
  1054. nvmeq = dev->queues[0];
  1055. if (!nvmeq) {
  1056. nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
  1057. if (!nvmeq)
  1058. return -ENOMEM;
  1059. }
  1060. aqa = nvmeq->q_depth - 1;
  1061. aqa |= aqa << 16;
  1062. writel(aqa, dev->bar + NVME_REG_AQA);
  1063. lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
  1064. lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
  1065. result = nvme_enable_ctrl(&dev->ctrl, cap);
  1066. if (result)
  1067. return result;
  1068. nvmeq->cq_vector = 0;
  1069. nvme_init_queue(nvmeq, 0);
  1070. result = queue_request_irq(nvmeq);
  1071. if (result) {
  1072. nvmeq->cq_vector = -1;
  1073. return result;
  1074. }
  1075. return result;
  1076. }
  1077. static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
  1078. {
  1079. /* If true, indicates loss of adapter communication, possibly by a
  1080. * NVMe Subsystem reset.
  1081. */
  1082. bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
  1083. /* If there is a reset ongoing, we shouldn't reset again. */
  1084. if (dev->ctrl.state == NVME_CTRL_RESETTING)
  1085. return false;
  1086. /* We shouldn't reset unless the controller is on fatal error state
  1087. * _or_ if we lost the communication with it.
  1088. */
  1089. if (!(csts & NVME_CSTS_CFS) && !nssro)
  1090. return false;
  1091. /* If PCI error recovery process is happening, we cannot reset or
  1092. * the recovery mechanism will surely fail.
  1093. */
  1094. if (pci_channel_offline(to_pci_dev(dev->dev)))
  1095. return false;
  1096. return true;
  1097. }
  1098. static void nvme_watchdog_timer(unsigned long data)
  1099. {
  1100. struct nvme_dev *dev = (struct nvme_dev *)data;
  1101. u32 csts = readl(dev->bar + NVME_REG_CSTS);
  1102. /* Skip controllers under certain specific conditions. */
  1103. if (nvme_should_reset(dev, csts)) {
  1104. if (!nvme_reset(dev))
  1105. dev_warn(dev->dev,
  1106. "Failed status: 0x%x, reset controller.\n",
  1107. csts);
  1108. return;
  1109. }
  1110. mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
  1111. }
  1112. static int nvme_create_io_queues(struct nvme_dev *dev)
  1113. {
  1114. unsigned i, max;
  1115. int ret = 0;
  1116. for (i = dev->queue_count; i <= dev->max_qid; i++) {
  1117. if (!nvme_alloc_queue(dev, i, dev->q_depth)) {
  1118. ret = -ENOMEM;
  1119. break;
  1120. }
  1121. }
  1122. max = min(dev->max_qid, dev->queue_count - 1);
  1123. for (i = dev->online_queues; i <= max; i++) {
  1124. ret = nvme_create_queue(dev->queues[i], i);
  1125. if (ret)
  1126. break;
  1127. }
  1128. /*
  1129. * Ignore failing Create SQ/CQ commands, we can continue with less
  1130. * than the desired aount of queues, and even a controller without
  1131. * I/O queues an still be used to issue admin commands. This might
  1132. * be useful to upgrade a buggy firmware for example.
  1133. */
  1134. return ret >= 0 ? 0 : ret;
  1135. }
  1136. static ssize_t nvme_cmb_show(struct device *dev,
  1137. struct device_attribute *attr,
  1138. char *buf)
  1139. {
  1140. struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
  1141. return snprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
  1142. ndev->cmbloc, ndev->cmbsz);
  1143. }
  1144. static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
  1145. static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
  1146. {
  1147. u64 szu, size, offset;
  1148. resource_size_t bar_size;
  1149. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1150. void __iomem *cmb;
  1151. int bar;
  1152. dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
  1153. if (!(NVME_CMB_SZ(dev->cmbsz)))
  1154. return NULL;
  1155. dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
  1156. if (!use_cmb_sqes)
  1157. return NULL;
  1158. szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
  1159. size = szu * NVME_CMB_SZ(dev->cmbsz);
  1160. offset = szu * NVME_CMB_OFST(dev->cmbloc);
  1161. bar = NVME_CMB_BIR(dev->cmbloc);
  1162. bar_size = pci_resource_len(pdev, bar);
  1163. if (offset > bar_size)
  1164. return NULL;
  1165. /*
  1166. * Controllers may support a CMB size larger than their BAR,
  1167. * for example, due to being behind a bridge. Reduce the CMB to
  1168. * the reported size of the BAR
  1169. */
  1170. if (size > bar_size - offset)
  1171. size = bar_size - offset;
  1172. cmb = ioremap_wc(pci_resource_start(pdev, bar) + offset, size);
  1173. if (!cmb)
  1174. return NULL;
  1175. dev->cmb_bus_addr = pci_bus_address(pdev, bar) + offset;
  1176. dev->cmb_size = size;
  1177. return cmb;
  1178. }
  1179. static inline void nvme_release_cmb(struct nvme_dev *dev)
  1180. {
  1181. if (dev->cmb) {
  1182. iounmap(dev->cmb);
  1183. dev->cmb = NULL;
  1184. if (dev->cmbsz) {
  1185. sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
  1186. &dev_attr_cmb.attr, NULL);
  1187. dev->cmbsz = 0;
  1188. }
  1189. }
  1190. }
  1191. static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
  1192. {
  1193. return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
  1194. }
  1195. static int nvme_setup_io_queues(struct nvme_dev *dev)
  1196. {
  1197. struct nvme_queue *adminq = dev->queues[0];
  1198. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1199. int result, nr_io_queues, size;
  1200. nr_io_queues = num_online_cpus();
  1201. result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
  1202. if (result < 0)
  1203. return result;
  1204. if (nr_io_queues == 0)
  1205. return 0;
  1206. if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
  1207. result = nvme_cmb_qdepth(dev, nr_io_queues,
  1208. sizeof(struct nvme_command));
  1209. if (result > 0)
  1210. dev->q_depth = result;
  1211. else
  1212. nvme_release_cmb(dev);
  1213. }
  1214. size = db_bar_size(dev, nr_io_queues);
  1215. if (size > 8192) {
  1216. iounmap(dev->bar);
  1217. do {
  1218. dev->bar = ioremap(pci_resource_start(pdev, 0), size);
  1219. if (dev->bar)
  1220. break;
  1221. if (!--nr_io_queues)
  1222. return -ENOMEM;
  1223. size = db_bar_size(dev, nr_io_queues);
  1224. } while (1);
  1225. dev->dbs = dev->bar + 4096;
  1226. adminq->q_db = dev->dbs;
  1227. }
  1228. /* Deregister the admin queue's interrupt */
  1229. free_irq(pci_irq_vector(pdev, 0), adminq);
  1230. /*
  1231. * If we enable msix early due to not intx, disable it again before
  1232. * setting up the full range we need.
  1233. */
  1234. pci_free_irq_vectors(pdev);
  1235. nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
  1236. PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
  1237. if (nr_io_queues <= 0)
  1238. return -EIO;
  1239. dev->max_qid = nr_io_queues;
  1240. /*
  1241. * Should investigate if there's a performance win from allocating
  1242. * more queues than interrupt vectors; it might allow the submission
  1243. * path to scale better, even if the receive path is limited by the
  1244. * number of interrupts.
  1245. */
  1246. result = queue_request_irq(adminq);
  1247. if (result) {
  1248. adminq->cq_vector = -1;
  1249. return result;
  1250. }
  1251. return nvme_create_io_queues(dev);
  1252. }
  1253. static void nvme_del_queue_end(struct request *req, int error)
  1254. {
  1255. struct nvme_queue *nvmeq = req->end_io_data;
  1256. blk_mq_free_request(req);
  1257. complete(&nvmeq->dev->ioq_wait);
  1258. }
  1259. static void nvme_del_cq_end(struct request *req, int error)
  1260. {
  1261. struct nvme_queue *nvmeq = req->end_io_data;
  1262. if (!error) {
  1263. unsigned long flags;
  1264. /*
  1265. * We might be called with the AQ q_lock held
  1266. * and the I/O queue q_lock should always
  1267. * nest inside the AQ one.
  1268. */
  1269. spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
  1270. SINGLE_DEPTH_NESTING);
  1271. nvme_process_cq(nvmeq);
  1272. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  1273. }
  1274. nvme_del_queue_end(req, error);
  1275. }
  1276. static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
  1277. {
  1278. struct request_queue *q = nvmeq->dev->ctrl.admin_q;
  1279. struct request *req;
  1280. struct nvme_command cmd;
  1281. memset(&cmd, 0, sizeof(cmd));
  1282. cmd.delete_queue.opcode = opcode;
  1283. cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
  1284. req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
  1285. if (IS_ERR(req))
  1286. return PTR_ERR(req);
  1287. req->timeout = ADMIN_TIMEOUT;
  1288. req->end_io_data = nvmeq;
  1289. blk_execute_rq_nowait(q, NULL, req, false,
  1290. opcode == nvme_admin_delete_cq ?
  1291. nvme_del_cq_end : nvme_del_queue_end);
  1292. return 0;
  1293. }
  1294. static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
  1295. {
  1296. int pass;
  1297. unsigned long timeout;
  1298. u8 opcode = nvme_admin_delete_sq;
  1299. for (pass = 0; pass < 2; pass++) {
  1300. int sent = 0, i = queues;
  1301. reinit_completion(&dev->ioq_wait);
  1302. retry:
  1303. timeout = ADMIN_TIMEOUT;
  1304. for (; i > 0; i--, sent++)
  1305. if (nvme_delete_queue(dev->queues[i], opcode))
  1306. break;
  1307. while (sent--) {
  1308. timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
  1309. if (timeout == 0)
  1310. return;
  1311. if (i)
  1312. goto retry;
  1313. }
  1314. opcode = nvme_admin_delete_cq;
  1315. }
  1316. }
  1317. /*
  1318. * Return: error value if an error occurred setting up the queues or calling
  1319. * Identify Device. 0 if these succeeded, even if adding some of the
  1320. * namespaces failed. At the moment, these failures are silent. TBD which
  1321. * failures should be reported.
  1322. */
  1323. static int nvme_dev_add(struct nvme_dev *dev)
  1324. {
  1325. if (!dev->ctrl.tagset) {
  1326. dev->tagset.ops = &nvme_mq_ops;
  1327. dev->tagset.nr_hw_queues = dev->online_queues - 1;
  1328. dev->tagset.timeout = NVME_IO_TIMEOUT;
  1329. dev->tagset.numa_node = dev_to_node(dev->dev);
  1330. dev->tagset.queue_depth =
  1331. min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
  1332. dev->tagset.cmd_size = nvme_cmd_size(dev);
  1333. dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
  1334. dev->tagset.driver_data = dev;
  1335. if (blk_mq_alloc_tag_set(&dev->tagset))
  1336. return 0;
  1337. dev->ctrl.tagset = &dev->tagset;
  1338. } else {
  1339. blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
  1340. /* Free previously allocated queues that are no longer usable */
  1341. nvme_free_queues(dev, dev->online_queues);
  1342. }
  1343. return 0;
  1344. }
  1345. static int nvme_pci_enable(struct nvme_dev *dev)
  1346. {
  1347. u64 cap;
  1348. int result = -ENOMEM;
  1349. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1350. if (pci_enable_device_mem(pdev))
  1351. return result;
  1352. pci_set_master(pdev);
  1353. if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
  1354. dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
  1355. goto disable;
  1356. if (readl(dev->bar + NVME_REG_CSTS) == -1) {
  1357. result = -ENODEV;
  1358. goto disable;
  1359. }
  1360. /*
  1361. * Some devices and/or platforms don't advertise or work with INTx
  1362. * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
  1363. * adjust this later.
  1364. */
  1365. result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  1366. if (result < 0)
  1367. return result;
  1368. cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
  1369. dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
  1370. dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
  1371. dev->dbs = dev->bar + 4096;
  1372. /*
  1373. * Temporary fix for the Apple controller found in the MacBook8,1 and
  1374. * some MacBook7,1 to avoid controller resets and data loss.
  1375. */
  1376. if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
  1377. dev->q_depth = 2;
  1378. dev_warn(dev->dev, "detected Apple NVMe controller, set "
  1379. "queue depth=%u to work around controller resets\n",
  1380. dev->q_depth);
  1381. }
  1382. /*
  1383. * CMBs can currently only exist on >=1.2 PCIe devices. We only
  1384. * populate sysfs if a CMB is implemented. Note that we add the
  1385. * CMB attribute to the nvme_ctrl kobj which removes the need to remove
  1386. * it on exit. Since nvme_dev_attrs_group has no name we can pass
  1387. * NULL as final argument to sysfs_add_file_to_group.
  1388. */
  1389. if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
  1390. dev->cmb = nvme_map_cmb(dev);
  1391. if (dev->cmbsz) {
  1392. if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
  1393. &dev_attr_cmb.attr, NULL))
  1394. dev_warn(dev->dev,
  1395. "failed to add sysfs attribute for CMB\n");
  1396. }
  1397. }
  1398. pci_enable_pcie_error_reporting(pdev);
  1399. pci_save_state(pdev);
  1400. return 0;
  1401. disable:
  1402. pci_disable_device(pdev);
  1403. return result;
  1404. }
  1405. static void nvme_dev_unmap(struct nvme_dev *dev)
  1406. {
  1407. if (dev->bar)
  1408. iounmap(dev->bar);
  1409. pci_release_mem_regions(to_pci_dev(dev->dev));
  1410. }
  1411. static void nvme_pci_disable(struct nvme_dev *dev)
  1412. {
  1413. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1414. nvme_release_cmb(dev);
  1415. pci_free_irq_vectors(pdev);
  1416. if (pci_is_enabled(pdev)) {
  1417. pci_disable_pcie_error_reporting(pdev);
  1418. pci_disable_device(pdev);
  1419. }
  1420. }
  1421. static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
  1422. {
  1423. int i, queues;
  1424. u32 csts = -1;
  1425. del_timer_sync(&dev->watchdog_timer);
  1426. mutex_lock(&dev->shutdown_lock);
  1427. if (pci_is_enabled(to_pci_dev(dev->dev))) {
  1428. nvme_stop_queues(&dev->ctrl);
  1429. csts = readl(dev->bar + NVME_REG_CSTS);
  1430. }
  1431. queues = dev->online_queues - 1;
  1432. for (i = dev->queue_count - 1; i > 0; i--)
  1433. nvme_suspend_queue(dev->queues[i]);
  1434. if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
  1435. /* A device might become IO incapable very soon during
  1436. * probe, before the admin queue is configured. Thus,
  1437. * queue_count can be 0 here.
  1438. */
  1439. if (dev->queue_count)
  1440. nvme_suspend_queue(dev->queues[0]);
  1441. } else {
  1442. nvme_disable_io_queues(dev, queues);
  1443. nvme_disable_admin_queue(dev, shutdown);
  1444. }
  1445. nvme_pci_disable(dev);
  1446. blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
  1447. blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
  1448. mutex_unlock(&dev->shutdown_lock);
  1449. }
  1450. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1451. {
  1452. dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
  1453. PAGE_SIZE, PAGE_SIZE, 0);
  1454. if (!dev->prp_page_pool)
  1455. return -ENOMEM;
  1456. /* Optimisation for I/Os between 4k and 128k */
  1457. dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
  1458. 256, 256, 0);
  1459. if (!dev->prp_small_pool) {
  1460. dma_pool_destroy(dev->prp_page_pool);
  1461. return -ENOMEM;
  1462. }
  1463. return 0;
  1464. }
  1465. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1466. {
  1467. dma_pool_destroy(dev->prp_page_pool);
  1468. dma_pool_destroy(dev->prp_small_pool);
  1469. }
  1470. static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
  1471. {
  1472. struct nvme_dev *dev = to_nvme_dev(ctrl);
  1473. put_device(dev->dev);
  1474. if (dev->tagset.tags)
  1475. blk_mq_free_tag_set(&dev->tagset);
  1476. if (dev->ctrl.admin_q)
  1477. blk_put_queue(dev->ctrl.admin_q);
  1478. kfree(dev->queues);
  1479. kfree(dev);
  1480. }
  1481. static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
  1482. {
  1483. dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
  1484. kref_get(&dev->ctrl.kref);
  1485. nvme_dev_disable(dev, false);
  1486. if (!schedule_work(&dev->remove_work))
  1487. nvme_put_ctrl(&dev->ctrl);
  1488. }
  1489. static void nvme_reset_work(struct work_struct *work)
  1490. {
  1491. struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
  1492. int result = -ENODEV;
  1493. if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
  1494. goto out;
  1495. /*
  1496. * If we're called to reset a live controller first shut it down before
  1497. * moving on.
  1498. */
  1499. if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
  1500. nvme_dev_disable(dev, false);
  1501. result = nvme_pci_enable(dev);
  1502. if (result)
  1503. goto out;
  1504. result = nvme_configure_admin_queue(dev);
  1505. if (result)
  1506. goto out;
  1507. result = nvme_alloc_admin_tags(dev);
  1508. if (result)
  1509. goto out;
  1510. result = nvme_init_identify(&dev->ctrl);
  1511. if (result)
  1512. goto out;
  1513. result = nvme_setup_io_queues(dev);
  1514. if (result)
  1515. goto out;
  1516. /*
  1517. * A controller that can not execute IO typically requires user
  1518. * intervention to correct. For such degraded controllers, the driver
  1519. * should not submit commands the user did not request, so skip
  1520. * registering for asynchronous event notification on this condition.
  1521. */
  1522. if (dev->online_queues > 1)
  1523. nvme_queue_async_events(&dev->ctrl);
  1524. mod_timer(&dev->watchdog_timer, round_jiffies(jiffies + HZ));
  1525. /*
  1526. * Keep the controller around but remove all namespaces if we don't have
  1527. * any working I/O queue.
  1528. */
  1529. if (dev->online_queues < 2) {
  1530. dev_warn(dev->ctrl.device, "IO queues not created\n");
  1531. nvme_kill_queues(&dev->ctrl);
  1532. nvme_remove_namespaces(&dev->ctrl);
  1533. } else {
  1534. nvme_start_queues(&dev->ctrl);
  1535. nvme_dev_add(dev);
  1536. }
  1537. if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
  1538. dev_warn(dev->ctrl.device, "failed to mark controller live\n");
  1539. goto out;
  1540. }
  1541. if (dev->online_queues > 1)
  1542. nvme_queue_scan(&dev->ctrl);
  1543. return;
  1544. out:
  1545. nvme_remove_dead_ctrl(dev, result);
  1546. }
  1547. static void nvme_remove_dead_ctrl_work(struct work_struct *work)
  1548. {
  1549. struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
  1550. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1551. nvme_kill_queues(&dev->ctrl);
  1552. if (pci_get_drvdata(pdev))
  1553. device_release_driver(&pdev->dev);
  1554. nvme_put_ctrl(&dev->ctrl);
  1555. }
  1556. static int nvme_reset(struct nvme_dev *dev)
  1557. {
  1558. if (!dev->ctrl.admin_q || blk_queue_dying(dev->ctrl.admin_q))
  1559. return -ENODEV;
  1560. if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING))
  1561. return -EBUSY;
  1562. if (!queue_work(nvme_workq, &dev->reset_work))
  1563. return -EBUSY;
  1564. return 0;
  1565. }
  1566. static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
  1567. {
  1568. *val = readl(to_nvme_dev(ctrl)->bar + off);
  1569. return 0;
  1570. }
  1571. static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
  1572. {
  1573. writel(val, to_nvme_dev(ctrl)->bar + off);
  1574. return 0;
  1575. }
  1576. static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
  1577. {
  1578. *val = readq(to_nvme_dev(ctrl)->bar + off);
  1579. return 0;
  1580. }
  1581. static int nvme_pci_reset_ctrl(struct nvme_ctrl *ctrl)
  1582. {
  1583. struct nvme_dev *dev = to_nvme_dev(ctrl);
  1584. int ret = nvme_reset(dev);
  1585. if (!ret)
  1586. flush_work(&dev->reset_work);
  1587. return ret;
  1588. }
  1589. static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
  1590. .name = "pcie",
  1591. .module = THIS_MODULE,
  1592. .reg_read32 = nvme_pci_reg_read32,
  1593. .reg_write32 = nvme_pci_reg_write32,
  1594. .reg_read64 = nvme_pci_reg_read64,
  1595. .reset_ctrl = nvme_pci_reset_ctrl,
  1596. .free_ctrl = nvme_pci_free_ctrl,
  1597. .submit_async_event = nvme_pci_submit_async_event,
  1598. };
  1599. static int nvme_dev_map(struct nvme_dev *dev)
  1600. {
  1601. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1602. if (pci_request_mem_regions(pdev, "nvme"))
  1603. return -ENODEV;
  1604. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  1605. if (!dev->bar)
  1606. goto release;
  1607. return 0;
  1608. release:
  1609. pci_release_mem_regions(pdev);
  1610. return -ENODEV;
  1611. }
  1612. static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1613. {
  1614. int node, result = -ENOMEM;
  1615. struct nvme_dev *dev;
  1616. node = dev_to_node(&pdev->dev);
  1617. if (node == NUMA_NO_NODE)
  1618. set_dev_node(&pdev->dev, first_memory_node);
  1619. dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
  1620. if (!dev)
  1621. return -ENOMEM;
  1622. dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
  1623. GFP_KERNEL, node);
  1624. if (!dev->queues)
  1625. goto free;
  1626. dev->dev = get_device(&pdev->dev);
  1627. pci_set_drvdata(pdev, dev);
  1628. result = nvme_dev_map(dev);
  1629. if (result)
  1630. goto free;
  1631. INIT_WORK(&dev->reset_work, nvme_reset_work);
  1632. INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
  1633. setup_timer(&dev->watchdog_timer, nvme_watchdog_timer,
  1634. (unsigned long)dev);
  1635. mutex_init(&dev->shutdown_lock);
  1636. init_completion(&dev->ioq_wait);
  1637. result = nvme_setup_prp_pools(dev);
  1638. if (result)
  1639. goto put_pci;
  1640. result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
  1641. id->driver_data);
  1642. if (result)
  1643. goto release_pools;
  1644. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
  1645. dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
  1646. queue_work(nvme_workq, &dev->reset_work);
  1647. return 0;
  1648. release_pools:
  1649. nvme_release_prp_pools(dev);
  1650. put_pci:
  1651. put_device(dev->dev);
  1652. nvme_dev_unmap(dev);
  1653. free:
  1654. kfree(dev->queues);
  1655. kfree(dev);
  1656. return result;
  1657. }
  1658. static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
  1659. {
  1660. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1661. if (prepare)
  1662. nvme_dev_disable(dev, false);
  1663. else
  1664. nvme_reset(dev);
  1665. }
  1666. static void nvme_shutdown(struct pci_dev *pdev)
  1667. {
  1668. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1669. nvme_dev_disable(dev, true);
  1670. }
  1671. /*
  1672. * The driver's remove may be called on a device in a partially initialized
  1673. * state. This function must not have any dependencies on the device state in
  1674. * order to proceed.
  1675. */
  1676. static void nvme_remove(struct pci_dev *pdev)
  1677. {
  1678. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1679. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
  1680. cancel_work_sync(&dev->reset_work);
  1681. pci_set_drvdata(pdev, NULL);
  1682. if (!pci_device_is_present(pdev)) {
  1683. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
  1684. nvme_dev_disable(dev, false);
  1685. }
  1686. flush_work(&dev->reset_work);
  1687. nvme_uninit_ctrl(&dev->ctrl);
  1688. nvme_dev_disable(dev, true);
  1689. nvme_dev_remove_admin(dev);
  1690. nvme_free_queues(dev, 0);
  1691. nvme_release_prp_pools(dev);
  1692. nvme_dev_unmap(dev);
  1693. nvme_put_ctrl(&dev->ctrl);
  1694. }
  1695. static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
  1696. {
  1697. int ret = 0;
  1698. if (numvfs == 0) {
  1699. if (pci_vfs_assigned(pdev)) {
  1700. dev_warn(&pdev->dev,
  1701. "Cannot disable SR-IOV VFs while assigned\n");
  1702. return -EPERM;
  1703. }
  1704. pci_disable_sriov(pdev);
  1705. return 0;
  1706. }
  1707. ret = pci_enable_sriov(pdev, numvfs);
  1708. return ret ? ret : numvfs;
  1709. }
  1710. #ifdef CONFIG_PM_SLEEP
  1711. static int nvme_suspend(struct device *dev)
  1712. {
  1713. struct pci_dev *pdev = to_pci_dev(dev);
  1714. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  1715. nvme_dev_disable(ndev, true);
  1716. return 0;
  1717. }
  1718. static int nvme_resume(struct device *dev)
  1719. {
  1720. struct pci_dev *pdev = to_pci_dev(dev);
  1721. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  1722. nvme_reset(ndev);
  1723. return 0;
  1724. }
  1725. #endif
  1726. static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  1727. static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
  1728. pci_channel_state_t state)
  1729. {
  1730. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1731. /*
  1732. * A frozen channel requires a reset. When detected, this method will
  1733. * shutdown the controller to quiesce. The controller will be restarted
  1734. * after the slot reset through driver's slot_reset callback.
  1735. */
  1736. switch (state) {
  1737. case pci_channel_io_normal:
  1738. return PCI_ERS_RESULT_CAN_RECOVER;
  1739. case pci_channel_io_frozen:
  1740. dev_warn(dev->ctrl.device,
  1741. "frozen state error detected, reset controller\n");
  1742. nvme_dev_disable(dev, false);
  1743. return PCI_ERS_RESULT_NEED_RESET;
  1744. case pci_channel_io_perm_failure:
  1745. dev_warn(dev->ctrl.device,
  1746. "failure state error detected, request disconnect\n");
  1747. return PCI_ERS_RESULT_DISCONNECT;
  1748. }
  1749. return PCI_ERS_RESULT_NEED_RESET;
  1750. }
  1751. static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
  1752. {
  1753. struct nvme_dev *dev = pci_get_drvdata(pdev);
  1754. dev_info(dev->ctrl.device, "restart after slot reset\n");
  1755. pci_restore_state(pdev);
  1756. nvme_reset(dev);
  1757. return PCI_ERS_RESULT_RECOVERED;
  1758. }
  1759. static void nvme_error_resume(struct pci_dev *pdev)
  1760. {
  1761. pci_cleanup_aer_uncorrect_error_status(pdev);
  1762. }
  1763. static const struct pci_error_handlers nvme_err_handler = {
  1764. .error_detected = nvme_error_detected,
  1765. .slot_reset = nvme_slot_reset,
  1766. .resume = nvme_error_resume,
  1767. .reset_notify = nvme_reset_notify,
  1768. };
  1769. /* Move to pci_ids.h later */
  1770. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  1771. static const struct pci_device_id nvme_id_table[] = {
  1772. { PCI_VDEVICE(INTEL, 0x0953),
  1773. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  1774. NVME_QUIRK_DISCARD_ZEROES, },
  1775. { PCI_VDEVICE(INTEL, 0x0a53),
  1776. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  1777. NVME_QUIRK_DISCARD_ZEROES, },
  1778. { PCI_VDEVICE(INTEL, 0x0a54),
  1779. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  1780. NVME_QUIRK_DISCARD_ZEROES, },
  1781. { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
  1782. .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
  1783. { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
  1784. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  1785. { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
  1786. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  1787. { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
  1788. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  1789. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  1790. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
  1791. { 0, }
  1792. };
  1793. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  1794. static struct pci_driver nvme_driver = {
  1795. .name = "nvme",
  1796. .id_table = nvme_id_table,
  1797. .probe = nvme_probe,
  1798. .remove = nvme_remove,
  1799. .shutdown = nvme_shutdown,
  1800. .driver = {
  1801. .pm = &nvme_dev_pm_ops,
  1802. },
  1803. .sriov_configure = nvme_pci_sriov_configure,
  1804. .err_handler = &nvme_err_handler,
  1805. };
  1806. static int __init nvme_init(void)
  1807. {
  1808. int result;
  1809. nvme_workq = alloc_workqueue("nvme", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
  1810. if (!nvme_workq)
  1811. return -ENOMEM;
  1812. result = pci_register_driver(&nvme_driver);
  1813. if (result)
  1814. destroy_workqueue(nvme_workq);
  1815. return result;
  1816. }
  1817. static void __exit nvme_exit(void)
  1818. {
  1819. pci_unregister_driver(&nvme_driver);
  1820. destroy_workqueue(nvme_workq);
  1821. _nvme_check_size();
  1822. }
  1823. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  1824. MODULE_LICENSE("GPL");
  1825. MODULE_VERSION("1.0");
  1826. module_init(nvme_init);
  1827. module_exit(nvme_exit);