be_main.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863
  1. /**
  2. * Copyright (C) 2005 - 2011 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. struct device_attribute *beiscsi_attrs[] = {
  145. &dev_attr_beiscsi_log_enable,
  146. &dev_attr_beiscsi_drvr_ver,
  147. NULL,
  148. };
  149. static char const *cqe_desc[] = {
  150. "RESERVED_DESC",
  151. "SOL_CMD_COMPLETE",
  152. "SOL_CMD_KILLED_DATA_DIGEST_ERR",
  153. "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
  154. "CXN_KILLED_BURST_LEN_MISMATCH",
  155. "CXN_KILLED_AHS_RCVD",
  156. "CXN_KILLED_HDR_DIGEST_ERR",
  157. "CXN_KILLED_UNKNOWN_HDR",
  158. "CXN_KILLED_STALE_ITT_TTT_RCVD",
  159. "CXN_KILLED_INVALID_ITT_TTT_RCVD",
  160. "CXN_KILLED_RST_RCVD",
  161. "CXN_KILLED_TIMED_OUT",
  162. "CXN_KILLED_RST_SENT",
  163. "CXN_KILLED_FIN_RCVD",
  164. "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
  165. "CXN_KILLED_BAD_WRB_INDEX_ERROR",
  166. "CXN_KILLED_OVER_RUN_RESIDUAL",
  167. "CXN_KILLED_UNDER_RUN_RESIDUAL",
  168. "CMD_KILLED_INVALID_STATSN_RCVD",
  169. "CMD_KILLED_INVALID_R2T_RCVD",
  170. "CMD_CXN_KILLED_LUN_INVALID",
  171. "CMD_CXN_KILLED_ICD_INVALID",
  172. "CMD_CXN_KILLED_ITT_INVALID",
  173. "CMD_CXN_KILLED_SEQ_OUTOFORDER",
  174. "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
  175. "CXN_INVALIDATE_NOTIFY",
  176. "CXN_INVALIDATE_INDEX_NOTIFY",
  177. "CMD_INVALIDATED_NOTIFY",
  178. "UNSOL_HDR_NOTIFY",
  179. "UNSOL_DATA_NOTIFY",
  180. "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
  181. "DRIVERMSG_NOTIFY",
  182. "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
  183. "SOL_CMD_KILLED_DIF_ERR",
  184. "CXN_KILLED_SYN_RCVD",
  185. "CXN_KILLED_IMM_DATA_RCVD"
  186. };
  187. static int beiscsi_slave_configure(struct scsi_device *sdev)
  188. {
  189. blk_queue_max_segment_size(sdev->request_queue, 65536);
  190. return 0;
  191. }
  192. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  193. {
  194. struct iscsi_cls_session *cls_session;
  195. struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
  196. struct beiscsi_io_task *aborted_io_task;
  197. struct iscsi_conn *conn;
  198. struct beiscsi_conn *beiscsi_conn;
  199. struct beiscsi_hba *phba;
  200. struct iscsi_session *session;
  201. struct invalidate_command_table *inv_tbl;
  202. struct be_dma_mem nonemb_cmd;
  203. unsigned int cid, tag, num_invalidate;
  204. cls_session = starget_to_session(scsi_target(sc->device));
  205. session = cls_session->dd_data;
  206. spin_lock_bh(&session->lock);
  207. if (!aborted_task || !aborted_task->sc) {
  208. /* we raced */
  209. spin_unlock_bh(&session->lock);
  210. return SUCCESS;
  211. }
  212. aborted_io_task = aborted_task->dd_data;
  213. if (!aborted_io_task->scsi_cmnd) {
  214. /* raced or invalid command */
  215. spin_unlock_bh(&session->lock);
  216. return SUCCESS;
  217. }
  218. spin_unlock_bh(&session->lock);
  219. conn = aborted_task->conn;
  220. beiscsi_conn = conn->dd_data;
  221. phba = beiscsi_conn->phba;
  222. /* invalidate iocb */
  223. cid = beiscsi_conn->beiscsi_conn_cid;
  224. inv_tbl = phba->inv_tbl;
  225. memset(inv_tbl, 0x0, sizeof(*inv_tbl));
  226. inv_tbl->cid = cid;
  227. inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
  228. num_invalidate = 1;
  229. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  230. sizeof(struct invalidate_commands_params_in),
  231. &nonemb_cmd.dma);
  232. if (nonemb_cmd.va == NULL) {
  233. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  234. "BM_%d : Failed to allocate memory for"
  235. "mgmt_invalidate_icds\n");
  236. return FAILED;
  237. }
  238. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  239. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  240. cid, &nonemb_cmd);
  241. if (!tag) {
  242. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  243. "BM_%d : mgmt_invalidate_icds could not be"
  244. "submitted\n");
  245. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  246. nonemb_cmd.va, nonemb_cmd.dma);
  247. return FAILED;
  248. } else {
  249. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  250. phba->ctrl.mcc_numtag[tag]);
  251. free_mcc_tag(&phba->ctrl, tag);
  252. }
  253. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  254. nonemb_cmd.va, nonemb_cmd.dma);
  255. return iscsi_eh_abort(sc);
  256. }
  257. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  258. {
  259. struct iscsi_task *abrt_task;
  260. struct beiscsi_io_task *abrt_io_task;
  261. struct iscsi_conn *conn;
  262. struct beiscsi_conn *beiscsi_conn;
  263. struct beiscsi_hba *phba;
  264. struct iscsi_session *session;
  265. struct iscsi_cls_session *cls_session;
  266. struct invalidate_command_table *inv_tbl;
  267. struct be_dma_mem nonemb_cmd;
  268. unsigned int cid, tag, i, num_invalidate;
  269. /* invalidate iocbs */
  270. cls_session = starget_to_session(scsi_target(sc->device));
  271. session = cls_session->dd_data;
  272. spin_lock_bh(&session->lock);
  273. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
  274. spin_unlock_bh(&session->lock);
  275. return FAILED;
  276. }
  277. conn = session->leadconn;
  278. beiscsi_conn = conn->dd_data;
  279. phba = beiscsi_conn->phba;
  280. cid = beiscsi_conn->beiscsi_conn_cid;
  281. inv_tbl = phba->inv_tbl;
  282. memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
  283. num_invalidate = 0;
  284. for (i = 0; i < conn->session->cmds_max; i++) {
  285. abrt_task = conn->session->cmds[i];
  286. abrt_io_task = abrt_task->dd_data;
  287. if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
  288. continue;
  289. if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
  290. continue;
  291. inv_tbl->cid = cid;
  292. inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
  293. num_invalidate++;
  294. inv_tbl++;
  295. }
  296. spin_unlock_bh(&session->lock);
  297. inv_tbl = phba->inv_tbl;
  298. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  299. sizeof(struct invalidate_commands_params_in),
  300. &nonemb_cmd.dma);
  301. if (nonemb_cmd.va == NULL) {
  302. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  303. "BM_%d : Failed to allocate memory for"
  304. "mgmt_invalidate_icds\n");
  305. return FAILED;
  306. }
  307. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  308. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  309. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  310. cid, &nonemb_cmd);
  311. if (!tag) {
  312. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  313. "BM_%d : mgmt_invalidate_icds could not be"
  314. " submitted\n");
  315. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  316. nonemb_cmd.va, nonemb_cmd.dma);
  317. return FAILED;
  318. } else {
  319. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  320. phba->ctrl.mcc_numtag[tag]);
  321. free_mcc_tag(&phba->ctrl, tag);
  322. }
  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. static void beiscsi_get_params(struct beiscsi_hba *phba)
  635. {
  636. phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
  637. - (phba->fw_config.iscsi_cid_count
  638. + BE2_TMFS
  639. + BE2_NOPOUT_REQ));
  640. phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
  641. phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
  642. phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
  643. phba->params.num_sge_per_io = BE2_SGE;
  644. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  645. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  646. phba->params.eq_timer = 64;
  647. phba->params.num_eq_entries =
  648. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  649. + BE2_TMFS) / 512) + 1) * 512;
  650. phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
  651. ? 1024 : phba->params.num_eq_entries;
  652. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  653. "BM_%d : phba->params.num_eq_entries=%d\n",
  654. phba->params.num_eq_entries);
  655. phba->params.num_cq_entries =
  656. (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
  657. + BE2_TMFS) / 512) + 1) * 512;
  658. phba->params.wrbs_per_cxn = 256;
  659. }
  660. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  661. unsigned int id, unsigned int clr_interrupt,
  662. unsigned int num_processed,
  663. unsigned char rearm, unsigned char event)
  664. {
  665. u32 val = 0;
  666. val |= id & DB_EQ_RING_ID_MASK;
  667. if (rearm)
  668. val |= 1 << DB_EQ_REARM_SHIFT;
  669. if (clr_interrupt)
  670. val |= 1 << DB_EQ_CLR_SHIFT;
  671. if (event)
  672. val |= 1 << DB_EQ_EVNT_SHIFT;
  673. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  674. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  675. }
  676. /**
  677. * be_isr_mcc - The isr routine of the driver.
  678. * @irq: Not used
  679. * @dev_id: Pointer to host adapter structure
  680. */
  681. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  682. {
  683. struct beiscsi_hba *phba;
  684. struct be_eq_entry *eqe = NULL;
  685. struct be_queue_info *eq;
  686. struct be_queue_info *mcc;
  687. unsigned int num_eq_processed;
  688. struct be_eq_obj *pbe_eq;
  689. unsigned long flags;
  690. pbe_eq = dev_id;
  691. eq = &pbe_eq->q;
  692. phba = pbe_eq->phba;
  693. mcc = &phba->ctrl.mcc_obj.cq;
  694. eqe = queue_tail_node(eq);
  695. num_eq_processed = 0;
  696. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  697. & EQE_VALID_MASK) {
  698. if (((eqe->dw[offsetof(struct amap_eq_entry,
  699. resource_id) / 32] &
  700. EQE_RESID_MASK) >> 16) == mcc->id) {
  701. spin_lock_irqsave(&phba->isr_lock, flags);
  702. pbe_eq->todo_mcc_cq = true;
  703. spin_unlock_irqrestore(&phba->isr_lock, flags);
  704. }
  705. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  706. queue_tail_inc(eq);
  707. eqe = queue_tail_node(eq);
  708. num_eq_processed++;
  709. }
  710. if (pbe_eq->todo_mcc_cq)
  711. queue_work(phba->wq, &pbe_eq->work_cqs);
  712. if (num_eq_processed)
  713. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
  714. return IRQ_HANDLED;
  715. }
  716. /**
  717. * be_isr_msix - The isr routine of the driver.
  718. * @irq: Not used
  719. * @dev_id: Pointer to host adapter structure
  720. */
  721. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  722. {
  723. struct beiscsi_hba *phba;
  724. struct be_eq_entry *eqe = NULL;
  725. struct be_queue_info *eq;
  726. struct be_queue_info *cq;
  727. unsigned int num_eq_processed;
  728. struct be_eq_obj *pbe_eq;
  729. unsigned long flags;
  730. pbe_eq = dev_id;
  731. eq = &pbe_eq->q;
  732. cq = pbe_eq->cq;
  733. eqe = queue_tail_node(eq);
  734. phba = pbe_eq->phba;
  735. num_eq_processed = 0;
  736. if (blk_iopoll_enabled) {
  737. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  738. & EQE_VALID_MASK) {
  739. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  740. blk_iopoll_sched(&pbe_eq->iopoll);
  741. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  742. queue_tail_inc(eq);
  743. eqe = queue_tail_node(eq);
  744. num_eq_processed++;
  745. }
  746. } else {
  747. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  748. & EQE_VALID_MASK) {
  749. spin_lock_irqsave(&phba->isr_lock, flags);
  750. pbe_eq->todo_cq = true;
  751. spin_unlock_irqrestore(&phba->isr_lock, flags);
  752. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  753. queue_tail_inc(eq);
  754. eqe = queue_tail_node(eq);
  755. num_eq_processed++;
  756. }
  757. if (pbe_eq->todo_cq)
  758. queue_work(phba->wq, &pbe_eq->work_cqs);
  759. }
  760. if (num_eq_processed)
  761. hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
  762. return IRQ_HANDLED;
  763. }
  764. /**
  765. * be_isr - The isr routine of the driver.
  766. * @irq: Not used
  767. * @dev_id: Pointer to host adapter structure
  768. */
  769. static irqreturn_t be_isr(int irq, void *dev_id)
  770. {
  771. struct beiscsi_hba *phba;
  772. struct hwi_controller *phwi_ctrlr;
  773. struct hwi_context_memory *phwi_context;
  774. struct be_eq_entry *eqe = NULL;
  775. struct be_queue_info *eq;
  776. struct be_queue_info *cq;
  777. struct be_queue_info *mcc;
  778. unsigned long flags, index;
  779. unsigned int num_mcceq_processed, num_ioeq_processed;
  780. struct be_ctrl_info *ctrl;
  781. struct be_eq_obj *pbe_eq;
  782. int isr;
  783. phba = dev_id;
  784. ctrl = &phba->ctrl;
  785. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  786. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  787. if (!isr)
  788. return IRQ_NONE;
  789. phwi_ctrlr = phba->phwi_ctrlr;
  790. phwi_context = phwi_ctrlr->phwi_ctxt;
  791. pbe_eq = &phwi_context->be_eq[0];
  792. eq = &phwi_context->be_eq[0].q;
  793. mcc = &phba->ctrl.mcc_obj.cq;
  794. index = 0;
  795. eqe = queue_tail_node(eq);
  796. num_ioeq_processed = 0;
  797. num_mcceq_processed = 0;
  798. if (blk_iopoll_enabled) {
  799. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  800. & EQE_VALID_MASK) {
  801. if (((eqe->dw[offsetof(struct amap_eq_entry,
  802. resource_id) / 32] &
  803. EQE_RESID_MASK) >> 16) == mcc->id) {
  804. spin_lock_irqsave(&phba->isr_lock, flags);
  805. pbe_eq->todo_mcc_cq = true;
  806. spin_unlock_irqrestore(&phba->isr_lock, flags);
  807. num_mcceq_processed++;
  808. } else {
  809. if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
  810. blk_iopoll_sched(&pbe_eq->iopoll);
  811. num_ioeq_processed++;
  812. }
  813. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  814. queue_tail_inc(eq);
  815. eqe = queue_tail_node(eq);
  816. }
  817. if (num_ioeq_processed || num_mcceq_processed) {
  818. if (pbe_eq->todo_mcc_cq)
  819. queue_work(phba->wq, &pbe_eq->work_cqs);
  820. if ((num_mcceq_processed) && (!num_ioeq_processed))
  821. hwi_ring_eq_db(phba, eq->id, 0,
  822. (num_ioeq_processed +
  823. num_mcceq_processed) , 1, 1);
  824. else
  825. hwi_ring_eq_db(phba, eq->id, 0,
  826. (num_ioeq_processed +
  827. num_mcceq_processed), 0, 1);
  828. return IRQ_HANDLED;
  829. } else
  830. return IRQ_NONE;
  831. } else {
  832. cq = &phwi_context->be_cq[0];
  833. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  834. & EQE_VALID_MASK) {
  835. if (((eqe->dw[offsetof(struct amap_eq_entry,
  836. resource_id) / 32] &
  837. EQE_RESID_MASK) >> 16) != cq->id) {
  838. spin_lock_irqsave(&phba->isr_lock, flags);
  839. pbe_eq->todo_mcc_cq = true;
  840. spin_unlock_irqrestore(&phba->isr_lock, flags);
  841. } else {
  842. spin_lock_irqsave(&phba->isr_lock, flags);
  843. pbe_eq->todo_cq = true;
  844. spin_unlock_irqrestore(&phba->isr_lock, flags);
  845. }
  846. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  847. queue_tail_inc(eq);
  848. eqe = queue_tail_node(eq);
  849. num_ioeq_processed++;
  850. }
  851. if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
  852. queue_work(phba->wq, &pbe_eq->work_cqs);
  853. if (num_ioeq_processed) {
  854. hwi_ring_eq_db(phba, eq->id, 0,
  855. num_ioeq_processed, 1, 1);
  856. return IRQ_HANDLED;
  857. } else
  858. return IRQ_NONE;
  859. }
  860. }
  861. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  862. {
  863. struct pci_dev *pcidev = phba->pcidev;
  864. struct hwi_controller *phwi_ctrlr;
  865. struct hwi_context_memory *phwi_context;
  866. int ret, msix_vec, i, j;
  867. phwi_ctrlr = phba->phwi_ctrlr;
  868. phwi_context = phwi_ctrlr->phwi_ctxt;
  869. if (phba->msix_enabled) {
  870. for (i = 0; i < phba->num_cpus; i++) {
  871. phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
  872. GFP_KERNEL);
  873. if (!phba->msi_name[i]) {
  874. ret = -ENOMEM;
  875. goto free_msix_irqs;
  876. }
  877. sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
  878. phba->shost->host_no, i);
  879. msix_vec = phba->msix_entries[i].vector;
  880. ret = request_irq(msix_vec, be_isr_msix, 0,
  881. phba->msi_name[i],
  882. &phwi_context->be_eq[i]);
  883. if (ret) {
  884. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  885. "BM_%d : beiscsi_init_irqs-Failed to"
  886. "register msix for i = %d\n",
  887. i);
  888. kfree(phba->msi_name[i]);
  889. goto free_msix_irqs;
  890. }
  891. }
  892. phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
  893. if (!phba->msi_name[i]) {
  894. ret = -ENOMEM;
  895. goto free_msix_irqs;
  896. }
  897. sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
  898. phba->shost->host_no);
  899. msix_vec = phba->msix_entries[i].vector;
  900. ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
  901. &phwi_context->be_eq[i]);
  902. if (ret) {
  903. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
  904. "BM_%d : beiscsi_init_irqs-"
  905. "Failed to register beiscsi_msix_mcc\n");
  906. kfree(phba->msi_name[i]);
  907. goto free_msix_irqs;
  908. }
  909. } else {
  910. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  911. "beiscsi", phba);
  912. if (ret) {
  913. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  914. "BM_%d : beiscsi_init_irqs-"
  915. "Failed to register irq\\n");
  916. return ret;
  917. }
  918. }
  919. return 0;
  920. free_msix_irqs:
  921. for (j = i - 1; j >= 0; j--) {
  922. kfree(phba->msi_name[j]);
  923. msix_vec = phba->msix_entries[j].vector;
  924. free_irq(msix_vec, &phwi_context->be_eq[j]);
  925. }
  926. return ret;
  927. }
  928. static void hwi_ring_cq_db(struct beiscsi_hba *phba,
  929. unsigned int id, unsigned int num_processed,
  930. unsigned char rearm, unsigned char event)
  931. {
  932. u32 val = 0;
  933. val |= id & DB_CQ_RING_ID_MASK;
  934. if (rearm)
  935. val |= 1 << DB_CQ_REARM_SHIFT;
  936. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  937. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  938. }
  939. static unsigned int
  940. beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
  941. struct beiscsi_hba *phba,
  942. unsigned short cid,
  943. struct pdu_base *ppdu,
  944. unsigned long pdu_len,
  945. void *pbuffer, unsigned long buf_len)
  946. {
  947. struct iscsi_conn *conn = beiscsi_conn->conn;
  948. struct iscsi_session *session = conn->session;
  949. struct iscsi_task *task;
  950. struct beiscsi_io_task *io_task;
  951. struct iscsi_hdr *login_hdr;
  952. switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
  953. PDUBASE_OPCODE_MASK) {
  954. case ISCSI_OP_NOOP_IN:
  955. pbuffer = NULL;
  956. buf_len = 0;
  957. break;
  958. case ISCSI_OP_ASYNC_EVENT:
  959. break;
  960. case ISCSI_OP_REJECT:
  961. WARN_ON(!pbuffer);
  962. WARN_ON(!(buf_len == 48));
  963. beiscsi_log(phba, KERN_ERR,
  964. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  965. "BM_%d : In ISCSI_OP_REJECT\n");
  966. break;
  967. case ISCSI_OP_LOGIN_RSP:
  968. case ISCSI_OP_TEXT_RSP:
  969. task = conn->login_task;
  970. io_task = task->dd_data;
  971. login_hdr = (struct iscsi_hdr *)ppdu;
  972. login_hdr->itt = io_task->libiscsi_itt;
  973. break;
  974. default:
  975. beiscsi_log(phba, KERN_WARNING,
  976. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  977. "BM_%d : Unrecognized opcode 0x%x in async msg\n",
  978. (ppdu->
  979. dw[offsetof(struct amap_pdu_base, opcode) / 32]
  980. & PDUBASE_OPCODE_MASK));
  981. return 1;
  982. }
  983. spin_lock_bh(&session->lock);
  984. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
  985. spin_unlock_bh(&session->lock);
  986. return 0;
  987. }
  988. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  989. {
  990. struct sgl_handle *psgl_handle;
  991. if (phba->io_sgl_hndl_avbl) {
  992. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  993. "BM_%d : In alloc_io_sgl_handle,"
  994. " io_sgl_alloc_index=%d\n",
  995. phba->io_sgl_alloc_index);
  996. psgl_handle = phba->io_sgl_hndl_base[phba->
  997. io_sgl_alloc_index];
  998. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  999. phba->io_sgl_hndl_avbl--;
  1000. if (phba->io_sgl_alloc_index == (phba->params.
  1001. ios_per_ctrl - 1))
  1002. phba->io_sgl_alloc_index = 0;
  1003. else
  1004. phba->io_sgl_alloc_index++;
  1005. } else
  1006. psgl_handle = NULL;
  1007. return psgl_handle;
  1008. }
  1009. static void
  1010. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  1011. {
  1012. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  1013. "BM_%d : In free_,io_sgl_free_index=%d\n",
  1014. phba->io_sgl_free_index);
  1015. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  1016. /*
  1017. * this can happen if clean_task is called on a task that
  1018. * failed in xmit_task or alloc_pdu.
  1019. */
  1020. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  1021. "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
  1022. "value there=%p\n", phba->io_sgl_free_index,
  1023. phba->io_sgl_hndl_base
  1024. [phba->io_sgl_free_index]);
  1025. return;
  1026. }
  1027. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  1028. phba->io_sgl_hndl_avbl++;
  1029. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  1030. phba->io_sgl_free_index = 0;
  1031. else
  1032. phba->io_sgl_free_index++;
  1033. }
  1034. /**
  1035. * alloc_wrb_handle - To allocate a wrb handle
  1036. * @phba: The hba pointer
  1037. * @cid: The cid to use for allocation
  1038. *
  1039. * This happens under session_lock until submission to chip
  1040. */
  1041. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
  1042. {
  1043. struct hwi_wrb_context *pwrb_context;
  1044. struct hwi_controller *phwi_ctrlr;
  1045. struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
  1046. phwi_ctrlr = phba->phwi_ctrlr;
  1047. pwrb_context = &phwi_ctrlr->wrb_context[cid];
  1048. if (pwrb_context->wrb_handles_available >= 2) {
  1049. pwrb_handle = pwrb_context->pwrb_handle_base[
  1050. pwrb_context->alloc_index];
  1051. pwrb_context->wrb_handles_available--;
  1052. if (pwrb_context->alloc_index ==
  1053. (phba->params.wrbs_per_cxn - 1))
  1054. pwrb_context->alloc_index = 0;
  1055. else
  1056. pwrb_context->alloc_index++;
  1057. pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
  1058. pwrb_context->alloc_index];
  1059. pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
  1060. } else
  1061. pwrb_handle = NULL;
  1062. return pwrb_handle;
  1063. }
  1064. /**
  1065. * free_wrb_handle - To free the wrb handle back to pool
  1066. * @phba: The hba pointer
  1067. * @pwrb_context: The context to free from
  1068. * @pwrb_handle: The wrb_handle to free
  1069. *
  1070. * This happens under session_lock until submission to chip
  1071. */
  1072. static void
  1073. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  1074. struct wrb_handle *pwrb_handle)
  1075. {
  1076. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  1077. pwrb_context->wrb_handles_available++;
  1078. if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
  1079. pwrb_context->free_index = 0;
  1080. else
  1081. pwrb_context->free_index++;
  1082. beiscsi_log(phba, KERN_INFO,
  1083. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1084. "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
  1085. "wrb_handles_available=%d\n",
  1086. pwrb_handle, pwrb_context->free_index,
  1087. pwrb_context->wrb_handles_available);
  1088. }
  1089. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  1090. {
  1091. struct sgl_handle *psgl_handle;
  1092. if (phba->eh_sgl_hndl_avbl) {
  1093. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  1094. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  1095. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  1096. "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
  1097. phba->eh_sgl_alloc_index,
  1098. phba->eh_sgl_alloc_index);
  1099. phba->eh_sgl_hndl_avbl--;
  1100. if (phba->eh_sgl_alloc_index ==
  1101. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  1102. 1))
  1103. phba->eh_sgl_alloc_index = 0;
  1104. else
  1105. phba->eh_sgl_alloc_index++;
  1106. } else
  1107. psgl_handle = NULL;
  1108. return psgl_handle;
  1109. }
  1110. void
  1111. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  1112. {
  1113. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  1114. "BM_%d : In free_mgmt_sgl_handle,"
  1115. "eh_sgl_free_index=%d\n",
  1116. phba->eh_sgl_free_index);
  1117. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  1118. /*
  1119. * this can happen if clean_task is called on a task that
  1120. * failed in xmit_task or alloc_pdu.
  1121. */
  1122. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
  1123. "BM_%d : Double Free in eh SGL ,"
  1124. "eh_sgl_free_index=%d\n",
  1125. phba->eh_sgl_free_index);
  1126. return;
  1127. }
  1128. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  1129. phba->eh_sgl_hndl_avbl++;
  1130. if (phba->eh_sgl_free_index ==
  1131. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  1132. phba->eh_sgl_free_index = 0;
  1133. else
  1134. phba->eh_sgl_free_index++;
  1135. }
  1136. static void
  1137. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  1138. struct iscsi_task *task, struct sol_cqe *psol)
  1139. {
  1140. struct beiscsi_io_task *io_task = task->dd_data;
  1141. struct be_status_bhs *sts_bhs =
  1142. (struct be_status_bhs *)io_task->cmd_bhs;
  1143. struct iscsi_conn *conn = beiscsi_conn->conn;
  1144. unsigned char *sense;
  1145. u32 resid = 0, exp_cmdsn, max_cmdsn;
  1146. u8 rsp, status, flags;
  1147. exp_cmdsn = (psol->
  1148. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1149. & SOL_EXP_CMD_SN_MASK);
  1150. max_cmdsn = ((psol->
  1151. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1152. & SOL_EXP_CMD_SN_MASK) +
  1153. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1154. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1155. rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
  1156. & SOL_RESP_MASK) >> 16);
  1157. status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
  1158. & SOL_STS_MASK) >> 8);
  1159. flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1160. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1161. if (!task->sc) {
  1162. if (io_task->scsi_cmnd)
  1163. scsi_dma_unmap(io_task->scsi_cmnd);
  1164. return;
  1165. }
  1166. task->sc->result = (DID_OK << 16) | status;
  1167. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  1168. task->sc->result = DID_ERROR << 16;
  1169. goto unmap;
  1170. }
  1171. /* bidi not initially supported */
  1172. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  1173. resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
  1174. 32] & SOL_RES_CNT_MASK);
  1175. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  1176. task->sc->result = DID_ERROR << 16;
  1177. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  1178. scsi_set_resid(task->sc, resid);
  1179. if (!status && (scsi_bufflen(task->sc) - resid <
  1180. task->sc->underflow))
  1181. task->sc->result = DID_ERROR << 16;
  1182. }
  1183. }
  1184. if (status == SAM_STAT_CHECK_CONDITION) {
  1185. u16 sense_len;
  1186. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  1187. sense = sts_bhs->sense_info + sizeof(unsigned short);
  1188. sense_len = be16_to_cpu(*slen);
  1189. memcpy(task->sc->sense_buffer, sense,
  1190. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  1191. }
  1192. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
  1193. if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  1194. & SOL_RES_CNT_MASK)
  1195. conn->rxdata_octets += (psol->
  1196. dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
  1197. & SOL_RES_CNT_MASK);
  1198. }
  1199. unmap:
  1200. scsi_dma_unmap(io_task->scsi_cmnd);
  1201. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  1202. }
  1203. static void
  1204. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  1205. struct iscsi_task *task, struct sol_cqe *psol)
  1206. {
  1207. struct iscsi_logout_rsp *hdr;
  1208. struct beiscsi_io_task *io_task = task->dd_data;
  1209. struct iscsi_conn *conn = beiscsi_conn->conn;
  1210. hdr = (struct iscsi_logout_rsp *)task->hdr;
  1211. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  1212. hdr->t2wait = 5;
  1213. hdr->t2retain = 0;
  1214. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1215. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1216. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  1217. 32] & SOL_RESP_MASK);
  1218. hdr->exp_cmdsn = cpu_to_be32(psol->
  1219. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1220. & SOL_EXP_CMD_SN_MASK);
  1221. hdr->max_cmdsn = be32_to_cpu((psol->
  1222. dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
  1223. & SOL_EXP_CMD_SN_MASK) +
  1224. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1225. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1226. hdr->dlength[0] = 0;
  1227. hdr->dlength[1] = 0;
  1228. hdr->dlength[2] = 0;
  1229. hdr->hlength = 0;
  1230. hdr->itt = io_task->libiscsi_itt;
  1231. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1232. }
  1233. static void
  1234. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  1235. struct iscsi_task *task, struct sol_cqe *psol)
  1236. {
  1237. struct iscsi_tm_rsp *hdr;
  1238. struct iscsi_conn *conn = beiscsi_conn->conn;
  1239. struct beiscsi_io_task *io_task = task->dd_data;
  1240. hdr = (struct iscsi_tm_rsp *)task->hdr;
  1241. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  1242. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1243. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1244. hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
  1245. 32] & SOL_RESP_MASK);
  1246. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  1247. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  1248. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  1249. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  1250. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1251. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1252. hdr->itt = io_task->libiscsi_itt;
  1253. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1254. }
  1255. static void
  1256. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  1257. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1258. {
  1259. struct hwi_wrb_context *pwrb_context;
  1260. struct wrb_handle *pwrb_handle = NULL;
  1261. struct hwi_controller *phwi_ctrlr;
  1262. struct iscsi_task *task;
  1263. struct beiscsi_io_task *io_task;
  1264. struct iscsi_conn *conn = beiscsi_conn->conn;
  1265. struct iscsi_session *session = conn->session;
  1266. phwi_ctrlr = phba->phwi_ctrlr;
  1267. pwrb_context = &phwi_ctrlr->wrb_context[((psol->
  1268. dw[offsetof(struct amap_sol_cqe, cid) / 32] &
  1269. SOL_CID_MASK) >> 6) -
  1270. phba->fw_config.iscsi_cid_start];
  1271. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  1272. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  1273. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  1274. task = pwrb_handle->pio_handle;
  1275. io_task = task->dd_data;
  1276. spin_lock_bh(&phba->mgmt_sgl_lock);
  1277. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  1278. spin_unlock_bh(&phba->mgmt_sgl_lock);
  1279. spin_lock_bh(&session->lock);
  1280. free_wrb_handle(phba, pwrb_context, pwrb_handle);
  1281. spin_unlock_bh(&session->lock);
  1282. }
  1283. static void
  1284. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  1285. struct iscsi_task *task, struct sol_cqe *psol)
  1286. {
  1287. struct iscsi_nopin *hdr;
  1288. struct iscsi_conn *conn = beiscsi_conn->conn;
  1289. struct beiscsi_io_task *io_task = task->dd_data;
  1290. hdr = (struct iscsi_nopin *)task->hdr;
  1291. hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
  1292. & SOL_FLAGS_MASK) >> 24) | 0x80;
  1293. hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
  1294. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
  1295. hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
  1296. i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
  1297. ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
  1298. / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
  1299. hdr->opcode = ISCSI_OP_NOOP_IN;
  1300. hdr->itt = io_task->libiscsi_itt;
  1301. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1302. }
  1303. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  1304. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1305. {
  1306. struct hwi_wrb_context *pwrb_context;
  1307. struct wrb_handle *pwrb_handle;
  1308. struct iscsi_wrb *pwrb = NULL;
  1309. struct hwi_controller *phwi_ctrlr;
  1310. struct iscsi_task *task;
  1311. unsigned int type;
  1312. struct iscsi_conn *conn = beiscsi_conn->conn;
  1313. struct iscsi_session *session = conn->session;
  1314. phwi_ctrlr = phba->phwi_ctrlr;
  1315. pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
  1316. (struct amap_sol_cqe, cid) / 32]
  1317. & SOL_CID_MASK) >> 6) -
  1318. phba->fw_config.iscsi_cid_start];
  1319. pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
  1320. dw[offsetof(struct amap_sol_cqe, wrb_index) /
  1321. 32] & SOL_WRB_INDEX_MASK) >> 16)];
  1322. task = pwrb_handle->pio_handle;
  1323. pwrb = pwrb_handle->pwrb;
  1324. type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
  1325. WRB_TYPE_MASK) >> 28;
  1326. spin_lock_bh(&session->lock);
  1327. switch (type) {
  1328. case HWH_TYPE_IO:
  1329. case HWH_TYPE_IO_RD:
  1330. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1331. ISCSI_OP_NOOP_OUT)
  1332. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1333. else
  1334. be_complete_io(beiscsi_conn, task, psol);
  1335. break;
  1336. case HWH_TYPE_LOGOUT:
  1337. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1338. be_complete_logout(beiscsi_conn, task, psol);
  1339. else
  1340. be_complete_tmf(beiscsi_conn, task, psol);
  1341. break;
  1342. case HWH_TYPE_LOGIN:
  1343. beiscsi_log(phba, KERN_ERR,
  1344. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1345. "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
  1346. " hwi_complete_cmd- Solicited path\n");
  1347. break;
  1348. case HWH_TYPE_NOP:
  1349. be_complete_nopin_resp(beiscsi_conn, task, psol);
  1350. break;
  1351. default:
  1352. beiscsi_log(phba, KERN_WARNING,
  1353. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1354. "BM_%d : In hwi_complete_cmd, unknown type = %d"
  1355. "wrb_index 0x%x CID 0x%x\n", type,
  1356. ((psol->dw[offsetof(struct amap_iscsi_wrb,
  1357. type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
  1358. ((psol->dw[offsetof(struct amap_sol_cqe,
  1359. cid) / 32] & SOL_CID_MASK) >> 6));
  1360. break;
  1361. }
  1362. spin_unlock_bh(&session->lock);
  1363. }
  1364. static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
  1365. *pasync_ctx, unsigned int is_header,
  1366. unsigned int host_write_ptr)
  1367. {
  1368. if (is_header)
  1369. return &pasync_ctx->async_entry[host_write_ptr].
  1370. header_busy_list;
  1371. else
  1372. return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
  1373. }
  1374. static struct async_pdu_handle *
  1375. hwi_get_async_handle(struct beiscsi_hba *phba,
  1376. struct beiscsi_conn *beiscsi_conn,
  1377. struct hwi_async_pdu_context *pasync_ctx,
  1378. struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
  1379. {
  1380. struct be_bus_address phys_addr;
  1381. struct list_head *pbusy_list;
  1382. struct async_pdu_handle *pasync_handle = NULL;
  1383. unsigned char is_header = 0;
  1384. phys_addr.u.a32.address_lo =
  1385. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
  1386. ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1387. & PDUCQE_DPL_MASK) >> 16);
  1388. phys_addr.u.a32.address_hi =
  1389. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
  1390. phys_addr.u.a64.address =
  1391. *((unsigned long long *)(&phys_addr.u.a64.address));
  1392. switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
  1393. & PDUCQE_CODE_MASK) {
  1394. case UNSOL_HDR_NOTIFY:
  1395. is_header = 1;
  1396. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
  1397. (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1398. index) / 32] & PDUCQE_INDEX_MASK));
  1399. break;
  1400. case UNSOL_DATA_NOTIFY:
  1401. pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
  1402. dw[offsetof(struct amap_i_t_dpdu_cqe,
  1403. index) / 32] & PDUCQE_INDEX_MASK));
  1404. break;
  1405. default:
  1406. pbusy_list = NULL;
  1407. beiscsi_log(phba, KERN_WARNING,
  1408. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1409. "BM_%d : Unexpected code=%d\n",
  1410. pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1411. code) / 32] & PDUCQE_CODE_MASK);
  1412. return NULL;
  1413. }
  1414. WARN_ON(list_empty(pbusy_list));
  1415. list_for_each_entry(pasync_handle, pbusy_list, link) {
  1416. if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
  1417. break;
  1418. }
  1419. WARN_ON(!pasync_handle);
  1420. pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
  1421. phba->fw_config.iscsi_cid_start;
  1422. pasync_handle->is_header = is_header;
  1423. pasync_handle->buffer_len = ((pdpdu_cqe->
  1424. dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
  1425. & PDUCQE_DPL_MASK) >> 16);
  1426. *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
  1427. index) / 32] & PDUCQE_INDEX_MASK);
  1428. return pasync_handle;
  1429. }
  1430. static unsigned int
  1431. hwi_update_async_writables(struct beiscsi_hba *phba,
  1432. struct hwi_async_pdu_context *pasync_ctx,
  1433. unsigned int is_header, unsigned int cq_index)
  1434. {
  1435. struct list_head *pbusy_list;
  1436. struct async_pdu_handle *pasync_handle;
  1437. unsigned int num_entries, writables = 0;
  1438. unsigned int *pep_read_ptr, *pwritables;
  1439. num_entries = pasync_ctx->num_entries;
  1440. if (is_header) {
  1441. pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
  1442. pwritables = &pasync_ctx->async_header.writables;
  1443. } else {
  1444. pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
  1445. pwritables = &pasync_ctx->async_data.writables;
  1446. }
  1447. while ((*pep_read_ptr) != cq_index) {
  1448. (*pep_read_ptr)++;
  1449. *pep_read_ptr = (*pep_read_ptr) % num_entries;
  1450. pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
  1451. *pep_read_ptr);
  1452. if (writables == 0)
  1453. WARN_ON(list_empty(pbusy_list));
  1454. if (!list_empty(pbusy_list)) {
  1455. pasync_handle = list_entry(pbusy_list->next,
  1456. struct async_pdu_handle,
  1457. link);
  1458. WARN_ON(!pasync_handle);
  1459. pasync_handle->consumed = 1;
  1460. }
  1461. writables++;
  1462. }
  1463. if (!writables) {
  1464. beiscsi_log(phba, KERN_ERR,
  1465. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1466. "BM_%d : Duplicate notification received - index 0x%x!!\n",
  1467. cq_index);
  1468. WARN_ON(1);
  1469. }
  1470. *pwritables = *pwritables + writables;
  1471. return 0;
  1472. }
  1473. static void hwi_free_async_msg(struct beiscsi_hba *phba,
  1474. unsigned int cri)
  1475. {
  1476. struct hwi_controller *phwi_ctrlr;
  1477. struct hwi_async_pdu_context *pasync_ctx;
  1478. struct async_pdu_handle *pasync_handle, *tmp_handle;
  1479. struct list_head *plist;
  1480. phwi_ctrlr = phba->phwi_ctrlr;
  1481. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1482. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1483. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
  1484. list_del(&pasync_handle->link);
  1485. if (pasync_handle->is_header) {
  1486. list_add_tail(&pasync_handle->link,
  1487. &pasync_ctx->async_header.free_list);
  1488. pasync_ctx->async_header.free_entries++;
  1489. } else {
  1490. list_add_tail(&pasync_handle->link,
  1491. &pasync_ctx->async_data.free_list);
  1492. pasync_ctx->async_data.free_entries++;
  1493. }
  1494. }
  1495. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
  1496. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
  1497. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1498. }
  1499. static struct phys_addr *
  1500. hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
  1501. unsigned int is_header, unsigned int host_write_ptr)
  1502. {
  1503. struct phys_addr *pasync_sge = NULL;
  1504. if (is_header)
  1505. pasync_sge = pasync_ctx->async_header.ring_base;
  1506. else
  1507. pasync_sge = pasync_ctx->async_data.ring_base;
  1508. return pasync_sge + host_write_ptr;
  1509. }
  1510. static void hwi_post_async_buffers(struct beiscsi_hba *phba,
  1511. unsigned int is_header)
  1512. {
  1513. struct hwi_controller *phwi_ctrlr;
  1514. struct hwi_async_pdu_context *pasync_ctx;
  1515. struct async_pdu_handle *pasync_handle;
  1516. struct list_head *pfree_link, *pbusy_list;
  1517. struct phys_addr *pasync_sge;
  1518. unsigned int ring_id, num_entries;
  1519. unsigned int host_write_num;
  1520. unsigned int writables;
  1521. unsigned int i = 0;
  1522. u32 doorbell = 0;
  1523. phwi_ctrlr = phba->phwi_ctrlr;
  1524. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1525. num_entries = pasync_ctx->num_entries;
  1526. if (is_header) {
  1527. writables = min(pasync_ctx->async_header.writables,
  1528. pasync_ctx->async_header.free_entries);
  1529. pfree_link = pasync_ctx->async_header.free_list.next;
  1530. host_write_num = pasync_ctx->async_header.host_write_ptr;
  1531. ring_id = phwi_ctrlr->default_pdu_hdr.id;
  1532. } else {
  1533. writables = min(pasync_ctx->async_data.writables,
  1534. pasync_ctx->async_data.free_entries);
  1535. pfree_link = pasync_ctx->async_data.free_list.next;
  1536. host_write_num = pasync_ctx->async_data.host_write_ptr;
  1537. ring_id = phwi_ctrlr->default_pdu_data.id;
  1538. }
  1539. writables = (writables / 8) * 8;
  1540. if (writables) {
  1541. for (i = 0; i < writables; i++) {
  1542. pbusy_list =
  1543. hwi_get_async_busy_list(pasync_ctx, is_header,
  1544. host_write_num);
  1545. pasync_handle =
  1546. list_entry(pfree_link, struct async_pdu_handle,
  1547. link);
  1548. WARN_ON(!pasync_handle);
  1549. pasync_handle->consumed = 0;
  1550. pfree_link = pfree_link->next;
  1551. pasync_sge = hwi_get_ring_address(pasync_ctx,
  1552. is_header, host_write_num);
  1553. pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
  1554. pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
  1555. list_move(&pasync_handle->link, pbusy_list);
  1556. host_write_num++;
  1557. host_write_num = host_write_num % num_entries;
  1558. }
  1559. if (is_header) {
  1560. pasync_ctx->async_header.host_write_ptr =
  1561. host_write_num;
  1562. pasync_ctx->async_header.free_entries -= writables;
  1563. pasync_ctx->async_header.writables -= writables;
  1564. pasync_ctx->async_header.busy_entries += writables;
  1565. } else {
  1566. pasync_ctx->async_data.host_write_ptr = host_write_num;
  1567. pasync_ctx->async_data.free_entries -= writables;
  1568. pasync_ctx->async_data.writables -= writables;
  1569. pasync_ctx->async_data.busy_entries += writables;
  1570. }
  1571. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1572. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1573. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1574. doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
  1575. << DB_DEF_PDU_CQPROC_SHIFT;
  1576. iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
  1577. }
  1578. }
  1579. static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
  1580. struct beiscsi_conn *beiscsi_conn,
  1581. struct i_t_dpdu_cqe *pdpdu_cqe)
  1582. {
  1583. struct hwi_controller *phwi_ctrlr;
  1584. struct hwi_async_pdu_context *pasync_ctx;
  1585. struct async_pdu_handle *pasync_handle = NULL;
  1586. unsigned int cq_index = -1;
  1587. phwi_ctrlr = phba->phwi_ctrlr;
  1588. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1589. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1590. pdpdu_cqe, &cq_index);
  1591. BUG_ON(pasync_handle->is_header != 0);
  1592. if (pasync_handle->consumed == 0)
  1593. hwi_update_async_writables(phba, pasync_ctx,
  1594. pasync_handle->is_header, cq_index);
  1595. hwi_free_async_msg(phba, pasync_handle->cri);
  1596. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1597. }
  1598. static unsigned int
  1599. hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
  1600. struct beiscsi_hba *phba,
  1601. struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
  1602. {
  1603. struct list_head *plist;
  1604. struct async_pdu_handle *pasync_handle;
  1605. void *phdr = NULL;
  1606. unsigned int hdr_len = 0, buf_len = 0;
  1607. unsigned int status, index = 0, offset = 0;
  1608. void *pfirst_buffer = NULL;
  1609. unsigned int num_buf = 0;
  1610. plist = &pasync_ctx->async_entry[cri].wait_queue.list;
  1611. list_for_each_entry(pasync_handle, plist, link) {
  1612. if (index == 0) {
  1613. phdr = pasync_handle->pbuffer;
  1614. hdr_len = pasync_handle->buffer_len;
  1615. } else {
  1616. buf_len = pasync_handle->buffer_len;
  1617. if (!num_buf) {
  1618. pfirst_buffer = pasync_handle->pbuffer;
  1619. num_buf++;
  1620. }
  1621. memcpy(pfirst_buffer + offset,
  1622. pasync_handle->pbuffer, buf_len);
  1623. offset += buf_len;
  1624. }
  1625. index++;
  1626. }
  1627. status = beiscsi_process_async_pdu(beiscsi_conn, phba,
  1628. (beiscsi_conn->beiscsi_conn_cid -
  1629. phba->fw_config.iscsi_cid_start),
  1630. phdr, hdr_len, pfirst_buffer,
  1631. offset);
  1632. hwi_free_async_msg(phba, cri);
  1633. return 0;
  1634. }
  1635. static unsigned int
  1636. hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
  1637. struct beiscsi_hba *phba,
  1638. struct async_pdu_handle *pasync_handle)
  1639. {
  1640. struct hwi_async_pdu_context *pasync_ctx;
  1641. struct hwi_controller *phwi_ctrlr;
  1642. unsigned int bytes_needed = 0, status = 0;
  1643. unsigned short cri = pasync_handle->cri;
  1644. struct pdu_base *ppdu;
  1645. phwi_ctrlr = phba->phwi_ctrlr;
  1646. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1647. list_del(&pasync_handle->link);
  1648. if (pasync_handle->is_header) {
  1649. pasync_ctx->async_header.busy_entries--;
  1650. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1651. hwi_free_async_msg(phba, cri);
  1652. BUG();
  1653. }
  1654. pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
  1655. pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
  1656. pasync_ctx->async_entry[cri].wait_queue.hdr_len =
  1657. (unsigned short)pasync_handle->buffer_len;
  1658. list_add_tail(&pasync_handle->link,
  1659. &pasync_ctx->async_entry[cri].wait_queue.list);
  1660. ppdu = pasync_handle->pbuffer;
  1661. bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
  1662. data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
  1663. 0xFFFF0000) | ((be16_to_cpu((ppdu->
  1664. dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
  1665. & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
  1666. if (status == 0) {
  1667. pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
  1668. bytes_needed;
  1669. if (bytes_needed == 0)
  1670. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1671. pasync_ctx, cri);
  1672. }
  1673. } else {
  1674. pasync_ctx->async_data.busy_entries--;
  1675. if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
  1676. list_add_tail(&pasync_handle->link,
  1677. &pasync_ctx->async_entry[cri].wait_queue.
  1678. list);
  1679. pasync_ctx->async_entry[cri].wait_queue.
  1680. bytes_received +=
  1681. (unsigned short)pasync_handle->buffer_len;
  1682. if (pasync_ctx->async_entry[cri].wait_queue.
  1683. bytes_received >=
  1684. pasync_ctx->async_entry[cri].wait_queue.
  1685. bytes_needed)
  1686. status = hwi_fwd_async_msg(beiscsi_conn, phba,
  1687. pasync_ctx, cri);
  1688. }
  1689. }
  1690. return status;
  1691. }
  1692. static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
  1693. struct beiscsi_hba *phba,
  1694. struct i_t_dpdu_cqe *pdpdu_cqe)
  1695. {
  1696. struct hwi_controller *phwi_ctrlr;
  1697. struct hwi_async_pdu_context *pasync_ctx;
  1698. struct async_pdu_handle *pasync_handle = NULL;
  1699. unsigned int cq_index = -1;
  1700. phwi_ctrlr = phba->phwi_ctrlr;
  1701. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
  1702. pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
  1703. pdpdu_cqe, &cq_index);
  1704. if (pasync_handle->consumed == 0)
  1705. hwi_update_async_writables(phba, pasync_ctx,
  1706. pasync_handle->is_header, cq_index);
  1707. hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
  1708. hwi_post_async_buffers(phba, pasync_handle->is_header);
  1709. }
  1710. static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
  1711. {
  1712. struct be_queue_info *mcc_cq;
  1713. struct be_mcc_compl *mcc_compl;
  1714. unsigned int num_processed = 0;
  1715. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1716. mcc_compl = queue_tail_node(mcc_cq);
  1717. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1718. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1719. if (num_processed >= 32) {
  1720. hwi_ring_cq_db(phba, mcc_cq->id,
  1721. num_processed, 0, 0);
  1722. num_processed = 0;
  1723. }
  1724. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1725. /* Interpret flags as an async trailer */
  1726. if (is_link_state_evt(mcc_compl->flags))
  1727. /* Interpret compl as a async link evt */
  1728. beiscsi_async_link_state_process(phba,
  1729. (struct be_async_event_link_state *) mcc_compl);
  1730. else
  1731. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
  1732. "BM_%d : Unsupported Async Event, flags"
  1733. " = 0x%08x\n",
  1734. mcc_compl->flags);
  1735. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1736. be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
  1737. atomic_dec(&phba->ctrl.mcc_obj.q.used);
  1738. }
  1739. mcc_compl->flags = 0;
  1740. queue_tail_inc(mcc_cq);
  1741. mcc_compl = queue_tail_node(mcc_cq);
  1742. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1743. num_processed++;
  1744. }
  1745. if (num_processed > 0)
  1746. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
  1747. }
  1748. /**
  1749. * beiscsi_process_cq()- Process the Completion Queue
  1750. * @pbe_eq: Event Q on which the Completion has come
  1751. *
  1752. * return
  1753. * Number of Completion Entries processed.
  1754. **/
  1755. static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
  1756. {
  1757. struct be_queue_info *cq;
  1758. struct sol_cqe *sol;
  1759. struct dmsg_cqe *dmsg;
  1760. unsigned int num_processed = 0;
  1761. unsigned int tot_nump = 0;
  1762. unsigned short code = 0, cid = 0;
  1763. struct beiscsi_conn *beiscsi_conn;
  1764. struct beiscsi_endpoint *beiscsi_ep;
  1765. struct iscsi_endpoint *ep;
  1766. struct beiscsi_hba *phba;
  1767. cq = pbe_eq->cq;
  1768. sol = queue_tail_node(cq);
  1769. phba = pbe_eq->phba;
  1770. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1771. CQE_VALID_MASK) {
  1772. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1773. cid = ((sol->dw[offsetof(struct amap_sol_cqe, cid)/32] &
  1774. CQE_CID_MASK) >> 6);
  1775. code = (sol->dw[offsetof(struct amap_sol_cqe, code)/32] &
  1776. CQE_CODE_MASK);
  1777. ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
  1778. beiscsi_ep = ep->dd_data;
  1779. beiscsi_conn = beiscsi_ep->conn;
  1780. if (num_processed >= 32) {
  1781. hwi_ring_cq_db(phba, cq->id,
  1782. num_processed, 0, 0);
  1783. tot_nump += num_processed;
  1784. num_processed = 0;
  1785. }
  1786. switch (code) {
  1787. case SOL_CMD_COMPLETE:
  1788. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1789. break;
  1790. case DRIVERMSG_NOTIFY:
  1791. beiscsi_log(phba, KERN_INFO,
  1792. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1793. "BM_%d : Received %s[%d] on CID : %d\n",
  1794. cqe_desc[code], code, cid);
  1795. dmsg = (struct dmsg_cqe *)sol;
  1796. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1797. break;
  1798. case UNSOL_HDR_NOTIFY:
  1799. beiscsi_log(phba, KERN_INFO,
  1800. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1801. "BM_%d : Received %s[%d] on CID : %d\n",
  1802. cqe_desc[code], code, cid);
  1803. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1804. (struct i_t_dpdu_cqe *)sol);
  1805. break;
  1806. case UNSOL_DATA_NOTIFY:
  1807. beiscsi_log(phba, KERN_INFO,
  1808. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1809. "BM_%d : Received %s[%d] on CID : %d\n",
  1810. cqe_desc[code], code, cid);
  1811. hwi_process_default_pdu_ring(beiscsi_conn, phba,
  1812. (struct i_t_dpdu_cqe *)sol);
  1813. break;
  1814. case CXN_INVALIDATE_INDEX_NOTIFY:
  1815. case CMD_INVALIDATED_NOTIFY:
  1816. case CXN_INVALIDATE_NOTIFY:
  1817. beiscsi_log(phba, KERN_ERR,
  1818. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1819. "BM_%d : Ignoring %s[%d] on CID : %d\n",
  1820. cqe_desc[code], code, cid);
  1821. break;
  1822. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1823. case CMD_KILLED_INVALID_STATSN_RCVD:
  1824. case CMD_KILLED_INVALID_R2T_RCVD:
  1825. case CMD_CXN_KILLED_LUN_INVALID:
  1826. case CMD_CXN_KILLED_ICD_INVALID:
  1827. case CMD_CXN_KILLED_ITT_INVALID:
  1828. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1829. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1830. beiscsi_log(phba, KERN_ERR,
  1831. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1832. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1833. cqe_desc[code], code, cid);
  1834. break;
  1835. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1836. beiscsi_log(phba, KERN_ERR,
  1837. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1838. "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
  1839. cqe_desc[code], code, cid);
  1840. hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
  1841. (struct i_t_dpdu_cqe *) sol);
  1842. break;
  1843. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1844. case CXN_KILLED_BURST_LEN_MISMATCH:
  1845. case CXN_KILLED_AHS_RCVD:
  1846. case CXN_KILLED_HDR_DIGEST_ERR:
  1847. case CXN_KILLED_UNKNOWN_HDR:
  1848. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1849. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1850. case CXN_KILLED_TIMED_OUT:
  1851. case CXN_KILLED_FIN_RCVD:
  1852. case CXN_KILLED_RST_SENT:
  1853. case CXN_KILLED_RST_RCVD:
  1854. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1855. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1856. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1857. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1858. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1859. beiscsi_log(phba, KERN_ERR,
  1860. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1861. "BM_%d : Event %s[%d] received on CID : %d\n",
  1862. cqe_desc[code], code, cid);
  1863. if (beiscsi_conn)
  1864. iscsi_conn_failure(beiscsi_conn->conn,
  1865. ISCSI_ERR_CONN_FAILED);
  1866. break;
  1867. default:
  1868. beiscsi_log(phba, KERN_ERR,
  1869. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1870. "BM_%d : Invalid CQE Event Received Code : %d"
  1871. "CID 0x%x...\n",
  1872. code, cid);
  1873. break;
  1874. }
  1875. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1876. queue_tail_inc(cq);
  1877. sol = queue_tail_node(cq);
  1878. num_processed++;
  1879. }
  1880. if (num_processed > 0) {
  1881. tot_nump += num_processed;
  1882. hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
  1883. }
  1884. return tot_nump;
  1885. }
  1886. void beiscsi_process_all_cqs(struct work_struct *work)
  1887. {
  1888. unsigned long flags;
  1889. struct hwi_controller *phwi_ctrlr;
  1890. struct hwi_context_memory *phwi_context;
  1891. struct beiscsi_hba *phba;
  1892. struct be_eq_obj *pbe_eq =
  1893. container_of(work, struct be_eq_obj, work_cqs);
  1894. phba = pbe_eq->phba;
  1895. phwi_ctrlr = phba->phwi_ctrlr;
  1896. phwi_context = phwi_ctrlr->phwi_ctxt;
  1897. if (pbe_eq->todo_mcc_cq) {
  1898. spin_lock_irqsave(&phba->isr_lock, flags);
  1899. pbe_eq->todo_mcc_cq = false;
  1900. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1901. beiscsi_process_mcc_isr(phba);
  1902. }
  1903. if (pbe_eq->todo_cq) {
  1904. spin_lock_irqsave(&phba->isr_lock, flags);
  1905. pbe_eq->todo_cq = false;
  1906. spin_unlock_irqrestore(&phba->isr_lock, flags);
  1907. beiscsi_process_cq(pbe_eq);
  1908. }
  1909. /* rearm EQ for further interrupts */
  1910. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1911. }
  1912. static int be_iopoll(struct blk_iopoll *iop, int budget)
  1913. {
  1914. static unsigned int ret;
  1915. struct beiscsi_hba *phba;
  1916. struct be_eq_obj *pbe_eq;
  1917. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1918. ret = beiscsi_process_cq(pbe_eq);
  1919. if (ret < budget) {
  1920. phba = pbe_eq->phba;
  1921. blk_iopoll_complete(iop);
  1922. beiscsi_log(phba, KERN_INFO,
  1923. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1924. "BM_%d : rearm pbe_eq->q.id =%d\n",
  1925. pbe_eq->q.id);
  1926. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1927. }
  1928. return ret;
  1929. }
  1930. static void
  1931. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1932. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1933. {
  1934. struct iscsi_sge *psgl;
  1935. unsigned int sg_len, index;
  1936. unsigned int sge_len = 0;
  1937. unsigned long long addr;
  1938. struct scatterlist *l_sg;
  1939. unsigned int offset;
  1940. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1941. io_task->bhs_pa.u.a32.address_lo);
  1942. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1943. io_task->bhs_pa.u.a32.address_hi);
  1944. l_sg = sg;
  1945. for (index = 0; (index < num_sg) && (index < 2); index++,
  1946. sg = sg_next(sg)) {
  1947. if (index == 0) {
  1948. sg_len = sg_dma_len(sg);
  1949. addr = (u64) sg_dma_address(sg);
  1950. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1951. ((u32)(addr & 0xFFFFFFFF)));
  1952. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1953. ((u32)(addr >> 32)));
  1954. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1955. sg_len);
  1956. sge_len = sg_len;
  1957. } else {
  1958. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1959. pwrb, sge_len);
  1960. sg_len = sg_dma_len(sg);
  1961. addr = (u64) sg_dma_address(sg);
  1962. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1963. ((u32)(addr & 0xFFFFFFFF)));
  1964. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  1965. ((u32)(addr >> 32)));
  1966. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  1967. sg_len);
  1968. }
  1969. }
  1970. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1971. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1972. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1973. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1974. io_task->bhs_pa.u.a32.address_hi);
  1975. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1976. io_task->bhs_pa.u.a32.address_lo);
  1977. if (num_sg == 1) {
  1978. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1979. 1);
  1980. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1981. 0);
  1982. } else if (num_sg == 2) {
  1983. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1984. 0);
  1985. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1986. 1);
  1987. } else {
  1988. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  1989. 0);
  1990. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  1991. 0);
  1992. }
  1993. sg = l_sg;
  1994. psgl++;
  1995. psgl++;
  1996. offset = 0;
  1997. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1998. sg_len = sg_dma_len(sg);
  1999. addr = (u64) sg_dma_address(sg);
  2000. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2001. (addr & 0xFFFFFFFF));
  2002. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2003. (addr >> 32));
  2004. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  2005. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  2006. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2007. offset += sg_len;
  2008. }
  2009. psgl--;
  2010. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2011. }
  2012. /**
  2013. * hwi_write_buffer()- Populate the WRB with task info
  2014. * @pwrb: ptr to the WRB entry
  2015. * @task: iscsi task which is to be executed
  2016. **/
  2017. static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  2018. {
  2019. struct iscsi_sge *psgl;
  2020. struct beiscsi_io_task *io_task = task->dd_data;
  2021. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  2022. struct beiscsi_hba *phba = beiscsi_conn->phba;
  2023. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  2024. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  2025. io_task->bhs_pa.u.a32.address_lo);
  2026. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  2027. io_task->bhs_pa.u.a32.address_hi);
  2028. if (task->data) {
  2029. if (task->data_count) {
  2030. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  2031. io_task->mtask_addr = pci_map_single(phba->pcidev,
  2032. task->data,
  2033. task->data_count,
  2034. PCI_DMA_TODEVICE);
  2035. io_task->mtask_data_count = task->data_count;
  2036. } else {
  2037. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  2038. io_task->mtask_addr = 0;
  2039. }
  2040. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  2041. lower_32_bits(io_task->mtask_addr));
  2042. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  2043. upper_32_bits(io_task->mtask_addr));
  2044. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  2045. task->data_count);
  2046. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  2047. } else {
  2048. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  2049. io_task->mtask_addr = 0;
  2050. }
  2051. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2052. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  2053. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2054. io_task->bhs_pa.u.a32.address_hi);
  2055. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2056. io_task->bhs_pa.u.a32.address_lo);
  2057. if (task->data) {
  2058. psgl++;
  2059. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  2060. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  2061. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  2062. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  2063. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  2064. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2065. psgl++;
  2066. if (task->data) {
  2067. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2068. lower_32_bits(io_task->mtask_addr));
  2069. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2070. upper_32_bits(io_task->mtask_addr));
  2071. }
  2072. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  2073. }
  2074. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2075. }
  2076. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  2077. {
  2078. unsigned int num_cq_pages, num_async_pdu_buf_pages;
  2079. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  2080. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  2081. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2082. sizeof(struct sol_cqe));
  2083. num_async_pdu_buf_pages =
  2084. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  2085. phba->params.defpdu_hdr_sz);
  2086. num_async_pdu_buf_sgl_pages =
  2087. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  2088. sizeof(struct phys_addr));
  2089. num_async_pdu_data_pages =
  2090. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  2091. phba->params.defpdu_data_sz);
  2092. num_async_pdu_data_sgl_pages =
  2093. PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
  2094. sizeof(struct phys_addr));
  2095. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  2096. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  2097. BE_ISCSI_PDU_HEADER_SIZE;
  2098. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  2099. sizeof(struct hwi_context_memory);
  2100. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  2101. * (phba->params.wrbs_per_cxn)
  2102. * phba->params.cxns_per_ctrl;
  2103. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  2104. (phba->params.wrbs_per_cxn);
  2105. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  2106. phba->params.cxns_per_ctrl);
  2107. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  2108. phba->params.icds_per_ctrl;
  2109. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  2110. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  2111. phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
  2112. num_async_pdu_buf_pages * PAGE_SIZE;
  2113. phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
  2114. num_async_pdu_data_pages * PAGE_SIZE;
  2115. phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
  2116. num_async_pdu_buf_sgl_pages * PAGE_SIZE;
  2117. phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
  2118. num_async_pdu_data_sgl_pages * PAGE_SIZE;
  2119. phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
  2120. phba->params.asyncpdus_per_ctrl *
  2121. sizeof(struct async_pdu_handle);
  2122. phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
  2123. phba->params.asyncpdus_per_ctrl *
  2124. sizeof(struct async_pdu_handle);
  2125. phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
  2126. sizeof(struct hwi_async_pdu_context) +
  2127. (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
  2128. }
  2129. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  2130. {
  2131. struct be_mem_descriptor *mem_descr;
  2132. dma_addr_t bus_add;
  2133. struct mem_array *mem_arr, *mem_arr_orig;
  2134. unsigned int i, j, alloc_size, curr_alloc_size;
  2135. phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  2136. if (!phba->phwi_ctrlr)
  2137. return -ENOMEM;
  2138. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  2139. GFP_KERNEL);
  2140. if (!phba->init_mem) {
  2141. kfree(phba->phwi_ctrlr);
  2142. return -ENOMEM;
  2143. }
  2144. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  2145. GFP_KERNEL);
  2146. if (!mem_arr_orig) {
  2147. kfree(phba->init_mem);
  2148. kfree(phba->phwi_ctrlr);
  2149. return -ENOMEM;
  2150. }
  2151. mem_descr = phba->init_mem;
  2152. for (i = 0; i < SE_MEM_MAX; i++) {
  2153. j = 0;
  2154. mem_arr = mem_arr_orig;
  2155. alloc_size = phba->mem_req[i];
  2156. memset(mem_arr, 0, sizeof(struct mem_array) *
  2157. BEISCSI_MAX_FRAGS_INIT);
  2158. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  2159. do {
  2160. mem_arr->virtual_address = pci_alloc_consistent(
  2161. phba->pcidev,
  2162. curr_alloc_size,
  2163. &bus_add);
  2164. if (!mem_arr->virtual_address) {
  2165. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  2166. goto free_mem;
  2167. if (curr_alloc_size -
  2168. rounddown_pow_of_two(curr_alloc_size))
  2169. curr_alloc_size = rounddown_pow_of_two
  2170. (curr_alloc_size);
  2171. else
  2172. curr_alloc_size = curr_alloc_size / 2;
  2173. } else {
  2174. mem_arr->bus_address.u.
  2175. a64.address = (__u64) bus_add;
  2176. mem_arr->size = curr_alloc_size;
  2177. alloc_size -= curr_alloc_size;
  2178. curr_alloc_size = min(be_max_phys_size *
  2179. 1024, alloc_size);
  2180. j++;
  2181. mem_arr++;
  2182. }
  2183. } while (alloc_size);
  2184. mem_descr->num_elements = j;
  2185. mem_descr->size_in_bytes = phba->mem_req[i];
  2186. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  2187. GFP_KERNEL);
  2188. if (!mem_descr->mem_array)
  2189. goto free_mem;
  2190. memcpy(mem_descr->mem_array, mem_arr_orig,
  2191. sizeof(struct mem_array) * j);
  2192. mem_descr++;
  2193. }
  2194. kfree(mem_arr_orig);
  2195. return 0;
  2196. free_mem:
  2197. mem_descr->num_elements = j;
  2198. while ((i) || (j)) {
  2199. for (j = mem_descr->num_elements; j > 0; j--) {
  2200. pci_free_consistent(phba->pcidev,
  2201. mem_descr->mem_array[j - 1].size,
  2202. mem_descr->mem_array[j - 1].
  2203. virtual_address,
  2204. (unsigned long)mem_descr->
  2205. mem_array[j - 1].
  2206. bus_address.u.a64.address);
  2207. }
  2208. if (i) {
  2209. i--;
  2210. kfree(mem_descr->mem_array);
  2211. mem_descr--;
  2212. }
  2213. }
  2214. kfree(mem_arr_orig);
  2215. kfree(phba->init_mem);
  2216. kfree(phba->phwi_ctrlr);
  2217. return -ENOMEM;
  2218. }
  2219. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  2220. {
  2221. beiscsi_find_mem_req(phba);
  2222. return beiscsi_alloc_mem(phba);
  2223. }
  2224. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  2225. {
  2226. struct pdu_data_out *pdata_out;
  2227. struct pdu_nop_out *pnop_out;
  2228. struct be_mem_descriptor *mem_descr;
  2229. mem_descr = phba->init_mem;
  2230. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  2231. pdata_out =
  2232. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  2233. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2234. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  2235. IIOC_SCSI_DATA);
  2236. pnop_out =
  2237. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  2238. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  2239. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2240. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  2241. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  2242. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  2243. }
  2244. static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  2245. {
  2246. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  2247. struct wrb_handle *pwrb_handle = NULL;
  2248. struct hwi_controller *phwi_ctrlr;
  2249. struct hwi_wrb_context *pwrb_context;
  2250. struct iscsi_wrb *pwrb = NULL;
  2251. unsigned int num_cxn_wrbh = 0;
  2252. unsigned int num_cxn_wrb = 0, j, idx = 0, index;
  2253. mem_descr_wrbh = phba->init_mem;
  2254. mem_descr_wrbh += HWI_MEM_WRBH;
  2255. mem_descr_wrb = phba->init_mem;
  2256. mem_descr_wrb += HWI_MEM_WRB;
  2257. phwi_ctrlr = phba->phwi_ctrlr;
  2258. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2259. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2260. pwrb_context->pwrb_handle_base =
  2261. kzalloc(sizeof(struct wrb_handle *) *
  2262. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2263. if (!pwrb_context->pwrb_handle_base) {
  2264. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2265. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2266. goto init_wrb_hndl_failed;
  2267. }
  2268. pwrb_context->pwrb_handle_basestd =
  2269. kzalloc(sizeof(struct wrb_handle *) *
  2270. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2271. if (!pwrb_context->pwrb_handle_basestd) {
  2272. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2273. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2274. goto init_wrb_hndl_failed;
  2275. }
  2276. if (!num_cxn_wrbh) {
  2277. pwrb_handle =
  2278. mem_descr_wrbh->mem_array[idx].virtual_address;
  2279. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  2280. ((sizeof(struct wrb_handle)) *
  2281. phba->params.wrbs_per_cxn));
  2282. idx++;
  2283. }
  2284. pwrb_context->alloc_index = 0;
  2285. pwrb_context->wrb_handles_available = 0;
  2286. pwrb_context->free_index = 0;
  2287. if (num_cxn_wrbh) {
  2288. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2289. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2290. pwrb_context->pwrb_handle_basestd[j] =
  2291. pwrb_handle;
  2292. pwrb_context->wrb_handles_available++;
  2293. pwrb_handle->wrb_index = j;
  2294. pwrb_handle++;
  2295. }
  2296. num_cxn_wrbh--;
  2297. }
  2298. }
  2299. idx = 0;
  2300. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2301. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2302. if (!num_cxn_wrb) {
  2303. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2304. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2305. ((sizeof(struct iscsi_wrb) *
  2306. phba->params.wrbs_per_cxn));
  2307. idx++;
  2308. }
  2309. if (num_cxn_wrb) {
  2310. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2311. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2312. pwrb_handle->pwrb = pwrb;
  2313. pwrb++;
  2314. }
  2315. num_cxn_wrb--;
  2316. }
  2317. }
  2318. return 0;
  2319. init_wrb_hndl_failed:
  2320. for (j = index; j > 0; j--) {
  2321. pwrb_context = &phwi_ctrlr->wrb_context[j];
  2322. kfree(pwrb_context->pwrb_handle_base);
  2323. kfree(pwrb_context->pwrb_handle_basestd);
  2324. }
  2325. return -ENOMEM;
  2326. }
  2327. static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2328. {
  2329. struct hwi_controller *phwi_ctrlr;
  2330. struct hba_parameters *p = &phba->params;
  2331. struct hwi_async_pdu_context *pasync_ctx;
  2332. struct async_pdu_handle *pasync_header_h, *pasync_data_h;
  2333. unsigned int index, idx, num_per_mem, num_async_data;
  2334. struct be_mem_descriptor *mem_descr;
  2335. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2336. mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
  2337. phwi_ctrlr = phba->phwi_ctrlr;
  2338. phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
  2339. mem_descr->mem_array[0].virtual_address;
  2340. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
  2341. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2342. pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
  2343. pasync_ctx->buffer_size = p->defpdu_hdr_sz;
  2344. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2345. mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
  2346. if (mem_descr->mem_array[0].virtual_address) {
  2347. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2348. "BM_%d : hwi_init_async_pdu_ctx"
  2349. " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
  2350. mem_descr->mem_array[0].virtual_address);
  2351. } else
  2352. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2353. "BM_%d : No Virtual address\n");
  2354. pasync_ctx->async_header.va_base =
  2355. mem_descr->mem_array[0].virtual_address;
  2356. pasync_ctx->async_header.pa_base.u.a64.address =
  2357. mem_descr->mem_array[0].bus_address.u.a64.address;
  2358. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2359. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2360. if (mem_descr->mem_array[0].virtual_address) {
  2361. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2362. "BM_%d : hwi_init_async_pdu_ctx"
  2363. " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
  2364. mem_descr->mem_array[0].virtual_address);
  2365. } else
  2366. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2367. "BM_%d : No Virtual address\n");
  2368. pasync_ctx->async_header.ring_base =
  2369. mem_descr->mem_array[0].virtual_address;
  2370. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2371. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
  2372. if (mem_descr->mem_array[0].virtual_address) {
  2373. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2374. "BM_%d : hwi_init_async_pdu_ctx"
  2375. " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
  2376. mem_descr->mem_array[0].virtual_address);
  2377. } else
  2378. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2379. "BM_%d : No Virtual address\n");
  2380. pasync_ctx->async_header.handle_base =
  2381. mem_descr->mem_array[0].virtual_address;
  2382. pasync_ctx->async_header.writables = 0;
  2383. INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
  2384. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2385. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2386. if (mem_descr->mem_array[0].virtual_address) {
  2387. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2388. "BM_%d : hwi_init_async_pdu_ctx"
  2389. " HWI_MEM_ASYNC_DATA_RING va=%p\n",
  2390. mem_descr->mem_array[0].virtual_address);
  2391. } else
  2392. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2393. "BM_%d : No Virtual address\n");
  2394. pasync_ctx->async_data.ring_base =
  2395. mem_descr->mem_array[0].virtual_address;
  2396. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2397. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
  2398. if (!mem_descr->mem_array[0].virtual_address)
  2399. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2400. "BM_%d : No Virtual address\n");
  2401. pasync_ctx->async_data.handle_base =
  2402. mem_descr->mem_array[0].virtual_address;
  2403. pasync_ctx->async_data.writables = 0;
  2404. INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
  2405. pasync_header_h =
  2406. (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
  2407. pasync_data_h =
  2408. (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
  2409. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2410. mem_descr += HWI_MEM_ASYNC_DATA_BUF;
  2411. if (mem_descr->mem_array[0].virtual_address) {
  2412. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2413. "BM_%d : hwi_init_async_pdu_ctx"
  2414. " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
  2415. mem_descr->mem_array[0].virtual_address);
  2416. } else
  2417. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  2418. "BM_%d : No Virtual address\n");
  2419. idx = 0;
  2420. pasync_ctx->async_data.va_base =
  2421. mem_descr->mem_array[idx].virtual_address;
  2422. pasync_ctx->async_data.pa_base.u.a64.address =
  2423. mem_descr->mem_array[idx].bus_address.u.a64.address;
  2424. num_async_data = ((mem_descr->mem_array[idx].size) /
  2425. phba->params.defpdu_data_sz);
  2426. num_per_mem = 0;
  2427. for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
  2428. pasync_header_h->cri = -1;
  2429. pasync_header_h->index = (char)index;
  2430. INIT_LIST_HEAD(&pasync_header_h->link);
  2431. pasync_header_h->pbuffer =
  2432. (void *)((unsigned long)
  2433. (pasync_ctx->async_header.va_base) +
  2434. (p->defpdu_hdr_sz * index));
  2435. pasync_header_h->pa.u.a64.address =
  2436. pasync_ctx->async_header.pa_base.u.a64.address +
  2437. (p->defpdu_hdr_sz * index);
  2438. list_add_tail(&pasync_header_h->link,
  2439. &pasync_ctx->async_header.free_list);
  2440. pasync_header_h++;
  2441. pasync_ctx->async_header.free_entries++;
  2442. pasync_ctx->async_header.writables++;
  2443. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
  2444. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2445. header_busy_list);
  2446. pasync_data_h->cri = -1;
  2447. pasync_data_h->index = (char)index;
  2448. INIT_LIST_HEAD(&pasync_data_h->link);
  2449. if (!num_async_data) {
  2450. num_per_mem = 0;
  2451. idx++;
  2452. pasync_ctx->async_data.va_base =
  2453. mem_descr->mem_array[idx].virtual_address;
  2454. pasync_ctx->async_data.pa_base.u.a64.address =
  2455. mem_descr->mem_array[idx].
  2456. bus_address.u.a64.address;
  2457. num_async_data = ((mem_descr->mem_array[idx].size) /
  2458. phba->params.defpdu_data_sz);
  2459. }
  2460. pasync_data_h->pbuffer =
  2461. (void *)((unsigned long)
  2462. (pasync_ctx->async_data.va_base) +
  2463. (p->defpdu_data_sz * num_per_mem));
  2464. pasync_data_h->pa.u.a64.address =
  2465. pasync_ctx->async_data.pa_base.u.a64.address +
  2466. (p->defpdu_data_sz * num_per_mem);
  2467. num_per_mem++;
  2468. num_async_data--;
  2469. list_add_tail(&pasync_data_h->link,
  2470. &pasync_ctx->async_data.free_list);
  2471. pasync_data_h++;
  2472. pasync_ctx->async_data.free_entries++;
  2473. pasync_ctx->async_data.writables++;
  2474. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
  2475. }
  2476. pasync_ctx->async_header.host_write_ptr = 0;
  2477. pasync_ctx->async_header.ep_read_ptr = -1;
  2478. pasync_ctx->async_data.host_write_ptr = 0;
  2479. pasync_ctx->async_data.ep_read_ptr = -1;
  2480. }
  2481. static int
  2482. be_sgl_create_contiguous(void *virtual_address,
  2483. u64 physical_address, u32 length,
  2484. struct be_dma_mem *sgl)
  2485. {
  2486. WARN_ON(!virtual_address);
  2487. WARN_ON(!physical_address);
  2488. WARN_ON(!length > 0);
  2489. WARN_ON(!sgl);
  2490. sgl->va = virtual_address;
  2491. sgl->dma = (unsigned long)physical_address;
  2492. sgl->size = length;
  2493. return 0;
  2494. }
  2495. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2496. {
  2497. memset(sgl, 0, sizeof(*sgl));
  2498. }
  2499. static void
  2500. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2501. struct mem_array *pmem, struct be_dma_mem *sgl)
  2502. {
  2503. if (sgl->va)
  2504. be_sgl_destroy_contiguous(sgl);
  2505. be_sgl_create_contiguous(pmem->virtual_address,
  2506. pmem->bus_address.u.a64.address,
  2507. pmem->size, sgl);
  2508. }
  2509. static void
  2510. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2511. struct mem_array *pmem, struct be_dma_mem *sgl)
  2512. {
  2513. if (sgl->va)
  2514. be_sgl_destroy_contiguous(sgl);
  2515. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2516. pmem->bus_address.u.a64.address,
  2517. pmem->size, sgl);
  2518. }
  2519. static int be_fill_queue(struct be_queue_info *q,
  2520. u16 len, u16 entry_size, void *vaddress)
  2521. {
  2522. struct be_dma_mem *mem = &q->dma_mem;
  2523. memset(q, 0, sizeof(*q));
  2524. q->len = len;
  2525. q->entry_size = entry_size;
  2526. mem->size = len * entry_size;
  2527. mem->va = vaddress;
  2528. if (!mem->va)
  2529. return -ENOMEM;
  2530. memset(mem->va, 0, mem->size);
  2531. return 0;
  2532. }
  2533. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2534. struct hwi_context_memory *phwi_context)
  2535. {
  2536. unsigned int i, num_eq_pages;
  2537. int ret = 0, eq_for_mcc;
  2538. struct be_queue_info *eq;
  2539. struct be_dma_mem *mem;
  2540. void *eq_vaddress;
  2541. dma_addr_t paddr;
  2542. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2543. sizeof(struct be_eq_entry));
  2544. if (phba->msix_enabled)
  2545. eq_for_mcc = 1;
  2546. else
  2547. eq_for_mcc = 0;
  2548. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2549. eq = &phwi_context->be_eq[i].q;
  2550. mem = &eq->dma_mem;
  2551. phwi_context->be_eq[i].phba = phba;
  2552. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2553. num_eq_pages * PAGE_SIZE,
  2554. &paddr);
  2555. if (!eq_vaddress)
  2556. goto create_eq_error;
  2557. mem->va = eq_vaddress;
  2558. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2559. sizeof(struct be_eq_entry), eq_vaddress);
  2560. if (ret) {
  2561. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2562. "BM_%d : be_fill_queue Failed for EQ\n");
  2563. goto create_eq_error;
  2564. }
  2565. mem->dma = paddr;
  2566. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2567. phwi_context->cur_eqd);
  2568. if (ret) {
  2569. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2570. "BM_%d : beiscsi_cmd_eq_create"
  2571. "Failed for EQ\n");
  2572. goto create_eq_error;
  2573. }
  2574. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2575. "BM_%d : eqid = %d\n",
  2576. phwi_context->be_eq[i].q.id);
  2577. }
  2578. return 0;
  2579. create_eq_error:
  2580. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2581. eq = &phwi_context->be_eq[i].q;
  2582. mem = &eq->dma_mem;
  2583. if (mem->va)
  2584. pci_free_consistent(phba->pcidev, num_eq_pages
  2585. * PAGE_SIZE,
  2586. mem->va, mem->dma);
  2587. }
  2588. return ret;
  2589. }
  2590. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2591. struct hwi_context_memory *phwi_context)
  2592. {
  2593. unsigned int i, num_cq_pages;
  2594. int ret = 0;
  2595. struct be_queue_info *cq, *eq;
  2596. struct be_dma_mem *mem;
  2597. struct be_eq_obj *pbe_eq;
  2598. void *cq_vaddress;
  2599. dma_addr_t paddr;
  2600. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2601. sizeof(struct sol_cqe));
  2602. for (i = 0; i < phba->num_cpus; i++) {
  2603. cq = &phwi_context->be_cq[i];
  2604. eq = &phwi_context->be_eq[i].q;
  2605. pbe_eq = &phwi_context->be_eq[i];
  2606. pbe_eq->cq = cq;
  2607. pbe_eq->phba = phba;
  2608. mem = &cq->dma_mem;
  2609. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2610. num_cq_pages * PAGE_SIZE,
  2611. &paddr);
  2612. if (!cq_vaddress)
  2613. goto create_cq_error;
  2614. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2615. sizeof(struct sol_cqe), cq_vaddress);
  2616. if (ret) {
  2617. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2618. "BM_%d : be_fill_queue Failed "
  2619. "for ISCSI CQ\n");
  2620. goto create_cq_error;
  2621. }
  2622. mem->dma = paddr;
  2623. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2624. false, 0);
  2625. if (ret) {
  2626. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2627. "BM_%d : beiscsi_cmd_eq_create"
  2628. "Failed for ISCSI CQ\n");
  2629. goto create_cq_error;
  2630. }
  2631. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2632. "BM_%d : iscsi cq_id is %d for eq_id %d\n"
  2633. "iSCSI CQ CREATED\n", cq->id, eq->id);
  2634. }
  2635. return 0;
  2636. create_cq_error:
  2637. for (i = 0; i < phba->num_cpus; i++) {
  2638. cq = &phwi_context->be_cq[i];
  2639. mem = &cq->dma_mem;
  2640. if (mem->va)
  2641. pci_free_consistent(phba->pcidev, num_cq_pages
  2642. * PAGE_SIZE,
  2643. mem->va, mem->dma);
  2644. }
  2645. return ret;
  2646. }
  2647. static int
  2648. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2649. struct hwi_context_memory *phwi_context,
  2650. struct hwi_controller *phwi_ctrlr,
  2651. unsigned int def_pdu_ring_sz)
  2652. {
  2653. unsigned int idx;
  2654. int ret;
  2655. struct be_queue_info *dq, *cq;
  2656. struct be_dma_mem *mem;
  2657. struct be_mem_descriptor *mem_descr;
  2658. void *dq_vaddress;
  2659. idx = 0;
  2660. dq = &phwi_context->be_def_hdrq;
  2661. cq = &phwi_context->be_cq[0];
  2662. mem = &dq->dma_mem;
  2663. mem_descr = phba->init_mem;
  2664. mem_descr += HWI_MEM_ASYNC_HEADER_RING;
  2665. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2666. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2667. sizeof(struct phys_addr),
  2668. sizeof(struct phys_addr), dq_vaddress);
  2669. if (ret) {
  2670. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2671. "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
  2672. return ret;
  2673. }
  2674. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2675. bus_address.u.a64.address;
  2676. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2677. def_pdu_ring_sz,
  2678. phba->params.defpdu_hdr_sz);
  2679. if (ret) {
  2680. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2681. "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
  2682. return ret;
  2683. }
  2684. phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
  2685. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2686. "BM_%d : iscsi def pdu id is %d\n",
  2687. phwi_context->be_def_hdrq.id);
  2688. hwi_post_async_buffers(phba, 1);
  2689. return 0;
  2690. }
  2691. static int
  2692. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2693. struct hwi_context_memory *phwi_context,
  2694. struct hwi_controller *phwi_ctrlr,
  2695. unsigned int def_pdu_ring_sz)
  2696. {
  2697. unsigned int idx;
  2698. int ret;
  2699. struct be_queue_info *dataq, *cq;
  2700. struct be_dma_mem *mem;
  2701. struct be_mem_descriptor *mem_descr;
  2702. void *dq_vaddress;
  2703. idx = 0;
  2704. dataq = &phwi_context->be_def_dataq;
  2705. cq = &phwi_context->be_cq[0];
  2706. mem = &dataq->dma_mem;
  2707. mem_descr = phba->init_mem;
  2708. mem_descr += HWI_MEM_ASYNC_DATA_RING;
  2709. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2710. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2711. sizeof(struct phys_addr),
  2712. sizeof(struct phys_addr), dq_vaddress);
  2713. if (ret) {
  2714. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2715. "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
  2716. return ret;
  2717. }
  2718. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2719. bus_address.u.a64.address;
  2720. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2721. def_pdu_ring_sz,
  2722. phba->params.defpdu_data_sz);
  2723. if (ret) {
  2724. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2725. "BM_%d be_cmd_create_default_pdu_queue"
  2726. " Failed for DEF PDU DATA\n");
  2727. return ret;
  2728. }
  2729. phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
  2730. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2731. "BM_%d : iscsi def data id is %d\n",
  2732. phwi_context->be_def_dataq.id);
  2733. hwi_post_async_buffers(phba, 0);
  2734. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2735. "BM_%d : DEFAULT PDU DATA RING CREATED\n");
  2736. return 0;
  2737. }
  2738. static int
  2739. beiscsi_post_pages(struct beiscsi_hba *phba)
  2740. {
  2741. struct be_mem_descriptor *mem_descr;
  2742. struct mem_array *pm_arr;
  2743. unsigned int page_offset, i;
  2744. struct be_dma_mem sgl;
  2745. int status;
  2746. mem_descr = phba->init_mem;
  2747. mem_descr += HWI_MEM_SGE;
  2748. pm_arr = mem_descr->mem_array;
  2749. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2750. phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
  2751. for (i = 0; i < mem_descr->num_elements; i++) {
  2752. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2753. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2754. page_offset,
  2755. (pm_arr->size / PAGE_SIZE));
  2756. page_offset += pm_arr->size / PAGE_SIZE;
  2757. if (status != 0) {
  2758. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2759. "BM_%d : post sgl failed.\n");
  2760. return status;
  2761. }
  2762. pm_arr++;
  2763. }
  2764. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2765. "BM_%d : POSTED PAGES\n");
  2766. return 0;
  2767. }
  2768. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2769. {
  2770. struct be_dma_mem *mem = &q->dma_mem;
  2771. if (mem->va) {
  2772. pci_free_consistent(phba->pcidev, mem->size,
  2773. mem->va, mem->dma);
  2774. mem->va = NULL;
  2775. }
  2776. }
  2777. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2778. u16 len, u16 entry_size)
  2779. {
  2780. struct be_dma_mem *mem = &q->dma_mem;
  2781. memset(q, 0, sizeof(*q));
  2782. q->len = len;
  2783. q->entry_size = entry_size;
  2784. mem->size = len * entry_size;
  2785. mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2786. if (!mem->va)
  2787. return -ENOMEM;
  2788. memset(mem->va, 0, mem->size);
  2789. return 0;
  2790. }
  2791. static int
  2792. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  2793. struct hwi_context_memory *phwi_context,
  2794. struct hwi_controller *phwi_ctrlr)
  2795. {
  2796. unsigned int wrb_mem_index, offset, size, num_wrb_rings;
  2797. u64 pa_addr_lo;
  2798. unsigned int idx, num, i;
  2799. struct mem_array *pwrb_arr;
  2800. void *wrb_vaddr;
  2801. struct be_dma_mem sgl;
  2802. struct be_mem_descriptor *mem_descr;
  2803. int status;
  2804. idx = 0;
  2805. mem_descr = phba->init_mem;
  2806. mem_descr += HWI_MEM_WRB;
  2807. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  2808. GFP_KERNEL);
  2809. if (!pwrb_arr) {
  2810. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2811. "BM_%d : Memory alloc failed in create wrb ring.\n");
  2812. return -ENOMEM;
  2813. }
  2814. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2815. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  2816. num_wrb_rings = mem_descr->mem_array[idx].size /
  2817. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  2818. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  2819. if (num_wrb_rings) {
  2820. pwrb_arr[num].virtual_address = wrb_vaddr;
  2821. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  2822. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2823. sizeof(struct iscsi_wrb);
  2824. wrb_vaddr += pwrb_arr[num].size;
  2825. pa_addr_lo += pwrb_arr[num].size;
  2826. num_wrb_rings--;
  2827. } else {
  2828. idx++;
  2829. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  2830. pa_addr_lo = mem_descr->mem_array[idx].\
  2831. bus_address.u.a64.address;
  2832. num_wrb_rings = mem_descr->mem_array[idx].size /
  2833. (phba->params.wrbs_per_cxn *
  2834. sizeof(struct iscsi_wrb));
  2835. pwrb_arr[num].virtual_address = wrb_vaddr;
  2836. pwrb_arr[num].bus_address.u.a64.address\
  2837. = pa_addr_lo;
  2838. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  2839. sizeof(struct iscsi_wrb);
  2840. wrb_vaddr += pwrb_arr[num].size;
  2841. pa_addr_lo += pwrb_arr[num].size;
  2842. num_wrb_rings--;
  2843. }
  2844. }
  2845. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2846. wrb_mem_index = 0;
  2847. offset = 0;
  2848. size = 0;
  2849. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  2850. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  2851. &phwi_context->be_wrbq[i]);
  2852. if (status != 0) {
  2853. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2854. "BM_%d : wrbq create failed.");
  2855. kfree(pwrb_arr);
  2856. return status;
  2857. }
  2858. phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
  2859. id;
  2860. }
  2861. kfree(pwrb_arr);
  2862. return 0;
  2863. }
  2864. static void free_wrb_handles(struct beiscsi_hba *phba)
  2865. {
  2866. unsigned int index;
  2867. struct hwi_controller *phwi_ctrlr;
  2868. struct hwi_wrb_context *pwrb_context;
  2869. phwi_ctrlr = phba->phwi_ctrlr;
  2870. for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
  2871. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2872. kfree(pwrb_context->pwrb_handle_base);
  2873. kfree(pwrb_context->pwrb_handle_basestd);
  2874. }
  2875. }
  2876. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  2877. {
  2878. struct be_queue_info *q;
  2879. struct be_ctrl_info *ctrl = &phba->ctrl;
  2880. q = &phba->ctrl.mcc_obj.q;
  2881. if (q->created)
  2882. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  2883. be_queue_free(phba, q);
  2884. q = &phba->ctrl.mcc_obj.cq;
  2885. if (q->created)
  2886. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2887. be_queue_free(phba, q);
  2888. }
  2889. static void hwi_cleanup(struct beiscsi_hba *phba)
  2890. {
  2891. struct be_queue_info *q;
  2892. struct be_ctrl_info *ctrl = &phba->ctrl;
  2893. struct hwi_controller *phwi_ctrlr;
  2894. struct hwi_context_memory *phwi_context;
  2895. int i, eq_num;
  2896. phwi_ctrlr = phba->phwi_ctrlr;
  2897. phwi_context = phwi_ctrlr->phwi_ctxt;
  2898. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  2899. q = &phwi_context->be_wrbq[i];
  2900. if (q->created)
  2901. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  2902. }
  2903. free_wrb_handles(phba);
  2904. q = &phwi_context->be_def_hdrq;
  2905. if (q->created)
  2906. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2907. q = &phwi_context->be_def_dataq;
  2908. if (q->created)
  2909. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  2910. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  2911. for (i = 0; i < (phba->num_cpus); i++) {
  2912. q = &phwi_context->be_cq[i];
  2913. if (q->created)
  2914. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  2915. }
  2916. if (phba->msix_enabled)
  2917. eq_num = 1;
  2918. else
  2919. eq_num = 0;
  2920. for (i = 0; i < (phba->num_cpus + eq_num); i++) {
  2921. q = &phwi_context->be_eq[i].q;
  2922. if (q->created)
  2923. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  2924. }
  2925. be_mcc_queues_destroy(phba);
  2926. }
  2927. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  2928. struct hwi_context_memory *phwi_context)
  2929. {
  2930. struct be_queue_info *q, *cq;
  2931. struct be_ctrl_info *ctrl = &phba->ctrl;
  2932. /* Alloc MCC compl queue */
  2933. cq = &phba->ctrl.mcc_obj.cq;
  2934. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  2935. sizeof(struct be_mcc_compl)))
  2936. goto err;
  2937. /* Ask BE to create MCC compl queue; */
  2938. if (phba->msix_enabled) {
  2939. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
  2940. [phba->num_cpus].q, false, true, 0))
  2941. goto mcc_cq_free;
  2942. } else {
  2943. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  2944. false, true, 0))
  2945. goto mcc_cq_free;
  2946. }
  2947. /* Alloc MCC queue */
  2948. q = &phba->ctrl.mcc_obj.q;
  2949. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  2950. goto mcc_cq_destroy;
  2951. /* Ask BE to create MCC queue */
  2952. if (beiscsi_cmd_mccq_create(phba, q, cq))
  2953. goto mcc_q_free;
  2954. return 0;
  2955. mcc_q_free:
  2956. be_queue_free(phba, q);
  2957. mcc_cq_destroy:
  2958. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  2959. mcc_cq_free:
  2960. be_queue_free(phba, cq);
  2961. err:
  2962. return -ENOMEM;
  2963. }
  2964. /**
  2965. * find_num_cpus()- Get the CPU online count
  2966. * @phba: ptr to priv structure
  2967. *
  2968. * CPU count is used for creating EQ.
  2969. **/
  2970. static void find_num_cpus(struct beiscsi_hba *phba)
  2971. {
  2972. int num_cpus = 0;
  2973. num_cpus = num_online_cpus();
  2974. switch (phba->generation) {
  2975. case BE_GEN2:
  2976. case BE_GEN3:
  2977. phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
  2978. BEISCSI_MAX_NUM_CPUS : num_cpus;
  2979. break;
  2980. case BE_GEN4:
  2981. phba->num_cpus = (num_cpus > OC_SKH_MAX_NUM_CPUS) ?
  2982. OC_SKH_MAX_NUM_CPUS : num_cpus;
  2983. break;
  2984. default:
  2985. phba->num_cpus = 1;
  2986. }
  2987. }
  2988. static int hwi_init_port(struct beiscsi_hba *phba)
  2989. {
  2990. struct hwi_controller *phwi_ctrlr;
  2991. struct hwi_context_memory *phwi_context;
  2992. unsigned int def_pdu_ring_sz;
  2993. struct be_ctrl_info *ctrl = &phba->ctrl;
  2994. int status;
  2995. def_pdu_ring_sz =
  2996. phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
  2997. phwi_ctrlr = phba->phwi_ctrlr;
  2998. phwi_context = phwi_ctrlr->phwi_ctxt;
  2999. phwi_context->max_eqd = 0;
  3000. phwi_context->min_eqd = 0;
  3001. phwi_context->cur_eqd = 64;
  3002. be_cmd_fw_initialize(&phba->ctrl);
  3003. status = beiscsi_create_eqs(phba, phwi_context);
  3004. if (status != 0) {
  3005. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3006. "BM_%d : EQ not created\n");
  3007. goto error;
  3008. }
  3009. status = be_mcc_queues_create(phba, phwi_context);
  3010. if (status != 0)
  3011. goto error;
  3012. status = mgmt_check_supported_fw(ctrl, phba);
  3013. if (status != 0) {
  3014. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3015. "BM_%d : Unsupported fw version\n");
  3016. goto error;
  3017. }
  3018. status = beiscsi_create_cqs(phba, phwi_context);
  3019. if (status != 0) {
  3020. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3021. "BM_%d : CQ not created\n");
  3022. goto error;
  3023. }
  3024. status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
  3025. def_pdu_ring_sz);
  3026. if (status != 0) {
  3027. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3028. "BM_%d : Default Header not created\n");
  3029. goto error;
  3030. }
  3031. status = beiscsi_create_def_data(phba, phwi_context,
  3032. phwi_ctrlr, def_pdu_ring_sz);
  3033. if (status != 0) {
  3034. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3035. "BM_%d : Default Data not created\n");
  3036. goto error;
  3037. }
  3038. status = beiscsi_post_pages(phba);
  3039. if (status != 0) {
  3040. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3041. "BM_%d : Post SGL Pages Failed\n");
  3042. goto error;
  3043. }
  3044. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  3045. if (status != 0) {
  3046. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3047. "BM_%d : WRB Rings not created\n");
  3048. goto error;
  3049. }
  3050. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3051. "BM_%d : hwi_init_port success\n");
  3052. return 0;
  3053. error:
  3054. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3055. "BM_%d : hwi_init_port failed");
  3056. hwi_cleanup(phba);
  3057. return status;
  3058. }
  3059. static int hwi_init_controller(struct beiscsi_hba *phba)
  3060. {
  3061. struct hwi_controller *phwi_ctrlr;
  3062. phwi_ctrlr = phba->phwi_ctrlr;
  3063. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  3064. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  3065. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  3066. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3067. "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
  3068. phwi_ctrlr->phwi_ctxt);
  3069. } else {
  3070. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3071. "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
  3072. "than one element.Failing to load\n");
  3073. return -ENOMEM;
  3074. }
  3075. iscsi_init_global_templates(phba);
  3076. if (beiscsi_init_wrb_handle(phba))
  3077. return -ENOMEM;
  3078. hwi_init_async_pdu_ctx(phba);
  3079. if (hwi_init_port(phba) != 0) {
  3080. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3081. "BM_%d : hwi_init_controller failed\n");
  3082. return -ENOMEM;
  3083. }
  3084. return 0;
  3085. }
  3086. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  3087. {
  3088. struct be_mem_descriptor *mem_descr;
  3089. int i, j;
  3090. mem_descr = phba->init_mem;
  3091. i = 0;
  3092. j = 0;
  3093. for (i = 0; i < SE_MEM_MAX; i++) {
  3094. for (j = mem_descr->num_elements; j > 0; j--) {
  3095. pci_free_consistent(phba->pcidev,
  3096. mem_descr->mem_array[j - 1].size,
  3097. mem_descr->mem_array[j - 1].virtual_address,
  3098. (unsigned long)mem_descr->mem_array[j - 1].
  3099. bus_address.u.a64.address);
  3100. }
  3101. kfree(mem_descr->mem_array);
  3102. mem_descr++;
  3103. }
  3104. kfree(phba->init_mem);
  3105. kfree(phba->phwi_ctrlr);
  3106. }
  3107. static int beiscsi_init_controller(struct beiscsi_hba *phba)
  3108. {
  3109. int ret = -ENOMEM;
  3110. ret = beiscsi_get_memory(phba);
  3111. if (ret < 0) {
  3112. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3113. "BM_%d : beiscsi_dev_probe -"
  3114. "Failed in beiscsi_alloc_memory\n");
  3115. return ret;
  3116. }
  3117. ret = hwi_init_controller(phba);
  3118. if (ret)
  3119. goto free_init;
  3120. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3121. "BM_%d : Return success from beiscsi_init_controller");
  3122. return 0;
  3123. free_init:
  3124. beiscsi_free_mem(phba);
  3125. return ret;
  3126. }
  3127. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  3128. {
  3129. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  3130. struct sgl_handle *psgl_handle;
  3131. struct iscsi_sge *pfrag;
  3132. unsigned int arr_index, i, idx;
  3133. phba->io_sgl_hndl_avbl = 0;
  3134. phba->eh_sgl_hndl_avbl = 0;
  3135. mem_descr_sglh = phba->init_mem;
  3136. mem_descr_sglh += HWI_MEM_SGLH;
  3137. if (1 == mem_descr_sglh->num_elements) {
  3138. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3139. phba->params.ios_per_ctrl,
  3140. GFP_KERNEL);
  3141. if (!phba->io_sgl_hndl_base) {
  3142. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3143. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3144. return -ENOMEM;
  3145. }
  3146. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3147. (phba->params.icds_per_ctrl -
  3148. phba->params.ios_per_ctrl),
  3149. GFP_KERNEL);
  3150. if (!phba->eh_sgl_hndl_base) {
  3151. kfree(phba->io_sgl_hndl_base);
  3152. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3153. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3154. return -ENOMEM;
  3155. }
  3156. } else {
  3157. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3158. "BM_%d : HWI_MEM_SGLH is more than one element."
  3159. "Failing to load\n");
  3160. return -ENOMEM;
  3161. }
  3162. arr_index = 0;
  3163. idx = 0;
  3164. while (idx < mem_descr_sglh->num_elements) {
  3165. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  3166. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  3167. sizeof(struct sgl_handle)); i++) {
  3168. if (arr_index < phba->params.ios_per_ctrl) {
  3169. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  3170. phba->io_sgl_hndl_avbl++;
  3171. arr_index++;
  3172. } else {
  3173. phba->eh_sgl_hndl_base[arr_index -
  3174. phba->params.ios_per_ctrl] =
  3175. psgl_handle;
  3176. arr_index++;
  3177. phba->eh_sgl_hndl_avbl++;
  3178. }
  3179. psgl_handle++;
  3180. }
  3181. idx++;
  3182. }
  3183. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3184. "BM_%d : phba->io_sgl_hndl_avbl=%d"
  3185. "phba->eh_sgl_hndl_avbl=%d\n",
  3186. phba->io_sgl_hndl_avbl,
  3187. phba->eh_sgl_hndl_avbl);
  3188. mem_descr_sg = phba->init_mem;
  3189. mem_descr_sg += HWI_MEM_SGE;
  3190. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3191. "\n BM_%d : mem_descr_sg->num_elements=%d\n",
  3192. mem_descr_sg->num_elements);
  3193. arr_index = 0;
  3194. idx = 0;
  3195. while (idx < mem_descr_sg->num_elements) {
  3196. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  3197. for (i = 0;
  3198. i < (mem_descr_sg->mem_array[idx].size) /
  3199. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  3200. i++) {
  3201. if (arr_index < phba->params.ios_per_ctrl)
  3202. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  3203. else
  3204. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  3205. phba->params.ios_per_ctrl];
  3206. psgl_handle->pfrag = pfrag;
  3207. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  3208. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  3209. pfrag += phba->params.num_sge_per_io;
  3210. psgl_handle->sgl_index =
  3211. phba->fw_config.iscsi_icd_start + arr_index++;
  3212. }
  3213. idx++;
  3214. }
  3215. phba->io_sgl_free_index = 0;
  3216. phba->io_sgl_alloc_index = 0;
  3217. phba->eh_sgl_free_index = 0;
  3218. phba->eh_sgl_alloc_index = 0;
  3219. return 0;
  3220. }
  3221. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  3222. {
  3223. int i, new_cid;
  3224. phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
  3225. GFP_KERNEL);
  3226. if (!phba->cid_array) {
  3227. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3228. "BM_%d : Failed to allocate memory in "
  3229. "hba_setup_cid_tbls\n");
  3230. return -ENOMEM;
  3231. }
  3232. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  3233. phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
  3234. if (!phba->ep_array) {
  3235. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3236. "BM_%d : Failed to allocate memory in "
  3237. "hba_setup_cid_tbls\n");
  3238. kfree(phba->cid_array);
  3239. return -ENOMEM;
  3240. }
  3241. new_cid = phba->fw_config.iscsi_cid_start;
  3242. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3243. phba->cid_array[i] = new_cid;
  3244. new_cid += 2;
  3245. }
  3246. phba->avlbl_cids = phba->params.cxns_per_ctrl;
  3247. return 0;
  3248. }
  3249. static void hwi_enable_intr(struct beiscsi_hba *phba)
  3250. {
  3251. struct be_ctrl_info *ctrl = &phba->ctrl;
  3252. struct hwi_controller *phwi_ctrlr;
  3253. struct hwi_context_memory *phwi_context;
  3254. struct be_queue_info *eq;
  3255. u8 __iomem *addr;
  3256. u32 reg, i;
  3257. u32 enabled;
  3258. phwi_ctrlr = phba->phwi_ctrlr;
  3259. phwi_context = phwi_ctrlr->phwi_ctxt;
  3260. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  3261. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  3262. reg = ioread32(addr);
  3263. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3264. if (!enabled) {
  3265. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3266. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3267. "BM_%d : reg =x%08x addr=%p\n", reg, addr);
  3268. iowrite32(reg, addr);
  3269. }
  3270. if (!phba->msix_enabled) {
  3271. eq = &phwi_context->be_eq[0].q;
  3272. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3273. "BM_%d : eq->id=%d\n", eq->id);
  3274. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3275. } else {
  3276. for (i = 0; i <= phba->num_cpus; i++) {
  3277. eq = &phwi_context->be_eq[i].q;
  3278. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3279. "BM_%d : eq->id=%d\n", eq->id);
  3280. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3281. }
  3282. }
  3283. }
  3284. static void hwi_disable_intr(struct beiscsi_hba *phba)
  3285. {
  3286. struct be_ctrl_info *ctrl = &phba->ctrl;
  3287. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  3288. u32 reg = ioread32(addr);
  3289. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3290. if (enabled) {
  3291. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3292. iowrite32(reg, addr);
  3293. } else
  3294. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  3295. "BM_%d : In hwi_disable_intr, Already Disabled\n");
  3296. }
  3297. /**
  3298. * beiscsi_get_boot_info()- Get the boot session info
  3299. * @phba: The device priv structure instance
  3300. *
  3301. * Get the boot target info and store in driver priv structure
  3302. *
  3303. * return values
  3304. * Success: 0
  3305. * Failure: Non-Zero Value
  3306. **/
  3307. static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
  3308. {
  3309. struct be_cmd_get_session_resp *session_resp;
  3310. struct be_mcc_wrb *wrb;
  3311. struct be_dma_mem nonemb_cmd;
  3312. unsigned int tag, wrb_num;
  3313. unsigned short status, extd_status;
  3314. unsigned int s_handle;
  3315. struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
  3316. int ret = -ENOMEM;
  3317. /* Get the session handle of the boot target */
  3318. ret = be_mgmt_get_boot_shandle(phba, &s_handle);
  3319. if (ret) {
  3320. beiscsi_log(phba, KERN_ERR,
  3321. BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
  3322. "BM_%d : No boot session\n");
  3323. return ret;
  3324. }
  3325. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  3326. sizeof(*session_resp),
  3327. &nonemb_cmd.dma);
  3328. if (nonemb_cmd.va == NULL) {
  3329. beiscsi_log(phba, KERN_ERR,
  3330. BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
  3331. "BM_%d : Failed to allocate memory for"
  3332. "beiscsi_get_session_info\n");
  3333. return -ENOMEM;
  3334. }
  3335. memset(nonemb_cmd.va, 0, sizeof(*session_resp));
  3336. tag = mgmt_get_session_info(phba, s_handle,
  3337. &nonemb_cmd);
  3338. if (!tag) {
  3339. beiscsi_log(phba, KERN_ERR,
  3340. BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
  3341. "BM_%d : beiscsi_get_session_info"
  3342. " Failed\n");
  3343. goto boot_freemem;
  3344. } else
  3345. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  3346. phba->ctrl.mcc_numtag[tag]);
  3347. wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
  3348. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  3349. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  3350. if (status || extd_status) {
  3351. beiscsi_log(phba, KERN_ERR,
  3352. BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
  3353. "BM_%d : beiscsi_get_session_info Failed"
  3354. " status = %d extd_status = %d\n",
  3355. status, extd_status);
  3356. free_mcc_tag(&phba->ctrl, tag);
  3357. goto boot_freemem;
  3358. }
  3359. wrb = queue_get_wrb(mccq, wrb_num);
  3360. free_mcc_tag(&phba->ctrl, tag);
  3361. session_resp = nonemb_cmd.va ;
  3362. memcpy(&phba->boot_sess, &session_resp->session_info,
  3363. sizeof(struct mgmt_session_info));
  3364. ret = 0;
  3365. boot_freemem:
  3366. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  3367. nonemb_cmd.va, nonemb_cmd.dma);
  3368. return ret;
  3369. }
  3370. static void beiscsi_boot_release(void *data)
  3371. {
  3372. struct beiscsi_hba *phba = data;
  3373. scsi_host_put(phba->shost);
  3374. }
  3375. static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
  3376. {
  3377. struct iscsi_boot_kobj *boot_kobj;
  3378. /* get boot info using mgmt cmd */
  3379. if (beiscsi_get_boot_info(phba))
  3380. /* Try to see if we can carry on without this */
  3381. return 0;
  3382. phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
  3383. if (!phba->boot_kset)
  3384. return -ENOMEM;
  3385. /* get a ref because the show function will ref the phba */
  3386. if (!scsi_host_get(phba->shost))
  3387. goto free_kset;
  3388. boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
  3389. beiscsi_show_boot_tgt_info,
  3390. beiscsi_tgt_get_attr_visibility,
  3391. beiscsi_boot_release);
  3392. if (!boot_kobj)
  3393. goto put_shost;
  3394. if (!scsi_host_get(phba->shost))
  3395. goto free_kset;
  3396. boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
  3397. beiscsi_show_boot_ini_info,
  3398. beiscsi_ini_get_attr_visibility,
  3399. beiscsi_boot_release);
  3400. if (!boot_kobj)
  3401. goto put_shost;
  3402. if (!scsi_host_get(phba->shost))
  3403. goto free_kset;
  3404. boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
  3405. beiscsi_show_boot_eth_info,
  3406. beiscsi_eth_get_attr_visibility,
  3407. beiscsi_boot_release);
  3408. if (!boot_kobj)
  3409. goto put_shost;
  3410. return 0;
  3411. put_shost:
  3412. scsi_host_put(phba->shost);
  3413. free_kset:
  3414. iscsi_boot_destroy_kset(phba->boot_kset);
  3415. return -ENOMEM;
  3416. }
  3417. static int beiscsi_init_port(struct beiscsi_hba *phba)
  3418. {
  3419. int ret;
  3420. ret = beiscsi_init_controller(phba);
  3421. if (ret < 0) {
  3422. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3423. "BM_%d : beiscsi_dev_probe - Failed in"
  3424. "beiscsi_init_controller\n");
  3425. return ret;
  3426. }
  3427. ret = beiscsi_init_sgl_handle(phba);
  3428. if (ret < 0) {
  3429. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3430. "BM_%d : beiscsi_dev_probe - Failed in"
  3431. "beiscsi_init_sgl_handle\n");
  3432. goto do_cleanup_ctrlr;
  3433. }
  3434. if (hba_setup_cid_tbls(phba)) {
  3435. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3436. "BM_%d : Failed in hba_setup_cid_tbls\n");
  3437. kfree(phba->io_sgl_hndl_base);
  3438. kfree(phba->eh_sgl_hndl_base);
  3439. goto do_cleanup_ctrlr;
  3440. }
  3441. return ret;
  3442. do_cleanup_ctrlr:
  3443. hwi_cleanup(phba);
  3444. return ret;
  3445. }
  3446. static void hwi_purge_eq(struct beiscsi_hba *phba)
  3447. {
  3448. struct hwi_controller *phwi_ctrlr;
  3449. struct hwi_context_memory *phwi_context;
  3450. struct be_queue_info *eq;
  3451. struct be_eq_entry *eqe = NULL;
  3452. int i, eq_msix;
  3453. unsigned int num_processed;
  3454. phwi_ctrlr = phba->phwi_ctrlr;
  3455. phwi_context = phwi_ctrlr->phwi_ctxt;
  3456. if (phba->msix_enabled)
  3457. eq_msix = 1;
  3458. else
  3459. eq_msix = 0;
  3460. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  3461. eq = &phwi_context->be_eq[i].q;
  3462. eqe = queue_tail_node(eq);
  3463. num_processed = 0;
  3464. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  3465. & EQE_VALID_MASK) {
  3466. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  3467. queue_tail_inc(eq);
  3468. eqe = queue_tail_node(eq);
  3469. num_processed++;
  3470. }
  3471. if (num_processed)
  3472. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  3473. }
  3474. }
  3475. static void beiscsi_clean_port(struct beiscsi_hba *phba)
  3476. {
  3477. int mgmt_status;
  3478. mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
  3479. if (mgmt_status)
  3480. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  3481. "BM_%d : mgmt_epfw_cleanup FAILED\n");
  3482. hwi_purge_eq(phba);
  3483. hwi_cleanup(phba);
  3484. kfree(phba->io_sgl_hndl_base);
  3485. kfree(phba->eh_sgl_hndl_base);
  3486. kfree(phba->cid_array);
  3487. kfree(phba->ep_array);
  3488. }
  3489. /**
  3490. * beiscsi_cleanup_task()- Free driver resources of the task
  3491. * @task: ptr to the iscsi task
  3492. *
  3493. **/
  3494. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3495. {
  3496. struct beiscsi_io_task *io_task = task->dd_data;
  3497. struct iscsi_conn *conn = task->conn;
  3498. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3499. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3500. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3501. struct hwi_wrb_context *pwrb_context;
  3502. struct hwi_controller *phwi_ctrlr;
  3503. phwi_ctrlr = phba->phwi_ctrlr;
  3504. pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
  3505. - phba->fw_config.iscsi_cid_start];
  3506. if (io_task->cmd_bhs) {
  3507. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3508. io_task->bhs_pa.u.a64.address);
  3509. io_task->cmd_bhs = NULL;
  3510. }
  3511. if (task->sc) {
  3512. if (io_task->pwrb_handle) {
  3513. free_wrb_handle(phba, pwrb_context,
  3514. io_task->pwrb_handle);
  3515. io_task->pwrb_handle = NULL;
  3516. }
  3517. if (io_task->psgl_handle) {
  3518. spin_lock(&phba->io_sgl_lock);
  3519. free_io_sgl_handle(phba, io_task->psgl_handle);
  3520. spin_unlock(&phba->io_sgl_lock);
  3521. io_task->psgl_handle = NULL;
  3522. }
  3523. } else {
  3524. if (!beiscsi_conn->login_in_progress) {
  3525. if (io_task->pwrb_handle) {
  3526. free_wrb_handle(phba, pwrb_context,
  3527. io_task->pwrb_handle);
  3528. io_task->pwrb_handle = NULL;
  3529. }
  3530. if (io_task->psgl_handle) {
  3531. spin_lock(&phba->mgmt_sgl_lock);
  3532. free_mgmt_sgl_handle(phba,
  3533. io_task->psgl_handle);
  3534. spin_unlock(&phba->mgmt_sgl_lock);
  3535. io_task->psgl_handle = NULL;
  3536. }
  3537. if (io_task->mtask_addr) {
  3538. pci_unmap_single(phba->pcidev,
  3539. io_task->mtask_addr,
  3540. io_task->mtask_data_count,
  3541. PCI_DMA_TODEVICE);
  3542. io_task->mtask_addr = 0;
  3543. }
  3544. }
  3545. }
  3546. }
  3547. void
  3548. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3549. struct beiscsi_offload_params *params)
  3550. {
  3551. struct wrb_handle *pwrb_handle;
  3552. struct iscsi_target_context_update_wrb *pwrb = NULL;
  3553. struct be_mem_descriptor *mem_descr;
  3554. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3555. struct iscsi_task *task = beiscsi_conn->task;
  3556. struct iscsi_session *session = task->conn->session;
  3557. u32 doorbell = 0;
  3558. /*
  3559. * We can always use 0 here because it is reserved by libiscsi for
  3560. * login/startup related tasks.
  3561. */
  3562. beiscsi_conn->login_in_progress = 0;
  3563. spin_lock_bh(&session->lock);
  3564. beiscsi_cleanup_task(task);
  3565. spin_unlock_bh(&session->lock);
  3566. pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
  3567. phba->fw_config.iscsi_cid_start));
  3568. pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
  3569. memset(pwrb, 0, sizeof(*pwrb));
  3570. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3571. max_burst_length, pwrb, params->dw[offsetof
  3572. (struct amap_beiscsi_offload_params,
  3573. max_burst_length) / 32]);
  3574. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3575. max_send_data_segment_length, pwrb,
  3576. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3577. max_send_data_segment_length) / 32]);
  3578. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3579. first_burst_length,
  3580. pwrb,
  3581. params->dw[offsetof(struct amap_beiscsi_offload_params,
  3582. first_burst_length) / 32]);
  3583. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
  3584. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3585. erl) / 32] & OFFLD_PARAMS_ERL));
  3586. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
  3587. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3588. dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
  3589. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
  3590. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3591. hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
  3592. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
  3593. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3594. ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
  3595. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
  3596. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3597. imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
  3598. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
  3599. pwrb,
  3600. (params->dw[offsetof(struct amap_beiscsi_offload_params,
  3601. exp_statsn) / 32] + 1));
  3602. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
  3603. 0x7);
  3604. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
  3605. pwrb, pwrb_handle->wrb_index);
  3606. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
  3607. pwrb, pwrb_handle->nxt_wrb_index);
  3608. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3609. session_state, pwrb, 0);
  3610. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
  3611. pwrb, 1);
  3612. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
  3613. pwrb, 0);
  3614. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
  3615. 0);
  3616. mem_descr = phba->init_mem;
  3617. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  3618. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3619. pad_buffer_addr_hi, pwrb,
  3620. mem_descr->mem_array[0].bus_address.u.a32.address_hi);
  3621. AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
  3622. pad_buffer_addr_lo, pwrb,
  3623. mem_descr->mem_array[0].bus_address.u.a32.address_lo);
  3624. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
  3625. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3626. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3627. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3628. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3629. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3630. }
  3631. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3632. int *index, int *age)
  3633. {
  3634. *index = (int)itt;
  3635. if (age)
  3636. *age = conn->session->age;
  3637. }
  3638. /**
  3639. * beiscsi_alloc_pdu - allocates pdu and related resources
  3640. * @task: libiscsi task
  3641. * @opcode: opcode of pdu for task
  3642. *
  3643. * This is called with the session lock held. It will allocate
  3644. * the wrb and sgl if needed for the command. And it will prep
  3645. * the pdu's itt. beiscsi_parse_pdu will later translate
  3646. * the pdu itt to the libiscsi task itt.
  3647. */
  3648. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3649. {
  3650. struct beiscsi_io_task *io_task = task->dd_data;
  3651. struct iscsi_conn *conn = task->conn;
  3652. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3653. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3654. struct hwi_wrb_context *pwrb_context;
  3655. struct hwi_controller *phwi_ctrlr;
  3656. itt_t itt;
  3657. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3658. dma_addr_t paddr;
  3659. io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
  3660. GFP_ATOMIC, &paddr);
  3661. if (!io_task->cmd_bhs)
  3662. return -ENOMEM;
  3663. io_task->bhs_pa.u.a64.address = paddr;
  3664. io_task->libiscsi_itt = (itt_t)task->itt;
  3665. io_task->conn = beiscsi_conn;
  3666. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3667. task->hdr_max = sizeof(struct be_cmd_bhs);
  3668. io_task->psgl_handle = NULL;
  3669. io_task->pwrb_handle = NULL;
  3670. if (task->sc) {
  3671. spin_lock(&phba->io_sgl_lock);
  3672. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3673. spin_unlock(&phba->io_sgl_lock);
  3674. if (!io_task->psgl_handle) {
  3675. beiscsi_log(phba, KERN_ERR,
  3676. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3677. "BM_%d : Alloc of IO_SGL_ICD Failed"
  3678. "for the CID : %d\n",
  3679. beiscsi_conn->beiscsi_conn_cid);
  3680. goto free_hndls;
  3681. }
  3682. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3683. beiscsi_conn->beiscsi_conn_cid -
  3684. phba->fw_config.iscsi_cid_start);
  3685. if (!io_task->pwrb_handle) {
  3686. beiscsi_log(phba, KERN_ERR,
  3687. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3688. "BM_%d : Alloc of WRB_HANDLE Failed"
  3689. "for the CID : %d\n",
  3690. beiscsi_conn->beiscsi_conn_cid);
  3691. goto free_io_hndls;
  3692. }
  3693. } else {
  3694. io_task->scsi_cmnd = NULL;
  3695. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3696. if (!beiscsi_conn->login_in_progress) {
  3697. spin_lock(&phba->mgmt_sgl_lock);
  3698. io_task->psgl_handle = (struct sgl_handle *)
  3699. alloc_mgmt_sgl_handle(phba);
  3700. spin_unlock(&phba->mgmt_sgl_lock);
  3701. if (!io_task->psgl_handle) {
  3702. beiscsi_log(phba, KERN_ERR,
  3703. BEISCSI_LOG_IO |
  3704. BEISCSI_LOG_CONFIG,
  3705. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3706. "for the CID : %d\n",
  3707. beiscsi_conn->
  3708. beiscsi_conn_cid);
  3709. goto free_hndls;
  3710. }
  3711. beiscsi_conn->login_in_progress = 1;
  3712. beiscsi_conn->plogin_sgl_handle =
  3713. io_task->psgl_handle;
  3714. io_task->pwrb_handle =
  3715. alloc_wrb_handle(phba,
  3716. beiscsi_conn->beiscsi_conn_cid -
  3717. phba->fw_config.iscsi_cid_start);
  3718. if (!io_task->pwrb_handle) {
  3719. beiscsi_log(phba, KERN_ERR,
  3720. BEISCSI_LOG_IO |
  3721. BEISCSI_LOG_CONFIG,
  3722. "BM_%d : Alloc of WRB_HANDLE Failed"
  3723. "for the CID : %d\n",
  3724. beiscsi_conn->
  3725. beiscsi_conn_cid);
  3726. goto free_mgmt_hndls;
  3727. }
  3728. beiscsi_conn->plogin_wrb_handle =
  3729. io_task->pwrb_handle;
  3730. } else {
  3731. io_task->psgl_handle =
  3732. beiscsi_conn->plogin_sgl_handle;
  3733. io_task->pwrb_handle =
  3734. beiscsi_conn->plogin_wrb_handle;
  3735. }
  3736. beiscsi_conn->task = task;
  3737. } else {
  3738. spin_lock(&phba->mgmt_sgl_lock);
  3739. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3740. spin_unlock(&phba->mgmt_sgl_lock);
  3741. if (!io_task->psgl_handle) {
  3742. beiscsi_log(phba, KERN_ERR,
  3743. BEISCSI_LOG_IO |
  3744. BEISCSI_LOG_CONFIG,
  3745. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3746. "for the CID : %d\n",
  3747. beiscsi_conn->
  3748. beiscsi_conn_cid);
  3749. goto free_hndls;
  3750. }
  3751. io_task->pwrb_handle =
  3752. alloc_wrb_handle(phba,
  3753. beiscsi_conn->beiscsi_conn_cid -
  3754. phba->fw_config.iscsi_cid_start);
  3755. if (!io_task->pwrb_handle) {
  3756. beiscsi_log(phba, KERN_ERR,
  3757. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3758. "BM_%d : Alloc of WRB_HANDLE Failed"
  3759. "for the CID : %d\n",
  3760. beiscsi_conn->beiscsi_conn_cid);
  3761. goto free_mgmt_hndls;
  3762. }
  3763. }
  3764. }
  3765. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  3766. wrb_index << 16) | (unsigned int)
  3767. (io_task->psgl_handle->sgl_index));
  3768. io_task->pwrb_handle->pio_handle = task;
  3769. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  3770. return 0;
  3771. free_io_hndls:
  3772. spin_lock(&phba->io_sgl_lock);
  3773. free_io_sgl_handle(phba, io_task->psgl_handle);
  3774. spin_unlock(&phba->io_sgl_lock);
  3775. goto free_hndls;
  3776. free_mgmt_hndls:
  3777. spin_lock(&phba->mgmt_sgl_lock);
  3778. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3779. spin_unlock(&phba->mgmt_sgl_lock);
  3780. free_hndls:
  3781. phwi_ctrlr = phba->phwi_ctrlr;
  3782. pwrb_context = &phwi_ctrlr->wrb_context[
  3783. beiscsi_conn->beiscsi_conn_cid -
  3784. phba->fw_config.iscsi_cid_start];
  3785. if (io_task->pwrb_handle)
  3786. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3787. io_task->pwrb_handle = NULL;
  3788. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3789. io_task->bhs_pa.u.a64.address);
  3790. io_task->cmd_bhs = NULL;
  3791. return -ENOMEM;
  3792. }
  3793. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  3794. unsigned int num_sg, unsigned int xferlen,
  3795. unsigned int writedir)
  3796. {
  3797. struct beiscsi_io_task *io_task = task->dd_data;
  3798. struct iscsi_conn *conn = task->conn;
  3799. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3800. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3801. struct iscsi_wrb *pwrb = NULL;
  3802. unsigned int doorbell = 0;
  3803. pwrb = io_task->pwrb_handle->pwrb;
  3804. io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
  3805. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  3806. if (writedir) {
  3807. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3808. INI_WR_CMD);
  3809. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  3810. } else {
  3811. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3812. INI_RD_CMD);
  3813. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  3814. }
  3815. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  3816. cpu_to_be16(*(unsigned short *)
  3817. &io_task->cmd_bhs->iscsi_hdr.lun));
  3818. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  3819. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3820. io_task->pwrb_handle->wrb_index);
  3821. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3822. be32_to_cpu(task->cmdsn));
  3823. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3824. io_task->psgl_handle->sgl_index);
  3825. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  3826. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3827. io_task->pwrb_handle->nxt_wrb_index);
  3828. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3829. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3830. doorbell |= (io_task->pwrb_handle->wrb_index &
  3831. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3832. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3833. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3834. return 0;
  3835. }
  3836. static int beiscsi_mtask(struct iscsi_task *task)
  3837. {
  3838. struct beiscsi_io_task *io_task = task->dd_data;
  3839. struct iscsi_conn *conn = task->conn;
  3840. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3841. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3842. struct iscsi_wrb *pwrb = NULL;
  3843. unsigned int doorbell = 0;
  3844. unsigned int cid;
  3845. cid = beiscsi_conn->beiscsi_conn_cid;
  3846. pwrb = io_task->pwrb_handle->pwrb;
  3847. memset(pwrb, 0, sizeof(*pwrb));
  3848. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  3849. be32_to_cpu(task->cmdsn));
  3850. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  3851. io_task->pwrb_handle->wrb_index);
  3852. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  3853. io_task->psgl_handle->sgl_index);
  3854. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  3855. case ISCSI_OP_LOGIN:
  3856. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3857. TGT_DM_CMD);
  3858. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3859. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  3860. hwi_write_buffer(pwrb, task);
  3861. break;
  3862. case ISCSI_OP_NOOP_OUT:
  3863. if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
  3864. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3865. TGT_DM_CMD);
  3866. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
  3867. pwrb, 0);
  3868. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
  3869. } else {
  3870. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3871. INI_RD_CMD);
  3872. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3873. }
  3874. hwi_write_buffer(pwrb, task);
  3875. break;
  3876. case ISCSI_OP_TEXT:
  3877. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3878. TGT_DM_CMD);
  3879. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3880. hwi_write_buffer(pwrb, task);
  3881. break;
  3882. case ISCSI_OP_SCSI_TMFUNC:
  3883. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3884. INI_TMF_CMD);
  3885. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3886. hwi_write_buffer(pwrb, task);
  3887. break;
  3888. case ISCSI_OP_LOGOUT:
  3889. AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
  3890. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  3891. HWH_TYPE_LOGOUT);
  3892. hwi_write_buffer(pwrb, task);
  3893. break;
  3894. default:
  3895. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  3896. "BM_%d : opcode =%d Not supported\n",
  3897. task->hdr->opcode & ISCSI_OPCODE_MASK);
  3898. return -EINVAL;
  3899. }
  3900. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  3901. task->data_count);
  3902. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  3903. io_task->pwrb_handle->nxt_wrb_index);
  3904. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  3905. doorbell |= cid & DB_WRB_POST_CID_MASK;
  3906. doorbell |= (io_task->pwrb_handle->wrb_index &
  3907. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3908. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3909. iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
  3910. return 0;
  3911. }
  3912. static int beiscsi_task_xmit(struct iscsi_task *task)
  3913. {
  3914. struct beiscsi_io_task *io_task = task->dd_data;
  3915. struct scsi_cmnd *sc = task->sc;
  3916. struct scatterlist *sg;
  3917. int num_sg;
  3918. unsigned int writedir = 0, xferlen = 0;
  3919. if (!sc)
  3920. return beiscsi_mtask(task);
  3921. io_task->scsi_cmnd = sc;
  3922. num_sg = scsi_dma_map(sc);
  3923. if (num_sg < 0) {
  3924. struct iscsi_conn *conn = task->conn;
  3925. struct beiscsi_hba *phba = NULL;
  3926. phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
  3927. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
  3928. "BM_%d : scsi_dma_map Failed\n");
  3929. return num_sg;
  3930. }
  3931. xferlen = scsi_bufflen(sc);
  3932. sg = scsi_sglist(sc);
  3933. if (sc->sc_data_direction == DMA_TO_DEVICE)
  3934. writedir = 1;
  3935. else
  3936. writedir = 0;
  3937. return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
  3938. }
  3939. /**
  3940. * beiscsi_bsg_request - handle bsg request from ISCSI transport
  3941. * @job: job to handle
  3942. */
  3943. static int beiscsi_bsg_request(struct bsg_job *job)
  3944. {
  3945. struct Scsi_Host *shost;
  3946. struct beiscsi_hba *phba;
  3947. struct iscsi_bsg_request *bsg_req = job->request;
  3948. int rc = -EINVAL;
  3949. unsigned int tag;
  3950. struct be_dma_mem nonemb_cmd;
  3951. struct be_cmd_resp_hdr *resp;
  3952. struct iscsi_bsg_reply *bsg_reply = job->reply;
  3953. unsigned short status, extd_status;
  3954. shost = iscsi_job_to_shost(job);
  3955. phba = iscsi_host_priv(shost);
  3956. switch (bsg_req->msgcode) {
  3957. case ISCSI_BSG_HST_VENDOR:
  3958. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  3959. job->request_payload.payload_len,
  3960. &nonemb_cmd.dma);
  3961. if (nonemb_cmd.va == NULL) {
  3962. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  3963. "BM_%d : Failed to allocate memory for "
  3964. "beiscsi_bsg_request\n");
  3965. return -ENOMEM;
  3966. }
  3967. tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
  3968. &nonemb_cmd);
  3969. if (!tag) {
  3970. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  3971. "BM_%d : MBX Tag Allocation Failed\n");
  3972. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  3973. nonemb_cmd.va, nonemb_cmd.dma);
  3974. return -EAGAIN;
  3975. } else
  3976. wait_event_interruptible(phba->ctrl.mcc_wait[tag],
  3977. phba->ctrl.mcc_numtag[tag]);
  3978. extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
  3979. status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
  3980. free_mcc_tag(&phba->ctrl, tag);
  3981. resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
  3982. sg_copy_from_buffer(job->reply_payload.sg_list,
  3983. job->reply_payload.sg_cnt,
  3984. nonemb_cmd.va, (resp->response_length
  3985. + sizeof(*resp)));
  3986. bsg_reply->reply_payload_rcv_len = resp->response_length;
  3987. bsg_reply->result = status;
  3988. bsg_job_done(job, bsg_reply->result,
  3989. bsg_reply->reply_payload_rcv_len);
  3990. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  3991. nonemb_cmd.va, nonemb_cmd.dma);
  3992. if (status || extd_status) {
  3993. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  3994. "BM_%d : MBX Cmd Failed"
  3995. " status = %d extd_status = %d\n",
  3996. status, extd_status);
  3997. return -EIO;
  3998. } else {
  3999. rc = 0;
  4000. }
  4001. break;
  4002. default:
  4003. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4004. "BM_%d : Unsupported bsg command: 0x%x\n",
  4005. bsg_req->msgcode);
  4006. break;
  4007. }
  4008. return rc;
  4009. }
  4010. void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
  4011. {
  4012. /* Set the logging parameter */
  4013. beiscsi_log_enable_init(phba, beiscsi_log_enable);
  4014. }
  4015. /*
  4016. * beiscsi_quiesce()- Cleanup Driver resources
  4017. * @phba: Instance Priv structure
  4018. *
  4019. * Free the OS and HW resources held by the driver
  4020. **/
  4021. static void beiscsi_quiesce(struct beiscsi_hba *phba)
  4022. {
  4023. struct hwi_controller *phwi_ctrlr;
  4024. struct hwi_context_memory *phwi_context;
  4025. struct be_eq_obj *pbe_eq;
  4026. unsigned int i, msix_vec;
  4027. phwi_ctrlr = phba->phwi_ctrlr;
  4028. phwi_context = phwi_ctrlr->phwi_ctxt;
  4029. hwi_disable_intr(phba);
  4030. if (phba->msix_enabled) {
  4031. for (i = 0; i <= phba->num_cpus; i++) {
  4032. msix_vec = phba->msix_entries[i].vector;
  4033. free_irq(msix_vec, &phwi_context->be_eq[i]);
  4034. kfree(phba->msi_name[i]);
  4035. }
  4036. } else
  4037. if (phba->pcidev->irq)
  4038. free_irq(phba->pcidev->irq, phba);
  4039. pci_disable_msix(phba->pcidev);
  4040. destroy_workqueue(phba->wq);
  4041. if (blk_iopoll_enabled)
  4042. for (i = 0; i < phba->num_cpus; i++) {
  4043. pbe_eq = &phwi_context->be_eq[i];
  4044. blk_iopoll_disable(&pbe_eq->iopoll);
  4045. }
  4046. beiscsi_clean_port(phba);
  4047. beiscsi_free_mem(phba);
  4048. beiscsi_unmap_pci_function(phba);
  4049. pci_free_consistent(phba->pcidev,
  4050. phba->ctrl.mbox_mem_alloced.size,
  4051. phba->ctrl.mbox_mem_alloced.va,
  4052. phba->ctrl.mbox_mem_alloced.dma);
  4053. }
  4054. static void beiscsi_remove(struct pci_dev *pcidev)
  4055. {
  4056. struct beiscsi_hba *phba = NULL;
  4057. phba = pci_get_drvdata(pcidev);
  4058. if (!phba) {
  4059. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  4060. return;
  4061. }
  4062. beiscsi_destroy_def_ifaces(phba);
  4063. beiscsi_quiesce(phba);
  4064. iscsi_boot_destroy_kset(phba->boot_kset);
  4065. iscsi_host_remove(phba->shost);
  4066. pci_dev_put(phba->pcidev);
  4067. iscsi_host_free(phba->shost);
  4068. pci_disable_device(pcidev);
  4069. }
  4070. static void beiscsi_shutdown(struct pci_dev *pcidev)
  4071. {
  4072. struct beiscsi_hba *phba = NULL;
  4073. phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
  4074. if (!phba) {
  4075. dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
  4076. return;
  4077. }
  4078. beiscsi_quiesce(phba);
  4079. pci_disable_device(pcidev);
  4080. }
  4081. static void beiscsi_msix_enable(struct beiscsi_hba *phba)
  4082. {
  4083. int i, status;
  4084. for (i = 0; i <= phba->num_cpus; i++)
  4085. phba->msix_entries[i].entry = i;
  4086. status = pci_enable_msix(phba->pcidev, phba->msix_entries,
  4087. (phba->num_cpus + 1));
  4088. if (!status)
  4089. phba->msix_enabled = true;
  4090. return;
  4091. }
  4092. static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
  4093. const struct pci_device_id *id)
  4094. {
  4095. struct beiscsi_hba *phba = NULL;
  4096. struct hwi_controller *phwi_ctrlr;
  4097. struct hwi_context_memory *phwi_context;
  4098. struct be_eq_obj *pbe_eq;
  4099. int ret, i;
  4100. ret = beiscsi_enable_pci(pcidev);
  4101. if (ret < 0) {
  4102. dev_err(&pcidev->dev,
  4103. "beiscsi_dev_probe - Failed to enable pci device\n");
  4104. return ret;
  4105. }
  4106. phba = beiscsi_hba_alloc(pcidev);
  4107. if (!phba) {
  4108. dev_err(&pcidev->dev,
  4109. "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
  4110. goto disable_pci;
  4111. }
  4112. /* Initialize Driver configuration Paramters */
  4113. beiscsi_hba_attrs_init(phba);
  4114. switch (pcidev->device) {
  4115. case BE_DEVICE_ID1:
  4116. case OC_DEVICE_ID1:
  4117. case OC_DEVICE_ID2:
  4118. phba->generation = BE_GEN2;
  4119. break;
  4120. case BE_DEVICE_ID2:
  4121. case OC_DEVICE_ID3:
  4122. phba->generation = BE_GEN3;
  4123. break;
  4124. case OC_SKH_ID1:
  4125. phba->generation = BE_GEN4;
  4126. default:
  4127. phba->generation = 0;
  4128. }
  4129. if (enable_msix)
  4130. find_num_cpus(phba);
  4131. else
  4132. phba->num_cpus = 1;
  4133. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  4134. "BM_%d : num_cpus = %d\n",
  4135. phba->num_cpus);
  4136. if (enable_msix) {
  4137. beiscsi_msix_enable(phba);
  4138. if (!phba->msix_enabled)
  4139. phba->num_cpus = 1;
  4140. }
  4141. ret = be_ctrl_init(phba, pcidev);
  4142. if (ret) {
  4143. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4144. "BM_%d : beiscsi_dev_probe-"
  4145. "Failed in be_ctrl_init\n");
  4146. goto hba_free;
  4147. }
  4148. ret = beiscsi_cmd_reset_function(phba);
  4149. if (ret) {
  4150. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4151. "BM_%d : Reset Failed. Aborting Crashdump\n");
  4152. goto hba_free;
  4153. }
  4154. ret = be_chk_reset_complete(phba);
  4155. if (ret) {
  4156. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4157. "BM_%d : Failed to get out of reset."
  4158. "Aborting Crashdump\n");
  4159. goto hba_free;
  4160. }
  4161. spin_lock_init(&phba->io_sgl_lock);
  4162. spin_lock_init(&phba->mgmt_sgl_lock);
  4163. spin_lock_init(&phba->isr_lock);
  4164. ret = mgmt_get_fw_config(&phba->ctrl, phba);
  4165. if (ret != 0) {
  4166. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4167. "BM_%d : Error getting fw config\n");
  4168. goto free_port;
  4169. }
  4170. phba->shost->max_id = phba->fw_config.iscsi_cid_count;
  4171. beiscsi_get_params(phba);
  4172. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4173. ret = beiscsi_init_port(phba);
  4174. if (ret < 0) {
  4175. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4176. "BM_%d : beiscsi_dev_probe-"
  4177. "Failed in beiscsi_init_port\n");
  4178. goto free_port;
  4179. }
  4180. for (i = 0; i < MAX_MCC_CMD ; i++) {
  4181. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  4182. phba->ctrl.mcc_tag[i] = i + 1;
  4183. phba->ctrl.mcc_numtag[i + 1] = 0;
  4184. phba->ctrl.mcc_tag_available++;
  4185. }
  4186. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  4187. snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
  4188. phba->shost->host_no);
  4189. phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
  4190. if (!phba->wq) {
  4191. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4192. "BM_%d : beiscsi_dev_probe-"
  4193. "Failed to allocate work queue\n");
  4194. goto free_twq;
  4195. }
  4196. phwi_ctrlr = phba->phwi_ctrlr;
  4197. phwi_context = phwi_ctrlr->phwi_ctxt;
  4198. if (blk_iopoll_enabled) {
  4199. for (i = 0; i < phba->num_cpus; i++) {
  4200. pbe_eq = &phwi_context->be_eq[i];
  4201. blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
  4202. be_iopoll);
  4203. blk_iopoll_enable(&pbe_eq->iopoll);
  4204. }
  4205. i = (phba->msix_enabled) ? i : 0;
  4206. /* Work item for MCC handling */
  4207. pbe_eq = &phwi_context->be_eq[i];
  4208. INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
  4209. } else {
  4210. if (phba->msix_enabled) {
  4211. for (i = 0; i <= phba->num_cpus; i++) {
  4212. pbe_eq = &phwi_context->be_eq[i];
  4213. INIT_WORK(&pbe_eq->work_cqs,
  4214. beiscsi_process_all_cqs);
  4215. }
  4216. } else {
  4217. pbe_eq = &phwi_context->be_eq[0];
  4218. INIT_WORK(&pbe_eq->work_cqs,
  4219. beiscsi_process_all_cqs);
  4220. }
  4221. }
  4222. ret = beiscsi_init_irqs(phba);
  4223. if (ret < 0) {
  4224. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4225. "BM_%d : beiscsi_dev_probe-"
  4226. "Failed to beiscsi_init_irqs\n");
  4227. goto free_blkenbld;
  4228. }
  4229. hwi_enable_intr(phba);
  4230. if (beiscsi_setup_boot_info(phba))
  4231. /*
  4232. * log error but continue, because we may not be using
  4233. * iscsi boot.
  4234. */
  4235. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4236. "BM_%d : Could not set up "
  4237. "iSCSI boot info.\n");
  4238. beiscsi_create_def_ifaces(phba);
  4239. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  4240. "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
  4241. return 0;
  4242. free_blkenbld:
  4243. destroy_workqueue(phba->wq);
  4244. if (blk_iopoll_enabled)
  4245. for (i = 0; i < phba->num_cpus; i++) {
  4246. pbe_eq = &phwi_context->be_eq[i];
  4247. blk_iopoll_disable(&pbe_eq->iopoll);
  4248. }
  4249. free_twq:
  4250. beiscsi_clean_port(phba);
  4251. beiscsi_free_mem(phba);
  4252. free_port:
  4253. pci_free_consistent(phba->pcidev,
  4254. phba->ctrl.mbox_mem_alloced.size,
  4255. phba->ctrl.mbox_mem_alloced.va,
  4256. phba->ctrl.mbox_mem_alloced.dma);
  4257. beiscsi_unmap_pci_function(phba);
  4258. hba_free:
  4259. if (phba->msix_enabled)
  4260. pci_disable_msix(phba->pcidev);
  4261. iscsi_host_remove(phba->shost);
  4262. pci_dev_put(phba->pcidev);
  4263. iscsi_host_free(phba->shost);
  4264. disable_pci:
  4265. pci_disable_device(pcidev);
  4266. return ret;
  4267. }
  4268. struct iscsi_transport beiscsi_iscsi_transport = {
  4269. .owner = THIS_MODULE,
  4270. .name = DRV_NAME,
  4271. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  4272. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  4273. .create_session = beiscsi_session_create,
  4274. .destroy_session = beiscsi_session_destroy,
  4275. .create_conn = beiscsi_conn_create,
  4276. .bind_conn = beiscsi_conn_bind,
  4277. .destroy_conn = iscsi_conn_teardown,
  4278. .attr_is_visible = be2iscsi_attr_is_visible,
  4279. .set_iface_param = be2iscsi_iface_set_param,
  4280. .get_iface_param = be2iscsi_iface_get_param,
  4281. .set_param = beiscsi_set_param,
  4282. .get_conn_param = iscsi_conn_get_param,
  4283. .get_session_param = iscsi_session_get_param,
  4284. .get_host_param = beiscsi_get_host_param,
  4285. .start_conn = beiscsi_conn_start,
  4286. .stop_conn = iscsi_conn_stop,
  4287. .send_pdu = iscsi_conn_send_pdu,
  4288. .xmit_task = beiscsi_task_xmit,
  4289. .cleanup_task = beiscsi_cleanup_task,
  4290. .alloc_pdu = beiscsi_alloc_pdu,
  4291. .parse_pdu_itt = beiscsi_parse_pdu,
  4292. .get_stats = beiscsi_conn_get_stats,
  4293. .get_ep_param = beiscsi_ep_get_param,
  4294. .ep_connect = beiscsi_ep_connect,
  4295. .ep_poll = beiscsi_ep_poll,
  4296. .ep_disconnect = beiscsi_ep_disconnect,
  4297. .session_recovery_timedout = iscsi_session_recovery_timedout,
  4298. .bsg_request = beiscsi_bsg_request,
  4299. };
  4300. static struct pci_driver beiscsi_pci_driver = {
  4301. .name = DRV_NAME,
  4302. .probe = beiscsi_dev_probe,
  4303. .remove = beiscsi_remove,
  4304. .shutdown = beiscsi_shutdown,
  4305. .id_table = beiscsi_pci_id_table
  4306. };
  4307. static int __init beiscsi_module_init(void)
  4308. {
  4309. int ret;
  4310. beiscsi_scsi_transport =
  4311. iscsi_register_transport(&beiscsi_iscsi_transport);
  4312. if (!beiscsi_scsi_transport) {
  4313. printk(KERN_ERR
  4314. "beiscsi_module_init - Unable to register beiscsi transport.\n");
  4315. return -ENOMEM;
  4316. }
  4317. printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
  4318. &beiscsi_iscsi_transport);
  4319. ret = pci_register_driver(&beiscsi_pci_driver);
  4320. if (ret) {
  4321. printk(KERN_ERR
  4322. "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
  4323. goto unregister_iscsi_transport;
  4324. }
  4325. return 0;
  4326. unregister_iscsi_transport:
  4327. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  4328. return ret;
  4329. }
  4330. static void __exit beiscsi_module_exit(void)
  4331. {
  4332. pci_unregister_driver(&beiscsi_pci_driver);
  4333. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  4334. }
  4335. module_init(beiscsi_module_init);
  4336. module_exit(beiscsi_module_exit);