lpfc_bsg.c 146 KB

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