lpfc_bsg.c 148 KB

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