pci.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555
  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/bitops.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/blk-mq.h>
  17. #include <linux/cpu.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/genhd.h>
  22. #include <linux/hdreg.h>
  23. #include <linux/idr.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/kdev_t.h>
  28. #include <linux/kthread.h>
  29. #include <linux/kernel.h>
  30. #include <linux/list_sort.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/pci.h>
  35. #include <linux/poison.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/sched.h>
  38. #include <linux/slab.h>
  39. #include <linux/t10-pi.h>
  40. #include <linux/types.h>
  41. #include <linux/pr.h>
  42. #include <scsi/sg.h>
  43. #include <linux/io-64-nonatomic-lo-hi.h>
  44. #include <asm/unaligned.h>
  45. #include <uapi/linux/nvme_ioctl.h>
  46. #include "nvme.h"
  47. #define NVME_MINORS (1U << MINORBITS)
  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. #define ADMIN_TIMEOUT (admin_timeout * HZ)
  53. #define SHUTDOWN_TIMEOUT (shutdown_timeout * HZ)
  54. static unsigned char admin_timeout = 60;
  55. module_param(admin_timeout, byte, 0644);
  56. MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
  57. unsigned char nvme_io_timeout = 30;
  58. module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
  59. MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
  60. static unsigned char shutdown_timeout = 5;
  61. module_param(shutdown_timeout, byte, 0644);
  62. MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
  63. static int nvme_major;
  64. module_param(nvme_major, int, 0);
  65. static int nvme_char_major;
  66. module_param(nvme_char_major, int, 0);
  67. static int use_threaded_interrupts;
  68. module_param(use_threaded_interrupts, int, 0);
  69. static bool use_cmb_sqes = true;
  70. module_param(use_cmb_sqes, bool, 0644);
  71. MODULE_PARM_DESC(use_cmb_sqes, "use controller's memory buffer for I/O SQes");
  72. static DEFINE_SPINLOCK(dev_list_lock);
  73. static LIST_HEAD(dev_list);
  74. static struct task_struct *nvme_thread;
  75. static struct workqueue_struct *nvme_workq;
  76. static wait_queue_head_t nvme_kthread_wait;
  77. static struct class *nvme_class;
  78. static int __nvme_reset(struct nvme_dev *dev);
  79. static int nvme_reset(struct nvme_dev *dev);
  80. static void nvme_process_cq(struct nvme_queue *nvmeq);
  81. static void nvme_dead_ctrl(struct nvme_dev *dev);
  82. struct async_cmd_info {
  83. struct kthread_work work;
  84. struct kthread_worker *worker;
  85. struct request *req;
  86. u32 result;
  87. int status;
  88. void *ctx;
  89. };
  90. /*
  91. * An NVM Express queue. Each device has at least two (one for admin
  92. * commands and one for I/O commands).
  93. */
  94. struct nvme_queue {
  95. struct device *q_dmadev;
  96. struct nvme_dev *dev;
  97. char irqname[24]; /* nvme4294967295-65535\0 */
  98. spinlock_t q_lock;
  99. struct nvme_command *sq_cmds;
  100. struct nvme_command __iomem *sq_cmds_io;
  101. volatile struct nvme_completion *cqes;
  102. struct blk_mq_tags **tags;
  103. dma_addr_t sq_dma_addr;
  104. dma_addr_t cq_dma_addr;
  105. u32 __iomem *q_db;
  106. u16 q_depth;
  107. s16 cq_vector;
  108. u16 sq_head;
  109. u16 sq_tail;
  110. u16 cq_head;
  111. u16 qid;
  112. u8 cq_phase;
  113. u8 cqe_seen;
  114. struct async_cmd_info cmdinfo;
  115. };
  116. /*
  117. * Check we didin't inadvertently grow the command struct
  118. */
  119. static inline void _nvme_check_size(void)
  120. {
  121. BUILD_BUG_ON(sizeof(struct nvme_rw_command) != 64);
  122. BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64);
  123. BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64);
  124. BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64);
  125. BUILD_BUG_ON(sizeof(struct nvme_features) != 64);
  126. BUILD_BUG_ON(sizeof(struct nvme_format_cmd) != 64);
  127. BUILD_BUG_ON(sizeof(struct nvme_abort_cmd) != 64);
  128. BUILD_BUG_ON(sizeof(struct nvme_command) != 64);
  129. BUILD_BUG_ON(sizeof(struct nvme_id_ctrl) != 4096);
  130. BUILD_BUG_ON(sizeof(struct nvme_id_ns) != 4096);
  131. BUILD_BUG_ON(sizeof(struct nvme_lba_range_type) != 64);
  132. BUILD_BUG_ON(sizeof(struct nvme_smart_log) != 512);
  133. }
  134. typedef void (*nvme_completion_fn)(struct nvme_queue *, void *,
  135. struct nvme_completion *);
  136. struct nvme_cmd_info {
  137. nvme_completion_fn fn;
  138. void *ctx;
  139. int aborted;
  140. struct nvme_queue *nvmeq;
  141. struct nvme_iod iod[0];
  142. };
  143. /*
  144. * Max size of iod being embedded in the request payload
  145. */
  146. #define NVME_INT_PAGES 2
  147. #define NVME_INT_BYTES(dev) (NVME_INT_PAGES * (dev)->page_size)
  148. #define NVME_INT_MASK 0x01
  149. /*
  150. * Will slightly overestimate the number of pages needed. This is OK
  151. * as it only leads to a small amount of wasted memory for the lifetime of
  152. * the I/O.
  153. */
  154. static int nvme_npages(unsigned size, struct nvme_dev *dev)
  155. {
  156. unsigned nprps = DIV_ROUND_UP(size + dev->page_size, dev->page_size);
  157. return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
  158. }
  159. static unsigned int nvme_cmd_size(struct nvme_dev *dev)
  160. {
  161. unsigned int ret = sizeof(struct nvme_cmd_info);
  162. ret += sizeof(struct nvme_iod);
  163. ret += sizeof(__le64 *) * nvme_npages(NVME_INT_BYTES(dev), dev);
  164. ret += sizeof(struct scatterlist) * NVME_INT_PAGES;
  165. return ret;
  166. }
  167. static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  168. unsigned int hctx_idx)
  169. {
  170. struct nvme_dev *dev = data;
  171. struct nvme_queue *nvmeq = dev->queues[0];
  172. WARN_ON(hctx_idx != 0);
  173. WARN_ON(dev->admin_tagset.tags[0] != hctx->tags);
  174. WARN_ON(nvmeq->tags);
  175. hctx->driver_data = nvmeq;
  176. nvmeq->tags = &dev->admin_tagset.tags[0];
  177. return 0;
  178. }
  179. static void nvme_admin_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
  180. {
  181. struct nvme_queue *nvmeq = hctx->driver_data;
  182. nvmeq->tags = NULL;
  183. }
  184. static int nvme_admin_init_request(void *data, struct request *req,
  185. unsigned int hctx_idx, unsigned int rq_idx,
  186. unsigned int numa_node)
  187. {
  188. struct nvme_dev *dev = data;
  189. struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
  190. struct nvme_queue *nvmeq = dev->queues[0];
  191. BUG_ON(!nvmeq);
  192. cmd->nvmeq = nvmeq;
  193. return 0;
  194. }
  195. static int nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  196. unsigned int hctx_idx)
  197. {
  198. struct nvme_dev *dev = data;
  199. struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
  200. if (!nvmeq->tags)
  201. nvmeq->tags = &dev->tagset.tags[hctx_idx];
  202. WARN_ON(dev->tagset.tags[hctx_idx] != hctx->tags);
  203. hctx->driver_data = nvmeq;
  204. return 0;
  205. }
  206. static int nvme_init_request(void *data, struct request *req,
  207. unsigned int hctx_idx, unsigned int rq_idx,
  208. unsigned int numa_node)
  209. {
  210. struct nvme_dev *dev = data;
  211. struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
  212. struct nvme_queue *nvmeq = dev->queues[hctx_idx + 1];
  213. BUG_ON(!nvmeq);
  214. cmd->nvmeq = nvmeq;
  215. return 0;
  216. }
  217. static void nvme_set_info(struct nvme_cmd_info *cmd, void *ctx,
  218. nvme_completion_fn handler)
  219. {
  220. cmd->fn = handler;
  221. cmd->ctx = ctx;
  222. cmd->aborted = 0;
  223. blk_mq_start_request(blk_mq_rq_from_pdu(cmd));
  224. }
  225. static void *iod_get_private(struct nvme_iod *iod)
  226. {
  227. return (void *) (iod->private & ~0x1UL);
  228. }
  229. /*
  230. * If bit 0 is set, the iod is embedded in the request payload.
  231. */
  232. static bool iod_should_kfree(struct nvme_iod *iod)
  233. {
  234. return (iod->private & NVME_INT_MASK) == 0;
  235. }
  236. /* Special values must be less than 0x1000 */
  237. #define CMD_CTX_BASE ((void *)POISON_POINTER_DELTA)
  238. #define CMD_CTX_CANCELLED (0x30C + CMD_CTX_BASE)
  239. #define CMD_CTX_COMPLETED (0x310 + CMD_CTX_BASE)
  240. #define CMD_CTX_INVALID (0x314 + CMD_CTX_BASE)
  241. static void special_completion(struct nvme_queue *nvmeq, void *ctx,
  242. struct nvme_completion *cqe)
  243. {
  244. if (ctx == CMD_CTX_CANCELLED)
  245. return;
  246. if (ctx == CMD_CTX_COMPLETED) {
  247. dev_warn(nvmeq->q_dmadev,
  248. "completed id %d twice on queue %d\n",
  249. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  250. return;
  251. }
  252. if (ctx == CMD_CTX_INVALID) {
  253. dev_warn(nvmeq->q_dmadev,
  254. "invalid id %d completed on queue %d\n",
  255. cqe->command_id, le16_to_cpup(&cqe->sq_id));
  256. return;
  257. }
  258. dev_warn(nvmeq->q_dmadev, "Unknown special completion %p\n", ctx);
  259. }
  260. static void *cancel_cmd_info(struct nvme_cmd_info *cmd, nvme_completion_fn *fn)
  261. {
  262. void *ctx;
  263. if (fn)
  264. *fn = cmd->fn;
  265. ctx = cmd->ctx;
  266. cmd->fn = special_completion;
  267. cmd->ctx = CMD_CTX_CANCELLED;
  268. return ctx;
  269. }
  270. static void async_req_completion(struct nvme_queue *nvmeq, void *ctx,
  271. struct nvme_completion *cqe)
  272. {
  273. u32 result = le32_to_cpup(&cqe->result);
  274. u16 status = le16_to_cpup(&cqe->status) >> 1;
  275. if (status == NVME_SC_SUCCESS || status == NVME_SC_ABORT_REQ)
  276. ++nvmeq->dev->event_limit;
  277. if (status != NVME_SC_SUCCESS)
  278. return;
  279. switch (result & 0xff07) {
  280. case NVME_AER_NOTICE_NS_CHANGED:
  281. dev_info(nvmeq->q_dmadev, "rescanning\n");
  282. schedule_work(&nvmeq->dev->scan_work);
  283. default:
  284. dev_warn(nvmeq->q_dmadev, "async event result %08x\n", result);
  285. }
  286. }
  287. static void abort_completion(struct nvme_queue *nvmeq, void *ctx,
  288. struct nvme_completion *cqe)
  289. {
  290. struct request *req = ctx;
  291. u16 status = le16_to_cpup(&cqe->status) >> 1;
  292. u32 result = le32_to_cpup(&cqe->result);
  293. blk_mq_free_request(req);
  294. dev_warn(nvmeq->q_dmadev, "Abort status:%x result:%x", status, result);
  295. ++nvmeq->dev->abort_limit;
  296. }
  297. static void async_completion(struct nvme_queue *nvmeq, void *ctx,
  298. struct nvme_completion *cqe)
  299. {
  300. struct async_cmd_info *cmdinfo = ctx;
  301. cmdinfo->result = le32_to_cpup(&cqe->result);
  302. cmdinfo->status = le16_to_cpup(&cqe->status) >> 1;
  303. blk_mq_free_request(cmdinfo->req);
  304. queue_kthread_work(cmdinfo->worker, &cmdinfo->work);
  305. }
  306. static inline struct nvme_cmd_info *get_cmd_from_tag(struct nvme_queue *nvmeq,
  307. unsigned int tag)
  308. {
  309. struct request *req = blk_mq_tag_to_rq(*nvmeq->tags, tag);
  310. return blk_mq_rq_to_pdu(req);
  311. }
  312. /*
  313. * Called with local interrupts disabled and the q_lock held. May not sleep.
  314. */
  315. static void *nvme_finish_cmd(struct nvme_queue *nvmeq, int tag,
  316. nvme_completion_fn *fn)
  317. {
  318. struct nvme_cmd_info *cmd = get_cmd_from_tag(nvmeq, tag);
  319. void *ctx;
  320. if (tag >= nvmeq->q_depth) {
  321. *fn = special_completion;
  322. return CMD_CTX_INVALID;
  323. }
  324. if (fn)
  325. *fn = cmd->fn;
  326. ctx = cmd->ctx;
  327. cmd->fn = special_completion;
  328. cmd->ctx = CMD_CTX_COMPLETED;
  329. return ctx;
  330. }
  331. /**
  332. * nvme_submit_cmd() - Copy a command into a queue and ring the doorbell
  333. * @nvmeq: The queue to use
  334. * @cmd: The command to send
  335. *
  336. * Safe to use from interrupt context
  337. */
  338. static void __nvme_submit_cmd(struct nvme_queue *nvmeq,
  339. struct nvme_command *cmd)
  340. {
  341. u16 tail = nvmeq->sq_tail;
  342. if (nvmeq->sq_cmds_io)
  343. memcpy_toio(&nvmeq->sq_cmds_io[tail], cmd, sizeof(*cmd));
  344. else
  345. memcpy(&nvmeq->sq_cmds[tail], cmd, sizeof(*cmd));
  346. if (++tail == nvmeq->q_depth)
  347. tail = 0;
  348. writel(tail, nvmeq->q_db);
  349. nvmeq->sq_tail = tail;
  350. }
  351. static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd)
  352. {
  353. unsigned long flags;
  354. spin_lock_irqsave(&nvmeq->q_lock, flags);
  355. __nvme_submit_cmd(nvmeq, cmd);
  356. spin_unlock_irqrestore(&nvmeq->q_lock, flags);
  357. }
  358. static __le64 **iod_list(struct nvme_iod *iod)
  359. {
  360. return ((void *)iod) + iod->offset;
  361. }
  362. static inline void iod_init(struct nvme_iod *iod, unsigned nbytes,
  363. unsigned nseg, unsigned long private)
  364. {
  365. iod->private = private;
  366. iod->offset = offsetof(struct nvme_iod, sg[nseg]);
  367. iod->npages = -1;
  368. iod->length = nbytes;
  369. iod->nents = 0;
  370. }
  371. static struct nvme_iod *
  372. __nvme_alloc_iod(unsigned nseg, unsigned bytes, struct nvme_dev *dev,
  373. unsigned long priv, gfp_t gfp)
  374. {
  375. struct nvme_iod *iod = kmalloc(sizeof(struct nvme_iod) +
  376. sizeof(__le64 *) * nvme_npages(bytes, dev) +
  377. sizeof(struct scatterlist) * nseg, gfp);
  378. if (iod)
  379. iod_init(iod, bytes, nseg, priv);
  380. return iod;
  381. }
  382. static struct nvme_iod *nvme_alloc_iod(struct request *rq, struct nvme_dev *dev,
  383. gfp_t gfp)
  384. {
  385. unsigned size = !(rq->cmd_flags & REQ_DISCARD) ? blk_rq_bytes(rq) :
  386. sizeof(struct nvme_dsm_range);
  387. struct nvme_iod *iod;
  388. if (rq->nr_phys_segments <= NVME_INT_PAGES &&
  389. size <= NVME_INT_BYTES(dev)) {
  390. struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(rq);
  391. iod = cmd->iod;
  392. iod_init(iod, size, rq->nr_phys_segments,
  393. (unsigned long) rq | NVME_INT_MASK);
  394. return iod;
  395. }
  396. return __nvme_alloc_iod(rq->nr_phys_segments, size, dev,
  397. (unsigned long) rq, gfp);
  398. }
  399. static void nvme_free_iod(struct nvme_dev *dev, struct nvme_iod *iod)
  400. {
  401. const int last_prp = dev->page_size / 8 - 1;
  402. int i;
  403. __le64 **list = iod_list(iod);
  404. dma_addr_t prp_dma = iod->first_dma;
  405. if (iod->npages == 0)
  406. dma_pool_free(dev->prp_small_pool, list[0], prp_dma);
  407. for (i = 0; i < iod->npages; i++) {
  408. __le64 *prp_list = list[i];
  409. dma_addr_t next_prp_dma = le64_to_cpu(prp_list[last_prp]);
  410. dma_pool_free(dev->prp_page_pool, prp_list, prp_dma);
  411. prp_dma = next_prp_dma;
  412. }
  413. if (iod_should_kfree(iod))
  414. kfree(iod);
  415. }
  416. static int nvme_error_status(u16 status)
  417. {
  418. switch (status & 0x7ff) {
  419. case NVME_SC_SUCCESS:
  420. return 0;
  421. case NVME_SC_CAP_EXCEEDED:
  422. return -ENOSPC;
  423. default:
  424. return -EIO;
  425. }
  426. }
  427. #ifdef CONFIG_BLK_DEV_INTEGRITY
  428. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  429. {
  430. if (be32_to_cpu(pi->ref_tag) == v)
  431. pi->ref_tag = cpu_to_be32(p);
  432. }
  433. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  434. {
  435. if (be32_to_cpu(pi->ref_tag) == p)
  436. pi->ref_tag = cpu_to_be32(v);
  437. }
  438. /**
  439. * nvme_dif_remap - remaps ref tags to bip seed and physical lba
  440. *
  441. * The virtual start sector is the one that was originally submitted by the
  442. * block layer. Due to partitioning, MD/DM cloning, etc. the actual physical
  443. * start sector may be different. Remap protection information to match the
  444. * physical LBA on writes, and back to the original seed on reads.
  445. *
  446. * Type 0 and 3 do not have a ref tag, so no remapping required.
  447. */
  448. static void nvme_dif_remap(struct request *req,
  449. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  450. {
  451. struct nvme_ns *ns = req->rq_disk->private_data;
  452. struct bio_integrity_payload *bip;
  453. struct t10_pi_tuple *pi;
  454. void *p, *pmap;
  455. u32 i, nlb, ts, phys, virt;
  456. if (!ns->pi_type || ns->pi_type == NVME_NS_DPS_PI_TYPE3)
  457. return;
  458. bip = bio_integrity(req->bio);
  459. if (!bip)
  460. return;
  461. pmap = kmap_atomic(bip->bip_vec->bv_page) + bip->bip_vec->bv_offset;
  462. p = pmap;
  463. virt = bip_get_seed(bip);
  464. phys = nvme_block_nr(ns, blk_rq_pos(req));
  465. nlb = (blk_rq_bytes(req) >> ns->lba_shift);
  466. ts = ns->disk->queue->integrity.tuple_size;
  467. for (i = 0; i < nlb; i++, virt++, phys++) {
  468. pi = (struct t10_pi_tuple *)p;
  469. dif_swap(phys, virt, pi);
  470. p += ts;
  471. }
  472. kunmap_atomic(pmap);
  473. }
  474. static void nvme_init_integrity(struct nvme_ns *ns)
  475. {
  476. struct blk_integrity integrity;
  477. switch (ns->pi_type) {
  478. case NVME_NS_DPS_PI_TYPE3:
  479. integrity.profile = &t10_pi_type3_crc;
  480. break;
  481. case NVME_NS_DPS_PI_TYPE1:
  482. case NVME_NS_DPS_PI_TYPE2:
  483. integrity.profile = &t10_pi_type1_crc;
  484. break;
  485. default:
  486. integrity.profile = NULL;
  487. break;
  488. }
  489. integrity.tuple_size = ns->ms;
  490. blk_integrity_register(ns->disk, &integrity);
  491. blk_queue_max_integrity_segments(ns->queue, 1);
  492. }
  493. #else /* CONFIG_BLK_DEV_INTEGRITY */
  494. static void nvme_dif_remap(struct request *req,
  495. void (*dif_swap)(u32 p, u32 v, struct t10_pi_tuple *pi))
  496. {
  497. }
  498. static void nvme_dif_prep(u32 p, u32 v, struct t10_pi_tuple *pi)
  499. {
  500. }
  501. static void nvme_dif_complete(u32 p, u32 v, struct t10_pi_tuple *pi)
  502. {
  503. }
  504. static void nvme_init_integrity(struct nvme_ns *ns)
  505. {
  506. }
  507. #endif
  508. static void req_completion(struct nvme_queue *nvmeq, void *ctx,
  509. struct nvme_completion *cqe)
  510. {
  511. struct nvme_iod *iod = ctx;
  512. struct request *req = iod_get_private(iod);
  513. struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
  514. u16 status = le16_to_cpup(&cqe->status) >> 1;
  515. bool requeue = false;
  516. int error = 0;
  517. if (unlikely(status)) {
  518. if (!(status & NVME_SC_DNR || blk_noretry_request(req))
  519. && (jiffies - req->start_time) < req->timeout) {
  520. unsigned long flags;
  521. requeue = true;
  522. blk_mq_requeue_request(req);
  523. spin_lock_irqsave(req->q->queue_lock, flags);
  524. if (!blk_queue_stopped(req->q))
  525. blk_mq_kick_requeue_list(req->q);
  526. spin_unlock_irqrestore(req->q->queue_lock, flags);
  527. goto release_iod;
  528. }
  529. if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
  530. if (cmd_rq->ctx == CMD_CTX_CANCELLED)
  531. error = -EINTR;
  532. else
  533. error = status;
  534. } else {
  535. error = nvme_error_status(status);
  536. }
  537. }
  538. if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
  539. u32 result = le32_to_cpup(&cqe->result);
  540. req->special = (void *)(uintptr_t)result;
  541. }
  542. if (cmd_rq->aborted)
  543. dev_warn(nvmeq->dev->dev,
  544. "completing aborted command with status:%04x\n",
  545. error);
  546. release_iod:
  547. if (iod->nents) {
  548. dma_unmap_sg(nvmeq->dev->dev, iod->sg, iod->nents,
  549. rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  550. if (blk_integrity_rq(req)) {
  551. if (!rq_data_dir(req))
  552. nvme_dif_remap(req, nvme_dif_complete);
  553. dma_unmap_sg(nvmeq->dev->dev, iod->meta_sg, 1,
  554. rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  555. }
  556. }
  557. nvme_free_iod(nvmeq->dev, iod);
  558. if (likely(!requeue))
  559. blk_mq_complete_request(req, error);
  560. }
  561. /* length is in bytes. gfp flags indicates whether we may sleep. */
  562. static int nvme_setup_prps(struct nvme_dev *dev, struct nvme_iod *iod,
  563. int total_len, gfp_t gfp)
  564. {
  565. struct dma_pool *pool;
  566. int length = total_len;
  567. struct scatterlist *sg = iod->sg;
  568. int dma_len = sg_dma_len(sg);
  569. u64 dma_addr = sg_dma_address(sg);
  570. u32 page_size = dev->page_size;
  571. int offset = dma_addr & (page_size - 1);
  572. __le64 *prp_list;
  573. __le64 **list = iod_list(iod);
  574. dma_addr_t prp_dma;
  575. int nprps, i;
  576. length -= (page_size - offset);
  577. if (length <= 0)
  578. return total_len;
  579. dma_len -= (page_size - offset);
  580. if (dma_len) {
  581. dma_addr += (page_size - offset);
  582. } else {
  583. sg = sg_next(sg);
  584. dma_addr = sg_dma_address(sg);
  585. dma_len = sg_dma_len(sg);
  586. }
  587. if (length <= page_size) {
  588. iod->first_dma = dma_addr;
  589. return total_len;
  590. }
  591. nprps = DIV_ROUND_UP(length, page_size);
  592. if (nprps <= (256 / 8)) {
  593. pool = dev->prp_small_pool;
  594. iod->npages = 0;
  595. } else {
  596. pool = dev->prp_page_pool;
  597. iod->npages = 1;
  598. }
  599. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  600. if (!prp_list) {
  601. iod->first_dma = dma_addr;
  602. iod->npages = -1;
  603. return (total_len - length) + page_size;
  604. }
  605. list[0] = prp_list;
  606. iod->first_dma = prp_dma;
  607. i = 0;
  608. for (;;) {
  609. if (i == page_size >> 3) {
  610. __le64 *old_prp_list = prp_list;
  611. prp_list = dma_pool_alloc(pool, gfp, &prp_dma);
  612. if (!prp_list)
  613. return total_len - length;
  614. list[iod->npages++] = prp_list;
  615. prp_list[0] = old_prp_list[i - 1];
  616. old_prp_list[i - 1] = cpu_to_le64(prp_dma);
  617. i = 1;
  618. }
  619. prp_list[i++] = cpu_to_le64(dma_addr);
  620. dma_len -= page_size;
  621. dma_addr += page_size;
  622. length -= page_size;
  623. if (length <= 0)
  624. break;
  625. if (dma_len > 0)
  626. continue;
  627. BUG_ON(dma_len < 0);
  628. sg = sg_next(sg);
  629. dma_addr = sg_dma_address(sg);
  630. dma_len = sg_dma_len(sg);
  631. }
  632. return total_len;
  633. }
  634. static void nvme_submit_priv(struct nvme_queue *nvmeq, struct request *req,
  635. struct nvme_iod *iod)
  636. {
  637. struct nvme_command cmnd;
  638. memcpy(&cmnd, req->cmd, sizeof(cmnd));
  639. cmnd.rw.command_id = req->tag;
  640. if (req->nr_phys_segments) {
  641. cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  642. cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
  643. }
  644. __nvme_submit_cmd(nvmeq, &cmnd);
  645. }
  646. /*
  647. * We reuse the small pool to allocate the 16-byte range here as it is not
  648. * worth having a special pool for these or additional cases to handle freeing
  649. * the iod.
  650. */
  651. static void nvme_submit_discard(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  652. struct request *req, struct nvme_iod *iod)
  653. {
  654. struct nvme_dsm_range *range =
  655. (struct nvme_dsm_range *)iod_list(iod)[0];
  656. struct nvme_command cmnd;
  657. range->cattr = cpu_to_le32(0);
  658. range->nlb = cpu_to_le32(blk_rq_bytes(req) >> ns->lba_shift);
  659. range->slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
  660. memset(&cmnd, 0, sizeof(cmnd));
  661. cmnd.dsm.opcode = nvme_cmd_dsm;
  662. cmnd.dsm.command_id = req->tag;
  663. cmnd.dsm.nsid = cpu_to_le32(ns->ns_id);
  664. cmnd.dsm.prp1 = cpu_to_le64(iod->first_dma);
  665. cmnd.dsm.nr = 0;
  666. cmnd.dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  667. __nvme_submit_cmd(nvmeq, &cmnd);
  668. }
  669. static void nvme_submit_flush(struct nvme_queue *nvmeq, struct nvme_ns *ns,
  670. int cmdid)
  671. {
  672. struct nvme_command cmnd;
  673. memset(&cmnd, 0, sizeof(cmnd));
  674. cmnd.common.opcode = nvme_cmd_flush;
  675. cmnd.common.command_id = cmdid;
  676. cmnd.common.nsid = cpu_to_le32(ns->ns_id);
  677. __nvme_submit_cmd(nvmeq, &cmnd);
  678. }
  679. static int nvme_submit_iod(struct nvme_queue *nvmeq, struct nvme_iod *iod,
  680. struct nvme_ns *ns)
  681. {
  682. struct request *req = iod_get_private(iod);
  683. struct nvme_command cmnd;
  684. u16 control = 0;
  685. u32 dsmgmt = 0;
  686. if (req->cmd_flags & REQ_FUA)
  687. control |= NVME_RW_FUA;
  688. if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  689. control |= NVME_RW_LR;
  690. if (req->cmd_flags & REQ_RAHEAD)
  691. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  692. memset(&cmnd, 0, sizeof(cmnd));
  693. cmnd.rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
  694. cmnd.rw.command_id = req->tag;
  695. cmnd.rw.nsid = cpu_to_le32(ns->ns_id);
  696. cmnd.rw.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
  697. cmnd.rw.prp2 = cpu_to_le64(iod->first_dma);
  698. cmnd.rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
  699. cmnd.rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
  700. if (ns->ms) {
  701. switch (ns->pi_type) {
  702. case NVME_NS_DPS_PI_TYPE3:
  703. control |= NVME_RW_PRINFO_PRCHK_GUARD;
  704. break;
  705. case NVME_NS_DPS_PI_TYPE1:
  706. case NVME_NS_DPS_PI_TYPE2:
  707. control |= NVME_RW_PRINFO_PRCHK_GUARD |
  708. NVME_RW_PRINFO_PRCHK_REF;
  709. cmnd.rw.reftag = cpu_to_le32(
  710. nvme_block_nr(ns, blk_rq_pos(req)));
  711. break;
  712. }
  713. if (blk_integrity_rq(req))
  714. cmnd.rw.metadata =
  715. cpu_to_le64(sg_dma_address(iod->meta_sg));
  716. else
  717. control |= NVME_RW_PRINFO_PRACT;
  718. }
  719. cmnd.rw.control = cpu_to_le16(control);
  720. cmnd.rw.dsmgmt = cpu_to_le32(dsmgmt);
  721. __nvme_submit_cmd(nvmeq, &cmnd);
  722. return 0;
  723. }
  724. /*
  725. * NOTE: ns is NULL when called on the admin queue.
  726. */
  727. static int nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
  728. const struct blk_mq_queue_data *bd)
  729. {
  730. struct nvme_ns *ns = hctx->queue->queuedata;
  731. struct nvme_queue *nvmeq = hctx->driver_data;
  732. struct nvme_dev *dev = nvmeq->dev;
  733. struct request *req = bd->rq;
  734. struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
  735. struct nvme_iod *iod;
  736. enum dma_data_direction dma_dir;
  737. /*
  738. * If formated with metadata, require the block layer provide a buffer
  739. * unless this namespace is formated such that the metadata can be
  740. * stripped/generated by the controller with PRACT=1.
  741. */
  742. if (ns && ns->ms && !blk_integrity_rq(req)) {
  743. if (!(ns->pi_type && ns->ms == 8) &&
  744. req->cmd_type != REQ_TYPE_DRV_PRIV) {
  745. blk_mq_complete_request(req, -EFAULT);
  746. return BLK_MQ_RQ_QUEUE_OK;
  747. }
  748. }
  749. iod = nvme_alloc_iod(req, dev, GFP_ATOMIC);
  750. if (!iod)
  751. return BLK_MQ_RQ_QUEUE_BUSY;
  752. if (req->cmd_flags & REQ_DISCARD) {
  753. void *range;
  754. /*
  755. * We reuse the small pool to allocate the 16-byte range here
  756. * as it is not worth having a special pool for these or
  757. * additional cases to handle freeing the iod.
  758. */
  759. range = dma_pool_alloc(dev->prp_small_pool, GFP_ATOMIC,
  760. &iod->first_dma);
  761. if (!range)
  762. goto retry_cmd;
  763. iod_list(iod)[0] = (__le64 *)range;
  764. iod->npages = 0;
  765. } else if (req->nr_phys_segments) {
  766. dma_dir = rq_data_dir(req) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  767. sg_init_table(iod->sg, req->nr_phys_segments);
  768. iod->nents = blk_rq_map_sg(req->q, req, iod->sg);
  769. if (!iod->nents)
  770. goto error_cmd;
  771. if (!dma_map_sg(nvmeq->q_dmadev, iod->sg, iod->nents, dma_dir))
  772. goto retry_cmd;
  773. if (blk_rq_bytes(req) !=
  774. nvme_setup_prps(dev, iod, blk_rq_bytes(req), GFP_ATOMIC)) {
  775. dma_unmap_sg(dev->dev, iod->sg, iod->nents, dma_dir);
  776. goto retry_cmd;
  777. }
  778. if (blk_integrity_rq(req)) {
  779. if (blk_rq_count_integrity_sg(req->q, req->bio) != 1) {
  780. dma_unmap_sg(dev->dev, iod->sg, iod->nents,
  781. dma_dir);
  782. goto error_cmd;
  783. }
  784. sg_init_table(iod->meta_sg, 1);
  785. if (blk_rq_map_integrity_sg(
  786. req->q, req->bio, iod->meta_sg) != 1) {
  787. dma_unmap_sg(dev->dev, iod->sg, iod->nents,
  788. dma_dir);
  789. goto error_cmd;
  790. }
  791. if (rq_data_dir(req))
  792. nvme_dif_remap(req, nvme_dif_prep);
  793. if (!dma_map_sg(nvmeq->q_dmadev, iod->meta_sg, 1, dma_dir)) {
  794. dma_unmap_sg(dev->dev, iod->sg, iod->nents,
  795. dma_dir);
  796. goto error_cmd;
  797. }
  798. }
  799. }
  800. nvme_set_info(cmd, iod, req_completion);
  801. spin_lock_irq(&nvmeq->q_lock);
  802. if (req->cmd_type == REQ_TYPE_DRV_PRIV)
  803. nvme_submit_priv(nvmeq, req, iod);
  804. else if (req->cmd_flags & REQ_DISCARD)
  805. nvme_submit_discard(nvmeq, ns, req, iod);
  806. else if (req->cmd_flags & REQ_FLUSH)
  807. nvme_submit_flush(nvmeq, ns, req->tag);
  808. else
  809. nvme_submit_iod(nvmeq, iod, ns);
  810. nvme_process_cq(nvmeq);
  811. spin_unlock_irq(&nvmeq->q_lock);
  812. return BLK_MQ_RQ_QUEUE_OK;
  813. error_cmd:
  814. nvme_free_iod(dev, iod);
  815. return BLK_MQ_RQ_QUEUE_ERROR;
  816. retry_cmd:
  817. nvme_free_iod(dev, iod);
  818. return BLK_MQ_RQ_QUEUE_BUSY;
  819. }
  820. static void __nvme_process_cq(struct nvme_queue *nvmeq, unsigned int *tag)
  821. {
  822. u16 head, phase;
  823. head = nvmeq->cq_head;
  824. phase = nvmeq->cq_phase;
  825. for (;;) {
  826. void *ctx;
  827. nvme_completion_fn fn;
  828. struct nvme_completion cqe = nvmeq->cqes[head];
  829. if ((le16_to_cpu(cqe.status) & 1) != phase)
  830. break;
  831. nvmeq->sq_head = le16_to_cpu(cqe.sq_head);
  832. if (++head == nvmeq->q_depth) {
  833. head = 0;
  834. phase = !phase;
  835. }
  836. if (tag && *tag == cqe.command_id)
  837. *tag = -1;
  838. ctx = nvme_finish_cmd(nvmeq, cqe.command_id, &fn);
  839. fn(nvmeq, ctx, &cqe);
  840. }
  841. /* If the controller ignores the cq head doorbell and continuously
  842. * writes to the queue, it is theoretically possible to wrap around
  843. * the queue twice and mistakenly return IRQ_NONE. Linux only
  844. * requires that 0.1% of your interrupts are handled, so this isn't
  845. * a big problem.
  846. */
  847. if (head == nvmeq->cq_head && phase == nvmeq->cq_phase)
  848. return;
  849. if (likely(nvmeq->cq_vector >= 0))
  850. writel(head, nvmeq->q_db + nvmeq->dev->db_stride);
  851. nvmeq->cq_head = head;
  852. nvmeq->cq_phase = phase;
  853. nvmeq->cqe_seen = 1;
  854. }
  855. static void nvme_process_cq(struct nvme_queue *nvmeq)
  856. {
  857. __nvme_process_cq(nvmeq, NULL);
  858. }
  859. static irqreturn_t nvme_irq(int irq, void *data)
  860. {
  861. irqreturn_t result;
  862. struct nvme_queue *nvmeq = data;
  863. spin_lock(&nvmeq->q_lock);
  864. nvme_process_cq(nvmeq);
  865. result = nvmeq->cqe_seen ? IRQ_HANDLED : IRQ_NONE;
  866. nvmeq->cqe_seen = 0;
  867. spin_unlock(&nvmeq->q_lock);
  868. return result;
  869. }
  870. static irqreturn_t nvme_irq_check(int irq, void *data)
  871. {
  872. struct nvme_queue *nvmeq = data;
  873. struct nvme_completion cqe = nvmeq->cqes[nvmeq->cq_head];
  874. if ((le16_to_cpu(cqe.status) & 1) != nvmeq->cq_phase)
  875. return IRQ_NONE;
  876. return IRQ_WAKE_THREAD;
  877. }
  878. static int nvme_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
  879. {
  880. struct nvme_queue *nvmeq = hctx->driver_data;
  881. if ((le16_to_cpu(nvmeq->cqes[nvmeq->cq_head].status) & 1) ==
  882. nvmeq->cq_phase) {
  883. spin_lock_irq(&nvmeq->q_lock);
  884. __nvme_process_cq(nvmeq, &tag);
  885. spin_unlock_irq(&nvmeq->q_lock);
  886. if (tag == -1)
  887. return 1;
  888. }
  889. return 0;
  890. }
  891. /*
  892. * Returns 0 on success. If the result is negative, it's a Linux error code;
  893. * if the result is positive, it's an NVM Express status code
  894. */
  895. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  896. void *buffer, void __user *ubuffer, unsigned bufflen,
  897. u32 *result, unsigned timeout)
  898. {
  899. bool write = cmd->common.opcode & 1;
  900. struct bio *bio = NULL;
  901. struct request *req;
  902. int ret;
  903. req = blk_mq_alloc_request(q, write, GFP_KERNEL, false);
  904. if (IS_ERR(req))
  905. return PTR_ERR(req);
  906. req->cmd_type = REQ_TYPE_DRV_PRIV;
  907. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  908. req->__data_len = 0;
  909. req->__sector = (sector_t) -1;
  910. req->bio = req->biotail = NULL;
  911. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  912. req->cmd = (unsigned char *)cmd;
  913. req->cmd_len = sizeof(struct nvme_command);
  914. req->special = (void *)0;
  915. if (buffer && bufflen) {
  916. ret = blk_rq_map_kern(q, req, buffer, bufflen,
  917. __GFP_DIRECT_RECLAIM);
  918. if (ret)
  919. goto out;
  920. } else if (ubuffer && bufflen) {
  921. ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
  922. __GFP_DIRECT_RECLAIM);
  923. if (ret)
  924. goto out;
  925. bio = req->bio;
  926. }
  927. blk_execute_rq(req->q, NULL, req, 0);
  928. if (bio)
  929. blk_rq_unmap_user(bio);
  930. if (result)
  931. *result = (u32)(uintptr_t)req->special;
  932. ret = req->errors;
  933. out:
  934. blk_mq_free_request(req);
  935. return ret;
  936. }
  937. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  938. void *buffer, unsigned bufflen)
  939. {
  940. return __nvme_submit_sync_cmd(q, cmd, buffer, NULL, bufflen, NULL, 0);
  941. }
  942. static int nvme_submit_async_admin_req(struct nvme_dev *dev)
  943. {
  944. struct nvme_queue *nvmeq = dev->queues[0];
  945. struct nvme_command c;
  946. struct nvme_cmd_info *cmd_info;
  947. struct request *req;
  948. req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC, true);
  949. if (IS_ERR(req))
  950. return PTR_ERR(req);
  951. req->cmd_flags |= REQ_NO_TIMEOUT;
  952. cmd_info = blk_mq_rq_to_pdu(req);
  953. nvme_set_info(cmd_info, NULL, async_req_completion);
  954. memset(&c, 0, sizeof(c));
  955. c.common.opcode = nvme_admin_async_event;
  956. c.common.command_id = req->tag;
  957. blk_mq_free_request(req);
  958. __nvme_submit_cmd(nvmeq, &c);
  959. return 0;
  960. }
  961. static int nvme_submit_admin_async_cmd(struct nvme_dev *dev,
  962. struct nvme_command *cmd,
  963. struct async_cmd_info *cmdinfo, unsigned timeout)
  964. {
  965. struct nvme_queue *nvmeq = dev->queues[0];
  966. struct request *req;
  967. struct nvme_cmd_info *cmd_rq;
  968. req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_KERNEL, false);
  969. if (IS_ERR(req))
  970. return PTR_ERR(req);
  971. req->timeout = timeout;
  972. cmd_rq = blk_mq_rq_to_pdu(req);
  973. cmdinfo->req = req;
  974. nvme_set_info(cmd_rq, cmdinfo, async_completion);
  975. cmdinfo->status = -EINTR;
  976. cmd->common.command_id = req->tag;
  977. nvme_submit_cmd(nvmeq, cmd);
  978. return 0;
  979. }
  980. static int adapter_delete_queue(struct nvme_dev *dev, u8 opcode, u16 id)
  981. {
  982. struct nvme_command c;
  983. memset(&c, 0, sizeof(c));
  984. c.delete_queue.opcode = opcode;
  985. c.delete_queue.qid = cpu_to_le16(id);
  986. return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
  987. }
  988. static int adapter_alloc_cq(struct nvme_dev *dev, u16 qid,
  989. struct nvme_queue *nvmeq)
  990. {
  991. struct nvme_command c;
  992. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED;
  993. /*
  994. * Note: we (ab)use the fact the the prp fields survive if no data
  995. * is attached to the request.
  996. */
  997. memset(&c, 0, sizeof(c));
  998. c.create_cq.opcode = nvme_admin_create_cq;
  999. c.create_cq.prp1 = cpu_to_le64(nvmeq->cq_dma_addr);
  1000. c.create_cq.cqid = cpu_to_le16(qid);
  1001. c.create_cq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  1002. c.create_cq.cq_flags = cpu_to_le16(flags);
  1003. c.create_cq.irq_vector = cpu_to_le16(nvmeq->cq_vector);
  1004. return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
  1005. }
  1006. static int adapter_alloc_sq(struct nvme_dev *dev, u16 qid,
  1007. struct nvme_queue *nvmeq)
  1008. {
  1009. struct nvme_command c;
  1010. int flags = NVME_QUEUE_PHYS_CONTIG | NVME_SQ_PRIO_MEDIUM;
  1011. /*
  1012. * Note: we (ab)use the fact the the prp fields survive if no data
  1013. * is attached to the request.
  1014. */
  1015. memset(&c, 0, sizeof(c));
  1016. c.create_sq.opcode = nvme_admin_create_sq;
  1017. c.create_sq.prp1 = cpu_to_le64(nvmeq->sq_dma_addr);
  1018. c.create_sq.sqid = cpu_to_le16(qid);
  1019. c.create_sq.qsize = cpu_to_le16(nvmeq->q_depth - 1);
  1020. c.create_sq.sq_flags = cpu_to_le16(flags);
  1021. c.create_sq.cqid = cpu_to_le16(qid);
  1022. return nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0);
  1023. }
  1024. static int adapter_delete_cq(struct nvme_dev *dev, u16 cqid)
  1025. {
  1026. return adapter_delete_queue(dev, nvme_admin_delete_cq, cqid);
  1027. }
  1028. static int adapter_delete_sq(struct nvme_dev *dev, u16 sqid)
  1029. {
  1030. return adapter_delete_queue(dev, nvme_admin_delete_sq, sqid);
  1031. }
  1032. int nvme_identify_ctrl(struct nvme_dev *dev, struct nvme_id_ctrl **id)
  1033. {
  1034. struct nvme_command c = { };
  1035. int error;
  1036. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  1037. c.identify.opcode = nvme_admin_identify;
  1038. c.identify.cns = cpu_to_le32(1);
  1039. *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
  1040. if (!*id)
  1041. return -ENOMEM;
  1042. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  1043. sizeof(struct nvme_id_ctrl));
  1044. if (error)
  1045. kfree(*id);
  1046. return error;
  1047. }
  1048. int nvme_identify_ns(struct nvme_dev *dev, unsigned nsid,
  1049. struct nvme_id_ns **id)
  1050. {
  1051. struct nvme_command c = { };
  1052. int error;
  1053. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  1054. c.identify.opcode = nvme_admin_identify,
  1055. c.identify.nsid = cpu_to_le32(nsid),
  1056. *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
  1057. if (!*id)
  1058. return -ENOMEM;
  1059. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  1060. sizeof(struct nvme_id_ns));
  1061. if (error)
  1062. kfree(*id);
  1063. return error;
  1064. }
  1065. int nvme_get_features(struct nvme_dev *dev, unsigned fid, unsigned nsid,
  1066. dma_addr_t dma_addr, u32 *result)
  1067. {
  1068. struct nvme_command c;
  1069. memset(&c, 0, sizeof(c));
  1070. c.features.opcode = nvme_admin_get_features;
  1071. c.features.nsid = cpu_to_le32(nsid);
  1072. c.features.prp1 = cpu_to_le64(dma_addr);
  1073. c.features.fid = cpu_to_le32(fid);
  1074. return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
  1075. result, 0);
  1076. }
  1077. int nvme_set_features(struct nvme_dev *dev, unsigned fid, unsigned dword11,
  1078. dma_addr_t dma_addr, u32 *result)
  1079. {
  1080. struct nvme_command c;
  1081. memset(&c, 0, sizeof(c));
  1082. c.features.opcode = nvme_admin_set_features;
  1083. c.features.prp1 = cpu_to_le64(dma_addr);
  1084. c.features.fid = cpu_to_le32(fid);
  1085. c.features.dword11 = cpu_to_le32(dword11);
  1086. return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, NULL, 0,
  1087. result, 0);
  1088. }
  1089. int nvme_get_log_page(struct nvme_dev *dev, struct nvme_smart_log **log)
  1090. {
  1091. struct nvme_command c = { };
  1092. int error;
  1093. c.common.opcode = nvme_admin_get_log_page,
  1094. c.common.nsid = cpu_to_le32(0xFFFFFFFF),
  1095. c.common.cdw10[0] = cpu_to_le32(
  1096. (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
  1097. NVME_LOG_SMART),
  1098. *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
  1099. if (!*log)
  1100. return -ENOMEM;
  1101. error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
  1102. sizeof(struct nvme_smart_log));
  1103. if (error)
  1104. kfree(*log);
  1105. return error;
  1106. }
  1107. /**
  1108. * nvme_abort_req - Attempt aborting a request
  1109. *
  1110. * Schedule controller reset if the command was already aborted once before and
  1111. * still hasn't been returned to the driver, or if this is the admin queue.
  1112. */
  1113. static void nvme_abort_req(struct request *req)
  1114. {
  1115. struct nvme_cmd_info *cmd_rq = blk_mq_rq_to_pdu(req);
  1116. struct nvme_queue *nvmeq = cmd_rq->nvmeq;
  1117. struct nvme_dev *dev = nvmeq->dev;
  1118. struct request *abort_req;
  1119. struct nvme_cmd_info *abort_cmd;
  1120. struct nvme_command cmd;
  1121. if (!nvmeq->qid || cmd_rq->aborted) {
  1122. spin_lock(&dev_list_lock);
  1123. if (!__nvme_reset(dev)) {
  1124. dev_warn(dev->dev,
  1125. "I/O %d QID %d timeout, reset controller\n",
  1126. req->tag, nvmeq->qid);
  1127. }
  1128. spin_unlock(&dev_list_lock);
  1129. return;
  1130. }
  1131. if (!dev->abort_limit)
  1132. return;
  1133. abort_req = blk_mq_alloc_request(dev->admin_q, WRITE, GFP_ATOMIC,
  1134. false);
  1135. if (IS_ERR(abort_req))
  1136. return;
  1137. abort_cmd = blk_mq_rq_to_pdu(abort_req);
  1138. nvme_set_info(abort_cmd, abort_req, abort_completion);
  1139. memset(&cmd, 0, sizeof(cmd));
  1140. cmd.abort.opcode = nvme_admin_abort_cmd;
  1141. cmd.abort.cid = req->tag;
  1142. cmd.abort.sqid = cpu_to_le16(nvmeq->qid);
  1143. cmd.abort.command_id = abort_req->tag;
  1144. --dev->abort_limit;
  1145. cmd_rq->aborted = 1;
  1146. dev_warn(nvmeq->q_dmadev, "Aborting I/O %d QID %d\n", req->tag,
  1147. nvmeq->qid);
  1148. nvme_submit_cmd(dev->queues[0], &cmd);
  1149. }
  1150. static void nvme_cancel_queue_ios(struct request *req, void *data, bool reserved)
  1151. {
  1152. struct nvme_queue *nvmeq = data;
  1153. void *ctx;
  1154. nvme_completion_fn fn;
  1155. struct nvme_cmd_info *cmd;
  1156. struct nvme_completion cqe;
  1157. if (!blk_mq_request_started(req))
  1158. return;
  1159. cmd = blk_mq_rq_to_pdu(req);
  1160. if (cmd->ctx == CMD_CTX_CANCELLED)
  1161. return;
  1162. if (blk_queue_dying(req->q))
  1163. cqe.status = cpu_to_le16((NVME_SC_ABORT_REQ | NVME_SC_DNR) << 1);
  1164. else
  1165. cqe.status = cpu_to_le16(NVME_SC_ABORT_REQ << 1);
  1166. dev_warn(nvmeq->q_dmadev, "Cancelling I/O %d QID %d\n",
  1167. req->tag, nvmeq->qid);
  1168. ctx = cancel_cmd_info(cmd, &fn);
  1169. fn(nvmeq, ctx, &cqe);
  1170. }
  1171. static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
  1172. {
  1173. struct nvme_cmd_info *cmd = blk_mq_rq_to_pdu(req);
  1174. struct nvme_queue *nvmeq = cmd->nvmeq;
  1175. dev_warn(nvmeq->q_dmadev, "Timeout I/O %d QID %d\n", req->tag,
  1176. nvmeq->qid);
  1177. spin_lock_irq(&nvmeq->q_lock);
  1178. nvme_abort_req(req);
  1179. spin_unlock_irq(&nvmeq->q_lock);
  1180. /*
  1181. * The aborted req will be completed on receiving the abort req.
  1182. * We enable the timer again. If hit twice, it'll cause a device reset,
  1183. * as the device then is in a faulty state.
  1184. */
  1185. return BLK_EH_RESET_TIMER;
  1186. }
  1187. static void nvme_free_queue(struct nvme_queue *nvmeq)
  1188. {
  1189. dma_free_coherent(nvmeq->q_dmadev, CQ_SIZE(nvmeq->q_depth),
  1190. (void *)nvmeq->cqes, nvmeq->cq_dma_addr);
  1191. if (nvmeq->sq_cmds)
  1192. dma_free_coherent(nvmeq->q_dmadev, SQ_SIZE(nvmeq->q_depth),
  1193. nvmeq->sq_cmds, nvmeq->sq_dma_addr);
  1194. kfree(nvmeq);
  1195. }
  1196. static void nvme_free_queues(struct nvme_dev *dev, int lowest)
  1197. {
  1198. int i;
  1199. for (i = dev->queue_count - 1; i >= lowest; i--) {
  1200. struct nvme_queue *nvmeq = dev->queues[i];
  1201. dev->queue_count--;
  1202. dev->queues[i] = NULL;
  1203. nvme_free_queue(nvmeq);
  1204. }
  1205. }
  1206. /**
  1207. * nvme_suspend_queue - put queue into suspended state
  1208. * @nvmeq - queue to suspend
  1209. */
  1210. static int nvme_suspend_queue(struct nvme_queue *nvmeq)
  1211. {
  1212. int vector;
  1213. spin_lock_irq(&nvmeq->q_lock);
  1214. if (nvmeq->cq_vector == -1) {
  1215. spin_unlock_irq(&nvmeq->q_lock);
  1216. return 1;
  1217. }
  1218. vector = nvmeq->dev->entry[nvmeq->cq_vector].vector;
  1219. nvmeq->dev->online_queues--;
  1220. nvmeq->cq_vector = -1;
  1221. spin_unlock_irq(&nvmeq->q_lock);
  1222. if (!nvmeq->qid && nvmeq->dev->admin_q)
  1223. blk_mq_freeze_queue_start(nvmeq->dev->admin_q);
  1224. irq_set_affinity_hint(vector, NULL);
  1225. free_irq(vector, nvmeq);
  1226. return 0;
  1227. }
  1228. static void nvme_clear_queue(struct nvme_queue *nvmeq)
  1229. {
  1230. spin_lock_irq(&nvmeq->q_lock);
  1231. if (nvmeq->tags && *nvmeq->tags)
  1232. blk_mq_all_tag_busy_iter(*nvmeq->tags, nvme_cancel_queue_ios, nvmeq);
  1233. spin_unlock_irq(&nvmeq->q_lock);
  1234. }
  1235. static void nvme_disable_queue(struct nvme_dev *dev, int qid)
  1236. {
  1237. struct nvme_queue *nvmeq = dev->queues[qid];
  1238. if (!nvmeq)
  1239. return;
  1240. if (nvme_suspend_queue(nvmeq))
  1241. return;
  1242. /* Don't tell the adapter to delete the admin queue.
  1243. * Don't tell a removed adapter to delete IO queues. */
  1244. if (qid && readl(&dev->bar->csts) != -1) {
  1245. adapter_delete_sq(dev, qid);
  1246. adapter_delete_cq(dev, qid);
  1247. }
  1248. spin_lock_irq(&nvmeq->q_lock);
  1249. nvme_process_cq(nvmeq);
  1250. spin_unlock_irq(&nvmeq->q_lock);
  1251. }
  1252. static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
  1253. int entry_size)
  1254. {
  1255. int q_depth = dev->q_depth;
  1256. unsigned q_size_aligned = roundup(q_depth * entry_size, dev->page_size);
  1257. if (q_size_aligned * nr_io_queues > dev->cmb_size) {
  1258. u64 mem_per_q = div_u64(dev->cmb_size, nr_io_queues);
  1259. mem_per_q = round_down(mem_per_q, dev->page_size);
  1260. q_depth = div_u64(mem_per_q, entry_size);
  1261. /*
  1262. * Ensure the reduced q_depth is above some threshold where it
  1263. * would be better to map queues in system memory with the
  1264. * original depth
  1265. */
  1266. if (q_depth < 64)
  1267. return -ENOMEM;
  1268. }
  1269. return q_depth;
  1270. }
  1271. static int nvme_alloc_sq_cmds(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  1272. int qid, int depth)
  1273. {
  1274. if (qid && dev->cmb && use_cmb_sqes && NVME_CMB_SQS(dev->cmbsz)) {
  1275. unsigned offset = (qid - 1) *
  1276. roundup(SQ_SIZE(depth), dev->page_size);
  1277. nvmeq->sq_dma_addr = dev->cmb_dma_addr + offset;
  1278. nvmeq->sq_cmds_io = dev->cmb + offset;
  1279. } else {
  1280. nvmeq->sq_cmds = dma_alloc_coherent(dev->dev, SQ_SIZE(depth),
  1281. &nvmeq->sq_dma_addr, GFP_KERNEL);
  1282. if (!nvmeq->sq_cmds)
  1283. return -ENOMEM;
  1284. }
  1285. return 0;
  1286. }
  1287. static struct nvme_queue *nvme_alloc_queue(struct nvme_dev *dev, int qid,
  1288. int depth)
  1289. {
  1290. struct nvme_queue *nvmeq = kzalloc(sizeof(*nvmeq), GFP_KERNEL);
  1291. if (!nvmeq)
  1292. return NULL;
  1293. nvmeq->cqes = dma_zalloc_coherent(dev->dev, CQ_SIZE(depth),
  1294. &nvmeq->cq_dma_addr, GFP_KERNEL);
  1295. if (!nvmeq->cqes)
  1296. goto free_nvmeq;
  1297. if (nvme_alloc_sq_cmds(dev, nvmeq, qid, depth))
  1298. goto free_cqdma;
  1299. nvmeq->q_dmadev = dev->dev;
  1300. nvmeq->dev = dev;
  1301. snprintf(nvmeq->irqname, sizeof(nvmeq->irqname), "nvme%dq%d",
  1302. dev->instance, qid);
  1303. spin_lock_init(&nvmeq->q_lock);
  1304. nvmeq->cq_head = 0;
  1305. nvmeq->cq_phase = 1;
  1306. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1307. nvmeq->q_depth = depth;
  1308. nvmeq->qid = qid;
  1309. nvmeq->cq_vector = -1;
  1310. dev->queues[qid] = nvmeq;
  1311. /* make sure queue descriptor is set before queue count, for kthread */
  1312. mb();
  1313. dev->queue_count++;
  1314. return nvmeq;
  1315. free_cqdma:
  1316. dma_free_coherent(dev->dev, CQ_SIZE(depth), (void *)nvmeq->cqes,
  1317. nvmeq->cq_dma_addr);
  1318. free_nvmeq:
  1319. kfree(nvmeq);
  1320. return NULL;
  1321. }
  1322. static int queue_request_irq(struct nvme_dev *dev, struct nvme_queue *nvmeq,
  1323. const char *name)
  1324. {
  1325. if (use_threaded_interrupts)
  1326. return request_threaded_irq(dev->entry[nvmeq->cq_vector].vector,
  1327. nvme_irq_check, nvme_irq, IRQF_SHARED,
  1328. name, nvmeq);
  1329. return request_irq(dev->entry[nvmeq->cq_vector].vector, nvme_irq,
  1330. IRQF_SHARED, name, nvmeq);
  1331. }
  1332. static void nvme_init_queue(struct nvme_queue *nvmeq, u16 qid)
  1333. {
  1334. struct nvme_dev *dev = nvmeq->dev;
  1335. spin_lock_irq(&nvmeq->q_lock);
  1336. nvmeq->sq_tail = 0;
  1337. nvmeq->cq_head = 0;
  1338. nvmeq->cq_phase = 1;
  1339. nvmeq->q_db = &dev->dbs[qid * 2 * dev->db_stride];
  1340. memset((void *)nvmeq->cqes, 0, CQ_SIZE(nvmeq->q_depth));
  1341. dev->online_queues++;
  1342. spin_unlock_irq(&nvmeq->q_lock);
  1343. }
  1344. static int nvme_create_queue(struct nvme_queue *nvmeq, int qid)
  1345. {
  1346. struct nvme_dev *dev = nvmeq->dev;
  1347. int result;
  1348. nvmeq->cq_vector = qid - 1;
  1349. result = adapter_alloc_cq(dev, qid, nvmeq);
  1350. if (result < 0)
  1351. goto release_vector;
  1352. result = adapter_alloc_sq(dev, qid, nvmeq);
  1353. if (result < 0)
  1354. goto release_cq;
  1355. nvme_init_queue(nvmeq, qid);
  1356. result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
  1357. if (result < 0)
  1358. goto release_sq;
  1359. return result;
  1360. release_sq:
  1361. dev->online_queues--;
  1362. adapter_delete_sq(dev, qid);
  1363. release_cq:
  1364. adapter_delete_cq(dev, qid);
  1365. release_vector:
  1366. nvmeq->cq_vector = -1;
  1367. return result;
  1368. }
  1369. static int nvme_wait_ready(struct nvme_dev *dev, u64 cap, bool enabled)
  1370. {
  1371. unsigned long timeout;
  1372. u32 bit = enabled ? NVME_CSTS_RDY : 0;
  1373. timeout = ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  1374. while ((readl(&dev->bar->csts) & NVME_CSTS_RDY) != bit) {
  1375. msleep(100);
  1376. if (fatal_signal_pending(current))
  1377. return -EINTR;
  1378. if (time_after(jiffies, timeout)) {
  1379. dev_err(dev->dev,
  1380. "Device not ready; aborting %s\n", enabled ?
  1381. "initialisation" : "reset");
  1382. return -ENODEV;
  1383. }
  1384. }
  1385. return 0;
  1386. }
  1387. /*
  1388. * If the device has been passed off to us in an enabled state, just clear
  1389. * the enabled bit. The spec says we should set the 'shutdown notification
  1390. * bits', but doing so may cause the device to complete commands to the
  1391. * admin queue ... and we don't know what memory that might be pointing at!
  1392. */
  1393. static int nvme_disable_ctrl(struct nvme_dev *dev, u64 cap)
  1394. {
  1395. struct pci_dev *pdev = to_pci_dev(dev->dev);
  1396. dev->ctrl_config &= ~NVME_CC_SHN_MASK;
  1397. dev->ctrl_config &= ~NVME_CC_ENABLE;
  1398. writel(dev->ctrl_config, &dev->bar->cc);
  1399. if (pdev->vendor == 0x1c58 && pdev->device == 0x0003)
  1400. msleep(NVME_QUIRK_DELAY_AMOUNT);
  1401. return nvme_wait_ready(dev, cap, false);
  1402. }
  1403. static int nvme_enable_ctrl(struct nvme_dev *dev, u64 cap)
  1404. {
  1405. dev->ctrl_config &= ~NVME_CC_SHN_MASK;
  1406. dev->ctrl_config |= NVME_CC_ENABLE;
  1407. writel(dev->ctrl_config, &dev->bar->cc);
  1408. return nvme_wait_ready(dev, cap, true);
  1409. }
  1410. static int nvme_shutdown_ctrl(struct nvme_dev *dev)
  1411. {
  1412. unsigned long timeout;
  1413. dev->ctrl_config &= ~NVME_CC_SHN_MASK;
  1414. dev->ctrl_config |= NVME_CC_SHN_NORMAL;
  1415. writel(dev->ctrl_config, &dev->bar->cc);
  1416. timeout = SHUTDOWN_TIMEOUT + jiffies;
  1417. while ((readl(&dev->bar->csts) & NVME_CSTS_SHST_MASK) !=
  1418. NVME_CSTS_SHST_CMPLT) {
  1419. msleep(100);
  1420. if (fatal_signal_pending(current))
  1421. return -EINTR;
  1422. if (time_after(jiffies, timeout)) {
  1423. dev_err(dev->dev,
  1424. "Device shutdown incomplete; abort shutdown\n");
  1425. return -ENODEV;
  1426. }
  1427. }
  1428. return 0;
  1429. }
  1430. static struct blk_mq_ops nvme_mq_admin_ops = {
  1431. .queue_rq = nvme_queue_rq,
  1432. .map_queue = blk_mq_map_queue,
  1433. .init_hctx = nvme_admin_init_hctx,
  1434. .exit_hctx = nvme_admin_exit_hctx,
  1435. .init_request = nvme_admin_init_request,
  1436. .timeout = nvme_timeout,
  1437. };
  1438. static struct blk_mq_ops nvme_mq_ops = {
  1439. .queue_rq = nvme_queue_rq,
  1440. .map_queue = blk_mq_map_queue,
  1441. .init_hctx = nvme_init_hctx,
  1442. .init_request = nvme_init_request,
  1443. .timeout = nvme_timeout,
  1444. .poll = nvme_poll,
  1445. };
  1446. static void nvme_dev_remove_admin(struct nvme_dev *dev)
  1447. {
  1448. if (dev->admin_q && !blk_queue_dying(dev->admin_q)) {
  1449. blk_cleanup_queue(dev->admin_q);
  1450. blk_mq_free_tag_set(&dev->admin_tagset);
  1451. }
  1452. }
  1453. static int nvme_alloc_admin_tags(struct nvme_dev *dev)
  1454. {
  1455. if (!dev->admin_q) {
  1456. dev->admin_tagset.ops = &nvme_mq_admin_ops;
  1457. dev->admin_tagset.nr_hw_queues = 1;
  1458. dev->admin_tagset.queue_depth = NVME_AQ_DEPTH - 1;
  1459. dev->admin_tagset.reserved_tags = 1;
  1460. dev->admin_tagset.timeout = ADMIN_TIMEOUT;
  1461. dev->admin_tagset.numa_node = dev_to_node(dev->dev);
  1462. dev->admin_tagset.cmd_size = nvme_cmd_size(dev);
  1463. dev->admin_tagset.driver_data = dev;
  1464. if (blk_mq_alloc_tag_set(&dev->admin_tagset))
  1465. return -ENOMEM;
  1466. dev->admin_q = blk_mq_init_queue(&dev->admin_tagset);
  1467. if (IS_ERR(dev->admin_q)) {
  1468. blk_mq_free_tag_set(&dev->admin_tagset);
  1469. return -ENOMEM;
  1470. }
  1471. if (!blk_get_queue(dev->admin_q)) {
  1472. nvme_dev_remove_admin(dev);
  1473. dev->admin_q = NULL;
  1474. return -ENODEV;
  1475. }
  1476. } else
  1477. blk_mq_unfreeze_queue(dev->admin_q);
  1478. return 0;
  1479. }
  1480. static int nvme_configure_admin_queue(struct nvme_dev *dev)
  1481. {
  1482. int result;
  1483. u32 aqa;
  1484. u64 cap = lo_hi_readq(&dev->bar->cap);
  1485. struct nvme_queue *nvmeq;
  1486. /*
  1487. * default to a 4K page size, with the intention to update this
  1488. * path in the future to accomodate architectures with differing
  1489. * kernel and IO page sizes.
  1490. */
  1491. unsigned page_shift = 12;
  1492. unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12;
  1493. if (page_shift < dev_page_min) {
  1494. dev_err(dev->dev,
  1495. "Minimum device page size (%u) too large for "
  1496. "host (%u)\n", 1 << dev_page_min,
  1497. 1 << page_shift);
  1498. return -ENODEV;
  1499. }
  1500. dev->subsystem = readl(&dev->bar->vs) >= NVME_VS(1, 1) ?
  1501. NVME_CAP_NSSRC(cap) : 0;
  1502. if (dev->subsystem && (readl(&dev->bar->csts) & NVME_CSTS_NSSRO))
  1503. writel(NVME_CSTS_NSSRO, &dev->bar->csts);
  1504. result = nvme_disable_ctrl(dev, cap);
  1505. if (result < 0)
  1506. return result;
  1507. nvmeq = dev->queues[0];
  1508. if (!nvmeq) {
  1509. nvmeq = nvme_alloc_queue(dev, 0, NVME_AQ_DEPTH);
  1510. if (!nvmeq)
  1511. return -ENOMEM;
  1512. }
  1513. aqa = nvmeq->q_depth - 1;
  1514. aqa |= aqa << 16;
  1515. dev->page_size = 1 << page_shift;
  1516. dev->ctrl_config = NVME_CC_CSS_NVM;
  1517. dev->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
  1518. dev->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
  1519. dev->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  1520. writel(aqa, &dev->bar->aqa);
  1521. lo_hi_writeq(nvmeq->sq_dma_addr, &dev->bar->asq);
  1522. lo_hi_writeq(nvmeq->cq_dma_addr, &dev->bar->acq);
  1523. result = nvme_enable_ctrl(dev, cap);
  1524. if (result)
  1525. goto free_nvmeq;
  1526. nvmeq->cq_vector = 0;
  1527. nvme_init_queue(nvmeq, 0);
  1528. result = queue_request_irq(dev, nvmeq, nvmeq->irqname);
  1529. if (result) {
  1530. nvmeq->cq_vector = -1;
  1531. goto free_nvmeq;
  1532. }
  1533. return result;
  1534. free_nvmeq:
  1535. nvme_free_queues(dev, 0);
  1536. return result;
  1537. }
  1538. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  1539. {
  1540. struct nvme_dev *dev = ns->dev;
  1541. struct nvme_user_io io;
  1542. struct nvme_command c;
  1543. unsigned length, meta_len;
  1544. int status, write;
  1545. dma_addr_t meta_dma = 0;
  1546. void *meta = NULL;
  1547. void __user *metadata;
  1548. if (copy_from_user(&io, uio, sizeof(io)))
  1549. return -EFAULT;
  1550. switch (io.opcode) {
  1551. case nvme_cmd_write:
  1552. case nvme_cmd_read:
  1553. case nvme_cmd_compare:
  1554. break;
  1555. default:
  1556. return -EINVAL;
  1557. }
  1558. length = (io.nblocks + 1) << ns->lba_shift;
  1559. meta_len = (io.nblocks + 1) * ns->ms;
  1560. metadata = (void __user *)(uintptr_t)io.metadata;
  1561. write = io.opcode & 1;
  1562. if (ns->ext) {
  1563. length += meta_len;
  1564. meta_len = 0;
  1565. }
  1566. if (meta_len) {
  1567. if (((io.metadata & 3) || !io.metadata) && !ns->ext)
  1568. return -EINVAL;
  1569. meta = dma_alloc_coherent(dev->dev, meta_len,
  1570. &meta_dma, GFP_KERNEL);
  1571. if (!meta) {
  1572. status = -ENOMEM;
  1573. goto unmap;
  1574. }
  1575. if (write) {
  1576. if (copy_from_user(meta, metadata, meta_len)) {
  1577. status = -EFAULT;
  1578. goto unmap;
  1579. }
  1580. }
  1581. }
  1582. memset(&c, 0, sizeof(c));
  1583. c.rw.opcode = io.opcode;
  1584. c.rw.flags = io.flags;
  1585. c.rw.nsid = cpu_to_le32(ns->ns_id);
  1586. c.rw.slba = cpu_to_le64(io.slba);
  1587. c.rw.length = cpu_to_le16(io.nblocks);
  1588. c.rw.control = cpu_to_le16(io.control);
  1589. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  1590. c.rw.reftag = cpu_to_le32(io.reftag);
  1591. c.rw.apptag = cpu_to_le16(io.apptag);
  1592. c.rw.appmask = cpu_to_le16(io.appmask);
  1593. c.rw.metadata = cpu_to_le64(meta_dma);
  1594. status = __nvme_submit_sync_cmd(ns->queue, &c, NULL,
  1595. (void __user *)(uintptr_t)io.addr, length, NULL, 0);
  1596. unmap:
  1597. if (meta) {
  1598. if (status == NVME_SC_SUCCESS && !write) {
  1599. if (copy_to_user(metadata, meta, meta_len))
  1600. status = -EFAULT;
  1601. }
  1602. dma_free_coherent(dev->dev, meta_len, meta, meta_dma);
  1603. }
  1604. return status;
  1605. }
  1606. static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
  1607. struct nvme_passthru_cmd __user *ucmd)
  1608. {
  1609. struct nvme_passthru_cmd cmd;
  1610. struct nvme_command c;
  1611. unsigned timeout = 0;
  1612. int status;
  1613. if (!capable(CAP_SYS_ADMIN))
  1614. return -EACCES;
  1615. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  1616. return -EFAULT;
  1617. memset(&c, 0, sizeof(c));
  1618. c.common.opcode = cmd.opcode;
  1619. c.common.flags = cmd.flags;
  1620. c.common.nsid = cpu_to_le32(cmd.nsid);
  1621. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  1622. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  1623. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  1624. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  1625. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  1626. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  1627. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  1628. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  1629. if (cmd.timeout_ms)
  1630. timeout = msecs_to_jiffies(cmd.timeout_ms);
  1631. status = __nvme_submit_sync_cmd(ns ? ns->queue : dev->admin_q, &c,
  1632. NULL, (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
  1633. &cmd.result, timeout);
  1634. if (status >= 0) {
  1635. if (put_user(cmd.result, &ucmd->result))
  1636. return -EFAULT;
  1637. }
  1638. return status;
  1639. }
  1640. static int nvme_subsys_reset(struct nvme_dev *dev)
  1641. {
  1642. if (!dev->subsystem)
  1643. return -ENOTTY;
  1644. writel(0x4E564D65, &dev->bar->nssr); /* "NVMe" */
  1645. return 0;
  1646. }
  1647. static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
  1648. unsigned long arg)
  1649. {
  1650. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1651. switch (cmd) {
  1652. case NVME_IOCTL_ID:
  1653. force_successful_syscall_return();
  1654. return ns->ns_id;
  1655. case NVME_IOCTL_ADMIN_CMD:
  1656. return nvme_user_cmd(ns->dev, NULL, (void __user *)arg);
  1657. case NVME_IOCTL_IO_CMD:
  1658. return nvme_user_cmd(ns->dev, ns, (void __user *)arg);
  1659. case NVME_IOCTL_SUBMIT_IO:
  1660. return nvme_submit_io(ns, (void __user *)arg);
  1661. case SG_GET_VERSION_NUM:
  1662. return nvme_sg_get_version_num((void __user *)arg);
  1663. case SG_IO:
  1664. return nvme_sg_io(ns, (void __user *)arg);
  1665. default:
  1666. return -ENOTTY;
  1667. }
  1668. }
  1669. #ifdef CONFIG_COMPAT
  1670. static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1671. unsigned int cmd, unsigned long arg)
  1672. {
  1673. switch (cmd) {
  1674. case SG_IO:
  1675. return -ENOIOCTLCMD;
  1676. }
  1677. return nvme_ioctl(bdev, mode, cmd, arg);
  1678. }
  1679. #else
  1680. #define nvme_compat_ioctl NULL
  1681. #endif
  1682. static void nvme_free_dev(struct kref *kref);
  1683. static void nvme_free_ns(struct kref *kref)
  1684. {
  1685. struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
  1686. if (ns->type == NVME_NS_LIGHTNVM)
  1687. nvme_nvm_unregister(ns->queue, ns->disk->disk_name);
  1688. spin_lock(&dev_list_lock);
  1689. ns->disk->private_data = NULL;
  1690. spin_unlock(&dev_list_lock);
  1691. kref_put(&ns->dev->kref, nvme_free_dev);
  1692. put_disk(ns->disk);
  1693. kfree(ns);
  1694. }
  1695. static int nvme_open(struct block_device *bdev, fmode_t mode)
  1696. {
  1697. int ret = 0;
  1698. struct nvme_ns *ns;
  1699. spin_lock(&dev_list_lock);
  1700. ns = bdev->bd_disk->private_data;
  1701. if (!ns)
  1702. ret = -ENXIO;
  1703. else if (!kref_get_unless_zero(&ns->kref))
  1704. ret = -ENXIO;
  1705. spin_unlock(&dev_list_lock);
  1706. return ret;
  1707. }
  1708. static void nvme_release(struct gendisk *disk, fmode_t mode)
  1709. {
  1710. struct nvme_ns *ns = disk->private_data;
  1711. kref_put(&ns->kref, nvme_free_ns);
  1712. }
  1713. static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
  1714. {
  1715. /* some standard values */
  1716. geo->heads = 1 << 6;
  1717. geo->sectors = 1 << 5;
  1718. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  1719. return 0;
  1720. }
  1721. static void nvme_config_discard(struct nvme_ns *ns)
  1722. {
  1723. u32 logical_block_size = queue_logical_block_size(ns->queue);
  1724. ns->queue->limits.discard_zeroes_data = 0;
  1725. ns->queue->limits.discard_alignment = logical_block_size;
  1726. ns->queue->limits.discard_granularity = logical_block_size;
  1727. blk_queue_max_discard_sectors(ns->queue, 0xffffffff);
  1728. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
  1729. }
  1730. static int nvme_revalidate_disk(struct gendisk *disk)
  1731. {
  1732. struct nvme_ns *ns = disk->private_data;
  1733. struct nvme_dev *dev = ns->dev;
  1734. struct nvme_id_ns *id;
  1735. u8 lbaf, pi_type;
  1736. u16 old_ms;
  1737. unsigned short bs;
  1738. if (nvme_identify_ns(dev, ns->ns_id, &id)) {
  1739. dev_warn(dev->dev, "%s: Identify failure nvme%dn%d\n", __func__,
  1740. dev->instance, ns->ns_id);
  1741. return -ENODEV;
  1742. }
  1743. if (id->ncap == 0) {
  1744. kfree(id);
  1745. return -ENODEV;
  1746. }
  1747. if (nvme_nvm_ns_supported(ns, id) && ns->type != NVME_NS_LIGHTNVM) {
  1748. if (nvme_nvm_register(ns->queue, disk->disk_name)) {
  1749. dev_warn(dev->dev,
  1750. "%s: LightNVM init failure\n", __func__);
  1751. kfree(id);
  1752. return -ENODEV;
  1753. }
  1754. ns->type = NVME_NS_LIGHTNVM;
  1755. }
  1756. old_ms = ns->ms;
  1757. lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
  1758. ns->lba_shift = id->lbaf[lbaf].ds;
  1759. ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
  1760. ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
  1761. /*
  1762. * If identify namespace failed, use default 512 byte block size so
  1763. * block layer can use before failing read/write for 0 capacity.
  1764. */
  1765. if (ns->lba_shift == 0)
  1766. ns->lba_shift = 9;
  1767. bs = 1 << ns->lba_shift;
  1768. /* XXX: PI implementation requires metadata equal t10 pi tuple size */
  1769. pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
  1770. id->dps & NVME_NS_DPS_PI_MASK : 0;
  1771. blk_mq_freeze_queue(disk->queue);
  1772. if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
  1773. ns->ms != old_ms ||
  1774. bs != queue_logical_block_size(disk->queue) ||
  1775. (ns->ms && ns->ext)))
  1776. blk_integrity_unregister(disk);
  1777. ns->pi_type = pi_type;
  1778. blk_queue_logical_block_size(ns->queue, bs);
  1779. if (ns->ms && !ns->ext)
  1780. nvme_init_integrity(ns);
  1781. if ((ns->ms && !(ns->ms == 8 && ns->pi_type) &&
  1782. !blk_get_integrity(disk)) ||
  1783. ns->type == NVME_NS_LIGHTNVM)
  1784. set_capacity(disk, 0);
  1785. else
  1786. set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
  1787. if (dev->oncs & NVME_CTRL_ONCS_DSM)
  1788. nvme_config_discard(ns);
  1789. blk_mq_unfreeze_queue(disk->queue);
  1790. kfree(id);
  1791. return 0;
  1792. }
  1793. static char nvme_pr_type(enum pr_type type)
  1794. {
  1795. switch (type) {
  1796. case PR_WRITE_EXCLUSIVE:
  1797. return 1;
  1798. case PR_EXCLUSIVE_ACCESS:
  1799. return 2;
  1800. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  1801. return 3;
  1802. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  1803. return 4;
  1804. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  1805. return 5;
  1806. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  1807. return 6;
  1808. default:
  1809. return 0;
  1810. }
  1811. };
  1812. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  1813. u64 key, u64 sa_key, u8 op)
  1814. {
  1815. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1816. struct nvme_command c;
  1817. u8 data[16] = { 0, };
  1818. put_unaligned_le64(key, &data[0]);
  1819. put_unaligned_le64(sa_key, &data[8]);
  1820. memset(&c, 0, sizeof(c));
  1821. c.common.opcode = op;
  1822. c.common.nsid = cpu_to_le32(ns->ns_id);
  1823. c.common.cdw10[0] = cpu_to_le32(cdw10);
  1824. return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
  1825. }
  1826. static int nvme_pr_register(struct block_device *bdev, u64 old,
  1827. u64 new, unsigned flags)
  1828. {
  1829. u32 cdw10;
  1830. if (flags & ~PR_FL_IGNORE_KEY)
  1831. return -EOPNOTSUPP;
  1832. cdw10 = old ? 2 : 0;
  1833. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  1834. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  1835. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  1836. }
  1837. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  1838. enum pr_type type, unsigned flags)
  1839. {
  1840. u32 cdw10;
  1841. if (flags & ~PR_FL_IGNORE_KEY)
  1842. return -EOPNOTSUPP;
  1843. cdw10 = nvme_pr_type(type) << 8;
  1844. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  1845. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  1846. }
  1847. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  1848. enum pr_type type, bool abort)
  1849. {
  1850. u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
  1851. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  1852. }
  1853. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  1854. {
  1855. u32 cdw10 = 1 | (key ? 1 << 3 : 0);
  1856. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
  1857. }
  1858. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  1859. {
  1860. u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
  1861. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  1862. }
  1863. static const struct pr_ops nvme_pr_ops = {
  1864. .pr_register = nvme_pr_register,
  1865. .pr_reserve = nvme_pr_reserve,
  1866. .pr_release = nvme_pr_release,
  1867. .pr_preempt = nvme_pr_preempt,
  1868. .pr_clear = nvme_pr_clear,
  1869. };
  1870. static const struct block_device_operations nvme_fops = {
  1871. .owner = THIS_MODULE,
  1872. .ioctl = nvme_ioctl,
  1873. .compat_ioctl = nvme_compat_ioctl,
  1874. .open = nvme_open,
  1875. .release = nvme_release,
  1876. .getgeo = nvme_getgeo,
  1877. .revalidate_disk= nvme_revalidate_disk,
  1878. .pr_ops = &nvme_pr_ops,
  1879. };
  1880. static int nvme_kthread(void *data)
  1881. {
  1882. struct nvme_dev *dev, *next;
  1883. while (!kthread_should_stop()) {
  1884. set_current_state(TASK_INTERRUPTIBLE);
  1885. spin_lock(&dev_list_lock);
  1886. list_for_each_entry_safe(dev, next, &dev_list, node) {
  1887. int i;
  1888. u32 csts = readl(&dev->bar->csts);
  1889. if ((dev->subsystem && (csts & NVME_CSTS_NSSRO)) ||
  1890. csts & NVME_CSTS_CFS) {
  1891. if (!__nvme_reset(dev)) {
  1892. dev_warn(dev->dev,
  1893. "Failed status: %x, reset controller\n",
  1894. readl(&dev->bar->csts));
  1895. }
  1896. continue;
  1897. }
  1898. for (i = 0; i < dev->queue_count; i++) {
  1899. struct nvme_queue *nvmeq = dev->queues[i];
  1900. if (!nvmeq)
  1901. continue;
  1902. spin_lock_irq(&nvmeq->q_lock);
  1903. nvme_process_cq(nvmeq);
  1904. while ((i == 0) && (dev->event_limit > 0)) {
  1905. if (nvme_submit_async_admin_req(dev))
  1906. break;
  1907. dev->event_limit--;
  1908. }
  1909. spin_unlock_irq(&nvmeq->q_lock);
  1910. }
  1911. }
  1912. spin_unlock(&dev_list_lock);
  1913. schedule_timeout(round_jiffies_relative(HZ));
  1914. }
  1915. return 0;
  1916. }
  1917. static void nvme_alloc_ns(struct nvme_dev *dev, unsigned nsid)
  1918. {
  1919. struct nvme_ns *ns;
  1920. struct gendisk *disk;
  1921. int node = dev_to_node(dev->dev);
  1922. ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
  1923. if (!ns)
  1924. return;
  1925. ns->queue = blk_mq_init_queue(&dev->tagset);
  1926. if (IS_ERR(ns->queue))
  1927. goto out_free_ns;
  1928. queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, ns->queue);
  1929. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  1930. ns->dev = dev;
  1931. ns->queue->queuedata = ns;
  1932. disk = alloc_disk_node(0, node);
  1933. if (!disk)
  1934. goto out_free_queue;
  1935. kref_init(&ns->kref);
  1936. ns->ns_id = nsid;
  1937. ns->disk = disk;
  1938. ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
  1939. list_add_tail(&ns->list, &dev->namespaces);
  1940. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  1941. if (dev->max_hw_sectors) {
  1942. blk_queue_max_hw_sectors(ns->queue, dev->max_hw_sectors);
  1943. blk_queue_max_segments(ns->queue,
  1944. (dev->max_hw_sectors / (dev->page_size >> 9)) + 1);
  1945. }
  1946. if (dev->stripe_size)
  1947. blk_queue_chunk_sectors(ns->queue, dev->stripe_size >> 9);
  1948. if (dev->vwc & NVME_CTRL_VWC_PRESENT)
  1949. blk_queue_flush(ns->queue, REQ_FLUSH | REQ_FUA);
  1950. blk_queue_virt_boundary(ns->queue, dev->page_size - 1);
  1951. disk->major = nvme_major;
  1952. disk->first_minor = 0;
  1953. disk->fops = &nvme_fops;
  1954. disk->private_data = ns;
  1955. disk->queue = ns->queue;
  1956. disk->driverfs_dev = dev->device;
  1957. disk->flags = GENHD_FL_EXT_DEVT;
  1958. sprintf(disk->disk_name, "nvme%dn%d", dev->instance, nsid);
  1959. /*
  1960. * Initialize capacity to 0 until we establish the namespace format and
  1961. * setup integrity extentions if necessary. The revalidate_disk after
  1962. * add_disk allows the driver to register with integrity if the format
  1963. * requires it.
  1964. */
  1965. set_capacity(disk, 0);
  1966. if (nvme_revalidate_disk(ns->disk))
  1967. goto out_free_disk;
  1968. kref_get(&dev->kref);
  1969. if (ns->type != NVME_NS_LIGHTNVM) {
  1970. add_disk(ns->disk);
  1971. if (ns->ms) {
  1972. struct block_device *bd = bdget_disk(ns->disk, 0);
  1973. if (!bd)
  1974. return;
  1975. if (blkdev_get(bd, FMODE_READ, NULL)) {
  1976. bdput(bd);
  1977. return;
  1978. }
  1979. blkdev_reread_part(bd);
  1980. blkdev_put(bd, FMODE_READ);
  1981. }
  1982. }
  1983. return;
  1984. out_free_disk:
  1985. kfree(disk);
  1986. list_del(&ns->list);
  1987. out_free_queue:
  1988. blk_cleanup_queue(ns->queue);
  1989. out_free_ns:
  1990. kfree(ns);
  1991. }
  1992. /*
  1993. * Create I/O queues. Failing to create an I/O queue is not an issue,
  1994. * we can continue with less than the desired amount of queues, and
  1995. * even a controller without I/O queues an still be used to issue
  1996. * admin commands. This might be useful to upgrade a buggy firmware
  1997. * for example.
  1998. */
  1999. static void nvme_create_io_queues(struct nvme_dev *dev)
  2000. {
  2001. unsigned i;
  2002. for (i = dev->queue_count; i <= dev->max_qid; i++)
  2003. if (!nvme_alloc_queue(dev, i, dev->q_depth))
  2004. break;
  2005. for (i = dev->online_queues; i <= dev->queue_count - 1; i++)
  2006. if (nvme_create_queue(dev->queues[i], i)) {
  2007. nvme_free_queues(dev, i);
  2008. break;
  2009. }
  2010. }
  2011. static int set_queue_count(struct nvme_dev *dev, int count)
  2012. {
  2013. int status;
  2014. u32 result;
  2015. u32 q_count = (count - 1) | ((count - 1) << 16);
  2016. status = nvme_set_features(dev, NVME_FEAT_NUM_QUEUES, q_count, 0,
  2017. &result);
  2018. if (status < 0)
  2019. return status;
  2020. if (status > 0) {
  2021. dev_err(dev->dev, "Could not set queue count (%d)\n", status);
  2022. return 0;
  2023. }
  2024. return min(result & 0xffff, result >> 16) + 1;
  2025. }
  2026. static void __iomem *nvme_map_cmb(struct nvme_dev *dev)
  2027. {
  2028. u64 szu, size, offset;
  2029. u32 cmbloc;
  2030. resource_size_t bar_size;
  2031. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2032. void __iomem *cmb;
  2033. dma_addr_t dma_addr;
  2034. if (!use_cmb_sqes)
  2035. return NULL;
  2036. dev->cmbsz = readl(&dev->bar->cmbsz);
  2037. if (!(NVME_CMB_SZ(dev->cmbsz)))
  2038. return NULL;
  2039. cmbloc = readl(&dev->bar->cmbloc);
  2040. szu = (u64)1 << (12 + 4 * NVME_CMB_SZU(dev->cmbsz));
  2041. size = szu * NVME_CMB_SZ(dev->cmbsz);
  2042. offset = szu * NVME_CMB_OFST(cmbloc);
  2043. bar_size = pci_resource_len(pdev, NVME_CMB_BIR(cmbloc));
  2044. if (offset > bar_size)
  2045. return NULL;
  2046. /*
  2047. * Controllers may support a CMB size larger than their BAR,
  2048. * for example, due to being behind a bridge. Reduce the CMB to
  2049. * the reported size of the BAR
  2050. */
  2051. if (size > bar_size - offset)
  2052. size = bar_size - offset;
  2053. dma_addr = pci_resource_start(pdev, NVME_CMB_BIR(cmbloc)) + offset;
  2054. cmb = ioremap_wc(dma_addr, size);
  2055. if (!cmb)
  2056. return NULL;
  2057. dev->cmb_dma_addr = dma_addr;
  2058. dev->cmb_size = size;
  2059. return cmb;
  2060. }
  2061. static inline void nvme_release_cmb(struct nvme_dev *dev)
  2062. {
  2063. if (dev->cmb) {
  2064. iounmap(dev->cmb);
  2065. dev->cmb = NULL;
  2066. }
  2067. }
  2068. static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
  2069. {
  2070. return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
  2071. }
  2072. static int nvme_setup_io_queues(struct nvme_dev *dev)
  2073. {
  2074. struct nvme_queue *adminq = dev->queues[0];
  2075. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2076. int result, i, vecs, nr_io_queues, size;
  2077. nr_io_queues = num_possible_cpus();
  2078. result = set_queue_count(dev, nr_io_queues);
  2079. if (result <= 0)
  2080. return result;
  2081. if (result < nr_io_queues)
  2082. nr_io_queues = result;
  2083. if (dev->cmb && NVME_CMB_SQS(dev->cmbsz)) {
  2084. result = nvme_cmb_qdepth(dev, nr_io_queues,
  2085. sizeof(struct nvme_command));
  2086. if (result > 0)
  2087. dev->q_depth = result;
  2088. else
  2089. nvme_release_cmb(dev);
  2090. }
  2091. size = db_bar_size(dev, nr_io_queues);
  2092. if (size > 8192) {
  2093. iounmap(dev->bar);
  2094. do {
  2095. dev->bar = ioremap(pci_resource_start(pdev, 0), size);
  2096. if (dev->bar)
  2097. break;
  2098. if (!--nr_io_queues)
  2099. return -ENOMEM;
  2100. size = db_bar_size(dev, nr_io_queues);
  2101. } while (1);
  2102. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  2103. adminq->q_db = dev->dbs;
  2104. }
  2105. /* Deregister the admin queue's interrupt */
  2106. free_irq(dev->entry[0].vector, adminq);
  2107. /*
  2108. * If we enable msix early due to not intx, disable it again before
  2109. * setting up the full range we need.
  2110. */
  2111. if (!pdev->irq)
  2112. pci_disable_msix(pdev);
  2113. for (i = 0; i < nr_io_queues; i++)
  2114. dev->entry[i].entry = i;
  2115. vecs = pci_enable_msix_range(pdev, dev->entry, 1, nr_io_queues);
  2116. if (vecs < 0) {
  2117. vecs = pci_enable_msi_range(pdev, 1, min(nr_io_queues, 32));
  2118. if (vecs < 0) {
  2119. vecs = 1;
  2120. } else {
  2121. for (i = 0; i < vecs; i++)
  2122. dev->entry[i].vector = i + pdev->irq;
  2123. }
  2124. }
  2125. /*
  2126. * Should investigate if there's a performance win from allocating
  2127. * more queues than interrupt vectors; it might allow the submission
  2128. * path to scale better, even if the receive path is limited by the
  2129. * number of interrupts.
  2130. */
  2131. nr_io_queues = vecs;
  2132. dev->max_qid = nr_io_queues;
  2133. result = queue_request_irq(dev, adminq, adminq->irqname);
  2134. if (result) {
  2135. adminq->cq_vector = -1;
  2136. goto free_queues;
  2137. }
  2138. /* Free previously allocated queues that are no longer usable */
  2139. nvme_free_queues(dev, nr_io_queues + 1);
  2140. nvme_create_io_queues(dev);
  2141. return 0;
  2142. free_queues:
  2143. nvme_free_queues(dev, 1);
  2144. return result;
  2145. }
  2146. static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
  2147. {
  2148. struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
  2149. struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
  2150. return nsa->ns_id - nsb->ns_id;
  2151. }
  2152. static struct nvme_ns *nvme_find_ns(struct nvme_dev *dev, unsigned nsid)
  2153. {
  2154. struct nvme_ns *ns;
  2155. list_for_each_entry(ns, &dev->namespaces, list) {
  2156. if (ns->ns_id == nsid)
  2157. return ns;
  2158. if (ns->ns_id > nsid)
  2159. break;
  2160. }
  2161. return NULL;
  2162. }
  2163. static inline bool nvme_io_incapable(struct nvme_dev *dev)
  2164. {
  2165. return (!dev->bar || readl(&dev->bar->csts) & NVME_CSTS_CFS ||
  2166. dev->online_queues < 2);
  2167. }
  2168. static void nvme_ns_remove(struct nvme_ns *ns)
  2169. {
  2170. bool kill = nvme_io_incapable(ns->dev) && !blk_queue_dying(ns->queue);
  2171. if (kill) {
  2172. blk_set_queue_dying(ns->queue);
  2173. /*
  2174. * The controller was shutdown first if we got here through
  2175. * device removal. The shutdown may requeue outstanding
  2176. * requests. These need to be aborted immediately so
  2177. * del_gendisk doesn't block indefinitely for their completion.
  2178. */
  2179. blk_mq_abort_requeue_list(ns->queue);
  2180. }
  2181. if (ns->disk->flags & GENHD_FL_UP)
  2182. del_gendisk(ns->disk);
  2183. if (kill || !blk_queue_dying(ns->queue)) {
  2184. blk_mq_abort_requeue_list(ns->queue);
  2185. blk_cleanup_queue(ns->queue);
  2186. }
  2187. list_del_init(&ns->list);
  2188. kref_put(&ns->kref, nvme_free_ns);
  2189. }
  2190. static void nvme_scan_namespaces(struct nvme_dev *dev, unsigned nn)
  2191. {
  2192. struct nvme_ns *ns, *next;
  2193. unsigned i;
  2194. for (i = 1; i <= nn; i++) {
  2195. ns = nvme_find_ns(dev, i);
  2196. if (ns) {
  2197. if (revalidate_disk(ns->disk))
  2198. nvme_ns_remove(ns);
  2199. } else
  2200. nvme_alloc_ns(dev, i);
  2201. }
  2202. list_for_each_entry_safe(ns, next, &dev->namespaces, list) {
  2203. if (ns->ns_id > nn)
  2204. nvme_ns_remove(ns);
  2205. }
  2206. list_sort(NULL, &dev->namespaces, ns_cmp);
  2207. }
  2208. static void nvme_set_irq_hints(struct nvme_dev *dev)
  2209. {
  2210. struct nvme_queue *nvmeq;
  2211. int i;
  2212. for (i = 0; i < dev->online_queues; i++) {
  2213. nvmeq = dev->queues[i];
  2214. if (!nvmeq->tags || !(*nvmeq->tags))
  2215. continue;
  2216. irq_set_affinity_hint(dev->entry[nvmeq->cq_vector].vector,
  2217. blk_mq_tags_cpumask(*nvmeq->tags));
  2218. }
  2219. }
  2220. static void nvme_dev_scan(struct work_struct *work)
  2221. {
  2222. struct nvme_dev *dev = container_of(work, struct nvme_dev, scan_work);
  2223. struct nvme_id_ctrl *ctrl;
  2224. if (!dev->tagset.tags)
  2225. return;
  2226. if (nvme_identify_ctrl(dev, &ctrl))
  2227. return;
  2228. nvme_scan_namespaces(dev, le32_to_cpup(&ctrl->nn));
  2229. kfree(ctrl);
  2230. nvme_set_irq_hints(dev);
  2231. }
  2232. /*
  2233. * Return: error value if an error occurred setting up the queues or calling
  2234. * Identify Device. 0 if these succeeded, even if adding some of the
  2235. * namespaces failed. At the moment, these failures are silent. TBD which
  2236. * failures should be reported.
  2237. */
  2238. static int nvme_dev_add(struct nvme_dev *dev)
  2239. {
  2240. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2241. int res;
  2242. struct nvme_id_ctrl *ctrl;
  2243. int shift = NVME_CAP_MPSMIN(lo_hi_readq(&dev->bar->cap)) + 12;
  2244. res = nvme_identify_ctrl(dev, &ctrl);
  2245. if (res) {
  2246. dev_err(dev->dev, "Identify Controller failed (%d)\n", res);
  2247. return -EIO;
  2248. }
  2249. dev->oncs = le16_to_cpup(&ctrl->oncs);
  2250. dev->abort_limit = ctrl->acl + 1;
  2251. dev->vwc = ctrl->vwc;
  2252. memcpy(dev->serial, ctrl->sn, sizeof(ctrl->sn));
  2253. memcpy(dev->model, ctrl->mn, sizeof(ctrl->mn));
  2254. memcpy(dev->firmware_rev, ctrl->fr, sizeof(ctrl->fr));
  2255. if (ctrl->mdts)
  2256. dev->max_hw_sectors = 1 << (ctrl->mdts + shift - 9);
  2257. else
  2258. dev->max_hw_sectors = UINT_MAX;
  2259. if ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
  2260. (pdev->device == 0x0953) && ctrl->vs[3]) {
  2261. unsigned int max_hw_sectors;
  2262. dev->stripe_size = 1 << (ctrl->vs[3] + shift);
  2263. max_hw_sectors = dev->stripe_size >> (shift - 9);
  2264. if (dev->max_hw_sectors) {
  2265. dev->max_hw_sectors = min(max_hw_sectors,
  2266. dev->max_hw_sectors);
  2267. } else
  2268. dev->max_hw_sectors = max_hw_sectors;
  2269. }
  2270. kfree(ctrl);
  2271. if (!dev->tagset.tags) {
  2272. dev->tagset.ops = &nvme_mq_ops;
  2273. dev->tagset.nr_hw_queues = dev->online_queues - 1;
  2274. dev->tagset.timeout = NVME_IO_TIMEOUT;
  2275. dev->tagset.numa_node = dev_to_node(dev->dev);
  2276. dev->tagset.queue_depth =
  2277. min_t(int, dev->q_depth, BLK_MQ_MAX_DEPTH) - 1;
  2278. dev->tagset.cmd_size = nvme_cmd_size(dev);
  2279. dev->tagset.flags = BLK_MQ_F_SHOULD_MERGE;
  2280. dev->tagset.driver_data = dev;
  2281. if (blk_mq_alloc_tag_set(&dev->tagset))
  2282. return 0;
  2283. }
  2284. schedule_work(&dev->scan_work);
  2285. return 0;
  2286. }
  2287. static int nvme_pci_enable(struct nvme_dev *dev)
  2288. {
  2289. u64 cap;
  2290. int result = -ENOMEM;
  2291. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2292. if (pci_enable_device_mem(pdev))
  2293. return result;
  2294. dev->entry[0].vector = pdev->irq;
  2295. pci_set_master(pdev);
  2296. if (dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64)) &&
  2297. dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(32)))
  2298. goto disable;
  2299. if (readl(&dev->bar->csts) == -1) {
  2300. result = -ENODEV;
  2301. goto disable;
  2302. }
  2303. /*
  2304. * Some devices don't advertse INTx interrupts, pre-enable a single
  2305. * MSIX vec for setup. We'll adjust this later.
  2306. */
  2307. if (!pdev->irq) {
  2308. result = pci_enable_msix(pdev, dev->entry, 1);
  2309. if (result < 0)
  2310. goto disable;
  2311. }
  2312. cap = lo_hi_readq(&dev->bar->cap);
  2313. dev->q_depth = min_t(int, NVME_CAP_MQES(cap) + 1, NVME_Q_DEPTH);
  2314. dev->db_stride = 1 << NVME_CAP_STRIDE(cap);
  2315. dev->dbs = ((void __iomem *)dev->bar) + 4096;
  2316. /*
  2317. * Temporary fix for the Apple controller found in the MacBook8,1 and
  2318. * some MacBook7,1 to avoid controller resets and data loss.
  2319. */
  2320. if (pdev->vendor == PCI_VENDOR_ID_APPLE && pdev->device == 0x2001) {
  2321. dev->q_depth = 2;
  2322. dev_warn(dev->dev, "detected Apple NVMe controller, set "
  2323. "queue depth=%u to work around controller resets\n",
  2324. dev->q_depth);
  2325. }
  2326. if (readl(&dev->bar->vs) >= NVME_VS(1, 2))
  2327. dev->cmb = nvme_map_cmb(dev);
  2328. return 0;
  2329. disable:
  2330. pci_disable_device(pdev);
  2331. return result;
  2332. }
  2333. static void nvme_dev_unmap(struct nvme_dev *dev)
  2334. {
  2335. if (dev->bar)
  2336. iounmap(dev->bar);
  2337. pci_release_regions(to_pci_dev(dev->dev));
  2338. }
  2339. static void nvme_pci_disable(struct nvme_dev *dev)
  2340. {
  2341. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2342. if (pdev->msi_enabled)
  2343. pci_disable_msi(pdev);
  2344. else if (pdev->msix_enabled)
  2345. pci_disable_msix(pdev);
  2346. if (pci_is_enabled(pdev))
  2347. pci_disable_device(pdev);
  2348. }
  2349. struct nvme_delq_ctx {
  2350. struct task_struct *waiter;
  2351. struct kthread_worker *worker;
  2352. atomic_t refcount;
  2353. };
  2354. static void nvme_wait_dq(struct nvme_delq_ctx *dq, struct nvme_dev *dev)
  2355. {
  2356. dq->waiter = current;
  2357. mb();
  2358. for (;;) {
  2359. set_current_state(TASK_KILLABLE);
  2360. if (!atomic_read(&dq->refcount))
  2361. break;
  2362. if (!schedule_timeout(ADMIN_TIMEOUT) ||
  2363. fatal_signal_pending(current)) {
  2364. /*
  2365. * Disable the controller first since we can't trust it
  2366. * at this point, but leave the admin queue enabled
  2367. * until all queue deletion requests are flushed.
  2368. * FIXME: This may take a while if there are more h/w
  2369. * queues than admin tags.
  2370. */
  2371. set_current_state(TASK_RUNNING);
  2372. nvme_disable_ctrl(dev, lo_hi_readq(&dev->bar->cap));
  2373. nvme_clear_queue(dev->queues[0]);
  2374. flush_kthread_worker(dq->worker);
  2375. nvme_disable_queue(dev, 0);
  2376. return;
  2377. }
  2378. }
  2379. set_current_state(TASK_RUNNING);
  2380. }
  2381. static void nvme_put_dq(struct nvme_delq_ctx *dq)
  2382. {
  2383. atomic_dec(&dq->refcount);
  2384. if (dq->waiter)
  2385. wake_up_process(dq->waiter);
  2386. }
  2387. static struct nvme_delq_ctx *nvme_get_dq(struct nvme_delq_ctx *dq)
  2388. {
  2389. atomic_inc(&dq->refcount);
  2390. return dq;
  2391. }
  2392. static void nvme_del_queue_end(struct nvme_queue *nvmeq)
  2393. {
  2394. struct nvme_delq_ctx *dq = nvmeq->cmdinfo.ctx;
  2395. nvme_put_dq(dq);
  2396. spin_lock_irq(&nvmeq->q_lock);
  2397. nvme_process_cq(nvmeq);
  2398. spin_unlock_irq(&nvmeq->q_lock);
  2399. }
  2400. static int adapter_async_del_queue(struct nvme_queue *nvmeq, u8 opcode,
  2401. kthread_work_func_t fn)
  2402. {
  2403. struct nvme_command c;
  2404. memset(&c, 0, sizeof(c));
  2405. c.delete_queue.opcode = opcode;
  2406. c.delete_queue.qid = cpu_to_le16(nvmeq->qid);
  2407. init_kthread_work(&nvmeq->cmdinfo.work, fn);
  2408. return nvme_submit_admin_async_cmd(nvmeq->dev, &c, &nvmeq->cmdinfo,
  2409. ADMIN_TIMEOUT);
  2410. }
  2411. static void nvme_del_cq_work_handler(struct kthread_work *work)
  2412. {
  2413. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2414. cmdinfo.work);
  2415. nvme_del_queue_end(nvmeq);
  2416. }
  2417. static int nvme_delete_cq(struct nvme_queue *nvmeq)
  2418. {
  2419. return adapter_async_del_queue(nvmeq, nvme_admin_delete_cq,
  2420. nvme_del_cq_work_handler);
  2421. }
  2422. static void nvme_del_sq_work_handler(struct kthread_work *work)
  2423. {
  2424. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2425. cmdinfo.work);
  2426. int status = nvmeq->cmdinfo.status;
  2427. if (!status)
  2428. status = nvme_delete_cq(nvmeq);
  2429. if (status)
  2430. nvme_del_queue_end(nvmeq);
  2431. }
  2432. static int nvme_delete_sq(struct nvme_queue *nvmeq)
  2433. {
  2434. return adapter_async_del_queue(nvmeq, nvme_admin_delete_sq,
  2435. nvme_del_sq_work_handler);
  2436. }
  2437. static void nvme_del_queue_start(struct kthread_work *work)
  2438. {
  2439. struct nvme_queue *nvmeq = container_of(work, struct nvme_queue,
  2440. cmdinfo.work);
  2441. if (nvme_delete_sq(nvmeq))
  2442. nvme_del_queue_end(nvmeq);
  2443. }
  2444. static void nvme_disable_io_queues(struct nvme_dev *dev)
  2445. {
  2446. int i;
  2447. DEFINE_KTHREAD_WORKER_ONSTACK(worker);
  2448. struct nvme_delq_ctx dq;
  2449. struct task_struct *kworker_task = kthread_run(kthread_worker_fn,
  2450. &worker, "nvme%d", dev->instance);
  2451. if (IS_ERR(kworker_task)) {
  2452. dev_err(dev->dev,
  2453. "Failed to create queue del task\n");
  2454. for (i = dev->queue_count - 1; i > 0; i--)
  2455. nvme_disable_queue(dev, i);
  2456. return;
  2457. }
  2458. dq.waiter = NULL;
  2459. atomic_set(&dq.refcount, 0);
  2460. dq.worker = &worker;
  2461. for (i = dev->queue_count - 1; i > 0; i--) {
  2462. struct nvme_queue *nvmeq = dev->queues[i];
  2463. if (nvme_suspend_queue(nvmeq))
  2464. continue;
  2465. nvmeq->cmdinfo.ctx = nvme_get_dq(&dq);
  2466. nvmeq->cmdinfo.worker = dq.worker;
  2467. init_kthread_work(&nvmeq->cmdinfo.work, nvme_del_queue_start);
  2468. queue_kthread_work(dq.worker, &nvmeq->cmdinfo.work);
  2469. }
  2470. nvme_wait_dq(&dq, dev);
  2471. kthread_stop(kworker_task);
  2472. }
  2473. /*
  2474. * Remove the node from the device list and check
  2475. * for whether or not we need to stop the nvme_thread.
  2476. */
  2477. static void nvme_dev_list_remove(struct nvme_dev *dev)
  2478. {
  2479. struct task_struct *tmp = NULL;
  2480. spin_lock(&dev_list_lock);
  2481. list_del_init(&dev->node);
  2482. if (list_empty(&dev_list) && !IS_ERR_OR_NULL(nvme_thread)) {
  2483. tmp = nvme_thread;
  2484. nvme_thread = NULL;
  2485. }
  2486. spin_unlock(&dev_list_lock);
  2487. if (tmp)
  2488. kthread_stop(tmp);
  2489. }
  2490. static void nvme_freeze_queues(struct nvme_dev *dev)
  2491. {
  2492. struct nvme_ns *ns;
  2493. list_for_each_entry(ns, &dev->namespaces, list) {
  2494. blk_mq_freeze_queue_start(ns->queue);
  2495. spin_lock_irq(ns->queue->queue_lock);
  2496. queue_flag_set(QUEUE_FLAG_STOPPED, ns->queue);
  2497. spin_unlock_irq(ns->queue->queue_lock);
  2498. blk_mq_cancel_requeue_work(ns->queue);
  2499. blk_mq_stop_hw_queues(ns->queue);
  2500. }
  2501. }
  2502. static void nvme_unfreeze_queues(struct nvme_dev *dev)
  2503. {
  2504. struct nvme_ns *ns;
  2505. list_for_each_entry(ns, &dev->namespaces, list) {
  2506. queue_flag_clear_unlocked(QUEUE_FLAG_STOPPED, ns->queue);
  2507. blk_mq_unfreeze_queue(ns->queue);
  2508. blk_mq_start_stopped_hw_queues(ns->queue, true);
  2509. blk_mq_kick_requeue_list(ns->queue);
  2510. }
  2511. }
  2512. static void nvme_dev_shutdown(struct nvme_dev *dev)
  2513. {
  2514. int i;
  2515. u32 csts = -1;
  2516. nvme_dev_list_remove(dev);
  2517. mutex_lock(&dev->shutdown_lock);
  2518. if (pci_is_enabled(to_pci_dev(dev->dev))) {
  2519. nvme_freeze_queues(dev);
  2520. csts = readl(&dev->bar->csts);
  2521. }
  2522. if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
  2523. for (i = dev->queue_count - 1; i >= 0; i--) {
  2524. struct nvme_queue *nvmeq = dev->queues[i];
  2525. nvme_suspend_queue(nvmeq);
  2526. }
  2527. } else {
  2528. nvme_disable_io_queues(dev);
  2529. nvme_shutdown_ctrl(dev);
  2530. nvme_disable_queue(dev, 0);
  2531. }
  2532. nvme_pci_disable(dev);
  2533. for (i = dev->queue_count - 1; i >= 0; i--)
  2534. nvme_clear_queue(dev->queues[i]);
  2535. mutex_unlock(&dev->shutdown_lock);
  2536. }
  2537. static void nvme_remove_namespaces(struct nvme_dev *dev)
  2538. {
  2539. struct nvme_ns *ns, *next;
  2540. list_for_each_entry_safe(ns, next, &dev->namespaces, list)
  2541. nvme_ns_remove(ns);
  2542. }
  2543. static void nvme_dev_remove(struct nvme_dev *dev)
  2544. {
  2545. if (nvme_io_incapable(dev)) {
  2546. /*
  2547. * If the device is not capable of IO (surprise hot-removal,
  2548. * for example), we need to quiesce prior to deleting the
  2549. * namespaces. This will end outstanding requests and prevent
  2550. * attempts to sync dirty data.
  2551. */
  2552. nvme_dev_shutdown(dev);
  2553. }
  2554. nvme_remove_namespaces(dev);
  2555. }
  2556. static int nvme_setup_prp_pools(struct nvme_dev *dev)
  2557. {
  2558. dev->prp_page_pool = dma_pool_create("prp list page", dev->dev,
  2559. PAGE_SIZE, PAGE_SIZE, 0);
  2560. if (!dev->prp_page_pool)
  2561. return -ENOMEM;
  2562. /* Optimisation for I/Os between 4k and 128k */
  2563. dev->prp_small_pool = dma_pool_create("prp list 256", dev->dev,
  2564. 256, 256, 0);
  2565. if (!dev->prp_small_pool) {
  2566. dma_pool_destroy(dev->prp_page_pool);
  2567. return -ENOMEM;
  2568. }
  2569. return 0;
  2570. }
  2571. static void nvme_release_prp_pools(struct nvme_dev *dev)
  2572. {
  2573. dma_pool_destroy(dev->prp_page_pool);
  2574. dma_pool_destroy(dev->prp_small_pool);
  2575. }
  2576. static DEFINE_IDA(nvme_instance_ida);
  2577. static int nvme_set_instance(struct nvme_dev *dev)
  2578. {
  2579. int instance, error;
  2580. do {
  2581. if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
  2582. return -ENODEV;
  2583. spin_lock(&dev_list_lock);
  2584. error = ida_get_new(&nvme_instance_ida, &instance);
  2585. spin_unlock(&dev_list_lock);
  2586. } while (error == -EAGAIN);
  2587. if (error)
  2588. return -ENODEV;
  2589. dev->instance = instance;
  2590. return 0;
  2591. }
  2592. static void nvme_release_instance(struct nvme_dev *dev)
  2593. {
  2594. spin_lock(&dev_list_lock);
  2595. ida_remove(&nvme_instance_ida, dev->instance);
  2596. spin_unlock(&dev_list_lock);
  2597. }
  2598. static void nvme_free_dev(struct kref *kref)
  2599. {
  2600. struct nvme_dev *dev = container_of(kref, struct nvme_dev, kref);
  2601. put_device(dev->dev);
  2602. put_device(dev->device);
  2603. nvme_release_instance(dev);
  2604. if (dev->tagset.tags)
  2605. blk_mq_free_tag_set(&dev->tagset);
  2606. if (dev->admin_q)
  2607. blk_put_queue(dev->admin_q);
  2608. kfree(dev->queues);
  2609. kfree(dev->entry);
  2610. kfree(dev);
  2611. }
  2612. static int nvme_dev_open(struct inode *inode, struct file *f)
  2613. {
  2614. struct nvme_dev *dev;
  2615. int instance = iminor(inode);
  2616. int ret = -ENODEV;
  2617. spin_lock(&dev_list_lock);
  2618. list_for_each_entry(dev, &dev_list, node) {
  2619. if (dev->instance == instance) {
  2620. if (!dev->admin_q) {
  2621. ret = -EWOULDBLOCK;
  2622. break;
  2623. }
  2624. if (!kref_get_unless_zero(&dev->kref))
  2625. break;
  2626. f->private_data = dev;
  2627. ret = 0;
  2628. break;
  2629. }
  2630. }
  2631. spin_unlock(&dev_list_lock);
  2632. return ret;
  2633. }
  2634. static int nvme_dev_release(struct inode *inode, struct file *f)
  2635. {
  2636. struct nvme_dev *dev = f->private_data;
  2637. kref_put(&dev->kref, nvme_free_dev);
  2638. return 0;
  2639. }
  2640. static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  2641. {
  2642. struct nvme_dev *dev = f->private_data;
  2643. struct nvme_ns *ns;
  2644. switch (cmd) {
  2645. case NVME_IOCTL_ADMIN_CMD:
  2646. return nvme_user_cmd(dev, NULL, (void __user *)arg);
  2647. case NVME_IOCTL_IO_CMD:
  2648. if (list_empty(&dev->namespaces))
  2649. return -ENOTTY;
  2650. ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
  2651. return nvme_user_cmd(dev, ns, (void __user *)arg);
  2652. case NVME_IOCTL_RESET:
  2653. dev_warn(dev->dev, "resetting controller\n");
  2654. return nvme_reset(dev);
  2655. case NVME_IOCTL_SUBSYS_RESET:
  2656. return nvme_subsys_reset(dev);
  2657. default:
  2658. return -ENOTTY;
  2659. }
  2660. }
  2661. static const struct file_operations nvme_dev_fops = {
  2662. .owner = THIS_MODULE,
  2663. .open = nvme_dev_open,
  2664. .release = nvme_dev_release,
  2665. .unlocked_ioctl = nvme_dev_ioctl,
  2666. .compat_ioctl = nvme_dev_ioctl,
  2667. };
  2668. static void nvme_probe_work(struct work_struct *work)
  2669. {
  2670. struct nvme_dev *dev = container_of(work, struct nvme_dev, probe_work);
  2671. bool start_thread = false;
  2672. int result;
  2673. result = nvme_pci_enable(dev);
  2674. if (result)
  2675. goto out;
  2676. result = nvme_configure_admin_queue(dev);
  2677. if (result)
  2678. goto unmap;
  2679. spin_lock(&dev_list_lock);
  2680. if (list_empty(&dev_list) && IS_ERR_OR_NULL(nvme_thread)) {
  2681. start_thread = true;
  2682. nvme_thread = NULL;
  2683. }
  2684. list_add(&dev->node, &dev_list);
  2685. spin_unlock(&dev_list_lock);
  2686. if (start_thread) {
  2687. nvme_thread = kthread_run(nvme_kthread, NULL, "nvme");
  2688. wake_up_all(&nvme_kthread_wait);
  2689. } else
  2690. wait_event_killable(nvme_kthread_wait, nvme_thread);
  2691. if (IS_ERR_OR_NULL(nvme_thread)) {
  2692. result = nvme_thread ? PTR_ERR(nvme_thread) : -EINTR;
  2693. goto disable;
  2694. }
  2695. result = nvme_alloc_admin_tags(dev);
  2696. if (result)
  2697. goto disable;
  2698. result = nvme_setup_io_queues(dev);
  2699. if (result)
  2700. goto free_tags;
  2701. dev->event_limit = 1;
  2702. /*
  2703. * Keep the controller around but remove all namespaces if we don't have
  2704. * any working I/O queue.
  2705. */
  2706. if (dev->online_queues < 2) {
  2707. dev_warn(dev->dev, "IO queues not created\n");
  2708. nvme_remove_namespaces(dev);
  2709. } else {
  2710. nvme_unfreeze_queues(dev);
  2711. nvme_dev_add(dev);
  2712. }
  2713. return;
  2714. free_tags:
  2715. nvme_dev_remove_admin(dev);
  2716. blk_put_queue(dev->admin_q);
  2717. dev->admin_q = NULL;
  2718. dev->queues[0]->tags = NULL;
  2719. disable:
  2720. nvme_disable_queue(dev, 0);
  2721. nvme_dev_list_remove(dev);
  2722. unmap:
  2723. nvme_dev_unmap(dev);
  2724. out:
  2725. if (!work_busy(&dev->reset_work))
  2726. nvme_dead_ctrl(dev);
  2727. }
  2728. static int nvme_remove_dead_ctrl(void *arg)
  2729. {
  2730. struct nvme_dev *dev = (struct nvme_dev *)arg;
  2731. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2732. if (pci_get_drvdata(pdev))
  2733. pci_stop_and_remove_bus_device_locked(pdev);
  2734. kref_put(&dev->kref, nvme_free_dev);
  2735. return 0;
  2736. }
  2737. static void nvme_dead_ctrl(struct nvme_dev *dev)
  2738. {
  2739. dev_warn(dev->dev, "Device failed to resume\n");
  2740. kref_get(&dev->kref);
  2741. if (IS_ERR(kthread_run(nvme_remove_dead_ctrl, dev, "nvme%d",
  2742. dev->instance))) {
  2743. dev_err(dev->dev,
  2744. "Failed to start controller remove task\n");
  2745. kref_put(&dev->kref, nvme_free_dev);
  2746. }
  2747. }
  2748. static void nvme_reset_work(struct work_struct *ws)
  2749. {
  2750. struct nvme_dev *dev = container_of(ws, struct nvme_dev, reset_work);
  2751. bool in_probe = work_busy(&dev->probe_work);
  2752. nvme_dev_shutdown(dev);
  2753. /* Synchronize with device probe so that work will see failure status
  2754. * and exit gracefully without trying to schedule another reset */
  2755. flush_work(&dev->probe_work);
  2756. /* Fail this device if reset occured during probe to avoid
  2757. * infinite initialization loops. */
  2758. if (in_probe) {
  2759. nvme_dead_ctrl(dev);
  2760. return;
  2761. }
  2762. /* Schedule device resume asynchronously so the reset work is available
  2763. * to cleanup errors that may occur during reinitialization */
  2764. schedule_work(&dev->probe_work);
  2765. }
  2766. static int __nvme_reset(struct nvme_dev *dev)
  2767. {
  2768. if (work_pending(&dev->reset_work))
  2769. return -EBUSY;
  2770. list_del_init(&dev->node);
  2771. queue_work(nvme_workq, &dev->reset_work);
  2772. return 0;
  2773. }
  2774. static int nvme_reset(struct nvme_dev *dev)
  2775. {
  2776. int ret;
  2777. if (!dev->admin_q || blk_queue_dying(dev->admin_q))
  2778. return -ENODEV;
  2779. spin_lock(&dev_list_lock);
  2780. ret = __nvme_reset(dev);
  2781. spin_unlock(&dev_list_lock);
  2782. if (!ret) {
  2783. flush_work(&dev->reset_work);
  2784. flush_work(&dev->probe_work);
  2785. return 0;
  2786. }
  2787. return ret;
  2788. }
  2789. static ssize_t nvme_sysfs_reset(struct device *dev,
  2790. struct device_attribute *attr, const char *buf,
  2791. size_t count)
  2792. {
  2793. struct nvme_dev *ndev = dev_get_drvdata(dev);
  2794. int ret;
  2795. ret = nvme_reset(ndev);
  2796. if (ret < 0)
  2797. return ret;
  2798. return count;
  2799. }
  2800. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  2801. static int nvme_dev_map(struct nvme_dev *dev)
  2802. {
  2803. int bars;
  2804. struct pci_dev *pdev = to_pci_dev(dev->dev);
  2805. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2806. if (!bars)
  2807. return -ENODEV;
  2808. if (pci_request_selected_regions(pdev, bars, "nvme"))
  2809. return -ENODEV;
  2810. dev->bar = ioremap(pci_resource_start(pdev, 0), 8192);
  2811. if (!dev->bar)
  2812. goto release;
  2813. return 0;
  2814. release:
  2815. pci_release_regions(pdev);
  2816. return -ENODEV;
  2817. }
  2818. static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2819. {
  2820. int node, result = -ENOMEM;
  2821. struct nvme_dev *dev;
  2822. node = dev_to_node(&pdev->dev);
  2823. if (node == NUMA_NO_NODE)
  2824. set_dev_node(&pdev->dev, 0);
  2825. dev = kzalloc_node(sizeof(*dev), GFP_KERNEL, node);
  2826. if (!dev)
  2827. return -ENOMEM;
  2828. dev->entry = kzalloc_node(num_possible_cpus() * sizeof(*dev->entry),
  2829. GFP_KERNEL, node);
  2830. if (!dev->entry)
  2831. goto free;
  2832. dev->queues = kzalloc_node((num_possible_cpus() + 1) * sizeof(void *),
  2833. GFP_KERNEL, node);
  2834. if (!dev->queues)
  2835. goto free;
  2836. INIT_LIST_HEAD(&dev->namespaces);
  2837. INIT_WORK(&dev->reset_work, nvme_reset_work);
  2838. mutex_init(&dev->shutdown_lock);
  2839. dev->dev = get_device(&pdev->dev);
  2840. pci_set_drvdata(pdev, dev);
  2841. result = nvme_dev_map(dev);
  2842. if (result)
  2843. goto free;
  2844. result = nvme_set_instance(dev);
  2845. if (result)
  2846. goto put_pci;
  2847. result = nvme_setup_prp_pools(dev);
  2848. if (result)
  2849. goto release;
  2850. kref_init(&dev->kref);
  2851. dev->device = device_create(nvme_class, &pdev->dev,
  2852. MKDEV(nvme_char_major, dev->instance),
  2853. dev, "nvme%d", dev->instance);
  2854. if (IS_ERR(dev->device)) {
  2855. result = PTR_ERR(dev->device);
  2856. goto release_pools;
  2857. }
  2858. get_device(dev->device);
  2859. dev_set_drvdata(dev->device, dev);
  2860. result = device_create_file(dev->device, &dev_attr_reset_controller);
  2861. if (result)
  2862. goto put_dev;
  2863. INIT_LIST_HEAD(&dev->node);
  2864. INIT_WORK(&dev->scan_work, nvme_dev_scan);
  2865. INIT_WORK(&dev->probe_work, nvme_probe_work);
  2866. schedule_work(&dev->probe_work);
  2867. return 0;
  2868. put_dev:
  2869. device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
  2870. put_device(dev->device);
  2871. release_pools:
  2872. nvme_release_prp_pools(dev);
  2873. release:
  2874. nvme_release_instance(dev);
  2875. put_pci:
  2876. put_device(dev->dev);
  2877. nvme_dev_unmap(dev);
  2878. free:
  2879. kfree(dev->queues);
  2880. kfree(dev->entry);
  2881. kfree(dev);
  2882. return result;
  2883. }
  2884. static void nvme_reset_notify(struct pci_dev *pdev, bool prepare)
  2885. {
  2886. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2887. if (prepare)
  2888. nvme_dev_shutdown(dev);
  2889. else
  2890. schedule_work(&dev->probe_work);
  2891. }
  2892. static void nvme_shutdown(struct pci_dev *pdev)
  2893. {
  2894. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2895. nvme_dev_shutdown(dev);
  2896. }
  2897. static void nvme_remove(struct pci_dev *pdev)
  2898. {
  2899. struct nvme_dev *dev = pci_get_drvdata(pdev);
  2900. spin_lock(&dev_list_lock);
  2901. list_del_init(&dev->node);
  2902. spin_unlock(&dev_list_lock);
  2903. pci_set_drvdata(pdev, NULL);
  2904. flush_work(&dev->probe_work);
  2905. flush_work(&dev->reset_work);
  2906. flush_work(&dev->scan_work);
  2907. device_remove_file(dev->device, &dev_attr_reset_controller);
  2908. nvme_dev_remove(dev);
  2909. nvme_dev_shutdown(dev);
  2910. nvme_dev_remove_admin(dev);
  2911. device_destroy(nvme_class, MKDEV(nvme_char_major, dev->instance));
  2912. nvme_free_queues(dev, 0);
  2913. nvme_release_cmb(dev);
  2914. nvme_release_prp_pools(dev);
  2915. nvme_dev_unmap(dev);
  2916. kref_put(&dev->kref, nvme_free_dev);
  2917. }
  2918. /* These functions are yet to be implemented */
  2919. #define nvme_error_detected NULL
  2920. #define nvme_dump_registers NULL
  2921. #define nvme_link_reset NULL
  2922. #define nvme_slot_reset NULL
  2923. #define nvme_error_resume NULL
  2924. #ifdef CONFIG_PM_SLEEP
  2925. static int nvme_suspend(struct device *dev)
  2926. {
  2927. struct pci_dev *pdev = to_pci_dev(dev);
  2928. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2929. nvme_dev_shutdown(ndev);
  2930. return 0;
  2931. }
  2932. static int nvme_resume(struct device *dev)
  2933. {
  2934. struct pci_dev *pdev = to_pci_dev(dev);
  2935. struct nvme_dev *ndev = pci_get_drvdata(pdev);
  2936. schedule_work(&ndev->probe_work);
  2937. return 0;
  2938. }
  2939. #endif
  2940. static SIMPLE_DEV_PM_OPS(nvme_dev_pm_ops, nvme_suspend, nvme_resume);
  2941. static const struct pci_error_handlers nvme_err_handler = {
  2942. .error_detected = nvme_error_detected,
  2943. .mmio_enabled = nvme_dump_registers,
  2944. .link_reset = nvme_link_reset,
  2945. .slot_reset = nvme_slot_reset,
  2946. .resume = nvme_error_resume,
  2947. .reset_notify = nvme_reset_notify,
  2948. };
  2949. /* Move to pci_ids.h later */
  2950. #define PCI_CLASS_STORAGE_EXPRESS 0x010802
  2951. static const struct pci_device_id nvme_id_table[] = {
  2952. { PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_EXPRESS, 0xffffff) },
  2953. { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x2001) },
  2954. { 0, }
  2955. };
  2956. MODULE_DEVICE_TABLE(pci, nvme_id_table);
  2957. static struct pci_driver nvme_driver = {
  2958. .name = "nvme",
  2959. .id_table = nvme_id_table,
  2960. .probe = nvme_probe,
  2961. .remove = nvme_remove,
  2962. .shutdown = nvme_shutdown,
  2963. .driver = {
  2964. .pm = &nvme_dev_pm_ops,
  2965. },
  2966. .err_handler = &nvme_err_handler,
  2967. };
  2968. static int __init nvme_init(void)
  2969. {
  2970. int result;
  2971. init_waitqueue_head(&nvme_kthread_wait);
  2972. nvme_workq = create_singlethread_workqueue("nvme");
  2973. if (!nvme_workq)
  2974. return -ENOMEM;
  2975. result = register_blkdev(nvme_major, "nvme");
  2976. if (result < 0)
  2977. goto kill_workq;
  2978. else if (result > 0)
  2979. nvme_major = result;
  2980. result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
  2981. &nvme_dev_fops);
  2982. if (result < 0)
  2983. goto unregister_blkdev;
  2984. else if (result > 0)
  2985. nvme_char_major = result;
  2986. nvme_class = class_create(THIS_MODULE, "nvme");
  2987. if (IS_ERR(nvme_class)) {
  2988. result = PTR_ERR(nvme_class);
  2989. goto unregister_chrdev;
  2990. }
  2991. result = pci_register_driver(&nvme_driver);
  2992. if (result)
  2993. goto destroy_class;
  2994. return 0;
  2995. destroy_class:
  2996. class_destroy(nvme_class);
  2997. unregister_chrdev:
  2998. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  2999. unregister_blkdev:
  3000. unregister_blkdev(nvme_major, "nvme");
  3001. kill_workq:
  3002. destroy_workqueue(nvme_workq);
  3003. return result;
  3004. }
  3005. static void __exit nvme_exit(void)
  3006. {
  3007. pci_unregister_driver(&nvme_driver);
  3008. unregister_blkdev(nvme_major, "nvme");
  3009. destroy_workqueue(nvme_workq);
  3010. class_destroy(nvme_class);
  3011. __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
  3012. BUG_ON(nvme_thread && !IS_ERR(nvme_thread));
  3013. _nvme_check_size();
  3014. }
  3015. MODULE_AUTHOR("Matthew Wilcox <willy@linux.intel.com>");
  3016. MODULE_LICENSE("GPL");
  3017. MODULE_VERSION("1.0");
  3018. module_init(nvme_init);
  3019. module_exit(nvme_exit);