core.c 81 KB

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