core.c 80 KB

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