lpfc_bsg.c 146 KB

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