pci.c 59 KB

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