pci.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628
  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/dmi.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/once.h>
  27. #include <linux/pci.h>
  28. #include <linux/poison.h>
  29. #include <linux/t10-pi.h>
  30. #include <linux/timer.h>
  31. #include <linux/types.h>
  32. #include <linux/io-64-nonatomic-lo-hi.h>
  33. #include <asm/unaligned.h>
  34. #include <linux/sed-opal.h>
  35. #include "nvme.h"
  36. #define SQ_SIZE(depth) (depth * sizeof(struct nvme_command))
  37. #define CQ_SIZE(depth) (depth * sizeof(struct nvme_completion))
  38. /*
  39. * We handle AEN commands ourselves and don't even let the
  40. * block layer know about them.
  41. */
  42. #define NVME_AQ_BLKMQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AERS)
  43. static int use_threaded_interrupts;
  44. module_param(use_threaded_interrupts, int, 0);
  45. static bool use_cmb_sqes = true;
  46. module_param(use_cmb_sqes, bool, 0644);
  47. MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
  48. static unsigned int max_host_mem_size_mb = 128;
  49. module_param(max_host_mem_size_mb, uint, 0444);
  50. MODULE_PARM_DESC(max_host_mem_size_mb,
  51. "Maximum Host Memory Buffer (HMB) size per controller (in MiB)");
  52. static int io_queue_depth_set(const char *val, const struct kernel_param *kp);
  53. static const struct kernel_param_ops io_queue_depth_ops = {
  54. .set = io_queue_depth_set,
  55. .get = param_get_int,
  56. };
  57. static int io_queue_depth = 1024;
  58. module_param_cb(io_queue_depth, &io_queue_depth_ops, &io_queue_depth, 0644);
  59. MODULE_PARM_DESC(io_queue_depth, "set io queue depth, should >= 2");
  60. struct nvme_dev;
  61. struct nvme_queue;
  62. static void nvme_process_cq(struct nvme_queue *nvmeq);
  63. static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
  64. /*
  65. * Represents an NVM Express device. Each nvme_dev is a PCI function.
  66. */
  67. struct nvme_dev {
  68. struct nvme_queue *queues;
  69. struct blk_mq_tag_set tagset;
  70. struct blk_mq_tag_set admin_tagset;
  71. u32 __iomem *dbs;
  72. struct device *dev;
  73. struct dma_pool *prp_page_pool;
  74. struct dma_pool *prp_small_pool;
  75. unsigned online_queues;
  76. unsigned max_qid;
  77. int q_depth;
  78. u32 db_stride;
  79. void __iomem *bar;
  80. unsigned long bar_mapped_size;
  81. struct work_struct remove_work;
  82. struct mutex shutdown_lock;
  83. bool subsystem;
  84. void __iomem *cmb;
  85. pci_bus_addr_t cmb_bus_addr;
  86. u64 cmb_size;
  87. u32 cmbsz;
  88. u32 cmbloc;
  89. struct nvme_ctrl ctrl;
  90. struct completion ioq_wait;
  91. /* shadow doorbell buffer support: */
  92. u32 *dbbuf_dbs;
  93. dma_addr_t dbbuf_dbs_dma_addr;
  94. u32 *dbbuf_eis;
  95. dma_addr_t dbbuf_eis_dma_addr;
  96. /* host memory buffer support: */
  97. u64 host_mem_size;
  98. u32 nr_host_mem_descs;
  99. dma_addr_t host_mem_descs_dma;
  100. struct nvme_host_mem_buf_desc *host_mem_descs;
  101. void **host_mem_desc_bufs;
  102. };
  103. static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
  104. {
  105. int n = 0, ret;
  106. ret = kstrtoint(val, 10, &n);
  107. if (ret != 0 || n < 2)
  108. return -EINVAL;
  109. return param_set_int(val, kp);
  110. }
  111. static inline unsigned int sq_idx(unsigned int qid, u32 stride)
  112. {
  113. return qid * 2 * stride;
  114. }
  115. static inline unsigned int cq_idx(unsigned int qid, u32 stride)
  116. {
  117. return (qid * 2 + 1) * stride;
  118. }
  119. static inline struct nvme_dev *to_nvme_dev(struct nvme_ctrl *ctrl)
  120. {
  121. return container_of(ctrl, struct nvme_dev, ctrl);
  122. }
  123. /*
  124. * An NVM Express queue. Each device has at least two (one for admin
  125. * commands and one for I/O commands).
  126. */
  127. struct nvme_queue {
  128. struct device *q_dmadev;
  129. struct nvme_dev *dev;
  130. spinlock_t q_lock;
  131. struct nvme_command *sq_cmds;
  132. struct nvme_command __iomem *sq_cmds_io;
  133. volatile struct nvme_completion *cqes;
  134. struct blk_mq_tags **tags;
  135. dma_addr_t sq_dma_addr;
  136. dma_addr_t cq_dma_addr;
  137. u32 __iomem *q_db;
  138. u16 q_depth;
  139. s16 cq_vector;
  140. u16 sq_tail;
  141. u16 cq_head;
  142. u16 qid;
  143. u8 cq_phase;
  144. u8 cqe_seen;
  145. u32 *dbbuf_sq_db;
  146. u32 *dbbuf_cq_db;
  147. u32 *dbbuf_sq_ei;
  148. u32 *dbbuf_cq_ei;
  149. };
  150. /*
  151. * The nvme_iod describes the data in an I/O, including the list of PRP
  152. * entries. You can't see it in this data structure because C doesn't let
  153. * me express that. Use nvme_init_iod to ensure there's enough space
  154. * allocated to store the PRP list.
  155. */
  156. struct nvme_iod {
  157. struct nvme_request req;
  158. struct nvme_queue *nvmeq;
  159. int aborted;
  160. int npages; /* In the PRP list. 0 means small pool in use */
  161. int nents; /* Used in scatterlist */
  162. int length; /* Of data, in bytes */
  163. dma_addr_t first_dma;
  164. struct scatterlist meta_sg; /* metadata requires single contiguous buffer */
  165. struct scatterlist *sg;
  166. struct scatterlist inline_sg[0];
  167. };
  168. /*
  169. * Check we didin't inadvertently grow the command struct
  170. */
  171. static inline void _nvme_check_size(void)
  172. {
  173. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  174. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  175. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  176. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  177. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  178. BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
  179. BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
  180. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  181. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != NVME_IDENTIFY_DATA_SIZE);
  182. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != NVME_IDENTIFY_DATA_SIZE);
  183. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  184. BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
  185. BUILD_BUG_ON(sizeof(struct nvme_dbbuf) != 64);
  186. }
  187. static inline unsigned int nvme_dbbuf_size(u32 stride)
  188. {
  189. return ((num_possible_cpus() + 1) * 8 * stride);
  190. }
  191. static int nvme_dbbuf_dma_alloc(struct nvme_dev *dev)
  192. {
  193. unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
  194. if (dev->dbbuf_dbs)
  195. return 0;
  196. dev->dbbuf_dbs = dma_alloc_coherent(dev->dev, mem_size,
  197. &dev->dbbuf_dbs_dma_addr,
  198. GFP_KERNEL);
  199. if (!dev->dbbuf_dbs)
  200. return -ENOMEM;
  201. dev->dbbuf_eis = dma_alloc_coherent(dev->dev, mem_size,
  202. &dev->dbbuf_eis_dma_addr,
  203. GFP_KERNEL);
  204. if (!dev->dbbuf_eis) {
  205. dma_free_coherent(dev->dev, mem_size,
  206. dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
  207. dev->dbbuf_dbs = NULL;
  208. return -ENOMEM;
  209. }
  210. return 0;
  211. }
  212. static void nvme_dbbuf_dma_free(struct nvme_dev *dev)
  213. {
  214. unsigned int mem_size = nvme_dbbuf_size(dev->db_stride);
  215. if (dev->dbbuf_dbs) {
  216. dma_free_coherent(dev->dev, mem_size,
  217. dev->dbbuf_dbs, dev->dbbuf_dbs_dma_addr);
  218. dev->dbbuf_dbs = NULL;
  219. }
  220. if (dev->dbbuf_eis) {
  221. dma_free_coherent(dev->dev, mem_size,
  222. dev->dbbuf_eis, dev->dbbuf_eis_dma_addr);
  223. dev->dbbuf_eis = NULL;
  224. }
  225. }
  226. static void nvme_dbbuf_init(struct nvme_dev *dev,
  227. struct nvme_queue *nvmeq, int qid)
  228. {
  229. if (!dev->dbbuf_dbs || !qid)
  230. return;
  231. nvmeq->dbbuf_sq_db = &dev->dbbuf_dbs[sq_idx(qid, dev->db_stride)];
  232. nvmeq->dbbuf_cq_db = &dev->dbbuf_dbs[cq_idx(qid, dev->db_stride)];
  233. nvmeq->dbbuf_sq_ei = &dev->dbbuf_eis[sq_idx(qid, dev->db_stride)];
  234. nvmeq->dbbuf_cq_ei = &dev->dbbuf_eis[cq_idx(qid, dev->db_stride)];
  235. }
  236. static void nvme_dbbuf_free(struct nvme_queue *nvmeq)
  237. {
  238. if (!nvmeq->qid)
  239. return;
  240. nvmeq->dbbuf_sq_db = NULL;
  241. nvmeq->dbbuf_cq_db = NULL;
  242. nvmeq->dbbuf_sq_ei = NULL;
  243. nvmeq->dbbuf_cq_ei = NULL;
  244. }
  245. static void nvme_dbbuf_set(struct nvme_dev *dev)
  246. {
  247. struct nvme_command c;
  248. unsigned int i;
  249. if (!dev->dbbuf_dbs)
  250. return;
  251. memset(&c, 0, sizeof(c));
  252. c.dbbuf.opcode = nvme_admin_dbbuf;
  253. c.dbbuf.prp1 = cpu_to_le64(dev->dbbuf_dbs_dma_addr);
  254. c.dbbuf.prp2 = cpu_to_le64(dev->dbbuf_eis_dma_addr);
  255. if (nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0)) {
  256. dev_warn(dev->ctrl.device, "unable to set dbbuf\n");
  257. /* Free memory and continue on */
  258. nvme_dbbuf_dma_free(dev);
  259. for (i = 1; i <= dev->online_queues; i++)
  260. nvme_dbbuf_free(&dev->queues[i]);
  261. }
  262. }
  263. static inline int nvme_dbbuf_need_event(u16 event_idx, u16 new_idx, u16 old)
  264. {
  265. return (u16)(new_idx - event_idx - 1) < (u16)(new_idx - old);
  266. }
  267. /* Update dbbuf and return true if an MMIO is required */
  268. static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
  269. volatile u32 *dbbuf_ei)
  270. {
  271. if (dbbuf_db) {
  272. u16 old_value;
  273. /*
  274. * Ensure that the queue is written before updating
  275. * the doorbell in memory
  276. */
  277. wmb();
  278. old_value = *dbbuf_db;
  279. *dbbuf_db = value;
  280. /*
  281. * Ensure that the doorbell is updated before reading the event
  282. * index from memory. The controller needs to provide similar
  283. * ordering to ensure the envent index is updated before reading
  284. * the doorbell.
  285. */
  286. mb();
  287. if (!nvme_dbbuf_need_event(*dbbuf_ei, value, old_value))
  288. return false;
  289. }
  290. return true;
  291. }
  292. /*
  293. * Max size of iod being embedded in the request payload
  294. */
  295. #define NVME_INT_PAGES 2
  296. #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->ctrl.page_size)
  297. /*
  298. * Will slightly overestimate the number of pages needed. This is OK
  299. * as it only leads to a small amount of wasted memory for the lifetime of
  300. * the I/O.
  301. */
  302. static int nvme_npages(unsigned size, struct nvme_dev *dev)
  303. {
  304. unsigned nprps = DIV_ROUND_UP(size + dev->ctrl.page_size,
  305. dev->ctrl.page_size);
  306. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  307. }
  308. static unsigned int nvme_iod_alloc_size(struct nvme_dev *dev,
  309. unsigned int size, unsigned int nseg)
  310. {
  311. return sizeof(__le64 *) * nvme_npages(size, dev) +
  312. sizeof(struct scatterlist) * nseg;
  313. }
  314. static unsigned int nvme_cmd_size(struct nvme_dev *dev)
  315. {
  316. return sizeof(struct nvme_iod) +
  317. nvme_iod_alloc_size(dev, NVME_INT_BYTES(dev), NVME_INT_PAGES);
  318. }
  319. static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  320. unsigned int hctx_idx)
  321. {
  322. struct nvme_dev *dev = data;
  323. struct nvme_queue *nvmeq = &dev->queues[0];
  324. WARN_ON(hctx_idx != 0);
  325. WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
  326. WARN_ON(nvmeq->tags);
  327. hctx->driver_data = nvmeq;
  328. nvmeq->tags = &dev->admin_tagset.tags[0];
  329. return 0;
  330. }
  331. static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  332. {
  333. struct nvme_queue *nvmeq = hctx->driver_data;
  334. nvmeq->tags = NULL;
  335. }
  336. static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  337. unsigned int hctx_idx)
  338. {
  339. struct nvme_dev *dev = data;
  340. struct nvme_queue *nvmeq = &dev->queues[hctx_idx + 1];
  341. if (!nvmeq->tags)
  342. nvmeq->tags = &dev->tagset.tags[hctx_idx];
  343. WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
  344. hctx->driver_data = nvmeq;
  345. return 0;
  346. }
  347. static int nvme_init_request(struct blk_mq_tag_set *set, struct request *req,
  348. unsigned int hctx_idx, unsigned int numa_node)
  349. {
  350. struct nvme_dev *dev = set->driver_data;
  351. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  352. int queue_idx = (set == &dev->tagset) ? hctx_idx + 1 : 0;
  353. struct nvme_queue *nvmeq = &dev->queues[queue_idx];
  354. BUG_ON(!nvmeq);
  355. iod->nvmeq = nvmeq;
  356. return 0;
  357. }
  358. static int nvme_pci_map_queues(struct blk_mq_tag_set *set)
  359. {
  360. struct nvme_dev *dev = set->driver_data;
  361. return blk_mq_pci_map_queues(set, to_pci_dev(dev->dev));
  362. }
  363. /**
  364. * __nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  365. * @nvmeq: The queue to use
  366. * @cmd: The command to send
  367. *
  368. * Safe to use from interrupt context
  369. */
  370. static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
  371. struct nvme_command *cmd)
  372. {
  373. u16 tail = nvmeq->sq_tail;
  374. if (nvmeq->sq_cmds_io)
  375. memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
  376. else
  377. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  378. if (++tail == nvmeq->q_depth)
  379. tail = 0;
  380. if (nvme_dbbuf_update_and_check_event(tail, nvmeq->dbbuf_sq_db,
  381. nvmeq->dbbuf_sq_ei))
  382. writel(tail, nvmeq->q_db);
  383. nvmeq->sq_tail = tail;
  384. }
  385. static __le64 **iod_list(struct request *req)
  386. {
  387. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  388. return (__le64 **)(iod->sg + blk_rq_nr_phys_segments(req));
  389. }
  390. static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
  391. {
  392. struct nvme_iod *iod = blk_mq_rq_to_pdu(rq);
  393. int nseg = blk_rq_nr_phys_segments(rq);
  394. unsigned int size = blk_rq_payload_bytes(rq);
  395. if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
  396. iod->sg = kmalloc(nvme_iod_alloc_size(dev, size, nseg), GFP_ATOMIC);
  397. if (!iod->sg)
  398. return BLK_STS_RESOURCE;
  399. } else {
  400. iod->sg = iod->inline_sg;
  401. }
  402. iod->aborted = 0;
  403. iod->npages = -1;
  404. iod->nents = 0;
  405. iod->length = size;
  406. return BLK_STS_OK;
  407. }
  408. static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
  409. {
  410. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  411. const int last_prp = dev->ctrl.page_size / 8 - 1;
  412. int i;
  413. __le64 **list = iod_list(req);
  414. dma_addr_t prp_dma = iod->first_dma;
  415. if (iod->npages == 0)
  416. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  417. for (i = 0; i < iod->npages; i++) {
  418. __le64 *prp_list = list[i];
  419. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  420. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  421. prp_dma = next_prp_dma;
  422. }
  423. if (iod->sg != iod->inline_sg)
  424. kfree(iod->sg);
  425. }
  426. #ifdef CONFIG_BLK_DEV_INTEGRITY
  427. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  428. {
  429. if (be32_to_cpu(pi->ref_tag) == v)
  430. pi->ref_tag = cpu_to_be32(p);
  431. }
  432. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  433. {
  434. if (be32_to_cpu(pi->ref_tag) == p)
  435. pi->ref_tag = cpu_to_be32(v);
  436. }
  437. /**
  438. * nvme_dif_remap - remaps ref tags to bip seed and physical lba
  439. *
  440. * The virtual start sector is the one that was originally submitted by the
  441. * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
  442. * start sector may be different. Remap protection information to match the
  443. * physical LBA on writes, and back to the original seed on reads.
  444. *
  445. * Type 0 and 3 do not have a ref tag, so no remapping required.
  446. */
  447. static void nvme_dif_remap(struct request *req,
  448. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  449. {
  450. struct nvme_ns *ns = req->rq_disk->private_data;
  451. struct bio_integrity_payload *bip;
  452. struct t10_pi_tuple *pi;
  453. void *p, *pmap;
  454. u32 i, nlb, ts, phys, virt;
  455. if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
  456. return;
  457. bip = bio_integrity(req->bio);
  458. if (!bip)
  459. return;
  460. pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
  461. p = pmap;
  462. virt = bip_get_seed(bip);
  463. phys = nvme_block_nr(ns, blk_rq_pos(req));
  464. nlb = (blk_rq_bytes(req) >> ns->lba_shift);
  465. ts = ns->disk->queue->integrity.tuple_size;
  466. for (i = 0; i < nlb; i++, virt++, phys++) {
  467. pi = (struct t10_pi_tuple *)p;
  468. dif_swap(phys, virt, pi);
  469. p += ts;
  470. }
  471. kunmap_atomic(pmap);
  472. }
  473. #else /* CONFIG_BLK_DEV_INTEGRITY */
  474. static void nvme_dif_remap(struct request *req,
  475. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  476. {
  477. }
  478. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  479. {
  480. }
  481. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  482. {
  483. }
  484. #endif
  485. static void nvme_print_sgl(struct scatterlist *sgl, int nents)
  486. {
  487. int i;
  488. struct scatterlist *sg;
  489. for_each_sg(sgl, sg, nents, i) {
  490. dma_addr_t phys = sg_phys(sg);
  491. pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d "
  492. "dma_address:%pad dma_length:%d\n",
  493. i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
  494. sg_dma_len(sg));
  495. }
  496. }
  497. static blk_status_t nvme_setup_prps(struct nvme_dev *dev, struct request *req)
  498. {
  499. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  500. struct dma_pool *pool;
  501. int length = blk_rq_payload_bytes(req);
  502. struct scatterlist *sg = iod->sg;
  503. int dma_len = sg_dma_len(sg);
  504. u64 dma_addr = sg_dma_address(sg);
  505. u32 page_size = dev->ctrl.page_size;
  506. int offset = dma_addr & (page_size - 1);
  507. __le64 *prp_list;
  508. __le64 **list = iod_list(req);
  509. dma_addr_t prp_dma;
  510. int nprps, i;
  511. length -= (page_size - offset);
  512. if (length <= 0) {
  513. iod->first_dma = 0;
  514. return BLK_STS_OK;
  515. }
  516. dma_len -= (page_size - offset);
  517. if (dma_len) {
  518. dma_addr += (page_size - offset);
  519. } else {
  520. sg = sg_next(sg);
  521. dma_addr = sg_dma_address(sg);
  522. dma_len = sg_dma_len(sg);
  523. }
  524. if (length <= page_size) {
  525. iod->first_dma = dma_addr;
  526. return BLK_STS_OK;
  527. }
  528. nprps = DIV_ROUND_UP(length, page_size);
  529. if (nprps <= (256 / 8)) {
  530. pool = dev->prp_small_pool;
  531. iod->npages = 0;
  532. } else {
  533. pool = dev->prp_page_pool;
  534. iod->npages = 1;
  535. }
  536. prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
  537. if (!prp_list) {
  538. iod->first_dma = dma_addr;
  539. iod->npages = -1;
  540. return BLK_STS_RESOURCE;
  541. }
  542. list[0] = prp_list;
  543. iod->first_dma = prp_dma;
  544. i = 0;
  545. for (;;) {
  546. if (i == page_size >> 3) {
  547. __le64 *old_prp_list = prp_list;
  548. prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
  549. if (!prp_list)
  550. return BLK_STS_RESOURCE;
  551. list[iod->npages++] = prp_list;
  552. prp_list[0] = old_prp_list[i - 1];
  553. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  554. i = 1;
  555. }
  556. prp_list[i++] = cpu_to_le64(dma_addr);
  557. dma_len -= page_size;
  558. dma_addr += page_size;
  559. length -= page_size;
  560. if (length <= 0)
  561. break;
  562. if (dma_len > 0)
  563. continue;
  564. if (unlikely(dma_len < 0))
  565. goto bad_sgl;
  566. sg = sg_next(sg);
  567. dma_addr = sg_dma_address(sg);
  568. dma_len = sg_dma_len(sg);
  569. }
  570. return BLK_STS_OK;
  571. bad_sgl:
  572. WARN(DO_ONCE(nvme_print_sgl, iod->sg, iod->nents),
  573. "Invalid SGL for payload:%d nents:%d\n",
  574. blk_rq_payload_bytes(req), iod->nents);
  575. return BLK_STS_IOERR;
  576. }
  577. static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,
  578. struct nvme_command *cmnd)
  579. {
  580. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  581. struct request_queue *q = req->q;
  582. enum dma_data_direction dma_dir = rq_data_dir(req) ?
  583. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  584. blk_status_t ret = BLK_STS_IOERR;
  585. sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
  586. iod->nents = blk_rq_map_sg(q, req, iod->sg);
  587. if (!iod->nents)
  588. goto out;
  589. ret = BLK_STS_RESOURCE;
  590. if (!dma_map_sg_attrs(dev->dev, iod->sg, iod->nents, dma_dir,
  591. DMA_ATTR_NO_WARN))
  592. goto out;
  593. ret = nvme_setup_prps(dev, req);
  594. if (ret != BLK_STS_OK)
  595. goto out_unmap;
  596. ret = BLK_STS_IOERR;
  597. if (blk_integrity_rq(req)) {
  598. if (blk_rq_count_integrity_sg(q, req->bio) != 1)
  599. goto out_unmap;
  600. sg_init_table(&iod->meta_sg, 1);
  601. if (blk_rq_map_integrity_sg(q, req->bio, &iod->meta_sg) != 1)
  602. goto out_unmap;
  603. if (req_op(req) == REQ_OP_WRITE)
  604. nvme_dif_remap(req, nvme_dif_prep);
  605. if (!dma_map_sg(dev->dev, &iod->meta_sg, 1, dma_dir))
  606. goto out_unmap;
  607. }
  608. cmnd->rw.dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  609. cmnd->rw.dptr.prp2 = cpu_to_le64(iod->first_dma);
  610. if (blk_integrity_rq(req))
  611. cmnd->rw.metadata = cpu_to_le64(sg_dma_address(&iod->meta_sg));
  612. return BLK_STS_OK;
  613. out_unmap:
  614. dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
  615. out:
  616. return ret;
  617. }
  618. static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)
  619. {
  620. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  621. enum dma_data_direction dma_dir = rq_data_dir(req) ?
  622. DMA_TO_DEVICE : DMA_FROM_DEVICE;
  623. if (iod->nents) {
  624. dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
  625. if (blk_integrity_rq(req)) {
  626. if (req_op(req) == REQ_OP_READ)
  627. nvme_dif_remap(req, nvme_dif_complete);
  628. dma_unmap_sg(dev->dev, &iod->meta_sg, 1, dma_dir);
  629. }
  630. }
  631. nvme_cleanup_cmd(req);
  632. nvme_free_iod(dev, req);
  633. }
  634. /*
  635. * NOTE: ns is NULL when called on the admin queue.
  636. */
  637. static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
  638. const struct blk_mq_queue_data *bd)
  639. {
  640. struct nvme_ns *ns = hctx->queue->queuedata;
  641. struct nvme_queue *nvmeq = hctx->driver_data;
  642. struct nvme_dev *dev = nvmeq->dev;
  643. struct request *req = bd->rq;
  644. struct nvme_command cmnd;
  645. blk_status_t ret;
  646. ret = nvme_setup_cmd(ns, req, &cmnd);
  647. if (ret)
  648. return ret;
  649. ret = nvme_init_iod(req, dev);
  650. if (ret)
  651. goto out_free_cmd;
  652. if (blk_rq_nr_phys_segments(req)) {
  653. ret = nvme_map_data(dev, req, &cmnd);
  654. if (ret)
  655. goto out_cleanup_iod;
  656. }
  657. blk_mq_start_request(req);
  658. spin_lock_irq(&nvmeq->q_lock);
  659. if (unlikely(nvmeq->cq_vector < 0)) {
  660. ret = BLK_STS_IOERR;
  661. spin_unlock_irq(&nvmeq->q_lock);
  662. goto out_cleanup_iod;
  663. }
  664. __nvme_submit_cmd(nvmeq, &cmnd);
  665. nvme_process_cq(nvmeq);
  666. spin_unlock_irq(&nvmeq->q_lock);
  667. return BLK_STS_OK;
  668. out_cleanup_iod:
  669. nvme_free_iod(dev, req);
  670. out_free_cmd:
  671. nvme_cleanup_cmd(req);
  672. return ret;
  673. }
  674. static void nvme_pci_complete_rq(struct request *req)
  675. {
  676. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  677. nvme_unmap_data(iod->nvmeq->dev, req);
  678. nvme_complete_rq(req);
  679. }
  680. /* We read the CQE phase first to check if the rest of the entry is valid */
  681. static inline bool nvme_cqe_valid(struct nvme_queue *nvmeq, u16 head,
  682. u16 phase)
  683. {
  684. return (le16_to_cpu(nvmeq->cqes[head].status) & 1) == phase;
  685. }
  686. static inline void nvme_ring_cq_doorbell(struct nvme_queue *nvmeq)
  687. {
  688. u16 head = nvmeq->cq_head;
  689. if (likely(nvmeq->cq_vector >= 0)) {
  690. if (nvme_dbbuf_update_and_check_event(head, nvmeq->dbbuf_cq_db,
  691. nvmeq->dbbuf_cq_ei))
  692. writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
  693. }
  694. }
  695. static inline void nvme_handle_cqe(struct nvme_queue *nvmeq,
  696. struct nvme_completion *cqe)
  697. {
  698. struct request *req;
  699. if (unlikely(cqe->command_id >= nvmeq->q_depth)) {
  700. dev_warn(nvmeq->dev->ctrl.device,
  701. "invalid id %d completed on queue %d\n",
  702. cqe->command_id, le16_to_cpu(cqe->sq_id));
  703. return;
  704. }
  705. /*
  706. * AEN requests are special as they don't time out and can
  707. * survive any kind of queue freeze and often don't respond to
  708. * aborts. We don't even bother to allocate a struct request
  709. * for them but rather special case them here.
  710. */
  711. if (unlikely(nvmeq->qid == 0 &&
  712. cqe->command_id >= NVME_AQ_BLKMQ_DEPTH)) {
  713. nvme_complete_async_event(&nvmeq->dev->ctrl,
  714. cqe->status, &cqe->result);
  715. return;
  716. }
  717. nvmeq->cqe_seen = 1;
  718. req = blk_mq_tag_to_rq(*nvmeq->tags, cqe->command_id);
  719. nvme_end_request(req, cqe->status, cqe->result);
  720. }
  721. static inline bool nvme_read_cqe(struct nvme_queue *nvmeq,
  722. struct nvme_completion *cqe)
  723. {
  724. if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase)) {
  725. *cqe = nvmeq->cqes[nvmeq->cq_head];
  726. if (++nvmeq->cq_head == nvmeq->q_depth) {
  727. nvmeq->cq_head = 0;
  728. nvmeq->cq_phase = !nvmeq->cq_phase;
  729. }
  730. return true;
  731. }
  732. return false;
  733. }
  734. static void nvme_process_cq(struct nvme_queue *nvmeq)
  735. {
  736. struct nvme_completion cqe;
  737. int consumed = 0;
  738. while (nvme_read_cqe(nvmeq, &cqe)) {
  739. nvme_handle_cqe(nvmeq, &cqe);
  740. consumed++;
  741. }
  742. if (consumed)
  743. nvme_ring_cq_doorbell(nvmeq);
  744. }
  745. static irqreturn_t nvme_irq(int irq, void *data)
  746. {
  747. irqreturn_t result;
  748. struct nvme_queue *nvmeq = data;
  749. spin_lock(&nvmeq->q_lock);
  750. nvme_process_cq(nvmeq);
  751. result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
  752. nvmeq->cqe_seen = 0;
  753. spin_unlock(&nvmeq->q_lock);
  754. return result;
  755. }
  756. static irqreturn_t nvme_irq_check(int irq, void *data)
  757. {
  758. struct nvme_queue *nvmeq = data;
  759. if (nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
  760. return IRQ_WAKE_THREAD;
  761. return IRQ_NONE;
  762. }
  763. static int __nvme_poll(struct nvme_queue *nvmeq, unsigned int tag)
  764. {
  765. struct nvme_completion cqe;
  766. int found = 0, consumed = 0;
  767. if (!nvme_cqe_valid(nvmeq, nvmeq->cq_head, nvmeq->cq_phase))
  768. return 0;
  769. spin_lock_irq(&nvmeq->q_lock);
  770. while (nvme_read_cqe(nvmeq, &cqe)) {
  771. nvme_handle_cqe(nvmeq, &cqe);
  772. consumed++;
  773. if (tag == cqe.command_id) {
  774. found = 1;
  775. break;
  776. }
  777. }
  778. if (consumed)
  779. nvme_ring_cq_doorbell(nvmeq);
  780. spin_unlock_irq(&nvmeq->q_lock);
  781. return found;
  782. }
  783. static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
  784. {
  785. struct nvme_queue *nvmeq = hctx->driver_data;
  786. return __nvme_poll(nvmeq, tag);
  787. }
  788. static void nvme_pci_submit_async_event(struct nvme_ctrl *ctrl, int aer_idx)
  789. {
  790. struct nvme_dev *dev = to_nvme_dev(ctrl);
  791. struct nvme_queue *nvmeq = &dev->queues[0];
  792. struct nvme_command c;
  793. memset(&c, 0, sizeof(c));
  794. c.common.opcode = nvme_admin_async_event;
  795. c.common.command_id = NVME_AQ_BLKMQ_DEPTH + aer_idx;
  796. spin_lock_irq(&nvmeq->q_lock);
  797. __nvme_submit_cmd(nvmeq, &c);
  798. spin_unlock_irq(&nvmeq->q_lock);
  799. }
  800. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  801. {
  802. struct nvme_command c;
  803. memset(&c, 0, sizeof(c));
  804. c.delete_queue.opcode = opcode;
  805. c.delete_queue.qid = cpu_to_le16(id);
  806. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  807. }
  808. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  809. struct nvme_queue *nvmeq)
  810. {
  811. struct nvme_command c;
  812. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  813. /*
  814. * Note: we (ab)use the fact the the prp fields survive if no data
  815. * is attached to the request.
  816. */
  817. memset(&c, 0, sizeof(c));
  818. c.create_cq.opcode = nvme_admin_create_cq;
  819. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  820. c.create_cq.cqid = cpu_to_le16(qid);
  821. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  822. c.create_cq.cq_flags = cpu_to_le16(flags);
  823. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  824. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  825. }
  826. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  827. struct nvme_queue *nvmeq)
  828. {
  829. struct nvme_ctrl *ctrl = &dev->ctrl;
  830. struct nvme_command c;
  831. int flags = NVME_QUEUE_PHYS_CONTIG;
  832. /*
  833. * Some drives have a bug that auto-enables WRRU if MEDIUM isn't
  834. * set. Since URGENT priority is zeroes, it makes all queues
  835. * URGENT.
  836. */
  837. if (ctrl->quirks & NVME_QUIRK_MEDIUM_PRIO_SQ)
  838. flags |= NVME_SQ_PRIO_MEDIUM;
  839. /*
  840. * Note: we (ab)use the fact the the prp fields survive if no data
  841. * is attached to the request.
  842. */
  843. memset(&c, 0, sizeof(c));
  844. c.create_sq.opcode = nvme_admin_create_sq;
  845. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  846. c.create_sq.sqid = cpu_to_le16(qid);
  847. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  848. c.create_sq.sq_flags = cpu_to_le16(flags);
  849. c.create_sq.cqid = cpu_to_le16(qid);
  850. return nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  851. }
  852. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  853. {
  854. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  855. }
  856. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  857. {
  858. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  859. }
  860. static void abort_endio(struct request *req, blk_status_t error)
  861. {
  862. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  863. struct nvme_queue *nvmeq = iod->nvmeq;
  864. dev_warn(nvmeq->dev->ctrl.device,
  865. "Abort status: 0x%x", nvme_req(req)->status);
  866. atomic_inc(&nvmeq->dev->ctrl.abort_limit);
  867. blk_mq_free_request(req);
  868. }
  869. static bool nvme_should_reset(struct nvme_dev *dev, u32 csts)
  870. {
  871. /* If true, indicates loss of adapter communication, possibly by a
  872. * NVMe Subsystem reset.
  873. */
  874. bool nssro = dev->subsystem && (csts & NVME_CSTS_NSSRO);
  875. /* If there is a reset ongoing, we shouldn't reset again. */
  876. if (dev->ctrl.state == NVME_CTRL_RESETTING)
  877. return false;
  878. /* We shouldn't reset unless the controller is on fatal error state
  879. * _or_ if we lost the communication with it.
  880. */
  881. if (!(csts & NVME_CSTS_CFS) && !nssro)
  882. return false;
  883. return true;
  884. }
  885. static void nvme_warn_reset(struct nvme_dev *dev, u32 csts)
  886. {
  887. /* Read a config register to help see what died. */
  888. u16 pci_status;
  889. int result;
  890. result = pci_read_config_word(to_pci_dev(dev->dev), PCI_STATUS,
  891. &pci_status);
  892. if (result == PCIBIOS_SUCCESSFUL)
  893. dev_warn(dev->ctrl.device,
  894. "controller is down; will reset: CSTS=0x%x, PCI_STATUS=0x%hx\n",
  895. csts, pci_status);
  896. else
  897. dev_warn(dev->ctrl.device,
  898. "controller is down; will reset: CSTS=0x%x, PCI_STATUS read failed (%d)\n",
  899. csts, result);
  900. }
  901. static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
  902. {
  903. struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
  904. struct nvme_queue *nvmeq = iod->nvmeq;
  905. struct nvme_dev *dev = nvmeq->dev;
  906. struct request *abort_req;
  907. struct nvme_command cmd;
  908. u32 csts = readl(dev->bar + NVME_REG_CSTS);
  909. /* If PCI error recovery process is happening, we cannot reset or
  910. * the recovery mechanism will surely fail.
  911. */
  912. mb();
  913. if (pci_channel_offline(to_pci_dev(dev->dev)))
  914. return BLK_EH_RESET_TIMER;
  915. /*
  916. * Reset immediately if the controller is failed
  917. */
  918. if (nvme_should_reset(dev, csts)) {
  919. nvme_warn_reset(dev, csts);
  920. nvme_dev_disable(dev, false);
  921. nvme_reset_ctrl(&dev->ctrl);
  922. return BLK_EH_HANDLED;
  923. }
  924. /*
  925. * Did we miss an interrupt?
  926. */
  927. if (__nvme_poll(nvmeq, req->tag)) {
  928. dev_warn(dev->ctrl.device,
  929. "I/O %d QID %d timeout, completion polled\n",
  930. req->tag, nvmeq->qid);
  931. return BLK_EH_HANDLED;
  932. }
  933. /*
  934. * Shutdown immediately if controller times out while starting. The
  935. * reset work will see the pci device disabled when it gets the forced
  936. * cancellation error. All outstanding requests are completed on
  937. * shutdown, so we return BLK_EH_HANDLED.
  938. */
  939. if (dev->ctrl.state == NVME_CTRL_RESETTING) {
  940. dev_warn(dev->ctrl.device,
  941. "I/O %d QID %d timeout, disable controller\n",
  942. req->tag, nvmeq->qid);
  943. nvme_dev_disable(dev, false);
  944. nvme_req(req)->flags |= NVME_REQ_CANCELLED;
  945. return BLK_EH_HANDLED;
  946. }
  947. /*
  948. * Shutdown the controller immediately and schedule a reset if the
  949. * command was already aborted once before and still hasn't been
  950. * returned to the driver, or if this is the admin queue.
  951. */
  952. if (!nvmeq->qid || iod->aborted) {
  953. dev_warn(dev->ctrl.device,
  954. "I/O %d QID %d timeout, reset controller\n",
  955. req->tag, nvmeq->qid);
  956. nvme_dev_disable(dev, false);
  957. nvme_reset_ctrl(&dev->ctrl);
  958. /*
  959. * Mark the request as handled, since the inline shutdown
  960. * forces all outstanding requests to complete.
  961. */
  962. nvme_req(req)->flags |= NVME_REQ_CANCELLED;
  963. return BLK_EH_HANDLED;
  964. }
  965. if (atomic_dec_return(&dev->ctrl.abort_limit) < 0) {
  966. atomic_inc(&dev->ctrl.abort_limit);
  967. return BLK_EH_RESET_TIMER;
  968. }
  969. iod->aborted = 1;
  970. memset(&cmd, 0, sizeof(cmd));
  971. cmd.abort.opcode = nvme_admin_abort_cmd;
  972. cmd.abort.cid = req->tag;
  973. cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
  974. dev_warn(nvmeq->dev->ctrl.device,
  975. "I/O %d QID %d timeout, aborting\n",
  976. req->tag, nvmeq->qid);
  977. abort_req = nvme_alloc_request(dev->ctrl.admin_q, &cmd,
  978. BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
  979. if (IS_ERR(abort_req)) {
  980. atomic_inc(&dev->ctrl.abort_limit);
  981. return BLK_EH_RESET_TIMER;
  982. }
  983. abort_req->timeout = ADMIN_TIMEOUT;
  984. abort_req->end_io_data = NULL;
  985. blk_execute_rq_nowait(abort_req->q, NULL, abort_req, 0, abort_endio);
  986. /*
  987. * The aborted req will be completed on receiving the abort req.
  988. * We enable the timer again. If hit twice, it'll cause a device reset,
  989. * as the device then is in a faulty state.
  990. */
  991. return BLK_EH_RESET_TIMER;
  992. }
  993. static void nvme_free_queue(struct nvme_queue *nvmeq)
  994. {
  995. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  996. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  997. if (nvmeq->sq_cmds)
  998. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  999. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  1000. }
  1001. static void nvme_free_queues(struct nvme_dev *dev, int lowest)
  1002. {
  1003. int i;
  1004. for (i = dev->ctrl.queue_count - 1; i >= lowest; i--) {
  1005. dev->ctrl.queue_count--;
  1006. nvme_free_queue(&dev->queues[i]);
  1007. }
  1008. }
  1009. /**
  1010. * nvme_suspend_queue - put queue into suspended state
  1011. * @nvmeq - queue to suspend
  1012. */
  1013. static int nvme_suspend_queue(struct nvme_queue *nvmeq)
  1014. {
  1015. int vector;
  1016. spin_lock_irq(&nvmeq->q_lock);
  1017. if (nvmeq->cq_vector == -1) {
  1018. spin_unlock_irq(&nvmeq->q_lock);
  1019. return 1;
  1020. }
  1021. vector = nvmeq->cq_vector;
  1022. nvmeq->dev->online_queues--;
  1023. nvmeq->cq_vector = -1;
  1024. spin_unlock_irq(&nvmeq->q_lock);
  1025. if (!nvmeq->qid && nvmeq->dev->ctrl.admin_q)
  1026. blk_mq_quiesce_queue(nvmeq->dev->ctrl.admin_q);
  1027. pci_free_irq(to_pci_dev(nvmeq->dev->dev), vector, nvmeq);
  1028. return 0;
  1029. }
  1030. static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)
  1031. {
  1032. struct nvme_queue *nvmeq = &dev->queues[0];
  1033. if (nvme_suspend_queue(nvmeq))
  1034. return;
  1035. if (shutdown)
  1036. nvme_shutdown_ctrl(&dev->ctrl);
  1037. else
  1038. nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
  1039. spin_lock_irq(&nvmeq->q_lock);
  1040. nvme_process_cq(nvmeq);
  1041. spin_unlock_irq(&nvmeq->q_lock);
  1042. }
  1043. static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
  1044. int entry_size)
  1045. {
  1046. int q_depth = dev->q_depth;
  1047. unsigned q_size_aligned = roundup(q_depth * entry_size,
  1048. dev->ctrl.page_size);
  1049. if (q_size_aligned * nr_io_queues > dev->cmb_size) {
  1050. u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
  1051. mem_per_q = round_down(mem_per_q, dev->ctrl.page_size);
  1052. q_depth = div_u64(mem_per_q, entry_size);
  1053. /*
  1054. * Ensure the reduced q_depth is above some threshold where it
  1055. * would be better to map queues in system memory with the
  1056. * original depth
  1057. */
  1058. if (q_depth < 64)
  1059. return -ENOMEM;
  1060. }
  1061. return q_depth;
  1062. }
  1063. static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  1064. int qid, int depth)
  1065. {
  1066. /* CMB SQEs will be mapped before creation */
  1067. if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz))
  1068. return 0;
  1069. nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
  1070. &nvmeq->sq_dma_addr, GFP_KERNEL);
  1071. if (!nvmeq->sq_cmds)
  1072. return -ENOMEM;
  1073. return 0;
  1074. }
  1075. static int nvme_alloc_queue(struct nvme_dev *dev, int qid,
  1076. int depth, int node)
  1077. {
  1078. struct nvme_queue *nvmeq = &dev->queues[qid];
  1079. if (dev->ctrl.queue_count > qid)
  1080. return 0;
  1081. nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
  1082. &nvmeq->cq_dma_addr, GFP_KERNEL);
  1083. if (!nvmeq->cqes)
  1084. goto free_nvmeq;
  1085. if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
  1086. goto free_cqdma;
  1087. nvmeq->q_dmadev = dev->dev;
  1088. nvmeq->dev = dev;
  1089. spin_lock_init(&nvmeq->q_lock);
  1090. nvmeq->cq_head = 0;
  1091. nvmeq->cq_phase = 1;
  1092. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1093. nvmeq->q_depth = depth;
  1094. nvmeq->qid = qid;
  1095. nvmeq->cq_vector = -1;
  1096. dev->ctrl.queue_count++;
  1097. return 0;
  1098. free_cqdma:
  1099. dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
  1100. nvmeq->cq_dma_addr);
  1101. free_nvmeq:
  1102. return -ENOMEM;
  1103. }
  1104. static int queue_request_irq(struct nvme_queue *nvmeq)
  1105. {
  1106. struct pci_dev *pdev = to_pci_dev(nvmeq->dev->dev);
  1107. int nr = nvmeq->dev->ctrl.instance;
  1108. if (use_threaded_interrupts) {
  1109. return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq_check,
  1110. nvme_irq, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
  1111. } else {
  1112. return pci_request_irq(pdev, nvmeq->cq_vector, nvme_irq,
  1113. NULL, nvmeq, "nvme%dq%d", nr, nvmeq->qid);
  1114. }
  1115. }
  1116. static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
  1117. {
  1118. struct nvme_dev *dev = nvmeq->dev;
  1119. spin_lock_irq(&nvmeq->q_lock);
  1120. nvmeq->sq_tail = 0;
  1121. nvmeq->cq_head = 0;
  1122. nvmeq->cq_phase = 1;
  1123. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1124. memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
  1125. nvme_dbbuf_init(dev, nvmeq, qid);
  1126. dev->online_queues++;
  1127. spin_unlock_irq(&nvmeq->q_lock);
  1128. }
  1129. static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
  1130. {
  1131. struct nvme_dev *dev = nvmeq->dev;
  1132. int result;
  1133. if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
  1134. unsigned offset = (qid - 1) * roundup(SQ_SIZE(nvmeq->q_depth),
  1135. dev->ctrl.page_size);
  1136. nvmeq->sq_dma_addr = dev->cmb_bus_addr + offset;
  1137. nvmeq->sq_cmds_io = dev->cmb + offset;
  1138. }
  1139. nvmeq->cq_vector = qid - 1;
  1140. result = adapter_alloc_cq(dev, qid, nvmeq);
  1141. if (result < 0)
  1142. goto release_vector;
  1143. result = adapter_alloc_sq(dev, qid, nvmeq);
  1144. if (result < 0)
  1145. goto release_cq;
  1146. nvme_init_queue(nvmeq, qid);
  1147. result = queue_request_irq(nvmeq);
  1148. if (result < 0)
  1149. goto release_sq;
  1150. return result;
  1151. release_sq:
  1152. dev->online_queues--;
  1153. adapter_delete_sq(dev, qid);
  1154. release_cq:
  1155. adapter_delete_cq(dev, qid);
  1156. release_vector:
  1157. nvmeq->cq_vector = -1;
  1158. return result;
  1159. }
  1160. static const struct blk_mq_ops nvme_mq_admin_ops = {
  1161. .queue_rq = nvme_queue_rq,
  1162. .complete = nvme_pci_complete_rq,
  1163. .init_hctx = nvme_admin_init_hctx,
  1164. .exit_hctx = nvme_admin_exit_hctx,
  1165. .init_request = nvme_init_request,
  1166. .timeout = nvme_timeout,
  1167. };
  1168. static const struct blk_mq_ops nvme_mq_ops = {
  1169. .queue_rq = nvme_queue_rq,
  1170. .complete = nvme_pci_complete_rq,
  1171. .init_hctx = nvme_init_hctx,
  1172. .init_request = nvme_init_request,
  1173. .map_queues = nvme_pci_map_queues,
  1174. .timeout = nvme_timeout,
  1175. .poll = nvme_poll,
  1176. };
  1177. static void nvme_dev_remove_admin(struct nvme_dev *dev)
  1178. {
  1179. if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q)) {
  1180. /*
  1181. * If the controller was reset during removal, it's possible
  1182. * user requests may be waiting on a stopped queue. Start the
  1183. * queue to flush these to completion.
  1184. */
  1185. blk_mq_unquiesce_queue(dev->ctrl.admin_q);
  1186. blk_cleanup_queue(dev->ctrl.admin_q);
  1187. blk_mq_free_tag_set(&dev->admin_tagset);
  1188. }
  1189. }
  1190. static int nvme_alloc_admin_tags(struct nvme_dev *dev)
  1191. {
  1192. if (!dev->ctrl.admin_q) {
  1193. dev->admin_tagset.ops = &nvme_mq_admin_ops;
  1194. dev->admin_tagset.nr_hw_queues = 1;
  1195. /*
  1196. * Subtract one to leave an empty queue entry for 'Full Queue'
  1197. * condition. See NVM-Express 1.2 specification, section 4.1.2.
  1198. */
  1199. dev->admin_tagset.queue_depth = NVME_AQ_BLKMQ_DEPTH - 1;
  1200. dev->admin_tagset.timeout = ADMIN_TIMEOUT;
  1201. dev->admin_tagset.numa_node = dev_to_node(dev->dev);
  1202. dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
  1203. dev->admin_tagset.flags = BLK_MQ_F_NO_SCHED;
  1204. dev->admin_tagset.driver_data = dev;
  1205. if (blk_mq_alloc_tag_set(&dev->admin_tagset))
  1206. return -ENOMEM;
  1207. dev->ctrl.admin_tagset = &dev->admin_tagset;
  1208. dev->ctrl.admin_q = blk_mq_init_queue(&dev->admin_tagset);
  1209. if (IS_ERR(dev->ctrl.admin_q)) {
  1210. blk_mq_free_tag_set(&dev->admin_tagset);
  1211. return -ENOMEM;
  1212. }
  1213. if (!blk_get_queue(dev->ctrl.admin_q)) {
  1214. nvme_dev_remove_admin(dev);
  1215. dev->ctrl.admin_q = NULL;
  1216. return -ENODEV;
  1217. }
  1218. } else
  1219. blk_mq_unquiesce_queue(dev->ctrl.admin_q);
  1220. return 0;
  1221. }
  1222. static unsigned long db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
  1223. {
  1224. return NVME_REG_DBS + ((nr_io_queues + 1) * 8 * dev->db_stride);
  1225. }
  1226. static int nvme_remap_bar(struct nvme_dev *dev, unsigned long size)
  1227. {
  1228. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1229. if (size <= dev->bar_mapped_size)
  1230. return 0;
  1231. if (size > pci_resource_len(pdev, 0))
  1232. return -ENOMEM;
  1233. if (dev->bar)
  1234. iounmap(dev->bar);
  1235. dev->bar = ioremap(pci_resource_start(pdev, 0), size);
  1236. if (!dev->bar) {
  1237. dev->bar_mapped_size = 0;
  1238. return -ENOMEM;
  1239. }
  1240. dev->bar_mapped_size = size;
  1241. dev->dbs = dev->bar + NVME_REG_DBS;
  1242. return 0;
  1243. }
  1244. static int nvme_pci_configure_admin_queue(struct nvme_dev *dev)
  1245. {
  1246. int result;
  1247. u32 aqa;
  1248. struct nvme_queue *nvmeq;
  1249. result = nvme_remap_bar(dev, db_bar_size(dev, 0));
  1250. if (result < 0)
  1251. return result;
  1252. dev->subsystem = readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 1, 0) ?
  1253. NVME_CAP_NSSRC(dev->ctrl.cap) : 0;
  1254. if (dev->subsystem &&
  1255. (readl(dev->bar + NVME_REG_CSTS) & NVME_CSTS_NSSRO))
  1256. writel(NVME_CSTS_NSSRO, dev->bar + NVME_REG_CSTS);
  1257. result = nvme_disable_ctrl(&dev->ctrl, dev->ctrl.cap);
  1258. if (result < 0)
  1259. return result;
  1260. result = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH,
  1261. dev_to_node(dev->dev));
  1262. if (result)
  1263. return result;
  1264. nvmeq = &dev->queues[0];
  1265. aqa = nvmeq->q_depth - 1;
  1266. aqa |= aqa << 16;
  1267. writel(aqa, dev->bar + NVME_REG_AQA);
  1268. lo_hi_writeq(nvmeq->sq_dma_addr, dev->bar + NVME_REG_ASQ);
  1269. lo_hi_writeq(nvmeq->cq_dma_addr, dev->bar + NVME_REG_ACQ);
  1270. result = nvme_enable_ctrl(&dev->ctrl, dev->ctrl.cap);
  1271. if (result)
  1272. return result;
  1273. nvmeq->cq_vector = 0;
  1274. nvme_init_queue(nvmeq, 0);
  1275. result = queue_request_irq(nvmeq);
  1276. if (result) {
  1277. nvmeq->cq_vector = -1;
  1278. return result;
  1279. }
  1280. return result;
  1281. }
  1282. static int nvme_create_io_queues(struct nvme_dev *dev)
  1283. {
  1284. unsigned i, max;
  1285. int ret = 0;
  1286. for (i = dev->ctrl.queue_count; i <= dev->max_qid; i++) {
  1287. /* vector == qid - 1, match nvme_create_queue */
  1288. if (nvme_alloc_queue(dev, i, dev->q_depth,
  1289. pci_irq_get_node(to_pci_dev(dev->dev), i - 1))) {
  1290. ret = -ENOMEM;
  1291. break;
  1292. }
  1293. }
  1294. max = min(dev->max_qid, dev->ctrl.queue_count - 1);
  1295. for (i = dev->online_queues; i <= max; i++) {
  1296. ret = nvme_create_queue(&dev->queues[i], i);
  1297. if (ret)
  1298. break;
  1299. }
  1300. /*
  1301. * Ignore failing Create SQ/CQ commands, we can continue with less
  1302. * than the desired aount of queues, and even a controller without
  1303. * I/O queues an still be used to issue admin commands. This might
  1304. * be useful to upgrade a buggy firmware for example.
  1305. */
  1306. return ret >= 0 ? 0 : ret;
  1307. }
  1308. static ssize_t nvme_cmb_show(struct device *dev,
  1309. struct device_attribute *attr,
  1310. char *buf)
  1311. {
  1312. struct nvme_dev *ndev = to_nvme_dev(dev_get_drvdata(dev));
  1313. return scnprintf(buf, PAGE_SIZE, "cmbloc : x%08x\ncmbsz : x%08x\n",
  1314. ndev->cmbloc, ndev->cmbsz);
  1315. }
  1316. static DEVICE_ATTR(cmb, S_IRUGO, nvme_cmb_show, NULL);
  1317. static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
  1318. {
  1319. u64 szu, size, offset;
  1320. resource_size_t bar_size;
  1321. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1322. void __iomem *cmb;
  1323. int bar;
  1324. dev->cmbsz = readl(dev->bar + NVME_REG_CMBSZ);
  1325. if (!(NVME_CMB_SZ(dev->cmbsz)))
  1326. return NULL;
  1327. dev->cmbloc = readl(dev->bar + NVME_REG_CMBLOC);
  1328. if (!use_cmb_sqes)
  1329. return NULL;
  1330. szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
  1331. size = szu * NVME_CMB_SZ(dev->cmbsz);
  1332. offset = szu * NVME_CMB_OFST(dev->cmbloc);
  1333. bar = NVME_CMB_BIR(dev->cmbloc);
  1334. bar_size = pci_resource_len(pdev, bar);
  1335. if (offset > bar_size)
  1336. return NULL;
  1337. /*
  1338. * Controllers may support a CMB size larger than their BAR,
  1339. * for example, due to being behind a bridge. Reduce the CMB to
  1340. * the reported size of the BAR
  1341. */
  1342. if (size > bar_size - offset)
  1343. size = bar_size - offset;
  1344. cmb = ioremap_wc(pci_resource_start(pdev, bar) + offset, size);
  1345. if (!cmb)
  1346. return NULL;
  1347. dev->cmb_bus_addr = pci_bus_address(pdev, bar) + offset;
  1348. dev->cmb_size = size;
  1349. return cmb;
  1350. }
  1351. static inline void nvme_release_cmb(struct nvme_dev *dev)
  1352. {
  1353. if (dev->cmb) {
  1354. iounmap(dev->cmb);
  1355. dev->cmb = NULL;
  1356. sysfs_remove_file_from_group(&dev->ctrl.device->kobj,
  1357. &dev_attr_cmb.attr, NULL);
  1358. dev->cmbsz = 0;
  1359. }
  1360. }
  1361. static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
  1362. {
  1363. u64 dma_addr = dev->host_mem_descs_dma;
  1364. struct nvme_command c;
  1365. int ret;
  1366. memset(&c, 0, sizeof(c));
  1367. c.features.opcode = nvme_admin_set_features;
  1368. c.features.fid = cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
  1369. c.features.dword11 = cpu_to_le32(bits);
  1370. c.features.dword12 = cpu_to_le32(dev->host_mem_size >>
  1371. ilog2(dev->ctrl.page_size));
  1372. c.features.dword13 = cpu_to_le32(lower_32_bits(dma_addr));
  1373. c.features.dword14 = cpu_to_le32(upper_32_bits(dma_addr));
  1374. c.features.dword15 = cpu_to_le32(dev->nr_host_mem_descs);
  1375. ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
  1376. if (ret) {
  1377. dev_warn(dev->ctrl.device,
  1378. "failed to set host mem (err %d, flags %#x).\n",
  1379. ret, bits);
  1380. }
  1381. return ret;
  1382. }
  1383. static void nvme_free_host_mem(struct nvme_dev *dev)
  1384. {
  1385. int i;
  1386. for (i = 0; i < dev->nr_host_mem_descs; i++) {
  1387. struct nvme_host_mem_buf_desc *desc = &dev->host_mem_descs[i];
  1388. size_t size = le32_to_cpu(desc->size) * dev->ctrl.page_size;
  1389. dma_free_attrs(dev->dev, size, dev->host_mem_desc_bufs[i],
  1390. le64_to_cpu(desc->addr),
  1391. DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
  1392. }
  1393. kfree(dev->host_mem_desc_bufs);
  1394. dev->host_mem_desc_bufs = NULL;
  1395. dma_free_coherent(dev->dev,
  1396. dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
  1397. dev->host_mem_descs, dev->host_mem_descs_dma);
  1398. dev->host_mem_descs = NULL;
  1399. dev->nr_host_mem_descs = 0;
  1400. }
  1401. static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
  1402. u32 chunk_size)
  1403. {
  1404. struct nvme_host_mem_buf_desc *descs;
  1405. u32 max_entries, len;
  1406. dma_addr_t descs_dma;
  1407. int i = 0;
  1408. void **bufs;
  1409. u64 size = 0, tmp;
  1410. tmp = (preferred + chunk_size - 1);
  1411. do_div(tmp, chunk_size);
  1412. max_entries = tmp;
  1413. if (dev->ctrl.hmmaxd && dev->ctrl.hmmaxd < max_entries)
  1414. max_entries = dev->ctrl.hmmaxd;
  1415. descs = dma_zalloc_coherent(dev->dev, max_entries * sizeof(*descs),
  1416. &descs_dma, GFP_KERNEL);
  1417. if (!descs)
  1418. goto out;
  1419. bufs = kcalloc(max_entries, sizeof(*bufs), GFP_KERNEL);
  1420. if (!bufs)
  1421. goto out_free_descs;
  1422. for (size = 0; size < preferred && i < max_entries; size += len) {
  1423. dma_addr_t dma_addr;
  1424. len = min_t(u64, chunk_size, preferred - size);
  1425. bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
  1426. DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
  1427. if (!bufs[i])
  1428. break;
  1429. descs[i].addr = cpu_to_le64(dma_addr);
  1430. descs[i].size = cpu_to_le32(len / dev->ctrl.page_size);
  1431. i++;
  1432. }
  1433. if (!size)
  1434. goto out_free_bufs;
  1435. dev->nr_host_mem_descs = i;
  1436. dev->host_mem_size = size;
  1437. dev->host_mem_descs = descs;
  1438. dev->host_mem_descs_dma = descs_dma;
  1439. dev->host_mem_desc_bufs = bufs;
  1440. return 0;
  1441. out_free_bufs:
  1442. while (--i >= 0) {
  1443. size_t size = le32_to_cpu(descs[i].size) * dev->ctrl.page_size;
  1444. dma_free_attrs(dev->dev, size, bufs[i],
  1445. le64_to_cpu(descs[i].addr),
  1446. DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
  1447. }
  1448. kfree(bufs);
  1449. out_free_descs:
  1450. dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
  1451. descs_dma);
  1452. out:
  1453. dev->host_mem_descs = NULL;
  1454. return -ENOMEM;
  1455. }
  1456. static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
  1457. {
  1458. u32 chunk_size;
  1459. /* start big and work our way down */
  1460. for (chunk_size = min_t(u64, preferred, PAGE_SIZE * MAX_ORDER_NR_PAGES);
  1461. chunk_size >= max_t(u32, dev->ctrl.hmminds * 4096, PAGE_SIZE * 2);
  1462. chunk_size /= 2) {
  1463. if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
  1464. if (!min || dev->host_mem_size >= min)
  1465. return 0;
  1466. nvme_free_host_mem(dev);
  1467. }
  1468. }
  1469. return -ENOMEM;
  1470. }
  1471. static int nvme_setup_host_mem(struct nvme_dev *dev)
  1472. {
  1473. u64 max = (u64)max_host_mem_size_mb * SZ_1M;
  1474. u64 preferred = (u64)dev->ctrl.hmpre * 4096;
  1475. u64 min = (u64)dev->ctrl.hmmin * 4096;
  1476. u32 enable_bits = NVME_HOST_MEM_ENABLE;
  1477. int ret = 0;
  1478. preferred = min(preferred, max);
  1479. if (min > max) {
  1480. dev_warn(dev->ctrl.device,
  1481. "min host memory (%lld MiB) above limit (%d MiB).\n",
  1482. min >> ilog2(SZ_1M), max_host_mem_size_mb);
  1483. nvme_free_host_mem(dev);
  1484. return 0;
  1485. }
  1486. /*
  1487. * If we already have a buffer allocated check if we can reuse it.
  1488. */
  1489. if (dev->host_mem_descs) {
  1490. if (dev->host_mem_size >= min)
  1491. enable_bits |= NVME_HOST_MEM_RETURN;
  1492. else
  1493. nvme_free_host_mem(dev);
  1494. }
  1495. if (!dev->host_mem_descs) {
  1496. if (nvme_alloc_host_mem(dev, min, preferred)) {
  1497. dev_warn(dev->ctrl.device,
  1498. "failed to allocate host memory buffer.\n");
  1499. return 0; /* controller must work without HMB */
  1500. }
  1501. dev_info(dev->ctrl.device,
  1502. "allocated %lld MiB host memory buffer.\n",
  1503. dev->host_mem_size >> ilog2(SZ_1M));
  1504. }
  1505. ret = nvme_set_host_mem(dev, enable_bits);
  1506. if (ret)
  1507. nvme_free_host_mem(dev);
  1508. return ret;
  1509. }
  1510. static int nvme_setup_io_queues(struct nvme_dev *dev)
  1511. {
  1512. struct nvme_queue *adminq = &dev->queues[0];
  1513. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1514. int result, nr_io_queues;
  1515. unsigned long size;
  1516. nr_io_queues = num_possible_cpus();
  1517. result = nvme_set_queue_count(&dev->ctrl, &nr_io_queues);
  1518. if (result < 0)
  1519. return result;
  1520. if (nr_io_queues == 0)
  1521. return 0;
  1522. if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
  1523. result = nvme_cmb_qdepth(dev, nr_io_queues,
  1524. sizeof(struct nvme_command));
  1525. if (result > 0)
  1526. dev->q_depth = result;
  1527. else
  1528. nvme_release_cmb(dev);
  1529. }
  1530. do {
  1531. size = db_bar_size(dev, nr_io_queues);
  1532. result = nvme_remap_bar(dev, size);
  1533. if (!result)
  1534. break;
  1535. if (!--nr_io_queues)
  1536. return -ENOMEM;
  1537. } while (1);
  1538. adminq->q_db = dev->dbs;
  1539. /* Deregister the admin queue's interrupt */
  1540. pci_free_irq(pdev, 0, adminq);
  1541. /*
  1542. * If we enable msix early due to not intx, disable it again before
  1543. * setting up the full range we need.
  1544. */
  1545. pci_free_irq_vectors(pdev);
  1546. nr_io_queues = pci_alloc_irq_vectors(pdev, 1, nr_io_queues,
  1547. PCI_IRQ_ALL_TYPES | PCI_IRQ_AFFINITY);
  1548. if (nr_io_queues <= 0)
  1549. return -EIO;
  1550. dev->max_qid = nr_io_queues;
  1551. /*
  1552. * Should investigate if there's a performance win from allocating
  1553. * more queues than interrupt vectors; it might allow the submission
  1554. * path to scale better, even if the receive path is limited by the
  1555. * number of interrupts.
  1556. */
  1557. result = queue_request_irq(adminq);
  1558. if (result) {
  1559. adminq->cq_vector = -1;
  1560. return result;
  1561. }
  1562. return nvme_create_io_queues(dev);
  1563. }
  1564. static void nvme_del_queue_end(struct request *req, blk_status_t error)
  1565. {
  1566. struct nvme_queue *nvmeq = req->end_io_data;
  1567. blk_mq_free_request(req);
  1568. complete(&nvmeq->dev->ioq_wait);
  1569. }
  1570. static void nvme_del_cq_end(struct request *req, blk_status_t error)
  1571. {
  1572. struct nvme_queue *nvmeq = req->end_io_data;
  1573. if (!error) {
  1574. unsigned long flags;
  1575. /*
  1576. * We might be called with the AQ q_lock held
  1577. * and the I/O queue q_lock should always
  1578. * nest inside the AQ one.
  1579. */
  1580. spin_lock_irqsave_nested(&nvmeq->q_lock, flags,
  1581. SINGLE_DEPTH_NESTING);
  1582. nvme_process_cq(nvmeq);
  1583. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  1584. }
  1585. nvme_del_queue_end(req, error);
  1586. }
  1587. static int nvme_delete_queue(struct nvme_queue *nvmeq, u8 opcode)
  1588. {
  1589. struct request_queue *q = nvmeq->dev->ctrl.admin_q;
  1590. struct request *req;
  1591. struct nvme_command cmd;
  1592. memset(&cmd, 0, sizeof(cmd));
  1593. cmd.delete_queue.opcode = opcode;
  1594. cmd.delete_queue.qid = cpu_to_le16(nvmeq->qid);
  1595. req = nvme_alloc_request(q, &cmd, BLK_MQ_REQ_NOWAIT, NVME_QID_ANY);
  1596. if (IS_ERR(req))
  1597. return PTR_ERR(req);
  1598. req->timeout = ADMIN_TIMEOUT;
  1599. req->end_io_data = nvmeq;
  1600. blk_execute_rq_nowait(q, NULL, req, false,
  1601. opcode == nvme_admin_delete_cq ?
  1602. nvme_del_cq_end : nvme_del_queue_end);
  1603. return 0;
  1604. }
  1605. static void nvme_disable_io_queues(struct nvme_dev *dev, int queues)
  1606. {
  1607. int pass;
  1608. unsigned long timeout;
  1609. u8 opcode = nvme_admin_delete_sq;
  1610. for (pass = 0; pass < 2; pass++) {
  1611. int sent = 0, i = queues;
  1612. reinit_completion(&dev->ioq_wait);
  1613. retry:
  1614. timeout = ADMIN_TIMEOUT;
  1615. for (; i > 0; i--, sent++)
  1616. if (nvme_delete_queue(&dev->queues[i], opcode))
  1617. break;
  1618. while (sent--) {
  1619. timeout = wait_for_completion_io_timeout(&dev->ioq_wait, timeout);
  1620. if (timeout == 0)
  1621. return;
  1622. if (i)
  1623. goto retry;
  1624. }
  1625. opcode = nvme_admin_delete_cq;
  1626. }
  1627. }
  1628. /*
  1629. * Return: error value if an error occurred setting up the queues or calling
  1630. * Identify Device. 0 if these succeeded, even if adding some of the
  1631. * namespaces failed. At the moment, these failures are silent. TBD which
  1632. * failures should be reported.
  1633. */
  1634. static int nvme_dev_add(struct nvme_dev *dev)
  1635. {
  1636. if (!dev->ctrl.tagset) {
  1637. dev->tagset.ops = &nvme_mq_ops;
  1638. dev->tagset.nr_hw_queues = dev->online_queues - 1;
  1639. dev->tagset.timeout = NVME_IO_TIMEOUT;
  1640. dev->tagset.numa_node = dev_to_node(dev->dev);
  1641. dev->tagset.queue_depth =
  1642. min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
  1643. dev->tagset.cmd_size = nvme_cmd_size(dev);
  1644. dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
  1645. dev->tagset.driver_data = dev;
  1646. if (blk_mq_alloc_tag_set(&dev->tagset))
  1647. return 0;
  1648. dev->ctrl.tagset = &dev->tagset;
  1649. nvme_dbbuf_set(dev);
  1650. } else {
  1651. blk_mq_update_nr_hw_queues(&dev->tagset, dev->online_queues - 1);
  1652. /* Free previously allocated queues that are no longer usable */
  1653. nvme_free_queues(dev, dev->online_queues);
  1654. }
  1655. return 0;
  1656. }
  1657. static int nvme_pci_enable(struct nvme_dev *dev)
  1658. {
  1659. int result = -ENOMEM;
  1660. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1661. if (pci_enable_device_mem(pdev))
  1662. return result;
  1663. pci_set_master(pdev);
  1664. if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
  1665. dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
  1666. goto disable;
  1667. if (readl(dev->bar + NVME_REG_CSTS) == -1) {
  1668. result = -ENODEV;
  1669. goto disable;
  1670. }
  1671. /*
  1672. * Some devices and/or platforms don't advertise or work with INTx
  1673. * interrupts. Pre-enable a single MSIX or MSI vec for setup. We'll
  1674. * adjust this later.
  1675. */
  1676. result = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  1677. if (result < 0)
  1678. return result;
  1679. dev->ctrl.cap = lo_hi_readq(dev->bar + NVME_REG_CAP);
  1680. dev->q_depth = min_t(int, NVME_CAP_MQES(dev->ctrl.cap) + 1,
  1681. io_queue_depth);
  1682. dev->db_stride = 1 << NVME_CAP_STRIDE(dev->ctrl.cap);
  1683. dev->dbs = dev->bar + 4096;
  1684. /*
  1685. * Temporary fix for the Apple controller found in the MacBook8,1 and
  1686. * some MacBook7,1 to avoid controller resets and data loss.
  1687. */
  1688. if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
  1689. dev->q_depth = 2;
  1690. dev_warn(dev->ctrl.device, "detected Apple NVMe controller, "
  1691. "set queue depth=%u to work around controller resets\n",
  1692. dev->q_depth);
  1693. } else if (pdev->vendor == PCI_VENDOR_ID_SAMSUNG &&
  1694. (pdev->device == 0xa821 || pdev->device == 0xa822) &&
  1695. NVME_CAP_MQES(dev->ctrl.cap) == 0) {
  1696. dev->q_depth = 64;
  1697. dev_err(dev->ctrl.device, "detected PM1725 NVMe controller, "
  1698. "set queue depth=%u\n", dev->q_depth);
  1699. }
  1700. /*
  1701. * CMBs can currently only exist on >=1.2 PCIe devices. We only
  1702. * populate sysfs if a CMB is implemented. Since nvme_dev_attrs_group
  1703. * has no name we can pass NULL as final argument to
  1704. * sysfs_add_file_to_group.
  1705. */
  1706. if (readl(dev->bar + NVME_REG_VS) >= NVME_VS(1, 2, 0)) {
  1707. dev->cmb = nvme_map_cmb(dev);
  1708. if (dev->cmb) {
  1709. if (sysfs_add_file_to_group(&dev->ctrl.device->kobj,
  1710. &dev_attr_cmb.attr, NULL))
  1711. dev_warn(dev->ctrl.device,
  1712. "failed to add sysfs attribute for CMB\n");
  1713. }
  1714. }
  1715. pci_enable_pcie_error_reporting(pdev);
  1716. pci_save_state(pdev);
  1717. return 0;
  1718. disable:
  1719. pci_disable_device(pdev);
  1720. return result;
  1721. }
  1722. static void nvme_dev_unmap(struct nvme_dev *dev)
  1723. {
  1724. if (dev->bar)
  1725. iounmap(dev->bar);
  1726. pci_release_mem_regions(to_pci_dev(dev->dev));
  1727. }
  1728. static void nvme_pci_disable(struct nvme_dev *dev)
  1729. {
  1730. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1731. nvme_release_cmb(dev);
  1732. pci_free_irq_vectors(pdev);
  1733. if (pci_is_enabled(pdev)) {
  1734. pci_disable_pcie_error_reporting(pdev);
  1735. pci_disable_device(pdev);
  1736. }
  1737. }
  1738. static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
  1739. {
  1740. int i, queues;
  1741. bool dead = true;
  1742. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1743. mutex_lock(&dev->shutdown_lock);
  1744. if (pci_is_enabled(pdev)) {
  1745. u32 csts = readl(dev->bar + NVME_REG_CSTS);
  1746. if (dev->ctrl.state == NVME_CTRL_LIVE ||
  1747. dev->ctrl.state == NVME_CTRL_RESETTING)
  1748. nvme_start_freeze(&dev->ctrl);
  1749. dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
  1750. pdev->error_state != pci_channel_io_normal);
  1751. }
  1752. /*
  1753. * Give the controller a chance to complete all entered requests if
  1754. * doing a safe shutdown.
  1755. */
  1756. if (!dead) {
  1757. if (shutdown)
  1758. nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
  1759. /*
  1760. * If the controller is still alive tell it to stop using the
  1761. * host memory buffer. In theory the shutdown / reset should
  1762. * make sure that it doesn't access the host memoery anymore,
  1763. * but I'd rather be safe than sorry..
  1764. */
  1765. if (dev->host_mem_descs)
  1766. nvme_set_host_mem(dev, 0);
  1767. }
  1768. nvme_stop_queues(&dev->ctrl);
  1769. queues = dev->online_queues - 1;
  1770. for (i = dev->ctrl.queue_count - 1; i > 0; i--)
  1771. nvme_suspend_queue(&dev->queues[i]);
  1772. if (dead) {
  1773. /* A device might become IO incapable very soon during
  1774. * probe, before the admin queue is configured. Thus,
  1775. * queue_count can be 0 here.
  1776. */
  1777. if (dev->ctrl.queue_count)
  1778. nvme_suspend_queue(&dev->queues[0]);
  1779. } else {
  1780. nvme_disable_io_queues(dev, queues);
  1781. nvme_disable_admin_queue(dev, shutdown);
  1782. }
  1783. nvme_pci_disable(dev);
  1784. blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
  1785. blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
  1786. /*
  1787. * The driver will not be starting up queues again if shutting down so
  1788. * must flush all entered requests to their failed completion to avoid
  1789. * deadlocking blk-mq hot-cpu notifier.
  1790. */
  1791. if (shutdown) {
  1792. nvme_start_queues(&dev->ctrl);
  1793. if (dev->ctrl.admin_q && !blk_queue_dying(dev->ctrl.admin_q))
  1794. blk_mq_unquiesce_queue(dev->ctrl.admin_q);
  1795. }
  1796. mutex_unlock(&dev->shutdown_lock);
  1797. }
  1798. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  1799. {
  1800. dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
  1801. PAGE_SIZE, PAGE_SIZE, 0);
  1802. if (!dev->prp_page_pool)
  1803. return -ENOMEM;
  1804. /* Optimisation for I/Os between 4k and 128k */
  1805. dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
  1806. 256, 256, 0);
  1807. if (!dev->prp_small_pool) {
  1808. dma_pool_destroy(dev->prp_page_pool);
  1809. return -ENOMEM;
  1810. }
  1811. return 0;
  1812. }
  1813. static void nvme_release_prp_pools(struct nvme_dev *dev)
  1814. {
  1815. dma_pool_destroy(dev->prp_page_pool);
  1816. dma_pool_destroy(dev->prp_small_pool);
  1817. }
  1818. static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
  1819. {
  1820. struct nvme_dev *dev = to_nvme_dev(ctrl);
  1821. nvme_dbbuf_dma_free(dev);
  1822. put_device(dev->dev);
  1823. if (dev->tagset.tags)
  1824. blk_mq_free_tag_set(&dev->tagset);
  1825. if (dev->ctrl.admin_q)
  1826. blk_put_queue(dev->ctrl.admin_q);
  1827. kfree(dev->queues);
  1828. free_opal_dev(dev->ctrl.opal_dev);
  1829. kfree(dev);
  1830. }
  1831. static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
  1832. {
  1833. dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
  1834. kref_get(&dev->ctrl.kref);
  1835. nvme_dev_disable(dev, false);
  1836. if (!schedule_work(&dev->remove_work))
  1837. nvme_put_ctrl(&dev->ctrl);
  1838. }
  1839. static void nvme_reset_work(struct work_struct *work)
  1840. {
  1841. struct nvme_dev *dev =
  1842. container_of(work, struct nvme_dev, ctrl.reset_work);
  1843. bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
  1844. int result = -ENODEV;
  1845. if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
  1846. goto out;
  1847. /*
  1848. * If we're called to reset a live controller first shut it down before
  1849. * moving on.
  1850. */
  1851. if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
  1852. nvme_dev_disable(dev, false);
  1853. result = nvme_pci_enable(dev);
  1854. if (result)
  1855. goto out;
  1856. result = nvme_pci_configure_admin_queue(dev);
  1857. if (result)
  1858. goto out;
  1859. result = nvme_alloc_admin_tags(dev);
  1860. if (result)
  1861. goto out;
  1862. result = nvme_init_identify(&dev->ctrl);
  1863. if (result)
  1864. goto out;
  1865. if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
  1866. if (!dev->ctrl.opal_dev)
  1867. dev->ctrl.opal_dev =
  1868. init_opal_dev(&dev->ctrl, &nvme_sec_submit);
  1869. else if (was_suspend)
  1870. opal_unlock_from_suspend(dev->ctrl.opal_dev);
  1871. } else {
  1872. free_opal_dev(dev->ctrl.opal_dev);
  1873. dev->ctrl.opal_dev = NULL;
  1874. }
  1875. if (dev->ctrl.oacs & NVME_CTRL_OACS_DBBUF_SUPP) {
  1876. result = nvme_dbbuf_dma_alloc(dev);
  1877. if (result)
  1878. dev_warn(dev->dev,
  1879. "unable to allocate dma for dbbuf\n");
  1880. }
  1881. if (dev->ctrl.hmpre) {
  1882. result = nvme_setup_host_mem(dev);
  1883. if (result < 0)
  1884. goto out;
  1885. }
  1886. result = nvme_setup_io_queues(dev);
  1887. if (result)
  1888. goto out;
  1889. /*
  1890. * Keep the controller around but remove all namespaces if we don't have
  1891. * any working I/O queue.
  1892. */
  1893. if (dev->online_queues < 2) {
  1894. dev_warn(dev->ctrl.device, "IO queues not created\n");
  1895. nvme_kill_queues(&dev->ctrl);
  1896. nvme_remove_namespaces(&dev->ctrl);
  1897. } else {
  1898. nvme_start_queues(&dev->ctrl);
  1899. nvme_wait_freeze(&dev->ctrl);
  1900. nvme_dev_add(dev);
  1901. nvme_unfreeze(&dev->ctrl);
  1902. }
  1903. if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
  1904. dev_warn(dev->ctrl.device, "failed to mark controller live\n");
  1905. goto out;
  1906. }
  1907. nvme_start_ctrl(&dev->ctrl);
  1908. return;
  1909. out:
  1910. nvme_remove_dead_ctrl(dev, result);
  1911. }
  1912. static void nvme_remove_dead_ctrl_work(struct work_struct *work)
  1913. {
  1914. struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
  1915. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1916. nvme_kill_queues(&dev->ctrl);
  1917. if (pci_get_drvdata(pdev))
  1918. device_release_driver(&pdev->dev);
  1919. nvme_put_ctrl(&dev->ctrl);
  1920. }
  1921. static int nvme_pci_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
  1922. {
  1923. *val = readl(to_nvme_dev(ctrl)->bar + off);
  1924. return 0;
  1925. }
  1926. static int nvme_pci_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
  1927. {
  1928. writel(val, to_nvme_dev(ctrl)->bar + off);
  1929. return 0;
  1930. }
  1931. static int nvme_pci_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
  1932. {
  1933. *val = lo_hi_readq(to_nvme_dev(ctrl)->bar + off);
  1934. return 0;
  1935. }
  1936. static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = {
  1937. .name = "pcie",
  1938. .module = THIS_MODULE,
  1939. .flags = NVME_F_METADATA_SUPPORTED,
  1940. .reg_read32 = nvme_pci_reg_read32,
  1941. .reg_write32 = nvme_pci_reg_write32,
  1942. .reg_read64 = nvme_pci_reg_read64,
  1943. .free_ctrl = nvme_pci_free_ctrl,
  1944. .submit_async_event = nvme_pci_submit_async_event,
  1945. };
  1946. static int nvme_dev_map(struct nvme_dev *dev)
  1947. {
  1948. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1949. if (pci_request_mem_regions(pdev, "nvme"))
  1950. return -ENODEV;
  1951. if (nvme_remap_bar(dev, NVME_REG_DBS + 4096))
  1952. goto release;
  1953. return 0;
  1954. release:
  1955. pci_release_mem_regions(pdev);
  1956. return -ENODEV;
  1957. }
  1958. static unsigned long check_vendor_combination_bug(struct pci_dev *pdev)
  1959. {
  1960. if (pdev->vendor == 0x144d && pdev->device == 0xa802) {
  1961. /*
  1962. * Several Samsung devices seem to drop off the PCIe bus
  1963. * randomly when APST is on and uses the deepest sleep state.
  1964. * This has been observed on a Samsung "SM951 NVMe SAMSUNG
  1965. * 256GB", a "PM951 NVMe SAMSUNG 512GB", and a "Samsung SSD
  1966. * 950 PRO 256GB", but it seems to be restricted to two Dell
  1967. * laptops.
  1968. */
  1969. if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.") &&
  1970. (dmi_match(DMI_PRODUCT_NAME, "XPS 15 9550") ||
  1971. dmi_match(DMI_PRODUCT_NAME, "Precision 5510")))
  1972. return NVME_QUIRK_NO_DEEPEST_PS;
  1973. } else if (pdev->vendor == 0x144d && pdev->device == 0xa804) {
  1974. /*
  1975. * Samsung SSD 960 EVO drops off the PCIe bus after system
  1976. * suspend on a Ryzen board, ASUS PRIME B350M-A, as well as
  1977. * within few minutes after bootup on a Coffee Lake board -
  1978. * ASUS PRIME Z370-A
  1979. */
  1980. if (dmi_match(DMI_BOARD_VENDOR, "ASUSTeK COMPUTER INC.") &&
  1981. (dmi_match(DMI_BOARD_NAME, "PRIME B350M-A") ||
  1982. dmi_match(DMI_BOARD_NAME, "PRIME Z370-A")))
  1983. return NVME_QUIRK_NO_APST;
  1984. }
  1985. return 0;
  1986. }
  1987. static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1988. {
  1989. int node, result = -ENOMEM;
  1990. struct nvme_dev *dev;
  1991. unsigned long quirks = id->driver_data;
  1992. node = dev_to_node(&pdev->dev);
  1993. if (node == NUMA_NO_NODE)
  1994. set_dev_node(&pdev->dev, first_memory_node);
  1995. dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
  1996. if (!dev)
  1997. return -ENOMEM;
  1998. dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(struct nvme_queue),
  1999. GFP_KERNEL, node);
  2000. if (!dev->queues)
  2001. goto free;
  2002. dev->dev = get_device(&pdev->dev);
  2003. pci_set_drvdata(pdev, dev);
  2004. result = nvme_dev_map(dev);
  2005. if (result)
  2006. goto put_pci;
  2007. INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
  2008. INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
  2009. mutex_init(&dev->shutdown_lock);
  2010. init_completion(&dev->ioq_wait);
  2011. result = nvme_setup_prp_pools(dev);
  2012. if (result)
  2013. goto unmap;
  2014. quirks |= check_vendor_combination_bug(pdev);
  2015. result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
  2016. quirks);
  2017. if (result)
  2018. goto release_pools;
  2019. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING);
  2020. dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
  2021. queue_work(nvme_wq, &dev->ctrl.reset_work);
  2022. return 0;
  2023. release_pools:
  2024. nvme_release_prp_pools(dev);
  2025. unmap:
  2026. nvme_dev_unmap(dev);
  2027. put_pci:
  2028. put_device(dev->dev);
  2029. free:
  2030. kfree(dev->queues);
  2031. kfree(dev);
  2032. return result;
  2033. }
  2034. static void nvme_reset_prepare(struct pci_dev *pdev)
  2035. {
  2036. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2037. nvme_dev_disable(dev, false);
  2038. }
  2039. static void nvme_reset_done(struct pci_dev *pdev)
  2040. {
  2041. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2042. nvme_reset_ctrl(&dev->ctrl);
  2043. }
  2044. static void nvme_shutdown(struct pci_dev *pdev)
  2045. {
  2046. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2047. nvme_dev_disable(dev, true);
  2048. }
  2049. /*
  2050. * The driver's remove may be called on a device in a partially initialized
  2051. * state. This function must not have any dependencies on the device state in
  2052. * order to proceed.
  2053. */
  2054. static void nvme_remove(struct pci_dev *pdev)
  2055. {
  2056. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2057. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DELETING);
  2058. cancel_work_sync(&dev->ctrl.reset_work);
  2059. pci_set_drvdata(pdev, NULL);
  2060. if (!pci_device_is_present(pdev)) {
  2061. nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
  2062. nvme_dev_disable(dev, false);
  2063. }
  2064. flush_work(&dev->ctrl.reset_work);
  2065. nvme_stop_ctrl(&dev->ctrl);
  2066. nvme_remove_namespaces(&dev->ctrl);
  2067. nvme_dev_disable(dev, true);
  2068. nvme_free_host_mem(dev);
  2069. nvme_dev_remove_admin(dev);
  2070. nvme_free_queues(dev, 0);
  2071. nvme_uninit_ctrl(&dev->ctrl);
  2072. nvme_release_prp_pools(dev);
  2073. nvme_dev_unmap(dev);
  2074. nvme_put_ctrl(&dev->ctrl);
  2075. }
  2076. static int nvme_pci_sriov_configure(struct pci_dev *pdev, int numvfs)
  2077. {
  2078. int ret = 0;
  2079. if (numvfs == 0) {
  2080. if (pci_vfs_assigned(pdev)) {
  2081. dev_warn(&pdev->dev,
  2082. "Cannot disable SR-IOV VFs while assigned\n");
  2083. return -EPERM;
  2084. }
  2085. pci_disable_sriov(pdev);
  2086. return 0;
  2087. }
  2088. ret = pci_enable_sriov(pdev, numvfs);
  2089. return ret ? ret : numvfs;
  2090. }
  2091. #ifdef CONFIG_PM_SLEEP
  2092. static int nvme_suspend(struct device *dev)
  2093. {
  2094. struct pci_dev *pdev = to_pci_dev(dev);
  2095. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2096. nvme_dev_disable(ndev, true);
  2097. return 0;
  2098. }
  2099. static int nvme_resume(struct device *dev)
  2100. {
  2101. struct pci_dev *pdev = to_pci_dev(dev);
  2102. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2103. nvme_reset_ctrl(&ndev->ctrl);
  2104. return 0;
  2105. }
  2106. #endif
  2107. static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  2108. static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
  2109. pci_channel_state_t state)
  2110. {
  2111. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2112. /*
  2113. * A frozen channel requires a reset. When detected, this method will
  2114. * shutdown the controller to quiesce. The controller will be restarted
  2115. * after the slot reset through driver's slot_reset callback.
  2116. */
  2117. switch (state) {
  2118. case pci_channel_io_normal:
  2119. return PCI_ERS_RESULT_CAN_RECOVER;
  2120. case pci_channel_io_frozen:
  2121. dev_warn(dev->ctrl.device,
  2122. "frozen state error detected, reset controller\n");
  2123. nvme_dev_disable(dev, false);
  2124. return PCI_ERS_RESULT_NEED_RESET;
  2125. case pci_channel_io_perm_failure:
  2126. dev_warn(dev->ctrl.device,
  2127. "failure state error detected, request disconnect\n");
  2128. return PCI_ERS_RESULT_DISCONNECT;
  2129. }
  2130. return PCI_ERS_RESULT_NEED_RESET;
  2131. }
  2132. static pci_ers_result_t nvme_slot_reset(struct pci_dev *pdev)
  2133. {
  2134. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2135. dev_info(dev->ctrl.device, "restart after slot reset\n");
  2136. pci_restore_state(pdev);
  2137. nvme_reset_ctrl(&dev->ctrl);
  2138. return PCI_ERS_RESULT_RECOVERED;
  2139. }
  2140. static void nvme_error_resume(struct pci_dev *pdev)
  2141. {
  2142. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2143. flush_work(&dev->ctrl.reset_work);
  2144. pci_cleanup_aer_uncorrect_error_status(pdev);
  2145. }
  2146. static const struct pci_error_handlers nvme_err_handler = {
  2147. .error_detected = nvme_error_detected,
  2148. .slot_reset = nvme_slot_reset,
  2149. .resume = nvme_error_resume,
  2150. .reset_prepare = nvme_reset_prepare,
  2151. .reset_done = nvme_reset_done,
  2152. };
  2153. static const struct pci_device_id nvme_id_table[] = {
  2154. { PCI_VDEVICE(INTEL, 0x0953),
  2155. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  2156. NVME_QUIRK_DEALLOCATE_ZEROES, },
  2157. { PCI_VDEVICE(INTEL, 0x0a53),
  2158. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  2159. NVME_QUIRK_DEALLOCATE_ZEROES, },
  2160. { PCI_VDEVICE(INTEL, 0x0a54),
  2161. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  2162. NVME_QUIRK_DEALLOCATE_ZEROES, },
  2163. { PCI_VDEVICE(INTEL, 0x0a55),
  2164. .driver_data = NVME_QUIRK_STRIPE_SIZE |
  2165. NVME_QUIRK_DEALLOCATE_ZEROES, },
  2166. { PCI_VDEVICE(INTEL, 0xf1a5), /* Intel 600P/P3100 */
  2167. .driver_data = NVME_QUIRK_NO_DEEPEST_PS |
  2168. NVME_QUIRK_MEDIUM_PRIO_SQ },
  2169. { PCI_VDEVICE(INTEL, 0x5845), /* Qemu emulated controller */
  2170. .driver_data = NVME_QUIRK_IDENTIFY_CNS, },
  2171. { PCI_DEVICE(0x1c58, 0x0003), /* HGST adapter */
  2172. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  2173. { PCI_DEVICE(0x1c58, 0x0023), /* WDC SN200 adapter */
  2174. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  2175. { PCI_DEVICE(0x1c5f, 0x0540), /* Memblaze Pblaze4 adapter */
  2176. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  2177. { PCI_DEVICE(0x144d, 0xa821), /* Samsung PM1725 */
  2178. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  2179. { PCI_DEVICE(0x144d, 0xa822), /* Samsung PM1725a */
  2180. .driver_data = NVME_QUIRK_DELAY_BEFORE_CHK_RDY, },
  2181. { PCI_DEVICE(0x1d1d, 0x1f1f), /* LighNVM qemu device */
  2182. .driver_data = NVME_QUIRK_LIGHTNVM, },
  2183. { PCI_DEVICE(0x1d1d, 0x2807), /* CNEX WL */
  2184. .driver_data = NVME_QUIRK_LIGHTNVM, },
  2185. { PCI_DEVICE(0x1d1d, 0x2601), /* CNEX Granby */
  2186. .driver_data = NVME_QUIRK_LIGHTNVM, },
  2187. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  2188. { PCI_DEVICE(0x2646, 0x2263), /* KINGSTON A2000 NVMe SSD */
  2189. .driver_data = NVME_QUIRK_NO_DEEPEST_PS, },
  2190. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
  2191. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2003) },
  2192. { 0, }
  2193. };
  2194. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  2195. static struct pci_driver nvme_driver = {
  2196. .name = "nvme",
  2197. .id_table = nvme_id_table,
  2198. .probe = nvme_probe,
  2199. .remove = nvme_remove,
  2200. .shutdown = nvme_shutdown,
  2201. .driver = {
  2202. .pm = &nvme_dev_pm_ops,
  2203. },
  2204. .sriov_configure = nvme_pci_sriov_configure,
  2205. .err_handler = &nvme_err_handler,
  2206. };
  2207. static int __init nvme_init(void)
  2208. {
  2209. return pci_register_driver(&nvme_driver);
  2210. }
  2211. static void __exit nvme_exit(void)
  2212. {
  2213. pci_unregister_driver(&nvme_driver);
  2214. _nvme_check_size();
  2215. }
  2216. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  2217. MODULE_LICENSE("GPL");
  2218. MODULE_VERSION("1.0");
  2219. module_init(nvme_init);
  2220. module_exit(nvme_exit);