lpfc_bsg.c 148 KB

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