be_main.c 153 KB

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