core.c 94 KB

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