lpfc_bsg.c 150 KB

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