core.c 94 KB

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