lpfc_bsg.c 147 KB

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