core.c 82 KB

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