core.c 93 KB

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