main.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CXL Flash Device Driver
  4. *
  5. * Written by: Manoj N. Kumar <[email protected]>, IBM Corporation
  6. * Matthew R. Ochs <[email protected]>, IBM Corporation
  7. *
  8. * Copyright (C) 2015 IBM Corporation
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <asm/unaligned.h>
  15. #include <scsi/scsi_cmnd.h>
  16. #include <scsi/scsi_host.h>
  17. #include <uapi/scsi/cxlflash_ioctl.h>
  18. #include "main.h"
  19. #include "sislite.h"
  20. #include "common.h"
  21. MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
  22. MODULE_AUTHOR("Manoj N. Kumar <[email protected]>");
  23. MODULE_AUTHOR("Matthew R. Ochs <[email protected]>");
  24. MODULE_LICENSE("GPL");
  25. static struct class *cxlflash_class;
  26. static u32 cxlflash_major;
  27. static DECLARE_BITMAP(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
  28. /**
  29. * process_cmd_err() - command error handler
  30. * @cmd: AFU command that experienced the error.
  31. * @scp: SCSI command associated with the AFU command in error.
  32. *
  33. * Translates error bits from AFU command to SCSI command results.
  34. */
  35. static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
  36. {
  37. struct afu *afu = cmd->parent;
  38. struct cxlflash_cfg *cfg = afu->parent;
  39. struct device *dev = &cfg->dev->dev;
  40. struct sisl_ioasa *ioasa;
  41. u32 resid;
  42. ioasa = &(cmd->sa);
  43. if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
  44. resid = ioasa->resid;
  45. scsi_set_resid(scp, resid);
  46. dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
  47. __func__, cmd, scp, resid);
  48. }
  49. if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
  50. dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
  51. __func__, cmd, scp);
  52. scp->result = (DID_ERROR << 16);
  53. }
  54. dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
  55. "afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
  56. ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
  57. ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
  58. if (ioasa->rc.scsi_rc) {
  59. /* We have a SCSI status */
  60. if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
  61. memcpy(scp->sense_buffer, ioasa->sense_data,
  62. SISL_SENSE_DATA_LEN);
  63. scp->result = ioasa->rc.scsi_rc;
  64. } else
  65. scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
  66. }
  67. /*
  68. * We encountered an error. Set scp->result based on nature
  69. * of error.
  70. */
  71. if (ioasa->rc.fc_rc) {
  72. /* We have an FC status */
  73. switch (ioasa->rc.fc_rc) {
  74. case SISL_FC_RC_LINKDOWN:
  75. scp->result = (DID_REQUEUE << 16);
  76. break;
  77. case SISL_FC_RC_RESID:
  78. /* This indicates an FCP resid underrun */
  79. if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
  80. /* If the SISL_RC_FLAGS_OVERRUN flag was set,
  81. * then we will handle this error else where.
  82. * If not then we must handle it here.
  83. * This is probably an AFU bug.
  84. */
  85. scp->result = (DID_ERROR << 16);
  86. }
  87. break;
  88. case SISL_FC_RC_RESIDERR:
  89. /* Resid mismatch between adapter and device */
  90. case SISL_FC_RC_TGTABORT:
  91. case SISL_FC_RC_ABORTOK:
  92. case SISL_FC_RC_ABORTFAIL:
  93. case SISL_FC_RC_NOLOGI:
  94. case SISL_FC_RC_ABORTPEND:
  95. case SISL_FC_RC_WRABORTPEND:
  96. case SISL_FC_RC_NOEXP:
  97. case SISL_FC_RC_INUSE:
  98. scp->result = (DID_ERROR << 16);
  99. break;
  100. }
  101. }
  102. if (ioasa->rc.afu_rc) {
  103. /* We have an AFU error */
  104. switch (ioasa->rc.afu_rc) {
  105. case SISL_AFU_RC_NO_CHANNELS:
  106. scp->result = (DID_NO_CONNECT << 16);
  107. break;
  108. case SISL_AFU_RC_DATA_DMA_ERR:
  109. switch (ioasa->afu_extra) {
  110. case SISL_AFU_DMA_ERR_PAGE_IN:
  111. /* Retry */
  112. scp->result = (DID_IMM_RETRY << 16);
  113. break;
  114. case SISL_AFU_DMA_ERR_INVALID_EA:
  115. default:
  116. scp->result = (DID_ERROR << 16);
  117. }
  118. break;
  119. case SISL_AFU_RC_OUT_OF_DATA_BUFS:
  120. /* Retry */
  121. scp->result = (DID_ERROR << 16);
  122. break;
  123. default:
  124. scp->result = (DID_ERROR << 16);
  125. }
  126. }
  127. }
  128. /**
  129. * cmd_complete() - command completion handler
  130. * @cmd: AFU command that has completed.
  131. *
  132. * For SCSI commands this routine prepares and submits commands that have
  133. * either completed or timed out to the SCSI stack. For internal commands
  134. * (TMF or AFU), this routine simply notifies the originator that the
  135. * command has completed.
  136. */
  137. static void cmd_complete(struct afu_cmd *cmd)
  138. {
  139. struct scsi_cmnd *scp;
  140. ulong lock_flags;
  141. struct afu *afu = cmd->parent;
  142. struct cxlflash_cfg *cfg = afu->parent;
  143. struct device *dev = &cfg->dev->dev;
  144. struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
  145. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  146. list_del(&cmd->list);
  147. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  148. if (cmd->scp) {
  149. scp = cmd->scp;
  150. if (unlikely(cmd->sa.ioasc))
  151. process_cmd_err(cmd, scp);
  152. else
  153. scp->result = (DID_OK << 16);
  154. dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
  155. __func__, scp, scp->result, cmd->sa.ioasc);
  156. scsi_done(scp);
  157. } else if (cmd->cmd_tmf) {
  158. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  159. cfg->tmf_active = false;
  160. wake_up_all_locked(&cfg->tmf_waitq);
  161. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  162. } else
  163. complete(&cmd->cevent);
  164. }
  165. /**
  166. * flush_pending_cmds() - flush all pending commands on this hardware queue
  167. * @hwq: Hardware queue to flush.
  168. *
  169. * The hardware send queue lock associated with this hardware queue must be
  170. * held when calling this routine.
  171. */
  172. static void flush_pending_cmds(struct hwq *hwq)
  173. {
  174. struct cxlflash_cfg *cfg = hwq->afu->parent;
  175. struct afu_cmd *cmd, *tmp;
  176. struct scsi_cmnd *scp;
  177. ulong lock_flags;
  178. list_for_each_entry_safe(cmd, tmp, &hwq->pending_cmds, list) {
  179. /* Bypass command when on a doneq, cmd_complete() will handle */
  180. if (!list_empty(&cmd->queue))
  181. continue;
  182. list_del(&cmd->list);
  183. if (cmd->scp) {
  184. scp = cmd->scp;
  185. scp->result = (DID_IMM_RETRY << 16);
  186. scsi_done(scp);
  187. } else {
  188. cmd->cmd_aborted = true;
  189. if (cmd->cmd_tmf) {
  190. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  191. cfg->tmf_active = false;
  192. wake_up_all_locked(&cfg->tmf_waitq);
  193. spin_unlock_irqrestore(&cfg->tmf_slock,
  194. lock_flags);
  195. } else
  196. complete(&cmd->cevent);
  197. }
  198. }
  199. }
  200. /**
  201. * context_reset() - reset context via specified register
  202. * @hwq: Hardware queue owning the context to be reset.
  203. * @reset_reg: MMIO register to perform reset.
  204. *
  205. * When the reset is successful, the SISLite specification guarantees that
  206. * the AFU has aborted all currently pending I/O. Accordingly, these commands
  207. * must be flushed.
  208. *
  209. * Return: 0 on success, -errno on failure
  210. */
  211. static int context_reset(struct hwq *hwq, __be64 __iomem *reset_reg)
  212. {
  213. struct cxlflash_cfg *cfg = hwq->afu->parent;
  214. struct device *dev = &cfg->dev->dev;
  215. int rc = -ETIMEDOUT;
  216. int nretry = 0;
  217. u64 val = 0x1;
  218. ulong lock_flags;
  219. dev_dbg(dev, "%s: hwq=%p\n", __func__, hwq);
  220. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  221. writeq_be(val, reset_reg);
  222. do {
  223. val = readq_be(reset_reg);
  224. if ((val & 0x1) == 0x0) {
  225. rc = 0;
  226. break;
  227. }
  228. /* Double delay each time */
  229. udelay(1 << nretry);
  230. } while (nretry++ < MC_ROOM_RETRY_CNT);
  231. if (!rc)
  232. flush_pending_cmds(hwq);
  233. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  234. dev_dbg(dev, "%s: returning rc=%d, val=%016llx nretry=%d\n",
  235. __func__, rc, val, nretry);
  236. return rc;
  237. }
  238. /**
  239. * context_reset_ioarrin() - reset context via IOARRIN register
  240. * @hwq: Hardware queue owning the context to be reset.
  241. *
  242. * Return: 0 on success, -errno on failure
  243. */
  244. static int context_reset_ioarrin(struct hwq *hwq)
  245. {
  246. return context_reset(hwq, &hwq->host_map->ioarrin);
  247. }
  248. /**
  249. * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
  250. * @hwq: Hardware queue owning the context to be reset.
  251. *
  252. * Return: 0 on success, -errno on failure
  253. */
  254. static int context_reset_sq(struct hwq *hwq)
  255. {
  256. return context_reset(hwq, &hwq->host_map->sq_ctx_reset);
  257. }
  258. /**
  259. * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
  260. * @afu: AFU associated with the host.
  261. * @cmd: AFU command to send.
  262. *
  263. * Return:
  264. * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  265. */
  266. static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
  267. {
  268. struct cxlflash_cfg *cfg = afu->parent;
  269. struct device *dev = &cfg->dev->dev;
  270. struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
  271. int rc = 0;
  272. s64 room;
  273. ulong lock_flags;
  274. /*
  275. * To avoid the performance penalty of MMIO, spread the update of
  276. * 'room' over multiple commands.
  277. */
  278. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  279. if (--hwq->room < 0) {
  280. room = readq_be(&hwq->host_map->cmd_room);
  281. if (room <= 0) {
  282. dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
  283. "0x%02X, room=0x%016llX\n",
  284. __func__, cmd->rcb.cdb[0], room);
  285. hwq->room = 0;
  286. rc = SCSI_MLQUEUE_HOST_BUSY;
  287. goto out;
  288. }
  289. hwq->room = room - 1;
  290. }
  291. list_add(&cmd->list, &hwq->pending_cmds);
  292. writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
  293. out:
  294. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  295. dev_dbg_ratelimited(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n",
  296. __func__, cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
  297. return rc;
  298. }
  299. /**
  300. * send_cmd_sq() - sends an AFU command via SQ ring
  301. * @afu: AFU associated with the host.
  302. * @cmd: AFU command to send.
  303. *
  304. * Return:
  305. * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  306. */
  307. static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
  308. {
  309. struct cxlflash_cfg *cfg = afu->parent;
  310. struct device *dev = &cfg->dev->dev;
  311. struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
  312. int rc = 0;
  313. int newval;
  314. ulong lock_flags;
  315. newval = atomic_dec_if_positive(&hwq->hsq_credits);
  316. if (newval <= 0) {
  317. rc = SCSI_MLQUEUE_HOST_BUSY;
  318. goto out;
  319. }
  320. cmd->rcb.ioasa = &cmd->sa;
  321. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  322. *hwq->hsq_curr = cmd->rcb;
  323. if (hwq->hsq_curr < hwq->hsq_end)
  324. hwq->hsq_curr++;
  325. else
  326. hwq->hsq_curr = hwq->hsq_start;
  327. list_add(&cmd->list, &hwq->pending_cmds);
  328. writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
  329. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  330. out:
  331. dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
  332. "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
  333. cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
  334. readq_be(&hwq->host_map->sq_head),
  335. readq_be(&hwq->host_map->sq_tail));
  336. return rc;
  337. }
  338. /**
  339. * wait_resp() - polls for a response or timeout to a sent AFU command
  340. * @afu: AFU associated with the host.
  341. * @cmd: AFU command that was sent.
  342. *
  343. * Return: 0 on success, -errno on failure
  344. */
  345. static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
  346. {
  347. struct cxlflash_cfg *cfg = afu->parent;
  348. struct device *dev = &cfg->dev->dev;
  349. int rc = 0;
  350. ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
  351. timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
  352. if (!timeout)
  353. rc = -ETIMEDOUT;
  354. if (cmd->cmd_aborted)
  355. rc = -EAGAIN;
  356. if (unlikely(cmd->sa.ioasc != 0)) {
  357. dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
  358. __func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
  359. rc = -EIO;
  360. }
  361. return rc;
  362. }
  363. /**
  364. * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
  365. * @host: SCSI host associated with device.
  366. * @scp: SCSI command to send.
  367. * @afu: SCSI command to send.
  368. *
  369. * Hashes a command based upon the hardware queue mode.
  370. *
  371. * Return: Trusted index of target hardware queue
  372. */
  373. static u32 cmd_to_target_hwq(struct Scsi_Host *host, struct scsi_cmnd *scp,
  374. struct afu *afu)
  375. {
  376. u32 tag;
  377. u32 hwq = 0;
  378. if (afu->num_hwqs == 1)
  379. return 0;
  380. switch (afu->hwq_mode) {
  381. case HWQ_MODE_RR:
  382. hwq = afu->hwq_rr_count++ % afu->num_hwqs;
  383. break;
  384. case HWQ_MODE_TAG:
  385. tag = blk_mq_unique_tag(scsi_cmd_to_rq(scp));
  386. hwq = blk_mq_unique_tag_to_hwq(tag);
  387. break;
  388. case HWQ_MODE_CPU:
  389. hwq = smp_processor_id() % afu->num_hwqs;
  390. break;
  391. default:
  392. WARN_ON_ONCE(1);
  393. }
  394. return hwq;
  395. }
  396. /**
  397. * send_tmf() - sends a Task Management Function (TMF)
  398. * @cfg: Internal structure associated with the host.
  399. * @sdev: SCSI device destined for TMF.
  400. * @tmfcmd: TMF command to send.
  401. *
  402. * Return:
  403. * 0 on success, SCSI_MLQUEUE_HOST_BUSY or -errno on failure
  404. */
  405. static int send_tmf(struct cxlflash_cfg *cfg, struct scsi_device *sdev,
  406. u64 tmfcmd)
  407. {
  408. struct afu *afu = cfg->afu;
  409. struct afu_cmd *cmd = NULL;
  410. struct device *dev = &cfg->dev->dev;
  411. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  412. bool needs_deletion = false;
  413. char *buf = NULL;
  414. ulong lock_flags;
  415. int rc = 0;
  416. ulong to;
  417. buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
  418. if (unlikely(!buf)) {
  419. dev_err(dev, "%s: no memory for command\n", __func__);
  420. rc = -ENOMEM;
  421. goto out;
  422. }
  423. cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
  424. INIT_LIST_HEAD(&cmd->queue);
  425. /* When Task Management Function is active do not send another */
  426. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  427. if (cfg->tmf_active)
  428. wait_event_interruptible_lock_irq(cfg->tmf_waitq,
  429. !cfg->tmf_active,
  430. cfg->tmf_slock);
  431. cfg->tmf_active = true;
  432. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  433. cmd->parent = afu;
  434. cmd->cmd_tmf = true;
  435. cmd->hwq_index = hwq->index;
  436. cmd->rcb.ctx_id = hwq->ctx_hndl;
  437. cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
  438. cmd->rcb.port_sel = CHAN2PORTMASK(sdev->channel);
  439. cmd->rcb.lun_id = lun_to_lunid(sdev->lun);
  440. cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
  441. SISL_REQ_FLAGS_SUP_UNDERRUN |
  442. SISL_REQ_FLAGS_TMF_CMD);
  443. memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
  444. rc = afu->send_cmd(afu, cmd);
  445. if (unlikely(rc)) {
  446. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  447. cfg->tmf_active = false;
  448. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  449. goto out;
  450. }
  451. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  452. to = msecs_to_jiffies(5000);
  453. to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
  454. !cfg->tmf_active,
  455. cfg->tmf_slock,
  456. to);
  457. if (!to) {
  458. dev_err(dev, "%s: TMF timed out\n", __func__);
  459. rc = -ETIMEDOUT;
  460. needs_deletion = true;
  461. } else if (cmd->cmd_aborted) {
  462. dev_err(dev, "%s: TMF aborted\n", __func__);
  463. rc = -EAGAIN;
  464. } else if (cmd->sa.ioasc) {
  465. dev_err(dev, "%s: TMF failed ioasc=%08x\n",
  466. __func__, cmd->sa.ioasc);
  467. rc = -EIO;
  468. }
  469. cfg->tmf_active = false;
  470. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  471. if (needs_deletion) {
  472. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  473. list_del(&cmd->list);
  474. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  475. }
  476. out:
  477. kfree(buf);
  478. return rc;
  479. }
  480. /**
  481. * cxlflash_driver_info() - information handler for this host driver
  482. * @host: SCSI host associated with device.
  483. *
  484. * Return: A string describing the device.
  485. */
  486. static const char *cxlflash_driver_info(struct Scsi_Host *host)
  487. {
  488. return CXLFLASH_ADAPTER_NAME;
  489. }
  490. /**
  491. * cxlflash_queuecommand() - sends a mid-layer request
  492. * @host: SCSI host associated with device.
  493. * @scp: SCSI command to send.
  494. *
  495. * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
  496. */
  497. static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
  498. {
  499. struct cxlflash_cfg *cfg = shost_priv(host);
  500. struct afu *afu = cfg->afu;
  501. struct device *dev = &cfg->dev->dev;
  502. struct afu_cmd *cmd = sc_to_afuci(scp);
  503. struct scatterlist *sg = scsi_sglist(scp);
  504. int hwq_index = cmd_to_target_hwq(host, scp, afu);
  505. struct hwq *hwq = get_hwq(afu, hwq_index);
  506. u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
  507. ulong lock_flags;
  508. int rc = 0;
  509. dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
  510. "cdb=(%08x-%08x-%08x-%08x)\n",
  511. __func__, scp, host->host_no, scp->device->channel,
  512. scp->device->id, scp->device->lun,
  513. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  514. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  515. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  516. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  517. /*
  518. * If a Task Management Function is active, wait for it to complete
  519. * before continuing with regular commands.
  520. */
  521. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  522. if (cfg->tmf_active) {
  523. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  524. rc = SCSI_MLQUEUE_HOST_BUSY;
  525. goto out;
  526. }
  527. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  528. switch (cfg->state) {
  529. case STATE_PROBING:
  530. case STATE_PROBED:
  531. case STATE_RESET:
  532. dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
  533. rc = SCSI_MLQUEUE_HOST_BUSY;
  534. goto out;
  535. case STATE_FAILTERM:
  536. dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
  537. scp->result = (DID_NO_CONNECT << 16);
  538. scsi_done(scp);
  539. rc = 0;
  540. goto out;
  541. default:
  542. atomic_inc(&afu->cmds_active);
  543. break;
  544. }
  545. if (likely(sg)) {
  546. cmd->rcb.data_len = sg->length;
  547. cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
  548. }
  549. cmd->scp = scp;
  550. cmd->parent = afu;
  551. cmd->hwq_index = hwq_index;
  552. cmd->sa.ioasc = 0;
  553. cmd->rcb.ctx_id = hwq->ctx_hndl;
  554. cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
  555. cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
  556. cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
  557. if (scp->sc_data_direction == DMA_TO_DEVICE)
  558. req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
  559. cmd->rcb.req_flags = req_flags;
  560. memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
  561. rc = afu->send_cmd(afu, cmd);
  562. atomic_dec(&afu->cmds_active);
  563. out:
  564. return rc;
  565. }
  566. /**
  567. * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
  568. * @cfg: Internal structure associated with the host.
  569. */
  570. static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
  571. {
  572. struct pci_dev *pdev = cfg->dev;
  573. if (pci_channel_offline(pdev))
  574. wait_event_timeout(cfg->reset_waitq,
  575. !pci_channel_offline(pdev),
  576. CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
  577. }
  578. /**
  579. * free_mem() - free memory associated with the AFU
  580. * @cfg: Internal structure associated with the host.
  581. */
  582. static void free_mem(struct cxlflash_cfg *cfg)
  583. {
  584. struct afu *afu = cfg->afu;
  585. if (cfg->afu) {
  586. free_pages((ulong)afu, get_order(sizeof(struct afu)));
  587. cfg->afu = NULL;
  588. }
  589. }
  590. /**
  591. * cxlflash_reset_sync() - synchronizing point for asynchronous resets
  592. * @cfg: Internal structure associated with the host.
  593. */
  594. static void cxlflash_reset_sync(struct cxlflash_cfg *cfg)
  595. {
  596. if (cfg->async_reset_cookie == 0)
  597. return;
  598. /* Wait until all async calls prior to this cookie have completed */
  599. async_synchronize_cookie(cfg->async_reset_cookie + 1);
  600. cfg->async_reset_cookie = 0;
  601. }
  602. /**
  603. * stop_afu() - stops the AFU command timers and unmaps the MMIO space
  604. * @cfg: Internal structure associated with the host.
  605. *
  606. * Safe to call with AFU in a partially allocated/initialized state.
  607. *
  608. * Cancels scheduled worker threads, waits for any active internal AFU
  609. * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
  610. */
  611. static void stop_afu(struct cxlflash_cfg *cfg)
  612. {
  613. struct afu *afu = cfg->afu;
  614. struct hwq *hwq;
  615. int i;
  616. cancel_work_sync(&cfg->work_q);
  617. if (!current_is_async())
  618. cxlflash_reset_sync(cfg);
  619. if (likely(afu)) {
  620. while (atomic_read(&afu->cmds_active))
  621. ssleep(1);
  622. if (afu_is_irqpoll_enabled(afu)) {
  623. for (i = 0; i < afu->num_hwqs; i++) {
  624. hwq = get_hwq(afu, i);
  625. irq_poll_disable(&hwq->irqpoll);
  626. }
  627. }
  628. if (likely(afu->afu_map)) {
  629. cfg->ops->psa_unmap(afu->afu_map);
  630. afu->afu_map = NULL;
  631. }
  632. }
  633. }
  634. /**
  635. * term_intr() - disables all AFU interrupts
  636. * @cfg: Internal structure associated with the host.
  637. * @level: Depth of allocation, where to begin waterfall tear down.
  638. * @index: Index of the hardware queue.
  639. *
  640. * Safe to call with AFU/MC in partially allocated/initialized state.
  641. */
  642. static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
  643. u32 index)
  644. {
  645. struct afu *afu = cfg->afu;
  646. struct device *dev = &cfg->dev->dev;
  647. struct hwq *hwq;
  648. if (!afu) {
  649. dev_err(dev, "%s: returning with NULL afu\n", __func__);
  650. return;
  651. }
  652. hwq = get_hwq(afu, index);
  653. if (!hwq->ctx_cookie) {
  654. dev_err(dev, "%s: returning with NULL MC\n", __func__);
  655. return;
  656. }
  657. switch (level) {
  658. case UNMAP_THREE:
  659. /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
  660. if (index == PRIMARY_HWQ)
  661. cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 3, hwq);
  662. fallthrough;
  663. case UNMAP_TWO:
  664. cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 2, hwq);
  665. fallthrough;
  666. case UNMAP_ONE:
  667. cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 1, hwq);
  668. fallthrough;
  669. case FREE_IRQ:
  670. cfg->ops->free_afu_irqs(hwq->ctx_cookie);
  671. fallthrough;
  672. case UNDO_NOOP:
  673. /* No action required */
  674. break;
  675. }
  676. }
  677. /**
  678. * term_mc() - terminates the master context
  679. * @cfg: Internal structure associated with the host.
  680. * @index: Index of the hardware queue.
  681. *
  682. * Safe to call with AFU/MC in partially allocated/initialized state.
  683. */
  684. static void term_mc(struct cxlflash_cfg *cfg, u32 index)
  685. {
  686. struct afu *afu = cfg->afu;
  687. struct device *dev = &cfg->dev->dev;
  688. struct hwq *hwq;
  689. ulong lock_flags;
  690. if (!afu) {
  691. dev_err(dev, "%s: returning with NULL afu\n", __func__);
  692. return;
  693. }
  694. hwq = get_hwq(afu, index);
  695. if (!hwq->ctx_cookie) {
  696. dev_err(dev, "%s: returning with NULL MC\n", __func__);
  697. return;
  698. }
  699. WARN_ON(cfg->ops->stop_context(hwq->ctx_cookie));
  700. if (index != PRIMARY_HWQ)
  701. WARN_ON(cfg->ops->release_context(hwq->ctx_cookie));
  702. hwq->ctx_cookie = NULL;
  703. spin_lock_irqsave(&hwq->hrrq_slock, lock_flags);
  704. hwq->hrrq_online = false;
  705. spin_unlock_irqrestore(&hwq->hrrq_slock, lock_flags);
  706. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  707. flush_pending_cmds(hwq);
  708. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  709. }
  710. /**
  711. * term_afu() - terminates the AFU
  712. * @cfg: Internal structure associated with the host.
  713. *
  714. * Safe to call with AFU/MC in partially allocated/initialized state.
  715. */
  716. static void term_afu(struct cxlflash_cfg *cfg)
  717. {
  718. struct device *dev = &cfg->dev->dev;
  719. int k;
  720. /*
  721. * Tear down is carefully orchestrated to ensure
  722. * no interrupts can come in when the problem state
  723. * area is unmapped.
  724. *
  725. * 1) Disable all AFU interrupts for each master
  726. * 2) Unmap the problem state area
  727. * 3) Stop each master context
  728. */
  729. for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
  730. term_intr(cfg, UNMAP_THREE, k);
  731. stop_afu(cfg);
  732. for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
  733. term_mc(cfg, k);
  734. dev_dbg(dev, "%s: returning\n", __func__);
  735. }
  736. /**
  737. * notify_shutdown() - notifies device of pending shutdown
  738. * @cfg: Internal structure associated with the host.
  739. * @wait: Whether to wait for shutdown processing to complete.
  740. *
  741. * This function will notify the AFU that the adapter is being shutdown
  742. * and will wait for shutdown processing to complete if wait is true.
  743. * This notification should flush pending I/Os to the device and halt
  744. * further I/Os until the next AFU reset is issued and device restarted.
  745. */
  746. static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
  747. {
  748. struct afu *afu = cfg->afu;
  749. struct device *dev = &cfg->dev->dev;
  750. struct dev_dependent_vals *ddv;
  751. __be64 __iomem *fc_port_regs;
  752. u64 reg, status;
  753. int i, retry_cnt = 0;
  754. ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
  755. if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
  756. return;
  757. if (!afu || !afu->afu_map) {
  758. dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
  759. return;
  760. }
  761. /* Notify AFU */
  762. for (i = 0; i < cfg->num_fc_ports; i++) {
  763. fc_port_regs = get_fc_port_regs(cfg, i);
  764. reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
  765. reg |= SISL_FC_SHUTDOWN_NORMAL;
  766. writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
  767. }
  768. if (!wait)
  769. return;
  770. /* Wait up to 1.5 seconds for shutdown processing to complete */
  771. for (i = 0; i < cfg->num_fc_ports; i++) {
  772. fc_port_regs = get_fc_port_regs(cfg, i);
  773. retry_cnt = 0;
  774. while (true) {
  775. status = readq_be(&fc_port_regs[FC_STATUS / 8]);
  776. if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
  777. break;
  778. if (++retry_cnt >= MC_RETRY_CNT) {
  779. dev_dbg(dev, "%s: port %d shutdown processing "
  780. "not yet completed\n", __func__, i);
  781. break;
  782. }
  783. msleep(100 * retry_cnt);
  784. }
  785. }
  786. }
  787. /**
  788. * cxlflash_get_minor() - gets the first available minor number
  789. *
  790. * Return: Unique minor number that can be used to create the character device.
  791. */
  792. static int cxlflash_get_minor(void)
  793. {
  794. int minor;
  795. long bit;
  796. bit = find_first_zero_bit(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
  797. if (bit >= CXLFLASH_MAX_ADAPTERS)
  798. return -1;
  799. minor = bit & MINORMASK;
  800. set_bit(minor, cxlflash_minor);
  801. return minor;
  802. }
  803. /**
  804. * cxlflash_put_minor() - releases the minor number
  805. * @minor: Minor number that is no longer needed.
  806. */
  807. static void cxlflash_put_minor(int minor)
  808. {
  809. clear_bit(minor, cxlflash_minor);
  810. }
  811. /**
  812. * cxlflash_release_chrdev() - release the character device for the host
  813. * @cfg: Internal structure associated with the host.
  814. */
  815. static void cxlflash_release_chrdev(struct cxlflash_cfg *cfg)
  816. {
  817. device_unregister(cfg->chardev);
  818. cfg->chardev = NULL;
  819. cdev_del(&cfg->cdev);
  820. cxlflash_put_minor(MINOR(cfg->cdev.dev));
  821. }
  822. /**
  823. * cxlflash_remove() - PCI entry point to tear down host
  824. * @pdev: PCI device associated with the host.
  825. *
  826. * Safe to use as a cleanup in partially allocated/initialized state. Note that
  827. * the reset_waitq is flushed as part of the stop/termination of user contexts.
  828. */
  829. static void cxlflash_remove(struct pci_dev *pdev)
  830. {
  831. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  832. struct device *dev = &pdev->dev;
  833. ulong lock_flags;
  834. if (!pci_is_enabled(pdev)) {
  835. dev_dbg(dev, "%s: Device is disabled\n", __func__);
  836. return;
  837. }
  838. /* Yield to running recovery threads before continuing with remove */
  839. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
  840. cfg->state != STATE_PROBING);
  841. spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
  842. if (cfg->tmf_active)
  843. wait_event_interruptible_lock_irq(cfg->tmf_waitq,
  844. !cfg->tmf_active,
  845. cfg->tmf_slock);
  846. spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
  847. /* Notify AFU and wait for shutdown processing to complete */
  848. notify_shutdown(cfg, true);
  849. cfg->state = STATE_FAILTERM;
  850. cxlflash_stop_term_user_contexts(cfg);
  851. switch (cfg->init_state) {
  852. case INIT_STATE_CDEV:
  853. cxlflash_release_chrdev(cfg);
  854. fallthrough;
  855. case INIT_STATE_SCSI:
  856. cxlflash_term_local_luns(cfg);
  857. scsi_remove_host(cfg->host);
  858. fallthrough;
  859. case INIT_STATE_AFU:
  860. term_afu(cfg);
  861. fallthrough;
  862. case INIT_STATE_PCI:
  863. cfg->ops->destroy_afu(cfg->afu_cookie);
  864. pci_disable_device(pdev);
  865. fallthrough;
  866. case INIT_STATE_NONE:
  867. free_mem(cfg);
  868. scsi_host_put(cfg->host);
  869. break;
  870. }
  871. dev_dbg(dev, "%s: returning\n", __func__);
  872. }
  873. /**
  874. * alloc_mem() - allocates the AFU and its command pool
  875. * @cfg: Internal structure associated with the host.
  876. *
  877. * A partially allocated state remains on failure.
  878. *
  879. * Return:
  880. * 0 on success
  881. * -ENOMEM on failure to allocate memory
  882. */
  883. static int alloc_mem(struct cxlflash_cfg *cfg)
  884. {
  885. int rc = 0;
  886. struct device *dev = &cfg->dev->dev;
  887. /* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
  888. cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  889. get_order(sizeof(struct afu)));
  890. if (unlikely(!cfg->afu)) {
  891. dev_err(dev, "%s: cannot get %d free pages\n",
  892. __func__, get_order(sizeof(struct afu)));
  893. rc = -ENOMEM;
  894. goto out;
  895. }
  896. cfg->afu->parent = cfg;
  897. cfg->afu->desired_hwqs = CXLFLASH_DEF_HWQS;
  898. cfg->afu->afu_map = NULL;
  899. out:
  900. return rc;
  901. }
  902. /**
  903. * init_pci() - initializes the host as a PCI device
  904. * @cfg: Internal structure associated with the host.
  905. *
  906. * Return: 0 on success, -errno on failure
  907. */
  908. static int init_pci(struct cxlflash_cfg *cfg)
  909. {
  910. struct pci_dev *pdev = cfg->dev;
  911. struct device *dev = &cfg->dev->dev;
  912. int rc = 0;
  913. rc = pci_enable_device(pdev);
  914. if (rc || pci_channel_offline(pdev)) {
  915. if (pci_channel_offline(pdev)) {
  916. cxlflash_wait_for_pci_err_recovery(cfg);
  917. rc = pci_enable_device(pdev);
  918. }
  919. if (rc) {
  920. dev_err(dev, "%s: Cannot enable adapter\n", __func__);
  921. cxlflash_wait_for_pci_err_recovery(cfg);
  922. goto out;
  923. }
  924. }
  925. out:
  926. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  927. return rc;
  928. }
  929. /**
  930. * init_scsi() - adds the host to the SCSI stack and kicks off host scan
  931. * @cfg: Internal structure associated with the host.
  932. *
  933. * Return: 0 on success, -errno on failure
  934. */
  935. static int init_scsi(struct cxlflash_cfg *cfg)
  936. {
  937. struct pci_dev *pdev = cfg->dev;
  938. struct device *dev = &cfg->dev->dev;
  939. int rc = 0;
  940. rc = scsi_add_host(cfg->host, &pdev->dev);
  941. if (rc) {
  942. dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
  943. goto out;
  944. }
  945. scsi_scan_host(cfg->host);
  946. out:
  947. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  948. return rc;
  949. }
  950. /**
  951. * set_port_online() - transitions the specified host FC port to online state
  952. * @fc_regs: Top of MMIO region defined for specified port.
  953. *
  954. * The provided MMIO region must be mapped prior to call. Online state means
  955. * that the FC link layer has synced, completed the handshaking process, and
  956. * is ready for login to start.
  957. */
  958. static void set_port_online(__be64 __iomem *fc_regs)
  959. {
  960. u64 cmdcfg;
  961. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  962. cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */
  963. cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */
  964. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  965. }
  966. /**
  967. * set_port_offline() - transitions the specified host FC port to offline state
  968. * @fc_regs: Top of MMIO region defined for specified port.
  969. *
  970. * The provided MMIO region must be mapped prior to call.
  971. */
  972. static void set_port_offline(__be64 __iomem *fc_regs)
  973. {
  974. u64 cmdcfg;
  975. cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
  976. cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */
  977. cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */
  978. writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
  979. }
  980. /**
  981. * wait_port_online() - waits for the specified host FC port come online
  982. * @fc_regs: Top of MMIO region defined for specified port.
  983. * @delay_us: Number of microseconds to delay between reading port status.
  984. * @nretry: Number of cycles to retry reading port status.
  985. *
  986. * The provided MMIO region must be mapped prior to call. This will timeout
  987. * when the cable is not plugged in.
  988. *
  989. * Return:
  990. * TRUE (1) when the specified port is online
  991. * FALSE (0) when the specified port fails to come online after timeout
  992. */
  993. static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  994. {
  995. u64 status;
  996. WARN_ON(delay_us < 1000);
  997. do {
  998. msleep(delay_us / 1000);
  999. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  1000. if (status == U64_MAX)
  1001. nretry /= 2;
  1002. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
  1003. nretry--);
  1004. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
  1005. }
  1006. /**
  1007. * wait_port_offline() - waits for the specified host FC port go offline
  1008. * @fc_regs: Top of MMIO region defined for specified port.
  1009. * @delay_us: Number of microseconds to delay between reading port status.
  1010. * @nretry: Number of cycles to retry reading port status.
  1011. *
  1012. * The provided MMIO region must be mapped prior to call.
  1013. *
  1014. * Return:
  1015. * TRUE (1) when the specified port is offline
  1016. * FALSE (0) when the specified port fails to go offline after timeout
  1017. */
  1018. static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
  1019. {
  1020. u64 status;
  1021. WARN_ON(delay_us < 1000);
  1022. do {
  1023. msleep(delay_us / 1000);
  1024. status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
  1025. if (status == U64_MAX)
  1026. nretry /= 2;
  1027. } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
  1028. nretry--);
  1029. return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
  1030. }
  1031. /**
  1032. * afu_set_wwpn() - configures the WWPN for the specified host FC port
  1033. * @afu: AFU associated with the host that owns the specified FC port.
  1034. * @port: Port number being configured.
  1035. * @fc_regs: Top of MMIO region defined for specified port.
  1036. * @wwpn: The world-wide-port-number previously discovered for port.
  1037. *
  1038. * The provided MMIO region must be mapped prior to call. As part of the
  1039. * sequence to configure the WWPN, the port is toggled offline and then back
  1040. * online. This toggling action can cause this routine to delay up to a few
  1041. * seconds. When configured to use the internal LUN feature of the AFU, a
  1042. * failure to come online is overridden.
  1043. */
  1044. static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
  1045. u64 wwpn)
  1046. {
  1047. struct cxlflash_cfg *cfg = afu->parent;
  1048. struct device *dev = &cfg->dev->dev;
  1049. set_port_offline(fc_regs);
  1050. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1051. FC_PORT_STATUS_RETRY_CNT)) {
  1052. dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
  1053. __func__, port);
  1054. }
  1055. writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
  1056. set_port_online(fc_regs);
  1057. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1058. FC_PORT_STATUS_RETRY_CNT)) {
  1059. dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
  1060. __func__, port);
  1061. }
  1062. }
  1063. /**
  1064. * afu_link_reset() - resets the specified host FC port
  1065. * @afu: AFU associated with the host that owns the specified FC port.
  1066. * @port: Port number being configured.
  1067. * @fc_regs: Top of MMIO region defined for specified port.
  1068. *
  1069. * The provided MMIO region must be mapped prior to call. The sequence to
  1070. * reset the port involves toggling it offline and then back online. This
  1071. * action can cause this routine to delay up to a few seconds. An effort
  1072. * is made to maintain link with the device by switching to host to use
  1073. * the alternate port exclusively while the reset takes place.
  1074. * failure to come online is overridden.
  1075. */
  1076. static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
  1077. {
  1078. struct cxlflash_cfg *cfg = afu->parent;
  1079. struct device *dev = &cfg->dev->dev;
  1080. u64 port_sel;
  1081. /* first switch the AFU to the other links, if any */
  1082. port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
  1083. port_sel &= ~(1ULL << port);
  1084. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  1085. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  1086. set_port_offline(fc_regs);
  1087. if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1088. FC_PORT_STATUS_RETRY_CNT))
  1089. dev_err(dev, "%s: wait on port %d to go offline timed out\n",
  1090. __func__, port);
  1091. set_port_online(fc_regs);
  1092. if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
  1093. FC_PORT_STATUS_RETRY_CNT))
  1094. dev_err(dev, "%s: wait on port %d to go online timed out\n",
  1095. __func__, port);
  1096. /* switch back to include this port */
  1097. port_sel |= (1ULL << port);
  1098. writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
  1099. cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
  1100. dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
  1101. }
  1102. /**
  1103. * afu_err_intr_init() - clears and initializes the AFU for error interrupts
  1104. * @afu: AFU associated with the host.
  1105. */
  1106. static void afu_err_intr_init(struct afu *afu)
  1107. {
  1108. struct cxlflash_cfg *cfg = afu->parent;
  1109. __be64 __iomem *fc_port_regs;
  1110. int i;
  1111. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  1112. u64 reg;
  1113. /* global async interrupts: AFU clears afu_ctrl on context exit
  1114. * if async interrupts were sent to that context. This prevents
  1115. * the AFU form sending further async interrupts when
  1116. * there is
  1117. * nobody to receive them.
  1118. */
  1119. /* mask all */
  1120. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
  1121. /* set LISN# to send and point to primary master context */
  1122. reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
  1123. if (afu->internal_lun)
  1124. reg |= 1; /* Bit 63 indicates local lun */
  1125. writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
  1126. /* clear all */
  1127. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1128. /* unmask bits that are of interest */
  1129. /* note: afu can send an interrupt after this step */
  1130. writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
  1131. /* clear again in case a bit came on after previous clear but before */
  1132. /* unmask */
  1133. writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
  1134. /* Clear/Set internal lun bits */
  1135. fc_port_regs = get_fc_port_regs(cfg, 0);
  1136. reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
  1137. reg &= SISL_FC_INTERNAL_MASK;
  1138. if (afu->internal_lun)
  1139. reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
  1140. writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
  1141. /* now clear FC errors */
  1142. for (i = 0; i < cfg->num_fc_ports; i++) {
  1143. fc_port_regs = get_fc_port_regs(cfg, i);
  1144. writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
  1145. writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
  1146. }
  1147. /* sync interrupts for master's IOARRIN write */
  1148. /* note that unlike asyncs, there can be no pending sync interrupts */
  1149. /* at this time (this is a fresh context and master has not written */
  1150. /* IOARRIN yet), so there is nothing to clear. */
  1151. /* set LISN#, it is always sent to the context that wrote IOARRIN */
  1152. for (i = 0; i < afu->num_hwqs; i++) {
  1153. hwq = get_hwq(afu, i);
  1154. reg = readq_be(&hwq->host_map->ctx_ctrl);
  1155. WARN_ON((reg & SISL_CTX_CTRL_LISN_MASK) != 0);
  1156. reg |= SISL_MSI_SYNC_ERROR;
  1157. writeq_be(reg, &hwq->host_map->ctx_ctrl);
  1158. writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
  1159. }
  1160. }
  1161. /**
  1162. * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
  1163. * @irq: Interrupt number.
  1164. * @data: Private data provided at interrupt registration, the AFU.
  1165. *
  1166. * Return: Always return IRQ_HANDLED.
  1167. */
  1168. static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
  1169. {
  1170. struct hwq *hwq = (struct hwq *)data;
  1171. struct cxlflash_cfg *cfg = hwq->afu->parent;
  1172. struct device *dev = &cfg->dev->dev;
  1173. u64 reg;
  1174. u64 reg_unmasked;
  1175. reg = readq_be(&hwq->host_map->intr_status);
  1176. reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
  1177. if (reg_unmasked == 0UL) {
  1178. dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
  1179. __func__, reg);
  1180. goto cxlflash_sync_err_irq_exit;
  1181. }
  1182. dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
  1183. __func__, reg);
  1184. writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
  1185. cxlflash_sync_err_irq_exit:
  1186. return IRQ_HANDLED;
  1187. }
  1188. /**
  1189. * process_hrrq() - process the read-response queue
  1190. * @hwq: HWQ associated with the host.
  1191. * @doneq: Queue of commands harvested from the RRQ.
  1192. * @budget: Threshold of RRQ entries to process.
  1193. *
  1194. * This routine must be called holding the disabled RRQ spin lock.
  1195. *
  1196. * Return: The number of entries processed.
  1197. */
  1198. static int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
  1199. {
  1200. struct afu *afu = hwq->afu;
  1201. struct afu_cmd *cmd;
  1202. struct sisl_ioasa *ioasa;
  1203. struct sisl_ioarcb *ioarcb;
  1204. bool toggle = hwq->toggle;
  1205. int num_hrrq = 0;
  1206. u64 entry,
  1207. *hrrq_start = hwq->hrrq_start,
  1208. *hrrq_end = hwq->hrrq_end,
  1209. *hrrq_curr = hwq->hrrq_curr;
  1210. /* Process ready RRQ entries up to the specified budget (if any) */
  1211. while (true) {
  1212. entry = *hrrq_curr;
  1213. if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
  1214. break;
  1215. entry &= ~SISL_RESP_HANDLE_T_BIT;
  1216. if (afu_is_sq_cmd_mode(afu)) {
  1217. ioasa = (struct sisl_ioasa *)entry;
  1218. cmd = container_of(ioasa, struct afu_cmd, sa);
  1219. } else {
  1220. ioarcb = (struct sisl_ioarcb *)entry;
  1221. cmd = container_of(ioarcb, struct afu_cmd, rcb);
  1222. }
  1223. list_add_tail(&cmd->queue, doneq);
  1224. /* Advance to next entry or wrap and flip the toggle bit */
  1225. if (hrrq_curr < hrrq_end)
  1226. hrrq_curr++;
  1227. else {
  1228. hrrq_curr = hrrq_start;
  1229. toggle ^= SISL_RESP_HANDLE_T_BIT;
  1230. }
  1231. atomic_inc(&hwq->hsq_credits);
  1232. num_hrrq++;
  1233. if (budget > 0 && num_hrrq >= budget)
  1234. break;
  1235. }
  1236. hwq->hrrq_curr = hrrq_curr;
  1237. hwq->toggle = toggle;
  1238. return num_hrrq;
  1239. }
  1240. /**
  1241. * process_cmd_doneq() - process a queue of harvested RRQ commands
  1242. * @doneq: Queue of completed commands.
  1243. *
  1244. * Note that upon return the queue can no longer be trusted.
  1245. */
  1246. static void process_cmd_doneq(struct list_head *doneq)
  1247. {
  1248. struct afu_cmd *cmd, *tmp;
  1249. WARN_ON(list_empty(doneq));
  1250. list_for_each_entry_safe(cmd, tmp, doneq, queue)
  1251. cmd_complete(cmd);
  1252. }
  1253. /**
  1254. * cxlflash_irqpoll() - process a queue of harvested RRQ commands
  1255. * @irqpoll: IRQ poll structure associated with queue to poll.
  1256. * @budget: Threshold of RRQ entries to process per poll.
  1257. *
  1258. * Return: The number of entries processed.
  1259. */
  1260. static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
  1261. {
  1262. struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
  1263. unsigned long hrrq_flags;
  1264. LIST_HEAD(doneq);
  1265. int num_entries = 0;
  1266. spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
  1267. num_entries = process_hrrq(hwq, &doneq, budget);
  1268. if (num_entries < budget)
  1269. irq_poll_complete(irqpoll);
  1270. spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
  1271. process_cmd_doneq(&doneq);
  1272. return num_entries;
  1273. }
  1274. /**
  1275. * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
  1276. * @irq: Interrupt number.
  1277. * @data: Private data provided at interrupt registration, the AFU.
  1278. *
  1279. * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
  1280. */
  1281. static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
  1282. {
  1283. struct hwq *hwq = (struct hwq *)data;
  1284. struct afu *afu = hwq->afu;
  1285. unsigned long hrrq_flags;
  1286. LIST_HEAD(doneq);
  1287. int num_entries = 0;
  1288. spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
  1289. /* Silently drop spurious interrupts when queue is not online */
  1290. if (!hwq->hrrq_online) {
  1291. spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
  1292. return IRQ_HANDLED;
  1293. }
  1294. if (afu_is_irqpoll_enabled(afu)) {
  1295. irq_poll_sched(&hwq->irqpoll);
  1296. spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
  1297. return IRQ_HANDLED;
  1298. }
  1299. num_entries = process_hrrq(hwq, &doneq, -1);
  1300. spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
  1301. if (num_entries == 0)
  1302. return IRQ_NONE;
  1303. process_cmd_doneq(&doneq);
  1304. return IRQ_HANDLED;
  1305. }
  1306. /*
  1307. * Asynchronous interrupt information table
  1308. *
  1309. * NOTE:
  1310. * - Order matters here as this array is indexed by bit position.
  1311. *
  1312. * - The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
  1313. * as complex and complains due to a lack of parentheses/braces.
  1314. */
  1315. #define ASTATUS_FC(_a, _b, _c, _d) \
  1316. { SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
  1317. #define BUILD_SISL_ASTATUS_FC_PORT(_a) \
  1318. ASTATUS_FC(_a, LINK_UP, "link up", 0), \
  1319. ASTATUS_FC(_a, LINK_DN, "link down", 0), \
  1320. ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST), \
  1321. ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR), \
  1322. ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
  1323. ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET), \
  1324. ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0), \
  1325. ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
  1326. static const struct asyc_intr_info ainfo[] = {
  1327. BUILD_SISL_ASTATUS_FC_PORT(1),
  1328. BUILD_SISL_ASTATUS_FC_PORT(0),
  1329. BUILD_SISL_ASTATUS_FC_PORT(3),
  1330. BUILD_SISL_ASTATUS_FC_PORT(2)
  1331. };
  1332. /**
  1333. * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
  1334. * @irq: Interrupt number.
  1335. * @data: Private data provided at interrupt registration, the AFU.
  1336. *
  1337. * Return: Always return IRQ_HANDLED.
  1338. */
  1339. static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
  1340. {
  1341. struct hwq *hwq = (struct hwq *)data;
  1342. struct afu *afu = hwq->afu;
  1343. struct cxlflash_cfg *cfg = afu->parent;
  1344. struct device *dev = &cfg->dev->dev;
  1345. const struct asyc_intr_info *info;
  1346. struct sisl_global_map __iomem *global = &afu->afu_map->global;
  1347. __be64 __iomem *fc_port_regs;
  1348. u64 reg_unmasked;
  1349. u64 reg;
  1350. u64 bit;
  1351. u8 port;
  1352. reg = readq_be(&global->regs.aintr_status);
  1353. reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
  1354. if (unlikely(reg_unmasked == 0)) {
  1355. dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
  1356. __func__, reg);
  1357. goto out;
  1358. }
  1359. /* FYI, it is 'okay' to clear AFU status before FC_ERROR */
  1360. writeq_be(reg_unmasked, &global->regs.aintr_clear);
  1361. /* Check each bit that is on */
  1362. for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
  1363. if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
  1364. WARN_ON_ONCE(1);
  1365. continue;
  1366. }
  1367. info = &ainfo[bit];
  1368. if (unlikely(info->status != 1ULL << bit)) {
  1369. WARN_ON_ONCE(1);
  1370. continue;
  1371. }
  1372. port = info->port;
  1373. fc_port_regs = get_fc_port_regs(cfg, port);
  1374. dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
  1375. __func__, port, info->desc,
  1376. readq_be(&fc_port_regs[FC_STATUS / 8]));
  1377. /*
  1378. * Do link reset first, some OTHER errors will set FC_ERROR
  1379. * again if cleared before or w/o a reset
  1380. */
  1381. if (info->action & LINK_RESET) {
  1382. dev_err(dev, "%s: FC Port %d: resetting link\n",
  1383. __func__, port);
  1384. cfg->lr_state = LINK_RESET_REQUIRED;
  1385. cfg->lr_port = port;
  1386. schedule_work(&cfg->work_q);
  1387. }
  1388. if (info->action & CLR_FC_ERROR) {
  1389. reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
  1390. /*
  1391. * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
  1392. * should be the same and tracing one is sufficient.
  1393. */
  1394. dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
  1395. __func__, port, reg);
  1396. writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
  1397. writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
  1398. }
  1399. if (info->action & SCAN_HOST) {
  1400. atomic_inc(&cfg->scan_host_needed);
  1401. schedule_work(&cfg->work_q);
  1402. }
  1403. }
  1404. out:
  1405. return IRQ_HANDLED;
  1406. }
  1407. /**
  1408. * read_vpd() - obtains the WWPNs from VPD
  1409. * @cfg: Internal structure associated with the host.
  1410. * @wwpn: Array of size MAX_FC_PORTS to pass back WWPNs
  1411. *
  1412. * Return: 0 on success, -errno on failure
  1413. */
  1414. static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
  1415. {
  1416. struct device *dev = &cfg->dev->dev;
  1417. struct pci_dev *pdev = cfg->dev;
  1418. int i, k, rc = 0;
  1419. unsigned int kw_size;
  1420. ssize_t vpd_size;
  1421. char vpd_data[CXLFLASH_VPD_LEN];
  1422. char tmp_buf[WWPN_BUF_LEN] = { 0 };
  1423. const struct dev_dependent_vals *ddv = (struct dev_dependent_vals *)
  1424. cfg->dev_id->driver_data;
  1425. const bool wwpn_vpd_required = ddv->flags & CXLFLASH_WWPN_VPD_REQUIRED;
  1426. const char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
  1427. /* Get the VPD data from the device */
  1428. vpd_size = cfg->ops->read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
  1429. if (unlikely(vpd_size <= 0)) {
  1430. dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
  1431. __func__, vpd_size);
  1432. rc = -ENODEV;
  1433. goto out;
  1434. }
  1435. /*
  1436. * Find the offset of the WWPN tag within the read only
  1437. * VPD data and validate the found field (partials are
  1438. * no good to us). Convert the ASCII data to an integer
  1439. * value. Note that we must copy to a temporary buffer
  1440. * because the conversion service requires that the ASCII
  1441. * string be terminated.
  1442. *
  1443. * Allow for WWPN not being found for all devices, setting
  1444. * the returned WWPN to zero when not found. Notify with a
  1445. * log error for cards that should have had WWPN keywords
  1446. * in the VPD - cards requiring WWPN will not have their
  1447. * ports programmed and operate in an undefined state.
  1448. */
  1449. for (k = 0; k < cfg->num_fc_ports; k++) {
  1450. i = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
  1451. wwpn_vpd_tags[k], &kw_size);
  1452. if (i == -ENOENT) {
  1453. if (wwpn_vpd_required)
  1454. dev_err(dev, "%s: Port %d WWPN not found\n",
  1455. __func__, k);
  1456. wwpn[k] = 0ULL;
  1457. continue;
  1458. }
  1459. if (i < 0 || kw_size != WWPN_LEN) {
  1460. dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
  1461. __func__, k);
  1462. rc = -ENODEV;
  1463. goto out;
  1464. }
  1465. memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
  1466. rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
  1467. if (unlikely(rc)) {
  1468. dev_err(dev, "%s: WWPN conversion failed for port %d\n",
  1469. __func__, k);
  1470. rc = -ENODEV;
  1471. goto out;
  1472. }
  1473. dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
  1474. }
  1475. out:
  1476. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1477. return rc;
  1478. }
  1479. /**
  1480. * init_pcr() - initialize the provisioning and control registers
  1481. * @cfg: Internal structure associated with the host.
  1482. *
  1483. * Also sets up fast access to the mapped registers and initializes AFU
  1484. * command fields that never change.
  1485. */
  1486. static void init_pcr(struct cxlflash_cfg *cfg)
  1487. {
  1488. struct afu *afu = cfg->afu;
  1489. struct sisl_ctrl_map __iomem *ctrl_map;
  1490. struct hwq *hwq;
  1491. void *cookie;
  1492. int i;
  1493. for (i = 0; i < MAX_CONTEXT; i++) {
  1494. ctrl_map = &afu->afu_map->ctrls[i].ctrl;
  1495. /* Disrupt any clients that could be running */
  1496. /* e.g. clients that survived a master restart */
  1497. writeq_be(0, &ctrl_map->rht_start);
  1498. writeq_be(0, &ctrl_map->rht_cnt_id);
  1499. writeq_be(0, &ctrl_map->ctx_cap);
  1500. }
  1501. /* Copy frequently used fields into hwq */
  1502. for (i = 0; i < afu->num_hwqs; i++) {
  1503. hwq = get_hwq(afu, i);
  1504. cookie = hwq->ctx_cookie;
  1505. hwq->ctx_hndl = (u16) cfg->ops->process_element(cookie);
  1506. hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
  1507. hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
  1508. /* Program the Endian Control for the master context */
  1509. writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
  1510. }
  1511. }
  1512. /**
  1513. * init_global() - initialize AFU global registers
  1514. * @cfg: Internal structure associated with the host.
  1515. */
  1516. static int init_global(struct cxlflash_cfg *cfg)
  1517. {
  1518. struct afu *afu = cfg->afu;
  1519. struct device *dev = &cfg->dev->dev;
  1520. struct hwq *hwq;
  1521. struct sisl_host_map __iomem *hmap;
  1522. __be64 __iomem *fc_port_regs;
  1523. u64 wwpn[MAX_FC_PORTS]; /* wwpn of AFU ports */
  1524. int i = 0, num_ports = 0;
  1525. int rc = 0;
  1526. int j;
  1527. void *ctx;
  1528. u64 reg;
  1529. rc = read_vpd(cfg, &wwpn[0]);
  1530. if (rc) {
  1531. dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
  1532. goto out;
  1533. }
  1534. /* Set up RRQ and SQ in HWQ for master issued cmds */
  1535. for (i = 0; i < afu->num_hwqs; i++) {
  1536. hwq = get_hwq(afu, i);
  1537. hmap = hwq->host_map;
  1538. writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
  1539. writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
  1540. hwq->hrrq_online = true;
  1541. if (afu_is_sq_cmd_mode(afu)) {
  1542. writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
  1543. writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
  1544. }
  1545. }
  1546. /* AFU configuration */
  1547. reg = readq_be(&afu->afu_map->global.regs.afu_config);
  1548. reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
  1549. /* enable all auto retry options and control endianness */
  1550. /* leave others at default: */
  1551. /* CTX_CAP write protected, mbox_r does not clear on read and */
  1552. /* checker on if dual afu */
  1553. writeq_be(reg, &afu->afu_map->global.regs.afu_config);
  1554. /* Global port select: select either port */
  1555. if (afu->internal_lun) {
  1556. /* Only use port 0 */
  1557. writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
  1558. num_ports = 0;
  1559. } else {
  1560. writeq_be(PORT_MASK(cfg->num_fc_ports),
  1561. &afu->afu_map->global.regs.afu_port_sel);
  1562. num_ports = cfg->num_fc_ports;
  1563. }
  1564. for (i = 0; i < num_ports; i++) {
  1565. fc_port_regs = get_fc_port_regs(cfg, i);
  1566. /* Unmask all errors (but they are still masked at AFU) */
  1567. writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
  1568. /* Clear CRC error cnt & set a threshold */
  1569. (void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
  1570. writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
  1571. /* Set WWPNs. If already programmed, wwpn[i] is 0 */
  1572. if (wwpn[i] != 0)
  1573. afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
  1574. /* Programming WWPN back to back causes additional
  1575. * offline/online transitions and a PLOGI
  1576. */
  1577. msleep(100);
  1578. }
  1579. if (afu_is_ocxl_lisn(afu)) {
  1580. /* Set up the LISN effective address for each master */
  1581. for (i = 0; i < afu->num_hwqs; i++) {
  1582. hwq = get_hwq(afu, i);
  1583. ctx = hwq->ctx_cookie;
  1584. for (j = 0; j < hwq->num_irqs; j++) {
  1585. reg = cfg->ops->get_irq_objhndl(ctx, j);
  1586. writeq_be(reg, &hwq->ctrl_map->lisn_ea[j]);
  1587. }
  1588. reg = hwq->ctx_hndl;
  1589. writeq_be(SISL_LISN_PASID(reg, reg),
  1590. &hwq->ctrl_map->lisn_pasid[0]);
  1591. writeq_be(SISL_LISN_PASID(0UL, reg),
  1592. &hwq->ctrl_map->lisn_pasid[1]);
  1593. }
  1594. }
  1595. /* Set up master's own CTX_CAP to allow real mode, host translation */
  1596. /* tables, afu cmds and read/write GSCSI cmds. */
  1597. /* First, unlock ctx_cap write by reading mbox */
  1598. for (i = 0; i < afu->num_hwqs; i++) {
  1599. hwq = get_hwq(afu, i);
  1600. (void)readq_be(&hwq->ctrl_map->mbox_r); /* unlock ctx_cap */
  1601. writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
  1602. SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
  1603. SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
  1604. &hwq->ctrl_map->ctx_cap);
  1605. }
  1606. /*
  1607. * Determine write-same unmap support for host by evaluating the unmap
  1608. * sector support bit of the context control register associated with
  1609. * the primary hardware queue. Note that while this status is reflected
  1610. * in a context register, the outcome can be assumed to be host-wide.
  1611. */
  1612. hwq = get_hwq(afu, PRIMARY_HWQ);
  1613. reg = readq_be(&hwq->host_map->ctx_ctrl);
  1614. if (reg & SISL_CTX_CTRL_UNMAP_SECTOR)
  1615. cfg->ws_unmap = true;
  1616. /* Initialize heartbeat */
  1617. afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
  1618. out:
  1619. return rc;
  1620. }
  1621. /**
  1622. * start_afu() - initializes and starts the AFU
  1623. * @cfg: Internal structure associated with the host.
  1624. */
  1625. static int start_afu(struct cxlflash_cfg *cfg)
  1626. {
  1627. struct afu *afu = cfg->afu;
  1628. struct device *dev = &cfg->dev->dev;
  1629. struct hwq *hwq;
  1630. int rc = 0;
  1631. int i;
  1632. init_pcr(cfg);
  1633. /* Initialize each HWQ */
  1634. for (i = 0; i < afu->num_hwqs; i++) {
  1635. hwq = get_hwq(afu, i);
  1636. /* After an AFU reset, RRQ entries are stale, clear them */
  1637. memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
  1638. /* Initialize RRQ pointers */
  1639. hwq->hrrq_start = &hwq->rrq_entry[0];
  1640. hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
  1641. hwq->hrrq_curr = hwq->hrrq_start;
  1642. hwq->toggle = 1;
  1643. /* Initialize spin locks */
  1644. spin_lock_init(&hwq->hrrq_slock);
  1645. spin_lock_init(&hwq->hsq_slock);
  1646. /* Initialize SQ */
  1647. if (afu_is_sq_cmd_mode(afu)) {
  1648. memset(&hwq->sq, 0, sizeof(hwq->sq));
  1649. hwq->hsq_start = &hwq->sq[0];
  1650. hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
  1651. hwq->hsq_curr = hwq->hsq_start;
  1652. atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
  1653. }
  1654. /* Initialize IRQ poll */
  1655. if (afu_is_irqpoll_enabled(afu))
  1656. irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
  1657. cxlflash_irqpoll);
  1658. }
  1659. rc = init_global(cfg);
  1660. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1661. return rc;
  1662. }
  1663. /**
  1664. * init_intr() - setup interrupt handlers for the master context
  1665. * @cfg: Internal structure associated with the host.
  1666. * @hwq: Hardware queue to initialize.
  1667. *
  1668. * Return: 0 on success, -errno on failure
  1669. */
  1670. static enum undo_level init_intr(struct cxlflash_cfg *cfg,
  1671. struct hwq *hwq)
  1672. {
  1673. struct device *dev = &cfg->dev->dev;
  1674. void *ctx = hwq->ctx_cookie;
  1675. int rc = 0;
  1676. enum undo_level level = UNDO_NOOP;
  1677. bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
  1678. int num_irqs = hwq->num_irqs;
  1679. rc = cfg->ops->allocate_afu_irqs(ctx, num_irqs);
  1680. if (unlikely(rc)) {
  1681. dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
  1682. __func__, rc);
  1683. level = UNDO_NOOP;
  1684. goto out;
  1685. }
  1686. rc = cfg->ops->map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
  1687. "SISL_MSI_SYNC_ERROR");
  1688. if (unlikely(rc <= 0)) {
  1689. dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
  1690. level = FREE_IRQ;
  1691. goto out;
  1692. }
  1693. rc = cfg->ops->map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
  1694. "SISL_MSI_RRQ_UPDATED");
  1695. if (unlikely(rc <= 0)) {
  1696. dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
  1697. level = UNMAP_ONE;
  1698. goto out;
  1699. }
  1700. /* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
  1701. if (!is_primary_hwq)
  1702. goto out;
  1703. rc = cfg->ops->map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
  1704. "SISL_MSI_ASYNC_ERROR");
  1705. if (unlikely(rc <= 0)) {
  1706. dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
  1707. level = UNMAP_TWO;
  1708. goto out;
  1709. }
  1710. out:
  1711. return level;
  1712. }
  1713. /**
  1714. * init_mc() - create and register as the master context
  1715. * @cfg: Internal structure associated with the host.
  1716. * @index: HWQ Index of the master context.
  1717. *
  1718. * Return: 0 on success, -errno on failure
  1719. */
  1720. static int init_mc(struct cxlflash_cfg *cfg, u32 index)
  1721. {
  1722. void *ctx;
  1723. struct device *dev = &cfg->dev->dev;
  1724. struct hwq *hwq = get_hwq(cfg->afu, index);
  1725. int rc = 0;
  1726. int num_irqs;
  1727. enum undo_level level;
  1728. hwq->afu = cfg->afu;
  1729. hwq->index = index;
  1730. INIT_LIST_HEAD(&hwq->pending_cmds);
  1731. if (index == PRIMARY_HWQ) {
  1732. ctx = cfg->ops->get_context(cfg->dev, cfg->afu_cookie);
  1733. num_irqs = 3;
  1734. } else {
  1735. ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
  1736. num_irqs = 2;
  1737. }
  1738. if (IS_ERR_OR_NULL(ctx)) {
  1739. rc = -ENOMEM;
  1740. goto err1;
  1741. }
  1742. WARN_ON(hwq->ctx_cookie);
  1743. hwq->ctx_cookie = ctx;
  1744. hwq->num_irqs = num_irqs;
  1745. /* Set it up as a master with the CXL */
  1746. cfg->ops->set_master(ctx);
  1747. /* Reset AFU when initializing primary context */
  1748. if (index == PRIMARY_HWQ) {
  1749. rc = cfg->ops->afu_reset(ctx);
  1750. if (unlikely(rc)) {
  1751. dev_err(dev, "%s: AFU reset failed rc=%d\n",
  1752. __func__, rc);
  1753. goto err1;
  1754. }
  1755. }
  1756. level = init_intr(cfg, hwq);
  1757. if (unlikely(level)) {
  1758. dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
  1759. goto err2;
  1760. }
  1761. /* Finally, activate the context by starting it */
  1762. rc = cfg->ops->start_context(hwq->ctx_cookie);
  1763. if (unlikely(rc)) {
  1764. dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
  1765. level = UNMAP_THREE;
  1766. goto err2;
  1767. }
  1768. out:
  1769. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1770. return rc;
  1771. err2:
  1772. term_intr(cfg, level, index);
  1773. if (index != PRIMARY_HWQ)
  1774. cfg->ops->release_context(ctx);
  1775. err1:
  1776. hwq->ctx_cookie = NULL;
  1777. goto out;
  1778. }
  1779. /**
  1780. * get_num_afu_ports() - determines and configures the number of AFU ports
  1781. * @cfg: Internal structure associated with the host.
  1782. *
  1783. * This routine determines the number of AFU ports by converting the global
  1784. * port selection mask. The converted value is only valid following an AFU
  1785. * reset (explicit or power-on). This routine must be invoked shortly after
  1786. * mapping as other routines are dependent on the number of ports during the
  1787. * initialization sequence.
  1788. *
  1789. * To support legacy AFUs that might not have reflected an initial global
  1790. * port mask (value read is 0), default to the number of ports originally
  1791. * supported by the cxlflash driver (2) before hardware with other port
  1792. * offerings was introduced.
  1793. */
  1794. static void get_num_afu_ports(struct cxlflash_cfg *cfg)
  1795. {
  1796. struct afu *afu = cfg->afu;
  1797. struct device *dev = &cfg->dev->dev;
  1798. u64 port_mask;
  1799. int num_fc_ports = LEGACY_FC_PORTS;
  1800. port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
  1801. if (port_mask != 0ULL)
  1802. num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
  1803. dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
  1804. __func__, port_mask, num_fc_ports);
  1805. cfg->num_fc_ports = num_fc_ports;
  1806. cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
  1807. }
  1808. /**
  1809. * init_afu() - setup as master context and start AFU
  1810. * @cfg: Internal structure associated with the host.
  1811. *
  1812. * This routine is a higher level of control for configuring the
  1813. * AFU on probe and reset paths.
  1814. *
  1815. * Return: 0 on success, -errno on failure
  1816. */
  1817. static int init_afu(struct cxlflash_cfg *cfg)
  1818. {
  1819. u64 reg;
  1820. int rc = 0;
  1821. struct afu *afu = cfg->afu;
  1822. struct device *dev = &cfg->dev->dev;
  1823. struct hwq *hwq;
  1824. int i;
  1825. cfg->ops->perst_reloads_same_image(cfg->afu_cookie, true);
  1826. mutex_init(&afu->sync_active);
  1827. afu->num_hwqs = afu->desired_hwqs;
  1828. for (i = 0; i < afu->num_hwqs; i++) {
  1829. rc = init_mc(cfg, i);
  1830. if (rc) {
  1831. dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
  1832. __func__, rc, i);
  1833. goto err1;
  1834. }
  1835. }
  1836. /* Map the entire MMIO space of the AFU using the first context */
  1837. hwq = get_hwq(afu, PRIMARY_HWQ);
  1838. afu->afu_map = cfg->ops->psa_map(hwq->ctx_cookie);
  1839. if (!afu->afu_map) {
  1840. dev_err(dev, "%s: psa_map failed\n", __func__);
  1841. rc = -ENOMEM;
  1842. goto err1;
  1843. }
  1844. /* No byte reverse on reading afu_version or string will be backwards */
  1845. reg = readq(&afu->afu_map->global.regs.afu_version);
  1846. memcpy(afu->version, &reg, sizeof(reg));
  1847. afu->interface_version =
  1848. readq_be(&afu->afu_map->global.regs.interface_version);
  1849. if ((afu->interface_version + 1) == 0) {
  1850. dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
  1851. "interface version %016llx\n", afu->version,
  1852. afu->interface_version);
  1853. rc = -EINVAL;
  1854. goto err1;
  1855. }
  1856. if (afu_is_sq_cmd_mode(afu)) {
  1857. afu->send_cmd = send_cmd_sq;
  1858. afu->context_reset = context_reset_sq;
  1859. } else {
  1860. afu->send_cmd = send_cmd_ioarrin;
  1861. afu->context_reset = context_reset_ioarrin;
  1862. }
  1863. dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
  1864. afu->version, afu->interface_version);
  1865. get_num_afu_ports(cfg);
  1866. rc = start_afu(cfg);
  1867. if (rc) {
  1868. dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
  1869. goto err1;
  1870. }
  1871. afu_err_intr_init(cfg->afu);
  1872. for (i = 0; i < afu->num_hwqs; i++) {
  1873. hwq = get_hwq(afu, i);
  1874. hwq->room = readq_be(&hwq->host_map->cmd_room);
  1875. }
  1876. /* Restore the LUN mappings */
  1877. cxlflash_restore_luntable(cfg);
  1878. out:
  1879. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1880. return rc;
  1881. err1:
  1882. for (i = afu->num_hwqs - 1; i >= 0; i--) {
  1883. term_intr(cfg, UNMAP_THREE, i);
  1884. term_mc(cfg, i);
  1885. }
  1886. goto out;
  1887. }
  1888. /**
  1889. * afu_reset() - resets the AFU
  1890. * @cfg: Internal structure associated with the host.
  1891. *
  1892. * Return: 0 on success, -errno on failure
  1893. */
  1894. static int afu_reset(struct cxlflash_cfg *cfg)
  1895. {
  1896. struct device *dev = &cfg->dev->dev;
  1897. int rc = 0;
  1898. /* Stop the context before the reset. Since the context is
  1899. * no longer available restart it after the reset is complete
  1900. */
  1901. term_afu(cfg);
  1902. rc = init_afu(cfg);
  1903. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1904. return rc;
  1905. }
  1906. /**
  1907. * drain_ioctls() - wait until all currently executing ioctls have completed
  1908. * @cfg: Internal structure associated with the host.
  1909. *
  1910. * Obtain write access to read/write semaphore that wraps ioctl
  1911. * handling to 'drain' ioctls currently executing.
  1912. */
  1913. static void drain_ioctls(struct cxlflash_cfg *cfg)
  1914. {
  1915. down_write(&cfg->ioctl_rwsem);
  1916. up_write(&cfg->ioctl_rwsem);
  1917. }
  1918. /**
  1919. * cxlflash_async_reset_host() - asynchronous host reset handler
  1920. * @data: Private data provided while scheduling reset.
  1921. * @cookie: Cookie that can be used for checkpointing.
  1922. */
  1923. static void cxlflash_async_reset_host(void *data, async_cookie_t cookie)
  1924. {
  1925. struct cxlflash_cfg *cfg = data;
  1926. struct device *dev = &cfg->dev->dev;
  1927. int rc = 0;
  1928. if (cfg->state != STATE_RESET) {
  1929. dev_dbg(dev, "%s: Not performing a reset, state=%d\n",
  1930. __func__, cfg->state);
  1931. goto out;
  1932. }
  1933. drain_ioctls(cfg);
  1934. cxlflash_mark_contexts_error(cfg);
  1935. rc = afu_reset(cfg);
  1936. if (rc)
  1937. cfg->state = STATE_FAILTERM;
  1938. else
  1939. cfg->state = STATE_NORMAL;
  1940. wake_up_all(&cfg->reset_waitq);
  1941. out:
  1942. scsi_unblock_requests(cfg->host);
  1943. }
  1944. /**
  1945. * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
  1946. * @cfg: Internal structure associated with the host.
  1947. */
  1948. static void cxlflash_schedule_async_reset(struct cxlflash_cfg *cfg)
  1949. {
  1950. struct device *dev = &cfg->dev->dev;
  1951. if (cfg->state != STATE_NORMAL) {
  1952. dev_dbg(dev, "%s: Not performing reset state=%d\n",
  1953. __func__, cfg->state);
  1954. return;
  1955. }
  1956. cfg->state = STATE_RESET;
  1957. scsi_block_requests(cfg->host);
  1958. cfg->async_reset_cookie = async_schedule(cxlflash_async_reset_host,
  1959. cfg);
  1960. }
  1961. /**
  1962. * send_afu_cmd() - builds and sends an internal AFU command
  1963. * @afu: AFU associated with the host.
  1964. * @rcb: Pre-populated IOARCB describing command to send.
  1965. *
  1966. * The AFU can only take one internal AFU command at a time. This limitation is
  1967. * enforced by using a mutex to provide exclusive access to the AFU during the
  1968. * operation. This design point requires calling threads to not be on interrupt
  1969. * context due to the possibility of sleeping during concurrent AFU operations.
  1970. *
  1971. * The command status is optionally passed back to the caller when the caller
  1972. * populates the IOASA field of the IOARCB with a pointer to an IOASA structure.
  1973. *
  1974. * Return:
  1975. * 0 on success, -errno on failure
  1976. */
  1977. static int send_afu_cmd(struct afu *afu, struct sisl_ioarcb *rcb)
  1978. {
  1979. struct cxlflash_cfg *cfg = afu->parent;
  1980. struct device *dev = &cfg->dev->dev;
  1981. struct afu_cmd *cmd = NULL;
  1982. struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
  1983. ulong lock_flags;
  1984. char *buf = NULL;
  1985. int rc = 0;
  1986. int nretry = 0;
  1987. if (cfg->state != STATE_NORMAL) {
  1988. dev_dbg(dev, "%s: Sync not required state=%u\n",
  1989. __func__, cfg->state);
  1990. return 0;
  1991. }
  1992. mutex_lock(&afu->sync_active);
  1993. atomic_inc(&afu->cmds_active);
  1994. buf = kmalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
  1995. if (unlikely(!buf)) {
  1996. dev_err(dev, "%s: no memory for command\n", __func__);
  1997. rc = -ENOMEM;
  1998. goto out;
  1999. }
  2000. cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
  2001. retry:
  2002. memset(cmd, 0, sizeof(*cmd));
  2003. memcpy(&cmd->rcb, rcb, sizeof(*rcb));
  2004. INIT_LIST_HEAD(&cmd->queue);
  2005. init_completion(&cmd->cevent);
  2006. cmd->parent = afu;
  2007. cmd->hwq_index = hwq->index;
  2008. cmd->rcb.ctx_id = hwq->ctx_hndl;
  2009. dev_dbg(dev, "%s: afu=%p cmd=%p type=%02x nretry=%d\n",
  2010. __func__, afu, cmd, cmd->rcb.cdb[0], nretry);
  2011. rc = afu->send_cmd(afu, cmd);
  2012. if (unlikely(rc)) {
  2013. rc = -ENOBUFS;
  2014. goto out;
  2015. }
  2016. rc = wait_resp(afu, cmd);
  2017. switch (rc) {
  2018. case -ETIMEDOUT:
  2019. rc = afu->context_reset(hwq);
  2020. if (rc) {
  2021. /* Delete the command from pending_cmds list */
  2022. spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
  2023. list_del(&cmd->list);
  2024. spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
  2025. cxlflash_schedule_async_reset(cfg);
  2026. break;
  2027. }
  2028. fallthrough; /* to retry */
  2029. case -EAGAIN:
  2030. if (++nretry < 2)
  2031. goto retry;
  2032. fallthrough; /* to exit */
  2033. default:
  2034. break;
  2035. }
  2036. if (rcb->ioasa)
  2037. *rcb->ioasa = cmd->sa;
  2038. out:
  2039. atomic_dec(&afu->cmds_active);
  2040. mutex_unlock(&afu->sync_active);
  2041. kfree(buf);
  2042. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  2043. return rc;
  2044. }
  2045. /**
  2046. * cxlflash_afu_sync() - builds and sends an AFU sync command
  2047. * @afu: AFU associated with the host.
  2048. * @ctx: Identifies context requesting sync.
  2049. * @res: Identifies resource requesting sync.
  2050. * @mode: Type of sync to issue (lightweight, heavyweight, global).
  2051. *
  2052. * AFU sync operations are only necessary and allowed when the device is
  2053. * operating normally. When not operating normally, sync requests can occur as
  2054. * part of cleaning up resources associated with an adapter prior to removal.
  2055. * In this scenario, these requests are simply ignored (safe due to the AFU
  2056. * going away).
  2057. *
  2058. * Return:
  2059. * 0 on success, -errno on failure
  2060. */
  2061. int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx, res_hndl_t res, u8 mode)
  2062. {
  2063. struct cxlflash_cfg *cfg = afu->parent;
  2064. struct device *dev = &cfg->dev->dev;
  2065. struct sisl_ioarcb rcb = { 0 };
  2066. dev_dbg(dev, "%s: afu=%p ctx=%u res=%u mode=%u\n",
  2067. __func__, afu, ctx, res, mode);
  2068. rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
  2069. rcb.msi = SISL_MSI_RRQ_UPDATED;
  2070. rcb.timeout = MC_AFU_SYNC_TIMEOUT;
  2071. rcb.cdb[0] = SISL_AFU_CMD_SYNC;
  2072. rcb.cdb[1] = mode;
  2073. put_unaligned_be16(ctx, &rcb.cdb[2]);
  2074. put_unaligned_be32(res, &rcb.cdb[4]);
  2075. return send_afu_cmd(afu, &rcb);
  2076. }
  2077. /**
  2078. * cxlflash_eh_abort_handler() - abort a SCSI command
  2079. * @scp: SCSI command to abort.
  2080. *
  2081. * CXL Flash devices do not support a single command abort. Reset the context
  2082. * as per SISLite specification. Flush any pending commands in the hardware
  2083. * queue before the reset.
  2084. *
  2085. * Return: SUCCESS/FAILED as defined in scsi/scsi.h
  2086. */
  2087. static int cxlflash_eh_abort_handler(struct scsi_cmnd *scp)
  2088. {
  2089. int rc = FAILED;
  2090. struct Scsi_Host *host = scp->device->host;
  2091. struct cxlflash_cfg *cfg = shost_priv(host);
  2092. struct afu_cmd *cmd = sc_to_afuc(scp);
  2093. struct device *dev = &cfg->dev->dev;
  2094. struct afu *afu = cfg->afu;
  2095. struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
  2096. dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
  2097. "cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
  2098. scp->device->channel, scp->device->id, scp->device->lun,
  2099. get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
  2100. get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
  2101. get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
  2102. get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
  2103. /* When the state is not normal, another reset/reload is in progress.
  2104. * Return failed and the mid-layer will invoke host reset handler.
  2105. */
  2106. if (cfg->state != STATE_NORMAL) {
  2107. dev_dbg(dev, "%s: Invalid state for abort, state=%d\n",
  2108. __func__, cfg->state);
  2109. goto out;
  2110. }
  2111. rc = afu->context_reset(hwq);
  2112. if (unlikely(rc))
  2113. goto out;
  2114. rc = SUCCESS;
  2115. out:
  2116. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  2117. return rc;
  2118. }
  2119. /**
  2120. * cxlflash_eh_device_reset_handler() - reset a single LUN
  2121. * @scp: SCSI command to send.
  2122. *
  2123. * Return:
  2124. * SUCCESS as defined in scsi/scsi.h
  2125. * FAILED as defined in scsi/scsi.h
  2126. */
  2127. static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
  2128. {
  2129. int rc = SUCCESS;
  2130. struct scsi_device *sdev = scp->device;
  2131. struct Scsi_Host *host = sdev->host;
  2132. struct cxlflash_cfg *cfg = shost_priv(host);
  2133. struct device *dev = &cfg->dev->dev;
  2134. int rcr = 0;
  2135. dev_dbg(dev, "%s: %d/%d/%d/%llu\n", __func__,
  2136. host->host_no, sdev->channel, sdev->id, sdev->lun);
  2137. retry:
  2138. switch (cfg->state) {
  2139. case STATE_NORMAL:
  2140. rcr = send_tmf(cfg, sdev, TMF_LUN_RESET);
  2141. if (unlikely(rcr))
  2142. rc = FAILED;
  2143. break;
  2144. case STATE_RESET:
  2145. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  2146. goto retry;
  2147. default:
  2148. rc = FAILED;
  2149. break;
  2150. }
  2151. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  2152. return rc;
  2153. }
  2154. /**
  2155. * cxlflash_eh_host_reset_handler() - reset the host adapter
  2156. * @scp: SCSI command from stack identifying host.
  2157. *
  2158. * Following a reset, the state is evaluated again in case an EEH occurred
  2159. * during the reset. In such a scenario, the host reset will either yield
  2160. * until the EEH recovery is complete or return success or failure based
  2161. * upon the current device state.
  2162. *
  2163. * Return:
  2164. * SUCCESS as defined in scsi/scsi.h
  2165. * FAILED as defined in scsi/scsi.h
  2166. */
  2167. static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
  2168. {
  2169. int rc = SUCCESS;
  2170. int rcr = 0;
  2171. struct Scsi_Host *host = scp->device->host;
  2172. struct cxlflash_cfg *cfg = shost_priv(host);
  2173. struct device *dev = &cfg->dev->dev;
  2174. dev_dbg(dev, "%s: %d\n", __func__, host->host_no);
  2175. switch (cfg->state) {
  2176. case STATE_NORMAL:
  2177. cfg->state = STATE_RESET;
  2178. drain_ioctls(cfg);
  2179. cxlflash_mark_contexts_error(cfg);
  2180. rcr = afu_reset(cfg);
  2181. if (rcr) {
  2182. rc = FAILED;
  2183. cfg->state = STATE_FAILTERM;
  2184. } else
  2185. cfg->state = STATE_NORMAL;
  2186. wake_up_all(&cfg->reset_waitq);
  2187. ssleep(1);
  2188. fallthrough;
  2189. case STATE_RESET:
  2190. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  2191. if (cfg->state == STATE_NORMAL)
  2192. break;
  2193. fallthrough;
  2194. default:
  2195. rc = FAILED;
  2196. break;
  2197. }
  2198. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  2199. return rc;
  2200. }
  2201. /**
  2202. * cxlflash_change_queue_depth() - change the queue depth for the device
  2203. * @sdev: SCSI device destined for queue depth change.
  2204. * @qdepth: Requested queue depth value to set.
  2205. *
  2206. * The requested queue depth is capped to the maximum supported value.
  2207. *
  2208. * Return: The actual queue depth set.
  2209. */
  2210. static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
  2211. {
  2212. if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
  2213. qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
  2214. scsi_change_queue_depth(sdev, qdepth);
  2215. return sdev->queue_depth;
  2216. }
  2217. /**
  2218. * cxlflash_show_port_status() - queries and presents the current port status
  2219. * @port: Desired port for status reporting.
  2220. * @cfg: Internal structure associated with the host.
  2221. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2222. *
  2223. * Return: The size of the ASCII string returned in @buf or -EINVAL.
  2224. */
  2225. static ssize_t cxlflash_show_port_status(u32 port,
  2226. struct cxlflash_cfg *cfg,
  2227. char *buf)
  2228. {
  2229. struct device *dev = &cfg->dev->dev;
  2230. char *disp_status;
  2231. u64 status;
  2232. __be64 __iomem *fc_port_regs;
  2233. WARN_ON(port >= MAX_FC_PORTS);
  2234. if (port >= cfg->num_fc_ports) {
  2235. dev_info(dev, "%s: Port %d not supported on this card.\n",
  2236. __func__, port);
  2237. return -EINVAL;
  2238. }
  2239. fc_port_regs = get_fc_port_regs(cfg, port);
  2240. status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
  2241. status &= FC_MTIP_STATUS_MASK;
  2242. if (status == FC_MTIP_STATUS_ONLINE)
  2243. disp_status = "online";
  2244. else if (status == FC_MTIP_STATUS_OFFLINE)
  2245. disp_status = "offline";
  2246. else
  2247. disp_status = "unknown";
  2248. return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
  2249. }
  2250. /**
  2251. * port0_show() - queries and presents the current status of port 0
  2252. * @dev: Generic device associated with the host owning the port.
  2253. * @attr: Device attribute representing the port.
  2254. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2255. *
  2256. * Return: The size of the ASCII string returned in @buf.
  2257. */
  2258. static ssize_t port0_show(struct device *dev,
  2259. struct device_attribute *attr,
  2260. char *buf)
  2261. {
  2262. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2263. return cxlflash_show_port_status(0, cfg, buf);
  2264. }
  2265. /**
  2266. * port1_show() - queries and presents the current status of port 1
  2267. * @dev: Generic device associated with the host owning the port.
  2268. * @attr: Device attribute representing the port.
  2269. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2270. *
  2271. * Return: The size of the ASCII string returned in @buf.
  2272. */
  2273. static ssize_t port1_show(struct device *dev,
  2274. struct device_attribute *attr,
  2275. char *buf)
  2276. {
  2277. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2278. return cxlflash_show_port_status(1, cfg, buf);
  2279. }
  2280. /**
  2281. * port2_show() - queries and presents the current status of port 2
  2282. * @dev: Generic device associated with the host owning the port.
  2283. * @attr: Device attribute representing the port.
  2284. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2285. *
  2286. * Return: The size of the ASCII string returned in @buf.
  2287. */
  2288. static ssize_t port2_show(struct device *dev,
  2289. struct device_attribute *attr,
  2290. char *buf)
  2291. {
  2292. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2293. return cxlflash_show_port_status(2, cfg, buf);
  2294. }
  2295. /**
  2296. * port3_show() - queries and presents the current status of port 3
  2297. * @dev: Generic device associated with the host owning the port.
  2298. * @attr: Device attribute representing the port.
  2299. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2300. *
  2301. * Return: The size of the ASCII string returned in @buf.
  2302. */
  2303. static ssize_t port3_show(struct device *dev,
  2304. struct device_attribute *attr,
  2305. char *buf)
  2306. {
  2307. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2308. return cxlflash_show_port_status(3, cfg, buf);
  2309. }
  2310. /**
  2311. * lun_mode_show() - presents the current LUN mode of the host
  2312. * @dev: Generic device associated with the host.
  2313. * @attr: Device attribute representing the LUN mode.
  2314. * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
  2315. *
  2316. * Return: The size of the ASCII string returned in @buf.
  2317. */
  2318. static ssize_t lun_mode_show(struct device *dev,
  2319. struct device_attribute *attr, char *buf)
  2320. {
  2321. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2322. struct afu *afu = cfg->afu;
  2323. return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
  2324. }
  2325. /**
  2326. * lun_mode_store() - sets the LUN mode of the host
  2327. * @dev: Generic device associated with the host.
  2328. * @attr: Device attribute representing the LUN mode.
  2329. * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
  2330. * @count: Length of data resizing in @buf.
  2331. *
  2332. * The CXL Flash AFU supports a dummy LUN mode where the external
  2333. * links and storage are not required. Space on the FPGA is used
  2334. * to create 1 or 2 small LUNs which are presented to the system
  2335. * as if they were a normal storage device. This feature is useful
  2336. * during development and also provides manufacturing with a way
  2337. * to test the AFU without an actual device.
  2338. *
  2339. * 0 = external LUN[s] (default)
  2340. * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
  2341. * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
  2342. * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
  2343. * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
  2344. *
  2345. * Return: The size of the ASCII string returned in @buf.
  2346. */
  2347. static ssize_t lun_mode_store(struct device *dev,
  2348. struct device_attribute *attr,
  2349. const char *buf, size_t count)
  2350. {
  2351. struct Scsi_Host *shost = class_to_shost(dev);
  2352. struct cxlflash_cfg *cfg = shost_priv(shost);
  2353. struct afu *afu = cfg->afu;
  2354. int rc;
  2355. u32 lun_mode;
  2356. rc = kstrtouint(buf, 10, &lun_mode);
  2357. if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
  2358. afu->internal_lun = lun_mode;
  2359. /*
  2360. * When configured for internal LUN, there is only one channel,
  2361. * channel number 0, else there will be one less than the number
  2362. * of fc ports for this card.
  2363. */
  2364. if (afu->internal_lun)
  2365. shost->max_channel = 0;
  2366. else
  2367. shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
  2368. afu_reset(cfg);
  2369. scsi_scan_host(cfg->host);
  2370. }
  2371. return count;
  2372. }
  2373. /**
  2374. * ioctl_version_show() - presents the current ioctl version of the host
  2375. * @dev: Generic device associated with the host.
  2376. * @attr: Device attribute representing the ioctl version.
  2377. * @buf: Buffer of length PAGE_SIZE to report back the ioctl version.
  2378. *
  2379. * Return: The size of the ASCII string returned in @buf.
  2380. */
  2381. static ssize_t ioctl_version_show(struct device *dev,
  2382. struct device_attribute *attr, char *buf)
  2383. {
  2384. ssize_t bytes = 0;
  2385. bytes = scnprintf(buf, PAGE_SIZE,
  2386. "disk: %u\n", DK_CXLFLASH_VERSION_0);
  2387. bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
  2388. "host: %u\n", HT_CXLFLASH_VERSION_0);
  2389. return bytes;
  2390. }
  2391. /**
  2392. * cxlflash_show_port_lun_table() - queries and presents the port LUN table
  2393. * @port: Desired port for status reporting.
  2394. * @cfg: Internal structure associated with the host.
  2395. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2396. *
  2397. * Return: The size of the ASCII string returned in @buf or -EINVAL.
  2398. */
  2399. static ssize_t cxlflash_show_port_lun_table(u32 port,
  2400. struct cxlflash_cfg *cfg,
  2401. char *buf)
  2402. {
  2403. struct device *dev = &cfg->dev->dev;
  2404. __be64 __iomem *fc_port_luns;
  2405. int i;
  2406. ssize_t bytes = 0;
  2407. WARN_ON(port >= MAX_FC_PORTS);
  2408. if (port >= cfg->num_fc_ports) {
  2409. dev_info(dev, "%s: Port %d not supported on this card.\n",
  2410. __func__, port);
  2411. return -EINVAL;
  2412. }
  2413. fc_port_luns = get_fc_port_luns(cfg, port);
  2414. for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
  2415. bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
  2416. "%03d: %016llx\n",
  2417. i, readq_be(&fc_port_luns[i]));
  2418. return bytes;
  2419. }
  2420. /**
  2421. * port0_lun_table_show() - presents the current LUN table of port 0
  2422. * @dev: Generic device associated with the host owning the port.
  2423. * @attr: Device attribute representing the port.
  2424. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2425. *
  2426. * Return: The size of the ASCII string returned in @buf.
  2427. */
  2428. static ssize_t port0_lun_table_show(struct device *dev,
  2429. struct device_attribute *attr,
  2430. char *buf)
  2431. {
  2432. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2433. return cxlflash_show_port_lun_table(0, cfg, buf);
  2434. }
  2435. /**
  2436. * port1_lun_table_show() - presents the current LUN table of port 1
  2437. * @dev: Generic device associated with the host owning the port.
  2438. * @attr: Device attribute representing the port.
  2439. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2440. *
  2441. * Return: The size of the ASCII string returned in @buf.
  2442. */
  2443. static ssize_t port1_lun_table_show(struct device *dev,
  2444. struct device_attribute *attr,
  2445. char *buf)
  2446. {
  2447. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2448. return cxlflash_show_port_lun_table(1, cfg, buf);
  2449. }
  2450. /**
  2451. * port2_lun_table_show() - presents the current LUN table of port 2
  2452. * @dev: Generic device associated with the host owning the port.
  2453. * @attr: Device attribute representing the port.
  2454. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2455. *
  2456. * Return: The size of the ASCII string returned in @buf.
  2457. */
  2458. static ssize_t port2_lun_table_show(struct device *dev,
  2459. struct device_attribute *attr,
  2460. char *buf)
  2461. {
  2462. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2463. return cxlflash_show_port_lun_table(2, cfg, buf);
  2464. }
  2465. /**
  2466. * port3_lun_table_show() - presents the current LUN table of port 3
  2467. * @dev: Generic device associated with the host owning the port.
  2468. * @attr: Device attribute representing the port.
  2469. * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII.
  2470. *
  2471. * Return: The size of the ASCII string returned in @buf.
  2472. */
  2473. static ssize_t port3_lun_table_show(struct device *dev,
  2474. struct device_attribute *attr,
  2475. char *buf)
  2476. {
  2477. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2478. return cxlflash_show_port_lun_table(3, cfg, buf);
  2479. }
  2480. /**
  2481. * irqpoll_weight_show() - presents the current IRQ poll weight for the host
  2482. * @dev: Generic device associated with the host.
  2483. * @attr: Device attribute representing the IRQ poll weight.
  2484. * @buf: Buffer of length PAGE_SIZE to report back the current IRQ poll
  2485. * weight in ASCII.
  2486. *
  2487. * An IRQ poll weight of 0 indicates polling is disabled.
  2488. *
  2489. * Return: The size of the ASCII string returned in @buf.
  2490. */
  2491. static ssize_t irqpoll_weight_show(struct device *dev,
  2492. struct device_attribute *attr, char *buf)
  2493. {
  2494. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2495. struct afu *afu = cfg->afu;
  2496. return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
  2497. }
  2498. /**
  2499. * irqpoll_weight_store() - sets the current IRQ poll weight for the host
  2500. * @dev: Generic device associated with the host.
  2501. * @attr: Device attribute representing the IRQ poll weight.
  2502. * @buf: Buffer of length PAGE_SIZE containing the desired IRQ poll
  2503. * weight in ASCII.
  2504. * @count: Length of data resizing in @buf.
  2505. *
  2506. * An IRQ poll weight of 0 indicates polling is disabled.
  2507. *
  2508. * Return: The size of the ASCII string returned in @buf.
  2509. */
  2510. static ssize_t irqpoll_weight_store(struct device *dev,
  2511. struct device_attribute *attr,
  2512. const char *buf, size_t count)
  2513. {
  2514. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2515. struct device *cfgdev = &cfg->dev->dev;
  2516. struct afu *afu = cfg->afu;
  2517. struct hwq *hwq;
  2518. u32 weight;
  2519. int rc, i;
  2520. rc = kstrtouint(buf, 10, &weight);
  2521. if (rc)
  2522. return -EINVAL;
  2523. if (weight > 256) {
  2524. dev_info(cfgdev,
  2525. "Invalid IRQ poll weight. It must be 256 or less.\n");
  2526. return -EINVAL;
  2527. }
  2528. if (weight == afu->irqpoll_weight) {
  2529. dev_info(cfgdev,
  2530. "Current IRQ poll weight has the same weight.\n");
  2531. return -EINVAL;
  2532. }
  2533. if (afu_is_irqpoll_enabled(afu)) {
  2534. for (i = 0; i < afu->num_hwqs; i++) {
  2535. hwq = get_hwq(afu, i);
  2536. irq_poll_disable(&hwq->irqpoll);
  2537. }
  2538. }
  2539. afu->irqpoll_weight = weight;
  2540. if (weight > 0) {
  2541. for (i = 0; i < afu->num_hwqs; i++) {
  2542. hwq = get_hwq(afu, i);
  2543. irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
  2544. }
  2545. }
  2546. return count;
  2547. }
  2548. /**
  2549. * num_hwqs_show() - presents the number of hardware queues for the host
  2550. * @dev: Generic device associated with the host.
  2551. * @attr: Device attribute representing the number of hardware queues.
  2552. * @buf: Buffer of length PAGE_SIZE to report back the number of hardware
  2553. * queues in ASCII.
  2554. *
  2555. * Return: The size of the ASCII string returned in @buf.
  2556. */
  2557. static ssize_t num_hwqs_show(struct device *dev,
  2558. struct device_attribute *attr, char *buf)
  2559. {
  2560. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2561. struct afu *afu = cfg->afu;
  2562. return scnprintf(buf, PAGE_SIZE, "%u\n", afu->num_hwqs);
  2563. }
  2564. /**
  2565. * num_hwqs_store() - sets the number of hardware queues for the host
  2566. * @dev: Generic device associated with the host.
  2567. * @attr: Device attribute representing the number of hardware queues.
  2568. * @buf: Buffer of length PAGE_SIZE containing the number of hardware
  2569. * queues in ASCII.
  2570. * @count: Length of data resizing in @buf.
  2571. *
  2572. * n > 0: num_hwqs = n
  2573. * n = 0: num_hwqs = num_online_cpus()
  2574. * n < 0: num_online_cpus() / abs(n)
  2575. *
  2576. * Return: The size of the ASCII string returned in @buf.
  2577. */
  2578. static ssize_t num_hwqs_store(struct device *dev,
  2579. struct device_attribute *attr,
  2580. const char *buf, size_t count)
  2581. {
  2582. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2583. struct afu *afu = cfg->afu;
  2584. int rc;
  2585. int nhwqs, num_hwqs;
  2586. rc = kstrtoint(buf, 10, &nhwqs);
  2587. if (rc)
  2588. return -EINVAL;
  2589. if (nhwqs >= 1)
  2590. num_hwqs = nhwqs;
  2591. else if (nhwqs == 0)
  2592. num_hwqs = num_online_cpus();
  2593. else
  2594. num_hwqs = num_online_cpus() / abs(nhwqs);
  2595. afu->desired_hwqs = min(num_hwqs, CXLFLASH_MAX_HWQS);
  2596. WARN_ON_ONCE(afu->desired_hwqs == 0);
  2597. retry:
  2598. switch (cfg->state) {
  2599. case STATE_NORMAL:
  2600. cfg->state = STATE_RESET;
  2601. drain_ioctls(cfg);
  2602. cxlflash_mark_contexts_error(cfg);
  2603. rc = afu_reset(cfg);
  2604. if (rc)
  2605. cfg->state = STATE_FAILTERM;
  2606. else
  2607. cfg->state = STATE_NORMAL;
  2608. wake_up_all(&cfg->reset_waitq);
  2609. break;
  2610. case STATE_RESET:
  2611. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
  2612. if (cfg->state == STATE_NORMAL)
  2613. goto retry;
  2614. fallthrough;
  2615. default:
  2616. /* Ideally should not happen */
  2617. dev_err(dev, "%s: Device is not ready, state=%d\n",
  2618. __func__, cfg->state);
  2619. break;
  2620. }
  2621. return count;
  2622. }
  2623. static const char *hwq_mode_name[MAX_HWQ_MODE] = { "rr", "tag", "cpu" };
  2624. /**
  2625. * hwq_mode_show() - presents the HWQ steering mode for the host
  2626. * @dev: Generic device associated with the host.
  2627. * @attr: Device attribute representing the HWQ steering mode.
  2628. * @buf: Buffer of length PAGE_SIZE to report back the HWQ steering mode
  2629. * as a character string.
  2630. *
  2631. * Return: The size of the ASCII string returned in @buf.
  2632. */
  2633. static ssize_t hwq_mode_show(struct device *dev,
  2634. struct device_attribute *attr, char *buf)
  2635. {
  2636. struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
  2637. struct afu *afu = cfg->afu;
  2638. return scnprintf(buf, PAGE_SIZE, "%s\n", hwq_mode_name[afu->hwq_mode]);
  2639. }
  2640. /**
  2641. * hwq_mode_store() - sets the HWQ steering mode for the host
  2642. * @dev: Generic device associated with the host.
  2643. * @attr: Device attribute representing the HWQ steering mode.
  2644. * @buf: Buffer of length PAGE_SIZE containing the HWQ steering mode
  2645. * as a character string.
  2646. * @count: Length of data resizing in @buf.
  2647. *
  2648. * rr = Round-Robin
  2649. * tag = Block MQ Tagging
  2650. * cpu = CPU Affinity
  2651. *
  2652. * Return: The size of the ASCII string returned in @buf.
  2653. */
  2654. static ssize_t hwq_mode_store(struct device *dev,
  2655. struct device_attribute *attr,
  2656. const char *buf, size_t count)
  2657. {
  2658. struct Scsi_Host *shost = class_to_shost(dev);
  2659. struct cxlflash_cfg *cfg = shost_priv(shost);
  2660. struct device *cfgdev = &cfg->dev->dev;
  2661. struct afu *afu = cfg->afu;
  2662. int i;
  2663. u32 mode = MAX_HWQ_MODE;
  2664. for (i = 0; i < MAX_HWQ_MODE; i++) {
  2665. if (!strncmp(hwq_mode_name[i], buf, strlen(hwq_mode_name[i]))) {
  2666. mode = i;
  2667. break;
  2668. }
  2669. }
  2670. if (mode >= MAX_HWQ_MODE) {
  2671. dev_info(cfgdev, "Invalid HWQ steering mode.\n");
  2672. return -EINVAL;
  2673. }
  2674. afu->hwq_mode = mode;
  2675. return count;
  2676. }
  2677. /**
  2678. * mode_show() - presents the current mode of the device
  2679. * @dev: Generic device associated with the device.
  2680. * @attr: Device attribute representing the device mode.
  2681. * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
  2682. *
  2683. * Return: The size of the ASCII string returned in @buf.
  2684. */
  2685. static ssize_t mode_show(struct device *dev,
  2686. struct device_attribute *attr, char *buf)
  2687. {
  2688. struct scsi_device *sdev = to_scsi_device(dev);
  2689. return scnprintf(buf, PAGE_SIZE, "%s\n",
  2690. sdev->hostdata ? "superpipe" : "legacy");
  2691. }
  2692. /*
  2693. * Host attributes
  2694. */
  2695. static DEVICE_ATTR_RO(port0);
  2696. static DEVICE_ATTR_RO(port1);
  2697. static DEVICE_ATTR_RO(port2);
  2698. static DEVICE_ATTR_RO(port3);
  2699. static DEVICE_ATTR_RW(lun_mode);
  2700. static DEVICE_ATTR_RO(ioctl_version);
  2701. static DEVICE_ATTR_RO(port0_lun_table);
  2702. static DEVICE_ATTR_RO(port1_lun_table);
  2703. static DEVICE_ATTR_RO(port2_lun_table);
  2704. static DEVICE_ATTR_RO(port3_lun_table);
  2705. static DEVICE_ATTR_RW(irqpoll_weight);
  2706. static DEVICE_ATTR_RW(num_hwqs);
  2707. static DEVICE_ATTR_RW(hwq_mode);
  2708. static struct attribute *cxlflash_host_attrs[] = {
  2709. &dev_attr_port0.attr,
  2710. &dev_attr_port1.attr,
  2711. &dev_attr_port2.attr,
  2712. &dev_attr_port3.attr,
  2713. &dev_attr_lun_mode.attr,
  2714. &dev_attr_ioctl_version.attr,
  2715. &dev_attr_port0_lun_table.attr,
  2716. &dev_attr_port1_lun_table.attr,
  2717. &dev_attr_port2_lun_table.attr,
  2718. &dev_attr_port3_lun_table.attr,
  2719. &dev_attr_irqpoll_weight.attr,
  2720. &dev_attr_num_hwqs.attr,
  2721. &dev_attr_hwq_mode.attr,
  2722. NULL
  2723. };
  2724. ATTRIBUTE_GROUPS(cxlflash_host);
  2725. /*
  2726. * Device attributes
  2727. */
  2728. static DEVICE_ATTR_RO(mode);
  2729. static struct attribute *cxlflash_dev_attrs[] = {
  2730. &dev_attr_mode.attr,
  2731. NULL
  2732. };
  2733. ATTRIBUTE_GROUPS(cxlflash_dev);
  2734. /*
  2735. * Host template
  2736. */
  2737. static struct scsi_host_template driver_template = {
  2738. .module = THIS_MODULE,
  2739. .name = CXLFLASH_ADAPTER_NAME,
  2740. .info = cxlflash_driver_info,
  2741. .ioctl = cxlflash_ioctl,
  2742. .proc_name = CXLFLASH_NAME,
  2743. .queuecommand = cxlflash_queuecommand,
  2744. .eh_abort_handler = cxlflash_eh_abort_handler,
  2745. .eh_device_reset_handler = cxlflash_eh_device_reset_handler,
  2746. .eh_host_reset_handler = cxlflash_eh_host_reset_handler,
  2747. .change_queue_depth = cxlflash_change_queue_depth,
  2748. .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
  2749. .can_queue = CXLFLASH_MAX_CMDS,
  2750. .cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
  2751. .this_id = -1,
  2752. .sg_tablesize = 1, /* No scatter gather support */
  2753. .max_sectors = CXLFLASH_MAX_SECTORS,
  2754. .shost_groups = cxlflash_host_groups,
  2755. .sdev_groups = cxlflash_dev_groups,
  2756. };
  2757. /*
  2758. * Device dependent values
  2759. */
  2760. static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
  2761. CXLFLASH_WWPN_VPD_REQUIRED };
  2762. static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
  2763. CXLFLASH_NOTIFY_SHUTDOWN };
  2764. static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
  2765. (CXLFLASH_NOTIFY_SHUTDOWN |
  2766. CXLFLASH_OCXL_DEV) };
  2767. /*
  2768. * PCI device binding table
  2769. */
  2770. static struct pci_device_id cxlflash_pci_table[] = {
  2771. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
  2772. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
  2773. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
  2774. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
  2775. {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
  2776. PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
  2777. {}
  2778. };
  2779. MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
  2780. /**
  2781. * cxlflash_worker_thread() - work thread handler for the AFU
  2782. * @work: Work structure contained within cxlflash associated with host.
  2783. *
  2784. * Handles the following events:
  2785. * - Link reset which cannot be performed on interrupt context due to
  2786. * blocking up to a few seconds
  2787. * - Rescan the host
  2788. */
  2789. static void cxlflash_worker_thread(struct work_struct *work)
  2790. {
  2791. struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
  2792. work_q);
  2793. struct afu *afu = cfg->afu;
  2794. struct device *dev = &cfg->dev->dev;
  2795. __be64 __iomem *fc_port_regs;
  2796. int port;
  2797. ulong lock_flags;
  2798. /* Avoid MMIO if the device has failed */
  2799. if (cfg->state != STATE_NORMAL)
  2800. return;
  2801. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2802. if (cfg->lr_state == LINK_RESET_REQUIRED) {
  2803. port = cfg->lr_port;
  2804. if (port < 0)
  2805. dev_err(dev, "%s: invalid port index %d\n",
  2806. __func__, port);
  2807. else {
  2808. spin_unlock_irqrestore(cfg->host->host_lock,
  2809. lock_flags);
  2810. /* The reset can block... */
  2811. fc_port_regs = get_fc_port_regs(cfg, port);
  2812. afu_link_reset(afu, port, fc_port_regs);
  2813. spin_lock_irqsave(cfg->host->host_lock, lock_flags);
  2814. }
  2815. cfg->lr_state = LINK_RESET_COMPLETE;
  2816. }
  2817. spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
  2818. if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
  2819. scsi_scan_host(cfg->host);
  2820. }
  2821. /**
  2822. * cxlflash_chr_open() - character device open handler
  2823. * @inode: Device inode associated with this character device.
  2824. * @file: File pointer for this device.
  2825. *
  2826. * Only users with admin privileges are allowed to open the character device.
  2827. *
  2828. * Return: 0 on success, -errno on failure
  2829. */
  2830. static int cxlflash_chr_open(struct inode *inode, struct file *file)
  2831. {
  2832. struct cxlflash_cfg *cfg;
  2833. if (!capable(CAP_SYS_ADMIN))
  2834. return -EACCES;
  2835. cfg = container_of(inode->i_cdev, struct cxlflash_cfg, cdev);
  2836. file->private_data = cfg;
  2837. return 0;
  2838. }
  2839. /**
  2840. * decode_hioctl() - translates encoded host ioctl to easily identifiable string
  2841. * @cmd: The host ioctl command to decode.
  2842. *
  2843. * Return: A string identifying the decoded host ioctl.
  2844. */
  2845. static char *decode_hioctl(unsigned int cmd)
  2846. {
  2847. switch (cmd) {
  2848. case HT_CXLFLASH_LUN_PROVISION:
  2849. return __stringify_1(HT_CXLFLASH_LUN_PROVISION);
  2850. }
  2851. return "UNKNOWN";
  2852. }
  2853. /**
  2854. * cxlflash_lun_provision() - host LUN provisioning handler
  2855. * @cfg: Internal structure associated with the host.
  2856. * @lunprov: Kernel copy of userspace ioctl data structure.
  2857. *
  2858. * Return: 0 on success, -errno on failure
  2859. */
  2860. static int cxlflash_lun_provision(struct cxlflash_cfg *cfg,
  2861. struct ht_cxlflash_lun_provision *lunprov)
  2862. {
  2863. struct afu *afu = cfg->afu;
  2864. struct device *dev = &cfg->dev->dev;
  2865. struct sisl_ioarcb rcb;
  2866. struct sisl_ioasa asa;
  2867. __be64 __iomem *fc_port_regs;
  2868. u16 port = lunprov->port;
  2869. u16 scmd = lunprov->hdr.subcmd;
  2870. u16 type;
  2871. u64 reg;
  2872. u64 size;
  2873. u64 lun_id;
  2874. int rc = 0;
  2875. if (!afu_is_lun_provision(afu)) {
  2876. rc = -ENOTSUPP;
  2877. goto out;
  2878. }
  2879. if (port >= cfg->num_fc_ports) {
  2880. rc = -EINVAL;
  2881. goto out;
  2882. }
  2883. switch (scmd) {
  2884. case HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN:
  2885. type = SISL_AFU_LUN_PROVISION_CREATE;
  2886. size = lunprov->size;
  2887. lun_id = 0;
  2888. break;
  2889. case HT_CXLFLASH_LUN_PROVISION_SUBCMD_DELETE_LUN:
  2890. type = SISL_AFU_LUN_PROVISION_DELETE;
  2891. size = 0;
  2892. lun_id = lunprov->lun_id;
  2893. break;
  2894. case HT_CXLFLASH_LUN_PROVISION_SUBCMD_QUERY_PORT:
  2895. fc_port_regs = get_fc_port_regs(cfg, port);
  2896. reg = readq_be(&fc_port_regs[FC_MAX_NUM_LUNS / 8]);
  2897. lunprov->max_num_luns = reg;
  2898. reg = readq_be(&fc_port_regs[FC_CUR_NUM_LUNS / 8]);
  2899. lunprov->cur_num_luns = reg;
  2900. reg = readq_be(&fc_port_regs[FC_MAX_CAP_PORT / 8]);
  2901. lunprov->max_cap_port = reg;
  2902. reg = readq_be(&fc_port_regs[FC_CUR_CAP_PORT / 8]);
  2903. lunprov->cur_cap_port = reg;
  2904. goto out;
  2905. default:
  2906. rc = -EINVAL;
  2907. goto out;
  2908. }
  2909. memset(&rcb, 0, sizeof(rcb));
  2910. memset(&asa, 0, sizeof(asa));
  2911. rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
  2912. rcb.lun_id = lun_id;
  2913. rcb.msi = SISL_MSI_RRQ_UPDATED;
  2914. rcb.timeout = MC_LUN_PROV_TIMEOUT;
  2915. rcb.ioasa = &asa;
  2916. rcb.cdb[0] = SISL_AFU_CMD_LUN_PROVISION;
  2917. rcb.cdb[1] = type;
  2918. rcb.cdb[2] = port;
  2919. put_unaligned_be64(size, &rcb.cdb[8]);
  2920. rc = send_afu_cmd(afu, &rcb);
  2921. if (rc) {
  2922. dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
  2923. __func__, rc, asa.ioasc, asa.afu_extra);
  2924. goto out;
  2925. }
  2926. if (scmd == HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN) {
  2927. lunprov->lun_id = (u64)asa.lunid_hi << 32 | asa.lunid_lo;
  2928. memcpy(lunprov->wwid, asa.wwid, sizeof(lunprov->wwid));
  2929. }
  2930. out:
  2931. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  2932. return rc;
  2933. }
  2934. /**
  2935. * cxlflash_afu_debug() - host AFU debug handler
  2936. * @cfg: Internal structure associated with the host.
  2937. * @afu_dbg: Kernel copy of userspace ioctl data structure.
  2938. *
  2939. * For debug requests requiring a data buffer, always provide an aligned
  2940. * (cache line) buffer to the AFU to appease any alignment requirements.
  2941. *
  2942. * Return: 0 on success, -errno on failure
  2943. */
  2944. static int cxlflash_afu_debug(struct cxlflash_cfg *cfg,
  2945. struct ht_cxlflash_afu_debug *afu_dbg)
  2946. {
  2947. struct afu *afu = cfg->afu;
  2948. struct device *dev = &cfg->dev->dev;
  2949. struct sisl_ioarcb rcb;
  2950. struct sisl_ioasa asa;
  2951. char *buf = NULL;
  2952. char *kbuf = NULL;
  2953. void __user *ubuf = (__force void __user *)afu_dbg->data_ea;
  2954. u16 req_flags = SISL_REQ_FLAGS_AFU_CMD;
  2955. u32 ulen = afu_dbg->data_len;
  2956. bool is_write = afu_dbg->hdr.flags & HT_CXLFLASH_HOST_WRITE;
  2957. int rc = 0;
  2958. if (!afu_is_afu_debug(afu)) {
  2959. rc = -ENOTSUPP;
  2960. goto out;
  2961. }
  2962. if (ulen) {
  2963. req_flags |= SISL_REQ_FLAGS_SUP_UNDERRUN;
  2964. if (ulen > HT_CXLFLASH_AFU_DEBUG_MAX_DATA_LEN) {
  2965. rc = -EINVAL;
  2966. goto out;
  2967. }
  2968. buf = kmalloc(ulen + cache_line_size() - 1, GFP_KERNEL);
  2969. if (unlikely(!buf)) {
  2970. rc = -ENOMEM;
  2971. goto out;
  2972. }
  2973. kbuf = PTR_ALIGN(buf, cache_line_size());
  2974. if (is_write) {
  2975. req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
  2976. if (copy_from_user(kbuf, ubuf, ulen)) {
  2977. rc = -EFAULT;
  2978. goto out;
  2979. }
  2980. }
  2981. }
  2982. memset(&rcb, 0, sizeof(rcb));
  2983. memset(&asa, 0, sizeof(asa));
  2984. rcb.req_flags = req_flags;
  2985. rcb.msi = SISL_MSI_RRQ_UPDATED;
  2986. rcb.timeout = MC_AFU_DEBUG_TIMEOUT;
  2987. rcb.ioasa = &asa;
  2988. if (ulen) {
  2989. rcb.data_len = ulen;
  2990. rcb.data_ea = (uintptr_t)kbuf;
  2991. }
  2992. rcb.cdb[0] = SISL_AFU_CMD_DEBUG;
  2993. memcpy(&rcb.cdb[4], afu_dbg->afu_subcmd,
  2994. HT_CXLFLASH_AFU_DEBUG_SUBCMD_LEN);
  2995. rc = send_afu_cmd(afu, &rcb);
  2996. if (rc) {
  2997. dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
  2998. __func__, rc, asa.ioasc, asa.afu_extra);
  2999. goto out;
  3000. }
  3001. if (ulen && !is_write) {
  3002. if (copy_to_user(ubuf, kbuf, ulen))
  3003. rc = -EFAULT;
  3004. }
  3005. out:
  3006. kfree(buf);
  3007. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  3008. return rc;
  3009. }
  3010. /**
  3011. * cxlflash_chr_ioctl() - character device IOCTL handler
  3012. * @file: File pointer for this device.
  3013. * @cmd: IOCTL command.
  3014. * @arg: Userspace ioctl data structure.
  3015. *
  3016. * A read/write semaphore is used to implement a 'drain' of currently
  3017. * running ioctls. The read semaphore is taken at the beginning of each
  3018. * ioctl thread and released upon concluding execution. Additionally the
  3019. * semaphore should be released and then reacquired in any ioctl execution
  3020. * path which will wait for an event to occur that is outside the scope of
  3021. * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
  3022. * a thread simply needs to acquire the write semaphore.
  3023. *
  3024. * Return: 0 on success, -errno on failure
  3025. */
  3026. static long cxlflash_chr_ioctl(struct file *file, unsigned int cmd,
  3027. unsigned long arg)
  3028. {
  3029. typedef int (*hioctl) (struct cxlflash_cfg *, void *);
  3030. struct cxlflash_cfg *cfg = file->private_data;
  3031. struct device *dev = &cfg->dev->dev;
  3032. char buf[sizeof(union cxlflash_ht_ioctls)];
  3033. void __user *uarg = (void __user *)arg;
  3034. struct ht_cxlflash_hdr *hdr;
  3035. size_t size = 0;
  3036. bool known_ioctl = false;
  3037. int idx = 0;
  3038. int rc = 0;
  3039. hioctl do_ioctl = NULL;
  3040. static const struct {
  3041. size_t size;
  3042. hioctl ioctl;
  3043. } ioctl_tbl[] = { /* NOTE: order matters here */
  3044. { sizeof(struct ht_cxlflash_lun_provision),
  3045. (hioctl)cxlflash_lun_provision },
  3046. { sizeof(struct ht_cxlflash_afu_debug),
  3047. (hioctl)cxlflash_afu_debug },
  3048. };
  3049. /* Hold read semaphore so we can drain if needed */
  3050. down_read(&cfg->ioctl_rwsem);
  3051. dev_dbg(dev, "%s: cmd=%u idx=%d tbl_size=%lu\n",
  3052. __func__, cmd, idx, sizeof(ioctl_tbl));
  3053. switch (cmd) {
  3054. case HT_CXLFLASH_LUN_PROVISION:
  3055. case HT_CXLFLASH_AFU_DEBUG:
  3056. known_ioctl = true;
  3057. idx = _IOC_NR(HT_CXLFLASH_LUN_PROVISION) - _IOC_NR(cmd);
  3058. size = ioctl_tbl[idx].size;
  3059. do_ioctl = ioctl_tbl[idx].ioctl;
  3060. if (likely(do_ioctl))
  3061. break;
  3062. fallthrough;
  3063. default:
  3064. rc = -EINVAL;
  3065. goto out;
  3066. }
  3067. if (unlikely(copy_from_user(&buf, uarg, size))) {
  3068. dev_err(dev, "%s: copy_from_user() fail "
  3069. "size=%lu cmd=%d (%s) uarg=%p\n",
  3070. __func__, size, cmd, decode_hioctl(cmd), uarg);
  3071. rc = -EFAULT;
  3072. goto out;
  3073. }
  3074. hdr = (struct ht_cxlflash_hdr *)&buf;
  3075. if (hdr->version != HT_CXLFLASH_VERSION_0) {
  3076. dev_dbg(dev, "%s: Version %u not supported for %s\n",
  3077. __func__, hdr->version, decode_hioctl(cmd));
  3078. rc = -EINVAL;
  3079. goto out;
  3080. }
  3081. if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->return_flags) {
  3082. dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
  3083. rc = -EINVAL;
  3084. goto out;
  3085. }
  3086. rc = do_ioctl(cfg, (void *)&buf);
  3087. if (likely(!rc))
  3088. if (unlikely(copy_to_user(uarg, &buf, size))) {
  3089. dev_err(dev, "%s: copy_to_user() fail "
  3090. "size=%lu cmd=%d (%s) uarg=%p\n",
  3091. __func__, size, cmd, decode_hioctl(cmd), uarg);
  3092. rc = -EFAULT;
  3093. }
  3094. /* fall through to exit */
  3095. out:
  3096. up_read(&cfg->ioctl_rwsem);
  3097. if (unlikely(rc && known_ioctl))
  3098. dev_err(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
  3099. __func__, decode_hioctl(cmd), cmd, rc);
  3100. else
  3101. dev_dbg(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
  3102. __func__, decode_hioctl(cmd), cmd, rc);
  3103. return rc;
  3104. }
  3105. /*
  3106. * Character device file operations
  3107. */
  3108. static const struct file_operations cxlflash_chr_fops = {
  3109. .owner = THIS_MODULE,
  3110. .open = cxlflash_chr_open,
  3111. .unlocked_ioctl = cxlflash_chr_ioctl,
  3112. .compat_ioctl = compat_ptr_ioctl,
  3113. };
  3114. /**
  3115. * init_chrdev() - initialize the character device for the host
  3116. * @cfg: Internal structure associated with the host.
  3117. *
  3118. * Return: 0 on success, -errno on failure
  3119. */
  3120. static int init_chrdev(struct cxlflash_cfg *cfg)
  3121. {
  3122. struct device *dev = &cfg->dev->dev;
  3123. struct device *char_dev;
  3124. dev_t devno;
  3125. int minor;
  3126. int rc = 0;
  3127. minor = cxlflash_get_minor();
  3128. if (unlikely(minor < 0)) {
  3129. dev_err(dev, "%s: Exhausted allowed adapters\n", __func__);
  3130. rc = -ENOSPC;
  3131. goto out;
  3132. }
  3133. devno = MKDEV(cxlflash_major, minor);
  3134. cdev_init(&cfg->cdev, &cxlflash_chr_fops);
  3135. rc = cdev_add(&cfg->cdev, devno, 1);
  3136. if (rc) {
  3137. dev_err(dev, "%s: cdev_add failed rc=%d\n", __func__, rc);
  3138. goto err1;
  3139. }
  3140. char_dev = device_create(cxlflash_class, NULL, devno,
  3141. NULL, "cxlflash%d", minor);
  3142. if (IS_ERR(char_dev)) {
  3143. rc = PTR_ERR(char_dev);
  3144. dev_err(dev, "%s: device_create failed rc=%d\n",
  3145. __func__, rc);
  3146. goto err2;
  3147. }
  3148. cfg->chardev = char_dev;
  3149. out:
  3150. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  3151. return rc;
  3152. err2:
  3153. cdev_del(&cfg->cdev);
  3154. err1:
  3155. cxlflash_put_minor(minor);
  3156. goto out;
  3157. }
  3158. /**
  3159. * cxlflash_probe() - PCI entry point to add host
  3160. * @pdev: PCI device associated with the host.
  3161. * @dev_id: PCI device id associated with device.
  3162. *
  3163. * The device will initially start out in a 'probing' state and
  3164. * transition to the 'normal' state at the end of a successful
  3165. * probe. Should an EEH event occur during probe, the notification
  3166. * thread (error_detected()) will wait until the probe handler
  3167. * is nearly complete. At that time, the device will be moved to
  3168. * a 'probed' state and the EEH thread woken up to drive the slot
  3169. * reset and recovery (device moves to 'normal' state). Meanwhile,
  3170. * the probe will be allowed to exit successfully.
  3171. *
  3172. * Return: 0 on success, -errno on failure
  3173. */
  3174. static int cxlflash_probe(struct pci_dev *pdev,
  3175. const struct pci_device_id *dev_id)
  3176. {
  3177. struct Scsi_Host *host;
  3178. struct cxlflash_cfg *cfg = NULL;
  3179. struct device *dev = &pdev->dev;
  3180. struct dev_dependent_vals *ddv;
  3181. int rc = 0;
  3182. int k;
  3183. dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
  3184. __func__, pdev->irq);
  3185. ddv = (struct dev_dependent_vals *)dev_id->driver_data;
  3186. driver_template.max_sectors = ddv->max_sectors;
  3187. host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
  3188. if (!host) {
  3189. dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
  3190. rc = -ENOMEM;
  3191. goto out;
  3192. }
  3193. host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
  3194. host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
  3195. host->unique_id = host->host_no;
  3196. host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
  3197. cfg = shost_priv(host);
  3198. cfg->state = STATE_PROBING;
  3199. cfg->host = host;
  3200. rc = alloc_mem(cfg);
  3201. if (rc) {
  3202. dev_err(dev, "%s: alloc_mem failed\n", __func__);
  3203. rc = -ENOMEM;
  3204. scsi_host_put(cfg->host);
  3205. goto out;
  3206. }
  3207. cfg->init_state = INIT_STATE_NONE;
  3208. cfg->dev = pdev;
  3209. cfg->cxl_fops = cxlflash_cxl_fops;
  3210. cfg->ops = cxlflash_assign_ops(ddv);
  3211. WARN_ON_ONCE(!cfg->ops);
  3212. /*
  3213. * Promoted LUNs move to the top of the LUN table. The rest stay on
  3214. * the bottom half. The bottom half grows from the end (index = 255),
  3215. * whereas the top half grows from the beginning (index = 0).
  3216. *
  3217. * Initialize the last LUN index for all possible ports.
  3218. */
  3219. cfg->promote_lun_index = 0;
  3220. for (k = 0; k < MAX_FC_PORTS; k++)
  3221. cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
  3222. cfg->dev_id = (struct pci_device_id *)dev_id;
  3223. init_waitqueue_head(&cfg->tmf_waitq);
  3224. init_waitqueue_head(&cfg->reset_waitq);
  3225. INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
  3226. cfg->lr_state = LINK_RESET_INVALID;
  3227. cfg->lr_port = -1;
  3228. spin_lock_init(&cfg->tmf_slock);
  3229. mutex_init(&cfg->ctx_tbl_list_mutex);
  3230. mutex_init(&cfg->ctx_recovery_mutex);
  3231. init_rwsem(&cfg->ioctl_rwsem);
  3232. INIT_LIST_HEAD(&cfg->ctx_err_recovery);
  3233. INIT_LIST_HEAD(&cfg->lluns);
  3234. pci_set_drvdata(pdev, cfg);
  3235. rc = init_pci(cfg);
  3236. if (rc) {
  3237. dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
  3238. goto out_remove;
  3239. }
  3240. cfg->init_state = INIT_STATE_PCI;
  3241. cfg->afu_cookie = cfg->ops->create_afu(pdev);
  3242. if (unlikely(!cfg->afu_cookie)) {
  3243. dev_err(dev, "%s: create_afu failed\n", __func__);
  3244. rc = -ENOMEM;
  3245. goto out_remove;
  3246. }
  3247. rc = init_afu(cfg);
  3248. if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
  3249. dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
  3250. goto out_remove;
  3251. }
  3252. cfg->init_state = INIT_STATE_AFU;
  3253. rc = init_scsi(cfg);
  3254. if (rc) {
  3255. dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
  3256. goto out_remove;
  3257. }
  3258. cfg->init_state = INIT_STATE_SCSI;
  3259. rc = init_chrdev(cfg);
  3260. if (rc) {
  3261. dev_err(dev, "%s: init_chrdev failed rc=%d\n", __func__, rc);
  3262. goto out_remove;
  3263. }
  3264. cfg->init_state = INIT_STATE_CDEV;
  3265. if (wq_has_sleeper(&cfg->reset_waitq)) {
  3266. cfg->state = STATE_PROBED;
  3267. wake_up_all(&cfg->reset_waitq);
  3268. } else
  3269. cfg->state = STATE_NORMAL;
  3270. out:
  3271. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  3272. return rc;
  3273. out_remove:
  3274. cfg->state = STATE_PROBED;
  3275. cxlflash_remove(pdev);
  3276. goto out;
  3277. }
  3278. /**
  3279. * cxlflash_pci_error_detected() - called when a PCI error is detected
  3280. * @pdev: PCI device struct.
  3281. * @state: PCI channel state.
  3282. *
  3283. * When an EEH occurs during an active reset, wait until the reset is
  3284. * complete and then take action based upon the device state.
  3285. *
  3286. * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  3287. */
  3288. static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
  3289. pci_channel_state_t state)
  3290. {
  3291. int rc = 0;
  3292. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  3293. struct device *dev = &cfg->dev->dev;
  3294. dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
  3295. switch (state) {
  3296. case pci_channel_io_frozen:
  3297. wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
  3298. cfg->state != STATE_PROBING);
  3299. if (cfg->state == STATE_FAILTERM)
  3300. return PCI_ERS_RESULT_DISCONNECT;
  3301. cfg->state = STATE_RESET;
  3302. scsi_block_requests(cfg->host);
  3303. drain_ioctls(cfg);
  3304. rc = cxlflash_mark_contexts_error(cfg);
  3305. if (unlikely(rc))
  3306. dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
  3307. __func__, rc);
  3308. term_afu(cfg);
  3309. return PCI_ERS_RESULT_NEED_RESET;
  3310. case pci_channel_io_perm_failure:
  3311. cfg->state = STATE_FAILTERM;
  3312. wake_up_all(&cfg->reset_waitq);
  3313. scsi_unblock_requests(cfg->host);
  3314. return PCI_ERS_RESULT_DISCONNECT;
  3315. default:
  3316. break;
  3317. }
  3318. return PCI_ERS_RESULT_NEED_RESET;
  3319. }
  3320. /**
  3321. * cxlflash_pci_slot_reset() - called when PCI slot has been reset
  3322. * @pdev: PCI device struct.
  3323. *
  3324. * This routine is called by the pci error recovery code after the PCI
  3325. * slot has been reset, just before we should resume normal operations.
  3326. *
  3327. * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
  3328. */
  3329. static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
  3330. {
  3331. int rc = 0;
  3332. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  3333. struct device *dev = &cfg->dev->dev;
  3334. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  3335. rc = init_afu(cfg);
  3336. if (unlikely(rc)) {
  3337. dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
  3338. return PCI_ERS_RESULT_DISCONNECT;
  3339. }
  3340. return PCI_ERS_RESULT_RECOVERED;
  3341. }
  3342. /**
  3343. * cxlflash_pci_resume() - called when normal operation can resume
  3344. * @pdev: PCI device struct
  3345. */
  3346. static void cxlflash_pci_resume(struct pci_dev *pdev)
  3347. {
  3348. struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
  3349. struct device *dev = &cfg->dev->dev;
  3350. dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
  3351. cfg->state = STATE_NORMAL;
  3352. wake_up_all(&cfg->reset_waitq);
  3353. scsi_unblock_requests(cfg->host);
  3354. }
  3355. /**
  3356. * cxlflash_devnode() - provides devtmpfs for devices in the cxlflash class
  3357. * @dev: Character device.
  3358. * @mode: Mode that can be used to verify access.
  3359. *
  3360. * Return: Allocated string describing the devtmpfs structure.
  3361. */
  3362. static char *cxlflash_devnode(struct device *dev, umode_t *mode)
  3363. {
  3364. return kasprintf(GFP_KERNEL, "cxlflash/%s", dev_name(dev));
  3365. }
  3366. /**
  3367. * cxlflash_class_init() - create character device class
  3368. *
  3369. * Return: 0 on success, -errno on failure
  3370. */
  3371. static int cxlflash_class_init(void)
  3372. {
  3373. dev_t devno;
  3374. int rc = 0;
  3375. rc = alloc_chrdev_region(&devno, 0, CXLFLASH_MAX_ADAPTERS, "cxlflash");
  3376. if (unlikely(rc)) {
  3377. pr_err("%s: alloc_chrdev_region failed rc=%d\n", __func__, rc);
  3378. goto out;
  3379. }
  3380. cxlflash_major = MAJOR(devno);
  3381. cxlflash_class = class_create(THIS_MODULE, "cxlflash");
  3382. if (IS_ERR(cxlflash_class)) {
  3383. rc = PTR_ERR(cxlflash_class);
  3384. pr_err("%s: class_create failed rc=%d\n", __func__, rc);
  3385. goto err;
  3386. }
  3387. cxlflash_class->devnode = cxlflash_devnode;
  3388. out:
  3389. pr_debug("%s: returning rc=%d\n", __func__, rc);
  3390. return rc;
  3391. err:
  3392. unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
  3393. goto out;
  3394. }
  3395. /**
  3396. * cxlflash_class_exit() - destroy character device class
  3397. */
  3398. static void cxlflash_class_exit(void)
  3399. {
  3400. dev_t devno = MKDEV(cxlflash_major, 0);
  3401. class_destroy(cxlflash_class);
  3402. unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
  3403. }
  3404. static const struct pci_error_handlers cxlflash_err_handler = {
  3405. .error_detected = cxlflash_pci_error_detected,
  3406. .slot_reset = cxlflash_pci_slot_reset,
  3407. .resume = cxlflash_pci_resume,
  3408. };
  3409. /*
  3410. * PCI device structure
  3411. */
  3412. static struct pci_driver cxlflash_driver = {
  3413. .name = CXLFLASH_NAME,
  3414. .id_table = cxlflash_pci_table,
  3415. .probe = cxlflash_probe,
  3416. .remove = cxlflash_remove,
  3417. .shutdown = cxlflash_remove,
  3418. .err_handler = &cxlflash_err_handler,
  3419. };
  3420. /**
  3421. * init_cxlflash() - module entry point
  3422. *
  3423. * Return: 0 on success, -errno on failure
  3424. */
  3425. static int __init init_cxlflash(void)
  3426. {
  3427. int rc;
  3428. check_sizes();
  3429. cxlflash_list_init();
  3430. rc = cxlflash_class_init();
  3431. if (unlikely(rc))
  3432. goto out;
  3433. rc = pci_register_driver(&cxlflash_driver);
  3434. if (unlikely(rc))
  3435. goto err;
  3436. out:
  3437. pr_debug("%s: returning rc=%d\n", __func__, rc);
  3438. return rc;
  3439. err:
  3440. cxlflash_class_exit();
  3441. goto out;
  3442. }
  3443. /**
  3444. * exit_cxlflash() - module exit point
  3445. */
  3446. static void __exit exit_cxlflash(void)
  3447. {
  3448. cxlflash_term_global_luns();
  3449. cxlflash_free_errpage();
  3450. pci_unregister_driver(&cxlflash_driver);
  3451. cxlflash_class_exit();
  3452. }
  3453. module_init(init_cxlflash);
  3454. module_exit(exit_cxlflash);