superpipe.c 59 KB

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