superpipe.c 61 KB

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