superpipe.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CXL Flash Device Driver
  4. *
  5. * Written by: Manoj N. Kumar <[email protected]>, IBM Corporation
  6. * Matthew R. Ochs <[email protected]>, 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 <asm/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 semahpore 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,
  649. struct dk_cxlflash_release *release)
  650. {
  651. return _cxlflash_disk_release(sdev, NULL, release);
  652. }
  653. /**
  654. * destroy_context() - releases a context
  655. * @cfg: Internal structure associated with the host.
  656. * @ctxi: Context to release.
  657. *
  658. * This routine is safe to be called with a a non-initialized context.
  659. * Also note that the routine conditionally checks for the existence
  660. * of the context control map before clearing the RHT registers and
  661. * context capabilities because it is possible to destroy a context
  662. * while the context is in the error state (previous mapping was
  663. * removed [so there is no need to worry about clearing] and context
  664. * is waiting for a new mapping).
  665. */
  666. static void destroy_context(struct cxlflash_cfg *cfg,
  667. struct ctx_info *ctxi)
  668. {
  669. struct afu *afu = cfg->afu;
  670. if (ctxi->initialized) {
  671. WARN_ON(!list_empty(&ctxi->luns));
  672. /* Clear RHT registers and drop all capabilities for context */
  673. if (afu->afu_map && ctxi->ctrl_map) {
  674. writeq_be(0, &ctxi->ctrl_map->rht_start);
  675. writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
  676. writeq_be(0, &ctxi->ctrl_map->ctx_cap);
  677. }
  678. }
  679. /* Free memory associated with context */
  680. free_page((ulong)ctxi->rht_start);
  681. kfree(ctxi->rht_needs_ws);
  682. kfree(ctxi->rht_lun);
  683. kfree(ctxi);
  684. }
  685. /**
  686. * create_context() - allocates and initializes a context
  687. * @cfg: Internal structure associated with the host.
  688. *
  689. * Return: Allocated context on success, NULL on failure
  690. */
  691. static struct ctx_info *create_context(struct cxlflash_cfg *cfg)
  692. {
  693. struct device *dev = &cfg->dev->dev;
  694. struct ctx_info *ctxi = NULL;
  695. struct llun_info **lli = NULL;
  696. u8 *ws = NULL;
  697. struct sisl_rht_entry *rhte;
  698. ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL);
  699. lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL);
  700. ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL);
  701. if (unlikely(!ctxi || !lli || !ws)) {
  702. dev_err(dev, "%s: Unable to allocate context\n", __func__);
  703. goto err;
  704. }
  705. rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL);
  706. if (unlikely(!rhte)) {
  707. dev_err(dev, "%s: Unable to allocate RHT\n", __func__);
  708. goto err;
  709. }
  710. ctxi->rht_lun = lli;
  711. ctxi->rht_needs_ws = ws;
  712. ctxi->rht_start = rhte;
  713. out:
  714. return ctxi;
  715. err:
  716. kfree(ws);
  717. kfree(lli);
  718. kfree(ctxi);
  719. ctxi = NULL;
  720. goto out;
  721. }
  722. /**
  723. * init_context() - initializes a previously allocated context
  724. * @ctxi: Previously allocated context
  725. * @cfg: Internal structure associated with the host.
  726. * @ctx: Previously obtained context cookie.
  727. * @ctxid: Previously obtained process element associated with CXL context.
  728. * @file: Previously obtained file associated with CXL context.
  729. * @perms: User-specified permissions.
  730. * @irqs: User-specified number of interrupts.
  731. */
  732. static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
  733. void *ctx, int ctxid, struct file *file, u32 perms,
  734. u64 irqs)
  735. {
  736. struct afu *afu = cfg->afu;
  737. ctxi->rht_perms = perms;
  738. ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
  739. ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
  740. ctxi->irqs = irqs;
  741. ctxi->pid = task_tgid_nr(current); /* tgid = pid */
  742. ctxi->ctx = ctx;
  743. ctxi->cfg = cfg;
  744. ctxi->file = file;
  745. ctxi->initialized = true;
  746. mutex_init(&ctxi->mutex);
  747. kref_init(&ctxi->kref);
  748. INIT_LIST_HEAD(&ctxi->luns);
  749. INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
  750. }
  751. /**
  752. * remove_context() - context kref release handler
  753. * @kref: Kernel reference associated with context to be removed.
  754. *
  755. * When a context no longer has any references it can safely be removed
  756. * from global access and destroyed. Note that it is assumed the thread
  757. * relinquishing access to the context holds its mutex.
  758. */
  759. static void remove_context(struct kref *kref)
  760. {
  761. struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
  762. struct cxlflash_cfg *cfg = ctxi->cfg;
  763. u64 ctxid = DECODE_CTXID(ctxi->ctxid);
  764. /* Remove context from table/error list */
  765. WARN_ON(!mutex_is_locked(&ctxi->mutex));
  766. ctxi->unavail = true;
  767. mutex_unlock(&ctxi->mutex);
  768. mutex_lock(&cfg->ctx_tbl_list_mutex);
  769. mutex_lock(&ctxi->mutex);
  770. if (!list_empty(&ctxi->list))
  771. list_del(&ctxi->list);
  772. cfg->ctx_tbl[ctxid] = NULL;
  773. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  774. mutex_unlock(&ctxi->mutex);
  775. /* Context now completely uncoupled/unreachable */
  776. destroy_context(cfg, ctxi);
  777. }
  778. /**
  779. * _cxlflash_disk_detach() - detaches a LUN from a context
  780. * @sdev: SCSI device associated with LUN.
  781. * @ctxi: Context owning resources.
  782. * @detach: Detach ioctl data structure.
  783. *
  784. * As part of the detach, all per-context resources associated with the LUN
  785. * are cleaned up. When detaching the last LUN for a context, the context
  786. * itself is cleaned up and released.
  787. *
  788. * Return: 0 on success, -errno on failure
  789. */
  790. static int _cxlflash_disk_detach(struct scsi_device *sdev,
  791. struct ctx_info *ctxi,
  792. struct dk_cxlflash_detach *detach)
  793. {
  794. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  795. struct device *dev = &cfg->dev->dev;
  796. struct llun_info *lli = sdev->hostdata;
  797. struct lun_access *lun_access, *t;
  798. struct dk_cxlflash_release rel;
  799. bool put_ctx = false;
  800. int i;
  801. int rc = 0;
  802. u64 ctxid = DECODE_CTXID(detach->context_id),
  803. rctxid = detach->context_id;
  804. dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
  805. if (!ctxi) {
  806. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  807. if (unlikely(!ctxi)) {
  808. dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
  809. __func__, ctxid);
  810. rc = -EINVAL;
  811. goto out;
  812. }
  813. put_ctx = true;
  814. }
  815. /* Cleanup outstanding resources tied to this LUN */
  816. if (ctxi->rht_out) {
  817. marshal_det_to_rele(detach, &rel);
  818. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
  819. if (ctxi->rht_lun[i] == lli) {
  820. rel.rsrc_handle = i;
  821. _cxlflash_disk_release(sdev, ctxi, &rel);
  822. }
  823. /* No need to loop further if we're done */
  824. if (ctxi->rht_out == 0)
  825. break;
  826. }
  827. }
  828. /* Take our LUN out of context, free the node */
  829. list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
  830. if (lun_access->lli == lli) {
  831. list_del(&lun_access->list);
  832. kfree(lun_access);
  833. lun_access = NULL;
  834. break;
  835. }
  836. /*
  837. * Release the context reference and the sdev reference that
  838. * bound this LUN to the context.
  839. */
  840. if (kref_put(&ctxi->kref, remove_context))
  841. put_ctx = false;
  842. scsi_device_put(sdev);
  843. out:
  844. if (put_ctx)
  845. put_context(ctxi);
  846. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  847. return rc;
  848. }
  849. static int cxlflash_disk_detach(struct scsi_device *sdev,
  850. struct dk_cxlflash_detach *detach)
  851. {
  852. return _cxlflash_disk_detach(sdev, NULL, detach);
  853. }
  854. /**
  855. * cxlflash_cxl_release() - release handler for adapter file descriptor
  856. * @inode: File-system inode associated with fd.
  857. * @file: File installed with adapter file descriptor.
  858. *
  859. * This routine is the release handler for the fops registered with
  860. * the CXL services on an initial attach for a context. It is called
  861. * when a close (explicity by the user or as part of a process tear
  862. * down) is performed on the adapter file descriptor returned to the
  863. * user. The user should be aware that explicitly performing a close
  864. * considered catastrophic and subsequent usage of the superpipe API
  865. * with previously saved off tokens will fail.
  866. *
  867. * This routine derives the context reference and calls detach for
  868. * each LUN associated with the context.The final detach operation
  869. * causes the context itself to be freed. With exception to when the
  870. * CXL process element (context id) lookup fails (a case that should
  871. * theoretically never occur), every call into this routine results
  872. * in a complete freeing of a context.
  873. *
  874. * Detaching the LUN is typically an ioctl() operation and the underlying
  875. * code assumes that ioctl_rwsem has been acquired as a reader. To support
  876. * that design point, the semaphore is acquired and released around detach.
  877. *
  878. * Return: 0 on success
  879. */
  880. static int cxlflash_cxl_release(struct inode *inode, struct file *file)
  881. {
  882. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  883. cxl_fops);
  884. void *ctx = cfg->ops->fops_get_context(file);
  885. struct device *dev = &cfg->dev->dev;
  886. struct ctx_info *ctxi = NULL;
  887. struct dk_cxlflash_detach detach = { { 0 }, 0 };
  888. struct lun_access *lun_access, *t;
  889. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  890. int ctxid;
  891. ctxid = cfg->ops->process_element(ctx);
  892. if (unlikely(ctxid < 0)) {
  893. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  894. __func__, ctx, ctxid);
  895. goto out;
  896. }
  897. ctxi = get_context(cfg, ctxid, file, ctrl);
  898. if (unlikely(!ctxi)) {
  899. ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
  900. if (!ctxi) {
  901. dev_dbg(dev, "%s: ctxid=%d already free\n",
  902. __func__, ctxid);
  903. goto out_release;
  904. }
  905. dev_dbg(dev, "%s: Another process owns ctxid=%d\n",
  906. __func__, ctxid);
  907. put_context(ctxi);
  908. goto out;
  909. }
  910. dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid);
  911. down_read(&cfg->ioctl_rwsem);
  912. detach.context_id = ctxi->ctxid;
  913. list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
  914. _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
  915. up_read(&cfg->ioctl_rwsem);
  916. out_release:
  917. cfg->ops->fd_release(inode, file);
  918. out:
  919. dev_dbg(dev, "%s: returning\n", __func__);
  920. return 0;
  921. }
  922. /**
  923. * unmap_context() - clears a previously established mapping
  924. * @ctxi: Context owning the mapping.
  925. *
  926. * This routine is used to switch between the error notification page
  927. * (dummy page of all 1's) and the real mapping (established by the CXL
  928. * fault handler).
  929. */
  930. static void unmap_context(struct ctx_info *ctxi)
  931. {
  932. unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
  933. }
  934. /**
  935. * get_err_page() - obtains and allocates the error notification page
  936. * @cfg: Internal structure associated with the host.
  937. *
  938. * Return: error notification page on success, NULL on failure
  939. */
  940. static struct page *get_err_page(struct cxlflash_cfg *cfg)
  941. {
  942. struct page *err_page = global.err_page;
  943. struct device *dev = &cfg->dev->dev;
  944. if (unlikely(!err_page)) {
  945. err_page = alloc_page(GFP_KERNEL);
  946. if (unlikely(!err_page)) {
  947. dev_err(dev, "%s: Unable to allocate err_page\n",
  948. __func__);
  949. goto out;
  950. }
  951. memset(page_address(err_page), -1, PAGE_SIZE);
  952. /* Serialize update w/ other threads to avoid a leak */
  953. mutex_lock(&global.mutex);
  954. if (likely(!global.err_page))
  955. global.err_page = err_page;
  956. else {
  957. __free_page(err_page);
  958. err_page = global.err_page;
  959. }
  960. mutex_unlock(&global.mutex);
  961. }
  962. out:
  963. dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page);
  964. return err_page;
  965. }
  966. /**
  967. * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
  968. * @vmf: VM fault associated with current fault.
  969. *
  970. * To support error notification via MMIO, faults are 'caught' by this routine
  971. * that was inserted before passing back the adapter file descriptor on attach.
  972. * When a fault occurs, this routine evaluates if error recovery is active and
  973. * if so, installs the error page to 'notify' the user about the error state.
  974. * During normal operation, the fault is simply handled by the original fault
  975. * handler that was installed by CXL services as part of initializing the
  976. * adapter file descriptor. The VMA's page protection bits are toggled to
  977. * indicate cached/not-cached depending on the memory backing the fault.
  978. *
  979. * Return: 0 on success, VM_FAULT_SIGBUS on failure
  980. */
  981. static vm_fault_t cxlflash_mmap_fault(struct vm_fault *vmf)
  982. {
  983. struct vm_area_struct *vma = vmf->vma;
  984. struct file *file = vma->vm_file;
  985. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  986. cxl_fops);
  987. void *ctx = cfg->ops->fops_get_context(file);
  988. struct device *dev = &cfg->dev->dev;
  989. struct ctx_info *ctxi = NULL;
  990. struct page *err_page = NULL;
  991. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  992. vm_fault_t rc = 0;
  993. int ctxid;
  994. ctxid = cfg->ops->process_element(ctx);
  995. if (unlikely(ctxid < 0)) {
  996. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  997. __func__, ctx, ctxid);
  998. goto err;
  999. }
  1000. ctxi = get_context(cfg, ctxid, file, ctrl);
  1001. if (unlikely(!ctxi)) {
  1002. dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
  1003. goto err;
  1004. }
  1005. dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
  1006. if (likely(!ctxi->err_recovery_active)) {
  1007. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1008. rc = ctxi->cxl_mmap_vmops->fault(vmf);
  1009. } else {
  1010. dev_dbg(dev, "%s: err recovery active, use err_page\n",
  1011. __func__);
  1012. err_page = get_err_page(cfg);
  1013. if (unlikely(!err_page)) {
  1014. dev_err(dev, "%s: Could not get err_page\n", __func__);
  1015. rc = VM_FAULT_RETRY;
  1016. goto out;
  1017. }
  1018. get_page(err_page);
  1019. vmf->page = err_page;
  1020. vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
  1021. }
  1022. out:
  1023. if (likely(ctxi))
  1024. put_context(ctxi);
  1025. dev_dbg(dev, "%s: returning rc=%x\n", __func__, rc);
  1026. return rc;
  1027. err:
  1028. rc = VM_FAULT_SIGBUS;
  1029. goto out;
  1030. }
  1031. /*
  1032. * Local MMAP vmops to 'catch' faults
  1033. */
  1034. static const struct vm_operations_struct cxlflash_mmap_vmops = {
  1035. .fault = cxlflash_mmap_fault,
  1036. };
  1037. /**
  1038. * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
  1039. * @file: File installed with adapter file descriptor.
  1040. * @vma: VM area associated with mapping.
  1041. *
  1042. * Installs local mmap vmops to 'catch' faults for error notification support.
  1043. *
  1044. * Return: 0 on success, -errno on failure
  1045. */
  1046. static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
  1047. {
  1048. struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
  1049. cxl_fops);
  1050. void *ctx = cfg->ops->fops_get_context(file);
  1051. struct device *dev = &cfg->dev->dev;
  1052. struct ctx_info *ctxi = NULL;
  1053. enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
  1054. int ctxid;
  1055. int rc = 0;
  1056. ctxid = cfg->ops->process_element(ctx);
  1057. if (unlikely(ctxid < 0)) {
  1058. dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
  1059. __func__, ctx, ctxid);
  1060. rc = -EIO;
  1061. goto out;
  1062. }
  1063. ctxi = get_context(cfg, ctxid, file, ctrl);
  1064. if (unlikely(!ctxi)) {
  1065. dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
  1066. rc = -EIO;
  1067. goto out;
  1068. }
  1069. dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
  1070. rc = cfg->ops->fd_mmap(file, vma);
  1071. if (likely(!rc)) {
  1072. /* Insert ourself in the mmap fault handler path */
  1073. ctxi->cxl_mmap_vmops = vma->vm_ops;
  1074. vma->vm_ops = &cxlflash_mmap_vmops;
  1075. }
  1076. out:
  1077. if (likely(ctxi))
  1078. put_context(ctxi);
  1079. return rc;
  1080. }
  1081. const struct file_operations cxlflash_cxl_fops = {
  1082. .owner = THIS_MODULE,
  1083. .mmap = cxlflash_cxl_mmap,
  1084. .release = cxlflash_cxl_release,
  1085. };
  1086. /**
  1087. * cxlflash_mark_contexts_error() - move contexts to error state and list
  1088. * @cfg: Internal structure associated with the host.
  1089. *
  1090. * A context is only moved over to the error list when there are no outstanding
  1091. * references to it. This ensures that a running operation has completed.
  1092. *
  1093. * Return: 0 on success, -errno on failure
  1094. */
  1095. int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
  1096. {
  1097. int i, rc = 0;
  1098. struct ctx_info *ctxi = NULL;
  1099. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1100. for (i = 0; i < MAX_CONTEXT; i++) {
  1101. ctxi = cfg->ctx_tbl[i];
  1102. if (ctxi) {
  1103. mutex_lock(&ctxi->mutex);
  1104. cfg->ctx_tbl[i] = NULL;
  1105. list_add(&ctxi->list, &cfg->ctx_err_recovery);
  1106. ctxi->err_recovery_active = true;
  1107. ctxi->ctrl_map = NULL;
  1108. unmap_context(ctxi);
  1109. mutex_unlock(&ctxi->mutex);
  1110. }
  1111. }
  1112. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1113. return rc;
  1114. }
  1115. /*
  1116. * Dummy NULL fops
  1117. */
  1118. static const struct file_operations null_fops = {
  1119. .owner = THIS_MODULE,
  1120. };
  1121. /**
  1122. * check_state() - checks and responds to the current adapter state
  1123. * @cfg: Internal structure associated with the host.
  1124. *
  1125. * This routine can block and should only be used on process context.
  1126. * It assumes that the caller is an ioctl thread and holding the ioctl
  1127. * read semaphore. This is temporarily let up across the wait to allow
  1128. * for draining actively running ioctls. Also note that when waking up
  1129. * from waiting in reset, the state is unknown and must be checked again
  1130. * before proceeding.
  1131. *
  1132. * Return: 0 on success, -errno on failure
  1133. */
  1134. int check_state(struct cxlflash_cfg *cfg)
  1135. {
  1136. struct device *dev = &cfg->dev->dev;
  1137. int rc = 0;
  1138. retry:
  1139. switch (cfg->state) {
  1140. case STATE_RESET:
  1141. dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
  1142. up_read(&cfg->ioctl_rwsem);
  1143. rc = wait_event_interruptible(cfg->reset_waitq,
  1144. cfg->state != STATE_RESET);
  1145. down_read(&cfg->ioctl_rwsem);
  1146. if (unlikely(rc))
  1147. break;
  1148. goto retry;
  1149. case STATE_FAILTERM:
  1150. dev_dbg(dev, "%s: Failed/Terminating\n", __func__);
  1151. rc = -ENODEV;
  1152. break;
  1153. default:
  1154. break;
  1155. }
  1156. return rc;
  1157. }
  1158. /**
  1159. * cxlflash_disk_attach() - attach a LUN to a context
  1160. * @sdev: SCSI device associated with LUN.
  1161. * @attach: Attach ioctl data structure.
  1162. *
  1163. * Creates a context and attaches LUN to it. A LUN can only be attached
  1164. * one time to a context (subsequent attaches for the same context/LUN pair
  1165. * are not supported). Additional LUNs can be attached to a context by
  1166. * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
  1167. *
  1168. * Return: 0 on success, -errno on failure
  1169. */
  1170. static int cxlflash_disk_attach(struct scsi_device *sdev,
  1171. struct dk_cxlflash_attach *attach)
  1172. {
  1173. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1174. struct device *dev = &cfg->dev->dev;
  1175. struct afu *afu = cfg->afu;
  1176. struct llun_info *lli = sdev->hostdata;
  1177. struct glun_info *gli = lli->parent;
  1178. struct ctx_info *ctxi = NULL;
  1179. struct lun_access *lun_access = NULL;
  1180. int rc = 0;
  1181. u32 perms;
  1182. int ctxid = -1;
  1183. u64 irqs = attach->num_interrupts;
  1184. u64 flags = 0UL;
  1185. u64 rctxid = 0UL;
  1186. struct file *file = NULL;
  1187. void *ctx = NULL;
  1188. int fd = -1;
  1189. if (irqs > 4) {
  1190. dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
  1191. __func__, irqs);
  1192. rc = -EINVAL;
  1193. goto out;
  1194. }
  1195. if (gli->max_lba == 0) {
  1196. dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n",
  1197. __func__, lli->lun_id[sdev->channel]);
  1198. rc = read_cap16(sdev, lli);
  1199. if (rc) {
  1200. dev_err(dev, "%s: Invalid device rc=%d\n",
  1201. __func__, rc);
  1202. rc = -ENODEV;
  1203. goto out;
  1204. }
  1205. dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba);
  1206. dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len);
  1207. }
  1208. if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
  1209. rctxid = attach->context_id;
  1210. ctxi = get_context(cfg, rctxid, NULL, 0);
  1211. if (!ctxi) {
  1212. dev_dbg(dev, "%s: Bad context rctxid=%016llx\n",
  1213. __func__, rctxid);
  1214. rc = -EINVAL;
  1215. goto out;
  1216. }
  1217. list_for_each_entry(lun_access, &ctxi->luns, list)
  1218. if (lun_access->lli == lli) {
  1219. dev_dbg(dev, "%s: Already attached\n",
  1220. __func__);
  1221. rc = -EINVAL;
  1222. goto out;
  1223. }
  1224. }
  1225. rc = scsi_device_get(sdev);
  1226. if (unlikely(rc)) {
  1227. dev_err(dev, "%s: Unable to get sdev reference\n", __func__);
  1228. goto out;
  1229. }
  1230. lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
  1231. if (unlikely(!lun_access)) {
  1232. dev_err(dev, "%s: Unable to allocate lun_access\n", __func__);
  1233. rc = -ENOMEM;
  1234. goto err;
  1235. }
  1236. lun_access->lli = lli;
  1237. lun_access->sdev = sdev;
  1238. /* Non-NULL context indicates reuse (another context reference) */
  1239. if (ctxi) {
  1240. dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n",
  1241. __func__, rctxid);
  1242. kref_get(&ctxi->kref);
  1243. list_add(&lun_access->list, &ctxi->luns);
  1244. goto out_attach;
  1245. }
  1246. ctxi = create_context(cfg);
  1247. if (unlikely(!ctxi)) {
  1248. dev_err(dev, "%s: Failed to create context ctxid=%d\n",
  1249. __func__, ctxid);
  1250. rc = -ENOMEM;
  1251. goto err;
  1252. }
  1253. ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
  1254. if (IS_ERR_OR_NULL(ctx)) {
  1255. dev_err(dev, "%s: Could not initialize context %p\n",
  1256. __func__, ctx);
  1257. rc = -ENODEV;
  1258. goto err;
  1259. }
  1260. rc = cfg->ops->start_work(ctx, irqs);
  1261. if (unlikely(rc)) {
  1262. dev_dbg(dev, "%s: Could not start context rc=%d\n",
  1263. __func__, rc);
  1264. goto err;
  1265. }
  1266. ctxid = cfg->ops->process_element(ctx);
  1267. if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
  1268. dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
  1269. rc = -EPERM;
  1270. goto err;
  1271. }
  1272. file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
  1273. if (unlikely(fd < 0)) {
  1274. rc = -ENODEV;
  1275. dev_err(dev, "%s: Could not get file descriptor\n", __func__);
  1276. goto err;
  1277. }
  1278. /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
  1279. perms = SISL_RHT_PERM(attach->hdr.flags + 1);
  1280. /* Context mutex is locked upon return */
  1281. init_context(ctxi, cfg, ctx, ctxid, file, perms, irqs);
  1282. rc = afu_attach(cfg, ctxi);
  1283. if (unlikely(rc)) {
  1284. dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
  1285. goto err;
  1286. }
  1287. /*
  1288. * No error paths after this point. Once the fd is installed it's
  1289. * visible to user space and can't be undone safely on this thread.
  1290. * There is no need to worry about a deadlock here because no one
  1291. * knows about us yet; we can be the only one holding our mutex.
  1292. */
  1293. list_add(&lun_access->list, &ctxi->luns);
  1294. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1295. mutex_lock(&ctxi->mutex);
  1296. cfg->ctx_tbl[ctxid] = ctxi;
  1297. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1298. fd_install(fd, file);
  1299. out_attach:
  1300. if (fd != -1)
  1301. flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD;
  1302. if (afu_is_sq_cmd_mode(afu))
  1303. flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
  1304. attach->hdr.return_flags = flags;
  1305. attach->context_id = ctxi->ctxid;
  1306. attach->block_size = gli->blk_len;
  1307. attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
  1308. attach->last_lba = gli->max_lba;
  1309. attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
  1310. attach->max_xfer /= gli->blk_len;
  1311. out:
  1312. attach->adap_fd = fd;
  1313. if (ctxi)
  1314. put_context(ctxi);
  1315. dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
  1316. __func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
  1317. return rc;
  1318. err:
  1319. /* Cleanup CXL context; okay to 'stop' even if it was not started */
  1320. if (!IS_ERR_OR_NULL(ctx)) {
  1321. cfg->ops->stop_context(ctx);
  1322. cfg->ops->release_context(ctx);
  1323. ctx = NULL;
  1324. }
  1325. /*
  1326. * Here, we're overriding the fops with a dummy all-NULL fops because
  1327. * fput() calls the release fop, which will cause us to mistakenly
  1328. * call into the CXL code. Rather than try to add yet more complexity
  1329. * to that routine (cxlflash_cxl_release) we should try to fix the
  1330. * issue here.
  1331. */
  1332. if (fd > 0) {
  1333. file->f_op = &null_fops;
  1334. fput(file);
  1335. put_unused_fd(fd);
  1336. fd = -1;
  1337. file = NULL;
  1338. }
  1339. /* Cleanup our context */
  1340. if (ctxi) {
  1341. destroy_context(cfg, ctxi);
  1342. ctxi = NULL;
  1343. }
  1344. kfree(lun_access);
  1345. scsi_device_put(sdev);
  1346. goto out;
  1347. }
  1348. /**
  1349. * recover_context() - recovers a context in error
  1350. * @cfg: Internal structure associated with the host.
  1351. * @ctxi: Context to release.
  1352. * @adap_fd: Adapter file descriptor associated with new/recovered context.
  1353. *
  1354. * Restablishes the state for a context-in-error.
  1355. *
  1356. * Return: 0 on success, -errno on failure
  1357. */
  1358. static int recover_context(struct cxlflash_cfg *cfg,
  1359. struct ctx_info *ctxi,
  1360. int *adap_fd)
  1361. {
  1362. struct device *dev = &cfg->dev->dev;
  1363. int rc = 0;
  1364. int fd = -1;
  1365. int ctxid = -1;
  1366. struct file *file;
  1367. void *ctx;
  1368. struct afu *afu = cfg->afu;
  1369. ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
  1370. if (IS_ERR_OR_NULL(ctx)) {
  1371. dev_err(dev, "%s: Could not initialize context %p\n",
  1372. __func__, ctx);
  1373. rc = -ENODEV;
  1374. goto out;
  1375. }
  1376. rc = cfg->ops->start_work(ctx, ctxi->irqs);
  1377. if (unlikely(rc)) {
  1378. dev_dbg(dev, "%s: Could not start context rc=%d\n",
  1379. __func__, rc);
  1380. goto err1;
  1381. }
  1382. ctxid = cfg->ops->process_element(ctx);
  1383. if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
  1384. dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
  1385. rc = -EPERM;
  1386. goto err2;
  1387. }
  1388. file = cfg->ops->get_fd(ctx, &cfg->cxl_fops, &fd);
  1389. if (unlikely(fd < 0)) {
  1390. rc = -ENODEV;
  1391. dev_err(dev, "%s: Could not get file descriptor\n", __func__);
  1392. goto err2;
  1393. }
  1394. /* Update with new MMIO area based on updated context id */
  1395. ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
  1396. rc = afu_attach(cfg, ctxi);
  1397. if (rc) {
  1398. dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
  1399. goto err3;
  1400. }
  1401. /*
  1402. * No error paths after this point. Once the fd is installed it's
  1403. * visible to user space and can't be undone safely on this thread.
  1404. */
  1405. ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
  1406. ctxi->ctx = ctx;
  1407. ctxi->file = file;
  1408. /*
  1409. * Put context back in table (note the reinit of the context list);
  1410. * we must first drop the context's mutex and then acquire it in
  1411. * order with the table/list mutex to avoid a deadlock - safe to do
  1412. * here because no one can find us at this moment in time.
  1413. */
  1414. mutex_unlock(&ctxi->mutex);
  1415. mutex_lock(&cfg->ctx_tbl_list_mutex);
  1416. mutex_lock(&ctxi->mutex);
  1417. list_del_init(&ctxi->list);
  1418. cfg->ctx_tbl[ctxid] = ctxi;
  1419. mutex_unlock(&cfg->ctx_tbl_list_mutex);
  1420. fd_install(fd, file);
  1421. *adap_fd = fd;
  1422. out:
  1423. dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
  1424. __func__, ctxid, fd, rc);
  1425. return rc;
  1426. err3:
  1427. fput(file);
  1428. put_unused_fd(fd);
  1429. err2:
  1430. cfg->ops->stop_context(ctx);
  1431. err1:
  1432. cfg->ops->release_context(ctx);
  1433. goto out;
  1434. }
  1435. /**
  1436. * cxlflash_afu_recover() - initiates AFU recovery
  1437. * @sdev: SCSI device associated with LUN.
  1438. * @recover: Recover ioctl data structure.
  1439. *
  1440. * Only a single recovery is allowed at a time to avoid exhausting CXL
  1441. * resources (leading to recovery failure) in the event that we're up
  1442. * against the maximum number of contexts limit. For similar reasons,
  1443. * a context recovery is retried if there are multiple recoveries taking
  1444. * place at the same time and the failure was due to CXL services being
  1445. * unable to keep up.
  1446. *
  1447. * As this routine is called on ioctl context, it holds the ioctl r/w
  1448. * semaphore that is used to drain ioctls in recovery scenarios. The
  1449. * implementation to achieve the pacing described above (a local mutex)
  1450. * requires that the ioctl r/w semaphore be dropped and reacquired to
  1451. * avoid a 3-way deadlock when multiple process recoveries operate in
  1452. * parallel.
  1453. *
  1454. * Because a user can detect an error condition before the kernel, it is
  1455. * quite possible for this routine to act as the kernel's EEH detection
  1456. * source (MMIO read of mbox_r). Because of this, there is a window of
  1457. * time where an EEH might have been detected but not yet 'serviced'
  1458. * (callback invoked, causing the device to enter reset state). To avoid
  1459. * looping in this routine during that window, a 1 second sleep is in place
  1460. * between the time the MMIO failure is detected and the time a wait on the
  1461. * reset wait queue is attempted via check_state().
  1462. *
  1463. * Return: 0 on success, -errno on failure
  1464. */
  1465. static int cxlflash_afu_recover(struct scsi_device *sdev,
  1466. struct dk_cxlflash_recover_afu *recover)
  1467. {
  1468. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1469. struct device *dev = &cfg->dev->dev;
  1470. struct llun_info *lli = sdev->hostdata;
  1471. struct afu *afu = cfg->afu;
  1472. struct ctx_info *ctxi = NULL;
  1473. struct mutex *mutex = &cfg->ctx_recovery_mutex;
  1474. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  1475. u64 flags;
  1476. u64 ctxid = DECODE_CTXID(recover->context_id),
  1477. rctxid = recover->context_id;
  1478. long reg;
  1479. bool locked = true;
  1480. int lretry = 20; /* up to 2 seconds */
  1481. int new_adap_fd = -1;
  1482. int rc = 0;
  1483. atomic_inc(&cfg->recovery_threads);
  1484. up_read(&cfg->ioctl_rwsem);
  1485. rc = mutex_lock_interruptible(mutex);
  1486. down_read(&cfg->ioctl_rwsem);
  1487. if (rc) {
  1488. locked = false;
  1489. goto out;
  1490. }
  1491. rc = check_state(cfg);
  1492. if (rc) {
  1493. dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc);
  1494. rc = -ENODEV;
  1495. goto out;
  1496. }
  1497. dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n",
  1498. __func__, recover->reason, rctxid);
  1499. retry:
  1500. /* Ensure that this process is attached to the context */
  1501. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  1502. if (unlikely(!ctxi)) {
  1503. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1504. rc = -EINVAL;
  1505. goto out;
  1506. }
  1507. if (ctxi->err_recovery_active) {
  1508. retry_recover:
  1509. rc = recover_context(cfg, ctxi, &new_adap_fd);
  1510. if (unlikely(rc)) {
  1511. dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n",
  1512. __func__, ctxid, rc);
  1513. if ((rc == -ENODEV) &&
  1514. ((atomic_read(&cfg->recovery_threads) > 1) ||
  1515. (lretry--))) {
  1516. dev_dbg(dev, "%s: Going to try again\n",
  1517. __func__);
  1518. mutex_unlock(mutex);
  1519. msleep(100);
  1520. rc = mutex_lock_interruptible(mutex);
  1521. if (rc) {
  1522. locked = false;
  1523. goto out;
  1524. }
  1525. goto retry_recover;
  1526. }
  1527. goto out;
  1528. }
  1529. ctxi->err_recovery_active = false;
  1530. flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
  1531. DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
  1532. if (afu_is_sq_cmd_mode(afu))
  1533. flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
  1534. recover->hdr.return_flags = flags;
  1535. recover->context_id = ctxi->ctxid;
  1536. recover->adap_fd = new_adap_fd;
  1537. recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
  1538. goto out;
  1539. }
  1540. /* Test if in error state */
  1541. reg = readq_be(&hwq->ctrl_map->mbox_r);
  1542. if (reg == -1) {
  1543. dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
  1544. /*
  1545. * Before checking the state, put back the context obtained with
  1546. * get_context() as it is no longer needed and sleep for a short
  1547. * period of time (see prolog notes).
  1548. */
  1549. put_context(ctxi);
  1550. ctxi = NULL;
  1551. ssleep(1);
  1552. rc = check_state(cfg);
  1553. if (unlikely(rc))
  1554. goto out;
  1555. goto retry;
  1556. }
  1557. dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__);
  1558. out:
  1559. if (likely(ctxi))
  1560. put_context(ctxi);
  1561. if (locked)
  1562. mutex_unlock(mutex);
  1563. atomic_dec_if_positive(&cfg->recovery_threads);
  1564. return rc;
  1565. }
  1566. /**
  1567. * process_sense() - evaluates and processes sense data
  1568. * @sdev: SCSI device associated with LUN.
  1569. * @verify: Verify ioctl data structure.
  1570. *
  1571. * Return: 0 on success, -errno on failure
  1572. */
  1573. static int process_sense(struct scsi_device *sdev,
  1574. struct dk_cxlflash_verify *verify)
  1575. {
  1576. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1577. struct device *dev = &cfg->dev->dev;
  1578. struct llun_info *lli = sdev->hostdata;
  1579. struct glun_info *gli = lli->parent;
  1580. u64 prev_lba = gli->max_lba;
  1581. struct scsi_sense_hdr sshdr = { 0 };
  1582. int rc = 0;
  1583. rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
  1584. DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
  1585. if (!rc) {
  1586. dev_err(dev, "%s: Failed to normalize sense data\n", __func__);
  1587. rc = -EINVAL;
  1588. goto out;
  1589. }
  1590. switch (sshdr.sense_key) {
  1591. case NO_SENSE:
  1592. case RECOVERED_ERROR:
  1593. case NOT_READY:
  1594. break;
  1595. case UNIT_ATTENTION:
  1596. switch (sshdr.asc) {
  1597. case 0x29: /* Power on Reset or Device Reset */
  1598. fallthrough;
  1599. case 0x2A: /* Device settings/capacity changed */
  1600. rc = read_cap16(sdev, lli);
  1601. if (rc) {
  1602. rc = -ENODEV;
  1603. break;
  1604. }
  1605. if (prev_lba != gli->max_lba)
  1606. dev_dbg(dev, "%s: Capacity changed old=%lld "
  1607. "new=%lld\n", __func__, prev_lba,
  1608. gli->max_lba);
  1609. break;
  1610. case 0x3F: /* Report LUNs changed, Rescan. */
  1611. scsi_scan_host(cfg->host);
  1612. break;
  1613. default:
  1614. rc = -EIO;
  1615. break;
  1616. }
  1617. break;
  1618. default:
  1619. rc = -EIO;
  1620. break;
  1621. }
  1622. out:
  1623. dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
  1624. sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
  1625. return rc;
  1626. }
  1627. /**
  1628. * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
  1629. * @sdev: SCSI device associated with LUN.
  1630. * @verify: Verify ioctl data structure.
  1631. *
  1632. * Return: 0 on success, -errno on failure
  1633. */
  1634. static int cxlflash_disk_verify(struct scsi_device *sdev,
  1635. struct dk_cxlflash_verify *verify)
  1636. {
  1637. int rc = 0;
  1638. struct ctx_info *ctxi = NULL;
  1639. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1640. struct device *dev = &cfg->dev->dev;
  1641. struct llun_info *lli = sdev->hostdata;
  1642. struct glun_info *gli = lli->parent;
  1643. struct sisl_rht_entry *rhte = NULL;
  1644. res_hndl_t rhndl = verify->rsrc_handle;
  1645. u64 ctxid = DECODE_CTXID(verify->context_id),
  1646. rctxid = verify->context_id;
  1647. u64 last_lba = 0;
  1648. dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
  1649. "flags=%016llx\n", __func__, ctxid, verify->rsrc_handle,
  1650. verify->hint, verify->hdr.flags);
  1651. ctxi = get_context(cfg, rctxid, lli, 0);
  1652. if (unlikely(!ctxi)) {
  1653. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1654. rc = -EINVAL;
  1655. goto out;
  1656. }
  1657. rhte = get_rhte(ctxi, rhndl, lli);
  1658. if (unlikely(!rhte)) {
  1659. dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
  1660. __func__, rhndl);
  1661. rc = -EINVAL;
  1662. goto out;
  1663. }
  1664. /*
  1665. * Look at the hint/sense to see if it requires us to redrive
  1666. * inquiry (i.e. the Unit attention is due to the WWN changing).
  1667. */
  1668. if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
  1669. /* Can't hold mutex across process_sense/read_cap16,
  1670. * since we could have an intervening EEH event.
  1671. */
  1672. ctxi->unavail = true;
  1673. mutex_unlock(&ctxi->mutex);
  1674. rc = process_sense(sdev, verify);
  1675. if (unlikely(rc)) {
  1676. dev_err(dev, "%s: Failed to validate sense data (%d)\n",
  1677. __func__, rc);
  1678. mutex_lock(&ctxi->mutex);
  1679. ctxi->unavail = false;
  1680. goto out;
  1681. }
  1682. mutex_lock(&ctxi->mutex);
  1683. ctxi->unavail = false;
  1684. }
  1685. switch (gli->mode) {
  1686. case MODE_PHYSICAL:
  1687. last_lba = gli->max_lba;
  1688. break;
  1689. case MODE_VIRTUAL:
  1690. /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
  1691. last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
  1692. last_lba /= CXLFLASH_BLOCK_SIZE;
  1693. last_lba--;
  1694. break;
  1695. default:
  1696. WARN(1, "Unsupported LUN mode!");
  1697. }
  1698. verify->last_lba = last_lba;
  1699. out:
  1700. if (likely(ctxi))
  1701. put_context(ctxi);
  1702. dev_dbg(dev, "%s: returning rc=%d llba=%llx\n",
  1703. __func__, rc, verify->last_lba);
  1704. return rc;
  1705. }
  1706. /**
  1707. * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
  1708. * @cmd: The ioctl command to decode.
  1709. *
  1710. * Return: A string identifying the decoded ioctl.
  1711. */
  1712. static char *decode_ioctl(unsigned int cmd)
  1713. {
  1714. switch (cmd) {
  1715. case DK_CXLFLASH_ATTACH:
  1716. return __stringify_1(DK_CXLFLASH_ATTACH);
  1717. case DK_CXLFLASH_USER_DIRECT:
  1718. return __stringify_1(DK_CXLFLASH_USER_DIRECT);
  1719. case DK_CXLFLASH_USER_VIRTUAL:
  1720. return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
  1721. case DK_CXLFLASH_VLUN_RESIZE:
  1722. return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
  1723. case DK_CXLFLASH_RELEASE:
  1724. return __stringify_1(DK_CXLFLASH_RELEASE);
  1725. case DK_CXLFLASH_DETACH:
  1726. return __stringify_1(DK_CXLFLASH_DETACH);
  1727. case DK_CXLFLASH_VERIFY:
  1728. return __stringify_1(DK_CXLFLASH_VERIFY);
  1729. case DK_CXLFLASH_VLUN_CLONE:
  1730. return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
  1731. case DK_CXLFLASH_RECOVER_AFU:
  1732. return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
  1733. case DK_CXLFLASH_MANAGE_LUN:
  1734. return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
  1735. }
  1736. return "UNKNOWN";
  1737. }
  1738. /**
  1739. * cxlflash_disk_direct_open() - opens a direct (physical) disk
  1740. * @sdev: SCSI device associated with LUN.
  1741. * @arg: UDirect ioctl data structure.
  1742. *
  1743. * On successful return, the user is informed of the resource handle
  1744. * to be used to identify the direct lun and the size (in blocks) of
  1745. * the direct lun in last LBA format.
  1746. *
  1747. * Return: 0 on success, -errno on failure
  1748. */
  1749. static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
  1750. {
  1751. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1752. struct device *dev = &cfg->dev->dev;
  1753. struct afu *afu = cfg->afu;
  1754. struct llun_info *lli = sdev->hostdata;
  1755. struct glun_info *gli = lli->parent;
  1756. struct dk_cxlflash_release rel = { { 0 }, 0 };
  1757. struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
  1758. u64 ctxid = DECODE_CTXID(pphys->context_id),
  1759. rctxid = pphys->context_id;
  1760. u64 lun_size = 0;
  1761. u64 last_lba = 0;
  1762. u64 rsrc_handle = -1;
  1763. u32 port = CHAN2PORTMASK(sdev->channel);
  1764. int rc = 0;
  1765. struct ctx_info *ctxi = NULL;
  1766. struct sisl_rht_entry *rhte = NULL;
  1767. dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
  1768. rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
  1769. if (unlikely(rc)) {
  1770. dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__);
  1771. goto out;
  1772. }
  1773. ctxi = get_context(cfg, rctxid, lli, 0);
  1774. if (unlikely(!ctxi)) {
  1775. dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  1776. rc = -EINVAL;
  1777. goto err1;
  1778. }
  1779. rhte = rhte_checkout(ctxi, lli);
  1780. if (unlikely(!rhte)) {
  1781. dev_dbg(dev, "%s: Too many opens ctxid=%lld\n",
  1782. __func__, ctxid);
  1783. rc = -EMFILE; /* too many opens */
  1784. goto err1;
  1785. }
  1786. rsrc_handle = (rhte - ctxi->rht_start);
  1787. rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
  1788. last_lba = gli->max_lba;
  1789. pphys->hdr.return_flags = 0;
  1790. pphys->last_lba = last_lba;
  1791. pphys->rsrc_handle = rsrc_handle;
  1792. rc = cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
  1793. if (unlikely(rc)) {
  1794. dev_dbg(dev, "%s: AFU sync failed rc=%d\n", __func__, rc);
  1795. goto err2;
  1796. }
  1797. out:
  1798. if (likely(ctxi))
  1799. put_context(ctxi);
  1800. dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
  1801. __func__, rsrc_handle, rc, last_lba);
  1802. return rc;
  1803. err2:
  1804. marshal_udir_to_rele(pphys, &rel);
  1805. _cxlflash_disk_release(sdev, ctxi, &rel);
  1806. goto out;
  1807. err1:
  1808. cxlflash_lun_detach(gli);
  1809. goto out;
  1810. }
  1811. /**
  1812. * ioctl_common() - common IOCTL handler for driver
  1813. * @sdev: SCSI device associated with LUN.
  1814. * @cmd: IOCTL command.
  1815. *
  1816. * Handles common fencing operations that are valid for multiple ioctls. Always
  1817. * allow through ioctls that are cleanup oriented in nature, even when operating
  1818. * in a failed/terminating state.
  1819. *
  1820. * Return: 0 on success, -errno on failure
  1821. */
  1822. static int ioctl_common(struct scsi_device *sdev, unsigned int cmd)
  1823. {
  1824. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1825. struct device *dev = &cfg->dev->dev;
  1826. struct llun_info *lli = sdev->hostdata;
  1827. int rc = 0;
  1828. if (unlikely(!lli)) {
  1829. dev_dbg(dev, "%s: Unknown LUN\n", __func__);
  1830. rc = -EINVAL;
  1831. goto out;
  1832. }
  1833. rc = check_state(cfg);
  1834. if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
  1835. switch (cmd) {
  1836. case DK_CXLFLASH_VLUN_RESIZE:
  1837. case DK_CXLFLASH_RELEASE:
  1838. case DK_CXLFLASH_DETACH:
  1839. dev_dbg(dev, "%s: Command override rc=%d\n",
  1840. __func__, rc);
  1841. rc = 0;
  1842. break;
  1843. }
  1844. }
  1845. out:
  1846. return rc;
  1847. }
  1848. /**
  1849. * cxlflash_ioctl() - IOCTL handler for driver
  1850. * @sdev: SCSI device associated with LUN.
  1851. * @cmd: IOCTL command.
  1852. * @arg: Userspace ioctl data structure.
  1853. *
  1854. * A read/write semaphore is used to implement a 'drain' of currently
  1855. * running ioctls. The read semaphore is taken at the beginning of each
  1856. * ioctl thread and released upon concluding execution. Additionally the
  1857. * semaphore should be released and then reacquired in any ioctl execution
  1858. * path which will wait for an event to occur that is outside the scope of
  1859. * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
  1860. * a thread simply needs to acquire the write semaphore.
  1861. *
  1862. * Return: 0 on success, -errno on failure
  1863. */
  1864. int cxlflash_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
  1865. {
  1866. typedef int (*sioctl) (struct scsi_device *, void *);
  1867. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1868. struct device *dev = &cfg->dev->dev;
  1869. struct afu *afu = cfg->afu;
  1870. struct dk_cxlflash_hdr *hdr;
  1871. char buf[sizeof(union cxlflash_ioctls)];
  1872. size_t size = 0;
  1873. bool known_ioctl = false;
  1874. int idx;
  1875. int rc = 0;
  1876. struct Scsi_Host *shost = sdev->host;
  1877. sioctl do_ioctl = NULL;
  1878. static const struct {
  1879. size_t size;
  1880. sioctl ioctl;
  1881. } ioctl_tbl[] = { /* NOTE: order matters here */
  1882. {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
  1883. {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
  1884. {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
  1885. {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
  1886. {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
  1887. {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
  1888. {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
  1889. {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
  1890. {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
  1891. {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
  1892. };
  1893. /* Hold read semaphore so we can drain if needed */
  1894. down_read(&cfg->ioctl_rwsem);
  1895. /* Restrict command set to physical support only for internal LUN */
  1896. if (afu->internal_lun)
  1897. switch (cmd) {
  1898. case DK_CXLFLASH_RELEASE:
  1899. case DK_CXLFLASH_USER_VIRTUAL:
  1900. case DK_CXLFLASH_VLUN_RESIZE:
  1901. case DK_CXLFLASH_VLUN_CLONE:
  1902. dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
  1903. __func__, decode_ioctl(cmd), afu->internal_lun);
  1904. rc = -EINVAL;
  1905. goto cxlflash_ioctl_exit;
  1906. }
  1907. switch (cmd) {
  1908. case DK_CXLFLASH_ATTACH:
  1909. case DK_CXLFLASH_USER_DIRECT:
  1910. case DK_CXLFLASH_RELEASE:
  1911. case DK_CXLFLASH_DETACH:
  1912. case DK_CXLFLASH_VERIFY:
  1913. case DK_CXLFLASH_RECOVER_AFU:
  1914. case DK_CXLFLASH_USER_VIRTUAL:
  1915. case DK_CXLFLASH_VLUN_RESIZE:
  1916. case DK_CXLFLASH_VLUN_CLONE:
  1917. dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
  1918. __func__, decode_ioctl(cmd), cmd, shost->host_no,
  1919. sdev->channel, sdev->id, sdev->lun);
  1920. rc = ioctl_common(sdev, cmd);
  1921. if (unlikely(rc))
  1922. goto cxlflash_ioctl_exit;
  1923. fallthrough;
  1924. case DK_CXLFLASH_MANAGE_LUN:
  1925. known_ioctl = true;
  1926. idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
  1927. size = ioctl_tbl[idx].size;
  1928. do_ioctl = ioctl_tbl[idx].ioctl;
  1929. if (likely(do_ioctl))
  1930. break;
  1931. fallthrough;
  1932. default:
  1933. rc = -EINVAL;
  1934. goto cxlflash_ioctl_exit;
  1935. }
  1936. if (unlikely(copy_from_user(&buf, arg, size))) {
  1937. dev_err(dev, "%s: copy_from_user() fail size=%lu cmd=%u (%s) arg=%p\n",
  1938. __func__, size, cmd, decode_ioctl(cmd), arg);
  1939. rc = -EFAULT;
  1940. goto cxlflash_ioctl_exit;
  1941. }
  1942. hdr = (struct dk_cxlflash_hdr *)&buf;
  1943. if (hdr->version != DK_CXLFLASH_VERSION_0) {
  1944. dev_dbg(dev, "%s: Version %u not supported for %s\n",
  1945. __func__, hdr->version, decode_ioctl(cmd));
  1946. rc = -EINVAL;
  1947. goto cxlflash_ioctl_exit;
  1948. }
  1949. if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
  1950. dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
  1951. rc = -EINVAL;
  1952. goto cxlflash_ioctl_exit;
  1953. }
  1954. rc = do_ioctl(sdev, (void *)&buf);
  1955. if (likely(!rc))
  1956. if (unlikely(copy_to_user(arg, &buf, size))) {
  1957. dev_err(dev, "%s: copy_to_user() fail size=%lu cmd=%u (%s) arg=%p\n",
  1958. __func__, size, cmd, decode_ioctl(cmd), arg);
  1959. rc = -EFAULT;
  1960. }
  1961. /* fall through to exit */
  1962. cxlflash_ioctl_exit:
  1963. up_read(&cfg->ioctl_rwsem);
  1964. if (unlikely(rc && known_ioctl))
  1965. dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
  1966. "returned rc %d\n", __func__,
  1967. decode_ioctl(cmd), cmd, shost->host_no,
  1968. sdev->channel, sdev->id, sdev->lun, rc);
  1969. else
  1970. dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
  1971. "returned rc %d\n", __func__, decode_ioctl(cmd),
  1972. cmd, shost->host_no, sdev->channel, sdev->id,
  1973. sdev->lun, rc);
  1974. return rc;
  1975. }