core.c 92 KB

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