core.c 96 KB

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