lpfc_bsg.c 149 KB

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