ibmvfc.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  4. *
  5. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  6. *
  7. * Copyright (C) IBM Corporation, 2008
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/kthread.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/pm.h>
  20. #include <linux/stringify.h>
  21. #include <linux/bsg-lib.h>
  22. #include <asm/firmware.h>
  23. #include <asm/irq.h>
  24. #include <asm/vio.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/scsi_bsg_fc.h>
  32. #include "ibmvfc.h"
  33. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  34. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  35. static u64 max_lun = IBMVFC_MAX_LUN;
  36. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  37. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  38. static u16 max_sectors = IBMVFC_MAX_SECTORS;
  39. static u16 scsi_qdepth = IBMVFC_SCSI_QDEPTH;
  40. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  41. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  42. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  43. static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
  44. static unsigned int mq_enabled = IBMVFC_MQ;
  45. static unsigned int nr_scsi_hw_queues = IBMVFC_SCSI_HW_QUEUES;
  46. static unsigned int nr_scsi_channels = IBMVFC_SCSI_CHANNELS;
  47. static unsigned int mig_channels_only = IBMVFC_MIG_NO_SUB_TO_CRQ;
  48. static unsigned int mig_no_less_channels = IBMVFC_MIG_NO_N_TO_M;
  49. static LIST_HEAD(ibmvfc_head);
  50. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  51. static struct scsi_transport_template *ibmvfc_transport_template;
  52. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  53. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  54. MODULE_LICENSE("GPL");
  55. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  56. module_param_named(mq, mq_enabled, uint, S_IRUGO);
  57. MODULE_PARM_DESC(mq, "Enable multiqueue support. "
  58. "[Default=" __stringify(IBMVFC_MQ) "]");
  59. module_param_named(scsi_host_queues, nr_scsi_hw_queues, uint, S_IRUGO);
  60. MODULE_PARM_DESC(scsi_host_queues, "Number of SCSI Host submission queues. "
  61. "[Default=" __stringify(IBMVFC_SCSI_HW_QUEUES) "]");
  62. module_param_named(scsi_hw_channels, nr_scsi_channels, uint, S_IRUGO);
  63. MODULE_PARM_DESC(scsi_hw_channels, "Number of hw scsi channels to request. "
  64. "[Default=" __stringify(IBMVFC_SCSI_CHANNELS) "]");
  65. module_param_named(mig_channels_only, mig_channels_only, uint, S_IRUGO);
  66. MODULE_PARM_DESC(mig_channels_only, "Prevent migration to non-channelized system. "
  67. "[Default=" __stringify(IBMVFC_MIG_NO_SUB_TO_CRQ) "]");
  68. module_param_named(mig_no_less_channels, mig_no_less_channels, uint, S_IRUGO);
  69. MODULE_PARM_DESC(mig_no_less_channels, "Prevent migration to system with less channels. "
  70. "[Default=" __stringify(IBMVFC_MIG_NO_N_TO_M) "]");
  71. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  72. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  73. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  74. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  75. MODULE_PARM_DESC(default_timeout,
  76. "Default timeout in seconds for initialization and EH commands. "
  77. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  78. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  79. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  80. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  81. module_param_named(max_sectors, max_sectors, ushort, S_IRUGO);
  82. MODULE_PARM_DESC(max_sectors, "Maximum sectors for this adapter. "
  83. "[Default=" __stringify(IBMVFC_MAX_SECTORS) "]");
  84. module_param_named(scsi_qdepth, scsi_qdepth, ushort, S_IRUGO);
  85. MODULE_PARM_DESC(scsi_qdepth, "Maximum scsi command depth per adapter queue. "
  86. "[Default=" __stringify(IBMVFC_SCSI_QDEPTH) "]");
  87. module_param_named(max_lun, max_lun, ullong, S_IRUGO);
  88. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  89. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  90. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  91. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  92. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  93. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  94. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  95. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  96. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  98. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  99. module_param_named(log_level, log_level, uint, 0);
  100. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  101. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  102. module_param_named(cls3_error, cls3_error, uint, 0);
  103. MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
  104. "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
  105. static const struct {
  106. u16 status;
  107. u16 error;
  108. u8 result;
  109. u8 retry;
  110. int log;
  111. char *name;
  112. } cmd_status [] = {
  113. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  114. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  115. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  116. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  117. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  118. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  119. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  120. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  121. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  122. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  123. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  124. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  125. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  126. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  127. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  128. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  129. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  130. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  131. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  132. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  133. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  134. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  135. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  136. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  137. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  138. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  139. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  140. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  141. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  142. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  143. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  144. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  145. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  146. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  147. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  148. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  149. { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
  150. };
  151. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  152. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  153. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  154. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  155. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  156. static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
  157. static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
  158. static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
  159. static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *, struct ibmvfc_channels *);
  160. static const char *unknown_error = "unknown error";
  161. static long h_reg_sub_crq(unsigned long unit_address, unsigned long ioba,
  162. unsigned long length, unsigned long *cookie,
  163. unsigned long *irq)
  164. {
  165. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  166. long rc;
  167. rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, ioba, length);
  168. *cookie = retbuf[0];
  169. *irq = retbuf[1];
  170. return rc;
  171. }
  172. static int ibmvfc_check_caps(struct ibmvfc_host *vhost, unsigned long cap_flags)
  173. {
  174. u64 host_caps = be64_to_cpu(vhost->login_buf->resp.capabilities);
  175. return (host_caps & cap_flags) ? 1 : 0;
  176. }
  177. static struct ibmvfc_fcp_cmd_iu *ibmvfc_get_fcp_iu(struct ibmvfc_host *vhost,
  178. struct ibmvfc_cmd *vfc_cmd)
  179. {
  180. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  181. return &vfc_cmd->v2.iu;
  182. else
  183. return &vfc_cmd->v1.iu;
  184. }
  185. static struct ibmvfc_fcp_rsp *ibmvfc_get_fcp_rsp(struct ibmvfc_host *vhost,
  186. struct ibmvfc_cmd *vfc_cmd)
  187. {
  188. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  189. return &vfc_cmd->v2.rsp;
  190. else
  191. return &vfc_cmd->v1.rsp;
  192. }
  193. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  194. /**
  195. * ibmvfc_trc_start - Log a start trace entry
  196. * @evt: ibmvfc event struct
  197. *
  198. **/
  199. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  200. {
  201. struct ibmvfc_host *vhost = evt->vhost;
  202. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  203. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  204. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  205. struct ibmvfc_trace_entry *entry;
  206. int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
  207. entry = &vhost->trace[index];
  208. entry->evt = evt;
  209. entry->time = jiffies;
  210. entry->fmt = evt->crq.format;
  211. entry->type = IBMVFC_TRC_START;
  212. switch (entry->fmt) {
  213. case IBMVFC_CMD_FORMAT:
  214. entry->op_code = iu->cdb[0];
  215. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  216. entry->lun = scsilun_to_int(&iu->lun);
  217. entry->tmf_flags = iu->tmf_flags;
  218. entry->u.start.xfer_len = be32_to_cpu(iu->xfer_len);
  219. break;
  220. case IBMVFC_MAD_FORMAT:
  221. entry->op_code = be32_to_cpu(mad->opcode);
  222. break;
  223. default:
  224. break;
  225. }
  226. }
  227. /**
  228. * ibmvfc_trc_end - Log an end trace entry
  229. * @evt: ibmvfc event struct
  230. *
  231. **/
  232. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  233. {
  234. struct ibmvfc_host *vhost = evt->vhost;
  235. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  236. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  237. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  238. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  239. struct ibmvfc_trace_entry *entry;
  240. int index = atomic_inc_return(&vhost->trace_index) & IBMVFC_TRACE_INDEX_MASK;
  241. entry = &vhost->trace[index];
  242. entry->evt = evt;
  243. entry->time = jiffies;
  244. entry->fmt = evt->crq.format;
  245. entry->type = IBMVFC_TRC_END;
  246. switch (entry->fmt) {
  247. case IBMVFC_CMD_FORMAT:
  248. entry->op_code = iu->cdb[0];
  249. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  250. entry->lun = scsilun_to_int(&iu->lun);
  251. entry->tmf_flags = iu->tmf_flags;
  252. entry->u.end.status = be16_to_cpu(vfc_cmd->status);
  253. entry->u.end.error = be16_to_cpu(vfc_cmd->error);
  254. entry->u.end.fcp_rsp_flags = rsp->flags;
  255. entry->u.end.rsp_code = rsp->data.info.rsp_code;
  256. entry->u.end.scsi_status = rsp->scsi_status;
  257. break;
  258. case IBMVFC_MAD_FORMAT:
  259. entry->op_code = be32_to_cpu(mad->opcode);
  260. entry->u.end.status = be16_to_cpu(mad->status);
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. #else
  267. #define ibmvfc_trc_start(evt) do { } while (0)
  268. #define ibmvfc_trc_end(evt) do { } while (0)
  269. #endif
  270. /**
  271. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  272. * @status: status / error class
  273. * @error: error
  274. *
  275. * Return value:
  276. * index into cmd_status / -EINVAL on failure
  277. **/
  278. static int ibmvfc_get_err_index(u16 status, u16 error)
  279. {
  280. int i;
  281. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  282. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  283. cmd_status[i].error == error)
  284. return i;
  285. return -EINVAL;
  286. }
  287. /**
  288. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  289. * @status: status / error class
  290. * @error: error
  291. *
  292. * Return value:
  293. * error description string
  294. **/
  295. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  296. {
  297. int rc = ibmvfc_get_err_index(status, error);
  298. if (rc >= 0)
  299. return cmd_status[rc].name;
  300. return unknown_error;
  301. }
  302. /**
  303. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  304. * @vhost: ibmvfc host struct
  305. * @vfc_cmd: ibmvfc command struct
  306. *
  307. * Return value:
  308. * SCSI result value to return for completed command
  309. **/
  310. static int ibmvfc_get_err_result(struct ibmvfc_host *vhost, struct ibmvfc_cmd *vfc_cmd)
  311. {
  312. int err;
  313. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  314. int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  315. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  316. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  317. rsp->data.info.rsp_code))
  318. return DID_ERROR << 16;
  319. err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  320. if (err >= 0)
  321. return rsp->scsi_status | (cmd_status[err].result << 16);
  322. return rsp->scsi_status | (DID_ERROR << 16);
  323. }
  324. /**
  325. * ibmvfc_retry_cmd - Determine if error status is retryable
  326. * @status: status / error class
  327. * @error: error
  328. *
  329. * Return value:
  330. * 1 if error should be retried / 0 if it should not
  331. **/
  332. static int ibmvfc_retry_cmd(u16 status, u16 error)
  333. {
  334. int rc = ibmvfc_get_err_index(status, error);
  335. if (rc >= 0)
  336. return cmd_status[rc].retry;
  337. return 1;
  338. }
  339. static const char *unknown_fc_explain = "unknown fc explain";
  340. static const struct {
  341. u16 fc_explain;
  342. char *name;
  343. } ls_explain [] = {
  344. { 0x00, "no additional explanation" },
  345. { 0x01, "service parameter error - options" },
  346. { 0x03, "service parameter error - initiator control" },
  347. { 0x05, "service parameter error - recipient control" },
  348. { 0x07, "service parameter error - received data field size" },
  349. { 0x09, "service parameter error - concurrent seq" },
  350. { 0x0B, "service parameter error - credit" },
  351. { 0x0D, "invalid N_Port/F_Port_Name" },
  352. { 0x0E, "invalid node/Fabric Name" },
  353. { 0x0F, "invalid common service parameters" },
  354. { 0x11, "invalid association header" },
  355. { 0x13, "association header required" },
  356. { 0x15, "invalid originator S_ID" },
  357. { 0x17, "invalid OX_ID-RX-ID combination" },
  358. { 0x19, "command (request) already in progress" },
  359. { 0x1E, "N_Port Login requested" },
  360. { 0x1F, "Invalid N_Port_ID" },
  361. };
  362. static const struct {
  363. u16 fc_explain;
  364. char *name;
  365. } gs_explain [] = {
  366. { 0x00, "no additional explanation" },
  367. { 0x01, "port identifier not registered" },
  368. { 0x02, "port name not registered" },
  369. { 0x03, "node name not registered" },
  370. { 0x04, "class of service not registered" },
  371. { 0x06, "initial process associator not registered" },
  372. { 0x07, "FC-4 TYPEs not registered" },
  373. { 0x08, "symbolic port name not registered" },
  374. { 0x09, "symbolic node name not registered" },
  375. { 0x0A, "port type not registered" },
  376. { 0xF0, "authorization exception" },
  377. { 0xF1, "authentication exception" },
  378. { 0xF2, "data base full" },
  379. { 0xF3, "data base empty" },
  380. { 0xF4, "processing request" },
  381. { 0xF5, "unable to verify connection" },
  382. { 0xF6, "devices not in a common zone" },
  383. };
  384. /**
  385. * ibmvfc_get_ls_explain - Return the FC Explain description text
  386. * @status: FC Explain status
  387. *
  388. * Returns:
  389. * error string
  390. **/
  391. static const char *ibmvfc_get_ls_explain(u16 status)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  395. if (ls_explain[i].fc_explain == status)
  396. return ls_explain[i].name;
  397. return unknown_fc_explain;
  398. }
  399. /**
  400. * ibmvfc_get_gs_explain - Return the FC Explain description text
  401. * @status: FC Explain status
  402. *
  403. * Returns:
  404. * error string
  405. **/
  406. static const char *ibmvfc_get_gs_explain(u16 status)
  407. {
  408. int i;
  409. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  410. if (gs_explain[i].fc_explain == status)
  411. return gs_explain[i].name;
  412. return unknown_fc_explain;
  413. }
  414. static const struct {
  415. enum ibmvfc_fc_type fc_type;
  416. char *name;
  417. } fc_type [] = {
  418. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  419. { IBMVFC_PORT_REJECT, "port reject" },
  420. { IBMVFC_LS_REJECT, "ELS reject" },
  421. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  422. { IBMVFC_PORT_BUSY, "port busy" },
  423. { IBMVFC_BASIC_REJECT, "basic reject" },
  424. };
  425. static const char *unknown_fc_type = "unknown fc type";
  426. /**
  427. * ibmvfc_get_fc_type - Return the FC Type description text
  428. * @status: FC Type error status
  429. *
  430. * Returns:
  431. * error string
  432. **/
  433. static const char *ibmvfc_get_fc_type(u16 status)
  434. {
  435. int i;
  436. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  437. if (fc_type[i].fc_type == status)
  438. return fc_type[i].name;
  439. return unknown_fc_type;
  440. }
  441. /**
  442. * ibmvfc_set_tgt_action - Set the next init action for the target
  443. * @tgt: ibmvfc target struct
  444. * @action: action to perform
  445. *
  446. * Returns:
  447. * 0 if action changed / non-zero if not changed
  448. **/
  449. static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  450. enum ibmvfc_target_action action)
  451. {
  452. int rc = -EINVAL;
  453. switch (tgt->action) {
  454. case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
  455. if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
  456. action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  457. tgt->action = action;
  458. rc = 0;
  459. }
  460. break;
  461. case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
  462. if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
  463. action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  464. tgt->action = action;
  465. rc = 0;
  466. }
  467. break;
  468. case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
  469. if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
  470. tgt->action = action;
  471. rc = 0;
  472. }
  473. break;
  474. case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
  475. if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  476. tgt->action = action;
  477. rc = 0;
  478. }
  479. break;
  480. case IBMVFC_TGT_ACTION_DEL_RPORT:
  481. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  482. tgt->action = action;
  483. rc = 0;
  484. }
  485. break;
  486. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  487. break;
  488. default:
  489. tgt->action = action;
  490. rc = 0;
  491. break;
  492. }
  493. if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
  494. tgt->add_rport = 0;
  495. return rc;
  496. }
  497. /**
  498. * ibmvfc_set_host_state - Set the state for the host
  499. * @vhost: ibmvfc host struct
  500. * @state: state to set host to
  501. *
  502. * Returns:
  503. * 0 if state changed / non-zero if not changed
  504. **/
  505. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  506. enum ibmvfc_host_state state)
  507. {
  508. int rc = 0;
  509. switch (vhost->state) {
  510. case IBMVFC_HOST_OFFLINE:
  511. rc = -EINVAL;
  512. break;
  513. default:
  514. vhost->state = state;
  515. break;
  516. }
  517. return rc;
  518. }
  519. /**
  520. * ibmvfc_set_host_action - Set the next init action for the host
  521. * @vhost: ibmvfc host struct
  522. * @action: action to perform
  523. *
  524. **/
  525. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  526. enum ibmvfc_host_action action)
  527. {
  528. switch (action) {
  529. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  530. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  531. vhost->action = action;
  532. break;
  533. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  534. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  535. vhost->action = action;
  536. break;
  537. case IBMVFC_HOST_ACTION_INIT_WAIT:
  538. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  539. vhost->action = action;
  540. break;
  541. case IBMVFC_HOST_ACTION_QUERY:
  542. switch (vhost->action) {
  543. case IBMVFC_HOST_ACTION_INIT_WAIT:
  544. case IBMVFC_HOST_ACTION_NONE:
  545. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  546. vhost->action = action;
  547. break;
  548. default:
  549. break;
  550. }
  551. break;
  552. case IBMVFC_HOST_ACTION_TGT_INIT:
  553. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  554. vhost->action = action;
  555. break;
  556. case IBMVFC_HOST_ACTION_REENABLE:
  557. case IBMVFC_HOST_ACTION_RESET:
  558. vhost->action = action;
  559. break;
  560. case IBMVFC_HOST_ACTION_INIT:
  561. case IBMVFC_HOST_ACTION_TGT_DEL:
  562. case IBMVFC_HOST_ACTION_LOGO:
  563. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  564. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  565. case IBMVFC_HOST_ACTION_NONE:
  566. default:
  567. switch (vhost->action) {
  568. case IBMVFC_HOST_ACTION_RESET:
  569. case IBMVFC_HOST_ACTION_REENABLE:
  570. break;
  571. default:
  572. vhost->action = action;
  573. break;
  574. }
  575. break;
  576. }
  577. }
  578. /**
  579. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  580. * @vhost: ibmvfc host struct
  581. *
  582. * Return value:
  583. * nothing
  584. **/
  585. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  586. {
  587. if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
  588. vhost->state == IBMVFC_ACTIVE) {
  589. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  590. scsi_block_requests(vhost->host);
  591. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  592. }
  593. } else
  594. vhost->reinit = 1;
  595. wake_up(&vhost->work_wait_q);
  596. }
  597. /**
  598. * ibmvfc_del_tgt - Schedule cleanup and removal of the target
  599. * @tgt: ibmvfc target struct
  600. **/
  601. static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
  602. {
  603. if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT)) {
  604. tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
  605. tgt->init_retries = 0;
  606. }
  607. wake_up(&tgt->vhost->work_wait_q);
  608. }
  609. /**
  610. * ibmvfc_link_down - Handle a link down event from the adapter
  611. * @vhost: ibmvfc host struct
  612. * @state: ibmvfc host state to enter
  613. *
  614. **/
  615. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  616. enum ibmvfc_host_state state)
  617. {
  618. struct ibmvfc_target *tgt;
  619. ENTER;
  620. scsi_block_requests(vhost->host);
  621. list_for_each_entry(tgt, &vhost->targets, queue)
  622. ibmvfc_del_tgt(tgt);
  623. ibmvfc_set_host_state(vhost, state);
  624. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  625. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  626. wake_up(&vhost->work_wait_q);
  627. LEAVE;
  628. }
  629. /**
  630. * ibmvfc_init_host - Start host initialization
  631. * @vhost: ibmvfc host struct
  632. *
  633. * Return value:
  634. * nothing
  635. **/
  636. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  637. {
  638. struct ibmvfc_target *tgt;
  639. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  640. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  641. dev_err(vhost->dev,
  642. "Host initialization retries exceeded. Taking adapter offline\n");
  643. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  644. return;
  645. }
  646. }
  647. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  648. memset(vhost->async_crq.msgs.async, 0, PAGE_SIZE);
  649. vhost->async_crq.cur = 0;
  650. list_for_each_entry(tgt, &vhost->targets, queue) {
  651. if (vhost->client_migrated)
  652. tgt->need_login = 1;
  653. else
  654. ibmvfc_del_tgt(tgt);
  655. }
  656. scsi_block_requests(vhost->host);
  657. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  658. vhost->job_step = ibmvfc_npiv_login;
  659. wake_up(&vhost->work_wait_q);
  660. }
  661. }
  662. /**
  663. * ibmvfc_send_crq - Send a CRQ
  664. * @vhost: ibmvfc host struct
  665. * @word1: the first 64 bits of the data
  666. * @word2: the second 64 bits of the data
  667. *
  668. * Return value:
  669. * 0 on success / other on failure
  670. **/
  671. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  672. {
  673. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  674. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  675. }
  676. static int ibmvfc_send_sub_crq(struct ibmvfc_host *vhost, u64 cookie, u64 word1,
  677. u64 word2, u64 word3, u64 word4)
  678. {
  679. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  680. return plpar_hcall_norets(H_SEND_SUB_CRQ, vdev->unit_address, cookie,
  681. word1, word2, word3, word4);
  682. }
  683. /**
  684. * ibmvfc_send_crq_init - Send a CRQ init message
  685. * @vhost: ibmvfc host struct
  686. *
  687. * Return value:
  688. * 0 on success / other on failure
  689. **/
  690. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  691. {
  692. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  693. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  694. }
  695. /**
  696. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  697. * @vhost: ibmvfc host struct
  698. *
  699. * Return value:
  700. * 0 on success / other on failure
  701. **/
  702. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  703. {
  704. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  705. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  706. }
  707. /**
  708. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  709. * @vhost: ibmvfc host who owns the event pool
  710. * @queue: ibmvfc queue struct
  711. *
  712. * Returns zero on success.
  713. **/
  714. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost,
  715. struct ibmvfc_queue *queue)
  716. {
  717. int i;
  718. struct ibmvfc_event_pool *pool = &queue->evt_pool;
  719. ENTER;
  720. if (!queue->total_depth)
  721. return 0;
  722. pool->size = queue->total_depth;
  723. pool->events = kzalloc_objs(*pool->events, pool->size);
  724. if (!pool->events)
  725. return -ENOMEM;
  726. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  727. pool->size * sizeof(*pool->iu_storage),
  728. &pool->iu_token, 0);
  729. if (!pool->iu_storage) {
  730. kfree(pool->events);
  731. return -ENOMEM;
  732. }
  733. INIT_LIST_HEAD(&queue->sent);
  734. INIT_LIST_HEAD(&queue->free);
  735. queue->evt_free = queue->evt_depth;
  736. queue->reserved_free = queue->reserved_depth;
  737. spin_lock_init(&queue->l_lock);
  738. for (i = 0; i < pool->size; ++i) {
  739. struct ibmvfc_event *evt = &pool->events[i];
  740. /*
  741. * evt->active states
  742. * 1 = in flight
  743. * 0 = being completed
  744. * -1 = free/freed
  745. */
  746. atomic_set(&evt->active, -1);
  747. atomic_set(&evt->free, 1);
  748. evt->crq.valid = 0x80;
  749. evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
  750. evt->xfer_iu = pool->iu_storage + i;
  751. evt->vhost = vhost;
  752. evt->queue = queue;
  753. evt->ext_list = NULL;
  754. list_add_tail(&evt->queue_list, &queue->free);
  755. }
  756. LEAVE;
  757. return 0;
  758. }
  759. /**
  760. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  761. * @vhost: ibmvfc host who owns the event pool
  762. * @queue: ibmvfc queue struct
  763. *
  764. **/
  765. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost,
  766. struct ibmvfc_queue *queue)
  767. {
  768. int i;
  769. struct ibmvfc_event_pool *pool = &queue->evt_pool;
  770. ENTER;
  771. for (i = 0; i < pool->size; ++i) {
  772. list_del(&pool->events[i].queue_list);
  773. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  774. if (pool->events[i].ext_list)
  775. dma_pool_free(vhost->sg_pool,
  776. pool->events[i].ext_list,
  777. pool->events[i].ext_list_token);
  778. }
  779. kfree(pool->events);
  780. dma_free_coherent(vhost->dev,
  781. pool->size * sizeof(*pool->iu_storage),
  782. pool->iu_storage, pool->iu_token);
  783. LEAVE;
  784. }
  785. /**
  786. * ibmvfc_free_queue - Deallocate queue
  787. * @vhost: ibmvfc host struct
  788. * @queue: ibmvfc queue struct
  789. *
  790. * Unmaps dma and deallocates page for messages
  791. **/
  792. static void ibmvfc_free_queue(struct ibmvfc_host *vhost,
  793. struct ibmvfc_queue *queue)
  794. {
  795. struct device *dev = vhost->dev;
  796. dma_unmap_single(dev, queue->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  797. free_page((unsigned long)queue->msgs.handle);
  798. queue->msgs.handle = NULL;
  799. ibmvfc_free_event_pool(vhost, queue);
  800. }
  801. /**
  802. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  803. * @vhost: ibmvfc host struct
  804. *
  805. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  806. * the crq with the hypervisor.
  807. **/
  808. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  809. {
  810. long rc = 0;
  811. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  812. struct ibmvfc_queue *crq = &vhost->crq;
  813. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  814. free_irq(vdev->irq, vhost);
  815. tasklet_kill(&vhost->tasklet);
  816. do {
  817. if (rc)
  818. msleep(100);
  819. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  820. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  821. vhost->state = IBMVFC_NO_CRQ;
  822. vhost->logged_in = 0;
  823. ibmvfc_free_queue(vhost, crq);
  824. }
  825. /**
  826. * ibmvfc_reenable_crq_queue - reenables the CRQ
  827. * @vhost: ibmvfc host struct
  828. *
  829. * Return value:
  830. * 0 on success / other on failure
  831. **/
  832. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  833. {
  834. int rc = 0;
  835. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  836. unsigned long flags;
  837. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  838. /* Re-enable the CRQ */
  839. do {
  840. if (rc)
  841. msleep(100);
  842. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  843. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  844. if (rc)
  845. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  846. spin_lock_irqsave(vhost->host->host_lock, flags);
  847. spin_lock(vhost->crq.q_lock);
  848. vhost->do_enquiry = 1;
  849. vhost->using_channels = 0;
  850. spin_unlock(vhost->crq.q_lock);
  851. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  852. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  853. return rc;
  854. }
  855. /**
  856. * ibmvfc_reset_crq - resets a crq after a failure
  857. * @vhost: ibmvfc host struct
  858. *
  859. * Return value:
  860. * 0 on success / other on failure
  861. **/
  862. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  863. {
  864. int rc = 0;
  865. unsigned long flags;
  866. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  867. struct ibmvfc_queue *crq = &vhost->crq;
  868. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  869. /* Close the CRQ */
  870. do {
  871. if (rc)
  872. msleep(100);
  873. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  874. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  875. spin_lock_irqsave(vhost->host->host_lock, flags);
  876. spin_lock(vhost->crq.q_lock);
  877. vhost->state = IBMVFC_NO_CRQ;
  878. vhost->logged_in = 0;
  879. vhost->do_enquiry = 1;
  880. vhost->using_channels = 0;
  881. /* Clean out the queue */
  882. memset(crq->msgs.crq, 0, PAGE_SIZE);
  883. crq->cur = 0;
  884. /* And re-open it again */
  885. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  886. crq->msg_token, PAGE_SIZE);
  887. if (rc == H_CLOSED)
  888. /* Adapter is good, but other end is not ready */
  889. dev_warn(vhost->dev, "Partner adapter not ready\n");
  890. else if (rc != 0)
  891. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  892. spin_unlock(vhost->crq.q_lock);
  893. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  894. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  895. return rc;
  896. }
  897. /**
  898. * ibmvfc_valid_event - Determines if event is valid.
  899. * @pool: event_pool that contains the event
  900. * @evt: ibmvfc event to be checked for validity
  901. *
  902. * Return value:
  903. * 1 if event is valid / 0 if event is not valid
  904. **/
  905. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  906. struct ibmvfc_event *evt)
  907. {
  908. int index = evt - pool->events;
  909. if (index < 0 || index >= pool->size) /* outside of bounds */
  910. return 0;
  911. if (evt != pool->events + index) /* unaligned */
  912. return 0;
  913. return 1;
  914. }
  915. /**
  916. * ibmvfc_free_event - Free the specified event
  917. * @evt: ibmvfc_event to be freed
  918. *
  919. **/
  920. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  921. {
  922. struct ibmvfc_event_pool *pool = &evt->queue->evt_pool;
  923. unsigned long flags;
  924. BUG_ON(!ibmvfc_valid_event(pool, evt));
  925. BUG_ON(atomic_inc_return(&evt->free) != 1);
  926. BUG_ON(atomic_dec_and_test(&evt->active));
  927. spin_lock_irqsave(&evt->queue->l_lock, flags);
  928. list_add_tail(&evt->queue_list, &evt->queue->free);
  929. if (evt->reserved) {
  930. evt->reserved = 0;
  931. evt->queue->reserved_free++;
  932. } else {
  933. evt->queue->evt_free++;
  934. }
  935. if (evt->eh_comp)
  936. complete(evt->eh_comp);
  937. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  938. }
  939. /**
  940. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  941. * @evt: ibmvfc event struct
  942. *
  943. * This function does not setup any error status, that must be done
  944. * before this function gets called.
  945. **/
  946. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  947. {
  948. struct scsi_cmnd *cmnd = evt->cmnd;
  949. if (cmnd) {
  950. scsi_dma_unmap(cmnd);
  951. scsi_done(cmnd);
  952. }
  953. ibmvfc_free_event(evt);
  954. }
  955. /**
  956. * ibmvfc_complete_purge - Complete failed command list
  957. * @purge_list: list head of failed commands
  958. *
  959. * This function runs completions on commands to fail as a result of a
  960. * host reset or platform migration.
  961. **/
  962. static void ibmvfc_complete_purge(struct list_head *purge_list)
  963. {
  964. struct ibmvfc_event *evt, *pos;
  965. list_for_each_entry_safe(evt, pos, purge_list, queue_list) {
  966. list_del(&evt->queue_list);
  967. ibmvfc_trc_end(evt);
  968. evt->done(evt);
  969. }
  970. }
  971. /**
  972. * ibmvfc_fail_request - Fail request with specified error code
  973. * @evt: ibmvfc event struct
  974. * @error_code: error code to fail request with
  975. *
  976. * Return value:
  977. * none
  978. **/
  979. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  980. {
  981. /*
  982. * Anything we are failing should still be active. Otherwise, it
  983. * implies we already got a response for the command and are doing
  984. * something bad like double completing it.
  985. */
  986. BUG_ON(!atomic_dec_and_test(&evt->active));
  987. if (evt->cmnd) {
  988. evt->cmnd->result = (error_code << 16);
  989. evt->done = ibmvfc_scsi_eh_done;
  990. } else
  991. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
  992. timer_delete(&evt->timer);
  993. }
  994. /**
  995. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  996. * @vhost: ibmvfc host struct
  997. * @error_code: error code to fail requests with
  998. *
  999. * Return value:
  1000. * none
  1001. **/
  1002. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  1003. {
  1004. struct ibmvfc_event *evt, *pos;
  1005. struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
  1006. unsigned long flags;
  1007. int hwqs = 0;
  1008. int i;
  1009. if (vhost->using_channels)
  1010. hwqs = vhost->scsi_scrqs.active_queues;
  1011. ibmvfc_dbg(vhost, "Purging all requests\n");
  1012. spin_lock_irqsave(&vhost->crq.l_lock, flags);
  1013. list_for_each_entry_safe(evt, pos, &vhost->crq.sent, queue_list)
  1014. ibmvfc_fail_request(evt, error_code);
  1015. list_splice_init(&vhost->crq.sent, &vhost->purge);
  1016. spin_unlock_irqrestore(&vhost->crq.l_lock, flags);
  1017. for (i = 0; i < hwqs; i++) {
  1018. spin_lock_irqsave(queues[i].q_lock, flags);
  1019. spin_lock(&queues[i].l_lock);
  1020. list_for_each_entry_safe(evt, pos, &queues[i].sent, queue_list)
  1021. ibmvfc_fail_request(evt, error_code);
  1022. list_splice_init(&queues[i].sent, &vhost->purge);
  1023. spin_unlock(&queues[i].l_lock);
  1024. spin_unlock_irqrestore(queues[i].q_lock, flags);
  1025. }
  1026. }
  1027. /**
  1028. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  1029. * @vhost: struct ibmvfc host to reset
  1030. **/
  1031. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  1032. {
  1033. ibmvfc_purge_requests(vhost, DID_ERROR);
  1034. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  1035. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  1036. }
  1037. /**
  1038. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  1039. * @vhost: struct ibmvfc host to reset
  1040. **/
  1041. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  1042. {
  1043. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  1044. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  1045. scsi_block_requests(vhost->host);
  1046. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  1047. vhost->job_step = ibmvfc_npiv_logout;
  1048. wake_up(&vhost->work_wait_q);
  1049. } else
  1050. ibmvfc_hard_reset_host(vhost);
  1051. }
  1052. /**
  1053. * ibmvfc_reset_host - Reset the connection to the server
  1054. * @vhost: ibmvfc host struct
  1055. **/
  1056. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  1057. {
  1058. unsigned long flags;
  1059. spin_lock_irqsave(vhost->host->host_lock, flags);
  1060. __ibmvfc_reset_host(vhost);
  1061. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1062. }
  1063. /**
  1064. * ibmvfc_retry_host_init - Retry host initialization if allowed
  1065. * @vhost: ibmvfc host struct
  1066. *
  1067. * Returns: 1 if init will be retried / 0 if not
  1068. *
  1069. **/
  1070. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  1071. {
  1072. int retry = 0;
  1073. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  1074. vhost->delay_init = 1;
  1075. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  1076. dev_err(vhost->dev,
  1077. "Host initialization retries exceeded. Taking adapter offline\n");
  1078. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  1079. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  1080. __ibmvfc_reset_host(vhost);
  1081. else {
  1082. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  1083. retry = 1;
  1084. }
  1085. }
  1086. wake_up(&vhost->work_wait_q);
  1087. return retry;
  1088. }
  1089. /**
  1090. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  1091. * @starget: scsi target struct
  1092. *
  1093. * Return value:
  1094. * ibmvfc_target struct / NULL if not found
  1095. **/
  1096. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  1097. {
  1098. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1099. struct ibmvfc_host *vhost = shost_priv(shost);
  1100. struct ibmvfc_target *tgt;
  1101. list_for_each_entry(tgt, &vhost->targets, queue)
  1102. if (tgt->target_id == starget->id) {
  1103. kref_get(&tgt->kref);
  1104. return tgt;
  1105. }
  1106. return NULL;
  1107. }
  1108. /**
  1109. * ibmvfc_get_target - Find the specified scsi_target
  1110. * @starget: scsi target struct
  1111. *
  1112. * Return value:
  1113. * ibmvfc_target struct / NULL if not found
  1114. **/
  1115. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  1116. {
  1117. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1118. struct ibmvfc_target *tgt;
  1119. unsigned long flags;
  1120. spin_lock_irqsave(shost->host_lock, flags);
  1121. tgt = __ibmvfc_get_target(starget);
  1122. spin_unlock_irqrestore(shost->host_lock, flags);
  1123. return tgt;
  1124. }
  1125. /**
  1126. * ibmvfc_get_host_speed - Get host port speed
  1127. * @shost: scsi host struct
  1128. *
  1129. * Return value:
  1130. * none
  1131. **/
  1132. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  1133. {
  1134. struct ibmvfc_host *vhost = shost_priv(shost);
  1135. unsigned long flags;
  1136. spin_lock_irqsave(shost->host_lock, flags);
  1137. if (vhost->state == IBMVFC_ACTIVE) {
  1138. switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
  1139. case 1:
  1140. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  1141. break;
  1142. case 2:
  1143. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  1144. break;
  1145. case 4:
  1146. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  1147. break;
  1148. case 8:
  1149. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  1150. break;
  1151. case 10:
  1152. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  1153. break;
  1154. case 16:
  1155. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  1156. break;
  1157. default:
  1158. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  1159. be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
  1160. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  1161. break;
  1162. }
  1163. } else
  1164. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  1165. spin_unlock_irqrestore(shost->host_lock, flags);
  1166. }
  1167. /**
  1168. * ibmvfc_get_host_port_state - Get host port state
  1169. * @shost: scsi host struct
  1170. *
  1171. * Return value:
  1172. * none
  1173. **/
  1174. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  1175. {
  1176. struct ibmvfc_host *vhost = shost_priv(shost);
  1177. unsigned long flags;
  1178. spin_lock_irqsave(shost->host_lock, flags);
  1179. switch (vhost->state) {
  1180. case IBMVFC_INITIALIZING:
  1181. case IBMVFC_ACTIVE:
  1182. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  1183. break;
  1184. case IBMVFC_LINK_DOWN:
  1185. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  1186. break;
  1187. case IBMVFC_LINK_DEAD:
  1188. case IBMVFC_HOST_OFFLINE:
  1189. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  1190. break;
  1191. case IBMVFC_HALTED:
  1192. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  1193. break;
  1194. case IBMVFC_NO_CRQ:
  1195. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  1196. break;
  1197. default:
  1198. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  1199. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  1200. break;
  1201. }
  1202. spin_unlock_irqrestore(shost->host_lock, flags);
  1203. }
  1204. /**
  1205. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  1206. * @rport: rport struct
  1207. * @timeout: timeout value
  1208. *
  1209. * Return value:
  1210. * none
  1211. **/
  1212. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  1213. {
  1214. if (timeout)
  1215. rport->dev_loss_tmo = timeout;
  1216. else
  1217. rport->dev_loss_tmo = 1;
  1218. }
  1219. /**
  1220. * ibmvfc_release_tgt - Free memory allocated for a target
  1221. * @kref: kref struct
  1222. *
  1223. **/
  1224. static void ibmvfc_release_tgt(struct kref *kref)
  1225. {
  1226. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  1227. kfree(tgt);
  1228. }
  1229. /**
  1230. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  1231. * @starget: scsi target struct
  1232. *
  1233. * Return value:
  1234. * none
  1235. **/
  1236. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  1237. {
  1238. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1239. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  1240. if (tgt)
  1241. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1242. }
  1243. /**
  1244. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  1245. * @starget: scsi target struct
  1246. *
  1247. * Return value:
  1248. * none
  1249. **/
  1250. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  1251. {
  1252. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1253. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  1254. if (tgt)
  1255. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1256. }
  1257. /**
  1258. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  1259. * @starget: scsi target struct
  1260. *
  1261. * Return value:
  1262. * none
  1263. **/
  1264. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  1265. {
  1266. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  1267. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  1268. if (tgt)
  1269. kref_put(&tgt->kref, ibmvfc_release_tgt);
  1270. }
  1271. /**
  1272. * ibmvfc_wait_while_resetting - Wait while the host resets
  1273. * @vhost: ibmvfc host struct
  1274. *
  1275. * Return value:
  1276. * 0 on success / other on failure
  1277. **/
  1278. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1279. {
  1280. long timeout = wait_event_timeout(vhost->init_wait_q,
  1281. ((vhost->state == IBMVFC_ACTIVE ||
  1282. vhost->state == IBMVFC_HOST_OFFLINE ||
  1283. vhost->state == IBMVFC_LINK_DEAD) &&
  1284. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1285. (init_timeout * HZ));
  1286. return timeout ? 0 : -EIO;
  1287. }
  1288. /**
  1289. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1290. * @shost: scsi host struct
  1291. *
  1292. * Return value:
  1293. * 0 on success / other on failure
  1294. **/
  1295. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1296. {
  1297. struct ibmvfc_host *vhost = shost_priv(shost);
  1298. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1299. ibmvfc_reset_host(vhost);
  1300. return ibmvfc_wait_while_resetting(vhost);
  1301. }
  1302. /**
  1303. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1304. * @vhost: ibmvfc host struct
  1305. *
  1306. * Return value:
  1307. * none
  1308. **/
  1309. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1310. {
  1311. struct device_node *rootdn;
  1312. const char *name;
  1313. const unsigned int *num;
  1314. rootdn = of_find_node_by_path("/");
  1315. if (!rootdn)
  1316. return;
  1317. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1318. if (name)
  1319. strscpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1320. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1321. if (num)
  1322. vhost->partition_number = *num;
  1323. of_node_put(rootdn);
  1324. }
  1325. /**
  1326. * ibmvfc_set_login_info - Setup info for NPIV login
  1327. * @vhost: ibmvfc host struct
  1328. *
  1329. * Return value:
  1330. * none
  1331. **/
  1332. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1333. {
  1334. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1335. struct ibmvfc_queue *async_crq = &vhost->async_crq;
  1336. struct device_node *of_node = vhost->dev->of_node;
  1337. const char *location;
  1338. u16 max_cmds;
  1339. max_cmds = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
  1340. if (mq_enabled)
  1341. max_cmds += (scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ) *
  1342. vhost->scsi_scrqs.desired_queues;
  1343. memset(login_info, 0, sizeof(*login_info));
  1344. login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
  1345. login_info->max_dma_len = cpu_to_be64(max_sectors << 9);
  1346. login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
  1347. login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
  1348. login_info->partition_num = cpu_to_be32(vhost->partition_number);
  1349. login_info->vfc_frame_version = cpu_to_be32(1);
  1350. login_info->fcp_version = cpu_to_be16(3);
  1351. login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
  1352. if (vhost->client_migrated)
  1353. login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
  1354. login_info->max_cmds = cpu_to_be32(max_cmds);
  1355. login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE | IBMVFC_CAN_SEND_VF_WWPN);
  1356. if (vhost->mq_enabled || vhost->using_channels)
  1357. login_info->capabilities |= cpu_to_be64(IBMVFC_CAN_USE_CHANNELS);
  1358. login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
  1359. login_info->async.len = cpu_to_be32(async_crq->size *
  1360. sizeof(*async_crq->msgs.async));
  1361. strscpy(login_info->partition_name, vhost->partition_name,
  1362. sizeof(login_info->partition_name));
  1363. strscpy(login_info->device_name,
  1364. dev_name(&vhost->host->shost_gendev), sizeof(login_info->device_name));
  1365. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1366. location = location ? location : dev_name(vhost->dev);
  1367. strscpy(login_info->drc_name, location, sizeof(login_info->drc_name));
  1368. }
  1369. /**
  1370. * __ibmvfc_get_event - Gets the next free event in pool
  1371. * @queue: ibmvfc queue struct
  1372. * @reserved: event is for a reserved management command
  1373. *
  1374. * Returns a free event from the pool.
  1375. **/
  1376. static struct ibmvfc_event *__ibmvfc_get_event(struct ibmvfc_queue *queue, int reserved)
  1377. {
  1378. struct ibmvfc_event *evt = NULL;
  1379. unsigned long flags;
  1380. spin_lock_irqsave(&queue->l_lock, flags);
  1381. if (reserved && queue->reserved_free) {
  1382. evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
  1383. evt->reserved = 1;
  1384. queue->reserved_free--;
  1385. } else if (queue->evt_free) {
  1386. evt = list_entry(queue->free.next, struct ibmvfc_event, queue_list);
  1387. queue->evt_free--;
  1388. } else {
  1389. goto out;
  1390. }
  1391. atomic_set(&evt->free, 0);
  1392. list_del(&evt->queue_list);
  1393. out:
  1394. spin_unlock_irqrestore(&queue->l_lock, flags);
  1395. return evt;
  1396. }
  1397. #define ibmvfc_get_event(queue) __ibmvfc_get_event(queue, 0)
  1398. #define ibmvfc_get_reserved_event(queue) __ibmvfc_get_event(queue, 1)
  1399. /**
  1400. * ibmvfc_locked_done - Calls evt completion with host_lock held
  1401. * @evt: ibmvfc evt to complete
  1402. *
  1403. * All non-scsi command completion callbacks have the expectation that the
  1404. * host_lock is held. This callback is used by ibmvfc_init_event to wrap a
  1405. * MAD evt with the host_lock.
  1406. **/
  1407. static void ibmvfc_locked_done(struct ibmvfc_event *evt)
  1408. {
  1409. unsigned long flags;
  1410. spin_lock_irqsave(evt->vhost->host->host_lock, flags);
  1411. evt->_done(evt);
  1412. spin_unlock_irqrestore(evt->vhost->host->host_lock, flags);
  1413. }
  1414. /**
  1415. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1416. * required.
  1417. * @evt: The event
  1418. * @done: Routine to call when the event is responded to
  1419. * @format: SRP or MAD format
  1420. **/
  1421. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1422. void (*done) (struct ibmvfc_event *), u8 format)
  1423. {
  1424. evt->cmnd = NULL;
  1425. evt->sync_iu = NULL;
  1426. evt->eh_comp = NULL;
  1427. evt->crq.format = format;
  1428. if (format == IBMVFC_CMD_FORMAT)
  1429. evt->done = done;
  1430. else {
  1431. evt->_done = done;
  1432. evt->done = ibmvfc_locked_done;
  1433. }
  1434. evt->hwq = 0;
  1435. }
  1436. /**
  1437. * ibmvfc_map_sg_list - Initialize scatterlist
  1438. * @scmd: scsi command struct
  1439. * @nseg: number of scatterlist segments
  1440. * @md: memory descriptor list to initialize
  1441. **/
  1442. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1443. struct srp_direct_buf *md)
  1444. {
  1445. int i;
  1446. struct scatterlist *sg;
  1447. scsi_for_each_sg(scmd, sg, nseg, i) {
  1448. md[i].va = cpu_to_be64(sg_dma_address(sg));
  1449. md[i].len = cpu_to_be32(sg_dma_len(sg));
  1450. md[i].key = 0;
  1451. }
  1452. }
  1453. /**
  1454. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
  1455. * @scmd: struct scsi_cmnd with the scatterlist
  1456. * @evt: ibmvfc event struct
  1457. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1458. * @dev: device for which to map dma memory
  1459. *
  1460. * Returns:
  1461. * 0 on success / non-zero on failure
  1462. **/
  1463. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1464. struct ibmvfc_event *evt,
  1465. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1466. {
  1467. int sg_mapped;
  1468. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1469. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1470. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(evt->vhost, vfc_cmd);
  1471. if (cls3_error)
  1472. vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
  1473. sg_mapped = scsi_dma_map(scmd);
  1474. if (!sg_mapped) {
  1475. vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
  1476. return 0;
  1477. } else if (unlikely(sg_mapped < 0)) {
  1478. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1479. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1480. return sg_mapped;
  1481. }
  1482. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1483. vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
  1484. iu->add_cdb_len |= IBMVFC_WRDATA;
  1485. } else {
  1486. vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
  1487. iu->add_cdb_len |= IBMVFC_RDDATA;
  1488. }
  1489. if (sg_mapped == 1) {
  1490. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1491. return 0;
  1492. }
  1493. vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
  1494. if (!evt->ext_list) {
  1495. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1496. &evt->ext_list_token);
  1497. if (!evt->ext_list) {
  1498. scsi_dma_unmap(scmd);
  1499. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1500. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1501. return -ENOMEM;
  1502. }
  1503. }
  1504. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1505. data->va = cpu_to_be64(evt->ext_list_token);
  1506. data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
  1507. data->key = 0;
  1508. return 0;
  1509. }
  1510. /**
  1511. * ibmvfc_timeout - Internal command timeout handler
  1512. * @t: struct ibmvfc_event that timed out
  1513. *
  1514. * Called when an internally generated command times out
  1515. **/
  1516. static void ibmvfc_timeout(struct timer_list *t)
  1517. {
  1518. struct ibmvfc_event *evt = timer_container_of(evt, t, timer);
  1519. struct ibmvfc_host *vhost = evt->vhost;
  1520. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1521. ibmvfc_reset_host(vhost);
  1522. }
  1523. /**
  1524. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1525. * @evt: event to be sent
  1526. * @vhost: ibmvfc host struct
  1527. * @timeout: timeout in seconds - 0 means do not time command
  1528. *
  1529. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1530. **/
  1531. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1532. struct ibmvfc_host *vhost, unsigned long timeout)
  1533. {
  1534. __be64 *crq_as_u64 = (__be64 *) &evt->crq;
  1535. unsigned long flags;
  1536. int rc;
  1537. /* Copy the IU into the transfer area */
  1538. *evt->xfer_iu = evt->iu;
  1539. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1540. evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
  1541. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1542. evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
  1543. else
  1544. BUG();
  1545. timer_setup(&evt->timer, ibmvfc_timeout, 0);
  1546. if (timeout) {
  1547. evt->timer.expires = jiffies + (timeout * HZ);
  1548. add_timer(&evt->timer);
  1549. }
  1550. spin_lock_irqsave(&evt->queue->l_lock, flags);
  1551. list_add_tail(&evt->queue_list, &evt->queue->sent);
  1552. atomic_set(&evt->active, 1);
  1553. mb();
  1554. if (evt->queue->fmt == IBMVFC_SUB_CRQ_FMT)
  1555. rc = ibmvfc_send_sub_crq(vhost,
  1556. evt->queue->vios_cookie,
  1557. be64_to_cpu(crq_as_u64[0]),
  1558. be64_to_cpu(crq_as_u64[1]),
  1559. 0, 0);
  1560. else
  1561. rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
  1562. be64_to_cpu(crq_as_u64[1]));
  1563. if (rc) {
  1564. atomic_set(&evt->active, 0);
  1565. list_del(&evt->queue_list);
  1566. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  1567. timer_delete(&evt->timer);
  1568. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1569. * Firmware will send a CRQ with a transport event (0xFF) to
  1570. * tell this client what has happened to the transport. This
  1571. * will be handled in ibmvfc_handle_crq()
  1572. */
  1573. if (rc == H_CLOSED) {
  1574. if (printk_ratelimit())
  1575. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1576. if (evt->cmnd)
  1577. scsi_dma_unmap(evt->cmnd);
  1578. ibmvfc_free_event(evt);
  1579. return SCSI_MLQUEUE_HOST_BUSY;
  1580. }
  1581. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1582. if (evt->cmnd) {
  1583. evt->cmnd->result = DID_ERROR << 16;
  1584. evt->done = ibmvfc_scsi_eh_done;
  1585. } else
  1586. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
  1587. evt->done(evt);
  1588. } else {
  1589. spin_unlock_irqrestore(&evt->queue->l_lock, flags);
  1590. ibmvfc_trc_start(evt);
  1591. }
  1592. return 0;
  1593. }
  1594. /**
  1595. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1596. * @evt: ibmvfc event to log
  1597. *
  1598. **/
  1599. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1600. {
  1601. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1602. struct ibmvfc_host *vhost = evt->vhost;
  1603. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  1604. struct scsi_cmnd *cmnd = evt->cmnd;
  1605. const char *err = unknown_error;
  1606. int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  1607. int logerr = 0;
  1608. int rsp_code = 0;
  1609. if (index >= 0) {
  1610. logerr = cmd_status[index].log;
  1611. err = cmd_status[index].name;
  1612. }
  1613. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1614. return;
  1615. if (rsp->flags & FCP_RSP_LEN_VALID)
  1616. rsp_code = rsp->data.info.rsp_code;
  1617. scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
  1618. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1619. cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
  1620. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1621. }
  1622. /**
  1623. * ibmvfc_relogin - Log back into the specified device
  1624. * @sdev: scsi device struct
  1625. *
  1626. **/
  1627. static void ibmvfc_relogin(struct scsi_device *sdev)
  1628. {
  1629. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1630. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1631. struct ibmvfc_target *tgt;
  1632. unsigned long flags;
  1633. spin_lock_irqsave(vhost->host->host_lock, flags);
  1634. list_for_each_entry(tgt, &vhost->targets, queue) {
  1635. if (rport == tgt->rport) {
  1636. ibmvfc_del_tgt(tgt);
  1637. break;
  1638. }
  1639. }
  1640. ibmvfc_reinit_host(vhost);
  1641. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1642. }
  1643. /**
  1644. * ibmvfc_scsi_done - Handle responses from commands
  1645. * @evt: ibmvfc event to be handled
  1646. *
  1647. * Used as a callback when sending scsi cmds.
  1648. **/
  1649. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1650. {
  1651. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1652. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(evt->vhost, vfc_cmd);
  1653. struct scsi_cmnd *cmnd = evt->cmnd;
  1654. u32 rsp_len = 0;
  1655. u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
  1656. if (cmnd) {
  1657. if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
  1658. scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
  1659. else if (rsp->flags & FCP_RESID_UNDER)
  1660. scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
  1661. else
  1662. scsi_set_resid(cmnd, 0);
  1663. if (vfc_cmd->status) {
  1664. cmnd->result = ibmvfc_get_err_result(evt->vhost, vfc_cmd);
  1665. if (rsp->flags & FCP_RSP_LEN_VALID)
  1666. rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  1667. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1668. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1669. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1670. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1671. if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
  1672. (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
  1673. ibmvfc_relogin(cmnd->device);
  1674. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1675. cmnd->result = (DID_ERROR << 16);
  1676. ibmvfc_log_error(evt);
  1677. }
  1678. if (!cmnd->result &&
  1679. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1680. cmnd->result = (DID_ERROR << 16);
  1681. scsi_dma_unmap(cmnd);
  1682. scsi_done(cmnd);
  1683. }
  1684. ibmvfc_free_event(evt);
  1685. }
  1686. /**
  1687. * ibmvfc_host_chkready - Check if the host can accept commands
  1688. * @vhost: struct ibmvfc host
  1689. *
  1690. * Returns:
  1691. * 1 if host can accept command / 0 if not
  1692. **/
  1693. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1694. {
  1695. int result = 0;
  1696. switch (vhost->state) {
  1697. case IBMVFC_LINK_DEAD:
  1698. case IBMVFC_HOST_OFFLINE:
  1699. result = DID_NO_CONNECT << 16;
  1700. break;
  1701. case IBMVFC_NO_CRQ:
  1702. case IBMVFC_INITIALIZING:
  1703. case IBMVFC_HALTED:
  1704. case IBMVFC_LINK_DOWN:
  1705. result = DID_REQUEUE << 16;
  1706. break;
  1707. case IBMVFC_ACTIVE:
  1708. result = 0;
  1709. break;
  1710. }
  1711. return result;
  1712. }
  1713. static struct ibmvfc_cmd *ibmvfc_init_vfc_cmd(struct ibmvfc_event *evt, struct scsi_device *sdev)
  1714. {
  1715. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1716. struct ibmvfc_host *vhost = evt->vhost;
  1717. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  1718. struct ibmvfc_fcp_cmd_iu *iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  1719. struct ibmvfc_fcp_rsp *rsp = ibmvfc_get_fcp_rsp(vhost, vfc_cmd);
  1720. size_t offset;
  1721. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1722. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  1723. offset = offsetof(struct ibmvfc_cmd, v2.rsp);
  1724. vfc_cmd->target_wwpn = cpu_to_be64(rport->port_name);
  1725. } else
  1726. offset = offsetof(struct ibmvfc_cmd, v1.rsp);
  1727. vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offset);
  1728. vfc_cmd->resp.len = cpu_to_be32(sizeof(*rsp));
  1729. vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1730. vfc_cmd->payload_len = cpu_to_be32(sizeof(*iu));
  1731. vfc_cmd->resp_len = cpu_to_be32(sizeof(*rsp));
  1732. vfc_cmd->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1733. vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1734. int_to_scsilun(sdev->lun, &iu->lun);
  1735. return vfc_cmd;
  1736. }
  1737. /**
  1738. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1739. * @shost: scsi host struct
  1740. * @cmnd: struct scsi_cmnd to be executed
  1741. *
  1742. * Returns:
  1743. * 0 on success / other on failure
  1744. **/
  1745. static enum scsi_qc_status ibmvfc_queuecommand(struct Scsi_Host *shost,
  1746. struct scsi_cmnd *cmnd)
  1747. {
  1748. struct ibmvfc_host *vhost = shost_priv(shost);
  1749. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1750. struct ibmvfc_cmd *vfc_cmd;
  1751. struct ibmvfc_fcp_cmd_iu *iu;
  1752. struct ibmvfc_event *evt;
  1753. u32 tag_and_hwq = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
  1754. u16 hwq = blk_mq_unique_tag_to_hwq(tag_and_hwq);
  1755. u16 scsi_channel;
  1756. int rc;
  1757. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1758. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1759. cmnd->result = rc;
  1760. scsi_done(cmnd);
  1761. return 0;
  1762. }
  1763. cmnd->result = (DID_OK << 16);
  1764. if (vhost->using_channels) {
  1765. scsi_channel = hwq % vhost->scsi_scrqs.active_queues;
  1766. evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[scsi_channel]);
  1767. if (!evt)
  1768. return SCSI_MLQUEUE_HOST_BUSY;
  1769. evt->hwq = hwq % vhost->scsi_scrqs.active_queues;
  1770. } else {
  1771. evt = ibmvfc_get_event(&vhost->crq);
  1772. if (!evt)
  1773. return SCSI_MLQUEUE_HOST_BUSY;
  1774. }
  1775. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1776. evt->cmnd = cmnd;
  1777. vfc_cmd = ibmvfc_init_vfc_cmd(evt, cmnd->device);
  1778. iu = ibmvfc_get_fcp_iu(vhost, vfc_cmd);
  1779. iu->xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
  1780. memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
  1781. if (cmnd->flags & SCMD_TAGGED) {
  1782. vfc_cmd->task_tag = cpu_to_be64(scsi_cmd_to_rq(cmnd)->tag);
  1783. iu->pri_task_attr = IBMVFC_SIMPLE_TASK;
  1784. }
  1785. vfc_cmd->correlation = cpu_to_be64((u64)evt);
  1786. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1787. return ibmvfc_send_event(evt, vhost, 0);
  1788. ibmvfc_free_event(evt);
  1789. if (rc == -ENOMEM)
  1790. return SCSI_MLQUEUE_HOST_BUSY;
  1791. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1792. scmd_printk(KERN_ERR, cmnd,
  1793. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1794. cmnd->result = DID_ERROR << 16;
  1795. scsi_done(cmnd);
  1796. return 0;
  1797. }
  1798. /**
  1799. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1800. * @evt: ibmvfc event struct
  1801. *
  1802. **/
  1803. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1804. {
  1805. /* copy the response back */
  1806. if (evt->sync_iu)
  1807. *evt->sync_iu = *evt->xfer_iu;
  1808. complete(&evt->comp);
  1809. }
  1810. /**
  1811. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1812. * @evt: struct ibmvfc_event
  1813. *
  1814. **/
  1815. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1816. {
  1817. struct ibmvfc_host *vhost = evt->vhost;
  1818. ibmvfc_free_event(evt);
  1819. vhost->aborting_passthru = 0;
  1820. dev_info(vhost->dev, "Passthru command cancelled\n");
  1821. }
  1822. /**
  1823. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1824. * @job: struct bsg_job that timed out
  1825. *
  1826. * Returns:
  1827. * 0 on success / other on failure
  1828. **/
  1829. static int ibmvfc_bsg_timeout(struct bsg_job *job)
  1830. {
  1831. struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
  1832. unsigned long port_id = (unsigned long)job->dd_data;
  1833. struct ibmvfc_event *evt;
  1834. struct ibmvfc_tmf *tmf;
  1835. unsigned long flags;
  1836. int rc;
  1837. ENTER;
  1838. spin_lock_irqsave(vhost->host->host_lock, flags);
  1839. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1840. __ibmvfc_reset_host(vhost);
  1841. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1842. return 0;
  1843. }
  1844. vhost->aborting_passthru = 1;
  1845. evt = ibmvfc_get_reserved_event(&vhost->crq);
  1846. if (!evt) {
  1847. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1848. return -ENOMEM;
  1849. }
  1850. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1851. tmf = &evt->iu.tmf;
  1852. memset(tmf, 0, sizeof(*tmf));
  1853. tmf->common.version = cpu_to_be32(1);
  1854. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1855. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1856. tmf->scsi_id = cpu_to_be64(port_id);
  1857. tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1858. tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
  1859. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1860. if (rc != 0) {
  1861. vhost->aborting_passthru = 0;
  1862. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1863. rc = -EIO;
  1864. } else
  1865. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1866. port_id);
  1867. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1868. LEAVE;
  1869. return rc;
  1870. }
  1871. /**
  1872. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1873. * @vhost: struct ibmvfc_host to send command
  1874. * @port_id: port ID to send command
  1875. *
  1876. * Returns:
  1877. * 0 on success / other on failure
  1878. **/
  1879. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1880. {
  1881. struct ibmvfc_port_login *plogi;
  1882. struct ibmvfc_target *tgt;
  1883. struct ibmvfc_event *evt;
  1884. union ibmvfc_iu rsp_iu;
  1885. unsigned long flags;
  1886. int rc = 0, issue_login = 1;
  1887. ENTER;
  1888. spin_lock_irqsave(vhost->host->host_lock, flags);
  1889. list_for_each_entry(tgt, &vhost->targets, queue) {
  1890. if (tgt->scsi_id == port_id) {
  1891. issue_login = 0;
  1892. break;
  1893. }
  1894. }
  1895. if (!issue_login)
  1896. goto unlock_out;
  1897. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1898. goto unlock_out;
  1899. evt = ibmvfc_get_reserved_event(&vhost->crq);
  1900. if (!evt) {
  1901. rc = -ENOMEM;
  1902. goto unlock_out;
  1903. }
  1904. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1905. plogi = &evt->iu.plogi;
  1906. memset(plogi, 0, sizeof(*plogi));
  1907. plogi->common.version = cpu_to_be32(1);
  1908. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  1909. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  1910. plogi->scsi_id = cpu_to_be64(port_id);
  1911. evt->sync_iu = &rsp_iu;
  1912. init_completion(&evt->comp);
  1913. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1914. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1915. if (rc)
  1916. return -EIO;
  1917. wait_for_completion(&evt->comp);
  1918. if (rsp_iu.plogi.common.status)
  1919. rc = -EIO;
  1920. spin_lock_irqsave(vhost->host->host_lock, flags);
  1921. ibmvfc_free_event(evt);
  1922. unlock_out:
  1923. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1924. LEAVE;
  1925. return rc;
  1926. }
  1927. /**
  1928. * ibmvfc_bsg_request - Handle a BSG request
  1929. * @job: struct bsg_job to be executed
  1930. *
  1931. * Returns:
  1932. * 0 on success / other on failure
  1933. **/
  1934. static int ibmvfc_bsg_request(struct bsg_job *job)
  1935. {
  1936. struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
  1937. struct fc_rport *rport = fc_bsg_to_rport(job);
  1938. struct ibmvfc_passthru_mad *mad;
  1939. struct ibmvfc_event *evt;
  1940. union ibmvfc_iu rsp_iu;
  1941. unsigned long flags, port_id = -1;
  1942. struct fc_bsg_request *bsg_request = job->request;
  1943. struct fc_bsg_reply *bsg_reply = job->reply;
  1944. unsigned int code = bsg_request->msgcode;
  1945. int rc = 0, req_seg, rsp_seg, issue_login = 0;
  1946. u32 fc_flags, rsp_len;
  1947. ENTER;
  1948. bsg_reply->reply_payload_rcv_len = 0;
  1949. if (rport)
  1950. port_id = rport->port_id;
  1951. switch (code) {
  1952. case FC_BSG_HST_ELS_NOLOGIN:
  1953. port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
  1954. (bsg_request->rqst_data.h_els.port_id[1] << 8) |
  1955. bsg_request->rqst_data.h_els.port_id[2];
  1956. fallthrough;
  1957. case FC_BSG_RPT_ELS:
  1958. fc_flags = IBMVFC_FC_ELS;
  1959. break;
  1960. case FC_BSG_HST_CT:
  1961. issue_login = 1;
  1962. port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
  1963. (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
  1964. bsg_request->rqst_data.h_ct.port_id[2];
  1965. fallthrough;
  1966. case FC_BSG_RPT_CT:
  1967. fc_flags = IBMVFC_FC_CT_IU;
  1968. break;
  1969. default:
  1970. return -ENOTSUPP;
  1971. }
  1972. if (port_id == -1)
  1973. return -EINVAL;
  1974. if (!mutex_trylock(&vhost->passthru_mutex))
  1975. return -EBUSY;
  1976. job->dd_data = (void *)port_id;
  1977. req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
  1978. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1979. if (!req_seg) {
  1980. mutex_unlock(&vhost->passthru_mutex);
  1981. return -ENOMEM;
  1982. }
  1983. rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
  1984. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1985. if (!rsp_seg) {
  1986. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1987. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1988. mutex_unlock(&vhost->passthru_mutex);
  1989. return -ENOMEM;
  1990. }
  1991. if (req_seg > 1 || rsp_seg > 1) {
  1992. rc = -EINVAL;
  1993. goto out;
  1994. }
  1995. if (issue_login)
  1996. rc = ibmvfc_bsg_plogi(vhost, port_id);
  1997. spin_lock_irqsave(vhost->host->host_lock, flags);
  1998. if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
  1999. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  2000. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2001. goto out;
  2002. }
  2003. evt = ibmvfc_get_reserved_event(&vhost->crq);
  2004. if (!evt) {
  2005. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2006. rc = -ENOMEM;
  2007. goto out;
  2008. }
  2009. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  2010. mad = &evt->iu.passthru;
  2011. memset(mad, 0, sizeof(*mad));
  2012. mad->common.version = cpu_to_be32(1);
  2013. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  2014. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  2015. mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
  2016. offsetof(struct ibmvfc_passthru_mad, iu));
  2017. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  2018. mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
  2019. mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
  2020. mad->iu.flags = cpu_to_be32(fc_flags);
  2021. mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  2022. mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
  2023. mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
  2024. mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
  2025. mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
  2026. mad->iu.scsi_id = cpu_to_be64(port_id);
  2027. mad->iu.tag = cpu_to_be64((u64)evt);
  2028. rsp_len = be32_to_cpu(mad->iu.rsp.len);
  2029. evt->sync_iu = &rsp_iu;
  2030. init_completion(&evt->comp);
  2031. rc = ibmvfc_send_event(evt, vhost, 0);
  2032. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2033. if (rc) {
  2034. rc = -EIO;
  2035. goto out;
  2036. }
  2037. wait_for_completion(&evt->comp);
  2038. if (rsp_iu.passthru.common.status)
  2039. rc = -EIO;
  2040. else
  2041. bsg_reply->reply_payload_rcv_len = rsp_len;
  2042. spin_lock_irqsave(vhost->host->host_lock, flags);
  2043. ibmvfc_free_event(evt);
  2044. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2045. bsg_reply->result = rc;
  2046. bsg_job_done(job, bsg_reply->result,
  2047. bsg_reply->reply_payload_rcv_len);
  2048. rc = 0;
  2049. out:
  2050. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  2051. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  2052. dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
  2053. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  2054. mutex_unlock(&vhost->passthru_mutex);
  2055. LEAVE;
  2056. return rc;
  2057. }
  2058. /**
  2059. * ibmvfc_reset_device - Reset the device with the specified reset type
  2060. * @sdev: scsi device to reset
  2061. * @type: reset type
  2062. * @desc: reset type description for log messages
  2063. *
  2064. * Returns:
  2065. * 0 on success / other on failure
  2066. **/
  2067. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  2068. {
  2069. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2070. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2071. struct ibmvfc_cmd *tmf;
  2072. struct ibmvfc_event *evt = NULL;
  2073. union ibmvfc_iu rsp_iu;
  2074. struct ibmvfc_fcp_cmd_iu *iu;
  2075. struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
  2076. int rsp_rc = -EBUSY;
  2077. unsigned long flags;
  2078. int rsp_code = 0;
  2079. spin_lock_irqsave(vhost->host->host_lock, flags);
  2080. if (vhost->state == IBMVFC_ACTIVE) {
  2081. if (vhost->using_channels)
  2082. evt = ibmvfc_get_event(&vhost->scsi_scrqs.scrqs[0]);
  2083. else
  2084. evt = ibmvfc_get_event(&vhost->crq);
  2085. if (!evt) {
  2086. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2087. return -ENOMEM;
  2088. }
  2089. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2090. tmf = ibmvfc_init_vfc_cmd(evt, sdev);
  2091. iu = ibmvfc_get_fcp_iu(vhost, tmf);
  2092. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2093. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  2094. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2095. iu->tmf_flags = type;
  2096. evt->sync_iu = &rsp_iu;
  2097. init_completion(&evt->comp);
  2098. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2099. }
  2100. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2101. if (rsp_rc != 0) {
  2102. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  2103. desc, rsp_rc);
  2104. return -EIO;
  2105. }
  2106. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  2107. wait_for_completion(&evt->comp);
  2108. if (rsp_iu.cmd.status)
  2109. rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
  2110. if (rsp_code) {
  2111. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2112. rsp_code = fc_rsp->data.info.rsp_code;
  2113. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  2114. "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
  2115. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2116. be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
  2117. fc_rsp->scsi_status);
  2118. rsp_rc = -EIO;
  2119. } else
  2120. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  2121. spin_lock_irqsave(vhost->host->host_lock, flags);
  2122. ibmvfc_free_event(evt);
  2123. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2124. return rsp_rc;
  2125. }
  2126. /**
  2127. * ibmvfc_match_rport - Match function for specified remote port
  2128. * @evt: ibmvfc event struct
  2129. * @rport: device to match
  2130. *
  2131. * Returns:
  2132. * 1 if event matches rport / 0 if event does not match rport
  2133. **/
  2134. static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
  2135. {
  2136. struct fc_rport *cmd_rport;
  2137. if (evt->cmnd) {
  2138. cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
  2139. if (cmd_rport == rport)
  2140. return 1;
  2141. }
  2142. return 0;
  2143. }
  2144. /**
  2145. * ibmvfc_match_target - Match function for specified target
  2146. * @evt: ibmvfc event struct
  2147. * @device: device to match (starget)
  2148. *
  2149. * Returns:
  2150. * 1 if event matches starget / 0 if event does not match starget
  2151. **/
  2152. static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
  2153. {
  2154. if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
  2155. return 1;
  2156. return 0;
  2157. }
  2158. /**
  2159. * ibmvfc_match_lun - Match function for specified LUN
  2160. * @evt: ibmvfc event struct
  2161. * @device: device to match (sdev)
  2162. *
  2163. * Returns:
  2164. * 1 if event matches sdev / 0 if event does not match sdev
  2165. **/
  2166. static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
  2167. {
  2168. if (evt->cmnd && evt->cmnd->device == device)
  2169. return 1;
  2170. return 0;
  2171. }
  2172. /**
  2173. * ibmvfc_event_is_free - Check if event is free or not
  2174. * @evt: ibmvfc event struct
  2175. *
  2176. * Returns:
  2177. * true / false
  2178. **/
  2179. static bool ibmvfc_event_is_free(struct ibmvfc_event *evt)
  2180. {
  2181. struct ibmvfc_event *loop_evt;
  2182. list_for_each_entry(loop_evt, &evt->queue->free, queue_list)
  2183. if (loop_evt == evt)
  2184. return true;
  2185. return false;
  2186. }
  2187. /**
  2188. * ibmvfc_wait_for_ops - Wait for ops to complete
  2189. * @vhost: ibmvfc host struct
  2190. * @device: device to match (starget or sdev)
  2191. * @match: match function
  2192. *
  2193. * Returns:
  2194. * SUCCESS / FAILED
  2195. **/
  2196. static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
  2197. int (*match) (struct ibmvfc_event *, void *))
  2198. {
  2199. struct ibmvfc_event *evt;
  2200. DECLARE_COMPLETION_ONSTACK(comp);
  2201. int wait, i, q_index, q_size;
  2202. unsigned long flags;
  2203. signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
  2204. struct ibmvfc_queue *queues;
  2205. ENTER;
  2206. if (vhost->mq_enabled && vhost->using_channels) {
  2207. queues = vhost->scsi_scrqs.scrqs;
  2208. q_size = vhost->scsi_scrqs.active_queues;
  2209. } else {
  2210. queues = &vhost->crq;
  2211. q_size = 1;
  2212. }
  2213. do {
  2214. wait = 0;
  2215. spin_lock_irqsave(vhost->host->host_lock, flags);
  2216. for (q_index = 0; q_index < q_size; q_index++) {
  2217. spin_lock(&queues[q_index].l_lock);
  2218. for (i = 0; i < queues[q_index].evt_pool.size; i++) {
  2219. evt = &queues[q_index].evt_pool.events[i];
  2220. if (!ibmvfc_event_is_free(evt)) {
  2221. if (match(evt, device)) {
  2222. evt->eh_comp = &comp;
  2223. wait++;
  2224. }
  2225. }
  2226. }
  2227. spin_unlock(&queues[q_index].l_lock);
  2228. }
  2229. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2230. if (wait) {
  2231. timeout = wait_for_completion_timeout(&comp, timeout);
  2232. if (!timeout) {
  2233. wait = 0;
  2234. spin_lock_irqsave(vhost->host->host_lock, flags);
  2235. for (q_index = 0; q_index < q_size; q_index++) {
  2236. spin_lock(&queues[q_index].l_lock);
  2237. for (i = 0; i < queues[q_index].evt_pool.size; i++) {
  2238. evt = &queues[q_index].evt_pool.events[i];
  2239. if (!ibmvfc_event_is_free(evt)) {
  2240. if (match(evt, device)) {
  2241. evt->eh_comp = NULL;
  2242. wait++;
  2243. }
  2244. }
  2245. }
  2246. spin_unlock(&queues[q_index].l_lock);
  2247. }
  2248. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2249. if (wait)
  2250. dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
  2251. LEAVE;
  2252. return wait ? FAILED : SUCCESS;
  2253. }
  2254. }
  2255. } while (wait);
  2256. LEAVE;
  2257. return SUCCESS;
  2258. }
  2259. static struct ibmvfc_event *ibmvfc_init_tmf(struct ibmvfc_queue *queue,
  2260. struct scsi_device *sdev,
  2261. int type)
  2262. {
  2263. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2264. struct scsi_target *starget = scsi_target(sdev);
  2265. struct fc_rport *rport = starget_to_rport(starget);
  2266. struct ibmvfc_event *evt;
  2267. struct ibmvfc_tmf *tmf;
  2268. evt = ibmvfc_get_reserved_event(queue);
  2269. if (!evt)
  2270. return NULL;
  2271. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  2272. tmf = &evt->iu.tmf;
  2273. memset(tmf, 0, sizeof(*tmf));
  2274. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  2275. tmf->common.version = cpu_to_be32(2);
  2276. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2277. } else {
  2278. tmf->common.version = cpu_to_be32(1);
  2279. }
  2280. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  2281. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  2282. tmf->scsi_id = cpu_to_be64(rport->port_id);
  2283. int_to_scsilun(sdev->lun, &tmf->lun);
  2284. if (!ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPRESS_ABTS))
  2285. type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
  2286. if (vhost->state == IBMVFC_ACTIVE)
  2287. tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
  2288. else
  2289. tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
  2290. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  2291. tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
  2292. init_completion(&evt->comp);
  2293. return evt;
  2294. }
  2295. static int ibmvfc_cancel_all_mq(struct scsi_device *sdev, int type)
  2296. {
  2297. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2298. struct ibmvfc_event *evt, *found_evt, *temp;
  2299. struct ibmvfc_queue *queues = vhost->scsi_scrqs.scrqs;
  2300. unsigned long flags;
  2301. int num_hwq, i;
  2302. int fail = 0;
  2303. LIST_HEAD(cancelq);
  2304. u16 status;
  2305. ENTER;
  2306. spin_lock_irqsave(vhost->host->host_lock, flags);
  2307. num_hwq = vhost->scsi_scrqs.active_queues;
  2308. for (i = 0; i < num_hwq; i++) {
  2309. spin_lock(queues[i].q_lock);
  2310. spin_lock(&queues[i].l_lock);
  2311. found_evt = NULL;
  2312. list_for_each_entry(evt, &queues[i].sent, queue_list) {
  2313. if (evt->cmnd && evt->cmnd->device == sdev) {
  2314. found_evt = evt;
  2315. break;
  2316. }
  2317. }
  2318. spin_unlock(&queues[i].l_lock);
  2319. if (found_evt && vhost->logged_in) {
  2320. evt = ibmvfc_init_tmf(&queues[i], sdev, type);
  2321. if (!evt) {
  2322. spin_unlock(queues[i].q_lock);
  2323. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2324. return -ENOMEM;
  2325. }
  2326. evt->sync_iu = &queues[i].cancel_rsp;
  2327. ibmvfc_send_event(evt, vhost, default_timeout);
  2328. list_add_tail(&evt->cancel, &cancelq);
  2329. }
  2330. spin_unlock(queues[i].q_lock);
  2331. }
  2332. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2333. if (list_empty(&cancelq)) {
  2334. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2335. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  2336. return 0;
  2337. }
  2338. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  2339. list_for_each_entry_safe(evt, temp, &cancelq, cancel) {
  2340. wait_for_completion(&evt->comp);
  2341. status = be16_to_cpu(evt->queue->cancel_rsp.mad_common.status);
  2342. list_del(&evt->cancel);
  2343. ibmvfc_free_event(evt);
  2344. if (status != IBMVFC_MAD_SUCCESS) {
  2345. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  2346. switch (status) {
  2347. case IBMVFC_MAD_DRIVER_FAILED:
  2348. case IBMVFC_MAD_CRQ_ERROR:
  2349. /* Host adapter most likely going through reset, return success to
  2350. * the caller will wait for the command being cancelled to get returned
  2351. */
  2352. break;
  2353. default:
  2354. fail = 1;
  2355. break;
  2356. }
  2357. }
  2358. }
  2359. if (fail)
  2360. return -EIO;
  2361. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  2362. LEAVE;
  2363. return 0;
  2364. }
  2365. static int ibmvfc_cancel_all_sq(struct scsi_device *sdev, int type)
  2366. {
  2367. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2368. struct ibmvfc_event *evt, *found_evt;
  2369. union ibmvfc_iu rsp;
  2370. int rsp_rc = -EBUSY;
  2371. unsigned long flags;
  2372. u16 status;
  2373. ENTER;
  2374. found_evt = NULL;
  2375. spin_lock_irqsave(vhost->host->host_lock, flags);
  2376. spin_lock(&vhost->crq.l_lock);
  2377. list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
  2378. if (evt->cmnd && evt->cmnd->device == sdev) {
  2379. found_evt = evt;
  2380. break;
  2381. }
  2382. }
  2383. spin_unlock(&vhost->crq.l_lock);
  2384. if (!found_evt) {
  2385. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2386. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  2387. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2388. return 0;
  2389. }
  2390. if (vhost->logged_in) {
  2391. evt = ibmvfc_init_tmf(&vhost->crq, sdev, type);
  2392. evt->sync_iu = &rsp;
  2393. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2394. }
  2395. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2396. if (rsp_rc != 0) {
  2397. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  2398. /* If failure is received, the host adapter is most likely going
  2399. through reset, return success so the caller will wait for the command
  2400. being cancelled to get returned */
  2401. return 0;
  2402. }
  2403. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  2404. wait_for_completion(&evt->comp);
  2405. status = be16_to_cpu(rsp.mad_common.status);
  2406. spin_lock_irqsave(vhost->host->host_lock, flags);
  2407. ibmvfc_free_event(evt);
  2408. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2409. if (status != IBMVFC_MAD_SUCCESS) {
  2410. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  2411. switch (status) {
  2412. case IBMVFC_MAD_DRIVER_FAILED:
  2413. case IBMVFC_MAD_CRQ_ERROR:
  2414. /* Host adapter most likely going through reset, return success to
  2415. the caller will wait for the command being cancelled to get returned */
  2416. return 0;
  2417. default:
  2418. return -EIO;
  2419. };
  2420. }
  2421. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  2422. return 0;
  2423. }
  2424. /**
  2425. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  2426. * @sdev: scsi device to cancel commands
  2427. * @type: type of error recovery being performed
  2428. *
  2429. * This sends a cancel to the VIOS for the specified device. This does
  2430. * NOT send any abort to the actual device. That must be done separately.
  2431. *
  2432. * Returns:
  2433. * 0 on success / other on failure
  2434. **/
  2435. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  2436. {
  2437. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2438. if (vhost->mq_enabled && vhost->using_channels)
  2439. return ibmvfc_cancel_all_mq(sdev, type);
  2440. else
  2441. return ibmvfc_cancel_all_sq(sdev, type);
  2442. }
  2443. /**
  2444. * ibmvfc_match_key - Match function for specified cancel key
  2445. * @evt: ibmvfc event struct
  2446. * @key: cancel key to match
  2447. *
  2448. * Returns:
  2449. * 1 if event matches key / 0 if event does not match key
  2450. **/
  2451. static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
  2452. {
  2453. unsigned long cancel_key = (unsigned long)key;
  2454. if (evt->crq.format == IBMVFC_CMD_FORMAT &&
  2455. be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
  2456. return 1;
  2457. return 0;
  2458. }
  2459. /**
  2460. * ibmvfc_match_evt - Match function for specified event
  2461. * @evt: ibmvfc event struct
  2462. * @match: event to match
  2463. *
  2464. * Returns:
  2465. * 1 if event matches key / 0 if event does not match key
  2466. **/
  2467. static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
  2468. {
  2469. if (evt == match)
  2470. return 1;
  2471. return 0;
  2472. }
  2473. /**
  2474. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  2475. * @sdev: scsi device to abort commands
  2476. *
  2477. * This sends an Abort Task Set to the VIOS for the specified device. This does
  2478. * NOT send any cancel to the VIOS. That must be done separately.
  2479. *
  2480. * Returns:
  2481. * 0 on success / other on failure
  2482. **/
  2483. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  2484. {
  2485. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2486. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2487. struct ibmvfc_cmd *tmf;
  2488. struct ibmvfc_event *evt, *found_evt;
  2489. union ibmvfc_iu rsp_iu;
  2490. struct ibmvfc_fcp_cmd_iu *iu;
  2491. struct ibmvfc_fcp_rsp *fc_rsp = ibmvfc_get_fcp_rsp(vhost, &rsp_iu.cmd);
  2492. int rc, rsp_rc = -EBUSY;
  2493. unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
  2494. int rsp_code = 0;
  2495. found_evt = NULL;
  2496. spin_lock_irqsave(vhost->host->host_lock, flags);
  2497. spin_lock(&vhost->crq.l_lock);
  2498. list_for_each_entry(evt, &vhost->crq.sent, queue_list) {
  2499. if (evt->cmnd && evt->cmnd->device == sdev) {
  2500. found_evt = evt;
  2501. break;
  2502. }
  2503. }
  2504. spin_unlock(&vhost->crq.l_lock);
  2505. if (!found_evt) {
  2506. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2507. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  2508. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2509. return 0;
  2510. }
  2511. if (vhost->state == IBMVFC_ACTIVE) {
  2512. evt = ibmvfc_get_event(&vhost->crq);
  2513. if (!evt) {
  2514. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2515. return -ENOMEM;
  2516. }
  2517. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2518. tmf = ibmvfc_init_vfc_cmd(evt, sdev);
  2519. iu = ibmvfc_get_fcp_iu(vhost, tmf);
  2520. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN))
  2521. tmf->target_wwpn = cpu_to_be64(rport->port_name);
  2522. iu->tmf_flags = IBMVFC_ABORT_TASK_SET;
  2523. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2524. evt->sync_iu = &rsp_iu;
  2525. tmf->correlation = cpu_to_be64((u64)evt);
  2526. init_completion(&evt->comp);
  2527. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2528. }
  2529. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2530. if (rsp_rc != 0) {
  2531. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  2532. return -EIO;
  2533. }
  2534. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  2535. timeout = wait_for_completion_timeout(&evt->comp, timeout);
  2536. if (!timeout) {
  2537. rc = ibmvfc_cancel_all(sdev, 0);
  2538. if (!rc) {
  2539. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2540. if (rc == SUCCESS)
  2541. rc = 0;
  2542. }
  2543. if (rc) {
  2544. sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
  2545. ibmvfc_reset_host(vhost);
  2546. rsp_rc = -EIO;
  2547. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2548. if (rc == SUCCESS)
  2549. rsp_rc = 0;
  2550. rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
  2551. if (rc != SUCCESS) {
  2552. spin_lock_irqsave(vhost->host->host_lock, flags);
  2553. ibmvfc_hard_reset_host(vhost);
  2554. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2555. rsp_rc = 0;
  2556. }
  2557. goto out;
  2558. }
  2559. }
  2560. if (rsp_iu.cmd.status)
  2561. rsp_code = ibmvfc_get_err_result(vhost, &rsp_iu.cmd);
  2562. if (rsp_code) {
  2563. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2564. rsp_code = fc_rsp->data.info.rsp_code;
  2565. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  2566. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  2567. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2568. be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
  2569. fc_rsp->scsi_status);
  2570. rsp_rc = -EIO;
  2571. } else
  2572. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  2573. out:
  2574. spin_lock_irqsave(vhost->host->host_lock, flags);
  2575. ibmvfc_free_event(evt);
  2576. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2577. return rsp_rc;
  2578. }
  2579. /**
  2580. * ibmvfc_eh_abort_handler - Abort a command
  2581. * @cmd: scsi command to abort
  2582. *
  2583. * Returns:
  2584. * SUCCESS / FAST_IO_FAIL / FAILED
  2585. **/
  2586. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  2587. {
  2588. struct scsi_device *sdev = cmd->device;
  2589. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2590. int cancel_rc, block_rc;
  2591. int rc = FAILED;
  2592. ENTER;
  2593. block_rc = fc_block_scsi_eh(cmd);
  2594. ibmvfc_wait_while_resetting(vhost);
  2595. if (block_rc != FAST_IO_FAIL) {
  2596. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2597. ibmvfc_abort_task_set(sdev);
  2598. } else
  2599. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2600. if (!cancel_rc)
  2601. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2602. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2603. rc = FAST_IO_FAIL;
  2604. LEAVE;
  2605. return rc;
  2606. }
  2607. /**
  2608. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  2609. * @cmd: scsi command struct
  2610. *
  2611. * Returns:
  2612. * SUCCESS / FAST_IO_FAIL / FAILED
  2613. **/
  2614. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  2615. {
  2616. struct scsi_device *sdev = cmd->device;
  2617. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2618. int cancel_rc, block_rc, reset_rc = 0;
  2619. int rc = FAILED;
  2620. ENTER;
  2621. block_rc = fc_block_scsi_eh(cmd);
  2622. ibmvfc_wait_while_resetting(vhost);
  2623. if (block_rc != FAST_IO_FAIL) {
  2624. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
  2625. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
  2626. } else
  2627. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2628. if (!cancel_rc && !reset_rc)
  2629. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2630. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2631. rc = FAST_IO_FAIL;
  2632. LEAVE;
  2633. return rc;
  2634. }
  2635. /**
  2636. * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
  2637. * @sdev: scsi device struct
  2638. * @data: return code
  2639. *
  2640. **/
  2641. static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
  2642. {
  2643. unsigned long *rc = data;
  2644. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2645. }
  2646. /**
  2647. * ibmvfc_eh_target_reset_handler - Reset the target
  2648. * @cmd: scsi command struct
  2649. *
  2650. * Returns:
  2651. * SUCCESS / FAST_IO_FAIL / FAILED
  2652. **/
  2653. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  2654. {
  2655. struct scsi_target *starget = scsi_target(cmd->device);
  2656. struct fc_rport *rport = starget_to_rport(starget);
  2657. struct Scsi_Host *shost = rport_to_shost(rport);
  2658. struct ibmvfc_host *vhost = shost_priv(shost);
  2659. int block_rc;
  2660. int reset_rc = 0;
  2661. int rc = FAILED;
  2662. unsigned long cancel_rc = 0;
  2663. bool tgt_reset = false;
  2664. ENTER;
  2665. block_rc = fc_block_rport(rport);
  2666. ibmvfc_wait_while_resetting(vhost);
  2667. if (block_rc != FAST_IO_FAIL) {
  2668. struct scsi_device *sdev;
  2669. shost_for_each_device(sdev, shost) {
  2670. if ((sdev->channel != starget->channel) ||
  2671. (sdev->id != starget->id))
  2672. continue;
  2673. cancel_rc |= ibmvfc_cancel_all(sdev,
  2674. IBMVFC_TMF_TGT_RESET);
  2675. if (!tgt_reset) {
  2676. reset_rc = ibmvfc_reset_device(sdev,
  2677. IBMVFC_TARGET_RESET, "target");
  2678. tgt_reset = true;
  2679. }
  2680. }
  2681. } else
  2682. starget_for_each_device(starget, &cancel_rc,
  2683. ibmvfc_dev_cancel_all_noreset);
  2684. if (!cancel_rc && !reset_rc)
  2685. rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
  2686. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2687. rc = FAST_IO_FAIL;
  2688. LEAVE;
  2689. return rc;
  2690. }
  2691. /**
  2692. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  2693. * @cmd: struct scsi_cmnd having problems
  2694. *
  2695. **/
  2696. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2697. {
  2698. int rc;
  2699. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  2700. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  2701. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  2702. return rc ? FAILED : SUCCESS;
  2703. }
  2704. /**
  2705. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  2706. * @rport: rport struct
  2707. *
  2708. * Return value:
  2709. * none
  2710. **/
  2711. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  2712. {
  2713. struct Scsi_Host *shost = rport_to_shost(rport);
  2714. struct ibmvfc_host *vhost = shost_priv(shost);
  2715. struct fc_rport *dev_rport;
  2716. struct scsi_device *sdev;
  2717. struct ibmvfc_target *tgt;
  2718. unsigned long rc, flags;
  2719. unsigned int found;
  2720. ENTER;
  2721. shost_for_each_device(sdev, shost) {
  2722. dev_rport = starget_to_rport(scsi_target(sdev));
  2723. if (dev_rport != rport)
  2724. continue;
  2725. ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2726. }
  2727. rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
  2728. if (rc == FAILED)
  2729. ibmvfc_issue_fc_host_lip(shost);
  2730. spin_lock_irqsave(shost->host_lock, flags);
  2731. found = 0;
  2732. list_for_each_entry(tgt, &vhost->targets, queue) {
  2733. if (tgt->scsi_id == rport->port_id) {
  2734. found++;
  2735. break;
  2736. }
  2737. }
  2738. if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  2739. /*
  2740. * If we get here, that means we previously attempted to send
  2741. * an implicit logout to the target but it failed, most likely
  2742. * due to I/O being pending, so we need to send it again
  2743. */
  2744. ibmvfc_del_tgt(tgt);
  2745. ibmvfc_reinit_host(vhost);
  2746. }
  2747. spin_unlock_irqrestore(shost->host_lock, flags);
  2748. LEAVE;
  2749. }
  2750. static const struct ibmvfc_async_desc ae_desc [] = {
  2751. { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2752. { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2753. { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2754. { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2755. { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2756. { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
  2757. { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
  2758. { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
  2759. { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
  2760. { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
  2761. { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
  2762. { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
  2763. { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
  2764. };
  2765. static const struct ibmvfc_async_desc unknown_ae = {
  2766. "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
  2767. };
  2768. /**
  2769. * ibmvfc_get_ae_desc - Get text description for async event
  2770. * @ae: async event
  2771. *
  2772. **/
  2773. static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
  2774. {
  2775. int i;
  2776. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  2777. if (ae_desc[i].ae == ae)
  2778. return &ae_desc[i];
  2779. return &unknown_ae;
  2780. }
  2781. static const struct {
  2782. enum ibmvfc_ae_link_state state;
  2783. const char *desc;
  2784. } link_desc [] = {
  2785. { IBMVFC_AE_LS_LINK_UP, " link up" },
  2786. { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
  2787. { IBMVFC_AE_LS_LINK_DOWN, " link down" },
  2788. { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
  2789. };
  2790. /**
  2791. * ibmvfc_get_link_state - Get text description for link state
  2792. * @state: link state
  2793. *
  2794. **/
  2795. static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
  2796. {
  2797. int i;
  2798. for (i = 0; i < ARRAY_SIZE(link_desc); i++)
  2799. if (link_desc[i].state == state)
  2800. return link_desc[i].desc;
  2801. return "";
  2802. }
  2803. /**
  2804. * ibmvfc_handle_async - Handle an async event from the adapter
  2805. * @crq: crq to process
  2806. * @vhost: ibmvfc host struct
  2807. *
  2808. **/
  2809. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  2810. struct ibmvfc_host *vhost)
  2811. {
  2812. const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
  2813. struct ibmvfc_target *tgt;
  2814. ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
  2815. " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
  2816. be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
  2817. ibmvfc_get_link_state(crq->link_state));
  2818. switch (be64_to_cpu(crq->event)) {
  2819. case IBMVFC_AE_RESUME:
  2820. switch (crq->link_state) {
  2821. case IBMVFC_AE_LS_LINK_DOWN:
  2822. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2823. break;
  2824. case IBMVFC_AE_LS_LINK_DEAD:
  2825. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2826. break;
  2827. case IBMVFC_AE_LS_LINK_UP:
  2828. case IBMVFC_AE_LS_LINK_BOUNCED:
  2829. default:
  2830. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2831. vhost->delay_init = 1;
  2832. __ibmvfc_reset_host(vhost);
  2833. break;
  2834. }
  2835. break;
  2836. case IBMVFC_AE_LINK_UP:
  2837. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2838. vhost->delay_init = 1;
  2839. __ibmvfc_reset_host(vhost);
  2840. break;
  2841. case IBMVFC_AE_SCN_FABRIC:
  2842. case IBMVFC_AE_SCN_DOMAIN:
  2843. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2844. if (vhost->state < IBMVFC_HALTED) {
  2845. vhost->delay_init = 1;
  2846. __ibmvfc_reset_host(vhost);
  2847. }
  2848. break;
  2849. case IBMVFC_AE_SCN_NPORT:
  2850. case IBMVFC_AE_SCN_GROUP:
  2851. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2852. ibmvfc_reinit_host(vhost);
  2853. break;
  2854. case IBMVFC_AE_ELS_LOGO:
  2855. case IBMVFC_AE_ELS_PRLO:
  2856. case IBMVFC_AE_ELS_PLOGI:
  2857. list_for_each_entry(tgt, &vhost->targets, queue) {
  2858. if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
  2859. break;
  2860. if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
  2861. continue;
  2862. if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
  2863. continue;
  2864. if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
  2865. continue;
  2866. if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
  2867. tgt->logo_rcvd = 1;
  2868. if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
  2869. ibmvfc_del_tgt(tgt);
  2870. ibmvfc_reinit_host(vhost);
  2871. }
  2872. }
  2873. break;
  2874. case IBMVFC_AE_LINK_DOWN:
  2875. case IBMVFC_AE_ADAPTER_FAILED:
  2876. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2877. break;
  2878. case IBMVFC_AE_LINK_DEAD:
  2879. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2880. break;
  2881. case IBMVFC_AE_HALT:
  2882. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  2883. break;
  2884. default:
  2885. dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
  2886. break;
  2887. }
  2888. }
  2889. /**
  2890. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  2891. * @crq: Command/Response queue
  2892. * @vhost: ibmvfc host struct
  2893. * @evt_doneq: Event done queue
  2894. *
  2895. **/
  2896. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
  2897. struct list_head *evt_doneq)
  2898. {
  2899. long rc;
  2900. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  2901. switch (crq->valid) {
  2902. case IBMVFC_CRQ_INIT_RSP:
  2903. switch (crq->format) {
  2904. case IBMVFC_CRQ_INIT:
  2905. dev_info(vhost->dev, "Partner initialized\n");
  2906. /* Send back a response */
  2907. rc = ibmvfc_send_crq_init_complete(vhost);
  2908. if (rc == 0)
  2909. ibmvfc_init_host(vhost);
  2910. else
  2911. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  2912. break;
  2913. case IBMVFC_CRQ_INIT_COMPLETE:
  2914. dev_info(vhost->dev, "Partner initialization complete\n");
  2915. ibmvfc_init_host(vhost);
  2916. break;
  2917. default:
  2918. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  2919. }
  2920. return;
  2921. case IBMVFC_CRQ_XPORT_EVENT:
  2922. vhost->state = IBMVFC_NO_CRQ;
  2923. vhost->logged_in = 0;
  2924. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  2925. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  2926. /* We need to re-setup the interpartition connection */
  2927. dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
  2928. vhost->client_migrated = 1;
  2929. scsi_block_requests(vhost->host);
  2930. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  2931. ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
  2932. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
  2933. wake_up(&vhost->work_wait_q);
  2934. } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
  2935. dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
  2936. ibmvfc_purge_requests(vhost, DID_ERROR);
  2937. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2938. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  2939. } else {
  2940. dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
  2941. }
  2942. return;
  2943. case IBMVFC_CRQ_CMD_RSP:
  2944. break;
  2945. default:
  2946. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  2947. return;
  2948. }
  2949. if (crq->format == IBMVFC_ASYNC_EVENT)
  2950. return;
  2951. /* The only kind of payload CRQs we should get are responses to
  2952. * things we send. Make sure this response is to something we
  2953. * actually sent
  2954. */
  2955. if (unlikely(!ibmvfc_valid_event(&vhost->crq.evt_pool, evt))) {
  2956. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  2957. crq->ioba);
  2958. return;
  2959. }
  2960. if (unlikely(atomic_dec_if_positive(&evt->active))) {
  2961. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  2962. crq->ioba);
  2963. return;
  2964. }
  2965. spin_lock(&evt->queue->l_lock);
  2966. list_move_tail(&evt->queue_list, evt_doneq);
  2967. spin_unlock(&evt->queue->l_lock);
  2968. }
  2969. /**
  2970. * ibmvfc_scan_finished - Check if the device scan is done.
  2971. * @shost: scsi host struct
  2972. * @time: current elapsed time
  2973. *
  2974. * Returns:
  2975. * 0 if scan is not done / 1 if scan is done
  2976. **/
  2977. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  2978. {
  2979. unsigned long flags;
  2980. struct ibmvfc_host *vhost = shost_priv(shost);
  2981. int done = 0;
  2982. spin_lock_irqsave(shost->host_lock, flags);
  2983. if (!vhost->scan_timeout)
  2984. done = 1;
  2985. else if (time >= (vhost->scan_timeout * HZ)) {
  2986. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  2987. "continuing initialization\n", vhost->scan_timeout);
  2988. done = 1;
  2989. }
  2990. if (vhost->scan_complete) {
  2991. vhost->scan_timeout = init_timeout;
  2992. done = 1;
  2993. }
  2994. spin_unlock_irqrestore(shost->host_lock, flags);
  2995. return done;
  2996. }
  2997. /**
  2998. * ibmvfc_sdev_init - Setup the device's task set value
  2999. * @sdev: struct scsi_device device to configure
  3000. *
  3001. * Set the device's task set value so that error handling works as
  3002. * expected.
  3003. *
  3004. * Returns:
  3005. * 0 on success / -ENXIO if device does not exist
  3006. **/
  3007. static int ibmvfc_sdev_init(struct scsi_device *sdev)
  3008. {
  3009. struct Scsi_Host *shost = sdev->host;
  3010. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3011. struct ibmvfc_host *vhost = shost_priv(shost);
  3012. unsigned long flags = 0;
  3013. if (!rport || fc_remote_port_chkready(rport))
  3014. return -ENXIO;
  3015. spin_lock_irqsave(shost->host_lock, flags);
  3016. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  3017. spin_unlock_irqrestore(shost->host_lock, flags);
  3018. return 0;
  3019. }
  3020. /**
  3021. * ibmvfc_target_alloc - Setup the target's task set value
  3022. * @starget: struct scsi_target
  3023. *
  3024. * Set the target's task set value so that error handling works as
  3025. * expected.
  3026. *
  3027. * Returns:
  3028. * 0 on success / -ENXIO if device does not exist
  3029. **/
  3030. static int ibmvfc_target_alloc(struct scsi_target *starget)
  3031. {
  3032. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  3033. struct ibmvfc_host *vhost = shost_priv(shost);
  3034. unsigned long flags = 0;
  3035. spin_lock_irqsave(shost->host_lock, flags);
  3036. starget->hostdata = (void *)(unsigned long)vhost->task_set++;
  3037. spin_unlock_irqrestore(shost->host_lock, flags);
  3038. return 0;
  3039. }
  3040. /**
  3041. * ibmvfc_sdev_configure - Configure the device
  3042. * @sdev: struct scsi_device device to configure
  3043. * @lim: Request queue limits
  3044. *
  3045. * Enable allow_restart for a device if it is a disk. Adjust the
  3046. * queue_depth here also.
  3047. *
  3048. * Returns:
  3049. * 0
  3050. **/
  3051. static int ibmvfc_sdev_configure(struct scsi_device *sdev,
  3052. struct queue_limits *lim)
  3053. {
  3054. struct Scsi_Host *shost = sdev->host;
  3055. unsigned long flags = 0;
  3056. spin_lock_irqsave(shost->host_lock, flags);
  3057. if (sdev->type == TYPE_DISK) {
  3058. sdev->allow_restart = 1;
  3059. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  3060. }
  3061. spin_unlock_irqrestore(shost->host_lock, flags);
  3062. return 0;
  3063. }
  3064. /**
  3065. * ibmvfc_change_queue_depth - Change the device's queue depth
  3066. * @sdev: scsi device struct
  3067. * @qdepth: depth to set
  3068. *
  3069. * Return value:
  3070. * actual depth set
  3071. **/
  3072. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
  3073. {
  3074. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  3075. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  3076. return scsi_change_queue_depth(sdev, qdepth);
  3077. }
  3078. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  3079. struct device_attribute *attr, char *buf)
  3080. {
  3081. struct Scsi_Host *shost = class_to_shost(dev);
  3082. struct ibmvfc_host *vhost = shost_priv(shost);
  3083. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.partition_name);
  3084. }
  3085. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  3086. struct device_attribute *attr, char *buf)
  3087. {
  3088. struct Scsi_Host *shost = class_to_shost(dev);
  3089. struct ibmvfc_host *vhost = shost_priv(shost);
  3090. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.device_name);
  3091. }
  3092. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  3093. struct device_attribute *attr, char *buf)
  3094. {
  3095. struct Scsi_Host *shost = class_to_shost(dev);
  3096. struct ibmvfc_host *vhost = shost_priv(shost);
  3097. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.port_loc_code);
  3098. }
  3099. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  3100. struct device_attribute *attr, char *buf)
  3101. {
  3102. struct Scsi_Host *shost = class_to_shost(dev);
  3103. struct ibmvfc_host *vhost = shost_priv(shost);
  3104. return sysfs_emit(buf, "%s\n", vhost->login_buf->resp.drc_name);
  3105. }
  3106. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  3107. struct device_attribute *attr, char *buf)
  3108. {
  3109. struct Scsi_Host *shost = class_to_shost(dev);
  3110. struct ibmvfc_host *vhost = shost_priv(shost);
  3111. return sysfs_emit(buf, "%d\n",
  3112. be32_to_cpu(vhost->login_buf->resp.version));
  3113. }
  3114. static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
  3115. struct device_attribute *attr, char *buf)
  3116. {
  3117. struct Scsi_Host *shost = class_to_shost(dev);
  3118. struct ibmvfc_host *vhost = shost_priv(shost);
  3119. return sysfs_emit(buf, "%llx\n",
  3120. be64_to_cpu(vhost->login_buf->resp.capabilities));
  3121. }
  3122. /**
  3123. * ibmvfc_show_log_level - Show the adapter's error logging level
  3124. * @dev: class device struct
  3125. * @attr: unused
  3126. * @buf: buffer
  3127. *
  3128. * Return value:
  3129. * number of bytes printed to buffer
  3130. **/
  3131. static ssize_t ibmvfc_show_log_level(struct device *dev,
  3132. struct device_attribute *attr, char *buf)
  3133. {
  3134. struct Scsi_Host *shost = class_to_shost(dev);
  3135. struct ibmvfc_host *vhost = shost_priv(shost);
  3136. unsigned long flags = 0;
  3137. int len;
  3138. spin_lock_irqsave(shost->host_lock, flags);
  3139. len = sysfs_emit(buf, "%d\n", vhost->log_level);
  3140. spin_unlock_irqrestore(shost->host_lock, flags);
  3141. return len;
  3142. }
  3143. /**
  3144. * ibmvfc_store_log_level - Change the adapter's error logging level
  3145. * @dev: class device struct
  3146. * @attr: unused
  3147. * @buf: buffer
  3148. * @count: buffer size
  3149. *
  3150. * Return value:
  3151. * number of bytes printed to buffer
  3152. **/
  3153. static ssize_t ibmvfc_store_log_level(struct device *dev,
  3154. struct device_attribute *attr,
  3155. const char *buf, size_t count)
  3156. {
  3157. struct Scsi_Host *shost = class_to_shost(dev);
  3158. struct ibmvfc_host *vhost = shost_priv(shost);
  3159. unsigned long flags = 0;
  3160. spin_lock_irqsave(shost->host_lock, flags);
  3161. vhost->log_level = simple_strtoul(buf, NULL, 10);
  3162. spin_unlock_irqrestore(shost->host_lock, flags);
  3163. return strlen(buf);
  3164. }
  3165. static ssize_t ibmvfc_show_scsi_channels(struct device *dev,
  3166. struct device_attribute *attr, char *buf)
  3167. {
  3168. struct Scsi_Host *shost = class_to_shost(dev);
  3169. struct ibmvfc_host *vhost = shost_priv(shost);
  3170. struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
  3171. unsigned long flags = 0;
  3172. int len;
  3173. spin_lock_irqsave(shost->host_lock, flags);
  3174. len = sysfs_emit(buf, "%d\n", scsi->desired_queues);
  3175. spin_unlock_irqrestore(shost->host_lock, flags);
  3176. return len;
  3177. }
  3178. static ssize_t ibmvfc_store_scsi_channels(struct device *dev,
  3179. struct device_attribute *attr,
  3180. const char *buf, size_t count)
  3181. {
  3182. struct Scsi_Host *shost = class_to_shost(dev);
  3183. struct ibmvfc_host *vhost = shost_priv(shost);
  3184. struct ibmvfc_channels *scsi = &vhost->scsi_scrqs;
  3185. unsigned long flags = 0;
  3186. unsigned int channels;
  3187. spin_lock_irqsave(shost->host_lock, flags);
  3188. channels = simple_strtoul(buf, NULL, 10);
  3189. scsi->desired_queues = min(channels, shost->nr_hw_queues);
  3190. ibmvfc_hard_reset_host(vhost);
  3191. spin_unlock_irqrestore(shost->host_lock, flags);
  3192. return strlen(buf);
  3193. }
  3194. static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
  3195. static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
  3196. static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
  3197. static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
  3198. static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
  3199. static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
  3200. static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
  3201. ibmvfc_show_log_level, ibmvfc_store_log_level);
  3202. static DEVICE_ATTR(nr_scsi_channels, S_IRUGO | S_IWUSR,
  3203. ibmvfc_show_scsi_channels, ibmvfc_store_scsi_channels);
  3204. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  3205. /**
  3206. * ibmvfc_read_trace - Dump the adapter trace
  3207. * @filp: open sysfs file
  3208. * @kobj: kobject struct
  3209. * @bin_attr: bin_attribute struct
  3210. * @buf: buffer
  3211. * @off: offset
  3212. * @count: buffer size
  3213. *
  3214. * Return value:
  3215. * number of bytes printed to buffer
  3216. **/
  3217. static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
  3218. const struct bin_attribute *bin_attr,
  3219. char *buf, loff_t off, size_t count)
  3220. {
  3221. struct device *dev = kobj_to_dev(kobj);
  3222. struct Scsi_Host *shost = class_to_shost(dev);
  3223. struct ibmvfc_host *vhost = shost_priv(shost);
  3224. unsigned long flags = 0;
  3225. int size = IBMVFC_TRACE_SIZE;
  3226. char *src = (char *)vhost->trace;
  3227. if (off > size)
  3228. return 0;
  3229. if (off + count > size) {
  3230. size -= off;
  3231. count = size;
  3232. }
  3233. spin_lock_irqsave(shost->host_lock, flags);
  3234. memcpy(buf, &src[off], count);
  3235. spin_unlock_irqrestore(shost->host_lock, flags);
  3236. return count;
  3237. }
  3238. static const struct bin_attribute ibmvfc_trace_attr = {
  3239. .attr = {
  3240. .name = "trace",
  3241. .mode = S_IRUGO,
  3242. },
  3243. .size = 0,
  3244. .read = ibmvfc_read_trace,
  3245. };
  3246. #endif
  3247. static struct attribute *ibmvfc_host_attrs[] = {
  3248. &dev_attr_partition_name.attr,
  3249. &dev_attr_device_name.attr,
  3250. &dev_attr_port_loc_code.attr,
  3251. &dev_attr_drc_name.attr,
  3252. &dev_attr_npiv_version.attr,
  3253. &dev_attr_capabilities.attr,
  3254. &dev_attr_log_level.attr,
  3255. &dev_attr_nr_scsi_channels.attr,
  3256. NULL
  3257. };
  3258. ATTRIBUTE_GROUPS(ibmvfc_host);
  3259. static const struct scsi_host_template driver_template = {
  3260. .module = THIS_MODULE,
  3261. .name = "IBM POWER Virtual FC Adapter",
  3262. .proc_name = IBMVFC_NAME,
  3263. .queuecommand = ibmvfc_queuecommand,
  3264. .eh_timed_out = fc_eh_timed_out,
  3265. .eh_abort_handler = ibmvfc_eh_abort_handler,
  3266. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  3267. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  3268. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  3269. .sdev_init = ibmvfc_sdev_init,
  3270. .sdev_configure = ibmvfc_sdev_configure,
  3271. .target_alloc = ibmvfc_target_alloc,
  3272. .scan_finished = ibmvfc_scan_finished,
  3273. .change_queue_depth = ibmvfc_change_queue_depth,
  3274. .cmd_per_lun = 16,
  3275. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  3276. .this_id = -1,
  3277. .sg_tablesize = SG_ALL,
  3278. .max_sectors = IBMVFC_MAX_SECTORS,
  3279. .shost_groups = ibmvfc_host_groups,
  3280. .track_queue_depth = 1,
  3281. };
  3282. /**
  3283. * ibmvfc_next_async_crq - Returns the next entry in async queue
  3284. * @vhost: ibmvfc host struct
  3285. *
  3286. * Returns:
  3287. * Pointer to next entry in queue / NULL if empty
  3288. **/
  3289. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  3290. {
  3291. struct ibmvfc_queue *async_crq = &vhost->async_crq;
  3292. struct ibmvfc_async_crq *crq;
  3293. crq = &async_crq->msgs.async[async_crq->cur];
  3294. if (crq->valid & 0x80) {
  3295. if (++async_crq->cur == async_crq->size)
  3296. async_crq->cur = 0;
  3297. rmb();
  3298. } else
  3299. crq = NULL;
  3300. return crq;
  3301. }
  3302. /**
  3303. * ibmvfc_next_crq - Returns the next entry in message queue
  3304. * @vhost: ibmvfc host struct
  3305. *
  3306. * Returns:
  3307. * Pointer to next entry in queue / NULL if empty
  3308. **/
  3309. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  3310. {
  3311. struct ibmvfc_queue *queue = &vhost->crq;
  3312. struct ibmvfc_crq *crq;
  3313. crq = &queue->msgs.crq[queue->cur];
  3314. if (crq->valid & 0x80) {
  3315. if (++queue->cur == queue->size)
  3316. queue->cur = 0;
  3317. rmb();
  3318. } else
  3319. crq = NULL;
  3320. return crq;
  3321. }
  3322. /**
  3323. * ibmvfc_interrupt - Interrupt handler
  3324. * @irq: number of irq to handle, not used
  3325. * @dev_instance: ibmvfc_host that received interrupt
  3326. *
  3327. * Returns:
  3328. * IRQ_HANDLED
  3329. **/
  3330. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  3331. {
  3332. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  3333. unsigned long flags;
  3334. spin_lock_irqsave(vhost->host->host_lock, flags);
  3335. vio_disable_interrupts(to_vio_dev(vhost->dev));
  3336. tasklet_schedule(&vhost->tasklet);
  3337. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3338. return IRQ_HANDLED;
  3339. }
  3340. /**
  3341. * ibmvfc_tasklet - Interrupt handler tasklet
  3342. * @data: ibmvfc host struct
  3343. *
  3344. * Returns:
  3345. * Nothing
  3346. **/
  3347. static void ibmvfc_tasklet(void *data)
  3348. {
  3349. struct ibmvfc_host *vhost = data;
  3350. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  3351. struct ibmvfc_crq *crq;
  3352. struct ibmvfc_async_crq *async;
  3353. struct ibmvfc_event *evt, *temp;
  3354. unsigned long flags;
  3355. int done = 0;
  3356. LIST_HEAD(evt_doneq);
  3357. spin_lock_irqsave(vhost->host->host_lock, flags);
  3358. spin_lock(vhost->crq.q_lock);
  3359. while (!done) {
  3360. /* Pull all the valid messages off the async CRQ */
  3361. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  3362. ibmvfc_handle_async(async, vhost);
  3363. async->valid = 0;
  3364. wmb();
  3365. }
  3366. /* Pull all the valid messages off the CRQ */
  3367. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  3368. ibmvfc_handle_crq(crq, vhost, &evt_doneq);
  3369. crq->valid = 0;
  3370. wmb();
  3371. }
  3372. vio_enable_interrupts(vdev);
  3373. if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  3374. vio_disable_interrupts(vdev);
  3375. ibmvfc_handle_async(async, vhost);
  3376. async->valid = 0;
  3377. wmb();
  3378. } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  3379. vio_disable_interrupts(vdev);
  3380. ibmvfc_handle_crq(crq, vhost, &evt_doneq);
  3381. crq->valid = 0;
  3382. wmb();
  3383. } else
  3384. done = 1;
  3385. }
  3386. spin_unlock(vhost->crq.q_lock);
  3387. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3388. list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
  3389. timer_delete(&evt->timer);
  3390. list_del(&evt->queue_list);
  3391. ibmvfc_trc_end(evt);
  3392. evt->done(evt);
  3393. }
  3394. }
  3395. static int ibmvfc_toggle_scrq_irq(struct ibmvfc_queue *scrq, int enable)
  3396. {
  3397. struct device *dev = scrq->vhost->dev;
  3398. struct vio_dev *vdev = to_vio_dev(dev);
  3399. unsigned long rc;
  3400. int irq_action = H_ENABLE_VIO_INTERRUPT;
  3401. if (!enable)
  3402. irq_action = H_DISABLE_VIO_INTERRUPT;
  3403. rc = plpar_hcall_norets(H_VIOCTL, vdev->unit_address, irq_action,
  3404. scrq->hw_irq, 0, 0);
  3405. if (rc)
  3406. dev_err(dev, "Couldn't %s sub-crq[%lu] irq. rc=%ld\n",
  3407. enable ? "enable" : "disable", scrq->hwq_id, rc);
  3408. return rc;
  3409. }
  3410. static void ibmvfc_handle_scrq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost,
  3411. struct list_head *evt_doneq)
  3412. {
  3413. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  3414. switch (crq->valid) {
  3415. case IBMVFC_CRQ_CMD_RSP:
  3416. break;
  3417. case IBMVFC_CRQ_XPORT_EVENT:
  3418. return;
  3419. default:
  3420. dev_err(vhost->dev, "Got and invalid message type 0x%02x\n", crq->valid);
  3421. return;
  3422. }
  3423. /* The only kind of payload CRQs we should get are responses to
  3424. * things we send. Make sure this response is to something we
  3425. * actually sent
  3426. */
  3427. if (unlikely(!ibmvfc_valid_event(&evt->queue->evt_pool, evt))) {
  3428. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  3429. crq->ioba);
  3430. return;
  3431. }
  3432. if (unlikely(atomic_dec_if_positive(&evt->active))) {
  3433. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  3434. crq->ioba);
  3435. return;
  3436. }
  3437. spin_lock(&evt->queue->l_lock);
  3438. list_move_tail(&evt->queue_list, evt_doneq);
  3439. spin_unlock(&evt->queue->l_lock);
  3440. }
  3441. static struct ibmvfc_crq *ibmvfc_next_scrq(struct ibmvfc_queue *scrq)
  3442. {
  3443. struct ibmvfc_crq *crq;
  3444. crq = &scrq->msgs.scrq[scrq->cur].crq;
  3445. if (crq->valid & 0x80) {
  3446. if (++scrq->cur == scrq->size)
  3447. scrq->cur = 0;
  3448. rmb();
  3449. } else
  3450. crq = NULL;
  3451. return crq;
  3452. }
  3453. static void ibmvfc_drain_sub_crq(struct ibmvfc_queue *scrq)
  3454. {
  3455. struct ibmvfc_crq *crq;
  3456. struct ibmvfc_event *evt, *temp;
  3457. unsigned long flags;
  3458. int done = 0;
  3459. LIST_HEAD(evt_doneq);
  3460. spin_lock_irqsave(scrq->q_lock, flags);
  3461. while (!done) {
  3462. while ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
  3463. ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
  3464. crq->valid = 0;
  3465. wmb();
  3466. }
  3467. ibmvfc_toggle_scrq_irq(scrq, 1);
  3468. if ((crq = ibmvfc_next_scrq(scrq)) != NULL) {
  3469. ibmvfc_toggle_scrq_irq(scrq, 0);
  3470. ibmvfc_handle_scrq(crq, scrq->vhost, &evt_doneq);
  3471. crq->valid = 0;
  3472. wmb();
  3473. } else
  3474. done = 1;
  3475. }
  3476. spin_unlock_irqrestore(scrq->q_lock, flags);
  3477. list_for_each_entry_safe(evt, temp, &evt_doneq, queue_list) {
  3478. timer_delete(&evt->timer);
  3479. list_del(&evt->queue_list);
  3480. ibmvfc_trc_end(evt);
  3481. evt->done(evt);
  3482. }
  3483. }
  3484. static irqreturn_t ibmvfc_interrupt_mq(int irq, void *scrq_instance)
  3485. {
  3486. struct ibmvfc_queue *scrq = (struct ibmvfc_queue *)scrq_instance;
  3487. ibmvfc_toggle_scrq_irq(scrq, 0);
  3488. ibmvfc_drain_sub_crq(scrq);
  3489. return IRQ_HANDLED;
  3490. }
  3491. /**
  3492. * ibmvfc_init_tgt - Set the next init job step for the target
  3493. * @tgt: ibmvfc target struct
  3494. * @job_step: job step to perform
  3495. *
  3496. **/
  3497. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  3498. void (*job_step) (struct ibmvfc_target *))
  3499. {
  3500. if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
  3501. tgt->job_step = job_step;
  3502. wake_up(&tgt->vhost->work_wait_q);
  3503. }
  3504. /**
  3505. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  3506. * @tgt: ibmvfc target struct
  3507. * @job_step: initialization job step
  3508. *
  3509. * Returns: 1 if step will be retried / 0 if not
  3510. *
  3511. **/
  3512. static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  3513. void (*job_step) (struct ibmvfc_target *))
  3514. {
  3515. if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
  3516. ibmvfc_del_tgt(tgt);
  3517. wake_up(&tgt->vhost->work_wait_q);
  3518. return 0;
  3519. } else
  3520. ibmvfc_init_tgt(tgt, job_step);
  3521. return 1;
  3522. }
  3523. /* Defined in FC-LS */
  3524. static const struct {
  3525. int code;
  3526. int retry;
  3527. int logged_in;
  3528. } prli_rsp [] = {
  3529. { 0, 1, 0 },
  3530. { 1, 0, 1 },
  3531. { 2, 1, 0 },
  3532. { 3, 1, 0 },
  3533. { 4, 0, 0 },
  3534. { 5, 0, 0 },
  3535. { 6, 0, 1 },
  3536. { 7, 0, 0 },
  3537. { 8, 1, 0 },
  3538. };
  3539. /**
  3540. * ibmvfc_get_prli_rsp - Find PRLI response index
  3541. * @flags: PRLI response flags
  3542. *
  3543. **/
  3544. static int ibmvfc_get_prli_rsp(u16 flags)
  3545. {
  3546. int i;
  3547. int code = (flags & 0x0f00) >> 8;
  3548. for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
  3549. if (prli_rsp[i].code == code)
  3550. return i;
  3551. return 0;
  3552. }
  3553. /**
  3554. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  3555. * @evt: ibmvfc event struct
  3556. *
  3557. **/
  3558. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  3559. {
  3560. struct ibmvfc_target *tgt = evt->tgt;
  3561. struct ibmvfc_host *vhost = evt->vhost;
  3562. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  3563. struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
  3564. u32 status = be16_to_cpu(rsp->common.status);
  3565. int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
  3566. vhost->discovery_threads--;
  3567. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3568. switch (status) {
  3569. case IBMVFC_MAD_SUCCESS:
  3570. tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
  3571. parms->type, parms->flags, parms->service_parms);
  3572. if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
  3573. index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
  3574. if (prli_rsp[index].logged_in) {
  3575. if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
  3576. tgt->need_login = 0;
  3577. tgt->ids.roles = 0;
  3578. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
  3579. tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
  3580. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
  3581. tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
  3582. tgt->add_rport = 1;
  3583. } else
  3584. ibmvfc_del_tgt(tgt);
  3585. } else if (prli_rsp[index].retry)
  3586. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3587. else
  3588. ibmvfc_del_tgt(tgt);
  3589. } else
  3590. ibmvfc_del_tgt(tgt);
  3591. break;
  3592. case IBMVFC_MAD_DRIVER_FAILED:
  3593. break;
  3594. case IBMVFC_MAD_CRQ_ERROR:
  3595. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3596. break;
  3597. case IBMVFC_MAD_FAILED:
  3598. default:
  3599. if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
  3600. be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
  3601. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3602. else if (tgt->logo_rcvd)
  3603. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3604. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3605. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3606. else
  3607. ibmvfc_del_tgt(tgt);
  3608. tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  3609. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3610. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
  3611. break;
  3612. }
  3613. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3614. ibmvfc_free_event(evt);
  3615. wake_up(&vhost->work_wait_q);
  3616. }
  3617. /**
  3618. * ibmvfc_tgt_send_prli - Send a process login
  3619. * @tgt: ibmvfc target struct
  3620. *
  3621. **/
  3622. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  3623. {
  3624. struct ibmvfc_process_login *prli;
  3625. struct ibmvfc_host *vhost = tgt->vhost;
  3626. struct ibmvfc_event *evt;
  3627. if (vhost->discovery_threads >= disc_threads)
  3628. return;
  3629. kref_get(&tgt->kref);
  3630. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3631. if (!evt) {
  3632. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3633. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3634. __ibmvfc_reset_host(vhost);
  3635. return;
  3636. }
  3637. vhost->discovery_threads++;
  3638. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  3639. evt->tgt = tgt;
  3640. prli = &evt->iu.prli;
  3641. memset(prli, 0, sizeof(*prli));
  3642. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  3643. prli->common.version = cpu_to_be32(2);
  3644. prli->target_wwpn = cpu_to_be64(tgt->wwpn);
  3645. } else {
  3646. prli->common.version = cpu_to_be32(1);
  3647. }
  3648. prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
  3649. prli->common.length = cpu_to_be16(sizeof(*prli));
  3650. prli->scsi_id = cpu_to_be64(tgt->scsi_id);
  3651. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  3652. prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
  3653. prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
  3654. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
  3655. if (cls3_error)
  3656. prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
  3657. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3658. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3659. vhost->discovery_threads--;
  3660. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3661. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3662. } else
  3663. tgt_dbg(tgt, "Sent process login\n");
  3664. }
  3665. /**
  3666. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  3667. * @evt: ibmvfc event struct
  3668. *
  3669. **/
  3670. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  3671. {
  3672. struct ibmvfc_target *tgt = evt->tgt;
  3673. struct ibmvfc_host *vhost = evt->vhost;
  3674. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  3675. u32 status = be16_to_cpu(rsp->common.status);
  3676. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3677. vhost->discovery_threads--;
  3678. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3679. switch (status) {
  3680. case IBMVFC_MAD_SUCCESS:
  3681. tgt_dbg(tgt, "Port Login succeeded\n");
  3682. if (tgt->ids.port_name &&
  3683. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  3684. vhost->reinit = 1;
  3685. tgt_dbg(tgt, "Port re-init required\n");
  3686. break;
  3687. }
  3688. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3689. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3690. tgt->ids.port_id = tgt->scsi_id;
  3691. memcpy(&tgt->service_parms, &rsp->service_parms,
  3692. sizeof(tgt->service_parms));
  3693. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3694. sizeof(tgt->service_parms_change));
  3695. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3696. break;
  3697. case IBMVFC_MAD_DRIVER_FAILED:
  3698. break;
  3699. case IBMVFC_MAD_CRQ_ERROR:
  3700. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3701. break;
  3702. case IBMVFC_MAD_FAILED:
  3703. default:
  3704. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3705. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3706. else
  3707. ibmvfc_del_tgt(tgt);
  3708. tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3709. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3710. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
  3711. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
  3712. ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
  3713. break;
  3714. }
  3715. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3716. ibmvfc_free_event(evt);
  3717. wake_up(&vhost->work_wait_q);
  3718. }
  3719. /**
  3720. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  3721. * @tgt: ibmvfc target struct
  3722. *
  3723. **/
  3724. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  3725. {
  3726. struct ibmvfc_port_login *plogi;
  3727. struct ibmvfc_host *vhost = tgt->vhost;
  3728. struct ibmvfc_event *evt;
  3729. if (vhost->discovery_threads >= disc_threads)
  3730. return;
  3731. kref_get(&tgt->kref);
  3732. tgt->logo_rcvd = 0;
  3733. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3734. if (!evt) {
  3735. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3736. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3737. __ibmvfc_reset_host(vhost);
  3738. return;
  3739. }
  3740. vhost->discovery_threads++;
  3741. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3742. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  3743. evt->tgt = tgt;
  3744. plogi = &evt->iu.plogi;
  3745. memset(plogi, 0, sizeof(*plogi));
  3746. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  3747. plogi->common.version = cpu_to_be32(2);
  3748. plogi->target_wwpn = cpu_to_be64(tgt->wwpn);
  3749. } else {
  3750. plogi->common.version = cpu_to_be32(1);
  3751. }
  3752. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  3753. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  3754. plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
  3755. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3756. vhost->discovery_threads--;
  3757. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3758. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3759. } else
  3760. tgt_dbg(tgt, "Sent port login\n");
  3761. }
  3762. /**
  3763. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  3764. * @evt: ibmvfc event struct
  3765. *
  3766. **/
  3767. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  3768. {
  3769. struct ibmvfc_target *tgt = evt->tgt;
  3770. struct ibmvfc_host *vhost = evt->vhost;
  3771. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  3772. u32 status = be16_to_cpu(rsp->common.status);
  3773. vhost->discovery_threads--;
  3774. ibmvfc_free_event(evt);
  3775. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3776. switch (status) {
  3777. case IBMVFC_MAD_SUCCESS:
  3778. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  3779. break;
  3780. case IBMVFC_MAD_DRIVER_FAILED:
  3781. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3782. wake_up(&vhost->work_wait_q);
  3783. return;
  3784. case IBMVFC_MAD_FAILED:
  3785. default:
  3786. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  3787. break;
  3788. }
  3789. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  3790. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3791. wake_up(&vhost->work_wait_q);
  3792. }
  3793. /**
  3794. * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
  3795. * @tgt: ibmvfc target struct
  3796. * @done: Routine to call when the event is responded to
  3797. *
  3798. * Returns:
  3799. * Allocated and initialized ibmvfc_event struct
  3800. **/
  3801. static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
  3802. void (*done) (struct ibmvfc_event *))
  3803. {
  3804. struct ibmvfc_implicit_logout *mad;
  3805. struct ibmvfc_host *vhost = tgt->vhost;
  3806. struct ibmvfc_event *evt;
  3807. kref_get(&tgt->kref);
  3808. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3809. if (!evt)
  3810. return NULL;
  3811. ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
  3812. evt->tgt = tgt;
  3813. mad = &evt->iu.implicit_logout;
  3814. memset(mad, 0, sizeof(*mad));
  3815. mad->common.version = cpu_to_be32(1);
  3816. mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
  3817. mad->common.length = cpu_to_be16(sizeof(*mad));
  3818. mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3819. return evt;
  3820. }
  3821. /**
  3822. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  3823. * @tgt: ibmvfc target struct
  3824. *
  3825. **/
  3826. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  3827. {
  3828. struct ibmvfc_host *vhost = tgt->vhost;
  3829. struct ibmvfc_event *evt;
  3830. if (vhost->discovery_threads >= disc_threads)
  3831. return;
  3832. vhost->discovery_threads++;
  3833. evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
  3834. ibmvfc_tgt_implicit_logout_done);
  3835. if (!evt) {
  3836. vhost->discovery_threads--;
  3837. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3838. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3839. __ibmvfc_reset_host(vhost);
  3840. return;
  3841. }
  3842. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3843. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3844. vhost->discovery_threads--;
  3845. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3846. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3847. } else
  3848. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3849. }
  3850. /**
  3851. * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
  3852. * @evt: ibmvfc event struct
  3853. *
  3854. **/
  3855. static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
  3856. {
  3857. struct ibmvfc_target *tgt = evt->tgt;
  3858. struct ibmvfc_host *vhost = evt->vhost;
  3859. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  3860. u32 status = be16_to_cpu(mad->common.status);
  3861. vhost->discovery_threads--;
  3862. ibmvfc_free_event(evt);
  3863. /*
  3864. * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
  3865. * driver in which case we need to free up all the targets. If we are
  3866. * not unloading, we will still go through a hard reset to get out of
  3867. * offline state, so there is no need to track the old targets in that
  3868. * case.
  3869. */
  3870. if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
  3871. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3872. else
  3873. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
  3874. tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
  3875. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3876. wake_up(&vhost->work_wait_q);
  3877. }
  3878. /**
  3879. * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
  3880. * @tgt: ibmvfc target struct
  3881. *
  3882. **/
  3883. static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
  3884. {
  3885. struct ibmvfc_host *vhost = tgt->vhost;
  3886. struct ibmvfc_event *evt;
  3887. if (!vhost->logged_in) {
  3888. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3889. return;
  3890. }
  3891. if (vhost->discovery_threads >= disc_threads)
  3892. return;
  3893. vhost->discovery_threads++;
  3894. evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
  3895. ibmvfc_tgt_implicit_logout_and_del_done);
  3896. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
  3897. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3898. vhost->discovery_threads--;
  3899. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3900. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3901. } else
  3902. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3903. }
  3904. /**
  3905. * ibmvfc_tgt_move_login_done - Completion handler for Move Login
  3906. * @evt: ibmvfc event struct
  3907. *
  3908. **/
  3909. static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
  3910. {
  3911. struct ibmvfc_target *tgt = evt->tgt;
  3912. struct ibmvfc_host *vhost = evt->vhost;
  3913. struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
  3914. u32 status = be16_to_cpu(rsp->common.status);
  3915. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3916. vhost->discovery_threads--;
  3917. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3918. switch (status) {
  3919. case IBMVFC_MAD_SUCCESS:
  3920. tgt_dbg(tgt, "Move Login succeeded for new scsi_id: %llX\n", tgt->new_scsi_id);
  3921. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3922. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3923. tgt->scsi_id = tgt->new_scsi_id;
  3924. tgt->ids.port_id = tgt->scsi_id;
  3925. memcpy(&tgt->service_parms, &rsp->service_parms,
  3926. sizeof(tgt->service_parms));
  3927. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3928. sizeof(tgt->service_parms_change));
  3929. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3930. break;
  3931. case IBMVFC_MAD_DRIVER_FAILED:
  3932. break;
  3933. case IBMVFC_MAD_CRQ_ERROR:
  3934. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
  3935. break;
  3936. case IBMVFC_MAD_FAILED:
  3937. default:
  3938. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
  3939. tgt_log(tgt, level,
  3940. "Move Login failed: new scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
  3941. tgt->new_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
  3942. status);
  3943. break;
  3944. }
  3945. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3946. ibmvfc_free_event(evt);
  3947. wake_up(&vhost->work_wait_q);
  3948. }
  3949. /**
  3950. * ibmvfc_tgt_move_login - Initiate a move login for specified target
  3951. * @tgt: ibmvfc target struct
  3952. *
  3953. **/
  3954. static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
  3955. {
  3956. struct ibmvfc_host *vhost = tgt->vhost;
  3957. struct ibmvfc_move_login *move;
  3958. struct ibmvfc_event *evt;
  3959. if (vhost->discovery_threads >= disc_threads)
  3960. return;
  3961. kref_get(&tgt->kref);
  3962. evt = ibmvfc_get_reserved_event(&vhost->crq);
  3963. if (!evt) {
  3964. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3965. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3966. __ibmvfc_reset_host(vhost);
  3967. return;
  3968. }
  3969. vhost->discovery_threads++;
  3970. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3971. ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
  3972. evt->tgt = tgt;
  3973. move = &evt->iu.move_login;
  3974. memset(move, 0, sizeof(*move));
  3975. move->common.version = cpu_to_be32(1);
  3976. move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
  3977. move->common.length = cpu_to_be16(sizeof(*move));
  3978. move->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3979. move->new_scsi_id = cpu_to_be64(tgt->new_scsi_id);
  3980. move->wwpn = cpu_to_be64(tgt->wwpn);
  3981. move->node_name = cpu_to_be64(tgt->ids.node_name);
  3982. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3983. vhost->discovery_threads--;
  3984. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3985. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3986. } else
  3987. tgt_dbg(tgt, "Sent Move Login for new scsi_id: %llX\n", tgt->new_scsi_id);
  3988. }
  3989. /**
  3990. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  3991. * @mad: ibmvfc passthru mad struct
  3992. * @tgt: ibmvfc target struct
  3993. *
  3994. * Returns:
  3995. * 1 if PLOGI needed / 0 if PLOGI not needed
  3996. **/
  3997. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  3998. struct ibmvfc_target *tgt)
  3999. {
  4000. if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
  4001. return 1;
  4002. if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
  4003. return 1;
  4004. if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
  4005. return 1;
  4006. return 0;
  4007. }
  4008. /**
  4009. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  4010. * @evt: ibmvfc event struct
  4011. *
  4012. **/
  4013. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  4014. {
  4015. struct ibmvfc_target *tgt = evt->tgt;
  4016. struct ibmvfc_host *vhost = evt->vhost;
  4017. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  4018. u32 status = be16_to_cpu(mad->common.status);
  4019. u8 fc_reason, fc_explain;
  4020. vhost->discovery_threads--;
  4021. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4022. timer_delete(&tgt->timer);
  4023. switch (status) {
  4024. case IBMVFC_MAD_SUCCESS:
  4025. tgt_dbg(tgt, "ADISC succeeded\n");
  4026. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  4027. ibmvfc_del_tgt(tgt);
  4028. break;
  4029. case IBMVFC_MAD_DRIVER_FAILED:
  4030. break;
  4031. case IBMVFC_MAD_FAILED:
  4032. default:
  4033. ibmvfc_del_tgt(tgt);
  4034. fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
  4035. fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
  4036. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  4037. ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
  4038. be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
  4039. ibmvfc_get_fc_type(fc_reason), fc_reason,
  4040. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  4041. break;
  4042. }
  4043. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4044. ibmvfc_free_event(evt);
  4045. wake_up(&vhost->work_wait_q);
  4046. }
  4047. /**
  4048. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  4049. * @evt: ibmvfc event struct
  4050. *
  4051. **/
  4052. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  4053. {
  4054. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  4055. memset(mad, 0, sizeof(*mad));
  4056. mad->common.version = cpu_to_be32(1);
  4057. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  4058. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  4059. mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4060. offsetof(struct ibmvfc_passthru_mad, iu));
  4061. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  4062. mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  4063. mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
  4064. mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4065. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  4066. offsetof(struct ibmvfc_passthru_fc_iu, payload));
  4067. mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  4068. mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  4069. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  4070. offsetof(struct ibmvfc_passthru_fc_iu, response));
  4071. mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
  4072. }
  4073. /**
  4074. * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
  4075. * @evt: ibmvfc event struct
  4076. *
  4077. * Just cleanup this event struct. Everything else is handled by
  4078. * the ADISC completion handler. If the ADISC never actually comes
  4079. * back, we still have the timer running on the ADISC event struct
  4080. * which will fire and cause the CRQ to get reset.
  4081. *
  4082. **/
  4083. static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
  4084. {
  4085. struct ibmvfc_host *vhost = evt->vhost;
  4086. struct ibmvfc_target *tgt = evt->tgt;
  4087. tgt_dbg(tgt, "ADISC cancel complete\n");
  4088. vhost->abort_threads--;
  4089. ibmvfc_free_event(evt);
  4090. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4091. wake_up(&vhost->work_wait_q);
  4092. }
  4093. /**
  4094. * ibmvfc_adisc_timeout - Handle an ADISC timeout
  4095. * @t: ibmvfc target struct
  4096. *
  4097. * If an ADISC times out, send a cancel. If the cancel times
  4098. * out, reset the CRQ. When the ADISC comes back as cancelled,
  4099. * log back into the target.
  4100. **/
  4101. static void ibmvfc_adisc_timeout(struct timer_list *t)
  4102. {
  4103. struct ibmvfc_target *tgt = timer_container_of(tgt, t, timer);
  4104. struct ibmvfc_host *vhost = tgt->vhost;
  4105. struct ibmvfc_event *evt;
  4106. struct ibmvfc_tmf *tmf;
  4107. unsigned long flags;
  4108. int rc;
  4109. tgt_dbg(tgt, "ADISC timeout\n");
  4110. spin_lock_irqsave(vhost->host->host_lock, flags);
  4111. if (vhost->abort_threads >= disc_threads ||
  4112. tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
  4113. vhost->state != IBMVFC_INITIALIZING ||
  4114. vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
  4115. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4116. return;
  4117. }
  4118. vhost->abort_threads++;
  4119. kref_get(&tgt->kref);
  4120. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4121. if (!evt) {
  4122. tgt_err(tgt, "Failed to get cancel event for ADISC.\n");
  4123. vhost->abort_threads--;
  4124. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4125. __ibmvfc_reset_host(vhost);
  4126. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4127. return;
  4128. }
  4129. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
  4130. evt->tgt = tgt;
  4131. tmf = &evt->iu.tmf;
  4132. memset(tmf, 0, sizeof(*tmf));
  4133. if (ibmvfc_check_caps(vhost, IBMVFC_HANDLE_VF_WWPN)) {
  4134. tmf->common.version = cpu_to_be32(2);
  4135. tmf->target_wwpn = cpu_to_be64(tgt->wwpn);
  4136. } else {
  4137. tmf->common.version = cpu_to_be32(1);
  4138. }
  4139. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  4140. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  4141. tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
  4142. tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
  4143. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  4144. if (rc) {
  4145. tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
  4146. vhost->abort_threads--;
  4147. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4148. __ibmvfc_reset_host(vhost);
  4149. } else
  4150. tgt_dbg(tgt, "Attempting to cancel ADISC\n");
  4151. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4152. }
  4153. /**
  4154. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  4155. * @tgt: ibmvfc target struct
  4156. *
  4157. * When sending an ADISC we end up with two timers running. The
  4158. * first timer is the timer in the ibmvfc target struct. If this
  4159. * fires, we send a cancel to the target. The second timer is the
  4160. * timer on the ibmvfc event for the ADISC, which is longer. If that
  4161. * fires, it means the ADISC timed out and our attempt to cancel it
  4162. * also failed, so we need to reset the CRQ.
  4163. **/
  4164. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  4165. {
  4166. struct ibmvfc_passthru_mad *mad;
  4167. struct ibmvfc_host *vhost = tgt->vhost;
  4168. struct ibmvfc_event *evt;
  4169. if (vhost->discovery_threads >= disc_threads)
  4170. return;
  4171. kref_get(&tgt->kref);
  4172. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4173. if (!evt) {
  4174. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4175. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4176. __ibmvfc_reset_host(vhost);
  4177. return;
  4178. }
  4179. vhost->discovery_threads++;
  4180. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  4181. evt->tgt = tgt;
  4182. ibmvfc_init_passthru(evt);
  4183. mad = &evt->iu.passthru;
  4184. mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
  4185. mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
  4186. mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
  4187. mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
  4188. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  4189. sizeof(vhost->login_buf->resp.port_name));
  4190. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  4191. sizeof(vhost->login_buf->resp.node_name));
  4192. mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
  4193. if (timer_pending(&tgt->timer))
  4194. mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
  4195. else {
  4196. tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
  4197. add_timer(&tgt->timer);
  4198. }
  4199. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  4200. if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
  4201. vhost->discovery_threads--;
  4202. timer_delete(&tgt->timer);
  4203. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4204. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4205. } else
  4206. tgt_dbg(tgt, "Sent ADISC\n");
  4207. }
  4208. /**
  4209. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  4210. * @evt: ibmvfc event struct
  4211. *
  4212. **/
  4213. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  4214. {
  4215. struct ibmvfc_target *tgt = evt->tgt;
  4216. struct ibmvfc_host *vhost = evt->vhost;
  4217. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  4218. u32 status = be16_to_cpu(rsp->common.status);
  4219. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4220. vhost->discovery_threads--;
  4221. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4222. switch (status) {
  4223. case IBMVFC_MAD_SUCCESS:
  4224. tgt_dbg(tgt, "Query Target succeeded\n");
  4225. if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
  4226. ibmvfc_del_tgt(tgt);
  4227. else
  4228. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  4229. break;
  4230. case IBMVFC_MAD_DRIVER_FAILED:
  4231. break;
  4232. case IBMVFC_MAD_CRQ_ERROR:
  4233. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  4234. break;
  4235. case IBMVFC_MAD_FAILED:
  4236. default:
  4237. if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  4238. be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  4239. be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
  4240. ibmvfc_del_tgt(tgt);
  4241. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  4242. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  4243. else
  4244. ibmvfc_del_tgt(tgt);
  4245. tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  4246. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4247. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
  4248. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
  4249. ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
  4250. status);
  4251. break;
  4252. }
  4253. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4254. ibmvfc_free_event(evt);
  4255. wake_up(&vhost->work_wait_q);
  4256. }
  4257. /**
  4258. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  4259. * @tgt: ibmvfc target struct
  4260. *
  4261. **/
  4262. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  4263. {
  4264. struct ibmvfc_query_tgt *query_tgt;
  4265. struct ibmvfc_host *vhost = tgt->vhost;
  4266. struct ibmvfc_event *evt;
  4267. if (vhost->discovery_threads >= disc_threads)
  4268. return;
  4269. kref_get(&tgt->kref);
  4270. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4271. if (!evt) {
  4272. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4273. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4274. __ibmvfc_reset_host(vhost);
  4275. return;
  4276. }
  4277. vhost->discovery_threads++;
  4278. evt->tgt = tgt;
  4279. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  4280. query_tgt = &evt->iu.query_tgt;
  4281. memset(query_tgt, 0, sizeof(*query_tgt));
  4282. query_tgt->common.version = cpu_to_be32(1);
  4283. query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
  4284. query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
  4285. query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
  4286. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  4287. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  4288. vhost->discovery_threads--;
  4289. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  4290. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4291. } else
  4292. tgt_dbg(tgt, "Sent Query Target\n");
  4293. }
  4294. /**
  4295. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  4296. * @vhost: ibmvfc host struct
  4297. * @target: Holds SCSI ID to allocate target forand the WWPN
  4298. *
  4299. * Returns:
  4300. * 0 on success / other on failure
  4301. **/
  4302. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
  4303. struct ibmvfc_discover_targets_entry *target)
  4304. {
  4305. struct ibmvfc_target *stgt = NULL;
  4306. struct ibmvfc_target *wtgt = NULL;
  4307. struct ibmvfc_target *tgt;
  4308. unsigned long flags;
  4309. u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
  4310. u64 wwpn = be64_to_cpu(target->wwpn);
  4311. /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
  4312. spin_lock_irqsave(vhost->host->host_lock, flags);
  4313. list_for_each_entry(tgt, &vhost->targets, queue) {
  4314. if (tgt->wwpn == wwpn) {
  4315. wtgt = tgt;
  4316. break;
  4317. }
  4318. }
  4319. list_for_each_entry(tgt, &vhost->targets, queue) {
  4320. if (tgt->scsi_id == scsi_id) {
  4321. stgt = tgt;
  4322. break;
  4323. }
  4324. }
  4325. if (wtgt && !stgt) {
  4326. /*
  4327. * A WWPN target has moved and we still are tracking the old
  4328. * SCSI ID. The only way we should be able to get here is if
  4329. * we attempted to send an implicit logout for the old SCSI ID
  4330. * and it failed for some reason, such as there being I/O
  4331. * pending to the target. In this case, we will have already
  4332. * deleted the rport from the FC transport so we do a move
  4333. * login, which works even with I/O pending, however, if
  4334. * there is still I/O pending, it will stay outstanding, so
  4335. * we only do this if fast fail is disabled for the rport,
  4336. * otherwise we let terminate_rport_io clean up the port
  4337. * before we login at the new location.
  4338. */
  4339. if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
  4340. if (wtgt->move_login) {
  4341. /*
  4342. * Do a move login here. The old target is no longer
  4343. * known to the transport layer We don't use the
  4344. * normal ibmvfc_set_tgt_action to set this, as we
  4345. * don't normally want to allow this state change.
  4346. */
  4347. wtgt->new_scsi_id = scsi_id;
  4348. wtgt->action = IBMVFC_TGT_ACTION_INIT;
  4349. wtgt->init_retries = 0;
  4350. ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
  4351. }
  4352. goto unlock_out;
  4353. } else {
  4354. tgt_err(wtgt, "Unexpected target state: %d, %p\n",
  4355. wtgt->action, wtgt->rport);
  4356. }
  4357. } else if (stgt) {
  4358. if (tgt->need_login)
  4359. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  4360. goto unlock_out;
  4361. }
  4362. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4363. tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
  4364. memset(tgt, 0, sizeof(*tgt));
  4365. tgt->scsi_id = scsi_id;
  4366. tgt->wwpn = wwpn;
  4367. tgt->vhost = vhost;
  4368. tgt->need_login = 1;
  4369. timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
  4370. kref_init(&tgt->kref);
  4371. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  4372. spin_lock_irqsave(vhost->host->host_lock, flags);
  4373. tgt->cancel_key = vhost->task_set++;
  4374. list_add_tail(&tgt->queue, &vhost->targets);
  4375. unlock_out:
  4376. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4377. return 0;
  4378. }
  4379. /**
  4380. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  4381. * @vhost: ibmvfc host struct
  4382. *
  4383. * Returns:
  4384. * 0 on success / other on failure
  4385. **/
  4386. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  4387. {
  4388. int i, rc;
  4389. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  4390. rc = ibmvfc_alloc_target(vhost, &vhost->scsi_scrqs.disc_buf[i]);
  4391. return rc;
  4392. }
  4393. /**
  4394. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  4395. * @evt: ibmvfc event struct
  4396. *
  4397. **/
  4398. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  4399. {
  4400. struct ibmvfc_host *vhost = evt->vhost;
  4401. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  4402. u32 mad_status = be16_to_cpu(rsp->common.status);
  4403. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4404. switch (mad_status) {
  4405. case IBMVFC_MAD_SUCCESS:
  4406. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  4407. vhost->num_targets = min_t(u32, be32_to_cpu(rsp->num_written),
  4408. max_targets);
  4409. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  4410. break;
  4411. case IBMVFC_MAD_FAILED:
  4412. level += ibmvfc_retry_host_init(vhost);
  4413. ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
  4414. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4415. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
  4416. break;
  4417. case IBMVFC_MAD_DRIVER_FAILED:
  4418. break;
  4419. default:
  4420. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  4421. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4422. break;
  4423. }
  4424. ibmvfc_free_event(evt);
  4425. wake_up(&vhost->work_wait_q);
  4426. }
  4427. /**
  4428. * ibmvfc_discover_targets - Send Discover Targets MAD
  4429. * @vhost: ibmvfc host struct
  4430. *
  4431. **/
  4432. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  4433. {
  4434. struct ibmvfc_discover_targets *mad;
  4435. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4436. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4437. if (!evt) {
  4438. ibmvfc_log(vhost, level, "Discover Targets failed: no available events\n");
  4439. ibmvfc_hard_reset_host(vhost);
  4440. return;
  4441. }
  4442. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  4443. mad = &evt->iu.discover_targets;
  4444. memset(mad, 0, sizeof(*mad));
  4445. mad->common.version = cpu_to_be32(1);
  4446. mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
  4447. mad->common.length = cpu_to_be16(sizeof(*mad));
  4448. mad->bufflen = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
  4449. mad->buffer.va = cpu_to_be64(vhost->scsi_scrqs.disc_buf_dma);
  4450. mad->buffer.len = cpu_to_be32(vhost->scsi_scrqs.disc_buf_sz);
  4451. mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
  4452. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4453. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4454. ibmvfc_dbg(vhost, "Sent discover targets\n");
  4455. else
  4456. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4457. }
  4458. static void ibmvfc_channel_setup_done(struct ibmvfc_event *evt)
  4459. {
  4460. struct ibmvfc_host *vhost = evt->vhost;
  4461. struct ibmvfc_channel_setup *setup = vhost->channel_setup_buf;
  4462. struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
  4463. u32 mad_status = be16_to_cpu(evt->xfer_iu->channel_setup.common.status);
  4464. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4465. int flags, active_queues, i;
  4466. ibmvfc_free_event(evt);
  4467. switch (mad_status) {
  4468. case IBMVFC_MAD_SUCCESS:
  4469. ibmvfc_dbg(vhost, "Channel Setup succeeded\n");
  4470. flags = be32_to_cpu(setup->flags);
  4471. vhost->do_enquiry = 0;
  4472. active_queues = be32_to_cpu(setup->num_scsi_subq_channels);
  4473. scrqs->active_queues = active_queues;
  4474. if (flags & IBMVFC_CHANNELS_CANCELED) {
  4475. ibmvfc_dbg(vhost, "Channels Canceled\n");
  4476. vhost->using_channels = 0;
  4477. } else {
  4478. if (active_queues)
  4479. vhost->using_channels = 1;
  4480. for (i = 0; i < active_queues; i++)
  4481. scrqs->scrqs[i].vios_cookie =
  4482. be64_to_cpu(setup->channel_handles[i]);
  4483. ibmvfc_dbg(vhost, "Using %u channels\n",
  4484. vhost->scsi_scrqs.active_queues);
  4485. }
  4486. break;
  4487. case IBMVFC_MAD_FAILED:
  4488. level += ibmvfc_retry_host_init(vhost);
  4489. ibmvfc_log(vhost, level, "Channel Setup failed\n");
  4490. fallthrough;
  4491. case IBMVFC_MAD_DRIVER_FAILED:
  4492. return;
  4493. default:
  4494. dev_err(vhost->dev, "Invalid Channel Setup response: 0x%x\n",
  4495. mad_status);
  4496. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4497. return;
  4498. }
  4499. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  4500. wake_up(&vhost->work_wait_q);
  4501. }
  4502. static void ibmvfc_channel_setup(struct ibmvfc_host *vhost)
  4503. {
  4504. struct ibmvfc_channel_setup_mad *mad;
  4505. struct ibmvfc_channel_setup *setup_buf = vhost->channel_setup_buf;
  4506. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4507. struct ibmvfc_channels *scrqs = &vhost->scsi_scrqs;
  4508. unsigned int num_channels =
  4509. min(scrqs->desired_queues, vhost->max_vios_scsi_channels);
  4510. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4511. int i;
  4512. if (!evt) {
  4513. ibmvfc_log(vhost, level, "Channel Setup failed: no available events\n");
  4514. ibmvfc_hard_reset_host(vhost);
  4515. return;
  4516. }
  4517. memset(setup_buf, 0, sizeof(*setup_buf));
  4518. if (num_channels == 0)
  4519. setup_buf->flags = cpu_to_be32(IBMVFC_CANCEL_CHANNELS);
  4520. else {
  4521. setup_buf->num_scsi_subq_channels = cpu_to_be32(num_channels);
  4522. for (i = 0; i < num_channels; i++)
  4523. setup_buf->channel_handles[i] = cpu_to_be64(scrqs->scrqs[i].cookie);
  4524. }
  4525. ibmvfc_init_event(evt, ibmvfc_channel_setup_done, IBMVFC_MAD_FORMAT);
  4526. mad = &evt->iu.channel_setup;
  4527. memset(mad, 0, sizeof(*mad));
  4528. mad->common.version = cpu_to_be32(1);
  4529. mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_SETUP);
  4530. mad->common.length = cpu_to_be16(sizeof(*mad));
  4531. mad->buffer.va = cpu_to_be64(vhost->channel_setup_dma);
  4532. mad->buffer.len = cpu_to_be32(sizeof(*vhost->channel_setup_buf));
  4533. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4534. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4535. ibmvfc_dbg(vhost, "Sent channel setup\n");
  4536. else
  4537. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  4538. }
  4539. static void ibmvfc_channel_enquiry_done(struct ibmvfc_event *evt)
  4540. {
  4541. struct ibmvfc_host *vhost = evt->vhost;
  4542. struct ibmvfc_channel_enquiry *rsp = &evt->xfer_iu->channel_enquiry;
  4543. u32 mad_status = be16_to_cpu(rsp->common.status);
  4544. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4545. switch (mad_status) {
  4546. case IBMVFC_MAD_SUCCESS:
  4547. ibmvfc_dbg(vhost, "Channel Enquiry succeeded\n");
  4548. vhost->max_vios_scsi_channels = be32_to_cpu(rsp->num_scsi_subq_channels);
  4549. ibmvfc_free_event(evt);
  4550. break;
  4551. case IBMVFC_MAD_FAILED:
  4552. level += ibmvfc_retry_host_init(vhost);
  4553. ibmvfc_log(vhost, level, "Channel Enquiry failed\n");
  4554. fallthrough;
  4555. case IBMVFC_MAD_DRIVER_FAILED:
  4556. ibmvfc_free_event(evt);
  4557. return;
  4558. default:
  4559. dev_err(vhost->dev, "Invalid Channel Enquiry response: 0x%x\n",
  4560. mad_status);
  4561. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4562. ibmvfc_free_event(evt);
  4563. return;
  4564. }
  4565. ibmvfc_channel_setup(vhost);
  4566. }
  4567. static void ibmvfc_channel_enquiry(struct ibmvfc_host *vhost)
  4568. {
  4569. struct ibmvfc_channel_enquiry *mad;
  4570. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4571. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4572. if (!evt) {
  4573. ibmvfc_log(vhost, level, "Channel Enquiry failed: no available events\n");
  4574. ibmvfc_hard_reset_host(vhost);
  4575. return;
  4576. }
  4577. ibmvfc_init_event(evt, ibmvfc_channel_enquiry_done, IBMVFC_MAD_FORMAT);
  4578. mad = &evt->iu.channel_enquiry;
  4579. memset(mad, 0, sizeof(*mad));
  4580. mad->common.version = cpu_to_be32(1);
  4581. mad->common.opcode = cpu_to_be32(IBMVFC_CHANNEL_ENQUIRY);
  4582. mad->common.length = cpu_to_be16(sizeof(*mad));
  4583. if (mig_channels_only)
  4584. mad->flags |= cpu_to_be32(IBMVFC_NO_CHANNELS_TO_CRQ_SUPPORT);
  4585. if (mig_no_less_channels)
  4586. mad->flags |= cpu_to_be32(IBMVFC_NO_N_TO_M_CHANNELS_SUPPORT);
  4587. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4588. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4589. ibmvfc_dbg(vhost, "Send channel enquiry\n");
  4590. else
  4591. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4592. }
  4593. /**
  4594. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  4595. * @evt: ibmvfc event struct
  4596. *
  4597. **/
  4598. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  4599. {
  4600. struct ibmvfc_host *vhost = evt->vhost;
  4601. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
  4602. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  4603. unsigned int npiv_max_sectors;
  4604. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  4605. switch (mad_status) {
  4606. case IBMVFC_MAD_SUCCESS:
  4607. ibmvfc_free_event(evt);
  4608. break;
  4609. case IBMVFC_MAD_FAILED:
  4610. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  4611. level += ibmvfc_retry_host_init(vhost);
  4612. else
  4613. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4614. ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
  4615. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  4616. be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
  4617. ibmvfc_free_event(evt);
  4618. return;
  4619. case IBMVFC_MAD_CRQ_ERROR:
  4620. ibmvfc_retry_host_init(vhost);
  4621. fallthrough;
  4622. case IBMVFC_MAD_DRIVER_FAILED:
  4623. ibmvfc_free_event(evt);
  4624. return;
  4625. default:
  4626. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  4627. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4628. ibmvfc_free_event(evt);
  4629. return;
  4630. }
  4631. vhost->client_migrated = 0;
  4632. if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
  4633. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  4634. rsp->flags);
  4635. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4636. wake_up(&vhost->work_wait_q);
  4637. return;
  4638. }
  4639. if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
  4640. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  4641. rsp->max_cmds);
  4642. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4643. wake_up(&vhost->work_wait_q);
  4644. return;
  4645. }
  4646. vhost->logged_in = 1;
  4647. npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), max_sectors);
  4648. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  4649. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  4650. rsp->drc_name, npiv_max_sectors);
  4651. fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
  4652. fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
  4653. fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
  4654. fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
  4655. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  4656. fc_host_supported_classes(vhost->host) = 0;
  4657. if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
  4658. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  4659. if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
  4660. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  4661. if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
  4662. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  4663. fc_host_maxframe_size(vhost->host) =
  4664. be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
  4665. vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
  4666. vhost->host->max_sectors = npiv_max_sectors;
  4667. if (ibmvfc_check_caps(vhost, IBMVFC_CAN_SUPPORT_CHANNELS) && vhost->do_enquiry) {
  4668. ibmvfc_channel_enquiry(vhost);
  4669. } else {
  4670. vhost->do_enquiry = 0;
  4671. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  4672. wake_up(&vhost->work_wait_q);
  4673. }
  4674. }
  4675. /**
  4676. * ibmvfc_npiv_login - Sends NPIV login
  4677. * @vhost: ibmvfc host struct
  4678. *
  4679. **/
  4680. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  4681. {
  4682. struct ibmvfc_npiv_login_mad *mad;
  4683. struct ibmvfc_event *evt = ibmvfc_get_reserved_event(&vhost->crq);
  4684. if (!evt) {
  4685. ibmvfc_dbg(vhost, "NPIV Login failed: no available events\n");
  4686. ibmvfc_hard_reset_host(vhost);
  4687. return;
  4688. }
  4689. ibmvfc_gather_partition_info(vhost);
  4690. ibmvfc_set_login_info(vhost);
  4691. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  4692. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  4693. mad = &evt->iu.npiv_login;
  4694. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  4695. mad->common.version = cpu_to_be32(1);
  4696. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
  4697. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
  4698. mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
  4699. mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
  4700. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  4701. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4702. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  4703. else
  4704. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4705. }
  4706. /**
  4707. * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
  4708. * @evt: ibmvfc event struct
  4709. *
  4710. **/
  4711. static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
  4712. {
  4713. struct ibmvfc_host *vhost = evt->vhost;
  4714. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
  4715. ibmvfc_free_event(evt);
  4716. switch (mad_status) {
  4717. case IBMVFC_MAD_SUCCESS:
  4718. if (list_empty(&vhost->crq.sent) &&
  4719. vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
  4720. ibmvfc_init_host(vhost);
  4721. return;
  4722. }
  4723. break;
  4724. case IBMVFC_MAD_FAILED:
  4725. case IBMVFC_MAD_NOT_SUPPORTED:
  4726. case IBMVFC_MAD_CRQ_ERROR:
  4727. case IBMVFC_MAD_DRIVER_FAILED:
  4728. default:
  4729. ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
  4730. break;
  4731. }
  4732. ibmvfc_hard_reset_host(vhost);
  4733. }
  4734. /**
  4735. * ibmvfc_npiv_logout - Issue an NPIV Logout
  4736. * @vhost: ibmvfc host struct
  4737. *
  4738. **/
  4739. static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
  4740. {
  4741. struct ibmvfc_npiv_logout_mad *mad;
  4742. struct ibmvfc_event *evt;
  4743. evt = ibmvfc_get_reserved_event(&vhost->crq);
  4744. if (!evt) {
  4745. ibmvfc_dbg(vhost, "NPIV Logout failed: no available events\n");
  4746. ibmvfc_hard_reset_host(vhost);
  4747. return;
  4748. }
  4749. ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
  4750. mad = &evt->iu.npiv_logout;
  4751. memset(mad, 0, sizeof(*mad));
  4752. mad->common.version = cpu_to_be32(1);
  4753. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
  4754. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
  4755. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
  4756. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  4757. ibmvfc_dbg(vhost, "Sent NPIV logout\n");
  4758. else
  4759. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4760. }
  4761. /**
  4762. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  4763. * @vhost: ibmvfc host struct
  4764. *
  4765. * Returns:
  4766. * 1 if work to do / 0 if not
  4767. **/
  4768. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  4769. {
  4770. struct ibmvfc_target *tgt;
  4771. list_for_each_entry(tgt, &vhost->targets, queue) {
  4772. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  4773. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  4774. return 1;
  4775. }
  4776. return 0;
  4777. }
  4778. /**
  4779. * ibmvfc_dev_logo_to_do - Is there target logout work to do?
  4780. * @vhost: ibmvfc host struct
  4781. *
  4782. * Returns:
  4783. * 1 if work to do / 0 if not
  4784. **/
  4785. static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
  4786. {
  4787. struct ibmvfc_target *tgt;
  4788. list_for_each_entry(tgt, &vhost->targets, queue) {
  4789. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
  4790. tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
  4791. return 1;
  4792. }
  4793. return 0;
  4794. }
  4795. /**
  4796. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  4797. * @vhost: ibmvfc host struct
  4798. *
  4799. * Returns:
  4800. * 1 if work to do / 0 if not
  4801. **/
  4802. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  4803. {
  4804. struct ibmvfc_target *tgt;
  4805. if (kthread_should_stop())
  4806. return 1;
  4807. switch (vhost->action) {
  4808. case IBMVFC_HOST_ACTION_NONE:
  4809. case IBMVFC_HOST_ACTION_INIT_WAIT:
  4810. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  4811. return 0;
  4812. case IBMVFC_HOST_ACTION_TGT_INIT:
  4813. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  4814. if (vhost->discovery_threads == disc_threads)
  4815. return 0;
  4816. list_for_each_entry(tgt, &vhost->targets, queue)
  4817. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  4818. return 1;
  4819. list_for_each_entry(tgt, &vhost->targets, queue)
  4820. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  4821. return 0;
  4822. return 1;
  4823. case IBMVFC_HOST_ACTION_TGT_DEL:
  4824. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  4825. if (vhost->discovery_threads == disc_threads)
  4826. return 0;
  4827. list_for_each_entry(tgt, &vhost->targets, queue)
  4828. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
  4829. return 1;
  4830. list_for_each_entry(tgt, &vhost->targets, queue)
  4831. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
  4832. return 0;
  4833. return 1;
  4834. case IBMVFC_HOST_ACTION_LOGO:
  4835. case IBMVFC_HOST_ACTION_INIT:
  4836. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  4837. case IBMVFC_HOST_ACTION_QUERY:
  4838. case IBMVFC_HOST_ACTION_RESET:
  4839. case IBMVFC_HOST_ACTION_REENABLE:
  4840. default:
  4841. break;
  4842. }
  4843. return 1;
  4844. }
  4845. /**
  4846. * ibmvfc_work_to_do - Is there task level work to do?
  4847. * @vhost: ibmvfc host struct
  4848. *
  4849. * Returns:
  4850. * 1 if work to do / 0 if not
  4851. **/
  4852. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  4853. {
  4854. unsigned long flags;
  4855. int rc;
  4856. spin_lock_irqsave(vhost->host->host_lock, flags);
  4857. rc = __ibmvfc_work_to_do(vhost);
  4858. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4859. return rc;
  4860. }
  4861. /**
  4862. * ibmvfc_log_ae - Log async events if necessary
  4863. * @vhost: ibmvfc host struct
  4864. * @events: events to log
  4865. *
  4866. **/
  4867. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  4868. {
  4869. if (events & IBMVFC_AE_RSCN)
  4870. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  4871. if ((events & IBMVFC_AE_LINKDOWN) &&
  4872. vhost->state >= IBMVFC_HALTED)
  4873. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  4874. if ((events & IBMVFC_AE_LINKUP) &&
  4875. vhost->state == IBMVFC_INITIALIZING)
  4876. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  4877. }
  4878. /**
  4879. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  4880. * @tgt: ibmvfc target struct
  4881. *
  4882. **/
  4883. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  4884. {
  4885. struct ibmvfc_host *vhost = tgt->vhost;
  4886. struct fc_rport *rport;
  4887. unsigned long flags;
  4888. tgt_dbg(tgt, "Adding rport\n");
  4889. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  4890. spin_lock_irqsave(vhost->host->host_lock, flags);
  4891. if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  4892. tgt_dbg(tgt, "Deleting rport\n");
  4893. list_del(&tgt->queue);
  4894. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  4895. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4896. fc_remote_port_delete(rport);
  4897. timer_delete_sync(&tgt->timer);
  4898. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4899. return;
  4900. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  4901. tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
  4902. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
  4903. tgt->rport = NULL;
  4904. tgt->init_retries = 0;
  4905. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4906. fc_remote_port_delete(rport);
  4907. return;
  4908. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  4909. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4910. return;
  4911. }
  4912. if (rport) {
  4913. tgt_dbg(tgt, "rport add succeeded\n");
  4914. tgt->rport = rport;
  4915. rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
  4916. rport->supported_classes = 0;
  4917. tgt->target_id = rport->scsi_target_id;
  4918. if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
  4919. rport->supported_classes |= FC_COS_CLASS1;
  4920. if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
  4921. rport->supported_classes |= FC_COS_CLASS2;
  4922. if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
  4923. rport->supported_classes |= FC_COS_CLASS3;
  4924. } else
  4925. tgt_dbg(tgt, "rport add failed\n");
  4926. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4927. }
  4928. /**
  4929. * ibmvfc_do_work - Do task level work
  4930. * @vhost: ibmvfc host struct
  4931. *
  4932. **/
  4933. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  4934. {
  4935. struct ibmvfc_target *tgt;
  4936. unsigned long flags;
  4937. struct fc_rport *rport;
  4938. LIST_HEAD(purge);
  4939. int rc;
  4940. ibmvfc_log_ae(vhost, vhost->events_to_log);
  4941. spin_lock_irqsave(vhost->host->host_lock, flags);
  4942. vhost->events_to_log = 0;
  4943. switch (vhost->action) {
  4944. case IBMVFC_HOST_ACTION_NONE:
  4945. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  4946. case IBMVFC_HOST_ACTION_INIT_WAIT:
  4947. break;
  4948. case IBMVFC_HOST_ACTION_RESET:
  4949. list_splice_init(&vhost->purge, &purge);
  4950. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4951. ibmvfc_complete_purge(&purge);
  4952. rc = ibmvfc_reset_crq(vhost);
  4953. spin_lock_irqsave(vhost->host->host_lock, flags);
  4954. if (!rc || rc == H_CLOSED)
  4955. vio_enable_interrupts(to_vio_dev(vhost->dev));
  4956. if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
  4957. /*
  4958. * The only action we could have changed to would have
  4959. * been reenable, in which case, we skip the rest of
  4960. * this path and wait until we've done the re-enable
  4961. * before sending the crq init.
  4962. */
  4963. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  4964. if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
  4965. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  4966. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4967. dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
  4968. }
  4969. }
  4970. break;
  4971. case IBMVFC_HOST_ACTION_REENABLE:
  4972. list_splice_init(&vhost->purge, &purge);
  4973. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4974. ibmvfc_complete_purge(&purge);
  4975. rc = ibmvfc_reenable_crq_queue(vhost);
  4976. spin_lock_irqsave(vhost->host->host_lock, flags);
  4977. if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
  4978. /*
  4979. * The only action we could have changed to would have
  4980. * been reset, in which case, we skip the rest of this
  4981. * path and wait until we've done the reset before
  4982. * sending the crq init.
  4983. */
  4984. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  4985. if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
  4986. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  4987. dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
  4988. }
  4989. }
  4990. break;
  4991. case IBMVFC_HOST_ACTION_LOGO:
  4992. vhost->job_step(vhost);
  4993. break;
  4994. case IBMVFC_HOST_ACTION_INIT:
  4995. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  4996. if (vhost->delay_init) {
  4997. vhost->delay_init = 0;
  4998. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4999. ssleep(15);
  5000. return;
  5001. } else
  5002. vhost->job_step(vhost);
  5003. break;
  5004. case IBMVFC_HOST_ACTION_QUERY:
  5005. list_for_each_entry(tgt, &vhost->targets, queue)
  5006. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  5007. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  5008. break;
  5009. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  5010. list_for_each_entry(tgt, &vhost->targets, queue) {
  5011. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  5012. tgt->job_step(tgt);
  5013. break;
  5014. }
  5015. }
  5016. if (!ibmvfc_dev_init_to_do(vhost))
  5017. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  5018. break;
  5019. case IBMVFC_HOST_ACTION_TGT_DEL:
  5020. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  5021. list_for_each_entry(tgt, &vhost->targets, queue) {
  5022. if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
  5023. tgt->job_step(tgt);
  5024. break;
  5025. }
  5026. }
  5027. if (ibmvfc_dev_logo_to_do(vhost)) {
  5028. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5029. return;
  5030. }
  5031. list_for_each_entry(tgt, &vhost->targets, queue) {
  5032. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  5033. tgt_dbg(tgt, "Deleting rport\n");
  5034. rport = tgt->rport;
  5035. tgt->rport = NULL;
  5036. list_del(&tgt->queue);
  5037. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  5038. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5039. if (rport)
  5040. fc_remote_port_delete(rport);
  5041. timer_delete_sync(&tgt->timer);
  5042. kref_put(&tgt->kref, ibmvfc_release_tgt);
  5043. return;
  5044. } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
  5045. tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
  5046. rport = tgt->rport;
  5047. tgt->rport = NULL;
  5048. tgt->init_retries = 0;
  5049. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
  5050. /*
  5051. * If fast fail is enabled, we wait for it to fire and then clean up
  5052. * the old port, since we expect the fast fail timer to clean up the
  5053. * outstanding I/O faster than waiting for normal command timeouts.
  5054. * However, if fast fail is disabled, any I/O outstanding to the
  5055. * rport LUNs will stay outstanding indefinitely, since the EH handlers
  5056. * won't get invoked for I/O's timing out. If this is a NPIV failover
  5057. * scenario, the better alternative is to use the move login.
  5058. */
  5059. if (rport && rport->fast_io_fail_tmo == -1)
  5060. tgt->move_login = 1;
  5061. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5062. if (rport)
  5063. fc_remote_port_delete(rport);
  5064. return;
  5065. }
  5066. }
  5067. if (vhost->state == IBMVFC_INITIALIZING) {
  5068. if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
  5069. if (vhost->reinit) {
  5070. vhost->reinit = 0;
  5071. scsi_block_requests(vhost->host);
  5072. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  5073. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5074. } else {
  5075. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  5076. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  5077. wake_up(&vhost->init_wait_q);
  5078. schedule_work(&vhost->rport_add_work_q);
  5079. vhost->init_retries = 0;
  5080. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5081. scsi_unblock_requests(vhost->host);
  5082. }
  5083. return;
  5084. } else {
  5085. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  5086. vhost->job_step = ibmvfc_discover_targets;
  5087. }
  5088. } else {
  5089. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  5090. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5091. scsi_unblock_requests(vhost->host);
  5092. wake_up(&vhost->init_wait_q);
  5093. return;
  5094. }
  5095. break;
  5096. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  5097. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  5098. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5099. ibmvfc_alloc_targets(vhost);
  5100. spin_lock_irqsave(vhost->host->host_lock, flags);
  5101. break;
  5102. case IBMVFC_HOST_ACTION_TGT_INIT:
  5103. list_for_each_entry(tgt, &vhost->targets, queue) {
  5104. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  5105. tgt->job_step(tgt);
  5106. break;
  5107. }
  5108. }
  5109. if (!ibmvfc_dev_init_to_do(vhost))
  5110. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
  5111. break;
  5112. default:
  5113. break;
  5114. }
  5115. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5116. }
  5117. /**
  5118. * ibmvfc_work - Do task level work
  5119. * @data: ibmvfc host struct
  5120. *
  5121. * Returns:
  5122. * zero
  5123. **/
  5124. static int ibmvfc_work(void *data)
  5125. {
  5126. struct ibmvfc_host *vhost = data;
  5127. int rc;
  5128. set_user_nice(current, MIN_NICE);
  5129. while (1) {
  5130. rc = wait_event_interruptible(vhost->work_wait_q,
  5131. ibmvfc_work_to_do(vhost));
  5132. BUG_ON(rc);
  5133. if (kthread_should_stop())
  5134. break;
  5135. ibmvfc_do_work(vhost);
  5136. }
  5137. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  5138. return 0;
  5139. }
  5140. /**
  5141. * ibmvfc_alloc_queue - Allocate queue
  5142. * @vhost: ibmvfc host struct
  5143. * @queue: ibmvfc queue to allocate
  5144. * @fmt: queue format to allocate
  5145. *
  5146. * Returns:
  5147. * 0 on success / non-zero on failure
  5148. **/
  5149. static int ibmvfc_alloc_queue(struct ibmvfc_host *vhost,
  5150. struct ibmvfc_queue *queue,
  5151. enum ibmvfc_msg_fmt fmt)
  5152. {
  5153. struct device *dev = vhost->dev;
  5154. size_t fmt_size;
  5155. ENTER;
  5156. spin_lock_init(&queue->_lock);
  5157. queue->q_lock = &queue->_lock;
  5158. switch (fmt) {
  5159. case IBMVFC_CRQ_FMT:
  5160. fmt_size = sizeof(*queue->msgs.crq);
  5161. queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_REQ;
  5162. queue->evt_depth = scsi_qdepth;
  5163. queue->reserved_depth = IBMVFC_NUM_INTERNAL_REQ;
  5164. break;
  5165. case IBMVFC_ASYNC_FMT:
  5166. fmt_size = sizeof(*queue->msgs.async);
  5167. break;
  5168. case IBMVFC_SUB_CRQ_FMT:
  5169. fmt_size = sizeof(*queue->msgs.scrq);
  5170. /* We need one extra event for Cancel Commands */
  5171. queue->total_depth = scsi_qdepth + IBMVFC_NUM_INTERNAL_SUBQ_REQ;
  5172. queue->evt_depth = scsi_qdepth;
  5173. queue->reserved_depth = IBMVFC_NUM_INTERNAL_SUBQ_REQ;
  5174. break;
  5175. default:
  5176. dev_warn(dev, "Unknown command/response queue message format: %d\n", fmt);
  5177. return -EINVAL;
  5178. }
  5179. queue->fmt = fmt;
  5180. if (ibmvfc_init_event_pool(vhost, queue)) {
  5181. dev_err(dev, "Couldn't initialize event pool.\n");
  5182. return -ENOMEM;
  5183. }
  5184. queue->msgs.handle = (void *)get_zeroed_page(GFP_KERNEL);
  5185. if (!queue->msgs.handle)
  5186. return -ENOMEM;
  5187. queue->msg_token = dma_map_single(dev, queue->msgs.handle, PAGE_SIZE,
  5188. DMA_BIDIRECTIONAL);
  5189. if (dma_mapping_error(dev, queue->msg_token)) {
  5190. free_page((unsigned long)queue->msgs.handle);
  5191. queue->msgs.handle = NULL;
  5192. return -ENOMEM;
  5193. }
  5194. queue->cur = 0;
  5195. queue->size = PAGE_SIZE / fmt_size;
  5196. queue->vhost = vhost;
  5197. return 0;
  5198. }
  5199. /**
  5200. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  5201. * @vhost: ibmvfc host struct
  5202. *
  5203. * Allocates a page for messages, maps it for dma, and registers
  5204. * the crq with the hypervisor.
  5205. *
  5206. * Return value:
  5207. * zero on success / other on failure
  5208. **/
  5209. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  5210. {
  5211. int rc, retrc = -ENOMEM;
  5212. struct device *dev = vhost->dev;
  5213. struct vio_dev *vdev = to_vio_dev(dev);
  5214. struct ibmvfc_queue *crq = &vhost->crq;
  5215. ENTER;
  5216. if (ibmvfc_alloc_queue(vhost, crq, IBMVFC_CRQ_FMT))
  5217. return -ENOMEM;
  5218. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  5219. crq->msg_token, PAGE_SIZE);
  5220. if (rc == H_RESOURCE)
  5221. /* maybe kexecing and resource is busy. try a reset */
  5222. retrc = rc = ibmvfc_reset_crq(vhost);
  5223. if (rc == H_CLOSED)
  5224. dev_warn(dev, "Partner adapter not ready\n");
  5225. else if (rc) {
  5226. dev_warn(dev, "Error %d opening adapter\n", rc);
  5227. goto reg_crq_failed;
  5228. }
  5229. retrc = 0;
  5230. tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
  5231. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  5232. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  5233. goto req_irq_failed;
  5234. }
  5235. if ((rc = vio_enable_interrupts(vdev))) {
  5236. dev_err(dev, "Error %d enabling interrupts\n", rc);
  5237. goto req_irq_failed;
  5238. }
  5239. LEAVE;
  5240. return retrc;
  5241. req_irq_failed:
  5242. tasklet_kill(&vhost->tasklet);
  5243. do {
  5244. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  5245. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5246. reg_crq_failed:
  5247. ibmvfc_free_queue(vhost, crq);
  5248. return retrc;
  5249. }
  5250. static int ibmvfc_register_channel(struct ibmvfc_host *vhost,
  5251. struct ibmvfc_channels *channels,
  5252. int index)
  5253. {
  5254. struct device *dev = vhost->dev;
  5255. struct vio_dev *vdev = to_vio_dev(dev);
  5256. struct ibmvfc_queue *scrq = &channels->scrqs[index];
  5257. int rc = -ENOMEM;
  5258. ENTER;
  5259. rc = h_reg_sub_crq(vdev->unit_address, scrq->msg_token, PAGE_SIZE,
  5260. &scrq->cookie, &scrq->hw_irq);
  5261. /* H_CLOSED indicates successful register, but no CRQ partner */
  5262. if (rc && rc != H_CLOSED) {
  5263. dev_warn(dev, "Error registering sub-crq: %d\n", rc);
  5264. if (rc == H_PARAMETER)
  5265. dev_warn_once(dev, "Firmware may not support MQ\n");
  5266. goto reg_failed;
  5267. }
  5268. scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
  5269. if (!scrq->irq) {
  5270. rc = -EINVAL;
  5271. dev_err(dev, "Error mapping sub-crq[%d] irq\n", index);
  5272. goto irq_failed;
  5273. }
  5274. switch (channels->protocol) {
  5275. case IBMVFC_PROTO_SCSI:
  5276. snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-scsi%d",
  5277. vdev->unit_address, index);
  5278. scrq->handler = ibmvfc_interrupt_mq;
  5279. break;
  5280. case IBMVFC_PROTO_NVME:
  5281. snprintf(scrq->name, sizeof(scrq->name), "ibmvfc-%x-nvmf%d",
  5282. vdev->unit_address, index);
  5283. scrq->handler = ibmvfc_interrupt_mq;
  5284. break;
  5285. default:
  5286. dev_err(dev, "Unknown channel protocol (%d)\n",
  5287. channels->protocol);
  5288. goto irq_failed;
  5289. }
  5290. rc = request_irq(scrq->irq, scrq->handler, 0, scrq->name, scrq);
  5291. if (rc) {
  5292. dev_err(dev, "Couldn't register sub-crq[%d] irq\n", index);
  5293. irq_dispose_mapping(scrq->irq);
  5294. goto irq_failed;
  5295. }
  5296. scrq->hwq_id = index;
  5297. LEAVE;
  5298. return 0;
  5299. irq_failed:
  5300. do {
  5301. rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address, scrq->cookie);
  5302. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5303. reg_failed:
  5304. LEAVE;
  5305. return rc;
  5306. }
  5307. static void ibmvfc_deregister_channel(struct ibmvfc_host *vhost,
  5308. struct ibmvfc_channels *channels,
  5309. int index)
  5310. {
  5311. struct device *dev = vhost->dev;
  5312. struct vio_dev *vdev = to_vio_dev(dev);
  5313. struct ibmvfc_queue *scrq = &channels->scrqs[index];
  5314. long rc;
  5315. ENTER;
  5316. free_irq(scrq->irq, scrq);
  5317. irq_dispose_mapping(scrq->irq);
  5318. scrq->irq = 0;
  5319. do {
  5320. rc = plpar_hcall_norets(H_FREE_SUB_CRQ, vdev->unit_address,
  5321. scrq->cookie);
  5322. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  5323. if (rc)
  5324. dev_err(dev, "Failed to free sub-crq[%d]: rc=%ld\n", index, rc);
  5325. /* Clean out the queue */
  5326. memset(scrq->msgs.crq, 0, PAGE_SIZE);
  5327. scrq->cur = 0;
  5328. LEAVE;
  5329. }
  5330. static void ibmvfc_reg_sub_crqs(struct ibmvfc_host *vhost,
  5331. struct ibmvfc_channels *channels)
  5332. {
  5333. int i, j;
  5334. ENTER;
  5335. if (!vhost->mq_enabled || !channels->scrqs)
  5336. return;
  5337. for (i = 0; i < channels->max_queues; i++) {
  5338. if (ibmvfc_register_channel(vhost, channels, i)) {
  5339. for (j = i; j > 0; j--)
  5340. ibmvfc_deregister_channel(vhost, channels, j - 1);
  5341. vhost->do_enquiry = 0;
  5342. return;
  5343. }
  5344. }
  5345. LEAVE;
  5346. }
  5347. static void ibmvfc_dereg_sub_crqs(struct ibmvfc_host *vhost,
  5348. struct ibmvfc_channels *channels)
  5349. {
  5350. int i;
  5351. ENTER;
  5352. if (!vhost->mq_enabled || !channels->scrqs)
  5353. return;
  5354. for (i = 0; i < channels->max_queues; i++)
  5355. ibmvfc_deregister_channel(vhost, channels, i);
  5356. LEAVE;
  5357. }
  5358. static int ibmvfc_alloc_channels(struct ibmvfc_host *vhost,
  5359. struct ibmvfc_channels *channels)
  5360. {
  5361. struct ibmvfc_queue *scrq;
  5362. int i, j;
  5363. int rc = 0;
  5364. channels->scrqs = kzalloc_objs(*channels->scrqs, channels->max_queues);
  5365. if (!channels->scrqs)
  5366. return -ENOMEM;
  5367. for (i = 0; i < channels->max_queues; i++) {
  5368. scrq = &channels->scrqs[i];
  5369. rc = ibmvfc_alloc_queue(vhost, scrq, IBMVFC_SUB_CRQ_FMT);
  5370. if (rc) {
  5371. for (j = i; j > 0; j--) {
  5372. scrq = &channels->scrqs[j - 1];
  5373. ibmvfc_free_queue(vhost, scrq);
  5374. }
  5375. kfree(channels->scrqs);
  5376. channels->scrqs = NULL;
  5377. channels->active_queues = 0;
  5378. return rc;
  5379. }
  5380. }
  5381. return rc;
  5382. }
  5383. static void ibmvfc_init_sub_crqs(struct ibmvfc_host *vhost)
  5384. {
  5385. ENTER;
  5386. if (!vhost->mq_enabled)
  5387. return;
  5388. if (ibmvfc_alloc_channels(vhost, &vhost->scsi_scrqs)) {
  5389. vhost->do_enquiry = 0;
  5390. vhost->mq_enabled = 0;
  5391. return;
  5392. }
  5393. ibmvfc_reg_sub_crqs(vhost, &vhost->scsi_scrqs);
  5394. LEAVE;
  5395. }
  5396. static void ibmvfc_release_channels(struct ibmvfc_host *vhost,
  5397. struct ibmvfc_channels *channels)
  5398. {
  5399. struct ibmvfc_queue *scrq;
  5400. int i;
  5401. if (channels->scrqs) {
  5402. for (i = 0; i < channels->max_queues; i++) {
  5403. scrq = &channels->scrqs[i];
  5404. ibmvfc_free_queue(vhost, scrq);
  5405. }
  5406. kfree(channels->scrqs);
  5407. channels->scrqs = NULL;
  5408. channels->active_queues = 0;
  5409. }
  5410. }
  5411. static void ibmvfc_release_sub_crqs(struct ibmvfc_host *vhost)
  5412. {
  5413. ENTER;
  5414. if (!vhost->scsi_scrqs.scrqs)
  5415. return;
  5416. ibmvfc_dereg_sub_crqs(vhost, &vhost->scsi_scrqs);
  5417. ibmvfc_release_channels(vhost, &vhost->scsi_scrqs);
  5418. LEAVE;
  5419. }
  5420. static void ibmvfc_free_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
  5421. {
  5422. dma_free_coherent(dev, channels->disc_buf_sz, channels->disc_buf,
  5423. channels->disc_buf_dma);
  5424. }
  5425. /**
  5426. * ibmvfc_free_mem - Free memory for vhost
  5427. * @vhost: ibmvfc host struct
  5428. *
  5429. * Return value:
  5430. * none
  5431. **/
  5432. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  5433. {
  5434. struct ibmvfc_queue *async_q = &vhost->async_crq;
  5435. ENTER;
  5436. mempool_destroy(vhost->tgt_pool);
  5437. kfree(vhost->trace);
  5438. ibmvfc_free_disc_buf(vhost->dev, &vhost->scsi_scrqs);
  5439. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  5440. vhost->login_buf, vhost->login_buf_dma);
  5441. dma_free_coherent(vhost->dev, sizeof(*vhost->channel_setup_buf),
  5442. vhost->channel_setup_buf, vhost->channel_setup_dma);
  5443. dma_pool_destroy(vhost->sg_pool);
  5444. ibmvfc_free_queue(vhost, async_q);
  5445. LEAVE;
  5446. }
  5447. static int ibmvfc_alloc_disc_buf(struct device *dev, struct ibmvfc_channels *channels)
  5448. {
  5449. channels->disc_buf_sz = sizeof(*channels->disc_buf) * max_targets;
  5450. channels->disc_buf = dma_alloc_coherent(dev, channels->disc_buf_sz,
  5451. &channels->disc_buf_dma, GFP_KERNEL);
  5452. if (!channels->disc_buf) {
  5453. dev_err(dev, "Couldn't allocate %s Discover Targets buffer\n",
  5454. (channels->protocol == IBMVFC_PROTO_SCSI) ? "SCSI" : "NVMe");
  5455. return -ENOMEM;
  5456. }
  5457. return 0;
  5458. }
  5459. /**
  5460. * ibmvfc_alloc_mem - Allocate memory for vhost
  5461. * @vhost: ibmvfc host struct
  5462. *
  5463. * Return value:
  5464. * 0 on success / non-zero on failure
  5465. **/
  5466. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  5467. {
  5468. struct ibmvfc_queue *async_q = &vhost->async_crq;
  5469. struct device *dev = vhost->dev;
  5470. ENTER;
  5471. if (ibmvfc_alloc_queue(vhost, async_q, IBMVFC_ASYNC_FMT)) {
  5472. dev_err(dev, "Couldn't allocate/map async queue.\n");
  5473. goto nomem;
  5474. }
  5475. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  5476. SG_ALL * sizeof(struct srp_direct_buf),
  5477. sizeof(struct srp_direct_buf), 0);
  5478. if (!vhost->sg_pool) {
  5479. dev_err(dev, "Failed to allocate sg pool\n");
  5480. goto unmap_async_crq;
  5481. }
  5482. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  5483. &vhost->login_buf_dma, GFP_KERNEL);
  5484. if (!vhost->login_buf) {
  5485. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  5486. goto free_sg_pool;
  5487. }
  5488. if (ibmvfc_alloc_disc_buf(dev, &vhost->scsi_scrqs))
  5489. goto free_login_buffer;
  5490. vhost->trace = kzalloc_objs(struct ibmvfc_trace_entry,
  5491. IBMVFC_NUM_TRACE_ENTRIES);
  5492. atomic_set(&vhost->trace_index, -1);
  5493. if (!vhost->trace)
  5494. goto free_scsi_disc_buffer;
  5495. vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  5496. sizeof(struct ibmvfc_target));
  5497. if (!vhost->tgt_pool) {
  5498. dev_err(dev, "Couldn't allocate target memory pool\n");
  5499. goto free_trace;
  5500. }
  5501. vhost->channel_setup_buf = dma_alloc_coherent(dev, sizeof(*vhost->channel_setup_buf),
  5502. &vhost->channel_setup_dma,
  5503. GFP_KERNEL);
  5504. if (!vhost->channel_setup_buf) {
  5505. dev_err(dev, "Couldn't allocate Channel Setup buffer\n");
  5506. goto free_tgt_pool;
  5507. }
  5508. LEAVE;
  5509. return 0;
  5510. free_tgt_pool:
  5511. mempool_destroy(vhost->tgt_pool);
  5512. free_trace:
  5513. kfree(vhost->trace);
  5514. free_scsi_disc_buffer:
  5515. ibmvfc_free_disc_buf(dev, &vhost->scsi_scrqs);
  5516. free_login_buffer:
  5517. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  5518. vhost->login_buf, vhost->login_buf_dma);
  5519. free_sg_pool:
  5520. dma_pool_destroy(vhost->sg_pool);
  5521. unmap_async_crq:
  5522. ibmvfc_free_queue(vhost, async_q);
  5523. nomem:
  5524. LEAVE;
  5525. return -ENOMEM;
  5526. }
  5527. /**
  5528. * ibmvfc_rport_add_thread - Worker thread for rport adds
  5529. * @work: work struct
  5530. *
  5531. **/
  5532. static void ibmvfc_rport_add_thread(struct work_struct *work)
  5533. {
  5534. struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
  5535. rport_add_work_q);
  5536. struct ibmvfc_target *tgt;
  5537. struct fc_rport *rport;
  5538. unsigned long flags;
  5539. int did_work;
  5540. ENTER;
  5541. spin_lock_irqsave(vhost->host->host_lock, flags);
  5542. do {
  5543. did_work = 0;
  5544. if (vhost->state != IBMVFC_ACTIVE)
  5545. break;
  5546. list_for_each_entry(tgt, &vhost->targets, queue) {
  5547. if (tgt->add_rport) {
  5548. did_work = 1;
  5549. tgt->add_rport = 0;
  5550. kref_get(&tgt->kref);
  5551. rport = tgt->rport;
  5552. if (!rport) {
  5553. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5554. ibmvfc_tgt_add_rport(tgt);
  5555. } else if (get_device(&rport->dev)) {
  5556. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5557. tgt_dbg(tgt, "Setting rport roles\n");
  5558. fc_remote_port_rolechg(rport, tgt->ids.roles);
  5559. put_device(&rport->dev);
  5560. } else {
  5561. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5562. }
  5563. kref_put(&tgt->kref, ibmvfc_release_tgt);
  5564. spin_lock_irqsave(vhost->host->host_lock, flags);
  5565. break;
  5566. }
  5567. }
  5568. } while(did_work);
  5569. if (vhost->state == IBMVFC_ACTIVE)
  5570. vhost->scan_complete = 1;
  5571. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5572. LEAVE;
  5573. }
  5574. /**
  5575. * ibmvfc_probe - Adapter hot plug add entry point
  5576. * @vdev: vio device struct
  5577. * @id: vio device id struct
  5578. *
  5579. * Return value:
  5580. * 0 on success / non-zero on failure
  5581. **/
  5582. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  5583. {
  5584. struct ibmvfc_host *vhost;
  5585. struct Scsi_Host *shost;
  5586. struct device *dev = &vdev->dev;
  5587. int rc = -ENOMEM;
  5588. unsigned int online_cpus = num_online_cpus();
  5589. unsigned int max_scsi_queues = min((unsigned int)IBMVFC_MAX_SCSI_QUEUES, online_cpus);
  5590. ENTER;
  5591. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  5592. if (!shost) {
  5593. dev_err(dev, "Couldn't allocate host data\n");
  5594. goto out;
  5595. }
  5596. shost->transportt = ibmvfc_transport_template;
  5597. shost->can_queue = scsi_qdepth;
  5598. shost->max_lun = max_lun;
  5599. shost->max_id = max_targets;
  5600. shost->max_sectors = max_sectors;
  5601. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  5602. shost->unique_id = shost->host_no;
  5603. shost->nr_hw_queues = mq_enabled ? min(max_scsi_queues, nr_scsi_hw_queues) : 1;
  5604. vhost = shost_priv(shost);
  5605. INIT_LIST_HEAD(&vhost->targets);
  5606. INIT_LIST_HEAD(&vhost->purge);
  5607. sprintf(vhost->name, IBMVFC_NAME);
  5608. vhost->host = shost;
  5609. vhost->dev = dev;
  5610. vhost->partition_number = -1;
  5611. vhost->log_level = log_level;
  5612. vhost->task_set = 1;
  5613. vhost->mq_enabled = mq_enabled;
  5614. vhost->scsi_scrqs.desired_queues = min(shost->nr_hw_queues, nr_scsi_channels);
  5615. vhost->scsi_scrqs.max_queues = shost->nr_hw_queues;
  5616. vhost->scsi_scrqs.protocol = IBMVFC_PROTO_SCSI;
  5617. vhost->using_channels = 0;
  5618. vhost->do_enquiry = 1;
  5619. vhost->scan_timeout = 0;
  5620. strcpy(vhost->partition_name, "UNKNOWN");
  5621. init_waitqueue_head(&vhost->work_wait_q);
  5622. init_waitqueue_head(&vhost->init_wait_q);
  5623. INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
  5624. mutex_init(&vhost->passthru_mutex);
  5625. if ((rc = ibmvfc_alloc_mem(vhost)))
  5626. goto free_scsi_host;
  5627. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  5628. shost->host_no);
  5629. if (IS_ERR(vhost->work_thread)) {
  5630. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  5631. PTR_ERR(vhost->work_thread));
  5632. rc = PTR_ERR(vhost->work_thread);
  5633. goto free_host_mem;
  5634. }
  5635. if ((rc = ibmvfc_init_crq(vhost))) {
  5636. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  5637. goto kill_kthread;
  5638. }
  5639. if ((rc = scsi_add_host(shost, dev)))
  5640. goto release_crq;
  5641. fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
  5642. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  5643. &ibmvfc_trace_attr))) {
  5644. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  5645. goto remove_shost;
  5646. }
  5647. ibmvfc_init_sub_crqs(vhost);
  5648. dev_set_drvdata(dev, vhost);
  5649. spin_lock(&ibmvfc_driver_lock);
  5650. list_add_tail(&vhost->queue, &ibmvfc_head);
  5651. spin_unlock(&ibmvfc_driver_lock);
  5652. ibmvfc_send_crq_init(vhost);
  5653. scsi_scan_host(shost);
  5654. return 0;
  5655. remove_shost:
  5656. scsi_remove_host(shost);
  5657. release_crq:
  5658. ibmvfc_release_crq_queue(vhost);
  5659. kill_kthread:
  5660. kthread_stop(vhost->work_thread);
  5661. free_host_mem:
  5662. ibmvfc_free_mem(vhost);
  5663. free_scsi_host:
  5664. scsi_host_put(shost);
  5665. out:
  5666. LEAVE;
  5667. return rc;
  5668. }
  5669. /**
  5670. * ibmvfc_remove - Adapter hot plug remove entry point
  5671. * @vdev: vio device struct
  5672. *
  5673. * Return value:
  5674. * 0
  5675. **/
  5676. static void ibmvfc_remove(struct vio_dev *vdev)
  5677. {
  5678. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  5679. LIST_HEAD(purge);
  5680. unsigned long flags;
  5681. ENTER;
  5682. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  5683. spin_lock_irqsave(vhost->host->host_lock, flags);
  5684. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  5685. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5686. ibmvfc_wait_while_resetting(vhost);
  5687. kthread_stop(vhost->work_thread);
  5688. fc_remove_host(vhost->host);
  5689. scsi_remove_host(vhost->host);
  5690. spin_lock_irqsave(vhost->host->host_lock, flags);
  5691. ibmvfc_purge_requests(vhost, DID_ERROR);
  5692. list_splice_init(&vhost->purge, &purge);
  5693. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5694. ibmvfc_complete_purge(&purge);
  5695. ibmvfc_release_sub_crqs(vhost);
  5696. ibmvfc_release_crq_queue(vhost);
  5697. ibmvfc_free_mem(vhost);
  5698. spin_lock(&ibmvfc_driver_lock);
  5699. list_del(&vhost->queue);
  5700. spin_unlock(&ibmvfc_driver_lock);
  5701. scsi_host_put(vhost->host);
  5702. LEAVE;
  5703. }
  5704. /**
  5705. * ibmvfc_resume - Resume from suspend
  5706. * @dev: device struct
  5707. *
  5708. * We may have lost an interrupt across suspend/resume, so kick the
  5709. * interrupt handler
  5710. *
  5711. */
  5712. static int ibmvfc_resume(struct device *dev)
  5713. {
  5714. unsigned long flags;
  5715. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  5716. struct vio_dev *vdev = to_vio_dev(dev);
  5717. spin_lock_irqsave(vhost->host->host_lock, flags);
  5718. vio_disable_interrupts(vdev);
  5719. tasklet_schedule(&vhost->tasklet);
  5720. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  5721. return 0;
  5722. }
  5723. /**
  5724. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  5725. * @vdev: vio device struct
  5726. *
  5727. * Return value:
  5728. * Number of bytes the driver will need to DMA map at the same time in
  5729. * order to perform well.
  5730. */
  5731. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  5732. {
  5733. unsigned long pool_dma;
  5734. pool_dma = (IBMVFC_MAX_SCSI_QUEUES * scsi_qdepth) * sizeof(union ibmvfc_iu);
  5735. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  5736. }
  5737. static const struct vio_device_id ibmvfc_device_table[] = {
  5738. {"fcp", "IBM,vfc-client"},
  5739. { "", "" }
  5740. };
  5741. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  5742. static const struct dev_pm_ops ibmvfc_pm_ops = {
  5743. .resume = ibmvfc_resume
  5744. };
  5745. static struct vio_driver ibmvfc_driver = {
  5746. .id_table = ibmvfc_device_table,
  5747. .probe = ibmvfc_probe,
  5748. .remove = ibmvfc_remove,
  5749. .get_desired_dma = ibmvfc_get_desired_dma,
  5750. .name = IBMVFC_NAME,
  5751. .pm = &ibmvfc_pm_ops,
  5752. };
  5753. static struct fc_function_template ibmvfc_transport_functions = {
  5754. .show_host_fabric_name = 1,
  5755. .show_host_node_name = 1,
  5756. .show_host_port_name = 1,
  5757. .show_host_supported_classes = 1,
  5758. .show_host_port_type = 1,
  5759. .show_host_port_id = 1,
  5760. .show_host_maxframe_size = 1,
  5761. .get_host_port_state = ibmvfc_get_host_port_state,
  5762. .show_host_port_state = 1,
  5763. .get_host_speed = ibmvfc_get_host_speed,
  5764. .show_host_speed = 1,
  5765. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  5766. .terminate_rport_io = ibmvfc_terminate_rport_io,
  5767. .show_rport_maxframe_size = 1,
  5768. .show_rport_supported_classes = 1,
  5769. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  5770. .show_rport_dev_loss_tmo = 1,
  5771. .get_starget_node_name = ibmvfc_get_starget_node_name,
  5772. .show_starget_node_name = 1,
  5773. .get_starget_port_name = ibmvfc_get_starget_port_name,
  5774. .show_starget_port_name = 1,
  5775. .get_starget_port_id = ibmvfc_get_starget_port_id,
  5776. .show_starget_port_id = 1,
  5777. .max_bsg_segments = 1,
  5778. .bsg_request = ibmvfc_bsg_request,
  5779. .bsg_timeout = ibmvfc_bsg_timeout,
  5780. };
  5781. /**
  5782. * ibmvfc_module_init - Initialize the ibmvfc module
  5783. *
  5784. * Return value:
  5785. * 0 on success / other on failure
  5786. **/
  5787. static int __init ibmvfc_module_init(void)
  5788. {
  5789. int min_max_sectors = PAGE_SIZE >> 9;
  5790. int rc;
  5791. if (!firmware_has_feature(FW_FEATURE_VIO))
  5792. return -ENODEV;
  5793. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  5794. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  5795. /*
  5796. * Range check the max_sectors module parameter. The upper bounds is
  5797. * implicity checked since the parameter is a ushort.
  5798. */
  5799. if (max_sectors < min_max_sectors) {
  5800. printk(KERN_ERR IBMVFC_NAME ": max_sectors must be at least %d.\n",
  5801. min_max_sectors);
  5802. max_sectors = min_max_sectors;
  5803. }
  5804. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  5805. if (!ibmvfc_transport_template)
  5806. return -ENOMEM;
  5807. rc = vio_register_driver(&ibmvfc_driver);
  5808. if (rc)
  5809. fc_release_transport(ibmvfc_transport_template);
  5810. return rc;
  5811. }
  5812. /**
  5813. * ibmvfc_module_exit - Teardown the ibmvfc module
  5814. *
  5815. * Return value:
  5816. * nothing
  5817. **/
  5818. static void __exit ibmvfc_module_exit(void)
  5819. {
  5820. vio_unregister_driver(&ibmvfc_driver);
  5821. fc_release_transport(ibmvfc_transport_template);
  5822. }
  5823. module_init(ibmvfc_module_init);
  5824. module_exit(ibmvfc_module_exit);