core.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522
  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/blkdev.h>
  15. #include <linux/blk-mq.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/list_sort.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/pr.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/nvme_ioctl.h>
  27. #include <linux/t10-pi.h>
  28. #include <linux/pm_qos.h>
  29. #include <asm/unaligned.h>
  30. #include "nvme.h"
  31. #include "fabrics.h"
  32. #define NVME_MINORS (1U << MINORBITS)
  33. unsigned int admin_timeout = 60;
  34. module_param(admin_timeout, uint, 0644);
  35. MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
  36. EXPORT_SYMBOL_GPL(admin_timeout);
  37. unsigned int nvme_io_timeout = 30;
  38. module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
  39. MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
  40. EXPORT_SYMBOL_GPL(nvme_io_timeout);
  41. static unsigned char shutdown_timeout = 5;
  42. module_param(shutdown_timeout, byte, 0644);
  43. MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
  44. static u8 nvme_max_retries = 5;
  45. module_param_named(max_retries, nvme_max_retries, byte, 0644);
  46. MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
  47. static unsigned long default_ps_max_latency_us = 100000;
  48. module_param(default_ps_max_latency_us, ulong, 0644);
  49. MODULE_PARM_DESC(default_ps_max_latency_us,
  50. "max power saving latency for new devices; use PM QOS to change per device");
  51. static bool force_apst;
  52. module_param(force_apst, bool, 0644);
  53. MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
  54. static bool streams;
  55. module_param(streams, bool, 0644);
  56. MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
  57. struct workqueue_struct *nvme_wq;
  58. EXPORT_SYMBOL_GPL(nvme_wq);
  59. static DEFINE_IDA(nvme_subsystems_ida);
  60. static LIST_HEAD(nvme_subsystems);
  61. static DEFINE_MUTEX(nvme_subsystems_lock);
  62. static DEFINE_IDA(nvme_instance_ida);
  63. static dev_t nvme_chr_devt;
  64. static struct class *nvme_class;
  65. static struct class *nvme_subsys_class;
  66. static void nvme_ns_remove(struct nvme_ns *ns);
  67. static int nvme_revalidate_disk(struct gendisk *disk);
  68. static __le32 nvme_get_log_dw10(u8 lid, size_t size)
  69. {
  70. return cpu_to_le32((((size / 4) - 1) << 16) | lid);
  71. }
  72. int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
  73. {
  74. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
  75. return -EBUSY;
  76. if (!queue_work(nvme_wq, &ctrl->reset_work))
  77. return -EBUSY;
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
  81. static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
  82. {
  83. int ret;
  84. ret = nvme_reset_ctrl(ctrl);
  85. if (!ret)
  86. flush_work(&ctrl->reset_work);
  87. return ret;
  88. }
  89. static void nvme_delete_ctrl_work(struct work_struct *work)
  90. {
  91. struct nvme_ctrl *ctrl =
  92. container_of(work, struct nvme_ctrl, delete_work);
  93. flush_work(&ctrl->reset_work);
  94. nvme_stop_ctrl(ctrl);
  95. nvme_remove_namespaces(ctrl);
  96. ctrl->ops->delete_ctrl(ctrl);
  97. nvme_uninit_ctrl(ctrl);
  98. nvme_put_ctrl(ctrl);
  99. }
  100. int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
  101. {
  102. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
  103. return -EBUSY;
  104. if (!queue_work(nvme_wq, &ctrl->delete_work))
  105. return -EBUSY;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
  109. int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
  110. {
  111. int ret = 0;
  112. /*
  113. * Keep a reference until the work is flushed since ->delete_ctrl
  114. * can free the controller.
  115. */
  116. nvme_get_ctrl(ctrl);
  117. ret = nvme_delete_ctrl(ctrl);
  118. if (!ret)
  119. flush_work(&ctrl->delete_work);
  120. nvme_put_ctrl(ctrl);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
  124. static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
  125. {
  126. return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
  127. }
  128. static blk_status_t nvme_error_status(struct request *req)
  129. {
  130. switch (nvme_req(req)->status & 0x7ff) {
  131. case NVME_SC_SUCCESS:
  132. return BLK_STS_OK;
  133. case NVME_SC_CAP_EXCEEDED:
  134. return BLK_STS_NOSPC;
  135. case NVME_SC_ONCS_NOT_SUPPORTED:
  136. return BLK_STS_NOTSUPP;
  137. case NVME_SC_WRITE_FAULT:
  138. case NVME_SC_READ_ERROR:
  139. case NVME_SC_UNWRITTEN_BLOCK:
  140. case NVME_SC_ACCESS_DENIED:
  141. case NVME_SC_READ_ONLY:
  142. return BLK_STS_MEDIUM;
  143. case NVME_SC_GUARD_CHECK:
  144. case NVME_SC_APPTAG_CHECK:
  145. case NVME_SC_REFTAG_CHECK:
  146. case NVME_SC_INVALID_PI:
  147. return BLK_STS_PROTECTION;
  148. case NVME_SC_RESERVATION_CONFLICT:
  149. return BLK_STS_NEXUS;
  150. default:
  151. return BLK_STS_IOERR;
  152. }
  153. }
  154. static inline bool nvme_req_needs_retry(struct request *req)
  155. {
  156. if (blk_noretry_request(req))
  157. return false;
  158. if (nvme_req(req)->status & NVME_SC_DNR)
  159. return false;
  160. if (nvme_req(req)->retries >= nvme_max_retries)
  161. return false;
  162. return true;
  163. }
  164. void nvme_complete_rq(struct request *req)
  165. {
  166. if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
  167. if (nvme_req_needs_failover(req)) {
  168. nvme_failover_req(req);
  169. return;
  170. }
  171. if (!blk_queue_dying(req->q)) {
  172. nvme_req(req)->retries++;
  173. blk_mq_requeue_request(req, true);
  174. return;
  175. }
  176. }
  177. blk_mq_end_request(req, nvme_error_status(req));
  178. }
  179. EXPORT_SYMBOL_GPL(nvme_complete_rq);
  180. void nvme_cancel_request(struct request *req, void *data, bool reserved)
  181. {
  182. if (!blk_mq_request_started(req))
  183. return;
  184. dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
  185. "Cancelling I/O %d", req->tag);
  186. nvme_req(req)->status = NVME_SC_ABORT_REQ;
  187. blk_mq_complete_request(req);
  188. }
  189. EXPORT_SYMBOL_GPL(nvme_cancel_request);
  190. bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
  191. enum nvme_ctrl_state new_state)
  192. {
  193. enum nvme_ctrl_state old_state;
  194. unsigned long flags;
  195. bool changed = false;
  196. spin_lock_irqsave(&ctrl->lock, flags);
  197. old_state = ctrl->state;
  198. switch (new_state) {
  199. case NVME_CTRL_LIVE:
  200. switch (old_state) {
  201. case NVME_CTRL_NEW:
  202. case NVME_CTRL_RESETTING:
  203. case NVME_CTRL_RECONNECTING:
  204. changed = true;
  205. /* FALLTHRU */
  206. default:
  207. break;
  208. }
  209. break;
  210. case NVME_CTRL_RESETTING:
  211. switch (old_state) {
  212. case NVME_CTRL_NEW:
  213. case NVME_CTRL_LIVE:
  214. changed = true;
  215. /* FALLTHRU */
  216. default:
  217. break;
  218. }
  219. break;
  220. case NVME_CTRL_RECONNECTING:
  221. switch (old_state) {
  222. case NVME_CTRL_LIVE:
  223. case NVME_CTRL_RESETTING:
  224. changed = true;
  225. /* FALLTHRU */
  226. default:
  227. break;
  228. }
  229. break;
  230. case NVME_CTRL_DELETING:
  231. switch (old_state) {
  232. case NVME_CTRL_LIVE:
  233. case NVME_CTRL_RESETTING:
  234. case NVME_CTRL_RECONNECTING:
  235. changed = true;
  236. /* FALLTHRU */
  237. default:
  238. break;
  239. }
  240. break;
  241. case NVME_CTRL_DEAD:
  242. switch (old_state) {
  243. case NVME_CTRL_DELETING:
  244. changed = true;
  245. /* FALLTHRU */
  246. default:
  247. break;
  248. }
  249. break;
  250. default:
  251. break;
  252. }
  253. if (changed)
  254. ctrl->state = new_state;
  255. spin_unlock_irqrestore(&ctrl->lock, flags);
  256. if (changed && ctrl->state == NVME_CTRL_LIVE)
  257. nvme_kick_requeue_lists(ctrl);
  258. return changed;
  259. }
  260. EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
  261. static void nvme_free_ns_head(struct kref *ref)
  262. {
  263. struct nvme_ns_head *head =
  264. container_of(ref, struct nvme_ns_head, ref);
  265. nvme_mpath_remove_disk(head);
  266. ida_simple_remove(&head->subsys->ns_ida, head->instance);
  267. list_del_init(&head->entry);
  268. cleanup_srcu_struct(&head->srcu);
  269. kfree(head);
  270. }
  271. static void nvme_put_ns_head(struct nvme_ns_head *head)
  272. {
  273. kref_put(&head->ref, nvme_free_ns_head);
  274. }
  275. static void nvme_free_ns(struct kref *kref)
  276. {
  277. struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
  278. if (ns->ndev)
  279. nvme_nvm_unregister(ns);
  280. put_disk(ns->disk);
  281. nvme_put_ns_head(ns->head);
  282. nvme_put_ctrl(ns->ctrl);
  283. kfree(ns);
  284. }
  285. static void nvme_put_ns(struct nvme_ns *ns)
  286. {
  287. kref_put(&ns->kref, nvme_free_ns);
  288. }
  289. struct request *nvme_alloc_request(struct request_queue *q,
  290. struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
  291. {
  292. unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
  293. struct request *req;
  294. if (qid == NVME_QID_ANY) {
  295. req = blk_mq_alloc_request(q, op, flags);
  296. } else {
  297. req = blk_mq_alloc_request_hctx(q, op, flags,
  298. qid ? qid - 1 : 0);
  299. }
  300. if (IS_ERR(req))
  301. return req;
  302. req->cmd_flags |= REQ_FAILFAST_DRIVER;
  303. nvme_req(req)->cmd = cmd;
  304. return req;
  305. }
  306. EXPORT_SYMBOL_GPL(nvme_alloc_request);
  307. static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
  308. {
  309. struct nvme_command c;
  310. memset(&c, 0, sizeof(c));
  311. c.directive.opcode = nvme_admin_directive_send;
  312. c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
  313. c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
  314. c.directive.dtype = NVME_DIR_IDENTIFY;
  315. c.directive.tdtype = NVME_DIR_STREAMS;
  316. c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
  317. return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
  318. }
  319. static int nvme_disable_streams(struct nvme_ctrl *ctrl)
  320. {
  321. return nvme_toggle_streams(ctrl, false);
  322. }
  323. static int nvme_enable_streams(struct nvme_ctrl *ctrl)
  324. {
  325. return nvme_toggle_streams(ctrl, true);
  326. }
  327. static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
  328. struct streams_directive_params *s, u32 nsid)
  329. {
  330. struct nvme_command c;
  331. memset(&c, 0, sizeof(c));
  332. memset(s, 0, sizeof(*s));
  333. c.directive.opcode = nvme_admin_directive_recv;
  334. c.directive.nsid = cpu_to_le32(nsid);
  335. c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
  336. c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
  337. c.directive.dtype = NVME_DIR_STREAMS;
  338. return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
  339. }
  340. static int nvme_configure_directives(struct nvme_ctrl *ctrl)
  341. {
  342. struct streams_directive_params s;
  343. int ret;
  344. if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
  345. return 0;
  346. if (!streams)
  347. return 0;
  348. ret = nvme_enable_streams(ctrl);
  349. if (ret)
  350. return ret;
  351. ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
  352. if (ret)
  353. return ret;
  354. ctrl->nssa = le16_to_cpu(s.nssa);
  355. if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
  356. dev_info(ctrl->device, "too few streams (%u) available\n",
  357. ctrl->nssa);
  358. nvme_disable_streams(ctrl);
  359. return 0;
  360. }
  361. ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
  362. dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
  363. return 0;
  364. }
  365. /*
  366. * Check if 'req' has a write hint associated with it. If it does, assign
  367. * a valid namespace stream to the write.
  368. */
  369. static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
  370. struct request *req, u16 *control,
  371. u32 *dsmgmt)
  372. {
  373. enum rw_hint streamid = req->write_hint;
  374. if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
  375. streamid = 0;
  376. else {
  377. streamid--;
  378. if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
  379. return;
  380. *control |= NVME_RW_DTYPE_STREAMS;
  381. *dsmgmt |= streamid << 16;
  382. }
  383. if (streamid < ARRAY_SIZE(req->q->write_hints))
  384. req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
  385. }
  386. static inline void nvme_setup_flush(struct nvme_ns *ns,
  387. struct nvme_command *cmnd)
  388. {
  389. memset(cmnd, 0, sizeof(*cmnd));
  390. cmnd->common.opcode = nvme_cmd_flush;
  391. cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
  392. }
  393. static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
  394. struct nvme_command *cmnd)
  395. {
  396. unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
  397. struct nvme_dsm_range *range;
  398. struct bio *bio;
  399. range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
  400. if (!range)
  401. return BLK_STS_RESOURCE;
  402. __rq_for_each_bio(bio, req) {
  403. u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
  404. u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
  405. range[n].cattr = cpu_to_le32(0);
  406. range[n].nlb = cpu_to_le32(nlb);
  407. range[n].slba = cpu_to_le64(slba);
  408. n++;
  409. }
  410. if (WARN_ON_ONCE(n != segments)) {
  411. kfree(range);
  412. return BLK_STS_IOERR;
  413. }
  414. memset(cmnd, 0, sizeof(*cmnd));
  415. cmnd->dsm.opcode = nvme_cmd_dsm;
  416. cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
  417. cmnd->dsm.nr = cpu_to_le32(segments - 1);
  418. cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
  419. req->special_vec.bv_page = virt_to_page(range);
  420. req->special_vec.bv_offset = offset_in_page(range);
  421. req->special_vec.bv_len = sizeof(*range) * segments;
  422. req->rq_flags |= RQF_SPECIAL_PAYLOAD;
  423. return BLK_STS_OK;
  424. }
  425. static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
  426. struct request *req, struct nvme_command *cmnd)
  427. {
  428. struct nvme_ctrl *ctrl = ns->ctrl;
  429. u16 control = 0;
  430. u32 dsmgmt = 0;
  431. if (req->cmd_flags & REQ_FUA)
  432. control |= NVME_RW_FUA;
  433. if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
  434. control |= NVME_RW_LR;
  435. if (req->cmd_flags & REQ_RAHEAD)
  436. dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
  437. memset(cmnd, 0, sizeof(*cmnd));
  438. cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
  439. cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
  440. cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
  441. cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
  442. if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
  443. nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
  444. if (ns->ms) {
  445. /*
  446. * If formated with metadata, the block layer always provides a
  447. * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
  448. * we enable the PRACT bit for protection information or set the
  449. * namespace capacity to zero to prevent any I/O.
  450. */
  451. if (!blk_integrity_rq(req)) {
  452. if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
  453. return BLK_STS_NOTSUPP;
  454. control |= NVME_RW_PRINFO_PRACT;
  455. }
  456. switch (ns->pi_type) {
  457. case NVME_NS_DPS_PI_TYPE3:
  458. control |= NVME_RW_PRINFO_PRCHK_GUARD;
  459. break;
  460. case NVME_NS_DPS_PI_TYPE1:
  461. case NVME_NS_DPS_PI_TYPE2:
  462. control |= NVME_RW_PRINFO_PRCHK_GUARD |
  463. NVME_RW_PRINFO_PRCHK_REF;
  464. cmnd->rw.reftag = cpu_to_le32(
  465. nvme_block_nr(ns, blk_rq_pos(req)));
  466. break;
  467. }
  468. }
  469. cmnd->rw.control = cpu_to_le16(control);
  470. cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
  471. return 0;
  472. }
  473. blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
  474. struct nvme_command *cmd)
  475. {
  476. blk_status_t ret = BLK_STS_OK;
  477. if (!(req->rq_flags & RQF_DONTPREP)) {
  478. nvme_req(req)->retries = 0;
  479. nvme_req(req)->flags = 0;
  480. req->rq_flags |= RQF_DONTPREP;
  481. }
  482. switch (req_op(req)) {
  483. case REQ_OP_DRV_IN:
  484. case REQ_OP_DRV_OUT:
  485. memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
  486. break;
  487. case REQ_OP_FLUSH:
  488. nvme_setup_flush(ns, cmd);
  489. break;
  490. case REQ_OP_WRITE_ZEROES:
  491. /* currently only aliased to deallocate for a few ctrls: */
  492. case REQ_OP_DISCARD:
  493. ret = nvme_setup_discard(ns, req, cmd);
  494. break;
  495. case REQ_OP_READ:
  496. case REQ_OP_WRITE:
  497. ret = nvme_setup_rw(ns, req, cmd);
  498. break;
  499. default:
  500. WARN_ON_ONCE(1);
  501. return BLK_STS_IOERR;
  502. }
  503. cmd->common.command_id = req->tag;
  504. return ret;
  505. }
  506. EXPORT_SYMBOL_GPL(nvme_setup_cmd);
  507. /*
  508. * Returns 0 on success. If the result is negative, it's a Linux error code;
  509. * if the result is positive, it's an NVM Express status code
  510. */
  511. int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  512. union nvme_result *result, void *buffer, unsigned bufflen,
  513. unsigned timeout, int qid, int at_head,
  514. blk_mq_req_flags_t flags)
  515. {
  516. struct request *req;
  517. int ret;
  518. req = nvme_alloc_request(q, cmd, flags, qid);
  519. if (IS_ERR(req))
  520. return PTR_ERR(req);
  521. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  522. if (buffer && bufflen) {
  523. ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
  524. if (ret)
  525. goto out;
  526. }
  527. blk_execute_rq(req->q, NULL, req, at_head);
  528. if (result)
  529. *result = nvme_req(req)->result;
  530. if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
  531. ret = -EINTR;
  532. else
  533. ret = nvme_req(req)->status;
  534. out:
  535. blk_mq_free_request(req);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
  539. int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
  540. void *buffer, unsigned bufflen)
  541. {
  542. return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
  543. NVME_QID_ANY, 0, 0);
  544. }
  545. EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
  546. static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
  547. unsigned len, u32 seed, bool write)
  548. {
  549. struct bio_integrity_payload *bip;
  550. int ret = -ENOMEM;
  551. void *buf;
  552. buf = kmalloc(len, GFP_KERNEL);
  553. if (!buf)
  554. goto out;
  555. ret = -EFAULT;
  556. if (write && copy_from_user(buf, ubuf, len))
  557. goto out_free_meta;
  558. bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
  559. if (IS_ERR(bip)) {
  560. ret = PTR_ERR(bip);
  561. goto out_free_meta;
  562. }
  563. bip->bip_iter.bi_size = len;
  564. bip->bip_iter.bi_sector = seed;
  565. ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
  566. offset_in_page(buf));
  567. if (ret == len)
  568. return buf;
  569. ret = -ENOMEM;
  570. out_free_meta:
  571. kfree(buf);
  572. out:
  573. return ERR_PTR(ret);
  574. }
  575. static int nvme_submit_user_cmd(struct request_queue *q,
  576. struct nvme_command *cmd, void __user *ubuffer,
  577. unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
  578. u32 meta_seed, u32 *result, unsigned timeout)
  579. {
  580. bool write = nvme_is_write(cmd);
  581. struct nvme_ns *ns = q->queuedata;
  582. struct gendisk *disk = ns ? ns->disk : NULL;
  583. struct request *req;
  584. struct bio *bio = NULL;
  585. void *meta = NULL;
  586. int ret;
  587. req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
  588. if (IS_ERR(req))
  589. return PTR_ERR(req);
  590. req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
  591. if (ubuffer && bufflen) {
  592. ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
  593. GFP_KERNEL);
  594. if (ret)
  595. goto out;
  596. bio = req->bio;
  597. bio->bi_disk = disk;
  598. if (disk && meta_buffer && meta_len) {
  599. meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
  600. meta_seed, write);
  601. if (IS_ERR(meta)) {
  602. ret = PTR_ERR(meta);
  603. goto out_unmap;
  604. }
  605. }
  606. }
  607. blk_execute_rq(req->q, disk, req, 0);
  608. if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
  609. ret = -EINTR;
  610. else
  611. ret = nvme_req(req)->status;
  612. if (result)
  613. *result = le32_to_cpu(nvme_req(req)->result.u32);
  614. if (meta && !ret && !write) {
  615. if (copy_to_user(meta_buffer, meta, meta_len))
  616. ret = -EFAULT;
  617. }
  618. kfree(meta);
  619. out_unmap:
  620. if (bio)
  621. blk_rq_unmap_user(bio);
  622. out:
  623. blk_mq_free_request(req);
  624. return ret;
  625. }
  626. static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
  627. {
  628. struct nvme_ctrl *ctrl = rq->end_io_data;
  629. blk_mq_free_request(rq);
  630. if (status) {
  631. dev_err(ctrl->device,
  632. "failed nvme_keep_alive_end_io error=%d\n",
  633. status);
  634. return;
  635. }
  636. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  637. }
  638. static int nvme_keep_alive(struct nvme_ctrl *ctrl)
  639. {
  640. struct nvme_command c;
  641. struct request *rq;
  642. memset(&c, 0, sizeof(c));
  643. c.common.opcode = nvme_admin_keep_alive;
  644. rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
  645. NVME_QID_ANY);
  646. if (IS_ERR(rq))
  647. return PTR_ERR(rq);
  648. rq->timeout = ctrl->kato * HZ;
  649. rq->end_io_data = ctrl;
  650. blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
  651. return 0;
  652. }
  653. static void nvme_keep_alive_work(struct work_struct *work)
  654. {
  655. struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
  656. struct nvme_ctrl, ka_work);
  657. if (nvme_keep_alive(ctrl)) {
  658. /* allocation failure, reset the controller */
  659. dev_err(ctrl->device, "keep-alive failed\n");
  660. nvme_reset_ctrl(ctrl);
  661. return;
  662. }
  663. }
  664. void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
  665. {
  666. if (unlikely(ctrl->kato == 0))
  667. return;
  668. INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
  669. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  670. }
  671. EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
  672. void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
  673. {
  674. if (unlikely(ctrl->kato == 0))
  675. return;
  676. cancel_delayed_work_sync(&ctrl->ka_work);
  677. }
  678. EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
  679. static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
  680. {
  681. struct nvme_command c = { };
  682. int error;
  683. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  684. c.identify.opcode = nvme_admin_identify;
  685. c.identify.cns = NVME_ID_CNS_CTRL;
  686. *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
  687. if (!*id)
  688. return -ENOMEM;
  689. error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
  690. sizeof(struct nvme_id_ctrl));
  691. if (error)
  692. kfree(*id);
  693. return error;
  694. }
  695. static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
  696. struct nvme_ns_ids *ids)
  697. {
  698. struct nvme_command c = { };
  699. int status;
  700. void *data;
  701. int pos;
  702. int len;
  703. c.identify.opcode = nvme_admin_identify;
  704. c.identify.nsid = cpu_to_le32(nsid);
  705. c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
  706. data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
  707. if (!data)
  708. return -ENOMEM;
  709. status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
  710. NVME_IDENTIFY_DATA_SIZE);
  711. if (status)
  712. goto free_data;
  713. for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
  714. struct nvme_ns_id_desc *cur = data + pos;
  715. if (cur->nidl == 0)
  716. break;
  717. switch (cur->nidt) {
  718. case NVME_NIDT_EUI64:
  719. if (cur->nidl != NVME_NIDT_EUI64_LEN) {
  720. dev_warn(ctrl->device,
  721. "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
  722. cur->nidl);
  723. goto free_data;
  724. }
  725. len = NVME_NIDT_EUI64_LEN;
  726. memcpy(ids->eui64, data + pos + sizeof(*cur), len);
  727. break;
  728. case NVME_NIDT_NGUID:
  729. if (cur->nidl != NVME_NIDT_NGUID_LEN) {
  730. dev_warn(ctrl->device,
  731. "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
  732. cur->nidl);
  733. goto free_data;
  734. }
  735. len = NVME_NIDT_NGUID_LEN;
  736. memcpy(ids->nguid, data + pos + sizeof(*cur), len);
  737. break;
  738. case NVME_NIDT_UUID:
  739. if (cur->nidl != NVME_NIDT_UUID_LEN) {
  740. dev_warn(ctrl->device,
  741. "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
  742. cur->nidl);
  743. goto free_data;
  744. }
  745. len = NVME_NIDT_UUID_LEN;
  746. uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
  747. break;
  748. default:
  749. /* Skip unnkown types */
  750. len = cur->nidl;
  751. break;
  752. }
  753. len += sizeof(*cur);
  754. }
  755. free_data:
  756. kfree(data);
  757. return status;
  758. }
  759. static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
  760. {
  761. struct nvme_command c = { };
  762. c.identify.opcode = nvme_admin_identify;
  763. c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
  764. c.identify.nsid = cpu_to_le32(nsid);
  765. return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
  766. }
  767. static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
  768. unsigned nsid)
  769. {
  770. struct nvme_id_ns *id;
  771. struct nvme_command c = { };
  772. int error;
  773. /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
  774. c.identify.opcode = nvme_admin_identify;
  775. c.identify.nsid = cpu_to_le32(nsid);
  776. c.identify.cns = NVME_ID_CNS_NS;
  777. id = kmalloc(sizeof(*id), GFP_KERNEL);
  778. if (!id)
  779. return NULL;
  780. error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
  781. if (error) {
  782. dev_warn(ctrl->device, "Identify namespace failed\n");
  783. kfree(id);
  784. return NULL;
  785. }
  786. return id;
  787. }
  788. static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
  789. void *buffer, size_t buflen, u32 *result)
  790. {
  791. struct nvme_command c;
  792. union nvme_result res;
  793. int ret;
  794. memset(&c, 0, sizeof(c));
  795. c.features.opcode = nvme_admin_set_features;
  796. c.features.fid = cpu_to_le32(fid);
  797. c.features.dword11 = cpu_to_le32(dword11);
  798. ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
  799. buffer, buflen, 0, NVME_QID_ANY, 0, 0);
  800. if (ret >= 0 && result)
  801. *result = le32_to_cpu(res.u32);
  802. return ret;
  803. }
  804. int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
  805. {
  806. u32 q_count = (*count - 1) | ((*count - 1) << 16);
  807. u32 result;
  808. int status, nr_io_queues;
  809. status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
  810. &result);
  811. if (status < 0)
  812. return status;
  813. /*
  814. * Degraded controllers might return an error when setting the queue
  815. * count. We still want to be able to bring them online and offer
  816. * access to the admin queue, as that might be only way to fix them up.
  817. */
  818. if (status > 0) {
  819. dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
  820. *count = 0;
  821. } else {
  822. nr_io_queues = min(result & 0xffff, result >> 16) + 1;
  823. *count = min(*count, nr_io_queues);
  824. }
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(nvme_set_queue_count);
  828. static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
  829. {
  830. struct nvme_user_io io;
  831. struct nvme_command c;
  832. unsigned length, meta_len;
  833. void __user *metadata;
  834. if (copy_from_user(&io, uio, sizeof(io)))
  835. return -EFAULT;
  836. if (io.flags)
  837. return -EINVAL;
  838. switch (io.opcode) {
  839. case nvme_cmd_write:
  840. case nvme_cmd_read:
  841. case nvme_cmd_compare:
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. length = (io.nblocks + 1) << ns->lba_shift;
  847. meta_len = (io.nblocks + 1) * ns->ms;
  848. metadata = (void __user *)(uintptr_t)io.metadata;
  849. if (ns->ext) {
  850. length += meta_len;
  851. meta_len = 0;
  852. } else if (meta_len) {
  853. if ((io.metadata & 3) || !io.metadata)
  854. return -EINVAL;
  855. }
  856. memset(&c, 0, sizeof(c));
  857. c.rw.opcode = io.opcode;
  858. c.rw.flags = io.flags;
  859. c.rw.nsid = cpu_to_le32(ns->head->ns_id);
  860. c.rw.slba = cpu_to_le64(io.slba);
  861. c.rw.length = cpu_to_le16(io.nblocks);
  862. c.rw.control = cpu_to_le16(io.control);
  863. c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
  864. c.rw.reftag = cpu_to_le32(io.reftag);
  865. c.rw.apptag = cpu_to_le16(io.apptag);
  866. c.rw.appmask = cpu_to_le16(io.appmask);
  867. return nvme_submit_user_cmd(ns->queue, &c,
  868. (void __user *)(uintptr_t)io.addr, length,
  869. metadata, meta_len, io.slba, NULL, 0);
  870. }
  871. static u32 nvme_known_admin_effects(u8 opcode)
  872. {
  873. switch (opcode) {
  874. case nvme_admin_format_nvm:
  875. return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
  876. NVME_CMD_EFFECTS_CSE_MASK;
  877. case nvme_admin_sanitize_nvm:
  878. return NVME_CMD_EFFECTS_CSE_MASK;
  879. default:
  880. break;
  881. }
  882. return 0;
  883. }
  884. static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  885. u8 opcode)
  886. {
  887. u32 effects = 0;
  888. if (ns) {
  889. if (ctrl->effects)
  890. effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
  891. if (effects & ~NVME_CMD_EFFECTS_CSUPP)
  892. dev_warn(ctrl->device,
  893. "IO command:%02x has unhandled effects:%08x\n",
  894. opcode, effects);
  895. return 0;
  896. }
  897. if (ctrl->effects)
  898. effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
  899. else
  900. effects = nvme_known_admin_effects(opcode);
  901. /*
  902. * For simplicity, IO to all namespaces is quiesced even if the command
  903. * effects say only one namespace is affected.
  904. */
  905. if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
  906. nvme_start_freeze(ctrl);
  907. nvme_wait_freeze(ctrl);
  908. }
  909. return effects;
  910. }
  911. static void nvme_update_formats(struct nvme_ctrl *ctrl)
  912. {
  913. struct nvme_ns *ns;
  914. mutex_lock(&ctrl->namespaces_mutex);
  915. list_for_each_entry(ns, &ctrl->namespaces, list) {
  916. if (ns->disk && nvme_revalidate_disk(ns->disk))
  917. nvme_ns_remove(ns);
  918. }
  919. mutex_unlock(&ctrl->namespaces_mutex);
  920. }
  921. static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
  922. {
  923. /*
  924. * Revalidate LBA changes prior to unfreezing. This is necessary to
  925. * prevent memory corruption if a logical block size was changed by
  926. * this command.
  927. */
  928. if (effects & NVME_CMD_EFFECTS_LBCC)
  929. nvme_update_formats(ctrl);
  930. if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
  931. nvme_unfreeze(ctrl);
  932. if (effects & NVME_CMD_EFFECTS_CCC)
  933. nvme_init_identify(ctrl);
  934. if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
  935. nvme_queue_scan(ctrl);
  936. }
  937. static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
  938. struct nvme_passthru_cmd __user *ucmd)
  939. {
  940. struct nvme_passthru_cmd cmd;
  941. struct nvme_command c;
  942. unsigned timeout = 0;
  943. u32 effects;
  944. int status;
  945. if (!capable(CAP_SYS_ADMIN))
  946. return -EACCES;
  947. if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
  948. return -EFAULT;
  949. if (cmd.flags)
  950. return -EINVAL;
  951. memset(&c, 0, sizeof(c));
  952. c.common.opcode = cmd.opcode;
  953. c.common.flags = cmd.flags;
  954. c.common.nsid = cpu_to_le32(cmd.nsid);
  955. c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
  956. c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
  957. c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
  958. c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
  959. c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
  960. c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
  961. c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
  962. c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
  963. if (cmd.timeout_ms)
  964. timeout = msecs_to_jiffies(cmd.timeout_ms);
  965. effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
  966. status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
  967. (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
  968. (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
  969. 0, &cmd.result, timeout);
  970. nvme_passthru_end(ctrl, effects);
  971. if (status >= 0) {
  972. if (put_user(cmd.result, &ucmd->result))
  973. return -EFAULT;
  974. }
  975. return status;
  976. }
  977. /*
  978. * Issue ioctl requests on the first available path. Note that unlike normal
  979. * block layer requests we will not retry failed request on another controller.
  980. */
  981. static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
  982. struct nvme_ns_head **head, int *srcu_idx)
  983. {
  984. #ifdef CONFIG_NVME_MULTIPATH
  985. if (disk->fops == &nvme_ns_head_ops) {
  986. *head = disk->private_data;
  987. *srcu_idx = srcu_read_lock(&(*head)->srcu);
  988. return nvme_find_path(*head);
  989. }
  990. #endif
  991. *head = NULL;
  992. *srcu_idx = -1;
  993. return disk->private_data;
  994. }
  995. static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
  996. {
  997. if (head)
  998. srcu_read_unlock(&head->srcu, idx);
  999. }
  1000. static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
  1001. {
  1002. switch (cmd) {
  1003. case NVME_IOCTL_ID:
  1004. force_successful_syscall_return();
  1005. return ns->head->ns_id;
  1006. case NVME_IOCTL_ADMIN_CMD:
  1007. return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
  1008. case NVME_IOCTL_IO_CMD:
  1009. return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
  1010. case NVME_IOCTL_SUBMIT_IO:
  1011. return nvme_submit_io(ns, (void __user *)arg);
  1012. default:
  1013. #ifdef CONFIG_NVM
  1014. if (ns->ndev)
  1015. return nvme_nvm_ioctl(ns, cmd, arg);
  1016. #endif
  1017. if (is_sed_ioctl(cmd))
  1018. return sed_ioctl(ns->ctrl->opal_dev, cmd,
  1019. (void __user *) arg);
  1020. return -ENOTTY;
  1021. }
  1022. }
  1023. static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
  1024. unsigned int cmd, unsigned long arg)
  1025. {
  1026. struct nvme_ns_head *head = NULL;
  1027. struct nvme_ns *ns;
  1028. int srcu_idx, ret;
  1029. ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
  1030. if (unlikely(!ns))
  1031. ret = -EWOULDBLOCK;
  1032. else
  1033. ret = nvme_ns_ioctl(ns, cmd, arg);
  1034. nvme_put_ns_from_disk(head, srcu_idx);
  1035. return ret;
  1036. }
  1037. static int nvme_open(struct block_device *bdev, fmode_t mode)
  1038. {
  1039. struct nvme_ns *ns = bdev->bd_disk->private_data;
  1040. #ifdef CONFIG_NVME_MULTIPATH
  1041. /* should never be called due to GENHD_FL_HIDDEN */
  1042. if (WARN_ON_ONCE(ns->head->disk))
  1043. return -ENXIO;
  1044. #endif
  1045. if (!kref_get_unless_zero(&ns->kref))
  1046. return -ENXIO;
  1047. return 0;
  1048. }
  1049. static void nvme_release(struct gendisk *disk, fmode_t mode)
  1050. {
  1051. nvme_put_ns(disk->private_data);
  1052. }
  1053. static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1054. {
  1055. /* some standard values */
  1056. geo->heads = 1 << 6;
  1057. geo->sectors = 1 << 5;
  1058. geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
  1059. return 0;
  1060. }
  1061. #ifdef CONFIG_BLK_DEV_INTEGRITY
  1062. static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
  1063. {
  1064. struct blk_integrity integrity;
  1065. memset(&integrity, 0, sizeof(integrity));
  1066. switch (pi_type) {
  1067. case NVME_NS_DPS_PI_TYPE3:
  1068. integrity.profile = &t10_pi_type3_crc;
  1069. integrity.tag_size = sizeof(u16) + sizeof(u32);
  1070. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  1071. break;
  1072. case NVME_NS_DPS_PI_TYPE1:
  1073. case NVME_NS_DPS_PI_TYPE2:
  1074. integrity.profile = &t10_pi_type1_crc;
  1075. integrity.tag_size = sizeof(u16);
  1076. integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
  1077. break;
  1078. default:
  1079. integrity.profile = NULL;
  1080. break;
  1081. }
  1082. integrity.tuple_size = ms;
  1083. blk_integrity_register(disk, &integrity);
  1084. blk_queue_max_integrity_segments(disk->queue, 1);
  1085. }
  1086. #else
  1087. static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
  1088. {
  1089. }
  1090. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  1091. static void nvme_set_chunk_size(struct nvme_ns *ns)
  1092. {
  1093. u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
  1094. blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
  1095. }
  1096. static void nvme_config_discard(struct nvme_ctrl *ctrl,
  1097. unsigned stream_alignment, struct request_queue *queue)
  1098. {
  1099. u32 size = queue_logical_block_size(queue);
  1100. if (stream_alignment)
  1101. size *= stream_alignment;
  1102. BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
  1103. NVME_DSM_MAX_RANGES);
  1104. queue->limits.discard_alignment = size;
  1105. queue->limits.discard_granularity = size;
  1106. blk_queue_max_discard_sectors(queue, UINT_MAX);
  1107. blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
  1108. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
  1109. if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
  1110. blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
  1111. }
  1112. static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
  1113. struct nvme_id_ns *id, struct nvme_ns_ids *ids)
  1114. {
  1115. memset(ids, 0, sizeof(*ids));
  1116. if (ctrl->vs >= NVME_VS(1, 1, 0))
  1117. memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
  1118. if (ctrl->vs >= NVME_VS(1, 2, 0))
  1119. memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
  1120. if (ctrl->vs >= NVME_VS(1, 3, 0)) {
  1121. /* Don't treat error as fatal we potentially
  1122. * already have a NGUID or EUI-64
  1123. */
  1124. if (nvme_identify_ns_descs(ctrl, nsid, ids))
  1125. dev_warn(ctrl->device,
  1126. "%s: Identify Descriptors failed\n", __func__);
  1127. }
  1128. }
  1129. static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
  1130. {
  1131. return !uuid_is_null(&ids->uuid) ||
  1132. memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
  1133. memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
  1134. }
  1135. static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
  1136. {
  1137. return uuid_equal(&a->uuid, &b->uuid) &&
  1138. memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
  1139. memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
  1140. }
  1141. static void nvme_update_disk_info(struct gendisk *disk,
  1142. struct nvme_ns *ns, struct nvme_id_ns *id)
  1143. {
  1144. sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
  1145. unsigned stream_alignment = 0;
  1146. if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
  1147. stream_alignment = ns->sws * ns->sgs;
  1148. blk_mq_freeze_queue(disk->queue);
  1149. blk_integrity_unregister(disk);
  1150. blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
  1151. if (ns->ms && !ns->ext &&
  1152. (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
  1153. nvme_init_integrity(disk, ns->ms, ns->pi_type);
  1154. if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
  1155. capacity = 0;
  1156. set_capacity(disk, capacity);
  1157. if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
  1158. nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
  1159. blk_mq_unfreeze_queue(disk->queue);
  1160. }
  1161. static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
  1162. {
  1163. struct nvme_ns *ns = disk->private_data;
  1164. /*
  1165. * If identify namespace failed, use default 512 byte block size so
  1166. * block layer can use before failing read/write for 0 capacity.
  1167. */
  1168. ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
  1169. if (ns->lba_shift == 0)
  1170. ns->lba_shift = 9;
  1171. ns->noiob = le16_to_cpu(id->noiob);
  1172. ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
  1173. ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
  1174. /* the PI implementation requires metadata equal t10 pi tuple size */
  1175. if (ns->ms == sizeof(struct t10_pi_tuple))
  1176. ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
  1177. else
  1178. ns->pi_type = 0;
  1179. if (ns->noiob)
  1180. nvme_set_chunk_size(ns);
  1181. nvme_update_disk_info(disk, ns, id);
  1182. #ifdef CONFIG_NVME_MULTIPATH
  1183. if (ns->head->disk)
  1184. nvme_update_disk_info(ns->head->disk, ns, id);
  1185. #endif
  1186. }
  1187. static int nvme_revalidate_disk(struct gendisk *disk)
  1188. {
  1189. struct nvme_ns *ns = disk->private_data;
  1190. struct nvme_ctrl *ctrl = ns->ctrl;
  1191. struct nvme_id_ns *id;
  1192. struct nvme_ns_ids ids;
  1193. int ret = 0;
  1194. if (test_bit(NVME_NS_DEAD, &ns->flags)) {
  1195. set_capacity(disk, 0);
  1196. return -ENODEV;
  1197. }
  1198. id = nvme_identify_ns(ctrl, ns->head->ns_id);
  1199. if (!id)
  1200. return -ENODEV;
  1201. if (id->ncap == 0) {
  1202. ret = -ENODEV;
  1203. goto out;
  1204. }
  1205. __nvme_revalidate_disk(disk, id);
  1206. nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
  1207. if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
  1208. dev_err(ctrl->device,
  1209. "identifiers changed for nsid %d\n", ns->head->ns_id);
  1210. ret = -ENODEV;
  1211. }
  1212. out:
  1213. kfree(id);
  1214. return ret;
  1215. }
  1216. static char nvme_pr_type(enum pr_type type)
  1217. {
  1218. switch (type) {
  1219. case PR_WRITE_EXCLUSIVE:
  1220. return 1;
  1221. case PR_EXCLUSIVE_ACCESS:
  1222. return 2;
  1223. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  1224. return 3;
  1225. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  1226. return 4;
  1227. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  1228. return 5;
  1229. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  1230. return 6;
  1231. default:
  1232. return 0;
  1233. }
  1234. };
  1235. static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
  1236. u64 key, u64 sa_key, u8 op)
  1237. {
  1238. struct nvme_ns_head *head = NULL;
  1239. struct nvme_ns *ns;
  1240. struct nvme_command c;
  1241. int srcu_idx, ret;
  1242. u8 data[16] = { 0, };
  1243. put_unaligned_le64(key, &data[0]);
  1244. put_unaligned_le64(sa_key, &data[8]);
  1245. memset(&c, 0, sizeof(c));
  1246. c.common.opcode = op;
  1247. c.common.nsid = cpu_to_le32(head->ns_id);
  1248. c.common.cdw10[0] = cpu_to_le32(cdw10);
  1249. ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
  1250. if (unlikely(!ns))
  1251. ret = -EWOULDBLOCK;
  1252. else
  1253. ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
  1254. nvme_put_ns_from_disk(head, srcu_idx);
  1255. return ret;
  1256. }
  1257. static int nvme_pr_register(struct block_device *bdev, u64 old,
  1258. u64 new, unsigned flags)
  1259. {
  1260. u32 cdw10;
  1261. if (flags & ~PR_FL_IGNORE_KEY)
  1262. return -EOPNOTSUPP;
  1263. cdw10 = old ? 2 : 0;
  1264. cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
  1265. cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
  1266. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
  1267. }
  1268. static int nvme_pr_reserve(struct block_device *bdev, u64 key,
  1269. enum pr_type type, unsigned flags)
  1270. {
  1271. u32 cdw10;
  1272. if (flags & ~PR_FL_IGNORE_KEY)
  1273. return -EOPNOTSUPP;
  1274. cdw10 = nvme_pr_type(type) << 8;
  1275. cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
  1276. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
  1277. }
  1278. static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
  1279. enum pr_type type, bool abort)
  1280. {
  1281. u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
  1282. return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
  1283. }
  1284. static int nvme_pr_clear(struct block_device *bdev, u64 key)
  1285. {
  1286. u32 cdw10 = 1 | (key ? 1 << 3 : 0);
  1287. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
  1288. }
  1289. static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  1290. {
  1291. u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
  1292. return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
  1293. }
  1294. static const struct pr_ops nvme_pr_ops = {
  1295. .pr_register = nvme_pr_register,
  1296. .pr_reserve = nvme_pr_reserve,
  1297. .pr_release = nvme_pr_release,
  1298. .pr_preempt = nvme_pr_preempt,
  1299. .pr_clear = nvme_pr_clear,
  1300. };
  1301. #ifdef CONFIG_BLK_SED_OPAL
  1302. int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
  1303. bool send)
  1304. {
  1305. struct nvme_ctrl *ctrl = data;
  1306. struct nvme_command cmd;
  1307. memset(&cmd, 0, sizeof(cmd));
  1308. if (send)
  1309. cmd.common.opcode = nvme_admin_security_send;
  1310. else
  1311. cmd.common.opcode = nvme_admin_security_recv;
  1312. cmd.common.nsid = 0;
  1313. cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
  1314. cmd.common.cdw10[1] = cpu_to_le32(len);
  1315. return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
  1316. ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
  1317. }
  1318. EXPORT_SYMBOL_GPL(nvme_sec_submit);
  1319. #endif /* CONFIG_BLK_SED_OPAL */
  1320. static const struct block_device_operations nvme_fops = {
  1321. .owner = THIS_MODULE,
  1322. .ioctl = nvme_ioctl,
  1323. .compat_ioctl = nvme_ioctl,
  1324. .open = nvme_open,
  1325. .release = nvme_release,
  1326. .getgeo = nvme_getgeo,
  1327. .revalidate_disk= nvme_revalidate_disk,
  1328. .pr_ops = &nvme_pr_ops,
  1329. };
  1330. #ifdef CONFIG_NVME_MULTIPATH
  1331. static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
  1332. {
  1333. struct nvme_ns_head *head = bdev->bd_disk->private_data;
  1334. if (!kref_get_unless_zero(&head->ref))
  1335. return -ENXIO;
  1336. return 0;
  1337. }
  1338. static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
  1339. {
  1340. nvme_put_ns_head(disk->private_data);
  1341. }
  1342. const struct block_device_operations nvme_ns_head_ops = {
  1343. .owner = THIS_MODULE,
  1344. .open = nvme_ns_head_open,
  1345. .release = nvme_ns_head_release,
  1346. .ioctl = nvme_ioctl,
  1347. .compat_ioctl = nvme_ioctl,
  1348. .getgeo = nvme_getgeo,
  1349. .pr_ops = &nvme_pr_ops,
  1350. };
  1351. #endif /* CONFIG_NVME_MULTIPATH */
  1352. static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
  1353. {
  1354. unsigned long timeout =
  1355. ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
  1356. u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
  1357. int ret;
  1358. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  1359. if (csts == ~0)
  1360. return -ENODEV;
  1361. if ((csts & NVME_CSTS_RDY) == bit)
  1362. break;
  1363. msleep(100);
  1364. if (fatal_signal_pending(current))
  1365. return -EINTR;
  1366. if (time_after(jiffies, timeout)) {
  1367. dev_err(ctrl->device,
  1368. "Device not ready; aborting %s\n", enabled ?
  1369. "initialisation" : "reset");
  1370. return -ENODEV;
  1371. }
  1372. }
  1373. return ret;
  1374. }
  1375. /*
  1376. * If the device has been passed off to us in an enabled state, just clear
  1377. * the enabled bit. The spec says we should set the 'shutdown notification
  1378. * bits', but doing so may cause the device to complete commands to the
  1379. * admin queue ... and we don't know what memory that might be pointing at!
  1380. */
  1381. int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  1382. {
  1383. int ret;
  1384. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  1385. ctrl->ctrl_config &= ~NVME_CC_ENABLE;
  1386. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  1387. if (ret)
  1388. return ret;
  1389. if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
  1390. msleep(NVME_QUIRK_DELAY_AMOUNT);
  1391. return nvme_wait_ready(ctrl, cap, false);
  1392. }
  1393. EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
  1394. int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
  1395. {
  1396. /*
  1397. * Default to a 4K page size, with the intention to update this
  1398. * path in the future to accomodate architectures with differing
  1399. * kernel and IO page sizes.
  1400. */
  1401. unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
  1402. int ret;
  1403. if (page_shift < dev_page_min) {
  1404. dev_err(ctrl->device,
  1405. "Minimum device page size %u too large for host (%u)\n",
  1406. 1 << dev_page_min, 1 << page_shift);
  1407. return -ENODEV;
  1408. }
  1409. ctrl->page_size = 1 << page_shift;
  1410. ctrl->ctrl_config = NVME_CC_CSS_NVM;
  1411. ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
  1412. ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
  1413. ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
  1414. ctrl->ctrl_config |= NVME_CC_ENABLE;
  1415. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  1416. if (ret)
  1417. return ret;
  1418. return nvme_wait_ready(ctrl, cap, true);
  1419. }
  1420. EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
  1421. int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
  1422. {
  1423. unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
  1424. u32 csts;
  1425. int ret;
  1426. ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
  1427. ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
  1428. ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
  1429. if (ret)
  1430. return ret;
  1431. while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
  1432. if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
  1433. break;
  1434. msleep(100);
  1435. if (fatal_signal_pending(current))
  1436. return -EINTR;
  1437. if (time_after(jiffies, timeout)) {
  1438. dev_err(ctrl->device,
  1439. "Device shutdown incomplete; abort shutdown\n");
  1440. return -ENODEV;
  1441. }
  1442. }
  1443. return ret;
  1444. }
  1445. EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
  1446. static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
  1447. struct request_queue *q)
  1448. {
  1449. bool vwc = false;
  1450. if (ctrl->max_hw_sectors) {
  1451. u32 max_segments =
  1452. (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
  1453. blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
  1454. blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
  1455. }
  1456. if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
  1457. blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
  1458. blk_queue_virt_boundary(q, ctrl->page_size - 1);
  1459. if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
  1460. vwc = true;
  1461. blk_queue_write_cache(q, vwc, vwc);
  1462. }
  1463. static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
  1464. {
  1465. __le64 ts;
  1466. int ret;
  1467. if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
  1468. return 0;
  1469. ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
  1470. ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
  1471. NULL);
  1472. if (ret)
  1473. dev_warn_once(ctrl->device,
  1474. "could not set timestamp (%d)\n", ret);
  1475. return ret;
  1476. }
  1477. static int nvme_configure_apst(struct nvme_ctrl *ctrl)
  1478. {
  1479. /*
  1480. * APST (Autonomous Power State Transition) lets us program a
  1481. * table of power state transitions that the controller will
  1482. * perform automatically. We configure it with a simple
  1483. * heuristic: we are willing to spend at most 2% of the time
  1484. * transitioning between power states. Therefore, when running
  1485. * in any given state, we will enter the next lower-power
  1486. * non-operational state after waiting 50 * (enlat + exlat)
  1487. * microseconds, as long as that state's exit latency is under
  1488. * the requested maximum latency.
  1489. *
  1490. * We will not autonomously enter any non-operational state for
  1491. * which the total latency exceeds ps_max_latency_us. Users
  1492. * can set ps_max_latency_us to zero to turn off APST.
  1493. */
  1494. unsigned apste;
  1495. struct nvme_feat_auto_pst *table;
  1496. u64 max_lat_us = 0;
  1497. int max_ps = -1;
  1498. int ret;
  1499. /*
  1500. * If APST isn't supported or if we haven't been initialized yet,
  1501. * then don't do anything.
  1502. */
  1503. if (!ctrl->apsta)
  1504. return 0;
  1505. if (ctrl->npss > 31) {
  1506. dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
  1507. return 0;
  1508. }
  1509. table = kzalloc(sizeof(*table), GFP_KERNEL);
  1510. if (!table)
  1511. return 0;
  1512. if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
  1513. /* Turn off APST. */
  1514. apste = 0;
  1515. dev_dbg(ctrl->device, "APST disabled\n");
  1516. } else {
  1517. __le64 target = cpu_to_le64(0);
  1518. int state;
  1519. /*
  1520. * Walk through all states from lowest- to highest-power.
  1521. * According to the spec, lower-numbered states use more
  1522. * power. NPSS, despite the name, is the index of the
  1523. * lowest-power state, not the number of states.
  1524. */
  1525. for (state = (int)ctrl->npss; state >= 0; state--) {
  1526. u64 total_latency_us, exit_latency_us, transition_ms;
  1527. if (target)
  1528. table->entries[state] = target;
  1529. /*
  1530. * Don't allow transitions to the deepest state
  1531. * if it's quirked off.
  1532. */
  1533. if (state == ctrl->npss &&
  1534. (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
  1535. continue;
  1536. /*
  1537. * Is this state a useful non-operational state for
  1538. * higher-power states to autonomously transition to?
  1539. */
  1540. if (!(ctrl->psd[state].flags &
  1541. NVME_PS_FLAGS_NON_OP_STATE))
  1542. continue;
  1543. exit_latency_us =
  1544. (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
  1545. if (exit_latency_us > ctrl->ps_max_latency_us)
  1546. continue;
  1547. total_latency_us =
  1548. exit_latency_us +
  1549. le32_to_cpu(ctrl->psd[state].entry_lat);
  1550. /*
  1551. * This state is good. Use it as the APST idle
  1552. * target for higher power states.
  1553. */
  1554. transition_ms = total_latency_us + 19;
  1555. do_div(transition_ms, 20);
  1556. if (transition_ms > (1 << 24) - 1)
  1557. transition_ms = (1 << 24) - 1;
  1558. target = cpu_to_le64((state << 3) |
  1559. (transition_ms << 8));
  1560. if (max_ps == -1)
  1561. max_ps = state;
  1562. if (total_latency_us > max_lat_us)
  1563. max_lat_us = total_latency_us;
  1564. }
  1565. apste = 1;
  1566. if (max_ps == -1) {
  1567. dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
  1568. } else {
  1569. dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
  1570. max_ps, max_lat_us, (int)sizeof(*table), table);
  1571. }
  1572. }
  1573. ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
  1574. table, sizeof(*table), NULL);
  1575. if (ret)
  1576. dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
  1577. kfree(table);
  1578. return ret;
  1579. }
  1580. static void nvme_set_latency_tolerance(struct device *dev, s32 val)
  1581. {
  1582. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  1583. u64 latency;
  1584. switch (val) {
  1585. case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
  1586. case PM_QOS_LATENCY_ANY:
  1587. latency = U64_MAX;
  1588. break;
  1589. default:
  1590. latency = val;
  1591. }
  1592. if (ctrl->ps_max_latency_us != latency) {
  1593. ctrl->ps_max_latency_us = latency;
  1594. nvme_configure_apst(ctrl);
  1595. }
  1596. }
  1597. struct nvme_core_quirk_entry {
  1598. /*
  1599. * NVMe model and firmware strings are padded with spaces. For
  1600. * simplicity, strings in the quirk table are padded with NULLs
  1601. * instead.
  1602. */
  1603. u16 vid;
  1604. const char *mn;
  1605. const char *fr;
  1606. unsigned long quirks;
  1607. };
  1608. static const struct nvme_core_quirk_entry core_quirks[] = {
  1609. {
  1610. /*
  1611. * This Toshiba device seems to die using any APST states. See:
  1612. * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
  1613. */
  1614. .vid = 0x1179,
  1615. .mn = "THNSF5256GPUK TOSHIBA",
  1616. .quirks = NVME_QUIRK_NO_APST,
  1617. }
  1618. };
  1619. /* match is null-terminated but idstr is space-padded. */
  1620. static bool string_matches(const char *idstr, const char *match, size_t len)
  1621. {
  1622. size_t matchlen;
  1623. if (!match)
  1624. return true;
  1625. matchlen = strlen(match);
  1626. WARN_ON_ONCE(matchlen > len);
  1627. if (memcmp(idstr, match, matchlen))
  1628. return false;
  1629. for (; matchlen < len; matchlen++)
  1630. if (idstr[matchlen] != ' ')
  1631. return false;
  1632. return true;
  1633. }
  1634. static bool quirk_matches(const struct nvme_id_ctrl *id,
  1635. const struct nvme_core_quirk_entry *q)
  1636. {
  1637. return q->vid == le16_to_cpu(id->vid) &&
  1638. string_matches(id->mn, q->mn, sizeof(id->mn)) &&
  1639. string_matches(id->fr, q->fr, sizeof(id->fr));
  1640. }
  1641. static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
  1642. struct nvme_id_ctrl *id)
  1643. {
  1644. size_t nqnlen;
  1645. int off;
  1646. nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
  1647. if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
  1648. strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
  1649. return;
  1650. }
  1651. if (ctrl->vs >= NVME_VS(1, 2, 1))
  1652. dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
  1653. /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
  1654. off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
  1655. "nqn.2014.08.org.nvmexpress:%4x%4x",
  1656. le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
  1657. memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
  1658. off += sizeof(id->sn);
  1659. memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
  1660. off += sizeof(id->mn);
  1661. memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
  1662. }
  1663. static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
  1664. {
  1665. ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
  1666. kfree(subsys);
  1667. }
  1668. static void nvme_release_subsystem(struct device *dev)
  1669. {
  1670. __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
  1671. }
  1672. static void nvme_destroy_subsystem(struct kref *ref)
  1673. {
  1674. struct nvme_subsystem *subsys =
  1675. container_of(ref, struct nvme_subsystem, ref);
  1676. mutex_lock(&nvme_subsystems_lock);
  1677. list_del(&subsys->entry);
  1678. mutex_unlock(&nvme_subsystems_lock);
  1679. ida_destroy(&subsys->ns_ida);
  1680. device_del(&subsys->dev);
  1681. put_device(&subsys->dev);
  1682. }
  1683. static void nvme_put_subsystem(struct nvme_subsystem *subsys)
  1684. {
  1685. kref_put(&subsys->ref, nvme_destroy_subsystem);
  1686. }
  1687. static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
  1688. {
  1689. struct nvme_subsystem *subsys;
  1690. lockdep_assert_held(&nvme_subsystems_lock);
  1691. list_for_each_entry(subsys, &nvme_subsystems, entry) {
  1692. if (strcmp(subsys->subnqn, subsysnqn))
  1693. continue;
  1694. if (!kref_get_unless_zero(&subsys->ref))
  1695. continue;
  1696. return subsys;
  1697. }
  1698. return NULL;
  1699. }
  1700. #define SUBSYS_ATTR_RO(_name, _mode, _show) \
  1701. struct device_attribute subsys_attr_##_name = \
  1702. __ATTR(_name, _mode, _show, NULL)
  1703. static ssize_t nvme_subsys_show_nqn(struct device *dev,
  1704. struct device_attribute *attr,
  1705. char *buf)
  1706. {
  1707. struct nvme_subsystem *subsys =
  1708. container_of(dev, struct nvme_subsystem, dev);
  1709. return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
  1710. }
  1711. static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
  1712. #define nvme_subsys_show_str_function(field) \
  1713. static ssize_t subsys_##field##_show(struct device *dev, \
  1714. struct device_attribute *attr, char *buf) \
  1715. { \
  1716. struct nvme_subsystem *subsys = \
  1717. container_of(dev, struct nvme_subsystem, dev); \
  1718. return sprintf(buf, "%.*s\n", \
  1719. (int)sizeof(subsys->field), subsys->field); \
  1720. } \
  1721. static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
  1722. nvme_subsys_show_str_function(model);
  1723. nvme_subsys_show_str_function(serial);
  1724. nvme_subsys_show_str_function(firmware_rev);
  1725. static struct attribute *nvme_subsys_attrs[] = {
  1726. &subsys_attr_model.attr,
  1727. &subsys_attr_serial.attr,
  1728. &subsys_attr_firmware_rev.attr,
  1729. &subsys_attr_subsysnqn.attr,
  1730. NULL,
  1731. };
  1732. static struct attribute_group nvme_subsys_attrs_group = {
  1733. .attrs = nvme_subsys_attrs,
  1734. };
  1735. static const struct attribute_group *nvme_subsys_attrs_groups[] = {
  1736. &nvme_subsys_attrs_group,
  1737. NULL,
  1738. };
  1739. static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  1740. {
  1741. struct nvme_subsystem *subsys, *found;
  1742. int ret;
  1743. subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
  1744. if (!subsys)
  1745. return -ENOMEM;
  1746. ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
  1747. if (ret < 0) {
  1748. kfree(subsys);
  1749. return ret;
  1750. }
  1751. subsys->instance = ret;
  1752. mutex_init(&subsys->lock);
  1753. kref_init(&subsys->ref);
  1754. INIT_LIST_HEAD(&subsys->ctrls);
  1755. INIT_LIST_HEAD(&subsys->nsheads);
  1756. nvme_init_subnqn(subsys, ctrl, id);
  1757. memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
  1758. memcpy(subsys->model, id->mn, sizeof(subsys->model));
  1759. memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
  1760. subsys->vendor_id = le16_to_cpu(id->vid);
  1761. subsys->cmic = id->cmic;
  1762. subsys->dev.class = nvme_subsys_class;
  1763. subsys->dev.release = nvme_release_subsystem;
  1764. subsys->dev.groups = nvme_subsys_attrs_groups;
  1765. dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
  1766. device_initialize(&subsys->dev);
  1767. mutex_lock(&nvme_subsystems_lock);
  1768. found = __nvme_find_get_subsystem(subsys->subnqn);
  1769. if (found) {
  1770. /*
  1771. * Verify that the subsystem actually supports multiple
  1772. * controllers, else bail out.
  1773. */
  1774. if (!(id->cmic & (1 << 1))) {
  1775. dev_err(ctrl->device,
  1776. "ignoring ctrl due to duplicate subnqn (%s).\n",
  1777. found->subnqn);
  1778. nvme_put_subsystem(found);
  1779. ret = -EINVAL;
  1780. goto out_unlock;
  1781. }
  1782. __nvme_release_subsystem(subsys);
  1783. subsys = found;
  1784. } else {
  1785. ret = device_add(&subsys->dev);
  1786. if (ret) {
  1787. dev_err(ctrl->device,
  1788. "failed to register subsystem device.\n");
  1789. goto out_unlock;
  1790. }
  1791. ida_init(&subsys->ns_ida);
  1792. list_add_tail(&subsys->entry, &nvme_subsystems);
  1793. }
  1794. ctrl->subsys = subsys;
  1795. mutex_unlock(&nvme_subsystems_lock);
  1796. if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
  1797. dev_name(ctrl->device))) {
  1798. dev_err(ctrl->device,
  1799. "failed to create sysfs link from subsystem.\n");
  1800. /* the transport driver will eventually put the subsystem */
  1801. return -EINVAL;
  1802. }
  1803. mutex_lock(&subsys->lock);
  1804. list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
  1805. mutex_unlock(&subsys->lock);
  1806. return 0;
  1807. out_unlock:
  1808. mutex_unlock(&nvme_subsystems_lock);
  1809. put_device(&subsys->dev);
  1810. return ret;
  1811. }
  1812. static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
  1813. size_t size)
  1814. {
  1815. struct nvme_command c = { };
  1816. c.common.opcode = nvme_admin_get_log_page;
  1817. c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
  1818. c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
  1819. return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
  1820. }
  1821. static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
  1822. {
  1823. int ret;
  1824. if (!ctrl->effects)
  1825. ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
  1826. if (!ctrl->effects)
  1827. return 0;
  1828. ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
  1829. sizeof(*ctrl->effects));
  1830. if (ret) {
  1831. kfree(ctrl->effects);
  1832. ctrl->effects = NULL;
  1833. }
  1834. return ret;
  1835. }
  1836. /*
  1837. * Initialize the cached copies of the Identify data and various controller
  1838. * register in our nvme_ctrl structure. This should be called as soon as
  1839. * the admin queue is fully up and running.
  1840. */
  1841. int nvme_init_identify(struct nvme_ctrl *ctrl)
  1842. {
  1843. struct nvme_id_ctrl *id;
  1844. u64 cap;
  1845. int ret, page_shift;
  1846. u32 max_hw_sectors;
  1847. bool prev_apst_enabled;
  1848. ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
  1849. if (ret) {
  1850. dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
  1851. return ret;
  1852. }
  1853. ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
  1854. if (ret) {
  1855. dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
  1856. return ret;
  1857. }
  1858. page_shift = NVME_CAP_MPSMIN(cap) + 12;
  1859. if (ctrl->vs >= NVME_VS(1, 1, 0))
  1860. ctrl->subsystem = NVME_CAP_NSSRC(cap);
  1861. ret = nvme_identify_ctrl(ctrl, &id);
  1862. if (ret) {
  1863. dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
  1864. return -EIO;
  1865. }
  1866. if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
  1867. ret = nvme_get_effects_log(ctrl);
  1868. if (ret < 0)
  1869. return ret;
  1870. }
  1871. if (!ctrl->identified) {
  1872. int i;
  1873. ret = nvme_init_subsystem(ctrl, id);
  1874. if (ret)
  1875. goto out_free;
  1876. /*
  1877. * Check for quirks. Quirk can depend on firmware version,
  1878. * so, in principle, the set of quirks present can change
  1879. * across a reset. As a possible future enhancement, we
  1880. * could re-scan for quirks every time we reinitialize
  1881. * the device, but we'd have to make sure that the driver
  1882. * behaves intelligently if the quirks change.
  1883. */
  1884. for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
  1885. if (quirk_matches(id, &core_quirks[i]))
  1886. ctrl->quirks |= core_quirks[i].quirks;
  1887. }
  1888. }
  1889. if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
  1890. dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
  1891. ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
  1892. }
  1893. ctrl->oacs = le16_to_cpu(id->oacs);
  1894. ctrl->oncs = le16_to_cpup(&id->oncs);
  1895. atomic_set(&ctrl->abort_limit, id->acl + 1);
  1896. ctrl->vwc = id->vwc;
  1897. ctrl->cntlid = le16_to_cpup(&id->cntlid);
  1898. if (id->mdts)
  1899. max_hw_sectors = 1 << (id->mdts + page_shift - 9);
  1900. else
  1901. max_hw_sectors = UINT_MAX;
  1902. ctrl->max_hw_sectors =
  1903. min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
  1904. nvme_set_queue_limits(ctrl, ctrl->admin_q);
  1905. ctrl->sgls = le32_to_cpu(id->sgls);
  1906. ctrl->kas = le16_to_cpu(id->kas);
  1907. if (id->rtd3e) {
  1908. /* us -> s */
  1909. u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
  1910. ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
  1911. shutdown_timeout, 60);
  1912. if (ctrl->shutdown_timeout != shutdown_timeout)
  1913. dev_warn(ctrl->device,
  1914. "Shutdown timeout set to %u seconds\n",
  1915. ctrl->shutdown_timeout);
  1916. } else
  1917. ctrl->shutdown_timeout = shutdown_timeout;
  1918. ctrl->npss = id->npss;
  1919. ctrl->apsta = id->apsta;
  1920. prev_apst_enabled = ctrl->apst_enabled;
  1921. if (ctrl->quirks & NVME_QUIRK_NO_APST) {
  1922. if (force_apst && id->apsta) {
  1923. dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
  1924. ctrl->apst_enabled = true;
  1925. } else {
  1926. ctrl->apst_enabled = false;
  1927. }
  1928. } else {
  1929. ctrl->apst_enabled = id->apsta;
  1930. }
  1931. memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
  1932. if (ctrl->ops->flags & NVME_F_FABRICS) {
  1933. ctrl->icdoff = le16_to_cpu(id->icdoff);
  1934. ctrl->ioccsz = le32_to_cpu(id->ioccsz);
  1935. ctrl->iorcsz = le32_to_cpu(id->iorcsz);
  1936. ctrl->maxcmd = le16_to_cpu(id->maxcmd);
  1937. /*
  1938. * In fabrics we need to verify the cntlid matches the
  1939. * admin connect
  1940. */
  1941. if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
  1942. ret = -EINVAL;
  1943. goto out_free;
  1944. }
  1945. if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
  1946. dev_err(ctrl->device,
  1947. "keep-alive support is mandatory for fabrics\n");
  1948. ret = -EINVAL;
  1949. goto out_free;
  1950. }
  1951. } else {
  1952. ctrl->cntlid = le16_to_cpu(id->cntlid);
  1953. ctrl->hmpre = le32_to_cpu(id->hmpre);
  1954. ctrl->hmmin = le32_to_cpu(id->hmmin);
  1955. ctrl->hmminds = le32_to_cpu(id->hmminds);
  1956. ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
  1957. }
  1958. kfree(id);
  1959. if (ctrl->apst_enabled && !prev_apst_enabled)
  1960. dev_pm_qos_expose_latency_tolerance(ctrl->device);
  1961. else if (!ctrl->apst_enabled && prev_apst_enabled)
  1962. dev_pm_qos_hide_latency_tolerance(ctrl->device);
  1963. ret = nvme_configure_apst(ctrl);
  1964. if (ret < 0)
  1965. return ret;
  1966. ret = nvme_configure_timestamp(ctrl);
  1967. if (ret < 0)
  1968. return ret;
  1969. ret = nvme_configure_directives(ctrl);
  1970. if (ret < 0)
  1971. return ret;
  1972. ctrl->identified = true;
  1973. return 0;
  1974. out_free:
  1975. kfree(id);
  1976. return ret;
  1977. }
  1978. EXPORT_SYMBOL_GPL(nvme_init_identify);
  1979. static int nvme_dev_open(struct inode *inode, struct file *file)
  1980. {
  1981. struct nvme_ctrl *ctrl =
  1982. container_of(inode->i_cdev, struct nvme_ctrl, cdev);
  1983. if (ctrl->state != NVME_CTRL_LIVE)
  1984. return -EWOULDBLOCK;
  1985. file->private_data = ctrl;
  1986. return 0;
  1987. }
  1988. static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
  1989. {
  1990. struct nvme_ns *ns;
  1991. int ret;
  1992. mutex_lock(&ctrl->namespaces_mutex);
  1993. if (list_empty(&ctrl->namespaces)) {
  1994. ret = -ENOTTY;
  1995. goto out_unlock;
  1996. }
  1997. ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
  1998. if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
  1999. dev_warn(ctrl->device,
  2000. "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
  2001. ret = -EINVAL;
  2002. goto out_unlock;
  2003. }
  2004. dev_warn(ctrl->device,
  2005. "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
  2006. kref_get(&ns->kref);
  2007. mutex_unlock(&ctrl->namespaces_mutex);
  2008. ret = nvme_user_cmd(ctrl, ns, argp);
  2009. nvme_put_ns(ns);
  2010. return ret;
  2011. out_unlock:
  2012. mutex_unlock(&ctrl->namespaces_mutex);
  2013. return ret;
  2014. }
  2015. static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
  2016. unsigned long arg)
  2017. {
  2018. struct nvme_ctrl *ctrl = file->private_data;
  2019. void __user *argp = (void __user *)arg;
  2020. switch (cmd) {
  2021. case NVME_IOCTL_ADMIN_CMD:
  2022. return nvme_user_cmd(ctrl, NULL, argp);
  2023. case NVME_IOCTL_IO_CMD:
  2024. return nvme_dev_user_cmd(ctrl, argp);
  2025. case NVME_IOCTL_RESET:
  2026. dev_warn(ctrl->device, "resetting controller\n");
  2027. return nvme_reset_ctrl_sync(ctrl);
  2028. case NVME_IOCTL_SUBSYS_RESET:
  2029. return nvme_reset_subsystem(ctrl);
  2030. case NVME_IOCTL_RESCAN:
  2031. nvme_queue_scan(ctrl);
  2032. return 0;
  2033. default:
  2034. return -ENOTTY;
  2035. }
  2036. }
  2037. static const struct file_operations nvme_dev_fops = {
  2038. .owner = THIS_MODULE,
  2039. .open = nvme_dev_open,
  2040. .unlocked_ioctl = nvme_dev_ioctl,
  2041. .compat_ioctl = nvme_dev_ioctl,
  2042. };
  2043. static ssize_t nvme_sysfs_reset(struct device *dev,
  2044. struct device_attribute *attr, const char *buf,
  2045. size_t count)
  2046. {
  2047. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2048. int ret;
  2049. ret = nvme_reset_ctrl_sync(ctrl);
  2050. if (ret < 0)
  2051. return ret;
  2052. return count;
  2053. }
  2054. static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
  2055. static ssize_t nvme_sysfs_rescan(struct device *dev,
  2056. struct device_attribute *attr, const char *buf,
  2057. size_t count)
  2058. {
  2059. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2060. nvme_queue_scan(ctrl);
  2061. return count;
  2062. }
  2063. static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
  2064. static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
  2065. {
  2066. struct gendisk *disk = dev_to_disk(dev);
  2067. if (disk->fops == &nvme_fops)
  2068. return nvme_get_ns_from_dev(dev)->head;
  2069. else
  2070. return disk->private_data;
  2071. }
  2072. static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
  2073. char *buf)
  2074. {
  2075. struct nvme_ns_head *head = dev_to_ns_head(dev);
  2076. struct nvme_ns_ids *ids = &head->ids;
  2077. struct nvme_subsystem *subsys = head->subsys;
  2078. int serial_len = sizeof(subsys->serial);
  2079. int model_len = sizeof(subsys->model);
  2080. if (!uuid_is_null(&ids->uuid))
  2081. return sprintf(buf, "uuid.%pU\n", &ids->uuid);
  2082. if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  2083. return sprintf(buf, "eui.%16phN\n", ids->nguid);
  2084. if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
  2085. return sprintf(buf, "eui.%8phN\n", ids->eui64);
  2086. while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
  2087. subsys->serial[serial_len - 1] == '\0'))
  2088. serial_len--;
  2089. while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
  2090. subsys->model[model_len - 1] == '\0'))
  2091. model_len--;
  2092. return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
  2093. serial_len, subsys->serial, model_len, subsys->model,
  2094. head->ns_id);
  2095. }
  2096. static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
  2097. static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
  2098. char *buf)
  2099. {
  2100. return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
  2101. }
  2102. static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
  2103. static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
  2104. char *buf)
  2105. {
  2106. struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
  2107. /* For backward compatibility expose the NGUID to userspace if
  2108. * we have no UUID set
  2109. */
  2110. if (uuid_is_null(&ids->uuid)) {
  2111. printk_ratelimited(KERN_WARNING
  2112. "No UUID available providing old NGUID\n");
  2113. return sprintf(buf, "%pU\n", ids->nguid);
  2114. }
  2115. return sprintf(buf, "%pU\n", &ids->uuid);
  2116. }
  2117. static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
  2118. static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
  2119. char *buf)
  2120. {
  2121. return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
  2122. }
  2123. static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
  2124. static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
  2125. char *buf)
  2126. {
  2127. return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
  2128. }
  2129. static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
  2130. static struct attribute *nvme_ns_id_attrs[] = {
  2131. &dev_attr_wwid.attr,
  2132. &dev_attr_uuid.attr,
  2133. &dev_attr_nguid.attr,
  2134. &dev_attr_eui.attr,
  2135. &dev_attr_nsid.attr,
  2136. NULL,
  2137. };
  2138. static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
  2139. struct attribute *a, int n)
  2140. {
  2141. struct device *dev = container_of(kobj, struct device, kobj);
  2142. struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
  2143. if (a == &dev_attr_uuid.attr) {
  2144. if (uuid_is_null(&ids->uuid) &&
  2145. !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  2146. return 0;
  2147. }
  2148. if (a == &dev_attr_nguid.attr) {
  2149. if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
  2150. return 0;
  2151. }
  2152. if (a == &dev_attr_eui.attr) {
  2153. if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
  2154. return 0;
  2155. }
  2156. return a->mode;
  2157. }
  2158. const struct attribute_group nvme_ns_id_attr_group = {
  2159. .attrs = nvme_ns_id_attrs,
  2160. .is_visible = nvme_ns_id_attrs_are_visible,
  2161. };
  2162. #define nvme_show_str_function(field) \
  2163. static ssize_t field##_show(struct device *dev, \
  2164. struct device_attribute *attr, char *buf) \
  2165. { \
  2166. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  2167. return sprintf(buf, "%.*s\n", \
  2168. (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
  2169. } \
  2170. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  2171. nvme_show_str_function(model);
  2172. nvme_show_str_function(serial);
  2173. nvme_show_str_function(firmware_rev);
  2174. #define nvme_show_int_function(field) \
  2175. static ssize_t field##_show(struct device *dev, \
  2176. struct device_attribute *attr, char *buf) \
  2177. { \
  2178. struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
  2179. return sprintf(buf, "%d\n", ctrl->field); \
  2180. } \
  2181. static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
  2182. nvme_show_int_function(cntlid);
  2183. static ssize_t nvme_sysfs_delete(struct device *dev,
  2184. struct device_attribute *attr, const char *buf,
  2185. size_t count)
  2186. {
  2187. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2188. if (device_remove_file_self(dev, attr))
  2189. nvme_delete_ctrl_sync(ctrl);
  2190. return count;
  2191. }
  2192. static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
  2193. static ssize_t nvme_sysfs_show_transport(struct device *dev,
  2194. struct device_attribute *attr,
  2195. char *buf)
  2196. {
  2197. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2198. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
  2199. }
  2200. static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
  2201. static ssize_t nvme_sysfs_show_state(struct device *dev,
  2202. struct device_attribute *attr,
  2203. char *buf)
  2204. {
  2205. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2206. static const char *const state_name[] = {
  2207. [NVME_CTRL_NEW] = "new",
  2208. [NVME_CTRL_LIVE] = "live",
  2209. [NVME_CTRL_RESETTING] = "resetting",
  2210. [NVME_CTRL_RECONNECTING]= "reconnecting",
  2211. [NVME_CTRL_DELETING] = "deleting",
  2212. [NVME_CTRL_DEAD] = "dead",
  2213. };
  2214. if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
  2215. state_name[ctrl->state])
  2216. return sprintf(buf, "%s\n", state_name[ctrl->state]);
  2217. return sprintf(buf, "unknown state\n");
  2218. }
  2219. static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
  2220. static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
  2221. struct device_attribute *attr,
  2222. char *buf)
  2223. {
  2224. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2225. return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
  2226. }
  2227. static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
  2228. static ssize_t nvme_sysfs_show_address(struct device *dev,
  2229. struct device_attribute *attr,
  2230. char *buf)
  2231. {
  2232. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2233. return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
  2234. }
  2235. static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
  2236. static struct attribute *nvme_dev_attrs[] = {
  2237. &dev_attr_reset_controller.attr,
  2238. &dev_attr_rescan_controller.attr,
  2239. &dev_attr_model.attr,
  2240. &dev_attr_serial.attr,
  2241. &dev_attr_firmware_rev.attr,
  2242. &dev_attr_cntlid.attr,
  2243. &dev_attr_delete_controller.attr,
  2244. &dev_attr_transport.attr,
  2245. &dev_attr_subsysnqn.attr,
  2246. &dev_attr_address.attr,
  2247. &dev_attr_state.attr,
  2248. NULL
  2249. };
  2250. static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  2251. struct attribute *a, int n)
  2252. {
  2253. struct device *dev = container_of(kobj, struct device, kobj);
  2254. struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
  2255. if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
  2256. return 0;
  2257. if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
  2258. return 0;
  2259. return a->mode;
  2260. }
  2261. static struct attribute_group nvme_dev_attrs_group = {
  2262. .attrs = nvme_dev_attrs,
  2263. .is_visible = nvme_dev_attrs_are_visible,
  2264. };
  2265. static const struct attribute_group *nvme_dev_attr_groups[] = {
  2266. &nvme_dev_attrs_group,
  2267. NULL,
  2268. };
  2269. static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
  2270. unsigned nsid)
  2271. {
  2272. struct nvme_ns_head *h;
  2273. lockdep_assert_held(&subsys->lock);
  2274. list_for_each_entry(h, &subsys->nsheads, entry) {
  2275. if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
  2276. return h;
  2277. }
  2278. return NULL;
  2279. }
  2280. static int __nvme_check_ids(struct nvme_subsystem *subsys,
  2281. struct nvme_ns_head *new)
  2282. {
  2283. struct nvme_ns_head *h;
  2284. lockdep_assert_held(&subsys->lock);
  2285. list_for_each_entry(h, &subsys->nsheads, entry) {
  2286. if (nvme_ns_ids_valid(&new->ids) &&
  2287. nvme_ns_ids_equal(&new->ids, &h->ids))
  2288. return -EINVAL;
  2289. }
  2290. return 0;
  2291. }
  2292. static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
  2293. unsigned nsid, struct nvme_id_ns *id)
  2294. {
  2295. struct nvme_ns_head *head;
  2296. int ret = -ENOMEM;
  2297. head = kzalloc(sizeof(*head), GFP_KERNEL);
  2298. if (!head)
  2299. goto out;
  2300. ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
  2301. if (ret < 0)
  2302. goto out_free_head;
  2303. head->instance = ret;
  2304. INIT_LIST_HEAD(&head->list);
  2305. init_srcu_struct(&head->srcu);
  2306. head->subsys = ctrl->subsys;
  2307. head->ns_id = nsid;
  2308. kref_init(&head->ref);
  2309. nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
  2310. ret = __nvme_check_ids(ctrl->subsys, head);
  2311. if (ret) {
  2312. dev_err(ctrl->device,
  2313. "duplicate IDs for nsid %d\n", nsid);
  2314. goto out_cleanup_srcu;
  2315. }
  2316. ret = nvme_mpath_alloc_disk(ctrl, head);
  2317. if (ret)
  2318. goto out_cleanup_srcu;
  2319. list_add_tail(&head->entry, &ctrl->subsys->nsheads);
  2320. return head;
  2321. out_cleanup_srcu:
  2322. cleanup_srcu_struct(&head->srcu);
  2323. ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
  2324. out_free_head:
  2325. kfree(head);
  2326. out:
  2327. return ERR_PTR(ret);
  2328. }
  2329. static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
  2330. struct nvme_id_ns *id, bool *new)
  2331. {
  2332. struct nvme_ctrl *ctrl = ns->ctrl;
  2333. bool is_shared = id->nmic & (1 << 0);
  2334. struct nvme_ns_head *head = NULL;
  2335. int ret = 0;
  2336. mutex_lock(&ctrl->subsys->lock);
  2337. if (is_shared)
  2338. head = __nvme_find_ns_head(ctrl->subsys, nsid);
  2339. if (!head) {
  2340. head = nvme_alloc_ns_head(ctrl, nsid, id);
  2341. if (IS_ERR(head)) {
  2342. ret = PTR_ERR(head);
  2343. goto out_unlock;
  2344. }
  2345. *new = true;
  2346. } else {
  2347. struct nvme_ns_ids ids;
  2348. nvme_report_ns_ids(ctrl, nsid, id, &ids);
  2349. if (!nvme_ns_ids_equal(&head->ids, &ids)) {
  2350. dev_err(ctrl->device,
  2351. "IDs don't match for shared namespace %d\n",
  2352. nsid);
  2353. ret = -EINVAL;
  2354. goto out_unlock;
  2355. }
  2356. *new = false;
  2357. }
  2358. list_add_tail(&ns->siblings, &head->list);
  2359. ns->head = head;
  2360. out_unlock:
  2361. mutex_unlock(&ctrl->subsys->lock);
  2362. return ret;
  2363. }
  2364. static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
  2365. {
  2366. struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
  2367. struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
  2368. return nsa->head->ns_id - nsb->head->ns_id;
  2369. }
  2370. static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  2371. {
  2372. struct nvme_ns *ns, *ret = NULL;
  2373. mutex_lock(&ctrl->namespaces_mutex);
  2374. list_for_each_entry(ns, &ctrl->namespaces, list) {
  2375. if (ns->head->ns_id == nsid) {
  2376. if (!kref_get_unless_zero(&ns->kref))
  2377. continue;
  2378. ret = ns;
  2379. break;
  2380. }
  2381. if (ns->head->ns_id > nsid)
  2382. break;
  2383. }
  2384. mutex_unlock(&ctrl->namespaces_mutex);
  2385. return ret;
  2386. }
  2387. static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
  2388. {
  2389. struct streams_directive_params s;
  2390. int ret;
  2391. if (!ctrl->nr_streams)
  2392. return 0;
  2393. ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
  2394. if (ret)
  2395. return ret;
  2396. ns->sws = le32_to_cpu(s.sws);
  2397. ns->sgs = le16_to_cpu(s.sgs);
  2398. if (ns->sws) {
  2399. unsigned int bs = 1 << ns->lba_shift;
  2400. blk_queue_io_min(ns->queue, bs * ns->sws);
  2401. if (ns->sgs)
  2402. blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
  2403. }
  2404. return 0;
  2405. }
  2406. static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  2407. {
  2408. struct nvme_ns *ns;
  2409. struct gendisk *disk;
  2410. struct nvme_id_ns *id;
  2411. char disk_name[DISK_NAME_LEN];
  2412. int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
  2413. bool new = true;
  2414. ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
  2415. if (!ns)
  2416. return;
  2417. ns->queue = blk_mq_init_queue(ctrl->tagset);
  2418. if (IS_ERR(ns->queue))
  2419. goto out_free_ns;
  2420. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
  2421. ns->queue->queuedata = ns;
  2422. ns->ctrl = ctrl;
  2423. kref_init(&ns->kref);
  2424. ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
  2425. blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
  2426. nvme_set_queue_limits(ctrl, ns->queue);
  2427. nvme_setup_streams_ns(ctrl, ns);
  2428. id = nvme_identify_ns(ctrl, nsid);
  2429. if (!id)
  2430. goto out_free_queue;
  2431. if (id->ncap == 0)
  2432. goto out_free_id;
  2433. if (nvme_init_ns_head(ns, nsid, id, &new))
  2434. goto out_free_id;
  2435. #ifdef CONFIG_NVME_MULTIPATH
  2436. /*
  2437. * If multipathing is enabled we need to always use the subsystem
  2438. * instance number for numbering our devices to avoid conflicts
  2439. * between subsystems that have multiple controllers and thus use
  2440. * the multipath-aware subsystem node and those that have a single
  2441. * controller and use the controller node directly.
  2442. */
  2443. if (ns->head->disk) {
  2444. sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
  2445. ctrl->cntlid, ns->head->instance);
  2446. flags = GENHD_FL_HIDDEN;
  2447. } else {
  2448. sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
  2449. ns->head->instance);
  2450. }
  2451. #else
  2452. /*
  2453. * But without the multipath code enabled, multiple controller per
  2454. * subsystems are visible as devices and thus we cannot use the
  2455. * subsystem instance.
  2456. */
  2457. sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
  2458. #endif
  2459. if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
  2460. if (nvme_nvm_register(ns, disk_name, node)) {
  2461. dev_warn(ctrl->device, "LightNVM init failure\n");
  2462. goto out_unlink_ns;
  2463. }
  2464. }
  2465. disk = alloc_disk_node(0, node);
  2466. if (!disk)
  2467. goto out_unlink_ns;
  2468. disk->fops = &nvme_fops;
  2469. disk->private_data = ns;
  2470. disk->queue = ns->queue;
  2471. disk->flags = flags;
  2472. memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
  2473. ns->disk = disk;
  2474. __nvme_revalidate_disk(disk, id);
  2475. mutex_lock(&ctrl->namespaces_mutex);
  2476. list_add_tail(&ns->list, &ctrl->namespaces);
  2477. mutex_unlock(&ctrl->namespaces_mutex);
  2478. nvme_get_ctrl(ctrl);
  2479. kfree(id);
  2480. device_add_disk(ctrl->device, ns->disk);
  2481. if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
  2482. &nvme_ns_id_attr_group))
  2483. pr_warn("%s: failed to create sysfs group for identification\n",
  2484. ns->disk->disk_name);
  2485. if (ns->ndev && nvme_nvm_register_sysfs(ns))
  2486. pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
  2487. ns->disk->disk_name);
  2488. if (new)
  2489. nvme_mpath_add_disk(ns->head);
  2490. nvme_mpath_add_disk_links(ns);
  2491. return;
  2492. out_unlink_ns:
  2493. mutex_lock(&ctrl->subsys->lock);
  2494. list_del_rcu(&ns->siblings);
  2495. mutex_unlock(&ctrl->subsys->lock);
  2496. out_free_id:
  2497. kfree(id);
  2498. out_free_queue:
  2499. blk_cleanup_queue(ns->queue);
  2500. out_free_ns:
  2501. kfree(ns);
  2502. }
  2503. static void nvme_ns_remove(struct nvme_ns *ns)
  2504. {
  2505. struct nvme_ns_head *head = ns->head;
  2506. if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
  2507. return;
  2508. if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
  2509. if (blk_get_integrity(ns->disk))
  2510. blk_integrity_unregister(ns->disk);
  2511. nvme_mpath_remove_disk_links(ns);
  2512. sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
  2513. &nvme_ns_id_attr_group);
  2514. if (ns->ndev)
  2515. nvme_nvm_unregister_sysfs(ns);
  2516. del_gendisk(ns->disk);
  2517. blk_cleanup_queue(ns->queue);
  2518. }
  2519. mutex_lock(&ns->ctrl->subsys->lock);
  2520. nvme_mpath_clear_current_path(ns);
  2521. if (head)
  2522. list_del_rcu(&ns->siblings);
  2523. mutex_unlock(&ns->ctrl->subsys->lock);
  2524. mutex_lock(&ns->ctrl->namespaces_mutex);
  2525. list_del_init(&ns->list);
  2526. mutex_unlock(&ns->ctrl->namespaces_mutex);
  2527. synchronize_srcu(&head->srcu);
  2528. nvme_put_ns(ns);
  2529. }
  2530. static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
  2531. {
  2532. struct nvme_ns *ns;
  2533. ns = nvme_find_get_ns(ctrl, nsid);
  2534. if (ns) {
  2535. if (ns->disk && revalidate_disk(ns->disk))
  2536. nvme_ns_remove(ns);
  2537. nvme_put_ns(ns);
  2538. } else
  2539. nvme_alloc_ns(ctrl, nsid);
  2540. }
  2541. static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
  2542. unsigned nsid)
  2543. {
  2544. struct nvme_ns *ns, *next;
  2545. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
  2546. if (ns->head->ns_id > nsid)
  2547. nvme_ns_remove(ns);
  2548. }
  2549. }
  2550. static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
  2551. {
  2552. struct nvme_ns *ns;
  2553. __le32 *ns_list;
  2554. unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
  2555. int ret = 0;
  2556. ns_list = kzalloc(0x1000, GFP_KERNEL);
  2557. if (!ns_list)
  2558. return -ENOMEM;
  2559. for (i = 0; i < num_lists; i++) {
  2560. ret = nvme_identify_ns_list(ctrl, prev, ns_list);
  2561. if (ret)
  2562. goto free;
  2563. for (j = 0; j < min(nn, 1024U); j++) {
  2564. nsid = le32_to_cpu(ns_list[j]);
  2565. if (!nsid)
  2566. goto out;
  2567. nvme_validate_ns(ctrl, nsid);
  2568. while (++prev < nsid) {
  2569. ns = nvme_find_get_ns(ctrl, prev);
  2570. if (ns) {
  2571. nvme_ns_remove(ns);
  2572. nvme_put_ns(ns);
  2573. }
  2574. }
  2575. }
  2576. nn -= j;
  2577. }
  2578. out:
  2579. nvme_remove_invalid_namespaces(ctrl, prev);
  2580. free:
  2581. kfree(ns_list);
  2582. return ret;
  2583. }
  2584. static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
  2585. {
  2586. unsigned i;
  2587. for (i = 1; i <= nn; i++)
  2588. nvme_validate_ns(ctrl, i);
  2589. nvme_remove_invalid_namespaces(ctrl, nn);
  2590. }
  2591. static void nvme_scan_work(struct work_struct *work)
  2592. {
  2593. struct nvme_ctrl *ctrl =
  2594. container_of(work, struct nvme_ctrl, scan_work);
  2595. struct nvme_id_ctrl *id;
  2596. unsigned nn;
  2597. if (ctrl->state != NVME_CTRL_LIVE)
  2598. return;
  2599. if (nvme_identify_ctrl(ctrl, &id))
  2600. return;
  2601. nn = le32_to_cpu(id->nn);
  2602. if (ctrl->vs >= NVME_VS(1, 1, 0) &&
  2603. !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
  2604. if (!nvme_scan_ns_list(ctrl, nn))
  2605. goto done;
  2606. }
  2607. nvme_scan_ns_sequential(ctrl, nn);
  2608. done:
  2609. mutex_lock(&ctrl->namespaces_mutex);
  2610. list_sort(NULL, &ctrl->namespaces, ns_cmp);
  2611. mutex_unlock(&ctrl->namespaces_mutex);
  2612. kfree(id);
  2613. }
  2614. void nvme_queue_scan(struct nvme_ctrl *ctrl)
  2615. {
  2616. /*
  2617. * Do not queue new scan work when a controller is reset during
  2618. * removal.
  2619. */
  2620. if (ctrl->state == NVME_CTRL_LIVE)
  2621. queue_work(nvme_wq, &ctrl->scan_work);
  2622. }
  2623. EXPORT_SYMBOL_GPL(nvme_queue_scan);
  2624. /*
  2625. * This function iterates the namespace list unlocked to allow recovery from
  2626. * controller failure. It is up to the caller to ensure the namespace list is
  2627. * not modified by scan work while this function is executing.
  2628. */
  2629. void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
  2630. {
  2631. struct nvme_ns *ns, *next;
  2632. /*
  2633. * The dead states indicates the controller was not gracefully
  2634. * disconnected. In that case, we won't be able to flush any data while
  2635. * removing the namespaces' disks; fail all the queues now to avoid
  2636. * potentially having to clean up the failed sync later.
  2637. */
  2638. if (ctrl->state == NVME_CTRL_DEAD)
  2639. nvme_kill_queues(ctrl);
  2640. list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
  2641. nvme_ns_remove(ns);
  2642. }
  2643. EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
  2644. static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
  2645. {
  2646. char *envp[2] = { NULL, NULL };
  2647. u32 aen_result = ctrl->aen_result;
  2648. ctrl->aen_result = 0;
  2649. if (!aen_result)
  2650. return;
  2651. envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
  2652. if (!envp[0])
  2653. return;
  2654. kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
  2655. kfree(envp[0]);
  2656. }
  2657. static void nvme_async_event_work(struct work_struct *work)
  2658. {
  2659. struct nvme_ctrl *ctrl =
  2660. container_of(work, struct nvme_ctrl, async_event_work);
  2661. nvme_aen_uevent(ctrl);
  2662. ctrl->ops->submit_async_event(ctrl);
  2663. }
  2664. static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
  2665. {
  2666. u32 csts;
  2667. if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
  2668. return false;
  2669. if (csts == ~0)
  2670. return false;
  2671. return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
  2672. }
  2673. static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
  2674. {
  2675. struct nvme_fw_slot_info_log *log;
  2676. log = kmalloc(sizeof(*log), GFP_KERNEL);
  2677. if (!log)
  2678. return;
  2679. if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
  2680. dev_warn(ctrl->device,
  2681. "Get FW SLOT INFO log error\n");
  2682. kfree(log);
  2683. }
  2684. static void nvme_fw_act_work(struct work_struct *work)
  2685. {
  2686. struct nvme_ctrl *ctrl = container_of(work,
  2687. struct nvme_ctrl, fw_act_work);
  2688. unsigned long fw_act_timeout;
  2689. if (ctrl->mtfa)
  2690. fw_act_timeout = jiffies +
  2691. msecs_to_jiffies(ctrl->mtfa * 100);
  2692. else
  2693. fw_act_timeout = jiffies +
  2694. msecs_to_jiffies(admin_timeout * 1000);
  2695. nvme_stop_queues(ctrl);
  2696. while (nvme_ctrl_pp_status(ctrl)) {
  2697. if (time_after(jiffies, fw_act_timeout)) {
  2698. dev_warn(ctrl->device,
  2699. "Fw activation timeout, reset controller\n");
  2700. nvme_reset_ctrl(ctrl);
  2701. break;
  2702. }
  2703. msleep(100);
  2704. }
  2705. if (ctrl->state != NVME_CTRL_LIVE)
  2706. return;
  2707. nvme_start_queues(ctrl);
  2708. /* read FW slot information to clear the AER */
  2709. nvme_get_fw_slot_info(ctrl);
  2710. }
  2711. void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
  2712. union nvme_result *res)
  2713. {
  2714. u32 result = le32_to_cpu(res->u32);
  2715. if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
  2716. return;
  2717. switch (result & 0x7) {
  2718. case NVME_AER_ERROR:
  2719. case NVME_AER_SMART:
  2720. case NVME_AER_CSS:
  2721. case NVME_AER_VS:
  2722. ctrl->aen_result = result;
  2723. break;
  2724. default:
  2725. break;
  2726. }
  2727. switch (result & 0xff07) {
  2728. case NVME_AER_NOTICE_NS_CHANGED:
  2729. dev_info(ctrl->device, "rescanning\n");
  2730. nvme_queue_scan(ctrl);
  2731. break;
  2732. case NVME_AER_NOTICE_FW_ACT_STARTING:
  2733. queue_work(nvme_wq, &ctrl->fw_act_work);
  2734. break;
  2735. default:
  2736. dev_warn(ctrl->device, "async event result %08x\n", result);
  2737. }
  2738. queue_work(nvme_wq, &ctrl->async_event_work);
  2739. }
  2740. EXPORT_SYMBOL_GPL(nvme_complete_async_event);
  2741. void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
  2742. {
  2743. nvme_stop_keep_alive(ctrl);
  2744. flush_work(&ctrl->async_event_work);
  2745. flush_work(&ctrl->scan_work);
  2746. cancel_work_sync(&ctrl->fw_act_work);
  2747. }
  2748. EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
  2749. void nvme_start_ctrl(struct nvme_ctrl *ctrl)
  2750. {
  2751. if (ctrl->kato)
  2752. nvme_start_keep_alive(ctrl);
  2753. if (ctrl->queue_count > 1) {
  2754. nvme_queue_scan(ctrl);
  2755. queue_work(nvme_wq, &ctrl->async_event_work);
  2756. nvme_start_queues(ctrl);
  2757. }
  2758. }
  2759. EXPORT_SYMBOL_GPL(nvme_start_ctrl);
  2760. void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
  2761. {
  2762. cdev_device_del(&ctrl->cdev, ctrl->device);
  2763. }
  2764. EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
  2765. static void nvme_free_ctrl(struct device *dev)
  2766. {
  2767. struct nvme_ctrl *ctrl =
  2768. container_of(dev, struct nvme_ctrl, ctrl_device);
  2769. struct nvme_subsystem *subsys = ctrl->subsys;
  2770. ida_simple_remove(&nvme_instance_ida, ctrl->instance);
  2771. kfree(ctrl->effects);
  2772. if (subsys) {
  2773. mutex_lock(&subsys->lock);
  2774. list_del(&ctrl->subsys_entry);
  2775. mutex_unlock(&subsys->lock);
  2776. sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
  2777. }
  2778. ctrl->ops->free_ctrl(ctrl);
  2779. if (subsys)
  2780. nvme_put_subsystem(subsys);
  2781. }
  2782. /*
  2783. * Initialize a NVMe controller structures. This needs to be called during
  2784. * earliest initialization so that we have the initialized structured around
  2785. * during probing.
  2786. */
  2787. int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
  2788. const struct nvme_ctrl_ops *ops, unsigned long quirks)
  2789. {
  2790. int ret;
  2791. ctrl->state = NVME_CTRL_NEW;
  2792. spin_lock_init(&ctrl->lock);
  2793. INIT_LIST_HEAD(&ctrl->namespaces);
  2794. mutex_init(&ctrl->namespaces_mutex);
  2795. ctrl->dev = dev;
  2796. ctrl->ops = ops;
  2797. ctrl->quirks = quirks;
  2798. INIT_WORK(&ctrl->scan_work, nvme_scan_work);
  2799. INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
  2800. INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
  2801. INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
  2802. ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
  2803. if (ret < 0)
  2804. goto out;
  2805. ctrl->instance = ret;
  2806. device_initialize(&ctrl->ctrl_device);
  2807. ctrl->device = &ctrl->ctrl_device;
  2808. ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
  2809. ctrl->device->class = nvme_class;
  2810. ctrl->device->parent = ctrl->dev;
  2811. ctrl->device->groups = nvme_dev_attr_groups;
  2812. ctrl->device->release = nvme_free_ctrl;
  2813. dev_set_drvdata(ctrl->device, ctrl);
  2814. ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
  2815. if (ret)
  2816. goto out_release_instance;
  2817. cdev_init(&ctrl->cdev, &nvme_dev_fops);
  2818. ctrl->cdev.owner = ops->module;
  2819. ret = cdev_device_add(&ctrl->cdev, ctrl->device);
  2820. if (ret)
  2821. goto out_free_name;
  2822. /*
  2823. * Initialize latency tolerance controls. The sysfs files won't
  2824. * be visible to userspace unless the device actually supports APST.
  2825. */
  2826. ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
  2827. dev_pm_qos_update_user_latency_tolerance(ctrl->device,
  2828. min(default_ps_max_latency_us, (unsigned long)S32_MAX));
  2829. return 0;
  2830. out_free_name:
  2831. kfree_const(dev->kobj.name);
  2832. out_release_instance:
  2833. ida_simple_remove(&nvme_instance_ida, ctrl->instance);
  2834. out:
  2835. return ret;
  2836. }
  2837. EXPORT_SYMBOL_GPL(nvme_init_ctrl);
  2838. /**
  2839. * nvme_kill_queues(): Ends all namespace queues
  2840. * @ctrl: the dead controller that needs to end
  2841. *
  2842. * Call this function when the driver determines it is unable to get the
  2843. * controller in a state capable of servicing IO.
  2844. */
  2845. void nvme_kill_queues(struct nvme_ctrl *ctrl)
  2846. {
  2847. struct nvme_ns *ns;
  2848. mutex_lock(&ctrl->namespaces_mutex);
  2849. /* Forcibly unquiesce queues to avoid blocking dispatch */
  2850. if (ctrl->admin_q)
  2851. blk_mq_unquiesce_queue(ctrl->admin_q);
  2852. list_for_each_entry(ns, &ctrl->namespaces, list) {
  2853. /*
  2854. * Revalidating a dead namespace sets capacity to 0. This will
  2855. * end buffered writers dirtying pages that can't be synced.
  2856. */
  2857. if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
  2858. continue;
  2859. revalidate_disk(ns->disk);
  2860. blk_set_queue_dying(ns->queue);
  2861. /* Forcibly unquiesce queues to avoid blocking dispatch */
  2862. blk_mq_unquiesce_queue(ns->queue);
  2863. }
  2864. mutex_unlock(&ctrl->namespaces_mutex);
  2865. }
  2866. EXPORT_SYMBOL_GPL(nvme_kill_queues);
  2867. void nvme_unfreeze(struct nvme_ctrl *ctrl)
  2868. {
  2869. struct nvme_ns *ns;
  2870. mutex_lock(&ctrl->namespaces_mutex);
  2871. list_for_each_entry(ns, &ctrl->namespaces, list)
  2872. blk_mq_unfreeze_queue(ns->queue);
  2873. mutex_unlock(&ctrl->namespaces_mutex);
  2874. }
  2875. EXPORT_SYMBOL_GPL(nvme_unfreeze);
  2876. void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
  2877. {
  2878. struct nvme_ns *ns;
  2879. mutex_lock(&ctrl->namespaces_mutex);
  2880. list_for_each_entry(ns, &ctrl->namespaces, list) {
  2881. timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
  2882. if (timeout <= 0)
  2883. break;
  2884. }
  2885. mutex_unlock(&ctrl->namespaces_mutex);
  2886. }
  2887. EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
  2888. void nvme_wait_freeze(struct nvme_ctrl *ctrl)
  2889. {
  2890. struct nvme_ns *ns;
  2891. mutex_lock(&ctrl->namespaces_mutex);
  2892. list_for_each_entry(ns, &ctrl->namespaces, list)
  2893. blk_mq_freeze_queue_wait(ns->queue);
  2894. mutex_unlock(&ctrl->namespaces_mutex);
  2895. }
  2896. EXPORT_SYMBOL_GPL(nvme_wait_freeze);
  2897. void nvme_start_freeze(struct nvme_ctrl *ctrl)
  2898. {
  2899. struct nvme_ns *ns;
  2900. mutex_lock(&ctrl->namespaces_mutex);
  2901. list_for_each_entry(ns, &ctrl->namespaces, list)
  2902. blk_freeze_queue_start(ns->queue);
  2903. mutex_unlock(&ctrl->namespaces_mutex);
  2904. }
  2905. EXPORT_SYMBOL_GPL(nvme_start_freeze);
  2906. void nvme_stop_queues(struct nvme_ctrl *ctrl)
  2907. {
  2908. struct nvme_ns *ns;
  2909. mutex_lock(&ctrl->namespaces_mutex);
  2910. list_for_each_entry(ns, &ctrl->namespaces, list)
  2911. blk_mq_quiesce_queue(ns->queue);
  2912. mutex_unlock(&ctrl->namespaces_mutex);
  2913. }
  2914. EXPORT_SYMBOL_GPL(nvme_stop_queues);
  2915. void nvme_start_queues(struct nvme_ctrl *ctrl)
  2916. {
  2917. struct nvme_ns *ns;
  2918. mutex_lock(&ctrl->namespaces_mutex);
  2919. list_for_each_entry(ns, &ctrl->namespaces, list)
  2920. blk_mq_unquiesce_queue(ns->queue);
  2921. mutex_unlock(&ctrl->namespaces_mutex);
  2922. }
  2923. EXPORT_SYMBOL_GPL(nvme_start_queues);
  2924. int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
  2925. {
  2926. if (!ctrl->ops->reinit_request)
  2927. return 0;
  2928. return blk_mq_tagset_iter(set, set->driver_data,
  2929. ctrl->ops->reinit_request);
  2930. }
  2931. EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
  2932. int __init nvme_core_init(void)
  2933. {
  2934. int result;
  2935. nvme_wq = alloc_workqueue("nvme-wq",
  2936. WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
  2937. if (!nvme_wq)
  2938. return -ENOMEM;
  2939. result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
  2940. if (result < 0)
  2941. goto destroy_wq;
  2942. nvme_class = class_create(THIS_MODULE, "nvme");
  2943. if (IS_ERR(nvme_class)) {
  2944. result = PTR_ERR(nvme_class);
  2945. goto unregister_chrdev;
  2946. }
  2947. nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
  2948. if (IS_ERR(nvme_subsys_class)) {
  2949. result = PTR_ERR(nvme_subsys_class);
  2950. goto destroy_class;
  2951. }
  2952. return 0;
  2953. destroy_class:
  2954. class_destroy(nvme_class);
  2955. unregister_chrdev:
  2956. unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
  2957. destroy_wq:
  2958. destroy_workqueue(nvme_wq);
  2959. return result;
  2960. }
  2961. void nvme_core_exit(void)
  2962. {
  2963. ida_destroy(&nvme_subsystems_ida);
  2964. class_destroy(nvme_subsys_class);
  2965. class_destroy(nvme_class);
  2966. unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
  2967. destroy_workqueue(nvme_wq);
  2968. }
  2969. MODULE_LICENSE("GPL");
  2970. MODULE_VERSION("1.0");
  2971. module_init(nvme_core_init);
  2972. module_exit(nvme_core_exit);