lpfc_bsg.c 157 KB

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