main.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  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_intr() - disables all AFU interrupts
  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_intr(struct cxlflash_cfg *cfg, enum undo_level level)
  607. {
  608. struct afu *afu = cfg->afu;
  609. struct device *dev = &cfg->dev->dev;
  610. if (!afu || !cfg->mcctx) {
  611. dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
  612. return;
  613. }
  614. switch (level) {
  615. case UNMAP_THREE:
  616. cxl_unmap_afu_irq(cfg->mcctx, 3, afu);
  617. case UNMAP_TWO:
  618. cxl_unmap_afu_irq(cfg->mcctx, 2, afu);
  619. case UNMAP_ONE:
  620. cxl_unmap_afu_irq(cfg->mcctx, 1, afu);
  621. case FREE_IRQ:
  622. cxl_free_afu_irqs(cfg->mcctx);
  623. /* fall through */
  624. case UNDO_NOOP:
  625. /* No action required */
  626. break;
  627. }
  628. }
  629. /**
  630. * term_mc() - terminates the master context
  631. * @cfg: Internal structure associated with the host.
  632. * @level: Depth of allocation, where to begin waterfall tear down.
  633. *
  634. * Safe to call with AFU/MC in partially allocated/initialized state.
  635. */
  636. static void term_mc(struct cxlflash_cfg *cfg)
  637. {
  638. int rc = 0;
  639. struct afu *afu = cfg->afu;
  640. struct device *dev = &cfg->dev->dev;
  641. if (!afu || !cfg->mcctx) {
  642. dev_err(dev, "%s: returning with NULL afu or MC\n", __func__);
  643. return;
  644. }
  645. rc = cxl_stop_context(cfg->mcctx);
  646. WARN_ON(rc);
  647. cfg->mcctx = NULL;
  648. }
  649. /**
  650. * term_afu() - terminates the AFU
  651. * @cfg: Internal structure associated with the host.
  652. *
  653. * Safe to call with AFU/MC in partially allocated/initialized state.
  654. */
  655. static void term_afu(struct cxlflash_cfg *cfg)
  656. {
  657. /*
  658. * Tear down is carefully orchestrated to ensure
  659. * no interrupts can come in when the problem state
  660. * area is unmapped.
  661. *
  662. * 1) Disable all AFU interrupts
  663. * 2) Unmap the problem state area
  664. * 3) Stop the master context
  665. */
  666. term_intr(cfg, UNMAP_THREE);
  667. if (cfg->afu)
  668. stop_afu(cfg);
  669. term_mc(cfg);
  670. pr_debug("%s: returning\n", __func__);
  671. }
  672. /**
  673. * notify_shutdown() - notifies device of pending shutdown
  674. * @cfg: Internal structure associated with the host.
  675. * @wait: Whether to wait for shutdown processing to complete.
  676. *
  677. * This function will notify the AFU that the adapter is being shutdown
  678. * and will wait for shutdown processing to complete if wait is true.
  679. * This notification should flush pending I/Os to the device and halt
  680. * further I/Os until the next AFU reset is issued and device restarted.
  681. */
  682. static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
  683. {
  684. struct afu *afu = cfg->afu;
  685. struct device *dev = &cfg->dev->dev;
  686. struct sisl_global_map __iomem *global;
  687. struct dev_dependent_vals *ddv;
  688. u64 reg, status;
  689. int i, retry_cnt = 0;
  690. ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
  691. if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
  692. return;
  693. if (!afu || !afu->afu_map) {
  694. dev_dbg(dev, "%s: The problem state area is not mapped\n",
  695. __func__);
  696. return;
  697. }
  698. global = &afu->afu_map->global;
  699. /* Notify AFU */
  700. for (i = 0; i < NUM_FC_PORTS; i++) {
  701. reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]);
  702. reg |= SISL_FC_SHUTDOWN_NORMAL;
  703. writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]);
  704. }
  705. if (!wait)
  706. return;
  707. /* Wait up to 1.5 seconds for shutdown processing to complete */
  708. for (i = 0; i < NUM_FC_PORTS; i++) {
  709. retry_cnt = 0;
  710. while (true) {
  711. status = readq_be(&global->fc_regs[i][FC_STATUS / 8]);
  712. if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
  713. break;
  714. if (++retry_cnt >= MC_RETRY_CNT) {
  715. dev_dbg(dev, "%s: port %d shutdown processing "
  716. "not yet completed\n", __func__, i);
  717. break;
  718. }
  719. msleep(100 * retry_cnt);
  720. }
  721. }
  722. }
  723. /**
  724. * cxlflash_remove() - PCI entry point to tear down host
  725. * @pdev: PCI device associated with the host.
  726. *
  727. * Safe to use as a cleanup in partially allocated/initialized state.
  728. */
  729. static void cxlflash_remove(struct pci_dev *pdev)
  730. {
  731. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  732. ulong lock_flags;
  733. if (!pci_is_enabled(pdev)) {
  734. pr_debug("%s: Device is disabled\n", __func__);
  735. return;
  736. }
  737. /* If a Task Management Function is active, wait for it to complete
  738. * before continuing with remove.
  739. */
  740. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  741. if (cfg->tmf_active)
  742. wait_event_interruptible_lock_irq(cfg->tmf_waitq,
  743. !cfg->tmf_active,
  744. cfg->tmf_slock);
  745. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  746. /* Notify AFU and wait for shutdown processing to complete */
  747. notify_shutdown(cfg, true);
  748. cfg->state = STATE_FAILTERM;
  749. cxlflash_stop_term_user_contexts(cfg);
  750. switch (cfg->init_state) {
  751. case INIT_STATE_SCSI:
  752. cxlflash_term_local_luns(cfg);
  753. scsi_remove_host(cfg->host);
  754. /* fall through */
  755. case INIT_STATE_AFU:
  756. cancel_work_sync(&cfg->work_q);
  757. term_afu(cfg);
  758. case INIT_STATE_PCI:
  759. pci_disable_device(pdev);
  760. case INIT_STATE_NONE:
  761. free_mem(cfg);
  762. scsi_host_put(cfg->host);
  763. break;
  764. }
  765. pr_debug("%s: returning\n", __func__);
  766. }
  767. /**
  768. * alloc_mem() - allocates the AFU and its command pool
  769. * @cfg: Internal structure associated with the host.
  770. *
  771. * A partially allocated state remains on failure.
  772. *
  773. * Return:
  774. * 0 on success
  775. * -ENOMEM on failure to allocate memory
  776. */
  777. static int alloc_mem(struct cxlflash_cfg *cfg)
  778. {
  779. int rc = 0;
  780. int i;
  781. char *buf = NULL;
  782. struct device *dev = &cfg->dev->dev;
  783. /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */
  784. cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  785. get_order(sizeof(struct afu)));
  786. if (unlikely(!cfg->afu)) {
  787. dev_err(dev, "%s: cannot get %d free pages\n",
  788. __func__, get_order(sizeof(struct afu)));
  789. rc = -ENOMEM;
  790. goto out;
  791. }
  792. cfg->afu->parent = cfg;
  793. cfg->afu->afu_map = NULL;
  794. for (i = 0; i < CXLFLASH_NUM_CMDS; buf += CMD_BUFSIZE, i++) {
  795. if (!((u64)buf & (PAGE_SIZE - 1))) {
  796. buf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  797. if (unlikely(!buf)) {
  798. dev_err(dev,
  799. "%s: Allocate command buffers fail!\n",
  800. __func__);
  801. rc = -ENOMEM;
  802. free_mem(cfg);
  803. goto out;
  804. }
  805. }
  806. cfg->afu->cmd[i].buf = buf;
  807. atomic_set(&cfg->afu->cmd[i].free, 1);
  808. cfg->afu->cmd[i].slot = i;
  809. }
  810. out:
  811. return rc;
  812. }
  813. /**
  814. * init_pci() - initializes the host as a PCI device
  815. * @cfg: Internal structure associated with the host.
  816. *
  817. * Return: 0 on success, -errno on failure
  818. */
  819. static int init_pci(struct cxlflash_cfg *cfg)
  820. {
  821. struct pci_dev *pdev = cfg->dev;
  822. int rc = 0;
  823. rc = pci_enable_device(pdev);
  824. if (rc || pci_channel_offline(pdev)) {
  825. if (pci_channel_offline(pdev)) {
  826. cxlflash_wait_for_pci_err_recovery(cfg);
  827. rc = pci_enable_device(pdev);
  828. }
  829. if (rc) {
  830. dev_err(&pdev->dev, "%s: Cannot enable adapter\n",
  831. __func__);
  832. cxlflash_wait_for_pci_err_recovery(cfg);
  833. goto out;
  834. }
  835. }
  836. out:
  837. pr_debug("%s: returning rc=%d\n", __func__, rc);
  838. return rc;
  839. }
  840. /**
  841. * init_scsi() - adds the host to the SCSI stack and kicks off host scan
  842. * @cfg: Internal structure associated with the host.
  843. *
  844. * Return: 0 on success, -errno on failure
  845. */
  846. static int init_scsi(struct cxlflash_cfg *cfg)
  847. {
  848. struct pci_dev *pdev = cfg->dev;
  849. int rc = 0;
  850. rc = scsi_add_host(cfg->host, &pdev->dev);
  851. if (rc) {
  852. dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n",
  853. __func__, rc);
  854. goto out;
  855. }
  856. scsi_scan_host(cfg->host);
  857. out:
  858. pr_debug("%s: returning rc=%d\n", __func__, rc);
  859. return rc;
  860. }
  861. /**
  862. * set_port_online() - transitions the specified host FC port to online state
  863. * @fc_regs: Top of MMIO region defined for specified port.
  864. *
  865. * The provided MMIO region must be mapped prior to call. Online state means
  866. * that the FC link layer has synced, completed the handshaking process, and
  867. * is ready for login to start.
  868. */
  869. static void set_port_online(__be64 __iomem *fc_regs)
  870. {
  871. u64 cmdcfg;
  872. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  873. cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
  874. cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
  875. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  876. }
  877. /**
  878. * set_port_offline() - transitions the specified host FC port to offline state
  879. * @fc_regs: Top of MMIO region defined for specified port.
  880. *
  881. * The provided MMIO region must be mapped prior to call.
  882. */
  883. static void set_port_offline(__be64 __iomem *fc_regs)
  884. {
  885. u64 cmdcfg;
  886. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  887. cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
  888. cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
  889. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  890. }
  891. /**
  892. * wait_port_online() - waits for the specified host FC port come online
  893. * @fc_regs: Top of MMIO region defined for specified port.
  894. * @delay_us: Number of microseconds to delay between reading port status.
  895. * @nretry: Number of cycles to retry reading port status.
  896. *
  897. * The provided MMIO region must be mapped prior to call. This will timeout
  898. * when the cable is not plugged in.
  899. *
  900. * Return:
  901. * TRUE (1) when the specified port is online
  902. * FALSE (0) when the specified port fails to come online after timeout
  903. * -EINVAL when @delay_us is less than 1000
  904. */
  905. static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  906. {
  907. u64 status;
  908. if (delay_us < 1000) {
  909. pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
  910. return -EINVAL;
  911. }
  912. do {
  913. msleep(delay_us / 1000);
  914. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  915. if (status == U64_MAX)
  916. nretry /= 2;
  917. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
  918. nretry--);
  919. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
  920. }
  921. /**
  922. * wait_port_offline() - waits for the specified host FC port go offline
  923. * @fc_regs: Top of MMIO region defined for specified port.
  924. * @delay_us: Number of microseconds to delay between reading port status.
  925. * @nretry: Number of cycles to retry reading port status.
  926. *
  927. * The provided MMIO region must be mapped prior to call.
  928. *
  929. * Return:
  930. * TRUE (1) when the specified port is offline
  931. * FALSE (0) when the specified port fails to go offline after timeout
  932. * -EINVAL when @delay_us is less than 1000
  933. */
  934. static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  935. {
  936. u64 status;
  937. if (delay_us < 1000) {
  938. pr_err("%s: invalid delay specified %d\n", __func__, delay_us);
  939. return -EINVAL;
  940. }
  941. do {
  942. msleep(delay_us / 1000);
  943. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  944. if (status == U64_MAX)
  945. nretry /= 2;
  946. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
  947. nretry--);
  948. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
  949. }
  950. /**
  951. * afu_set_wwpn() - configures the WWPN for 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. * @wwpn: The world-wide-port-number previously discovered for port.
  956. *
  957. * The provided MMIO region must be mapped prior to call. As part of the
  958. * sequence to configure the WWPN, the port is toggled offline and then back
  959. * online. This toggling action can cause this routine to delay up to a few
  960. * seconds. When configured to use the internal LUN feature of the AFU, a
  961. * failure to come online is overridden.
  962. */
  963. static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
  964. u64 wwpn)
  965. {
  966. set_port_offline(fc_regs);
  967. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  968. FC_PORT_STATUS_RETRY_CNT)) {
  969. pr_debug("%s: wait on port %d to go offline timed out\n",
  970. __func__, port);
  971. }
  972. writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
  973. set_port_online(fc_regs);
  974. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  975. FC_PORT_STATUS_RETRY_CNT)) {
  976. pr_debug("%s: wait on port %d to go online timed out\n",
  977. __func__, port);
  978. }
  979. }
  980. /**
  981. * afu_link_reset() - resets the specified host FC port
  982. * @afu: AFU associated with the host that owns the specified FC port.
  983. * @port: Port number being configured.
  984. * @fc_regs: Top of MMIO region defined for specified port.
  985. *
  986. * The provided MMIO region must be mapped prior to call. The sequence to
  987. * reset the port involves toggling it offline and then back online. This
  988. * action can cause this routine to delay up to a few seconds. An effort
  989. * is made to maintain link with the device by switching to host to use
  990. * the alternate port exclusively while the reset takes place.
  991. * failure to come online is overridden.
  992. */
  993. static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
  994. {
  995. u64 port_sel;
  996. /* first switch the AFU to the other links, if any */
  997. port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
  998. port_sel &= ~(1ULL << port);
  999. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  1000. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  1001. set_port_offline(fc_regs);
  1002. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1003. FC_PORT_STATUS_RETRY_CNT))
  1004. pr_err("%s: wait on port %d to go offline timed out\n",
  1005. __func__, port);
  1006. set_port_online(fc_regs);
  1007. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1008. FC_PORT_STATUS_RETRY_CNT))
  1009. pr_err("%s: wait on port %d to go online timed out\n",
  1010. __func__, port);
  1011. /* switch back to include this port */
  1012. port_sel |= (1ULL << port);
  1013. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  1014. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  1015. pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel);
  1016. }
  1017. /*
  1018. * Asynchronous interrupt information table
  1019. */
  1020. static const struct asyc_intr_info ainfo[] = {
  1021. {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET},
  1022. {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0},
  1023. {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET},
  1024. {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET},
  1025. {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR},
  1026. {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST},
  1027. {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0},
  1028. {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0},
  1029. {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET},
  1030. {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0},
  1031. {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET},
  1032. {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET},
  1033. {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR},
  1034. {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST},
  1035. {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0},
  1036. {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0},
  1037. {0x0, "", 0, 0} /* terminator */
  1038. };
  1039. /**
  1040. * find_ainfo() - locates and returns asynchronous interrupt information
  1041. * @status: Status code set by AFU on error.
  1042. *
  1043. * Return: The located information or NULL when the status code is invalid.
  1044. */
  1045. static const struct asyc_intr_info *find_ainfo(u64 status)
  1046. {
  1047. const struct asyc_intr_info *info;
  1048. for (info = &ainfo[0]; info->status; info++)
  1049. if (info->status == status)
  1050. return info;
  1051. return NULL;
  1052. }
  1053. /**
  1054. * afu_err_intr_init() - clears and initializes the AFU for error interrupts
  1055. * @afu: AFU associated with the host.
  1056. */
  1057. static void afu_err_intr_init(struct afu *afu)
  1058. {
  1059. int i;
  1060. u64 reg;
  1061. /* global async interrupts: AFU clears afu_ctrl on context exit
  1062. * if async interrupts were sent to that context. This prevents
  1063. * the AFU form sending further async interrupts when
  1064. * there is
  1065. * nobody to receive them.
  1066. */
  1067. /* mask all */
  1068. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
  1069. /* set LISN# to send and point to master context */
  1070. reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
  1071. if (afu->internal_lun)
  1072. reg |= 1; /* Bit 63 indicates local lun */
  1073. writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
  1074. /* clear all */
  1075. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1076. /* unmask bits that are of interest */
  1077. /* note: afu can send an interrupt after this step */
  1078. writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
  1079. /* clear again in case a bit came on after previous clear but before */
  1080. /* unmask */
  1081. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1082. /* Clear/Set internal lun bits */
  1083. reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
  1084. reg &= SISL_FC_INTERNAL_MASK;
  1085. if (afu->internal_lun)
  1086. reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
  1087. writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]);
  1088. /* now clear FC errors */
  1089. for (i = 0; i < NUM_FC_PORTS; i++) {
  1090. writeq_be(0xFFFFFFFFU,
  1091. &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]);
  1092. writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]);
  1093. }
  1094. /* sync interrupts for master's IOARRIN write */
  1095. /* note that unlike asyncs, there can be no pending sync interrupts */
  1096. /* at this time (this is a fresh context and master has not written */
  1097. /* IOARRIN yet), so there is nothing to clear. */
  1098. /* set LISN#, it is always sent to the context that wrote IOARRIN */
  1099. writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl);
  1100. writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask);
  1101. }
  1102. /**
  1103. * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
  1104. * @irq: Interrupt number.
  1105. * @data: Private data provided at interrupt registration, the AFU.
  1106. *
  1107. * Return: Always return IRQ_HANDLED.
  1108. */
  1109. static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
  1110. {
  1111. struct afu *afu = (struct afu *)data;
  1112. u64 reg;
  1113. u64 reg_unmasked;
  1114. reg = readq_be(&afu->host_map->intr_status);
  1115. reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
  1116. if (reg_unmasked == 0UL) {
  1117. pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n",
  1118. __func__, (u64)afu, reg);
  1119. goto cxlflash_sync_err_irq_exit;
  1120. }
  1121. pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n",
  1122. __func__, (u64)afu, reg);
  1123. writeq_be(reg_unmasked, &afu->host_map->intr_clear);
  1124. cxlflash_sync_err_irq_exit:
  1125. pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED);
  1126. return IRQ_HANDLED;
  1127. }
  1128. /**
  1129. * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
  1130. * @irq: Interrupt number.
  1131. * @data: Private data provided at interrupt registration, the AFU.
  1132. *
  1133. * Return: Always return IRQ_HANDLED.
  1134. */
  1135. static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
  1136. {
  1137. struct afu *afu = (struct afu *)data;
  1138. struct afu_cmd *cmd;
  1139. bool toggle = afu->toggle;
  1140. u64 entry,
  1141. *hrrq_start = afu->hrrq_start,
  1142. *hrrq_end = afu->hrrq_end,
  1143. *hrrq_curr = afu->hrrq_curr;
  1144. /* Process however many RRQ entries that are ready */
  1145. while (true) {
  1146. entry = *hrrq_curr;
  1147. if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
  1148. break;
  1149. cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT);
  1150. cmd_complete(cmd);
  1151. /* Advance to next entry or wrap and flip the toggle bit */
  1152. if (hrrq_curr < hrrq_end)
  1153. hrrq_curr++;
  1154. else {
  1155. hrrq_curr = hrrq_start;
  1156. toggle ^= SISL_RESP_HANDLE_T_BIT;
  1157. }
  1158. }
  1159. afu->hrrq_curr = hrrq_curr;
  1160. afu->toggle = toggle;
  1161. return IRQ_HANDLED;
  1162. }
  1163. /**
  1164. * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
  1165. * @irq: Interrupt number.
  1166. * @data: Private data provided at interrupt registration, the AFU.
  1167. *
  1168. * Return: Always return IRQ_HANDLED.
  1169. */
  1170. static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
  1171. {
  1172. struct afu *afu = (struct afu *)data;
  1173. struct cxlflash_cfg *cfg = afu->parent;
  1174. struct device *dev = &cfg->dev->dev;
  1175. u64 reg_unmasked;
  1176. const struct asyc_intr_info *info;
  1177. struct sisl_global_map __iomem *global = &afu->afu_map->global;
  1178. u64 reg;
  1179. u8 port;
  1180. int i;
  1181. reg = readq_be(&global->regs.aintr_status);
  1182. reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
  1183. if (reg_unmasked == 0) {
  1184. dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n",
  1185. __func__, reg);
  1186. goto out;
  1187. }
  1188. /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
  1189. writeq_be(reg_unmasked, &global->regs.aintr_clear);
  1190. /* Check each bit that is on */
  1191. for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) {
  1192. info = find_ainfo(1ULL << i);
  1193. if (((reg_unmasked & 0x1) == 0) || !info)
  1194. continue;
  1195. port = info->port;
  1196. dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n",
  1197. __func__, port, info->desc,
  1198. readq_be(&global->fc_regs[port][FC_STATUS / 8]));
  1199. /*
  1200. * Do link reset first, some OTHER errors will set FC_ERROR
  1201. * again if cleared before or w/o a reset
  1202. */
  1203. if (info->action & LINK_RESET) {
  1204. dev_err(dev, "%s: FC Port %d: resetting link\n",
  1205. __func__, port);
  1206. cfg->lr_state = LINK_RESET_REQUIRED;
  1207. cfg->lr_port = port;
  1208. kref_get(&cfg->afu->mapcount);
  1209. schedule_work(&cfg->work_q);
  1210. }
  1211. if (info->action & CLR_FC_ERROR) {
  1212. reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]);
  1213. /*
  1214. * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
  1215. * should be the same and tracing one is sufficient.
  1216. */
  1217. dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n",
  1218. __func__, port, reg);
  1219. writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]);
  1220. writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]);
  1221. }
  1222. if (info->action & SCAN_HOST) {
  1223. atomic_inc(&cfg->scan_host_needed);
  1224. kref_get(&cfg->afu->mapcount);
  1225. schedule_work(&cfg->work_q);
  1226. }
  1227. }
  1228. out:
  1229. dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu);
  1230. return IRQ_HANDLED;
  1231. }
  1232. /**
  1233. * start_context() - starts the master context
  1234. * @cfg: Internal structure associated with the host.
  1235. *
  1236. * Return: A success or failure value from CXL services.
  1237. */
  1238. static int start_context(struct cxlflash_cfg *cfg)
  1239. {
  1240. int rc = 0;
  1241. rc = cxl_start_context(cfg->mcctx,
  1242. cfg->afu->work.work_element_descriptor,
  1243. NULL);
  1244. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1245. return rc;
  1246. }
  1247. /**
  1248. * read_vpd() - obtains the WWPNs from VPD
  1249. * @cfg: Internal structure associated with the host.
  1250. * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs
  1251. *
  1252. * Return: 0 on success, -errno on failure
  1253. */
  1254. static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
  1255. {
  1256. struct pci_dev *dev = cfg->dev;
  1257. int rc = 0;
  1258. int ro_start, ro_size, i, j, k;
  1259. ssize_t vpd_size;
  1260. char vpd_data[CXLFLASH_VPD_LEN];
  1261. char tmp_buf[WWPN_BUF_LEN] = { 0 };
  1262. char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" };
  1263. /* Get the VPD data from the device */
  1264. vpd_size = cxl_read_adapter_vpd(dev, vpd_data, sizeof(vpd_data));
  1265. if (unlikely(vpd_size <= 0)) {
  1266. dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n",
  1267. __func__, vpd_size);
  1268. rc = -ENODEV;
  1269. goto out;
  1270. }
  1271. /* Get the read only section offset */
  1272. ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size,
  1273. PCI_VPD_LRDT_RO_DATA);
  1274. if (unlikely(ro_start < 0)) {
  1275. dev_err(&dev->dev, "%s: VPD Read-only data not found\n",
  1276. __func__);
  1277. rc = -ENODEV;
  1278. goto out;
  1279. }
  1280. /* Get the read only section size, cap when extends beyond read VPD */
  1281. ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
  1282. j = ro_size;
  1283. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  1284. if (unlikely((i + j) > vpd_size)) {
  1285. pr_debug("%s: Might need to read more VPD (%d > %ld)\n",
  1286. __func__, (i + j), vpd_size);
  1287. ro_size = vpd_size - i;
  1288. }
  1289. /*
  1290. * Find the offset of the WWPN tag within the read only
  1291. * VPD data and validate the found field (partials are
  1292. * no good to us). Convert the ASCII data to an integer
  1293. * value. Note that we must copy to a temporary buffer
  1294. * because the conversion service requires that the ASCII
  1295. * string be terminated.
  1296. */
  1297. for (k = 0; k < NUM_FC_PORTS; k++) {
  1298. j = ro_size;
  1299. i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
  1300. i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]);
  1301. if (unlikely(i < 0)) {
  1302. dev_err(&dev->dev, "%s: Port %d WWPN not found "
  1303. "in VPD\n", __func__, k);
  1304. rc = -ENODEV;
  1305. goto out;
  1306. }
  1307. j = pci_vpd_info_field_size(&vpd_data[i]);
  1308. i += PCI_VPD_INFO_FLD_HDR_SIZE;
  1309. if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) {
  1310. dev_err(&dev->dev, "%s: Port %d WWPN incomplete or "
  1311. "VPD corrupt\n",
  1312. __func__, k);
  1313. rc = -ENODEV;
  1314. goto out;
  1315. }
  1316. memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
  1317. rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
  1318. if (unlikely(rc)) {
  1319. dev_err(&dev->dev, "%s: Fail to convert port %d WWPN "
  1320. "to integer\n", __func__, k);
  1321. rc = -ENODEV;
  1322. goto out;
  1323. }
  1324. }
  1325. out:
  1326. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1327. return rc;
  1328. }
  1329. /**
  1330. * init_pcr() - initialize the provisioning and control registers
  1331. * @cfg: Internal structure associated with the host.
  1332. *
  1333. * Also sets up fast access to the mapped registers and initializes AFU
  1334. * command fields that never change.
  1335. */
  1336. static void init_pcr(struct cxlflash_cfg *cfg)
  1337. {
  1338. struct afu *afu = cfg->afu;
  1339. struct sisl_ctrl_map __iomem *ctrl_map;
  1340. int i;
  1341. for (i = 0; i < MAX_CONTEXT; i++) {
  1342. ctrl_map = &afu->afu_map->ctrls[i].ctrl;
  1343. /* Disrupt any clients that could be running */
  1344. /* e.g. clients that survived a master restart */
  1345. writeq_be(0, &ctrl_map->rht_start);
  1346. writeq_be(0, &ctrl_map->rht_cnt_id);
  1347. writeq_be(0, &ctrl_map->ctx_cap);
  1348. }
  1349. /* Copy frequently used fields into afu */
  1350. afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx);
  1351. afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host;
  1352. afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl;
  1353. /* Program the Endian Control for the master context */
  1354. writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl);
  1355. /* Initialize cmd fields that never change */
  1356. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  1357. afu->cmd[i].rcb.ctx_id = afu->ctx_hndl;
  1358. afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED;
  1359. afu->cmd[i].rcb.rrq = 0x0;
  1360. }
  1361. }
  1362. /**
  1363. * init_global() - initialize AFU global registers
  1364. * @cfg: Internal structure associated with the host.
  1365. */
  1366. static int init_global(struct cxlflash_cfg *cfg)
  1367. {
  1368. struct afu *afu = cfg->afu;
  1369. struct device *dev = &cfg->dev->dev;
  1370. u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */
  1371. int i = 0, num_ports = 0;
  1372. int rc = 0;
  1373. u64 reg;
  1374. rc = read_vpd(cfg, &wwpn[0]);
  1375. if (rc) {
  1376. dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
  1377. goto out;
  1378. }
  1379. pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]);
  1380. /* Set up RRQ in AFU for master issued cmds */
  1381. writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start);
  1382. writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end);
  1383. /* AFU configuration */
  1384. reg = readq_be(&afu->afu_map->global.regs.afu_config);
  1385. reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
  1386. /* enable all auto retry options and control endianness */
  1387. /* leave others at default: */
  1388. /* CTX_CAP write protected, mbox_r does not clear on read and */
  1389. /* checker on if dual afu */
  1390. writeq_be(reg, &afu->afu_map->global.regs.afu_config);
  1391. /* Global port select: select either port */
  1392. if (afu->internal_lun) {
  1393. /* Only use port 0 */
  1394. writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
  1395. num_ports = NUM_FC_PORTS - 1;
  1396. } else {
  1397. writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel);
  1398. num_ports = NUM_FC_PORTS;
  1399. }
  1400. for (i = 0; i < num_ports; i++) {
  1401. /* Unmask all errors (but they are still masked at AFU) */
  1402. writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]);
  1403. /* Clear CRC error cnt & set a threshold */
  1404. (void)readq_be(&afu->afu_map->global.
  1405. fc_regs[i][FC_CNT_CRCERR / 8]);
  1406. writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i]
  1407. [FC_CRC_THRESH / 8]);
  1408. /* Set WWPNs. If already programmed, wwpn[i] is 0 */
  1409. if (wwpn[i] != 0)
  1410. afu_set_wwpn(afu, i,
  1411. &afu->afu_map->global.fc_regs[i][0],
  1412. wwpn[i]);
  1413. /* Programming WWPN back to back causes additional
  1414. * offline/online transitions and a PLOGI
  1415. */
  1416. msleep(100);
  1417. }
  1418. /* Set up master's own CTX_CAP to allow real mode, host translation */
  1419. /* tables, afu cmds and read/write GSCSI cmds. */
  1420. /* First, unlock ctx_cap write by reading mbox */
  1421. (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */
  1422. writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
  1423. SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
  1424. SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
  1425. &afu->ctrl_map->ctx_cap);
  1426. /* Initialize heartbeat */
  1427. afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
  1428. out:
  1429. return rc;
  1430. }
  1431. /**
  1432. * start_afu() - initializes and starts the AFU
  1433. * @cfg: Internal structure associated with the host.
  1434. */
  1435. static int start_afu(struct cxlflash_cfg *cfg)
  1436. {
  1437. struct afu *afu = cfg->afu;
  1438. struct afu_cmd *cmd;
  1439. int i = 0;
  1440. int rc = 0;
  1441. for (i = 0; i < CXLFLASH_NUM_CMDS; i++) {
  1442. cmd = &afu->cmd[i];
  1443. init_completion(&cmd->cevent);
  1444. spin_lock_init(&cmd->slock);
  1445. cmd->parent = afu;
  1446. }
  1447. init_pcr(cfg);
  1448. /* After an AFU reset, RRQ entries are stale, clear them */
  1449. memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry));
  1450. /* Initialize RRQ pointers */
  1451. afu->hrrq_start = &afu->rrq_entry[0];
  1452. afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1];
  1453. afu->hrrq_curr = afu->hrrq_start;
  1454. afu->toggle = 1;
  1455. rc = init_global(cfg);
  1456. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1457. return rc;
  1458. }
  1459. /**
  1460. * init_intr() - setup interrupt handlers for the master context
  1461. * @cfg: Internal structure associated with the host.
  1462. *
  1463. * Return: 0 on success, -errno on failure
  1464. */
  1465. static enum undo_level init_intr(struct cxlflash_cfg *cfg,
  1466. struct cxl_context *ctx)
  1467. {
  1468. struct afu *afu = cfg->afu;
  1469. struct device *dev = &cfg->dev->dev;
  1470. int rc = 0;
  1471. enum undo_level level = UNDO_NOOP;
  1472. rc = cxl_allocate_afu_irqs(ctx, 3);
  1473. if (unlikely(rc)) {
  1474. dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n",
  1475. __func__, rc);
  1476. level = UNDO_NOOP;
  1477. goto out;
  1478. }
  1479. rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu,
  1480. "SISL_MSI_SYNC_ERROR");
  1481. if (unlikely(rc <= 0)) {
  1482. dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n",
  1483. __func__);
  1484. level = FREE_IRQ;
  1485. goto out;
  1486. }
  1487. rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu,
  1488. "SISL_MSI_RRQ_UPDATED");
  1489. if (unlikely(rc <= 0)) {
  1490. dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n",
  1491. __func__);
  1492. level = UNMAP_ONE;
  1493. goto out;
  1494. }
  1495. rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu,
  1496. "SISL_MSI_ASYNC_ERROR");
  1497. if (unlikely(rc <= 0)) {
  1498. dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n",
  1499. __func__);
  1500. level = UNMAP_TWO;
  1501. goto out;
  1502. }
  1503. out:
  1504. return level;
  1505. }
  1506. /**
  1507. * init_mc() - create and register as the master context
  1508. * @cfg: Internal structure associated with the host.
  1509. *
  1510. * Return: 0 on success, -errno on failure
  1511. */
  1512. static int init_mc(struct cxlflash_cfg *cfg)
  1513. {
  1514. struct cxl_context *ctx;
  1515. struct device *dev = &cfg->dev->dev;
  1516. int rc = 0;
  1517. enum undo_level level;
  1518. ctx = cxl_get_context(cfg->dev);
  1519. if (unlikely(!ctx)) {
  1520. rc = -ENOMEM;
  1521. goto ret;
  1522. }
  1523. cfg->mcctx = ctx;
  1524. /* Set it up as a master with the CXL */
  1525. cxl_set_master(ctx);
  1526. /* During initialization reset the AFU to start from a clean slate */
  1527. rc = cxl_afu_reset(cfg->mcctx);
  1528. if (unlikely(rc)) {
  1529. dev_err(dev, "%s: initial AFU reset failed rc=%d\n",
  1530. __func__, rc);
  1531. goto ret;
  1532. }
  1533. level = init_intr(cfg, ctx);
  1534. if (unlikely(level)) {
  1535. dev_err(dev, "%s: setting up interrupts failed rc=%d\n",
  1536. __func__, rc);
  1537. goto out;
  1538. }
  1539. /* This performs the equivalent of the CXL_IOCTL_START_WORK.
  1540. * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process
  1541. * element (pe) that is embedded in the context (ctx)
  1542. */
  1543. rc = start_context(cfg);
  1544. if (unlikely(rc)) {
  1545. dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
  1546. level = UNMAP_THREE;
  1547. goto out;
  1548. }
  1549. ret:
  1550. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1551. return rc;
  1552. out:
  1553. term_intr(cfg, level);
  1554. goto ret;
  1555. }
  1556. /**
  1557. * init_afu() - setup as master context and start AFU
  1558. * @cfg: Internal structure associated with the host.
  1559. *
  1560. * This routine is a higher level of control for configuring the
  1561. * AFU on probe and reset paths.
  1562. *
  1563. * Return: 0 on success, -errno on failure
  1564. */
  1565. static int init_afu(struct cxlflash_cfg *cfg)
  1566. {
  1567. u64 reg;
  1568. int rc = 0;
  1569. struct afu *afu = cfg->afu;
  1570. struct device *dev = &cfg->dev->dev;
  1571. cxl_perst_reloads_same_image(cfg->cxl_afu, true);
  1572. rc = init_mc(cfg);
  1573. if (rc) {
  1574. dev_err(dev, "%s: call to init_mc failed, rc=%d!\n",
  1575. __func__, rc);
  1576. goto out;
  1577. }
  1578. /* Map the entire MMIO space of the AFU */
  1579. afu->afu_map = cxl_psa_map(cfg->mcctx);
  1580. if (!afu->afu_map) {
  1581. dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__);
  1582. rc = -ENOMEM;
  1583. goto err1;
  1584. }
  1585. kref_init(&afu->mapcount);
  1586. /* No byte reverse on reading afu_version or string will be backwards */
  1587. reg = readq(&afu->afu_map->global.regs.afu_version);
  1588. memcpy(afu->version, &reg, sizeof(reg));
  1589. afu->interface_version =
  1590. readq_be(&afu->afu_map->global.regs.interface_version);
  1591. if ((afu->interface_version + 1) == 0) {
  1592. pr_err("Back level AFU, please upgrade. AFU version %s "
  1593. "interface version 0x%llx\n", afu->version,
  1594. afu->interface_version);
  1595. rc = -EINVAL;
  1596. goto err2;
  1597. }
  1598. pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__,
  1599. afu->version, afu->interface_version);
  1600. rc = start_afu(cfg);
  1601. if (rc) {
  1602. dev_err(dev, "%s: call to start_afu failed, rc=%d!\n",
  1603. __func__, rc);
  1604. goto err2;
  1605. }
  1606. afu_err_intr_init(cfg->afu);
  1607. atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
  1608. /* Restore the LUN mappings */
  1609. cxlflash_restore_luntable(cfg);
  1610. out:
  1611. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1612. return rc;
  1613. err2:
  1614. kref_put(&afu->mapcount, afu_unmap);
  1615. err1:
  1616. term_intr(cfg, UNMAP_THREE);
  1617. term_mc(cfg);
  1618. goto out;
  1619. }
  1620. /**
  1621. * cxlflash_afu_sync() - builds and sends an AFU sync command
  1622. * @afu: AFU associated with the host.
  1623. * @ctx_hndl_u: Identifies context requesting sync.
  1624. * @res_hndl_u: Identifies resource requesting sync.
  1625. * @mode: Type of sync to issue (lightweight, heavyweight, global).
  1626. *
  1627. * The AFU can only take 1 sync command at a time. This routine enforces this
  1628. * limitation by using a mutex to provide exclusive access to the AFU during
  1629. * the sync. This design point requires calling threads to not be on interrupt
  1630. * context due to the possibility of sleeping during concurrent sync operations.
  1631. *
  1632. * AFU sync operations are only necessary and allowed when the device is
  1633. * operating normally. When not operating normally, sync requests can occur as
  1634. * part of cleaning up resources associated with an adapter prior to removal.
  1635. * In this scenario, these requests are simply ignored (safe due to the AFU
  1636. * going away).
  1637. *
  1638. * Return:
  1639. * 0 on success
  1640. * -1 on failure
  1641. */
  1642. int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u,
  1643. res_hndl_t res_hndl_u, u8 mode)
  1644. {
  1645. struct cxlflash_cfg *cfg = afu->parent;
  1646. struct device *dev = &cfg->dev->dev;
  1647. struct afu_cmd *cmd = NULL;
  1648. int rc = 0;
  1649. int retry_cnt = 0;
  1650. static DEFINE_MUTEX(sync_active);
  1651. if (cfg->state != STATE_NORMAL) {
  1652. pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state);
  1653. return 0;
  1654. }
  1655. mutex_lock(&sync_active);
  1656. retry:
  1657. cmd = cmd_checkout(afu);
  1658. if (unlikely(!cmd)) {
  1659. retry_cnt++;
  1660. udelay(1000 * retry_cnt);
  1661. if (retry_cnt < MC_RETRY_CNT)
  1662. goto retry;
  1663. dev_err(dev, "%s: could not get a free command\n", __func__);
  1664. rc = -1;
  1665. goto out;
  1666. }
  1667. pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u);
  1668. memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb));
  1669. cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
  1670. cmd->rcb.port_sel = 0x0; /* NA */
  1671. cmd->rcb.lun_id = 0x0; /* NA */
  1672. cmd->rcb.data_len = 0x0;
  1673. cmd->rcb.data_ea = 0x0;
  1674. cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT;
  1675. cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */
  1676. cmd->rcb.cdb[1] = mode;
  1677. /* The cdb is aligned, no unaligned accessors required */
  1678. *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u);
  1679. *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u);
  1680. rc = send_cmd(afu, cmd);
  1681. if (unlikely(rc))
  1682. goto out;
  1683. wait_resp(afu, cmd);
  1684. /* Set on timeout */
  1685. if (unlikely((cmd->sa.ioasc != 0) ||
  1686. (cmd->sa.host_use_b[0] & B_ERROR)))
  1687. rc = -1;
  1688. out:
  1689. mutex_unlock(&sync_active);
  1690. if (cmd)
  1691. cmd_checkin(cmd);
  1692. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1693. return rc;
  1694. }
  1695. /**
  1696. * afu_reset() - resets the AFU
  1697. * @cfg: Internal structure associated with the host.
  1698. *
  1699. * Return: 0 on success, -errno on failure
  1700. */
  1701. static int afu_reset(struct cxlflash_cfg *cfg)
  1702. {
  1703. int rc = 0;
  1704. /* Stop the context before the reset. Since the context is
  1705. * no longer available restart it after the reset is complete
  1706. */
  1707. term_afu(cfg);
  1708. rc = init_afu(cfg);
  1709. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1710. return rc;
  1711. }
  1712. /**
  1713. * drain_ioctls() - wait until all currently executing ioctls have completed
  1714. * @cfg: Internal structure associated with the host.
  1715. *
  1716. * Obtain write access to read/write semaphore that wraps ioctl
  1717. * handling to 'drain' ioctls currently executing.
  1718. */
  1719. static void drain_ioctls(struct cxlflash_cfg *cfg)
  1720. {
  1721. down_write(&cfg->ioctl_rwsem);
  1722. up_write(&cfg->ioctl_rwsem);
  1723. }
  1724. /**
  1725. * cxlflash_eh_device_reset_handler() - reset a single LUN
  1726. * @scp: SCSI command to send.
  1727. *
  1728. * Return:
  1729. * SUCCESS as defined in scsi/scsi.h
  1730. * FAILED as defined in scsi/scsi.h
  1731. */
  1732. static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
  1733. {
  1734. int rc = SUCCESS;
  1735. struct Scsi_Host *host = scp->device->host;
  1736. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  1737. struct afu *afu = cfg->afu;
  1738. int rcr = 0;
  1739. pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
  1740. "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
  1741. host->host_no, scp->device->channel,
  1742. scp->device->id, scp->device->lun,
  1743. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  1744. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  1745. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  1746. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  1747. retry:
  1748. switch (cfg->state) {
  1749. case STATE_NORMAL:
  1750. rcr = send_tmf(afu, scp, TMF_LUN_RESET);
  1751. if (unlikely(rcr))
  1752. rc = FAILED;
  1753. break;
  1754. case STATE_RESET:
  1755. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  1756. goto retry;
  1757. default:
  1758. rc = FAILED;
  1759. break;
  1760. }
  1761. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1762. return rc;
  1763. }
  1764. /**
  1765. * cxlflash_eh_host_reset_handler() - reset the host adapter
  1766. * @scp: SCSI command from stack identifying host.
  1767. *
  1768. * Following a reset, the state is evaluated again in case an EEH occurred
  1769. * during the reset. In such a scenario, the host reset will either yield
  1770. * until the EEH recovery is complete or return success or failure based
  1771. * upon the current device state.
  1772. *
  1773. * Return:
  1774. * SUCCESS as defined in scsi/scsi.h
  1775. * FAILED as defined in scsi/scsi.h
  1776. */
  1777. static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
  1778. {
  1779. int rc = SUCCESS;
  1780. int rcr = 0;
  1781. struct Scsi_Host *host = scp->device->host;
  1782. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata;
  1783. pr_debug("%s: (scp=%p) %d/%d/%d/%llu "
  1784. "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp,
  1785. host->host_no, scp->device->channel,
  1786. scp->device->id, scp->device->lun,
  1787. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  1788. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  1789. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  1790. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  1791. switch (cfg->state) {
  1792. case STATE_NORMAL:
  1793. cfg->state = STATE_RESET;
  1794. drain_ioctls(cfg);
  1795. cxlflash_mark_contexts_error(cfg);
  1796. rcr = afu_reset(cfg);
  1797. if (rcr) {
  1798. rc = FAILED;
  1799. cfg->state = STATE_FAILTERM;
  1800. } else
  1801. cfg->state = STATE_NORMAL;
  1802. wake_up_all(&cfg->reset_waitq);
  1803. ssleep(1);
  1804. /* fall through */
  1805. case STATE_RESET:
  1806. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  1807. if (cfg->state == STATE_NORMAL)
  1808. break;
  1809. /* fall through */
  1810. default:
  1811. rc = FAILED;
  1812. break;
  1813. }
  1814. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1815. return rc;
  1816. }
  1817. /**
  1818. * cxlflash_change_queue_depth() - change the queue depth for the device
  1819. * @sdev: SCSI device destined for queue depth change.
  1820. * @qdepth: Requested queue depth value to set.
  1821. *
  1822. * The requested queue depth is capped to the maximum supported value.
  1823. *
  1824. * Return: The actual queue depth set.
  1825. */
  1826. static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1827. {
  1828. if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
  1829. qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
  1830. scsi_change_queue_depth(sdev, qdepth);
  1831. return sdev->queue_depth;
  1832. }
  1833. /**
  1834. * cxlflash_show_port_status() - queries and presents the current port status
  1835. * @port: Desired port for status reporting.
  1836. * @afu: AFU owning the specified port.
  1837. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1838. *
  1839. * Return: The size of the ASCII string returned in @buf.
  1840. */
  1841. static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf)
  1842. {
  1843. char *disp_status;
  1844. u64 status;
  1845. __be64 __iomem *fc_regs;
  1846. if (port >= NUM_FC_PORTS)
  1847. return 0;
  1848. fc_regs = &afu->afu_map->global.fc_regs[port][0];
  1849. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  1850. status &= FC_MTIP_STATUS_MASK;
  1851. if (status == FC_MTIP_STATUS_ONLINE)
  1852. disp_status = "online";
  1853. else if (status == FC_MTIP_STATUS_OFFLINE)
  1854. disp_status = "offline";
  1855. else
  1856. disp_status = "unknown";
  1857. return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
  1858. }
  1859. /**
  1860. * port0_show() - queries and presents the current status of port 0
  1861. * @dev: Generic device associated with the host owning the port.
  1862. * @attr: Device attribute representing the port.
  1863. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1864. *
  1865. * Return: The size of the ASCII string returned in @buf.
  1866. */
  1867. static ssize_t port0_show(struct device *dev,
  1868. struct device_attribute *attr,
  1869. char *buf)
  1870. {
  1871. struct Scsi_Host *shost = class_to_shost(dev);
  1872. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1873. struct afu *afu = cfg->afu;
  1874. return cxlflash_show_port_status(0, afu, buf);
  1875. }
  1876. /**
  1877. * port1_show() - queries and presents the current status of port 1
  1878. * @dev: Generic device associated with the host owning the port.
  1879. * @attr: Device attribute representing the port.
  1880. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1881. *
  1882. * Return: The size of the ASCII string returned in @buf.
  1883. */
  1884. static ssize_t port1_show(struct device *dev,
  1885. struct device_attribute *attr,
  1886. char *buf)
  1887. {
  1888. struct Scsi_Host *shost = class_to_shost(dev);
  1889. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1890. struct afu *afu = cfg->afu;
  1891. return cxlflash_show_port_status(1, afu, buf);
  1892. }
  1893. /**
  1894. * lun_mode_show() - presents the current LUN mode of the host
  1895. * @dev: Generic device associated with the host.
  1896. * @attr: Device attribute representing the LUN mode.
  1897. * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
  1898. *
  1899. * Return: The size of the ASCII string returned in @buf.
  1900. */
  1901. static ssize_t lun_mode_show(struct device *dev,
  1902. struct device_attribute *attr, char *buf)
  1903. {
  1904. struct Scsi_Host *shost = class_to_shost(dev);
  1905. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1906. struct afu *afu = cfg->afu;
  1907. return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
  1908. }
  1909. /**
  1910. * lun_mode_store() - sets the LUN mode of the host
  1911. * @dev: Generic device associated with the host.
  1912. * @attr: Device attribute representing the LUN mode.
  1913. * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
  1914. * @count: Length of data resizing in @buf.
  1915. *
  1916. * The CXL Flash AFU supports a dummy LUN mode where the external
  1917. * links and storage are not required. Space on the FPGA is used
  1918. * to create 1 or 2 small LUNs which are presented to the system
  1919. * as if they were a normal storage device. This feature is useful
  1920. * during development and also provides manufacturing with a way
  1921. * to test the AFU without an actual device.
  1922. *
  1923. * 0 = external LUN[s] (default)
  1924. * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
  1925. * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
  1926. * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
  1927. * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
  1928. *
  1929. * Return: The size of the ASCII string returned in @buf.
  1930. */
  1931. static ssize_t lun_mode_store(struct device *dev,
  1932. struct device_attribute *attr,
  1933. const char *buf, size_t count)
  1934. {
  1935. struct Scsi_Host *shost = class_to_shost(dev);
  1936. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  1937. struct afu *afu = cfg->afu;
  1938. int rc;
  1939. u32 lun_mode;
  1940. rc = kstrtouint(buf, 10, &lun_mode);
  1941. if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
  1942. afu->internal_lun = lun_mode;
  1943. /*
  1944. * When configured for internal LUN, there is only one channel,
  1945. * channel number 0, else there will be 2 (default).
  1946. */
  1947. if (afu->internal_lun)
  1948. shost->max_channel = 0;
  1949. else
  1950. shost->max_channel = NUM_FC_PORTS - 1;
  1951. afu_reset(cfg);
  1952. scsi_scan_host(cfg->host);
  1953. }
  1954. return count;
  1955. }
  1956. /**
  1957. * ioctl_version_show() - presents the current ioctl version of the host
  1958. * @dev: Generic device associated with the host.
  1959. * @attr: Device attribute representing the ioctl version.
  1960. * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
  1961. *
  1962. * Return: The size of the ASCII string returned in @buf.
  1963. */
  1964. static ssize_t ioctl_version_show(struct device *dev,
  1965. struct device_attribute *attr, char *buf)
  1966. {
  1967. return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0);
  1968. }
  1969. /**
  1970. * cxlflash_show_port_lun_table() - queries and presents the port LUN table
  1971. * @port: Desired port for status reporting.
  1972. * @afu: AFU owning the specified port.
  1973. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1974. *
  1975. * Return: The size of the ASCII string returned in @buf.
  1976. */
  1977. static ssize_t cxlflash_show_port_lun_table(u32 port,
  1978. struct afu *afu,
  1979. char *buf)
  1980. {
  1981. int i;
  1982. ssize_t bytes = 0;
  1983. __be64 __iomem *fc_port;
  1984. if (port >= NUM_FC_PORTS)
  1985. return 0;
  1986. fc_port = &afu->afu_map->global.fc_port[port][0];
  1987. for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
  1988. bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
  1989. "%03d: %016llX\n", i, readq_be(&fc_port[i]));
  1990. return bytes;
  1991. }
  1992. /**
  1993. * port0_lun_table_show() - presents the current LUN table of port 0
  1994. * @dev: Generic device associated with the host owning the port.
  1995. * @attr: Device attribute representing the port.
  1996. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  1997. *
  1998. * Return: The size of the ASCII string returned in @buf.
  1999. */
  2000. static ssize_t port0_lun_table_show(struct device *dev,
  2001. struct device_attribute *attr,
  2002. char *buf)
  2003. {
  2004. struct Scsi_Host *shost = class_to_shost(dev);
  2005. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  2006. struct afu *afu = cfg->afu;
  2007. return cxlflash_show_port_lun_table(0, afu, buf);
  2008. }
  2009. /**
  2010. * port1_lun_table_show() - presents the current LUN table of port 1
  2011. * @dev: Generic device associated with the host owning the port.
  2012. * @attr: Device attribute representing the port.
  2013. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2014. *
  2015. * Return: The size of the ASCII string returned in @buf.
  2016. */
  2017. static ssize_t port1_lun_table_show(struct device *dev,
  2018. struct device_attribute *attr,
  2019. char *buf)
  2020. {
  2021. struct Scsi_Host *shost = class_to_shost(dev);
  2022. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata;
  2023. struct afu *afu = cfg->afu;
  2024. return cxlflash_show_port_lun_table(1, afu, buf);
  2025. }
  2026. /**
  2027. * mode_show() - presents the current mode of the device
  2028. * @dev: Generic device associated with the device.
  2029. * @attr: Device attribute representing the device mode.
  2030. * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
  2031. *
  2032. * Return: The size of the ASCII string returned in @buf.
  2033. */
  2034. static ssize_t mode_show(struct device *dev,
  2035. struct device_attribute *attr, char *buf)
  2036. {
  2037. struct scsi_device *sdev = to_scsi_device(dev);
  2038. return scnprintf(buf, PAGE_SIZE, "%s\n",
  2039. sdev->hostdata ? "superpipe" : "legacy");
  2040. }
  2041. /*
  2042. * Host attributes
  2043. */
  2044. static DEVICE_ATTR_RO(port0);
  2045. static DEVICE_ATTR_RO(port1);
  2046. static DEVICE_ATTR_RW(lun_mode);
  2047. static DEVICE_ATTR_RO(ioctl_version);
  2048. static DEVICE_ATTR_RO(port0_lun_table);
  2049. static DEVICE_ATTR_RO(port1_lun_table);
  2050. static struct device_attribute *cxlflash_host_attrs[] = {
  2051. &dev_attr_port0,
  2052. &dev_attr_port1,
  2053. &dev_attr_lun_mode,
  2054. &dev_attr_ioctl_version,
  2055. &dev_attr_port0_lun_table,
  2056. &dev_attr_port1_lun_table,
  2057. NULL
  2058. };
  2059. /*
  2060. * Device attributes
  2061. */
  2062. static DEVICE_ATTR_RO(mode);
  2063. static struct device_attribute *cxlflash_dev_attrs[] = {
  2064. &dev_attr_mode,
  2065. NULL
  2066. };
  2067. /*
  2068. * Host template
  2069. */
  2070. static struct scsi_host_template driver_template = {
  2071. .module = THIS_MODULE,
  2072. .name = CXLFLASH_ADAPTER_NAME,
  2073. .info = cxlflash_driver_info,
  2074. .ioctl = cxlflash_ioctl,
  2075. .proc_name = CXLFLASH_NAME,
  2076. .queuecommand = cxlflash_queuecommand,
  2077. .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
  2078. .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
  2079. .change_queue_depth = cxlflash_change_queue_depth,
  2080. .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
  2081. .can_queue = CXLFLASH_MAX_CMDS,
  2082. .this_id = -1,
  2083. .sg_tablesize = SG_NONE, /* No scatter gather support */
  2084. .max_sectors = CXLFLASH_MAX_SECTORS,
  2085. .use_clustering = ENABLE_CLUSTERING,
  2086. .shost_attrs = cxlflash_host_attrs,
  2087. .sdev_attrs = cxlflash_dev_attrs,
  2088. };
  2089. /*
  2090. * Device dependent values
  2091. */
  2092. static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
  2093. 0ULL };
  2094. static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
  2095. CXLFLASH_NOTIFY_SHUTDOWN };
  2096. /*
  2097. * PCI device binding table
  2098. */
  2099. static struct pci_device_id cxlflash_pci_table[] = {
  2100. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
  2101. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
  2102. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
  2103. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
  2104. {}
  2105. };
  2106. MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
  2107. /**
  2108. * cxlflash_worker_thread() - work thread handler for the AFU
  2109. * @work: Work structure contained within cxlflash associated with host.
  2110. *
  2111. * Handles the following events:
  2112. * - Link reset which cannot be performed on interrupt context due to
  2113. * blocking up to a few seconds
  2114. * - Read AFU command room
  2115. * - Rescan the host
  2116. */
  2117. static void cxlflash_worker_thread(struct work_struct *work)
  2118. {
  2119. struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
  2120. work_q);
  2121. struct afu *afu = cfg->afu;
  2122. struct device *dev = &cfg->dev->dev;
  2123. int port;
  2124. ulong lock_flags;
  2125. /* Avoid MMIO if the device has failed */
  2126. if (cfg->state != STATE_NORMAL)
  2127. return;
  2128. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2129. if (cfg->lr_state == LINK_RESET_REQUIRED) {
  2130. port = cfg->lr_port;
  2131. if (port < 0)
  2132. dev_err(dev, "%s: invalid port index %d\n",
  2133. __func__, port);
  2134. else {
  2135. spin_unlock_irqrestore(cfg->host->host_lock,
  2136. lock_flags);
  2137. /* The reset can block... */
  2138. afu_link_reset(afu, port,
  2139. &afu->afu_map->global.fc_regs[port][0]);
  2140. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2141. }
  2142. cfg->lr_state = LINK_RESET_COMPLETE;
  2143. }
  2144. if (afu->read_room) {
  2145. atomic64_set(&afu->room, readq_be(&afu->host_map->cmd_room));
  2146. afu->read_room = false;
  2147. }
  2148. spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
  2149. if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
  2150. scsi_scan_host(cfg->host);
  2151. kref_put(&afu->mapcount, afu_unmap);
  2152. }
  2153. /**
  2154. * cxlflash_probe() - PCI entry point to add host
  2155. * @pdev: PCI device associated with the host.
  2156. * @dev_id: PCI device id associated with device.
  2157. *
  2158. * Return: 0 on success, -errno on failure
  2159. */
  2160. static int cxlflash_probe(struct pci_dev *pdev,
  2161. const struct pci_device_id *dev_id)
  2162. {
  2163. struct Scsi_Host *host;
  2164. struct cxlflash_cfg *cfg = NULL;
  2165. struct dev_dependent_vals *ddv;
  2166. int rc = 0;
  2167. dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
  2168. __func__, pdev->irq);
  2169. ddv = (struct dev_dependent_vals *)dev_id->driver_data;
  2170. driver_template.max_sectors = ddv->max_sectors;
  2171. host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
  2172. if (!host) {
  2173. dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n",
  2174. __func__);
  2175. rc = -ENOMEM;
  2176. goto out;
  2177. }
  2178. host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
  2179. host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
  2180. host->max_channel = NUM_FC_PORTS - 1;
  2181. host->unique_id = host->host_no;
  2182. host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
  2183. cfg = (struct cxlflash_cfg *)host->hostdata;
  2184. cfg->host = host;
  2185. rc = alloc_mem(cfg);
  2186. if (rc) {
  2187. dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n",
  2188. __func__);
  2189. rc = -ENOMEM;
  2190. scsi_host_put(cfg->host);
  2191. goto out;
  2192. }
  2193. cfg->init_state = INIT_STATE_NONE;
  2194. cfg->dev = pdev;
  2195. cfg->cxl_fops = cxlflash_cxl_fops;
  2196. /*
  2197. * The promoted LUNs move to the top of the LUN table. The rest stay
  2198. * on the bottom half. The bottom half grows from the end
  2199. * (index = 255), whereas the top half grows from the beginning
  2200. * (index = 0).
  2201. */
  2202. cfg->promote_lun_index = 0;
  2203. cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1;
  2204. cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1;
  2205. cfg->dev_id = (struct pci_device_id *)dev_id;
  2206. init_waitqueue_head(&cfg->tmf_waitq);
  2207. init_waitqueue_head(&cfg->reset_waitq);
  2208. INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
  2209. cfg->lr_state = LINK_RESET_INVALID;
  2210. cfg->lr_port = -1;
  2211. spin_lock_init(&cfg->tmf_slock);
  2212. mutex_init(&cfg->ctx_tbl_list_mutex);
  2213. mutex_init(&cfg->ctx_recovery_mutex);
  2214. init_rwsem(&cfg->ioctl_rwsem);
  2215. INIT_LIST_HEAD(&cfg->ctx_err_recovery);
  2216. INIT_LIST_HEAD(&cfg->lluns);
  2217. pci_set_drvdata(pdev, cfg);
  2218. cfg->cxl_afu = cxl_pci_to_afu(pdev);
  2219. rc = init_pci(cfg);
  2220. if (rc) {
  2221. dev_err(&pdev->dev, "%s: call to init_pci "
  2222. "failed rc=%d!\n", __func__, rc);
  2223. goto out_remove;
  2224. }
  2225. cfg->init_state = INIT_STATE_PCI;
  2226. rc = init_afu(cfg);
  2227. if (rc) {
  2228. dev_err(&pdev->dev, "%s: call to init_afu "
  2229. "failed rc=%d!\n", __func__, rc);
  2230. goto out_remove;
  2231. }
  2232. cfg->init_state = INIT_STATE_AFU;
  2233. rc = init_scsi(cfg);
  2234. if (rc) {
  2235. dev_err(&pdev->dev, "%s: call to init_scsi "
  2236. "failed rc=%d!\n", __func__, rc);
  2237. goto out_remove;
  2238. }
  2239. cfg->init_state = INIT_STATE_SCSI;
  2240. out:
  2241. pr_debug("%s: returning rc=%d\n", __func__, rc);
  2242. return rc;
  2243. out_remove:
  2244. cxlflash_remove(pdev);
  2245. goto out;
  2246. }
  2247. /**
  2248. * cxlflash_pci_error_detected() - called when a PCI error is detected
  2249. * @pdev: PCI device struct.
  2250. * @state: PCI channel state.
  2251. *
  2252. * When an EEH occurs during an active reset, wait until the reset is
  2253. * complete and then take action based upon the device state.
  2254. *
  2255. * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  2256. */
  2257. static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
  2258. pci_channel_state_t state)
  2259. {
  2260. int rc = 0;
  2261. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2262. struct device *dev = &cfg->dev->dev;
  2263. dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
  2264. switch (state) {
  2265. case pci_channel_io_frozen:
  2266. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  2267. if (cfg->state == STATE_FAILTERM)
  2268. return PCI_ERS_RESULT_DISCONNECT;
  2269. cfg->state = STATE_RESET;
  2270. scsi_block_requests(cfg->host);
  2271. drain_ioctls(cfg);
  2272. rc = cxlflash_mark_contexts_error(cfg);
  2273. if (unlikely(rc))
  2274. dev_err(dev, "%s: Failed to mark user contexts!(%d)\n",
  2275. __func__, rc);
  2276. term_afu(cfg);
  2277. return PCI_ERS_RESULT_NEED_RESET;
  2278. case pci_channel_io_perm_failure:
  2279. cfg->state = STATE_FAILTERM;
  2280. wake_up_all(&cfg->reset_waitq);
  2281. scsi_unblock_requests(cfg->host);
  2282. return PCI_ERS_RESULT_DISCONNECT;
  2283. default:
  2284. break;
  2285. }
  2286. return PCI_ERS_RESULT_NEED_RESET;
  2287. }
  2288. /**
  2289. * cxlflash_pci_slot_reset() - called when PCI slot has been reset
  2290. * @pdev: PCI device struct.
  2291. *
  2292. * This routine is called by the pci error recovery code after the PCI
  2293. * slot has been reset, just before we should resume normal operations.
  2294. *
  2295. * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
  2296. */
  2297. static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
  2298. {
  2299. int rc = 0;
  2300. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2301. struct device *dev = &cfg->dev->dev;
  2302. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  2303. rc = init_afu(cfg);
  2304. if (unlikely(rc)) {
  2305. dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc);
  2306. return PCI_ERS_RESULT_DISCONNECT;
  2307. }
  2308. return PCI_ERS_RESULT_RECOVERED;
  2309. }
  2310. /**
  2311. * cxlflash_pci_resume() - called when normal operation can resume
  2312. * @pdev: PCI device struct
  2313. */
  2314. static void cxlflash_pci_resume(struct pci_dev *pdev)
  2315. {
  2316. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  2317. struct device *dev = &cfg->dev->dev;
  2318. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  2319. cfg->state = STATE_NORMAL;
  2320. wake_up_all(&cfg->reset_waitq);
  2321. scsi_unblock_requests(cfg->host);
  2322. }
  2323. static const struct pci_error_handlers cxlflash_err_handler = {
  2324. .error_detected = cxlflash_pci_error_detected,
  2325. .slot_reset = cxlflash_pci_slot_reset,
  2326. .resume = cxlflash_pci_resume,
  2327. };
  2328. /*
  2329. * PCI device structure
  2330. */
  2331. static struct pci_driver cxlflash_driver = {
  2332. .name = CXLFLASH_NAME,
  2333. .id_table = cxlflash_pci_table,
  2334. .probe = cxlflash_probe,
  2335. .remove = cxlflash_remove,
  2336. .shutdown = cxlflash_remove,
  2337. .err_handler = &cxlflash_err_handler,
  2338. };
  2339. /**
  2340. * init_cxlflash() - module entry point
  2341. *
  2342. * Return: 0 on success, -errno on failure
  2343. */
  2344. static int __init init_cxlflash(void)
  2345. {
  2346. pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME);
  2347. cxlflash_list_init();
  2348. return pci_register_driver(&cxlflash_driver);
  2349. }
  2350. /**
  2351. * exit_cxlflash() - module exit point
  2352. */
  2353. static void __exit exit_cxlflash(void)
  2354. {
  2355. cxlflash_term_global_luns();
  2356. cxlflash_free_errpage();
  2357. pci_unregister_driver(&cxlflash_driver);
  2358. }
  2359. module_init(init_cxlflash);
  2360. module_exit(exit_cxlflash);