main.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  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/list.h>
  16. #include <linux/module.h>
  17. #include <linux/pci.h>
  18. #include <asm/unaligned.h>
  19. #include <misc/cxl.h>
  20. #include <scsi/scsi_cmnd.h>
  21. #include <scsi/scsi_host.h>
  22. #include <uapi/scsi/cxlflash_ioctl.h>
  23. #include "main.h"
  24. #include "sislite.h"
  25. #include "common.h"
  26. MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
  27. MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
  28. MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
  29. MODULE_LICENSE("GPL");
  30. /**
  31. * cmd_checkout() - checks out an AFU command
  32. * @afu: AFU to checkout from.
  33. *
  34. * Commands are checked out in a round-robin fashion. Note that since
  35. * the command pool is larger than the hardware queue, the majority of
  36. * times we will only loop once or twice before getting a command. The
  37. * buffer and CDB within the command are initialized (zeroed) prior to
  38. * returning.
  39. *
  40. * Return: The checked out command or NULL when command pool is empty.
  41. */
  42. static struct afu_cmd *cmd_checkout(struct afu *afu)
  43. {
  44. int k, dec = CXLFLASH_NUM_CMDS;
  45. struct afu_cmd *cmd;
  46. while (dec--) {
  47. k = (afu->cmd_couts++ & (CXLFLASH_NUM_CMDS - 1));
  48. cmd = &afu->cmd[k];
  49. if (!atomic_dec_if_positive(&cmd->free)) {
  50. pr_devel("%s: returning found index=%d cmd=%p\n",
  51. __func__, cmd->slot, cmd);
  52. memset(cmd->buf, 0, CMD_BUFSIZE);
  53. memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
  54. return cmd;
  55. }
  56. }
  57. return NULL;
  58. }
  59. /**
  60. * cmd_checkin() - checks in an AFU command
  61. * @cmd: AFU command to checkin.
  62. *
  63. * Safe to pass commands that have already been checked in. Several
  64. * internal tracking fields are reset as part of the checkin. Note
  65. * that these are intentionally reset prior to toggling the free bit
  66. * to avoid clobbering values in the event that the command is checked
  67. * out right away.
  68. */
  69. static void cmd_checkin(struct afu_cmd *cmd)
  70. {
  71. cmd->rcb.scp = NULL;
  72. cmd->rcb.timeout = 0;
  73. cmd->sa.ioasc = 0;
  74. cmd->cmd_tmf = false;
  75. cmd->sa.host_use[0] = 0; /* clears both completion and retry bytes */
  76. if (unlikely(atomic_inc_return(&cmd->free) != 1)) {
  77. pr_err("%s: Freeing cmd (%d) that is not in use!\n",
  78. __func__, cmd->slot);
  79. return;
  80. }
  81. pr_devel("%s: released cmd %p index=%d\n", __func__, cmd, cmd->slot);
  82. }
  83. /**
  84. * process_cmd_err() - command error handler
  85. * @cmd: AFU command that experienced the error.
  86. * @scp: SCSI command associated with the AFU command in error.
  87. *
  88. * Translates error bits from AFU command to SCSI command results.
  89. */
  90. static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
  91. {
  92. struct sisl_ioarcb *ioarcb;
  93. struct sisl_ioasa *ioasa;
  94. u32 resid;
  95. if (unlikely(!cmd))
  96. return;
  97. ioarcb = &(cmd->rcb);
  98. ioasa = &(cmd->sa);
  99. if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
  100. resid = ioasa->resid;
  101. scsi_set_resid(scp, resid);
  102. pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
  103. __func__, cmd, scp, resid);
  104. }
  105. if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
  106. pr_debug("%s: cmd underrun cmd = %p scp = %p\n",
  107. __func__, cmd, scp);
  108. scp->result = (DID_ERROR << 16);
  109. }
  110. pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d "
  111. "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n",
  112. __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc,
  113. ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra,
  114. ioasa->fc_extra);
  115. if (ioasa->rc.scsi_rc) {
  116. /* We have a SCSI status */
  117. if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
  118. memcpy(scp->sense_buffer, ioasa->sense_data,
  119. SISL_SENSE_DATA_LEN);
  120. scp->result = ioasa->rc.scsi_rc;
  121. } else
  122. scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
  123. }
  124. /*
  125. * We encountered an error. Set scp->result based on nature
  126. * of error.
  127. */
  128. if (ioasa->rc.fc_rc) {
  129. /* We have an FC status */
  130. switch (ioasa->rc.fc_rc) {
  131. case SISL_FC_RC_LINKDOWN:
  132. scp->result = (DID_REQUEUE << 16);
  133. break;
  134. case SISL_FC_RC_RESID:
  135. /* This indicates an FCP resid underrun */
  136. if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
  137. /* If the SISL_RC_FLAGS_OVERRUN flag was set,
  138. * then we will handle this error else where.
  139. * If not then we must handle it here.
  140. * This is probably an AFU bug.
  141. */
  142. scp->result = (DID_ERROR << 16);
  143. }
  144. break;
  145. case SISL_FC_RC_RESIDERR:
  146. /* Resid mismatch between adapter and device */
  147. case SISL_FC_RC_TGTABORT:
  148. case SISL_FC_RC_ABORTOK:
  149. case SISL_FC_RC_ABORTFAIL:
  150. case SISL_FC_RC_NOLOGI:
  151. case SISL_FC_RC_ABORTPEND:
  152. case SISL_FC_RC_WRABORTPEND:
  153. case SISL_FC_RC_NOEXP:
  154. case SISL_FC_RC_INUSE:
  155. scp->result = (DID_ERROR << 16);
  156. break;
  157. }
  158. }
  159. if (ioasa->rc.afu_rc) {
  160. /* We have an AFU error */
  161. switch (ioasa->rc.afu_rc) {
  162. case SISL_AFU_RC_NO_CHANNELS:
  163. scp->result = (DID_NO_CONNECT << 16);
  164. break;
  165. case SISL_AFU_RC_DATA_DMA_ERR:
  166. switch (ioasa->afu_extra) {
  167. case SISL_AFU_DMA_ERR_PAGE_IN:
  168. /* Retry */
  169. scp->result = (DID_IMM_RETRY << 16);
  170. break;
  171. case SISL_AFU_DMA_ERR_INVALID_EA:
  172. default:
  173. scp->result = (DID_ERROR << 16);
  174. }
  175. break;
  176. case SISL_AFU_RC_OUT_OF_DATA_BUFS:
  177. /* Retry */
  178. scp->result = (DID_ALLOC_FAILURE << 16);
  179. break;
  180. default:
  181. scp->result = (DID_ERROR << 16);
  182. }
  183. }
  184. }
  185. /**
  186. * cmd_complete() - command completion handler
  187. * @cmd: AFU command that has completed.
  188. *
  189. * Prepares and submits command that has either completed or timed out to
  190. * the SCSI stack. Checks AFU command back into command pool for non-internal
  191. * (rcb.scp populated) commands.
  192. */
  193. static void cmd_complete(struct afu_cmd *cmd)
  194. {
  195. struct scsi_cmnd *scp;
  196. ulong lock_flags;
  197. struct afu *afu = cmd->parent;
  198. struct cxlflash_cfg *cfg = afu->parent;
  199. bool cmd_is_tmf;
  200. spin_lock_irqsave(&cmd->slock, lock_flags);
  201. cmd->sa.host_use_b[0] |= B_DONE;
  202. spin_unlock_irqrestore(&cmd->slock, lock_flags);
  203. if (cmd->rcb.scp) {
  204. scp = cmd->rcb.scp;
  205. if (unlikely(cmd->sa.ioasc))
  206. process_cmd_err(cmd, scp);
  207. else
  208. scp->result = (DID_OK << 16);
  209. cmd_is_tmf = cmd->cmd_tmf;
  210. cmd_checkin(cmd); /* Don't use cmd after here */
  211. pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X "
  212. "ioasc=%d\n", __func__, scp, scp->result,
  213. cmd->sa.ioasc);
  214. scsi_dma_unmap(scp);
  215. scp->scsi_done(scp);
  216. if (cmd_is_tmf) {
  217. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  218. cfg->tmf_active = false;
  219. wake_up_all_locked(&cfg->tmf_waitq);
  220. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  221. }
  222. } else
  223. complete(&cmd->cevent);
  224. }
  225. /**
  226. * context_reset() - timeout handler for AFU commands
  227. * @cmd: AFU command that timed out.
  228. *
  229. * Sends a reset to the AFU.
  230. */
  231. static void context_reset(struct afu_cmd *cmd)
  232. {
  233. int nretry = 0;
  234. u64 rrin = 0x1;
  235. u64 room = 0;
  236. struct afu *afu = cmd->parent;
  237. ulong lock_flags;
  238. pr_debug("%s: cmd=%p\n", __func__, cmd);
  239. spin_lock_irqsave(&cmd->slock, lock_flags);
  240. /* Already completed? */
  241. if (cmd->sa.host_use_b[0] & B_DONE) {
  242. spin_unlock_irqrestore(&cmd->slock, lock_flags);
  243. return;
  244. }
  245. cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT);
  246. spin_unlock_irqrestore(&cmd->slock, lock_flags);
  247. /*
  248. * We really want to send this reset at all costs, so spread
  249. * out wait time on successive retries for available room.
  250. */
  251. do {
  252. room = readq_be(&afu->host_map->cmd_room);
  253. atomic64_set(&afu->room, room);
  254. if (room)
  255. goto write_rrin;
  256. udelay(1 << nretry);
  257. } while (nretry++ < MC_ROOM_RETRY_CNT);
  258. pr_err("%s: no cmd_room to send reset\n", __func__);
  259. return;
  260. write_rrin:
  261. nretry = 0;
  262. writeq_be(rrin, &afu->host_map->ioarrin);
  263. do {
  264. rrin = readq_be(&afu->host_map->ioarrin);
  265. if (rrin != 0x1)
  266. break;
  267. /* Double delay each time */
  268. udelay(1 << nretry);
  269. } while (nretry++ < MC_ROOM_RETRY_CNT);
  270. }
  271. /**
  272. * send_cmd() - sends an AFU command
  273. * @afu: AFU associated with the host.
  274. * @cmd: AFU command to send.
  275. *
  276. * Return:
  277. * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  278. */
  279. static int send_cmd(struct afu *afu, struct afu_cmd *cmd)
  280. {
  281. struct cxlflash_cfg *cfg = afu->parent;
  282. struct device *dev = &cfg->dev->dev;
  283. int nretry = 0;
  284. int rc = 0;
  285. u64 room;
  286. long newval;
  287. /*
  288. * This routine is used by critical users such an AFU sync and to
  289. * send a task management function (TMF). Thus we want to retry a
  290. * bit before returning an error. To avoid the performance penalty
  291. * of MMIO, we spread the update of 'room' over multiple commands.
  292. */
  293. retry:
  294. newval = atomic64_dec_if_positive(&afu->room);
  295. if (!newval) {
  296. do {
  297. room = readq_be(&afu->host_map->cmd_room);
  298. atomic64_set(&afu->room, room);
  299. if (room)
  300. goto write_ioarrin;
  301. udelay(1 << nretry);
  302. } while (nretry++ < MC_ROOM_RETRY_CNT);
  303. dev_err(dev, "%s: no cmd_room to send 0x%X\n",
  304. __func__, cmd->rcb.cdb[0]);
  305. goto no_room;
  306. } else if (unlikely(newval < 0)) {
  307. /* This should be rare. i.e. Only if two threads race and
  308. * decrement before the MMIO read is done. In this case
  309. * just benefit from the other thread having updated
  310. * afu->room.
  311. */
  312. if (nretry++ < MC_ROOM_RETRY_CNT) {
  313. udelay(1 << nretry);
  314. goto retry;
  315. }
  316. goto no_room;
  317. }
  318. write_ioarrin:
  319. writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin);
  320. out:
  321. pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd,
  322. cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc);
  323. return rc;
  324. no_room:
  325. afu->read_room = true;
  326. kref_get(&cfg->afu->mapcount);
  327. schedule_work(&cfg->work_q);
  328. rc = SCSI_MLQUEUE_HOST_BUSY;
  329. goto out;
  330. }
  331. /**
  332. * wait_resp() - polls for a response or timeout to a sent AFU command
  333. * @afu: AFU associated with the host.
  334. * @cmd: AFU command that was sent.
  335. */
  336. static void wait_resp(struct afu *afu, struct afu_cmd *cmd)
  337. {
  338. ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
  339. timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
  340. if (!timeout)
  341. context_reset(cmd);
  342. if (unlikely(cmd->sa.ioasc != 0))
  343. pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, "
  344. "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0],
  345. cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc,
  346. cmd->sa.rc.fc_rc);
  347. }
  348. /**
  349. * send_tmf() - sends a Task Management Function (TMF)
  350. * @afu: AFU to checkout from.
  351. * @scp: SCSI command from stack.
  352. * @tmfcmd: TMF command to send.
  353. *
  354. * Return:
  355. * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  356. */
  357. static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd)
  358. {
  359. struct afu_cmd *cmd;
  360. u32 port_sel = scp->device->channel + 1;
  361. short lflag = 0;
  362. struct Scsi_Host *host = scp->device->host;
  363. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  364. struct device *dev = &cfg->dev->dev;
  365. ulong lock_flags;
  366. int rc = 0;
  367. ulong to;
  368. cmd = cmd_checkout(afu);
  369. if (unlikely(!cmd)) {
  370. dev_err(dev, "%s: could not get a free command\n", __func__);
  371. rc = SCSI_MLQUEUE_HOST_BUSY;
  372. goto out;
  373. }
  374. /* When Task Management Function is active do not send another */
  375. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  376. if (cfg->tmf_active)
  377. wait_event_interruptible_lock_irq(cfg->tmf_waitq,
  378. !cfg->tmf_active,
  379. cfg->tmf_slock);
  380. cfg->tmf_active = true;
  381. cmd->cmd_tmf = true;
  382. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  383. cmd->rcb.ctx_id = afu->ctx_hndl;
  384. cmd->rcb.port_sel = port_sel;
  385. cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
  386. lflag = SISL_REQ_FLAGS_TMF_CMD;
  387. cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
  388. SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
  389. /* Stash the scp in the reserved field, for reuse during interrupt */
  390. cmd->rcb.scp = scp;
  391. /* Copy the CDB from the cmd passed in */
  392. memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
  393. /* Send the command */
  394. rc = send_cmd(afu, cmd);
  395. if (unlikely(rc)) {
  396. cmd_checkin(cmd);
  397. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  398. cfg->tmf_active = false;
  399. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  400. goto out;
  401. }
  402. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  403. to = msecs_to_jiffies(5000);
  404. to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
  405. !cfg->tmf_active,
  406. cfg->tmf_slock,
  407. to);
  408. if (!to) {
  409. cfg->tmf_active = false;
  410. dev_err(dev, "%s: TMF timed out!\n", __func__);
  411. rc = -1;
  412. }
  413. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  414. out:
  415. return rc;
  416. }
  417. static void afu_unmap(struct kref *ref)
  418. {
  419. struct afu *afu = container_of(ref, struct afu, mapcount);
  420. if (likely(afu->afu_map)) {
  421. cxl_psa_unmap((void __iomem *)afu->afu_map);
  422. afu->afu_map = NULL;
  423. }
  424. }
  425. /**
  426. * cxlflash_driver_info() - information handler for this host driver
  427. * @host: SCSI host associated with device.
  428. *
  429. * Return: A string describing the device.
  430. */
  431. static const char *cxlflash_driver_info(struct Scsi_Host *host)
  432. {
  433. return CXLFLASH_ADAPTER_NAME;
  434. }
  435. /**
  436. * cxlflash_queuecommand() - sends a mid-layer request
  437. * @host: SCSI host associated with device.
  438. * @scp: SCSI command to send.
  439. *
  440. * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  441. */
  442. static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
  443. {
  444. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  445. struct afu *afu = cfg->afu;
  446. struct device *dev = &cfg->dev->dev;
  447. struct afu_cmd *cmd;
  448. u32 port_sel = scp->device->channel + 1;
  449. int nseg, i, ncount;
  450. struct scatterlist *sg;
  451. ulong lock_flags;
  452. short lflag = 0;
  453. int rc = 0;
  454. int kref_got = 0;
  455. dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
  456. "cdb=(%08X-%08X-%08X-%08X)\n",
  457. __func__, scp, host->host_no, scp->device->channel,
  458. scp->device->id, scp->device->lun,
  459. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  460. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  461. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  462. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  463. /*
  464. * If a Task Management Function is active, wait for it to complete
  465. * before continuing with regular commands.
  466. */
  467. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  468. if (cfg->tmf_active) {
  469. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  470. rc = SCSI_MLQUEUE_HOST_BUSY;
  471. goto out;
  472. }
  473. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  474. switch (cfg->state) {
  475. case STATE_RESET:
  476. dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__);
  477. rc = SCSI_MLQUEUE_HOST_BUSY;
  478. goto out;
  479. case STATE_FAILTERM:
  480. dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__);
  481. scp->result = (DID_NO_CONNECT << 16);
  482. scp->scsi_done(scp);
  483. rc = 0;
  484. goto out;
  485. default:
  486. break;
  487. }
  488. cmd = cmd_checkout(afu);
  489. if (unlikely(!cmd)) {
  490. dev_err(dev, "%s: could not get a free command\n", __func__);
  491. rc = SCSI_MLQUEUE_HOST_BUSY;
  492. goto out;
  493. }
  494. kref_get(&cfg->afu->mapcount);
  495. kref_got = 1;
  496. cmd->rcb.ctx_id = afu->ctx_hndl;
  497. cmd->rcb.port_sel = port_sel;
  498. cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
  499. if (scp->sc_data_direction == DMA_TO_DEVICE)
  500. lflag = SISL_REQ_FLAGS_HOST_WRITE;
  501. else
  502. lflag = SISL_REQ_FLAGS_HOST_READ;
  503. cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
  504. SISL_REQ_FLAGS_SUP_UNDERRUN | lflag);
  505. /* Stash the scp in the reserved field, for reuse during interrupt */
  506. cmd->rcb.scp = scp;
  507. nseg = scsi_dma_map(scp);
  508. if (unlikely(nseg < 0)) {
  509. dev_err(dev, "%s: Fail DMA map! nseg=%d\n",
  510. __func__, nseg);
  511. rc = SCSI_MLQUEUE_HOST_BUSY;
  512. goto out;
  513. }
  514. ncount = scsi_sg_count(scp);
  515. scsi_for_each_sg(scp, sg, ncount, i) {
  516. cmd->rcb.data_len = sg_dma_len(sg);
  517. cmd->rcb.data_ea = sg_dma_address(sg);
  518. }
  519. /* Copy the CDB from the scsi_cmnd passed in */
  520. memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
  521. /* Send the command */
  522. rc = send_cmd(afu, cmd);
  523. if (unlikely(rc)) {
  524. cmd_checkin(cmd);
  525. scsi_dma_unmap(scp);
  526. }
  527. out:
  528. if (kref_got)
  529. kref_put(&afu->mapcount, afu_unmap);
  530. pr_devel("%s: returning rc=%d\n", __func__, rc);
  531. return rc;
  532. }
  533. /**
  534. * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
  535. * @cfg: Internal structure associated with the host.
  536. */
  537. static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
  538. {
  539. struct pci_dev *pdev = cfg->dev;
  540. if (pci_channel_offline(pdev))
  541. wait_event_timeout(cfg->reset_waitq,
  542. !pci_channel_offline(pdev),
  543. CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
  544. }
  545. /**
  546. * free_mem() - free memory associated with the AFU
  547. * @cfg: Internal structure associated with the host.
  548. */
  549. static void free_mem(struct cxlflash_cfg *cfg)
  550. {
  551. int i;
  552. char *buf = NULL;
  553. struct afu *afu = cfg->afu;
  554. if (cfg->afu) {
  555. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  556. buf = afu->cmd[i].buf;
  557. if (!((u64)buf & (PAGE_SIZE - 1)))
  558. free_page((ulong)buf);
  559. }
  560. free_pages((ulong)afu, get_order(sizeof(struct afu)));
  561. cfg->afu = NULL;
  562. }
  563. }
  564. /**
  565. * stop_afu() - stops the AFU command timers and unmaps the MMIO space
  566. * @cfg: Internal structure associated with the host.
  567. *
  568. * Safe to call with AFU in a partially allocated/initialized state.
  569. *
  570. * Cleans up all state associated with the command queue, and unmaps
  571. * the MMIO space.
  572. *
  573. * - complete() will take care of commands we initiated (they'll be checked
  574. * in as part of the cleanup that occurs after the completion)
  575. *
  576. * - cmd_checkin() will take care of entries that we did not initiate and that
  577. * have not (and will not) complete because they are sitting on a [now stale]
  578. * hardware queue
  579. */
  580. static void stop_afu(struct cxlflash_cfg *cfg)
  581. {
  582. int i;
  583. struct afu *afu = cfg->afu;
  584. struct afu_cmd *cmd;
  585. if (likely(afu)) {
  586. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  587. cmd = &afu->cmd[i];
  588. complete(&cmd->cevent);
  589. if (!atomic_read(&cmd->free))
  590. cmd_checkin(cmd);
  591. }
  592. if (likely(afu->afu_map)) {
  593. cxl_psa_unmap((void __iomem *)afu->afu_map);
  594. afu->afu_map = NULL;
  595. }
  596. kref_put(&afu->mapcount, afu_unmap);
  597. }
  598. }
  599. /**
  600. * term_mc() - terminates the master context
  601. * @cfg: Internal structure associated with the host.
  602. * @level: Depth of allocation, where to begin waterfall tear down.
  603. *
  604. * Safe to call with AFU/MC in partially allocated/initialized state.
  605. */
  606. static void term_mc(struct cxlflash_cfg *cfg, enum undo_level level)
  607. {
  608. int rc = 0;
  609. struct afu *afu = cfg->afu;
  610. struct device *dev = &cfg->dev->dev;
  611. if (!afu || !cfg->mcctx) {
  612. dev_err(dev, "%s: returning from term_mc with NULL afu or MC\n",
  613. __func__);
  614. return;
  615. }
  616. switch (level) {
  617. case UNDO_START:
  618. rc = cxl_stop_context(cfg->mcctx);
  619. BUG_ON(rc);
  620. case UNMAP_THREE:
  621. cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
  622. case UNMAP_TWO:
  623. cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
  624. case UNMAP_ONE:
  625. cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
  626. case FREE_IRQ:
  627. cxl_free_afu_irqs(cfg->mcctx);
  628. case RELEASE_CONTEXT:
  629. cfg->mcctx = NULL;
  630. }
  631. }
  632. /**
  633. * term_afu() - terminates the AFU
  634. * @cfg: Internal structure associated with the host.
  635. *
  636. * Safe to call with AFU/MC in partially allocated/initialized state.
  637. */
  638. static void term_afu(struct cxlflash_cfg *cfg)
  639. {
  640. term_mc(cfg, UNDO_START);
  641. if (cfg->afu)
  642. stop_afu(cfg);
  643. pr_debug("%s: returning\n", __func__);
  644. }
  645. /**
  646. * cxlflash_remove() - PCI entry point to tear down host
  647. * @pdev: PCI device associated with the host.
  648. *
  649. * Safe to use as a cleanup in partially allocated/initialized state.
  650. */
  651. static void cxlflash_remove(struct pci_dev *pdev)
  652. {
  653. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  654. ulong lock_flags;
  655. /* If a Task Management Function is active, wait for it to complete
  656. * before continuing with remove.
  657. */
  658. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  659. if (cfg->tmf_active)
  660. wait_event_interruptible_lock_irq(cfg->tmf_waitq,
  661. !cfg->tmf_active,
  662. cfg->tmf_slock);
  663. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  664. cfg->state = STATE_FAILTERM;
  665. cxlflash_stop_term_user_contexts(cfg);
  666. switch (cfg->init_state) {
  667. case INIT_STATE_SCSI:
  668. cxlflash_term_local_luns(cfg);
  669. scsi_remove_host(cfg->host);
  670. /* fall through */
  671. case INIT_STATE_AFU:
  672. cancel_work_sync(&cfg->work_q);
  673. term_afu(cfg);
  674. case INIT_STATE_PCI:
  675. pci_release_regions(cfg->dev);
  676. pci_disable_device(pdev);
  677. case INIT_STATE_NONE:
  678. free_mem(cfg);
  679. scsi_host_put(cfg->host);
  680. break;
  681. }
  682. pr_debug("%s: returning\n", __func__);
  683. }
  684. /**
  685. * alloc_mem() - allocates the AFU and its command pool
  686. * @cfg: Internal structure associated with the host.
  687. *
  688. * A partially allocated state remains on failure.
  689. *
  690. * Return:
  691. * 0 on success
  692. * -ENOMEM on failure to allocate memory
  693. */
  694. static int alloc_mem(struct cxlflash_cfg *cfg)
  695. {
  696. int rc = 0;
  697. int i;
  698. char *buf = NULL;
  699. struct device *dev = &cfg->dev->dev;
  700. /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
  701. cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  702. get_order(sizeof(struct afu)));
  703. if (unlikely(!cfg->afu)) {
  704. dev_err(dev, "%s: cannot get %d free pages\n",
  705. __func__, get_order(sizeof(struct afu)));
  706. rc = -ENOMEM;
  707. goto out;
  708. }
  709. cfg->afu->parent = cfg;
  710. cfg->afu->afu_map = NULL;
  711. for (i = 0; i < CXLFLASH_NUM_CMDS; buf += CMD_BUFSIZE, i++) {
  712. if (!((u64)buf & (PAGE_SIZE - 1))) {
  713. buf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  714. if (unlikely(!buf)) {
  715. dev_err(dev,
  716. "%s: Allocate command buffers fail!\n",
  717. __func__);
  718. rc = -ENOMEM;
  719. free_mem(cfg);
  720. goto out;
  721. }
  722. }
  723. cfg->afu->cmd[i].buf = buf;
  724. atomic_set(&cfg->afu->cmd[i].free, 1);
  725. cfg->afu->cmd[i].slot = i;
  726. }
  727. out:
  728. return rc;
  729. }
  730. /**
  731. * init_pci() - initializes the host as a PCI device
  732. * @cfg: Internal structure associated with the host.
  733. *
  734. * Return: 0 on success, -errno on failure
  735. */
  736. static int init_pci(struct cxlflash_cfg *cfg)
  737. {
  738. struct pci_dev *pdev = cfg->dev;
  739. int rc = 0;
  740. cfg->cxlflash_regs_pci = pci_resource_start(pdev, 0);
  741. rc = pci_request_regions(pdev, CXLFLASH_NAME);
  742. if (rc < 0) {
  743. dev_err(&pdev->dev,
  744. "%s: Couldn't register memory range of registers\n",
  745. __func__);
  746. goto out;
  747. }
  748. rc = pci_enable_device(pdev);
  749. if (rc || pci_channel_offline(pdev)) {
  750. if (pci_channel_offline(pdev)) {
  751. cxlflash_wait_for_pci_err_recovery(cfg);
  752. rc = pci_enable_device(pdev);
  753. }
  754. if (rc) {
  755. dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
  756. __func__);
  757. cxlflash_wait_for_pci_err_recovery(cfg);
  758. goto out_release_regions;
  759. }
  760. }
  761. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  762. if (rc < 0) {
  763. dev_dbg(&pdev->dev, "%s: Failed to set 64 bit PCI DMA mask\n",
  764. __func__);
  765. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  766. }
  767. if (rc < 0) {
  768. dev_err(&pdev->dev, "%s: Failed to set PCI DMA mask\n",
  769. __func__);
  770. goto out_disable;
  771. }
  772. pci_set_master(pdev);
  773. if (pci_channel_offline(pdev)) {
  774. cxlflash_wait_for_pci_err_recovery(cfg);
  775. if (pci_channel_offline(pdev)) {
  776. rc = -EIO;
  777. goto out_msi_disable;
  778. }
  779. }
  780. rc = pci_save_state(pdev);
  781. if (rc != PCIBIOS_SUCCESSFUL) {
  782. dev_err(&pdev->dev, "%s: Failed to save PCI config space\n",
  783. __func__);
  784. rc = -EIO;
  785. goto cleanup_nolog;
  786. }
  787. out:
  788. pr_debug("%s: returning rc=%d\n", __func__, rc);
  789. return rc;
  790. cleanup_nolog:
  791. out_msi_disable:
  792. cxlflash_wait_for_pci_err_recovery(cfg);
  793. out_disable:
  794. pci_disable_device(pdev);
  795. out_release_regions:
  796. pci_release_regions(pdev);
  797. goto out;
  798. }
  799. /**
  800. * init_scsi() - adds the host to the SCSI stack and kicks off host scan
  801. * @cfg: Internal structure associated with the host.
  802. *
  803. * Return: 0 on success, -errno on failure
  804. */
  805. static int init_scsi(struct cxlflash_cfg *cfg)
  806. {
  807. struct pci_dev *pdev = cfg->dev;
  808. int rc = 0;
  809. rc = scsi_add_host(cfg->host, &pdev->dev);
  810. if (rc) {
  811. dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
  812. __func__, rc);
  813. goto out;
  814. }
  815. scsi_scan_host(cfg->host);
  816. out:
  817. pr_debug("%s: returning rc=%d\n", __func__, rc);
  818. return rc;
  819. }
  820. /**
  821. * set_port_online() - transitions the specified host FC port to online state
  822. * @fc_regs: Top of MMIO region defined for specified port.
  823. *
  824. * The provided MMIO region must be mapped prior to call. Online state means
  825. * that the FC link layer has synced, completed the handshaking process, and
  826. * is ready for login to start.
  827. */
  828. static void set_port_online(__be64 __iomem *fc_regs)
  829. {
  830. u64 cmdcfg;
  831. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  832. cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
  833. cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
  834. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  835. }
  836. /**
  837. * set_port_offline() - transitions the specified host FC port to offline state
  838. * @fc_regs: Top of MMIO region defined for specified port.
  839. *
  840. * The provided MMIO region must be mapped prior to call.
  841. */
  842. static void set_port_offline(__be64 __iomem *fc_regs)
  843. {
  844. u64 cmdcfg;
  845. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  846. cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
  847. cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
  848. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  849. }
  850. /**
  851. * wait_port_online() - waits for the specified host FC port come online
  852. * @fc_regs: Top of MMIO region defined for specified port.
  853. * @delay_us: Number of microseconds to delay between reading port status.
  854. * @nretry: Number of cycles to retry reading port status.
  855. *
  856. * The provided MMIO region must be mapped prior to call. This will timeout
  857. * when the cable is not plugged in.
  858. *
  859. * Return:
  860. * TRUE (1) when the specified port is online
  861. * FALSE (0) when the specified port fails to come online after timeout
  862. * -EINVAL when @delay_us is less than 1000
  863. */
  864. static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  865. {
  866. u64 status;
  867. if (delay_us < 1000) {
  868. pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
  869. return -EINVAL;
  870. }
  871. do {
  872. msleep(delay_us / 1000);
  873. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  874. if (status == U64_MAX)
  875. nretry /= 2;
  876. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
  877. nretry--);
  878. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
  879. }
  880. /**
  881. * wait_port_offline() - waits for the specified host FC port go offline
  882. * @fc_regs: Top of MMIO region defined for specified port.
  883. * @delay_us: Number of microseconds to delay between reading port status.
  884. * @nretry: Number of cycles to retry reading port status.
  885. *
  886. * The provided MMIO region must be mapped prior to call.
  887. *
  888. * Return:
  889. * TRUE (1) when the specified port is offline
  890. * FALSE (0) when the specified port fails to go offline after timeout
  891. * -EINVAL when @delay_us is less than 1000
  892. */
  893. static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  894. {
  895. u64 status;
  896. if (delay_us < 1000) {
  897. pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
  898. return -EINVAL;
  899. }
  900. do {
  901. msleep(delay_us / 1000);
  902. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  903. if (status == U64_MAX)
  904. nretry /= 2;
  905. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
  906. nretry--);
  907. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
  908. }
  909. /**
  910. * afu_set_wwpn() - configures the WWPN for the specified host FC port
  911. * @afu: AFU associated with the host that owns the specified FC port.
  912. * @port: Port number being configured.
  913. * @fc_regs: Top of MMIO region defined for specified port.
  914. * @wwpn: The world-wide-port-number previously discovered for port.
  915. *
  916. * The provided MMIO region must be mapped prior to call. As part of the
  917. * sequence to configure the WWPN, the port is toggled offline and then back
  918. * online. This toggling action can cause this routine to delay up to a few
  919. * seconds. When configured to use the internal LUN feature of the AFU, a
  920. * failure to come online is overridden.
  921. *
  922. * Return:
  923. * 0 when the WWPN is successfully written and the port comes back online
  924. * -1 when the port fails to go offline or come back up online
  925. */
  926. static int afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
  927. u64 wwpn)
  928. {
  929. int rc = 0;
  930. set_port_offline(fc_regs);
  931. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  932. FC_PORT_STATUS_RETRY_CNT)) {
  933. pr_debug("%s: wait on port %d to go offline timed out\n",
  934. __func__, port);
  935. rc = -1; /* but continue on to leave the port back online */
  936. }
  937. if (rc == 0)
  938. writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
  939. /* Always return success after programming WWPN */
  940. rc = 0;
  941. set_port_online(fc_regs);
  942. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  943. FC_PORT_STATUS_RETRY_CNT)) {
  944. pr_err("%s: wait on port %d to go online timed out\n",
  945. __func__, port);
  946. }
  947. pr_debug("%s: returning rc=%d\n", __func__, rc);
  948. return rc;
  949. }
  950. /**
  951. * afu_link_reset() - resets the specified host FC port
  952. * @afu: AFU associated with the host that owns the specified FC port.
  953. * @port: Port number being configured.
  954. * @fc_regs: Top of MMIO region defined for specified port.
  955. *
  956. * The provided MMIO region must be mapped prior to call. The sequence to
  957. * reset the port involves toggling it offline and then back online. This
  958. * action can cause this routine to delay up to a few seconds. An effort
  959. * is made to maintain link with the device by switching to host to use
  960. * the alternate port exclusively while the reset takes place.
  961. * failure to come online is overridden.
  962. */
  963. static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
  964. {
  965. u64 port_sel;
  966. /* first switch the AFU to the other links, if any */
  967. port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
  968. port_sel &= ~(1ULL << port);
  969. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  970. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  971. set_port_offline(fc_regs);
  972. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  973. FC_PORT_STATUS_RETRY_CNT))
  974. pr_err("%s: wait on port %d to go offline timed out\n",
  975. __func__, port);
  976. set_port_online(fc_regs);
  977. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  978. FC_PORT_STATUS_RETRY_CNT))
  979. pr_err("%s: wait on port %d to go online timed out\n",
  980. __func__, port);
  981. /* switch back to include this port */
  982. port_sel |= (1ULL << port);
  983. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  984. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  985. pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
  986. }
  987. /*
  988. * Asynchronous interrupt information table
  989. */
  990. static const struct asyc_intr_info ainfo[] = {
  991. {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
  992. {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
  993. {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
  994. {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
  995. {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
  996. {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
  997. {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
  998. {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
  999. {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
  1000. {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
  1001. {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
  1002. {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
  1003. {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
  1004. {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
  1005. {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
  1006. {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
  1007. {0x0, "", 0, 0} /* terminator */
  1008. };
  1009. /**
  1010. * find_ainfo() - locates and returns asynchronous interrupt information
  1011. * @status: Status code set by AFU on error.
  1012. *
  1013. * Return: The located information or NULL when the status code is invalid.
  1014. */
  1015. static const struct asyc_intr_info *find_ainfo(u64 status)
  1016. {
  1017. const struct asyc_intr_info *info;
  1018. for (info = &ainfo[0]; info->status; info++)
  1019. if (info->status == status)
  1020. return info;
  1021. return NULL;
  1022. }
  1023. /**
  1024. * afu_err_intr_init() - clears and initializes the AFU for error interrupts
  1025. * @afu: AFU associated with the host.
  1026. */
  1027. static void afu_err_intr_init(struct afu *afu)
  1028. {
  1029. int i;
  1030. u64 reg;
  1031. /* global async interrupts: AFU clears afu_ctrl on context exit
  1032. * if async interrupts were sent to that context. This prevents
  1033. * the AFU form sending further async interrupts when
  1034. * there is
  1035. * nobody to receive them.
  1036. */
  1037. /* mask all */
  1038. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
  1039. /* set LISN# to send and point to master context */
  1040. reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
  1041. if (afu->internal_lun)
  1042. reg |= 1; /* Bit 63 indicates local lun */
  1043. writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
  1044. /* clear all */
  1045. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1046. /* unmask bits that are of interest */
  1047. /* note: afu can send an interrupt after this step */
  1048. writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
  1049. /* clear again in case a bit came on after previous clear but before */
  1050. /* unmask */
  1051. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1052. /* Clear/Set internal lun bits */
  1053. reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
  1054. reg &= SISL_FC_INTERNAL_MASK;
  1055. if (afu->internal_lun)
  1056. reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
  1057. writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
  1058. /* now clear FC errors */
  1059. for (i = 0; i < NUM_FC_PORTS; i++) {
  1060. writeq_be(0xFFFFFFFFU,
  1061. &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
  1062. writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
  1063. }
  1064. /* sync interrupts for master's IOARRIN write */
  1065. /* note that unlike asyncs, there can be no pending sync interrupts */
  1066. /* at this time (this is a fresh context and master has not written */
  1067. /* IOARRIN yet), so there is nothing to clear. */
  1068. /* set LISN#, it is always sent to the context that wrote IOARRIN */
  1069. writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
  1070. writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
  1071. }
  1072. /**
  1073. * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
  1074. * @irq: Interrupt number.
  1075. * @data: Private data provided at interrupt registration, the AFU.
  1076. *
  1077. * Return: Always return IRQ_HANDLED.
  1078. */
  1079. static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
  1080. {
  1081. struct afu *afu = (struct afu *)data;
  1082. u64 reg;
  1083. u64 reg_unmasked;
  1084. reg = readq_be(&afu->host_map->intr_status);
  1085. reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
  1086. if (reg_unmasked == 0UL) {
  1087. pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
  1088. __func__, (u64)afu, reg);
  1089. goto cxlflash_sync_err_irq_exit;
  1090. }
  1091. pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
  1092. __func__, (u64)afu, reg);
  1093. writeq_be(reg_unmasked, &afu->host_map->intr_clear);
  1094. cxlflash_sync_err_irq_exit:
  1095. pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
  1096. return IRQ_HANDLED;
  1097. }
  1098. /**
  1099. * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
  1100. * @irq: Interrupt number.
  1101. * @data: Private data provided at interrupt registration, the AFU.
  1102. *
  1103. * Return: Always return IRQ_HANDLED.
  1104. */
  1105. static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
  1106. {
  1107. struct afu *afu = (struct afu *)data;
  1108. struct afu_cmd *cmd;
  1109. bool toggle = afu->toggle;
  1110. u64 entry,
  1111. *hrrq_start = afu->hrrq_start,
  1112. *hrrq_end = afu->hrrq_end,
  1113. *hrrq_curr = afu->hrrq_curr;
  1114. /* Process however many RRQ entries that are ready */
  1115. while (true) {
  1116. entry = *hrrq_curr;
  1117. if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
  1118. break;
  1119. cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
  1120. cmd_complete(cmd);
  1121. /* Advance to next entry or wrap and flip the toggle bit */
  1122. if (hrrq_curr < hrrq_end)
  1123. hrrq_curr++;
  1124. else {
  1125. hrrq_curr = hrrq_start;
  1126. toggle ^= SISL_RESP_HANDLE_T_BIT;
  1127. }
  1128. }
  1129. afu->hrrq_curr = hrrq_curr;
  1130. afu->toggle = toggle;
  1131. return IRQ_HANDLED;
  1132. }
  1133. /**
  1134. * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
  1135. * @irq: Interrupt number.
  1136. * @data: Private data provided at interrupt registration, the AFU.
  1137. *
  1138. * Return: Always return IRQ_HANDLED.
  1139. */
  1140. static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
  1141. {
  1142. struct afu *afu = (struct afu *)data;
  1143. struct cxlflash_cfg *cfg = afu->parent;
  1144. struct device *dev = &cfg->dev->dev;
  1145. u64 reg_unmasked;
  1146. const struct asyc_intr_info *info;
  1147. struct sisl_global_map __iomem *global = &afu->afu_map->global;
  1148. u64 reg;
  1149. u8 port;
  1150. int i;
  1151. reg = readq_be(&global->regs.aintr_status);
  1152. reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
  1153. if (reg_unmasked == 0) {
  1154. dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
  1155. __func__, reg);
  1156. goto out;
  1157. }
  1158. /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
  1159. writeq_be(reg_unmasked, &global->regs.aintr_clear);
  1160. /* Check each bit that is on */
  1161. for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
  1162. info = find_ainfo(1ULL << i);
  1163. if (((reg_unmasked & 0x1) == 0) || !info)
  1164. continue;
  1165. port = info->port;
  1166. dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
  1167. __func__, port, info->desc,
  1168. readq_be(&global->fc_regs[port][FC_STATUS / 8]));
  1169. /*
  1170. * Do link reset first, some OTHER errors will set FC_ERROR
  1171. * again if cleared before or w/o a reset
  1172. */
  1173. if (info->action & LINK_RESET) {
  1174. dev_err(dev, "%s: FC Port %d: resetting link\n",
  1175. __func__, port);
  1176. cfg->lr_state = LINK_RESET_REQUIRED;
  1177. cfg->lr_port = port;
  1178. kref_get(&cfg->afu->mapcount);
  1179. schedule_work(&cfg->work_q);
  1180. }
  1181. if (info->action & CLR_FC_ERROR) {
  1182. reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
  1183. /*
  1184. * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
  1185. * should be the same and tracing one is sufficient.
  1186. */
  1187. dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
  1188. __func__, port, reg);
  1189. writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
  1190. writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
  1191. }
  1192. if (info->action & SCAN_HOST) {
  1193. atomic_inc(&cfg->scan_host_needed);
  1194. kref_get(&cfg->afu->mapcount);
  1195. schedule_work(&cfg->work_q);
  1196. }
  1197. }
  1198. out:
  1199. dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
  1200. return IRQ_HANDLED;
  1201. }
  1202. /**
  1203. * start_context() - starts the master context
  1204. * @cfg: Internal structure associated with the host.
  1205. *
  1206. * Return: A success or failure value from CXL services.
  1207. */
  1208. static int start_context(struct cxlflash_cfg *cfg)
  1209. {
  1210. int rc = 0;
  1211. rc = cxl_start_context(cfg->mcctx,
  1212. cfg->afu->work.work_element_descriptor,
  1213. NULL);
  1214. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1215. return rc;
  1216. }
  1217. /**
  1218. * read_vpd() - obtains the WWPNs from VPD
  1219. * @cfg: Internal structure associated with the host.
  1220. * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
  1221. *
  1222. * Return: 0 on success, -errno on failure
  1223. */
  1224. static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
  1225. {
  1226. struct pci_dev *dev = cfg->parent_dev;
  1227. int rc = 0;
  1228. int ro_start, ro_size, i, j, k;
  1229. ssize_t vpd_size;
  1230. char vpd_data[CXLFLASH_VPD_LEN];
  1231. char tmp_buf[WWPN_BUF_LEN] = { 0 };
  1232. char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
  1233. /* Get the VPD data from the device */
  1234. vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
  1235. if (unlikely(vpd_size <= 0)) {
  1236. dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
  1237. __func__, vpd_size);
  1238. rc = -ENODEV;
  1239. goto out;
  1240. }
  1241. /* Get the read only section offset */
  1242. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
  1243. PCI_VPD_LRDT_RO_DATA);
  1244. if (unlikely(ro_start < 0)) {
  1245. dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
  1246. __func__);
  1247. rc = -ENODEV;
  1248. goto out;
  1249. }
  1250. /* Get the read only section size, cap when extends beyond read VPD */
  1251. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  1252. j = ro_size;
  1253. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  1254. if (unlikely((i + j) > vpd_size)) {
  1255. pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
  1256. __func__, (i + j), vpd_size);
  1257. ro_size = vpd_size - i;
  1258. }
  1259. /*
  1260. * Find the offset of the WWPN tag within the read only
  1261. * VPD data and validate the found field (partials are
  1262. * no good to us). Convert the ASCII data to an integer
  1263. * value. Note that we must copy to a temporary buffer
  1264. * because the conversion service requires that the ASCII
  1265. * string be terminated.
  1266. */
  1267. for (k = 0; k < NUM_FC_PORTS; k++) {
  1268. j = ro_size;
  1269. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  1270. i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
  1271. if (unlikely(i < 0)) {
  1272. dev_err(&dev->dev, "%s: Port %d WWPN not found "
  1273. "in VPD\n", __func__, k);
  1274. rc = -ENODEV;
  1275. goto out;
  1276. }
  1277. j = pci_vpd_info_field_size(&vpd_data[i]);
  1278. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  1279. if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
  1280. dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
  1281. "VPD corrupt\n",
  1282. __func__, k);
  1283. rc = -ENODEV;
  1284. goto out;
  1285. }
  1286. memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
  1287. rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
  1288. if (unlikely(rc)) {
  1289. dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
  1290. "to integer\n", __func__, k);
  1291. rc = -ENODEV;
  1292. goto out;
  1293. }
  1294. }
  1295. out:
  1296. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1297. return rc;
  1298. }
  1299. /**
  1300. * init_pcr() - initialize the provisioning and control registers
  1301. * @cfg: Internal structure associated with the host.
  1302. *
  1303. * Also sets up fast access to the mapped registers and initializes AFU
  1304. * command fields that never change.
  1305. */
  1306. static void init_pcr(struct cxlflash_cfg *cfg)
  1307. {
  1308. struct afu *afu = cfg->afu;
  1309. struct sisl_ctrl_map __iomem *ctrl_map;
  1310. int i;
  1311. for (i = 0; i < MAX_CONTEXT; i++) {
  1312. ctrl_map = &afu->afu_map->ctrls[i].ctrl;
  1313. /* Disrupt any clients that could be running */
  1314. /* e.g. clients that survived a master restart */
  1315. writeq_be(0, &ctrl_map->rht_start);
  1316. writeq_be(0, &ctrl_map->rht_cnt_id);
  1317. writeq_be(0, &ctrl_map->ctx_cap);
  1318. }
  1319. /* Copy frequently used fields into afu */
  1320. afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
  1321. afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
  1322. afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
  1323. /* Program the Endian Control for the master context */
  1324. writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
  1325. /* Initialize cmd fields that never change */
  1326. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  1327. afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
  1328. afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
  1329. afu->cmd[i].rcb.rrq = 0x0;
  1330. }
  1331. }
  1332. /**
  1333. * init_global() - initialize AFU global registers
  1334. * @cfg: Internal structure associated with the host.
  1335. */
  1336. static int init_global(struct cxlflash_cfg *cfg)
  1337. {
  1338. struct afu *afu = cfg->afu;
  1339. struct device *dev = &cfg->dev->dev;
  1340. u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
  1341. int i = 0, num_ports = 0;
  1342. int rc = 0;
  1343. u64 reg;
  1344. rc = read_vpd(cfg, &wwpn[0]);
  1345. if (rc) {
  1346. dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
  1347. goto out;
  1348. }
  1349. pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
  1350. /* Set up RRQ in AFU for master issued cmds */
  1351. writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
  1352. writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
  1353. /* AFU configuration */
  1354. reg = readq_be(&afu->afu_map->global.regs.afu_config);
  1355. reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
  1356. /* enable all auto retry options and control endianness */
  1357. /* leave others at default: */
  1358. /* CTX_CAP write protected, mbox_r does not clear on read and */
  1359. /* checker on if dual afu */
  1360. writeq_be(reg, &afu->afu_map->global.regs.afu_config);
  1361. /* Global port select: select either port */
  1362. if (afu->internal_lun) {
  1363. /* Only use port 0 */
  1364. writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
  1365. num_ports = NUM_FC_PORTS - 1;
  1366. } else {
  1367. writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
  1368. num_ports = NUM_FC_PORTS;
  1369. }
  1370. for (i = 0; i < num_ports; i++) {
  1371. /* Unmask all errors (but they are still masked at AFU) */
  1372. writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
  1373. /* Clear CRC error cnt & set a threshold */
  1374. (void)readq_be(&afu->afu_map->global.
  1375. fc_regs[i][FC_CNT_CRCERR / 8]);
  1376. writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
  1377. [FC_CRC_THRESH / 8]);
  1378. /* Set WWPNs. If already programmed, wwpn[i] is 0 */
  1379. if (wwpn[i] != 0 &&
  1380. afu_set_wwpn(afu, i,
  1381. &afu->afu_map->global.fc_regs[i][0],
  1382. wwpn[i])) {
  1383. dev_err(dev, "%s: failed to set WWPN on port %d\n",
  1384. __func__, i);
  1385. rc = -EIO;
  1386. goto out;
  1387. }
  1388. /* Programming WWPN back to back causes additional
  1389. * offline/online transitions and a PLOGI
  1390. */
  1391. msleep(100);
  1392. }
  1393. /* Set up master's own CTX_CAP to allow real mode, host translation */
  1394. /* tables, afu cmds and read/write GSCSI cmds. */
  1395. /* First, unlock ctx_cap write by reading mbox */
  1396. (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
  1397. writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
  1398. SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
  1399. SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
  1400. &afu->ctrl_map->ctx_cap);
  1401. /* Initialize heartbeat */
  1402. afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
  1403. out:
  1404. return rc;
  1405. }
  1406. /**
  1407. * start_afu() - initializes and starts the AFU
  1408. * @cfg: Internal structure associated with the host.
  1409. */
  1410. static int start_afu(struct cxlflash_cfg *cfg)
  1411. {
  1412. struct afu *afu = cfg->afu;
  1413. struct afu_cmd *cmd;
  1414. int i = 0;
  1415. int rc = 0;
  1416. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  1417. cmd = &afu->cmd[i];
  1418. init_completion(&cmd->cevent);
  1419. spin_lock_init(&cmd->slock);
  1420. cmd->parent = afu;
  1421. }
  1422. init_pcr(cfg);
  1423. /* After an AFU reset, RRQ entries are stale, clear them */
  1424. memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
  1425. /* Initialize RRQ pointers */
  1426. afu->hrrq_start = &afu->rrq_entry[0];
  1427. afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
  1428. afu->hrrq_curr = afu->hrrq_start;
  1429. afu->toggle = 1;
  1430. rc = init_global(cfg);
  1431. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1432. return rc;
  1433. }
  1434. /**
  1435. * init_mc() - create and register as the master context
  1436. * @cfg: Internal structure associated with the host.
  1437. *
  1438. * Return: 0 on success, -errno on failure
  1439. */
  1440. static int init_mc(struct cxlflash_cfg *cfg)
  1441. {
  1442. struct cxl_context *ctx;
  1443. struct device *dev = &cfg->dev->dev;
  1444. struct afu *afu = cfg->afu;
  1445. int rc = 0;
  1446. enum undo_level level;
  1447. ctx = cxl_get_context(cfg->dev);
  1448. if (unlikely(!ctx))
  1449. return -ENOMEM;
  1450. cfg->mcctx = ctx;
  1451. /* Set it up as a master with the CXL */
  1452. cxl_set_master(ctx);
  1453. /* During initialization reset the AFU to start from a clean slate */
  1454. rc = cxl_afu_reset(cfg->mcctx);
  1455. if (unlikely(rc)) {
  1456. dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
  1457. __func__, rc);
  1458. level = RELEASE_CONTEXT;
  1459. goto out;
  1460. }
  1461. rc = cxl_allocate_afu_irqs(ctx, 3);
  1462. if (unlikely(rc)) {
  1463. dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
  1464. __func__, rc);
  1465. level = RELEASE_CONTEXT;
  1466. goto out;
  1467. }
  1468. rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
  1469. "SISL_MSI_SYNC_ERROR");
  1470. if (unlikely(rc <= 0)) {
  1471. dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
  1472. __func__);
  1473. level = FREE_IRQ;
  1474. goto out;
  1475. }
  1476. rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
  1477. "SISL_MSI_RRQ_UPDATED");
  1478. if (unlikely(rc <= 0)) {
  1479. dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
  1480. __func__);
  1481. level = UNMAP_ONE;
  1482. goto out;
  1483. }
  1484. rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
  1485. "SISL_MSI_ASYNC_ERROR");
  1486. if (unlikely(rc <= 0)) {
  1487. dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
  1488. __func__);
  1489. level = UNMAP_TWO;
  1490. goto out;
  1491. }
  1492. rc = 0;
  1493. /* This performs the equivalent of the CXL_IOCTL_START_WORK.
  1494. * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
  1495. * element (pe) that is embedded in the context (ctx)
  1496. */
  1497. rc = start_context(cfg);
  1498. if (unlikely(rc)) {
  1499. dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
  1500. level = UNMAP_THREE;
  1501. goto out;
  1502. }
  1503. ret:
  1504. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1505. return rc;
  1506. out:
  1507. term_mc(cfg, level);
  1508. goto ret;
  1509. }
  1510. /**
  1511. * init_afu() - setup as master context and start AFU
  1512. * @cfg: Internal structure associated with the host.
  1513. *
  1514. * This routine is a higher level of control for configuring the
  1515. * AFU on probe and reset paths.
  1516. *
  1517. * Return: 0 on success, -errno on failure
  1518. */
  1519. static int init_afu(struct cxlflash_cfg *cfg)
  1520. {
  1521. u64 reg;
  1522. int rc = 0;
  1523. struct afu *afu = cfg->afu;
  1524. struct device *dev = &cfg->dev->dev;
  1525. cxl_perst_reloads_same_image(cfg->cxl_afu, true);
  1526. rc = init_mc(cfg);
  1527. if (rc) {
  1528. dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
  1529. __func__, rc);
  1530. goto out;
  1531. }
  1532. /* Map the entire MMIO space of the AFU */
  1533. afu->afu_map = cxl_psa_map(cfg->mcctx);
  1534. if (!afu->afu_map) {
  1535. dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
  1536. rc = -ENOMEM;
  1537. goto err1;
  1538. }
  1539. kref_init(&afu->mapcount);
  1540. /* No byte reverse on reading afu_version or string will be backwards */
  1541. reg = readq(&afu->afu_map->global.regs.afu_version);
  1542. memcpy(afu->version, &reg, sizeof(reg));
  1543. afu->interface_version =
  1544. readq_be(&afu->afu_map->global.regs.interface_version);
  1545. if ((afu->interface_version + 1) == 0) {
  1546. pr_err("Back level AFU, please upgrade. AFU version %s "
  1547. "interface version 0x%llx\n", afu->version,
  1548. afu->interface_version);
  1549. rc = -EINVAL;
  1550. goto err2;
  1551. }
  1552. pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__,
  1553. afu->version, afu->interface_version);
  1554. rc = start_afu(cfg);
  1555. if (rc) {
  1556. dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
  1557. __func__, rc);
  1558. goto err2;
  1559. }
  1560. afu_err_intr_init(cfg->afu);
  1561. atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
  1562. /* Restore the LUN mappings */
  1563. cxlflash_restore_luntable(cfg);
  1564. out:
  1565. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1566. return rc;
  1567. err2:
  1568. kref_put(&afu->mapcount, afu_unmap);
  1569. err1:
  1570. term_mc(cfg, UNDO_START);
  1571. goto out;
  1572. }
  1573. /**
  1574. * cxlflash_afu_sync() - builds and sends an AFU sync command
  1575. * @afu: AFU associated with the host.
  1576. * @ctx_hndl_u: Identifies context requesting sync.
  1577. * @res_hndl_u: Identifies resource requesting sync.
  1578. * @mode: Type of sync to issue (lightweight, heavyweight, global).
  1579. *
  1580. * The AFU can only take 1 sync command at a time. This routine enforces this
  1581. * limitation by using a mutex to provide exclusive access to the AFU during
  1582. * the sync. This design point requires calling threads to not be on interrupt
  1583. * context due to the possibility of sleeping during concurrent sync operations.
  1584. *
  1585. * AFU sync operations are only necessary and allowed when the device is
  1586. * operating normally. When not operating normally, sync requests can occur as
  1587. * part of cleaning up resources associated with an adapter prior to removal.
  1588. * In this scenario, these requests are simply ignored (safe due to the AFU
  1589. * going away).
  1590. *
  1591. * Return:
  1592. * 0 on success
  1593. * -1 on failure
  1594. */
  1595. int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
  1596. res_hndl_t res_hndl_u, u8 mode)
  1597. {
  1598. struct cxlflash_cfg *cfg = afu->parent;
  1599. struct device *dev = &cfg->dev->dev;
  1600. struct afu_cmd *cmd = NULL;
  1601. int rc = 0;
  1602. int retry_cnt = 0;
  1603. static DEFINE_MUTEX(sync_active);
  1604. if (cfg->state != STATE_NORMAL) {
  1605. pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
  1606. return 0;
  1607. }
  1608. mutex_lock(&sync_active);
  1609. retry:
  1610. cmd = cmd_checkout(afu);
  1611. if (unlikely(!cmd)) {
  1612. retry_cnt++;
  1613. udelay(1000 * retry_cnt);
  1614. if (retry_cnt < MC_RETRY_CNT)
  1615. goto retry;
  1616. dev_err(dev, "%s: could not get a free command\n", __func__);
  1617. rc = -1;
  1618. goto out;
  1619. }
  1620. pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
  1621. memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
  1622. cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
  1623. cmd->rcb.port_sel = 0x0; /* NA */
  1624. cmd->rcb.lun_id = 0x0; /* NA */
  1625. cmd->rcb.data_len = 0x0;
  1626. cmd->rcb.data_ea = 0x0;
  1627. cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
  1628. cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
  1629. cmd->rcb.cdb[1] = mode;
  1630. /* The cdb is aligned, no unaligned accessors required */
  1631. *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
  1632. *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
  1633. rc = send_cmd(afu, cmd);
  1634. if (unlikely(rc))
  1635. goto out;
  1636. wait_resp(afu, cmd);
  1637. /* Set on timeout */
  1638. if (unlikely((cmd->sa.ioasc != 0) ||
  1639. (cmd->sa.host_use_b[0] & B_ERROR)))
  1640. rc = -1;
  1641. out:
  1642. mutex_unlock(&sync_active);
  1643. if (cmd)
  1644. cmd_checkin(cmd);
  1645. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1646. return rc;
  1647. }
  1648. /**
  1649. * afu_reset() - resets the AFU
  1650. * @cfg: Internal structure associated with the host.
  1651. *
  1652. * Return: 0 on success, -errno on failure
  1653. */
  1654. static int afu_reset(struct cxlflash_cfg *cfg)
  1655. {
  1656. int rc = 0;
  1657. /* Stop the context before the reset. Since the context is
  1658. * no longer available restart it after the reset is complete
  1659. */
  1660. term_afu(cfg);
  1661. rc = init_afu(cfg);
  1662. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1663. return rc;
  1664. }
  1665. /**
  1666. * cxlflash_eh_device_reset_handler() - reset a single LUN
  1667. * @scp: SCSI command to send.
  1668. *
  1669. * Return:
  1670. * SUCCESS as defined in scsi/scsi.h
  1671. * FAILED as defined in scsi/scsi.h
  1672. */
  1673. static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
  1674. {
  1675. int rc = SUCCESS;
  1676. struct Scsi_Host *host = scp->device->host;
  1677. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  1678. struct afu *afu = cfg->afu;
  1679. int rcr = 0;
  1680. pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
  1681. "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
  1682. host->host_no, scp->device->channel,
  1683. scp->device->id, scp->device->lun,
  1684. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  1685. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  1686. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  1687. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  1688. retry:
  1689. switch (cfg->state) {
  1690. case STATE_NORMAL:
  1691. rcr = send_tmf(afu, scp, TMF_LUN_RESET);
  1692. if (unlikely(rcr))
  1693. rc = FAILED;
  1694. break;
  1695. case STATE_RESET:
  1696. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  1697. goto retry;
  1698. default:
  1699. rc = FAILED;
  1700. break;
  1701. }
  1702. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1703. return rc;
  1704. }
  1705. /**
  1706. * cxlflash_eh_host_reset_handler() - reset the host adapter
  1707. * @scp: SCSI command from stack identifying host.
  1708. *
  1709. * Following a reset, the state is evaluated again in case an EEH occurred
  1710. * during the reset. In such a scenario, the host reset will either yield
  1711. * until the EEH recovery is complete or return success or failure based
  1712. * upon the current device state.
  1713. *
  1714. * Return:
  1715. * SUCCESS as defined in scsi/scsi.h
  1716. * FAILED as defined in scsi/scsi.h
  1717. */
  1718. static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
  1719. {
  1720. int rc = SUCCESS;
  1721. int rcr = 0;
  1722. struct Scsi_Host *host = scp->device->host;
  1723. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  1724. pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
  1725. "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
  1726. host->host_no, scp->device->channel,
  1727. scp->device->id, scp->device->lun,
  1728. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  1729. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  1730. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  1731. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  1732. switch (cfg->state) {
  1733. case STATE_NORMAL:
  1734. cfg->state = STATE_RESET;
  1735. cxlflash_mark_contexts_error(cfg);
  1736. rcr = afu_reset(cfg);
  1737. if (rcr) {
  1738. rc = FAILED;
  1739. cfg->state = STATE_FAILTERM;
  1740. } else
  1741. cfg->state = STATE_NORMAL;
  1742. wake_up_all(&cfg->reset_waitq);
  1743. ssleep(1);
  1744. /* fall through */
  1745. case STATE_RESET:
  1746. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  1747. if (cfg->state == STATE_NORMAL)
  1748. break;
  1749. /* fall through */
  1750. default:
  1751. rc = FAILED;
  1752. break;
  1753. }
  1754. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1755. return rc;
  1756. }
  1757. /**
  1758. * cxlflash_change_queue_depth() - change the queue depth for the device
  1759. * @sdev: SCSI device destined for queue depth change.
  1760. * @qdepth: Requested queue depth value to set.
  1761. *
  1762. * The requested queue depth is capped to the maximum supported value.
  1763. *
  1764. * Return: The actual queue depth set.
  1765. */
  1766. static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1767. {
  1768. if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
  1769. qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
  1770. scsi_change_queue_depth(sdev, qdepth);
  1771. return sdev->queue_depth;
  1772. }
  1773. /**
  1774. * cxlflash_show_port_status() - queries and presents the current port status
  1775. * @port: Desired port for status reporting.
  1776. * @afu: AFU owning the specified port.
  1777. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1778. *
  1779. * Return: The size of the ASCII string returned in @buf.
  1780. */
  1781. static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
  1782. {
  1783. char *disp_status;
  1784. u64 status;
  1785. __be64 __iomem *fc_regs;
  1786. if (port >= NUM_FC_PORTS)
  1787. return 0;
  1788. fc_regs = &afu->afu_map->global.fc_regs[port][0];
  1789. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  1790. status &= FC_MTIP_STATUS_MASK;
  1791. if (status == FC_MTIP_STATUS_ONLINE)
  1792. disp_status = "online";
  1793. else if (status == FC_MTIP_STATUS_OFFLINE)
  1794. disp_status = "offline";
  1795. else
  1796. disp_status = "unknown";
  1797. return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
  1798. }
  1799. /**
  1800. * port0_show() - queries and presents the current status of port 0
  1801. * @dev: Generic device associated with the host owning the port.
  1802. * @attr: Device attribute representing the port.
  1803. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1804. *
  1805. * Return: The size of the ASCII string returned in @buf.
  1806. */
  1807. static ssize_t port0_show(struct device *dev,
  1808. struct device_attribute *attr,
  1809. char *buf)
  1810. {
  1811. struct Scsi_Host *shost = class_to_shost(dev);
  1812. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1813. struct afu *afu = cfg->afu;
  1814. return cxlflash_show_port_status(0, afu, buf);
  1815. }
  1816. /**
  1817. * port1_show() - queries and presents the current status of port 1
  1818. * @dev: Generic device associated with the host owning the port.
  1819. * @attr: Device attribute representing the port.
  1820. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1821. *
  1822. * Return: The size of the ASCII string returned in @buf.
  1823. */
  1824. static ssize_t port1_show(struct device *dev,
  1825. struct device_attribute *attr,
  1826. char *buf)
  1827. {
  1828. struct Scsi_Host *shost = class_to_shost(dev);
  1829. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1830. struct afu *afu = cfg->afu;
  1831. return cxlflash_show_port_status(1, afu, buf);
  1832. }
  1833. /**
  1834. * lun_mode_show() - presents the current LUN mode of the host
  1835. * @dev: Generic device associated with the host.
  1836. * @attr: Device attribute representing the LUN mode.
  1837. * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
  1838. *
  1839. * Return: The size of the ASCII string returned in @buf.
  1840. */
  1841. static ssize_t lun_mode_show(struct device *dev,
  1842. struct device_attribute *attr, char *buf)
  1843. {
  1844. struct Scsi_Host *shost = class_to_shost(dev);
  1845. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1846. struct afu *afu = cfg->afu;
  1847. return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
  1848. }
  1849. /**
  1850. * lun_mode_store() - sets the LUN mode of the host
  1851. * @dev: Generic device associated with the host.
  1852. * @attr: Device attribute representing the LUN mode.
  1853. * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
  1854. * @count: Length of data resizing in @buf.
  1855. *
  1856. * The CXL Flash AFU supports a dummy LUN mode where the external
  1857. * links and storage are not required. Space on the FPGA is used
  1858. * to create 1 or 2 small LUNs which are presented to the system
  1859. * as if they were a normal storage device. This feature is useful
  1860. * during development and also provides manufacturing with a way
  1861. * to test the AFU without an actual device.
  1862. *
  1863. * 0 = external LUN[s] (default)
  1864. * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
  1865. * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
  1866. * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
  1867. * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
  1868. *
  1869. * Return: The size of the ASCII string returned in @buf.
  1870. */
  1871. static ssize_t lun_mode_store(struct device *dev,
  1872. struct device_attribute *attr,
  1873. const char *buf, size_t count)
  1874. {
  1875. struct Scsi_Host *shost = class_to_shost(dev);
  1876. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1877. struct afu *afu = cfg->afu;
  1878. int rc;
  1879. u32 lun_mode;
  1880. rc = kstrtouint(buf, 10, &lun_mode);
  1881. if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
  1882. afu->internal_lun = lun_mode;
  1883. /*
  1884. * When configured for internal LUN, there is only one channel,
  1885. * channel number 0, else there will be 2 (default).
  1886. */
  1887. if (afu->internal_lun)
  1888. shost->max_channel = 0;
  1889. else
  1890. shost->max_channel = NUM_FC_PORTS - 1;
  1891. afu_reset(cfg);
  1892. scsi_scan_host(cfg->host);
  1893. }
  1894. return count;
  1895. }
  1896. /**
  1897. * ioctl_version_show() - presents the current ioctl version of the host
  1898. * @dev: Generic device associated with the host.
  1899. * @attr: Device attribute representing the ioctl version.
  1900. * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
  1901. *
  1902. * Return: The size of the ASCII string returned in @buf.
  1903. */
  1904. static ssize_t ioctl_version_show(struct device *dev,
  1905. struct device_attribute *attr, char *buf)
  1906. {
  1907. return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
  1908. }
  1909. /**
  1910. * cxlflash_show_port_lun_table() - queries and presents the port LUN table
  1911. * @port: Desired port for status reporting.
  1912. * @afu: AFU owning the specified port.
  1913. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1914. *
  1915. * Return: The size of the ASCII string returned in @buf.
  1916. */
  1917. static ssize_t cxlflash_show_port_lun_table(u32 port,
  1918. struct afu *afu,
  1919. char *buf)
  1920. {
  1921. int i;
  1922. ssize_t bytes = 0;
  1923. __be64 __iomem *fc_port;
  1924. if (port >= NUM_FC_PORTS)
  1925. return 0;
  1926. fc_port = &afu->afu_map->global.fc_port[port][0];
  1927. for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
  1928. bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
  1929. "%03d: %016llX\n", i, readq_be(&fc_port[i]));
  1930. return bytes;
  1931. }
  1932. /**
  1933. * port0_lun_table_show() - presents the current LUN table of port 0
  1934. * @dev: Generic device associated with the host owning the port.
  1935. * @attr: Device attribute representing the port.
  1936. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1937. *
  1938. * Return: The size of the ASCII string returned in @buf.
  1939. */
  1940. static ssize_t port0_lun_table_show(struct device *dev,
  1941. struct device_attribute *attr,
  1942. char *buf)
  1943. {
  1944. struct Scsi_Host *shost = class_to_shost(dev);
  1945. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1946. struct afu *afu = cfg->afu;
  1947. return cxlflash_show_port_lun_table(0, afu, buf);
  1948. }
  1949. /**
  1950. * port1_lun_table_show() - presents the current LUN table of port 1
  1951. * @dev: Generic device associated with the host owning the port.
  1952. * @attr: Device attribute representing the port.
  1953. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1954. *
  1955. * Return: The size of the ASCII string returned in @buf.
  1956. */
  1957. static ssize_t port1_lun_table_show(struct device *dev,
  1958. struct device_attribute *attr,
  1959. char *buf)
  1960. {
  1961. struct Scsi_Host *shost = class_to_shost(dev);
  1962. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1963. struct afu *afu = cfg->afu;
  1964. return cxlflash_show_port_lun_table(1, afu, buf);
  1965. }
  1966. /**
  1967. * mode_show() - presents the current mode of the device
  1968. * @dev: Generic device associated with the device.
  1969. * @attr: Device attribute representing the device mode.
  1970. * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
  1971. *
  1972. * Return: The size of the ASCII string returned in @buf.
  1973. */
  1974. static ssize_t mode_show(struct device *dev,
  1975. struct device_attribute *attr, char *buf)
  1976. {
  1977. struct scsi_device *sdev = to_scsi_device(dev);
  1978. return scnprintf(buf, PAGE_SIZE, "%s\n",
  1979. sdev->hostdata ? "superpipe" : "legacy");
  1980. }
  1981. /*
  1982. * Host attributes
  1983. */
  1984. static DEVICE_ATTR_RO(port0);
  1985. static DEVICE_ATTR_RO(port1);
  1986. static DEVICE_ATTR_RW(lun_mode);
  1987. static DEVICE_ATTR_RO(ioctl_version);
  1988. static DEVICE_ATTR_RO(port0_lun_table);
  1989. static DEVICE_ATTR_RO(port1_lun_table);
  1990. static struct device_attribute *cxlflash_host_attrs[] = {
  1991. &dev_attr_port0,
  1992. &dev_attr_port1,
  1993. &dev_attr_lun_mode,
  1994. &dev_attr_ioctl_version,
  1995. &dev_attr_port0_lun_table,
  1996. &dev_attr_port1_lun_table,
  1997. NULL
  1998. };
  1999. /*
  2000. * Device attributes
  2001. */
  2002. static DEVICE_ATTR_RO(mode);
  2003. static struct device_attribute *cxlflash_dev_attrs[] = {
  2004. &dev_attr_mode,
  2005. NULL
  2006. };
  2007. /*
  2008. * Host template
  2009. */
  2010. static struct scsi_host_template driver_template = {
  2011. .module = THIS_MODULE,
  2012. .name = CXLFLASH_ADAPTER_NAME,
  2013. .info = cxlflash_driver_info,
  2014. .ioctl = cxlflash_ioctl,
  2015. .proc_name = CXLFLASH_NAME,
  2016. .queuecommand = cxlflash_queuecommand,
  2017. .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
  2018. .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
  2019. .change_queue_depth = cxlflash_change_queue_depth,
  2020. .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
  2021. .can_queue = CXLFLASH_MAX_CMDS,
  2022. .this_id = -1,
  2023. .sg_tablesize = SG_NONE, /* No scatter gather support */
  2024. .max_sectors = CXLFLASH_MAX_SECTORS,
  2025. .use_clustering = ENABLE_CLUSTERING,
  2026. .shost_attrs = cxlflash_host_attrs,
  2027. .sdev_attrs = cxlflash_dev_attrs,
  2028. };
  2029. /*
  2030. * Device dependent values
  2031. */
  2032. static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS };
  2033. static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS };
  2034. /*
  2035. * PCI device binding table
  2036. */
  2037. static struct pci_device_id cxlflash_pci_table[] = {
  2038. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
  2039. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
  2040. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
  2041. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
  2042. {}
  2043. };
  2044. MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
  2045. /**
  2046. * cxlflash_worker_thread() - work thread handler for the AFU
  2047. * @work: Work structure contained within cxlflash associated with host.
  2048. *
  2049. * Handles the following events:
  2050. * - Link reset which cannot be performed on interrupt context due to
  2051. * blocking up to a few seconds
  2052. * - Read AFU command room
  2053. * - Rescan the host
  2054. */
  2055. static void cxlflash_worker_thread(struct work_struct *work)
  2056. {
  2057. struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
  2058. work_q);
  2059. struct afu *afu = cfg->afu;
  2060. struct device *dev = &cfg->dev->dev;
  2061. int port;
  2062. ulong lock_flags;
  2063. /* Avoid MMIO if the device has failed */
  2064. if (cfg->state != STATE_NORMAL)
  2065. return;
  2066. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2067. if (cfg->lr_state == LINK_RESET_REQUIRED) {
  2068. port = cfg->lr_port;
  2069. if (port < 0)
  2070. dev_err(dev, "%s: invalid port index %d\n",
  2071. __func__, port);
  2072. else {
  2073. spin_unlock_irqrestore(cfg->host->host_lock,
  2074. lock_flags);
  2075. /* The reset can block... */
  2076. afu_link_reset(afu, port,
  2077. &afu->afu_map->global.fc_regs[port][0]);
  2078. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2079. }
  2080. cfg->lr_state = LINK_RESET_COMPLETE;
  2081. }
  2082. if (afu->read_room) {
  2083. atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
  2084. afu->read_room = false;
  2085. }
  2086. spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
  2087. if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
  2088. scsi_scan_host(cfg->host);
  2089. kref_put(&afu->mapcount, afu_unmap);
  2090. }
  2091. /**
  2092. * cxlflash_probe() - PCI entry point to add host
  2093. * @pdev: PCI device associated with the host.
  2094. * @dev_id: PCI device id associated with device.
  2095. *
  2096. * Return: 0 on success, -errno on failure
  2097. */
  2098. static int cxlflash_probe(struct pci_dev *pdev,
  2099. const struct pci_device_id *dev_id)
  2100. {
  2101. struct Scsi_Host *host;
  2102. struct cxlflash_cfg *cfg = NULL;
  2103. struct device *phys_dev;
  2104. struct dev_dependent_vals *ddv;
  2105. int rc = 0;
  2106. dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
  2107. __func__, pdev->irq);
  2108. ddv = (struct dev_dependent_vals *)dev_id->driver_data;
  2109. driver_template.max_sectors = ddv->max_sectors;
  2110. host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
  2111. if (!host) {
  2112. dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
  2113. __func__);
  2114. rc = -ENOMEM;
  2115. goto out;
  2116. }
  2117. host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
  2118. host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
  2119. host->max_channel = NUM_FC_PORTS - 1;
  2120. host->unique_id = host->host_no;
  2121. host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
  2122. cfg = (struct cxlflash_cfg *)host->hostdata;
  2123. cfg->host = host;
  2124. rc = alloc_mem(cfg);
  2125. if (rc) {
  2126. dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
  2127. __func__);
  2128. rc = -ENOMEM;
  2129. scsi_host_put(cfg->host);
  2130. goto out;
  2131. }
  2132. cfg->init_state = INIT_STATE_NONE;
  2133. cfg->dev = pdev;
  2134. cfg->cxl_fops = cxlflash_cxl_fops;
  2135. /*
  2136. * The promoted LUNs move to the top of the LUN table. The rest stay
  2137. * on the bottom half. The bottom half grows from the end
  2138. * (index = 255), whereas the top half grows from the beginning
  2139. * (index = 0).
  2140. */
  2141. cfg->promote_lun_index = 0;
  2142. cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
  2143. cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
  2144. cfg->dev_id = (struct pci_device_id *)dev_id;
  2145. init_waitqueue_head(&cfg->tmf_waitq);
  2146. init_waitqueue_head(&cfg->reset_waitq);
  2147. INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
  2148. cfg->lr_state = LINK_RESET_INVALID;
  2149. cfg->lr_port = -1;
  2150. spin_lock_init(&cfg->tmf_slock);
  2151. mutex_init(&cfg->ctx_tbl_list_mutex);
  2152. mutex_init(&cfg->ctx_recovery_mutex);
  2153. init_rwsem(&cfg->ioctl_rwsem);
  2154. INIT_LIST_HEAD(&cfg->ctx_err_recovery);
  2155. INIT_LIST_HEAD(&cfg->lluns);
  2156. pci_set_drvdata(pdev, cfg);
  2157. /*
  2158. * Use the special service provided to look up the physical
  2159. * PCI device, since we are called on the probe of the virtual
  2160. * PCI host bus (vphb)
  2161. */
  2162. phys_dev = cxl_get_phys_dev(pdev);
  2163. if (!dev_is_pci(phys_dev)) {
  2164. dev_err(&pdev->dev, "%s: not a pci dev\n", __func__);
  2165. rc = -ENODEV;
  2166. goto out_remove;
  2167. }
  2168. cfg->parent_dev = to_pci_dev(phys_dev);
  2169. cfg->cxl_afu = cxl_pci_to_afu(pdev);
  2170. rc = init_pci(cfg);
  2171. if (rc) {
  2172. dev_err(&pdev->dev, "%s: call to init_pci "
  2173. "failed rc=%d!\n", __func__, rc);
  2174. goto out_remove;
  2175. }
  2176. cfg->init_state = INIT_STATE_PCI;
  2177. rc = init_afu(cfg);
  2178. if (rc) {
  2179. dev_err(&pdev->dev, "%s: call to init_afu "
  2180. "failed rc=%d!\n", __func__, rc);
  2181. goto out_remove;
  2182. }
  2183. cfg->init_state = INIT_STATE_AFU;
  2184. rc = init_scsi(cfg);
  2185. if (rc) {
  2186. dev_err(&pdev->dev, "%s: call to init_scsi "
  2187. "failed rc=%d!\n", __func__, rc);
  2188. goto out_remove;
  2189. }
  2190. cfg->init_state = INIT_STATE_SCSI;
  2191. out:
  2192. pr_debug("%s: returning rc=%d\n", __func__, rc);
  2193. return rc;
  2194. out_remove:
  2195. cxlflash_remove(pdev);
  2196. goto out;
  2197. }
  2198. /**
  2199. * drain_ioctls() - wait until all currently executing ioctls have completed
  2200. * @cfg: Internal structure associated with the host.
  2201. *
  2202. * Obtain write access to read/write semaphore that wraps ioctl
  2203. * handling to 'drain' ioctls currently executing.
  2204. */
  2205. static void drain_ioctls(struct cxlflash_cfg *cfg)
  2206. {
  2207. down_write(&cfg->ioctl_rwsem);
  2208. up_write(&cfg->ioctl_rwsem);
  2209. }
  2210. /**
  2211. * cxlflash_pci_error_detected() - called when a PCI error is detected
  2212. * @pdev: PCI device struct.
  2213. * @state: PCI channel state.
  2214. *
  2215. * When an EEH occurs during an active reset, wait until the reset is
  2216. * complete and then take action based upon the device state.
  2217. *
  2218. * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  2219. */
  2220. static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
  2221. pci_channel_state_t state)
  2222. {
  2223. int rc = 0;
  2224. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2225. struct device *dev = &cfg->dev->dev;
  2226. dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
  2227. switch (state) {
  2228. case pci_channel_io_frozen:
  2229. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  2230. if (cfg->state == STATE_FAILTERM)
  2231. return PCI_ERS_RESULT_DISCONNECT;
  2232. cfg->state = STATE_RESET;
  2233. scsi_block_requests(cfg->host);
  2234. drain_ioctls(cfg);
  2235. rc = cxlflash_mark_contexts_error(cfg);
  2236. if (unlikely(rc))
  2237. dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
  2238. __func__, rc);
  2239. term_mc(cfg, UNDO_START);
  2240. stop_afu(cfg);
  2241. return PCI_ERS_RESULT_NEED_RESET;
  2242. case pci_channel_io_perm_failure:
  2243. cfg->state = STATE_FAILTERM;
  2244. wake_up_all(&cfg->reset_waitq);
  2245. scsi_unblock_requests(cfg->host);
  2246. return PCI_ERS_RESULT_DISCONNECT;
  2247. default:
  2248. break;
  2249. }
  2250. return PCI_ERS_RESULT_NEED_RESET;
  2251. }
  2252. /**
  2253. * cxlflash_pci_slot_reset() - called when PCI slot has been reset
  2254. * @pdev: PCI device struct.
  2255. *
  2256. * This routine is called by the pci error recovery code after the PCI
  2257. * slot has been reset, just before we should resume normal operations.
  2258. *
  2259. * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
  2260. */
  2261. static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
  2262. {
  2263. int rc = 0;
  2264. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2265. struct device *dev = &cfg->dev->dev;
  2266. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  2267. rc = init_afu(cfg);
  2268. if (unlikely(rc)) {
  2269. dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
  2270. return PCI_ERS_RESULT_DISCONNECT;
  2271. }
  2272. return PCI_ERS_RESULT_RECOVERED;
  2273. }
  2274. /**
  2275. * cxlflash_pci_resume() - called when normal operation can resume
  2276. * @pdev: PCI device struct
  2277. */
  2278. static void cxlflash_pci_resume(struct pci_dev *pdev)
  2279. {
  2280. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2281. struct device *dev = &cfg->dev->dev;
  2282. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  2283. cfg->state = STATE_NORMAL;
  2284. wake_up_all(&cfg->reset_waitq);
  2285. scsi_unblock_requests(cfg->host);
  2286. }
  2287. static const struct pci_error_handlers cxlflash_err_handler = {
  2288. .error_detected = cxlflash_pci_error_detected,
  2289. .slot_reset = cxlflash_pci_slot_reset,
  2290. .resume = cxlflash_pci_resume,
  2291. };
  2292. /*
  2293. * PCI device structure
  2294. */
  2295. static struct pci_driver cxlflash_driver = {
  2296. .name = CXLFLASH_NAME,
  2297. .id_table = cxlflash_pci_table,
  2298. .probe = cxlflash_probe,
  2299. .remove = cxlflash_remove,
  2300. .err_handler = &cxlflash_err_handler,
  2301. };
  2302. /**
  2303. * init_cxlflash() - module entry point
  2304. *
  2305. * Return: 0 on success, -errno on failure
  2306. */
  2307. static int __init init_cxlflash(void)
  2308. {
  2309. pr_info("%s: IBM Power CXL Flash Adapter: %s\n",
  2310. __func__, CXLFLASH_DRIVER_DATE);
  2311. cxlflash_list_init();
  2312. return pci_register_driver(&cxlflash_driver);
  2313. }
  2314. /**
  2315. * exit_cxlflash() - module exit point
  2316. */
  2317. static void __exit exit_cxlflash(void)
  2318. {
  2319. cxlflash_term_global_luns();
  2320. cxlflash_free_errpage();
  2321. pci_unregister_driver(&cxlflash_driver);
  2322. }
  2323. module_init(init_cxlflash);
  2324. module_exit(exit_cxlflash);