core.c 92 KB

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