main.c 107 KB

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