lpfc_bsg.c 146 KB

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