be_main.c 164 KB

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