core.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/list_sort.h>
  6. #include <linux/libnvdimm.h>
  7. #include <linux/module.h>
  8. #include <linux/nospec.h>
  9. #include <linux/mutex.h>
  10. #include <linux/ndctl.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/delay.h>
  13. #include <linux/list.h>
  14. #include <linux/acpi.h>
  15. #include <linux/sort.h>
  16. #include <linux/io.h>
  17. #include <linux/nd.h>
  18. #include <asm/cacheflush.h>
  19. #include <acpi/nfit.h>
  20. #include "intel.h"
  21. #include "nfit.h"
  22. /*
  23. * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
  24. * irrelevant.
  25. */
  26. #include <linux/io-64-nonatomic-hi-lo.h>
  27. static bool force_enable_dimms;
  28. module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
  29. MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
  30. static bool disable_vendor_specific;
  31. module_param(disable_vendor_specific, bool, S_IRUGO);
  32. MODULE_PARM_DESC(disable_vendor_specific,
  33. "Limit commands to the publicly specified set");
  34. static unsigned long override_dsm_mask;
  35. module_param(override_dsm_mask, ulong, S_IRUGO);
  36. MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
  37. static int default_dsm_family = -1;
  38. module_param(default_dsm_family, int, S_IRUGO);
  39. MODULE_PARM_DESC(default_dsm_family,
  40. "Try this DSM type first when identifying NVDIMM family");
  41. static bool no_init_ars;
  42. module_param(no_init_ars, bool, 0644);
  43. MODULE_PARM_DESC(no_init_ars, "Skip ARS run at nfit init time");
  44. static bool force_labels;
  45. module_param(force_labels, bool, 0444);
  46. MODULE_PARM_DESC(force_labels, "Opt-in to labels despite missing methods");
  47. LIST_HEAD(acpi_descs);
  48. DEFINE_MUTEX(acpi_desc_lock);
  49. static struct workqueue_struct *nfit_wq;
  50. struct nfit_table_prev {
  51. struct list_head spas;
  52. struct list_head memdevs;
  53. struct list_head dcrs;
  54. struct list_head bdws;
  55. struct list_head idts;
  56. struct list_head flushes;
  57. };
  58. static guid_t nfit_uuid[NFIT_UUID_MAX];
  59. const guid_t *to_nfit_uuid(enum nfit_uuids id)
  60. {
  61. return &nfit_uuid[id];
  62. }
  63. EXPORT_SYMBOL(to_nfit_uuid);
  64. static const guid_t *to_nfit_bus_uuid(int family)
  65. {
  66. if (WARN_ONCE(family == NVDIMM_BUS_FAMILY_NFIT,
  67. "only secondary bus families can be translated\n"))
  68. return NULL;
  69. /*
  70. * The index of bus UUIDs starts immediately following the last
  71. * NVDIMM/leaf family.
  72. */
  73. return to_nfit_uuid(family + NVDIMM_FAMILY_MAX);
  74. }
  75. static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
  76. {
  77. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  78. /*
  79. * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
  80. * acpi_device.
  81. */
  82. if (!nd_desc->provider_name
  83. || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
  84. return NULL;
  85. return to_acpi_device(acpi_desc->dev);
  86. }
  87. static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
  88. {
  89. struct nd_cmd_clear_error *clear_err;
  90. struct nd_cmd_ars_status *ars_status;
  91. u16 flags;
  92. switch (cmd) {
  93. case ND_CMD_ARS_CAP:
  94. if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
  95. return -ENOTTY;
  96. /* Command failed */
  97. if (status & 0xffff)
  98. return -EIO;
  99. /* No supported scan types for this range */
  100. flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
  101. if ((status >> 16 & flags) == 0)
  102. return -ENOTTY;
  103. return 0;
  104. case ND_CMD_ARS_START:
  105. /* ARS is in progress */
  106. if ((status & 0xffff) == NFIT_ARS_START_BUSY)
  107. return -EBUSY;
  108. /* Command failed */
  109. if (status & 0xffff)
  110. return -EIO;
  111. return 0;
  112. case ND_CMD_ARS_STATUS:
  113. ars_status = buf;
  114. /* Command failed */
  115. if (status & 0xffff)
  116. return -EIO;
  117. /* Check extended status (Upper two bytes) */
  118. if (status == NFIT_ARS_STATUS_DONE)
  119. return 0;
  120. /* ARS is in progress */
  121. if (status == NFIT_ARS_STATUS_BUSY)
  122. return -EBUSY;
  123. /* No ARS performed for the current boot */
  124. if (status == NFIT_ARS_STATUS_NONE)
  125. return -EAGAIN;
  126. /*
  127. * ARS interrupted, either we overflowed or some other
  128. * agent wants the scan to stop. If we didn't overflow
  129. * then just continue with the returned results.
  130. */
  131. if (status == NFIT_ARS_STATUS_INTR) {
  132. if (ars_status->out_length >= 40 && (ars_status->flags
  133. & NFIT_ARS_F_OVERFLOW))
  134. return -ENOSPC;
  135. return 0;
  136. }
  137. /* Unknown status */
  138. if (status >> 16)
  139. return -EIO;
  140. return 0;
  141. case ND_CMD_CLEAR_ERROR:
  142. clear_err = buf;
  143. if (status & 0xffff)
  144. return -EIO;
  145. if (!clear_err->cleared)
  146. return -EIO;
  147. if (clear_err->length > clear_err->cleared)
  148. return clear_err->cleared;
  149. return 0;
  150. default:
  151. break;
  152. }
  153. /* all other non-zero status results in an error */
  154. if (status)
  155. return -EIO;
  156. return 0;
  157. }
  158. #define ACPI_LABELS_LOCKED 3
  159. static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
  160. u32 status)
  161. {
  162. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  163. switch (cmd) {
  164. case ND_CMD_GET_CONFIG_SIZE:
  165. /*
  166. * In the _LSI, _LSR, _LSW case the locked status is
  167. * communicated via the read/write commands
  168. */
  169. if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
  170. break;
  171. if (status >> 16 & ND_CONFIG_LOCKED)
  172. return -EACCES;
  173. break;
  174. case ND_CMD_GET_CONFIG_DATA:
  175. if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
  176. && status == ACPI_LABELS_LOCKED)
  177. return -EACCES;
  178. break;
  179. case ND_CMD_SET_CONFIG_DATA:
  180. if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
  181. && status == ACPI_LABELS_LOCKED)
  182. return -EACCES;
  183. break;
  184. default:
  185. break;
  186. }
  187. /* all other non-zero status results in an error */
  188. if (status)
  189. return -EIO;
  190. return 0;
  191. }
  192. static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
  193. u32 status)
  194. {
  195. if (!nvdimm)
  196. return xlat_bus_status(buf, cmd, status);
  197. return xlat_nvdimm_status(nvdimm, buf, cmd, status);
  198. }
  199. /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
  200. static union acpi_object *pkg_to_buf(union acpi_object *pkg)
  201. {
  202. int i;
  203. void *dst;
  204. size_t size = 0;
  205. union acpi_object *buf = NULL;
  206. if (pkg->type != ACPI_TYPE_PACKAGE) {
  207. WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
  208. pkg->type);
  209. goto err;
  210. }
  211. for (i = 0; i < pkg->package.count; i++) {
  212. union acpi_object *obj = &pkg->package.elements[i];
  213. if (obj->type == ACPI_TYPE_INTEGER)
  214. size += 4;
  215. else if (obj->type == ACPI_TYPE_BUFFER)
  216. size += obj->buffer.length;
  217. else {
  218. WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
  219. obj->type);
  220. goto err;
  221. }
  222. }
  223. buf = ACPI_ALLOCATE(sizeof(*buf) + size);
  224. if (!buf)
  225. goto err;
  226. dst = buf + 1;
  227. buf->type = ACPI_TYPE_BUFFER;
  228. buf->buffer.length = size;
  229. buf->buffer.pointer = dst;
  230. for (i = 0; i < pkg->package.count; i++) {
  231. union acpi_object *obj = &pkg->package.elements[i];
  232. if (obj->type == ACPI_TYPE_INTEGER) {
  233. memcpy(dst, &obj->integer.value, 4);
  234. dst += 4;
  235. } else if (obj->type == ACPI_TYPE_BUFFER) {
  236. memcpy(dst, obj->buffer.pointer, obj->buffer.length);
  237. dst += obj->buffer.length;
  238. }
  239. }
  240. err:
  241. ACPI_FREE(pkg);
  242. return buf;
  243. }
  244. static union acpi_object *int_to_buf(union acpi_object *integer)
  245. {
  246. union acpi_object *buf = NULL;
  247. void *dst = NULL;
  248. if (integer->type != ACPI_TYPE_INTEGER) {
  249. WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
  250. integer->type);
  251. goto err;
  252. }
  253. buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
  254. if (!buf)
  255. goto err;
  256. dst = buf + 1;
  257. buf->type = ACPI_TYPE_BUFFER;
  258. buf->buffer.length = 4;
  259. buf->buffer.pointer = dst;
  260. memcpy(dst, &integer->integer.value, 4);
  261. err:
  262. ACPI_FREE(integer);
  263. return buf;
  264. }
  265. static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
  266. u32 len, void *data)
  267. {
  268. acpi_status rc;
  269. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  270. struct acpi_object_list input = {
  271. .count = 3,
  272. .pointer = (union acpi_object []) {
  273. [0] = {
  274. .integer.type = ACPI_TYPE_INTEGER,
  275. .integer.value = offset,
  276. },
  277. [1] = {
  278. .integer.type = ACPI_TYPE_INTEGER,
  279. .integer.value = len,
  280. },
  281. [2] = {
  282. .buffer.type = ACPI_TYPE_BUFFER,
  283. .buffer.pointer = data,
  284. .buffer.length = len,
  285. },
  286. },
  287. };
  288. rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
  289. if (ACPI_FAILURE(rc))
  290. return NULL;
  291. return int_to_buf(buf.pointer);
  292. }
  293. static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
  294. u32 len)
  295. {
  296. acpi_status rc;
  297. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  298. struct acpi_object_list input = {
  299. .count = 2,
  300. .pointer = (union acpi_object []) {
  301. [0] = {
  302. .integer.type = ACPI_TYPE_INTEGER,
  303. .integer.value = offset,
  304. },
  305. [1] = {
  306. .integer.type = ACPI_TYPE_INTEGER,
  307. .integer.value = len,
  308. },
  309. },
  310. };
  311. rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
  312. if (ACPI_FAILURE(rc))
  313. return NULL;
  314. return pkg_to_buf(buf.pointer);
  315. }
  316. static union acpi_object *acpi_label_info(acpi_handle handle)
  317. {
  318. acpi_status rc;
  319. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  320. rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
  321. if (ACPI_FAILURE(rc))
  322. return NULL;
  323. return pkg_to_buf(buf.pointer);
  324. }
  325. static u8 nfit_dsm_revid(unsigned family, unsigned func)
  326. {
  327. static const u8 revid_table[NVDIMM_FAMILY_MAX+1][NVDIMM_CMD_MAX+1] = {
  328. [NVDIMM_FAMILY_INTEL] = {
  329. [NVDIMM_INTEL_GET_MODES ...
  330. NVDIMM_INTEL_FW_ACTIVATE_ARM] = 2,
  331. },
  332. };
  333. u8 id;
  334. if (family > NVDIMM_FAMILY_MAX)
  335. return 0;
  336. if (func > NVDIMM_CMD_MAX)
  337. return 0;
  338. id = revid_table[family][func];
  339. if (id == 0)
  340. return 1; /* default */
  341. return id;
  342. }
  343. static bool payload_dumpable(struct nvdimm *nvdimm, unsigned int func)
  344. {
  345. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  346. if (nfit_mem && nfit_mem->family == NVDIMM_FAMILY_INTEL
  347. && func >= NVDIMM_INTEL_GET_SECURITY_STATE
  348. && func <= NVDIMM_INTEL_MASTER_SECURE_ERASE)
  349. return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG);
  350. return true;
  351. }
  352. static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
  353. struct nd_cmd_pkg *call_pkg, int *family)
  354. {
  355. if (call_pkg) {
  356. int i;
  357. if (nfit_mem && nfit_mem->family != call_pkg->nd_family)
  358. return -ENOTTY;
  359. for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
  360. if (call_pkg->nd_reserved2[i])
  361. return -EINVAL;
  362. *family = call_pkg->nd_family;
  363. return call_pkg->nd_command;
  364. }
  365. /* In the !call_pkg case, bus commands == bus functions */
  366. if (!nfit_mem)
  367. return cmd;
  368. /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
  369. if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
  370. return cmd;
  371. /*
  372. * Force function number validation to fail since 0 is never
  373. * published as a valid function in dsm_mask.
  374. */
  375. return 0;
  376. }
  377. int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
  378. unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
  379. {
  380. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  381. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  382. union acpi_object in_obj, in_buf, *out_obj;
  383. const struct nd_cmd_desc *desc = NULL;
  384. struct device *dev = acpi_desc->dev;
  385. struct nd_cmd_pkg *call_pkg = NULL;
  386. const char *cmd_name, *dimm_name;
  387. unsigned long cmd_mask, dsm_mask;
  388. u32 offset, fw_status = 0;
  389. acpi_handle handle;
  390. const guid_t *guid;
  391. int func, rc, i;
  392. int family = 0;
  393. if (cmd_rc)
  394. *cmd_rc = -EINVAL;
  395. if (cmd == ND_CMD_CALL) {
  396. if (!buf || buf_len < sizeof(*call_pkg))
  397. return -EINVAL;
  398. call_pkg = buf;
  399. }
  400. func = cmd_to_func(nfit_mem, cmd, call_pkg, &family);
  401. if (func < 0)
  402. return func;
  403. if (nvdimm) {
  404. struct acpi_device *adev = nfit_mem->adev;
  405. if (!adev)
  406. return -ENOTTY;
  407. dimm_name = nvdimm_name(nvdimm);
  408. cmd_name = nvdimm_cmd_name(cmd);
  409. cmd_mask = nvdimm_cmd_mask(nvdimm);
  410. dsm_mask = nfit_mem->dsm_mask;
  411. desc = nd_cmd_dimm_desc(cmd);
  412. guid = to_nfit_uuid(nfit_mem->family);
  413. handle = adev->handle;
  414. } else {
  415. struct acpi_device *adev = to_acpi_dev(acpi_desc);
  416. cmd_name = nvdimm_bus_cmd_name(cmd);
  417. cmd_mask = nd_desc->cmd_mask;
  418. if (cmd == ND_CMD_CALL && call_pkg->nd_family) {
  419. family = call_pkg->nd_family;
  420. if (call_pkg->nd_family > NVDIMM_BUS_FAMILY_MAX ||
  421. !test_bit(family, &nd_desc->bus_family_mask))
  422. return -EINVAL;
  423. family = array_index_nospec(family,
  424. NVDIMM_BUS_FAMILY_MAX + 1);
  425. dsm_mask = acpi_desc->family_dsm_mask[family];
  426. guid = to_nfit_bus_uuid(family);
  427. } else {
  428. dsm_mask = acpi_desc->bus_dsm_mask;
  429. guid = to_nfit_uuid(NFIT_DEV_BUS);
  430. }
  431. desc = nd_cmd_bus_desc(cmd);
  432. handle = adev->handle;
  433. dimm_name = "bus";
  434. }
  435. if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
  436. return -ENOTTY;
  437. /*
  438. * Check for a valid command. For ND_CMD_CALL, we also have to
  439. * make sure that the DSM function is supported.
  440. */
  441. if (cmd == ND_CMD_CALL &&
  442. (func > NVDIMM_CMD_MAX || !test_bit(func, &dsm_mask)))
  443. return -ENOTTY;
  444. else if (!test_bit(cmd, &cmd_mask))
  445. return -ENOTTY;
  446. in_obj.type = ACPI_TYPE_PACKAGE;
  447. in_obj.package.count = 1;
  448. in_obj.package.elements = &in_buf;
  449. in_buf.type = ACPI_TYPE_BUFFER;
  450. in_buf.buffer.pointer = buf;
  451. in_buf.buffer.length = 0;
  452. /* libnvdimm has already validated the input envelope */
  453. for (i = 0; i < desc->in_num; i++)
  454. in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
  455. i, buf);
  456. if (call_pkg) {
  457. /* skip over package wrapper */
  458. in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
  459. in_buf.buffer.length = call_pkg->nd_size_in;
  460. }
  461. dev_dbg(dev, "%s cmd: %d: family: %d func: %d input length: %d\n",
  462. dimm_name, cmd, family, func, in_buf.buffer.length);
  463. if (payload_dumpable(nvdimm, func))
  464. print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
  465. in_buf.buffer.pointer,
  466. min_t(u32, 256, in_buf.buffer.length), true);
  467. /* call the BIOS, prefer the named methods over _DSM if available */
  468. if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
  469. && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
  470. out_obj = acpi_label_info(handle);
  471. else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
  472. && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
  473. struct nd_cmd_get_config_data_hdr *p = buf;
  474. out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
  475. } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
  476. && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
  477. struct nd_cmd_set_config_hdr *p = buf;
  478. out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
  479. p->in_buf);
  480. } else {
  481. u8 revid;
  482. if (nvdimm)
  483. revid = nfit_dsm_revid(nfit_mem->family, func);
  484. else
  485. revid = 1;
  486. out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
  487. }
  488. if (!out_obj) {
  489. dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
  490. return -EINVAL;
  491. }
  492. if (out_obj->type != ACPI_TYPE_BUFFER) {
  493. dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
  494. dimm_name, cmd_name, out_obj->type);
  495. rc = -EINVAL;
  496. goto out;
  497. }
  498. dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
  499. cmd_name, out_obj->buffer.length);
  500. print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
  501. out_obj->buffer.pointer,
  502. min_t(u32, 128, out_obj->buffer.length), true);
  503. if (call_pkg) {
  504. call_pkg->nd_fw_size = out_obj->buffer.length;
  505. memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
  506. out_obj->buffer.pointer,
  507. min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
  508. ACPI_FREE(out_obj);
  509. /*
  510. * Need to support FW function w/o known size in advance.
  511. * Caller can determine required size based upon nd_fw_size.
  512. * If we return an error (like elsewhere) then caller wouldn't
  513. * be able to rely upon data returned to make calculation.
  514. */
  515. if (cmd_rc)
  516. *cmd_rc = 0;
  517. return 0;
  518. }
  519. for (i = 0, offset = 0; i < desc->out_num; i++) {
  520. u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
  521. (u32 *) out_obj->buffer.pointer,
  522. out_obj->buffer.length - offset);
  523. if (offset + out_size > out_obj->buffer.length) {
  524. dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
  525. dimm_name, cmd_name, i);
  526. break;
  527. }
  528. if (in_buf.buffer.length + offset + out_size > buf_len) {
  529. dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
  530. dimm_name, cmd_name, i);
  531. rc = -ENXIO;
  532. goto out;
  533. }
  534. memcpy(buf + in_buf.buffer.length + offset,
  535. out_obj->buffer.pointer + offset, out_size);
  536. offset += out_size;
  537. }
  538. /*
  539. * Set fw_status for all the commands with a known format to be
  540. * later interpreted by xlat_status().
  541. */
  542. if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
  543. && cmd <= ND_CMD_CLEAR_ERROR)
  544. || (nvdimm && cmd >= ND_CMD_SMART
  545. && cmd <= ND_CMD_VENDOR)))
  546. fw_status = *(u32 *) out_obj->buffer.pointer;
  547. if (offset + in_buf.buffer.length < buf_len) {
  548. if (i >= 1) {
  549. /*
  550. * status valid, return the number of bytes left
  551. * unfilled in the output buffer
  552. */
  553. rc = buf_len - offset - in_buf.buffer.length;
  554. if (cmd_rc)
  555. *cmd_rc = xlat_status(nvdimm, buf, cmd,
  556. fw_status);
  557. } else {
  558. dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
  559. __func__, dimm_name, cmd_name, buf_len,
  560. offset);
  561. rc = -ENXIO;
  562. }
  563. } else {
  564. rc = 0;
  565. if (cmd_rc)
  566. *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
  567. }
  568. out:
  569. ACPI_FREE(out_obj);
  570. return rc;
  571. }
  572. EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
  573. static const char *spa_type_name(u16 type)
  574. {
  575. static const char *to_name[] = {
  576. [NFIT_SPA_VOLATILE] = "volatile",
  577. [NFIT_SPA_PM] = "pmem",
  578. [NFIT_SPA_DCR] = "dimm-control-region",
  579. [NFIT_SPA_BDW] = "block-data-window",
  580. [NFIT_SPA_VDISK] = "volatile-disk",
  581. [NFIT_SPA_VCD] = "volatile-cd",
  582. [NFIT_SPA_PDISK] = "persistent-disk",
  583. [NFIT_SPA_PCD] = "persistent-cd",
  584. };
  585. if (type > NFIT_SPA_PCD)
  586. return "unknown";
  587. return to_name[type];
  588. }
  589. int nfit_spa_type(struct acpi_nfit_system_address *spa)
  590. {
  591. guid_t guid;
  592. int i;
  593. import_guid(&guid, spa->range_guid);
  594. for (i = 0; i < NFIT_UUID_MAX; i++)
  595. if (guid_equal(to_nfit_uuid(i), &guid))
  596. return i;
  597. return -1;
  598. }
  599. static size_t sizeof_spa(struct acpi_nfit_system_address *spa)
  600. {
  601. if (spa->flags & ACPI_NFIT_LOCATION_COOKIE_VALID)
  602. return sizeof(*spa);
  603. return sizeof(*spa) - 8;
  604. }
  605. static bool add_spa(struct acpi_nfit_desc *acpi_desc,
  606. struct nfit_table_prev *prev,
  607. struct acpi_nfit_system_address *spa)
  608. {
  609. struct device *dev = acpi_desc->dev;
  610. struct nfit_spa *nfit_spa;
  611. if (spa->header.length != sizeof_spa(spa))
  612. return false;
  613. list_for_each_entry(nfit_spa, &prev->spas, list) {
  614. if (memcmp(nfit_spa->spa, spa, sizeof_spa(spa)) == 0) {
  615. list_move_tail(&nfit_spa->list, &acpi_desc->spas);
  616. return true;
  617. }
  618. }
  619. nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof_spa(spa),
  620. GFP_KERNEL);
  621. if (!nfit_spa)
  622. return false;
  623. INIT_LIST_HEAD(&nfit_spa->list);
  624. memcpy(nfit_spa->spa, spa, sizeof_spa(spa));
  625. list_add_tail(&nfit_spa->list, &acpi_desc->spas);
  626. dev_dbg(dev, "spa index: %d type: %s\n",
  627. spa->range_index,
  628. spa_type_name(nfit_spa_type(spa)));
  629. return true;
  630. }
  631. static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
  632. struct nfit_table_prev *prev,
  633. struct acpi_nfit_memory_map *memdev)
  634. {
  635. struct device *dev = acpi_desc->dev;
  636. struct nfit_memdev *nfit_memdev;
  637. if (memdev->header.length != sizeof(*memdev))
  638. return false;
  639. list_for_each_entry(nfit_memdev, &prev->memdevs, list)
  640. if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
  641. list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
  642. return true;
  643. }
  644. nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
  645. GFP_KERNEL);
  646. if (!nfit_memdev)
  647. return false;
  648. INIT_LIST_HEAD(&nfit_memdev->list);
  649. memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
  650. list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
  651. dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
  652. memdev->device_handle, memdev->range_index,
  653. memdev->region_index, memdev->flags);
  654. return true;
  655. }
  656. int nfit_get_smbios_id(u32 device_handle, u16 *flags)
  657. {
  658. struct acpi_nfit_memory_map *memdev;
  659. struct acpi_nfit_desc *acpi_desc;
  660. struct nfit_mem *nfit_mem;
  661. u16 physical_id;
  662. mutex_lock(&acpi_desc_lock);
  663. list_for_each_entry(acpi_desc, &acpi_descs, list) {
  664. mutex_lock(&acpi_desc->init_mutex);
  665. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  666. memdev = __to_nfit_memdev(nfit_mem);
  667. if (memdev->device_handle == device_handle) {
  668. *flags = memdev->flags;
  669. physical_id = memdev->physical_id;
  670. mutex_unlock(&acpi_desc->init_mutex);
  671. mutex_unlock(&acpi_desc_lock);
  672. return physical_id;
  673. }
  674. }
  675. mutex_unlock(&acpi_desc->init_mutex);
  676. }
  677. mutex_unlock(&acpi_desc_lock);
  678. return -ENODEV;
  679. }
  680. EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
  681. /*
  682. * An implementation may provide a truncated control region if no block windows
  683. * are defined.
  684. */
  685. static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
  686. {
  687. if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
  688. window_size))
  689. return 0;
  690. if (dcr->windows)
  691. return sizeof(*dcr);
  692. return offsetof(struct acpi_nfit_control_region, window_size);
  693. }
  694. static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
  695. struct nfit_table_prev *prev,
  696. struct acpi_nfit_control_region *dcr)
  697. {
  698. struct device *dev = acpi_desc->dev;
  699. struct nfit_dcr *nfit_dcr;
  700. if (!sizeof_dcr(dcr))
  701. return false;
  702. list_for_each_entry(nfit_dcr, &prev->dcrs, list)
  703. if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
  704. list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
  705. return true;
  706. }
  707. nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
  708. GFP_KERNEL);
  709. if (!nfit_dcr)
  710. return false;
  711. INIT_LIST_HEAD(&nfit_dcr->list);
  712. memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
  713. list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
  714. dev_dbg(dev, "dcr index: %d windows: %d\n",
  715. dcr->region_index, dcr->windows);
  716. return true;
  717. }
  718. static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
  719. struct nfit_table_prev *prev,
  720. struct acpi_nfit_data_region *bdw)
  721. {
  722. struct device *dev = acpi_desc->dev;
  723. struct nfit_bdw *nfit_bdw;
  724. if (bdw->header.length != sizeof(*bdw))
  725. return false;
  726. list_for_each_entry(nfit_bdw, &prev->bdws, list)
  727. if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
  728. list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
  729. return true;
  730. }
  731. nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
  732. GFP_KERNEL);
  733. if (!nfit_bdw)
  734. return false;
  735. INIT_LIST_HEAD(&nfit_bdw->list);
  736. memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
  737. list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
  738. dev_dbg(dev, "bdw dcr: %d windows: %d\n",
  739. bdw->region_index, bdw->windows);
  740. return true;
  741. }
  742. static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
  743. {
  744. if (idt->header.length < sizeof(*idt))
  745. return 0;
  746. return sizeof(*idt) + sizeof(u32) * idt->line_count;
  747. }
  748. static bool add_idt(struct acpi_nfit_desc *acpi_desc,
  749. struct nfit_table_prev *prev,
  750. struct acpi_nfit_interleave *idt)
  751. {
  752. struct device *dev = acpi_desc->dev;
  753. struct nfit_idt *nfit_idt;
  754. if (!sizeof_idt(idt))
  755. return false;
  756. list_for_each_entry(nfit_idt, &prev->idts, list) {
  757. if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
  758. continue;
  759. if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
  760. list_move_tail(&nfit_idt->list, &acpi_desc->idts);
  761. return true;
  762. }
  763. }
  764. nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
  765. GFP_KERNEL);
  766. if (!nfit_idt)
  767. return false;
  768. INIT_LIST_HEAD(&nfit_idt->list);
  769. memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
  770. list_add_tail(&nfit_idt->list, &acpi_desc->idts);
  771. dev_dbg(dev, "idt index: %d num_lines: %d\n",
  772. idt->interleave_index, idt->line_count);
  773. return true;
  774. }
  775. static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
  776. {
  777. if (flush->header.length < sizeof(*flush))
  778. return 0;
  779. return struct_size(flush, hint_address, flush->hint_count);
  780. }
  781. static bool add_flush(struct acpi_nfit_desc *acpi_desc,
  782. struct nfit_table_prev *prev,
  783. struct acpi_nfit_flush_address *flush)
  784. {
  785. struct device *dev = acpi_desc->dev;
  786. struct nfit_flush *nfit_flush;
  787. if (!sizeof_flush(flush))
  788. return false;
  789. list_for_each_entry(nfit_flush, &prev->flushes, list) {
  790. if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
  791. continue;
  792. if (memcmp(nfit_flush->flush, flush,
  793. sizeof_flush(flush)) == 0) {
  794. list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
  795. return true;
  796. }
  797. }
  798. nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
  799. + sizeof_flush(flush), GFP_KERNEL);
  800. if (!nfit_flush)
  801. return false;
  802. INIT_LIST_HEAD(&nfit_flush->list);
  803. memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
  804. list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
  805. dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
  806. flush->device_handle, flush->hint_count);
  807. return true;
  808. }
  809. static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
  810. struct acpi_nfit_capabilities *pcap)
  811. {
  812. struct device *dev = acpi_desc->dev;
  813. u32 mask;
  814. mask = (1 << (pcap->highest_capability + 1)) - 1;
  815. acpi_desc->platform_cap = pcap->capabilities & mask;
  816. dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
  817. return true;
  818. }
  819. static void *add_table(struct acpi_nfit_desc *acpi_desc,
  820. struct nfit_table_prev *prev, void *table, const void *end)
  821. {
  822. struct device *dev = acpi_desc->dev;
  823. struct acpi_nfit_header *hdr;
  824. void *err = ERR_PTR(-ENOMEM);
  825. if (table >= end)
  826. return NULL;
  827. hdr = table;
  828. if (!hdr->length) {
  829. dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
  830. hdr->type);
  831. return NULL;
  832. }
  833. switch (hdr->type) {
  834. case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
  835. if (!add_spa(acpi_desc, prev, table))
  836. return err;
  837. break;
  838. case ACPI_NFIT_TYPE_MEMORY_MAP:
  839. if (!add_memdev(acpi_desc, prev, table))
  840. return err;
  841. break;
  842. case ACPI_NFIT_TYPE_CONTROL_REGION:
  843. if (!add_dcr(acpi_desc, prev, table))
  844. return err;
  845. break;
  846. case ACPI_NFIT_TYPE_DATA_REGION:
  847. if (!add_bdw(acpi_desc, prev, table))
  848. return err;
  849. break;
  850. case ACPI_NFIT_TYPE_INTERLEAVE:
  851. if (!add_idt(acpi_desc, prev, table))
  852. return err;
  853. break;
  854. case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
  855. if (!add_flush(acpi_desc, prev, table))
  856. return err;
  857. break;
  858. case ACPI_NFIT_TYPE_SMBIOS:
  859. dev_dbg(dev, "smbios\n");
  860. break;
  861. case ACPI_NFIT_TYPE_CAPABILITIES:
  862. if (!add_platform_cap(acpi_desc, table))
  863. return err;
  864. break;
  865. default:
  866. dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
  867. break;
  868. }
  869. return table + hdr->length;
  870. }
  871. static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
  872. struct acpi_nfit_system_address *spa)
  873. {
  874. struct nfit_mem *nfit_mem, *found;
  875. struct nfit_memdev *nfit_memdev;
  876. int type = spa ? nfit_spa_type(spa) : 0;
  877. switch (type) {
  878. case NFIT_SPA_DCR:
  879. case NFIT_SPA_PM:
  880. break;
  881. default:
  882. if (spa)
  883. return 0;
  884. }
  885. /*
  886. * This loop runs in two modes, when a dimm is mapped the loop
  887. * adds memdev associations to an existing dimm, or creates a
  888. * dimm. In the unmapped dimm case this loop sweeps for memdev
  889. * instances with an invalid / zero range_index and adds those
  890. * dimms without spa associations.
  891. */
  892. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  893. struct nfit_flush *nfit_flush;
  894. struct nfit_dcr *nfit_dcr;
  895. u32 device_handle;
  896. u16 dcr;
  897. if (spa && nfit_memdev->memdev->range_index != spa->range_index)
  898. continue;
  899. if (!spa && nfit_memdev->memdev->range_index)
  900. continue;
  901. found = NULL;
  902. dcr = nfit_memdev->memdev->region_index;
  903. device_handle = nfit_memdev->memdev->device_handle;
  904. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
  905. if (__to_nfit_memdev(nfit_mem)->device_handle
  906. == device_handle) {
  907. found = nfit_mem;
  908. break;
  909. }
  910. if (found)
  911. nfit_mem = found;
  912. else {
  913. nfit_mem = devm_kzalloc(acpi_desc->dev,
  914. sizeof(*nfit_mem), GFP_KERNEL);
  915. if (!nfit_mem)
  916. return -ENOMEM;
  917. INIT_LIST_HEAD(&nfit_mem->list);
  918. nfit_mem->acpi_desc = acpi_desc;
  919. list_add(&nfit_mem->list, &acpi_desc->dimms);
  920. }
  921. list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
  922. if (nfit_dcr->dcr->region_index != dcr)
  923. continue;
  924. /*
  925. * Record the control region for the dimm. For
  926. * the ACPI 6.1 case, where there are separate
  927. * control regions for the pmem vs blk
  928. * interfaces, be sure to record the extended
  929. * blk details.
  930. */
  931. if (!nfit_mem->dcr)
  932. nfit_mem->dcr = nfit_dcr->dcr;
  933. else if (nfit_mem->dcr->windows == 0
  934. && nfit_dcr->dcr->windows)
  935. nfit_mem->dcr = nfit_dcr->dcr;
  936. break;
  937. }
  938. list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
  939. struct acpi_nfit_flush_address *flush;
  940. u16 i;
  941. if (nfit_flush->flush->device_handle != device_handle)
  942. continue;
  943. nfit_mem->nfit_flush = nfit_flush;
  944. flush = nfit_flush->flush;
  945. nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
  946. flush->hint_count,
  947. sizeof(struct resource),
  948. GFP_KERNEL);
  949. if (!nfit_mem->flush_wpq)
  950. return -ENOMEM;
  951. for (i = 0; i < flush->hint_count; i++) {
  952. struct resource *res = &nfit_mem->flush_wpq[i];
  953. res->start = flush->hint_address[i];
  954. res->end = res->start + 8 - 1;
  955. }
  956. break;
  957. }
  958. if (dcr && !nfit_mem->dcr) {
  959. dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
  960. spa->range_index, dcr);
  961. return -ENODEV;
  962. }
  963. if (type == NFIT_SPA_DCR) {
  964. struct nfit_idt *nfit_idt;
  965. u16 idt_idx;
  966. /* multiple dimms may share a SPA when interleaved */
  967. nfit_mem->spa_dcr = spa;
  968. nfit_mem->memdev_dcr = nfit_memdev->memdev;
  969. idt_idx = nfit_memdev->memdev->interleave_index;
  970. list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
  971. if (nfit_idt->idt->interleave_index != idt_idx)
  972. continue;
  973. nfit_mem->idt_dcr = nfit_idt->idt;
  974. break;
  975. }
  976. } else if (type == NFIT_SPA_PM) {
  977. /*
  978. * A single dimm may belong to multiple SPA-PM
  979. * ranges, record at least one in addition to
  980. * any SPA-DCR range.
  981. */
  982. nfit_mem->memdev_pmem = nfit_memdev->memdev;
  983. } else
  984. nfit_mem->memdev_dcr = nfit_memdev->memdev;
  985. }
  986. return 0;
  987. }
  988. static int nfit_mem_cmp(void *priv, const struct list_head *_a,
  989. const struct list_head *_b)
  990. {
  991. struct nfit_mem *a = container_of(_a, typeof(*a), list);
  992. struct nfit_mem *b = container_of(_b, typeof(*b), list);
  993. u32 handleA, handleB;
  994. handleA = __to_nfit_memdev(a)->device_handle;
  995. handleB = __to_nfit_memdev(b)->device_handle;
  996. if (handleA < handleB)
  997. return -1;
  998. else if (handleA > handleB)
  999. return 1;
  1000. return 0;
  1001. }
  1002. static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
  1003. {
  1004. struct nfit_spa *nfit_spa;
  1005. int rc;
  1006. /*
  1007. * For each SPA-DCR or SPA-PMEM address range find its
  1008. * corresponding MEMDEV(s). From each MEMDEV find the
  1009. * corresponding DCR. Then, if we're operating on a SPA-DCR,
  1010. * try to find a SPA-BDW and a corresponding BDW that references
  1011. * the DCR. Throw it all into an nfit_mem object. Note, that
  1012. * BDWs are optional.
  1013. */
  1014. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  1015. rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
  1016. if (rc)
  1017. return rc;
  1018. }
  1019. /*
  1020. * If a DIMM has failed to be mapped into SPA there will be no
  1021. * SPA entries above. Find and register all the unmapped DIMMs
  1022. * for reporting and recovery purposes.
  1023. */
  1024. rc = __nfit_mem_init(acpi_desc, NULL);
  1025. if (rc)
  1026. return rc;
  1027. list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
  1028. return 0;
  1029. }
  1030. static ssize_t bus_dsm_mask_show(struct device *dev,
  1031. struct device_attribute *attr, char *buf)
  1032. {
  1033. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  1034. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1035. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1036. return sysfs_emit(buf, "%#lx\n", acpi_desc->bus_dsm_mask);
  1037. }
  1038. static struct device_attribute dev_attr_bus_dsm_mask =
  1039. __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
  1040. static ssize_t revision_show(struct device *dev,
  1041. struct device_attribute *attr, char *buf)
  1042. {
  1043. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  1044. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1045. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1046. return sysfs_emit(buf, "%d\n", acpi_desc->acpi_header.revision);
  1047. }
  1048. static DEVICE_ATTR_RO(revision);
  1049. static ssize_t hw_error_scrub_show(struct device *dev,
  1050. struct device_attribute *attr, char *buf)
  1051. {
  1052. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  1053. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1054. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1055. return sysfs_emit(buf, "%d\n", acpi_desc->scrub_mode);
  1056. }
  1057. /*
  1058. * The 'hw_error_scrub' attribute can have the following values written to it:
  1059. * '0': Switch to the default mode where an exception will only insert
  1060. * the address of the memory error into the poison and badblocks lists.
  1061. * '1': Enable a full scrub to happen if an exception for a memory error is
  1062. * received.
  1063. */
  1064. static ssize_t hw_error_scrub_store(struct device *dev,
  1065. struct device_attribute *attr, const char *buf, size_t size)
  1066. {
  1067. struct nvdimm_bus_descriptor *nd_desc;
  1068. ssize_t rc;
  1069. long val;
  1070. rc = kstrtol(buf, 0, &val);
  1071. if (rc)
  1072. return rc;
  1073. device_lock(dev);
  1074. nd_desc = dev_get_drvdata(dev);
  1075. if (nd_desc) {
  1076. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1077. switch (val) {
  1078. case HW_ERROR_SCRUB_ON:
  1079. acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
  1080. break;
  1081. case HW_ERROR_SCRUB_OFF:
  1082. acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
  1083. break;
  1084. default:
  1085. rc = -EINVAL;
  1086. break;
  1087. }
  1088. }
  1089. device_unlock(dev);
  1090. if (rc)
  1091. return rc;
  1092. return size;
  1093. }
  1094. static DEVICE_ATTR_RW(hw_error_scrub);
  1095. /*
  1096. * This shows the number of full Address Range Scrubs that have been
  1097. * completed since driver load time. Userspace can wait on this using
  1098. * select/poll etc. A '+' at the end indicates an ARS is in progress
  1099. */
  1100. static ssize_t scrub_show(struct device *dev,
  1101. struct device_attribute *attr, char *buf)
  1102. {
  1103. struct nvdimm_bus_descriptor *nd_desc;
  1104. struct acpi_nfit_desc *acpi_desc;
  1105. ssize_t rc = -ENXIO;
  1106. bool busy;
  1107. device_lock(dev);
  1108. nd_desc = dev_get_drvdata(dev);
  1109. if (!nd_desc) {
  1110. device_unlock(dev);
  1111. return rc;
  1112. }
  1113. acpi_desc = to_acpi_desc(nd_desc);
  1114. mutex_lock(&acpi_desc->init_mutex);
  1115. busy = test_bit(ARS_BUSY, &acpi_desc->scrub_flags)
  1116. && !test_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
  1117. rc = sysfs_emit(buf, "%d%s", acpi_desc->scrub_count, busy ? "+\n" : "\n");
  1118. /* Allow an admin to poll the busy state at a higher rate */
  1119. if (busy && capable(CAP_SYS_RAWIO) && !test_and_set_bit(ARS_POLL,
  1120. &acpi_desc->scrub_flags)) {
  1121. acpi_desc->scrub_tmo = 1;
  1122. mod_delayed_work(nfit_wq, &acpi_desc->dwork, HZ);
  1123. }
  1124. mutex_unlock(&acpi_desc->init_mutex);
  1125. device_unlock(dev);
  1126. return rc;
  1127. }
  1128. static ssize_t scrub_store(struct device *dev,
  1129. struct device_attribute *attr, const char *buf, size_t size)
  1130. {
  1131. struct nvdimm_bus_descriptor *nd_desc;
  1132. ssize_t rc;
  1133. long val;
  1134. rc = kstrtol(buf, 0, &val);
  1135. if (rc)
  1136. return rc;
  1137. if (val != 1)
  1138. return -EINVAL;
  1139. device_lock(dev);
  1140. nd_desc = dev_get_drvdata(dev);
  1141. if (nd_desc) {
  1142. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  1143. rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
  1144. }
  1145. device_unlock(dev);
  1146. if (rc)
  1147. return rc;
  1148. return size;
  1149. }
  1150. static DEVICE_ATTR_RW(scrub);
  1151. static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
  1152. {
  1153. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1154. const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
  1155. | 1 << ND_CMD_ARS_STATUS;
  1156. return (nd_desc->cmd_mask & mask) == mask;
  1157. }
  1158. static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
  1159. {
  1160. struct device *dev = kobj_to_dev(kobj);
  1161. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  1162. if (a == &dev_attr_scrub.attr)
  1163. return ars_supported(nvdimm_bus) ? a->mode : 0;
  1164. if (a == &dev_attr_firmware_activate_noidle.attr)
  1165. return intel_fwa_supported(nvdimm_bus) ? a->mode : 0;
  1166. return a->mode;
  1167. }
  1168. static struct attribute *acpi_nfit_attributes[] = {
  1169. &dev_attr_revision.attr,
  1170. &dev_attr_scrub.attr,
  1171. &dev_attr_hw_error_scrub.attr,
  1172. &dev_attr_bus_dsm_mask.attr,
  1173. &dev_attr_firmware_activate_noidle.attr,
  1174. NULL,
  1175. };
  1176. static const struct attribute_group acpi_nfit_attribute_group = {
  1177. .name = "nfit",
  1178. .attrs = acpi_nfit_attributes,
  1179. .is_visible = nfit_visible,
  1180. };
  1181. static const struct attribute_group *acpi_nfit_attribute_groups[] = {
  1182. &acpi_nfit_attribute_group,
  1183. NULL,
  1184. };
  1185. static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
  1186. {
  1187. struct nvdimm *nvdimm = to_nvdimm(dev);
  1188. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1189. return __to_nfit_memdev(nfit_mem);
  1190. }
  1191. static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
  1192. {
  1193. struct nvdimm *nvdimm = to_nvdimm(dev);
  1194. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1195. return nfit_mem->dcr;
  1196. }
  1197. static ssize_t handle_show(struct device *dev,
  1198. struct device_attribute *attr, char *buf)
  1199. {
  1200. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  1201. return sysfs_emit(buf, "%#x\n", memdev->device_handle);
  1202. }
  1203. static DEVICE_ATTR_RO(handle);
  1204. static ssize_t phys_id_show(struct device *dev,
  1205. struct device_attribute *attr, char *buf)
  1206. {
  1207. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  1208. return sysfs_emit(buf, "%#x\n", memdev->physical_id);
  1209. }
  1210. static DEVICE_ATTR_RO(phys_id);
  1211. static ssize_t vendor_show(struct device *dev,
  1212. struct device_attribute *attr, char *buf)
  1213. {
  1214. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1215. return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
  1216. }
  1217. static DEVICE_ATTR_RO(vendor);
  1218. static ssize_t rev_id_show(struct device *dev,
  1219. struct device_attribute *attr, char *buf)
  1220. {
  1221. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1222. return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
  1223. }
  1224. static DEVICE_ATTR_RO(rev_id);
  1225. static ssize_t device_show(struct device *dev,
  1226. struct device_attribute *attr, char *buf)
  1227. {
  1228. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1229. return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
  1230. }
  1231. static DEVICE_ATTR_RO(device);
  1232. static ssize_t subsystem_vendor_show(struct device *dev,
  1233. struct device_attribute *attr, char *buf)
  1234. {
  1235. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1236. return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
  1237. }
  1238. static DEVICE_ATTR_RO(subsystem_vendor);
  1239. static ssize_t subsystem_rev_id_show(struct device *dev,
  1240. struct device_attribute *attr, char *buf)
  1241. {
  1242. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1243. return sysfs_emit(buf, "0x%04x\n",
  1244. be16_to_cpu(dcr->subsystem_revision_id));
  1245. }
  1246. static DEVICE_ATTR_RO(subsystem_rev_id);
  1247. static ssize_t subsystem_device_show(struct device *dev,
  1248. struct device_attribute *attr, char *buf)
  1249. {
  1250. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1251. return sysfs_emit(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
  1252. }
  1253. static DEVICE_ATTR_RO(subsystem_device);
  1254. static int num_nvdimm_formats(struct nvdimm *nvdimm)
  1255. {
  1256. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1257. int formats = 0;
  1258. if (nfit_mem->memdev_pmem)
  1259. formats++;
  1260. return formats;
  1261. }
  1262. static ssize_t format_show(struct device *dev,
  1263. struct device_attribute *attr, char *buf)
  1264. {
  1265. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1266. return sysfs_emit(buf, "0x%04x\n", le16_to_cpu(dcr->code));
  1267. }
  1268. static DEVICE_ATTR_RO(format);
  1269. static ssize_t format1_show(struct device *dev,
  1270. struct device_attribute *attr, char *buf)
  1271. {
  1272. u32 handle;
  1273. ssize_t rc = -ENXIO;
  1274. struct nfit_mem *nfit_mem;
  1275. struct nfit_memdev *nfit_memdev;
  1276. struct acpi_nfit_desc *acpi_desc;
  1277. struct nvdimm *nvdimm = to_nvdimm(dev);
  1278. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1279. nfit_mem = nvdimm_provider_data(nvdimm);
  1280. acpi_desc = nfit_mem->acpi_desc;
  1281. handle = to_nfit_memdev(dev)->device_handle;
  1282. /* assumes DIMMs have at most 2 published interface codes */
  1283. mutex_lock(&acpi_desc->init_mutex);
  1284. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  1285. struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
  1286. struct nfit_dcr *nfit_dcr;
  1287. if (memdev->device_handle != handle)
  1288. continue;
  1289. list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
  1290. if (nfit_dcr->dcr->region_index != memdev->region_index)
  1291. continue;
  1292. if (nfit_dcr->dcr->code == dcr->code)
  1293. continue;
  1294. rc = sysfs_emit(buf, "0x%04x\n",
  1295. le16_to_cpu(nfit_dcr->dcr->code));
  1296. break;
  1297. }
  1298. if (rc != -ENXIO)
  1299. break;
  1300. }
  1301. mutex_unlock(&acpi_desc->init_mutex);
  1302. return rc;
  1303. }
  1304. static DEVICE_ATTR_RO(format1);
  1305. static ssize_t formats_show(struct device *dev,
  1306. struct device_attribute *attr, char *buf)
  1307. {
  1308. struct nvdimm *nvdimm = to_nvdimm(dev);
  1309. return sysfs_emit(buf, "%d\n", num_nvdimm_formats(nvdimm));
  1310. }
  1311. static DEVICE_ATTR_RO(formats);
  1312. static ssize_t serial_show(struct device *dev,
  1313. struct device_attribute *attr, char *buf)
  1314. {
  1315. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1316. return sysfs_emit(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
  1317. }
  1318. static DEVICE_ATTR_RO(serial);
  1319. static ssize_t family_show(struct device *dev,
  1320. struct device_attribute *attr, char *buf)
  1321. {
  1322. struct nvdimm *nvdimm = to_nvdimm(dev);
  1323. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1324. if (nfit_mem->family < 0)
  1325. return -ENXIO;
  1326. return sysfs_emit(buf, "%d\n", nfit_mem->family);
  1327. }
  1328. static DEVICE_ATTR_RO(family);
  1329. static ssize_t dsm_mask_show(struct device *dev,
  1330. struct device_attribute *attr, char *buf)
  1331. {
  1332. struct nvdimm *nvdimm = to_nvdimm(dev);
  1333. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1334. if (nfit_mem->family < 0)
  1335. return -ENXIO;
  1336. return sysfs_emit(buf, "%#lx\n", nfit_mem->dsm_mask);
  1337. }
  1338. static DEVICE_ATTR_RO(dsm_mask);
  1339. static ssize_t flags_show(struct device *dev,
  1340. struct device_attribute *attr, char *buf)
  1341. {
  1342. struct nvdimm *nvdimm = to_nvdimm(dev);
  1343. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1344. u16 flags = __to_nfit_memdev(nfit_mem)->flags;
  1345. if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
  1346. flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
  1347. return sysfs_emit(buf, "%s%s%s%s%s%s%s\n",
  1348. flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
  1349. flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
  1350. flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
  1351. flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
  1352. flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
  1353. flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
  1354. flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
  1355. }
  1356. static DEVICE_ATTR_RO(flags);
  1357. static ssize_t id_show(struct device *dev,
  1358. struct device_attribute *attr, char *buf)
  1359. {
  1360. struct nvdimm *nvdimm = to_nvdimm(dev);
  1361. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1362. return sysfs_emit(buf, "%s\n", nfit_mem->id);
  1363. }
  1364. static DEVICE_ATTR_RO(id);
  1365. static ssize_t dirty_shutdown_show(struct device *dev,
  1366. struct device_attribute *attr, char *buf)
  1367. {
  1368. struct nvdimm *nvdimm = to_nvdimm(dev);
  1369. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1370. return sysfs_emit(buf, "%d\n", nfit_mem->dirty_shutdown);
  1371. }
  1372. static DEVICE_ATTR_RO(dirty_shutdown);
  1373. static struct attribute *acpi_nfit_dimm_attributes[] = {
  1374. &dev_attr_handle.attr,
  1375. &dev_attr_phys_id.attr,
  1376. &dev_attr_vendor.attr,
  1377. &dev_attr_device.attr,
  1378. &dev_attr_rev_id.attr,
  1379. &dev_attr_subsystem_vendor.attr,
  1380. &dev_attr_subsystem_device.attr,
  1381. &dev_attr_subsystem_rev_id.attr,
  1382. &dev_attr_format.attr,
  1383. &dev_attr_formats.attr,
  1384. &dev_attr_format1.attr,
  1385. &dev_attr_serial.attr,
  1386. &dev_attr_flags.attr,
  1387. &dev_attr_id.attr,
  1388. &dev_attr_family.attr,
  1389. &dev_attr_dsm_mask.attr,
  1390. &dev_attr_dirty_shutdown.attr,
  1391. NULL,
  1392. };
  1393. static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
  1394. struct attribute *a, int n)
  1395. {
  1396. struct device *dev = kobj_to_dev(kobj);
  1397. struct nvdimm *nvdimm = to_nvdimm(dev);
  1398. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1399. if (!to_nfit_dcr(dev)) {
  1400. /* Without a dcr only the memdev attributes can be surfaced */
  1401. if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
  1402. || a == &dev_attr_flags.attr
  1403. || a == &dev_attr_family.attr
  1404. || a == &dev_attr_dsm_mask.attr)
  1405. return a->mode;
  1406. return 0;
  1407. }
  1408. if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
  1409. return 0;
  1410. if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
  1411. && a == &dev_attr_dirty_shutdown.attr)
  1412. return 0;
  1413. return a->mode;
  1414. }
  1415. static const struct attribute_group acpi_nfit_dimm_attribute_group = {
  1416. .name = "nfit",
  1417. .attrs = acpi_nfit_dimm_attributes,
  1418. .is_visible = acpi_nfit_dimm_attr_visible,
  1419. };
  1420. static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
  1421. &acpi_nfit_dimm_attribute_group,
  1422. NULL,
  1423. };
  1424. static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
  1425. u32 device_handle)
  1426. {
  1427. struct nfit_mem *nfit_mem;
  1428. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
  1429. if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
  1430. return nfit_mem->nvdimm;
  1431. return NULL;
  1432. }
  1433. void __acpi_nvdimm_notify(struct device *dev, u32 event)
  1434. {
  1435. struct nfit_mem *nfit_mem;
  1436. struct acpi_nfit_desc *acpi_desc;
  1437. dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
  1438. event);
  1439. if (event != NFIT_NOTIFY_DIMM_HEALTH) {
  1440. dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
  1441. event);
  1442. return;
  1443. }
  1444. acpi_desc = dev_get_drvdata(dev->parent);
  1445. if (!acpi_desc)
  1446. return;
  1447. /*
  1448. * If we successfully retrieved acpi_desc, then we know nfit_mem data
  1449. * is still valid.
  1450. */
  1451. nfit_mem = dev_get_drvdata(dev);
  1452. if (nfit_mem && nfit_mem->flags_attr)
  1453. sysfs_notify_dirent(nfit_mem->flags_attr);
  1454. }
  1455. EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
  1456. static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
  1457. {
  1458. struct acpi_device *adev = data;
  1459. struct device *dev = &adev->dev;
  1460. device_lock(dev->parent);
  1461. __acpi_nvdimm_notify(dev, event);
  1462. device_unlock(dev->parent);
  1463. }
  1464. static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
  1465. {
  1466. acpi_handle handle;
  1467. acpi_status status;
  1468. status = acpi_get_handle(adev->handle, method, &handle);
  1469. if (ACPI_SUCCESS(status))
  1470. return true;
  1471. return false;
  1472. }
  1473. __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
  1474. {
  1475. struct device *dev = &nfit_mem->adev->dev;
  1476. struct nd_intel_smart smart = { 0 };
  1477. union acpi_object in_buf = {
  1478. .buffer.type = ACPI_TYPE_BUFFER,
  1479. .buffer.length = 0,
  1480. };
  1481. union acpi_object in_obj = {
  1482. .package.type = ACPI_TYPE_PACKAGE,
  1483. .package.count = 1,
  1484. .package.elements = &in_buf,
  1485. };
  1486. const u8 func = ND_INTEL_SMART;
  1487. const guid_t *guid = to_nfit_uuid(nfit_mem->family);
  1488. u8 revid = nfit_dsm_revid(nfit_mem->family, func);
  1489. struct acpi_device *adev = nfit_mem->adev;
  1490. acpi_handle handle = adev->handle;
  1491. union acpi_object *out_obj;
  1492. if ((nfit_mem->dsm_mask & (1 << func)) == 0)
  1493. return;
  1494. out_obj = acpi_evaluate_dsm_typed(handle, guid, revid, func, &in_obj, ACPI_TYPE_BUFFER);
  1495. if (!out_obj || out_obj->buffer.length < sizeof(smart)) {
  1496. dev_dbg(dev->parent, "%s: failed to retrieve initial health\n",
  1497. dev_name(dev));
  1498. ACPI_FREE(out_obj);
  1499. return;
  1500. }
  1501. memcpy(&smart, out_obj->buffer.pointer, sizeof(smart));
  1502. ACPI_FREE(out_obj);
  1503. if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
  1504. if (smart.shutdown_state)
  1505. set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
  1506. }
  1507. if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
  1508. set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
  1509. nfit_mem->dirty_shutdown = smart.shutdown_count;
  1510. }
  1511. }
  1512. static void populate_shutdown_status(struct nfit_mem *nfit_mem)
  1513. {
  1514. /*
  1515. * For DIMMs that provide a dynamic facility to retrieve a
  1516. * dirty-shutdown status and/or a dirty-shutdown count, cache
  1517. * these values in nfit_mem.
  1518. */
  1519. if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
  1520. nfit_intel_shutdown_status(nfit_mem);
  1521. }
  1522. static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
  1523. struct nfit_mem *nfit_mem, u32 device_handle)
  1524. {
  1525. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1526. struct acpi_device *adev, *adev_dimm;
  1527. struct device *dev = acpi_desc->dev;
  1528. unsigned long dsm_mask, label_mask;
  1529. const guid_t *guid;
  1530. int i;
  1531. int family = -1;
  1532. struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
  1533. /* nfit test assumes 1:1 relationship between commands and dsms */
  1534. nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
  1535. nfit_mem->family = NVDIMM_FAMILY_INTEL;
  1536. set_bit(NVDIMM_FAMILY_INTEL, &nd_desc->dimm_family_mask);
  1537. if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
  1538. sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
  1539. be16_to_cpu(dcr->vendor_id),
  1540. dcr->manufacturing_location,
  1541. be16_to_cpu(dcr->manufacturing_date),
  1542. be32_to_cpu(dcr->serial_number));
  1543. else
  1544. sprintf(nfit_mem->id, "%04x-%08x",
  1545. be16_to_cpu(dcr->vendor_id),
  1546. be32_to_cpu(dcr->serial_number));
  1547. adev = to_acpi_dev(acpi_desc);
  1548. if (!adev) {
  1549. /* unit test case */
  1550. populate_shutdown_status(nfit_mem);
  1551. return 0;
  1552. }
  1553. adev_dimm = acpi_find_child_device(adev, device_handle, false);
  1554. nfit_mem->adev = adev_dimm;
  1555. if (!adev_dimm) {
  1556. dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
  1557. device_handle);
  1558. return force_enable_dimms ? 0 : -ENODEV;
  1559. }
  1560. if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
  1561. ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
  1562. dev_err(dev, "%s: notification registration failed\n",
  1563. dev_name(&adev_dimm->dev));
  1564. return -ENXIO;
  1565. }
  1566. /*
  1567. * Record nfit_mem for the notification path to track back to
  1568. * the nfit sysfs attributes for this dimm device object.
  1569. */
  1570. dev_set_drvdata(&adev_dimm->dev, nfit_mem);
  1571. /*
  1572. * There are 4 "legacy" NVDIMM command sets
  1573. * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
  1574. * an EFI working group was established to constrain this
  1575. * proliferation. The nfit driver probes for the supported command
  1576. * set by GUID. Note, if you're a platform developer looking to add
  1577. * a new command set to this probe, consider using an existing set,
  1578. * or otherwise seek approval to publish the command set at
  1579. * http://www.uefi.org/RFIC_LIST.
  1580. *
  1581. * Note, that checking for function0 (bit0) tells us if any commands
  1582. * are reachable through this GUID.
  1583. */
  1584. clear_bit(NVDIMM_FAMILY_INTEL, &nd_desc->dimm_family_mask);
  1585. for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
  1586. if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1)) {
  1587. set_bit(i, &nd_desc->dimm_family_mask);
  1588. if (family < 0 || i == default_dsm_family)
  1589. family = i;
  1590. }
  1591. /* limit the supported commands to those that are publicly documented */
  1592. nfit_mem->family = family;
  1593. if (override_dsm_mask && !disable_vendor_specific)
  1594. dsm_mask = override_dsm_mask;
  1595. else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
  1596. dsm_mask = NVDIMM_INTEL_CMDMASK;
  1597. if (disable_vendor_specific)
  1598. dsm_mask &= ~(1 << ND_CMD_VENDOR);
  1599. } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
  1600. dsm_mask = 0x1c3c76;
  1601. } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
  1602. dsm_mask = 0x1fe;
  1603. if (disable_vendor_specific)
  1604. dsm_mask &= ~(1 << 8);
  1605. } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
  1606. dsm_mask = 0xffffffff;
  1607. } else if (nfit_mem->family == NVDIMM_FAMILY_HYPERV) {
  1608. dsm_mask = 0x1f;
  1609. } else {
  1610. dev_dbg(dev, "unknown dimm command family\n");
  1611. nfit_mem->family = -1;
  1612. /* DSMs are optional, continue loading the driver... */
  1613. return 0;
  1614. }
  1615. /*
  1616. * Function 0 is the command interrogation function, don't
  1617. * export it to potential userspace use, and enable it to be
  1618. * used as an error value in acpi_nfit_ctl().
  1619. */
  1620. dsm_mask &= ~1UL;
  1621. guid = to_nfit_uuid(nfit_mem->family);
  1622. for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
  1623. if (acpi_check_dsm(adev_dimm->handle, guid,
  1624. nfit_dsm_revid(nfit_mem->family, i),
  1625. 1ULL << i))
  1626. set_bit(i, &nfit_mem->dsm_mask);
  1627. /*
  1628. * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
  1629. * due to their better semantics handling locked capacity.
  1630. */
  1631. label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
  1632. | 1 << ND_CMD_SET_CONFIG_DATA;
  1633. if (family == NVDIMM_FAMILY_INTEL
  1634. && (dsm_mask & label_mask) == label_mask)
  1635. /* skip _LS{I,R,W} enabling */;
  1636. else {
  1637. if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
  1638. && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
  1639. dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
  1640. set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
  1641. }
  1642. if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
  1643. && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
  1644. dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
  1645. set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
  1646. }
  1647. /*
  1648. * Quirk read-only label configurations to preserve
  1649. * access to label-less namespaces by default.
  1650. */
  1651. if (!test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
  1652. && !force_labels) {
  1653. dev_dbg(dev, "%s: No _LSW, disable labels\n",
  1654. dev_name(&adev_dimm->dev));
  1655. clear_bit(NFIT_MEM_LSR, &nfit_mem->flags);
  1656. } else
  1657. dev_dbg(dev, "%s: Force enable labels\n",
  1658. dev_name(&adev_dimm->dev));
  1659. }
  1660. populate_shutdown_status(nfit_mem);
  1661. return 0;
  1662. }
  1663. static void shutdown_dimm_notify(void *data)
  1664. {
  1665. struct acpi_nfit_desc *acpi_desc = data;
  1666. struct nfit_mem *nfit_mem;
  1667. mutex_lock(&acpi_desc->init_mutex);
  1668. /*
  1669. * Clear out the nfit_mem->flags_attr and shut down dimm event
  1670. * notifications.
  1671. */
  1672. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1673. struct acpi_device *adev_dimm = nfit_mem->adev;
  1674. if (nfit_mem->flags_attr) {
  1675. sysfs_put(nfit_mem->flags_attr);
  1676. nfit_mem->flags_attr = NULL;
  1677. }
  1678. if (adev_dimm) {
  1679. acpi_remove_notify_handler(adev_dimm->handle,
  1680. ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
  1681. dev_set_drvdata(&adev_dimm->dev, NULL);
  1682. }
  1683. }
  1684. mutex_unlock(&acpi_desc->init_mutex);
  1685. }
  1686. static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
  1687. {
  1688. switch (family) {
  1689. case NVDIMM_FAMILY_INTEL:
  1690. return intel_security_ops;
  1691. default:
  1692. return NULL;
  1693. }
  1694. }
  1695. static const struct nvdimm_fw_ops *acpi_nfit_get_fw_ops(
  1696. struct nfit_mem *nfit_mem)
  1697. {
  1698. unsigned long mask;
  1699. struct acpi_nfit_desc *acpi_desc = nfit_mem->acpi_desc;
  1700. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1701. if (!nd_desc->fw_ops)
  1702. return NULL;
  1703. if (nfit_mem->family != NVDIMM_FAMILY_INTEL)
  1704. return NULL;
  1705. mask = nfit_mem->dsm_mask & NVDIMM_INTEL_FW_ACTIVATE_CMDMASK;
  1706. if (mask != NVDIMM_INTEL_FW_ACTIVATE_CMDMASK)
  1707. return NULL;
  1708. return intel_fw_ops;
  1709. }
  1710. static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
  1711. {
  1712. struct nfit_mem *nfit_mem;
  1713. int dimm_count = 0, rc;
  1714. struct nvdimm *nvdimm;
  1715. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1716. struct acpi_nfit_flush_address *flush;
  1717. unsigned long flags = 0, cmd_mask;
  1718. struct nfit_memdev *nfit_memdev;
  1719. u32 device_handle;
  1720. u16 mem_flags;
  1721. device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
  1722. nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
  1723. if (nvdimm) {
  1724. dimm_count++;
  1725. continue;
  1726. }
  1727. /* collate flags across all memdevs for this dimm */
  1728. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  1729. struct acpi_nfit_memory_map *dimm_memdev;
  1730. dimm_memdev = __to_nfit_memdev(nfit_mem);
  1731. if (dimm_memdev->device_handle
  1732. != nfit_memdev->memdev->device_handle)
  1733. continue;
  1734. dimm_memdev->flags |= nfit_memdev->memdev->flags;
  1735. }
  1736. mem_flags = __to_nfit_memdev(nfit_mem)->flags;
  1737. if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
  1738. set_bit(NDD_UNARMED, &flags);
  1739. rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
  1740. if (rc)
  1741. continue;
  1742. /*
  1743. * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
  1744. * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
  1745. * userspace interface.
  1746. */
  1747. cmd_mask = 1UL << ND_CMD_CALL;
  1748. if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
  1749. /*
  1750. * These commands have a 1:1 correspondence
  1751. * between DSM payload and libnvdimm ioctl
  1752. * payload format.
  1753. */
  1754. cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
  1755. }
  1756. if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
  1757. set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
  1758. set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
  1759. }
  1760. if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
  1761. set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
  1762. flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
  1763. : NULL;
  1764. nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
  1765. acpi_nfit_dimm_attribute_groups,
  1766. flags, cmd_mask, flush ? flush->hint_count : 0,
  1767. nfit_mem->flush_wpq, &nfit_mem->id[0],
  1768. acpi_nfit_get_security_ops(nfit_mem->family),
  1769. acpi_nfit_get_fw_ops(nfit_mem));
  1770. if (!nvdimm)
  1771. return -ENOMEM;
  1772. nfit_mem->nvdimm = nvdimm;
  1773. dimm_count++;
  1774. if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
  1775. continue;
  1776. dev_err(acpi_desc->dev, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
  1777. nvdimm_name(nvdimm),
  1778. mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
  1779. mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
  1780. mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
  1781. mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
  1782. mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
  1783. }
  1784. rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
  1785. if (rc)
  1786. return rc;
  1787. /*
  1788. * Now that dimms are successfully registered, and async registration
  1789. * is flushed, attempt to enable event notification.
  1790. */
  1791. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1792. struct kernfs_node *nfit_kernfs;
  1793. nvdimm = nfit_mem->nvdimm;
  1794. if (!nvdimm)
  1795. continue;
  1796. nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
  1797. if (nfit_kernfs)
  1798. nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
  1799. "flags");
  1800. sysfs_put(nfit_kernfs);
  1801. if (!nfit_mem->flags_attr)
  1802. dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
  1803. nvdimm_name(nvdimm));
  1804. }
  1805. return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
  1806. acpi_desc);
  1807. }
  1808. /*
  1809. * These constants are private because there are no kernel consumers of
  1810. * these commands.
  1811. */
  1812. enum nfit_aux_cmds {
  1813. NFIT_CMD_TRANSLATE_SPA = 5,
  1814. NFIT_CMD_ARS_INJECT_SET = 7,
  1815. NFIT_CMD_ARS_INJECT_CLEAR = 8,
  1816. NFIT_CMD_ARS_INJECT_GET = 9,
  1817. };
  1818. static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
  1819. {
  1820. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1821. const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
  1822. unsigned long dsm_mask, *mask;
  1823. struct acpi_device *adev;
  1824. int i;
  1825. set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
  1826. set_bit(NVDIMM_BUS_FAMILY_NFIT, &nd_desc->bus_family_mask);
  1827. /* enable nfit_test to inject bus command emulation */
  1828. if (acpi_desc->bus_cmd_force_en) {
  1829. nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
  1830. mask = &nd_desc->bus_family_mask;
  1831. if (acpi_desc->family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL]) {
  1832. set_bit(NVDIMM_BUS_FAMILY_INTEL, mask);
  1833. nd_desc->fw_ops = intel_bus_fw_ops;
  1834. }
  1835. }
  1836. adev = to_acpi_dev(acpi_desc);
  1837. if (!adev)
  1838. return;
  1839. for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
  1840. if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
  1841. set_bit(i, &nd_desc->cmd_mask);
  1842. dsm_mask =
  1843. (1 << ND_CMD_ARS_CAP) |
  1844. (1 << ND_CMD_ARS_START) |
  1845. (1 << ND_CMD_ARS_STATUS) |
  1846. (1 << ND_CMD_CLEAR_ERROR) |
  1847. (1 << NFIT_CMD_TRANSLATE_SPA) |
  1848. (1 << NFIT_CMD_ARS_INJECT_SET) |
  1849. (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
  1850. (1 << NFIT_CMD_ARS_INJECT_GET);
  1851. for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
  1852. if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
  1853. set_bit(i, &acpi_desc->bus_dsm_mask);
  1854. /* Enumerate allowed NVDIMM_BUS_FAMILY_INTEL commands */
  1855. dsm_mask = NVDIMM_BUS_INTEL_FW_ACTIVATE_CMDMASK;
  1856. guid = to_nfit_bus_uuid(NVDIMM_BUS_FAMILY_INTEL);
  1857. mask = &acpi_desc->family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL];
  1858. for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
  1859. if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
  1860. set_bit(i, mask);
  1861. if (*mask == dsm_mask) {
  1862. set_bit(NVDIMM_BUS_FAMILY_INTEL, &nd_desc->bus_family_mask);
  1863. nd_desc->fw_ops = intel_bus_fw_ops;
  1864. }
  1865. }
  1866. static ssize_t range_index_show(struct device *dev,
  1867. struct device_attribute *attr, char *buf)
  1868. {
  1869. struct nd_region *nd_region = to_nd_region(dev);
  1870. struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
  1871. return sysfs_emit(buf, "%d\n", nfit_spa->spa->range_index);
  1872. }
  1873. static DEVICE_ATTR_RO(range_index);
  1874. static struct attribute *acpi_nfit_region_attributes[] = {
  1875. &dev_attr_range_index.attr,
  1876. NULL,
  1877. };
  1878. static const struct attribute_group acpi_nfit_region_attribute_group = {
  1879. .name = "nfit",
  1880. .attrs = acpi_nfit_region_attributes,
  1881. };
  1882. static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
  1883. &acpi_nfit_region_attribute_group,
  1884. NULL,
  1885. };
  1886. /* enough info to uniquely specify an interleave set */
  1887. struct nfit_set_info {
  1888. u64 region_offset;
  1889. u32 serial_number;
  1890. u32 pad;
  1891. };
  1892. struct nfit_set_info2 {
  1893. u64 region_offset;
  1894. u32 serial_number;
  1895. u16 vendor_id;
  1896. u16 manufacturing_date;
  1897. u8 manufacturing_location;
  1898. u8 reserved[31];
  1899. };
  1900. static int cmp_map_compat(const void *m0, const void *m1)
  1901. {
  1902. const struct nfit_set_info *map0 = m0;
  1903. const struct nfit_set_info *map1 = m1;
  1904. return memcmp(&map0->region_offset, &map1->region_offset,
  1905. sizeof(u64));
  1906. }
  1907. static int cmp_map(const void *m0, const void *m1)
  1908. {
  1909. const struct nfit_set_info *map0 = m0;
  1910. const struct nfit_set_info *map1 = m1;
  1911. if (map0->region_offset < map1->region_offset)
  1912. return -1;
  1913. else if (map0->region_offset > map1->region_offset)
  1914. return 1;
  1915. return 0;
  1916. }
  1917. static int cmp_map2(const void *m0, const void *m1)
  1918. {
  1919. const struct nfit_set_info2 *map0 = m0;
  1920. const struct nfit_set_info2 *map1 = m1;
  1921. if (map0->region_offset < map1->region_offset)
  1922. return -1;
  1923. else if (map0->region_offset > map1->region_offset)
  1924. return 1;
  1925. return 0;
  1926. }
  1927. /* Retrieve the nth entry referencing this spa */
  1928. static struct acpi_nfit_memory_map *memdev_from_spa(
  1929. struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
  1930. {
  1931. struct nfit_memdev *nfit_memdev;
  1932. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
  1933. if (nfit_memdev->memdev->range_index == range_index)
  1934. if (n-- == 0)
  1935. return nfit_memdev->memdev;
  1936. return NULL;
  1937. }
  1938. static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
  1939. struct nd_region_desc *ndr_desc,
  1940. struct acpi_nfit_system_address *spa)
  1941. {
  1942. u16 nr = ndr_desc->num_mappings;
  1943. struct nfit_set_info2 *info2 __free(kfree) =
  1944. kcalloc(nr, sizeof(*info2), GFP_KERNEL);
  1945. struct nfit_set_info *info __free(kfree) =
  1946. kcalloc(nr, sizeof(*info), GFP_KERNEL);
  1947. struct device *dev = acpi_desc->dev;
  1948. struct nd_interleave_set *nd_set;
  1949. int i;
  1950. if (!info || !info2)
  1951. return -ENOMEM;
  1952. nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
  1953. if (!nd_set)
  1954. return -ENOMEM;
  1955. import_guid(&nd_set->type_guid, spa->range_guid);
  1956. for (i = 0; i < nr; i++) {
  1957. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  1958. struct nvdimm *nvdimm = mapping->nvdimm;
  1959. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1960. struct nfit_set_info *map = &info[i];
  1961. struct nfit_set_info2 *map2 = &info2[i];
  1962. struct acpi_nfit_memory_map *memdev =
  1963. memdev_from_spa(acpi_desc, spa->range_index, i);
  1964. struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
  1965. if (!memdev || !nfit_mem->dcr) {
  1966. dev_err(dev, "%s: failed to find DCR\n", __func__);
  1967. return -ENODEV;
  1968. }
  1969. map->region_offset = memdev->region_offset;
  1970. map->serial_number = dcr->serial_number;
  1971. map2->region_offset = memdev->region_offset;
  1972. map2->serial_number = dcr->serial_number;
  1973. map2->vendor_id = dcr->vendor_id;
  1974. map2->manufacturing_date = dcr->manufacturing_date;
  1975. map2->manufacturing_location = dcr->manufacturing_location;
  1976. }
  1977. /* v1.1 namespaces */
  1978. sort(info, nr, sizeof(*info), cmp_map, NULL);
  1979. nd_set->cookie1 = nd_fletcher64(info, sizeof(*info) * nr, 0);
  1980. /* v1.2 namespaces */
  1981. sort(info2, nr, sizeof(*info2), cmp_map2, NULL);
  1982. nd_set->cookie2 = nd_fletcher64(info2, sizeof(*info2) * nr, 0);
  1983. /* support v1.1 namespaces created with the wrong sort order */
  1984. sort(info, nr, sizeof(*info), cmp_map_compat, NULL);
  1985. nd_set->altcookie = nd_fletcher64(info, sizeof(*info) * nr, 0);
  1986. /* record the result of the sort for the mapping position */
  1987. for (i = 0; i < nr; i++) {
  1988. struct nfit_set_info2 *map2 = &info2[i];
  1989. int j;
  1990. for (j = 0; j < nr; j++) {
  1991. struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
  1992. struct nvdimm *nvdimm = mapping->nvdimm;
  1993. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1994. struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
  1995. if (map2->serial_number == dcr->serial_number &&
  1996. map2->vendor_id == dcr->vendor_id &&
  1997. map2->manufacturing_date == dcr->manufacturing_date &&
  1998. map2->manufacturing_location
  1999. == dcr->manufacturing_location) {
  2000. mapping->position = i;
  2001. break;
  2002. }
  2003. }
  2004. }
  2005. ndr_desc->nd_set = nd_set;
  2006. return 0;
  2007. }
  2008. static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
  2009. struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
  2010. {
  2011. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  2012. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2013. int cmd_rc, rc;
  2014. cmd->address = spa->address;
  2015. cmd->length = spa->length;
  2016. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
  2017. sizeof(*cmd), &cmd_rc);
  2018. if (rc < 0)
  2019. return rc;
  2020. return cmd_rc;
  2021. }
  2022. static int ars_start(struct acpi_nfit_desc *acpi_desc,
  2023. struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
  2024. {
  2025. int rc;
  2026. int cmd_rc;
  2027. struct nd_cmd_ars_start ars_start;
  2028. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2029. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  2030. memset(&ars_start, 0, sizeof(ars_start));
  2031. ars_start.address = spa->address;
  2032. ars_start.length = spa->length;
  2033. if (req_type == ARS_REQ_SHORT)
  2034. ars_start.flags = ND_ARS_RETURN_PREV_DATA;
  2035. if (nfit_spa_type(spa) == NFIT_SPA_PM)
  2036. ars_start.type = ND_ARS_PERSISTENT;
  2037. else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
  2038. ars_start.type = ND_ARS_VOLATILE;
  2039. else
  2040. return -ENOTTY;
  2041. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
  2042. sizeof(ars_start), &cmd_rc);
  2043. if (rc < 0)
  2044. return rc;
  2045. if (cmd_rc < 0)
  2046. return cmd_rc;
  2047. set_bit(ARS_VALID, &acpi_desc->scrub_flags);
  2048. return 0;
  2049. }
  2050. static int ars_continue(struct acpi_nfit_desc *acpi_desc)
  2051. {
  2052. int rc, cmd_rc;
  2053. struct nd_cmd_ars_start ars_start;
  2054. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  2055. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  2056. ars_start = (struct nd_cmd_ars_start) {
  2057. .address = ars_status->restart_address,
  2058. .length = ars_status->restart_length,
  2059. .type = ars_status->type,
  2060. };
  2061. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
  2062. sizeof(ars_start), &cmd_rc);
  2063. if (rc < 0)
  2064. return rc;
  2065. return cmd_rc;
  2066. }
  2067. static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
  2068. {
  2069. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  2070. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  2071. int rc, cmd_rc;
  2072. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
  2073. acpi_desc->max_ars, &cmd_rc);
  2074. if (rc < 0)
  2075. return rc;
  2076. return cmd_rc;
  2077. }
  2078. static void ars_complete(struct acpi_nfit_desc *acpi_desc,
  2079. struct nfit_spa *nfit_spa)
  2080. {
  2081. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  2082. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2083. struct nd_region *nd_region = nfit_spa->nd_region;
  2084. struct device *dev;
  2085. lockdep_assert_held(&acpi_desc->init_mutex);
  2086. /*
  2087. * Only advance the ARS state for ARS runs initiated by the
  2088. * kernel, ignore ARS results from BIOS initiated runs for scrub
  2089. * completion tracking.
  2090. */
  2091. if (acpi_desc->scrub_spa != nfit_spa)
  2092. return;
  2093. if ((ars_status->address >= spa->address && ars_status->address
  2094. < spa->address + spa->length)
  2095. || (ars_status->address < spa->address)) {
  2096. /*
  2097. * Assume that if a scrub starts at an offset from the
  2098. * start of nfit_spa that we are in the continuation
  2099. * case.
  2100. *
  2101. * Otherwise, if the scrub covers the spa range, mark
  2102. * any pending request complete.
  2103. */
  2104. if (ars_status->address + ars_status->length
  2105. >= spa->address + spa->length)
  2106. /* complete */;
  2107. else
  2108. return;
  2109. } else
  2110. return;
  2111. acpi_desc->scrub_spa = NULL;
  2112. if (nd_region) {
  2113. dev = nd_region_dev(nd_region);
  2114. nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
  2115. } else
  2116. dev = acpi_desc->dev;
  2117. dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
  2118. }
  2119. static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
  2120. {
  2121. struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
  2122. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  2123. int rc;
  2124. u32 i;
  2125. /*
  2126. * First record starts at 44 byte offset from the start of the
  2127. * payload.
  2128. */
  2129. if (ars_status->out_length < 44)
  2130. return 0;
  2131. /*
  2132. * Ignore potentially stale results that are only refreshed
  2133. * after a start-ARS event.
  2134. */
  2135. if (!test_and_clear_bit(ARS_VALID, &acpi_desc->scrub_flags)) {
  2136. dev_dbg(acpi_desc->dev, "skip %d stale records\n",
  2137. ars_status->num_records);
  2138. return 0;
  2139. }
  2140. for (i = 0; i < ars_status->num_records; i++) {
  2141. /* only process full records */
  2142. if (ars_status->out_length
  2143. < 44 + sizeof(struct nd_ars_record) * (i + 1))
  2144. break;
  2145. rc = nvdimm_bus_add_badrange(nvdimm_bus,
  2146. ars_status->records[i].err_address,
  2147. ars_status->records[i].length);
  2148. if (rc)
  2149. return rc;
  2150. }
  2151. if (i < ars_status->num_records)
  2152. dev_warn(acpi_desc->dev, "detected truncated ars results\n");
  2153. return 0;
  2154. }
  2155. static void acpi_nfit_remove_resource(void *data)
  2156. {
  2157. struct resource *res = data;
  2158. remove_resource(res);
  2159. }
  2160. static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
  2161. struct nd_region_desc *ndr_desc)
  2162. {
  2163. struct resource *res, *nd_res = ndr_desc->res;
  2164. int is_pmem, ret;
  2165. /* No operation if the region is already registered as PMEM */
  2166. is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
  2167. IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
  2168. if (is_pmem == REGION_INTERSECTS)
  2169. return 0;
  2170. res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
  2171. if (!res)
  2172. return -ENOMEM;
  2173. res->name = "Persistent Memory";
  2174. res->start = nd_res->start;
  2175. res->end = nd_res->end;
  2176. res->flags = IORESOURCE_MEM;
  2177. res->desc = IORES_DESC_PERSISTENT_MEMORY;
  2178. ret = insert_resource(&iomem_resource, res);
  2179. if (ret)
  2180. return ret;
  2181. ret = devm_add_action_or_reset(acpi_desc->dev,
  2182. acpi_nfit_remove_resource,
  2183. res);
  2184. if (ret)
  2185. return ret;
  2186. return 0;
  2187. }
  2188. static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
  2189. struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
  2190. struct acpi_nfit_memory_map *memdev,
  2191. struct nfit_spa *nfit_spa)
  2192. {
  2193. struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
  2194. memdev->device_handle);
  2195. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2196. if (!nvdimm) {
  2197. dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
  2198. spa->range_index, memdev->device_handle);
  2199. return -ENODEV;
  2200. }
  2201. mapping->nvdimm = nvdimm;
  2202. switch (nfit_spa_type(spa)) {
  2203. case NFIT_SPA_PM:
  2204. case NFIT_SPA_VOLATILE:
  2205. mapping->start = memdev->address;
  2206. mapping->size = memdev->region_size;
  2207. break;
  2208. }
  2209. return 0;
  2210. }
  2211. static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
  2212. {
  2213. return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
  2214. nfit_spa_type(spa) == NFIT_SPA_VCD ||
  2215. nfit_spa_type(spa) == NFIT_SPA_PDISK ||
  2216. nfit_spa_type(spa) == NFIT_SPA_PCD);
  2217. }
  2218. static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
  2219. {
  2220. return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
  2221. nfit_spa_type(spa) == NFIT_SPA_VCD ||
  2222. nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
  2223. }
  2224. static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
  2225. struct nfit_spa *nfit_spa)
  2226. {
  2227. static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
  2228. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2229. struct nd_region_desc *ndr_desc, _ndr_desc;
  2230. struct nfit_memdev *nfit_memdev;
  2231. struct nvdimm_bus *nvdimm_bus;
  2232. struct resource res;
  2233. int count = 0, rc;
  2234. if (nfit_spa->nd_region)
  2235. return 0;
  2236. if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
  2237. dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
  2238. return 0;
  2239. }
  2240. memset(&res, 0, sizeof(res));
  2241. memset(&mappings, 0, sizeof(mappings));
  2242. memset(&_ndr_desc, 0, sizeof(_ndr_desc));
  2243. res.start = spa->address;
  2244. res.end = res.start + spa->length - 1;
  2245. ndr_desc = &_ndr_desc;
  2246. ndr_desc->res = &res;
  2247. ndr_desc->provider_data = nfit_spa;
  2248. ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
  2249. if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) {
  2250. ndr_desc->numa_node = pxm_to_online_node(spa->proximity_domain);
  2251. ndr_desc->target_node = pxm_to_node(spa->proximity_domain);
  2252. } else {
  2253. ndr_desc->numa_node = NUMA_NO_NODE;
  2254. ndr_desc->target_node = NUMA_NO_NODE;
  2255. }
  2256. /* Fallback to address based numa information if node lookup failed */
  2257. if (ndr_desc->numa_node == NUMA_NO_NODE) {
  2258. ndr_desc->numa_node = memory_add_physaddr_to_nid(spa->address);
  2259. dev_info(acpi_desc->dev, "changing numa node from %d to %d for nfit region [%pa-%pa]",
  2260. NUMA_NO_NODE, ndr_desc->numa_node, &res.start, &res.end);
  2261. }
  2262. if (ndr_desc->target_node == NUMA_NO_NODE) {
  2263. ndr_desc->target_node = phys_to_target_node(spa->address);
  2264. dev_info(acpi_desc->dev, "changing target node from %d to %d for nfit region [%pa-%pa]",
  2265. NUMA_NO_NODE, ndr_desc->target_node, &res.start, &res.end);
  2266. }
  2267. /*
  2268. * Persistence domain bits are hierarchical, if
  2269. * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
  2270. * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
  2271. */
  2272. if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
  2273. set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
  2274. else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
  2275. set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
  2276. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  2277. struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
  2278. struct nd_mapping_desc *mapping;
  2279. /* range index 0 == unmapped in SPA or invalid-SPA */
  2280. if (memdev->range_index == 0 || spa->range_index == 0)
  2281. continue;
  2282. if (memdev->range_index != spa->range_index)
  2283. continue;
  2284. if (count >= ND_MAX_MAPPINGS) {
  2285. dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
  2286. spa->range_index, ND_MAX_MAPPINGS);
  2287. return -ENXIO;
  2288. }
  2289. mapping = &mappings[count++];
  2290. rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
  2291. memdev, nfit_spa);
  2292. if (rc)
  2293. goto out;
  2294. }
  2295. ndr_desc->mapping = mappings;
  2296. ndr_desc->num_mappings = count;
  2297. rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
  2298. if (rc)
  2299. goto out;
  2300. nvdimm_bus = acpi_desc->nvdimm_bus;
  2301. if (nfit_spa_type(spa) == NFIT_SPA_PM) {
  2302. rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
  2303. if (rc) {
  2304. dev_warn(acpi_desc->dev,
  2305. "failed to insert pmem resource to iomem: %d\n",
  2306. rc);
  2307. goto out;
  2308. }
  2309. nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
  2310. ndr_desc);
  2311. if (!nfit_spa->nd_region)
  2312. rc = -ENOMEM;
  2313. } else if (nfit_spa_is_volatile(spa)) {
  2314. nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
  2315. ndr_desc);
  2316. if (!nfit_spa->nd_region)
  2317. rc = -ENOMEM;
  2318. } else if (nfit_spa_is_virtual(spa)) {
  2319. nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
  2320. ndr_desc);
  2321. if (!nfit_spa->nd_region)
  2322. rc = -ENOMEM;
  2323. }
  2324. out:
  2325. if (rc)
  2326. dev_err(acpi_desc->dev, "failed to register spa range %d\n",
  2327. nfit_spa->spa->range_index);
  2328. return rc;
  2329. }
  2330. static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
  2331. {
  2332. struct device *dev = acpi_desc->dev;
  2333. struct nd_cmd_ars_status *ars_status;
  2334. if (acpi_desc->ars_status) {
  2335. memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
  2336. return 0;
  2337. }
  2338. ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
  2339. if (!ars_status)
  2340. return -ENOMEM;
  2341. acpi_desc->ars_status = ars_status;
  2342. return 0;
  2343. }
  2344. static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
  2345. {
  2346. int rc;
  2347. if (ars_status_alloc(acpi_desc))
  2348. return -ENOMEM;
  2349. rc = ars_get_status(acpi_desc);
  2350. if (rc < 0 && rc != -ENOSPC)
  2351. return rc;
  2352. if (ars_status_process_records(acpi_desc))
  2353. dev_err(acpi_desc->dev, "Failed to process ARS records\n");
  2354. return rc;
  2355. }
  2356. static int ars_register(struct acpi_nfit_desc *acpi_desc,
  2357. struct nfit_spa *nfit_spa)
  2358. {
  2359. int rc;
  2360. if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
  2361. return acpi_nfit_register_region(acpi_desc, nfit_spa);
  2362. set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
  2363. if (!no_init_ars)
  2364. set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
  2365. switch (acpi_nfit_query_poison(acpi_desc)) {
  2366. case 0:
  2367. case -ENOSPC:
  2368. case -EAGAIN:
  2369. rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
  2370. /* shouldn't happen, try again later */
  2371. if (rc == -EBUSY)
  2372. break;
  2373. if (rc) {
  2374. set_bit(ARS_FAILED, &nfit_spa->ars_state);
  2375. break;
  2376. }
  2377. clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
  2378. rc = acpi_nfit_query_poison(acpi_desc);
  2379. if (rc)
  2380. break;
  2381. acpi_desc->scrub_spa = nfit_spa;
  2382. ars_complete(acpi_desc, nfit_spa);
  2383. /*
  2384. * If ars_complete() says we didn't complete the
  2385. * short scrub, we'll try again with a long
  2386. * request.
  2387. */
  2388. acpi_desc->scrub_spa = NULL;
  2389. break;
  2390. case -EBUSY:
  2391. case -ENOMEM:
  2392. /*
  2393. * BIOS was using ARS, wait for it to complete (or
  2394. * resources to become available) and then perform our
  2395. * own scrubs.
  2396. */
  2397. break;
  2398. default:
  2399. set_bit(ARS_FAILED, &nfit_spa->ars_state);
  2400. break;
  2401. }
  2402. return acpi_nfit_register_region(acpi_desc, nfit_spa);
  2403. }
  2404. static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
  2405. {
  2406. struct nfit_spa *nfit_spa;
  2407. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2408. if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
  2409. continue;
  2410. ars_complete(acpi_desc, nfit_spa);
  2411. }
  2412. }
  2413. static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
  2414. int query_rc)
  2415. {
  2416. unsigned int tmo = acpi_desc->scrub_tmo;
  2417. struct device *dev = acpi_desc->dev;
  2418. struct nfit_spa *nfit_spa;
  2419. lockdep_assert_held(&acpi_desc->init_mutex);
  2420. if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags))
  2421. return 0;
  2422. if (query_rc == -EBUSY) {
  2423. dev_dbg(dev, "ARS: ARS busy\n");
  2424. return min(30U * 60U, tmo * 2);
  2425. }
  2426. if (query_rc == -ENOSPC) {
  2427. dev_dbg(dev, "ARS: ARS continue\n");
  2428. ars_continue(acpi_desc);
  2429. return 1;
  2430. }
  2431. if (query_rc && query_rc != -EAGAIN) {
  2432. unsigned long long addr, end;
  2433. addr = acpi_desc->ars_status->address;
  2434. end = addr + acpi_desc->ars_status->length;
  2435. dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
  2436. query_rc);
  2437. }
  2438. ars_complete_all(acpi_desc);
  2439. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2440. enum nfit_ars_state req_type;
  2441. int rc;
  2442. if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
  2443. continue;
  2444. /* prefer short ARS requests first */
  2445. if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
  2446. req_type = ARS_REQ_SHORT;
  2447. else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
  2448. req_type = ARS_REQ_LONG;
  2449. else
  2450. continue;
  2451. rc = ars_start(acpi_desc, nfit_spa, req_type);
  2452. dev = nd_region_dev(nfit_spa->nd_region);
  2453. dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
  2454. nfit_spa->spa->range_index,
  2455. req_type == ARS_REQ_SHORT ? "short" : "long",
  2456. rc);
  2457. /*
  2458. * Hmm, we raced someone else starting ARS? Try again in
  2459. * a bit.
  2460. */
  2461. if (rc == -EBUSY)
  2462. return 1;
  2463. if (rc == 0) {
  2464. dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
  2465. "scrub start while range %d active\n",
  2466. acpi_desc->scrub_spa->spa->range_index);
  2467. clear_bit(req_type, &nfit_spa->ars_state);
  2468. acpi_desc->scrub_spa = nfit_spa;
  2469. /*
  2470. * Consider this spa last for future scrub
  2471. * requests
  2472. */
  2473. list_move_tail(&nfit_spa->list, &acpi_desc->spas);
  2474. return 1;
  2475. }
  2476. dev_err(dev, "ARS: range %d ARS failed (%d)\n",
  2477. nfit_spa->spa->range_index, rc);
  2478. set_bit(ARS_FAILED, &nfit_spa->ars_state);
  2479. }
  2480. return 0;
  2481. }
  2482. static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
  2483. {
  2484. lockdep_assert_held(&acpi_desc->init_mutex);
  2485. set_bit(ARS_BUSY, &acpi_desc->scrub_flags);
  2486. /* note this should only be set from within the workqueue */
  2487. if (tmo)
  2488. acpi_desc->scrub_tmo = tmo;
  2489. queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
  2490. }
  2491. static void sched_ars(struct acpi_nfit_desc *acpi_desc)
  2492. {
  2493. __sched_ars(acpi_desc, 0);
  2494. }
  2495. static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
  2496. {
  2497. lockdep_assert_held(&acpi_desc->init_mutex);
  2498. clear_bit(ARS_BUSY, &acpi_desc->scrub_flags);
  2499. acpi_desc->scrub_count++;
  2500. if (acpi_desc->scrub_count_state)
  2501. sysfs_notify_dirent(acpi_desc->scrub_count_state);
  2502. }
  2503. static void acpi_nfit_scrub(struct work_struct *work)
  2504. {
  2505. struct acpi_nfit_desc *acpi_desc;
  2506. unsigned int tmo;
  2507. int query_rc;
  2508. acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
  2509. mutex_lock(&acpi_desc->init_mutex);
  2510. query_rc = acpi_nfit_query_poison(acpi_desc);
  2511. tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
  2512. if (tmo)
  2513. __sched_ars(acpi_desc, tmo);
  2514. else
  2515. notify_ars_done(acpi_desc);
  2516. memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
  2517. clear_bit(ARS_POLL, &acpi_desc->scrub_flags);
  2518. mutex_unlock(&acpi_desc->init_mutex);
  2519. }
  2520. static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
  2521. struct nfit_spa *nfit_spa)
  2522. {
  2523. int type = nfit_spa_type(nfit_spa->spa);
  2524. struct nd_cmd_ars_cap ars_cap;
  2525. int rc;
  2526. set_bit(ARS_FAILED, &nfit_spa->ars_state);
  2527. memset(&ars_cap, 0, sizeof(ars_cap));
  2528. rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
  2529. if (rc < 0)
  2530. return;
  2531. /* check that the supported scrub types match the spa type */
  2532. if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
  2533. & ND_ARS_VOLATILE) == 0)
  2534. return;
  2535. if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
  2536. & ND_ARS_PERSISTENT) == 0)
  2537. return;
  2538. nfit_spa->max_ars = ars_cap.max_ars_out;
  2539. nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
  2540. acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
  2541. clear_bit(ARS_FAILED, &nfit_spa->ars_state);
  2542. }
  2543. static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
  2544. {
  2545. struct nfit_spa *nfit_spa;
  2546. int rc, do_sched_ars = 0;
  2547. set_bit(ARS_VALID, &acpi_desc->scrub_flags);
  2548. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2549. switch (nfit_spa_type(nfit_spa->spa)) {
  2550. case NFIT_SPA_VOLATILE:
  2551. case NFIT_SPA_PM:
  2552. acpi_nfit_init_ars(acpi_desc, nfit_spa);
  2553. break;
  2554. }
  2555. }
  2556. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2557. switch (nfit_spa_type(nfit_spa->spa)) {
  2558. case NFIT_SPA_VOLATILE:
  2559. case NFIT_SPA_PM:
  2560. /* register regions and kick off initial ARS run */
  2561. rc = ars_register(acpi_desc, nfit_spa);
  2562. if (rc)
  2563. return rc;
  2564. /*
  2565. * Kick off background ARS if at least one
  2566. * region successfully registered ARS
  2567. */
  2568. if (!test_bit(ARS_FAILED, &nfit_spa->ars_state))
  2569. do_sched_ars++;
  2570. break;
  2571. case NFIT_SPA_BDW:
  2572. /* nothing to register */
  2573. break;
  2574. case NFIT_SPA_DCR:
  2575. case NFIT_SPA_VDISK:
  2576. case NFIT_SPA_VCD:
  2577. case NFIT_SPA_PDISK:
  2578. case NFIT_SPA_PCD:
  2579. /* register known regions that don't support ARS */
  2580. rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
  2581. if (rc)
  2582. return rc;
  2583. break;
  2584. default:
  2585. /* don't register unknown regions */
  2586. break;
  2587. }
  2588. }
  2589. if (do_sched_ars)
  2590. sched_ars(acpi_desc);
  2591. return 0;
  2592. }
  2593. static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
  2594. struct nfit_table_prev *prev)
  2595. {
  2596. struct device *dev = acpi_desc->dev;
  2597. if (!list_empty(&prev->spas) ||
  2598. !list_empty(&prev->memdevs) ||
  2599. !list_empty(&prev->dcrs) ||
  2600. !list_empty(&prev->bdws) ||
  2601. !list_empty(&prev->idts) ||
  2602. !list_empty(&prev->flushes)) {
  2603. dev_err(dev, "new nfit deletes entries (unsupported)\n");
  2604. return -ENXIO;
  2605. }
  2606. return 0;
  2607. }
  2608. static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
  2609. {
  2610. struct device *dev = acpi_desc->dev;
  2611. struct kernfs_node *nfit;
  2612. struct device *bus_dev;
  2613. if (!ars_supported(acpi_desc->nvdimm_bus))
  2614. return 0;
  2615. bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
  2616. nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
  2617. if (!nfit) {
  2618. dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
  2619. return -ENODEV;
  2620. }
  2621. acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
  2622. sysfs_put(nfit);
  2623. if (!acpi_desc->scrub_count_state) {
  2624. dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
  2625. return -ENODEV;
  2626. }
  2627. return 0;
  2628. }
  2629. static void acpi_nfit_unregister(void *data)
  2630. {
  2631. struct acpi_nfit_desc *acpi_desc = data;
  2632. nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
  2633. }
  2634. int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
  2635. {
  2636. struct device *dev = acpi_desc->dev;
  2637. struct nfit_table_prev prev;
  2638. const void *end;
  2639. int rc;
  2640. if (!acpi_desc->nvdimm_bus) {
  2641. acpi_nfit_init_dsms(acpi_desc);
  2642. acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
  2643. &acpi_desc->nd_desc);
  2644. if (!acpi_desc->nvdimm_bus)
  2645. return -ENOMEM;
  2646. rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
  2647. acpi_desc);
  2648. if (rc)
  2649. return rc;
  2650. rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
  2651. if (rc)
  2652. return rc;
  2653. /* register this acpi_desc for mce notifications */
  2654. mutex_lock(&acpi_desc_lock);
  2655. list_add_tail(&acpi_desc->list, &acpi_descs);
  2656. mutex_unlock(&acpi_desc_lock);
  2657. }
  2658. mutex_lock(&acpi_desc->init_mutex);
  2659. INIT_LIST_HEAD(&prev.spas);
  2660. INIT_LIST_HEAD(&prev.memdevs);
  2661. INIT_LIST_HEAD(&prev.dcrs);
  2662. INIT_LIST_HEAD(&prev.bdws);
  2663. INIT_LIST_HEAD(&prev.idts);
  2664. INIT_LIST_HEAD(&prev.flushes);
  2665. list_cut_position(&prev.spas, &acpi_desc->spas,
  2666. acpi_desc->spas.prev);
  2667. list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
  2668. acpi_desc->memdevs.prev);
  2669. list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
  2670. acpi_desc->dcrs.prev);
  2671. list_cut_position(&prev.bdws, &acpi_desc->bdws,
  2672. acpi_desc->bdws.prev);
  2673. list_cut_position(&prev.idts, &acpi_desc->idts,
  2674. acpi_desc->idts.prev);
  2675. list_cut_position(&prev.flushes, &acpi_desc->flushes,
  2676. acpi_desc->flushes.prev);
  2677. end = data + sz;
  2678. while (!IS_ERR_OR_NULL(data))
  2679. data = add_table(acpi_desc, &prev, data, end);
  2680. if (IS_ERR(data)) {
  2681. dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
  2682. rc = PTR_ERR(data);
  2683. goto out_unlock;
  2684. }
  2685. rc = acpi_nfit_check_deletions(acpi_desc, &prev);
  2686. if (rc)
  2687. goto out_unlock;
  2688. rc = nfit_mem_init(acpi_desc);
  2689. if (rc)
  2690. goto out_unlock;
  2691. rc = acpi_nfit_register_dimms(acpi_desc);
  2692. if (rc)
  2693. goto out_unlock;
  2694. rc = acpi_nfit_register_regions(acpi_desc);
  2695. out_unlock:
  2696. mutex_unlock(&acpi_desc->init_mutex);
  2697. return rc;
  2698. }
  2699. EXPORT_SYMBOL_GPL(acpi_nfit_init);
  2700. static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
  2701. {
  2702. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  2703. struct device *dev = acpi_desc->dev;
  2704. /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
  2705. device_lock(dev);
  2706. device_unlock(dev);
  2707. /* Bounce the init_mutex to complete initial registration */
  2708. mutex_lock(&acpi_desc->init_mutex);
  2709. mutex_unlock(&acpi_desc->init_mutex);
  2710. return 0;
  2711. }
  2712. static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
  2713. struct nvdimm *nvdimm, unsigned int cmd)
  2714. {
  2715. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  2716. if (nvdimm)
  2717. return 0;
  2718. if (cmd != ND_CMD_ARS_START)
  2719. return 0;
  2720. /*
  2721. * The kernel and userspace may race to initiate a scrub, but
  2722. * the scrub thread is prepared to lose that initial race. It
  2723. * just needs guarantees that any ARS it initiates are not
  2724. * interrupted by any intervening start requests from userspace.
  2725. */
  2726. if (work_busy(&acpi_desc->dwork.work))
  2727. return -EBUSY;
  2728. return 0;
  2729. }
  2730. /*
  2731. * Prevent security and firmware activate commands from being issued via
  2732. * ioctl.
  2733. */
  2734. static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
  2735. struct nvdimm *nvdimm, unsigned int cmd, void *buf)
  2736. {
  2737. struct nd_cmd_pkg *call_pkg = buf;
  2738. unsigned int func;
  2739. if (nvdimm && cmd == ND_CMD_CALL &&
  2740. call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
  2741. func = call_pkg->nd_command;
  2742. if (func > NVDIMM_CMD_MAX ||
  2743. (1 << func) & NVDIMM_INTEL_DENY_CMDMASK)
  2744. return -EOPNOTSUPP;
  2745. }
  2746. /* block all non-nfit bus commands */
  2747. if (!nvdimm && cmd == ND_CMD_CALL &&
  2748. call_pkg->nd_family != NVDIMM_BUS_FAMILY_NFIT)
  2749. return -EOPNOTSUPP;
  2750. return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
  2751. }
  2752. int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
  2753. enum nfit_ars_state req_type)
  2754. {
  2755. struct device *dev = acpi_desc->dev;
  2756. int scheduled = 0, busy = 0;
  2757. struct nfit_spa *nfit_spa;
  2758. mutex_lock(&acpi_desc->init_mutex);
  2759. if (test_bit(ARS_CANCEL, &acpi_desc->scrub_flags)) {
  2760. mutex_unlock(&acpi_desc->init_mutex);
  2761. return 0;
  2762. }
  2763. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2764. int type = nfit_spa_type(nfit_spa->spa);
  2765. if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
  2766. continue;
  2767. if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
  2768. continue;
  2769. if (test_and_set_bit(req_type, &nfit_spa->ars_state))
  2770. busy++;
  2771. else
  2772. scheduled++;
  2773. }
  2774. if (scheduled) {
  2775. sched_ars(acpi_desc);
  2776. dev_dbg(dev, "ars_scan triggered\n");
  2777. }
  2778. mutex_unlock(&acpi_desc->init_mutex);
  2779. if (scheduled)
  2780. return 0;
  2781. if (busy)
  2782. return -EBUSY;
  2783. return -ENOTTY;
  2784. }
  2785. void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
  2786. {
  2787. struct nvdimm_bus_descriptor *nd_desc;
  2788. dev_set_drvdata(dev, acpi_desc);
  2789. acpi_desc->dev = dev;
  2790. nd_desc = &acpi_desc->nd_desc;
  2791. nd_desc->provider_name = "ACPI.NFIT";
  2792. nd_desc->module = THIS_MODULE;
  2793. nd_desc->ndctl = acpi_nfit_ctl;
  2794. nd_desc->flush_probe = acpi_nfit_flush_probe;
  2795. nd_desc->clear_to_send = acpi_nfit_clear_to_send;
  2796. nd_desc->attr_groups = acpi_nfit_attribute_groups;
  2797. INIT_LIST_HEAD(&acpi_desc->spas);
  2798. INIT_LIST_HEAD(&acpi_desc->dcrs);
  2799. INIT_LIST_HEAD(&acpi_desc->bdws);
  2800. INIT_LIST_HEAD(&acpi_desc->idts);
  2801. INIT_LIST_HEAD(&acpi_desc->flushes);
  2802. INIT_LIST_HEAD(&acpi_desc->memdevs);
  2803. INIT_LIST_HEAD(&acpi_desc->dimms);
  2804. INIT_LIST_HEAD(&acpi_desc->list);
  2805. mutex_init(&acpi_desc->init_mutex);
  2806. acpi_desc->scrub_tmo = 1;
  2807. INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
  2808. }
  2809. EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
  2810. static void acpi_nfit_put_table(void *table)
  2811. {
  2812. acpi_put_table(table);
  2813. }
  2814. static void acpi_nfit_notify(acpi_handle handle, u32 event, void *data)
  2815. {
  2816. struct acpi_device *adev = data;
  2817. device_lock(&adev->dev);
  2818. __acpi_nfit_notify(&adev->dev, handle, event);
  2819. device_unlock(&adev->dev);
  2820. }
  2821. static void acpi_nfit_remove_notify_handler(void *data)
  2822. {
  2823. struct acpi_device *adev = data;
  2824. acpi_dev_remove_notify_handler(adev, ACPI_DEVICE_NOTIFY,
  2825. acpi_nfit_notify);
  2826. }
  2827. void acpi_nfit_shutdown(void *data)
  2828. {
  2829. struct acpi_nfit_desc *acpi_desc = data;
  2830. struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
  2831. /*
  2832. * Destruct under acpi_desc_lock so that nfit_handle_mce does not
  2833. * race teardown
  2834. */
  2835. mutex_lock(&acpi_desc_lock);
  2836. list_del(&acpi_desc->list);
  2837. mutex_unlock(&acpi_desc_lock);
  2838. mutex_lock(&acpi_desc->init_mutex);
  2839. set_bit(ARS_CANCEL, &acpi_desc->scrub_flags);
  2840. mutex_unlock(&acpi_desc->init_mutex);
  2841. cancel_delayed_work_sync(&acpi_desc->dwork);
  2842. /*
  2843. * Bounce the nvdimm bus lock to make sure any in-flight
  2844. * acpi_nfit_ars_rescan() submissions have had a chance to
  2845. * either submit or see ->cancel set.
  2846. */
  2847. device_lock(bus_dev);
  2848. device_unlock(bus_dev);
  2849. flush_workqueue(nfit_wq);
  2850. }
  2851. EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
  2852. static int acpi_nfit_add(struct acpi_device *adev)
  2853. {
  2854. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  2855. struct acpi_nfit_desc *acpi_desc;
  2856. struct device *dev = &adev->dev;
  2857. struct acpi_table_header *tbl;
  2858. acpi_status status = AE_OK;
  2859. acpi_size sz;
  2860. int rc = 0;
  2861. rc = acpi_dev_install_notify_handler(adev, ACPI_DEVICE_NOTIFY,
  2862. acpi_nfit_notify, adev);
  2863. if (rc)
  2864. return rc;
  2865. rc = devm_add_action_or_reset(dev, acpi_nfit_remove_notify_handler,
  2866. adev);
  2867. if (rc)
  2868. return rc;
  2869. status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
  2870. if (ACPI_FAILURE(status)) {
  2871. /* The NVDIMM root device allows OS to trigger enumeration of
  2872. * NVDIMMs through NFIT at boot time and re-enumeration at
  2873. * root level via the _FIT method during runtime.
  2874. * This is ok to return 0 here, we could have an nvdimm
  2875. * hotplugged later and evaluate _FIT method which returns
  2876. * data in the format of a series of NFIT Structures.
  2877. */
  2878. dev_dbg(dev, "failed to find NFIT at startup\n");
  2879. return 0;
  2880. }
  2881. rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
  2882. if (rc)
  2883. return rc;
  2884. sz = tbl->length;
  2885. acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
  2886. if (!acpi_desc)
  2887. return -ENOMEM;
  2888. acpi_nfit_desc_init(acpi_desc, &adev->dev);
  2889. /* Save the acpi header for exporting the revision via sysfs */
  2890. acpi_desc->acpi_header = *tbl;
  2891. /* Evaluate _FIT and override with that if present */
  2892. status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
  2893. if (ACPI_SUCCESS(status) && buf.length > 0) {
  2894. union acpi_object *obj = buf.pointer;
  2895. if (obj->type == ACPI_TYPE_BUFFER)
  2896. rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
  2897. obj->buffer.length);
  2898. else
  2899. dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
  2900. (int) obj->type);
  2901. kfree(buf.pointer);
  2902. } else
  2903. /* skip over the lead-in header table */
  2904. rc = acpi_nfit_init(acpi_desc, (void *) tbl
  2905. + sizeof(struct acpi_table_nfit),
  2906. sz - sizeof(struct acpi_table_nfit));
  2907. if (rc)
  2908. return rc;
  2909. return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
  2910. }
  2911. static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
  2912. {
  2913. struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
  2914. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  2915. union acpi_object *obj;
  2916. acpi_status status;
  2917. int ret;
  2918. if (!dev->driver) {
  2919. /* dev->driver may be null if we're being removed */
  2920. dev_dbg(dev, "no driver found for dev\n");
  2921. return;
  2922. }
  2923. if (!acpi_desc) {
  2924. acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
  2925. if (!acpi_desc)
  2926. return;
  2927. acpi_nfit_desc_init(acpi_desc, dev);
  2928. } else {
  2929. /*
  2930. * Finish previous registration before considering new
  2931. * regions.
  2932. */
  2933. flush_workqueue(nfit_wq);
  2934. }
  2935. /* Evaluate _FIT */
  2936. status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
  2937. if (ACPI_FAILURE(status)) {
  2938. dev_err(dev, "failed to evaluate _FIT\n");
  2939. return;
  2940. }
  2941. obj = buf.pointer;
  2942. if (obj->type == ACPI_TYPE_BUFFER) {
  2943. ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
  2944. obj->buffer.length);
  2945. if (ret)
  2946. dev_err(dev, "failed to merge updated NFIT\n");
  2947. } else
  2948. dev_err(dev, "Invalid _FIT\n");
  2949. kfree(buf.pointer);
  2950. }
  2951. static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
  2952. {
  2953. struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
  2954. if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
  2955. acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
  2956. else
  2957. acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
  2958. }
  2959. void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
  2960. {
  2961. dev_dbg(dev, "event: 0x%x\n", event);
  2962. switch (event) {
  2963. case NFIT_NOTIFY_UPDATE:
  2964. return acpi_nfit_update_notify(dev, handle);
  2965. case NFIT_NOTIFY_UC_MEMORY_ERROR:
  2966. return acpi_nfit_uc_error_notify(dev, handle);
  2967. default:
  2968. return;
  2969. }
  2970. }
  2971. EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
  2972. static const struct acpi_device_id acpi_nfit_ids[] = {
  2973. { "ACPI0012", 0 },
  2974. { "", 0 },
  2975. };
  2976. MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
  2977. static struct acpi_driver acpi_nfit_driver = {
  2978. .name = KBUILD_MODNAME,
  2979. .ids = acpi_nfit_ids,
  2980. .ops = {
  2981. .add = acpi_nfit_add,
  2982. },
  2983. };
  2984. static __init int nfit_init(void)
  2985. {
  2986. int ret;
  2987. BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
  2988. BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 64);
  2989. BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
  2990. BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 16);
  2991. BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 8);
  2992. BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
  2993. BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
  2994. BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
  2995. guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
  2996. guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
  2997. guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
  2998. guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
  2999. guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
  3000. guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
  3001. guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
  3002. guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
  3003. guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
  3004. guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
  3005. guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
  3006. guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
  3007. guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
  3008. guid_parse(UUID_NFIT_DIMM_N_HYPERV, &nfit_uuid[NFIT_DEV_DIMM_N_HYPERV]);
  3009. guid_parse(UUID_INTEL_BUS, &nfit_uuid[NFIT_BUS_INTEL]);
  3010. nfit_wq = create_singlethread_workqueue("nfit");
  3011. if (!nfit_wq)
  3012. return -ENOMEM;
  3013. nfit_mce_register();
  3014. ret = acpi_bus_register_driver(&acpi_nfit_driver);
  3015. if (ret) {
  3016. nfit_mce_unregister();
  3017. destroy_workqueue(nfit_wq);
  3018. }
  3019. return ret;
  3020. }
  3021. static __exit void nfit_exit(void)
  3022. {
  3023. nfit_mce_unregister();
  3024. acpi_bus_unregister_driver(&acpi_nfit_driver);
  3025. destroy_workqueue(nfit_wq);
  3026. WARN_ON(!list_empty(&acpi_descs));
  3027. }
  3028. module_init(nfit_init);
  3029. module_exit(nfit_exit);
  3030. MODULE_DESCRIPTION("ACPI NVDIMM Firmware Interface Table (NFIT) driver");
  3031. MODULE_LICENSE("GPL v2");
  3032. MODULE_AUTHOR("Intel Corporation");