lpfc_bsg.c 139 KB

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