core.c 90 KB

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