core.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969
  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. rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
  840. (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
  841. }
  842. device_unlock(dev);
  843. return rc;
  844. }
  845. static ssize_t scrub_store(struct device *dev,
  846. struct device_attribute *attr, const char *buf, size_t size)
  847. {
  848. struct nvdimm_bus_descriptor *nd_desc;
  849. ssize_t rc;
  850. long val;
  851. rc = kstrtol(buf, 0, &val);
  852. if (rc)
  853. return rc;
  854. if (val != 1)
  855. return -EINVAL;
  856. device_lock(dev);
  857. nd_desc = dev_get_drvdata(dev);
  858. if (nd_desc) {
  859. struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
  860. rc = acpi_nfit_ars_rescan(acpi_desc);
  861. }
  862. device_unlock(dev);
  863. if (rc)
  864. return rc;
  865. return size;
  866. }
  867. static DEVICE_ATTR_RW(scrub);
  868. static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
  869. {
  870. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  871. const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
  872. | 1 << ND_CMD_ARS_STATUS;
  873. return (nd_desc->cmd_mask & mask) == mask;
  874. }
  875. static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
  876. {
  877. struct device *dev = container_of(kobj, struct device, kobj);
  878. struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
  879. if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
  880. return 0;
  881. return a->mode;
  882. }
  883. static struct attribute *acpi_nfit_attributes[] = {
  884. &dev_attr_revision.attr,
  885. &dev_attr_scrub.attr,
  886. &dev_attr_hw_error_scrub.attr,
  887. NULL,
  888. };
  889. static struct attribute_group acpi_nfit_attribute_group = {
  890. .name = "nfit",
  891. .attrs = acpi_nfit_attributes,
  892. .is_visible = nfit_visible,
  893. };
  894. static const struct attribute_group *acpi_nfit_attribute_groups[] = {
  895. &nvdimm_bus_attribute_group,
  896. &acpi_nfit_attribute_group,
  897. NULL,
  898. };
  899. static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
  900. {
  901. struct nvdimm *nvdimm = to_nvdimm(dev);
  902. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  903. return __to_nfit_memdev(nfit_mem);
  904. }
  905. static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
  906. {
  907. struct nvdimm *nvdimm = to_nvdimm(dev);
  908. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  909. return nfit_mem->dcr;
  910. }
  911. static ssize_t handle_show(struct device *dev,
  912. struct device_attribute *attr, char *buf)
  913. {
  914. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  915. return sprintf(buf, "%#x\n", memdev->device_handle);
  916. }
  917. static DEVICE_ATTR_RO(handle);
  918. static ssize_t phys_id_show(struct device *dev,
  919. struct device_attribute *attr, char *buf)
  920. {
  921. struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
  922. return sprintf(buf, "%#x\n", memdev->physical_id);
  923. }
  924. static DEVICE_ATTR_RO(phys_id);
  925. static ssize_t vendor_show(struct device *dev,
  926. struct device_attribute *attr, char *buf)
  927. {
  928. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  929. return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
  930. }
  931. static DEVICE_ATTR_RO(vendor);
  932. static ssize_t rev_id_show(struct device *dev,
  933. struct device_attribute *attr, char *buf)
  934. {
  935. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  936. return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
  937. }
  938. static DEVICE_ATTR_RO(rev_id);
  939. static ssize_t device_show(struct device *dev,
  940. struct device_attribute *attr, char *buf)
  941. {
  942. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  943. return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
  944. }
  945. static DEVICE_ATTR_RO(device);
  946. static ssize_t subsystem_vendor_show(struct device *dev,
  947. struct device_attribute *attr, char *buf)
  948. {
  949. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  950. return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
  951. }
  952. static DEVICE_ATTR_RO(subsystem_vendor);
  953. static ssize_t subsystem_rev_id_show(struct device *dev,
  954. struct device_attribute *attr, char *buf)
  955. {
  956. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  957. return sprintf(buf, "0x%04x\n",
  958. be16_to_cpu(dcr->subsystem_revision_id));
  959. }
  960. static DEVICE_ATTR_RO(subsystem_rev_id);
  961. static ssize_t subsystem_device_show(struct device *dev,
  962. struct device_attribute *attr, char *buf)
  963. {
  964. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  965. return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
  966. }
  967. static DEVICE_ATTR_RO(subsystem_device);
  968. static int num_nvdimm_formats(struct nvdimm *nvdimm)
  969. {
  970. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  971. int formats = 0;
  972. if (nfit_mem->memdev_pmem)
  973. formats++;
  974. if (nfit_mem->memdev_bdw)
  975. formats++;
  976. return formats;
  977. }
  978. static ssize_t format_show(struct device *dev,
  979. struct device_attribute *attr, char *buf)
  980. {
  981. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  982. return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
  983. }
  984. static DEVICE_ATTR_RO(format);
  985. static ssize_t format1_show(struct device *dev,
  986. struct device_attribute *attr, char *buf)
  987. {
  988. u32 handle;
  989. ssize_t rc = -ENXIO;
  990. struct nfit_mem *nfit_mem;
  991. struct nfit_memdev *nfit_memdev;
  992. struct acpi_nfit_desc *acpi_desc;
  993. struct nvdimm *nvdimm = to_nvdimm(dev);
  994. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  995. nfit_mem = nvdimm_provider_data(nvdimm);
  996. acpi_desc = nfit_mem->acpi_desc;
  997. handle = to_nfit_memdev(dev)->device_handle;
  998. /* assumes DIMMs have at most 2 published interface codes */
  999. mutex_lock(&acpi_desc->init_mutex);
  1000. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  1001. struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
  1002. struct nfit_dcr *nfit_dcr;
  1003. if (memdev->device_handle != handle)
  1004. continue;
  1005. list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
  1006. if (nfit_dcr->dcr->region_index != memdev->region_index)
  1007. continue;
  1008. if (nfit_dcr->dcr->code == dcr->code)
  1009. continue;
  1010. rc = sprintf(buf, "0x%04x\n",
  1011. le16_to_cpu(nfit_dcr->dcr->code));
  1012. break;
  1013. }
  1014. if (rc != ENXIO)
  1015. break;
  1016. }
  1017. mutex_unlock(&acpi_desc->init_mutex);
  1018. return rc;
  1019. }
  1020. static DEVICE_ATTR_RO(format1);
  1021. static ssize_t formats_show(struct device *dev,
  1022. struct device_attribute *attr, char *buf)
  1023. {
  1024. struct nvdimm *nvdimm = to_nvdimm(dev);
  1025. return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
  1026. }
  1027. static DEVICE_ATTR_RO(formats);
  1028. static ssize_t serial_show(struct device *dev,
  1029. struct device_attribute *attr, char *buf)
  1030. {
  1031. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1032. return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
  1033. }
  1034. static DEVICE_ATTR_RO(serial);
  1035. static ssize_t family_show(struct device *dev,
  1036. struct device_attribute *attr, char *buf)
  1037. {
  1038. struct nvdimm *nvdimm = to_nvdimm(dev);
  1039. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1040. if (nfit_mem->family < 0)
  1041. return -ENXIO;
  1042. return sprintf(buf, "%d\n", nfit_mem->family);
  1043. }
  1044. static DEVICE_ATTR_RO(family);
  1045. static ssize_t dsm_mask_show(struct device *dev,
  1046. struct device_attribute *attr, char *buf)
  1047. {
  1048. struct nvdimm *nvdimm = to_nvdimm(dev);
  1049. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1050. if (nfit_mem->family < 0)
  1051. return -ENXIO;
  1052. return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
  1053. }
  1054. static DEVICE_ATTR_RO(dsm_mask);
  1055. static ssize_t flags_show(struct device *dev,
  1056. struct device_attribute *attr, char *buf)
  1057. {
  1058. u16 flags = to_nfit_memdev(dev)->flags;
  1059. return sprintf(buf, "%s%s%s%s%s\n",
  1060. flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
  1061. flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
  1062. flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
  1063. flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
  1064. flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
  1065. }
  1066. static DEVICE_ATTR_RO(flags);
  1067. static ssize_t id_show(struct device *dev,
  1068. struct device_attribute *attr, char *buf)
  1069. {
  1070. struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
  1071. if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
  1072. return sprintf(buf, "%04x-%02x-%04x-%08x\n",
  1073. be16_to_cpu(dcr->vendor_id),
  1074. dcr->manufacturing_location,
  1075. be16_to_cpu(dcr->manufacturing_date),
  1076. be32_to_cpu(dcr->serial_number));
  1077. else
  1078. return sprintf(buf, "%04x-%08x\n",
  1079. be16_to_cpu(dcr->vendor_id),
  1080. be32_to_cpu(dcr->serial_number));
  1081. }
  1082. static DEVICE_ATTR_RO(id);
  1083. static struct attribute *acpi_nfit_dimm_attributes[] = {
  1084. &dev_attr_handle.attr,
  1085. &dev_attr_phys_id.attr,
  1086. &dev_attr_vendor.attr,
  1087. &dev_attr_device.attr,
  1088. &dev_attr_rev_id.attr,
  1089. &dev_attr_subsystem_vendor.attr,
  1090. &dev_attr_subsystem_device.attr,
  1091. &dev_attr_subsystem_rev_id.attr,
  1092. &dev_attr_format.attr,
  1093. &dev_attr_formats.attr,
  1094. &dev_attr_format1.attr,
  1095. &dev_attr_serial.attr,
  1096. &dev_attr_flags.attr,
  1097. &dev_attr_id.attr,
  1098. &dev_attr_family.attr,
  1099. &dev_attr_dsm_mask.attr,
  1100. NULL,
  1101. };
  1102. static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
  1103. struct attribute *a, int n)
  1104. {
  1105. struct device *dev = container_of(kobj, struct device, kobj);
  1106. struct nvdimm *nvdimm = to_nvdimm(dev);
  1107. if (!to_nfit_dcr(dev))
  1108. return 0;
  1109. if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
  1110. return 0;
  1111. return a->mode;
  1112. }
  1113. static struct attribute_group acpi_nfit_dimm_attribute_group = {
  1114. .name = "nfit",
  1115. .attrs = acpi_nfit_dimm_attributes,
  1116. .is_visible = acpi_nfit_dimm_attr_visible,
  1117. };
  1118. static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
  1119. &nvdimm_attribute_group,
  1120. &nd_device_attribute_group,
  1121. &acpi_nfit_dimm_attribute_group,
  1122. NULL,
  1123. };
  1124. static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
  1125. u32 device_handle)
  1126. {
  1127. struct nfit_mem *nfit_mem;
  1128. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
  1129. if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
  1130. return nfit_mem->nvdimm;
  1131. return NULL;
  1132. }
  1133. void __acpi_nvdimm_notify(struct device *dev, u32 event)
  1134. {
  1135. struct nfit_mem *nfit_mem;
  1136. struct acpi_nfit_desc *acpi_desc;
  1137. dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
  1138. event);
  1139. if (event != NFIT_NOTIFY_DIMM_HEALTH) {
  1140. dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
  1141. event);
  1142. return;
  1143. }
  1144. acpi_desc = dev_get_drvdata(dev->parent);
  1145. if (!acpi_desc)
  1146. return;
  1147. /*
  1148. * If we successfully retrieved acpi_desc, then we know nfit_mem data
  1149. * is still valid.
  1150. */
  1151. nfit_mem = dev_get_drvdata(dev);
  1152. if (nfit_mem && nfit_mem->flags_attr)
  1153. sysfs_notify_dirent(nfit_mem->flags_attr);
  1154. }
  1155. EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
  1156. static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
  1157. {
  1158. struct acpi_device *adev = data;
  1159. struct device *dev = &adev->dev;
  1160. device_lock(dev->parent);
  1161. __acpi_nvdimm_notify(dev, event);
  1162. device_unlock(dev->parent);
  1163. }
  1164. static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
  1165. struct nfit_mem *nfit_mem, u32 device_handle)
  1166. {
  1167. struct acpi_device *adev, *adev_dimm;
  1168. struct device *dev = acpi_desc->dev;
  1169. unsigned long dsm_mask;
  1170. const u8 *uuid;
  1171. int i;
  1172. /* nfit test assumes 1:1 relationship between commands and dsms */
  1173. nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
  1174. nfit_mem->family = NVDIMM_FAMILY_INTEL;
  1175. adev = to_acpi_dev(acpi_desc);
  1176. if (!adev)
  1177. return 0;
  1178. adev_dimm = acpi_find_child_device(adev, device_handle, false);
  1179. nfit_mem->adev = adev_dimm;
  1180. if (!adev_dimm) {
  1181. dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
  1182. device_handle);
  1183. return force_enable_dimms ? 0 : -ENODEV;
  1184. }
  1185. if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
  1186. ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
  1187. dev_err(dev, "%s: notification registration failed\n",
  1188. dev_name(&adev_dimm->dev));
  1189. return -ENXIO;
  1190. }
  1191. /*
  1192. * Until standardization materializes we need to consider 4
  1193. * different command sets. Note, that checking for function0 (bit0)
  1194. * tells us if any commands are reachable through this uuid.
  1195. */
  1196. for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
  1197. if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
  1198. break;
  1199. /* limit the supported commands to those that are publicly documented */
  1200. nfit_mem->family = i;
  1201. if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
  1202. dsm_mask = 0x3fe;
  1203. if (disable_vendor_specific)
  1204. dsm_mask &= ~(1 << ND_CMD_VENDOR);
  1205. } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
  1206. dsm_mask = 0x1c3c76;
  1207. } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
  1208. dsm_mask = 0x1fe;
  1209. if (disable_vendor_specific)
  1210. dsm_mask &= ~(1 << 8);
  1211. } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
  1212. dsm_mask = 0xffffffff;
  1213. } else {
  1214. dev_dbg(dev, "unknown dimm command family\n");
  1215. nfit_mem->family = -1;
  1216. /* DSMs are optional, continue loading the driver... */
  1217. return 0;
  1218. }
  1219. uuid = to_nfit_uuid(nfit_mem->family);
  1220. for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
  1221. if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
  1222. set_bit(i, &nfit_mem->dsm_mask);
  1223. return 0;
  1224. }
  1225. static void shutdown_dimm_notify(void *data)
  1226. {
  1227. struct acpi_nfit_desc *acpi_desc = data;
  1228. struct nfit_mem *nfit_mem;
  1229. mutex_lock(&acpi_desc->init_mutex);
  1230. /*
  1231. * Clear out the nfit_mem->flags_attr and shut down dimm event
  1232. * notifications.
  1233. */
  1234. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1235. struct acpi_device *adev_dimm = nfit_mem->adev;
  1236. if (nfit_mem->flags_attr) {
  1237. sysfs_put(nfit_mem->flags_attr);
  1238. nfit_mem->flags_attr = NULL;
  1239. }
  1240. if (adev_dimm)
  1241. acpi_remove_notify_handler(adev_dimm->handle,
  1242. ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
  1243. }
  1244. mutex_unlock(&acpi_desc->init_mutex);
  1245. }
  1246. static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
  1247. {
  1248. struct nfit_mem *nfit_mem;
  1249. int dimm_count = 0, rc;
  1250. struct nvdimm *nvdimm;
  1251. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1252. struct acpi_nfit_flush_address *flush;
  1253. unsigned long flags = 0, cmd_mask;
  1254. u32 device_handle;
  1255. u16 mem_flags;
  1256. device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
  1257. nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
  1258. if (nvdimm) {
  1259. dimm_count++;
  1260. continue;
  1261. }
  1262. if (nfit_mem->bdw && nfit_mem->memdev_pmem)
  1263. flags |= NDD_ALIASING;
  1264. mem_flags = __to_nfit_memdev(nfit_mem)->flags;
  1265. if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
  1266. flags |= NDD_UNARMED;
  1267. rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
  1268. if (rc)
  1269. continue;
  1270. /*
  1271. * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
  1272. * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
  1273. * userspace interface.
  1274. */
  1275. cmd_mask = 1UL << ND_CMD_CALL;
  1276. if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
  1277. cmd_mask |= nfit_mem->dsm_mask;
  1278. flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
  1279. : NULL;
  1280. nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
  1281. acpi_nfit_dimm_attribute_groups,
  1282. flags, cmd_mask, flush ? flush->hint_count : 0,
  1283. nfit_mem->flush_wpq);
  1284. if (!nvdimm)
  1285. return -ENOMEM;
  1286. nfit_mem->nvdimm = nvdimm;
  1287. dimm_count++;
  1288. if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
  1289. continue;
  1290. dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
  1291. nvdimm_name(nvdimm),
  1292. mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
  1293. mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
  1294. mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
  1295. mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
  1296. }
  1297. rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
  1298. if (rc)
  1299. return rc;
  1300. /*
  1301. * Now that dimms are successfully registered, and async registration
  1302. * is flushed, attempt to enable event notification.
  1303. */
  1304. list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
  1305. struct kernfs_node *nfit_kernfs;
  1306. nvdimm = nfit_mem->nvdimm;
  1307. nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
  1308. if (nfit_kernfs)
  1309. nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
  1310. "flags");
  1311. sysfs_put(nfit_kernfs);
  1312. if (!nfit_mem->flags_attr)
  1313. dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
  1314. nvdimm_name(nvdimm));
  1315. }
  1316. return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
  1317. acpi_desc);
  1318. }
  1319. static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
  1320. {
  1321. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1322. const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
  1323. struct acpi_device *adev;
  1324. int i;
  1325. nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
  1326. adev = to_acpi_dev(acpi_desc);
  1327. if (!adev)
  1328. return;
  1329. for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
  1330. if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
  1331. set_bit(i, &nd_desc->cmd_mask);
  1332. }
  1333. static ssize_t range_index_show(struct device *dev,
  1334. struct device_attribute *attr, char *buf)
  1335. {
  1336. struct nd_region *nd_region = to_nd_region(dev);
  1337. struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
  1338. return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
  1339. }
  1340. static DEVICE_ATTR_RO(range_index);
  1341. static struct attribute *acpi_nfit_region_attributes[] = {
  1342. &dev_attr_range_index.attr,
  1343. NULL,
  1344. };
  1345. static struct attribute_group acpi_nfit_region_attribute_group = {
  1346. .name = "nfit",
  1347. .attrs = acpi_nfit_region_attributes,
  1348. };
  1349. static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
  1350. &nd_region_attribute_group,
  1351. &nd_mapping_attribute_group,
  1352. &nd_device_attribute_group,
  1353. &nd_numa_attribute_group,
  1354. &acpi_nfit_region_attribute_group,
  1355. NULL,
  1356. };
  1357. /* enough info to uniquely specify an interleave set */
  1358. struct nfit_set_info {
  1359. struct nfit_set_info_map {
  1360. u64 region_offset;
  1361. u32 serial_number;
  1362. u32 pad;
  1363. } mapping[0];
  1364. };
  1365. static size_t sizeof_nfit_set_info(int num_mappings)
  1366. {
  1367. return sizeof(struct nfit_set_info)
  1368. + num_mappings * sizeof(struct nfit_set_info_map);
  1369. }
  1370. static int cmp_map(const void *m0, const void *m1)
  1371. {
  1372. const struct nfit_set_info_map *map0 = m0;
  1373. const struct nfit_set_info_map *map1 = m1;
  1374. return memcmp(&map0->region_offset, &map1->region_offset,
  1375. sizeof(u64));
  1376. }
  1377. /* Retrieve the nth entry referencing this spa */
  1378. static struct acpi_nfit_memory_map *memdev_from_spa(
  1379. struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
  1380. {
  1381. struct nfit_memdev *nfit_memdev;
  1382. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
  1383. if (nfit_memdev->memdev->range_index == range_index)
  1384. if (n-- == 0)
  1385. return nfit_memdev->memdev;
  1386. return NULL;
  1387. }
  1388. static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
  1389. struct nd_region_desc *ndr_desc,
  1390. struct acpi_nfit_system_address *spa)
  1391. {
  1392. int i, spa_type = nfit_spa_type(spa);
  1393. struct device *dev = acpi_desc->dev;
  1394. struct nd_interleave_set *nd_set;
  1395. u16 nr = ndr_desc->num_mappings;
  1396. struct nfit_set_info *info;
  1397. if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
  1398. /* pass */;
  1399. else
  1400. return 0;
  1401. nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
  1402. if (!nd_set)
  1403. return -ENOMEM;
  1404. info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
  1405. if (!info)
  1406. return -ENOMEM;
  1407. for (i = 0; i < nr; i++) {
  1408. struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
  1409. struct nfit_set_info_map *map = &info->mapping[i];
  1410. struct nvdimm *nvdimm = mapping->nvdimm;
  1411. struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
  1412. struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
  1413. spa->range_index, i);
  1414. if (!memdev || !nfit_mem->dcr) {
  1415. dev_err(dev, "%s: failed to find DCR\n", __func__);
  1416. return -ENODEV;
  1417. }
  1418. map->region_offset = memdev->region_offset;
  1419. map->serial_number = nfit_mem->dcr->serial_number;
  1420. }
  1421. sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
  1422. cmp_map, NULL);
  1423. nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
  1424. ndr_desc->nd_set = nd_set;
  1425. devm_kfree(dev, info);
  1426. return 0;
  1427. }
  1428. static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
  1429. {
  1430. struct acpi_nfit_interleave *idt = mmio->idt;
  1431. u32 sub_line_offset, line_index, line_offset;
  1432. u64 line_no, table_skip_count, table_offset;
  1433. line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
  1434. table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
  1435. line_offset = idt->line_offset[line_index]
  1436. * mmio->line_size;
  1437. table_offset = table_skip_count * mmio->table_size;
  1438. return mmio->base_offset + line_offset + table_offset + sub_line_offset;
  1439. }
  1440. static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
  1441. {
  1442. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
  1443. u64 offset = nfit_blk->stat_offset + mmio->size * bw;
  1444. const u32 STATUS_MASK = 0x80000037;
  1445. if (mmio->num_lines)
  1446. offset = to_interleave_offset(offset, mmio);
  1447. return readl(mmio->addr.base + offset) & STATUS_MASK;
  1448. }
  1449. static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
  1450. resource_size_t dpa, unsigned int len, unsigned int write)
  1451. {
  1452. u64 cmd, offset;
  1453. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
  1454. enum {
  1455. BCW_OFFSET_MASK = (1ULL << 48)-1,
  1456. BCW_LEN_SHIFT = 48,
  1457. BCW_LEN_MASK = (1ULL << 8) - 1,
  1458. BCW_CMD_SHIFT = 56,
  1459. };
  1460. cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
  1461. len = len >> L1_CACHE_SHIFT;
  1462. cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
  1463. cmd |= ((u64) write) << BCW_CMD_SHIFT;
  1464. offset = nfit_blk->cmd_offset + mmio->size * bw;
  1465. if (mmio->num_lines)
  1466. offset = to_interleave_offset(offset, mmio);
  1467. writeq(cmd, mmio->addr.base + offset);
  1468. nvdimm_flush(nfit_blk->nd_region);
  1469. if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
  1470. readq(mmio->addr.base + offset);
  1471. }
  1472. static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
  1473. resource_size_t dpa, void *iobuf, size_t len, int rw,
  1474. unsigned int lane)
  1475. {
  1476. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
  1477. unsigned int copied = 0;
  1478. u64 base_offset;
  1479. int rc;
  1480. base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
  1481. + lane * mmio->size;
  1482. write_blk_ctl(nfit_blk, lane, dpa, len, rw);
  1483. while (len) {
  1484. unsigned int c;
  1485. u64 offset;
  1486. if (mmio->num_lines) {
  1487. u32 line_offset;
  1488. offset = to_interleave_offset(base_offset + copied,
  1489. mmio);
  1490. div_u64_rem(offset, mmio->line_size, &line_offset);
  1491. c = min_t(size_t, len, mmio->line_size - line_offset);
  1492. } else {
  1493. offset = base_offset + nfit_blk->bdw_offset;
  1494. c = len;
  1495. }
  1496. if (rw)
  1497. memcpy_to_pmem(mmio->addr.aperture + offset,
  1498. iobuf + copied, c);
  1499. else {
  1500. if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
  1501. mmio_flush_range((void __force *)
  1502. mmio->addr.aperture + offset, c);
  1503. memcpy_from_pmem(iobuf + copied,
  1504. mmio->addr.aperture + offset, c);
  1505. }
  1506. copied += c;
  1507. len -= c;
  1508. }
  1509. if (rw)
  1510. nvdimm_flush(nfit_blk->nd_region);
  1511. rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
  1512. return rc;
  1513. }
  1514. static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
  1515. resource_size_t dpa, void *iobuf, u64 len, int rw)
  1516. {
  1517. struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
  1518. struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
  1519. struct nd_region *nd_region = nfit_blk->nd_region;
  1520. unsigned int lane, copied = 0;
  1521. int rc = 0;
  1522. lane = nd_region_acquire_lane(nd_region);
  1523. while (len) {
  1524. u64 c = min(len, mmio->size);
  1525. rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
  1526. iobuf + copied, c, rw, lane);
  1527. if (rc)
  1528. break;
  1529. copied += c;
  1530. len -= c;
  1531. }
  1532. nd_region_release_lane(nd_region, lane);
  1533. return rc;
  1534. }
  1535. static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
  1536. struct acpi_nfit_interleave *idt, u16 interleave_ways)
  1537. {
  1538. if (idt) {
  1539. mmio->num_lines = idt->line_count;
  1540. mmio->line_size = idt->line_size;
  1541. if (interleave_ways == 0)
  1542. return -ENXIO;
  1543. mmio->table_size = mmio->num_lines * interleave_ways
  1544. * mmio->line_size;
  1545. }
  1546. return 0;
  1547. }
  1548. static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
  1549. struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
  1550. {
  1551. struct nd_cmd_dimm_flags flags;
  1552. int rc;
  1553. memset(&flags, 0, sizeof(flags));
  1554. rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
  1555. sizeof(flags), NULL);
  1556. if (rc >= 0 && flags.status == 0)
  1557. nfit_blk->dimm_flags = flags.flags;
  1558. else if (rc == -ENOTTY) {
  1559. /* fall back to a conservative default */
  1560. nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
  1561. rc = 0;
  1562. } else
  1563. rc = -ENXIO;
  1564. return rc;
  1565. }
  1566. static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
  1567. struct device *dev)
  1568. {
  1569. struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
  1570. struct nd_blk_region *ndbr = to_nd_blk_region(dev);
  1571. struct nfit_blk_mmio *mmio;
  1572. struct nfit_blk *nfit_blk;
  1573. struct nfit_mem *nfit_mem;
  1574. struct nvdimm *nvdimm;
  1575. int rc;
  1576. nvdimm = nd_blk_region_to_dimm(ndbr);
  1577. nfit_mem = nvdimm_provider_data(nvdimm);
  1578. if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
  1579. dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
  1580. nfit_mem ? "" : " nfit_mem",
  1581. (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
  1582. (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
  1583. return -ENXIO;
  1584. }
  1585. nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
  1586. if (!nfit_blk)
  1587. return -ENOMEM;
  1588. nd_blk_region_set_provider_data(ndbr, nfit_blk);
  1589. nfit_blk->nd_region = to_nd_region(dev);
  1590. /* map block aperture memory */
  1591. nfit_blk->bdw_offset = nfit_mem->bdw->offset;
  1592. mmio = &nfit_blk->mmio[BDW];
  1593. mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
  1594. nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
  1595. if (!mmio->addr.base) {
  1596. dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
  1597. nvdimm_name(nvdimm));
  1598. return -ENOMEM;
  1599. }
  1600. mmio->size = nfit_mem->bdw->size;
  1601. mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
  1602. mmio->idt = nfit_mem->idt_bdw;
  1603. mmio->spa = nfit_mem->spa_bdw;
  1604. rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
  1605. nfit_mem->memdev_bdw->interleave_ways);
  1606. if (rc) {
  1607. dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
  1608. __func__, nvdimm_name(nvdimm));
  1609. return rc;
  1610. }
  1611. /* map block control memory */
  1612. nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
  1613. nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
  1614. mmio = &nfit_blk->mmio[DCR];
  1615. mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
  1616. nfit_mem->spa_dcr->length);
  1617. if (!mmio->addr.base) {
  1618. dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
  1619. nvdimm_name(nvdimm));
  1620. return -ENOMEM;
  1621. }
  1622. mmio->size = nfit_mem->dcr->window_size;
  1623. mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
  1624. mmio->idt = nfit_mem->idt_dcr;
  1625. mmio->spa = nfit_mem->spa_dcr;
  1626. rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
  1627. nfit_mem->memdev_dcr->interleave_ways);
  1628. if (rc) {
  1629. dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
  1630. __func__, nvdimm_name(nvdimm));
  1631. return rc;
  1632. }
  1633. rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
  1634. if (rc < 0) {
  1635. dev_dbg(dev, "%s: %s failed get DIMM flags\n",
  1636. __func__, nvdimm_name(nvdimm));
  1637. return rc;
  1638. }
  1639. if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
  1640. dev_warn(dev, "unable to guarantee persistence of writes\n");
  1641. if (mmio->line_size == 0)
  1642. return 0;
  1643. if ((u32) nfit_blk->cmd_offset % mmio->line_size
  1644. + 8 > mmio->line_size) {
  1645. dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
  1646. return -ENXIO;
  1647. } else if ((u32) nfit_blk->stat_offset % mmio->line_size
  1648. + 8 > mmio->line_size) {
  1649. dev_dbg(dev, "stat_offset crosses interleave boundary\n");
  1650. return -ENXIO;
  1651. }
  1652. return 0;
  1653. }
  1654. static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
  1655. struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
  1656. {
  1657. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1658. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1659. int cmd_rc, rc;
  1660. cmd->address = spa->address;
  1661. cmd->length = spa->length;
  1662. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
  1663. sizeof(*cmd), &cmd_rc);
  1664. if (rc < 0)
  1665. return rc;
  1666. return cmd_rc;
  1667. }
  1668. static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
  1669. {
  1670. int rc;
  1671. int cmd_rc;
  1672. struct nd_cmd_ars_start ars_start;
  1673. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1674. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1675. memset(&ars_start, 0, sizeof(ars_start));
  1676. ars_start.address = spa->address;
  1677. ars_start.length = spa->length;
  1678. if (nfit_spa_type(spa) == NFIT_SPA_PM)
  1679. ars_start.type = ND_ARS_PERSISTENT;
  1680. else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
  1681. ars_start.type = ND_ARS_VOLATILE;
  1682. else
  1683. return -ENOTTY;
  1684. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
  1685. sizeof(ars_start), &cmd_rc);
  1686. if (rc < 0)
  1687. return rc;
  1688. return cmd_rc;
  1689. }
  1690. static int ars_continue(struct acpi_nfit_desc *acpi_desc)
  1691. {
  1692. int rc, cmd_rc;
  1693. struct nd_cmd_ars_start ars_start;
  1694. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1695. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  1696. memset(&ars_start, 0, sizeof(ars_start));
  1697. ars_start.address = ars_status->restart_address;
  1698. ars_start.length = ars_status->restart_length;
  1699. ars_start.type = ars_status->type;
  1700. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
  1701. sizeof(ars_start), &cmd_rc);
  1702. if (rc < 0)
  1703. return rc;
  1704. return cmd_rc;
  1705. }
  1706. static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
  1707. {
  1708. struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
  1709. struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
  1710. int rc, cmd_rc;
  1711. rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
  1712. acpi_desc->ars_status_size, &cmd_rc);
  1713. if (rc < 0)
  1714. return rc;
  1715. return cmd_rc;
  1716. }
  1717. static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
  1718. struct nd_cmd_ars_status *ars_status)
  1719. {
  1720. struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
  1721. int rc;
  1722. u32 i;
  1723. /*
  1724. * First record starts at 44 byte offset from the start of the
  1725. * payload.
  1726. */
  1727. if (ars_status->out_length < 44)
  1728. return 0;
  1729. for (i = 0; i < ars_status->num_records; i++) {
  1730. /* only process full records */
  1731. if (ars_status->out_length
  1732. < 44 + sizeof(struct nd_ars_record) * (i + 1))
  1733. break;
  1734. rc = nvdimm_bus_add_poison(nvdimm_bus,
  1735. ars_status->records[i].err_address,
  1736. ars_status->records[i].length);
  1737. if (rc)
  1738. return rc;
  1739. }
  1740. if (i < ars_status->num_records)
  1741. dev_warn(acpi_desc->dev, "detected truncated ars results\n");
  1742. return 0;
  1743. }
  1744. static void acpi_nfit_remove_resource(void *data)
  1745. {
  1746. struct resource *res = data;
  1747. remove_resource(res);
  1748. }
  1749. static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
  1750. struct nd_region_desc *ndr_desc)
  1751. {
  1752. struct resource *res, *nd_res = ndr_desc->res;
  1753. int is_pmem, ret;
  1754. /* No operation if the region is already registered as PMEM */
  1755. is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
  1756. IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
  1757. if (is_pmem == REGION_INTERSECTS)
  1758. return 0;
  1759. res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
  1760. if (!res)
  1761. return -ENOMEM;
  1762. res->name = "Persistent Memory";
  1763. res->start = nd_res->start;
  1764. res->end = nd_res->end;
  1765. res->flags = IORESOURCE_MEM;
  1766. res->desc = IORES_DESC_PERSISTENT_MEMORY;
  1767. ret = insert_resource(&iomem_resource, res);
  1768. if (ret)
  1769. return ret;
  1770. ret = devm_add_action_or_reset(acpi_desc->dev,
  1771. acpi_nfit_remove_resource,
  1772. res);
  1773. if (ret)
  1774. return ret;
  1775. return 0;
  1776. }
  1777. static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
  1778. struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
  1779. struct acpi_nfit_memory_map *memdev,
  1780. struct nfit_spa *nfit_spa)
  1781. {
  1782. struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
  1783. memdev->device_handle);
  1784. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1785. struct nd_blk_region_desc *ndbr_desc;
  1786. struct nfit_mem *nfit_mem;
  1787. int blk_valid = 0;
  1788. if (!nvdimm) {
  1789. dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
  1790. spa->range_index, memdev->device_handle);
  1791. return -ENODEV;
  1792. }
  1793. mapping->nvdimm = nvdimm;
  1794. switch (nfit_spa_type(spa)) {
  1795. case NFIT_SPA_PM:
  1796. case NFIT_SPA_VOLATILE:
  1797. mapping->start = memdev->address;
  1798. mapping->size = memdev->region_size;
  1799. break;
  1800. case NFIT_SPA_DCR:
  1801. nfit_mem = nvdimm_provider_data(nvdimm);
  1802. if (!nfit_mem || !nfit_mem->bdw) {
  1803. dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
  1804. spa->range_index, nvdimm_name(nvdimm));
  1805. } else {
  1806. mapping->size = nfit_mem->bdw->capacity;
  1807. mapping->start = nfit_mem->bdw->start_address;
  1808. ndr_desc->num_lanes = nfit_mem->bdw->windows;
  1809. blk_valid = 1;
  1810. }
  1811. ndr_desc->mapping = mapping;
  1812. ndr_desc->num_mappings = blk_valid;
  1813. ndbr_desc = to_blk_region_desc(ndr_desc);
  1814. ndbr_desc->enable = acpi_nfit_blk_region_enable;
  1815. ndbr_desc->do_io = acpi_desc->blk_do_io;
  1816. nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
  1817. ndr_desc);
  1818. if (!nfit_spa->nd_region)
  1819. return -ENOMEM;
  1820. break;
  1821. }
  1822. return 0;
  1823. }
  1824. static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
  1825. {
  1826. return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
  1827. nfit_spa_type(spa) == NFIT_SPA_VCD ||
  1828. nfit_spa_type(spa) == NFIT_SPA_PDISK ||
  1829. nfit_spa_type(spa) == NFIT_SPA_PCD);
  1830. }
  1831. static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
  1832. struct nfit_spa *nfit_spa)
  1833. {
  1834. static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
  1835. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1836. struct nd_blk_region_desc ndbr_desc;
  1837. struct nd_region_desc *ndr_desc;
  1838. struct nfit_memdev *nfit_memdev;
  1839. struct nvdimm_bus *nvdimm_bus;
  1840. struct resource res;
  1841. int count = 0, rc;
  1842. if (nfit_spa->nd_region)
  1843. return 0;
  1844. if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
  1845. dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
  1846. __func__);
  1847. return 0;
  1848. }
  1849. memset(&res, 0, sizeof(res));
  1850. memset(&mappings, 0, sizeof(mappings));
  1851. memset(&ndbr_desc, 0, sizeof(ndbr_desc));
  1852. res.start = spa->address;
  1853. res.end = res.start + spa->length - 1;
  1854. ndr_desc = &ndbr_desc.ndr_desc;
  1855. ndr_desc->res = &res;
  1856. ndr_desc->provider_data = nfit_spa;
  1857. ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
  1858. if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
  1859. ndr_desc->numa_node = acpi_map_pxm_to_online_node(
  1860. spa->proximity_domain);
  1861. else
  1862. ndr_desc->numa_node = NUMA_NO_NODE;
  1863. list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
  1864. struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
  1865. struct nd_mapping_desc *mapping;
  1866. if (memdev->range_index != spa->range_index)
  1867. continue;
  1868. if (count >= ND_MAX_MAPPINGS) {
  1869. dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
  1870. spa->range_index, ND_MAX_MAPPINGS);
  1871. return -ENXIO;
  1872. }
  1873. mapping = &mappings[count++];
  1874. rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
  1875. memdev, nfit_spa);
  1876. if (rc)
  1877. goto out;
  1878. }
  1879. ndr_desc->mapping = mappings;
  1880. ndr_desc->num_mappings = count;
  1881. rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
  1882. if (rc)
  1883. goto out;
  1884. nvdimm_bus = acpi_desc->nvdimm_bus;
  1885. if (nfit_spa_type(spa) == NFIT_SPA_PM) {
  1886. rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
  1887. if (rc) {
  1888. dev_warn(acpi_desc->dev,
  1889. "failed to insert pmem resource to iomem: %d\n",
  1890. rc);
  1891. goto out;
  1892. }
  1893. nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
  1894. ndr_desc);
  1895. if (!nfit_spa->nd_region)
  1896. rc = -ENOMEM;
  1897. } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
  1898. nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
  1899. ndr_desc);
  1900. if (!nfit_spa->nd_region)
  1901. rc = -ENOMEM;
  1902. } else if (nfit_spa_is_virtual(spa)) {
  1903. nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
  1904. ndr_desc);
  1905. if (!nfit_spa->nd_region)
  1906. rc = -ENOMEM;
  1907. }
  1908. out:
  1909. if (rc)
  1910. dev_err(acpi_desc->dev, "failed to register spa range %d\n",
  1911. nfit_spa->spa->range_index);
  1912. return rc;
  1913. }
  1914. static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
  1915. u32 max_ars)
  1916. {
  1917. struct device *dev = acpi_desc->dev;
  1918. struct nd_cmd_ars_status *ars_status;
  1919. if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
  1920. memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
  1921. return 0;
  1922. }
  1923. if (acpi_desc->ars_status)
  1924. devm_kfree(dev, acpi_desc->ars_status);
  1925. acpi_desc->ars_status = NULL;
  1926. ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
  1927. if (!ars_status)
  1928. return -ENOMEM;
  1929. acpi_desc->ars_status = ars_status;
  1930. acpi_desc->ars_status_size = max_ars;
  1931. return 0;
  1932. }
  1933. static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
  1934. struct nfit_spa *nfit_spa)
  1935. {
  1936. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1937. int rc;
  1938. if (!nfit_spa->max_ars) {
  1939. struct nd_cmd_ars_cap ars_cap;
  1940. memset(&ars_cap, 0, sizeof(ars_cap));
  1941. rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
  1942. if (rc < 0)
  1943. return rc;
  1944. nfit_spa->max_ars = ars_cap.max_ars_out;
  1945. nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
  1946. /* check that the supported scrub types match the spa type */
  1947. if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
  1948. ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
  1949. return -ENOTTY;
  1950. else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
  1951. ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
  1952. return -ENOTTY;
  1953. }
  1954. if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
  1955. return -ENOMEM;
  1956. rc = ars_get_status(acpi_desc);
  1957. if (rc < 0 && rc != -ENOSPC)
  1958. return rc;
  1959. if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
  1960. return -ENOMEM;
  1961. return 0;
  1962. }
  1963. static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
  1964. struct nfit_spa *nfit_spa)
  1965. {
  1966. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  1967. unsigned int overflow_retry = scrub_overflow_abort;
  1968. u64 init_ars_start = 0, init_ars_len = 0;
  1969. struct device *dev = acpi_desc->dev;
  1970. unsigned int tmo = scrub_timeout;
  1971. int rc;
  1972. if (!nfit_spa->ars_required || !nfit_spa->nd_region)
  1973. return;
  1974. rc = ars_start(acpi_desc, nfit_spa);
  1975. /*
  1976. * If we timed out the initial scan we'll still be busy here,
  1977. * and will wait another timeout before giving up permanently.
  1978. */
  1979. if (rc < 0 && rc != -EBUSY)
  1980. return;
  1981. do {
  1982. u64 ars_start, ars_len;
  1983. if (acpi_desc->cancel)
  1984. break;
  1985. rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
  1986. if (rc == -ENOTTY)
  1987. break;
  1988. if (rc == -EBUSY && !tmo) {
  1989. dev_warn(dev, "range %d ars timeout, aborting\n",
  1990. spa->range_index);
  1991. break;
  1992. }
  1993. if (rc == -EBUSY) {
  1994. /*
  1995. * Note, entries may be appended to the list
  1996. * while the lock is dropped, but the workqueue
  1997. * being active prevents entries being deleted /
  1998. * freed.
  1999. */
  2000. mutex_unlock(&acpi_desc->init_mutex);
  2001. ssleep(1);
  2002. tmo--;
  2003. mutex_lock(&acpi_desc->init_mutex);
  2004. continue;
  2005. }
  2006. /* we got some results, but there are more pending... */
  2007. if (rc == -ENOSPC && overflow_retry--) {
  2008. if (!init_ars_len) {
  2009. init_ars_len = acpi_desc->ars_status->length;
  2010. init_ars_start = acpi_desc->ars_status->address;
  2011. }
  2012. rc = ars_continue(acpi_desc);
  2013. }
  2014. if (rc < 0) {
  2015. dev_warn(dev, "range %d ars continuation failed\n",
  2016. spa->range_index);
  2017. break;
  2018. }
  2019. if (init_ars_len) {
  2020. ars_start = init_ars_start;
  2021. ars_len = init_ars_len;
  2022. } else {
  2023. ars_start = acpi_desc->ars_status->address;
  2024. ars_len = acpi_desc->ars_status->length;
  2025. }
  2026. dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
  2027. spa->range_index, ars_start, ars_len);
  2028. /* notify the region about new poison entries */
  2029. nvdimm_region_notify(nfit_spa->nd_region,
  2030. NVDIMM_REVALIDATE_POISON);
  2031. break;
  2032. } while (1);
  2033. }
  2034. static void acpi_nfit_scrub(struct work_struct *work)
  2035. {
  2036. struct device *dev;
  2037. u64 init_scrub_length = 0;
  2038. struct nfit_spa *nfit_spa;
  2039. u64 init_scrub_address = 0;
  2040. bool init_ars_done = false;
  2041. struct acpi_nfit_desc *acpi_desc;
  2042. unsigned int tmo = scrub_timeout;
  2043. unsigned int overflow_retry = scrub_overflow_abort;
  2044. acpi_desc = container_of(work, typeof(*acpi_desc), work);
  2045. dev = acpi_desc->dev;
  2046. /*
  2047. * We scrub in 2 phases. The first phase waits for any platform
  2048. * firmware initiated scrubs to complete and then we go search for the
  2049. * affected spa regions to mark them scanned. In the second phase we
  2050. * initiate a directed scrub for every range that was not scrubbed in
  2051. * phase 1. If we're called for a 'rescan', we harmlessly pass through
  2052. * the first phase, but really only care about running phase 2, where
  2053. * regions can be notified of new poison.
  2054. */
  2055. /* process platform firmware initiated scrubs */
  2056. retry:
  2057. mutex_lock(&acpi_desc->init_mutex);
  2058. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2059. struct nd_cmd_ars_status *ars_status;
  2060. struct acpi_nfit_system_address *spa;
  2061. u64 ars_start, ars_len;
  2062. int rc;
  2063. if (acpi_desc->cancel)
  2064. break;
  2065. if (nfit_spa->nd_region)
  2066. continue;
  2067. if (init_ars_done) {
  2068. /*
  2069. * No need to re-query, we're now just
  2070. * reconciling all the ranges covered by the
  2071. * initial scrub
  2072. */
  2073. rc = 0;
  2074. } else
  2075. rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
  2076. if (rc == -ENOTTY) {
  2077. /* no ars capability, just register spa and move on */
  2078. acpi_nfit_register_region(acpi_desc, nfit_spa);
  2079. continue;
  2080. }
  2081. if (rc == -EBUSY && !tmo) {
  2082. /* fallthrough to directed scrub in phase 2 */
  2083. dev_warn(dev, "timeout awaiting ars results, continuing...\n");
  2084. break;
  2085. } else if (rc == -EBUSY) {
  2086. mutex_unlock(&acpi_desc->init_mutex);
  2087. ssleep(1);
  2088. tmo--;
  2089. goto retry;
  2090. }
  2091. /* we got some results, but there are more pending... */
  2092. if (rc == -ENOSPC && overflow_retry--) {
  2093. ars_status = acpi_desc->ars_status;
  2094. /*
  2095. * Record the original scrub range, so that we
  2096. * can recall all the ranges impacted by the
  2097. * initial scrub.
  2098. */
  2099. if (!init_scrub_length) {
  2100. init_scrub_length = ars_status->length;
  2101. init_scrub_address = ars_status->address;
  2102. }
  2103. rc = ars_continue(acpi_desc);
  2104. if (rc == 0) {
  2105. mutex_unlock(&acpi_desc->init_mutex);
  2106. goto retry;
  2107. }
  2108. }
  2109. if (rc < 0) {
  2110. /*
  2111. * Initial scrub failed, we'll give it one more
  2112. * try below...
  2113. */
  2114. break;
  2115. }
  2116. /* We got some final results, record completed ranges */
  2117. ars_status = acpi_desc->ars_status;
  2118. if (init_scrub_length) {
  2119. ars_start = init_scrub_address;
  2120. ars_len = ars_start + init_scrub_length;
  2121. } else {
  2122. ars_start = ars_status->address;
  2123. ars_len = ars_status->length;
  2124. }
  2125. spa = nfit_spa->spa;
  2126. if (!init_ars_done) {
  2127. init_ars_done = true;
  2128. dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
  2129. ars_start, ars_len);
  2130. }
  2131. if (ars_start <= spa->address && ars_start + ars_len
  2132. >= spa->address + spa->length)
  2133. acpi_nfit_register_region(acpi_desc, nfit_spa);
  2134. }
  2135. /*
  2136. * For all the ranges not covered by an initial scrub we still
  2137. * want to see if there are errors, but it's ok to discover them
  2138. * asynchronously.
  2139. */
  2140. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2141. /*
  2142. * Flag all the ranges that still need scrubbing, but
  2143. * register them now to make data available.
  2144. */
  2145. if (!nfit_spa->nd_region) {
  2146. nfit_spa->ars_required = 1;
  2147. acpi_nfit_register_region(acpi_desc, nfit_spa);
  2148. }
  2149. }
  2150. list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
  2151. acpi_nfit_async_scrub(acpi_desc, nfit_spa);
  2152. acpi_desc->scrub_count++;
  2153. if (acpi_desc->scrub_count_state)
  2154. sysfs_notify_dirent(acpi_desc->scrub_count_state);
  2155. mutex_unlock(&acpi_desc->init_mutex);
  2156. }
  2157. static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
  2158. {
  2159. struct nfit_spa *nfit_spa;
  2160. int rc;
  2161. list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
  2162. if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
  2163. /* BLK regions don't need to wait for ars results */
  2164. rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
  2165. if (rc)
  2166. return rc;
  2167. }
  2168. queue_work(nfit_wq, &acpi_desc->work);
  2169. return 0;
  2170. }
  2171. static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
  2172. struct nfit_table_prev *prev)
  2173. {
  2174. struct device *dev = acpi_desc->dev;
  2175. if (!list_empty(&prev->spas) ||
  2176. !list_empty(&prev->memdevs) ||
  2177. !list_empty(&prev->dcrs) ||
  2178. !list_empty(&prev->bdws) ||
  2179. !list_empty(&prev->idts) ||
  2180. !list_empty(&prev->flushes)) {
  2181. dev_err(dev, "new nfit deletes entries (unsupported)\n");
  2182. return -ENXIO;
  2183. }
  2184. return 0;
  2185. }
  2186. static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
  2187. {
  2188. struct device *dev = acpi_desc->dev;
  2189. struct kernfs_node *nfit;
  2190. struct device *bus_dev;
  2191. if (!ars_supported(acpi_desc->nvdimm_bus))
  2192. return 0;
  2193. bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
  2194. nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
  2195. if (!nfit) {
  2196. dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
  2197. return -ENODEV;
  2198. }
  2199. acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
  2200. sysfs_put(nfit);
  2201. if (!acpi_desc->scrub_count_state) {
  2202. dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
  2203. return -ENODEV;
  2204. }
  2205. return 0;
  2206. }
  2207. static void acpi_nfit_destruct(void *data)
  2208. {
  2209. struct acpi_nfit_desc *acpi_desc = data;
  2210. struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
  2211. /*
  2212. * Destruct under acpi_desc_lock so that nfit_handle_mce does not
  2213. * race teardown
  2214. */
  2215. mutex_lock(&acpi_desc_lock);
  2216. acpi_desc->cancel = 1;
  2217. /*
  2218. * Bounce the nvdimm bus lock to make sure any in-flight
  2219. * acpi_nfit_ars_rescan() submissions have had a chance to
  2220. * either submit or see ->cancel set.
  2221. */
  2222. device_lock(bus_dev);
  2223. device_unlock(bus_dev);
  2224. flush_workqueue(nfit_wq);
  2225. if (acpi_desc->scrub_count_state)
  2226. sysfs_put(acpi_desc->scrub_count_state);
  2227. nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
  2228. acpi_desc->nvdimm_bus = NULL;
  2229. list_del(&acpi_desc->list);
  2230. mutex_unlock(&acpi_desc_lock);
  2231. }
  2232. int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
  2233. {
  2234. struct device *dev = acpi_desc->dev;
  2235. struct nfit_table_prev prev;
  2236. const void *end;
  2237. int rc;
  2238. if (!acpi_desc->nvdimm_bus) {
  2239. acpi_nfit_init_dsms(acpi_desc);
  2240. acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
  2241. &acpi_desc->nd_desc);
  2242. if (!acpi_desc->nvdimm_bus)
  2243. return -ENOMEM;
  2244. rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
  2245. acpi_desc);
  2246. if (rc)
  2247. return rc;
  2248. rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
  2249. if (rc)
  2250. return rc;
  2251. /* register this acpi_desc for mce notifications */
  2252. mutex_lock(&acpi_desc_lock);
  2253. list_add_tail(&acpi_desc->list, &acpi_descs);
  2254. mutex_unlock(&acpi_desc_lock);
  2255. }
  2256. mutex_lock(&acpi_desc->init_mutex);
  2257. INIT_LIST_HEAD(&prev.spas);
  2258. INIT_LIST_HEAD(&prev.memdevs);
  2259. INIT_LIST_HEAD(&prev.dcrs);
  2260. INIT_LIST_HEAD(&prev.bdws);
  2261. INIT_LIST_HEAD(&prev.idts);
  2262. INIT_LIST_HEAD(&prev.flushes);
  2263. list_cut_position(&prev.spas, &acpi_desc->spas,
  2264. acpi_desc->spas.prev);
  2265. list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
  2266. acpi_desc->memdevs.prev);
  2267. list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
  2268. acpi_desc->dcrs.prev);
  2269. list_cut_position(&prev.bdws, &acpi_desc->bdws,
  2270. acpi_desc->bdws.prev);
  2271. list_cut_position(&prev.idts, &acpi_desc->idts,
  2272. acpi_desc->idts.prev);
  2273. list_cut_position(&prev.flushes, &acpi_desc->flushes,
  2274. acpi_desc->flushes.prev);
  2275. end = data + sz;
  2276. while (!IS_ERR_OR_NULL(data))
  2277. data = add_table(acpi_desc, &prev, data, end);
  2278. if (IS_ERR(data)) {
  2279. dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
  2280. PTR_ERR(data));
  2281. rc = PTR_ERR(data);
  2282. goto out_unlock;
  2283. }
  2284. rc = acpi_nfit_check_deletions(acpi_desc, &prev);
  2285. if (rc)
  2286. goto out_unlock;
  2287. rc = nfit_mem_init(acpi_desc);
  2288. if (rc)
  2289. goto out_unlock;
  2290. rc = acpi_nfit_register_dimms(acpi_desc);
  2291. if (rc)
  2292. goto out_unlock;
  2293. rc = acpi_nfit_register_regions(acpi_desc);
  2294. out_unlock:
  2295. mutex_unlock(&acpi_desc->init_mutex);
  2296. return rc;
  2297. }
  2298. EXPORT_SYMBOL_GPL(acpi_nfit_init);
  2299. struct acpi_nfit_flush_work {
  2300. struct work_struct work;
  2301. struct completion cmp;
  2302. };
  2303. static void flush_probe(struct work_struct *work)
  2304. {
  2305. struct acpi_nfit_flush_work *flush;
  2306. flush = container_of(work, typeof(*flush), work);
  2307. complete(&flush->cmp);
  2308. }
  2309. static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
  2310. {
  2311. struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
  2312. struct device *dev = acpi_desc->dev;
  2313. struct acpi_nfit_flush_work flush;
  2314. /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
  2315. device_lock(dev);
  2316. device_unlock(dev);
  2317. /*
  2318. * Scrub work could take 10s of seconds, userspace may give up so we
  2319. * need to be interruptible while waiting.
  2320. */
  2321. INIT_WORK_ONSTACK(&flush.work, flush_probe);
  2322. COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
  2323. queue_work(nfit_wq, &flush.work);
  2324. return wait_for_completion_interruptible(&flush.cmp);
  2325. }
  2326. static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
  2327. struct nvdimm *nvdimm, unsigned int cmd)
  2328. {
  2329. struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
  2330. if (nvdimm)
  2331. return 0;
  2332. if (cmd != ND_CMD_ARS_START)
  2333. return 0;
  2334. /*
  2335. * The kernel and userspace may race to initiate a scrub, but
  2336. * the scrub thread is prepared to lose that initial race. It
  2337. * just needs guarantees that any ars it initiates are not
  2338. * interrupted by any intervening start reqeusts from userspace.
  2339. */
  2340. if (work_busy(&acpi_desc->work))
  2341. return -EBUSY;
  2342. return 0;
  2343. }
  2344. int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
  2345. {
  2346. struct device *dev = acpi_desc->dev;
  2347. struct nfit_spa *nfit_spa;
  2348. if (work_busy(&acpi_desc->work))
  2349. return -EBUSY;
  2350. if (acpi_desc->cancel)
  2351. return 0;
  2352. mutex_lock(&acpi_desc->init_mutex);
  2353. list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
  2354. struct acpi_nfit_system_address *spa = nfit_spa->spa;
  2355. if (nfit_spa_type(spa) != NFIT_SPA_PM)
  2356. continue;
  2357. nfit_spa->ars_required = 1;
  2358. }
  2359. queue_work(nfit_wq, &acpi_desc->work);
  2360. dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
  2361. mutex_unlock(&acpi_desc->init_mutex);
  2362. return 0;
  2363. }
  2364. void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
  2365. {
  2366. struct nvdimm_bus_descriptor *nd_desc;
  2367. dev_set_drvdata(dev, acpi_desc);
  2368. acpi_desc->dev = dev;
  2369. acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
  2370. nd_desc = &acpi_desc->nd_desc;
  2371. nd_desc->provider_name = "ACPI.NFIT";
  2372. nd_desc->module = THIS_MODULE;
  2373. nd_desc->ndctl = acpi_nfit_ctl;
  2374. nd_desc->flush_probe = acpi_nfit_flush_probe;
  2375. nd_desc->clear_to_send = acpi_nfit_clear_to_send;
  2376. nd_desc->attr_groups = acpi_nfit_attribute_groups;
  2377. INIT_LIST_HEAD(&acpi_desc->spas);
  2378. INIT_LIST_HEAD(&acpi_desc->dcrs);
  2379. INIT_LIST_HEAD(&acpi_desc->bdws);
  2380. INIT_LIST_HEAD(&acpi_desc->idts);
  2381. INIT_LIST_HEAD(&acpi_desc->flushes);
  2382. INIT_LIST_HEAD(&acpi_desc->memdevs);
  2383. INIT_LIST_HEAD(&acpi_desc->dimms);
  2384. INIT_LIST_HEAD(&acpi_desc->list);
  2385. mutex_init(&acpi_desc->init_mutex);
  2386. INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
  2387. }
  2388. EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
  2389. static int acpi_nfit_add(struct acpi_device *adev)
  2390. {
  2391. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  2392. struct acpi_nfit_desc *acpi_desc;
  2393. struct device *dev = &adev->dev;
  2394. struct acpi_table_header *tbl;
  2395. acpi_status status = AE_OK;
  2396. acpi_size sz;
  2397. int rc = 0;
  2398. status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
  2399. if (ACPI_FAILURE(status)) {
  2400. /* This is ok, we could have an nvdimm hotplugged later */
  2401. dev_dbg(dev, "failed to find NFIT at startup\n");
  2402. return 0;
  2403. }
  2404. sz = tbl->length;
  2405. acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
  2406. if (!acpi_desc)
  2407. return -ENOMEM;
  2408. acpi_nfit_desc_init(acpi_desc, &adev->dev);
  2409. /* Save the acpi header for exporting the revision via sysfs */
  2410. acpi_desc->acpi_header = *tbl;
  2411. /* Evaluate _FIT and override with that if present */
  2412. status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
  2413. if (ACPI_SUCCESS(status) && buf.length > 0) {
  2414. union acpi_object *obj = buf.pointer;
  2415. if (obj->type == ACPI_TYPE_BUFFER)
  2416. rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
  2417. obj->buffer.length);
  2418. else
  2419. dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
  2420. __func__, (int) obj->type);
  2421. kfree(buf.pointer);
  2422. } else
  2423. /* skip over the lead-in header table */
  2424. rc = acpi_nfit_init(acpi_desc, (void *) tbl
  2425. + sizeof(struct acpi_table_nfit),
  2426. sz - sizeof(struct acpi_table_nfit));
  2427. return rc;
  2428. }
  2429. static int acpi_nfit_remove(struct acpi_device *adev)
  2430. {
  2431. /* see acpi_nfit_destruct */
  2432. return 0;
  2433. }
  2434. void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
  2435. {
  2436. struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
  2437. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
  2438. union acpi_object *obj;
  2439. acpi_status status;
  2440. int ret;
  2441. dev_dbg(dev, "%s: event: %d\n", __func__, event);
  2442. if (event != NFIT_NOTIFY_UPDATE)
  2443. return;
  2444. if (!dev->driver) {
  2445. /* dev->driver may be null if we're being removed */
  2446. dev_dbg(dev, "%s: no driver found for dev\n", __func__);
  2447. return;
  2448. }
  2449. if (!acpi_desc) {
  2450. acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
  2451. if (!acpi_desc)
  2452. return;
  2453. acpi_nfit_desc_init(acpi_desc, dev);
  2454. } else {
  2455. /*
  2456. * Finish previous registration before considering new
  2457. * regions.
  2458. */
  2459. flush_workqueue(nfit_wq);
  2460. }
  2461. /* Evaluate _FIT */
  2462. status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
  2463. if (ACPI_FAILURE(status)) {
  2464. dev_err(dev, "failed to evaluate _FIT\n");
  2465. return;
  2466. }
  2467. obj = buf.pointer;
  2468. if (obj->type == ACPI_TYPE_BUFFER) {
  2469. ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
  2470. obj->buffer.length);
  2471. if (ret)
  2472. dev_err(dev, "failed to merge updated NFIT\n");
  2473. } else
  2474. dev_err(dev, "Invalid _FIT\n");
  2475. kfree(buf.pointer);
  2476. }
  2477. EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
  2478. static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
  2479. {
  2480. device_lock(&adev->dev);
  2481. __acpi_nfit_notify(&adev->dev, adev->handle, event);
  2482. device_unlock(&adev->dev);
  2483. }
  2484. static const struct acpi_device_id acpi_nfit_ids[] = {
  2485. { "ACPI0012", 0 },
  2486. { "", 0 },
  2487. };
  2488. MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
  2489. static struct acpi_driver acpi_nfit_driver = {
  2490. .name = KBUILD_MODNAME,
  2491. .ids = acpi_nfit_ids,
  2492. .ops = {
  2493. .add = acpi_nfit_add,
  2494. .remove = acpi_nfit_remove,
  2495. .notify = acpi_nfit_notify,
  2496. },
  2497. };
  2498. static __init int nfit_init(void)
  2499. {
  2500. BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
  2501. BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
  2502. BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
  2503. BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
  2504. BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
  2505. BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
  2506. BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
  2507. acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
  2508. acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
  2509. acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
  2510. acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
  2511. acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
  2512. acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
  2513. acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
  2514. acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
  2515. acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
  2516. acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
  2517. acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
  2518. acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
  2519. acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
  2520. nfit_wq = create_singlethread_workqueue("nfit");
  2521. if (!nfit_wq)
  2522. return -ENOMEM;
  2523. nfit_mce_register();
  2524. return acpi_bus_register_driver(&acpi_nfit_driver);
  2525. }
  2526. static __exit void nfit_exit(void)
  2527. {
  2528. nfit_mce_unregister();
  2529. acpi_bus_unregister_driver(&acpi_nfit_driver);
  2530. destroy_workqueue(nfit_wq);
  2531. WARN_ON(!list_empty(&acpi_descs));
  2532. }
  2533. module_init(nfit_init);
  2534. module_exit(nfit_exit);
  2535. MODULE_LICENSE("GPL v2");
  2536. MODULE_AUTHOR("Intel Corporation");