core.c 93 KB

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