lpfc_bsg.c 150 KB

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