be_main.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898
  1. /*
  2. * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
  3. * Host Bus Adapters. Refer to the README file included with this package
  4. * for driver version and adapter compatibility.
  5. *
  6. * Copyright (c) 2018 Broadcom. All Rights Reserved.
  7. * The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of version 2 of the GNU General Public License as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful. ALL EXPRESS
  14. * OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15. * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  16. * OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
  17. * DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
  18. * See the GNU General Public License for more details, a copy of which
  19. * can be found in the file COPYING included with this package.
  20. *
  21. * Contact Information:
  22. * linux-drivers@broadcom.com
  23. *
  24. */
  25. #include <linux/reboot.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/pci.h>
  31. #include <linux/string.h>
  32. #include <linux/kernel.h>
  33. #include <linux/semaphore.h>
  34. #include <linux/iscsi_boot_sysfs.h>
  35. #include <linux/module.h>
  36. #include <linux/bsg-lib.h>
  37. #include <linux/irq_poll.h>
  38. #include <scsi/libiscsi.h>
  39. #include <scsi/scsi_bsg_iscsi.h>
  40. #include <scsi/scsi_netlink.h>
  41. #include <scsi/scsi_transport_iscsi.h>
  42. #include <scsi/scsi_transport.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_host.h>
  46. #include <scsi/scsi.h>
  47. #include "be_main.h"
  48. #include "be_iscsi.h"
  49. #include "be_mgmt.h"
  50. #include "be_cmds.h"
  51. static unsigned int be_iopoll_budget = 10;
  52. static unsigned int be_max_phys_size = 64;
  53. static unsigned int enable_msix = 1;
  54. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  55. MODULE_VERSION(BUILD_STR);
  56. MODULE_AUTHOR("Emulex Corporation");
  57. MODULE_LICENSE("GPL");
  58. module_param(be_iopoll_budget, int, 0);
  59. module_param(enable_msix, int, 0);
  60. module_param(be_max_phys_size, uint, S_IRUGO);
  61. MODULE_PARM_DESC(be_max_phys_size,
  62. "Maximum Size (In Kilobytes) of physically contiguous "
  63. "memory that can be allocated. Range is 16 - 128");
  64. #define beiscsi_disp_param(_name)\
  65. static ssize_t \
  66. beiscsi_##_name##_disp(struct device *dev,\
  67. struct device_attribute *attrib, char *buf) \
  68. { \
  69. struct Scsi_Host *shost = class_to_shost(dev);\
  70. struct beiscsi_hba *phba = iscsi_host_priv(shost); \
  71. return snprintf(buf, PAGE_SIZE, "%d\n",\
  72. phba->attr_##_name);\
  73. }
  74. #define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
  75. static int \
  76. beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
  77. {\
  78. if (val >= _minval && val <= _maxval) {\
  79. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  80. "BA_%d : beiscsi_"#_name" updated "\
  81. "from 0x%x ==> 0x%x\n",\
  82. phba->attr_##_name, val); \
  83. phba->attr_##_name = val;\
  84. return 0;\
  85. } \
  86. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
  87. "BA_%d beiscsi_"#_name" attribute "\
  88. "cannot be updated to 0x%x, "\
  89. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  90. return -EINVAL;\
  91. }
  92. #define beiscsi_store_param(_name) \
  93. static ssize_t \
  94. beiscsi_##_name##_store(struct device *dev,\
  95. struct device_attribute *attr, const char *buf,\
  96. size_t count) \
  97. { \
  98. struct Scsi_Host *shost = class_to_shost(dev);\
  99. struct beiscsi_hba *phba = iscsi_host_priv(shost);\
  100. uint32_t param_val = 0;\
  101. if (!isdigit(buf[0]))\
  102. return -EINVAL;\
  103. if (sscanf(buf, "%i", &param_val) != 1)\
  104. return -EINVAL;\
  105. if (beiscsi_##_name##_change(phba, param_val) == 0) \
  106. return strlen(buf);\
  107. else \
  108. return -EINVAL;\
  109. }
  110. #define beiscsi_init_param(_name, _minval, _maxval, _defval) \
  111. static int \
  112. beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
  113. { \
  114. if (val >= _minval && val <= _maxval) {\
  115. phba->attr_##_name = val;\
  116. return 0;\
  117. } \
  118. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  119. "BA_%d beiscsi_"#_name" attribute " \
  120. "cannot be updated to 0x%x, "\
  121. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  122. phba->attr_##_name = _defval;\
  123. return -EINVAL;\
  124. }
  125. #define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
  126. static uint beiscsi_##_name = _defval;\
  127. module_param(beiscsi_##_name, uint, S_IRUGO);\
  128. MODULE_PARM_DESC(beiscsi_##_name, _descp);\
  129. beiscsi_disp_param(_name)\
  130. beiscsi_change_param(_name, _minval, _maxval, _defval)\
  131. beiscsi_store_param(_name)\
  132. beiscsi_init_param(_name, _minval, _maxval, _defval)\
  133. DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
  134. beiscsi_##_name##_disp, beiscsi_##_name##_store)
  135. /*
  136. * When new log level added update the
  137. * the MAX allowed value for log_enable
  138. */
  139. BEISCSI_RW_ATTR(log_enable, 0x00,
  140. 0xFF, 0x00, "Enable logging Bit Mask\n"
  141. "\t\t\t\tInitialization Events : 0x01\n"
  142. "\t\t\t\tMailbox Events : 0x02\n"
  143. "\t\t\t\tMiscellaneous Events : 0x04\n"
  144. "\t\t\t\tError Handling : 0x08\n"
  145. "\t\t\t\tIO Path Events : 0x10\n"
  146. "\t\t\t\tConfiguration Path : 0x20\n"
  147. "\t\t\t\tiSCSI Protocol : 0x40\n");
  148. DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
  149. DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
  150. DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
  151. DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
  152. DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
  153. beiscsi_active_session_disp, NULL);
  154. DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
  155. beiscsi_free_session_disp, NULL);
  156. struct device_attribute *beiscsi_attrs[] = {
  157. &dev_attr_beiscsi_log_enable,
  158. &dev_attr_beiscsi_drvr_ver,
  159. &dev_attr_beiscsi_adapter_family,
  160. &dev_attr_beiscsi_fw_ver,
  161. &dev_attr_beiscsi_active_session_count,
  162. &dev_attr_beiscsi_free_session_count,
  163. &dev_attr_beiscsi_phys_port,
  164. NULL,
  165. };
  166. static char const *cqe_desc[] = {
  167. "RESERVED_DESC",
  168. "SOL_CMD_COMPLETE",
  169. "SOL_CMD_KILLED_DATA_DIGEST_ERR",
  170. "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
  171. "CXN_KILLED_BURST_LEN_MISMATCH",
  172. "CXN_KILLED_AHS_RCVD",
  173. "CXN_KILLED_HDR_DIGEST_ERR",
  174. "CXN_KILLED_UNKNOWN_HDR",
  175. "CXN_KILLED_STALE_ITT_TTT_RCVD",
  176. "CXN_KILLED_INVALID_ITT_TTT_RCVD",
  177. "CXN_KILLED_RST_RCVD",
  178. "CXN_KILLED_TIMED_OUT",
  179. "CXN_KILLED_RST_SENT",
  180. "CXN_KILLED_FIN_RCVD",
  181. "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
  182. "CXN_KILLED_BAD_WRB_INDEX_ERROR",
  183. "CXN_KILLED_OVER_RUN_RESIDUAL",
  184. "CXN_KILLED_UNDER_RUN_RESIDUAL",
  185. "CMD_KILLED_INVALID_STATSN_RCVD",
  186. "CMD_KILLED_INVALID_R2T_RCVD",
  187. "CMD_CXN_KILLED_LUN_INVALID",
  188. "CMD_CXN_KILLED_ICD_INVALID",
  189. "CMD_CXN_KILLED_ITT_INVALID",
  190. "CMD_CXN_KILLED_SEQ_OUTOFORDER",
  191. "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
  192. "CXN_INVALIDATE_NOTIFY",
  193. "CXN_INVALIDATE_INDEX_NOTIFY",
  194. "CMD_INVALIDATED_NOTIFY",
  195. "UNSOL_HDR_NOTIFY",
  196. "UNSOL_DATA_NOTIFY",
  197. "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
  198. "DRIVERMSG_NOTIFY",
  199. "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
  200. "SOL_CMD_KILLED_DIF_ERR",
  201. "CXN_KILLED_SYN_RCVD",
  202. "CXN_KILLED_IMM_DATA_RCVD"
  203. };
  204. static int beiscsi_slave_configure(struct scsi_device *sdev)
  205. {
  206. blk_queue_max_segment_size(sdev->request_queue, 65536);
  207. return 0;
  208. }
  209. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  210. {
  211. struct iscsi_task *abrt_task = (struct iscsi_task *)sc->SCp.ptr;
  212. struct iscsi_cls_session *cls_session;
  213. struct beiscsi_io_task *abrt_io_task;
  214. struct beiscsi_conn *beiscsi_conn;
  215. struct iscsi_session *session;
  216. struct invldt_cmd_tbl inv_tbl;
  217. struct beiscsi_hba *phba;
  218. struct iscsi_conn *conn;
  219. int rc;
  220. cls_session = starget_to_session(scsi_target(sc->device));
  221. session = cls_session->dd_data;
  222. /* check if we raced, task just got cleaned up under us */
  223. spin_lock_bh(&session->back_lock);
  224. if (!abrt_task || !abrt_task->sc) {
  225. spin_unlock_bh(&session->back_lock);
  226. return SUCCESS;
  227. }
  228. /* get a task ref till FW processes the req for the ICD used */
  229. __iscsi_get_task(abrt_task);
  230. abrt_io_task = abrt_task->dd_data;
  231. conn = abrt_task->conn;
  232. beiscsi_conn = conn->dd_data;
  233. phba = beiscsi_conn->phba;
  234. /* mark WRB invalid which have been not processed by FW yet */
  235. if (is_chip_be2_be3r(phba)) {
  236. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  237. abrt_io_task->pwrb_handle->pwrb, 1);
  238. } else {
  239. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
  240. abrt_io_task->pwrb_handle->pwrb, 1);
  241. }
  242. inv_tbl.cid = beiscsi_conn->beiscsi_conn_cid;
  243. inv_tbl.icd = abrt_io_task->psgl_handle->sgl_index;
  244. spin_unlock_bh(&session->back_lock);
  245. rc = beiscsi_mgmt_invalidate_icds(phba, &inv_tbl, 1);
  246. iscsi_put_task(abrt_task);
  247. if (rc) {
  248. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  249. "BM_%d : sc %p invalidation failed %d\n",
  250. sc, rc);
  251. return FAILED;
  252. }
  253. return iscsi_eh_abort(sc);
  254. }
  255. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  256. {
  257. struct beiscsi_invldt_cmd_tbl {
  258. struct invldt_cmd_tbl tbl[BE_INVLDT_CMD_TBL_SZ];
  259. struct iscsi_task *task[BE_INVLDT_CMD_TBL_SZ];
  260. } *inv_tbl;
  261. struct iscsi_cls_session *cls_session;
  262. struct beiscsi_conn *beiscsi_conn;
  263. struct beiscsi_io_task *io_task;
  264. struct iscsi_session *session;
  265. struct beiscsi_hba *phba;
  266. struct iscsi_conn *conn;
  267. struct iscsi_task *task;
  268. unsigned int i, nents;
  269. int rc, more = 0;
  270. cls_session = starget_to_session(scsi_target(sc->device));
  271. session = cls_session->dd_data;
  272. spin_lock_bh(&session->frwd_lock);
  273. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
  274. spin_unlock_bh(&session->frwd_lock);
  275. return FAILED;
  276. }
  277. conn = session->leadconn;
  278. beiscsi_conn = conn->dd_data;
  279. phba = beiscsi_conn->phba;
  280. inv_tbl = kzalloc(sizeof(*inv_tbl), GFP_ATOMIC);
  281. if (!inv_tbl) {
  282. spin_unlock_bh(&session->frwd_lock);
  283. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  284. "BM_%d : invldt_cmd_tbl alloc failed\n");
  285. return FAILED;
  286. }
  287. nents = 0;
  288. /* take back_lock to prevent task from getting cleaned up under us */
  289. spin_lock(&session->back_lock);
  290. for (i = 0; i < conn->session->cmds_max; i++) {
  291. task = conn->session->cmds[i];
  292. if (!task->sc)
  293. continue;
  294. if (sc->device->lun != task->sc->device->lun)
  295. continue;
  296. /**
  297. * Can't fit in more cmds? Normally this won't happen b'coz
  298. * BEISCSI_CMD_PER_LUN is same as BE_INVLDT_CMD_TBL_SZ.
  299. */
  300. if (nents == BE_INVLDT_CMD_TBL_SZ) {
  301. more = 1;
  302. break;
  303. }
  304. /* get a task ref till FW processes the req for the ICD used */
  305. __iscsi_get_task(task);
  306. io_task = task->dd_data;
  307. /* mark WRB invalid which have been not processed by FW yet */
  308. if (is_chip_be2_be3r(phba)) {
  309. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  310. io_task->pwrb_handle->pwrb, 1);
  311. } else {
  312. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
  313. io_task->pwrb_handle->pwrb, 1);
  314. }
  315. inv_tbl->tbl[nents].cid = beiscsi_conn->beiscsi_conn_cid;
  316. inv_tbl->tbl[nents].icd = io_task->psgl_handle->sgl_index;
  317. inv_tbl->task[nents] = task;
  318. nents++;
  319. }
  320. spin_unlock(&session->back_lock);
  321. spin_unlock_bh(&session->frwd_lock);
  322. rc = SUCCESS;
  323. if (!nents)
  324. goto end_reset;
  325. if (more) {
  326. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  327. "BM_%d : number of cmds exceeds size of invalidation table\n");
  328. rc = FAILED;
  329. goto end_reset;
  330. }
  331. if (beiscsi_mgmt_invalidate_icds(phba, &inv_tbl->tbl[0], nents)) {
  332. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  333. "BM_%d : cid %u scmds invalidation failed\n",
  334. beiscsi_conn->beiscsi_conn_cid);
  335. rc = FAILED;
  336. }
  337. end_reset:
  338. for (i = 0; i < nents; i++)
  339. iscsi_put_task(inv_tbl->task[i]);
  340. kfree(inv_tbl);
  341. if (rc == SUCCESS)
  342. rc = iscsi_eh_device_reset(sc);
  343. return rc;
  344. }
  345. /*------------------- PCI Driver operations and data ----------------- */
  346. static const struct pci_device_id beiscsi_pci_id_table[] = {
  347. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  348. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  349. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  350. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  351. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  352. { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
  353. { 0 }
  354. };
  355. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  356. static struct scsi_host_template beiscsi_sht = {
  357. .module = THIS_MODULE,
  358. .name = "Emulex 10Gbe open-iscsi Initiator Driver",
  359. .proc_name = DRV_NAME,
  360. .queuecommand = iscsi_queuecommand,
  361. .change_queue_depth = scsi_change_queue_depth,
  362. .slave_configure = beiscsi_slave_configure,
  363. .target_alloc = iscsi_target_alloc,
  364. .eh_timed_out = iscsi_eh_cmd_timed_out,
  365. .eh_abort_handler = beiscsi_eh_abort,
  366. .eh_device_reset_handler = beiscsi_eh_device_reset,
  367. .eh_target_reset_handler = iscsi_eh_session_reset,
  368. .shost_attrs = beiscsi_attrs,
  369. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  370. .can_queue = BE2_IO_DEPTH,
  371. .this_id = -1,
  372. .max_sectors = BEISCSI_MAX_SECTORS,
  373. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  374. .use_clustering = ENABLE_CLUSTERING,
  375. .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
  376. .track_queue_depth = 1,
  377. };
  378. static struct scsi_transport_template *beiscsi_scsi_transport;
  379. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  380. {
  381. struct beiscsi_hba *phba;
  382. struct Scsi_Host *shost;
  383. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  384. if (!shost) {
  385. dev_err(&pcidev->dev,
  386. "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
  387. return NULL;
  388. }
  389. shost->max_id = BE2_MAX_SESSIONS;
  390. shost->max_channel = 0;
  391. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  392. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  393. shost->transportt = beiscsi_scsi_transport;
  394. phba = iscsi_host_priv(shost);
  395. memset(phba, 0, sizeof(*phba));
  396. phba->shost = shost;
  397. phba->pcidev = pci_dev_get(pcidev);
  398. pci_set_drvdata(pcidev, phba);
  399. phba->interface_handle = 0xFFFFFFFF;
  400. return phba;
  401. }
  402. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  403. {
  404. if (phba->csr_va) {
  405. iounmap(phba->csr_va);
  406. phba->csr_va = NULL;
  407. }
  408. if (phba->db_va) {
  409. iounmap(phba->db_va);
  410. phba->db_va = NULL;
  411. }
  412. if (phba->pci_va) {
  413. iounmap(phba->pci_va);
  414. phba->pci_va = NULL;
  415. }
  416. }
  417. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  418. struct pci_dev *pcidev)
  419. {
  420. u8 __iomem *addr;
  421. int pcicfg_reg;
  422. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  423. pci_resource_len(pcidev, 2));
  424. if (addr == NULL)
  425. return -ENOMEM;
  426. phba->ctrl.csr = addr;
  427. phba->csr_va = addr;
  428. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  429. if (addr == NULL)
  430. goto pci_map_err;
  431. phba->ctrl.db = addr;
  432. phba->db_va = addr;
  433. if (phba->generation == BE_GEN2)
  434. pcicfg_reg = 1;
  435. else
  436. pcicfg_reg = 0;
  437. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  438. pci_resource_len(pcidev, pcicfg_reg));
  439. if (addr == NULL)
  440. goto pci_map_err;
  441. phba->ctrl.pcicfg = addr;
  442. phba->pci_va = addr;
  443. return 0;
  444. pci_map_err:
  445. beiscsi_unmap_pci_function(phba);
  446. return -ENOMEM;
  447. }
  448. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  449. {
  450. int ret;
  451. ret = pci_enable_device(pcidev);
  452. if (ret) {
  453. dev_err(&pcidev->dev,
  454. "beiscsi_enable_pci - enable device failed\n");
  455. return ret;
  456. }
  457. ret = pci_request_regions(pcidev, DRV_NAME);
  458. if (ret) {
  459. dev_err(&pcidev->dev,
  460. "beiscsi_enable_pci - request region failed\n");
  461. goto pci_dev_disable;
  462. }
  463. pci_set_master(pcidev);
  464. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
  465. if (ret) {
  466. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
  467. if (ret) {
  468. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  469. goto pci_region_release;
  470. } else {
  471. ret = pci_set_consistent_dma_mask(pcidev,
  472. DMA_BIT_MASK(32));
  473. }
  474. } else {
  475. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
  476. if (ret) {
  477. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  478. goto pci_region_release;
  479. }
  480. }
  481. return 0;
  482. pci_region_release:
  483. pci_release_regions(pcidev);
  484. pci_dev_disable:
  485. pci_disable_device(pcidev);
  486. return ret;
  487. }
  488. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  489. {
  490. struct be_ctrl_info *ctrl = &phba->ctrl;
  491. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  492. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  493. int status = 0;
  494. ctrl->pdev = pdev;
  495. status = beiscsi_map_pci_bars(phba, pdev);
  496. if (status)
  497. return status;
  498. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  499. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  500. mbox_mem_alloc->size,
  501. &mbox_mem_alloc->dma);
  502. if (!mbox_mem_alloc->va) {
  503. beiscsi_unmap_pci_function(phba);
  504. return -ENOMEM;
  505. }
  506. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  507. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  508. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  509. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  510. mutex_init(&ctrl->mbox_lock);
  511. spin_lock_init(&phba->ctrl.mcc_lock);
  512. return status;
  513. }
  514. /**
  515. * beiscsi_get_params()- Set the config paramters
  516. * @phba: ptr device priv structure
  517. **/
  518. static void beiscsi_get_params(struct beiscsi_hba *phba)
  519. {
  520. uint32_t total_cid_count = 0;
  521. uint32_t total_icd_count = 0;
  522. uint8_t ulp_num = 0;
  523. total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
  524. BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
  525. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  526. uint32_t align_mask = 0;
  527. uint32_t icd_post_per_page = 0;
  528. uint32_t icd_count_unavailable = 0;
  529. uint32_t icd_start = 0, icd_count = 0;
  530. uint32_t icd_start_align = 0, icd_count_align = 0;
  531. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  532. icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  533. icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  534. /* Get ICD count that can be posted on each page */
  535. icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
  536. sizeof(struct iscsi_sge)));
  537. align_mask = (icd_post_per_page - 1);
  538. /* Check if icd_start is aligned ICD per page posting */
  539. if (icd_start % icd_post_per_page) {
  540. icd_start_align = ((icd_start +
  541. icd_post_per_page) &
  542. ~(align_mask));
  543. phba->fw_config.
  544. iscsi_icd_start[ulp_num] =
  545. icd_start_align;
  546. }
  547. icd_count_align = (icd_count & ~align_mask);
  548. /* ICD discarded in the process of alignment */
  549. if (icd_start_align)
  550. icd_count_unavailable = ((icd_start_align -
  551. icd_start) +
  552. (icd_count -
  553. icd_count_align));
  554. /* Updated ICD count available */
  555. phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
  556. icd_count_unavailable);
  557. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  558. "BM_%d : Aligned ICD values\n"
  559. "\t ICD Start : %d\n"
  560. "\t ICD Count : %d\n"
  561. "\t ICD Discarded : %d\n",
  562. phba->fw_config.
  563. iscsi_icd_start[ulp_num],
  564. phba->fw_config.
  565. iscsi_icd_count[ulp_num],
  566. icd_count_unavailable);
  567. break;
  568. }
  569. }
  570. total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  571. phba->params.ios_per_ctrl = (total_icd_count -
  572. (total_cid_count +
  573. BE2_TMFS + BE2_NOPOUT_REQ));
  574. phba->params.cxns_per_ctrl = total_cid_count;
  575. phba->params.icds_per_ctrl = total_icd_count;
  576. phba->params.num_sge_per_io = BE2_SGE;
  577. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  578. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  579. phba->params.num_eq_entries = 1024;
  580. phba->params.num_cq_entries = 1024;
  581. phba->params.wrbs_per_cxn = 256;
  582. }
  583. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  584. unsigned int id, unsigned int clr_interrupt,
  585. unsigned int num_processed,
  586. unsigned char rearm, unsigned char event)
  587. {
  588. u32 val = 0;
  589. if (rearm)
  590. val |= 1 << DB_EQ_REARM_SHIFT;
  591. if (clr_interrupt)
  592. val |= 1 << DB_EQ_CLR_SHIFT;
  593. if (event)
  594. val |= 1 << DB_EQ_EVNT_SHIFT;
  595. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  596. /* Setting lower order EQ_ID Bits */
  597. val |= (id & DB_EQ_RING_ID_LOW_MASK);
  598. /* Setting Higher order EQ_ID Bits */
  599. val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
  600. DB_EQ_RING_ID_HIGH_MASK)
  601. << DB_EQ_HIGH_SET_SHIFT);
  602. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  603. }
  604. /**
  605. * be_isr_mcc - The isr routine of the driver.
  606. * @irq: Not used
  607. * @dev_id: Pointer to host adapter structure
  608. */
  609. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  610. {
  611. struct beiscsi_hba *phba;
  612. struct be_eq_entry *eqe;
  613. struct be_queue_info *eq;
  614. struct be_queue_info *mcc;
  615. unsigned int mcc_events;
  616. struct be_eq_obj *pbe_eq;
  617. pbe_eq = dev_id;
  618. eq = &pbe_eq->q;
  619. phba = pbe_eq->phba;
  620. mcc = &phba->ctrl.mcc_obj.cq;
  621. eqe = queue_tail_node(eq);
  622. mcc_events = 0;
  623. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  624. & EQE_VALID_MASK) {
  625. if (((eqe->dw[offsetof(struct amap_eq_entry,
  626. resource_id) / 32] &
  627. EQE_RESID_MASK) >> 16) == mcc->id) {
  628. mcc_events++;
  629. }
  630. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  631. queue_tail_inc(eq);
  632. eqe = queue_tail_node(eq);
  633. }
  634. if (mcc_events) {
  635. queue_work(phba->wq, &pbe_eq->mcc_work);
  636. hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
  637. }
  638. return IRQ_HANDLED;
  639. }
  640. /**
  641. * be_isr_msix - The isr routine of the driver.
  642. * @irq: Not used
  643. * @dev_id: Pointer to host adapter structure
  644. */
  645. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  646. {
  647. struct beiscsi_hba *phba;
  648. struct be_queue_info *eq;
  649. struct be_eq_obj *pbe_eq;
  650. pbe_eq = dev_id;
  651. eq = &pbe_eq->q;
  652. phba = pbe_eq->phba;
  653. /* disable interrupt till iopoll completes */
  654. hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
  655. irq_poll_sched(&pbe_eq->iopoll);
  656. return IRQ_HANDLED;
  657. }
  658. /**
  659. * be_isr - The isr routine of the driver.
  660. * @irq: Not used
  661. * @dev_id: Pointer to host adapter structure
  662. */
  663. static irqreturn_t be_isr(int irq, void *dev_id)
  664. {
  665. struct beiscsi_hba *phba;
  666. struct hwi_controller *phwi_ctrlr;
  667. struct hwi_context_memory *phwi_context;
  668. struct be_eq_entry *eqe;
  669. struct be_queue_info *eq;
  670. struct be_queue_info *mcc;
  671. unsigned int mcc_events, io_events;
  672. struct be_ctrl_info *ctrl;
  673. struct be_eq_obj *pbe_eq;
  674. int isr, rearm;
  675. phba = dev_id;
  676. ctrl = &phba->ctrl;
  677. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  678. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  679. if (!isr)
  680. return IRQ_NONE;
  681. phwi_ctrlr = phba->phwi_ctrlr;
  682. phwi_context = phwi_ctrlr->phwi_ctxt;
  683. pbe_eq = &phwi_context->be_eq[0];
  684. eq = &phwi_context->be_eq[0].q;
  685. mcc = &phba->ctrl.mcc_obj.cq;
  686. eqe = queue_tail_node(eq);
  687. io_events = 0;
  688. mcc_events = 0;
  689. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  690. & EQE_VALID_MASK) {
  691. if (((eqe->dw[offsetof(struct amap_eq_entry,
  692. resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
  693. mcc_events++;
  694. else
  695. io_events++;
  696. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  697. queue_tail_inc(eq);
  698. eqe = queue_tail_node(eq);
  699. }
  700. if (!io_events && !mcc_events)
  701. return IRQ_NONE;
  702. /* no need to rearm if interrupt is only for IOs */
  703. rearm = 0;
  704. if (mcc_events) {
  705. queue_work(phba->wq, &pbe_eq->mcc_work);
  706. /* rearm for MCCQ */
  707. rearm = 1;
  708. }
  709. if (io_events)
  710. irq_poll_sched(&pbe_eq->iopoll);
  711. hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
  712. return IRQ_HANDLED;
  713. }
  714. static void beiscsi_free_irqs(struct beiscsi_hba *phba)
  715. {
  716. struct hwi_context_memory *phwi_context;
  717. int i;
  718. if (!phba->pcidev->msix_enabled) {
  719. if (phba->pcidev->irq)
  720. free_irq(phba->pcidev->irq, phba);
  721. return;
  722. }
  723. phwi_context = phba->phwi_ctrlr->phwi_ctxt;
  724. for (i = 0; i <= phba->num_cpus; i++) {
  725. free_irq(pci_irq_vector(phba->pcidev, i),
  726. &phwi_context->be_eq[i]);
  727. kfree(phba->msi_name[i]);
  728. }
  729. }
  730. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  731. {
  732. struct pci_dev *pcidev = phba->pcidev;
  733. struct hwi_controller *phwi_ctrlr;
  734. struct hwi_context_memory *phwi_context;
  735. int ret, i, j;
  736. phwi_ctrlr = phba->phwi_ctrlr;
  737. phwi_context = phwi_ctrlr->phwi_ctxt;
  738. if (pcidev->msix_enabled) {
  739. for (i = 0; i < phba->num_cpus; i++) {
  740. phba->msi_name[i] = kasprintf(GFP_KERNEL,
  741. "beiscsi_%02x_%02x",
  742. phba->shost->host_no, i);
  743. if (!phba->msi_name[i]) {
  744. ret = -ENOMEM;
  745. goto free_msix_irqs;
  746. }
  747. ret = request_irq(pci_irq_vector(pcidev, i),
  748. be_isr_msix, 0, phba->msi_name[i],
  749. &phwi_context->be_eq[i]);
  750. if (ret) {
  751. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  752. "BM_%d : beiscsi_init_irqs-Failed to"
  753. "register msix for i = %d\n",
  754. i);
  755. kfree(phba->msi_name[i]);
  756. goto free_msix_irqs;
  757. }
  758. }
  759. phba->msi_name[i] = kasprintf(GFP_KERNEL, "beiscsi_mcc_%02x",
  760. phba->shost->host_no);
  761. if (!phba->msi_name[i]) {
  762. ret = -ENOMEM;
  763. goto free_msix_irqs;
  764. }
  765. ret = request_irq(pci_irq_vector(pcidev, i), be_isr_mcc, 0,
  766. phba->msi_name[i], &phwi_context->be_eq[i]);
  767. if (ret) {
  768. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
  769. "BM_%d : beiscsi_init_irqs-"
  770. "Failed to register beiscsi_msix_mcc\n");
  771. kfree(phba->msi_name[i]);
  772. goto free_msix_irqs;
  773. }
  774. } else {
  775. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  776. "beiscsi", phba);
  777. if (ret) {
  778. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  779. "BM_%d : beiscsi_init_irqs-"
  780. "Failed to register irq\\n");
  781. return ret;
  782. }
  783. }
  784. return 0;
  785. free_msix_irqs:
  786. for (j = i - 1; j >= 0; j--) {
  787. free_irq(pci_irq_vector(pcidev, i), &phwi_context->be_eq[j]);
  788. kfree(phba->msi_name[j]);
  789. }
  790. return ret;
  791. }
  792. void hwi_ring_cq_db(struct beiscsi_hba *phba,
  793. unsigned int id, unsigned int num_processed,
  794. unsigned char rearm)
  795. {
  796. u32 val = 0;
  797. if (rearm)
  798. val |= 1 << DB_CQ_REARM_SHIFT;
  799. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  800. /* Setting lower order CQ_ID Bits */
  801. val |= (id & DB_CQ_RING_ID_LOW_MASK);
  802. /* Setting Higher order CQ_ID Bits */
  803. val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
  804. DB_CQ_RING_ID_HIGH_MASK)
  805. << DB_CQ_HIGH_SET_SHIFT);
  806. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  807. }
  808. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  809. {
  810. struct sgl_handle *psgl_handle;
  811. unsigned long flags;
  812. spin_lock_irqsave(&phba->io_sgl_lock, flags);
  813. if (phba->io_sgl_hndl_avbl) {
  814. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  815. "BM_%d : In alloc_io_sgl_handle,"
  816. " io_sgl_alloc_index=%d\n",
  817. phba->io_sgl_alloc_index);
  818. psgl_handle = phba->io_sgl_hndl_base[phba->
  819. io_sgl_alloc_index];
  820. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  821. phba->io_sgl_hndl_avbl--;
  822. if (phba->io_sgl_alloc_index == (phba->params.
  823. ios_per_ctrl - 1))
  824. phba->io_sgl_alloc_index = 0;
  825. else
  826. phba->io_sgl_alloc_index++;
  827. } else
  828. psgl_handle = NULL;
  829. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  830. return psgl_handle;
  831. }
  832. static void
  833. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  834. {
  835. unsigned long flags;
  836. spin_lock_irqsave(&phba->io_sgl_lock, flags);
  837. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  838. "BM_%d : In free_,io_sgl_free_index=%d\n",
  839. phba->io_sgl_free_index);
  840. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  841. /*
  842. * this can happen if clean_task is called on a task that
  843. * failed in xmit_task or alloc_pdu.
  844. */
  845. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  846. "BM_%d : Double Free in IO SGL io_sgl_free_index=%d, value there=%p\n",
  847. phba->io_sgl_free_index,
  848. phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
  849. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  850. return;
  851. }
  852. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  853. phba->io_sgl_hndl_avbl++;
  854. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  855. phba->io_sgl_free_index = 0;
  856. else
  857. phba->io_sgl_free_index++;
  858. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  859. }
  860. static inline struct wrb_handle *
  861. beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
  862. unsigned int wrbs_per_cxn)
  863. {
  864. struct wrb_handle *pwrb_handle;
  865. unsigned long flags;
  866. spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
  867. if (!pwrb_context->wrb_handles_available) {
  868. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  869. return NULL;
  870. }
  871. pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
  872. pwrb_context->wrb_handles_available--;
  873. if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
  874. pwrb_context->alloc_index = 0;
  875. else
  876. pwrb_context->alloc_index++;
  877. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  878. if (pwrb_handle)
  879. memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
  880. return pwrb_handle;
  881. }
  882. /**
  883. * alloc_wrb_handle - To allocate a wrb handle
  884. * @phba: The hba pointer
  885. * @cid: The cid to use for allocation
  886. * @pwrb_context: ptr to ptr to wrb context
  887. *
  888. * This happens under session_lock until submission to chip
  889. */
  890. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
  891. struct hwi_wrb_context **pcontext)
  892. {
  893. struct hwi_wrb_context *pwrb_context;
  894. struct hwi_controller *phwi_ctrlr;
  895. uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
  896. phwi_ctrlr = phba->phwi_ctrlr;
  897. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  898. /* return the context address */
  899. *pcontext = pwrb_context;
  900. return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
  901. }
  902. static inline void
  903. beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
  904. struct wrb_handle *pwrb_handle,
  905. unsigned int wrbs_per_cxn)
  906. {
  907. unsigned long flags;
  908. spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
  909. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  910. pwrb_context->wrb_handles_available++;
  911. if (pwrb_context->free_index == (wrbs_per_cxn - 1))
  912. pwrb_context->free_index = 0;
  913. else
  914. pwrb_context->free_index++;
  915. pwrb_handle->pio_handle = NULL;
  916. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  917. }
  918. /**
  919. * free_wrb_handle - To free the wrb handle back to pool
  920. * @phba: The hba pointer
  921. * @pwrb_context: The context to free from
  922. * @pwrb_handle: The wrb_handle to free
  923. *
  924. * This happens under session_lock until submission to chip
  925. */
  926. static void
  927. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  928. struct wrb_handle *pwrb_handle)
  929. {
  930. beiscsi_put_wrb_handle(pwrb_context,
  931. pwrb_handle,
  932. phba->params.wrbs_per_cxn);
  933. beiscsi_log(phba, KERN_INFO,
  934. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  935. "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
  936. "wrb_handles_available=%d\n",
  937. pwrb_handle, pwrb_context->free_index,
  938. pwrb_context->wrb_handles_available);
  939. }
  940. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  941. {
  942. struct sgl_handle *psgl_handle;
  943. unsigned long flags;
  944. spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
  945. if (phba->eh_sgl_hndl_avbl) {
  946. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  947. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  948. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  949. "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
  950. phba->eh_sgl_alloc_index,
  951. phba->eh_sgl_alloc_index);
  952. phba->eh_sgl_hndl_avbl--;
  953. if (phba->eh_sgl_alloc_index ==
  954. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  955. 1))
  956. phba->eh_sgl_alloc_index = 0;
  957. else
  958. phba->eh_sgl_alloc_index++;
  959. } else
  960. psgl_handle = NULL;
  961. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  962. return psgl_handle;
  963. }
  964. void
  965. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  966. {
  967. unsigned long flags;
  968. spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
  969. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  970. "BM_%d : In free_mgmt_sgl_handle,"
  971. "eh_sgl_free_index=%d\n",
  972. phba->eh_sgl_free_index);
  973. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  974. /*
  975. * this can happen if clean_task is called on a task that
  976. * failed in xmit_task or alloc_pdu.
  977. */
  978. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
  979. "BM_%d : Double Free in eh SGL ,"
  980. "eh_sgl_free_index=%d\n",
  981. phba->eh_sgl_free_index);
  982. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  983. return;
  984. }
  985. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  986. phba->eh_sgl_hndl_avbl++;
  987. if (phba->eh_sgl_free_index ==
  988. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  989. phba->eh_sgl_free_index = 0;
  990. else
  991. phba->eh_sgl_free_index++;
  992. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  993. }
  994. static void
  995. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  996. struct iscsi_task *task,
  997. struct common_sol_cqe *csol_cqe)
  998. {
  999. struct beiscsi_io_task *io_task = task->dd_data;
  1000. struct be_status_bhs *sts_bhs =
  1001. (struct be_status_bhs *)io_task->cmd_bhs;
  1002. struct iscsi_conn *conn = beiscsi_conn->conn;
  1003. unsigned char *sense;
  1004. u32 resid = 0, exp_cmdsn, max_cmdsn;
  1005. u8 rsp, status, flags;
  1006. exp_cmdsn = csol_cqe->exp_cmdsn;
  1007. max_cmdsn = (csol_cqe->exp_cmdsn +
  1008. csol_cqe->cmd_wnd - 1);
  1009. rsp = csol_cqe->i_resp;
  1010. status = csol_cqe->i_sts;
  1011. flags = csol_cqe->i_flags;
  1012. resid = csol_cqe->res_cnt;
  1013. if (!task->sc) {
  1014. if (io_task->scsi_cmnd) {
  1015. scsi_dma_unmap(io_task->scsi_cmnd);
  1016. io_task->scsi_cmnd = NULL;
  1017. }
  1018. return;
  1019. }
  1020. task->sc->result = (DID_OK << 16) | status;
  1021. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  1022. task->sc->result = DID_ERROR << 16;
  1023. goto unmap;
  1024. }
  1025. /* bidi not initially supported */
  1026. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  1027. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  1028. task->sc->result = DID_ERROR << 16;
  1029. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  1030. scsi_set_resid(task->sc, resid);
  1031. if (!status && (scsi_bufflen(task->sc) - resid <
  1032. task->sc->underflow))
  1033. task->sc->result = DID_ERROR << 16;
  1034. }
  1035. }
  1036. if (status == SAM_STAT_CHECK_CONDITION) {
  1037. u16 sense_len;
  1038. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  1039. sense = sts_bhs->sense_info + sizeof(unsigned short);
  1040. sense_len = be16_to_cpu(*slen);
  1041. memcpy(task->sc->sense_buffer, sense,
  1042. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  1043. }
  1044. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
  1045. conn->rxdata_octets += resid;
  1046. unmap:
  1047. if (io_task->scsi_cmnd) {
  1048. scsi_dma_unmap(io_task->scsi_cmnd);
  1049. io_task->scsi_cmnd = NULL;
  1050. }
  1051. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  1052. }
  1053. static void
  1054. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  1055. struct iscsi_task *task,
  1056. struct common_sol_cqe *csol_cqe)
  1057. {
  1058. struct iscsi_logout_rsp *hdr;
  1059. struct beiscsi_io_task *io_task = task->dd_data;
  1060. struct iscsi_conn *conn = beiscsi_conn->conn;
  1061. hdr = (struct iscsi_logout_rsp *)task->hdr;
  1062. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  1063. hdr->t2wait = 5;
  1064. hdr->t2retain = 0;
  1065. hdr->flags = csol_cqe->i_flags;
  1066. hdr->response = csol_cqe->i_resp;
  1067. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1068. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1069. csol_cqe->cmd_wnd - 1);
  1070. hdr->dlength[0] = 0;
  1071. hdr->dlength[1] = 0;
  1072. hdr->dlength[2] = 0;
  1073. hdr->hlength = 0;
  1074. hdr->itt = io_task->libiscsi_itt;
  1075. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1076. }
  1077. static void
  1078. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  1079. struct iscsi_task *task,
  1080. struct common_sol_cqe *csol_cqe)
  1081. {
  1082. struct iscsi_tm_rsp *hdr;
  1083. struct iscsi_conn *conn = beiscsi_conn->conn;
  1084. struct beiscsi_io_task *io_task = task->dd_data;
  1085. hdr = (struct iscsi_tm_rsp *)task->hdr;
  1086. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  1087. hdr->flags = csol_cqe->i_flags;
  1088. hdr->response = csol_cqe->i_resp;
  1089. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1090. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1091. csol_cqe->cmd_wnd - 1);
  1092. hdr->itt = io_task->libiscsi_itt;
  1093. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1094. }
  1095. static void
  1096. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  1097. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1098. {
  1099. struct hwi_wrb_context *pwrb_context;
  1100. uint16_t wrb_index, cid, cri_index;
  1101. struct hwi_controller *phwi_ctrlr;
  1102. struct wrb_handle *pwrb_handle;
  1103. struct iscsi_session *session;
  1104. struct iscsi_task *task;
  1105. phwi_ctrlr = phba->phwi_ctrlr;
  1106. if (is_chip_be2_be3r(phba)) {
  1107. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1108. wrb_idx, psol);
  1109. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1110. cid, psol);
  1111. } else {
  1112. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1113. wrb_idx, psol);
  1114. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1115. cid, psol);
  1116. }
  1117. cri_index = BE_GET_CRI_FROM_CID(cid);
  1118. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1119. pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
  1120. session = beiscsi_conn->conn->session;
  1121. spin_lock_bh(&session->back_lock);
  1122. task = pwrb_handle->pio_handle;
  1123. if (task)
  1124. __iscsi_put_task(task);
  1125. spin_unlock_bh(&session->back_lock);
  1126. }
  1127. static void
  1128. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  1129. struct iscsi_task *task,
  1130. struct common_sol_cqe *csol_cqe)
  1131. {
  1132. struct iscsi_nopin *hdr;
  1133. struct iscsi_conn *conn = beiscsi_conn->conn;
  1134. struct beiscsi_io_task *io_task = task->dd_data;
  1135. hdr = (struct iscsi_nopin *)task->hdr;
  1136. hdr->flags = csol_cqe->i_flags;
  1137. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1138. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1139. csol_cqe->cmd_wnd - 1);
  1140. hdr->opcode = ISCSI_OP_NOOP_IN;
  1141. hdr->itt = io_task->libiscsi_itt;
  1142. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1143. }
  1144. static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
  1145. struct sol_cqe *psol,
  1146. struct common_sol_cqe *csol_cqe)
  1147. {
  1148. if (is_chip_be2_be3r(phba)) {
  1149. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
  1150. i_exp_cmd_sn, psol);
  1151. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
  1152. i_res_cnt, psol);
  1153. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
  1154. i_cmd_wnd, psol);
  1155. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
  1156. wrb_index, psol);
  1157. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
  1158. cid, psol);
  1159. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1160. hw_sts, psol);
  1161. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
  1162. i_resp, psol);
  1163. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1164. i_sts, psol);
  1165. csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
  1166. i_flags, psol);
  1167. } else {
  1168. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1169. i_exp_cmd_sn, psol);
  1170. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1171. i_res_cnt, psol);
  1172. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1173. wrb_index, psol);
  1174. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1175. cid, psol);
  1176. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1177. hw_sts, psol);
  1178. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1179. i_cmd_wnd, psol);
  1180. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1181. cmd_cmpl, psol))
  1182. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1183. i_sts, psol);
  1184. else
  1185. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1186. i_sts, psol);
  1187. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1188. u, psol))
  1189. csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
  1190. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1191. o, psol))
  1192. csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
  1193. }
  1194. }
  1195. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  1196. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1197. {
  1198. struct iscsi_conn *conn = beiscsi_conn->conn;
  1199. struct iscsi_session *session = conn->session;
  1200. struct common_sol_cqe csol_cqe = {0};
  1201. struct hwi_wrb_context *pwrb_context;
  1202. struct hwi_controller *phwi_ctrlr;
  1203. struct wrb_handle *pwrb_handle;
  1204. struct iscsi_task *task;
  1205. uint16_t cri_index = 0;
  1206. uint8_t type;
  1207. phwi_ctrlr = phba->phwi_ctrlr;
  1208. /* Copy the elements to a common structure */
  1209. adapter_get_sol_cqe(phba, psol, &csol_cqe);
  1210. cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
  1211. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1212. pwrb_handle = pwrb_context->pwrb_handle_basestd[
  1213. csol_cqe.wrb_index];
  1214. spin_lock_bh(&session->back_lock);
  1215. task = pwrb_handle->pio_handle;
  1216. if (!task) {
  1217. spin_unlock_bh(&session->back_lock);
  1218. return;
  1219. }
  1220. type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
  1221. switch (type) {
  1222. case HWH_TYPE_IO:
  1223. case HWH_TYPE_IO_RD:
  1224. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1225. ISCSI_OP_NOOP_OUT)
  1226. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1227. else
  1228. be_complete_io(beiscsi_conn, task, &csol_cqe);
  1229. break;
  1230. case HWH_TYPE_LOGOUT:
  1231. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1232. be_complete_logout(beiscsi_conn, task, &csol_cqe);
  1233. else
  1234. be_complete_tmf(beiscsi_conn, task, &csol_cqe);
  1235. break;
  1236. case HWH_TYPE_LOGIN:
  1237. beiscsi_log(phba, KERN_ERR,
  1238. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1239. "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
  1240. " hwi_complete_cmd- Solicited path\n");
  1241. break;
  1242. case HWH_TYPE_NOP:
  1243. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1244. break;
  1245. default:
  1246. beiscsi_log(phba, KERN_WARNING,
  1247. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1248. "BM_%d : In hwi_complete_cmd, unknown type = %d"
  1249. "wrb_index 0x%x CID 0x%x\n", type,
  1250. csol_cqe.wrb_index,
  1251. csol_cqe.cid);
  1252. break;
  1253. }
  1254. spin_unlock_bh(&session->back_lock);
  1255. }
  1256. /**
  1257. * ASYNC PDUs include
  1258. * a. Unsolicited NOP-In (target initiated NOP-In)
  1259. * b. ASYNC Messages
  1260. * c. Reject PDU
  1261. * d. Login response
  1262. * These headers arrive unprocessed by the EP firmware.
  1263. * iSCSI layer processes them.
  1264. */
  1265. static unsigned int
  1266. beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
  1267. struct pdu_base *phdr, void *pdata, unsigned int dlen)
  1268. {
  1269. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1270. struct iscsi_conn *conn = beiscsi_conn->conn;
  1271. struct beiscsi_io_task *io_task;
  1272. struct iscsi_hdr *login_hdr;
  1273. struct iscsi_task *task;
  1274. u8 code;
  1275. code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
  1276. switch (code) {
  1277. case ISCSI_OP_NOOP_IN:
  1278. pdata = NULL;
  1279. dlen = 0;
  1280. break;
  1281. case ISCSI_OP_ASYNC_EVENT:
  1282. break;
  1283. case ISCSI_OP_REJECT:
  1284. WARN_ON(!pdata);
  1285. WARN_ON(!(dlen == 48));
  1286. beiscsi_log(phba, KERN_ERR,
  1287. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1288. "BM_%d : In ISCSI_OP_REJECT\n");
  1289. break;
  1290. case ISCSI_OP_LOGIN_RSP:
  1291. case ISCSI_OP_TEXT_RSP:
  1292. task = conn->login_task;
  1293. io_task = task->dd_data;
  1294. login_hdr = (struct iscsi_hdr *)phdr;
  1295. login_hdr->itt = io_task->libiscsi_itt;
  1296. break;
  1297. default:
  1298. beiscsi_log(phba, KERN_WARNING,
  1299. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1300. "BM_%d : unrecognized async PDU opcode 0x%x\n",
  1301. code);
  1302. return 1;
  1303. }
  1304. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
  1305. return 0;
  1306. }
  1307. static inline void
  1308. beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
  1309. struct hd_async_handle *pasync_handle)
  1310. {
  1311. pasync_handle->is_final = 0;
  1312. pasync_handle->buffer_len = 0;
  1313. pasync_handle->in_use = 0;
  1314. list_del_init(&pasync_handle->link);
  1315. }
  1316. static void
  1317. beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
  1318. struct hd_async_context *pasync_ctx,
  1319. u16 cri)
  1320. {
  1321. struct hd_async_handle *pasync_handle, *tmp_handle;
  1322. struct list_head *plist;
  1323. plist = &pasync_ctx->async_entry[cri].wq.list;
  1324. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link)
  1325. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1326. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
  1327. pasync_ctx->async_entry[cri].wq.hdr_len = 0;
  1328. pasync_ctx->async_entry[cri].wq.bytes_received = 0;
  1329. pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
  1330. }
  1331. static struct hd_async_handle *
  1332. beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
  1333. struct hd_async_context *pasync_ctx,
  1334. struct i_t_dpdu_cqe *pdpdu_cqe,
  1335. u8 *header)
  1336. {
  1337. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1338. struct hd_async_handle *pasync_handle;
  1339. struct be_bus_address phys_addr;
  1340. u16 cid, code, ci, cri;
  1341. u8 final, error = 0;
  1342. u32 dpl;
  1343. cid = beiscsi_conn->beiscsi_conn_cid;
  1344. cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
  1345. /**
  1346. * This function is invoked to get the right async_handle structure
  1347. * from a given DEF PDU CQ entry.
  1348. *
  1349. * - index in CQ entry gives the vertical index
  1350. * - address in CQ entry is the offset where the DMA last ended
  1351. * - final - no more notifications for this PDU
  1352. */
  1353. if (is_chip_be2_be3r(phba)) {
  1354. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1355. dpl, pdpdu_cqe);
  1356. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1357. index, pdpdu_cqe);
  1358. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1359. final, pdpdu_cqe);
  1360. } else {
  1361. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1362. dpl, pdpdu_cqe);
  1363. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1364. index, pdpdu_cqe);
  1365. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1366. final, pdpdu_cqe);
  1367. }
  1368. /**
  1369. * DB addr Hi/Lo is same for BE and SKH.
  1370. * Subtract the dataplacementlength to get to the base.
  1371. */
  1372. phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1373. db_addr_lo, pdpdu_cqe);
  1374. phys_addr.u.a32.address_lo -= dpl;
  1375. phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1376. db_addr_hi, pdpdu_cqe);
  1377. code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
  1378. switch (code) {
  1379. case UNSOL_HDR_NOTIFY:
  1380. pasync_handle = pasync_ctx->async_entry[ci].header;
  1381. *header = 1;
  1382. break;
  1383. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1384. error = 1;
  1385. case UNSOL_DATA_NOTIFY:
  1386. pasync_handle = pasync_ctx->async_entry[ci].data;
  1387. break;
  1388. /* called only for above codes */
  1389. default:
  1390. return NULL;
  1391. }
  1392. if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
  1393. pasync_handle->index != ci) {
  1394. /* driver bug - if ci does not match async handle index */
  1395. error = 1;
  1396. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1397. "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
  1398. cid, pasync_handle->is_header ? 'H' : 'D',
  1399. pasync_handle->pa.u.a64.address,
  1400. pasync_handle->index,
  1401. phys_addr.u.a64.address, ci);
  1402. /* FW has stale address - attempt continuing by dropping */
  1403. }
  1404. /**
  1405. * DEF PDU header and data buffers with errors should be simply
  1406. * dropped as there are no consumers for it.
  1407. */
  1408. if (error) {
  1409. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1410. return NULL;
  1411. }
  1412. if (pasync_handle->in_use || !list_empty(&pasync_handle->link)) {
  1413. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1414. "BM_%d : cid %d async PDU handle in use - code %d ci %d addr %llx\n",
  1415. cid, code, ci, phys_addr.u.a64.address);
  1416. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1417. }
  1418. list_del_init(&pasync_handle->link);
  1419. /**
  1420. * Each CID is associated with unique CRI.
  1421. * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
  1422. **/
  1423. pasync_handle->cri = cri;
  1424. pasync_handle->is_final = final;
  1425. pasync_handle->buffer_len = dpl;
  1426. pasync_handle->in_use = 1;
  1427. return pasync_handle;
  1428. }
  1429. static unsigned int
  1430. beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
  1431. struct hd_async_context *pasync_ctx,
  1432. u16 cri)
  1433. {
  1434. struct iscsi_session *session = beiscsi_conn->conn->session;
  1435. struct hd_async_handle *pasync_handle, *plast_handle;
  1436. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1437. void *phdr = NULL, *pdata = NULL;
  1438. u32 dlen = 0, status = 0;
  1439. struct list_head *plist;
  1440. plist = &pasync_ctx->async_entry[cri].wq.list;
  1441. plast_handle = NULL;
  1442. list_for_each_entry(pasync_handle, plist, link) {
  1443. plast_handle = pasync_handle;
  1444. /* get the header, the first entry */
  1445. if (!phdr) {
  1446. phdr = pasync_handle->pbuffer;
  1447. continue;
  1448. }
  1449. /* use first buffer to collect all the data */
  1450. if (!pdata) {
  1451. pdata = pasync_handle->pbuffer;
  1452. dlen = pasync_handle->buffer_len;
  1453. continue;
  1454. }
  1455. if (!pasync_handle->buffer_len ||
  1456. (dlen + pasync_handle->buffer_len) >
  1457. pasync_ctx->async_data.buffer_size)
  1458. break;
  1459. memcpy(pdata + dlen, pasync_handle->pbuffer,
  1460. pasync_handle->buffer_len);
  1461. dlen += pasync_handle->buffer_len;
  1462. }
  1463. if (!plast_handle->is_final) {
  1464. /* last handle should have final PDU notification from FW */
  1465. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1466. "BM_%d : cid %u %p fwd async PDU opcode %x with last handle missing - HL%u:DN%u:DR%u\n",
  1467. beiscsi_conn->beiscsi_conn_cid, plast_handle,
  1468. AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr),
  1469. pasync_ctx->async_entry[cri].wq.hdr_len,
  1470. pasync_ctx->async_entry[cri].wq.bytes_needed,
  1471. pasync_ctx->async_entry[cri].wq.bytes_received);
  1472. }
  1473. spin_lock_bh(&session->back_lock);
  1474. status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
  1475. spin_unlock_bh(&session->back_lock);
  1476. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1477. return status;
  1478. }
  1479. static unsigned int
  1480. beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
  1481. struct hd_async_context *pasync_ctx,
  1482. struct hd_async_handle *pasync_handle)
  1483. {
  1484. unsigned int bytes_needed = 0, status = 0;
  1485. u16 cri = pasync_handle->cri;
  1486. struct cri_wait_queue *wq;
  1487. struct beiscsi_hba *phba;
  1488. struct pdu_base *ppdu;
  1489. char *err = "";
  1490. phba = beiscsi_conn->phba;
  1491. wq = &pasync_ctx->async_entry[cri].wq;
  1492. if (pasync_handle->is_header) {
  1493. /* check if PDU hdr is rcv'd when old hdr not completed */
  1494. if (wq->hdr_len) {
  1495. err = "incomplete";
  1496. goto drop_pdu;
  1497. }
  1498. ppdu = pasync_handle->pbuffer;
  1499. bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
  1500. data_len_hi, ppdu);
  1501. bytes_needed <<= 16;
  1502. bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
  1503. data_len_lo, ppdu));
  1504. wq->hdr_len = pasync_handle->buffer_len;
  1505. wq->bytes_received = 0;
  1506. wq->bytes_needed = bytes_needed;
  1507. list_add_tail(&pasync_handle->link, &wq->list);
  1508. if (!bytes_needed)
  1509. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1510. pasync_ctx, cri);
  1511. } else {
  1512. /* check if data received has header and is needed */
  1513. if (!wq->hdr_len || !wq->bytes_needed) {
  1514. err = "header less";
  1515. goto drop_pdu;
  1516. }
  1517. wq->bytes_received += pasync_handle->buffer_len;
  1518. /* Something got overwritten? Better catch it here. */
  1519. if (wq->bytes_received > wq->bytes_needed) {
  1520. err = "overflow";
  1521. goto drop_pdu;
  1522. }
  1523. list_add_tail(&pasync_handle->link, &wq->list);
  1524. if (wq->bytes_received == wq->bytes_needed)
  1525. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1526. pasync_ctx, cri);
  1527. }
  1528. return status;
  1529. drop_pdu:
  1530. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1531. "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
  1532. beiscsi_conn->beiscsi_conn_cid, err,
  1533. pasync_handle->is_header ? 'H' : 'D',
  1534. wq->hdr_len, wq->bytes_needed,
  1535. pasync_handle->buffer_len);
  1536. /* discard this handle */
  1537. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1538. /* free all the other handles in cri_wait_queue */
  1539. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1540. /* try continuing */
  1541. return status;
  1542. }
  1543. static void
  1544. beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
  1545. u8 header, u8 ulp_num, u16 nbuf)
  1546. {
  1547. struct hd_async_handle *pasync_handle;
  1548. struct hd_async_context *pasync_ctx;
  1549. struct hwi_controller *phwi_ctrlr;
  1550. struct phys_addr *pasync_sge;
  1551. u32 ring_id, doorbell = 0;
  1552. u32 doorbell_offset;
  1553. u16 prod, pi;
  1554. phwi_ctrlr = phba->phwi_ctrlr;
  1555. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1556. if (header) {
  1557. pasync_sge = pasync_ctx->async_header.ring_base;
  1558. pi = pasync_ctx->async_header.pi;
  1559. ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
  1560. doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
  1561. doorbell_offset;
  1562. } else {
  1563. pasync_sge = pasync_ctx->async_data.ring_base;
  1564. pi = pasync_ctx->async_data.pi;
  1565. ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
  1566. doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
  1567. doorbell_offset;
  1568. }
  1569. for (prod = 0; prod < nbuf; prod++) {
  1570. if (header)
  1571. pasync_handle = pasync_ctx->async_entry[pi].header;
  1572. else
  1573. pasync_handle = pasync_ctx->async_entry[pi].data;
  1574. WARN_ON(pasync_handle->is_header != header);
  1575. WARN_ON(pasync_handle->index != pi);
  1576. /* setup the ring only once */
  1577. if (nbuf == pasync_ctx->num_entries) {
  1578. /* note hi is lo */
  1579. pasync_sge[pi].hi = pasync_handle->pa.u.a32.address_lo;
  1580. pasync_sge[pi].lo = pasync_handle->pa.u.a32.address_hi;
  1581. }
  1582. if (++pi == pasync_ctx->num_entries)
  1583. pi = 0;
  1584. }
  1585. if (header)
  1586. pasync_ctx->async_header.pi = pi;
  1587. else
  1588. pasync_ctx->async_data.pi = pi;
  1589. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1590. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1591. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1592. doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
  1593. iowrite32(doorbell, phba->db_va + doorbell_offset);
  1594. }
  1595. static void
  1596. beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
  1597. struct i_t_dpdu_cqe *pdpdu_cqe)
  1598. {
  1599. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1600. struct hd_async_handle *pasync_handle = NULL;
  1601. struct hd_async_context *pasync_ctx;
  1602. struct hwi_controller *phwi_ctrlr;
  1603. u8 ulp_num, consumed, header = 0;
  1604. u16 cid_cri;
  1605. phwi_ctrlr = phba->phwi_ctrlr;
  1606. cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
  1607. ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
  1608. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1609. pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
  1610. pdpdu_cqe, &header);
  1611. if (is_chip_be2_be3r(phba))
  1612. consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1613. num_cons, pdpdu_cqe);
  1614. else
  1615. consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1616. num_cons, pdpdu_cqe);
  1617. if (pasync_handle)
  1618. beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
  1619. /* num_cons indicates number of 8 RQEs consumed */
  1620. if (consumed)
  1621. beiscsi_hdq_post_handles(phba, header, ulp_num, 8 * consumed);
  1622. }
  1623. void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
  1624. {
  1625. struct be_queue_info *mcc_cq;
  1626. struct be_mcc_compl *mcc_compl;
  1627. unsigned int num_processed = 0;
  1628. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1629. mcc_compl = queue_tail_node(mcc_cq);
  1630. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1631. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1632. if (beiscsi_hba_in_error(phba))
  1633. return;
  1634. if (num_processed >= 32) {
  1635. hwi_ring_cq_db(phba, mcc_cq->id,
  1636. num_processed, 0);
  1637. num_processed = 0;
  1638. }
  1639. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1640. beiscsi_process_async_event(phba, mcc_compl);
  1641. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1642. beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
  1643. }
  1644. mcc_compl->flags = 0;
  1645. queue_tail_inc(mcc_cq);
  1646. mcc_compl = queue_tail_node(mcc_cq);
  1647. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1648. num_processed++;
  1649. }
  1650. if (num_processed > 0)
  1651. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
  1652. }
  1653. static void beiscsi_mcc_work(struct work_struct *work)
  1654. {
  1655. struct be_eq_obj *pbe_eq;
  1656. struct beiscsi_hba *phba;
  1657. pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
  1658. phba = pbe_eq->phba;
  1659. beiscsi_process_mcc_cq(phba);
  1660. /* rearm EQ for further interrupts */
  1661. if (!beiscsi_hba_in_error(phba))
  1662. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1663. }
  1664. /**
  1665. * beiscsi_process_cq()- Process the Completion Queue
  1666. * @pbe_eq: Event Q on which the Completion has come
  1667. * @budget: Max number of events to processed
  1668. *
  1669. * return
  1670. * Number of Completion Entries processed.
  1671. **/
  1672. unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
  1673. {
  1674. struct be_queue_info *cq;
  1675. struct sol_cqe *sol;
  1676. struct dmsg_cqe *dmsg;
  1677. unsigned int total = 0;
  1678. unsigned int num_processed = 0;
  1679. unsigned short code = 0, cid = 0;
  1680. uint16_t cri_index = 0;
  1681. struct beiscsi_conn *beiscsi_conn;
  1682. struct beiscsi_endpoint *beiscsi_ep;
  1683. struct iscsi_endpoint *ep;
  1684. struct beiscsi_hba *phba;
  1685. cq = pbe_eq->cq;
  1686. sol = queue_tail_node(cq);
  1687. phba = pbe_eq->phba;
  1688. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1689. CQE_VALID_MASK) {
  1690. if (beiscsi_hba_in_error(phba))
  1691. return 0;
  1692. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1693. code = (sol->dw[offsetof(struct amap_sol_cqe, code) / 32] &
  1694. CQE_CODE_MASK);
  1695. /* Get the CID */
  1696. if (is_chip_be2_be3r(phba)) {
  1697. cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
  1698. } else {
  1699. if ((code == DRIVERMSG_NOTIFY) ||
  1700. (code == UNSOL_HDR_NOTIFY) ||
  1701. (code == UNSOL_DATA_NOTIFY))
  1702. cid = AMAP_GET_BITS(
  1703. struct amap_i_t_dpdu_cqe_v2,
  1704. cid, sol);
  1705. else
  1706. cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1707. cid, sol);
  1708. }
  1709. cri_index = BE_GET_CRI_FROM_CID(cid);
  1710. ep = phba->ep_array[cri_index];
  1711. if (ep == NULL) {
  1712. /* connection has already been freed
  1713. * just move on to next one
  1714. */
  1715. beiscsi_log(phba, KERN_WARNING,
  1716. BEISCSI_LOG_INIT,
  1717. "BM_%d : proc cqe of disconn ep: cid %d\n",
  1718. cid);
  1719. goto proc_next_cqe;
  1720. }
  1721. beiscsi_ep = ep->dd_data;
  1722. beiscsi_conn = beiscsi_ep->conn;
  1723. /* replenish cq */
  1724. if (num_processed == 32) {
  1725. hwi_ring_cq_db(phba, cq->id, 32, 0);
  1726. num_processed = 0;
  1727. }
  1728. total++;
  1729. switch (code) {
  1730. case SOL_CMD_COMPLETE:
  1731. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1732. break;
  1733. case DRIVERMSG_NOTIFY:
  1734. beiscsi_log(phba, KERN_INFO,
  1735. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1736. "BM_%d : Received %s[%d] on CID : %d\n",
  1737. cqe_desc[code], code, cid);
  1738. dmsg = (struct dmsg_cqe *)sol;
  1739. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1740. break;
  1741. case UNSOL_HDR_NOTIFY:
  1742. beiscsi_log(phba, KERN_INFO,
  1743. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1744. "BM_%d : Received %s[%d] on CID : %d\n",
  1745. cqe_desc[code], code, cid);
  1746. spin_lock_bh(&phba->async_pdu_lock);
  1747. beiscsi_hdq_process_compl(beiscsi_conn,
  1748. (struct i_t_dpdu_cqe *)sol);
  1749. spin_unlock_bh(&phba->async_pdu_lock);
  1750. break;
  1751. case UNSOL_DATA_NOTIFY:
  1752. beiscsi_log(phba, KERN_INFO,
  1753. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1754. "BM_%d : Received %s[%d] on CID : %d\n",
  1755. cqe_desc[code], code, cid);
  1756. spin_lock_bh(&phba->async_pdu_lock);
  1757. beiscsi_hdq_process_compl(beiscsi_conn,
  1758. (struct i_t_dpdu_cqe *)sol);
  1759. spin_unlock_bh(&phba->async_pdu_lock);
  1760. break;
  1761. case CXN_INVALIDATE_INDEX_NOTIFY:
  1762. case CMD_INVALIDATED_NOTIFY:
  1763. case CXN_INVALIDATE_NOTIFY:
  1764. beiscsi_log(phba, KERN_ERR,
  1765. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1766. "BM_%d : Ignoring %s[%d] on CID : %d\n",
  1767. cqe_desc[code], code, cid);
  1768. break;
  1769. case CXN_KILLED_HDR_DIGEST_ERR:
  1770. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1771. beiscsi_log(phba, KERN_ERR,
  1772. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1773. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1774. cqe_desc[code], code, cid);
  1775. break;
  1776. case CMD_KILLED_INVALID_STATSN_RCVD:
  1777. case CMD_KILLED_INVALID_R2T_RCVD:
  1778. case CMD_CXN_KILLED_LUN_INVALID:
  1779. case CMD_CXN_KILLED_ICD_INVALID:
  1780. case CMD_CXN_KILLED_ITT_INVALID:
  1781. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1782. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1783. beiscsi_log(phba, KERN_ERR,
  1784. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1785. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1786. cqe_desc[code], code, cid);
  1787. break;
  1788. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1789. beiscsi_log(phba, KERN_ERR,
  1790. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1791. "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
  1792. cqe_desc[code], code, cid);
  1793. spin_lock_bh(&phba->async_pdu_lock);
  1794. /* driver consumes the entry and drops the contents */
  1795. beiscsi_hdq_process_compl(beiscsi_conn,
  1796. (struct i_t_dpdu_cqe *)sol);
  1797. spin_unlock_bh(&phba->async_pdu_lock);
  1798. break;
  1799. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1800. case CXN_KILLED_BURST_LEN_MISMATCH:
  1801. case CXN_KILLED_AHS_RCVD:
  1802. case CXN_KILLED_UNKNOWN_HDR:
  1803. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1804. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1805. case CXN_KILLED_TIMED_OUT:
  1806. case CXN_KILLED_FIN_RCVD:
  1807. case CXN_KILLED_RST_SENT:
  1808. case CXN_KILLED_RST_RCVD:
  1809. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1810. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1811. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1812. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1813. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1814. beiscsi_log(phba, KERN_ERR,
  1815. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1816. "BM_%d : Event %s[%d] received on CID : %d\n",
  1817. cqe_desc[code], code, cid);
  1818. if (beiscsi_conn)
  1819. iscsi_conn_failure(beiscsi_conn->conn,
  1820. ISCSI_ERR_CONN_FAILED);
  1821. break;
  1822. default:
  1823. beiscsi_log(phba, KERN_ERR,
  1824. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1825. "BM_%d : Invalid CQE Event Received Code : %d"
  1826. "CID 0x%x...\n",
  1827. code, cid);
  1828. break;
  1829. }
  1830. proc_next_cqe:
  1831. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1832. queue_tail_inc(cq);
  1833. sol = queue_tail_node(cq);
  1834. num_processed++;
  1835. if (total == budget)
  1836. break;
  1837. }
  1838. hwi_ring_cq_db(phba, cq->id, num_processed, 1);
  1839. return total;
  1840. }
  1841. static int be_iopoll(struct irq_poll *iop, int budget)
  1842. {
  1843. unsigned int ret, io_events;
  1844. struct beiscsi_hba *phba;
  1845. struct be_eq_obj *pbe_eq;
  1846. struct be_eq_entry *eqe = NULL;
  1847. struct be_queue_info *eq;
  1848. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1849. phba = pbe_eq->phba;
  1850. if (beiscsi_hba_in_error(phba)) {
  1851. irq_poll_complete(iop);
  1852. return 0;
  1853. }
  1854. io_events = 0;
  1855. eq = &pbe_eq->q;
  1856. eqe = queue_tail_node(eq);
  1857. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
  1858. EQE_VALID_MASK) {
  1859. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  1860. queue_tail_inc(eq);
  1861. eqe = queue_tail_node(eq);
  1862. io_events++;
  1863. }
  1864. hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
  1865. ret = beiscsi_process_cq(pbe_eq, budget);
  1866. pbe_eq->cq_count += ret;
  1867. if (ret < budget) {
  1868. irq_poll_complete(iop);
  1869. beiscsi_log(phba, KERN_INFO,
  1870. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1871. "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
  1872. pbe_eq->q.id, ret);
  1873. if (!beiscsi_hba_in_error(phba))
  1874. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1875. }
  1876. return ret;
  1877. }
  1878. static void
  1879. hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1880. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1881. {
  1882. struct iscsi_sge *psgl;
  1883. unsigned int sg_len, index;
  1884. unsigned int sge_len = 0;
  1885. unsigned long long addr;
  1886. struct scatterlist *l_sg;
  1887. unsigned int offset;
  1888. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
  1889. io_task->bhs_pa.u.a32.address_lo);
  1890. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
  1891. io_task->bhs_pa.u.a32.address_hi);
  1892. l_sg = sg;
  1893. for (index = 0; (index < num_sg) && (index < 2); index++,
  1894. sg = sg_next(sg)) {
  1895. if (index == 0) {
  1896. sg_len = sg_dma_len(sg);
  1897. addr = (u64) sg_dma_address(sg);
  1898. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1899. sge0_addr_lo, pwrb,
  1900. lower_32_bits(addr));
  1901. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1902. sge0_addr_hi, pwrb,
  1903. upper_32_bits(addr));
  1904. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1905. sge0_len, pwrb,
  1906. sg_len);
  1907. sge_len = sg_len;
  1908. } else {
  1909. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
  1910. pwrb, sge_len);
  1911. sg_len = sg_dma_len(sg);
  1912. addr = (u64) sg_dma_address(sg);
  1913. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1914. sge1_addr_lo, pwrb,
  1915. lower_32_bits(addr));
  1916. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1917. sge1_addr_hi, pwrb,
  1918. upper_32_bits(addr));
  1919. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1920. sge1_len, pwrb,
  1921. sg_len);
  1922. }
  1923. }
  1924. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1925. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1926. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1927. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1928. io_task->bhs_pa.u.a32.address_hi);
  1929. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1930. io_task->bhs_pa.u.a32.address_lo);
  1931. if (num_sg == 1) {
  1932. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1933. 1);
  1934. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1935. 0);
  1936. } else if (num_sg == 2) {
  1937. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1938. 0);
  1939. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1940. 1);
  1941. } else {
  1942. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1943. 0);
  1944. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1945. 0);
  1946. }
  1947. sg = l_sg;
  1948. psgl++;
  1949. psgl++;
  1950. offset = 0;
  1951. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1952. sg_len = sg_dma_len(sg);
  1953. addr = (u64) sg_dma_address(sg);
  1954. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1955. lower_32_bits(addr));
  1956. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1957. upper_32_bits(addr));
  1958. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1959. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1960. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1961. offset += sg_len;
  1962. }
  1963. psgl--;
  1964. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1965. }
  1966. static void
  1967. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1968. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1969. {
  1970. struct iscsi_sge *psgl;
  1971. unsigned int sg_len, index;
  1972. unsigned int sge_len = 0;
  1973. unsigned long long addr;
  1974. struct scatterlist *l_sg;
  1975. unsigned int offset;
  1976. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1977. io_task->bhs_pa.u.a32.address_lo);
  1978. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1979. io_task->bhs_pa.u.a32.address_hi);
  1980. l_sg = sg;
  1981. for (index = 0; (index < num_sg) && (index < 2); index++,
  1982. sg = sg_next(sg)) {
  1983. if (index == 0) {
  1984. sg_len = sg_dma_len(sg);
  1985. addr = (u64) sg_dma_address(sg);
  1986. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1987. ((u32)(addr & 0xFFFFFFFF)));
  1988. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1989. ((u32)(addr >> 32)));
  1990. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1991. sg_len);
  1992. sge_len = sg_len;
  1993. } else {
  1994. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1995. pwrb, sge_len);
  1996. sg_len = sg_dma_len(sg);
  1997. addr = (u64) sg_dma_address(sg);
  1998. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1999. ((u32)(addr & 0xFFFFFFFF)));
  2000. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  2001. ((u32)(addr >> 32)));
  2002. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  2003. sg_len);
  2004. }
  2005. }
  2006. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2007. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  2008. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  2009. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2010. io_task->bhs_pa.u.a32.address_hi);
  2011. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2012. io_task->bhs_pa.u.a32.address_lo);
  2013. if (num_sg == 1) {
  2014. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2015. 1);
  2016. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2017. 0);
  2018. } else if (num_sg == 2) {
  2019. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2020. 0);
  2021. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2022. 1);
  2023. } else {
  2024. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2025. 0);
  2026. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2027. 0);
  2028. }
  2029. sg = l_sg;
  2030. psgl++;
  2031. psgl++;
  2032. offset = 0;
  2033. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  2034. sg_len = sg_dma_len(sg);
  2035. addr = (u64) sg_dma_address(sg);
  2036. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2037. (addr & 0xFFFFFFFF));
  2038. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2039. (addr >> 32));
  2040. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  2041. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  2042. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2043. offset += sg_len;
  2044. }
  2045. psgl--;
  2046. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2047. }
  2048. /**
  2049. * hwi_write_buffer()- Populate the WRB with task info
  2050. * @pwrb: ptr to the WRB entry
  2051. * @task: iscsi task which is to be executed
  2052. **/
  2053. static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  2054. {
  2055. struct iscsi_sge *psgl;
  2056. struct beiscsi_io_task *io_task = task->dd_data;
  2057. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  2058. struct beiscsi_hba *phba = beiscsi_conn->phba;
  2059. uint8_t dsp_value = 0;
  2060. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  2061. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  2062. io_task->bhs_pa.u.a32.address_lo);
  2063. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  2064. io_task->bhs_pa.u.a32.address_hi);
  2065. if (task->data) {
  2066. /* Check for the data_count */
  2067. dsp_value = (task->data_count) ? 1 : 0;
  2068. if (is_chip_be2_be3r(phba))
  2069. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
  2070. pwrb, dsp_value);
  2071. else
  2072. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
  2073. pwrb, dsp_value);
  2074. /* Map addr only if there is data_count */
  2075. if (dsp_value) {
  2076. io_task->mtask_addr = pci_map_single(phba->pcidev,
  2077. task->data,
  2078. task->data_count,
  2079. PCI_DMA_TODEVICE);
  2080. if (pci_dma_mapping_error(phba->pcidev,
  2081. io_task->mtask_addr))
  2082. return -ENOMEM;
  2083. io_task->mtask_data_count = task->data_count;
  2084. } else
  2085. io_task->mtask_addr = 0;
  2086. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  2087. lower_32_bits(io_task->mtask_addr));
  2088. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  2089. upper_32_bits(io_task->mtask_addr));
  2090. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  2091. task->data_count);
  2092. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  2093. } else {
  2094. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  2095. io_task->mtask_addr = 0;
  2096. }
  2097. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2098. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  2099. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2100. io_task->bhs_pa.u.a32.address_hi);
  2101. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2102. io_task->bhs_pa.u.a32.address_lo);
  2103. if (task->data) {
  2104. psgl++;
  2105. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  2106. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  2107. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  2108. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  2109. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  2110. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2111. psgl++;
  2112. if (task->data) {
  2113. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2114. lower_32_bits(io_task->mtask_addr));
  2115. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2116. upper_32_bits(io_task->mtask_addr));
  2117. }
  2118. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  2119. }
  2120. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2121. return 0;
  2122. }
  2123. /**
  2124. * beiscsi_find_mem_req()- Find mem needed
  2125. * @phba: ptr to HBA struct
  2126. **/
  2127. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  2128. {
  2129. uint8_t mem_descr_index, ulp_num;
  2130. unsigned int num_async_pdu_buf_pages;
  2131. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  2132. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  2133. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  2134. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  2135. BE_ISCSI_PDU_HEADER_SIZE;
  2136. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  2137. sizeof(struct hwi_context_memory);
  2138. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  2139. * (phba->params.wrbs_per_cxn)
  2140. * phba->params.cxns_per_ctrl;
  2141. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  2142. (phba->params.wrbs_per_cxn);
  2143. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  2144. phba->params.cxns_per_ctrl);
  2145. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  2146. phba->params.icds_per_ctrl;
  2147. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  2148. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  2149. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2150. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2151. num_async_pdu_buf_sgl_pages =
  2152. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2153. phba, ulp_num) *
  2154. sizeof(struct phys_addr));
  2155. num_async_pdu_buf_pages =
  2156. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2157. phba, ulp_num) *
  2158. phba->params.defpdu_hdr_sz);
  2159. num_async_pdu_data_pages =
  2160. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2161. phba, ulp_num) *
  2162. phba->params.defpdu_data_sz);
  2163. num_async_pdu_data_sgl_pages =
  2164. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2165. phba, ulp_num) *
  2166. sizeof(struct phys_addr));
  2167. mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
  2168. (ulp_num * MEM_DESCR_OFFSET));
  2169. phba->mem_req[mem_descr_index] =
  2170. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2171. BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
  2172. mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2173. (ulp_num * MEM_DESCR_OFFSET));
  2174. phba->mem_req[mem_descr_index] =
  2175. num_async_pdu_buf_pages *
  2176. PAGE_SIZE;
  2177. mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2178. (ulp_num * MEM_DESCR_OFFSET));
  2179. phba->mem_req[mem_descr_index] =
  2180. num_async_pdu_data_pages *
  2181. PAGE_SIZE;
  2182. mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2183. (ulp_num * MEM_DESCR_OFFSET));
  2184. phba->mem_req[mem_descr_index] =
  2185. num_async_pdu_buf_sgl_pages *
  2186. PAGE_SIZE;
  2187. mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2188. (ulp_num * MEM_DESCR_OFFSET));
  2189. phba->mem_req[mem_descr_index] =
  2190. num_async_pdu_data_sgl_pages *
  2191. PAGE_SIZE;
  2192. mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2193. (ulp_num * MEM_DESCR_OFFSET));
  2194. phba->mem_req[mem_descr_index] =
  2195. BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2196. sizeof(struct hd_async_handle);
  2197. mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2198. (ulp_num * MEM_DESCR_OFFSET));
  2199. phba->mem_req[mem_descr_index] =
  2200. BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2201. sizeof(struct hd_async_handle);
  2202. mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2203. (ulp_num * MEM_DESCR_OFFSET));
  2204. phba->mem_req[mem_descr_index] =
  2205. sizeof(struct hd_async_context) +
  2206. (BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2207. sizeof(struct hd_async_entry));
  2208. }
  2209. }
  2210. }
  2211. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  2212. {
  2213. dma_addr_t bus_add;
  2214. struct hwi_controller *phwi_ctrlr;
  2215. struct be_mem_descriptor *mem_descr;
  2216. struct mem_array *mem_arr, *mem_arr_orig;
  2217. unsigned int i, j, alloc_size, curr_alloc_size;
  2218. phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  2219. if (!phba->phwi_ctrlr)
  2220. return -ENOMEM;
  2221. /* Allocate memory for wrb_context */
  2222. phwi_ctrlr = phba->phwi_ctrlr;
  2223. phwi_ctrlr->wrb_context = kcalloc(phba->params.cxns_per_ctrl,
  2224. sizeof(struct hwi_wrb_context),
  2225. GFP_KERNEL);
  2226. if (!phwi_ctrlr->wrb_context) {
  2227. kfree(phba->phwi_ctrlr);
  2228. return -ENOMEM;
  2229. }
  2230. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  2231. GFP_KERNEL);
  2232. if (!phba->init_mem) {
  2233. kfree(phwi_ctrlr->wrb_context);
  2234. kfree(phba->phwi_ctrlr);
  2235. return -ENOMEM;
  2236. }
  2237. mem_arr_orig = kmalloc_array(BEISCSI_MAX_FRAGS_INIT,
  2238. sizeof(*mem_arr_orig),
  2239. GFP_KERNEL);
  2240. if (!mem_arr_orig) {
  2241. kfree(phba->init_mem);
  2242. kfree(phwi_ctrlr->wrb_context);
  2243. kfree(phba->phwi_ctrlr);
  2244. return -ENOMEM;
  2245. }
  2246. mem_descr = phba->init_mem;
  2247. for (i = 0; i < SE_MEM_MAX; i++) {
  2248. if (!phba->mem_req[i]) {
  2249. mem_descr->mem_array = NULL;
  2250. mem_descr++;
  2251. continue;
  2252. }
  2253. j = 0;
  2254. mem_arr = mem_arr_orig;
  2255. alloc_size = phba->mem_req[i];
  2256. memset(mem_arr, 0, sizeof(struct mem_array) *
  2257. BEISCSI_MAX_FRAGS_INIT);
  2258. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  2259. do {
  2260. mem_arr->virtual_address = pci_alloc_consistent(
  2261. phba->pcidev,
  2262. curr_alloc_size,
  2263. &bus_add);
  2264. if (!mem_arr->virtual_address) {
  2265. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  2266. goto free_mem;
  2267. if (curr_alloc_size -
  2268. rounddown_pow_of_two(curr_alloc_size))
  2269. curr_alloc_size = rounddown_pow_of_two
  2270. (curr_alloc_size);
  2271. else
  2272. curr_alloc_size = curr_alloc_size / 2;
  2273. } else {
  2274. mem_arr->bus_address.u.
  2275. a64.address = (__u64) bus_add;
  2276. mem_arr->size = curr_alloc_size;
  2277. alloc_size -= curr_alloc_size;
  2278. curr_alloc_size = min(be_max_phys_size *
  2279. 1024, alloc_size);
  2280. j++;
  2281. mem_arr++;
  2282. }
  2283. } while (alloc_size);
  2284. mem_descr->num_elements = j;
  2285. mem_descr->size_in_bytes = phba->mem_req[i];
  2286. mem_descr->mem_array = kmalloc_array(j, sizeof(*mem_arr),
  2287. GFP_KERNEL);
  2288. if (!mem_descr->mem_array)
  2289. goto free_mem;
  2290. memcpy(mem_descr->mem_array, mem_arr_orig,
  2291. sizeof(struct mem_array) * j);
  2292. mem_descr++;
  2293. }
  2294. kfree(mem_arr_orig);
  2295. return 0;
  2296. free_mem:
  2297. mem_descr->num_elements = j;
  2298. while ((i) || (j)) {
  2299. for (j = mem_descr->num_elements; j > 0; j--) {
  2300. pci_free_consistent(phba->pcidev,
  2301. mem_descr->mem_array[j - 1].size,
  2302. mem_descr->mem_array[j - 1].
  2303. virtual_address,
  2304. (unsigned long)mem_descr->
  2305. mem_array[j - 1].
  2306. bus_address.u.a64.address);
  2307. }
  2308. if (i) {
  2309. i--;
  2310. kfree(mem_descr->mem_array);
  2311. mem_descr--;
  2312. }
  2313. }
  2314. kfree(mem_arr_orig);
  2315. kfree(phba->init_mem);
  2316. kfree(phba->phwi_ctrlr->wrb_context);
  2317. kfree(phba->phwi_ctrlr);
  2318. return -ENOMEM;
  2319. }
  2320. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  2321. {
  2322. beiscsi_find_mem_req(phba);
  2323. return beiscsi_alloc_mem(phba);
  2324. }
  2325. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  2326. {
  2327. struct pdu_data_out *pdata_out;
  2328. struct pdu_nop_out *pnop_out;
  2329. struct be_mem_descriptor *mem_descr;
  2330. mem_descr = phba->init_mem;
  2331. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  2332. pdata_out =
  2333. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  2334. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2335. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  2336. IIOC_SCSI_DATA);
  2337. pnop_out =
  2338. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  2339. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  2340. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2341. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  2342. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  2343. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  2344. }
  2345. static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  2346. {
  2347. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  2348. struct hwi_context_memory *phwi_ctxt;
  2349. struct wrb_handle *pwrb_handle = NULL;
  2350. struct hwi_controller *phwi_ctrlr;
  2351. struct hwi_wrb_context *pwrb_context;
  2352. struct iscsi_wrb *pwrb = NULL;
  2353. unsigned int num_cxn_wrbh = 0;
  2354. unsigned int num_cxn_wrb = 0, j, idx = 0, index;
  2355. mem_descr_wrbh = phba->init_mem;
  2356. mem_descr_wrbh += HWI_MEM_WRBH;
  2357. mem_descr_wrb = phba->init_mem;
  2358. mem_descr_wrb += HWI_MEM_WRB;
  2359. phwi_ctrlr = phba->phwi_ctrlr;
  2360. /* Allocate memory for WRBQ */
  2361. phwi_ctxt = phwi_ctrlr->phwi_ctxt;
  2362. phwi_ctxt->be_wrbq = kcalloc(phba->params.cxns_per_ctrl,
  2363. sizeof(struct be_queue_info),
  2364. GFP_KERNEL);
  2365. if (!phwi_ctxt->be_wrbq) {
  2366. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2367. "BM_%d : WRBQ Mem Alloc Failed\n");
  2368. return -ENOMEM;
  2369. }
  2370. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2371. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2372. pwrb_context->pwrb_handle_base =
  2373. kcalloc(phba->params.wrbs_per_cxn,
  2374. sizeof(struct wrb_handle *),
  2375. GFP_KERNEL);
  2376. if (!pwrb_context->pwrb_handle_base) {
  2377. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2378. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2379. goto init_wrb_hndl_failed;
  2380. }
  2381. pwrb_context->pwrb_handle_basestd =
  2382. kcalloc(phba->params.wrbs_per_cxn,
  2383. sizeof(struct wrb_handle *),
  2384. GFP_KERNEL);
  2385. if (!pwrb_context->pwrb_handle_basestd) {
  2386. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2387. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2388. goto init_wrb_hndl_failed;
  2389. }
  2390. if (!num_cxn_wrbh) {
  2391. pwrb_handle =
  2392. mem_descr_wrbh->mem_array[idx].virtual_address;
  2393. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  2394. ((sizeof(struct wrb_handle)) *
  2395. phba->params.wrbs_per_cxn));
  2396. idx++;
  2397. }
  2398. pwrb_context->alloc_index = 0;
  2399. pwrb_context->wrb_handles_available = 0;
  2400. pwrb_context->free_index = 0;
  2401. if (num_cxn_wrbh) {
  2402. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2403. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2404. pwrb_context->pwrb_handle_basestd[j] =
  2405. pwrb_handle;
  2406. pwrb_context->wrb_handles_available++;
  2407. pwrb_handle->wrb_index = j;
  2408. pwrb_handle++;
  2409. }
  2410. num_cxn_wrbh--;
  2411. }
  2412. spin_lock_init(&pwrb_context->wrb_lock);
  2413. }
  2414. idx = 0;
  2415. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2416. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2417. if (!num_cxn_wrb) {
  2418. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2419. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2420. ((sizeof(struct iscsi_wrb) *
  2421. phba->params.wrbs_per_cxn));
  2422. idx++;
  2423. }
  2424. if (num_cxn_wrb) {
  2425. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2426. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2427. pwrb_handle->pwrb = pwrb;
  2428. pwrb++;
  2429. }
  2430. num_cxn_wrb--;
  2431. }
  2432. }
  2433. return 0;
  2434. init_wrb_hndl_failed:
  2435. for (j = index; j > 0; j--) {
  2436. pwrb_context = &phwi_ctrlr->wrb_context[j];
  2437. kfree(pwrb_context->pwrb_handle_base);
  2438. kfree(pwrb_context->pwrb_handle_basestd);
  2439. }
  2440. return -ENOMEM;
  2441. }
  2442. static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2443. {
  2444. uint8_t ulp_num;
  2445. struct hwi_controller *phwi_ctrlr;
  2446. struct hba_parameters *p = &phba->params;
  2447. struct hd_async_context *pasync_ctx;
  2448. struct hd_async_handle *pasync_header_h, *pasync_data_h;
  2449. unsigned int index, idx, num_per_mem, num_async_data;
  2450. struct be_mem_descriptor *mem_descr;
  2451. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2452. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2453. /* get async_ctx for each ULP */
  2454. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2455. mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2456. (ulp_num * MEM_DESCR_OFFSET));
  2457. phwi_ctrlr = phba->phwi_ctrlr;
  2458. phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
  2459. (struct hd_async_context *)
  2460. mem_descr->mem_array[0].virtual_address;
  2461. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
  2462. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2463. pasync_ctx->async_entry =
  2464. (struct hd_async_entry *)
  2465. ((long unsigned int)pasync_ctx +
  2466. sizeof(struct hd_async_context));
  2467. pasync_ctx->num_entries = BEISCSI_ASYNC_HDQ_SIZE(phba,
  2468. ulp_num);
  2469. /* setup header buffers */
  2470. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2471. mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2472. (ulp_num * MEM_DESCR_OFFSET);
  2473. if (mem_descr->mem_array[0].virtual_address) {
  2474. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2475. "BM_%d : hwi_init_async_pdu_ctx"
  2476. " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
  2477. ulp_num,
  2478. mem_descr->mem_array[0].
  2479. virtual_address);
  2480. } else
  2481. beiscsi_log(phba, KERN_WARNING,
  2482. BEISCSI_LOG_INIT,
  2483. "BM_%d : No Virtual address for ULP : %d\n",
  2484. ulp_num);
  2485. pasync_ctx->async_header.pi = 0;
  2486. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  2487. pasync_ctx->async_header.va_base =
  2488. mem_descr->mem_array[0].virtual_address;
  2489. pasync_ctx->async_header.pa_base.u.a64.address =
  2490. mem_descr->mem_array[0].
  2491. bus_address.u.a64.address;
  2492. /* setup header buffer sgls */
  2493. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2494. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2495. (ulp_num * MEM_DESCR_OFFSET);
  2496. if (mem_descr->mem_array[0].virtual_address) {
  2497. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2498. "BM_%d : hwi_init_async_pdu_ctx"
  2499. " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
  2500. ulp_num,
  2501. mem_descr->mem_array[0].
  2502. virtual_address);
  2503. } else
  2504. beiscsi_log(phba, KERN_WARNING,
  2505. BEISCSI_LOG_INIT,
  2506. "BM_%d : No Virtual address for ULP : %d\n",
  2507. ulp_num);
  2508. pasync_ctx->async_header.ring_base =
  2509. mem_descr->mem_array[0].virtual_address;
  2510. /* setup header buffer handles */
  2511. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2512. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2513. (ulp_num * MEM_DESCR_OFFSET);
  2514. if (mem_descr->mem_array[0].virtual_address) {
  2515. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2516. "BM_%d : hwi_init_async_pdu_ctx"
  2517. " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
  2518. ulp_num,
  2519. mem_descr->mem_array[0].
  2520. virtual_address);
  2521. } else
  2522. beiscsi_log(phba, KERN_WARNING,
  2523. BEISCSI_LOG_INIT,
  2524. "BM_%d : No Virtual address for ULP : %d\n",
  2525. ulp_num);
  2526. pasync_ctx->async_header.handle_base =
  2527. mem_descr->mem_array[0].virtual_address;
  2528. /* setup data buffer sgls */
  2529. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2530. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2531. (ulp_num * MEM_DESCR_OFFSET);
  2532. if (mem_descr->mem_array[0].virtual_address) {
  2533. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2534. "BM_%d : hwi_init_async_pdu_ctx"
  2535. " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
  2536. ulp_num,
  2537. mem_descr->mem_array[0].
  2538. virtual_address);
  2539. } else
  2540. beiscsi_log(phba, KERN_WARNING,
  2541. BEISCSI_LOG_INIT,
  2542. "BM_%d : No Virtual address for ULP : %d\n",
  2543. ulp_num);
  2544. pasync_ctx->async_data.ring_base =
  2545. mem_descr->mem_array[0].virtual_address;
  2546. /* setup data buffer handles */
  2547. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2548. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2549. (ulp_num * MEM_DESCR_OFFSET);
  2550. if (!mem_descr->mem_array[0].virtual_address)
  2551. beiscsi_log(phba, KERN_WARNING,
  2552. BEISCSI_LOG_INIT,
  2553. "BM_%d : No Virtual address for ULP : %d\n",
  2554. ulp_num);
  2555. pasync_ctx->async_data.handle_base =
  2556. mem_descr->mem_array[0].virtual_address;
  2557. pasync_header_h =
  2558. (struct hd_async_handle *)
  2559. pasync_ctx->async_header.handle_base;
  2560. pasync_data_h =
  2561. (struct hd_async_handle *)
  2562. pasync_ctx->async_data.handle_base;
  2563. /* setup data buffers */
  2564. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2565. mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2566. (ulp_num * MEM_DESCR_OFFSET);
  2567. if (mem_descr->mem_array[0].virtual_address) {
  2568. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2569. "BM_%d : hwi_init_async_pdu_ctx"
  2570. " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
  2571. ulp_num,
  2572. mem_descr->mem_array[0].
  2573. virtual_address);
  2574. } else
  2575. beiscsi_log(phba, KERN_WARNING,
  2576. BEISCSI_LOG_INIT,
  2577. "BM_%d : No Virtual address for ULP : %d\n",
  2578. ulp_num);
  2579. idx = 0;
  2580. pasync_ctx->async_data.pi = 0;
  2581. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  2582. pasync_ctx->async_data.va_base =
  2583. mem_descr->mem_array[idx].virtual_address;
  2584. pasync_ctx->async_data.pa_base.u.a64.address =
  2585. mem_descr->mem_array[idx].
  2586. bus_address.u.a64.address;
  2587. num_async_data = ((mem_descr->mem_array[idx].size) /
  2588. phba->params.defpdu_data_sz);
  2589. num_per_mem = 0;
  2590. for (index = 0; index < BEISCSI_ASYNC_HDQ_SIZE
  2591. (phba, ulp_num); index++) {
  2592. pasync_header_h->cri = -1;
  2593. pasync_header_h->is_header = 1;
  2594. pasync_header_h->index = index;
  2595. INIT_LIST_HEAD(&pasync_header_h->link);
  2596. pasync_header_h->pbuffer =
  2597. (void *)((unsigned long)
  2598. (pasync_ctx->
  2599. async_header.va_base) +
  2600. (p->defpdu_hdr_sz * index));
  2601. pasync_header_h->pa.u.a64.address =
  2602. pasync_ctx->async_header.pa_base.u.a64.
  2603. address + (p->defpdu_hdr_sz * index);
  2604. pasync_ctx->async_entry[index].header =
  2605. pasync_header_h;
  2606. pasync_header_h++;
  2607. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2608. wq.list);
  2609. pasync_data_h->cri = -1;
  2610. pasync_data_h->is_header = 0;
  2611. pasync_data_h->index = index;
  2612. INIT_LIST_HEAD(&pasync_data_h->link);
  2613. if (!num_async_data) {
  2614. num_per_mem = 0;
  2615. idx++;
  2616. pasync_ctx->async_data.va_base =
  2617. mem_descr->mem_array[idx].
  2618. virtual_address;
  2619. pasync_ctx->async_data.pa_base.u.
  2620. a64.address =
  2621. mem_descr->mem_array[idx].
  2622. bus_address.u.a64.address;
  2623. num_async_data =
  2624. ((mem_descr->mem_array[idx].
  2625. size) /
  2626. phba->params.defpdu_data_sz);
  2627. }
  2628. pasync_data_h->pbuffer =
  2629. (void *)((unsigned long)
  2630. (pasync_ctx->async_data.va_base) +
  2631. (p->defpdu_data_sz * num_per_mem));
  2632. pasync_data_h->pa.u.a64.address =
  2633. pasync_ctx->async_data.pa_base.u.a64.
  2634. address + (p->defpdu_data_sz *
  2635. num_per_mem);
  2636. num_per_mem++;
  2637. num_async_data--;
  2638. pasync_ctx->async_entry[index].data =
  2639. pasync_data_h;
  2640. pasync_data_h++;
  2641. }
  2642. }
  2643. }
  2644. return 0;
  2645. }
  2646. static int
  2647. be_sgl_create_contiguous(void *virtual_address,
  2648. u64 physical_address, u32 length,
  2649. struct be_dma_mem *sgl)
  2650. {
  2651. WARN_ON(!virtual_address);
  2652. WARN_ON(!physical_address);
  2653. WARN_ON(!length);
  2654. WARN_ON(!sgl);
  2655. sgl->va = virtual_address;
  2656. sgl->dma = (unsigned long)physical_address;
  2657. sgl->size = length;
  2658. return 0;
  2659. }
  2660. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2661. {
  2662. memset(sgl, 0, sizeof(*sgl));
  2663. }
  2664. static void
  2665. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2666. struct mem_array *pmem, struct be_dma_mem *sgl)
  2667. {
  2668. if (sgl->va)
  2669. be_sgl_destroy_contiguous(sgl);
  2670. be_sgl_create_contiguous(pmem->virtual_address,
  2671. pmem->bus_address.u.a64.address,
  2672. pmem->size, sgl);
  2673. }
  2674. static void
  2675. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2676. struct mem_array *pmem, struct be_dma_mem *sgl)
  2677. {
  2678. if (sgl->va)
  2679. be_sgl_destroy_contiguous(sgl);
  2680. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2681. pmem->bus_address.u.a64.address,
  2682. pmem->size, sgl);
  2683. }
  2684. static int be_fill_queue(struct be_queue_info *q,
  2685. u16 len, u16 entry_size, void *vaddress)
  2686. {
  2687. struct be_dma_mem *mem = &q->dma_mem;
  2688. memset(q, 0, sizeof(*q));
  2689. q->len = len;
  2690. q->entry_size = entry_size;
  2691. mem->size = len * entry_size;
  2692. mem->va = vaddress;
  2693. if (!mem->va)
  2694. return -ENOMEM;
  2695. memset(mem->va, 0, mem->size);
  2696. return 0;
  2697. }
  2698. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2699. struct hwi_context_memory *phwi_context)
  2700. {
  2701. int ret = -ENOMEM, eq_for_mcc;
  2702. unsigned int i, num_eq_pages;
  2703. struct be_queue_info *eq;
  2704. struct be_dma_mem *mem;
  2705. void *eq_vaddress;
  2706. dma_addr_t paddr;
  2707. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2708. sizeof(struct be_eq_entry));
  2709. if (phba->pcidev->msix_enabled)
  2710. eq_for_mcc = 1;
  2711. else
  2712. eq_for_mcc = 0;
  2713. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2714. eq = &phwi_context->be_eq[i].q;
  2715. mem = &eq->dma_mem;
  2716. phwi_context->be_eq[i].phba = phba;
  2717. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2718. num_eq_pages * PAGE_SIZE,
  2719. &paddr);
  2720. if (!eq_vaddress) {
  2721. ret = -ENOMEM;
  2722. goto create_eq_error;
  2723. }
  2724. mem->va = eq_vaddress;
  2725. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2726. sizeof(struct be_eq_entry), eq_vaddress);
  2727. if (ret) {
  2728. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2729. "BM_%d : be_fill_queue Failed for EQ\n");
  2730. goto create_eq_error;
  2731. }
  2732. mem->dma = paddr;
  2733. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2734. BEISCSI_EQ_DELAY_DEF);
  2735. if (ret) {
  2736. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2737. "BM_%d : beiscsi_cmd_eq_create"
  2738. "Failed for EQ\n");
  2739. goto create_eq_error;
  2740. }
  2741. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2742. "BM_%d : eqid = %d\n",
  2743. phwi_context->be_eq[i].q.id);
  2744. }
  2745. return 0;
  2746. create_eq_error:
  2747. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2748. eq = &phwi_context->be_eq[i].q;
  2749. mem = &eq->dma_mem;
  2750. if (mem->va)
  2751. pci_free_consistent(phba->pcidev, num_eq_pages
  2752. * PAGE_SIZE,
  2753. mem->va, mem->dma);
  2754. }
  2755. return ret;
  2756. }
  2757. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2758. struct hwi_context_memory *phwi_context)
  2759. {
  2760. unsigned int i, num_cq_pages;
  2761. struct be_queue_info *cq, *eq;
  2762. struct be_dma_mem *mem;
  2763. struct be_eq_obj *pbe_eq;
  2764. void *cq_vaddress;
  2765. int ret = -ENOMEM;
  2766. dma_addr_t paddr;
  2767. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2768. sizeof(struct sol_cqe));
  2769. for (i = 0; i < phba->num_cpus; i++) {
  2770. cq = &phwi_context->be_cq[i];
  2771. eq = &phwi_context->be_eq[i].q;
  2772. pbe_eq = &phwi_context->be_eq[i];
  2773. pbe_eq->cq = cq;
  2774. pbe_eq->phba = phba;
  2775. mem = &cq->dma_mem;
  2776. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2777. num_cq_pages * PAGE_SIZE,
  2778. &paddr);
  2779. if (!cq_vaddress) {
  2780. ret = -ENOMEM;
  2781. goto create_cq_error;
  2782. }
  2783. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2784. sizeof(struct sol_cqe), cq_vaddress);
  2785. if (ret) {
  2786. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2787. "BM_%d : be_fill_queue Failed "
  2788. "for ISCSI CQ\n");
  2789. goto create_cq_error;
  2790. }
  2791. mem->dma = paddr;
  2792. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2793. false, 0);
  2794. if (ret) {
  2795. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2796. "BM_%d : beiscsi_cmd_eq_create"
  2797. "Failed for ISCSI CQ\n");
  2798. goto create_cq_error;
  2799. }
  2800. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2801. "BM_%d : iscsi cq_id is %d for eq_id %d\n"
  2802. "iSCSI CQ CREATED\n", cq->id, eq->id);
  2803. }
  2804. return 0;
  2805. create_cq_error:
  2806. for (i = 0; i < phba->num_cpus; i++) {
  2807. cq = &phwi_context->be_cq[i];
  2808. mem = &cq->dma_mem;
  2809. if (mem->va)
  2810. pci_free_consistent(phba->pcidev, num_cq_pages
  2811. * PAGE_SIZE,
  2812. mem->va, mem->dma);
  2813. }
  2814. return ret;
  2815. }
  2816. static int
  2817. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2818. struct hwi_context_memory *phwi_context,
  2819. struct hwi_controller *phwi_ctrlr,
  2820. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2821. {
  2822. unsigned int idx;
  2823. int ret;
  2824. struct be_queue_info *dq, *cq;
  2825. struct be_dma_mem *mem;
  2826. struct be_mem_descriptor *mem_descr;
  2827. void *dq_vaddress;
  2828. idx = 0;
  2829. dq = &phwi_context->be_def_hdrq[ulp_num];
  2830. cq = &phwi_context->be_cq[0];
  2831. mem = &dq->dma_mem;
  2832. mem_descr = phba->init_mem;
  2833. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2834. (ulp_num * MEM_DESCR_OFFSET);
  2835. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2836. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2837. sizeof(struct phys_addr),
  2838. sizeof(struct phys_addr), dq_vaddress);
  2839. if (ret) {
  2840. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2841. "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
  2842. ulp_num);
  2843. return ret;
  2844. }
  2845. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2846. bus_address.u.a64.address;
  2847. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2848. def_pdu_ring_sz,
  2849. phba->params.defpdu_hdr_sz,
  2850. BEISCSI_DEFQ_HDR, ulp_num);
  2851. if (ret) {
  2852. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2853. "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
  2854. ulp_num);
  2855. return ret;
  2856. }
  2857. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2858. "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
  2859. ulp_num,
  2860. phwi_context->be_def_hdrq[ulp_num].id);
  2861. return 0;
  2862. }
  2863. static int
  2864. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2865. struct hwi_context_memory *phwi_context,
  2866. struct hwi_controller *phwi_ctrlr,
  2867. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2868. {
  2869. unsigned int idx;
  2870. int ret;
  2871. struct be_queue_info *dataq, *cq;
  2872. struct be_dma_mem *mem;
  2873. struct be_mem_descriptor *mem_descr;
  2874. void *dq_vaddress;
  2875. idx = 0;
  2876. dataq = &phwi_context->be_def_dataq[ulp_num];
  2877. cq = &phwi_context->be_cq[0];
  2878. mem = &dataq->dma_mem;
  2879. mem_descr = phba->init_mem;
  2880. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2881. (ulp_num * MEM_DESCR_OFFSET);
  2882. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2883. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2884. sizeof(struct phys_addr),
  2885. sizeof(struct phys_addr), dq_vaddress);
  2886. if (ret) {
  2887. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2888. "BM_%d : be_fill_queue Failed for DEF PDU "
  2889. "DATA on ULP : %d\n",
  2890. ulp_num);
  2891. return ret;
  2892. }
  2893. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2894. bus_address.u.a64.address;
  2895. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2896. def_pdu_ring_sz,
  2897. phba->params.defpdu_data_sz,
  2898. BEISCSI_DEFQ_DATA, ulp_num);
  2899. if (ret) {
  2900. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2901. "BM_%d be_cmd_create_default_pdu_queue"
  2902. " Failed for DEF PDU DATA on ULP : %d\n",
  2903. ulp_num);
  2904. return ret;
  2905. }
  2906. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2907. "BM_%d : iscsi def data id on ULP : %d is %d\n",
  2908. ulp_num,
  2909. phwi_context->be_def_dataq[ulp_num].id);
  2910. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2911. "BM_%d : DEFAULT PDU DATA RING CREATED"
  2912. "on ULP : %d\n", ulp_num);
  2913. return 0;
  2914. }
  2915. static int
  2916. beiscsi_post_template_hdr(struct beiscsi_hba *phba)
  2917. {
  2918. struct be_mem_descriptor *mem_descr;
  2919. struct mem_array *pm_arr;
  2920. struct be_dma_mem sgl;
  2921. int status, ulp_num;
  2922. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2923. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2924. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2925. mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
  2926. (ulp_num * MEM_DESCR_OFFSET);
  2927. pm_arr = mem_descr->mem_array;
  2928. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2929. status = be_cmd_iscsi_post_template_hdr(
  2930. &phba->ctrl, &sgl);
  2931. if (status != 0) {
  2932. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2933. "BM_%d : Post Template HDR Failed for"
  2934. "ULP_%d\n", ulp_num);
  2935. return status;
  2936. }
  2937. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2938. "BM_%d : Template HDR Pages Posted for"
  2939. "ULP_%d\n", ulp_num);
  2940. }
  2941. }
  2942. return 0;
  2943. }
  2944. static int
  2945. beiscsi_post_pages(struct beiscsi_hba *phba)
  2946. {
  2947. struct be_mem_descriptor *mem_descr;
  2948. struct mem_array *pm_arr;
  2949. unsigned int page_offset, i;
  2950. struct be_dma_mem sgl;
  2951. int status, ulp_num = 0;
  2952. mem_descr = phba->init_mem;
  2953. mem_descr += HWI_MEM_SGE;
  2954. pm_arr = mem_descr->mem_array;
  2955. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  2956. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  2957. break;
  2958. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2959. phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
  2960. for (i = 0; i < mem_descr->num_elements; i++) {
  2961. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2962. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2963. page_offset,
  2964. (pm_arr->size / PAGE_SIZE));
  2965. page_offset += pm_arr->size / PAGE_SIZE;
  2966. if (status != 0) {
  2967. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2968. "BM_%d : post sgl failed.\n");
  2969. return status;
  2970. }
  2971. pm_arr++;
  2972. }
  2973. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2974. "BM_%d : POSTED PAGES\n");
  2975. return 0;
  2976. }
  2977. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2978. {
  2979. struct be_dma_mem *mem = &q->dma_mem;
  2980. if (mem->va) {
  2981. pci_free_consistent(phba->pcidev, mem->size,
  2982. mem->va, mem->dma);
  2983. mem->va = NULL;
  2984. }
  2985. }
  2986. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2987. u16 len, u16 entry_size)
  2988. {
  2989. struct be_dma_mem *mem = &q->dma_mem;
  2990. memset(q, 0, sizeof(*q));
  2991. q->len = len;
  2992. q->entry_size = entry_size;
  2993. mem->size = len * entry_size;
  2994. mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2995. if (!mem->va)
  2996. return -ENOMEM;
  2997. return 0;
  2998. }
  2999. static int
  3000. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  3001. struct hwi_context_memory *phwi_context,
  3002. struct hwi_controller *phwi_ctrlr)
  3003. {
  3004. unsigned int num_wrb_rings;
  3005. u64 pa_addr_lo;
  3006. unsigned int idx, num, i, ulp_num;
  3007. struct mem_array *pwrb_arr;
  3008. void *wrb_vaddr;
  3009. struct be_dma_mem sgl;
  3010. struct be_mem_descriptor *mem_descr;
  3011. struct hwi_wrb_context *pwrb_context;
  3012. int status;
  3013. uint8_t ulp_count = 0, ulp_base_num = 0;
  3014. uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
  3015. idx = 0;
  3016. mem_descr = phba->init_mem;
  3017. mem_descr += HWI_MEM_WRB;
  3018. pwrb_arr = kmalloc_array(phba->params.cxns_per_ctrl,
  3019. sizeof(*pwrb_arr),
  3020. GFP_KERNEL);
  3021. if (!pwrb_arr) {
  3022. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3023. "BM_%d : Memory alloc failed in create wrb ring.\n");
  3024. return -ENOMEM;
  3025. }
  3026. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3027. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  3028. num_wrb_rings = mem_descr->mem_array[idx].size /
  3029. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  3030. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  3031. if (num_wrb_rings) {
  3032. pwrb_arr[num].virtual_address = wrb_vaddr;
  3033. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  3034. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3035. sizeof(struct iscsi_wrb);
  3036. wrb_vaddr += pwrb_arr[num].size;
  3037. pa_addr_lo += pwrb_arr[num].size;
  3038. num_wrb_rings--;
  3039. } else {
  3040. idx++;
  3041. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3042. pa_addr_lo = mem_descr->mem_array[idx].\
  3043. bus_address.u.a64.address;
  3044. num_wrb_rings = mem_descr->mem_array[idx].size /
  3045. (phba->params.wrbs_per_cxn *
  3046. sizeof(struct iscsi_wrb));
  3047. pwrb_arr[num].virtual_address = wrb_vaddr;
  3048. pwrb_arr[num].bus_address.u.a64.address\
  3049. = pa_addr_lo;
  3050. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3051. sizeof(struct iscsi_wrb);
  3052. wrb_vaddr += pwrb_arr[num].size;
  3053. pa_addr_lo += pwrb_arr[num].size;
  3054. num_wrb_rings--;
  3055. }
  3056. }
  3057. /* Get the ULP Count */
  3058. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3059. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3060. ulp_count++;
  3061. ulp_base_num = ulp_num;
  3062. cid_count_ulp[ulp_num] =
  3063. BEISCSI_GET_CID_COUNT(phba, ulp_num);
  3064. }
  3065. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3066. if (ulp_count > 1) {
  3067. ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
  3068. if (!cid_count_ulp[ulp_base_num])
  3069. ulp_base_num = (ulp_base_num + 1) %
  3070. BEISCSI_ULP_COUNT;
  3071. cid_count_ulp[ulp_base_num]--;
  3072. }
  3073. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  3074. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  3075. &phwi_context->be_wrbq[i],
  3076. &phwi_ctrlr->wrb_context[i],
  3077. ulp_base_num);
  3078. if (status != 0) {
  3079. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3080. "BM_%d : wrbq create failed.");
  3081. kfree(pwrb_arr);
  3082. return status;
  3083. }
  3084. pwrb_context = &phwi_ctrlr->wrb_context[i];
  3085. BE_SET_CID_TO_CRI(i, pwrb_context->cid);
  3086. }
  3087. kfree(pwrb_arr);
  3088. return 0;
  3089. }
  3090. static void free_wrb_handles(struct beiscsi_hba *phba)
  3091. {
  3092. unsigned int index;
  3093. struct hwi_controller *phwi_ctrlr;
  3094. struct hwi_wrb_context *pwrb_context;
  3095. phwi_ctrlr = phba->phwi_ctrlr;
  3096. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  3097. pwrb_context = &phwi_ctrlr->wrb_context[index];
  3098. kfree(pwrb_context->pwrb_handle_base);
  3099. kfree(pwrb_context->pwrb_handle_basestd);
  3100. }
  3101. }
  3102. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  3103. {
  3104. struct be_ctrl_info *ctrl = &phba->ctrl;
  3105. struct be_dma_mem *ptag_mem;
  3106. struct be_queue_info *q;
  3107. int i, tag;
  3108. q = &phba->ctrl.mcc_obj.q;
  3109. for (i = 0; i < MAX_MCC_CMD; i++) {
  3110. tag = i + 1;
  3111. if (!test_bit(MCC_TAG_STATE_RUNNING,
  3112. &ctrl->ptag_state[tag].tag_state))
  3113. continue;
  3114. if (test_bit(MCC_TAG_STATE_TIMEOUT,
  3115. &ctrl->ptag_state[tag].tag_state)) {
  3116. ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
  3117. if (ptag_mem->size) {
  3118. pci_free_consistent(ctrl->pdev,
  3119. ptag_mem->size,
  3120. ptag_mem->va,
  3121. ptag_mem->dma);
  3122. ptag_mem->size = 0;
  3123. }
  3124. continue;
  3125. }
  3126. /**
  3127. * If MCC is still active and waiting then wake up the process.
  3128. * We are here only because port is going offline. The process
  3129. * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
  3130. * returned for the operation and allocated memory cleaned up.
  3131. */
  3132. if (waitqueue_active(&ctrl->mcc_wait[tag])) {
  3133. ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
  3134. ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
  3135. wake_up_interruptible(&ctrl->mcc_wait[tag]);
  3136. /*
  3137. * Control tag info gets reinitialized in enable
  3138. * so wait for the process to clear running state.
  3139. */
  3140. while (test_bit(MCC_TAG_STATE_RUNNING,
  3141. &ctrl->ptag_state[tag].tag_state))
  3142. schedule_timeout_uninterruptible(HZ);
  3143. }
  3144. /**
  3145. * For MCC with tag_states MCC_TAG_STATE_ASYNC and
  3146. * MCC_TAG_STATE_IGNORE nothing needs to done.
  3147. */
  3148. }
  3149. if (q->created) {
  3150. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  3151. be_queue_free(phba, q);
  3152. }
  3153. q = &phba->ctrl.mcc_obj.cq;
  3154. if (q->created) {
  3155. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3156. be_queue_free(phba, q);
  3157. }
  3158. }
  3159. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  3160. struct hwi_context_memory *phwi_context)
  3161. {
  3162. struct be_queue_info *q, *cq;
  3163. struct be_ctrl_info *ctrl = &phba->ctrl;
  3164. /* Alloc MCC compl queue */
  3165. cq = &phba->ctrl.mcc_obj.cq;
  3166. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  3167. sizeof(struct be_mcc_compl)))
  3168. goto err;
  3169. /* Ask BE to create MCC compl queue; */
  3170. if (phba->pcidev->msix_enabled) {
  3171. if (beiscsi_cmd_cq_create(ctrl, cq,
  3172. &phwi_context->be_eq[phba->num_cpus].q,
  3173. false, true, 0))
  3174. goto mcc_cq_free;
  3175. } else {
  3176. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  3177. false, true, 0))
  3178. goto mcc_cq_free;
  3179. }
  3180. /* Alloc MCC queue */
  3181. q = &phba->ctrl.mcc_obj.q;
  3182. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  3183. goto mcc_cq_destroy;
  3184. /* Ask BE to create MCC queue */
  3185. if (beiscsi_cmd_mccq_create(phba, q, cq))
  3186. goto mcc_q_free;
  3187. return 0;
  3188. mcc_q_free:
  3189. be_queue_free(phba, q);
  3190. mcc_cq_destroy:
  3191. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  3192. mcc_cq_free:
  3193. be_queue_free(phba, cq);
  3194. err:
  3195. return -ENOMEM;
  3196. }
  3197. static void be2iscsi_enable_msix(struct beiscsi_hba *phba)
  3198. {
  3199. int nvec = 1;
  3200. switch (phba->generation) {
  3201. case BE_GEN2:
  3202. case BE_GEN3:
  3203. nvec = BEISCSI_MAX_NUM_CPUS + 1;
  3204. break;
  3205. case BE_GEN4:
  3206. nvec = phba->fw_config.eqid_count;
  3207. break;
  3208. default:
  3209. nvec = 2;
  3210. break;
  3211. }
  3212. /* if eqid_count == 1 fall back to INTX */
  3213. if (enable_msix && nvec > 1) {
  3214. const struct irq_affinity desc = { .post_vectors = 1 };
  3215. if (pci_alloc_irq_vectors_affinity(phba->pcidev, 2, nvec,
  3216. PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc) < 0) {
  3217. phba->num_cpus = nvec - 1;
  3218. return;
  3219. }
  3220. }
  3221. phba->num_cpus = 1;
  3222. }
  3223. static void hwi_purge_eq(struct beiscsi_hba *phba)
  3224. {
  3225. struct hwi_controller *phwi_ctrlr;
  3226. struct hwi_context_memory *phwi_context;
  3227. struct be_queue_info *eq;
  3228. struct be_eq_entry *eqe = NULL;
  3229. int i, eq_msix;
  3230. unsigned int num_processed;
  3231. if (beiscsi_hba_in_error(phba))
  3232. return;
  3233. phwi_ctrlr = phba->phwi_ctrlr;
  3234. phwi_context = phwi_ctrlr->phwi_ctxt;
  3235. if (phba->pcidev->msix_enabled)
  3236. eq_msix = 1;
  3237. else
  3238. eq_msix = 0;
  3239. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  3240. eq = &phwi_context->be_eq[i].q;
  3241. eqe = queue_tail_node(eq);
  3242. num_processed = 0;
  3243. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  3244. & EQE_VALID_MASK) {
  3245. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  3246. queue_tail_inc(eq);
  3247. eqe = queue_tail_node(eq);
  3248. num_processed++;
  3249. }
  3250. if (num_processed)
  3251. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  3252. }
  3253. }
  3254. static void hwi_cleanup_port(struct beiscsi_hba *phba)
  3255. {
  3256. struct be_queue_info *q;
  3257. struct be_ctrl_info *ctrl = &phba->ctrl;
  3258. struct hwi_controller *phwi_ctrlr;
  3259. struct hwi_context_memory *phwi_context;
  3260. int i, eq_for_mcc, ulp_num;
  3261. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3262. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3263. beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
  3264. /**
  3265. * Purge all EQ entries that may have been left out. This is to
  3266. * workaround a problem we've seen occasionally where driver gets an
  3267. * interrupt with EQ entry bit set after stopping the controller.
  3268. */
  3269. hwi_purge_eq(phba);
  3270. phwi_ctrlr = phba->phwi_ctrlr;
  3271. phwi_context = phwi_ctrlr->phwi_ctxt;
  3272. be_cmd_iscsi_remove_template_hdr(ctrl);
  3273. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3274. q = &phwi_context->be_wrbq[i];
  3275. if (q->created)
  3276. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  3277. }
  3278. kfree(phwi_context->be_wrbq);
  3279. free_wrb_handles(phba);
  3280. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3281. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3282. q = &phwi_context->be_def_hdrq[ulp_num];
  3283. if (q->created)
  3284. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3285. q = &phwi_context->be_def_dataq[ulp_num];
  3286. if (q->created)
  3287. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3288. }
  3289. }
  3290. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  3291. for (i = 0; i < (phba->num_cpus); i++) {
  3292. q = &phwi_context->be_cq[i];
  3293. if (q->created) {
  3294. be_queue_free(phba, q);
  3295. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3296. }
  3297. }
  3298. be_mcc_queues_destroy(phba);
  3299. if (phba->pcidev->msix_enabled)
  3300. eq_for_mcc = 1;
  3301. else
  3302. eq_for_mcc = 0;
  3303. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  3304. q = &phwi_context->be_eq[i].q;
  3305. if (q->created) {
  3306. be_queue_free(phba, q);
  3307. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  3308. }
  3309. }
  3310. /* this ensures complete FW cleanup */
  3311. beiscsi_cmd_function_reset(phba);
  3312. /* last communication, indicate driver is unloading */
  3313. beiscsi_cmd_special_wrb(&phba->ctrl, 0);
  3314. }
  3315. static int hwi_init_port(struct beiscsi_hba *phba)
  3316. {
  3317. struct hwi_controller *phwi_ctrlr;
  3318. struct hwi_context_memory *phwi_context;
  3319. unsigned int def_pdu_ring_sz;
  3320. struct be_ctrl_info *ctrl = &phba->ctrl;
  3321. int status, ulp_num;
  3322. u16 nbufs;
  3323. phwi_ctrlr = phba->phwi_ctrlr;
  3324. phwi_context = phwi_ctrlr->phwi_ctxt;
  3325. /* set port optic state to unknown */
  3326. phba->optic_state = 0xff;
  3327. status = beiscsi_create_eqs(phba, phwi_context);
  3328. if (status != 0) {
  3329. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3330. "BM_%d : EQ not created\n");
  3331. goto error;
  3332. }
  3333. status = be_mcc_queues_create(phba, phwi_context);
  3334. if (status != 0)
  3335. goto error;
  3336. status = beiscsi_check_supported_fw(ctrl, phba);
  3337. if (status != 0) {
  3338. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3339. "BM_%d : Unsupported fw version\n");
  3340. goto error;
  3341. }
  3342. status = beiscsi_create_cqs(phba, phwi_context);
  3343. if (status != 0) {
  3344. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3345. "BM_%d : CQ not created\n");
  3346. goto error;
  3347. }
  3348. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3349. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3350. nbufs = phwi_context->pasync_ctx[ulp_num]->num_entries;
  3351. def_pdu_ring_sz = nbufs * sizeof(struct phys_addr);
  3352. status = beiscsi_create_def_hdr(phba, phwi_context,
  3353. phwi_ctrlr,
  3354. def_pdu_ring_sz,
  3355. ulp_num);
  3356. if (status != 0) {
  3357. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3358. "BM_%d : Default Header not created for ULP : %d\n",
  3359. ulp_num);
  3360. goto error;
  3361. }
  3362. status = beiscsi_create_def_data(phba, phwi_context,
  3363. phwi_ctrlr,
  3364. def_pdu_ring_sz,
  3365. ulp_num);
  3366. if (status != 0) {
  3367. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3368. "BM_%d : Default Data not created for ULP : %d\n",
  3369. ulp_num);
  3370. goto error;
  3371. }
  3372. /**
  3373. * Now that the default PDU rings have been created,
  3374. * let EP know about it.
  3375. */
  3376. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
  3377. ulp_num, nbufs);
  3378. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
  3379. ulp_num, nbufs);
  3380. }
  3381. }
  3382. status = beiscsi_post_pages(phba);
  3383. if (status != 0) {
  3384. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3385. "BM_%d : Post SGL Pages Failed\n");
  3386. goto error;
  3387. }
  3388. status = beiscsi_post_template_hdr(phba);
  3389. if (status != 0) {
  3390. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3391. "BM_%d : Template HDR Posting for CXN Failed\n");
  3392. }
  3393. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  3394. if (status != 0) {
  3395. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3396. "BM_%d : WRB Rings not created\n");
  3397. goto error;
  3398. }
  3399. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3400. uint16_t async_arr_idx = 0;
  3401. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3402. uint16_t cri = 0;
  3403. struct hd_async_context *pasync_ctx;
  3404. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
  3405. phwi_ctrlr, ulp_num);
  3406. for (cri = 0; cri <
  3407. phba->params.cxns_per_ctrl; cri++) {
  3408. if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
  3409. (phwi_ctrlr, cri))
  3410. pasync_ctx->cid_to_async_cri_map[
  3411. phwi_ctrlr->wrb_context[cri].cid] =
  3412. async_arr_idx++;
  3413. }
  3414. }
  3415. }
  3416. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3417. "BM_%d : hwi_init_port success\n");
  3418. return 0;
  3419. error:
  3420. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3421. "BM_%d : hwi_init_port failed");
  3422. hwi_cleanup_port(phba);
  3423. return status;
  3424. }
  3425. static int hwi_init_controller(struct beiscsi_hba *phba)
  3426. {
  3427. struct hwi_controller *phwi_ctrlr;
  3428. phwi_ctrlr = phba->phwi_ctrlr;
  3429. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  3430. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  3431. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  3432. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3433. "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
  3434. phwi_ctrlr->phwi_ctxt);
  3435. } else {
  3436. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3437. "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
  3438. "than one element.Failing to load\n");
  3439. return -ENOMEM;
  3440. }
  3441. iscsi_init_global_templates(phba);
  3442. if (beiscsi_init_wrb_handle(phba))
  3443. return -ENOMEM;
  3444. if (hwi_init_async_pdu_ctx(phba)) {
  3445. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3446. "BM_%d : hwi_init_async_pdu_ctx failed\n");
  3447. return -ENOMEM;
  3448. }
  3449. if (hwi_init_port(phba) != 0) {
  3450. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3451. "BM_%d : hwi_init_controller failed\n");
  3452. return -ENOMEM;
  3453. }
  3454. return 0;
  3455. }
  3456. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  3457. {
  3458. struct be_mem_descriptor *mem_descr;
  3459. int i, j;
  3460. mem_descr = phba->init_mem;
  3461. i = 0;
  3462. j = 0;
  3463. for (i = 0; i < SE_MEM_MAX; i++) {
  3464. for (j = mem_descr->num_elements; j > 0; j--) {
  3465. pci_free_consistent(phba->pcidev,
  3466. mem_descr->mem_array[j - 1].size,
  3467. mem_descr->mem_array[j - 1].virtual_address,
  3468. (unsigned long)mem_descr->mem_array[j - 1].
  3469. bus_address.u.a64.address);
  3470. }
  3471. kfree(mem_descr->mem_array);
  3472. mem_descr++;
  3473. }
  3474. kfree(phba->init_mem);
  3475. kfree(phba->phwi_ctrlr->wrb_context);
  3476. kfree(phba->phwi_ctrlr);
  3477. }
  3478. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  3479. {
  3480. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  3481. struct sgl_handle *psgl_handle;
  3482. struct iscsi_sge *pfrag;
  3483. unsigned int arr_index, i, idx;
  3484. unsigned int ulp_icd_start, ulp_num = 0;
  3485. phba->io_sgl_hndl_avbl = 0;
  3486. phba->eh_sgl_hndl_avbl = 0;
  3487. mem_descr_sglh = phba->init_mem;
  3488. mem_descr_sglh += HWI_MEM_SGLH;
  3489. if (1 == mem_descr_sglh->num_elements) {
  3490. phba->io_sgl_hndl_base = kcalloc(phba->params.ios_per_ctrl,
  3491. sizeof(struct sgl_handle *),
  3492. GFP_KERNEL);
  3493. if (!phba->io_sgl_hndl_base) {
  3494. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3495. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3496. return -ENOMEM;
  3497. }
  3498. phba->eh_sgl_hndl_base =
  3499. kcalloc(phba->params.icds_per_ctrl -
  3500. phba->params.ios_per_ctrl,
  3501. sizeof(struct sgl_handle *), GFP_KERNEL);
  3502. if (!phba->eh_sgl_hndl_base) {
  3503. kfree(phba->io_sgl_hndl_base);
  3504. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3505. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3506. return -ENOMEM;
  3507. }
  3508. } else {
  3509. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3510. "BM_%d : HWI_MEM_SGLH is more than one element."
  3511. "Failing to load\n");
  3512. return -ENOMEM;
  3513. }
  3514. arr_index = 0;
  3515. idx = 0;
  3516. while (idx < mem_descr_sglh->num_elements) {
  3517. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  3518. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  3519. sizeof(struct sgl_handle)); i++) {
  3520. if (arr_index < phba->params.ios_per_ctrl) {
  3521. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  3522. phba->io_sgl_hndl_avbl++;
  3523. arr_index++;
  3524. } else {
  3525. phba->eh_sgl_hndl_base[arr_index -
  3526. phba->params.ios_per_ctrl] =
  3527. psgl_handle;
  3528. arr_index++;
  3529. phba->eh_sgl_hndl_avbl++;
  3530. }
  3531. psgl_handle++;
  3532. }
  3533. idx++;
  3534. }
  3535. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3536. "BM_%d : phba->io_sgl_hndl_avbl=%d"
  3537. "phba->eh_sgl_hndl_avbl=%d\n",
  3538. phba->io_sgl_hndl_avbl,
  3539. phba->eh_sgl_hndl_avbl);
  3540. mem_descr_sg = phba->init_mem;
  3541. mem_descr_sg += HWI_MEM_SGE;
  3542. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3543. "\n BM_%d : mem_descr_sg->num_elements=%d\n",
  3544. mem_descr_sg->num_elements);
  3545. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3546. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3547. break;
  3548. ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  3549. arr_index = 0;
  3550. idx = 0;
  3551. while (idx < mem_descr_sg->num_elements) {
  3552. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  3553. for (i = 0;
  3554. i < (mem_descr_sg->mem_array[idx].size) /
  3555. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  3556. i++) {
  3557. if (arr_index < phba->params.ios_per_ctrl)
  3558. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  3559. else
  3560. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  3561. phba->params.ios_per_ctrl];
  3562. psgl_handle->pfrag = pfrag;
  3563. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  3564. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  3565. pfrag += phba->params.num_sge_per_io;
  3566. psgl_handle->sgl_index = ulp_icd_start + arr_index++;
  3567. }
  3568. idx++;
  3569. }
  3570. phba->io_sgl_free_index = 0;
  3571. phba->io_sgl_alloc_index = 0;
  3572. phba->eh_sgl_free_index = 0;
  3573. phba->eh_sgl_alloc_index = 0;
  3574. return 0;
  3575. }
  3576. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  3577. {
  3578. int ret;
  3579. uint16_t i, ulp_num;
  3580. struct ulp_cid_info *ptr_cid_info = NULL;
  3581. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3582. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3583. ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
  3584. GFP_KERNEL);
  3585. if (!ptr_cid_info) {
  3586. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3587. "BM_%d : Failed to allocate memory"
  3588. "for ULP_CID_INFO for ULP : %d\n",
  3589. ulp_num);
  3590. ret = -ENOMEM;
  3591. goto free_memory;
  3592. }
  3593. /* Allocate memory for CID array */
  3594. ptr_cid_info->cid_array =
  3595. kcalloc(BEISCSI_GET_CID_COUNT(phba, ulp_num),
  3596. sizeof(*ptr_cid_info->cid_array),
  3597. GFP_KERNEL);
  3598. if (!ptr_cid_info->cid_array) {
  3599. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3600. "BM_%d : Failed to allocate memory"
  3601. "for CID_ARRAY for ULP : %d\n",
  3602. ulp_num);
  3603. kfree(ptr_cid_info);
  3604. ptr_cid_info = NULL;
  3605. ret = -ENOMEM;
  3606. goto free_memory;
  3607. }
  3608. ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
  3609. phba, ulp_num);
  3610. /* Save the cid_info_array ptr */
  3611. phba->cid_array_info[ulp_num] = ptr_cid_info;
  3612. }
  3613. }
  3614. phba->ep_array = kcalloc(phba->params.cxns_per_ctrl,
  3615. sizeof(struct iscsi_endpoint *),
  3616. GFP_KERNEL);
  3617. if (!phba->ep_array) {
  3618. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3619. "BM_%d : Failed to allocate memory in "
  3620. "hba_setup_cid_tbls\n");
  3621. ret = -ENOMEM;
  3622. goto free_memory;
  3623. }
  3624. phba->conn_table = kcalloc(phba->params.cxns_per_ctrl,
  3625. sizeof(struct beiscsi_conn *),
  3626. GFP_KERNEL);
  3627. if (!phba->conn_table) {
  3628. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3629. "BM_%d : Failed to allocate memory in"
  3630. "hba_setup_cid_tbls\n");
  3631. kfree(phba->ep_array);
  3632. phba->ep_array = NULL;
  3633. ret = -ENOMEM;
  3634. goto free_memory;
  3635. }
  3636. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3637. ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
  3638. ptr_cid_info = phba->cid_array_info[ulp_num];
  3639. ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
  3640. phba->phwi_ctrlr->wrb_context[i].cid;
  3641. }
  3642. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3643. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3644. ptr_cid_info = phba->cid_array_info[ulp_num];
  3645. ptr_cid_info->cid_alloc = 0;
  3646. ptr_cid_info->cid_free = 0;
  3647. }
  3648. }
  3649. return 0;
  3650. free_memory:
  3651. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3652. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3653. ptr_cid_info = phba->cid_array_info[ulp_num];
  3654. if (ptr_cid_info) {
  3655. kfree(ptr_cid_info->cid_array);
  3656. kfree(ptr_cid_info);
  3657. phba->cid_array_info[ulp_num] = NULL;
  3658. }
  3659. }
  3660. }
  3661. return ret;
  3662. }
  3663. static void hwi_enable_intr(struct beiscsi_hba *phba)
  3664. {
  3665. struct be_ctrl_info *ctrl = &phba->ctrl;
  3666. struct hwi_controller *phwi_ctrlr;
  3667. struct hwi_context_memory *phwi_context;
  3668. struct be_queue_info *eq;
  3669. u8 __iomem *addr;
  3670. u32 reg, i;
  3671. u32 enabled;
  3672. phwi_ctrlr = phba->phwi_ctrlr;
  3673. phwi_context = phwi_ctrlr->phwi_ctxt;
  3674. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  3675. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  3676. reg = ioread32(addr);
  3677. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3678. if (!enabled) {
  3679. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3680. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3681. "BM_%d : reg =x%08x addr=%p\n", reg, addr);
  3682. iowrite32(reg, addr);
  3683. }
  3684. if (!phba->pcidev->msix_enabled) {
  3685. eq = &phwi_context->be_eq[0].q;
  3686. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3687. "BM_%d : eq->id=%d\n", eq->id);
  3688. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3689. } else {
  3690. for (i = 0; i <= phba->num_cpus; i++) {
  3691. eq = &phwi_context->be_eq[i].q;
  3692. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3693. "BM_%d : eq->id=%d\n", eq->id);
  3694. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3695. }
  3696. }
  3697. }
  3698. static void hwi_disable_intr(struct beiscsi_hba *phba)
  3699. {
  3700. struct be_ctrl_info *ctrl = &phba->ctrl;
  3701. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  3702. u32 reg = ioread32(addr);
  3703. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3704. if (enabled) {
  3705. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3706. iowrite32(reg, addr);
  3707. } else
  3708. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  3709. "BM_%d : In hwi_disable_intr, Already Disabled\n");
  3710. }
  3711. static int beiscsi_init_port(struct beiscsi_hba *phba)
  3712. {
  3713. int ret;
  3714. ret = hwi_init_controller(phba);
  3715. if (ret < 0) {
  3716. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3717. "BM_%d : init controller failed\n");
  3718. return ret;
  3719. }
  3720. ret = beiscsi_init_sgl_handle(phba);
  3721. if (ret < 0) {
  3722. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3723. "BM_%d : init sgl handles failed\n");
  3724. goto cleanup_port;
  3725. }
  3726. ret = hba_setup_cid_tbls(phba);
  3727. if (ret < 0) {
  3728. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3729. "BM_%d : setup CID table failed\n");
  3730. kfree(phba->io_sgl_hndl_base);
  3731. kfree(phba->eh_sgl_hndl_base);
  3732. goto cleanup_port;
  3733. }
  3734. return ret;
  3735. cleanup_port:
  3736. hwi_cleanup_port(phba);
  3737. return ret;
  3738. }
  3739. static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
  3740. {
  3741. struct ulp_cid_info *ptr_cid_info = NULL;
  3742. int ulp_num;
  3743. kfree(phba->io_sgl_hndl_base);
  3744. kfree(phba->eh_sgl_hndl_base);
  3745. kfree(phba->ep_array);
  3746. kfree(phba->conn_table);
  3747. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3748. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3749. ptr_cid_info = phba->cid_array_info[ulp_num];
  3750. if (ptr_cid_info) {
  3751. kfree(ptr_cid_info->cid_array);
  3752. kfree(ptr_cid_info);
  3753. phba->cid_array_info[ulp_num] = NULL;
  3754. }
  3755. }
  3756. }
  3757. }
  3758. /**
  3759. * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
  3760. * @beiscsi_conn: ptr to the conn to be cleaned up
  3761. * @task: ptr to iscsi_task resource to be freed.
  3762. *
  3763. * Free driver mgmt resources binded to CXN.
  3764. **/
  3765. void
  3766. beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
  3767. struct iscsi_task *task)
  3768. {
  3769. struct beiscsi_io_task *io_task;
  3770. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3771. struct hwi_wrb_context *pwrb_context;
  3772. struct hwi_controller *phwi_ctrlr;
  3773. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3774. beiscsi_conn->beiscsi_conn_cid);
  3775. phwi_ctrlr = phba->phwi_ctrlr;
  3776. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3777. io_task = task->dd_data;
  3778. if (io_task->pwrb_handle) {
  3779. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3780. io_task->pwrb_handle = NULL;
  3781. }
  3782. if (io_task->psgl_handle) {
  3783. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3784. io_task->psgl_handle = NULL;
  3785. }
  3786. if (io_task->mtask_addr) {
  3787. pci_unmap_single(phba->pcidev,
  3788. io_task->mtask_addr,
  3789. io_task->mtask_data_count,
  3790. PCI_DMA_TODEVICE);
  3791. io_task->mtask_addr = 0;
  3792. }
  3793. }
  3794. /**
  3795. * beiscsi_cleanup_task()- Free driver resources of the task
  3796. * @task: ptr to the iscsi task
  3797. *
  3798. **/
  3799. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3800. {
  3801. struct beiscsi_io_task *io_task = task->dd_data;
  3802. struct iscsi_conn *conn = task->conn;
  3803. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3804. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3805. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3806. struct hwi_wrb_context *pwrb_context;
  3807. struct hwi_controller *phwi_ctrlr;
  3808. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3809. beiscsi_conn->beiscsi_conn_cid);
  3810. phwi_ctrlr = phba->phwi_ctrlr;
  3811. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3812. if (io_task->cmd_bhs) {
  3813. dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3814. io_task->bhs_pa.u.a64.address);
  3815. io_task->cmd_bhs = NULL;
  3816. task->hdr = NULL;
  3817. }
  3818. if (task->sc) {
  3819. if (io_task->pwrb_handle) {
  3820. free_wrb_handle(phba, pwrb_context,
  3821. io_task->pwrb_handle);
  3822. io_task->pwrb_handle = NULL;
  3823. }
  3824. if (io_task->psgl_handle) {
  3825. free_io_sgl_handle(phba, io_task->psgl_handle);
  3826. io_task->psgl_handle = NULL;
  3827. }
  3828. if (io_task->scsi_cmnd) {
  3829. if (io_task->num_sg)
  3830. scsi_dma_unmap(io_task->scsi_cmnd);
  3831. io_task->scsi_cmnd = NULL;
  3832. }
  3833. } else {
  3834. if (!beiscsi_conn->login_in_progress)
  3835. beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
  3836. }
  3837. }
  3838. void
  3839. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3840. struct beiscsi_offload_params *params)
  3841. {
  3842. struct wrb_handle *pwrb_handle;
  3843. struct hwi_wrb_context *pwrb_context = NULL;
  3844. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3845. struct iscsi_task *task = beiscsi_conn->task;
  3846. struct iscsi_session *session = task->conn->session;
  3847. u32 doorbell = 0;
  3848. /*
  3849. * We can always use 0 here because it is reserved by libiscsi for
  3850. * login/startup related tasks.
  3851. */
  3852. beiscsi_conn->login_in_progress = 0;
  3853. spin_lock_bh(&session->back_lock);
  3854. beiscsi_cleanup_task(task);
  3855. spin_unlock_bh(&session->back_lock);
  3856. pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
  3857. &pwrb_context);
  3858. /* Check for the adapter family */
  3859. if (is_chip_be2_be3r(phba))
  3860. beiscsi_offload_cxn_v0(params, pwrb_handle,
  3861. phba->init_mem,
  3862. pwrb_context);
  3863. else
  3864. beiscsi_offload_cxn_v2(params, pwrb_handle,
  3865. pwrb_context);
  3866. be_dws_le_to_cpu(pwrb_handle->pwrb,
  3867. sizeof(struct iscsi_target_context_update_wrb));
  3868. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3869. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3870. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3871. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3872. iowrite32(doorbell, phba->db_va +
  3873. beiscsi_conn->doorbell_offset);
  3874. /*
  3875. * There is no completion for CONTEXT_UPDATE. The completion of next
  3876. * WRB posted guarantees FW's processing and DMA'ing of it.
  3877. * Use beiscsi_put_wrb_handle to put it back in the pool which makes
  3878. * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
  3879. */
  3880. beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
  3881. phba->params.wrbs_per_cxn);
  3882. beiscsi_log(phba, KERN_INFO,
  3883. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3884. "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
  3885. pwrb_handle, pwrb_context->free_index,
  3886. pwrb_context->wrb_handles_available);
  3887. }
  3888. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3889. int *index, int *age)
  3890. {
  3891. *index = (int)itt;
  3892. if (age)
  3893. *age = conn->session->age;
  3894. }
  3895. /**
  3896. * beiscsi_alloc_pdu - allocates pdu and related resources
  3897. * @task: libiscsi task
  3898. * @opcode: opcode of pdu for task
  3899. *
  3900. * This is called with the session lock held. It will allocate
  3901. * the wrb and sgl if needed for the command. And it will prep
  3902. * the pdu's itt. beiscsi_parse_pdu will later translate
  3903. * the pdu itt to the libiscsi task itt.
  3904. */
  3905. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3906. {
  3907. struct beiscsi_io_task *io_task = task->dd_data;
  3908. struct iscsi_conn *conn = task->conn;
  3909. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3910. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3911. struct hwi_wrb_context *pwrb_context;
  3912. struct hwi_controller *phwi_ctrlr;
  3913. itt_t itt;
  3914. uint16_t cri_index = 0;
  3915. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3916. dma_addr_t paddr;
  3917. io_task->cmd_bhs = dma_pool_alloc(beiscsi_sess->bhs_pool,
  3918. GFP_ATOMIC, &paddr);
  3919. if (!io_task->cmd_bhs)
  3920. return -ENOMEM;
  3921. io_task->bhs_pa.u.a64.address = paddr;
  3922. io_task->libiscsi_itt = (itt_t)task->itt;
  3923. io_task->conn = beiscsi_conn;
  3924. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3925. task->hdr_max = sizeof(struct be_cmd_bhs);
  3926. io_task->psgl_handle = NULL;
  3927. io_task->pwrb_handle = NULL;
  3928. if (task->sc) {
  3929. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3930. if (!io_task->psgl_handle) {
  3931. beiscsi_log(phba, KERN_ERR,
  3932. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3933. "BM_%d : Alloc of IO_SGL_ICD Failed"
  3934. "for the CID : %d\n",
  3935. beiscsi_conn->beiscsi_conn_cid);
  3936. goto free_hndls;
  3937. }
  3938. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3939. beiscsi_conn->beiscsi_conn_cid,
  3940. &io_task->pwrb_context);
  3941. if (!io_task->pwrb_handle) {
  3942. beiscsi_log(phba, KERN_ERR,
  3943. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3944. "BM_%d : Alloc of WRB_HANDLE Failed"
  3945. "for the CID : %d\n",
  3946. beiscsi_conn->beiscsi_conn_cid);
  3947. goto free_io_hndls;
  3948. }
  3949. } else {
  3950. io_task->scsi_cmnd = NULL;
  3951. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3952. beiscsi_conn->task = task;
  3953. if (!beiscsi_conn->login_in_progress) {
  3954. io_task->psgl_handle = (struct sgl_handle *)
  3955. alloc_mgmt_sgl_handle(phba);
  3956. if (!io_task->psgl_handle) {
  3957. beiscsi_log(phba, KERN_ERR,
  3958. BEISCSI_LOG_IO |
  3959. BEISCSI_LOG_CONFIG,
  3960. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3961. "for the CID : %d\n",
  3962. beiscsi_conn->
  3963. beiscsi_conn_cid);
  3964. goto free_hndls;
  3965. }
  3966. beiscsi_conn->login_in_progress = 1;
  3967. beiscsi_conn->plogin_sgl_handle =
  3968. io_task->psgl_handle;
  3969. io_task->pwrb_handle =
  3970. alloc_wrb_handle(phba,
  3971. beiscsi_conn->beiscsi_conn_cid,
  3972. &io_task->pwrb_context);
  3973. if (!io_task->pwrb_handle) {
  3974. beiscsi_log(phba, KERN_ERR,
  3975. BEISCSI_LOG_IO |
  3976. BEISCSI_LOG_CONFIG,
  3977. "BM_%d : Alloc of WRB_HANDLE Failed"
  3978. "for the CID : %d\n",
  3979. beiscsi_conn->
  3980. beiscsi_conn_cid);
  3981. goto free_mgmt_hndls;
  3982. }
  3983. beiscsi_conn->plogin_wrb_handle =
  3984. io_task->pwrb_handle;
  3985. } else {
  3986. io_task->psgl_handle =
  3987. beiscsi_conn->plogin_sgl_handle;
  3988. io_task->pwrb_handle =
  3989. beiscsi_conn->plogin_wrb_handle;
  3990. }
  3991. } else {
  3992. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3993. if (!io_task->psgl_handle) {
  3994. beiscsi_log(phba, KERN_ERR,
  3995. BEISCSI_LOG_IO |
  3996. BEISCSI_LOG_CONFIG,
  3997. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3998. "for the CID : %d\n",
  3999. beiscsi_conn->
  4000. beiscsi_conn_cid);
  4001. goto free_hndls;
  4002. }
  4003. io_task->pwrb_handle =
  4004. alloc_wrb_handle(phba,
  4005. beiscsi_conn->beiscsi_conn_cid,
  4006. &io_task->pwrb_context);
  4007. if (!io_task->pwrb_handle) {
  4008. beiscsi_log(phba, KERN_ERR,
  4009. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  4010. "BM_%d : Alloc of WRB_HANDLE Failed"
  4011. "for the CID : %d\n",
  4012. beiscsi_conn->beiscsi_conn_cid);
  4013. goto free_mgmt_hndls;
  4014. }
  4015. }
  4016. }
  4017. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  4018. wrb_index << 16) | (unsigned int)
  4019. (io_task->psgl_handle->sgl_index));
  4020. io_task->pwrb_handle->pio_handle = task;
  4021. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  4022. return 0;
  4023. free_io_hndls:
  4024. free_io_sgl_handle(phba, io_task->psgl_handle);
  4025. goto free_hndls;
  4026. free_mgmt_hndls:
  4027. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  4028. io_task->psgl_handle = NULL;
  4029. free_hndls:
  4030. phwi_ctrlr = phba->phwi_ctrlr;
  4031. cri_index = BE_GET_CRI_FROM_CID(
  4032. beiscsi_conn->beiscsi_conn_cid);
  4033. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  4034. if (io_task->pwrb_handle)
  4035. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  4036. io_task->pwrb_handle = NULL;
  4037. dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  4038. io_task->bhs_pa.u.a64.address);
  4039. io_task->cmd_bhs = NULL;
  4040. return -ENOMEM;
  4041. }
  4042. static int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
  4043. unsigned int num_sg, unsigned int xferlen,
  4044. unsigned int writedir)
  4045. {
  4046. struct beiscsi_io_task *io_task = task->dd_data;
  4047. struct iscsi_conn *conn = task->conn;
  4048. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4049. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4050. struct iscsi_wrb *pwrb = NULL;
  4051. unsigned int doorbell = 0;
  4052. pwrb = io_task->pwrb_handle->pwrb;
  4053. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4054. if (writedir) {
  4055. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4056. INI_WR_CMD);
  4057. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
  4058. } else {
  4059. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4060. INI_RD_CMD);
  4061. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
  4062. }
  4063. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
  4064. type, pwrb);
  4065. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
  4066. cpu_to_be16(*(unsigned short *)
  4067. &io_task->cmd_bhs->iscsi_hdr.lun));
  4068. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
  4069. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4070. io_task->pwrb_handle->wrb_index);
  4071. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4072. be32_to_cpu(task->cmdsn));
  4073. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4074. io_task->psgl_handle->sgl_index);
  4075. hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
  4076. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4077. io_task->pwrb_handle->wrb_index);
  4078. if (io_task->pwrb_context->plast_wrb)
  4079. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4080. io_task->pwrb_context->plast_wrb,
  4081. io_task->pwrb_handle->wrb_index);
  4082. io_task->pwrb_context->plast_wrb = pwrb;
  4083. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4084. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4085. doorbell |= (io_task->pwrb_handle->wrb_index &
  4086. DB_DEF_PDU_WRB_INDEX_MASK) <<
  4087. DB_DEF_PDU_WRB_INDEX_SHIFT;
  4088. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4089. iowrite32(doorbell, phba->db_va +
  4090. beiscsi_conn->doorbell_offset);
  4091. return 0;
  4092. }
  4093. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  4094. unsigned int num_sg, unsigned int xferlen,
  4095. unsigned int writedir)
  4096. {
  4097. struct beiscsi_io_task *io_task = task->dd_data;
  4098. struct iscsi_conn *conn = task->conn;
  4099. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4100. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4101. struct iscsi_wrb *pwrb = NULL;
  4102. unsigned int doorbell = 0;
  4103. pwrb = io_task->pwrb_handle->pwrb;
  4104. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4105. if (writedir) {
  4106. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4107. INI_WR_CMD);
  4108. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  4109. } else {
  4110. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4111. INI_RD_CMD);
  4112. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  4113. }
  4114. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
  4115. type, pwrb);
  4116. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  4117. cpu_to_be16(*(unsigned short *)
  4118. &io_task->cmd_bhs->iscsi_hdr.lun));
  4119. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  4120. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4121. io_task->pwrb_handle->wrb_index);
  4122. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4123. be32_to_cpu(task->cmdsn));
  4124. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4125. io_task->psgl_handle->sgl_index);
  4126. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  4127. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4128. io_task->pwrb_handle->wrb_index);
  4129. if (io_task->pwrb_context->plast_wrb)
  4130. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4131. io_task->pwrb_context->plast_wrb,
  4132. io_task->pwrb_handle->wrb_index);
  4133. io_task->pwrb_context->plast_wrb = pwrb;
  4134. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4135. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4136. doorbell |= (io_task->pwrb_handle->wrb_index &
  4137. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4138. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4139. iowrite32(doorbell, phba->db_va +
  4140. beiscsi_conn->doorbell_offset);
  4141. return 0;
  4142. }
  4143. static int beiscsi_mtask(struct iscsi_task *task)
  4144. {
  4145. struct beiscsi_io_task *io_task = task->dd_data;
  4146. struct iscsi_conn *conn = task->conn;
  4147. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4148. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4149. struct iscsi_wrb *pwrb = NULL;
  4150. unsigned int doorbell = 0;
  4151. unsigned int cid;
  4152. unsigned int pwrb_typeoffset = 0;
  4153. int ret = 0;
  4154. cid = beiscsi_conn->beiscsi_conn_cid;
  4155. pwrb = io_task->pwrb_handle->pwrb;
  4156. if (is_chip_be2_be3r(phba)) {
  4157. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4158. be32_to_cpu(task->cmdsn));
  4159. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4160. io_task->pwrb_handle->wrb_index);
  4161. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4162. io_task->psgl_handle->sgl_index);
  4163. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  4164. task->data_count);
  4165. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4166. io_task->pwrb_handle->wrb_index);
  4167. if (io_task->pwrb_context->plast_wrb)
  4168. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4169. io_task->pwrb_context->plast_wrb,
  4170. io_task->pwrb_handle->wrb_index);
  4171. io_task->pwrb_context->plast_wrb = pwrb;
  4172. pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
  4173. } else {
  4174. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4175. be32_to_cpu(task->cmdsn));
  4176. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4177. io_task->pwrb_handle->wrb_index);
  4178. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4179. io_task->psgl_handle->sgl_index);
  4180. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
  4181. task->data_count);
  4182. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4183. io_task->pwrb_handle->wrb_index);
  4184. if (io_task->pwrb_context->plast_wrb)
  4185. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4186. io_task->pwrb_context->plast_wrb,
  4187. io_task->pwrb_handle->wrb_index);
  4188. io_task->pwrb_context->plast_wrb = pwrb;
  4189. pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
  4190. }
  4191. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  4192. case ISCSI_OP_LOGIN:
  4193. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  4194. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4195. ret = hwi_write_buffer(pwrb, task);
  4196. break;
  4197. case ISCSI_OP_NOOP_OUT:
  4198. if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
  4199. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4200. if (is_chip_be2_be3r(phba))
  4201. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4202. dmsg, pwrb, 1);
  4203. else
  4204. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4205. dmsg, pwrb, 1);
  4206. } else {
  4207. ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
  4208. if (is_chip_be2_be3r(phba))
  4209. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4210. dmsg, pwrb, 0);
  4211. else
  4212. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4213. dmsg, pwrb, 0);
  4214. }
  4215. ret = hwi_write_buffer(pwrb, task);
  4216. break;
  4217. case ISCSI_OP_TEXT:
  4218. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4219. ret = hwi_write_buffer(pwrb, task);
  4220. break;
  4221. case ISCSI_OP_SCSI_TMFUNC:
  4222. ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
  4223. ret = hwi_write_buffer(pwrb, task);
  4224. break;
  4225. case ISCSI_OP_LOGOUT:
  4226. ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
  4227. ret = hwi_write_buffer(pwrb, task);
  4228. break;
  4229. default:
  4230. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4231. "BM_%d : opcode =%d Not supported\n",
  4232. task->hdr->opcode & ISCSI_OPCODE_MASK);
  4233. return -EINVAL;
  4234. }
  4235. if (ret)
  4236. return ret;
  4237. /* Set the task type */
  4238. io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
  4239. AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
  4240. AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
  4241. doorbell |= cid & DB_WRB_POST_CID_MASK;
  4242. doorbell |= (io_task->pwrb_handle->wrb_index &
  4243. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4244. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4245. iowrite32(doorbell, phba->db_va +
  4246. beiscsi_conn->doorbell_offset);
  4247. return 0;
  4248. }
  4249. static int beiscsi_task_xmit(struct iscsi_task *task)
  4250. {
  4251. struct beiscsi_io_task *io_task = task->dd_data;
  4252. struct scsi_cmnd *sc = task->sc;
  4253. struct beiscsi_hba *phba;
  4254. struct scatterlist *sg;
  4255. int num_sg;
  4256. unsigned int writedir = 0, xferlen = 0;
  4257. phba = io_task->conn->phba;
  4258. /**
  4259. * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
  4260. * operational if FW still gets heartbeat from EP FW. Is management
  4261. * path really needed to continue further?
  4262. */
  4263. if (!beiscsi_hba_is_online(phba))
  4264. return -EIO;
  4265. if (!io_task->conn->login_in_progress)
  4266. task->hdr->exp_statsn = 0;
  4267. if (!sc)
  4268. return beiscsi_mtask(task);
  4269. io_task->scsi_cmnd = sc;
  4270. io_task->num_sg = 0;
  4271. num_sg = scsi_dma_map(sc);
  4272. if (num_sg < 0) {
  4273. beiscsi_log(phba, KERN_ERR,
  4274. BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
  4275. "BM_%d : scsi_dma_map Failed "
  4276. "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
  4277. be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
  4278. io_task->libiscsi_itt, scsi_bufflen(sc));
  4279. return num_sg;
  4280. }
  4281. /**
  4282. * For scsi cmd task, check num_sg before unmapping in cleanup_task.
  4283. * For management task, cleanup_task checks mtask_addr before unmapping.
  4284. */
  4285. io_task->num_sg = num_sg;
  4286. xferlen = scsi_bufflen(sc);
  4287. sg = scsi_sglist(sc);
  4288. if (sc->sc_data_direction == DMA_TO_DEVICE)
  4289. writedir = 1;
  4290. else
  4291. writedir = 0;
  4292. return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
  4293. }
  4294. /**
  4295. * beiscsi_bsg_request - handle bsg request from ISCSI transport
  4296. * @job: job to handle
  4297. */
  4298. static int beiscsi_bsg_request(struct bsg_job *job)
  4299. {
  4300. struct Scsi_Host *shost;
  4301. struct beiscsi_hba *phba;
  4302. struct iscsi_bsg_request *bsg_req = job->request;
  4303. int rc = -EINVAL;
  4304. unsigned int tag;
  4305. struct be_dma_mem nonemb_cmd;
  4306. struct be_cmd_resp_hdr *resp;
  4307. struct iscsi_bsg_reply *bsg_reply = job->reply;
  4308. unsigned short status, extd_status;
  4309. shost = iscsi_job_to_shost(job);
  4310. phba = iscsi_host_priv(shost);
  4311. if (!beiscsi_hba_is_online(phba)) {
  4312. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  4313. "BM_%d : HBA in error 0x%lx\n", phba->state);
  4314. return -ENXIO;
  4315. }
  4316. switch (bsg_req->msgcode) {
  4317. case ISCSI_BSG_HST_VENDOR:
  4318. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  4319. job->request_payload.payload_len,
  4320. &nonemb_cmd.dma);
  4321. if (nonemb_cmd.va == NULL) {
  4322. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4323. "BM_%d : Failed to allocate memory for "
  4324. "beiscsi_bsg_request\n");
  4325. return -ENOMEM;
  4326. }
  4327. tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
  4328. &nonemb_cmd);
  4329. if (!tag) {
  4330. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4331. "BM_%d : MBX Tag Allocation Failed\n");
  4332. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4333. nonemb_cmd.va, nonemb_cmd.dma);
  4334. return -EAGAIN;
  4335. }
  4336. rc = wait_event_interruptible_timeout(
  4337. phba->ctrl.mcc_wait[tag],
  4338. phba->ctrl.mcc_tag_status[tag],
  4339. msecs_to_jiffies(
  4340. BEISCSI_HOST_MBX_TIMEOUT));
  4341. if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4342. clear_bit(MCC_TAG_STATE_RUNNING,
  4343. &phba->ctrl.ptag_state[tag].tag_state);
  4344. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4345. nonemb_cmd.va, nonemb_cmd.dma);
  4346. return -EIO;
  4347. }
  4348. extd_status = (phba->ctrl.mcc_tag_status[tag] &
  4349. CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
  4350. status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
  4351. free_mcc_wrb(&phba->ctrl, tag);
  4352. resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
  4353. sg_copy_from_buffer(job->reply_payload.sg_list,
  4354. job->reply_payload.sg_cnt,
  4355. nonemb_cmd.va, (resp->response_length
  4356. + sizeof(*resp)));
  4357. bsg_reply->reply_payload_rcv_len = resp->response_length;
  4358. bsg_reply->result = status;
  4359. bsg_job_done(job, bsg_reply->result,
  4360. bsg_reply->reply_payload_rcv_len);
  4361. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4362. nonemb_cmd.va, nonemb_cmd.dma);
  4363. if (status || extd_status) {
  4364. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4365. "BM_%d : MBX Cmd Failed"
  4366. " status = %d extd_status = %d\n",
  4367. status, extd_status);
  4368. return -EIO;
  4369. } else {
  4370. rc = 0;
  4371. }
  4372. break;
  4373. default:
  4374. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4375. "BM_%d : Unsupported bsg command: 0x%x\n",
  4376. bsg_req->msgcode);
  4377. break;
  4378. }
  4379. return rc;
  4380. }
  4381. static void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
  4382. {
  4383. /* Set the logging parameter */
  4384. beiscsi_log_enable_init(phba, beiscsi_log_enable);
  4385. }
  4386. void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
  4387. {
  4388. if (phba->boot_struct.boot_kset)
  4389. return;
  4390. /* skip if boot work is already in progress */
  4391. if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
  4392. return;
  4393. phba->boot_struct.retry = 3;
  4394. phba->boot_struct.tag = 0;
  4395. phba->boot_struct.s_handle = s_handle;
  4396. phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
  4397. schedule_work(&phba->boot_work);
  4398. }
  4399. /**
  4400. * Boot flag info for iscsi-utilities
  4401. * Bit 0 Block valid flag
  4402. * Bit 1 Firmware booting selected
  4403. */
  4404. #define BEISCSI_SYSFS_ISCSI_BOOT_FLAGS 3
  4405. static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
  4406. {
  4407. struct beiscsi_hba *phba = data;
  4408. struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
  4409. struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
  4410. char *str = buf;
  4411. int rc = -EPERM;
  4412. switch (type) {
  4413. case ISCSI_BOOT_TGT_NAME:
  4414. rc = sprintf(buf, "%.*s\n",
  4415. (int)strlen(boot_sess->target_name),
  4416. (char *)&boot_sess->target_name);
  4417. break;
  4418. case ISCSI_BOOT_TGT_IP_ADDR:
  4419. if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
  4420. rc = sprintf(buf, "%pI4\n",
  4421. (char *)&boot_conn->dest_ipaddr.addr);
  4422. else
  4423. rc = sprintf(str, "%pI6\n",
  4424. (char *)&boot_conn->dest_ipaddr.addr);
  4425. break;
  4426. case ISCSI_BOOT_TGT_PORT:
  4427. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  4428. break;
  4429. case ISCSI_BOOT_TGT_CHAP_NAME:
  4430. rc = sprintf(str, "%.*s\n",
  4431. boot_conn->negotiated_login_options.auth_data.chap.
  4432. target_chap_name_length,
  4433. (char *)&boot_conn->negotiated_login_options.
  4434. auth_data.chap.target_chap_name);
  4435. break;
  4436. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4437. rc = sprintf(str, "%.*s\n",
  4438. boot_conn->negotiated_login_options.auth_data.chap.
  4439. target_secret_length,
  4440. (char *)&boot_conn->negotiated_login_options.
  4441. auth_data.chap.target_secret);
  4442. break;
  4443. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4444. rc = sprintf(str, "%.*s\n",
  4445. boot_conn->negotiated_login_options.auth_data.chap.
  4446. intr_chap_name_length,
  4447. (char *)&boot_conn->negotiated_login_options.
  4448. auth_data.chap.intr_chap_name);
  4449. break;
  4450. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4451. rc = sprintf(str, "%.*s\n",
  4452. boot_conn->negotiated_login_options.auth_data.chap.
  4453. intr_secret_length,
  4454. (char *)&boot_conn->negotiated_login_options.
  4455. auth_data.chap.intr_secret);
  4456. break;
  4457. case ISCSI_BOOT_TGT_FLAGS:
  4458. rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
  4459. break;
  4460. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4461. rc = sprintf(str, "0\n");
  4462. break;
  4463. }
  4464. return rc;
  4465. }
  4466. static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
  4467. {
  4468. struct beiscsi_hba *phba = data;
  4469. char *str = buf;
  4470. int rc = -EPERM;
  4471. switch (type) {
  4472. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4473. rc = sprintf(str, "%s\n",
  4474. phba->boot_struct.boot_sess.initiator_iscsiname);
  4475. break;
  4476. }
  4477. return rc;
  4478. }
  4479. static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
  4480. {
  4481. struct beiscsi_hba *phba = data;
  4482. char *str = buf;
  4483. int rc = -EPERM;
  4484. switch (type) {
  4485. case ISCSI_BOOT_ETH_FLAGS:
  4486. rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
  4487. break;
  4488. case ISCSI_BOOT_ETH_INDEX:
  4489. rc = sprintf(str, "0\n");
  4490. break;
  4491. case ISCSI_BOOT_ETH_MAC:
  4492. rc = beiscsi_get_macaddr(str, phba);
  4493. break;
  4494. }
  4495. return rc;
  4496. }
  4497. static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
  4498. {
  4499. umode_t rc = 0;
  4500. switch (type) {
  4501. case ISCSI_BOOT_TGT_NAME:
  4502. case ISCSI_BOOT_TGT_IP_ADDR:
  4503. case ISCSI_BOOT_TGT_PORT:
  4504. case ISCSI_BOOT_TGT_CHAP_NAME:
  4505. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4506. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4507. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4508. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4509. case ISCSI_BOOT_TGT_FLAGS:
  4510. rc = S_IRUGO;
  4511. break;
  4512. }
  4513. return rc;
  4514. }
  4515. static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
  4516. {
  4517. umode_t rc = 0;
  4518. switch (type) {
  4519. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4520. rc = S_IRUGO;
  4521. break;
  4522. }
  4523. return rc;
  4524. }
  4525. static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
  4526. {
  4527. umode_t rc = 0;
  4528. switch (type) {
  4529. case ISCSI_BOOT_ETH_FLAGS:
  4530. case ISCSI_BOOT_ETH_MAC:
  4531. case ISCSI_BOOT_ETH_INDEX:
  4532. rc = S_IRUGO;
  4533. break;
  4534. }
  4535. return rc;
  4536. }
  4537. static void beiscsi_boot_kobj_release(void *data)
  4538. {
  4539. struct beiscsi_hba *phba = data;
  4540. scsi_host_put(phba->shost);
  4541. }
  4542. static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
  4543. {
  4544. struct boot_struct *bs = &phba->boot_struct;
  4545. struct iscsi_boot_kobj *boot_kobj;
  4546. if (bs->boot_kset) {
  4547. __beiscsi_log(phba, KERN_ERR,
  4548. "BM_%d: boot_kset already created\n");
  4549. return 0;
  4550. }
  4551. bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
  4552. if (!bs->boot_kset) {
  4553. __beiscsi_log(phba, KERN_ERR,
  4554. "BM_%d: boot_kset alloc failed\n");
  4555. return -ENOMEM;
  4556. }
  4557. /* get shost ref because the show function will refer phba */
  4558. if (!scsi_host_get(phba->shost))
  4559. goto free_kset;
  4560. boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
  4561. beiscsi_show_boot_tgt_info,
  4562. beiscsi_tgt_get_attr_visibility,
  4563. beiscsi_boot_kobj_release);
  4564. if (!boot_kobj)
  4565. goto put_shost;
  4566. if (!scsi_host_get(phba->shost))
  4567. goto free_kset;
  4568. boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
  4569. beiscsi_show_boot_ini_info,
  4570. beiscsi_ini_get_attr_visibility,
  4571. beiscsi_boot_kobj_release);
  4572. if (!boot_kobj)
  4573. goto put_shost;
  4574. if (!scsi_host_get(phba->shost))
  4575. goto free_kset;
  4576. boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
  4577. beiscsi_show_boot_eth_info,
  4578. beiscsi_eth_get_attr_visibility,
  4579. beiscsi_boot_kobj_release);
  4580. if (!boot_kobj)
  4581. goto put_shost;
  4582. return 0;
  4583. put_shost:
  4584. scsi_host_put(phba->shost);
  4585. free_kset:
  4586. iscsi_boot_destroy_kset(bs->boot_kset);
  4587. bs->boot_kset = NULL;
  4588. return -ENOMEM;
  4589. }
  4590. static void beiscsi_boot_work(struct work_struct *work)
  4591. {
  4592. struct beiscsi_hba *phba =
  4593. container_of(work, struct beiscsi_hba, boot_work);
  4594. struct boot_struct *bs = &phba->boot_struct;
  4595. unsigned int tag = 0;
  4596. if (!beiscsi_hba_is_online(phba))
  4597. return;
  4598. beiscsi_log(phba, KERN_INFO,
  4599. BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
  4600. "BM_%d : %s action %d\n",
  4601. __func__, phba->boot_struct.action);
  4602. switch (phba->boot_struct.action) {
  4603. case BEISCSI_BOOT_REOPEN_SESS:
  4604. tag = beiscsi_boot_reopen_sess(phba);
  4605. break;
  4606. case BEISCSI_BOOT_GET_SHANDLE:
  4607. tag = __beiscsi_boot_get_shandle(phba, 1);
  4608. break;
  4609. case BEISCSI_BOOT_GET_SINFO:
  4610. tag = beiscsi_boot_get_sinfo(phba);
  4611. break;
  4612. case BEISCSI_BOOT_LOGOUT_SESS:
  4613. tag = beiscsi_boot_logout_sess(phba);
  4614. break;
  4615. case BEISCSI_BOOT_CREATE_KSET:
  4616. beiscsi_boot_create_kset(phba);
  4617. /**
  4618. * updated boot_kset is made visible to all before
  4619. * ending the boot work.
  4620. */
  4621. mb();
  4622. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4623. return;
  4624. }
  4625. if (!tag) {
  4626. if (bs->retry--)
  4627. schedule_work(&phba->boot_work);
  4628. else
  4629. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4630. }
  4631. }
  4632. static void beiscsi_eqd_update_work(struct work_struct *work)
  4633. {
  4634. struct hwi_context_memory *phwi_context;
  4635. struct be_set_eqd set_eqd[MAX_CPUS];
  4636. struct hwi_controller *phwi_ctrlr;
  4637. struct be_eq_obj *pbe_eq;
  4638. struct beiscsi_hba *phba;
  4639. unsigned int pps, delta;
  4640. struct be_aic_obj *aic;
  4641. int eqd, i, num = 0;
  4642. unsigned long now;
  4643. phba = container_of(work, struct beiscsi_hba, eqd_update.work);
  4644. if (!beiscsi_hba_is_online(phba))
  4645. return;
  4646. phwi_ctrlr = phba->phwi_ctrlr;
  4647. phwi_context = phwi_ctrlr->phwi_ctxt;
  4648. for (i = 0; i <= phba->num_cpus; i++) {
  4649. aic = &phba->aic_obj[i];
  4650. pbe_eq = &phwi_context->be_eq[i];
  4651. now = jiffies;
  4652. if (!aic->jiffies || time_before(now, aic->jiffies) ||
  4653. pbe_eq->cq_count < aic->eq_prev) {
  4654. aic->jiffies = now;
  4655. aic->eq_prev = pbe_eq->cq_count;
  4656. continue;
  4657. }
  4658. delta = jiffies_to_msecs(now - aic->jiffies);
  4659. pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
  4660. eqd = (pps / 1500) << 2;
  4661. if (eqd < 8)
  4662. eqd = 0;
  4663. eqd = min_t(u32, eqd, BEISCSI_EQ_DELAY_MAX);
  4664. eqd = max_t(u32, eqd, BEISCSI_EQ_DELAY_MIN);
  4665. aic->jiffies = now;
  4666. aic->eq_prev = pbe_eq->cq_count;
  4667. if (eqd != aic->prev_eqd) {
  4668. set_eqd[num].delay_multiplier = (eqd * 65)/100;
  4669. set_eqd[num].eq_id = pbe_eq->q.id;
  4670. aic->prev_eqd = eqd;
  4671. num++;
  4672. }
  4673. }
  4674. if (num)
  4675. /* completion of this is ignored */
  4676. beiscsi_modify_eq_delay(phba, set_eqd, num);
  4677. schedule_delayed_work(&phba->eqd_update,
  4678. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4679. }
  4680. static void beiscsi_hw_tpe_check(struct timer_list *t)
  4681. {
  4682. struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
  4683. u32 wait;
  4684. /* if not TPE, do nothing */
  4685. if (!beiscsi_detect_tpe(phba))
  4686. return;
  4687. /* wait default 4000ms before recovering */
  4688. wait = 4000;
  4689. if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
  4690. wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
  4691. queue_delayed_work(phba->wq, &phba->recover_port,
  4692. msecs_to_jiffies(wait));
  4693. }
  4694. static void beiscsi_hw_health_check(struct timer_list *t)
  4695. {
  4696. struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
  4697. beiscsi_detect_ue(phba);
  4698. if (beiscsi_detect_ue(phba)) {
  4699. __beiscsi_log(phba, KERN_ERR,
  4700. "BM_%d : port in error: %lx\n", phba->state);
  4701. /* sessions are no longer valid, so first fail the sessions */
  4702. queue_work(phba->wq, &phba->sess_work);
  4703. /* detect UER supported */
  4704. if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
  4705. return;
  4706. /* modify this timer to check TPE */
  4707. phba->hw_check.function = beiscsi_hw_tpe_check;
  4708. }
  4709. mod_timer(&phba->hw_check,
  4710. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4711. }
  4712. /*
  4713. * beiscsi_enable_port()- Enables the disabled port.
  4714. * Only port resources freed in disable function are reallocated.
  4715. * This is called in HBA error handling path.
  4716. *
  4717. * @phba: Instance of driver private structure
  4718. *
  4719. **/
  4720. static int beiscsi_enable_port(struct beiscsi_hba *phba)
  4721. {
  4722. struct hwi_context_memory *phwi_context;
  4723. struct hwi_controller *phwi_ctrlr;
  4724. struct be_eq_obj *pbe_eq;
  4725. int ret, i;
  4726. if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4727. __beiscsi_log(phba, KERN_ERR,
  4728. "BM_%d : %s : port is online %lx\n",
  4729. __func__, phba->state);
  4730. return 0;
  4731. }
  4732. ret = beiscsi_init_sliport(phba);
  4733. if (ret)
  4734. return ret;
  4735. be2iscsi_enable_msix(phba);
  4736. beiscsi_get_params(phba);
  4737. beiscsi_set_host_data(phba);
  4738. /* Re-enable UER. If different TPE occurs then it is recoverable. */
  4739. beiscsi_set_uer_feature(phba);
  4740. phba->shost->max_id = phba->params.cxns_per_ctrl;
  4741. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4742. ret = beiscsi_init_port(phba);
  4743. if (ret < 0) {
  4744. __beiscsi_log(phba, KERN_ERR,
  4745. "BM_%d : init port failed\n");
  4746. goto disable_msix;
  4747. }
  4748. for (i = 0; i < MAX_MCC_CMD; i++) {
  4749. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  4750. phba->ctrl.mcc_tag[i] = i + 1;
  4751. phba->ctrl.mcc_tag_status[i + 1] = 0;
  4752. phba->ctrl.mcc_tag_available++;
  4753. }
  4754. phwi_ctrlr = phba->phwi_ctrlr;
  4755. phwi_context = phwi_ctrlr->phwi_ctxt;
  4756. for (i = 0; i < phba->num_cpus; i++) {
  4757. pbe_eq = &phwi_context->be_eq[i];
  4758. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  4759. }
  4760. i = (phba->pcidev->msix_enabled) ? i : 0;
  4761. /* Work item for MCC handling */
  4762. pbe_eq = &phwi_context->be_eq[i];
  4763. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  4764. ret = beiscsi_init_irqs(phba);
  4765. if (ret < 0) {
  4766. __beiscsi_log(phba, KERN_ERR,
  4767. "BM_%d : setup IRQs failed %d\n", ret);
  4768. goto cleanup_port;
  4769. }
  4770. hwi_enable_intr(phba);
  4771. /* port operational: clear all error bits */
  4772. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  4773. __beiscsi_log(phba, KERN_INFO,
  4774. "BM_%d : port online: 0x%lx\n", phba->state);
  4775. /* start hw_check timer and eqd_update work */
  4776. schedule_delayed_work(&phba->eqd_update,
  4777. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4778. /**
  4779. * Timer function gets modified for TPE detection.
  4780. * Always reinit to do health check first.
  4781. */
  4782. phba->hw_check.function = beiscsi_hw_health_check;
  4783. mod_timer(&phba->hw_check,
  4784. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4785. return 0;
  4786. cleanup_port:
  4787. for (i = 0; i < phba->num_cpus; i++) {
  4788. pbe_eq = &phwi_context->be_eq[i];
  4789. irq_poll_disable(&pbe_eq->iopoll);
  4790. }
  4791. hwi_cleanup_port(phba);
  4792. disable_msix:
  4793. pci_free_irq_vectors(phba->pcidev);
  4794. return ret;
  4795. }
  4796. /*
  4797. * beiscsi_disable_port()- Disable port and cleanup driver resources.
  4798. * This is called in HBA error handling and driver removal.
  4799. * @phba: Instance Priv structure
  4800. * @unload: indicate driver is unloading
  4801. *
  4802. * Free the OS and HW resources held by the driver
  4803. **/
  4804. static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
  4805. {
  4806. struct hwi_context_memory *phwi_context;
  4807. struct hwi_controller *phwi_ctrlr;
  4808. struct be_eq_obj *pbe_eq;
  4809. unsigned int i;
  4810. if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
  4811. return;
  4812. phwi_ctrlr = phba->phwi_ctrlr;
  4813. phwi_context = phwi_ctrlr->phwi_ctxt;
  4814. hwi_disable_intr(phba);
  4815. beiscsi_free_irqs(phba);
  4816. pci_free_irq_vectors(phba->pcidev);
  4817. for (i = 0; i < phba->num_cpus; i++) {
  4818. pbe_eq = &phwi_context->be_eq[i];
  4819. irq_poll_disable(&pbe_eq->iopoll);
  4820. }
  4821. cancel_delayed_work_sync(&phba->eqd_update);
  4822. cancel_work_sync(&phba->boot_work);
  4823. /* WQ might be running cancel queued mcc_work if we are not exiting */
  4824. if (!unload && beiscsi_hba_in_error(phba)) {
  4825. pbe_eq = &phwi_context->be_eq[i];
  4826. cancel_work_sync(&pbe_eq->mcc_work);
  4827. }
  4828. hwi_cleanup_port(phba);
  4829. beiscsi_cleanup_port(phba);
  4830. }
  4831. static void beiscsi_sess_work(struct work_struct *work)
  4832. {
  4833. struct beiscsi_hba *phba;
  4834. phba = container_of(work, struct beiscsi_hba, sess_work);
  4835. /*
  4836. * This work gets scheduled only in case of HBA error.
  4837. * Old sessions are gone so need to be re-established.
  4838. * iscsi_session_failure needs process context hence this work.
  4839. */
  4840. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4841. }
  4842. static void beiscsi_recover_port(struct work_struct *work)
  4843. {
  4844. struct beiscsi_hba *phba;
  4845. phba = container_of(work, struct beiscsi_hba, recover_port.work);
  4846. beiscsi_disable_port(phba, 0);
  4847. beiscsi_enable_port(phba);
  4848. }
  4849. static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
  4850. pci_channel_state_t state)
  4851. {
  4852. struct beiscsi_hba *phba = NULL;
  4853. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4854. set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
  4855. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4856. "BM_%d : EEH error detected\n");
  4857. /* first stop UE detection when PCI error detected */
  4858. del_timer_sync(&phba->hw_check);
  4859. cancel_delayed_work_sync(&phba->recover_port);
  4860. /* sessions are no longer valid, so first fail the sessions */
  4861. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4862. beiscsi_disable_port(phba, 0);
  4863. if (state == pci_channel_io_perm_failure) {
  4864. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4865. "BM_%d : EEH : State PERM Failure");
  4866. return PCI_ERS_RESULT_DISCONNECT;
  4867. }
  4868. pci_disable_device(pdev);
  4869. /* The error could cause the FW to trigger a flash debug dump.
  4870. * Resetting the card while flash dump is in progress
  4871. * can cause it not to recover; wait for it to finish.
  4872. * Wait only for first function as it is needed only once per
  4873. * adapter.
  4874. **/
  4875. if (pdev->devfn == 0)
  4876. ssleep(30);
  4877. return PCI_ERS_RESULT_NEED_RESET;
  4878. }
  4879. static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
  4880. {
  4881. struct beiscsi_hba *phba = NULL;
  4882. int status = 0;
  4883. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4884. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4885. "BM_%d : EEH Reset\n");
  4886. status = pci_enable_device(pdev);
  4887. if (status)
  4888. return PCI_ERS_RESULT_DISCONNECT;
  4889. pci_set_master(pdev);
  4890. pci_set_power_state(pdev, PCI_D0);
  4891. pci_restore_state(pdev);
  4892. status = beiscsi_check_fw_rdy(phba);
  4893. if (status) {
  4894. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4895. "BM_%d : EEH Reset Completed\n");
  4896. } else {
  4897. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4898. "BM_%d : EEH Reset Completion Failure\n");
  4899. return PCI_ERS_RESULT_DISCONNECT;
  4900. }
  4901. pci_cleanup_aer_uncorrect_error_status(pdev);
  4902. return PCI_ERS_RESULT_RECOVERED;
  4903. }
  4904. static void beiscsi_eeh_resume(struct pci_dev *pdev)
  4905. {
  4906. struct beiscsi_hba *phba;
  4907. int ret;
  4908. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4909. pci_save_state(pdev);
  4910. ret = beiscsi_enable_port(phba);
  4911. if (ret)
  4912. __beiscsi_log(phba, KERN_ERR,
  4913. "BM_%d : AER EEH resume failed\n");
  4914. }
  4915. static int beiscsi_dev_probe(struct pci_dev *pcidev,
  4916. const struct pci_device_id *id)
  4917. {
  4918. struct hwi_context_memory *phwi_context;
  4919. struct hwi_controller *phwi_ctrlr;
  4920. struct beiscsi_hba *phba = NULL;
  4921. struct be_eq_obj *pbe_eq;
  4922. unsigned int s_handle;
  4923. char wq_name[20];
  4924. int ret, i;
  4925. ret = beiscsi_enable_pci(pcidev);
  4926. if (ret < 0) {
  4927. dev_err(&pcidev->dev,
  4928. "beiscsi_dev_probe - Failed to enable pci device\n");
  4929. return ret;
  4930. }
  4931. phba = beiscsi_hba_alloc(pcidev);
  4932. if (!phba) {
  4933. dev_err(&pcidev->dev,
  4934. "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
  4935. ret = -ENOMEM;
  4936. goto disable_pci;
  4937. }
  4938. /* Enable EEH reporting */
  4939. ret = pci_enable_pcie_error_reporting(pcidev);
  4940. if (ret)
  4941. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4942. "BM_%d : PCIe Error Reporting "
  4943. "Enabling Failed\n");
  4944. pci_save_state(pcidev);
  4945. /* Initialize Driver configuration Paramters */
  4946. beiscsi_hba_attrs_init(phba);
  4947. phba->mac_addr_set = false;
  4948. switch (pcidev->device) {
  4949. case BE_DEVICE_ID1:
  4950. case OC_DEVICE_ID1:
  4951. case OC_DEVICE_ID2:
  4952. phba->generation = BE_GEN2;
  4953. phba->iotask_fn = beiscsi_iotask;
  4954. dev_warn(&pcidev->dev,
  4955. "Obsolete/Unsupported BE2 Adapter Family\n");
  4956. break;
  4957. case BE_DEVICE_ID2:
  4958. case OC_DEVICE_ID3:
  4959. phba->generation = BE_GEN3;
  4960. phba->iotask_fn = beiscsi_iotask;
  4961. break;
  4962. case OC_SKH_ID1:
  4963. phba->generation = BE_GEN4;
  4964. phba->iotask_fn = beiscsi_iotask_v2;
  4965. break;
  4966. default:
  4967. phba->generation = 0;
  4968. }
  4969. ret = be_ctrl_init(phba, pcidev);
  4970. if (ret) {
  4971. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4972. "BM_%d : be_ctrl_init failed\n");
  4973. goto free_hba;
  4974. }
  4975. ret = beiscsi_init_sliport(phba);
  4976. if (ret)
  4977. goto free_hba;
  4978. spin_lock_init(&phba->io_sgl_lock);
  4979. spin_lock_init(&phba->mgmt_sgl_lock);
  4980. spin_lock_init(&phba->async_pdu_lock);
  4981. ret = beiscsi_get_fw_config(&phba->ctrl, phba);
  4982. if (ret != 0) {
  4983. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4984. "BM_%d : Error getting fw config\n");
  4985. goto free_port;
  4986. }
  4987. beiscsi_get_port_name(&phba->ctrl, phba);
  4988. beiscsi_get_params(phba);
  4989. beiscsi_set_host_data(phba);
  4990. beiscsi_set_uer_feature(phba);
  4991. be2iscsi_enable_msix(phba);
  4992. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  4993. "BM_%d : num_cpus = %d\n",
  4994. phba->num_cpus);
  4995. phba->shost->max_id = phba->params.cxns_per_ctrl;
  4996. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4997. ret = beiscsi_get_memory(phba);
  4998. if (ret < 0) {
  4999. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5000. "BM_%d : alloc host mem failed\n");
  5001. goto free_port;
  5002. }
  5003. ret = beiscsi_init_port(phba);
  5004. if (ret < 0) {
  5005. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5006. "BM_%d : init port failed\n");
  5007. beiscsi_free_mem(phba);
  5008. goto free_port;
  5009. }
  5010. for (i = 0; i < MAX_MCC_CMD; i++) {
  5011. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  5012. phba->ctrl.mcc_tag[i] = i + 1;
  5013. phba->ctrl.mcc_tag_status[i + 1] = 0;
  5014. phba->ctrl.mcc_tag_available++;
  5015. memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
  5016. sizeof(struct be_dma_mem));
  5017. }
  5018. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  5019. snprintf(wq_name, sizeof(wq_name), "beiscsi_%02x_wq",
  5020. phba->shost->host_no);
  5021. phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, wq_name);
  5022. if (!phba->wq) {
  5023. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5024. "BM_%d : beiscsi_dev_probe-"
  5025. "Failed to allocate work queue\n");
  5026. ret = -ENOMEM;
  5027. goto free_twq;
  5028. }
  5029. INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
  5030. phwi_ctrlr = phba->phwi_ctrlr;
  5031. phwi_context = phwi_ctrlr->phwi_ctxt;
  5032. for (i = 0; i < phba->num_cpus; i++) {
  5033. pbe_eq = &phwi_context->be_eq[i];
  5034. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  5035. }
  5036. i = (phba->pcidev->msix_enabled) ? i : 0;
  5037. /* Work item for MCC handling */
  5038. pbe_eq = &phwi_context->be_eq[i];
  5039. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  5040. ret = beiscsi_init_irqs(phba);
  5041. if (ret < 0) {
  5042. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5043. "BM_%d : beiscsi_dev_probe-"
  5044. "Failed to beiscsi_init_irqs\n");
  5045. goto disable_iopoll;
  5046. }
  5047. hwi_enable_intr(phba);
  5048. ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
  5049. if (ret)
  5050. goto free_irqs;
  5051. /* set online bit after port is operational */
  5052. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  5053. __beiscsi_log(phba, KERN_INFO,
  5054. "BM_%d : port online: 0x%lx\n", phba->state);
  5055. INIT_WORK(&phba->boot_work, beiscsi_boot_work);
  5056. ret = beiscsi_boot_get_shandle(phba, &s_handle);
  5057. if (ret > 0) {
  5058. beiscsi_start_boot_work(phba, s_handle);
  5059. /**
  5060. * Set this bit after starting the work to let
  5061. * probe handle it first.
  5062. * ASYNC event can too schedule this work.
  5063. */
  5064. set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
  5065. }
  5066. beiscsi_iface_create_default(phba);
  5067. schedule_delayed_work(&phba->eqd_update,
  5068. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  5069. INIT_WORK(&phba->sess_work, beiscsi_sess_work);
  5070. INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
  5071. /**
  5072. * Start UE detection here. UE before this will cause stall in probe
  5073. * and eventually fail the probe.
  5074. */
  5075. timer_setup(&phba->hw_check, beiscsi_hw_health_check, 0);
  5076. mod_timer(&phba->hw_check,
  5077. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  5078. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  5079. "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
  5080. return 0;
  5081. free_irqs:
  5082. hwi_disable_intr(phba);
  5083. beiscsi_free_irqs(phba);
  5084. disable_iopoll:
  5085. for (i = 0; i < phba->num_cpus; i++) {
  5086. pbe_eq = &phwi_context->be_eq[i];
  5087. irq_poll_disable(&pbe_eq->iopoll);
  5088. }
  5089. destroy_workqueue(phba->wq);
  5090. free_twq:
  5091. hwi_cleanup_port(phba);
  5092. beiscsi_cleanup_port(phba);
  5093. beiscsi_free_mem(phba);
  5094. free_port:
  5095. pci_free_consistent(phba->pcidev,
  5096. phba->ctrl.mbox_mem_alloced.size,
  5097. phba->ctrl.mbox_mem_alloced.va,
  5098. phba->ctrl.mbox_mem_alloced.dma);
  5099. beiscsi_unmap_pci_function(phba);
  5100. free_hba:
  5101. pci_disable_msix(phba->pcidev);
  5102. pci_dev_put(phba->pcidev);
  5103. iscsi_host_free(phba->shost);
  5104. pci_set_drvdata(pcidev, NULL);
  5105. disable_pci:
  5106. pci_release_regions(pcidev);
  5107. pci_disable_device(pcidev);
  5108. return ret;
  5109. }
  5110. static void beiscsi_remove(struct pci_dev *pcidev)
  5111. {
  5112. struct beiscsi_hba *phba = NULL;
  5113. phba = pci_get_drvdata(pcidev);
  5114. if (!phba) {
  5115. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  5116. return;
  5117. }
  5118. /* first stop UE detection before unloading */
  5119. del_timer_sync(&phba->hw_check);
  5120. cancel_delayed_work_sync(&phba->recover_port);
  5121. cancel_work_sync(&phba->sess_work);
  5122. beiscsi_iface_destroy_default(phba);
  5123. iscsi_host_remove(phba->shost);
  5124. beiscsi_disable_port(phba, 1);
  5125. /* after cancelling boot_work */
  5126. iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
  5127. /* free all resources */
  5128. destroy_workqueue(phba->wq);
  5129. beiscsi_free_mem(phba);
  5130. /* ctrl uninit */
  5131. beiscsi_unmap_pci_function(phba);
  5132. pci_free_consistent(phba->pcidev,
  5133. phba->ctrl.mbox_mem_alloced.size,
  5134. phba->ctrl.mbox_mem_alloced.va,
  5135. phba->ctrl.mbox_mem_alloced.dma);
  5136. pci_dev_put(phba->pcidev);
  5137. iscsi_host_free(phba->shost);
  5138. pci_disable_pcie_error_reporting(pcidev);
  5139. pci_set_drvdata(pcidev, NULL);
  5140. pci_release_regions(pcidev);
  5141. pci_disable_device(pcidev);
  5142. }
  5143. static struct pci_error_handlers beiscsi_eeh_handlers = {
  5144. .error_detected = beiscsi_eeh_err_detected,
  5145. .slot_reset = beiscsi_eeh_reset,
  5146. .resume = beiscsi_eeh_resume,
  5147. };
  5148. struct iscsi_transport beiscsi_iscsi_transport = {
  5149. .owner = THIS_MODULE,
  5150. .name = DRV_NAME,
  5151. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  5152. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  5153. .create_session = beiscsi_session_create,
  5154. .destroy_session = beiscsi_session_destroy,
  5155. .create_conn = beiscsi_conn_create,
  5156. .bind_conn = beiscsi_conn_bind,
  5157. .destroy_conn = iscsi_conn_teardown,
  5158. .attr_is_visible = beiscsi_attr_is_visible,
  5159. .set_iface_param = beiscsi_iface_set_param,
  5160. .get_iface_param = beiscsi_iface_get_param,
  5161. .set_param = beiscsi_set_param,
  5162. .get_conn_param = iscsi_conn_get_param,
  5163. .get_session_param = iscsi_session_get_param,
  5164. .get_host_param = beiscsi_get_host_param,
  5165. .start_conn = beiscsi_conn_start,
  5166. .stop_conn = iscsi_conn_stop,
  5167. .send_pdu = iscsi_conn_send_pdu,
  5168. .xmit_task = beiscsi_task_xmit,
  5169. .cleanup_task = beiscsi_cleanup_task,
  5170. .alloc_pdu = beiscsi_alloc_pdu,
  5171. .parse_pdu_itt = beiscsi_parse_pdu,
  5172. .get_stats = beiscsi_conn_get_stats,
  5173. .get_ep_param = beiscsi_ep_get_param,
  5174. .ep_connect = beiscsi_ep_connect,
  5175. .ep_poll = beiscsi_ep_poll,
  5176. .ep_disconnect = beiscsi_ep_disconnect,
  5177. .session_recovery_timedout = iscsi_session_recovery_timedout,
  5178. .bsg_request = beiscsi_bsg_request,
  5179. };
  5180. static struct pci_driver beiscsi_pci_driver = {
  5181. .name = DRV_NAME,
  5182. .probe = beiscsi_dev_probe,
  5183. .remove = beiscsi_remove,
  5184. .id_table = beiscsi_pci_id_table,
  5185. .err_handler = &beiscsi_eeh_handlers
  5186. };
  5187. static int __init beiscsi_module_init(void)
  5188. {
  5189. int ret;
  5190. beiscsi_scsi_transport =
  5191. iscsi_register_transport(&beiscsi_iscsi_transport);
  5192. if (!beiscsi_scsi_transport) {
  5193. printk(KERN_ERR
  5194. "beiscsi_module_init - Unable to register beiscsi transport.\n");
  5195. return -ENOMEM;
  5196. }
  5197. printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
  5198. &beiscsi_iscsi_transport);
  5199. ret = pci_register_driver(&beiscsi_pci_driver);
  5200. if (ret) {
  5201. printk(KERN_ERR
  5202. "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
  5203. goto unregister_iscsi_transport;
  5204. }
  5205. return 0;
  5206. unregister_iscsi_transport:
  5207. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5208. return ret;
  5209. }
  5210. static void __exit beiscsi_module_exit(void)
  5211. {
  5212. pci_unregister_driver(&beiscsi_pci_driver);
  5213. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5214. }
  5215. module_init(beiscsi_module_init);
  5216. module_exit(beiscsi_module_exit);