superpipe.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  1. /*
  2. * CXL Flash Device Driver
  3. *
  4. * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
  5. * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
  6. *
  7. * Copyright (C) 2015 IBM Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/file.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/pci.h>
  18. #include <linux/syscalls.h>
  19. #include <asm/unaligned.h>
  20. #include <scsi/scsi.h>
  21. #include <scsi/scsi_host.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_eh.h>
  24. #include <uapi/scsi/cxlflash_ioctl.h>
  25. #include "sislite.h"
  26. #include "common.h"
  27. #include "vlun.h"
  28. #include "superpipe.h"
  29. struct cxlflash_global global;
  30. /**
  31. * marshal_rele_to_resize() - translate release to resize structure
  32. * @rele: Source structure from which to translate/copy.
  33. * @resize: Destination structure for the translate/copy.
  34. */
  35. static void marshal_rele_to_resize(struct dk_cxlflash_release *release,
  36. struct dk_cxlflash_resize *resize)
  37. {
  38. resize->hdr = release->hdr;
  39. resize->context_id = release->context_id;
  40. resize->rsrc_handle = release->rsrc_handle;
  41. }
  42. /**
  43. * marshal_det_to_rele() - translate detach to release structure
  44. * @detach: Destination structure for the translate/copy.
  45. * @rele: Source structure from which to translate/copy.
  46. */
  47. static void marshal_det_to_rele(struct dk_cxlflash_detach *detach,
  48. struct dk_cxlflash_release *release)
  49. {
  50. release->hdr = detach->hdr;
  51. release->context_id = detach->context_id;
  52. }
  53. /**
  54. * marshal_udir_to_rele() - translate udirect to release structure
  55. * @udirect: Source structure from which to translate/copy.
  56. * @release: Destination structure for the translate/copy.
  57. */
  58. static void marshal_udir_to_rele(struct dk_cxlflash_udirect *udirect,
  59. struct dk_cxlflash_release *release)
  60. {
  61. release->hdr = udirect->hdr;
  62. release->context_id = udirect->context_id;
  63. release->rsrc_handle = udirect->rsrc_handle;
  64. }
  65. /**
  66. * cxlflash_free_errpage() - frees resources associated with global error page
  67. */
  68. void cxlflash_free_errpage(void)
  69. {
  70. mutex_lock(&global.mutex);
  71. if (global.err_page) {
  72. __free_page(global.err_page);
  73. global.err_page = NULL;
  74. }
  75. mutex_unlock(&global.mutex);
  76. }
  77. /**
  78. * cxlflash_stop_term_user_contexts() - stops/terminates known user contexts
  79. * @cfg: Internal structure associated with the host.
  80. *
  81. * When the host needs to go down, all users must be quiesced and their
  82. * memory freed. This is accomplished by putting the contexts in error
  83. * state which will notify the user and let them 'drive' the tear down.
  84. * Meanwhile, this routine camps until all user contexts have been removed.
  85. *
  86. * Note that the main loop in this routine will always execute at least once
  87. * to flush the reset_waitq.
  88. */
  89. void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *cfg)
  90. {
  91. struct device *dev = &cfg->dev->dev;
  92. int i, found = true;
  93. cxlflash_mark_contexts_error(cfg);
  94. while (true) {
  95. for (i = 0; i < MAX_CONTEXT; i++)
  96. if (cfg->ctx_tbl[i]) {
  97. found = true;
  98. break;
  99. }
  100. if (!found && list_empty(&cfg->ctx_err_recovery))
  101. return;
  102. dev_dbg(dev, "%s: Wait for user contexts to quiesce...\n",
  103. __func__);
  104. wake_up_all(&cfg->reset_waitq);
  105. ssleep(1);
  106. found = false;
  107. }
  108. }
  109. /**
  110. * find_error_context() - locates a context by cookie on the error recovery list
  111. * @cfg: Internal structure associated with the host.
  112. * @rctxid: Desired context by id.
  113. * @file: Desired context by file.
  114. *
  115. * Return: Found context on success, NULL on failure
  116. */
  117. static struct ctx_info *find_error_context(struct cxlflash_cfg *cfg, u64 rctxid,
  118. struct file *file)
  119. {
  120. struct ctx_info *ctxi;
  121. list_for_each_entry(ctxi, &cfg->ctx_err_recovery, list)
  122. if ((ctxi->ctxid == rctxid) || (ctxi->file == file))
  123. return ctxi;
  124. return NULL;
  125. }
  126. /**
  127. * get_context() - obtains a validated and locked context reference
  128. * @cfg: Internal structure associated with the host.
  129. * @rctxid: Desired context (raw, un-decoded format).
  130. * @arg: LUN information or file associated with request.
  131. * @ctx_ctrl: Control information to 'steer' desired lookup.
  132. *
  133. * NOTE: despite the name pid, in linux, current->pid actually refers
  134. * to the lightweight process id (tid) and can change if the process is
  135. * multi threaded. The tgid remains constant for the process and only changes
  136. * when the process of fork. For all intents and purposes, think of tgid
  137. * as a pid in the traditional sense.
  138. *
  139. * Return: Validated context on success, NULL on failure
  140. */
  141. struct ctx_info *get_context(struct cxlflash_cfg *cfg, u64 rctxid,
  142. void *arg, enum ctx_ctrl ctx_ctrl)
  143. {
  144. struct device *dev = &cfg->dev->dev;
  145. struct ctx_info *ctxi = NULL;
  146. struct lun_access *lun_access = NULL;
  147. struct file *file = NULL;
  148. struct llun_info *lli = arg;
  149. u64 ctxid = DECODE_CTXID(rctxid);
  150. int rc;
  151. pid_t pid = task_tgid_nr(current), ctxpid = 0;
  152. if (ctx_ctrl & CTX_CTRL_FILE) {
  153. lli = NULL;
  154. file = (struct file *)arg;
  155. }
  156. if (ctx_ctrl & CTX_CTRL_CLONE)
  157. pid = task_ppid_nr(current);
  158. if (likely(ctxid < MAX_CONTEXT)) {
  159. while (true) {
  160. mutex_lock(&cfg->ctx_tbl_list_mutex);
  161. ctxi = cfg->ctx_tbl[ctxid];
  162. if (ctxi)
  163. if ((file && (ctxi->file != file)) ||
  164. (!file && (ctxi->ctxid != rctxid)))
  165. ctxi = NULL;
  166. if ((ctx_ctrl & CTX_CTRL_ERR) ||
  167. (!ctxi && (ctx_ctrl & CTX_CTRL_ERR_FALLBACK)))
  168. ctxi = find_error_context(cfg, rctxid, file);
  169. if (!ctxi) {
  170. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  171. goto out;
  172. }
  173. /*
  174. * Need to acquire ownership of the context while still
  175. * under the table/list lock to serialize with a remove
  176. * thread. Use the 'try' to avoid stalling the
  177. * table/list lock for a single context.
  178. *
  179. * Note that the lock order is:
  180. *
  181. * cfg->ctx_tbl_list_mutex -> ctxi->mutex
  182. *
  183. * Therefore release ctx_tbl_list_mutex before retrying.
  184. */
  185. rc = mutex_trylock(&ctxi->mutex);
  186. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  187. if (rc)
  188. break; /* got the context's lock! */
  189. }
  190. if (ctxi->unavail)
  191. goto denied;
  192. ctxpid = ctxi->pid;
  193. if (likely(!(ctx_ctrl & CTX_CTRL_NOPID)))
  194. if (pid != ctxpid)
  195. goto denied;
  196. if (lli) {
  197. list_for_each_entry(lun_access, &ctxi->luns, list)
  198. if (lun_access->lli == lli)
  199. goto out;
  200. goto denied;
  201. }
  202. }
  203. out:
  204. dev_dbg(dev, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u "
  205. "ctx_ctrl=%u\n", __func__, rctxid, ctxi, ctxpid, pid,
  206. ctx_ctrl);
  207. return ctxi;
  208. denied:
  209. mutex_unlock(&ctxi->mutex);
  210. ctxi = NULL;
  211. goto out;
  212. }
  213. /**
  214. * put_context() - release a context that was retrieved from get_context()
  215. * @ctxi: Context to release.
  216. *
  217. * For now, releasing the context equates to unlocking it's mutex.
  218. */
  219. void put_context(struct ctx_info *ctxi)
  220. {
  221. mutex_unlock(&ctxi->mutex);
  222. }
  223. /**
  224. * afu_attach() - attach a context to the AFU
  225. * @cfg: Internal structure associated with the host.
  226. * @ctxi: Context to attach.
  227. *
  228. * Upon setting the context capabilities, they must be confirmed with
  229. * a read back operation as the context might have been closed since
  230. * the mailbox was unlocked. When this occurs, registration is failed.
  231. *
  232. * Return: 0 on success, -errno on failure
  233. */
  234. static int afu_attach(struct cxlflash_cfg *cfg, struct ctx_info *ctxi)
  235. {
  236. struct device *dev = &cfg->dev->dev;
  237. struct afu *afu = cfg->afu;
  238. struct sisl_ctrl_map __iomem *ctrl_map = ctxi->ctrl_map;
  239. int rc = 0;
  240. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  241. u64 val;
  242. int i;
  243. /* Unlock cap and restrict user to read/write cmds in translated mode */
  244. readq_be(&ctrl_map->mbox_r);
  245. val = (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD);
  246. writeq_be(val, &ctrl_map->ctx_cap);
  247. val = readq_be(&ctrl_map->ctx_cap);
  248. if (val != (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD)) {
  249. dev_err(dev, "%s: ctx may be closed val=%016llx\n",
  250. __func__, val);
  251. rc = -EAGAIN;
  252. goto out;
  253. }
  254. if (afu_is_ocxl_lisn(afu)) {
  255. /* Set up the LISN effective address for each interrupt */
  256. for (i = 0; i < ctxi->irqs; i++) {
  257. val = cfg->ops->get_irq_objhndl(ctxi->ctx, i);
  258. writeq_be(val, &ctrl_map->lisn_ea[i]);
  259. }
  260. /* Use primary HWQ PASID as identifier for all interrupts */
  261. val = hwq->ctx_hndl;
  262. writeq_be(SISL_LISN_PASID(val, val), &ctrl_map->lisn_pasid[0]);
  263. writeq_be(SISL_LISN_PASID(0UL, val), &ctrl_map->lisn_pasid[1]);
  264. }
  265. /* Set up MMIO registers pointing to the RHT */
  266. writeq_be((u64)ctxi->rht_start, &ctrl_map->rht_start);
  267. val = SISL_RHT_CNT_ID((u64)MAX_RHT_PER_CONTEXT, (u64)(hwq->ctx_hndl));
  268. writeq_be(val, &ctrl_map->rht_cnt_id);
  269. out:
  270. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  271. return rc;
  272. }
  273. /**
  274. * read_cap16() - issues a SCSI READ_CAP16 command
  275. * @sdev: SCSI device associated with LUN.
  276. * @lli: LUN destined for capacity request.
  277. *
  278. * The READ_CAP16 can take quite a while to complete. Should an EEH occur while
  279. * in scsi_execute(), the EEH handler will attempt to recover. As part of the
  280. * recovery, the handler drains all currently running ioctls, waiting until they
  281. * have completed before proceeding with a reset. As this routine is used on the
  282. * ioctl path, this can create a condition where the EEH handler becomes stuck,
  283. * infinitely waiting for this ioctl thread. To avoid this behavior, temporarily
  284. * unmark this thread as an ioctl thread by releasing the ioctl read semaphore.
  285. * This will allow the EEH handler to proceed with a recovery while this thread
  286. * is still running. Once the scsi_execute() returns, reacquire the ioctl read
  287. * semaphore and check the adapter state in case it changed while inside of
  288. * scsi_execute(). The state check will wait if the adapter is still being
  289. * recovered or return a failure if the recovery failed. In the event that the
  290. * adapter reset failed, simply return the failure as the ioctl would be unable
  291. * to continue.
  292. *
  293. * Note that the above puts a requirement on this routine to only be called on
  294. * an ioctl thread.
  295. *
  296. * Return: 0 on success, -errno on failure
  297. */
  298. static int read_cap16(struct scsi_device *sdev, struct llun_info *lli)
  299. {
  300. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  301. struct device *dev = &cfg->dev->dev;
  302. struct glun_info *gli = lli->parent;
  303. struct scsi_sense_hdr sshdr;
  304. u8 *cmd_buf = NULL;
  305. u8 *scsi_cmd = NULL;
  306. int rc = 0;
  307. int result = 0;
  308. int retry_cnt = 0;
  309. u32 to = CMD_TIMEOUT * HZ;
  310. retry:
  311. cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
  312. scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
  313. if (unlikely(!cmd_buf || !scsi_cmd)) {
  314. rc = -ENOMEM;
  315. goto out;
  316. }
  317. scsi_cmd[0] = SERVICE_ACTION_IN_16; /* read cap(16) */
  318. scsi_cmd[1] = SAI_READ_CAPACITY_16; /* service action */
  319. put_unaligned_be32(CMD_BUFSIZE, &scsi_cmd[10]);
  320. dev_dbg(dev, "%s: %ssending cmd(%02x)\n", __func__,
  321. retry_cnt ? "re" : "", scsi_cmd[0]);
  322. /* Drop the ioctl read semahpore across lengthy call */
  323. up_read(&cfg->ioctl_rwsem);
  324. result = scsi_execute(sdev, scsi_cmd, DMA_FROM_DEVICE, cmd_buf,
  325. CMD_BUFSIZE, NULL, &sshdr, to, CMD_RETRIES,
  326. 0, 0, NULL);
  327. down_read(&cfg->ioctl_rwsem);
  328. rc = check_state(cfg);
  329. if (rc) {
  330. dev_err(dev, "%s: Failed state result=%08x\n",
  331. __func__, result);
  332. rc = -ENODEV;
  333. goto out;
  334. }
  335. if (driver_byte(result) == DRIVER_SENSE) {
  336. result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  337. if (result & SAM_STAT_CHECK_CONDITION) {
  338. switch (sshdr.sense_key) {
  339. case NO_SENSE:
  340. case RECOVERED_ERROR:
  341. /* fall through */
  342. case NOT_READY:
  343. result &= ~SAM_STAT_CHECK_CONDITION;
  344. break;
  345. case UNIT_ATTENTION:
  346. switch (sshdr.asc) {
  347. case 0x29: /* Power on Reset or Device Reset */
  348. /* fall through */
  349. case 0x2A: /* Device capacity changed */
  350. case 0x3F: /* Report LUNs changed */
  351. /* Retry the command once more */
  352. if (retry_cnt++ < 1) {
  353. kfree(cmd_buf);
  354. kfree(scsi_cmd);
  355. goto retry;
  356. }
  357. }
  358. break;
  359. default:
  360. break;
  361. }
  362. }
  363. }
  364. if (result) {
  365. dev_err(dev, "%s: command failed, result=%08x\n",
  366. __func__, result);
  367. rc = -EIO;
  368. goto out;
  369. }
  370. /*
  371. * Read cap was successful, grab values from the buffer;
  372. * note that we don't need to worry about unaligned access
  373. * as the buffer is allocated on an aligned boundary.
  374. */
  375. mutex_lock(&gli->mutex);
  376. gli->max_lba = be64_to_cpu(*((__be64 *)&cmd_buf[0]));
  377. gli->blk_len = be32_to_cpu(*((__be32 *)&cmd_buf[8]));
  378. mutex_unlock(&gli->mutex);
  379. out:
  380. kfree(cmd_buf);
  381. kfree(scsi_cmd);
  382. dev_dbg(dev, "%s: maxlba=%lld blklen=%d rc=%d\n",
  383. __func__, gli->max_lba, gli->blk_len, rc);
  384. return rc;
  385. }
  386. /**
  387. * get_rhte() - obtains validated resource handle table entry reference
  388. * @ctxi: Context owning the resource handle.
  389. * @rhndl: Resource handle associated with entry.
  390. * @lli: LUN associated with request.
  391. *
  392. * Return: Validated RHTE on success, NULL on failure
  393. */
  394. struct sisl_rht_entry *get_rhte(struct ctx_info *ctxi, res_hndl_t rhndl,
  395. struct llun_info *lli)
  396. {
  397. struct cxlflash_cfg *cfg = ctxi->cfg;
  398. struct device *dev = &cfg->dev->dev;
  399. struct sisl_rht_entry *rhte = NULL;
  400. if (unlikely(!ctxi->rht_start)) {
  401. dev_dbg(dev, "%s: Context does not have allocated RHT\n",
  402. __func__);
  403. goto out;
  404. }
  405. if (unlikely(rhndl >= MAX_RHT_PER_CONTEXT)) {
  406. dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
  407. __func__, rhndl);
  408. goto out;
  409. }
  410. if (unlikely(ctxi->rht_lun[rhndl] != lli)) {
  411. dev_dbg(dev, "%s: Bad resource handle LUN rhndl=%d\n",
  412. __func__, rhndl);
  413. goto out;
  414. }
  415. rhte = &ctxi->rht_start[rhndl];
  416. if (unlikely(rhte->nmask == 0)) {
  417. dev_dbg(dev, "%s: Unopened resource handle rhndl=%d\n",
  418. __func__, rhndl);
  419. rhte = NULL;
  420. goto out;
  421. }
  422. out:
  423. return rhte;
  424. }
  425. /**
  426. * rhte_checkout() - obtains free/empty resource handle table entry
  427. * @ctxi: Context owning the resource handle.
  428. * @lli: LUN associated with request.
  429. *
  430. * Return: Free RHTE on success, NULL on failure
  431. */
  432. struct sisl_rht_entry *rhte_checkout(struct ctx_info *ctxi,
  433. struct llun_info *lli)
  434. {
  435. struct cxlflash_cfg *cfg = ctxi->cfg;
  436. struct device *dev = &cfg->dev->dev;
  437. struct sisl_rht_entry *rhte = NULL;
  438. int i;
  439. /* Find a free RHT entry */
  440. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
  441. if (ctxi->rht_start[i].nmask == 0) {
  442. rhte = &ctxi->rht_start[i];
  443. ctxi->rht_out++;
  444. break;
  445. }
  446. if (likely(rhte))
  447. ctxi->rht_lun[i] = lli;
  448. dev_dbg(dev, "%s: returning rhte=%p index=%d\n", __func__, rhte, i);
  449. return rhte;
  450. }
  451. /**
  452. * rhte_checkin() - releases a resource handle table entry
  453. * @ctxi: Context owning the resource handle.
  454. * @rhte: RHTE to release.
  455. */
  456. void rhte_checkin(struct ctx_info *ctxi,
  457. struct sisl_rht_entry *rhte)
  458. {
  459. u32 rsrc_handle = rhte - ctxi->rht_start;
  460. rhte->nmask = 0;
  461. rhte->fp = 0;
  462. ctxi->rht_out--;
  463. ctxi->rht_lun[rsrc_handle] = NULL;
  464. ctxi->rht_needs_ws[rsrc_handle] = false;
  465. }
  466. /**
  467. * rhte_format1() - populates a RHTE for format 1
  468. * @rhte: RHTE to populate.
  469. * @lun_id: LUN ID of LUN associated with RHTE.
  470. * @perm: Desired permissions for RHTE.
  471. * @port_sel: Port selection mask
  472. */
  473. static void rht_format1(struct sisl_rht_entry *rhte, u64 lun_id, u32 perm,
  474. u32 port_sel)
  475. {
  476. /*
  477. * Populate the Format 1 RHT entry for direct access (physical
  478. * LUN) using the synchronization sequence defined in the
  479. * SISLite specification.
  480. */
  481. struct sisl_rht_entry_f1 dummy = { 0 };
  482. struct sisl_rht_entry_f1 *rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
  483. memset(rhte_f1, 0, sizeof(*rhte_f1));
  484. rhte_f1->fp = SISL_RHT_FP(1U, 0);
  485. dma_wmb(); /* Make setting of format bit visible */
  486. rhte_f1->lun_id = lun_id;
  487. dma_wmb(); /* Make setting of LUN id visible */
  488. /*
  489. * Use a dummy RHT Format 1 entry to build the second dword
  490. * of the entry that must be populated in a single write when
  491. * enabled (valid bit set to TRUE).
  492. */
  493. dummy.valid = 0x80;
  494. dummy.fp = SISL_RHT_FP(1U, perm);
  495. dummy.port_sel = port_sel;
  496. rhte_f1->dw = dummy.dw;
  497. dma_wmb(); /* Make remaining RHT entry fields visible */
  498. }
  499. /**
  500. * cxlflash_lun_attach() - attaches a user to a LUN and manages the LUN's mode
  501. * @gli: LUN to attach.
  502. * @mode: Desired mode of the LUN.
  503. * @locked: Mutex status on current thread.
  504. *
  505. * Return: 0 on success, -errno on failure
  506. */
  507. int cxlflash_lun_attach(struct glun_info *gli, enum lun_mode mode, bool locked)
  508. {
  509. int rc = 0;
  510. if (!locked)
  511. mutex_lock(&gli->mutex);
  512. if (gli->mode == MODE_NONE)
  513. gli->mode = mode;
  514. else if (gli->mode != mode) {
  515. pr_debug("%s: gli_mode=%d requested_mode=%d\n",
  516. __func__, gli->mode, mode);
  517. rc = -EINVAL;
  518. goto out;
  519. }
  520. gli->users++;
  521. WARN_ON(gli->users <= 0);
  522. out:
  523. pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
  524. __func__, rc, gli->mode, gli->users);
  525. if (!locked)
  526. mutex_unlock(&gli->mutex);
  527. return rc;
  528. }
  529. /**
  530. * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode
  531. * @gli: LUN to detach.
  532. *
  533. * When resetting the mode, terminate block allocation resources as they
  534. * are no longer required (service is safe to call even when block allocation
  535. * resources were not present - such as when transitioning from physical mode).
  536. * These resources will be reallocated when needed (subsequent transition to
  537. * virtual mode).
  538. */
  539. void cxlflash_lun_detach(struct glun_info *gli)
  540. {
  541. mutex_lock(&gli->mutex);
  542. WARN_ON(gli->mode == MODE_NONE);
  543. if (--gli->users == 0) {
  544. gli->mode = MODE_NONE;
  545. cxlflash_ba_terminate(&gli->blka.ba_lun);
  546. }
  547. pr_debug("%s: gli->users=%u\n", __func__, gli->users);
  548. WARN_ON(gli->users < 0);
  549. mutex_unlock(&gli->mutex);
  550. }
  551. /**
  552. * _cxlflash_disk_release() - releases the specified resource entry
  553. * @sdev: SCSI device associated with LUN.
  554. * @ctxi: Context owning resources.
  555. * @release: Release ioctl data structure.
  556. *
  557. * For LUNs in virtual mode, the virtual LUN associated with the specified
  558. * resource handle is resized to 0 prior to releasing the RHTE. Note that the
  559. * AFU sync should _not_ be performed when the context is sitting on the error
  560. * recovery list. A context on the error recovery list is not known to the AFU
  561. * due to reset. When the context is recovered, it will be reattached and made
  562. * known again to the AFU.
  563. *
  564. * Return: 0 on success, -errno on failure
  565. */
  566. int _cxlflash_disk_release(struct scsi_device *sdev,
  567. struct ctx_info *ctxi,
  568. struct dk_cxlflash_release *release)
  569. {
  570. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  571. struct device *dev = &cfg->dev->dev;
  572. struct llun_info *lli = sdev->hostdata;
  573. struct glun_info *gli = lli->parent;
  574. struct afu *afu = cfg->afu;
  575. bool put_ctx = false;
  576. struct dk_cxlflash_resize size;
  577. res_hndl_t rhndl = release->rsrc_handle;
  578. int rc = 0;
  579. int rcr = 0;
  580. u64 ctxid = DECODE_CTXID(release->context_id),
  581. rctxid = release->context_id;
  582. struct sisl_rht_entry *rhte;
  583. struct sisl_rht_entry_f1 *rhte_f1;
  584. dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n",
  585. __func__, ctxid, release->rsrc_handle, gli->mode, gli->users);
  586. if (!ctxi) {
  587. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  588. if (unlikely(!ctxi)) {
  589. dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
  590. __func__, ctxid);
  591. rc = -EINVAL;
  592. goto out;
  593. }
  594. put_ctx = true;
  595. }
  596. rhte = get_rhte(ctxi, rhndl, lli);
  597. if (unlikely(!rhte)) {
  598. dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
  599. __func__, rhndl);
  600. rc = -EINVAL;
  601. goto out;
  602. }
  603. /*
  604. * Resize to 0 for virtual LUNS by setting the size
  605. * to 0. This will clear LXT_START and LXT_CNT fields
  606. * in the RHT entry and properly sync with the AFU.
  607. *
  608. * Afterwards we clear the remaining fields.
  609. */
  610. switch (gli->mode) {
  611. case MODE_VIRTUAL:
  612. marshal_rele_to_resize(release, &size);
  613. size.req_size = 0;
  614. rc = _cxlflash_vlun_resize(sdev, ctxi, &size);
  615. if (rc) {
  616. dev_dbg(dev, "%s: resize failed rc %d\n", __func__, rc);
  617. goto out;
  618. }
  619. break;
  620. case MODE_PHYSICAL:
  621. /*
  622. * Clear the Format 1 RHT entry for direct access
  623. * (physical LUN) using the synchronization sequence
  624. * defined in the SISLite specification.
  625. */
  626. rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
  627. rhte_f1->valid = 0;
  628. dma_wmb(); /* Make revocation of RHT entry visible */
  629. rhte_f1->lun_id = 0;
  630. dma_wmb(); /* Make clearing of LUN id visible */
  631. rhte_f1->dw = 0;
  632. dma_wmb(); /* Make RHT entry bottom-half clearing visible */
  633. if (!ctxi->err_recovery_active) {
  634. rcr = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
  635. if (unlikely(rcr))
  636. dev_dbg(dev, "%s: AFU sync failed rc=%d\n",
  637. __func__, rcr);
  638. }
  639. break;
  640. default:
  641. WARN(1, "Unsupported LUN mode!");
  642. goto out;
  643. }
  644. rhte_checkin(ctxi, rhte);
  645. cxlflash_lun_detach(gli);
  646. out:
  647. if (put_ctx)
  648. put_context(ctxi);
  649. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  650. return rc;
  651. }
  652. int cxlflash_disk_release(struct scsi_device *sdev,
  653. struct dk_cxlflash_release *release)
  654. {
  655. return _cxlflash_disk_release(sdev, NULL, release);
  656. }
  657. /**
  658. * destroy_context() - releases a context
  659. * @cfg: Internal structure associated with the host.
  660. * @ctxi: Context to release.
  661. *
  662. * This routine is safe to be called with a a non-initialized context.
  663. * Also note that the routine conditionally checks for the existence
  664. * of the context control map before clearing the RHT registers and
  665. * context capabilities because it is possible to destroy a context
  666. * while the context is in the error state (previous mapping was
  667. * removed [so there is no need to worry about clearing] and context
  668. * is waiting for a new mapping).
  669. */
  670. static void destroy_context(struct cxlflash_cfg *cfg,
  671. struct ctx_info *ctxi)
  672. {
  673. struct afu *afu = cfg->afu;
  674. if (ctxi->initialized) {
  675. WARN_ON(!list_empty(&ctxi->luns));
  676. /* Clear RHT registers and drop all capabilities for context */
  677. if (afu->afu_map && ctxi->ctrl_map) {
  678. writeq_be(0, &ctxi->ctrl_map->rht_start);
  679. writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
  680. writeq_be(0, &ctxi->ctrl_map->ctx_cap);
  681. }
  682. }
  683. /* Free memory associated with context */
  684. free_page((ulong)ctxi->rht_start);
  685. kfree(ctxi->rht_needs_ws);
  686. kfree(ctxi->rht_lun);
  687. kfree(ctxi);
  688. }
  689. /**
  690. * create_context() - allocates and initializes a context
  691. * @cfg: Internal structure associated with the host.
  692. *
  693. * Return: Allocated context on success, NULL on failure
  694. */
  695. static struct ctx_info *create_context(struct cxlflash_cfg *cfg)
  696. {
  697. struct device *dev = &cfg->dev->dev;
  698. struct ctx_info *ctxi = NULL;
  699. struct llun_info **lli = NULL;
  700. u8 *ws = NULL;
  701. struct sisl_rht_entry *rhte;
  702. ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL);
  703. lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL);
  704. ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL);
  705. if (unlikely(!ctxi || !lli || !ws)) {
  706. dev_err(dev, "%s: Unable to allocate context\n", __func__);
  707. goto err;
  708. }
  709. rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL);
  710. if (unlikely(!rhte)) {
  711. dev_err(dev, "%s: Unable to allocate RHT\n", __func__);
  712. goto err;
  713. }
  714. ctxi->rht_lun = lli;
  715. ctxi->rht_needs_ws = ws;
  716. ctxi->rht_start = rhte;
  717. out:
  718. return ctxi;
  719. err:
  720. kfree(ws);
  721. kfree(lli);
  722. kfree(ctxi);
  723. ctxi = NULL;
  724. goto out;
  725. }
  726. /**
  727. * init_context() - initializes a previously allocated context
  728. * @ctxi: Previously allocated context
  729. * @cfg: Internal structure associated with the host.
  730. * @ctx: Previously obtained context cookie.
  731. * @ctxid: Previously obtained process element associated with CXL context.
  732. * @file: Previously obtained file associated with CXL context.
  733. * @perms: User-specified permissions.
  734. * @irqs: User-specified number of interrupts.
  735. */
  736. static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
  737. void *ctx, int ctxid, struct file *file, u32 perms,
  738. u64 irqs)
  739. {
  740. struct afu *afu = cfg->afu;
  741. ctxi->rht_perms = perms;
  742. ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
  743. ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
  744. ctxi->irqs = irqs;
  745. ctxi->pid = task_tgid_nr(current); /* tgid = pid */
  746. ctxi->ctx = ctx;
  747. ctxi->cfg = cfg;
  748. ctxi->file = file;
  749. ctxi->initialized = true;
  750. mutex_init(&ctxi->mutex);
  751. kref_init(&ctxi->kref);
  752. INIT_LIST_HEAD(&ctxi->luns);
  753. INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
  754. }
  755. /**
  756. * remove_context() - context kref release handler
  757. * @kref: Kernel reference associated with context to be removed.
  758. *
  759. * When a context no longer has any references it can safely be removed
  760. * from global access and destroyed. Note that it is assumed the thread
  761. * relinquishing access to the context holds its mutex.
  762. */
  763. static void remove_context(struct kref *kref)
  764. {
  765. struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
  766. struct cxlflash_cfg *cfg = ctxi->cfg;
  767. u64 ctxid = DECODE_CTXID(ctxi->ctxid);
  768. /* Remove context from table/error list */
  769. WARN_ON(!mutex_is_locked(&ctxi->mutex));
  770. ctxi->unavail = true;
  771. mutex_unlock(&ctxi->mutex);
  772. mutex_lock(&cfg->ctx_tbl_list_mutex);
  773. mutex_lock(&ctxi->mutex);
  774. if (!list_empty(&ctxi->list))
  775. list_del(&ctxi->list);
  776. cfg->ctx_tbl[ctxid] = NULL;
  777. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  778. mutex_unlock(&ctxi->mutex);
  779. /* Context now completely uncoupled/unreachable */
  780. destroy_context(cfg, ctxi);
  781. }
  782. /**
  783. * _cxlflash_disk_detach() - detaches a LUN from a context
  784. * @sdev: SCSI device associated with LUN.
  785. * @ctxi: Context owning resources.
  786. * @detach: Detach ioctl data structure.
  787. *
  788. * As part of the detach, all per-context resources associated with the LUN
  789. * are cleaned up. When detaching the last LUN for a context, the context
  790. * itself is cleaned up and released.
  791. *
  792. * Return: 0 on success, -errno on failure
  793. */
  794. static int _cxlflash_disk_detach(struct scsi_device *sdev,
  795. struct ctx_info *ctxi,
  796. struct dk_cxlflash_detach *detach)
  797. {
  798. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  799. struct device *dev = &cfg->dev->dev;
  800. struct llun_info *lli = sdev->hostdata;
  801. struct lun_access *lun_access, *t;
  802. struct dk_cxlflash_release rel;
  803. bool put_ctx = false;
  804. int i;
  805. int rc = 0;
  806. u64 ctxid = DECODE_CTXID(detach->context_id),
  807. rctxid = detach->context_id;
  808. dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
  809. if (!ctxi) {
  810. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  811. if (unlikely(!ctxi)) {
  812. dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
  813. __func__, ctxid);
  814. rc = -EINVAL;
  815. goto out;
  816. }
  817. put_ctx = true;
  818. }
  819. /* Cleanup outstanding resources tied to this LUN */
  820. if (ctxi->rht_out) {
  821. marshal_det_to_rele(detach, &rel);
  822. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
  823. if (ctxi->rht_lun[i] == lli) {
  824. rel.rsrc_handle = i;
  825. _cxlflash_disk_release(sdev, ctxi, &rel);
  826. }
  827. /* No need to loop further if we're done */
  828. if (ctxi->rht_out == 0)
  829. break;
  830. }
  831. }
  832. /* Take our LUN out of context, free the node */
  833. list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
  834. if (lun_access->lli == lli) {
  835. list_del(&lun_access->list);
  836. kfree(lun_access);
  837. lun_access = NULL;
  838. break;
  839. }
  840. /*
  841. * Release the context reference and the sdev reference that
  842. * bound this LUN to the context.
  843. */
  844. if (kref_put(&ctxi->kref, remove_context))
  845. put_ctx = false;
  846. scsi_device_put(sdev);
  847. out:
  848. if (put_ctx)
  849. put_context(ctxi);
  850. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  851. return rc;
  852. }
  853. static int cxlflash_disk_detach(struct scsi_device *sdev,
  854. struct dk_cxlflash_detach *detach)
  855. {
  856. return _cxlflash_disk_detach(sdev, NULL, detach);
  857. }
  858. /**
  859. * cxlflash_cxl_release() - release handler for adapter file descriptor
  860. * @inode: File-system inode associated with fd.
  861. * @file: File installed with adapter file descriptor.
  862. *
  863. * This routine is the release handler for the fops registered with
  864. * the CXL services on an initial attach for a context. It is called
  865. * when a close (explicity by the user or as part of a process tear
  866. * down) is performed on the adapter file descriptor returned to the
  867. * user. The user should be aware that explicitly performing a close
  868. * considered catastrophic and subsequent usage of the superpipe API
  869. * with previously saved off tokens will fail.
  870. *
  871. * This routine derives the context reference and calls detach for
  872. * each LUN associated with the context.The final detach operation
  873. * causes the context itself to be freed. With exception to when the
  874. * CXL process element (context id) lookup fails (a case that should
  875. * theoretically never occur), every call into this routine results
  876. * in a complete freeing of a context.
  877. *
  878. * Detaching the LUN is typically an ioctl() operation and the underlying
  879. * code assumes that ioctl_rwsem has been acquired as a reader. To support
  880. * that design point, the semaphore is acquired and released around detach.
  881. *
  882. * Return: 0 on success
  883. */
  884. static int cxlflash_cxl_release(struct inode *inode, struct file *file)
  885. {
  886. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  887. cxl_fops);
  888. void *ctx = cfg->ops->fops_get_context(file);
  889. struct device *dev = &cfg->dev->dev;
  890. struct ctx_info *ctxi = NULL;
  891. struct dk_cxlflash_detach detach = { { 0 }, 0 };
  892. struct lun_access *lun_access, *t;
  893. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  894. int ctxid;
  895. ctxid = cfg->ops->process_element(ctx);
  896. if (unlikely(ctxid < 0)) {
  897. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  898. __func__, ctx, ctxid);
  899. goto out;
  900. }
  901. ctxi = get_context(cfg, ctxid, file, ctrl);
  902. if (unlikely(!ctxi)) {
  903. ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
  904. if (!ctxi) {
  905. dev_dbg(dev, "%s: ctxid=%d already free\n",
  906. __func__, ctxid);
  907. goto out_release;
  908. }
  909. dev_dbg(dev, "%s: Another process owns ctxid=%d\n",
  910. __func__, ctxid);
  911. put_context(ctxi);
  912. goto out;
  913. }
  914. dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid);
  915. down_read(&cfg->ioctl_rwsem);
  916. detach.context_id = ctxi->ctxid;
  917. list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
  918. _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
  919. up_read(&cfg->ioctl_rwsem);
  920. out_release:
  921. cfg->ops->fd_release(inode, file);
  922. out:
  923. dev_dbg(dev, "%s: returning\n", __func__);
  924. return 0;
  925. }
  926. /**
  927. * unmap_context() - clears a previously established mapping
  928. * @ctxi: Context owning the mapping.
  929. *
  930. * This routine is used to switch between the error notification page
  931. * (dummy page of all 1's) and the real mapping (established by the CXL
  932. * fault handler).
  933. */
  934. static void unmap_context(struct ctx_info *ctxi)
  935. {
  936. unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
  937. }
  938. /**
  939. * get_err_page() - obtains and allocates the error notification page
  940. * @cfg: Internal structure associated with the host.
  941. *
  942. * Return: error notification page on success, NULL on failure
  943. */
  944. static struct page *get_err_page(struct cxlflash_cfg *cfg)
  945. {
  946. struct page *err_page = global.err_page;
  947. struct device *dev = &cfg->dev->dev;
  948. if (unlikely(!err_page)) {
  949. err_page = alloc_page(GFP_KERNEL);
  950. if (unlikely(!err_page)) {
  951. dev_err(dev, "%s: Unable to allocate err_page\n",
  952. __func__);
  953. goto out;
  954. }
  955. memset(page_address(err_page), -1, PAGE_SIZE);
  956. /* Serialize update w/ other threads to avoid a leak */
  957. mutex_lock(&global.mutex);
  958. if (likely(!global.err_page))
  959. global.err_page = err_page;
  960. else {
  961. __free_page(err_page);
  962. err_page = global.err_page;
  963. }
  964. mutex_unlock(&global.mutex);
  965. }
  966. out:
  967. dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page);
  968. return err_page;
  969. }
  970. /**
  971. * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
  972. * @vmf: VM fault associated with current fault.
  973. *
  974. * To support error notification via MMIO, faults are 'caught' by this routine
  975. * that was inserted before passing back the adapter file descriptor on attach.
  976. * When a fault occurs, this routine evaluates if error recovery is active and
  977. * if so, installs the error page to 'notify' the user about the error state.
  978. * During normal operation, the fault is simply handled by the original fault
  979. * handler that was installed by CXL services as part of initializing the
  980. * adapter file descriptor. The VMA's page protection bits are toggled to
  981. * indicate cached/not-cached depending on the memory backing the fault.
  982. *
  983. * Return: 0 on success, VM_FAULT_SIGBUS on failure
  984. */
  985. static vm_fault_t cxlflash_mmap_fault(struct vm_fault *vmf)
  986. {
  987. struct vm_area_struct *vma = vmf->vma;
  988. struct file *file = vma->vm_file;
  989. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  990. cxl_fops);
  991. void *ctx = cfg->ops->fops_get_context(file);
  992. struct device *dev = &cfg->dev->dev;
  993. struct ctx_info *ctxi = NULL;
  994. struct page *err_page = NULL;
  995. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  996. vm_fault_t rc = 0;
  997. int ctxid;
  998. ctxid = cfg->ops->process_element(ctx);
  999. if (unlikely(ctxid < 0)) {
  1000. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  1001. __func__, ctx, ctxid);
  1002. goto err;
  1003. }
  1004. ctxi = get_context(cfg, ctxid, file, ctrl);
  1005. if (unlikely(!ctxi)) {
  1006. dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
  1007. goto err;
  1008. }
  1009. dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
  1010. if (likely(!ctxi->err_recovery_active)) {
  1011. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1012. rc = ctxi->cxl_mmap_vmops->fault(vmf);
  1013. } else {
  1014. dev_dbg(dev, "%s: err recovery active, use err_page\n",
  1015. __func__);
  1016. err_page = get_err_page(cfg);
  1017. if (unlikely(!err_page)) {
  1018. dev_err(dev, "%s: Could not get err_page\n", __func__);
  1019. rc = VM_FAULT_RETRY;
  1020. goto out;
  1021. }
  1022. get_page(err_page);
  1023. vmf->page = err_page;
  1024. vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
  1025. }
  1026. out:
  1027. if (likely(ctxi))
  1028. put_context(ctxi);
  1029. dev_dbg(dev, "%s: returning rc=%x\n", __func__, rc);
  1030. return rc;
  1031. err:
  1032. rc = VM_FAULT_SIGBUS;
  1033. goto out;
  1034. }
  1035. /*
  1036. * Local MMAP vmops to 'catch' faults
  1037. */
  1038. static const struct vm_operations_struct cxlflash_mmap_vmops = {
  1039. .fault = cxlflash_mmap_fault,
  1040. };
  1041. /**
  1042. * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
  1043. * @file: File installed with adapter file descriptor.
  1044. * @vma: VM area associated with mapping.
  1045. *
  1046. * Installs local mmap vmops to 'catch' faults for error notification support.
  1047. *
  1048. * Return: 0 on success, -errno on failure
  1049. */
  1050. static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
  1051. {
  1052. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  1053. cxl_fops);
  1054. void *ctx = cfg->ops->fops_get_context(file);
  1055. struct device *dev = &cfg->dev->dev;
  1056. struct ctx_info *ctxi = NULL;
  1057. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  1058. int ctxid;
  1059. int rc = 0;
  1060. ctxid = cfg->ops->process_element(ctx);
  1061. if (unlikely(ctxid < 0)) {
  1062. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  1063. __func__, ctx, ctxid);
  1064. rc = -EIO;
  1065. goto out;
  1066. }
  1067. ctxi = get_context(cfg, ctxid, file, ctrl);
  1068. if (unlikely(!ctxi)) {
  1069. dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
  1070. rc = -EIO;
  1071. goto out;
  1072. }
  1073. dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
  1074. rc = cfg->ops->fd_mmap(file, vma);
  1075. if (likely(!rc)) {
  1076. /* Insert ourself in the mmap fault handler path */
  1077. ctxi->cxl_mmap_vmops = vma->vm_ops;
  1078. vma->vm_ops = &cxlflash_mmap_vmops;
  1079. }
  1080. out:
  1081. if (likely(ctxi))
  1082. put_context(ctxi);
  1083. return rc;
  1084. }
  1085. const struct file_operations cxlflash_cxl_fops = {
  1086. .owner = THIS_MODULE,
  1087. .mmap = cxlflash_cxl_mmap,
  1088. .release = cxlflash_cxl_release,
  1089. };
  1090. /**
  1091. * cxlflash_mark_contexts_error() - move contexts to error state and list
  1092. * @cfg: Internal structure associated with the host.
  1093. *
  1094. * A context is only moved over to the error list when there are no outstanding
  1095. * references to it. This ensures that a running operation has completed.
  1096. *
  1097. * Return: 0 on success, -errno on failure
  1098. */
  1099. int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
  1100. {
  1101. int i, rc = 0;
  1102. struct ctx_info *ctxi = NULL;
  1103. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1104. for (i = 0; i < MAX_CONTEXT; i++) {
  1105. ctxi = cfg->ctx_tbl[i];
  1106. if (ctxi) {
  1107. mutex_lock(&ctxi->mutex);
  1108. cfg->ctx_tbl[i] = NULL;
  1109. list_add(&ctxi->list, &cfg->ctx_err_recovery);
  1110. ctxi->err_recovery_active = true;
  1111. ctxi->ctrl_map = NULL;
  1112. unmap_context(ctxi);
  1113. mutex_unlock(&ctxi->mutex);
  1114. }
  1115. }
  1116. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1117. return rc;
  1118. }
  1119. /*
  1120. * Dummy NULL fops
  1121. */
  1122. static const struct file_operations null_fops = {
  1123. .owner = THIS_MODULE,
  1124. };
  1125. /**
  1126. * check_state() - checks and responds to the current adapter state
  1127. * @cfg: Internal structure associated with the host.
  1128. *
  1129. * This routine can block and should only be used on process context.
  1130. * It assumes that the caller is an ioctl thread and holding the ioctl
  1131. * read semaphore. This is temporarily let up across the wait to allow
  1132. * for draining actively running ioctls. Also note that when waking up
  1133. * from waiting in reset, the state is unknown and must be checked again
  1134. * before proceeding.
  1135. *
  1136. * Return: 0 on success, -errno on failure
  1137. */
  1138. int check_state(struct cxlflash_cfg *cfg)
  1139. {
  1140. struct device *dev = &cfg->dev->dev;
  1141. int rc = 0;
  1142. retry:
  1143. switch (cfg->state) {
  1144. case STATE_RESET:
  1145. dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
  1146. up_read(&cfg->ioctl_rwsem);
  1147. rc = wait_event_interruptible(cfg->reset_waitq,
  1148. cfg->state != STATE_RESET);
  1149. down_read(&cfg->ioctl_rwsem);
  1150. if (unlikely(rc))
  1151. break;
  1152. goto retry;
  1153. case STATE_FAILTERM:
  1154. dev_dbg(dev, "%s: Failed/Terminating\n", __func__);
  1155. rc = -ENODEV;
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. return rc;
  1161. }
  1162. /**
  1163. * cxlflash_disk_attach() - attach a LUN to a context
  1164. * @sdev: SCSI device associated with LUN.
  1165. * @attach: Attach ioctl data structure.
  1166. *
  1167. * Creates a context and attaches LUN to it. A LUN can only be attached
  1168. * one time to a context (subsequent attaches for the same context/LUN pair
  1169. * are not supported). Additional LUNs can be attached to a context by
  1170. * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
  1171. *
  1172. * Return: 0 on success, -errno on failure
  1173. */
  1174. static int cxlflash_disk_attach(struct scsi_device *sdev,
  1175. struct dk_cxlflash_attach *attach)
  1176. {
  1177. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1178. struct device *dev = &cfg->dev->dev;
  1179. struct afu *afu = cfg->afu;
  1180. struct llun_info *lli = sdev->hostdata;
  1181. struct glun_info *gli = lli->parent;
  1182. struct ctx_info *ctxi = NULL;
  1183. struct lun_access *lun_access = NULL;
  1184. int rc = 0;
  1185. u32 perms;
  1186. int ctxid = -1;
  1187. u64 irqs = attach->num_interrupts;
  1188. u64 flags = 0UL;
  1189. u64 rctxid = 0UL;
  1190. struct file *file = NULL;
  1191. void *ctx = NULL;
  1192. int fd = -1;
  1193. if (irqs > 4) {
  1194. dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
  1195. __func__, irqs);
  1196. rc = -EINVAL;
  1197. goto out;
  1198. }
  1199. if (gli->max_lba == 0) {
  1200. dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n",
  1201. __func__, lli->lun_id[sdev->channel]);
  1202. rc = read_cap16(sdev, lli);
  1203. if (rc) {
  1204. dev_err(dev, "%s: Invalid device rc=%d\n",
  1205. __func__, rc);
  1206. rc = -ENODEV;
  1207. goto out;
  1208. }
  1209. dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba);
  1210. dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len);
  1211. }
  1212. if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
  1213. rctxid = attach->context_id;
  1214. ctxi = get_context(cfg, rctxid, NULL, 0);
  1215. if (!ctxi) {
  1216. dev_dbg(dev, "%s: Bad context rctxid=%016llx\n",
  1217. __func__, rctxid);
  1218. rc = -EINVAL;
  1219. goto out;
  1220. }
  1221. list_for_each_entry(lun_access, &ctxi->luns, list)
  1222. if (lun_access->lli == lli) {
  1223. dev_dbg(dev, "%s: Already attached\n",
  1224. __func__);
  1225. rc = -EINVAL;
  1226. goto out;
  1227. }
  1228. }
  1229. rc = scsi_device_get(sdev);
  1230. if (unlikely(rc)) {
  1231. dev_err(dev, "%s: Unable to get sdev reference\n", __func__);
  1232. goto out;
  1233. }
  1234. lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
  1235. if (unlikely(!lun_access)) {
  1236. dev_err(dev, "%s: Unable to allocate lun_access\n", __func__);
  1237. rc = -ENOMEM;
  1238. goto err;
  1239. }
  1240. lun_access->lli = lli;
  1241. lun_access->sdev = sdev;
  1242. /* Non-NULL context indicates reuse (another context reference) */
  1243. if (ctxi) {
  1244. dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n",
  1245. __func__, rctxid);
  1246. kref_get(&ctxi->kref);
  1247. list_add(&lun_access->list, &ctxi->luns);
  1248. goto out_attach;
  1249. }
  1250. ctxi = create_context(cfg);
  1251. if (unlikely(!ctxi)) {
  1252. dev_err(dev, "%s: Failed to create context ctxid=%d\n",
  1253. __func__, ctxid);
  1254. rc = -ENOMEM;
  1255. goto err;
  1256. }
  1257. ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
  1258. if (IS_ERR_OR_NULL(ctx)) {
  1259. dev_err(dev, "%s: Could not initialize context %p\n",
  1260. __func__, ctx);
  1261. rc = -ENODEV;
  1262. goto err;
  1263. }
  1264. rc = cfg->ops->start_work(ctx, irqs);
  1265. if (unlikely(rc)) {
  1266. dev_dbg(dev, "%s: Could not start context rc=%d\n",
  1267. __func__, rc);
  1268. goto err;
  1269. }
  1270. ctxid = cfg->ops->process_element(ctx);
  1271. if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
  1272. dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
  1273. rc = -EPERM;
  1274. goto err;
  1275. }
  1276. file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
  1277. if (unlikely(fd < 0)) {
  1278. rc = -ENODEV;
  1279. dev_err(dev, "%s: Could not get file descriptor\n", __func__);
  1280. goto err;
  1281. }
  1282. /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
  1283. perms = SISL_RHT_PERM(attach->hdr.flags + 1);
  1284. /* Context mutex is locked upon return */
  1285. init_context(ctxi, cfg, ctx, ctxid, file, perms, irqs);
  1286. rc = afu_attach(cfg, ctxi);
  1287. if (unlikely(rc)) {
  1288. dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
  1289. goto err;
  1290. }
  1291. /*
  1292. * No error paths after this point. Once the fd is installed it's
  1293. * visible to user space and can't be undone safely on this thread.
  1294. * There is no need to worry about a deadlock here because no one
  1295. * knows about us yet; we can be the only one holding our mutex.
  1296. */
  1297. list_add(&lun_access->list, &ctxi->luns);
  1298. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1299. mutex_lock(&ctxi->mutex);
  1300. cfg->ctx_tbl[ctxid] = ctxi;
  1301. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1302. fd_install(fd, file);
  1303. out_attach:
  1304. if (fd != -1)
  1305. flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD;
  1306. if (afu_is_sq_cmd_mode(afu))
  1307. flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
  1308. attach->hdr.return_flags = flags;
  1309. attach->context_id = ctxi->ctxid;
  1310. attach->block_size = gli->blk_len;
  1311. attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
  1312. attach->last_lba = gli->max_lba;
  1313. attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
  1314. attach->max_xfer /= gli->blk_len;
  1315. out:
  1316. attach->adap_fd = fd;
  1317. if (ctxi)
  1318. put_context(ctxi);
  1319. dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
  1320. __func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
  1321. return rc;
  1322. err:
  1323. /* Cleanup CXL context; okay to 'stop' even if it was not started */
  1324. if (!IS_ERR_OR_NULL(ctx)) {
  1325. cfg->ops->stop_context(ctx);
  1326. cfg->ops->release_context(ctx);
  1327. ctx = NULL;
  1328. }
  1329. /*
  1330. * Here, we're overriding the fops with a dummy all-NULL fops because
  1331. * fput() calls the release fop, which will cause us to mistakenly
  1332. * call into the CXL code. Rather than try to add yet more complexity
  1333. * to that routine (cxlflash_cxl_release) we should try to fix the
  1334. * issue here.
  1335. */
  1336. if (fd > 0) {
  1337. file->f_op = &null_fops;
  1338. fput(file);
  1339. put_unused_fd(fd);
  1340. fd = -1;
  1341. file = NULL;
  1342. }
  1343. /* Cleanup our context */
  1344. if (ctxi) {
  1345. destroy_context(cfg, ctxi);
  1346. ctxi = NULL;
  1347. }
  1348. kfree(lun_access);
  1349. scsi_device_put(sdev);
  1350. goto out;
  1351. }
  1352. /**
  1353. * recover_context() - recovers a context in error
  1354. * @cfg: Internal structure associated with the host.
  1355. * @ctxi: Context to release.
  1356. * @adap_fd: Adapter file descriptor associated with new/recovered context.
  1357. *
  1358. * Restablishes the state for a context-in-error.
  1359. *
  1360. * Return: 0 on success, -errno on failure
  1361. */
  1362. static int recover_context(struct cxlflash_cfg *cfg,
  1363. struct ctx_info *ctxi,
  1364. int *adap_fd)
  1365. {
  1366. struct device *dev = &cfg->dev->dev;
  1367. int rc = 0;
  1368. int fd = -1;
  1369. int ctxid = -1;
  1370. struct file *file;
  1371. void *ctx;
  1372. struct afu *afu = cfg->afu;
  1373. ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
  1374. if (IS_ERR_OR_NULL(ctx)) {
  1375. dev_err(dev, "%s: Could not initialize context %p\n",
  1376. __func__, ctx);
  1377. rc = -ENODEV;
  1378. goto out;
  1379. }
  1380. rc = cfg->ops->start_work(ctx, ctxi->irqs);
  1381. if (unlikely(rc)) {
  1382. dev_dbg(dev, "%s: Could not start context rc=%d\n",
  1383. __func__, rc);
  1384. goto err1;
  1385. }
  1386. ctxid = cfg->ops->process_element(ctx);
  1387. if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
  1388. dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
  1389. rc = -EPERM;
  1390. goto err2;
  1391. }
  1392. file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
  1393. if (unlikely(fd < 0)) {
  1394. rc = -ENODEV;
  1395. dev_err(dev, "%s: Could not get file descriptor\n", __func__);
  1396. goto err2;
  1397. }
  1398. /* Update with new MMIO area based on updated context id */
  1399. ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
  1400. rc = afu_attach(cfg, ctxi);
  1401. if (rc) {
  1402. dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
  1403. goto err3;
  1404. }
  1405. /*
  1406. * No error paths after this point. Once the fd is installed it's
  1407. * visible to user space and can't be undone safely on this thread.
  1408. */
  1409. ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
  1410. ctxi->ctx = ctx;
  1411. ctxi->file = file;
  1412. /*
  1413. * Put context back in table (note the reinit of the context list);
  1414. * we must first drop the context's mutex and then acquire it in
  1415. * order with the table/list mutex to avoid a deadlock - safe to do
  1416. * here because no one can find us at this moment in time.
  1417. */
  1418. mutex_unlock(&ctxi->mutex);
  1419. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1420. mutex_lock(&ctxi->mutex);
  1421. list_del_init(&ctxi->list);
  1422. cfg->ctx_tbl[ctxid] = ctxi;
  1423. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1424. fd_install(fd, file);
  1425. *adap_fd = fd;
  1426. out:
  1427. dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
  1428. __func__, ctxid, fd, rc);
  1429. return rc;
  1430. err3:
  1431. fput(file);
  1432. put_unused_fd(fd);
  1433. err2:
  1434. cfg->ops->stop_context(ctx);
  1435. err1:
  1436. cfg->ops->release_context(ctx);
  1437. goto out;
  1438. }
  1439. /**
  1440. * cxlflash_afu_recover() - initiates AFU recovery
  1441. * @sdev: SCSI device associated with LUN.
  1442. * @recover: Recover ioctl data structure.
  1443. *
  1444. * Only a single recovery is allowed at a time to avoid exhausting CXL
  1445. * resources (leading to recovery failure) in the event that we're up
  1446. * against the maximum number of contexts limit. For similar reasons,
  1447. * a context recovery is retried if there are multiple recoveries taking
  1448. * place at the same time and the failure was due to CXL services being
  1449. * unable to keep up.
  1450. *
  1451. * As this routine is called on ioctl context, it holds the ioctl r/w
  1452. * semaphore that is used to drain ioctls in recovery scenarios. The
  1453. * implementation to achieve the pacing described above (a local mutex)
  1454. * requires that the ioctl r/w semaphore be dropped and reacquired to
  1455. * avoid a 3-way deadlock when multiple process recoveries operate in
  1456. * parallel.
  1457. *
  1458. * Because a user can detect an error condition before the kernel, it is
  1459. * quite possible for this routine to act as the kernel's EEH detection
  1460. * source (MMIO read of mbox_r). Because of this, there is a window of
  1461. * time where an EEH might have been detected but not yet 'serviced'
  1462. * (callback invoked, causing the device to enter reset state). To avoid
  1463. * looping in this routine during that window, a 1 second sleep is in place
  1464. * between the time the MMIO failure is detected and the time a wait on the
  1465. * reset wait queue is attempted via check_state().
  1466. *
  1467. * Return: 0 on success, -errno on failure
  1468. */
  1469. static int cxlflash_afu_recover(struct scsi_device *sdev,
  1470. struct dk_cxlflash_recover_afu *recover)
  1471. {
  1472. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1473. struct device *dev = &cfg->dev->dev;
  1474. struct llun_info *lli = sdev->hostdata;
  1475. struct afu *afu = cfg->afu;
  1476. struct ctx_info *ctxi = NULL;
  1477. struct mutex *mutex = &cfg->ctx_recovery_mutex;
  1478. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  1479. u64 flags;
  1480. u64 ctxid = DECODE_CTXID(recover->context_id),
  1481. rctxid = recover->context_id;
  1482. long reg;
  1483. bool locked = true;
  1484. int lretry = 20; /* up to 2 seconds */
  1485. int new_adap_fd = -1;
  1486. int rc = 0;
  1487. atomic_inc(&cfg->recovery_threads);
  1488. up_read(&cfg->ioctl_rwsem);
  1489. rc = mutex_lock_interruptible(mutex);
  1490. down_read(&cfg->ioctl_rwsem);
  1491. if (rc) {
  1492. locked = false;
  1493. goto out;
  1494. }
  1495. rc = check_state(cfg);
  1496. if (rc) {
  1497. dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc);
  1498. rc = -ENODEV;
  1499. goto out;
  1500. }
  1501. dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n",
  1502. __func__, recover->reason, rctxid);
  1503. retry:
  1504. /* Ensure that this process is attached to the context */
  1505. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  1506. if (unlikely(!ctxi)) {
  1507. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1508. rc = -EINVAL;
  1509. goto out;
  1510. }
  1511. if (ctxi->err_recovery_active) {
  1512. retry_recover:
  1513. rc = recover_context(cfg, ctxi, &new_adap_fd);
  1514. if (unlikely(rc)) {
  1515. dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n",
  1516. __func__, ctxid, rc);
  1517. if ((rc == -ENODEV) &&
  1518. ((atomic_read(&cfg->recovery_threads) > 1) ||
  1519. (lretry--))) {
  1520. dev_dbg(dev, "%s: Going to try again\n",
  1521. __func__);
  1522. mutex_unlock(mutex);
  1523. msleep(100);
  1524. rc = mutex_lock_interruptible(mutex);
  1525. if (rc) {
  1526. locked = false;
  1527. goto out;
  1528. }
  1529. goto retry_recover;
  1530. }
  1531. goto out;
  1532. }
  1533. ctxi->err_recovery_active = false;
  1534. flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
  1535. DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
  1536. if (afu_is_sq_cmd_mode(afu))
  1537. flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
  1538. recover->hdr.return_flags = flags;
  1539. recover->context_id = ctxi->ctxid;
  1540. recover->adap_fd = new_adap_fd;
  1541. recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
  1542. goto out;
  1543. }
  1544. /* Test if in error state */
  1545. reg = readq_be(&hwq->ctrl_map->mbox_r);
  1546. if (reg == -1) {
  1547. dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
  1548. /*
  1549. * Before checking the state, put back the context obtained with
  1550. * get_context() as it is no longer needed and sleep for a short
  1551. * period of time (see prolog notes).
  1552. */
  1553. put_context(ctxi);
  1554. ctxi = NULL;
  1555. ssleep(1);
  1556. rc = check_state(cfg);
  1557. if (unlikely(rc))
  1558. goto out;
  1559. goto retry;
  1560. }
  1561. dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__);
  1562. out:
  1563. if (likely(ctxi))
  1564. put_context(ctxi);
  1565. if (locked)
  1566. mutex_unlock(mutex);
  1567. atomic_dec_if_positive(&cfg->recovery_threads);
  1568. return rc;
  1569. }
  1570. /**
  1571. * process_sense() - evaluates and processes sense data
  1572. * @sdev: SCSI device associated with LUN.
  1573. * @verify: Verify ioctl data structure.
  1574. *
  1575. * Return: 0 on success, -errno on failure
  1576. */
  1577. static int process_sense(struct scsi_device *sdev,
  1578. struct dk_cxlflash_verify *verify)
  1579. {
  1580. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1581. struct device *dev = &cfg->dev->dev;
  1582. struct llun_info *lli = sdev->hostdata;
  1583. struct glun_info *gli = lli->parent;
  1584. u64 prev_lba = gli->max_lba;
  1585. struct scsi_sense_hdr sshdr = { 0 };
  1586. int rc = 0;
  1587. rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
  1588. DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
  1589. if (!rc) {
  1590. dev_err(dev, "%s: Failed to normalize sense data\n", __func__);
  1591. rc = -EINVAL;
  1592. goto out;
  1593. }
  1594. switch (sshdr.sense_key) {
  1595. case NO_SENSE:
  1596. case RECOVERED_ERROR:
  1597. /* fall through */
  1598. case NOT_READY:
  1599. break;
  1600. case UNIT_ATTENTION:
  1601. switch (sshdr.asc) {
  1602. case 0x29: /* Power on Reset or Device Reset */
  1603. /* fall through */
  1604. case 0x2A: /* Device settings/capacity changed */
  1605. rc = read_cap16(sdev, lli);
  1606. if (rc) {
  1607. rc = -ENODEV;
  1608. break;
  1609. }
  1610. if (prev_lba != gli->max_lba)
  1611. dev_dbg(dev, "%s: Capacity changed old=%lld "
  1612. "new=%lld\n", __func__, prev_lba,
  1613. gli->max_lba);
  1614. break;
  1615. case 0x3F: /* Report LUNs changed, Rescan. */
  1616. scsi_scan_host(cfg->host);
  1617. break;
  1618. default:
  1619. rc = -EIO;
  1620. break;
  1621. }
  1622. break;
  1623. default:
  1624. rc = -EIO;
  1625. break;
  1626. }
  1627. out:
  1628. dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
  1629. sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
  1630. return rc;
  1631. }
  1632. /**
  1633. * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
  1634. * @sdev: SCSI device associated with LUN.
  1635. * @verify: Verify ioctl data structure.
  1636. *
  1637. * Return: 0 on success, -errno on failure
  1638. */
  1639. static int cxlflash_disk_verify(struct scsi_device *sdev,
  1640. struct dk_cxlflash_verify *verify)
  1641. {
  1642. int rc = 0;
  1643. struct ctx_info *ctxi = NULL;
  1644. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1645. struct device *dev = &cfg->dev->dev;
  1646. struct llun_info *lli = sdev->hostdata;
  1647. struct glun_info *gli = lli->parent;
  1648. struct sisl_rht_entry *rhte = NULL;
  1649. res_hndl_t rhndl = verify->rsrc_handle;
  1650. u64 ctxid = DECODE_CTXID(verify->context_id),
  1651. rctxid = verify->context_id;
  1652. u64 last_lba = 0;
  1653. dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
  1654. "flags=%016llx\n", __func__, ctxid, verify->rsrc_handle,
  1655. verify->hint, verify->hdr.flags);
  1656. ctxi = get_context(cfg, rctxid, lli, 0);
  1657. if (unlikely(!ctxi)) {
  1658. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1659. rc = -EINVAL;
  1660. goto out;
  1661. }
  1662. rhte = get_rhte(ctxi, rhndl, lli);
  1663. if (unlikely(!rhte)) {
  1664. dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
  1665. __func__, rhndl);
  1666. rc = -EINVAL;
  1667. goto out;
  1668. }
  1669. /*
  1670. * Look at the hint/sense to see if it requires us to redrive
  1671. * inquiry (i.e. the Unit attention is due to the WWN changing).
  1672. */
  1673. if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
  1674. /* Can't hold mutex across process_sense/read_cap16,
  1675. * since we could have an intervening EEH event.
  1676. */
  1677. ctxi->unavail = true;
  1678. mutex_unlock(&ctxi->mutex);
  1679. rc = process_sense(sdev, verify);
  1680. if (unlikely(rc)) {
  1681. dev_err(dev, "%s: Failed to validate sense data (%d)\n",
  1682. __func__, rc);
  1683. mutex_lock(&ctxi->mutex);
  1684. ctxi->unavail = false;
  1685. goto out;
  1686. }
  1687. mutex_lock(&ctxi->mutex);
  1688. ctxi->unavail = false;
  1689. }
  1690. switch (gli->mode) {
  1691. case MODE_PHYSICAL:
  1692. last_lba = gli->max_lba;
  1693. break;
  1694. case MODE_VIRTUAL:
  1695. /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
  1696. last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
  1697. last_lba /= CXLFLASH_BLOCK_SIZE;
  1698. last_lba--;
  1699. break;
  1700. default:
  1701. WARN(1, "Unsupported LUN mode!");
  1702. }
  1703. verify->last_lba = last_lba;
  1704. out:
  1705. if (likely(ctxi))
  1706. put_context(ctxi);
  1707. dev_dbg(dev, "%s: returning rc=%d llba=%llx\n",
  1708. __func__, rc, verify->last_lba);
  1709. return rc;
  1710. }
  1711. /**
  1712. * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
  1713. * @cmd: The ioctl command to decode.
  1714. *
  1715. * Return: A string identifying the decoded ioctl.
  1716. */
  1717. static char *decode_ioctl(int cmd)
  1718. {
  1719. switch (cmd) {
  1720. case DK_CXLFLASH_ATTACH:
  1721. return __stringify_1(DK_CXLFLASH_ATTACH);
  1722. case DK_CXLFLASH_USER_DIRECT:
  1723. return __stringify_1(DK_CXLFLASH_USER_DIRECT);
  1724. case DK_CXLFLASH_USER_VIRTUAL:
  1725. return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
  1726. case DK_CXLFLASH_VLUN_RESIZE:
  1727. return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
  1728. case DK_CXLFLASH_RELEASE:
  1729. return __stringify_1(DK_CXLFLASH_RELEASE);
  1730. case DK_CXLFLASH_DETACH:
  1731. return __stringify_1(DK_CXLFLASH_DETACH);
  1732. case DK_CXLFLASH_VERIFY:
  1733. return __stringify_1(DK_CXLFLASH_VERIFY);
  1734. case DK_CXLFLASH_VLUN_CLONE:
  1735. return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
  1736. case DK_CXLFLASH_RECOVER_AFU:
  1737. return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
  1738. case DK_CXLFLASH_MANAGE_LUN:
  1739. return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
  1740. }
  1741. return "UNKNOWN";
  1742. }
  1743. /**
  1744. * cxlflash_disk_direct_open() - opens a direct (physical) disk
  1745. * @sdev: SCSI device associated with LUN.
  1746. * @arg: UDirect ioctl data structure.
  1747. *
  1748. * On successful return, the user is informed of the resource handle
  1749. * to be used to identify the direct lun and the size (in blocks) of
  1750. * the direct lun in last LBA format.
  1751. *
  1752. * Return: 0 on success, -errno on failure
  1753. */
  1754. static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
  1755. {
  1756. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1757. struct device *dev = &cfg->dev->dev;
  1758. struct afu *afu = cfg->afu;
  1759. struct llun_info *lli = sdev->hostdata;
  1760. struct glun_info *gli = lli->parent;
  1761. struct dk_cxlflash_release rel = { { 0 }, 0 };
  1762. struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
  1763. u64 ctxid = DECODE_CTXID(pphys->context_id),
  1764. rctxid = pphys->context_id;
  1765. u64 lun_size = 0;
  1766. u64 last_lba = 0;
  1767. u64 rsrc_handle = -1;
  1768. u32 port = CHAN2PORTMASK(sdev->channel);
  1769. int rc = 0;
  1770. struct ctx_info *ctxi = NULL;
  1771. struct sisl_rht_entry *rhte = NULL;
  1772. dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
  1773. rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
  1774. if (unlikely(rc)) {
  1775. dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__);
  1776. goto out;
  1777. }
  1778. ctxi = get_context(cfg, rctxid, lli, 0);
  1779. if (unlikely(!ctxi)) {
  1780. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1781. rc = -EINVAL;
  1782. goto err1;
  1783. }
  1784. rhte = rhte_checkout(ctxi, lli);
  1785. if (unlikely(!rhte)) {
  1786. dev_dbg(dev, "%s: Too many opens ctxid=%lld\n",
  1787. __func__, ctxid);
  1788. rc = -EMFILE; /* too many opens */
  1789. goto err1;
  1790. }
  1791. rsrc_handle = (rhte - ctxi->rht_start);
  1792. rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
  1793. last_lba = gli->max_lba;
  1794. pphys->hdr.return_flags = 0;
  1795. pphys->last_lba = last_lba;
  1796. pphys->rsrc_handle = rsrc_handle;
  1797. rc = cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
  1798. if (unlikely(rc)) {
  1799. dev_dbg(dev, "%s: AFU sync failed rc=%d\n", __func__, rc);
  1800. goto err2;
  1801. }
  1802. out:
  1803. if (likely(ctxi))
  1804. put_context(ctxi);
  1805. dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
  1806. __func__, rsrc_handle, rc, last_lba);
  1807. return rc;
  1808. err2:
  1809. marshal_udir_to_rele(pphys, &rel);
  1810. _cxlflash_disk_release(sdev, ctxi, &rel);
  1811. goto out;
  1812. err1:
  1813. cxlflash_lun_detach(gli);
  1814. goto out;
  1815. }
  1816. /**
  1817. * ioctl_common() - common IOCTL handler for driver
  1818. * @sdev: SCSI device associated with LUN.
  1819. * @cmd: IOCTL command.
  1820. *
  1821. * Handles common fencing operations that are valid for multiple ioctls. Always
  1822. * allow through ioctls that are cleanup oriented in nature, even when operating
  1823. * in a failed/terminating state.
  1824. *
  1825. * Return: 0 on success, -errno on failure
  1826. */
  1827. static int ioctl_common(struct scsi_device *sdev, int cmd)
  1828. {
  1829. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1830. struct device *dev = &cfg->dev->dev;
  1831. struct llun_info *lli = sdev->hostdata;
  1832. int rc = 0;
  1833. if (unlikely(!lli)) {
  1834. dev_dbg(dev, "%s: Unknown LUN\n", __func__);
  1835. rc = -EINVAL;
  1836. goto out;
  1837. }
  1838. rc = check_state(cfg);
  1839. if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
  1840. switch (cmd) {
  1841. case DK_CXLFLASH_VLUN_RESIZE:
  1842. case DK_CXLFLASH_RELEASE:
  1843. case DK_CXLFLASH_DETACH:
  1844. dev_dbg(dev, "%s: Command override rc=%d\n",
  1845. __func__, rc);
  1846. rc = 0;
  1847. break;
  1848. }
  1849. }
  1850. out:
  1851. return rc;
  1852. }
  1853. /**
  1854. * cxlflash_ioctl() - IOCTL handler for driver
  1855. * @sdev: SCSI device associated with LUN.
  1856. * @cmd: IOCTL command.
  1857. * @arg: Userspace ioctl data structure.
  1858. *
  1859. * A read/write semaphore is used to implement a 'drain' of currently
  1860. * running ioctls. The read semaphore is taken at the beginning of each
  1861. * ioctl thread and released upon concluding execution. Additionally the
  1862. * semaphore should be released and then reacquired in any ioctl execution
  1863. * path which will wait for an event to occur that is outside the scope of
  1864. * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
  1865. * a thread simply needs to acquire the write semaphore.
  1866. *
  1867. * Return: 0 on success, -errno on failure
  1868. */
  1869. int cxlflash_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  1870. {
  1871. typedef int (*sioctl) (struct scsi_device *, void *);
  1872. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1873. struct device *dev = &cfg->dev->dev;
  1874. struct afu *afu = cfg->afu;
  1875. struct dk_cxlflash_hdr *hdr;
  1876. char buf[sizeof(union cxlflash_ioctls)];
  1877. size_t size = 0;
  1878. bool known_ioctl = false;
  1879. int idx;
  1880. int rc = 0;
  1881. struct Scsi_Host *shost = sdev->host;
  1882. sioctl do_ioctl = NULL;
  1883. static const struct {
  1884. size_t size;
  1885. sioctl ioctl;
  1886. } ioctl_tbl[] = { /* NOTE: order matters here */
  1887. {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
  1888. {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
  1889. {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
  1890. {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
  1891. {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
  1892. {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
  1893. {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
  1894. {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
  1895. {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
  1896. {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
  1897. };
  1898. /* Hold read semaphore so we can drain if needed */
  1899. down_read(&cfg->ioctl_rwsem);
  1900. /* Restrict command set to physical support only for internal LUN */
  1901. if (afu->internal_lun)
  1902. switch (cmd) {
  1903. case DK_CXLFLASH_RELEASE:
  1904. case DK_CXLFLASH_USER_VIRTUAL:
  1905. case DK_CXLFLASH_VLUN_RESIZE:
  1906. case DK_CXLFLASH_VLUN_CLONE:
  1907. dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
  1908. __func__, decode_ioctl(cmd), afu->internal_lun);
  1909. rc = -EINVAL;
  1910. goto cxlflash_ioctl_exit;
  1911. }
  1912. switch (cmd) {
  1913. case DK_CXLFLASH_ATTACH:
  1914. case DK_CXLFLASH_USER_DIRECT:
  1915. case DK_CXLFLASH_RELEASE:
  1916. case DK_CXLFLASH_DETACH:
  1917. case DK_CXLFLASH_VERIFY:
  1918. case DK_CXLFLASH_RECOVER_AFU:
  1919. case DK_CXLFLASH_USER_VIRTUAL:
  1920. case DK_CXLFLASH_VLUN_RESIZE:
  1921. case DK_CXLFLASH_VLUN_CLONE:
  1922. dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
  1923. __func__, decode_ioctl(cmd), cmd, shost->host_no,
  1924. sdev->channel, sdev->id, sdev->lun);
  1925. rc = ioctl_common(sdev, cmd);
  1926. if (unlikely(rc))
  1927. goto cxlflash_ioctl_exit;
  1928. /* fall through */
  1929. case DK_CXLFLASH_MANAGE_LUN:
  1930. known_ioctl = true;
  1931. idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
  1932. size = ioctl_tbl[idx].size;
  1933. do_ioctl = ioctl_tbl[idx].ioctl;
  1934. if (likely(do_ioctl))
  1935. break;
  1936. /* fall through */
  1937. default:
  1938. rc = -EINVAL;
  1939. goto cxlflash_ioctl_exit;
  1940. }
  1941. if (unlikely(copy_from_user(&buf, arg, size))) {
  1942. dev_err(dev, "%s: copy_from_user() fail "
  1943. "size=%lu cmd=%d (%s) arg=%p\n",
  1944. __func__, size, cmd, decode_ioctl(cmd), arg);
  1945. rc = -EFAULT;
  1946. goto cxlflash_ioctl_exit;
  1947. }
  1948. hdr = (struct dk_cxlflash_hdr *)&buf;
  1949. if (hdr->version != DK_CXLFLASH_VERSION_0) {
  1950. dev_dbg(dev, "%s: Version %u not supported for %s\n",
  1951. __func__, hdr->version, decode_ioctl(cmd));
  1952. rc = -EINVAL;
  1953. goto cxlflash_ioctl_exit;
  1954. }
  1955. if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
  1956. dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
  1957. rc = -EINVAL;
  1958. goto cxlflash_ioctl_exit;
  1959. }
  1960. rc = do_ioctl(sdev, (void *)&buf);
  1961. if (likely(!rc))
  1962. if (unlikely(copy_to_user(arg, &buf, size))) {
  1963. dev_err(dev, "%s: copy_to_user() fail "
  1964. "size=%lu cmd=%d (%s) arg=%p\n",
  1965. __func__, size, cmd, decode_ioctl(cmd), arg);
  1966. rc = -EFAULT;
  1967. }
  1968. /* fall through to exit */
  1969. cxlflash_ioctl_exit:
  1970. up_read(&cfg->ioctl_rwsem);
  1971. if (unlikely(rc && known_ioctl))
  1972. dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
  1973. "returned rc %d\n", __func__,
  1974. decode_ioctl(cmd), cmd, shost->host_no,
  1975. sdev->channel, sdev->id, sdev->lun, rc);
  1976. else
  1977. dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
  1978. "returned rc %d\n", __func__, decode_ioctl(cmd),
  1979. cmd, shost->host_no, sdev->channel, sdev->id,
  1980. sdev->lun, rc);
  1981. return rc;
  1982. }