core.c 96 KB

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