lpfc_bsg.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2009-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *******************************************************************/
  20. #include <linux/interrupt.h>
  21. #include <linux/mempool.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/list.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_transport_fc.h>
  29. #include <scsi/scsi_bsg_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_bsg.h"
  37. #include "lpfc_disc.h"
  38. #include "lpfc_scsi.h"
  39. #include "lpfc.h"
  40. #include "lpfc_logmsg.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_debugfs.h"
  43. #include "lpfc_vport.h"
  44. #include "lpfc_version.h"
  45. struct lpfc_bsg_event {
  46. struct list_head node;
  47. struct kref kref;
  48. wait_queue_head_t wq;
  49. /* Event type and waiter identifiers */
  50. uint32_t type_mask;
  51. uint32_t req_id;
  52. uint32_t reg_id;
  53. /* next two flags are here for the auto-delete logic */
  54. unsigned long wait_time_stamp;
  55. int waiting;
  56. /* seen and not seen events */
  57. struct list_head events_to_get;
  58. struct list_head events_to_see;
  59. /* driver data associated with the job */
  60. void *dd_data;
  61. };
  62. struct lpfc_bsg_iocb {
  63. struct lpfc_iocbq *cmdiocbq;
  64. struct lpfc_dmabuf *rmp;
  65. struct lpfc_nodelist *ndlp;
  66. };
  67. struct lpfc_bsg_mbox {
  68. LPFC_MBOXQ_t *pmboxq;
  69. MAILBOX_t *mb;
  70. struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
  71. uint8_t *ext; /* extended mailbox data */
  72. uint32_t mbOffset; /* from app */
  73. uint32_t inExtWLen; /* from app */
  74. uint32_t outExtWLen; /* from app */
  75. };
  76. #define MENLO_DID 0x0000FC0E
  77. struct lpfc_bsg_menlo {
  78. struct lpfc_iocbq *cmdiocbq;
  79. struct lpfc_dmabuf *rmp;
  80. };
  81. #define TYPE_EVT 1
  82. #define TYPE_IOCB 2
  83. #define TYPE_MBOX 3
  84. #define TYPE_MENLO 4
  85. struct bsg_job_data {
  86. uint32_t type;
  87. struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
  88. union {
  89. struct lpfc_bsg_event *evt;
  90. struct lpfc_bsg_iocb iocb;
  91. struct lpfc_bsg_mbox mbox;
  92. struct lpfc_bsg_menlo menlo;
  93. } context_un;
  94. };
  95. struct event_data {
  96. struct list_head node;
  97. uint32_t type;
  98. uint32_t immed_dat;
  99. void *data;
  100. uint32_t len;
  101. };
  102. #define BUF_SZ_4K 4096
  103. #define SLI_CT_ELX_LOOPBACK 0x10
  104. enum ELX_LOOPBACK_CMD {
  105. ELX_LOOPBACK_XRI_SETUP,
  106. ELX_LOOPBACK_DATA,
  107. };
  108. #define ELX_LOOPBACK_HEADER_SZ \
  109. (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
  110. struct lpfc_dmabufext {
  111. struct lpfc_dmabuf dma;
  112. uint32_t size;
  113. uint32_t flag;
  114. };
  115. static void
  116. lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
  117. {
  118. struct lpfc_dmabuf *mlast, *next_mlast;
  119. if (mlist) {
  120. list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
  121. list) {
  122. lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
  123. list_del(&mlast->list);
  124. kfree(mlast);
  125. }
  126. lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
  127. kfree(mlist);
  128. }
  129. return;
  130. }
  131. static struct lpfc_dmabuf *
  132. lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
  133. int outbound_buffers, struct ulp_bde64 *bpl,
  134. int *bpl_entries)
  135. {
  136. struct lpfc_dmabuf *mlist = NULL;
  137. struct lpfc_dmabuf *mp;
  138. unsigned int bytes_left = size;
  139. /* Verify we can support the size specified */
  140. if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
  141. return NULL;
  142. /* Determine the number of dma buffers to allocate */
  143. *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
  144. size/LPFC_BPL_SIZE);
  145. /* Allocate dma buffer and place in BPL passed */
  146. while (bytes_left) {
  147. /* Allocate dma buffer */
  148. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  149. if (!mp) {
  150. if (mlist)
  151. lpfc_free_bsg_buffers(phba, mlist);
  152. return NULL;
  153. }
  154. INIT_LIST_HEAD(&mp->list);
  155. mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
  156. if (!mp->virt) {
  157. kfree(mp);
  158. if (mlist)
  159. lpfc_free_bsg_buffers(phba, mlist);
  160. return NULL;
  161. }
  162. /* Queue it to a linked list */
  163. if (!mlist)
  164. mlist = mp;
  165. else
  166. list_add_tail(&mp->list, &mlist->list);
  167. /* Add buffer to buffer pointer list */
  168. if (outbound_buffers)
  169. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  170. else
  171. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  172. bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
  173. bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
  174. bpl->tus.f.bdeSize = (uint16_t)
  175. (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
  176. bytes_left);
  177. bytes_left -= bpl->tus.f.bdeSize;
  178. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  179. bpl++;
  180. }
  181. return mlist;
  182. }
  183. static unsigned int
  184. lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
  185. struct fc_bsg_buffer *bsg_buffers,
  186. unsigned int bytes_to_transfer, int to_buffers)
  187. {
  188. struct lpfc_dmabuf *mp;
  189. unsigned int transfer_bytes, bytes_copied = 0;
  190. unsigned int sg_offset, dma_offset;
  191. unsigned char *dma_address, *sg_address;
  192. LIST_HEAD(temp_list);
  193. struct sg_mapping_iter miter;
  194. unsigned long flags;
  195. unsigned int sg_flags = SG_MITER_ATOMIC;
  196. bool sg_valid;
  197. list_splice_init(&dma_buffers->list, &temp_list);
  198. list_add(&dma_buffers->list, &temp_list);
  199. sg_offset = 0;
  200. if (to_buffers)
  201. sg_flags |= SG_MITER_FROM_SG;
  202. else
  203. sg_flags |= SG_MITER_TO_SG;
  204. sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
  205. sg_flags);
  206. local_irq_save(flags);
  207. sg_valid = sg_miter_next(&miter);
  208. list_for_each_entry(mp, &temp_list, list) {
  209. dma_offset = 0;
  210. while (bytes_to_transfer && sg_valid &&
  211. (dma_offset < LPFC_BPL_SIZE)) {
  212. dma_address = mp->virt + dma_offset;
  213. if (sg_offset) {
  214. /* Continue previous partial transfer of sg */
  215. sg_address = miter.addr + sg_offset;
  216. transfer_bytes = miter.length - sg_offset;
  217. } else {
  218. sg_address = miter.addr;
  219. transfer_bytes = miter.length;
  220. }
  221. if (bytes_to_transfer < transfer_bytes)
  222. transfer_bytes = bytes_to_transfer;
  223. if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
  224. transfer_bytes = LPFC_BPL_SIZE - dma_offset;
  225. if (to_buffers)
  226. memcpy(dma_address, sg_address, transfer_bytes);
  227. else
  228. memcpy(sg_address, dma_address, transfer_bytes);
  229. dma_offset += transfer_bytes;
  230. sg_offset += transfer_bytes;
  231. bytes_to_transfer -= transfer_bytes;
  232. bytes_copied += transfer_bytes;
  233. if (sg_offset >= miter.length) {
  234. sg_offset = 0;
  235. sg_valid = sg_miter_next(&miter);
  236. }
  237. }
  238. }
  239. sg_miter_stop(&miter);
  240. local_irq_restore(flags);
  241. list_del_init(&dma_buffers->list);
  242. list_splice(&temp_list, &dma_buffers->list);
  243. return bytes_copied;
  244. }
  245. /**
  246. * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
  247. * @phba: Pointer to HBA context object.
  248. * @cmdiocbq: Pointer to command iocb.
  249. * @rspiocbq: Pointer to response iocb.
  250. *
  251. * This function is the completion handler for iocbs issued using
  252. * lpfc_bsg_send_mgmt_cmd function. This function is called by the
  253. * ring event handler function without any lock held. This function
  254. * can be called from both worker thread context and interrupt
  255. * context. This function also can be called from another thread which
  256. * cleans up the SLI layer objects.
  257. * This function copies the contents of the response iocb to the
  258. * response iocb memory object provided by the caller of
  259. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  260. * sleeps for the iocb completion.
  261. **/
  262. static void
  263. lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
  264. struct lpfc_iocbq *cmdiocbq,
  265. struct lpfc_iocbq *rspiocbq)
  266. {
  267. struct bsg_job_data *dd_data;
  268. struct fc_bsg_job *job;
  269. IOCB_t *rsp;
  270. struct lpfc_dmabuf *bmp, *cmp, *rmp;
  271. struct lpfc_nodelist *ndlp;
  272. struct lpfc_bsg_iocb *iocb;
  273. unsigned long flags;
  274. unsigned int rsp_size;
  275. int rc = 0;
  276. dd_data = cmdiocbq->context1;
  277. /* Determine if job has been aborted */
  278. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  279. job = dd_data->set_job;
  280. if (job) {
  281. /* Prevent timeout handling from trying to abort job */
  282. job->dd_data = NULL;
  283. }
  284. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  285. iocb = &dd_data->context_un.iocb;
  286. ndlp = iocb->ndlp;
  287. rmp = iocb->rmp;
  288. cmp = cmdiocbq->context2;
  289. bmp = cmdiocbq->context3;
  290. rsp = &rspiocbq->iocb;
  291. /* Copy the completed data or set the error status */
  292. if (job) {
  293. if (rsp->ulpStatus) {
  294. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  295. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  296. case IOERR_SEQUENCE_TIMEOUT:
  297. rc = -ETIMEDOUT;
  298. break;
  299. case IOERR_INVALID_RPI:
  300. rc = -EFAULT;
  301. break;
  302. default:
  303. rc = -EACCES;
  304. break;
  305. }
  306. } else {
  307. rc = -EACCES;
  308. }
  309. } else {
  310. rsp_size = rsp->un.genreq64.bdl.bdeSize;
  311. job->reply->reply_payload_rcv_len =
  312. lpfc_bsg_copy_data(rmp, &job->reply_payload,
  313. rsp_size, 0);
  314. }
  315. }
  316. lpfc_free_bsg_buffers(phba, cmp);
  317. lpfc_free_bsg_buffers(phba, rmp);
  318. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  319. kfree(bmp);
  320. lpfc_sli_release_iocbq(phba, cmdiocbq);
  321. lpfc_nlp_put(ndlp);
  322. kfree(dd_data);
  323. /* Complete the job if the job is still active */
  324. if (job) {
  325. job->reply->result = rc;
  326. job->job_done(job);
  327. }
  328. return;
  329. }
  330. /**
  331. * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
  332. * @job: fc_bsg_job to handle
  333. **/
  334. static int
  335. lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
  336. {
  337. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  338. struct lpfc_hba *phba = vport->phba;
  339. struct lpfc_rport_data *rdata = job->rport->dd_data;
  340. struct lpfc_nodelist *ndlp = rdata->pnode;
  341. struct ulp_bde64 *bpl = NULL;
  342. uint32_t timeout;
  343. struct lpfc_iocbq *cmdiocbq = NULL;
  344. IOCB_t *cmd;
  345. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
  346. int request_nseg;
  347. int reply_nseg;
  348. struct bsg_job_data *dd_data;
  349. uint32_t creg_val;
  350. int rc = 0;
  351. int iocb_stat;
  352. /* in case no data is transferred */
  353. job->reply->reply_payload_rcv_len = 0;
  354. /* allocate our bsg tracking structure */
  355. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  356. if (!dd_data) {
  357. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  358. "2733 Failed allocation of dd_data\n");
  359. rc = -ENOMEM;
  360. goto no_dd_data;
  361. }
  362. if (!lpfc_nlp_get(ndlp)) {
  363. rc = -ENODEV;
  364. goto no_ndlp;
  365. }
  366. if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
  367. rc = -ENODEV;
  368. goto free_ndlp;
  369. }
  370. cmdiocbq = lpfc_sli_get_iocbq(phba);
  371. if (!cmdiocbq) {
  372. rc = -ENOMEM;
  373. goto free_ndlp;
  374. }
  375. cmd = &cmdiocbq->iocb;
  376. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  377. if (!bmp) {
  378. rc = -ENOMEM;
  379. goto free_cmdiocbq;
  380. }
  381. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  382. if (!bmp->virt) {
  383. rc = -ENOMEM;
  384. goto free_bmp;
  385. }
  386. INIT_LIST_HEAD(&bmp->list);
  387. bpl = (struct ulp_bde64 *) bmp->virt;
  388. request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
  389. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  390. 1, bpl, &request_nseg);
  391. if (!cmp) {
  392. rc = -ENOMEM;
  393. goto free_bmp;
  394. }
  395. lpfc_bsg_copy_data(cmp, &job->request_payload,
  396. job->request_payload.payload_len, 1);
  397. bpl += request_nseg;
  398. reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
  399. rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
  400. bpl, &reply_nseg);
  401. if (!rmp) {
  402. rc = -ENOMEM;
  403. goto free_cmp;
  404. }
  405. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  406. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  407. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  408. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  409. cmd->un.genreq64.bdl.bdeSize =
  410. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  411. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  412. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  413. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  414. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  415. cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
  416. cmd->ulpBdeCount = 1;
  417. cmd->ulpLe = 1;
  418. cmd->ulpClass = CLASS3;
  419. cmd->ulpContext = ndlp->nlp_rpi;
  420. if (phba->sli_rev == LPFC_SLI_REV4)
  421. cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  422. cmd->ulpOwner = OWN_CHIP;
  423. cmdiocbq->vport = phba->pport;
  424. cmdiocbq->context3 = bmp;
  425. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  426. timeout = phba->fc_ratov * 2;
  427. cmd->ulpTimeout = timeout;
  428. cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
  429. cmdiocbq->context1 = dd_data;
  430. cmdiocbq->context2 = cmp;
  431. cmdiocbq->context3 = bmp;
  432. cmdiocbq->context_un.ndlp = ndlp;
  433. dd_data->type = TYPE_IOCB;
  434. dd_data->set_job = job;
  435. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  436. dd_data->context_un.iocb.ndlp = ndlp;
  437. dd_data->context_un.iocb.rmp = rmp;
  438. job->dd_data = dd_data;
  439. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  440. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  441. rc = -EIO ;
  442. goto free_rmp;
  443. }
  444. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  445. writel(creg_val, phba->HCregaddr);
  446. readl(phba->HCregaddr); /* flush */
  447. }
  448. iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  449. if (iocb_stat == IOCB_SUCCESS)
  450. return 0; /* done for now */
  451. else if (iocb_stat == IOCB_BUSY)
  452. rc = -EAGAIN;
  453. else
  454. rc = -EIO;
  455. /* iocb failed so cleanup */
  456. free_rmp:
  457. lpfc_free_bsg_buffers(phba, rmp);
  458. free_cmp:
  459. lpfc_free_bsg_buffers(phba, cmp);
  460. free_bmp:
  461. if (bmp->virt)
  462. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  463. kfree(bmp);
  464. free_cmdiocbq:
  465. lpfc_sli_release_iocbq(phba, cmdiocbq);
  466. free_ndlp:
  467. lpfc_nlp_put(ndlp);
  468. no_ndlp:
  469. kfree(dd_data);
  470. no_dd_data:
  471. /* make error code available to userspace */
  472. job->reply->result = rc;
  473. job->dd_data = NULL;
  474. return rc;
  475. }
  476. /**
  477. * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
  478. * @phba: Pointer to HBA context object.
  479. * @cmdiocbq: Pointer to command iocb.
  480. * @rspiocbq: Pointer to response iocb.
  481. *
  482. * This function is the completion handler for iocbs issued using
  483. * lpfc_bsg_rport_els_cmp function. This function is called by the
  484. * ring event handler function without any lock held. This function
  485. * can be called from both worker thread context and interrupt
  486. * context. This function also can be called from other thread which
  487. * cleans up the SLI layer objects.
  488. * This function copies the contents of the response iocb to the
  489. * response iocb memory object provided by the caller of
  490. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  491. * sleeps for the iocb completion.
  492. **/
  493. static void
  494. lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
  495. struct lpfc_iocbq *cmdiocbq,
  496. struct lpfc_iocbq *rspiocbq)
  497. {
  498. struct bsg_job_data *dd_data;
  499. struct fc_bsg_job *job;
  500. IOCB_t *rsp;
  501. struct lpfc_nodelist *ndlp;
  502. struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
  503. struct fc_bsg_ctels_reply *els_reply;
  504. uint8_t *rjt_data;
  505. unsigned long flags;
  506. unsigned int rsp_size;
  507. int rc = 0;
  508. dd_data = cmdiocbq->context1;
  509. ndlp = dd_data->context_un.iocb.ndlp;
  510. cmdiocbq->context1 = ndlp;
  511. /* Determine if job has been aborted */
  512. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  513. job = dd_data->set_job;
  514. if (job) {
  515. /* Prevent timeout handling from trying to abort job */
  516. job->dd_data = NULL;
  517. }
  518. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  519. rsp = &rspiocbq->iocb;
  520. pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
  521. prsp = (struct lpfc_dmabuf *)pcmd->list.next;
  522. /* Copy the completed job data or determine the job status if job is
  523. * still active
  524. */
  525. if (job) {
  526. if (rsp->ulpStatus == IOSTAT_SUCCESS) {
  527. rsp_size = rsp->un.elsreq64.bdl.bdeSize;
  528. job->reply->reply_payload_rcv_len =
  529. sg_copy_from_buffer(job->reply_payload.sg_list,
  530. job->reply_payload.sg_cnt,
  531. prsp->virt,
  532. rsp_size);
  533. } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
  534. job->reply->reply_payload_rcv_len =
  535. sizeof(struct fc_bsg_ctels_reply);
  536. /* LS_RJT data returned in word 4 */
  537. rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
  538. els_reply = &job->reply->reply_data.ctels_reply;
  539. els_reply->status = FC_CTELS_STATUS_REJECT;
  540. els_reply->rjt_data.action = rjt_data[3];
  541. els_reply->rjt_data.reason_code = rjt_data[2];
  542. els_reply->rjt_data.reason_explanation = rjt_data[1];
  543. els_reply->rjt_data.vendor_unique = rjt_data[0];
  544. } else {
  545. rc = -EIO;
  546. }
  547. }
  548. lpfc_nlp_put(ndlp);
  549. lpfc_els_free_iocb(phba, cmdiocbq);
  550. kfree(dd_data);
  551. /* Complete the job if the job is still active */
  552. if (job) {
  553. job->reply->result = rc;
  554. job->job_done(job);
  555. }
  556. return;
  557. }
  558. /**
  559. * lpfc_bsg_rport_els - send an ELS command from a bsg request
  560. * @job: fc_bsg_job to handle
  561. **/
  562. static int
  563. lpfc_bsg_rport_els(struct fc_bsg_job *job)
  564. {
  565. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  566. struct lpfc_hba *phba = vport->phba;
  567. struct lpfc_rport_data *rdata = job->rport->dd_data;
  568. struct lpfc_nodelist *ndlp = rdata->pnode;
  569. uint32_t elscmd;
  570. uint32_t cmdsize;
  571. uint32_t rspsize;
  572. struct lpfc_iocbq *cmdiocbq;
  573. uint16_t rpi = 0;
  574. struct bsg_job_data *dd_data;
  575. uint32_t creg_val;
  576. int rc = 0;
  577. /* in case no data is transferred */
  578. job->reply->reply_payload_rcv_len = 0;
  579. /* verify the els command is not greater than the
  580. * maximum ELS transfer size.
  581. */
  582. if (job->request_payload.payload_len > FCELSSIZE) {
  583. rc = -EINVAL;
  584. goto no_dd_data;
  585. }
  586. /* allocate our bsg tracking structure */
  587. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  588. if (!dd_data) {
  589. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  590. "2735 Failed allocation of dd_data\n");
  591. rc = -ENOMEM;
  592. goto no_dd_data;
  593. }
  594. elscmd = job->request->rqst_data.r_els.els_code;
  595. cmdsize = job->request_payload.payload_len;
  596. rspsize = job->reply_payload.payload_len;
  597. if (!lpfc_nlp_get(ndlp)) {
  598. rc = -ENODEV;
  599. goto free_dd_data;
  600. }
  601. /* We will use the allocated dma buffers by prep els iocb for command
  602. * and response to ensure if the job times out and the request is freed,
  603. * we won't be dma into memory that is no longer allocated to for the
  604. * request.
  605. */
  606. cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
  607. ndlp->nlp_DID, elscmd);
  608. if (!cmdiocbq) {
  609. rc = -EIO;
  610. goto release_ndlp;
  611. }
  612. rpi = ndlp->nlp_rpi;
  613. /* Transfer the request payload to allocated command dma buffer */
  614. sg_copy_to_buffer(job->request_payload.sg_list,
  615. job->request_payload.sg_cnt,
  616. ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
  617. cmdsize);
  618. if (phba->sli_rev == LPFC_SLI_REV4)
  619. cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
  620. else
  621. cmdiocbq->iocb.ulpContext = rpi;
  622. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  623. cmdiocbq->context1 = dd_data;
  624. cmdiocbq->context_un.ndlp = ndlp;
  625. cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
  626. dd_data->type = TYPE_IOCB;
  627. dd_data->set_job = job;
  628. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  629. dd_data->context_un.iocb.ndlp = ndlp;
  630. dd_data->context_un.iocb.rmp = NULL;
  631. job->dd_data = dd_data;
  632. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  633. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  634. rc = -EIO;
  635. goto linkdown_err;
  636. }
  637. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  638. writel(creg_val, phba->HCregaddr);
  639. readl(phba->HCregaddr); /* flush */
  640. }
  641. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  642. if (rc == IOCB_SUCCESS)
  643. return 0; /* done for now */
  644. else if (rc == IOCB_BUSY)
  645. rc = -EAGAIN;
  646. else
  647. rc = -EIO;
  648. linkdown_err:
  649. cmdiocbq->context1 = ndlp;
  650. lpfc_els_free_iocb(phba, cmdiocbq);
  651. release_ndlp:
  652. lpfc_nlp_put(ndlp);
  653. free_dd_data:
  654. kfree(dd_data);
  655. no_dd_data:
  656. /* make error code available to userspace */
  657. job->reply->result = rc;
  658. job->dd_data = NULL;
  659. return rc;
  660. }
  661. /**
  662. * lpfc_bsg_event_free - frees an allocated event structure
  663. * @kref: Pointer to a kref.
  664. *
  665. * Called from kref_put. Back cast the kref into an event structure address.
  666. * Free any events to get, delete associated nodes, free any events to see,
  667. * free any data then free the event itself.
  668. **/
  669. static void
  670. lpfc_bsg_event_free(struct kref *kref)
  671. {
  672. struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
  673. kref);
  674. struct event_data *ed;
  675. list_del(&evt->node);
  676. while (!list_empty(&evt->events_to_get)) {
  677. ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
  678. list_del(&ed->node);
  679. kfree(ed->data);
  680. kfree(ed);
  681. }
  682. while (!list_empty(&evt->events_to_see)) {
  683. ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
  684. list_del(&ed->node);
  685. kfree(ed->data);
  686. kfree(ed);
  687. }
  688. kfree(evt->dd_data);
  689. kfree(evt);
  690. }
  691. /**
  692. * lpfc_bsg_event_ref - increments the kref for an event
  693. * @evt: Pointer to an event structure.
  694. **/
  695. static inline void
  696. lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
  697. {
  698. kref_get(&evt->kref);
  699. }
  700. /**
  701. * lpfc_bsg_event_unref - Uses kref_put to free an event structure
  702. * @evt: Pointer to an event structure.
  703. **/
  704. static inline void
  705. lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
  706. {
  707. kref_put(&evt->kref, lpfc_bsg_event_free);
  708. }
  709. /**
  710. * lpfc_bsg_event_new - allocate and initialize a event structure
  711. * @ev_mask: Mask of events.
  712. * @ev_reg_id: Event reg id.
  713. * @ev_req_id: Event request id.
  714. **/
  715. static struct lpfc_bsg_event *
  716. lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
  717. {
  718. struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  719. if (!evt)
  720. return NULL;
  721. INIT_LIST_HEAD(&evt->events_to_get);
  722. INIT_LIST_HEAD(&evt->events_to_see);
  723. evt->type_mask = ev_mask;
  724. evt->req_id = ev_req_id;
  725. evt->reg_id = ev_reg_id;
  726. evt->wait_time_stamp = jiffies;
  727. evt->dd_data = NULL;
  728. init_waitqueue_head(&evt->wq);
  729. kref_init(&evt->kref);
  730. return evt;
  731. }
  732. /**
  733. * diag_cmd_data_free - Frees an lpfc dma buffer extension
  734. * @phba: Pointer to HBA context object.
  735. * @mlist: Pointer to an lpfc dma buffer extension.
  736. **/
  737. static int
  738. diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
  739. {
  740. struct lpfc_dmabufext *mlast;
  741. struct pci_dev *pcidev;
  742. struct list_head head, *curr, *next;
  743. if ((!mlist) || (!lpfc_is_link_up(phba) &&
  744. (phba->link_flag & LS_LOOPBACK_MODE))) {
  745. return 0;
  746. }
  747. pcidev = phba->pcidev;
  748. list_add_tail(&head, &mlist->dma.list);
  749. list_for_each_safe(curr, next, &head) {
  750. mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
  751. if (mlast->dma.virt)
  752. dma_free_coherent(&pcidev->dev,
  753. mlast->size,
  754. mlast->dma.virt,
  755. mlast->dma.phys);
  756. kfree(mlast);
  757. }
  758. return 0;
  759. }
  760. /**
  761. * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
  762. * @phba:
  763. * @pring:
  764. * @piocbq:
  765. *
  766. * This function is called when an unsolicited CT command is received. It
  767. * forwards the event to any processes registered to receive CT events.
  768. **/
  769. int
  770. lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  771. struct lpfc_iocbq *piocbq)
  772. {
  773. uint32_t evt_req_id = 0;
  774. uint32_t cmd;
  775. uint32_t len;
  776. struct lpfc_dmabuf *dmabuf = NULL;
  777. struct lpfc_bsg_event *evt;
  778. struct event_data *evt_dat = NULL;
  779. struct lpfc_iocbq *iocbq;
  780. size_t offset = 0;
  781. struct list_head head;
  782. struct ulp_bde64 *bde;
  783. dma_addr_t dma_addr;
  784. int i;
  785. struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
  786. struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
  787. struct lpfc_hbq_entry *hbqe;
  788. struct lpfc_sli_ct_request *ct_req;
  789. struct fc_bsg_job *job = NULL;
  790. struct bsg_job_data *dd_data = NULL;
  791. unsigned long flags;
  792. int size = 0;
  793. INIT_LIST_HEAD(&head);
  794. list_add_tail(&head, &piocbq->list);
  795. if (piocbq->iocb.ulpBdeCount == 0 ||
  796. piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
  797. goto error_ct_unsol_exit;
  798. if (phba->link_state == LPFC_HBA_ERROR ||
  799. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
  800. goto error_ct_unsol_exit;
  801. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  802. dmabuf = bdeBuf1;
  803. else {
  804. dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
  805. piocbq->iocb.un.cont64[0].addrLow);
  806. dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
  807. }
  808. if (dmabuf == NULL)
  809. goto error_ct_unsol_exit;
  810. ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
  811. evt_req_id = ct_req->FsType;
  812. cmd = ct_req->CommandResponse.bits.CmdRsp;
  813. len = ct_req->CommandResponse.bits.Size;
  814. if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
  815. lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
  816. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  817. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  818. if (!(evt->type_mask & FC_REG_CT_EVENT) ||
  819. evt->req_id != evt_req_id)
  820. continue;
  821. lpfc_bsg_event_ref(evt);
  822. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  823. evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
  824. if (evt_dat == NULL) {
  825. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  826. lpfc_bsg_event_unref(evt);
  827. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  828. "2614 Memory allocation failed for "
  829. "CT event\n");
  830. break;
  831. }
  832. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  833. /* take accumulated byte count from the last iocbq */
  834. iocbq = list_entry(head.prev, typeof(*iocbq), list);
  835. evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
  836. } else {
  837. list_for_each_entry(iocbq, &head, list) {
  838. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
  839. evt_dat->len +=
  840. iocbq->iocb.un.cont64[i].tus.f.bdeSize;
  841. }
  842. }
  843. evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
  844. if (evt_dat->data == NULL) {
  845. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  846. "2615 Memory allocation failed for "
  847. "CT event data, size %d\n",
  848. evt_dat->len);
  849. kfree(evt_dat);
  850. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  851. lpfc_bsg_event_unref(evt);
  852. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  853. goto error_ct_unsol_exit;
  854. }
  855. list_for_each_entry(iocbq, &head, list) {
  856. size = 0;
  857. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  858. bdeBuf1 = iocbq->context2;
  859. bdeBuf2 = iocbq->context3;
  860. }
  861. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
  862. if (phba->sli3_options &
  863. LPFC_SLI3_HBQ_ENABLED) {
  864. if (i == 0) {
  865. hbqe = (struct lpfc_hbq_entry *)
  866. &iocbq->iocb.un.ulpWord[0];
  867. size = hbqe->bde.tus.f.bdeSize;
  868. dmabuf = bdeBuf1;
  869. } else if (i == 1) {
  870. hbqe = (struct lpfc_hbq_entry *)
  871. &iocbq->iocb.unsli3.
  872. sli3Words[4];
  873. size = hbqe->bde.tus.f.bdeSize;
  874. dmabuf = bdeBuf2;
  875. }
  876. if ((offset + size) > evt_dat->len)
  877. size = evt_dat->len - offset;
  878. } else {
  879. size = iocbq->iocb.un.cont64[i].
  880. tus.f.bdeSize;
  881. bde = &iocbq->iocb.un.cont64[i];
  882. dma_addr = getPaddr(bde->addrHigh,
  883. bde->addrLow);
  884. dmabuf = lpfc_sli_ringpostbuf_get(phba,
  885. pring, dma_addr);
  886. }
  887. if (!dmabuf) {
  888. lpfc_printf_log(phba, KERN_ERR,
  889. LOG_LIBDFC, "2616 No dmabuf "
  890. "found for iocbq 0x%p\n",
  891. iocbq);
  892. kfree(evt_dat->data);
  893. kfree(evt_dat);
  894. spin_lock_irqsave(&phba->ct_ev_lock,
  895. flags);
  896. lpfc_bsg_event_unref(evt);
  897. spin_unlock_irqrestore(
  898. &phba->ct_ev_lock, flags);
  899. goto error_ct_unsol_exit;
  900. }
  901. memcpy((char *)(evt_dat->data) + offset,
  902. dmabuf->virt, size);
  903. offset += size;
  904. if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
  905. !(phba->sli3_options &
  906. LPFC_SLI3_HBQ_ENABLED)) {
  907. lpfc_sli_ringpostbuf_put(phba, pring,
  908. dmabuf);
  909. } else {
  910. switch (cmd) {
  911. case ELX_LOOPBACK_DATA:
  912. if (phba->sli_rev <
  913. LPFC_SLI_REV4)
  914. diag_cmd_data_free(phba,
  915. (struct lpfc_dmabufext
  916. *)dmabuf);
  917. break;
  918. case ELX_LOOPBACK_XRI_SETUP:
  919. if ((phba->sli_rev ==
  920. LPFC_SLI_REV2) ||
  921. (phba->sli3_options &
  922. LPFC_SLI3_HBQ_ENABLED
  923. )) {
  924. lpfc_in_buf_free(phba,
  925. dmabuf);
  926. } else {
  927. lpfc_post_buffer(phba,
  928. pring,
  929. 1);
  930. }
  931. break;
  932. default:
  933. if (!(phba->sli3_options &
  934. LPFC_SLI3_HBQ_ENABLED))
  935. lpfc_post_buffer(phba,
  936. pring,
  937. 1);
  938. break;
  939. }
  940. }
  941. }
  942. }
  943. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  944. if (phba->sli_rev == LPFC_SLI_REV4) {
  945. evt_dat->immed_dat = phba->ctx_idx;
  946. phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
  947. /* Provide warning for over-run of the ct_ctx array */
  948. if (phba->ct_ctx[evt_dat->immed_dat].valid ==
  949. UNSOL_VALID)
  950. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  951. "2717 CT context array entry "
  952. "[%d] over-run: oxid:x%x, "
  953. "sid:x%x\n", phba->ctx_idx,
  954. phba->ct_ctx[
  955. evt_dat->immed_dat].oxid,
  956. phba->ct_ctx[
  957. evt_dat->immed_dat].SID);
  958. phba->ct_ctx[evt_dat->immed_dat].rxid =
  959. piocbq->iocb.ulpContext;
  960. phba->ct_ctx[evt_dat->immed_dat].oxid =
  961. piocbq->iocb.unsli3.rcvsli3.ox_id;
  962. phba->ct_ctx[evt_dat->immed_dat].SID =
  963. piocbq->iocb.un.rcvels.remoteID;
  964. phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
  965. } else
  966. evt_dat->immed_dat = piocbq->iocb.ulpContext;
  967. evt_dat->type = FC_REG_CT_EVENT;
  968. list_add(&evt_dat->node, &evt->events_to_see);
  969. if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
  970. wake_up_interruptible(&evt->wq);
  971. lpfc_bsg_event_unref(evt);
  972. break;
  973. }
  974. list_move(evt->events_to_see.prev, &evt->events_to_get);
  975. dd_data = (struct bsg_job_data *)evt->dd_data;
  976. job = dd_data->set_job;
  977. dd_data->set_job = NULL;
  978. lpfc_bsg_event_unref(evt);
  979. if (job) {
  980. job->reply->reply_payload_rcv_len = size;
  981. /* make error code available to userspace */
  982. job->reply->result = 0;
  983. job->dd_data = NULL;
  984. /* complete the job back to userspace */
  985. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  986. job->job_done(job);
  987. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  988. }
  989. }
  990. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  991. error_ct_unsol_exit:
  992. if (!list_empty(&head))
  993. list_del(&head);
  994. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  995. (evt_req_id == SLI_CT_ELX_LOOPBACK))
  996. return 0;
  997. return 1;
  998. }
  999. /**
  1000. * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
  1001. * @phba: Pointer to HBA context object.
  1002. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  1003. *
  1004. * This function handles abort to the CT command toward management plane
  1005. * for SLI4 port.
  1006. *
  1007. * If the pending context of a CT command to management plane present, clears
  1008. * such context and returns 1 for handled; otherwise, it returns 0 indicating
  1009. * no context exists.
  1010. **/
  1011. int
  1012. lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
  1013. {
  1014. struct fc_frame_header fc_hdr;
  1015. struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
  1016. int ctx_idx, handled = 0;
  1017. uint16_t oxid, rxid;
  1018. uint32_t sid;
  1019. memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  1020. sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
  1021. oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
  1022. rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
  1023. for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
  1024. if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
  1025. continue;
  1026. if (phba->ct_ctx[ctx_idx].rxid != rxid)
  1027. continue;
  1028. if (phba->ct_ctx[ctx_idx].oxid != oxid)
  1029. continue;
  1030. if (phba->ct_ctx[ctx_idx].SID != sid)
  1031. continue;
  1032. phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
  1033. handled = 1;
  1034. }
  1035. return handled;
  1036. }
  1037. /**
  1038. * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
  1039. * @job: SET_EVENT fc_bsg_job
  1040. **/
  1041. static int
  1042. lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
  1043. {
  1044. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1045. struct lpfc_hba *phba = vport->phba;
  1046. struct set_ct_event *event_req;
  1047. struct lpfc_bsg_event *evt;
  1048. int rc = 0;
  1049. struct bsg_job_data *dd_data = NULL;
  1050. uint32_t ev_mask;
  1051. unsigned long flags;
  1052. if (job->request_len <
  1053. sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
  1054. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1055. "2612 Received SET_CT_EVENT below minimum "
  1056. "size\n");
  1057. rc = -EINVAL;
  1058. goto job_error;
  1059. }
  1060. event_req = (struct set_ct_event *)
  1061. job->request->rqst_data.h_vendor.vendor_cmd;
  1062. ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
  1063. FC_REG_EVENT_MASK);
  1064. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1065. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  1066. if (evt->reg_id == event_req->ev_reg_id) {
  1067. lpfc_bsg_event_ref(evt);
  1068. evt->wait_time_stamp = jiffies;
  1069. dd_data = (struct bsg_job_data *)evt->dd_data;
  1070. break;
  1071. }
  1072. }
  1073. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1074. if (&evt->node == &phba->ct_ev_waiters) {
  1075. /* no event waiting struct yet - first call */
  1076. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  1077. if (dd_data == NULL) {
  1078. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1079. "2734 Failed allocation of dd_data\n");
  1080. rc = -ENOMEM;
  1081. goto job_error;
  1082. }
  1083. evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
  1084. event_req->ev_req_id);
  1085. if (!evt) {
  1086. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1087. "2617 Failed allocation of event "
  1088. "waiter\n");
  1089. rc = -ENOMEM;
  1090. goto job_error;
  1091. }
  1092. dd_data->type = TYPE_EVT;
  1093. dd_data->set_job = NULL;
  1094. dd_data->context_un.evt = evt;
  1095. evt->dd_data = (void *)dd_data;
  1096. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1097. list_add(&evt->node, &phba->ct_ev_waiters);
  1098. lpfc_bsg_event_ref(evt);
  1099. evt->wait_time_stamp = jiffies;
  1100. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1101. }
  1102. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1103. evt->waiting = 1;
  1104. dd_data->set_job = job; /* for unsolicited command */
  1105. job->dd_data = dd_data; /* for fc transport timeout callback*/
  1106. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1107. return 0; /* call job done later */
  1108. job_error:
  1109. if (dd_data != NULL)
  1110. kfree(dd_data);
  1111. job->dd_data = NULL;
  1112. return rc;
  1113. }
  1114. /**
  1115. * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
  1116. * @job: GET_EVENT fc_bsg_job
  1117. **/
  1118. static int
  1119. lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
  1120. {
  1121. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1122. struct lpfc_hba *phba = vport->phba;
  1123. struct get_ct_event *event_req;
  1124. struct get_ct_event_reply *event_reply;
  1125. struct lpfc_bsg_event *evt;
  1126. struct event_data *evt_dat = NULL;
  1127. unsigned long flags;
  1128. uint32_t rc = 0;
  1129. if (job->request_len <
  1130. sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
  1131. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1132. "2613 Received GET_CT_EVENT request below "
  1133. "minimum size\n");
  1134. rc = -EINVAL;
  1135. goto job_error;
  1136. }
  1137. event_req = (struct get_ct_event *)
  1138. job->request->rqst_data.h_vendor.vendor_cmd;
  1139. event_reply = (struct get_ct_event_reply *)
  1140. job->reply->reply_data.vendor_reply.vendor_rsp;
  1141. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1142. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  1143. if (evt->reg_id == event_req->ev_reg_id) {
  1144. if (list_empty(&evt->events_to_get))
  1145. break;
  1146. lpfc_bsg_event_ref(evt);
  1147. evt->wait_time_stamp = jiffies;
  1148. evt_dat = list_entry(evt->events_to_get.prev,
  1149. struct event_data, node);
  1150. list_del(&evt_dat->node);
  1151. break;
  1152. }
  1153. }
  1154. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1155. /* The app may continue to ask for event data until it gets
  1156. * an error indicating that there isn't anymore
  1157. */
  1158. if (evt_dat == NULL) {
  1159. job->reply->reply_payload_rcv_len = 0;
  1160. rc = -ENOENT;
  1161. goto job_error;
  1162. }
  1163. if (evt_dat->len > job->request_payload.payload_len) {
  1164. evt_dat->len = job->request_payload.payload_len;
  1165. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1166. "2618 Truncated event data at %d "
  1167. "bytes\n",
  1168. job->request_payload.payload_len);
  1169. }
  1170. event_reply->type = evt_dat->type;
  1171. event_reply->immed_data = evt_dat->immed_dat;
  1172. if (evt_dat->len > 0)
  1173. job->reply->reply_payload_rcv_len =
  1174. sg_copy_from_buffer(job->request_payload.sg_list,
  1175. job->request_payload.sg_cnt,
  1176. evt_dat->data, evt_dat->len);
  1177. else
  1178. job->reply->reply_payload_rcv_len = 0;
  1179. if (evt_dat) {
  1180. kfree(evt_dat->data);
  1181. kfree(evt_dat);
  1182. }
  1183. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1184. lpfc_bsg_event_unref(evt);
  1185. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1186. job->dd_data = NULL;
  1187. job->reply->result = 0;
  1188. job->job_done(job);
  1189. return 0;
  1190. job_error:
  1191. job->dd_data = NULL;
  1192. job->reply->result = rc;
  1193. return rc;
  1194. }
  1195. /**
  1196. * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
  1197. * @phba: Pointer to HBA context object.
  1198. * @cmdiocbq: Pointer to command iocb.
  1199. * @rspiocbq: Pointer to response iocb.
  1200. *
  1201. * This function is the completion handler for iocbs issued using
  1202. * lpfc_issue_ct_rsp_cmp function. This function is called by the
  1203. * ring event handler function without any lock held. This function
  1204. * can be called from both worker thread context and interrupt
  1205. * context. This function also can be called from other thread which
  1206. * cleans up the SLI layer objects.
  1207. * This function copy the contents of the response iocb to the
  1208. * response iocb memory object provided by the caller of
  1209. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  1210. * sleeps for the iocb completion.
  1211. **/
  1212. static void
  1213. lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
  1214. struct lpfc_iocbq *cmdiocbq,
  1215. struct lpfc_iocbq *rspiocbq)
  1216. {
  1217. struct bsg_job_data *dd_data;
  1218. struct fc_bsg_job *job;
  1219. IOCB_t *rsp;
  1220. struct lpfc_dmabuf *bmp, *cmp;
  1221. struct lpfc_nodelist *ndlp;
  1222. unsigned long flags;
  1223. int rc = 0;
  1224. dd_data = cmdiocbq->context1;
  1225. /* Determine if job has been aborted */
  1226. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1227. job = dd_data->set_job;
  1228. if (job) {
  1229. /* Prevent timeout handling from trying to abort job */
  1230. job->dd_data = NULL;
  1231. }
  1232. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1233. ndlp = dd_data->context_un.iocb.ndlp;
  1234. cmp = cmdiocbq->context2;
  1235. bmp = cmdiocbq->context3;
  1236. rsp = &rspiocbq->iocb;
  1237. /* Copy the completed job data or set the error status */
  1238. if (job) {
  1239. if (rsp->ulpStatus) {
  1240. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  1241. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  1242. case IOERR_SEQUENCE_TIMEOUT:
  1243. rc = -ETIMEDOUT;
  1244. break;
  1245. case IOERR_INVALID_RPI:
  1246. rc = -EFAULT;
  1247. break;
  1248. default:
  1249. rc = -EACCES;
  1250. break;
  1251. }
  1252. } else {
  1253. rc = -EACCES;
  1254. }
  1255. } else {
  1256. job->reply->reply_payload_rcv_len = 0;
  1257. }
  1258. }
  1259. lpfc_free_bsg_buffers(phba, cmp);
  1260. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1261. kfree(bmp);
  1262. lpfc_sli_release_iocbq(phba, cmdiocbq);
  1263. lpfc_nlp_put(ndlp);
  1264. kfree(dd_data);
  1265. /* Complete the job if the job is still active */
  1266. if (job) {
  1267. job->reply->result = rc;
  1268. job->job_done(job);
  1269. }
  1270. return;
  1271. }
  1272. /**
  1273. * lpfc_issue_ct_rsp - issue a ct response
  1274. * @phba: Pointer to HBA context object.
  1275. * @job: Pointer to the job object.
  1276. * @tag: tag index value into the ports context exchange array.
  1277. * @bmp: Pointer to a dma buffer descriptor.
  1278. * @num_entry: Number of enties in the bde.
  1279. **/
  1280. static int
  1281. lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
  1282. struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
  1283. int num_entry)
  1284. {
  1285. IOCB_t *icmd;
  1286. struct lpfc_iocbq *ctiocb = NULL;
  1287. int rc = 0;
  1288. struct lpfc_nodelist *ndlp = NULL;
  1289. struct bsg_job_data *dd_data;
  1290. uint32_t creg_val;
  1291. /* allocate our bsg tracking structure */
  1292. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  1293. if (!dd_data) {
  1294. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1295. "2736 Failed allocation of dd_data\n");
  1296. rc = -ENOMEM;
  1297. goto no_dd_data;
  1298. }
  1299. /* Allocate buffer for command iocb */
  1300. ctiocb = lpfc_sli_get_iocbq(phba);
  1301. if (!ctiocb) {
  1302. rc = -ENOMEM;
  1303. goto no_ctiocb;
  1304. }
  1305. icmd = &ctiocb->iocb;
  1306. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  1307. icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  1308. icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
  1309. icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  1310. icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
  1311. icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  1312. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  1313. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
  1314. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  1315. /* Fill in rest of iocb */
  1316. icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  1317. icmd->ulpBdeCount = 1;
  1318. icmd->ulpLe = 1;
  1319. icmd->ulpClass = CLASS3;
  1320. if (phba->sli_rev == LPFC_SLI_REV4) {
  1321. /* Do not issue unsol response if oxid not marked as valid */
  1322. if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
  1323. rc = IOCB_ERROR;
  1324. goto issue_ct_rsp_exit;
  1325. }
  1326. icmd->ulpContext = phba->ct_ctx[tag].rxid;
  1327. icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
  1328. ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
  1329. if (!ndlp) {
  1330. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  1331. "2721 ndlp null for oxid %x SID %x\n",
  1332. icmd->ulpContext,
  1333. phba->ct_ctx[tag].SID);
  1334. rc = IOCB_ERROR;
  1335. goto issue_ct_rsp_exit;
  1336. }
  1337. /* Check if the ndlp is active */
  1338. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  1339. rc = IOCB_ERROR;
  1340. goto issue_ct_rsp_exit;
  1341. }
  1342. /* get a refernece count so the ndlp doesn't go away while
  1343. * we respond
  1344. */
  1345. if (!lpfc_nlp_get(ndlp)) {
  1346. rc = IOCB_ERROR;
  1347. goto issue_ct_rsp_exit;
  1348. }
  1349. icmd->un.ulpWord[3] =
  1350. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  1351. /* The exchange is done, mark the entry as invalid */
  1352. phba->ct_ctx[tag].valid = UNSOL_INVALID;
  1353. } else
  1354. icmd->ulpContext = (ushort) tag;
  1355. icmd->ulpTimeout = phba->fc_ratov * 2;
  1356. /* Xmit CT response on exchange <xid> */
  1357. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  1358. "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
  1359. icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
  1360. ctiocb->iocb_cmpl = NULL;
  1361. ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
  1362. ctiocb->vport = phba->pport;
  1363. ctiocb->context1 = dd_data;
  1364. ctiocb->context2 = cmp;
  1365. ctiocb->context3 = bmp;
  1366. ctiocb->context_un.ndlp = ndlp;
  1367. ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
  1368. dd_data->type = TYPE_IOCB;
  1369. dd_data->set_job = job;
  1370. dd_data->context_un.iocb.cmdiocbq = ctiocb;
  1371. dd_data->context_un.iocb.ndlp = ndlp;
  1372. dd_data->context_un.iocb.rmp = NULL;
  1373. job->dd_data = dd_data;
  1374. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  1375. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  1376. rc = -IOCB_ERROR;
  1377. goto issue_ct_rsp_exit;
  1378. }
  1379. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  1380. writel(creg_val, phba->HCregaddr);
  1381. readl(phba->HCregaddr); /* flush */
  1382. }
  1383. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  1384. if (rc == IOCB_SUCCESS)
  1385. return 0; /* done for now */
  1386. issue_ct_rsp_exit:
  1387. lpfc_sli_release_iocbq(phba, ctiocb);
  1388. no_ctiocb:
  1389. kfree(dd_data);
  1390. no_dd_data:
  1391. return rc;
  1392. }
  1393. /**
  1394. * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
  1395. * @job: SEND_MGMT_RESP fc_bsg_job
  1396. **/
  1397. static int
  1398. lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
  1399. {
  1400. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1401. struct lpfc_hba *phba = vport->phba;
  1402. struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
  1403. job->request->rqst_data.h_vendor.vendor_cmd;
  1404. struct ulp_bde64 *bpl;
  1405. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
  1406. int bpl_entries;
  1407. uint32_t tag = mgmt_resp->tag;
  1408. unsigned long reqbfrcnt =
  1409. (unsigned long)job->request_payload.payload_len;
  1410. int rc = 0;
  1411. /* in case no data is transferred */
  1412. job->reply->reply_payload_rcv_len = 0;
  1413. if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
  1414. rc = -ERANGE;
  1415. goto send_mgmt_rsp_exit;
  1416. }
  1417. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1418. if (!bmp) {
  1419. rc = -ENOMEM;
  1420. goto send_mgmt_rsp_exit;
  1421. }
  1422. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  1423. if (!bmp->virt) {
  1424. rc = -ENOMEM;
  1425. goto send_mgmt_rsp_free_bmp;
  1426. }
  1427. INIT_LIST_HEAD(&bmp->list);
  1428. bpl = (struct ulp_bde64 *) bmp->virt;
  1429. bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
  1430. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  1431. 1, bpl, &bpl_entries);
  1432. if (!cmp) {
  1433. rc = -ENOMEM;
  1434. goto send_mgmt_rsp_free_bmp;
  1435. }
  1436. lpfc_bsg_copy_data(cmp, &job->request_payload,
  1437. job->request_payload.payload_len, 1);
  1438. rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
  1439. if (rc == IOCB_SUCCESS)
  1440. return 0; /* done for now */
  1441. rc = -EACCES;
  1442. lpfc_free_bsg_buffers(phba, cmp);
  1443. send_mgmt_rsp_free_bmp:
  1444. if (bmp->virt)
  1445. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1446. kfree(bmp);
  1447. send_mgmt_rsp_exit:
  1448. /* make error code available to userspace */
  1449. job->reply->result = rc;
  1450. job->dd_data = NULL;
  1451. return rc;
  1452. }
  1453. /**
  1454. * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
  1455. * @phba: Pointer to HBA context object.
  1456. *
  1457. * This function is responsible for preparing driver for diag loopback
  1458. * on device.
  1459. */
  1460. static int
  1461. lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
  1462. {
  1463. struct lpfc_vport **vports;
  1464. struct Scsi_Host *shost;
  1465. struct lpfc_sli *psli;
  1466. struct lpfc_sli_ring *pring;
  1467. int i = 0;
  1468. psli = &phba->sli;
  1469. if (!psli)
  1470. return -ENODEV;
  1471. pring = &psli->ring[LPFC_FCP_RING];
  1472. if (!pring)
  1473. return -ENODEV;
  1474. if ((phba->link_state == LPFC_HBA_ERROR) ||
  1475. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  1476. (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
  1477. return -EACCES;
  1478. vports = lpfc_create_vport_work_array(phba);
  1479. if (vports) {
  1480. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1481. shost = lpfc_shost_from_vport(vports[i]);
  1482. scsi_block_requests(shost);
  1483. }
  1484. lpfc_destroy_vport_work_array(phba, vports);
  1485. } else {
  1486. shost = lpfc_shost_from_vport(phba->pport);
  1487. scsi_block_requests(shost);
  1488. }
  1489. while (!list_empty(&pring->txcmplq)) {
  1490. if (i++ > 500) /* wait up to 5 seconds */
  1491. break;
  1492. msleep(10);
  1493. }
  1494. return 0;
  1495. }
  1496. /**
  1497. * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
  1498. * @phba: Pointer to HBA context object.
  1499. *
  1500. * This function is responsible for driver exit processing of setting up
  1501. * diag loopback mode on device.
  1502. */
  1503. static void
  1504. lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
  1505. {
  1506. struct Scsi_Host *shost;
  1507. struct lpfc_vport **vports;
  1508. int i;
  1509. vports = lpfc_create_vport_work_array(phba);
  1510. if (vports) {
  1511. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1512. shost = lpfc_shost_from_vport(vports[i]);
  1513. scsi_unblock_requests(shost);
  1514. }
  1515. lpfc_destroy_vport_work_array(phba, vports);
  1516. } else {
  1517. shost = lpfc_shost_from_vport(phba->pport);
  1518. scsi_unblock_requests(shost);
  1519. }
  1520. return;
  1521. }
  1522. /**
  1523. * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
  1524. * @phba: Pointer to HBA context object.
  1525. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1526. *
  1527. * This function is responsible for placing an sli3 port into diagnostic
  1528. * loopback mode in order to perform a diagnostic loopback test.
  1529. * All new scsi requests are blocked, a small delay is used to allow the
  1530. * scsi requests to complete then the link is brought down. If the link is
  1531. * is placed in loopback mode then scsi requests are again allowed
  1532. * so the scsi mid-layer doesn't give up on the port.
  1533. * All of this is done in-line.
  1534. */
  1535. static int
  1536. lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
  1537. {
  1538. struct diag_mode_set *loopback_mode;
  1539. uint32_t link_flags;
  1540. uint32_t timeout;
  1541. LPFC_MBOXQ_t *pmboxq = NULL;
  1542. int mbxstatus = MBX_SUCCESS;
  1543. int i = 0;
  1544. int rc = 0;
  1545. /* no data to return just the return code */
  1546. job->reply->reply_payload_rcv_len = 0;
  1547. if (job->request_len < sizeof(struct fc_bsg_request) +
  1548. sizeof(struct diag_mode_set)) {
  1549. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1550. "2738 Received DIAG MODE request size:%d "
  1551. "below the minimum size:%d\n",
  1552. job->request_len,
  1553. (int)(sizeof(struct fc_bsg_request) +
  1554. sizeof(struct diag_mode_set)));
  1555. rc = -EINVAL;
  1556. goto job_error;
  1557. }
  1558. rc = lpfc_bsg_diag_mode_enter(phba);
  1559. if (rc)
  1560. goto job_error;
  1561. /* bring the link to diagnostic mode */
  1562. loopback_mode = (struct diag_mode_set *)
  1563. job->request->rqst_data.h_vendor.vendor_cmd;
  1564. link_flags = loopback_mode->type;
  1565. timeout = loopback_mode->timeout * 100;
  1566. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1567. if (!pmboxq) {
  1568. rc = -ENOMEM;
  1569. goto loopback_mode_exit;
  1570. }
  1571. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1572. pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
  1573. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1574. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1575. if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
  1576. /* wait for link down before proceeding */
  1577. i = 0;
  1578. while (phba->link_state != LPFC_LINK_DOWN) {
  1579. if (i++ > timeout) {
  1580. rc = -ETIMEDOUT;
  1581. goto loopback_mode_exit;
  1582. }
  1583. msleep(10);
  1584. }
  1585. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1586. if (link_flags == INTERNAL_LOOP_BACK)
  1587. pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
  1588. else
  1589. pmboxq->u.mb.un.varInitLnk.link_flags =
  1590. FLAGS_TOPOLOGY_MODE_LOOP;
  1591. pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
  1592. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1593. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
  1594. LPFC_MBOX_TMO);
  1595. if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
  1596. rc = -ENODEV;
  1597. else {
  1598. spin_lock_irq(&phba->hbalock);
  1599. phba->link_flag |= LS_LOOPBACK_MODE;
  1600. spin_unlock_irq(&phba->hbalock);
  1601. /* wait for the link attention interrupt */
  1602. msleep(100);
  1603. i = 0;
  1604. while (phba->link_state != LPFC_HBA_READY) {
  1605. if (i++ > timeout) {
  1606. rc = -ETIMEDOUT;
  1607. break;
  1608. }
  1609. msleep(10);
  1610. }
  1611. }
  1612. } else
  1613. rc = -ENODEV;
  1614. loopback_mode_exit:
  1615. lpfc_bsg_diag_mode_exit(phba);
  1616. /*
  1617. * Let SLI layer release mboxq if mbox command completed after timeout.
  1618. */
  1619. if (pmboxq && mbxstatus != MBX_TIMEOUT)
  1620. mempool_free(pmboxq, phba->mbox_mem_pool);
  1621. job_error:
  1622. /* make error code available to userspace */
  1623. job->reply->result = rc;
  1624. /* complete the job back to userspace if no error */
  1625. if (rc == 0)
  1626. job->job_done(job);
  1627. return rc;
  1628. }
  1629. /**
  1630. * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
  1631. * @phba: Pointer to HBA context object.
  1632. * @diag: Flag for set link to diag or nomral operation state.
  1633. *
  1634. * This function is responsible for issuing a sli4 mailbox command for setting
  1635. * link to either diag state or normal operation state.
  1636. */
  1637. static int
  1638. lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
  1639. {
  1640. LPFC_MBOXQ_t *pmboxq;
  1641. struct lpfc_mbx_set_link_diag_state *link_diag_state;
  1642. uint32_t req_len, alloc_len;
  1643. int mbxstatus = MBX_SUCCESS, rc;
  1644. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1645. if (!pmboxq)
  1646. return -ENOMEM;
  1647. req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
  1648. sizeof(struct lpfc_sli4_cfg_mhdr));
  1649. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  1650. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
  1651. req_len, LPFC_SLI4_MBX_EMBED);
  1652. if (alloc_len != req_len) {
  1653. rc = -ENOMEM;
  1654. goto link_diag_state_set_out;
  1655. }
  1656. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1657. "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
  1658. diag, phba->sli4_hba.lnk_info.lnk_tp,
  1659. phba->sli4_hba.lnk_info.lnk_no);
  1660. link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
  1661. bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
  1662. LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
  1663. bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
  1664. phba->sli4_hba.lnk_info.lnk_no);
  1665. bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
  1666. phba->sli4_hba.lnk_info.lnk_tp);
  1667. if (diag)
  1668. bf_set(lpfc_mbx_set_diag_state_diag,
  1669. &link_diag_state->u.req, 1);
  1670. else
  1671. bf_set(lpfc_mbx_set_diag_state_diag,
  1672. &link_diag_state->u.req, 0);
  1673. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1674. if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
  1675. rc = 0;
  1676. else
  1677. rc = -ENODEV;
  1678. link_diag_state_set_out:
  1679. if (pmboxq && (mbxstatus != MBX_TIMEOUT))
  1680. mempool_free(pmboxq, phba->mbox_mem_pool);
  1681. return rc;
  1682. }
  1683. /**
  1684. * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
  1685. * @phba: Pointer to HBA context object.
  1686. *
  1687. * This function is responsible for issuing a sli4 mailbox command for setting
  1688. * up internal loopback diagnostic.
  1689. */
  1690. static int
  1691. lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
  1692. {
  1693. LPFC_MBOXQ_t *pmboxq;
  1694. uint32_t req_len, alloc_len;
  1695. struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
  1696. int mbxstatus = MBX_SUCCESS, rc = 0;
  1697. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1698. if (!pmboxq)
  1699. return -ENOMEM;
  1700. req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
  1701. sizeof(struct lpfc_sli4_cfg_mhdr));
  1702. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  1703. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
  1704. req_len, LPFC_SLI4_MBX_EMBED);
  1705. if (alloc_len != req_len) {
  1706. mempool_free(pmboxq, phba->mbox_mem_pool);
  1707. return -ENOMEM;
  1708. }
  1709. link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
  1710. bf_set(lpfc_mbx_set_diag_state_link_num,
  1711. &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
  1712. bf_set(lpfc_mbx_set_diag_state_link_type,
  1713. &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
  1714. bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
  1715. LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
  1716. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1717. if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
  1718. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1719. "3127 Failed setup loopback mode mailbox "
  1720. "command, rc:x%x, status:x%x\n", mbxstatus,
  1721. pmboxq->u.mb.mbxStatus);
  1722. rc = -ENODEV;
  1723. }
  1724. if (pmboxq && (mbxstatus != MBX_TIMEOUT))
  1725. mempool_free(pmboxq, phba->mbox_mem_pool);
  1726. return rc;
  1727. }
  1728. /**
  1729. * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
  1730. * @phba: Pointer to HBA context object.
  1731. *
  1732. * This function set up SLI4 FC port registrations for diagnostic run, which
  1733. * includes all the rpis, vfi, and also vpi.
  1734. */
  1735. static int
  1736. lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
  1737. {
  1738. int rc;
  1739. if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
  1740. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1741. "3136 Port still had vfi registered: "
  1742. "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
  1743. phba->pport->fc_myDID, phba->fcf.fcfi,
  1744. phba->sli4_hba.vfi_ids[phba->pport->vfi],
  1745. phba->vpi_ids[phba->pport->vpi]);
  1746. return -EINVAL;
  1747. }
  1748. rc = lpfc_issue_reg_vfi(phba->pport);
  1749. return rc;
  1750. }
  1751. /**
  1752. * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
  1753. * @phba: Pointer to HBA context object.
  1754. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1755. *
  1756. * This function is responsible for placing an sli4 port into diagnostic
  1757. * loopback mode in order to perform a diagnostic loopback test.
  1758. */
  1759. static int
  1760. lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
  1761. {
  1762. struct diag_mode_set *loopback_mode;
  1763. uint32_t link_flags, timeout;
  1764. int i, rc = 0;
  1765. /* no data to return just the return code */
  1766. job->reply->reply_payload_rcv_len = 0;
  1767. if (job->request_len < sizeof(struct fc_bsg_request) +
  1768. sizeof(struct diag_mode_set)) {
  1769. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1770. "3011 Received DIAG MODE request size:%d "
  1771. "below the minimum size:%d\n",
  1772. job->request_len,
  1773. (int)(sizeof(struct fc_bsg_request) +
  1774. sizeof(struct diag_mode_set)));
  1775. rc = -EINVAL;
  1776. goto job_error;
  1777. }
  1778. rc = lpfc_bsg_diag_mode_enter(phba);
  1779. if (rc)
  1780. goto job_error;
  1781. /* indicate we are in loobpack diagnostic mode */
  1782. spin_lock_irq(&phba->hbalock);
  1783. phba->link_flag |= LS_LOOPBACK_MODE;
  1784. spin_unlock_irq(&phba->hbalock);
  1785. /* reset port to start frome scratch */
  1786. rc = lpfc_selective_reset(phba);
  1787. if (rc)
  1788. goto job_error;
  1789. /* bring the link to diagnostic mode */
  1790. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1791. "3129 Bring link to diagnostic state.\n");
  1792. loopback_mode = (struct diag_mode_set *)
  1793. job->request->rqst_data.h_vendor.vendor_cmd;
  1794. link_flags = loopback_mode->type;
  1795. timeout = loopback_mode->timeout * 100;
  1796. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
  1797. if (rc) {
  1798. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1799. "3130 Failed to bring link to diagnostic "
  1800. "state, rc:x%x\n", rc);
  1801. goto loopback_mode_exit;
  1802. }
  1803. /* wait for link down before proceeding */
  1804. i = 0;
  1805. while (phba->link_state != LPFC_LINK_DOWN) {
  1806. if (i++ > timeout) {
  1807. rc = -ETIMEDOUT;
  1808. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1809. "3131 Timeout waiting for link to "
  1810. "diagnostic mode, timeout:%d ms\n",
  1811. timeout * 10);
  1812. goto loopback_mode_exit;
  1813. }
  1814. msleep(10);
  1815. }
  1816. /* set up loopback mode */
  1817. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1818. "3132 Set up loopback mode:x%x\n", link_flags);
  1819. if (link_flags == INTERNAL_LOOP_BACK)
  1820. rc = lpfc_sli4_bsg_set_internal_loopback(phba);
  1821. else if (link_flags == EXTERNAL_LOOP_BACK)
  1822. rc = lpfc_hba_init_link_fc_topology(phba,
  1823. FLAGS_TOPOLOGY_MODE_PT_PT,
  1824. MBX_NOWAIT);
  1825. else {
  1826. rc = -EINVAL;
  1827. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  1828. "3141 Loopback mode:x%x not supported\n",
  1829. link_flags);
  1830. goto loopback_mode_exit;
  1831. }
  1832. if (!rc) {
  1833. /* wait for the link attention interrupt */
  1834. msleep(100);
  1835. i = 0;
  1836. while (phba->link_state < LPFC_LINK_UP) {
  1837. if (i++ > timeout) {
  1838. rc = -ETIMEDOUT;
  1839. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1840. "3137 Timeout waiting for link up "
  1841. "in loopback mode, timeout:%d ms\n",
  1842. timeout * 10);
  1843. break;
  1844. }
  1845. msleep(10);
  1846. }
  1847. }
  1848. /* port resource registration setup for loopback diagnostic */
  1849. if (!rc) {
  1850. /* set up a none zero myDID for loopback test */
  1851. phba->pport->fc_myDID = 1;
  1852. rc = lpfc_sli4_diag_fcport_reg_setup(phba);
  1853. } else
  1854. goto loopback_mode_exit;
  1855. if (!rc) {
  1856. /* wait for the port ready */
  1857. msleep(100);
  1858. i = 0;
  1859. while (phba->link_state != LPFC_HBA_READY) {
  1860. if (i++ > timeout) {
  1861. rc = -ETIMEDOUT;
  1862. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1863. "3133 Timeout waiting for port "
  1864. "loopback mode ready, timeout:%d ms\n",
  1865. timeout * 10);
  1866. break;
  1867. }
  1868. msleep(10);
  1869. }
  1870. }
  1871. loopback_mode_exit:
  1872. /* clear loopback diagnostic mode */
  1873. if (rc) {
  1874. spin_lock_irq(&phba->hbalock);
  1875. phba->link_flag &= ~LS_LOOPBACK_MODE;
  1876. spin_unlock_irq(&phba->hbalock);
  1877. }
  1878. lpfc_bsg_diag_mode_exit(phba);
  1879. job_error:
  1880. /* make error code available to userspace */
  1881. job->reply->result = rc;
  1882. /* complete the job back to userspace if no error */
  1883. if (rc == 0)
  1884. job->job_done(job);
  1885. return rc;
  1886. }
  1887. /**
  1888. * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
  1889. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1890. *
  1891. * This function is responsible for responding to check and dispatch bsg diag
  1892. * command from the user to proper driver action routines.
  1893. */
  1894. static int
  1895. lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
  1896. {
  1897. struct Scsi_Host *shost;
  1898. struct lpfc_vport *vport;
  1899. struct lpfc_hba *phba;
  1900. int rc;
  1901. shost = job->shost;
  1902. if (!shost)
  1903. return -ENODEV;
  1904. vport = (struct lpfc_vport *)job->shost->hostdata;
  1905. if (!vport)
  1906. return -ENODEV;
  1907. phba = vport->phba;
  1908. if (!phba)
  1909. return -ENODEV;
  1910. if (phba->sli_rev < LPFC_SLI_REV4)
  1911. rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
  1912. else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  1913. LPFC_SLI_INTF_IF_TYPE_2)
  1914. rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
  1915. else
  1916. rc = -ENODEV;
  1917. return rc;
  1918. }
  1919. /**
  1920. * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
  1921. * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
  1922. *
  1923. * This function is responsible for responding to check and dispatch bsg diag
  1924. * command from the user to proper driver action routines.
  1925. */
  1926. static int
  1927. lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
  1928. {
  1929. struct Scsi_Host *shost;
  1930. struct lpfc_vport *vport;
  1931. struct lpfc_hba *phba;
  1932. struct diag_mode_set *loopback_mode_end_cmd;
  1933. uint32_t timeout;
  1934. int rc, i;
  1935. shost = job->shost;
  1936. if (!shost)
  1937. return -ENODEV;
  1938. vport = (struct lpfc_vport *)job->shost->hostdata;
  1939. if (!vport)
  1940. return -ENODEV;
  1941. phba = vport->phba;
  1942. if (!phba)
  1943. return -ENODEV;
  1944. if (phba->sli_rev < LPFC_SLI_REV4)
  1945. return -ENODEV;
  1946. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  1947. LPFC_SLI_INTF_IF_TYPE_2)
  1948. return -ENODEV;
  1949. /* clear loopback diagnostic mode */
  1950. spin_lock_irq(&phba->hbalock);
  1951. phba->link_flag &= ~LS_LOOPBACK_MODE;
  1952. spin_unlock_irq(&phba->hbalock);
  1953. loopback_mode_end_cmd = (struct diag_mode_set *)
  1954. job->request->rqst_data.h_vendor.vendor_cmd;
  1955. timeout = loopback_mode_end_cmd->timeout * 100;
  1956. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
  1957. if (rc) {
  1958. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1959. "3139 Failed to bring link to diagnostic "
  1960. "state, rc:x%x\n", rc);
  1961. goto loopback_mode_end_exit;
  1962. }
  1963. /* wait for link down before proceeding */
  1964. i = 0;
  1965. while (phba->link_state != LPFC_LINK_DOWN) {
  1966. if (i++ > timeout) {
  1967. rc = -ETIMEDOUT;
  1968. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1969. "3140 Timeout waiting for link to "
  1970. "diagnostic mode_end, timeout:%d ms\n",
  1971. timeout * 10);
  1972. /* there is nothing much we can do here */
  1973. break;
  1974. }
  1975. msleep(10);
  1976. }
  1977. /* reset port resource registrations */
  1978. rc = lpfc_selective_reset(phba);
  1979. phba->pport->fc_myDID = 0;
  1980. loopback_mode_end_exit:
  1981. /* make return code available to userspace */
  1982. job->reply->result = rc;
  1983. /* complete the job back to userspace if no error */
  1984. if (rc == 0)
  1985. job->job_done(job);
  1986. return rc;
  1987. }
  1988. /**
  1989. * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
  1990. * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
  1991. *
  1992. * This function is to perform SLI4 diag link test request from the user
  1993. * applicaiton.
  1994. */
  1995. static int
  1996. lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
  1997. {
  1998. struct Scsi_Host *shost;
  1999. struct lpfc_vport *vport;
  2000. struct lpfc_hba *phba;
  2001. LPFC_MBOXQ_t *pmboxq;
  2002. struct sli4_link_diag *link_diag_test_cmd;
  2003. uint32_t req_len, alloc_len;
  2004. uint32_t timeout;
  2005. struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
  2006. union lpfc_sli4_cfg_shdr *shdr;
  2007. uint32_t shdr_status, shdr_add_status;
  2008. struct diag_status *diag_status_reply;
  2009. int mbxstatus, rc = 0;
  2010. shost = job->shost;
  2011. if (!shost) {
  2012. rc = -ENODEV;
  2013. goto job_error;
  2014. }
  2015. vport = (struct lpfc_vport *)job->shost->hostdata;
  2016. if (!vport) {
  2017. rc = -ENODEV;
  2018. goto job_error;
  2019. }
  2020. phba = vport->phba;
  2021. if (!phba) {
  2022. rc = -ENODEV;
  2023. goto job_error;
  2024. }
  2025. if (phba->sli_rev < LPFC_SLI_REV4) {
  2026. rc = -ENODEV;
  2027. goto job_error;
  2028. }
  2029. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  2030. LPFC_SLI_INTF_IF_TYPE_2) {
  2031. rc = -ENODEV;
  2032. goto job_error;
  2033. }
  2034. if (job->request_len < sizeof(struct fc_bsg_request) +
  2035. sizeof(struct sli4_link_diag)) {
  2036. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2037. "3013 Received LINK DIAG TEST request "
  2038. " size:%d below the minimum size:%d\n",
  2039. job->request_len,
  2040. (int)(sizeof(struct fc_bsg_request) +
  2041. sizeof(struct sli4_link_diag)));
  2042. rc = -EINVAL;
  2043. goto job_error;
  2044. }
  2045. rc = lpfc_bsg_diag_mode_enter(phba);
  2046. if (rc)
  2047. goto job_error;
  2048. link_diag_test_cmd = (struct sli4_link_diag *)
  2049. job->request->rqst_data.h_vendor.vendor_cmd;
  2050. timeout = link_diag_test_cmd->timeout * 100;
  2051. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
  2052. if (rc)
  2053. goto job_error;
  2054. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2055. if (!pmboxq) {
  2056. rc = -ENOMEM;
  2057. goto link_diag_test_exit;
  2058. }
  2059. req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
  2060. sizeof(struct lpfc_sli4_cfg_mhdr));
  2061. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  2062. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
  2063. req_len, LPFC_SLI4_MBX_EMBED);
  2064. if (alloc_len != req_len) {
  2065. rc = -ENOMEM;
  2066. goto link_diag_test_exit;
  2067. }
  2068. run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
  2069. bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
  2070. phba->sli4_hba.lnk_info.lnk_no);
  2071. bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
  2072. phba->sli4_hba.lnk_info.lnk_tp);
  2073. bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
  2074. link_diag_test_cmd->test_id);
  2075. bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
  2076. link_diag_test_cmd->loops);
  2077. bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
  2078. link_diag_test_cmd->test_version);
  2079. bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
  2080. link_diag_test_cmd->error_action);
  2081. mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
  2082. shdr = (union lpfc_sli4_cfg_shdr *)
  2083. &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  2084. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  2085. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  2086. if (shdr_status || shdr_add_status || mbxstatus) {
  2087. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2088. "3010 Run link diag test mailbox failed with "
  2089. "mbx_status x%x status x%x, add_status x%x\n",
  2090. mbxstatus, shdr_status, shdr_add_status);
  2091. }
  2092. diag_status_reply = (struct diag_status *)
  2093. job->reply->reply_data.vendor_reply.vendor_rsp;
  2094. if (job->reply_len <
  2095. sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
  2096. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2097. "3012 Received Run link diag test reply "
  2098. "below minimum size (%d): reply_len:%d\n",
  2099. (int)(sizeof(struct fc_bsg_request) +
  2100. sizeof(struct diag_status)),
  2101. job->reply_len);
  2102. rc = -EINVAL;
  2103. goto job_error;
  2104. }
  2105. diag_status_reply->mbox_status = mbxstatus;
  2106. diag_status_reply->shdr_status = shdr_status;
  2107. diag_status_reply->shdr_add_status = shdr_add_status;
  2108. link_diag_test_exit:
  2109. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
  2110. if (pmboxq)
  2111. mempool_free(pmboxq, phba->mbox_mem_pool);
  2112. lpfc_bsg_diag_mode_exit(phba);
  2113. job_error:
  2114. /* make error code available to userspace */
  2115. job->reply->result = rc;
  2116. /* complete the job back to userspace if no error */
  2117. if (rc == 0)
  2118. job->job_done(job);
  2119. return rc;
  2120. }
  2121. /**
  2122. * lpfcdiag_loop_self_reg - obtains a remote port login id
  2123. * @phba: Pointer to HBA context object
  2124. * @rpi: Pointer to a remote port login id
  2125. *
  2126. * This function obtains a remote port login id so the diag loopback test
  2127. * can send and receive its own unsolicited CT command.
  2128. **/
  2129. static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
  2130. {
  2131. LPFC_MBOXQ_t *mbox;
  2132. struct lpfc_dmabuf *dmabuff;
  2133. int status;
  2134. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2135. if (!mbox)
  2136. return -ENOMEM;
  2137. if (phba->sli_rev < LPFC_SLI_REV4)
  2138. status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
  2139. (uint8_t *)&phba->pport->fc_sparam,
  2140. mbox, *rpi);
  2141. else {
  2142. *rpi = lpfc_sli4_alloc_rpi(phba);
  2143. status = lpfc_reg_rpi(phba, phba->pport->vpi,
  2144. phba->pport->fc_myDID,
  2145. (uint8_t *)&phba->pport->fc_sparam,
  2146. mbox, *rpi);
  2147. }
  2148. if (status) {
  2149. mempool_free(mbox, phba->mbox_mem_pool);
  2150. if (phba->sli_rev == LPFC_SLI_REV4)
  2151. lpfc_sli4_free_rpi(phba, *rpi);
  2152. return -ENOMEM;
  2153. }
  2154. dmabuff = (struct lpfc_dmabuf *) mbox->context1;
  2155. mbox->context1 = NULL;
  2156. mbox->context2 = NULL;
  2157. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2158. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  2159. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  2160. kfree(dmabuff);
  2161. if (status != MBX_TIMEOUT)
  2162. mempool_free(mbox, phba->mbox_mem_pool);
  2163. if (phba->sli_rev == LPFC_SLI_REV4)
  2164. lpfc_sli4_free_rpi(phba, *rpi);
  2165. return -ENODEV;
  2166. }
  2167. if (phba->sli_rev < LPFC_SLI_REV4)
  2168. *rpi = mbox->u.mb.un.varWords[0];
  2169. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  2170. kfree(dmabuff);
  2171. mempool_free(mbox, phba->mbox_mem_pool);
  2172. return 0;
  2173. }
  2174. /**
  2175. * lpfcdiag_loop_self_unreg - unregs from the rpi
  2176. * @phba: Pointer to HBA context object
  2177. * @rpi: Remote port login id
  2178. *
  2179. * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
  2180. **/
  2181. static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
  2182. {
  2183. LPFC_MBOXQ_t *mbox;
  2184. int status;
  2185. /* Allocate mboxq structure */
  2186. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2187. if (mbox == NULL)
  2188. return -ENOMEM;
  2189. if (phba->sli_rev < LPFC_SLI_REV4)
  2190. lpfc_unreg_login(phba, 0, rpi, mbox);
  2191. else
  2192. lpfc_unreg_login(phba, phba->pport->vpi,
  2193. phba->sli4_hba.rpi_ids[rpi], mbox);
  2194. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2195. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  2196. if (status != MBX_TIMEOUT)
  2197. mempool_free(mbox, phba->mbox_mem_pool);
  2198. return -EIO;
  2199. }
  2200. mempool_free(mbox, phba->mbox_mem_pool);
  2201. if (phba->sli_rev == LPFC_SLI_REV4)
  2202. lpfc_sli4_free_rpi(phba, rpi);
  2203. return 0;
  2204. }
  2205. /**
  2206. * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
  2207. * @phba: Pointer to HBA context object
  2208. * @rpi: Remote port login id
  2209. * @txxri: Pointer to transmit exchange id
  2210. * @rxxri: Pointer to response exchabge id
  2211. *
  2212. * This function obtains the transmit and receive ids required to send
  2213. * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
  2214. * flags are used to the unsolicted response handler is able to process
  2215. * the ct command sent on the same port.
  2216. **/
  2217. static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
  2218. uint16_t *txxri, uint16_t * rxxri)
  2219. {
  2220. struct lpfc_bsg_event *evt;
  2221. struct lpfc_iocbq *cmdiocbq, *rspiocbq;
  2222. IOCB_t *cmd, *rsp;
  2223. struct lpfc_dmabuf *dmabuf;
  2224. struct ulp_bde64 *bpl = NULL;
  2225. struct lpfc_sli_ct_request *ctreq = NULL;
  2226. int ret_val = 0;
  2227. int time_left;
  2228. int iocb_stat = 0;
  2229. unsigned long flags;
  2230. *txxri = 0;
  2231. *rxxri = 0;
  2232. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  2233. SLI_CT_ELX_LOOPBACK);
  2234. if (!evt)
  2235. return -ENOMEM;
  2236. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2237. list_add(&evt->node, &phba->ct_ev_waiters);
  2238. lpfc_bsg_event_ref(evt);
  2239. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2240. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2241. rspiocbq = lpfc_sli_get_iocbq(phba);
  2242. dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2243. if (dmabuf) {
  2244. dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
  2245. if (dmabuf->virt) {
  2246. INIT_LIST_HEAD(&dmabuf->list);
  2247. bpl = (struct ulp_bde64 *) dmabuf->virt;
  2248. memset(bpl, 0, sizeof(*bpl));
  2249. ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
  2250. bpl->addrHigh =
  2251. le32_to_cpu(putPaddrHigh(dmabuf->phys +
  2252. sizeof(*bpl)));
  2253. bpl->addrLow =
  2254. le32_to_cpu(putPaddrLow(dmabuf->phys +
  2255. sizeof(*bpl)));
  2256. bpl->tus.f.bdeFlags = 0;
  2257. bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
  2258. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  2259. }
  2260. }
  2261. if (cmdiocbq == NULL || rspiocbq == NULL ||
  2262. dmabuf == NULL || bpl == NULL || ctreq == NULL ||
  2263. dmabuf->virt == NULL) {
  2264. ret_val = -ENOMEM;
  2265. goto err_get_xri_exit;
  2266. }
  2267. cmd = &cmdiocbq->iocb;
  2268. rsp = &rspiocbq->iocb;
  2269. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  2270. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  2271. ctreq->RevisionId.bits.InId = 0;
  2272. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  2273. ctreq->FsSubType = 0;
  2274. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
  2275. ctreq->CommandResponse.bits.Size = 0;
  2276. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
  2277. cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
  2278. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  2279. cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
  2280. cmd->un.xseq64.w5.hcsw.Fctl = LA;
  2281. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  2282. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  2283. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  2284. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
  2285. cmd->ulpBdeCount = 1;
  2286. cmd->ulpLe = 1;
  2287. cmd->ulpClass = CLASS3;
  2288. cmd->ulpContext = rpi;
  2289. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  2290. cmdiocbq->vport = phba->pport;
  2291. iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  2292. rspiocbq,
  2293. (phba->fc_ratov * 2)
  2294. + LPFC_DRVR_TIMEOUT);
  2295. if (iocb_stat) {
  2296. ret_val = -EIO;
  2297. goto err_get_xri_exit;
  2298. }
  2299. *txxri = rsp->ulpContext;
  2300. evt->waiting = 1;
  2301. evt->wait_time_stamp = jiffies;
  2302. time_left = wait_event_interruptible_timeout(
  2303. evt->wq, !list_empty(&evt->events_to_see),
  2304. msecs_to_jiffies(1000 *
  2305. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
  2306. if (list_empty(&evt->events_to_see))
  2307. ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
  2308. else {
  2309. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2310. list_move(evt->events_to_see.prev, &evt->events_to_get);
  2311. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2312. *rxxri = (list_entry(evt->events_to_get.prev,
  2313. typeof(struct event_data),
  2314. node))->immed_dat;
  2315. }
  2316. evt->waiting = 0;
  2317. err_get_xri_exit:
  2318. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2319. lpfc_bsg_event_unref(evt); /* release ref */
  2320. lpfc_bsg_event_unref(evt); /* delete */
  2321. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2322. if (dmabuf) {
  2323. if (dmabuf->virt)
  2324. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  2325. kfree(dmabuf);
  2326. }
  2327. if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
  2328. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2329. if (rspiocbq)
  2330. lpfc_sli_release_iocbq(phba, rspiocbq);
  2331. return ret_val;
  2332. }
  2333. /**
  2334. * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
  2335. * @phba: Pointer to HBA context object
  2336. *
  2337. * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
  2338. * retruns the pointer to the buffer.
  2339. **/
  2340. static struct lpfc_dmabuf *
  2341. lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
  2342. {
  2343. struct lpfc_dmabuf *dmabuf;
  2344. struct pci_dev *pcidev = phba->pcidev;
  2345. /* allocate dma buffer struct */
  2346. dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2347. if (!dmabuf)
  2348. return NULL;
  2349. INIT_LIST_HEAD(&dmabuf->list);
  2350. /* now, allocate dma buffer */
  2351. dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
  2352. &(dmabuf->phys), GFP_KERNEL);
  2353. if (!dmabuf->virt) {
  2354. kfree(dmabuf);
  2355. return NULL;
  2356. }
  2357. memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
  2358. return dmabuf;
  2359. }
  2360. /**
  2361. * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
  2362. * @phba: Pointer to HBA context object.
  2363. * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
  2364. *
  2365. * This routine just simply frees a dma buffer and its associated buffer
  2366. * descriptor referred by @dmabuf.
  2367. **/
  2368. static void
  2369. lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
  2370. {
  2371. struct pci_dev *pcidev = phba->pcidev;
  2372. if (!dmabuf)
  2373. return;
  2374. if (dmabuf->virt)
  2375. dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
  2376. dmabuf->virt, dmabuf->phys);
  2377. kfree(dmabuf);
  2378. return;
  2379. }
  2380. /**
  2381. * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
  2382. * @phba: Pointer to HBA context object.
  2383. * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
  2384. *
  2385. * This routine just simply frees all dma buffers and their associated buffer
  2386. * descriptors referred by @dmabuf_list.
  2387. **/
  2388. static void
  2389. lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
  2390. struct list_head *dmabuf_list)
  2391. {
  2392. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  2393. if (list_empty(dmabuf_list))
  2394. return;
  2395. list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
  2396. list_del_init(&dmabuf->list);
  2397. lpfc_bsg_dma_page_free(phba, dmabuf);
  2398. }
  2399. return;
  2400. }
  2401. /**
  2402. * diag_cmd_data_alloc - fills in a bde struct with dma buffers
  2403. * @phba: Pointer to HBA context object
  2404. * @bpl: Pointer to 64 bit bde structure
  2405. * @size: Number of bytes to process
  2406. * @nocopydata: Flag to copy user data into the allocated buffer
  2407. *
  2408. * This function allocates page size buffers and populates an lpfc_dmabufext.
  2409. * If allowed the user data pointed to with indataptr is copied into the kernel
  2410. * memory. The chained list of page size buffers is returned.
  2411. **/
  2412. static struct lpfc_dmabufext *
  2413. diag_cmd_data_alloc(struct lpfc_hba *phba,
  2414. struct ulp_bde64 *bpl, uint32_t size,
  2415. int nocopydata)
  2416. {
  2417. struct lpfc_dmabufext *mlist = NULL;
  2418. struct lpfc_dmabufext *dmp;
  2419. int cnt, offset = 0, i = 0;
  2420. struct pci_dev *pcidev;
  2421. pcidev = phba->pcidev;
  2422. while (size) {
  2423. /* We get chunks of 4K */
  2424. if (size > BUF_SZ_4K)
  2425. cnt = BUF_SZ_4K;
  2426. else
  2427. cnt = size;
  2428. /* allocate struct lpfc_dmabufext buffer header */
  2429. dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
  2430. if (!dmp)
  2431. goto out;
  2432. INIT_LIST_HEAD(&dmp->dma.list);
  2433. /* Queue it to a linked list */
  2434. if (mlist)
  2435. list_add_tail(&dmp->dma.list, &mlist->dma.list);
  2436. else
  2437. mlist = dmp;
  2438. /* allocate buffer */
  2439. dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
  2440. cnt,
  2441. &(dmp->dma.phys),
  2442. GFP_KERNEL);
  2443. if (!dmp->dma.virt)
  2444. goto out;
  2445. dmp->size = cnt;
  2446. if (nocopydata) {
  2447. bpl->tus.f.bdeFlags = 0;
  2448. pci_dma_sync_single_for_device(phba->pcidev,
  2449. dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
  2450. } else {
  2451. memset((uint8_t *)dmp->dma.virt, 0, cnt);
  2452. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  2453. }
  2454. /* build buffer ptr list for IOCB */
  2455. bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
  2456. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
  2457. bpl->tus.f.bdeSize = (ushort) cnt;
  2458. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  2459. bpl++;
  2460. i++;
  2461. offset += cnt;
  2462. size -= cnt;
  2463. }
  2464. mlist->flag = i;
  2465. return mlist;
  2466. out:
  2467. diag_cmd_data_free(phba, mlist);
  2468. return NULL;
  2469. }
  2470. /**
  2471. * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
  2472. * @phba: Pointer to HBA context object
  2473. * @rxxri: Receive exchange id
  2474. * @len: Number of data bytes
  2475. *
  2476. * This function allocates and posts a data buffer of sufficient size to receive
  2477. * an unsolicted CT command.
  2478. **/
  2479. static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
  2480. size_t len)
  2481. {
  2482. struct lpfc_sli *psli = &phba->sli;
  2483. struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
  2484. struct lpfc_iocbq *cmdiocbq;
  2485. IOCB_t *cmd = NULL;
  2486. struct list_head head, *curr, *next;
  2487. struct lpfc_dmabuf *rxbmp;
  2488. struct lpfc_dmabuf *dmp;
  2489. struct lpfc_dmabuf *mp[2] = {NULL, NULL};
  2490. struct ulp_bde64 *rxbpl = NULL;
  2491. uint32_t num_bde;
  2492. struct lpfc_dmabufext *rxbuffer = NULL;
  2493. int ret_val = 0;
  2494. int iocb_stat;
  2495. int i = 0;
  2496. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2497. rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2498. if (rxbmp != NULL) {
  2499. rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
  2500. if (rxbmp->virt) {
  2501. INIT_LIST_HEAD(&rxbmp->list);
  2502. rxbpl = (struct ulp_bde64 *) rxbmp->virt;
  2503. rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
  2504. }
  2505. }
  2506. if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
  2507. ret_val = -ENOMEM;
  2508. goto err_post_rxbufs_exit;
  2509. }
  2510. /* Queue buffers for the receive exchange */
  2511. num_bde = (uint32_t)rxbuffer->flag;
  2512. dmp = &rxbuffer->dma;
  2513. cmd = &cmdiocbq->iocb;
  2514. i = 0;
  2515. INIT_LIST_HEAD(&head);
  2516. list_add_tail(&head, &dmp->list);
  2517. list_for_each_safe(curr, next, &head) {
  2518. mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
  2519. list_del(curr);
  2520. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2521. mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
  2522. cmd->un.quexri64cx.buff.bde.addrHigh =
  2523. putPaddrHigh(mp[i]->phys);
  2524. cmd->un.quexri64cx.buff.bde.addrLow =
  2525. putPaddrLow(mp[i]->phys);
  2526. cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
  2527. ((struct lpfc_dmabufext *)mp[i])->size;
  2528. cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
  2529. cmd->ulpCommand = CMD_QUE_XRI64_CX;
  2530. cmd->ulpPU = 0;
  2531. cmd->ulpLe = 1;
  2532. cmd->ulpBdeCount = 1;
  2533. cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
  2534. } else {
  2535. cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
  2536. cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
  2537. cmd->un.cont64[i].tus.f.bdeSize =
  2538. ((struct lpfc_dmabufext *)mp[i])->size;
  2539. cmd->ulpBdeCount = ++i;
  2540. if ((--num_bde > 0) && (i < 2))
  2541. continue;
  2542. cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
  2543. cmd->ulpLe = 1;
  2544. }
  2545. cmd->ulpClass = CLASS3;
  2546. cmd->ulpContext = rxxri;
  2547. iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
  2548. 0);
  2549. if (iocb_stat == IOCB_ERROR) {
  2550. diag_cmd_data_free(phba,
  2551. (struct lpfc_dmabufext *)mp[0]);
  2552. if (mp[1])
  2553. diag_cmd_data_free(phba,
  2554. (struct lpfc_dmabufext *)mp[1]);
  2555. dmp = list_entry(next, struct lpfc_dmabuf, list);
  2556. ret_val = -EIO;
  2557. goto err_post_rxbufs_exit;
  2558. }
  2559. lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
  2560. if (mp[1]) {
  2561. lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
  2562. mp[1] = NULL;
  2563. }
  2564. /* The iocb was freed by lpfc_sli_issue_iocb */
  2565. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2566. if (!cmdiocbq) {
  2567. dmp = list_entry(next, struct lpfc_dmabuf, list);
  2568. ret_val = -EIO;
  2569. goto err_post_rxbufs_exit;
  2570. }
  2571. cmd = &cmdiocbq->iocb;
  2572. i = 0;
  2573. }
  2574. list_del(&head);
  2575. err_post_rxbufs_exit:
  2576. if (rxbmp) {
  2577. if (rxbmp->virt)
  2578. lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
  2579. kfree(rxbmp);
  2580. }
  2581. if (cmdiocbq)
  2582. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2583. return ret_val;
  2584. }
  2585. /**
  2586. * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
  2587. * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
  2588. *
  2589. * This function receives a user data buffer to be transmitted and received on
  2590. * the same port, the link must be up and in loopback mode prior
  2591. * to being called.
  2592. * 1. A kernel buffer is allocated to copy the user data into.
  2593. * 2. The port registers with "itself".
  2594. * 3. The transmit and receive exchange ids are obtained.
  2595. * 4. The receive exchange id is posted.
  2596. * 5. A new els loopback event is created.
  2597. * 6. The command and response iocbs are allocated.
  2598. * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
  2599. *
  2600. * This function is meant to be called n times while the port is in loopback
  2601. * so it is the apps responsibility to issue a reset to take the port out
  2602. * of loopback mode.
  2603. **/
  2604. static int
  2605. lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
  2606. {
  2607. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2608. struct lpfc_hba *phba = vport->phba;
  2609. struct diag_mode_test *diag_mode;
  2610. struct lpfc_bsg_event *evt;
  2611. struct event_data *evdat;
  2612. struct lpfc_sli *psli = &phba->sli;
  2613. uint32_t size;
  2614. uint32_t full_size;
  2615. size_t segment_len = 0, segment_offset = 0, current_offset = 0;
  2616. uint16_t rpi = 0;
  2617. struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
  2618. IOCB_t *cmd, *rsp = NULL;
  2619. struct lpfc_sli_ct_request *ctreq;
  2620. struct lpfc_dmabuf *txbmp;
  2621. struct ulp_bde64 *txbpl = NULL;
  2622. struct lpfc_dmabufext *txbuffer = NULL;
  2623. struct list_head head;
  2624. struct lpfc_dmabuf *curr;
  2625. uint16_t txxri = 0, rxxri;
  2626. uint32_t num_bde;
  2627. uint8_t *ptr = NULL, *rx_databuf = NULL;
  2628. int rc = 0;
  2629. int time_left;
  2630. int iocb_stat;
  2631. unsigned long flags;
  2632. void *dataout = NULL;
  2633. uint32_t total_mem;
  2634. /* in case no data is returned return just the return code */
  2635. job->reply->reply_payload_rcv_len = 0;
  2636. if (job->request_len <
  2637. sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
  2638. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2639. "2739 Received DIAG TEST request below minimum "
  2640. "size\n");
  2641. rc = -EINVAL;
  2642. goto loopback_test_exit;
  2643. }
  2644. if (job->request_payload.payload_len !=
  2645. job->reply_payload.payload_len) {
  2646. rc = -EINVAL;
  2647. goto loopback_test_exit;
  2648. }
  2649. diag_mode = (struct diag_mode_test *)
  2650. job->request->rqst_data.h_vendor.vendor_cmd;
  2651. if ((phba->link_state == LPFC_HBA_ERROR) ||
  2652. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  2653. (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
  2654. rc = -EACCES;
  2655. goto loopback_test_exit;
  2656. }
  2657. if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
  2658. rc = -EACCES;
  2659. goto loopback_test_exit;
  2660. }
  2661. size = job->request_payload.payload_len;
  2662. full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
  2663. if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
  2664. rc = -ERANGE;
  2665. goto loopback_test_exit;
  2666. }
  2667. if (full_size >= BUF_SZ_4K) {
  2668. /*
  2669. * Allocate memory for ioctl data. If buffer is bigger than 64k,
  2670. * then we allocate 64k and re-use that buffer over and over to
  2671. * xfer the whole block. This is because Linux kernel has a
  2672. * problem allocating more than 120k of kernel space memory. Saw
  2673. * problem with GET_FCPTARGETMAPPING...
  2674. */
  2675. if (size <= (64 * 1024))
  2676. total_mem = full_size;
  2677. else
  2678. total_mem = 64 * 1024;
  2679. } else
  2680. /* Allocate memory for ioctl data */
  2681. total_mem = BUF_SZ_4K;
  2682. dataout = kmalloc(total_mem, GFP_KERNEL);
  2683. if (dataout == NULL) {
  2684. rc = -ENOMEM;
  2685. goto loopback_test_exit;
  2686. }
  2687. ptr = dataout;
  2688. ptr += ELX_LOOPBACK_HEADER_SZ;
  2689. sg_copy_to_buffer(job->request_payload.sg_list,
  2690. job->request_payload.sg_cnt,
  2691. ptr, size);
  2692. rc = lpfcdiag_loop_self_reg(phba, &rpi);
  2693. if (rc)
  2694. goto loopback_test_exit;
  2695. if (phba->sli_rev < LPFC_SLI_REV4) {
  2696. rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
  2697. if (rc) {
  2698. lpfcdiag_loop_self_unreg(phba, rpi);
  2699. goto loopback_test_exit;
  2700. }
  2701. rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
  2702. if (rc) {
  2703. lpfcdiag_loop_self_unreg(phba, rpi);
  2704. goto loopback_test_exit;
  2705. }
  2706. }
  2707. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  2708. SLI_CT_ELX_LOOPBACK);
  2709. if (!evt) {
  2710. lpfcdiag_loop_self_unreg(phba, rpi);
  2711. rc = -ENOMEM;
  2712. goto loopback_test_exit;
  2713. }
  2714. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2715. list_add(&evt->node, &phba->ct_ev_waiters);
  2716. lpfc_bsg_event_ref(evt);
  2717. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2718. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2719. if (phba->sli_rev < LPFC_SLI_REV4)
  2720. rspiocbq = lpfc_sli_get_iocbq(phba);
  2721. txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2722. if (txbmp) {
  2723. txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
  2724. if (txbmp->virt) {
  2725. INIT_LIST_HEAD(&txbmp->list);
  2726. txbpl = (struct ulp_bde64 *) txbmp->virt;
  2727. txbuffer = diag_cmd_data_alloc(phba,
  2728. txbpl, full_size, 0);
  2729. }
  2730. }
  2731. if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
  2732. rc = -ENOMEM;
  2733. goto err_loopback_test_exit;
  2734. }
  2735. if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
  2736. rc = -ENOMEM;
  2737. goto err_loopback_test_exit;
  2738. }
  2739. cmd = &cmdiocbq->iocb;
  2740. if (phba->sli_rev < LPFC_SLI_REV4)
  2741. rsp = &rspiocbq->iocb;
  2742. INIT_LIST_HEAD(&head);
  2743. list_add_tail(&head, &txbuffer->dma.list);
  2744. list_for_each_entry(curr, &head, list) {
  2745. segment_len = ((struct lpfc_dmabufext *)curr)->size;
  2746. if (current_offset == 0) {
  2747. ctreq = curr->virt;
  2748. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  2749. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  2750. ctreq->RevisionId.bits.InId = 0;
  2751. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  2752. ctreq->FsSubType = 0;
  2753. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
  2754. ctreq->CommandResponse.bits.Size = size;
  2755. segment_offset = ELX_LOOPBACK_HEADER_SZ;
  2756. } else
  2757. segment_offset = 0;
  2758. BUG_ON(segment_offset >= segment_len);
  2759. memcpy(curr->virt + segment_offset,
  2760. ptr + current_offset,
  2761. segment_len - segment_offset);
  2762. current_offset += segment_len - segment_offset;
  2763. BUG_ON(current_offset > size);
  2764. }
  2765. list_del(&head);
  2766. /* Build the XMIT_SEQUENCE iocb */
  2767. num_bde = (uint32_t)txbuffer->flag;
  2768. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
  2769. cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
  2770. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  2771. cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
  2772. cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  2773. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  2774. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  2775. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  2776. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  2777. cmd->ulpBdeCount = 1;
  2778. cmd->ulpLe = 1;
  2779. cmd->ulpClass = CLASS3;
  2780. if (phba->sli_rev < LPFC_SLI_REV4) {
  2781. cmd->ulpContext = txxri;
  2782. } else {
  2783. cmd->un.xseq64.bdl.ulpIoTag32 = 0;
  2784. cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
  2785. cmdiocbq->context3 = txbmp;
  2786. cmdiocbq->sli4_xritag = NO_XRI;
  2787. cmd->unsli3.rcvsli3.ox_id = 0xffff;
  2788. }
  2789. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  2790. cmdiocbq->vport = phba->pport;
  2791. iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  2792. rspiocbq, (phba->fc_ratov * 2) +
  2793. LPFC_DRVR_TIMEOUT);
  2794. if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
  2795. (rsp->ulpStatus != IOCB_SUCCESS))) {
  2796. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2797. "3126 Failed loopback test issue iocb: "
  2798. "iocb_stat:x%x\n", iocb_stat);
  2799. rc = -EIO;
  2800. goto err_loopback_test_exit;
  2801. }
  2802. evt->waiting = 1;
  2803. time_left = wait_event_interruptible_timeout(
  2804. evt->wq, !list_empty(&evt->events_to_see),
  2805. msecs_to_jiffies(1000 *
  2806. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
  2807. evt->waiting = 0;
  2808. if (list_empty(&evt->events_to_see)) {
  2809. rc = (time_left) ? -EINTR : -ETIMEDOUT;
  2810. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2811. "3125 Not receiving unsolicited event, "
  2812. "rc:x%x\n", rc);
  2813. } else {
  2814. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2815. list_move(evt->events_to_see.prev, &evt->events_to_get);
  2816. evdat = list_entry(evt->events_to_get.prev,
  2817. typeof(*evdat), node);
  2818. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2819. rx_databuf = evdat->data;
  2820. if (evdat->len != full_size) {
  2821. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2822. "1603 Loopback test did not receive expected "
  2823. "data length. actual length 0x%x expected "
  2824. "length 0x%x\n",
  2825. evdat->len, full_size);
  2826. rc = -EIO;
  2827. } else if (rx_databuf == NULL)
  2828. rc = -EIO;
  2829. else {
  2830. rc = IOCB_SUCCESS;
  2831. /* skip over elx loopback header */
  2832. rx_databuf += ELX_LOOPBACK_HEADER_SZ;
  2833. job->reply->reply_payload_rcv_len =
  2834. sg_copy_from_buffer(job->reply_payload.sg_list,
  2835. job->reply_payload.sg_cnt,
  2836. rx_databuf, size);
  2837. job->reply->reply_payload_rcv_len = size;
  2838. }
  2839. }
  2840. err_loopback_test_exit:
  2841. lpfcdiag_loop_self_unreg(phba, rpi);
  2842. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2843. lpfc_bsg_event_unref(evt); /* release ref */
  2844. lpfc_bsg_event_unref(evt); /* delete */
  2845. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2846. if (cmdiocbq != NULL)
  2847. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2848. if (rspiocbq != NULL)
  2849. lpfc_sli_release_iocbq(phba, rspiocbq);
  2850. if (txbmp != NULL) {
  2851. if (txbpl != NULL) {
  2852. if (txbuffer != NULL)
  2853. diag_cmd_data_free(phba, txbuffer);
  2854. lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
  2855. }
  2856. kfree(txbmp);
  2857. }
  2858. loopback_test_exit:
  2859. kfree(dataout);
  2860. /* make error code available to userspace */
  2861. job->reply->result = rc;
  2862. job->dd_data = NULL;
  2863. /* complete the job back to userspace if no error */
  2864. if (rc == IOCB_SUCCESS)
  2865. job->job_done(job);
  2866. return rc;
  2867. }
  2868. /**
  2869. * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
  2870. * @job: GET_DFC_REV fc_bsg_job
  2871. **/
  2872. static int
  2873. lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
  2874. {
  2875. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2876. struct lpfc_hba *phba = vport->phba;
  2877. struct get_mgmt_rev *event_req;
  2878. struct get_mgmt_rev_reply *event_reply;
  2879. int rc = 0;
  2880. if (job->request_len <
  2881. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
  2882. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2883. "2740 Received GET_DFC_REV request below "
  2884. "minimum size\n");
  2885. rc = -EINVAL;
  2886. goto job_error;
  2887. }
  2888. event_req = (struct get_mgmt_rev *)
  2889. job->request->rqst_data.h_vendor.vendor_cmd;
  2890. event_reply = (struct get_mgmt_rev_reply *)
  2891. job->reply->reply_data.vendor_reply.vendor_rsp;
  2892. if (job->reply_len <
  2893. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
  2894. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2895. "2741 Received GET_DFC_REV reply below "
  2896. "minimum size\n");
  2897. rc = -EINVAL;
  2898. goto job_error;
  2899. }
  2900. event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
  2901. event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
  2902. job_error:
  2903. job->reply->result = rc;
  2904. if (rc == 0)
  2905. job->job_done(job);
  2906. return rc;
  2907. }
  2908. /**
  2909. * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
  2910. * @phba: Pointer to HBA context object.
  2911. * @pmboxq: Pointer to mailbox command.
  2912. *
  2913. * This is completion handler function for mailbox commands issued from
  2914. * lpfc_bsg_issue_mbox function. This function is called by the
  2915. * mailbox event handler function with no lock held. This function
  2916. * will wake up thread waiting on the wait queue pointed by context1
  2917. * of the mailbox.
  2918. **/
  2919. void
  2920. lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  2921. {
  2922. struct bsg_job_data *dd_data;
  2923. struct fc_bsg_job *job;
  2924. uint32_t size;
  2925. unsigned long flags;
  2926. uint8_t *pmb, *pmb_buf;
  2927. dd_data = pmboxq->context1;
  2928. /*
  2929. * The outgoing buffer is readily referred from the dma buffer,
  2930. * just need to get header part from mailboxq structure.
  2931. */
  2932. pmb = (uint8_t *)&pmboxq->u.mb;
  2933. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  2934. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  2935. /* Determine if job has been aborted */
  2936. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2937. job = dd_data->set_job;
  2938. if (job) {
  2939. /* Prevent timeout handling from trying to abort job */
  2940. job->dd_data = NULL;
  2941. }
  2942. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2943. /* Copy the mailbox data to the job if it is still active */
  2944. if (job) {
  2945. size = job->reply_payload.payload_len;
  2946. job->reply->reply_payload_rcv_len =
  2947. sg_copy_from_buffer(job->reply_payload.sg_list,
  2948. job->reply_payload.sg_cnt,
  2949. pmb_buf, size);
  2950. }
  2951. dd_data->set_job = NULL;
  2952. mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
  2953. lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
  2954. kfree(dd_data);
  2955. /* Complete the job if the job is still active */
  2956. if (job) {
  2957. job->reply->result = 0;
  2958. job->job_done(job);
  2959. }
  2960. return;
  2961. }
  2962. /**
  2963. * lpfc_bsg_check_cmd_access - test for a supported mailbox command
  2964. * @phba: Pointer to HBA context object.
  2965. * @mb: Pointer to a mailbox object.
  2966. * @vport: Pointer to a vport object.
  2967. *
  2968. * Some commands require the port to be offline, some may not be called from
  2969. * the application.
  2970. **/
  2971. static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
  2972. MAILBOX_t *mb, struct lpfc_vport *vport)
  2973. {
  2974. /* return negative error values for bsg job */
  2975. switch (mb->mbxCommand) {
  2976. /* Offline only */
  2977. case MBX_INIT_LINK:
  2978. case MBX_DOWN_LINK:
  2979. case MBX_CONFIG_LINK:
  2980. case MBX_CONFIG_RING:
  2981. case MBX_RESET_RING:
  2982. case MBX_UNREG_LOGIN:
  2983. case MBX_CLEAR_LA:
  2984. case MBX_DUMP_CONTEXT:
  2985. case MBX_RUN_DIAGS:
  2986. case MBX_RESTART:
  2987. case MBX_SET_MASK:
  2988. if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
  2989. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2990. "2743 Command 0x%x is illegal in on-line "
  2991. "state\n",
  2992. mb->mbxCommand);
  2993. return -EPERM;
  2994. }
  2995. case MBX_WRITE_NV:
  2996. case MBX_WRITE_VPARMS:
  2997. case MBX_LOAD_SM:
  2998. case MBX_READ_NV:
  2999. case MBX_READ_CONFIG:
  3000. case MBX_READ_RCONFIG:
  3001. case MBX_READ_STATUS:
  3002. case MBX_READ_XRI:
  3003. case MBX_READ_REV:
  3004. case MBX_READ_LNK_STAT:
  3005. case MBX_DUMP_MEMORY:
  3006. case MBX_DOWN_LOAD:
  3007. case MBX_UPDATE_CFG:
  3008. case MBX_KILL_BOARD:
  3009. case MBX_LOAD_AREA:
  3010. case MBX_LOAD_EXP_ROM:
  3011. case MBX_BEACON:
  3012. case MBX_DEL_LD_ENTRY:
  3013. case MBX_SET_DEBUG:
  3014. case MBX_WRITE_WWN:
  3015. case MBX_SLI4_CONFIG:
  3016. case MBX_READ_EVENT_LOG:
  3017. case MBX_READ_EVENT_LOG_STATUS:
  3018. case MBX_WRITE_EVENT_LOG:
  3019. case MBX_PORT_CAPABILITIES:
  3020. case MBX_PORT_IOV_CONTROL:
  3021. case MBX_RUN_BIU_DIAG64:
  3022. break;
  3023. case MBX_SET_VARIABLE:
  3024. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3025. "1226 mbox: set_variable 0x%x, 0x%x\n",
  3026. mb->un.varWords[0],
  3027. mb->un.varWords[1]);
  3028. if ((mb->un.varWords[0] == SETVAR_MLOMNT)
  3029. && (mb->un.varWords[1] == 1)) {
  3030. phba->wait_4_mlo_maint_flg = 1;
  3031. } else if (mb->un.varWords[0] == SETVAR_MLORST) {
  3032. spin_lock_irq(&phba->hbalock);
  3033. phba->link_flag &= ~LS_LOOPBACK_MODE;
  3034. spin_unlock_irq(&phba->hbalock);
  3035. phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
  3036. }
  3037. break;
  3038. case MBX_READ_SPARM64:
  3039. case MBX_READ_TOPOLOGY:
  3040. case MBX_REG_LOGIN:
  3041. case MBX_REG_LOGIN64:
  3042. case MBX_CONFIG_PORT:
  3043. case MBX_RUN_BIU_DIAG:
  3044. default:
  3045. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  3046. "2742 Unknown Command 0x%x\n",
  3047. mb->mbxCommand);
  3048. return -EPERM;
  3049. }
  3050. return 0; /* ok */
  3051. }
  3052. /**
  3053. * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
  3054. * @phba: Pointer to HBA context object.
  3055. *
  3056. * This is routine clean up and reset BSG handling of multi-buffer mbox
  3057. * command session.
  3058. **/
  3059. static void
  3060. lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
  3061. {
  3062. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
  3063. return;
  3064. /* free all memory, including dma buffers */
  3065. lpfc_bsg_dma_page_list_free(phba,
  3066. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3067. lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
  3068. /* multi-buffer write mailbox command pass-through complete */
  3069. memset((char *)&phba->mbox_ext_buf_ctx, 0,
  3070. sizeof(struct lpfc_mbox_ext_buf_ctx));
  3071. INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3072. return;
  3073. }
  3074. /**
  3075. * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
  3076. * @phba: Pointer to HBA context object.
  3077. * @pmboxq: Pointer to mailbox command.
  3078. *
  3079. * This is routine handles BSG job for mailbox commands completions with
  3080. * multiple external buffers.
  3081. **/
  3082. static struct fc_bsg_job *
  3083. lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3084. {
  3085. struct bsg_job_data *dd_data;
  3086. struct fc_bsg_job *job;
  3087. uint8_t *pmb, *pmb_buf;
  3088. unsigned long flags;
  3089. uint32_t size;
  3090. int rc = 0;
  3091. struct lpfc_dmabuf *dmabuf;
  3092. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3093. uint8_t *pmbx;
  3094. dd_data = pmboxq->context1;
  3095. /* Determine if job has been aborted */
  3096. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  3097. job = dd_data->set_job;
  3098. if (job) {
  3099. /* Prevent timeout handling from trying to abort job */
  3100. job->dd_data = NULL;
  3101. }
  3102. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  3103. /*
  3104. * The outgoing buffer is readily referred from the dma buffer,
  3105. * just need to get header part from mailboxq structure.
  3106. */
  3107. pmb = (uint8_t *)&pmboxq->u.mb;
  3108. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  3109. /* Copy the byte swapped response mailbox back to the user */
  3110. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  3111. /* if there is any non-embedded extended data copy that too */
  3112. dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
  3113. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3114. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3115. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3116. pmbx = (uint8_t *)dmabuf->virt;
  3117. /* byte swap the extended data following the mailbox command */
  3118. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3119. &pmbx[sizeof(MAILBOX_t)],
  3120. sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
  3121. }
  3122. /* Complete the job if the job is still active */
  3123. if (job) {
  3124. size = job->reply_payload.payload_len;
  3125. job->reply->reply_payload_rcv_len =
  3126. sg_copy_from_buffer(job->reply_payload.sg_list,
  3127. job->reply_payload.sg_cnt,
  3128. pmb_buf, size);
  3129. /* result for successful */
  3130. job->reply->result = 0;
  3131. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3132. "2937 SLI_CONFIG ext-buffer maibox command "
  3133. "(x%x/x%x) complete bsg job done, bsize:%d\n",
  3134. phba->mbox_ext_buf_ctx.nembType,
  3135. phba->mbox_ext_buf_ctx.mboxType, size);
  3136. lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
  3137. phba->mbox_ext_buf_ctx.nembType,
  3138. phba->mbox_ext_buf_ctx.mboxType,
  3139. dma_ebuf, sta_pos_addr,
  3140. phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
  3141. } else {
  3142. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3143. "2938 SLI_CONFIG ext-buffer maibox "
  3144. "command (x%x/x%x) failure, rc:x%x\n",
  3145. phba->mbox_ext_buf_ctx.nembType,
  3146. phba->mbox_ext_buf_ctx.mboxType, rc);
  3147. }
  3148. /* state change */
  3149. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
  3150. kfree(dd_data);
  3151. return job;
  3152. }
  3153. /**
  3154. * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
  3155. * @phba: Pointer to HBA context object.
  3156. * @pmboxq: Pointer to mailbox command.
  3157. *
  3158. * This is completion handler function for mailbox read commands with multiple
  3159. * external buffers.
  3160. **/
  3161. static void
  3162. lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3163. {
  3164. struct fc_bsg_job *job;
  3165. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3166. /* handle the BSG job with mailbox command */
  3167. if (!job)
  3168. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3169. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3170. "2939 SLI_CONFIG ext-buffer rd maibox command "
  3171. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3172. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3173. if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
  3174. lpfc_bsg_mbox_ext_session_reset(phba);
  3175. /* free base driver mailbox structure memory */
  3176. mempool_free(pmboxq, phba->mbox_mem_pool);
  3177. /* if the job is still active, call job done */
  3178. if (job)
  3179. job->job_done(job);
  3180. return;
  3181. }
  3182. /**
  3183. * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
  3184. * @phba: Pointer to HBA context object.
  3185. * @pmboxq: Pointer to mailbox command.
  3186. *
  3187. * This is completion handler function for mailbox write commands with multiple
  3188. * external buffers.
  3189. **/
  3190. static void
  3191. lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3192. {
  3193. struct fc_bsg_job *job;
  3194. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3195. /* handle the BSG job with the mailbox command */
  3196. if (!job)
  3197. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3198. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3199. "2940 SLI_CONFIG ext-buffer wr maibox command "
  3200. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3201. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3202. /* free all memory, including dma buffers */
  3203. mempool_free(pmboxq, phba->mbox_mem_pool);
  3204. lpfc_bsg_mbox_ext_session_reset(phba);
  3205. /* if the job is still active, call job done */
  3206. if (job)
  3207. job->job_done(job);
  3208. return;
  3209. }
  3210. static void
  3211. lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
  3212. uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
  3213. struct lpfc_dmabuf *ext_dmabuf)
  3214. {
  3215. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3216. /* pointer to the start of mailbox command */
  3217. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
  3218. if (nemb_tp == nemb_mse) {
  3219. if (index == 0) {
  3220. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3221. mse[index].pa_hi =
  3222. putPaddrHigh(mbx_dmabuf->phys +
  3223. sizeof(MAILBOX_t));
  3224. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3225. mse[index].pa_lo =
  3226. putPaddrLow(mbx_dmabuf->phys +
  3227. sizeof(MAILBOX_t));
  3228. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3229. "2943 SLI_CONFIG(mse)[%d], "
  3230. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3231. index,
  3232. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3233. mse[index].buf_len,
  3234. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3235. mse[index].pa_hi,
  3236. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3237. mse[index].pa_lo);
  3238. } else {
  3239. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3240. mse[index].pa_hi =
  3241. putPaddrHigh(ext_dmabuf->phys);
  3242. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3243. mse[index].pa_lo =
  3244. putPaddrLow(ext_dmabuf->phys);
  3245. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3246. "2944 SLI_CONFIG(mse)[%d], "
  3247. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3248. index,
  3249. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3250. mse[index].buf_len,
  3251. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3252. mse[index].pa_hi,
  3253. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3254. mse[index].pa_lo);
  3255. }
  3256. } else {
  3257. if (index == 0) {
  3258. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3259. hbd[index].pa_hi =
  3260. putPaddrHigh(mbx_dmabuf->phys +
  3261. sizeof(MAILBOX_t));
  3262. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3263. hbd[index].pa_lo =
  3264. putPaddrLow(mbx_dmabuf->phys +
  3265. sizeof(MAILBOX_t));
  3266. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3267. "3007 SLI_CONFIG(hbd)[%d], "
  3268. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3269. index,
  3270. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3271. &sli_cfg_mbx->un.
  3272. sli_config_emb1_subsys.hbd[index]),
  3273. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3274. hbd[index].pa_hi,
  3275. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3276. hbd[index].pa_lo);
  3277. } else {
  3278. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3279. hbd[index].pa_hi =
  3280. putPaddrHigh(ext_dmabuf->phys);
  3281. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3282. hbd[index].pa_lo =
  3283. putPaddrLow(ext_dmabuf->phys);
  3284. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3285. "3008 SLI_CONFIG(hbd)[%d], "
  3286. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3287. index,
  3288. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3289. &sli_cfg_mbx->un.
  3290. sli_config_emb1_subsys.hbd[index]),
  3291. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3292. hbd[index].pa_hi,
  3293. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3294. hbd[index].pa_lo);
  3295. }
  3296. }
  3297. return;
  3298. }
  3299. /**
  3300. * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
  3301. * @phba: Pointer to HBA context object.
  3302. * @mb: Pointer to a BSG mailbox object.
  3303. * @nemb_tp: Enumerate of non-embedded mailbox command type.
  3304. * @dmabuff: Pointer to a DMA buffer descriptor.
  3305. *
  3306. * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
  3307. * non-embedded external bufffers.
  3308. **/
  3309. static int
  3310. lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3311. enum nemb_type nemb_tp,
  3312. struct lpfc_dmabuf *dmabuf)
  3313. {
  3314. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3315. struct dfc_mbox_req *mbox_req;
  3316. struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
  3317. uint32_t ext_buf_cnt, ext_buf_index;
  3318. struct lpfc_dmabuf *ext_dmabuf = NULL;
  3319. struct bsg_job_data *dd_data = NULL;
  3320. LPFC_MBOXQ_t *pmboxq = NULL;
  3321. MAILBOX_t *pmb;
  3322. uint8_t *pmbx;
  3323. int rc, i;
  3324. mbox_req =
  3325. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3326. /* pointer to the start of mailbox command */
  3327. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3328. if (nemb_tp == nemb_mse) {
  3329. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3330. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3331. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3332. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3333. "2945 Handled SLI_CONFIG(mse) rd, "
  3334. "ext_buf_cnt(%d) out of range(%d)\n",
  3335. ext_buf_cnt,
  3336. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3337. rc = -ERANGE;
  3338. goto job_error;
  3339. }
  3340. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3341. "2941 Handled SLI_CONFIG(mse) rd, "
  3342. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3343. } else {
  3344. /* sanity check on interface type for support */
  3345. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3346. LPFC_SLI_INTF_IF_TYPE_2) {
  3347. rc = -ENODEV;
  3348. goto job_error;
  3349. }
  3350. /* nemb_tp == nemb_hbd */
  3351. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3352. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3353. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3354. "2946 Handled SLI_CONFIG(hbd) rd, "
  3355. "ext_buf_cnt(%d) out of range(%d)\n",
  3356. ext_buf_cnt,
  3357. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3358. rc = -ERANGE;
  3359. goto job_error;
  3360. }
  3361. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3362. "2942 Handled SLI_CONFIG(hbd) rd, "
  3363. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3364. }
  3365. /* before dma descriptor setup */
  3366. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3367. sta_pre_addr, dmabuf, ext_buf_cnt);
  3368. /* reject non-embedded mailbox command with none external buffer */
  3369. if (ext_buf_cnt == 0) {
  3370. rc = -EPERM;
  3371. goto job_error;
  3372. } else if (ext_buf_cnt > 1) {
  3373. /* additional external read buffers */
  3374. for (i = 1; i < ext_buf_cnt; i++) {
  3375. ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
  3376. if (!ext_dmabuf) {
  3377. rc = -ENOMEM;
  3378. goto job_error;
  3379. }
  3380. list_add_tail(&ext_dmabuf->list,
  3381. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3382. }
  3383. }
  3384. /* bsg tracking structure */
  3385. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3386. if (!dd_data) {
  3387. rc = -ENOMEM;
  3388. goto job_error;
  3389. }
  3390. /* mailbox command structure for base driver */
  3391. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3392. if (!pmboxq) {
  3393. rc = -ENOMEM;
  3394. goto job_error;
  3395. }
  3396. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3397. /* for the first external buffer */
  3398. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3399. /* for the rest of external buffer descriptors if any */
  3400. if (ext_buf_cnt > 1) {
  3401. ext_buf_index = 1;
  3402. list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
  3403. &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
  3404. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
  3405. ext_buf_index, dmabuf,
  3406. curr_dmabuf);
  3407. ext_buf_index++;
  3408. }
  3409. }
  3410. /* after dma descriptor setup */
  3411. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3412. sta_pos_addr, dmabuf, ext_buf_cnt);
  3413. /* construct base driver mbox command */
  3414. pmb = &pmboxq->u.mb;
  3415. pmbx = (uint8_t *)dmabuf->virt;
  3416. memcpy(pmb, pmbx, sizeof(*pmb));
  3417. pmb->mbxOwner = OWN_HOST;
  3418. pmboxq->vport = phba->pport;
  3419. /* multi-buffer handling context */
  3420. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3421. phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
  3422. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3423. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3424. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3425. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3426. /* callback for multi-buffer read mailbox command */
  3427. pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
  3428. /* context fields to callback function */
  3429. pmboxq->context1 = dd_data;
  3430. dd_data->type = TYPE_MBOX;
  3431. dd_data->set_job = job;
  3432. dd_data->context_un.mbox.pmboxq = pmboxq;
  3433. dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
  3434. job->dd_data = dd_data;
  3435. /* state change */
  3436. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3437. /*
  3438. * Non-embedded mailbox subcommand data gets byte swapped here because
  3439. * the lower level driver code only does the first 64 mailbox words.
  3440. */
  3441. if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
  3442. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
  3443. (nemb_tp == nemb_mse))
  3444. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3445. &pmbx[sizeof(MAILBOX_t)],
  3446. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3447. mse[0].buf_len);
  3448. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3449. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3450. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3451. "2947 Issued SLI_CONFIG ext-buffer "
  3452. "maibox command, rc:x%x\n", rc);
  3453. return SLI_CONFIG_HANDLED;
  3454. }
  3455. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3456. "2948 Failed to issue SLI_CONFIG ext-buffer "
  3457. "maibox command, rc:x%x\n", rc);
  3458. rc = -EPIPE;
  3459. job_error:
  3460. if (pmboxq)
  3461. mempool_free(pmboxq, phba->mbox_mem_pool);
  3462. lpfc_bsg_dma_page_list_free(phba,
  3463. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3464. kfree(dd_data);
  3465. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
  3466. return rc;
  3467. }
  3468. /**
  3469. * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
  3470. * @phba: Pointer to HBA context object.
  3471. * @mb: Pointer to a BSG mailbox object.
  3472. * @dmabuff: Pointer to a DMA buffer descriptor.
  3473. *
  3474. * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
  3475. * non-embedded external bufffers.
  3476. **/
  3477. static int
  3478. lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3479. enum nemb_type nemb_tp,
  3480. struct lpfc_dmabuf *dmabuf)
  3481. {
  3482. struct dfc_mbox_req *mbox_req;
  3483. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3484. uint32_t ext_buf_cnt;
  3485. struct bsg_job_data *dd_data = NULL;
  3486. LPFC_MBOXQ_t *pmboxq = NULL;
  3487. MAILBOX_t *pmb;
  3488. uint8_t *mbx;
  3489. int rc = SLI_CONFIG_NOT_HANDLED, i;
  3490. mbox_req =
  3491. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3492. /* pointer to the start of mailbox command */
  3493. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3494. if (nemb_tp == nemb_mse) {
  3495. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3496. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3497. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3498. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3499. "2953 Failed SLI_CONFIG(mse) wr, "
  3500. "ext_buf_cnt(%d) out of range(%d)\n",
  3501. ext_buf_cnt,
  3502. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3503. return -ERANGE;
  3504. }
  3505. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3506. "2949 Handled SLI_CONFIG(mse) wr, "
  3507. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3508. } else {
  3509. /* sanity check on interface type for support */
  3510. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3511. LPFC_SLI_INTF_IF_TYPE_2)
  3512. return -ENODEV;
  3513. /* nemb_tp == nemb_hbd */
  3514. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3515. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3516. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3517. "2954 Failed SLI_CONFIG(hbd) wr, "
  3518. "ext_buf_cnt(%d) out of range(%d)\n",
  3519. ext_buf_cnt,
  3520. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3521. return -ERANGE;
  3522. }
  3523. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3524. "2950 Handled SLI_CONFIG(hbd) wr, "
  3525. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3526. }
  3527. /* before dma buffer descriptor setup */
  3528. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3529. sta_pre_addr, dmabuf, ext_buf_cnt);
  3530. if (ext_buf_cnt == 0)
  3531. return -EPERM;
  3532. /* for the first external buffer */
  3533. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3534. /* after dma descriptor setup */
  3535. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3536. sta_pos_addr, dmabuf, ext_buf_cnt);
  3537. /* log for looking forward */
  3538. for (i = 1; i < ext_buf_cnt; i++) {
  3539. if (nemb_tp == nemb_mse)
  3540. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3541. "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
  3542. i, sli_cfg_mbx->un.sli_config_emb0_subsys.
  3543. mse[i].buf_len);
  3544. else
  3545. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3546. "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
  3547. i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3548. &sli_cfg_mbx->un.sli_config_emb1_subsys.
  3549. hbd[i]));
  3550. }
  3551. /* multi-buffer handling context */
  3552. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3553. phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
  3554. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3555. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3556. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3557. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3558. if (ext_buf_cnt == 1) {
  3559. /* bsg tracking structure */
  3560. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3561. if (!dd_data) {
  3562. rc = -ENOMEM;
  3563. goto job_error;
  3564. }
  3565. /* mailbox command structure for base driver */
  3566. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3567. if (!pmboxq) {
  3568. rc = -ENOMEM;
  3569. goto job_error;
  3570. }
  3571. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3572. pmb = &pmboxq->u.mb;
  3573. mbx = (uint8_t *)dmabuf->virt;
  3574. memcpy(pmb, mbx, sizeof(*pmb));
  3575. pmb->mbxOwner = OWN_HOST;
  3576. pmboxq->vport = phba->pport;
  3577. /* callback for multi-buffer read mailbox command */
  3578. pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
  3579. /* context fields to callback function */
  3580. pmboxq->context1 = dd_data;
  3581. dd_data->type = TYPE_MBOX;
  3582. dd_data->set_job = job;
  3583. dd_data->context_un.mbox.pmboxq = pmboxq;
  3584. dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
  3585. job->dd_data = dd_data;
  3586. /* state change */
  3587. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3588. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3589. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3590. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3591. "2955 Issued SLI_CONFIG ext-buffer "
  3592. "maibox command, rc:x%x\n", rc);
  3593. return SLI_CONFIG_HANDLED;
  3594. }
  3595. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3596. "2956 Failed to issue SLI_CONFIG ext-buffer "
  3597. "maibox command, rc:x%x\n", rc);
  3598. rc = -EPIPE;
  3599. goto job_error;
  3600. }
  3601. /* wait for additoinal external buffers */
  3602. job->reply->result = 0;
  3603. job->job_done(job);
  3604. return SLI_CONFIG_HANDLED;
  3605. job_error:
  3606. if (pmboxq)
  3607. mempool_free(pmboxq, phba->mbox_mem_pool);
  3608. kfree(dd_data);
  3609. return rc;
  3610. }
  3611. /**
  3612. * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
  3613. * @phba: Pointer to HBA context object.
  3614. * @mb: Pointer to a BSG mailbox object.
  3615. * @dmabuff: Pointer to a DMA buffer descriptor.
  3616. *
  3617. * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
  3618. * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
  3619. * with embedded sussystem 0x1 and opcodes with external HBDs.
  3620. **/
  3621. static int
  3622. lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3623. struct lpfc_dmabuf *dmabuf)
  3624. {
  3625. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3626. uint32_t subsys;
  3627. uint32_t opcode;
  3628. int rc = SLI_CONFIG_NOT_HANDLED;
  3629. /* state change on new multi-buffer pass-through mailbox command */
  3630. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
  3631. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3632. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3633. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3634. subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
  3635. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3636. opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
  3637. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3638. if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
  3639. switch (opcode) {
  3640. case FCOE_OPCODE_READ_FCF:
  3641. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3642. "2957 Handled SLI_CONFIG "
  3643. "subsys_fcoe, opcode:x%x\n",
  3644. opcode);
  3645. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3646. nemb_mse, dmabuf);
  3647. break;
  3648. case FCOE_OPCODE_ADD_FCF:
  3649. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3650. "2958 Handled SLI_CONFIG "
  3651. "subsys_fcoe, opcode:x%x\n",
  3652. opcode);
  3653. rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
  3654. nemb_mse, dmabuf);
  3655. break;
  3656. default:
  3657. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3658. "2959 Reject SLI_CONFIG "
  3659. "subsys_fcoe, opcode:x%x\n",
  3660. opcode);
  3661. rc = -EPERM;
  3662. break;
  3663. }
  3664. } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
  3665. switch (opcode) {
  3666. case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
  3667. case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
  3668. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3669. "3106 Handled SLI_CONFIG "
  3670. "subsys_comn, opcode:x%x\n",
  3671. opcode);
  3672. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3673. nemb_mse, dmabuf);
  3674. break;
  3675. default:
  3676. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3677. "3107 Reject SLI_CONFIG "
  3678. "subsys_comn, opcode:x%x\n",
  3679. opcode);
  3680. rc = -EPERM;
  3681. break;
  3682. }
  3683. } else {
  3684. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3685. "2977 Reject SLI_CONFIG "
  3686. "subsys:x%d, opcode:x%x\n",
  3687. subsys, opcode);
  3688. rc = -EPERM;
  3689. }
  3690. } else {
  3691. subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
  3692. &sli_cfg_mbx->un.sli_config_emb1_subsys);
  3693. opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
  3694. &sli_cfg_mbx->un.sli_config_emb1_subsys);
  3695. if (subsys == SLI_CONFIG_SUBSYS_COMN) {
  3696. switch (opcode) {
  3697. case COMN_OPCODE_READ_OBJECT:
  3698. case COMN_OPCODE_READ_OBJECT_LIST:
  3699. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3700. "2960 Handled SLI_CONFIG "
  3701. "subsys_comn, opcode:x%x\n",
  3702. opcode);
  3703. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3704. nemb_hbd, dmabuf);
  3705. break;
  3706. case COMN_OPCODE_WRITE_OBJECT:
  3707. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3708. "2961 Handled SLI_CONFIG "
  3709. "subsys_comn, opcode:x%x\n",
  3710. opcode);
  3711. rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
  3712. nemb_hbd, dmabuf);
  3713. break;
  3714. default:
  3715. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3716. "2962 Not handled SLI_CONFIG "
  3717. "subsys_comn, opcode:x%x\n",
  3718. opcode);
  3719. rc = SLI_CONFIG_NOT_HANDLED;
  3720. break;
  3721. }
  3722. } else {
  3723. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3724. "2978 Not handled SLI_CONFIG "
  3725. "subsys:x%d, opcode:x%x\n",
  3726. subsys, opcode);
  3727. rc = SLI_CONFIG_NOT_HANDLED;
  3728. }
  3729. }
  3730. /* state reset on not handled new multi-buffer mailbox command */
  3731. if (rc != SLI_CONFIG_HANDLED)
  3732. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
  3733. return rc;
  3734. }
  3735. /**
  3736. * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
  3737. * @phba: Pointer to HBA context object.
  3738. *
  3739. * This routine is for requesting to abort a pass-through mailbox command with
  3740. * multiple external buffers due to error condition.
  3741. **/
  3742. static void
  3743. lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
  3744. {
  3745. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
  3746. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
  3747. else
  3748. lpfc_bsg_mbox_ext_session_reset(phba);
  3749. return;
  3750. }
  3751. /**
  3752. * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
  3753. * @phba: Pointer to HBA context object.
  3754. * @dmabuf: Pointer to a DMA buffer descriptor.
  3755. *
  3756. * This routine extracts the next mailbox read external buffer back to
  3757. * user space through BSG.
  3758. **/
  3759. static int
  3760. lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
  3761. {
  3762. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3763. struct lpfc_dmabuf *dmabuf;
  3764. uint8_t *pbuf;
  3765. uint32_t size;
  3766. uint32_t index;
  3767. index = phba->mbox_ext_buf_ctx.seqNum;
  3768. phba->mbox_ext_buf_ctx.seqNum++;
  3769. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
  3770. phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3771. if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
  3772. size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
  3773. &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
  3774. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3775. "2963 SLI_CONFIG (mse) ext-buffer rd get "
  3776. "buffer[%d], size:%d\n", index, size);
  3777. } else {
  3778. size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3779. &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
  3780. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3781. "2964 SLI_CONFIG (hbd) ext-buffer rd get "
  3782. "buffer[%d], size:%d\n", index, size);
  3783. }
  3784. if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
  3785. return -EPIPE;
  3786. dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
  3787. struct lpfc_dmabuf, list);
  3788. list_del_init(&dmabuf->list);
  3789. /* after dma buffer descriptor setup */
  3790. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
  3791. mbox_rd, dma_ebuf, sta_pos_addr,
  3792. dmabuf, index);
  3793. pbuf = (uint8_t *)dmabuf->virt;
  3794. job->reply->reply_payload_rcv_len =
  3795. sg_copy_from_buffer(job->reply_payload.sg_list,
  3796. job->reply_payload.sg_cnt,
  3797. pbuf, size);
  3798. lpfc_bsg_dma_page_free(phba, dmabuf);
  3799. if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
  3800. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3801. "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
  3802. "command session done\n");
  3803. lpfc_bsg_mbox_ext_session_reset(phba);
  3804. }
  3805. job->reply->result = 0;
  3806. job->job_done(job);
  3807. return SLI_CONFIG_HANDLED;
  3808. }
  3809. /**
  3810. * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
  3811. * @phba: Pointer to HBA context object.
  3812. * @dmabuf: Pointer to a DMA buffer descriptor.
  3813. *
  3814. * This routine sets up the next mailbox read external buffer obtained
  3815. * from user space through BSG.
  3816. **/
  3817. static int
  3818. lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3819. struct lpfc_dmabuf *dmabuf)
  3820. {
  3821. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3822. struct bsg_job_data *dd_data = NULL;
  3823. LPFC_MBOXQ_t *pmboxq = NULL;
  3824. MAILBOX_t *pmb;
  3825. enum nemb_type nemb_tp;
  3826. uint8_t *pbuf;
  3827. uint32_t size;
  3828. uint32_t index;
  3829. int rc;
  3830. index = phba->mbox_ext_buf_ctx.seqNum;
  3831. phba->mbox_ext_buf_ctx.seqNum++;
  3832. nemb_tp = phba->mbox_ext_buf_ctx.nembType;
  3833. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
  3834. phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3835. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3836. if (!dd_data) {
  3837. rc = -ENOMEM;
  3838. goto job_error;
  3839. }
  3840. pbuf = (uint8_t *)dmabuf->virt;
  3841. size = job->request_payload.payload_len;
  3842. sg_copy_to_buffer(job->request_payload.sg_list,
  3843. job->request_payload.sg_cnt,
  3844. pbuf, size);
  3845. if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
  3846. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3847. "2966 SLI_CONFIG (mse) ext-buffer wr set "
  3848. "buffer[%d], size:%d\n",
  3849. phba->mbox_ext_buf_ctx.seqNum, size);
  3850. } else {
  3851. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3852. "2967 SLI_CONFIG (hbd) ext-buffer wr set "
  3853. "buffer[%d], size:%d\n",
  3854. phba->mbox_ext_buf_ctx.seqNum, size);
  3855. }
  3856. /* set up external buffer descriptor and add to external buffer list */
  3857. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
  3858. phba->mbox_ext_buf_ctx.mbx_dmabuf,
  3859. dmabuf);
  3860. list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3861. /* after write dma buffer */
  3862. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
  3863. mbox_wr, dma_ebuf, sta_pos_addr,
  3864. dmabuf, index);
  3865. if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
  3866. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3867. "2968 SLI_CONFIG ext-buffer wr all %d "
  3868. "ebuffers received\n",
  3869. phba->mbox_ext_buf_ctx.numBuf);
  3870. /* mailbox command structure for base driver */
  3871. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3872. if (!pmboxq) {
  3873. rc = -ENOMEM;
  3874. goto job_error;
  3875. }
  3876. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3877. pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3878. pmb = &pmboxq->u.mb;
  3879. memcpy(pmb, pbuf, sizeof(*pmb));
  3880. pmb->mbxOwner = OWN_HOST;
  3881. pmboxq->vport = phba->pport;
  3882. /* callback for multi-buffer write mailbox command */
  3883. pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
  3884. /* context fields to callback function */
  3885. pmboxq->context1 = dd_data;
  3886. dd_data->type = TYPE_MBOX;
  3887. dd_data->set_job = job;
  3888. dd_data->context_un.mbox.pmboxq = pmboxq;
  3889. dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
  3890. job->dd_data = dd_data;
  3891. /* state change */
  3892. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3893. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3894. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3895. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3896. "2969 Issued SLI_CONFIG ext-buffer "
  3897. "maibox command, rc:x%x\n", rc);
  3898. return SLI_CONFIG_HANDLED;
  3899. }
  3900. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3901. "2970 Failed to issue SLI_CONFIG ext-buffer "
  3902. "maibox command, rc:x%x\n", rc);
  3903. rc = -EPIPE;
  3904. goto job_error;
  3905. }
  3906. /* wait for additoinal external buffers */
  3907. job->reply->result = 0;
  3908. job->job_done(job);
  3909. return SLI_CONFIG_HANDLED;
  3910. job_error:
  3911. lpfc_bsg_dma_page_free(phba, dmabuf);
  3912. kfree(dd_data);
  3913. return rc;
  3914. }
  3915. /**
  3916. * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
  3917. * @phba: Pointer to HBA context object.
  3918. * @mb: Pointer to a BSG mailbox object.
  3919. * @dmabuff: Pointer to a DMA buffer descriptor.
  3920. *
  3921. * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
  3922. * command with multiple non-embedded external buffers.
  3923. **/
  3924. static int
  3925. lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3926. struct lpfc_dmabuf *dmabuf)
  3927. {
  3928. int rc;
  3929. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3930. "2971 SLI_CONFIG buffer (type:x%x)\n",
  3931. phba->mbox_ext_buf_ctx.mboxType);
  3932. if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
  3933. if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
  3934. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3935. "2972 SLI_CONFIG rd buffer state "
  3936. "mismatch:x%x\n",
  3937. phba->mbox_ext_buf_ctx.state);
  3938. lpfc_bsg_mbox_ext_abort(phba);
  3939. return -EPIPE;
  3940. }
  3941. rc = lpfc_bsg_read_ebuf_get(phba, job);
  3942. if (rc == SLI_CONFIG_HANDLED)
  3943. lpfc_bsg_dma_page_free(phba, dmabuf);
  3944. } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
  3945. if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
  3946. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3947. "2973 SLI_CONFIG wr buffer state "
  3948. "mismatch:x%x\n",
  3949. phba->mbox_ext_buf_ctx.state);
  3950. lpfc_bsg_mbox_ext_abort(phba);
  3951. return -EPIPE;
  3952. }
  3953. rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
  3954. }
  3955. return rc;
  3956. }
  3957. /**
  3958. * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
  3959. * @phba: Pointer to HBA context object.
  3960. * @mb: Pointer to a BSG mailbox object.
  3961. * @dmabuff: Pointer to a DMA buffer descriptor.
  3962. *
  3963. * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
  3964. * (0x9B) mailbox commands and external buffers.
  3965. **/
  3966. static int
  3967. lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3968. struct lpfc_dmabuf *dmabuf)
  3969. {
  3970. struct dfc_mbox_req *mbox_req;
  3971. int rc = SLI_CONFIG_NOT_HANDLED;
  3972. mbox_req =
  3973. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3974. /* mbox command with/without single external buffer */
  3975. if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
  3976. return rc;
  3977. /* mbox command and first external buffer */
  3978. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
  3979. if (mbox_req->extSeqNum == 1) {
  3980. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3981. "2974 SLI_CONFIG mailbox: tag:%d, "
  3982. "seq:%d\n", mbox_req->extMboxTag,
  3983. mbox_req->extSeqNum);
  3984. rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
  3985. return rc;
  3986. } else
  3987. goto sli_cfg_ext_error;
  3988. }
  3989. /*
  3990. * handle additional external buffers
  3991. */
  3992. /* check broken pipe conditions */
  3993. if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
  3994. goto sli_cfg_ext_error;
  3995. if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
  3996. goto sli_cfg_ext_error;
  3997. if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
  3998. goto sli_cfg_ext_error;
  3999. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  4000. "2975 SLI_CONFIG mailbox external buffer: "
  4001. "extSta:x%x, tag:%d, seq:%d\n",
  4002. phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
  4003. mbox_req->extSeqNum);
  4004. rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
  4005. return rc;
  4006. sli_cfg_ext_error:
  4007. /* all other cases, broken pipe */
  4008. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  4009. "2976 SLI_CONFIG mailbox broken pipe: "
  4010. "ctxSta:x%x, ctxNumBuf:%d "
  4011. "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
  4012. phba->mbox_ext_buf_ctx.state,
  4013. phba->mbox_ext_buf_ctx.numBuf,
  4014. phba->mbox_ext_buf_ctx.mbxTag,
  4015. phba->mbox_ext_buf_ctx.seqNum,
  4016. mbox_req->extMboxTag, mbox_req->extSeqNum);
  4017. lpfc_bsg_mbox_ext_session_reset(phba);
  4018. return -EPIPE;
  4019. }
  4020. /**
  4021. * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
  4022. * @phba: Pointer to HBA context object.
  4023. * @mb: Pointer to a mailbox object.
  4024. * @vport: Pointer to a vport object.
  4025. *
  4026. * Allocate a tracking object, mailbox command memory, get a mailbox
  4027. * from the mailbox pool, copy the caller mailbox command.
  4028. *
  4029. * If offline and the sli is active we need to poll for the command (port is
  4030. * being reset) and com-plete the job, otherwise issue the mailbox command and
  4031. * let our completion handler finish the command.
  4032. **/
  4033. static uint32_t
  4034. lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  4035. struct lpfc_vport *vport)
  4036. {
  4037. LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
  4038. MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
  4039. /* a 4k buffer to hold the mb and extended data from/to the bsg */
  4040. uint8_t *pmbx = NULL;
  4041. struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
  4042. struct lpfc_dmabuf *dmabuf = NULL;
  4043. struct dfc_mbox_req *mbox_req;
  4044. struct READ_EVENT_LOG_VAR *rdEventLog;
  4045. uint32_t transmit_length, receive_length, mode;
  4046. struct lpfc_mbx_sli4_config *sli4_config;
  4047. struct lpfc_mbx_nembed_cmd *nembed_sge;
  4048. struct mbox_header *header;
  4049. struct ulp_bde64 *bde;
  4050. uint8_t *ext = NULL;
  4051. int rc = 0;
  4052. uint8_t *from;
  4053. uint32_t size;
  4054. /* in case no data is transferred */
  4055. job->reply->reply_payload_rcv_len = 0;
  4056. /* sanity check to protect driver */
  4057. if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
  4058. job->request_payload.payload_len > BSG_MBOX_SIZE) {
  4059. rc = -ERANGE;
  4060. goto job_done;
  4061. }
  4062. /*
  4063. * Don't allow mailbox commands to be sent when blocked or when in
  4064. * the middle of discovery
  4065. */
  4066. if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
  4067. rc = -EAGAIN;
  4068. goto job_done;
  4069. }
  4070. mbox_req =
  4071. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  4072. /* check if requested extended data lengths are valid */
  4073. if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
  4074. (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
  4075. rc = -ERANGE;
  4076. goto job_done;
  4077. }
  4078. dmabuf = lpfc_bsg_dma_page_alloc(phba);
  4079. if (!dmabuf || !dmabuf->virt) {
  4080. rc = -ENOMEM;
  4081. goto job_done;
  4082. }
  4083. /* Get the mailbox command or external buffer from BSG */
  4084. pmbx = (uint8_t *)dmabuf->virt;
  4085. size = job->request_payload.payload_len;
  4086. sg_copy_to_buffer(job->request_payload.sg_list,
  4087. job->request_payload.sg_cnt, pmbx, size);
  4088. /* Handle possible SLI_CONFIG with non-embedded payloads */
  4089. if (phba->sli_rev == LPFC_SLI_REV4) {
  4090. rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
  4091. if (rc == SLI_CONFIG_HANDLED)
  4092. goto job_cont;
  4093. if (rc)
  4094. goto job_done;
  4095. /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
  4096. }
  4097. rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
  4098. if (rc != 0)
  4099. goto job_done; /* must be negative */
  4100. /* allocate our bsg tracking structure */
  4101. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  4102. if (!dd_data) {
  4103. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4104. "2727 Failed allocation of dd_data\n");
  4105. rc = -ENOMEM;
  4106. goto job_done;
  4107. }
  4108. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4109. if (!pmboxq) {
  4110. rc = -ENOMEM;
  4111. goto job_done;
  4112. }
  4113. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  4114. pmb = &pmboxq->u.mb;
  4115. memcpy(pmb, pmbx, sizeof(*pmb));
  4116. pmb->mbxOwner = OWN_HOST;
  4117. pmboxq->vport = vport;
  4118. /* If HBA encountered an error attention, allow only DUMP
  4119. * or RESTART mailbox commands until the HBA is restarted.
  4120. */
  4121. if (phba->pport->stopped &&
  4122. pmb->mbxCommand != MBX_DUMP_MEMORY &&
  4123. pmb->mbxCommand != MBX_RESTART &&
  4124. pmb->mbxCommand != MBX_WRITE_VPARMS &&
  4125. pmb->mbxCommand != MBX_WRITE_WWN)
  4126. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
  4127. "2797 mbox: Issued mailbox cmd "
  4128. "0x%x while in stopped state.\n",
  4129. pmb->mbxCommand);
  4130. /* extended mailbox commands will need an extended buffer */
  4131. if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
  4132. from = pmbx;
  4133. ext = from + sizeof(MAILBOX_t);
  4134. pmboxq->context2 = ext;
  4135. pmboxq->in_ext_byte_len =
  4136. mbox_req->inExtWLen * sizeof(uint32_t);
  4137. pmboxq->out_ext_byte_len =
  4138. mbox_req->outExtWLen * sizeof(uint32_t);
  4139. pmboxq->mbox_offset_word = mbox_req->mbOffset;
  4140. }
  4141. /* biu diag will need a kernel buffer to transfer the data
  4142. * allocate our own buffer and setup the mailbox command to
  4143. * use ours
  4144. */
  4145. if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
  4146. transmit_length = pmb->un.varWords[1];
  4147. receive_length = pmb->un.varWords[4];
  4148. /* transmit length cannot be greater than receive length or
  4149. * mailbox extension size
  4150. */
  4151. if ((transmit_length > receive_length) ||
  4152. (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
  4153. rc = -ERANGE;
  4154. goto job_done;
  4155. }
  4156. pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
  4157. putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
  4158. pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
  4159. putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
  4160. pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
  4161. putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
  4162. + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
  4163. pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
  4164. putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
  4165. + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
  4166. } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
  4167. rdEventLog = &pmb->un.varRdEventLog;
  4168. receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
  4169. mode = bf_get(lpfc_event_log, rdEventLog);
  4170. /* receive length cannot be greater than mailbox
  4171. * extension size
  4172. */
  4173. if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
  4174. rc = -ERANGE;
  4175. goto job_done;
  4176. }
  4177. /* mode zero uses a bde like biu diags command */
  4178. if (mode == 0) {
  4179. pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
  4180. + sizeof(MAILBOX_t));
  4181. pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
  4182. + sizeof(MAILBOX_t));
  4183. }
  4184. } else if (phba->sli_rev == LPFC_SLI_REV4) {
  4185. /* Let type 4 (well known data) through because the data is
  4186. * returned in varwords[4-8]
  4187. * otherwise check the recieve length and fetch the buffer addr
  4188. */
  4189. if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
  4190. (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
  4191. /* rebuild the command for sli4 using our own buffers
  4192. * like we do for biu diags
  4193. */
  4194. receive_length = pmb->un.varWords[2];
  4195. /* receive length cannot be greater than mailbox
  4196. * extension size
  4197. */
  4198. if (receive_length == 0) {
  4199. rc = -ERANGE;
  4200. goto job_done;
  4201. }
  4202. pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
  4203. + sizeof(MAILBOX_t));
  4204. pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
  4205. + sizeof(MAILBOX_t));
  4206. } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
  4207. pmb->un.varUpdateCfg.co) {
  4208. bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
  4209. /* bde size cannot be greater than mailbox ext size */
  4210. if (bde->tus.f.bdeSize >
  4211. BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
  4212. rc = -ERANGE;
  4213. goto job_done;
  4214. }
  4215. bde->addrHigh = putPaddrHigh(dmabuf->phys
  4216. + sizeof(MAILBOX_t));
  4217. bde->addrLow = putPaddrLow(dmabuf->phys
  4218. + sizeof(MAILBOX_t));
  4219. } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
  4220. /* Handling non-embedded SLI_CONFIG mailbox command */
  4221. sli4_config = &pmboxq->u.mqe.un.sli4_config;
  4222. if (!bf_get(lpfc_mbox_hdr_emb,
  4223. &sli4_config->header.cfg_mhdr)) {
  4224. /* rebuild the command for sli4 using our
  4225. * own buffers like we do for biu diags
  4226. */
  4227. header = (struct mbox_header *)
  4228. &pmb->un.varWords[0];
  4229. nembed_sge = (struct lpfc_mbx_nembed_cmd *)
  4230. &pmb->un.varWords[0];
  4231. receive_length = nembed_sge->sge[0].length;
  4232. /* receive length cannot be greater than
  4233. * mailbox extension size
  4234. */
  4235. if ((receive_length == 0) ||
  4236. (receive_length >
  4237. BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
  4238. rc = -ERANGE;
  4239. goto job_done;
  4240. }
  4241. nembed_sge->sge[0].pa_hi =
  4242. putPaddrHigh(dmabuf->phys
  4243. + sizeof(MAILBOX_t));
  4244. nembed_sge->sge[0].pa_lo =
  4245. putPaddrLow(dmabuf->phys
  4246. + sizeof(MAILBOX_t));
  4247. }
  4248. }
  4249. }
  4250. dd_data->context_un.mbox.dmabuffers = dmabuf;
  4251. /* setup wake call as IOCB callback */
  4252. pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
  4253. /* setup context field to pass wait_queue pointer to wake function */
  4254. pmboxq->context1 = dd_data;
  4255. dd_data->type = TYPE_MBOX;
  4256. dd_data->set_job = job;
  4257. dd_data->context_un.mbox.pmboxq = pmboxq;
  4258. dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
  4259. dd_data->context_un.mbox.ext = ext;
  4260. dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
  4261. dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
  4262. dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
  4263. job->dd_data = dd_data;
  4264. if ((vport->fc_flag & FC_OFFLINE_MODE) ||
  4265. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
  4266. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
  4267. if (rc != MBX_SUCCESS) {
  4268. rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
  4269. goto job_done;
  4270. }
  4271. /* job finished, copy the data */
  4272. memcpy(pmbx, pmb, sizeof(*pmb));
  4273. job->reply->reply_payload_rcv_len =
  4274. sg_copy_from_buffer(job->reply_payload.sg_list,
  4275. job->reply_payload.sg_cnt,
  4276. pmbx, size);
  4277. /* not waiting mbox already done */
  4278. rc = 0;
  4279. goto job_done;
  4280. }
  4281. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  4282. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
  4283. return 1; /* job started */
  4284. job_done:
  4285. /* common exit for error or job completed inline */
  4286. if (pmboxq)
  4287. mempool_free(pmboxq, phba->mbox_mem_pool);
  4288. lpfc_bsg_dma_page_free(phba, dmabuf);
  4289. kfree(dd_data);
  4290. job_cont:
  4291. return rc;
  4292. }
  4293. /**
  4294. * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
  4295. * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
  4296. **/
  4297. static int
  4298. lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
  4299. {
  4300. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4301. struct lpfc_hba *phba = vport->phba;
  4302. struct dfc_mbox_req *mbox_req;
  4303. int rc = 0;
  4304. /* mix-and-match backward compatibility */
  4305. job->reply->reply_payload_rcv_len = 0;
  4306. if (job->request_len <
  4307. sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
  4308. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  4309. "2737 Mix-and-match backward compatibility "
  4310. "between MBOX_REQ old size:%d and "
  4311. "new request size:%d\n",
  4312. (int)(job->request_len -
  4313. sizeof(struct fc_bsg_request)),
  4314. (int)sizeof(struct dfc_mbox_req));
  4315. mbox_req = (struct dfc_mbox_req *)
  4316. job->request->rqst_data.h_vendor.vendor_cmd;
  4317. mbox_req->extMboxTag = 0;
  4318. mbox_req->extSeqNum = 0;
  4319. }
  4320. rc = lpfc_bsg_issue_mbox(phba, job, vport);
  4321. if (rc == 0) {
  4322. /* job done */
  4323. job->reply->result = 0;
  4324. job->dd_data = NULL;
  4325. job->job_done(job);
  4326. } else if (rc == 1)
  4327. /* job submitted, will complete later*/
  4328. rc = 0; /* return zero, no error */
  4329. else {
  4330. /* some error occurred */
  4331. job->reply->result = rc;
  4332. job->dd_data = NULL;
  4333. }
  4334. return rc;
  4335. }
  4336. /**
  4337. * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
  4338. * @phba: Pointer to HBA context object.
  4339. * @cmdiocbq: Pointer to command iocb.
  4340. * @rspiocbq: Pointer to response iocb.
  4341. *
  4342. * This function is the completion handler for iocbs issued using
  4343. * lpfc_menlo_cmd function. This function is called by the
  4344. * ring event handler function without any lock held. This function
  4345. * can be called from both worker thread context and interrupt
  4346. * context. This function also can be called from another thread which
  4347. * cleans up the SLI layer objects.
  4348. * This function copies the contents of the response iocb to the
  4349. * response iocb memory object provided by the caller of
  4350. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  4351. * sleeps for the iocb completion.
  4352. **/
  4353. static void
  4354. lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
  4355. struct lpfc_iocbq *cmdiocbq,
  4356. struct lpfc_iocbq *rspiocbq)
  4357. {
  4358. struct bsg_job_data *dd_data;
  4359. struct fc_bsg_job *job;
  4360. IOCB_t *rsp;
  4361. struct lpfc_dmabuf *bmp, *cmp, *rmp;
  4362. struct lpfc_bsg_menlo *menlo;
  4363. unsigned long flags;
  4364. struct menlo_response *menlo_resp;
  4365. unsigned int rsp_size;
  4366. int rc = 0;
  4367. dd_data = cmdiocbq->context1;
  4368. cmp = cmdiocbq->context2;
  4369. bmp = cmdiocbq->context3;
  4370. menlo = &dd_data->context_un.menlo;
  4371. rmp = menlo->rmp;
  4372. rsp = &rspiocbq->iocb;
  4373. /* Determine if job has been aborted */
  4374. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  4375. job = dd_data->set_job;
  4376. if (job) {
  4377. /* Prevent timeout handling from trying to abort job */
  4378. job->dd_data = NULL;
  4379. }
  4380. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4381. /* Copy the job data or set the failing status for the job */
  4382. if (job) {
  4383. /* always return the xri, this would be used in the case
  4384. * of a menlo download to allow the data to be sent as a
  4385. * continuation of the exchange.
  4386. */
  4387. menlo_resp = (struct menlo_response *)
  4388. job->reply->reply_data.vendor_reply.vendor_rsp;
  4389. menlo_resp->xri = rsp->ulpContext;
  4390. if (rsp->ulpStatus) {
  4391. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  4392. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  4393. case IOERR_SEQUENCE_TIMEOUT:
  4394. rc = -ETIMEDOUT;
  4395. break;
  4396. case IOERR_INVALID_RPI:
  4397. rc = -EFAULT;
  4398. break;
  4399. default:
  4400. rc = -EACCES;
  4401. break;
  4402. }
  4403. } else {
  4404. rc = -EACCES;
  4405. }
  4406. } else {
  4407. rsp_size = rsp->un.genreq64.bdl.bdeSize;
  4408. job->reply->reply_payload_rcv_len =
  4409. lpfc_bsg_copy_data(rmp, &job->reply_payload,
  4410. rsp_size, 0);
  4411. }
  4412. }
  4413. lpfc_sli_release_iocbq(phba, cmdiocbq);
  4414. lpfc_free_bsg_buffers(phba, cmp);
  4415. lpfc_free_bsg_buffers(phba, rmp);
  4416. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  4417. kfree(bmp);
  4418. kfree(dd_data);
  4419. /* Complete the job if active */
  4420. if (job) {
  4421. job->reply->result = rc;
  4422. job->job_done(job);
  4423. }
  4424. return;
  4425. }
  4426. /**
  4427. * lpfc_menlo_cmd - send an ioctl for menlo hardware
  4428. * @job: fc_bsg_job to handle
  4429. *
  4430. * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
  4431. * all the command completions will return the xri for the command.
  4432. * For menlo data requests a gen request 64 CX is used to continue the exchange
  4433. * supplied in the menlo request header xri field.
  4434. **/
  4435. static int
  4436. lpfc_menlo_cmd(struct fc_bsg_job *job)
  4437. {
  4438. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4439. struct lpfc_hba *phba = vport->phba;
  4440. struct lpfc_iocbq *cmdiocbq;
  4441. IOCB_t *cmd;
  4442. int rc = 0;
  4443. struct menlo_command *menlo_cmd;
  4444. struct menlo_response *menlo_resp;
  4445. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
  4446. int request_nseg;
  4447. int reply_nseg;
  4448. struct bsg_job_data *dd_data;
  4449. struct ulp_bde64 *bpl = NULL;
  4450. /* in case no data is returned return just the return code */
  4451. job->reply->reply_payload_rcv_len = 0;
  4452. if (job->request_len <
  4453. sizeof(struct fc_bsg_request) +
  4454. sizeof(struct menlo_command)) {
  4455. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4456. "2784 Received MENLO_CMD request below "
  4457. "minimum size\n");
  4458. rc = -ERANGE;
  4459. goto no_dd_data;
  4460. }
  4461. if (job->reply_len <
  4462. sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
  4463. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4464. "2785 Received MENLO_CMD reply below "
  4465. "minimum size\n");
  4466. rc = -ERANGE;
  4467. goto no_dd_data;
  4468. }
  4469. if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
  4470. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4471. "2786 Adapter does not support menlo "
  4472. "commands\n");
  4473. rc = -EPERM;
  4474. goto no_dd_data;
  4475. }
  4476. menlo_cmd = (struct menlo_command *)
  4477. job->request->rqst_data.h_vendor.vendor_cmd;
  4478. menlo_resp = (struct menlo_response *)
  4479. job->reply->reply_data.vendor_reply.vendor_rsp;
  4480. /* allocate our bsg tracking structure */
  4481. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  4482. if (!dd_data) {
  4483. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4484. "2787 Failed allocation of dd_data\n");
  4485. rc = -ENOMEM;
  4486. goto no_dd_data;
  4487. }
  4488. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4489. if (!bmp) {
  4490. rc = -ENOMEM;
  4491. goto free_dd;
  4492. }
  4493. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  4494. if (!bmp->virt) {
  4495. rc = -ENOMEM;
  4496. goto free_bmp;
  4497. }
  4498. INIT_LIST_HEAD(&bmp->list);
  4499. bpl = (struct ulp_bde64 *)bmp->virt;
  4500. request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
  4501. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  4502. 1, bpl, &request_nseg);
  4503. if (!cmp) {
  4504. rc = -ENOMEM;
  4505. goto free_bmp;
  4506. }
  4507. lpfc_bsg_copy_data(cmp, &job->request_payload,
  4508. job->request_payload.payload_len, 1);
  4509. bpl += request_nseg;
  4510. reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
  4511. rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
  4512. bpl, &reply_nseg);
  4513. if (!rmp) {
  4514. rc = -ENOMEM;
  4515. goto free_cmp;
  4516. }
  4517. cmdiocbq = lpfc_sli_get_iocbq(phba);
  4518. if (!cmdiocbq) {
  4519. rc = -ENOMEM;
  4520. goto free_rmp;
  4521. }
  4522. cmd = &cmdiocbq->iocb;
  4523. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  4524. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  4525. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  4526. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  4527. cmd->un.genreq64.bdl.bdeSize =
  4528. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  4529. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  4530. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  4531. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
  4532. cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
  4533. cmd->ulpBdeCount = 1;
  4534. cmd->ulpClass = CLASS3;
  4535. cmd->ulpOwner = OWN_CHIP;
  4536. cmd->ulpLe = 1; /* Limited Edition */
  4537. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  4538. cmdiocbq->vport = phba->pport;
  4539. /* We want the firmware to timeout before we do */
  4540. cmd->ulpTimeout = MENLO_TIMEOUT - 5;
  4541. cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
  4542. cmdiocbq->context1 = dd_data;
  4543. cmdiocbq->context2 = cmp;
  4544. cmdiocbq->context3 = bmp;
  4545. if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
  4546. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  4547. cmd->ulpPU = MENLO_PU; /* 3 */
  4548. cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
  4549. cmd->ulpContext = MENLO_CONTEXT; /* 0 */
  4550. } else {
  4551. cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
  4552. cmd->ulpPU = 1;
  4553. cmd->un.ulpWord[4] = 0;
  4554. cmd->ulpContext = menlo_cmd->xri;
  4555. }
  4556. dd_data->type = TYPE_MENLO;
  4557. dd_data->set_job = job;
  4558. dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
  4559. dd_data->context_un.menlo.rmp = rmp;
  4560. job->dd_data = dd_data;
  4561. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
  4562. MENLO_TIMEOUT - 5);
  4563. if (rc == IOCB_SUCCESS)
  4564. return 0; /* done for now */
  4565. lpfc_sli_release_iocbq(phba, cmdiocbq);
  4566. free_rmp:
  4567. lpfc_free_bsg_buffers(phba, rmp);
  4568. free_cmp:
  4569. lpfc_free_bsg_buffers(phba, cmp);
  4570. free_bmp:
  4571. if (bmp->virt)
  4572. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  4573. kfree(bmp);
  4574. free_dd:
  4575. kfree(dd_data);
  4576. no_dd_data:
  4577. /* make error code available to userspace */
  4578. job->reply->result = rc;
  4579. job->dd_data = NULL;
  4580. return rc;
  4581. }
  4582. /**
  4583. * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
  4584. * @job: fc_bsg_job to handle
  4585. **/
  4586. static int
  4587. lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
  4588. {
  4589. int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
  4590. int rc;
  4591. switch (command) {
  4592. case LPFC_BSG_VENDOR_SET_CT_EVENT:
  4593. rc = lpfc_bsg_hba_set_event(job);
  4594. break;
  4595. case LPFC_BSG_VENDOR_GET_CT_EVENT:
  4596. rc = lpfc_bsg_hba_get_event(job);
  4597. break;
  4598. case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
  4599. rc = lpfc_bsg_send_mgmt_rsp(job);
  4600. break;
  4601. case LPFC_BSG_VENDOR_DIAG_MODE:
  4602. rc = lpfc_bsg_diag_loopback_mode(job);
  4603. break;
  4604. case LPFC_BSG_VENDOR_DIAG_MODE_END:
  4605. rc = lpfc_sli4_bsg_diag_mode_end(job);
  4606. break;
  4607. case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
  4608. rc = lpfc_bsg_diag_loopback_run(job);
  4609. break;
  4610. case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
  4611. rc = lpfc_sli4_bsg_link_diag_test(job);
  4612. break;
  4613. case LPFC_BSG_VENDOR_GET_MGMT_REV:
  4614. rc = lpfc_bsg_get_dfc_rev(job);
  4615. break;
  4616. case LPFC_BSG_VENDOR_MBOX:
  4617. rc = lpfc_bsg_mbox_cmd(job);
  4618. break;
  4619. case LPFC_BSG_VENDOR_MENLO_CMD:
  4620. case LPFC_BSG_VENDOR_MENLO_DATA:
  4621. rc = lpfc_menlo_cmd(job);
  4622. break;
  4623. default:
  4624. rc = -EINVAL;
  4625. job->reply->reply_payload_rcv_len = 0;
  4626. /* make error code available to userspace */
  4627. job->reply->result = rc;
  4628. break;
  4629. }
  4630. return rc;
  4631. }
  4632. /**
  4633. * lpfc_bsg_request - handle a bsg request from the FC transport
  4634. * @job: fc_bsg_job to handle
  4635. **/
  4636. int
  4637. lpfc_bsg_request(struct fc_bsg_job *job)
  4638. {
  4639. uint32_t msgcode;
  4640. int rc;
  4641. msgcode = job->request->msgcode;
  4642. switch (msgcode) {
  4643. case FC_BSG_HST_VENDOR:
  4644. rc = lpfc_bsg_hst_vendor(job);
  4645. break;
  4646. case FC_BSG_RPT_ELS:
  4647. rc = lpfc_bsg_rport_els(job);
  4648. break;
  4649. case FC_BSG_RPT_CT:
  4650. rc = lpfc_bsg_send_mgmt_cmd(job);
  4651. break;
  4652. default:
  4653. rc = -EINVAL;
  4654. job->reply->reply_payload_rcv_len = 0;
  4655. /* make error code available to userspace */
  4656. job->reply->result = rc;
  4657. break;
  4658. }
  4659. return rc;
  4660. }
  4661. /**
  4662. * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
  4663. * @job: fc_bsg_job that has timed out
  4664. *
  4665. * This function just aborts the job's IOCB. The aborted IOCB will return to
  4666. * the waiting function which will handle passing the error back to userspace
  4667. **/
  4668. int
  4669. lpfc_bsg_timeout(struct fc_bsg_job *job)
  4670. {
  4671. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4672. struct lpfc_hba *phba = vport->phba;
  4673. struct lpfc_iocbq *cmdiocb;
  4674. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  4675. struct bsg_job_data *dd_data;
  4676. unsigned long flags;
  4677. int rc = 0;
  4678. LIST_HEAD(completions);
  4679. struct lpfc_iocbq *check_iocb, *next_iocb;
  4680. /* if job's driver data is NULL, the command completed or is in the
  4681. * the process of completing. In this case, return status to request
  4682. * so the timeout is retried. This avoids double completion issues
  4683. * and the request will be pulled off the timer queue when the
  4684. * command's completion handler executes. Otherwise, prevent the
  4685. * command's completion handler from executing the job done callback
  4686. * and continue processing to abort the outstanding the command.
  4687. */
  4688. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  4689. dd_data = (struct bsg_job_data *)job->dd_data;
  4690. if (dd_data) {
  4691. dd_data->set_job = NULL;
  4692. job->dd_data = NULL;
  4693. } else {
  4694. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4695. return -EAGAIN;
  4696. }
  4697. switch (dd_data->type) {
  4698. case TYPE_IOCB:
  4699. /* Check to see if IOCB was issued to the port or not. If not,
  4700. * remove it from the txq queue and call cancel iocbs.
  4701. * Otherwise, call abort iotag
  4702. */
  4703. cmdiocb = dd_data->context_un.iocb.cmdiocbq;
  4704. spin_lock_irq(&phba->hbalock);
  4705. list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
  4706. list) {
  4707. if (check_iocb == cmdiocb) {
  4708. list_move_tail(&check_iocb->list, &completions);
  4709. break;
  4710. }
  4711. }
  4712. if (list_empty(&completions))
  4713. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  4714. spin_unlock_irq(&phba->hbalock);
  4715. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4716. if (!list_empty(&completions)) {
  4717. lpfc_sli_cancel_iocbs(phba, &completions,
  4718. IOSTAT_LOCAL_REJECT,
  4719. IOERR_SLI_ABORTED);
  4720. }
  4721. break;
  4722. case TYPE_EVT:
  4723. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4724. break;
  4725. case TYPE_MBOX:
  4726. /* Update the ext buf ctx state if needed */
  4727. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
  4728. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
  4729. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4730. break;
  4731. case TYPE_MENLO:
  4732. /* Check to see if IOCB was issued to the port or not. If not,
  4733. * remove it from the txq queue and call cancel iocbs.
  4734. * Otherwise, call abort iotag.
  4735. */
  4736. cmdiocb = dd_data->context_un.menlo.cmdiocbq;
  4737. spin_lock_irq(&phba->hbalock);
  4738. list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
  4739. list) {
  4740. if (check_iocb == cmdiocb) {
  4741. list_move_tail(&check_iocb->list, &completions);
  4742. break;
  4743. }
  4744. }
  4745. if (list_empty(&completions))
  4746. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  4747. spin_unlock_irq(&phba->hbalock);
  4748. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4749. if (!list_empty(&completions)) {
  4750. lpfc_sli_cancel_iocbs(phba, &completions,
  4751. IOSTAT_LOCAL_REJECT,
  4752. IOERR_SLI_ABORTED);
  4753. }
  4754. break;
  4755. default:
  4756. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4757. break;
  4758. }
  4759. /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
  4760. * otherwise an error message will be displayed on the console
  4761. * so always return success (zero)
  4762. */
  4763. return rc;
  4764. }