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-2014 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *******************************************************************/
  20. #include <linux/interrupt.h>
  21. #include <linux/mempool.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/list.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_transport_fc.h>
  29. #include <scsi/scsi_bsg_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_bsg.h"
  37. #include "lpfc_disc.h"
  38. #include "lpfc_scsi.h"
  39. #include "lpfc.h"
  40. #include "lpfc_logmsg.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_debugfs.h"
  43. #include "lpfc_vport.h"
  44. #include "lpfc_version.h"
  45. struct lpfc_bsg_event {
  46. struct list_head node;
  47. struct kref kref;
  48. wait_queue_head_t wq;
  49. /* Event type and waiter identifiers */
  50. uint32_t type_mask;
  51. uint32_t req_id;
  52. uint32_t reg_id;
  53. /* next two flags are here for the auto-delete logic */
  54. unsigned long wait_time_stamp;
  55. int waiting;
  56. /* seen and not seen events */
  57. struct list_head events_to_get;
  58. struct list_head events_to_see;
  59. /* driver data associated with the job */
  60. void *dd_data;
  61. };
  62. struct lpfc_bsg_iocb {
  63. struct lpfc_iocbq *cmdiocbq;
  64. struct lpfc_dmabuf *rmp;
  65. struct lpfc_nodelist *ndlp;
  66. };
  67. struct lpfc_bsg_mbox {
  68. LPFC_MBOXQ_t *pmboxq;
  69. MAILBOX_t *mb;
  70. struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
  71. uint8_t *ext; /* extended mailbox data */
  72. uint32_t mbOffset; /* from app */
  73. uint32_t inExtWLen; /* from app */
  74. uint32_t outExtWLen; /* from app */
  75. };
  76. #define MENLO_DID 0x0000FC0E
  77. struct lpfc_bsg_menlo {
  78. struct lpfc_iocbq *cmdiocbq;
  79. struct lpfc_dmabuf *rmp;
  80. };
  81. #define TYPE_EVT 1
  82. #define TYPE_IOCB 2
  83. #define TYPE_MBOX 3
  84. #define TYPE_MENLO 4
  85. struct bsg_job_data {
  86. uint32_t type;
  87. struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
  88. union {
  89. struct lpfc_bsg_event *evt;
  90. struct lpfc_bsg_iocb iocb;
  91. struct lpfc_bsg_mbox mbox;
  92. struct lpfc_bsg_menlo menlo;
  93. } context_un;
  94. };
  95. struct event_data {
  96. struct list_head node;
  97. uint32_t type;
  98. uint32_t immed_dat;
  99. void *data;
  100. uint32_t len;
  101. };
  102. #define BUF_SZ_4K 4096
  103. #define SLI_CT_ELX_LOOPBACK 0x10
  104. enum ELX_LOOPBACK_CMD {
  105. ELX_LOOPBACK_XRI_SETUP,
  106. ELX_LOOPBACK_DATA,
  107. };
  108. #define ELX_LOOPBACK_HEADER_SZ \
  109. (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
  110. struct lpfc_dmabufext {
  111. struct lpfc_dmabuf dma;
  112. uint32_t size;
  113. uint32_t flag;
  114. };
  115. static void
  116. lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
  117. {
  118. struct lpfc_dmabuf *mlast, *next_mlast;
  119. if (mlist) {
  120. list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
  121. list) {
  122. lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
  123. list_del(&mlast->list);
  124. kfree(mlast);
  125. }
  126. lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
  127. kfree(mlist);
  128. }
  129. return;
  130. }
  131. static struct lpfc_dmabuf *
  132. lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
  133. int outbound_buffers, struct ulp_bde64 *bpl,
  134. int *bpl_entries)
  135. {
  136. struct lpfc_dmabuf *mlist = NULL;
  137. struct lpfc_dmabuf *mp;
  138. unsigned int bytes_left = size;
  139. /* Verify we can support the size specified */
  140. if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
  141. return NULL;
  142. /* Determine the number of dma buffers to allocate */
  143. *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
  144. size/LPFC_BPL_SIZE);
  145. /* Allocate dma buffer and place in BPL passed */
  146. while (bytes_left) {
  147. /* Allocate dma buffer */
  148. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  149. if (!mp) {
  150. if (mlist)
  151. lpfc_free_bsg_buffers(phba, mlist);
  152. return NULL;
  153. }
  154. INIT_LIST_HEAD(&mp->list);
  155. mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
  156. if (!mp->virt) {
  157. kfree(mp);
  158. if (mlist)
  159. lpfc_free_bsg_buffers(phba, mlist);
  160. return NULL;
  161. }
  162. /* Queue it to a linked list */
  163. if (!mlist)
  164. mlist = mp;
  165. else
  166. list_add_tail(&mp->list, &mlist->list);
  167. /* Add buffer to buffer pointer list */
  168. if (outbound_buffers)
  169. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  170. else
  171. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  172. bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
  173. bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
  174. bpl->tus.f.bdeSize = (uint16_t)
  175. (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
  176. bytes_left);
  177. bytes_left -= bpl->tus.f.bdeSize;
  178. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  179. bpl++;
  180. }
  181. return mlist;
  182. }
  183. static unsigned int
  184. lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
  185. struct fc_bsg_buffer *bsg_buffers,
  186. unsigned int bytes_to_transfer, int to_buffers)
  187. {
  188. struct lpfc_dmabuf *mp;
  189. unsigned int transfer_bytes, bytes_copied = 0;
  190. unsigned int sg_offset, dma_offset;
  191. unsigned char *dma_address, *sg_address;
  192. LIST_HEAD(temp_list);
  193. struct sg_mapping_iter miter;
  194. unsigned long flags;
  195. unsigned int sg_flags = SG_MITER_ATOMIC;
  196. bool sg_valid;
  197. list_splice_init(&dma_buffers->list, &temp_list);
  198. list_add(&dma_buffers->list, &temp_list);
  199. sg_offset = 0;
  200. if (to_buffers)
  201. sg_flags |= SG_MITER_FROM_SG;
  202. else
  203. sg_flags |= SG_MITER_TO_SG;
  204. sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
  205. sg_flags);
  206. local_irq_save(flags);
  207. sg_valid = sg_miter_next(&miter);
  208. list_for_each_entry(mp, &temp_list, list) {
  209. dma_offset = 0;
  210. while (bytes_to_transfer && sg_valid &&
  211. (dma_offset < LPFC_BPL_SIZE)) {
  212. dma_address = mp->virt + dma_offset;
  213. if (sg_offset) {
  214. /* Continue previous partial transfer of sg */
  215. sg_address = miter.addr + sg_offset;
  216. transfer_bytes = miter.length - sg_offset;
  217. } else {
  218. sg_address = miter.addr;
  219. transfer_bytes = miter.length;
  220. }
  221. if (bytes_to_transfer < transfer_bytes)
  222. transfer_bytes = bytes_to_transfer;
  223. if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
  224. transfer_bytes = LPFC_BPL_SIZE - dma_offset;
  225. if (to_buffers)
  226. memcpy(dma_address, sg_address, transfer_bytes);
  227. else
  228. memcpy(sg_address, dma_address, transfer_bytes);
  229. dma_offset += transfer_bytes;
  230. sg_offset += transfer_bytes;
  231. bytes_to_transfer -= transfer_bytes;
  232. bytes_copied += transfer_bytes;
  233. if (sg_offset >= miter.length) {
  234. sg_offset = 0;
  235. sg_valid = sg_miter_next(&miter);
  236. }
  237. }
  238. }
  239. sg_miter_stop(&miter);
  240. local_irq_restore(flags);
  241. list_del_init(&dma_buffers->list);
  242. list_splice(&temp_list, &dma_buffers->list);
  243. return bytes_copied;
  244. }
  245. /**
  246. * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
  247. * @phba: Pointer to HBA context object.
  248. * @cmdiocbq: Pointer to command iocb.
  249. * @rspiocbq: Pointer to response iocb.
  250. *
  251. * This function is the completion handler for iocbs issued using
  252. * lpfc_bsg_send_mgmt_cmd function. This function is called by the
  253. * ring event handler function without any lock held. This function
  254. * can be called from both worker thread context and interrupt
  255. * context. This function also can be called from another thread which
  256. * cleans up the SLI layer objects.
  257. * This function copies the contents of the response iocb to the
  258. * response iocb memory object provided by the caller of
  259. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  260. * sleeps for the iocb completion.
  261. **/
  262. static void
  263. lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
  264. struct lpfc_iocbq *cmdiocbq,
  265. struct lpfc_iocbq *rspiocbq)
  266. {
  267. struct bsg_job_data *dd_data;
  268. struct fc_bsg_job *job;
  269. IOCB_t *rsp;
  270. struct lpfc_dmabuf *bmp, *cmp, *rmp;
  271. struct lpfc_nodelist *ndlp;
  272. struct lpfc_bsg_iocb *iocb;
  273. unsigned long flags;
  274. unsigned int rsp_size;
  275. int rc = 0;
  276. dd_data = cmdiocbq->context1;
  277. /* Determine if job has been aborted */
  278. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  279. job = dd_data->set_job;
  280. if (job) {
  281. /* Prevent timeout handling from trying to abort job */
  282. job->dd_data = NULL;
  283. }
  284. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  285. /* Close the timeout handler abort window */
  286. spin_lock_irqsave(&phba->hbalock, flags);
  287. cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
  288. spin_unlock_irqrestore(&phba->hbalock, flags);
  289. iocb = &dd_data->context_un.iocb;
  290. ndlp = iocb->ndlp;
  291. rmp = iocb->rmp;
  292. cmp = cmdiocbq->context2;
  293. bmp = cmdiocbq->context3;
  294. rsp = &rspiocbq->iocb;
  295. /* Copy the completed data or set the error status */
  296. if (job) {
  297. if (rsp->ulpStatus) {
  298. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  299. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  300. case IOERR_SEQUENCE_TIMEOUT:
  301. rc = -ETIMEDOUT;
  302. break;
  303. case IOERR_INVALID_RPI:
  304. rc = -EFAULT;
  305. break;
  306. default:
  307. rc = -EACCES;
  308. break;
  309. }
  310. } else {
  311. rc = -EACCES;
  312. }
  313. } else {
  314. rsp_size = rsp->un.genreq64.bdl.bdeSize;
  315. job->reply->reply_payload_rcv_len =
  316. lpfc_bsg_copy_data(rmp, &job->reply_payload,
  317. rsp_size, 0);
  318. }
  319. }
  320. lpfc_free_bsg_buffers(phba, cmp);
  321. lpfc_free_bsg_buffers(phba, rmp);
  322. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  323. kfree(bmp);
  324. lpfc_sli_release_iocbq(phba, cmdiocbq);
  325. lpfc_nlp_put(ndlp);
  326. kfree(dd_data);
  327. /* Complete the job if the job is still active */
  328. if (job) {
  329. job->reply->result = rc;
  330. job->job_done(job);
  331. }
  332. return;
  333. }
  334. /**
  335. * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
  336. * @job: fc_bsg_job to handle
  337. **/
  338. static int
  339. lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
  340. {
  341. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  342. struct lpfc_hba *phba = vport->phba;
  343. struct lpfc_rport_data *rdata = job->rport->dd_data;
  344. struct lpfc_nodelist *ndlp = rdata->pnode;
  345. struct ulp_bde64 *bpl = NULL;
  346. uint32_t timeout;
  347. struct lpfc_iocbq *cmdiocbq = NULL;
  348. IOCB_t *cmd;
  349. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
  350. int request_nseg;
  351. int reply_nseg;
  352. struct bsg_job_data *dd_data;
  353. unsigned long flags;
  354. uint32_t creg_val;
  355. int rc = 0;
  356. int iocb_stat;
  357. /* in case no data is transferred */
  358. job->reply->reply_payload_rcv_len = 0;
  359. /* allocate our bsg tracking structure */
  360. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  361. if (!dd_data) {
  362. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  363. "2733 Failed allocation of dd_data\n");
  364. rc = -ENOMEM;
  365. goto no_dd_data;
  366. }
  367. if (!lpfc_nlp_get(ndlp)) {
  368. rc = -ENODEV;
  369. goto no_ndlp;
  370. }
  371. if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
  372. rc = -ENODEV;
  373. goto free_ndlp;
  374. }
  375. cmdiocbq = lpfc_sli_get_iocbq(phba);
  376. if (!cmdiocbq) {
  377. rc = -ENOMEM;
  378. goto free_ndlp;
  379. }
  380. cmd = &cmdiocbq->iocb;
  381. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  382. if (!bmp) {
  383. rc = -ENOMEM;
  384. goto free_cmdiocbq;
  385. }
  386. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  387. if (!bmp->virt) {
  388. rc = -ENOMEM;
  389. goto free_bmp;
  390. }
  391. INIT_LIST_HEAD(&bmp->list);
  392. bpl = (struct ulp_bde64 *) bmp->virt;
  393. request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
  394. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  395. 1, bpl, &request_nseg);
  396. if (!cmp) {
  397. rc = -ENOMEM;
  398. goto free_bmp;
  399. }
  400. lpfc_bsg_copy_data(cmp, &job->request_payload,
  401. job->request_payload.payload_len, 1);
  402. bpl += request_nseg;
  403. reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
  404. rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
  405. bpl, &reply_nseg);
  406. if (!rmp) {
  407. rc = -ENOMEM;
  408. goto free_cmp;
  409. }
  410. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  411. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  412. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  413. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  414. cmd->un.genreq64.bdl.bdeSize =
  415. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  416. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  417. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  418. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  419. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  420. cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
  421. cmd->ulpBdeCount = 1;
  422. cmd->ulpLe = 1;
  423. cmd->ulpClass = CLASS3;
  424. cmd->ulpContext = ndlp->nlp_rpi;
  425. if (phba->sli_rev == LPFC_SLI_REV4)
  426. cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  427. cmd->ulpOwner = OWN_CHIP;
  428. cmdiocbq->vport = phba->pport;
  429. cmdiocbq->context3 = bmp;
  430. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  431. timeout = phba->fc_ratov * 2;
  432. cmd->ulpTimeout = timeout;
  433. cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
  434. cmdiocbq->context1 = dd_data;
  435. cmdiocbq->context2 = cmp;
  436. cmdiocbq->context3 = bmp;
  437. cmdiocbq->context_un.ndlp = ndlp;
  438. dd_data->type = TYPE_IOCB;
  439. dd_data->set_job = job;
  440. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  441. dd_data->context_un.iocb.ndlp = ndlp;
  442. dd_data->context_un.iocb.rmp = rmp;
  443. job->dd_data = dd_data;
  444. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  445. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  446. rc = -EIO ;
  447. goto free_rmp;
  448. }
  449. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  450. writel(creg_val, phba->HCregaddr);
  451. readl(phba->HCregaddr); /* flush */
  452. }
  453. iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  454. if (iocb_stat == IOCB_SUCCESS) {
  455. spin_lock_irqsave(&phba->hbalock, flags);
  456. /* make sure the I/O had not been completed yet */
  457. if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
  458. /* open up abort window to timeout handler */
  459. cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
  460. }
  461. spin_unlock_irqrestore(&phba->hbalock, flags);
  462. return 0; /* done for now */
  463. } else if (iocb_stat == IOCB_BUSY) {
  464. rc = -EAGAIN;
  465. } else {
  466. rc = -EIO;
  467. }
  468. /* iocb failed so cleanup */
  469. job->dd_data = NULL;
  470. free_rmp:
  471. lpfc_free_bsg_buffers(phba, rmp);
  472. free_cmp:
  473. lpfc_free_bsg_buffers(phba, cmp);
  474. free_bmp:
  475. if (bmp->virt)
  476. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  477. kfree(bmp);
  478. free_cmdiocbq:
  479. lpfc_sli_release_iocbq(phba, cmdiocbq);
  480. free_ndlp:
  481. lpfc_nlp_put(ndlp);
  482. no_ndlp:
  483. kfree(dd_data);
  484. no_dd_data:
  485. /* make error code available to userspace */
  486. job->reply->result = rc;
  487. job->dd_data = NULL;
  488. return rc;
  489. }
  490. /**
  491. * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
  492. * @phba: Pointer to HBA context object.
  493. * @cmdiocbq: Pointer to command iocb.
  494. * @rspiocbq: Pointer to response iocb.
  495. *
  496. * This function is the completion handler for iocbs issued using
  497. * lpfc_bsg_rport_els_cmp function. This function is called by the
  498. * ring event handler function without any lock held. This function
  499. * can be called from both worker thread context and interrupt
  500. * context. This function also can be called from other thread which
  501. * cleans up the SLI layer objects.
  502. * This function copies the contents of the response iocb to the
  503. * response iocb memory object provided by the caller of
  504. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  505. * sleeps for the iocb completion.
  506. **/
  507. static void
  508. lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
  509. struct lpfc_iocbq *cmdiocbq,
  510. struct lpfc_iocbq *rspiocbq)
  511. {
  512. struct bsg_job_data *dd_data;
  513. struct fc_bsg_job *job;
  514. IOCB_t *rsp;
  515. struct lpfc_nodelist *ndlp;
  516. struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
  517. struct fc_bsg_ctels_reply *els_reply;
  518. uint8_t *rjt_data;
  519. unsigned long flags;
  520. unsigned int rsp_size;
  521. int rc = 0;
  522. dd_data = cmdiocbq->context1;
  523. ndlp = dd_data->context_un.iocb.ndlp;
  524. cmdiocbq->context1 = ndlp;
  525. /* Determine if job has been aborted */
  526. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  527. job = dd_data->set_job;
  528. if (job) {
  529. /* Prevent timeout handling from trying to abort job */
  530. job->dd_data = NULL;
  531. }
  532. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  533. /* Close the timeout handler abort window */
  534. spin_lock_irqsave(&phba->hbalock, flags);
  535. cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
  536. spin_unlock_irqrestore(&phba->hbalock, flags);
  537. rsp = &rspiocbq->iocb;
  538. pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
  539. prsp = (struct lpfc_dmabuf *)pcmd->list.next;
  540. /* Copy the completed job data or determine the job status if job is
  541. * still active
  542. */
  543. if (job) {
  544. if (rsp->ulpStatus == IOSTAT_SUCCESS) {
  545. rsp_size = rsp->un.elsreq64.bdl.bdeSize;
  546. job->reply->reply_payload_rcv_len =
  547. sg_copy_from_buffer(job->reply_payload.sg_list,
  548. job->reply_payload.sg_cnt,
  549. prsp->virt,
  550. rsp_size);
  551. } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
  552. job->reply->reply_payload_rcv_len =
  553. sizeof(struct fc_bsg_ctels_reply);
  554. /* LS_RJT data returned in word 4 */
  555. rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
  556. els_reply = &job->reply->reply_data.ctels_reply;
  557. els_reply->status = FC_CTELS_STATUS_REJECT;
  558. els_reply->rjt_data.action = rjt_data[3];
  559. els_reply->rjt_data.reason_code = rjt_data[2];
  560. els_reply->rjt_data.reason_explanation = rjt_data[1];
  561. els_reply->rjt_data.vendor_unique = rjt_data[0];
  562. } else {
  563. rc = -EIO;
  564. }
  565. }
  566. lpfc_nlp_put(ndlp);
  567. lpfc_els_free_iocb(phba, cmdiocbq);
  568. kfree(dd_data);
  569. /* Complete the job if the job is still active */
  570. if (job) {
  571. job->reply->result = rc;
  572. job->job_done(job);
  573. }
  574. return;
  575. }
  576. /**
  577. * lpfc_bsg_rport_els - send an ELS command from a bsg request
  578. * @job: fc_bsg_job to handle
  579. **/
  580. static int
  581. lpfc_bsg_rport_els(struct fc_bsg_job *job)
  582. {
  583. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  584. struct lpfc_hba *phba = vport->phba;
  585. struct lpfc_rport_data *rdata = job->rport->dd_data;
  586. struct lpfc_nodelist *ndlp = rdata->pnode;
  587. uint32_t elscmd;
  588. uint32_t cmdsize;
  589. struct lpfc_iocbq *cmdiocbq;
  590. uint16_t rpi = 0;
  591. struct bsg_job_data *dd_data;
  592. unsigned long flags;
  593. uint32_t creg_val;
  594. int rc = 0;
  595. /* in case no data is transferred */
  596. job->reply->reply_payload_rcv_len = 0;
  597. /* verify the els command is not greater than the
  598. * maximum ELS transfer size.
  599. */
  600. if (job->request_payload.payload_len > FCELSSIZE) {
  601. rc = -EINVAL;
  602. goto no_dd_data;
  603. }
  604. /* allocate our bsg tracking structure */
  605. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  606. if (!dd_data) {
  607. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  608. "2735 Failed allocation of dd_data\n");
  609. rc = -ENOMEM;
  610. goto no_dd_data;
  611. }
  612. elscmd = job->request->rqst_data.r_els.els_code;
  613. cmdsize = job->request_payload.payload_len;
  614. if (!lpfc_nlp_get(ndlp)) {
  615. rc = -ENODEV;
  616. goto free_dd_data;
  617. }
  618. /* We will use the allocated dma buffers by prep els iocb for command
  619. * and response to ensure if the job times out and the request is freed,
  620. * we won't be dma into memory that is no longer allocated to for the
  621. * request.
  622. */
  623. cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
  624. ndlp->nlp_DID, elscmd);
  625. if (!cmdiocbq) {
  626. rc = -EIO;
  627. goto release_ndlp;
  628. }
  629. rpi = ndlp->nlp_rpi;
  630. /* Transfer the request payload to allocated command dma buffer */
  631. sg_copy_to_buffer(job->request_payload.sg_list,
  632. job->request_payload.sg_cnt,
  633. ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
  634. cmdsize);
  635. if (phba->sli_rev == LPFC_SLI_REV4)
  636. cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
  637. else
  638. cmdiocbq->iocb.ulpContext = rpi;
  639. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  640. cmdiocbq->context1 = dd_data;
  641. cmdiocbq->context_un.ndlp = ndlp;
  642. cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
  643. dd_data->type = TYPE_IOCB;
  644. dd_data->set_job = job;
  645. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  646. dd_data->context_un.iocb.ndlp = ndlp;
  647. dd_data->context_un.iocb.rmp = NULL;
  648. job->dd_data = dd_data;
  649. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  650. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  651. rc = -EIO;
  652. goto linkdown_err;
  653. }
  654. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  655. writel(creg_val, phba->HCregaddr);
  656. readl(phba->HCregaddr); /* flush */
  657. }
  658. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  659. if (rc == IOCB_SUCCESS) {
  660. spin_lock_irqsave(&phba->hbalock, flags);
  661. /* make sure the I/O had not been completed/released */
  662. if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
  663. /* open up abort window to timeout handler */
  664. cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
  665. }
  666. spin_unlock_irqrestore(&phba->hbalock, flags);
  667. return 0; /* done for now */
  668. } else if (rc == IOCB_BUSY) {
  669. rc = -EAGAIN;
  670. } else {
  671. rc = -EIO;
  672. }
  673. /* iocb failed so cleanup */
  674. job->dd_data = NULL;
  675. linkdown_err:
  676. cmdiocbq->context1 = ndlp;
  677. lpfc_els_free_iocb(phba, cmdiocbq);
  678. release_ndlp:
  679. lpfc_nlp_put(ndlp);
  680. free_dd_data:
  681. kfree(dd_data);
  682. no_dd_data:
  683. /* make error code available to userspace */
  684. job->reply->result = rc;
  685. job->dd_data = NULL;
  686. return rc;
  687. }
  688. /**
  689. * lpfc_bsg_event_free - frees an allocated event structure
  690. * @kref: Pointer to a kref.
  691. *
  692. * Called from kref_put. Back cast the kref into an event structure address.
  693. * Free any events to get, delete associated nodes, free any events to see,
  694. * free any data then free the event itself.
  695. **/
  696. static void
  697. lpfc_bsg_event_free(struct kref *kref)
  698. {
  699. struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
  700. kref);
  701. struct event_data *ed;
  702. list_del(&evt->node);
  703. while (!list_empty(&evt->events_to_get)) {
  704. ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
  705. list_del(&ed->node);
  706. kfree(ed->data);
  707. kfree(ed);
  708. }
  709. while (!list_empty(&evt->events_to_see)) {
  710. ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
  711. list_del(&ed->node);
  712. kfree(ed->data);
  713. kfree(ed);
  714. }
  715. kfree(evt->dd_data);
  716. kfree(evt);
  717. }
  718. /**
  719. * lpfc_bsg_event_ref - increments the kref for an event
  720. * @evt: Pointer to an event structure.
  721. **/
  722. static inline void
  723. lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
  724. {
  725. kref_get(&evt->kref);
  726. }
  727. /**
  728. * lpfc_bsg_event_unref - Uses kref_put to free an event structure
  729. * @evt: Pointer to an event structure.
  730. **/
  731. static inline void
  732. lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
  733. {
  734. kref_put(&evt->kref, lpfc_bsg_event_free);
  735. }
  736. /**
  737. * lpfc_bsg_event_new - allocate and initialize a event structure
  738. * @ev_mask: Mask of events.
  739. * @ev_reg_id: Event reg id.
  740. * @ev_req_id: Event request id.
  741. **/
  742. static struct lpfc_bsg_event *
  743. lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
  744. {
  745. struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  746. if (!evt)
  747. return NULL;
  748. INIT_LIST_HEAD(&evt->events_to_get);
  749. INIT_LIST_HEAD(&evt->events_to_see);
  750. evt->type_mask = ev_mask;
  751. evt->req_id = ev_req_id;
  752. evt->reg_id = ev_reg_id;
  753. evt->wait_time_stamp = jiffies;
  754. evt->dd_data = NULL;
  755. init_waitqueue_head(&evt->wq);
  756. kref_init(&evt->kref);
  757. return evt;
  758. }
  759. /**
  760. * diag_cmd_data_free - Frees an lpfc dma buffer extension
  761. * @phba: Pointer to HBA context object.
  762. * @mlist: Pointer to an lpfc dma buffer extension.
  763. **/
  764. static int
  765. diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
  766. {
  767. struct lpfc_dmabufext *mlast;
  768. struct pci_dev *pcidev;
  769. struct list_head head, *curr, *next;
  770. if ((!mlist) || (!lpfc_is_link_up(phba) &&
  771. (phba->link_flag & LS_LOOPBACK_MODE))) {
  772. return 0;
  773. }
  774. pcidev = phba->pcidev;
  775. list_add_tail(&head, &mlist->dma.list);
  776. list_for_each_safe(curr, next, &head) {
  777. mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
  778. if (mlast->dma.virt)
  779. dma_free_coherent(&pcidev->dev,
  780. mlast->size,
  781. mlast->dma.virt,
  782. mlast->dma.phys);
  783. kfree(mlast);
  784. }
  785. return 0;
  786. }
  787. /**
  788. * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
  789. * @phba:
  790. * @pring:
  791. * @piocbq:
  792. *
  793. * This function is called when an unsolicited CT command is received. It
  794. * forwards the event to any processes registered to receive CT events.
  795. **/
  796. int
  797. lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  798. struct lpfc_iocbq *piocbq)
  799. {
  800. uint32_t evt_req_id = 0;
  801. uint32_t cmd;
  802. uint32_t len;
  803. struct lpfc_dmabuf *dmabuf = NULL;
  804. struct lpfc_bsg_event *evt;
  805. struct event_data *evt_dat = NULL;
  806. struct lpfc_iocbq *iocbq;
  807. size_t offset = 0;
  808. struct list_head head;
  809. struct ulp_bde64 *bde;
  810. dma_addr_t dma_addr;
  811. int i;
  812. struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
  813. struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
  814. struct lpfc_hbq_entry *hbqe;
  815. struct lpfc_sli_ct_request *ct_req;
  816. struct fc_bsg_job *job = NULL;
  817. struct bsg_job_data *dd_data = NULL;
  818. unsigned long flags;
  819. int size = 0;
  820. INIT_LIST_HEAD(&head);
  821. list_add_tail(&head, &piocbq->list);
  822. if (piocbq->iocb.ulpBdeCount == 0 ||
  823. piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
  824. goto error_ct_unsol_exit;
  825. if (phba->link_state == LPFC_HBA_ERROR ||
  826. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
  827. goto error_ct_unsol_exit;
  828. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  829. dmabuf = bdeBuf1;
  830. else {
  831. dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
  832. piocbq->iocb.un.cont64[0].addrLow);
  833. dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
  834. }
  835. if (dmabuf == NULL)
  836. goto error_ct_unsol_exit;
  837. ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
  838. evt_req_id = ct_req->FsType;
  839. cmd = ct_req->CommandResponse.bits.CmdRsp;
  840. len = ct_req->CommandResponse.bits.Size;
  841. if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
  842. lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
  843. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  844. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  845. if (!(evt->type_mask & FC_REG_CT_EVENT) ||
  846. evt->req_id != evt_req_id)
  847. continue;
  848. lpfc_bsg_event_ref(evt);
  849. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  850. evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
  851. if (evt_dat == NULL) {
  852. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  853. lpfc_bsg_event_unref(evt);
  854. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  855. "2614 Memory allocation failed for "
  856. "CT event\n");
  857. break;
  858. }
  859. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  860. /* take accumulated byte count from the last iocbq */
  861. iocbq = list_entry(head.prev, typeof(*iocbq), list);
  862. evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
  863. } else {
  864. list_for_each_entry(iocbq, &head, list) {
  865. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
  866. evt_dat->len +=
  867. iocbq->iocb.un.cont64[i].tus.f.bdeSize;
  868. }
  869. }
  870. evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
  871. if (evt_dat->data == NULL) {
  872. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  873. "2615 Memory allocation failed for "
  874. "CT event data, size %d\n",
  875. evt_dat->len);
  876. kfree(evt_dat);
  877. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  878. lpfc_bsg_event_unref(evt);
  879. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  880. goto error_ct_unsol_exit;
  881. }
  882. list_for_each_entry(iocbq, &head, list) {
  883. size = 0;
  884. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  885. bdeBuf1 = iocbq->context2;
  886. bdeBuf2 = iocbq->context3;
  887. }
  888. for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
  889. if (phba->sli3_options &
  890. LPFC_SLI3_HBQ_ENABLED) {
  891. if (i == 0) {
  892. hbqe = (struct lpfc_hbq_entry *)
  893. &iocbq->iocb.un.ulpWord[0];
  894. size = hbqe->bde.tus.f.bdeSize;
  895. dmabuf = bdeBuf1;
  896. } else if (i == 1) {
  897. hbqe = (struct lpfc_hbq_entry *)
  898. &iocbq->iocb.unsli3.
  899. sli3Words[4];
  900. size = hbqe->bde.tus.f.bdeSize;
  901. dmabuf = bdeBuf2;
  902. }
  903. if ((offset + size) > evt_dat->len)
  904. size = evt_dat->len - offset;
  905. } else {
  906. size = iocbq->iocb.un.cont64[i].
  907. tus.f.bdeSize;
  908. bde = &iocbq->iocb.un.cont64[i];
  909. dma_addr = getPaddr(bde->addrHigh,
  910. bde->addrLow);
  911. dmabuf = lpfc_sli_ringpostbuf_get(phba,
  912. pring, dma_addr);
  913. }
  914. if (!dmabuf) {
  915. lpfc_printf_log(phba, KERN_ERR,
  916. LOG_LIBDFC, "2616 No dmabuf "
  917. "found for iocbq 0x%p\n",
  918. iocbq);
  919. kfree(evt_dat->data);
  920. kfree(evt_dat);
  921. spin_lock_irqsave(&phba->ct_ev_lock,
  922. flags);
  923. lpfc_bsg_event_unref(evt);
  924. spin_unlock_irqrestore(
  925. &phba->ct_ev_lock, flags);
  926. goto error_ct_unsol_exit;
  927. }
  928. memcpy((char *)(evt_dat->data) + offset,
  929. dmabuf->virt, size);
  930. offset += size;
  931. if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
  932. !(phba->sli3_options &
  933. LPFC_SLI3_HBQ_ENABLED)) {
  934. lpfc_sli_ringpostbuf_put(phba, pring,
  935. dmabuf);
  936. } else {
  937. switch (cmd) {
  938. case ELX_LOOPBACK_DATA:
  939. if (phba->sli_rev <
  940. LPFC_SLI_REV4)
  941. diag_cmd_data_free(phba,
  942. (struct lpfc_dmabufext
  943. *)dmabuf);
  944. break;
  945. case ELX_LOOPBACK_XRI_SETUP:
  946. if ((phba->sli_rev ==
  947. LPFC_SLI_REV2) ||
  948. (phba->sli3_options &
  949. LPFC_SLI3_HBQ_ENABLED
  950. )) {
  951. lpfc_in_buf_free(phba,
  952. dmabuf);
  953. } else {
  954. lpfc_post_buffer(phba,
  955. pring,
  956. 1);
  957. }
  958. break;
  959. default:
  960. if (!(phba->sli3_options &
  961. LPFC_SLI3_HBQ_ENABLED))
  962. lpfc_post_buffer(phba,
  963. pring,
  964. 1);
  965. break;
  966. }
  967. }
  968. }
  969. }
  970. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  971. if (phba->sli_rev == LPFC_SLI_REV4) {
  972. evt_dat->immed_dat = phba->ctx_idx;
  973. phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
  974. /* Provide warning for over-run of the ct_ctx array */
  975. if (phba->ct_ctx[evt_dat->immed_dat].valid ==
  976. UNSOL_VALID)
  977. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  978. "2717 CT context array entry "
  979. "[%d] over-run: oxid:x%x, "
  980. "sid:x%x\n", phba->ctx_idx,
  981. phba->ct_ctx[
  982. evt_dat->immed_dat].oxid,
  983. phba->ct_ctx[
  984. evt_dat->immed_dat].SID);
  985. phba->ct_ctx[evt_dat->immed_dat].rxid =
  986. piocbq->iocb.ulpContext;
  987. phba->ct_ctx[evt_dat->immed_dat].oxid =
  988. piocbq->iocb.unsli3.rcvsli3.ox_id;
  989. phba->ct_ctx[evt_dat->immed_dat].SID =
  990. piocbq->iocb.un.rcvels.remoteID;
  991. phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
  992. } else
  993. evt_dat->immed_dat = piocbq->iocb.ulpContext;
  994. evt_dat->type = FC_REG_CT_EVENT;
  995. list_add(&evt_dat->node, &evt->events_to_see);
  996. if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
  997. wake_up_interruptible(&evt->wq);
  998. lpfc_bsg_event_unref(evt);
  999. break;
  1000. }
  1001. list_move(evt->events_to_see.prev, &evt->events_to_get);
  1002. dd_data = (struct bsg_job_data *)evt->dd_data;
  1003. job = dd_data->set_job;
  1004. dd_data->set_job = NULL;
  1005. lpfc_bsg_event_unref(evt);
  1006. if (job) {
  1007. job->reply->reply_payload_rcv_len = size;
  1008. /* make error code available to userspace */
  1009. job->reply->result = 0;
  1010. job->dd_data = NULL;
  1011. /* complete the job back to userspace */
  1012. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1013. job->job_done(job);
  1014. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1015. }
  1016. }
  1017. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1018. error_ct_unsol_exit:
  1019. if (!list_empty(&head))
  1020. list_del(&head);
  1021. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  1022. (evt_req_id == SLI_CT_ELX_LOOPBACK))
  1023. return 0;
  1024. return 1;
  1025. }
  1026. /**
  1027. * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
  1028. * @phba: Pointer to HBA context object.
  1029. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  1030. *
  1031. * This function handles abort to the CT command toward management plane
  1032. * for SLI4 port.
  1033. *
  1034. * If the pending context of a CT command to management plane present, clears
  1035. * such context and returns 1 for handled; otherwise, it returns 0 indicating
  1036. * no context exists.
  1037. **/
  1038. int
  1039. lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
  1040. {
  1041. struct fc_frame_header fc_hdr;
  1042. struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
  1043. int ctx_idx, handled = 0;
  1044. uint16_t oxid, rxid;
  1045. uint32_t sid;
  1046. memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  1047. sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
  1048. oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
  1049. rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
  1050. for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
  1051. if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
  1052. continue;
  1053. if (phba->ct_ctx[ctx_idx].rxid != rxid)
  1054. continue;
  1055. if (phba->ct_ctx[ctx_idx].oxid != oxid)
  1056. continue;
  1057. if (phba->ct_ctx[ctx_idx].SID != sid)
  1058. continue;
  1059. phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
  1060. handled = 1;
  1061. }
  1062. return handled;
  1063. }
  1064. /**
  1065. * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
  1066. * @job: SET_EVENT fc_bsg_job
  1067. **/
  1068. static int
  1069. lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
  1070. {
  1071. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1072. struct lpfc_hba *phba = vport->phba;
  1073. struct set_ct_event *event_req;
  1074. struct lpfc_bsg_event *evt;
  1075. int rc = 0;
  1076. struct bsg_job_data *dd_data = NULL;
  1077. uint32_t ev_mask;
  1078. unsigned long flags;
  1079. if (job->request_len <
  1080. sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
  1081. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1082. "2612 Received SET_CT_EVENT below minimum "
  1083. "size\n");
  1084. rc = -EINVAL;
  1085. goto job_error;
  1086. }
  1087. event_req = (struct set_ct_event *)
  1088. job->request->rqst_data.h_vendor.vendor_cmd;
  1089. ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
  1090. FC_REG_EVENT_MASK);
  1091. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1092. list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
  1093. if (evt->reg_id == event_req->ev_reg_id) {
  1094. lpfc_bsg_event_ref(evt);
  1095. evt->wait_time_stamp = jiffies;
  1096. dd_data = (struct bsg_job_data *)evt->dd_data;
  1097. break;
  1098. }
  1099. }
  1100. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1101. if (&evt->node == &phba->ct_ev_waiters) {
  1102. /* no event waiting struct yet - first call */
  1103. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  1104. if (dd_data == NULL) {
  1105. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1106. "2734 Failed allocation of dd_data\n");
  1107. rc = -ENOMEM;
  1108. goto job_error;
  1109. }
  1110. evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
  1111. event_req->ev_req_id);
  1112. if (!evt) {
  1113. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1114. "2617 Failed allocation of event "
  1115. "waiter\n");
  1116. rc = -ENOMEM;
  1117. goto job_error;
  1118. }
  1119. dd_data->type = TYPE_EVT;
  1120. dd_data->set_job = NULL;
  1121. dd_data->context_un.evt = evt;
  1122. evt->dd_data = (void *)dd_data;
  1123. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1124. list_add(&evt->node, &phba->ct_ev_waiters);
  1125. lpfc_bsg_event_ref(evt);
  1126. evt->wait_time_stamp = jiffies;
  1127. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1128. }
  1129. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1130. evt->waiting = 1;
  1131. dd_data->set_job = job; /* for unsolicited command */
  1132. job->dd_data = dd_data; /* for fc transport timeout callback*/
  1133. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1134. return 0; /* call job done later */
  1135. job_error:
  1136. if (dd_data != NULL)
  1137. kfree(dd_data);
  1138. job->dd_data = NULL;
  1139. return rc;
  1140. }
  1141. /**
  1142. * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
  1143. * @job: GET_EVENT fc_bsg_job
  1144. **/
  1145. static int
  1146. lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
  1147. {
  1148. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1149. struct lpfc_hba *phba = vport->phba;
  1150. struct get_ct_event *event_req;
  1151. struct get_ct_event_reply *event_reply;
  1152. struct lpfc_bsg_event *evt, *evt_next;
  1153. struct event_data *evt_dat = NULL;
  1154. unsigned long flags;
  1155. uint32_t rc = 0;
  1156. if (job->request_len <
  1157. sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
  1158. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1159. "2613 Received GET_CT_EVENT request below "
  1160. "minimum size\n");
  1161. rc = -EINVAL;
  1162. goto job_error;
  1163. }
  1164. event_req = (struct get_ct_event *)
  1165. job->request->rqst_data.h_vendor.vendor_cmd;
  1166. event_reply = (struct get_ct_event_reply *)
  1167. job->reply->reply_data.vendor_reply.vendor_rsp;
  1168. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1169. list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
  1170. if (evt->reg_id == event_req->ev_reg_id) {
  1171. if (list_empty(&evt->events_to_get))
  1172. break;
  1173. lpfc_bsg_event_ref(evt);
  1174. evt->wait_time_stamp = jiffies;
  1175. evt_dat = list_entry(evt->events_to_get.prev,
  1176. struct event_data, node);
  1177. list_del(&evt_dat->node);
  1178. break;
  1179. }
  1180. }
  1181. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1182. /* The app may continue to ask for event data until it gets
  1183. * an error indicating that there isn't anymore
  1184. */
  1185. if (evt_dat == NULL) {
  1186. job->reply->reply_payload_rcv_len = 0;
  1187. rc = -ENOENT;
  1188. goto job_error;
  1189. }
  1190. if (evt_dat->len > job->request_payload.payload_len) {
  1191. evt_dat->len = job->request_payload.payload_len;
  1192. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1193. "2618 Truncated event data at %d "
  1194. "bytes\n",
  1195. job->request_payload.payload_len);
  1196. }
  1197. event_reply->type = evt_dat->type;
  1198. event_reply->immed_data = evt_dat->immed_dat;
  1199. if (evt_dat->len > 0)
  1200. job->reply->reply_payload_rcv_len =
  1201. sg_copy_from_buffer(job->request_payload.sg_list,
  1202. job->request_payload.sg_cnt,
  1203. evt_dat->data, evt_dat->len);
  1204. else
  1205. job->reply->reply_payload_rcv_len = 0;
  1206. if (evt_dat) {
  1207. kfree(evt_dat->data);
  1208. kfree(evt_dat);
  1209. }
  1210. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1211. lpfc_bsg_event_unref(evt);
  1212. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1213. job->dd_data = NULL;
  1214. job->reply->result = 0;
  1215. job->job_done(job);
  1216. return 0;
  1217. job_error:
  1218. job->dd_data = NULL;
  1219. job->reply->result = rc;
  1220. return rc;
  1221. }
  1222. /**
  1223. * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
  1224. * @phba: Pointer to HBA context object.
  1225. * @cmdiocbq: Pointer to command iocb.
  1226. * @rspiocbq: Pointer to response iocb.
  1227. *
  1228. * This function is the completion handler for iocbs issued using
  1229. * lpfc_issue_ct_rsp_cmp function. This function is called by the
  1230. * ring event handler function without any lock held. This function
  1231. * can be called from both worker thread context and interrupt
  1232. * context. This function also can be called from other thread which
  1233. * cleans up the SLI layer objects.
  1234. * This function copy the contents of the response iocb to the
  1235. * response iocb memory object provided by the caller of
  1236. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  1237. * sleeps for the iocb completion.
  1238. **/
  1239. static void
  1240. lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
  1241. struct lpfc_iocbq *cmdiocbq,
  1242. struct lpfc_iocbq *rspiocbq)
  1243. {
  1244. struct bsg_job_data *dd_data;
  1245. struct fc_bsg_job *job;
  1246. IOCB_t *rsp;
  1247. struct lpfc_dmabuf *bmp, *cmp;
  1248. struct lpfc_nodelist *ndlp;
  1249. unsigned long flags;
  1250. int rc = 0;
  1251. dd_data = cmdiocbq->context1;
  1252. /* Determine if job has been aborted */
  1253. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  1254. job = dd_data->set_job;
  1255. if (job) {
  1256. /* Prevent timeout handling from trying to abort job */
  1257. job->dd_data = NULL;
  1258. }
  1259. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  1260. /* Close the timeout handler abort window */
  1261. spin_lock_irqsave(&phba->hbalock, flags);
  1262. cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
  1263. spin_unlock_irqrestore(&phba->hbalock, flags);
  1264. ndlp = dd_data->context_un.iocb.ndlp;
  1265. cmp = cmdiocbq->context2;
  1266. bmp = cmdiocbq->context3;
  1267. rsp = &rspiocbq->iocb;
  1268. /* Copy the completed job data or set the error status */
  1269. if (job) {
  1270. if (rsp->ulpStatus) {
  1271. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  1272. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  1273. case IOERR_SEQUENCE_TIMEOUT:
  1274. rc = -ETIMEDOUT;
  1275. break;
  1276. case IOERR_INVALID_RPI:
  1277. rc = -EFAULT;
  1278. break;
  1279. default:
  1280. rc = -EACCES;
  1281. break;
  1282. }
  1283. } else {
  1284. rc = -EACCES;
  1285. }
  1286. } else {
  1287. job->reply->reply_payload_rcv_len = 0;
  1288. }
  1289. }
  1290. lpfc_free_bsg_buffers(phba, cmp);
  1291. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1292. kfree(bmp);
  1293. lpfc_sli_release_iocbq(phba, cmdiocbq);
  1294. lpfc_nlp_put(ndlp);
  1295. kfree(dd_data);
  1296. /* Complete the job if the job is still active */
  1297. if (job) {
  1298. job->reply->result = rc;
  1299. job->job_done(job);
  1300. }
  1301. return;
  1302. }
  1303. /**
  1304. * lpfc_issue_ct_rsp - issue a ct response
  1305. * @phba: Pointer to HBA context object.
  1306. * @job: Pointer to the job object.
  1307. * @tag: tag index value into the ports context exchange array.
  1308. * @bmp: Pointer to a dma buffer descriptor.
  1309. * @num_entry: Number of enties in the bde.
  1310. **/
  1311. static int
  1312. lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
  1313. struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
  1314. int num_entry)
  1315. {
  1316. IOCB_t *icmd;
  1317. struct lpfc_iocbq *ctiocb = NULL;
  1318. int rc = 0;
  1319. struct lpfc_nodelist *ndlp = NULL;
  1320. struct bsg_job_data *dd_data;
  1321. unsigned long flags;
  1322. uint32_t creg_val;
  1323. /* allocate our bsg tracking structure */
  1324. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  1325. if (!dd_data) {
  1326. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1327. "2736 Failed allocation of dd_data\n");
  1328. rc = -ENOMEM;
  1329. goto no_dd_data;
  1330. }
  1331. /* Allocate buffer for command iocb */
  1332. ctiocb = lpfc_sli_get_iocbq(phba);
  1333. if (!ctiocb) {
  1334. rc = -ENOMEM;
  1335. goto no_ctiocb;
  1336. }
  1337. icmd = &ctiocb->iocb;
  1338. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  1339. icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  1340. icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
  1341. icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  1342. icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
  1343. icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  1344. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  1345. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
  1346. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  1347. /* Fill in rest of iocb */
  1348. icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  1349. icmd->ulpBdeCount = 1;
  1350. icmd->ulpLe = 1;
  1351. icmd->ulpClass = CLASS3;
  1352. if (phba->sli_rev == LPFC_SLI_REV4) {
  1353. /* Do not issue unsol response if oxid not marked as valid */
  1354. if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
  1355. rc = IOCB_ERROR;
  1356. goto issue_ct_rsp_exit;
  1357. }
  1358. icmd->ulpContext = phba->ct_ctx[tag].rxid;
  1359. icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
  1360. ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
  1361. if (!ndlp) {
  1362. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  1363. "2721 ndlp null for oxid %x SID %x\n",
  1364. icmd->ulpContext,
  1365. phba->ct_ctx[tag].SID);
  1366. rc = IOCB_ERROR;
  1367. goto issue_ct_rsp_exit;
  1368. }
  1369. /* Check if the ndlp is active */
  1370. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  1371. rc = IOCB_ERROR;
  1372. goto issue_ct_rsp_exit;
  1373. }
  1374. /* get a refernece count so the ndlp doesn't go away while
  1375. * we respond
  1376. */
  1377. if (!lpfc_nlp_get(ndlp)) {
  1378. rc = IOCB_ERROR;
  1379. goto issue_ct_rsp_exit;
  1380. }
  1381. icmd->un.ulpWord[3] =
  1382. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  1383. /* The exchange is done, mark the entry as invalid */
  1384. phba->ct_ctx[tag].valid = UNSOL_INVALID;
  1385. } else
  1386. icmd->ulpContext = (ushort) tag;
  1387. icmd->ulpTimeout = phba->fc_ratov * 2;
  1388. /* Xmit CT response on exchange <xid> */
  1389. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  1390. "2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
  1391. icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
  1392. ctiocb->iocb_cmpl = NULL;
  1393. ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
  1394. ctiocb->vport = phba->pport;
  1395. ctiocb->context1 = dd_data;
  1396. ctiocb->context2 = cmp;
  1397. ctiocb->context3 = bmp;
  1398. ctiocb->context_un.ndlp = ndlp;
  1399. ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
  1400. dd_data->type = TYPE_IOCB;
  1401. dd_data->set_job = job;
  1402. dd_data->context_un.iocb.cmdiocbq = ctiocb;
  1403. dd_data->context_un.iocb.ndlp = ndlp;
  1404. dd_data->context_un.iocb.rmp = NULL;
  1405. job->dd_data = dd_data;
  1406. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  1407. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  1408. rc = -IOCB_ERROR;
  1409. goto issue_ct_rsp_exit;
  1410. }
  1411. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  1412. writel(creg_val, phba->HCregaddr);
  1413. readl(phba->HCregaddr); /* flush */
  1414. }
  1415. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  1416. if (rc == IOCB_SUCCESS) {
  1417. spin_lock_irqsave(&phba->hbalock, flags);
  1418. /* make sure the I/O had not been completed/released */
  1419. if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
  1420. /* open up abort window to timeout handler */
  1421. ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
  1422. }
  1423. spin_unlock_irqrestore(&phba->hbalock, flags);
  1424. return 0; /* done for now */
  1425. }
  1426. /* iocb failed so cleanup */
  1427. job->dd_data = NULL;
  1428. issue_ct_rsp_exit:
  1429. lpfc_sli_release_iocbq(phba, ctiocb);
  1430. no_ctiocb:
  1431. kfree(dd_data);
  1432. no_dd_data:
  1433. return rc;
  1434. }
  1435. /**
  1436. * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
  1437. * @job: SEND_MGMT_RESP fc_bsg_job
  1438. **/
  1439. static int
  1440. lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
  1441. {
  1442. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  1443. struct lpfc_hba *phba = vport->phba;
  1444. struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
  1445. job->request->rqst_data.h_vendor.vendor_cmd;
  1446. struct ulp_bde64 *bpl;
  1447. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
  1448. int bpl_entries;
  1449. uint32_t tag = mgmt_resp->tag;
  1450. unsigned long reqbfrcnt =
  1451. (unsigned long)job->request_payload.payload_len;
  1452. int rc = 0;
  1453. /* in case no data is transferred */
  1454. job->reply->reply_payload_rcv_len = 0;
  1455. if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
  1456. rc = -ERANGE;
  1457. goto send_mgmt_rsp_exit;
  1458. }
  1459. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  1460. if (!bmp) {
  1461. rc = -ENOMEM;
  1462. goto send_mgmt_rsp_exit;
  1463. }
  1464. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  1465. if (!bmp->virt) {
  1466. rc = -ENOMEM;
  1467. goto send_mgmt_rsp_free_bmp;
  1468. }
  1469. INIT_LIST_HEAD(&bmp->list);
  1470. bpl = (struct ulp_bde64 *) bmp->virt;
  1471. bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
  1472. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  1473. 1, bpl, &bpl_entries);
  1474. if (!cmp) {
  1475. rc = -ENOMEM;
  1476. goto send_mgmt_rsp_free_bmp;
  1477. }
  1478. lpfc_bsg_copy_data(cmp, &job->request_payload,
  1479. job->request_payload.payload_len, 1);
  1480. rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
  1481. if (rc == IOCB_SUCCESS)
  1482. return 0; /* done for now */
  1483. rc = -EACCES;
  1484. lpfc_free_bsg_buffers(phba, cmp);
  1485. send_mgmt_rsp_free_bmp:
  1486. if (bmp->virt)
  1487. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  1488. kfree(bmp);
  1489. send_mgmt_rsp_exit:
  1490. /* make error code available to userspace */
  1491. job->reply->result = rc;
  1492. job->dd_data = NULL;
  1493. return rc;
  1494. }
  1495. /**
  1496. * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
  1497. * @phba: Pointer to HBA context object.
  1498. *
  1499. * This function is responsible for preparing driver for diag loopback
  1500. * on device.
  1501. */
  1502. static int
  1503. lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
  1504. {
  1505. struct lpfc_vport **vports;
  1506. struct Scsi_Host *shost;
  1507. struct lpfc_sli *psli;
  1508. struct lpfc_sli_ring *pring;
  1509. int i = 0;
  1510. psli = &phba->sli;
  1511. if (!psli)
  1512. return -ENODEV;
  1513. pring = &psli->ring[LPFC_FCP_RING];
  1514. if (!pring)
  1515. return -ENODEV;
  1516. if ((phba->link_state == LPFC_HBA_ERROR) ||
  1517. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  1518. (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
  1519. return -EACCES;
  1520. vports = lpfc_create_vport_work_array(phba);
  1521. if (vports) {
  1522. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1523. shost = lpfc_shost_from_vport(vports[i]);
  1524. scsi_block_requests(shost);
  1525. }
  1526. lpfc_destroy_vport_work_array(phba, vports);
  1527. } else {
  1528. shost = lpfc_shost_from_vport(phba->pport);
  1529. scsi_block_requests(shost);
  1530. }
  1531. while (!list_empty(&pring->txcmplq)) {
  1532. if (i++ > 500) /* wait up to 5 seconds */
  1533. break;
  1534. msleep(10);
  1535. }
  1536. return 0;
  1537. }
  1538. /**
  1539. * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
  1540. * @phba: Pointer to HBA context object.
  1541. *
  1542. * This function is responsible for driver exit processing of setting up
  1543. * diag loopback mode on device.
  1544. */
  1545. static void
  1546. lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
  1547. {
  1548. struct Scsi_Host *shost;
  1549. struct lpfc_vport **vports;
  1550. int i;
  1551. vports = lpfc_create_vport_work_array(phba);
  1552. if (vports) {
  1553. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1554. shost = lpfc_shost_from_vport(vports[i]);
  1555. scsi_unblock_requests(shost);
  1556. }
  1557. lpfc_destroy_vport_work_array(phba, vports);
  1558. } else {
  1559. shost = lpfc_shost_from_vport(phba->pport);
  1560. scsi_unblock_requests(shost);
  1561. }
  1562. return;
  1563. }
  1564. /**
  1565. * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
  1566. * @phba: Pointer to HBA context object.
  1567. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1568. *
  1569. * This function is responsible for placing an sli3 port into diagnostic
  1570. * loopback mode in order to perform a diagnostic loopback test.
  1571. * All new scsi requests are blocked, a small delay is used to allow the
  1572. * scsi requests to complete then the link is brought down. If the link is
  1573. * is placed in loopback mode then scsi requests are again allowed
  1574. * so the scsi mid-layer doesn't give up on the port.
  1575. * All of this is done in-line.
  1576. */
  1577. static int
  1578. lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
  1579. {
  1580. struct diag_mode_set *loopback_mode;
  1581. uint32_t link_flags;
  1582. uint32_t timeout;
  1583. LPFC_MBOXQ_t *pmboxq = NULL;
  1584. int mbxstatus = MBX_SUCCESS;
  1585. int i = 0;
  1586. int rc = 0;
  1587. /* no data to return just the return code */
  1588. job->reply->reply_payload_rcv_len = 0;
  1589. if (job->request_len < sizeof(struct fc_bsg_request) +
  1590. sizeof(struct diag_mode_set)) {
  1591. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1592. "2738 Received DIAG MODE request size:%d "
  1593. "below the minimum size:%d\n",
  1594. job->request_len,
  1595. (int)(sizeof(struct fc_bsg_request) +
  1596. sizeof(struct diag_mode_set)));
  1597. rc = -EINVAL;
  1598. goto job_error;
  1599. }
  1600. rc = lpfc_bsg_diag_mode_enter(phba);
  1601. if (rc)
  1602. goto job_error;
  1603. /* bring the link to diagnostic mode */
  1604. loopback_mode = (struct diag_mode_set *)
  1605. job->request->rqst_data.h_vendor.vendor_cmd;
  1606. link_flags = loopback_mode->type;
  1607. timeout = loopback_mode->timeout * 100;
  1608. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1609. if (!pmboxq) {
  1610. rc = -ENOMEM;
  1611. goto loopback_mode_exit;
  1612. }
  1613. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1614. pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
  1615. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1616. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1617. if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
  1618. /* wait for link down before proceeding */
  1619. i = 0;
  1620. while (phba->link_state != LPFC_LINK_DOWN) {
  1621. if (i++ > timeout) {
  1622. rc = -ETIMEDOUT;
  1623. goto loopback_mode_exit;
  1624. }
  1625. msleep(10);
  1626. }
  1627. memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  1628. if (link_flags == INTERNAL_LOOP_BACK)
  1629. pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
  1630. else
  1631. pmboxq->u.mb.un.varInitLnk.link_flags =
  1632. FLAGS_TOPOLOGY_MODE_LOOP;
  1633. pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
  1634. pmboxq->u.mb.mbxOwner = OWN_HOST;
  1635. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
  1636. LPFC_MBOX_TMO);
  1637. if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
  1638. rc = -ENODEV;
  1639. else {
  1640. spin_lock_irq(&phba->hbalock);
  1641. phba->link_flag |= LS_LOOPBACK_MODE;
  1642. spin_unlock_irq(&phba->hbalock);
  1643. /* wait for the link attention interrupt */
  1644. msleep(100);
  1645. i = 0;
  1646. while (phba->link_state != LPFC_HBA_READY) {
  1647. if (i++ > timeout) {
  1648. rc = -ETIMEDOUT;
  1649. break;
  1650. }
  1651. msleep(10);
  1652. }
  1653. }
  1654. } else
  1655. rc = -ENODEV;
  1656. loopback_mode_exit:
  1657. lpfc_bsg_diag_mode_exit(phba);
  1658. /*
  1659. * Let SLI layer release mboxq if mbox command completed after timeout.
  1660. */
  1661. if (pmboxq && mbxstatus != MBX_TIMEOUT)
  1662. mempool_free(pmboxq, phba->mbox_mem_pool);
  1663. job_error:
  1664. /* make error code available to userspace */
  1665. job->reply->result = rc;
  1666. /* complete the job back to userspace if no error */
  1667. if (rc == 0)
  1668. job->job_done(job);
  1669. return rc;
  1670. }
  1671. /**
  1672. * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
  1673. * @phba: Pointer to HBA context object.
  1674. * @diag: Flag for set link to diag or nomral operation state.
  1675. *
  1676. * This function is responsible for issuing a sli4 mailbox command for setting
  1677. * link to either diag state or normal operation state.
  1678. */
  1679. static int
  1680. lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
  1681. {
  1682. LPFC_MBOXQ_t *pmboxq;
  1683. struct lpfc_mbx_set_link_diag_state *link_diag_state;
  1684. uint32_t req_len, alloc_len;
  1685. int mbxstatus = MBX_SUCCESS, rc;
  1686. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1687. if (!pmboxq)
  1688. return -ENOMEM;
  1689. req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
  1690. sizeof(struct lpfc_sli4_cfg_mhdr));
  1691. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  1692. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
  1693. req_len, LPFC_SLI4_MBX_EMBED);
  1694. if (alloc_len != req_len) {
  1695. rc = -ENOMEM;
  1696. goto link_diag_state_set_out;
  1697. }
  1698. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1699. "3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
  1700. diag, phba->sli4_hba.lnk_info.lnk_tp,
  1701. phba->sli4_hba.lnk_info.lnk_no);
  1702. link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
  1703. bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
  1704. LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
  1705. bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
  1706. phba->sli4_hba.lnk_info.lnk_no);
  1707. bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
  1708. phba->sli4_hba.lnk_info.lnk_tp);
  1709. if (diag)
  1710. bf_set(lpfc_mbx_set_diag_state_diag,
  1711. &link_diag_state->u.req, 1);
  1712. else
  1713. bf_set(lpfc_mbx_set_diag_state_diag,
  1714. &link_diag_state->u.req, 0);
  1715. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1716. if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
  1717. rc = 0;
  1718. else
  1719. rc = -ENODEV;
  1720. link_diag_state_set_out:
  1721. if (pmboxq && (mbxstatus != MBX_TIMEOUT))
  1722. mempool_free(pmboxq, phba->mbox_mem_pool);
  1723. return rc;
  1724. }
  1725. /**
  1726. * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
  1727. * @phba: Pointer to HBA context object.
  1728. *
  1729. * This function is responsible for issuing a sli4 mailbox command for setting
  1730. * up internal loopback diagnostic.
  1731. */
  1732. static int
  1733. lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
  1734. {
  1735. LPFC_MBOXQ_t *pmboxq;
  1736. uint32_t req_len, alloc_len;
  1737. struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
  1738. int mbxstatus = MBX_SUCCESS, rc = 0;
  1739. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1740. if (!pmboxq)
  1741. return -ENOMEM;
  1742. req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
  1743. sizeof(struct lpfc_sli4_cfg_mhdr));
  1744. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  1745. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
  1746. req_len, LPFC_SLI4_MBX_EMBED);
  1747. if (alloc_len != req_len) {
  1748. mempool_free(pmboxq, phba->mbox_mem_pool);
  1749. return -ENOMEM;
  1750. }
  1751. link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
  1752. bf_set(lpfc_mbx_set_diag_state_link_num,
  1753. &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
  1754. bf_set(lpfc_mbx_set_diag_state_link_type,
  1755. &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
  1756. bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
  1757. LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
  1758. mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
  1759. if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
  1760. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1761. "3127 Failed setup loopback mode mailbox "
  1762. "command, rc:x%x, status:x%x\n", mbxstatus,
  1763. pmboxq->u.mb.mbxStatus);
  1764. rc = -ENODEV;
  1765. }
  1766. if (pmboxq && (mbxstatus != MBX_TIMEOUT))
  1767. mempool_free(pmboxq, phba->mbox_mem_pool);
  1768. return rc;
  1769. }
  1770. /**
  1771. * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
  1772. * @phba: Pointer to HBA context object.
  1773. *
  1774. * This function set up SLI4 FC port registrations for diagnostic run, which
  1775. * includes all the rpis, vfi, and also vpi.
  1776. */
  1777. static int
  1778. lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
  1779. {
  1780. int rc;
  1781. if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
  1782. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1783. "3136 Port still had vfi registered: "
  1784. "mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
  1785. phba->pport->fc_myDID, phba->fcf.fcfi,
  1786. phba->sli4_hba.vfi_ids[phba->pport->vfi],
  1787. phba->vpi_ids[phba->pport->vpi]);
  1788. return -EINVAL;
  1789. }
  1790. rc = lpfc_issue_reg_vfi(phba->pport);
  1791. return rc;
  1792. }
  1793. /**
  1794. * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
  1795. * @phba: Pointer to HBA context object.
  1796. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1797. *
  1798. * This function is responsible for placing an sli4 port into diagnostic
  1799. * loopback mode in order to perform a diagnostic loopback test.
  1800. */
  1801. static int
  1802. lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
  1803. {
  1804. struct diag_mode_set *loopback_mode;
  1805. uint32_t link_flags, timeout;
  1806. int i, rc = 0;
  1807. /* no data to return just the return code */
  1808. job->reply->reply_payload_rcv_len = 0;
  1809. if (job->request_len < sizeof(struct fc_bsg_request) +
  1810. sizeof(struct diag_mode_set)) {
  1811. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1812. "3011 Received DIAG MODE request size:%d "
  1813. "below the minimum size:%d\n",
  1814. job->request_len,
  1815. (int)(sizeof(struct fc_bsg_request) +
  1816. sizeof(struct diag_mode_set)));
  1817. rc = -EINVAL;
  1818. goto job_error;
  1819. }
  1820. rc = lpfc_bsg_diag_mode_enter(phba);
  1821. if (rc)
  1822. goto job_error;
  1823. /* indicate we are in loobpack diagnostic mode */
  1824. spin_lock_irq(&phba->hbalock);
  1825. phba->link_flag |= LS_LOOPBACK_MODE;
  1826. spin_unlock_irq(&phba->hbalock);
  1827. /* reset port to start frome scratch */
  1828. rc = lpfc_selective_reset(phba);
  1829. if (rc)
  1830. goto job_error;
  1831. /* bring the link to diagnostic mode */
  1832. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1833. "3129 Bring link to diagnostic state.\n");
  1834. loopback_mode = (struct diag_mode_set *)
  1835. job->request->rqst_data.h_vendor.vendor_cmd;
  1836. link_flags = loopback_mode->type;
  1837. timeout = loopback_mode->timeout * 100;
  1838. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
  1839. if (rc) {
  1840. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  1841. "3130 Failed to bring link to diagnostic "
  1842. "state, rc:x%x\n", rc);
  1843. goto loopback_mode_exit;
  1844. }
  1845. /* wait for link down before proceeding */
  1846. i = 0;
  1847. while (phba->link_state != LPFC_LINK_DOWN) {
  1848. if (i++ > timeout) {
  1849. rc = -ETIMEDOUT;
  1850. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1851. "3131 Timeout waiting for link to "
  1852. "diagnostic mode, timeout:%d ms\n",
  1853. timeout * 10);
  1854. goto loopback_mode_exit;
  1855. }
  1856. msleep(10);
  1857. }
  1858. /* set up loopback mode */
  1859. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1860. "3132 Set up loopback mode:x%x\n", link_flags);
  1861. if (link_flags == INTERNAL_LOOP_BACK)
  1862. rc = lpfc_sli4_bsg_set_internal_loopback(phba);
  1863. else if (link_flags == EXTERNAL_LOOP_BACK)
  1864. rc = lpfc_hba_init_link_fc_topology(phba,
  1865. FLAGS_TOPOLOGY_MODE_PT_PT,
  1866. MBX_NOWAIT);
  1867. else {
  1868. rc = -EINVAL;
  1869. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  1870. "3141 Loopback mode:x%x not supported\n",
  1871. link_flags);
  1872. goto loopback_mode_exit;
  1873. }
  1874. if (!rc) {
  1875. /* wait for the link attention interrupt */
  1876. msleep(100);
  1877. i = 0;
  1878. while (phba->link_state < LPFC_LINK_UP) {
  1879. if (i++ > timeout) {
  1880. rc = -ETIMEDOUT;
  1881. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1882. "3137 Timeout waiting for link up "
  1883. "in loopback mode, timeout:%d ms\n",
  1884. timeout * 10);
  1885. break;
  1886. }
  1887. msleep(10);
  1888. }
  1889. }
  1890. /* port resource registration setup for loopback diagnostic */
  1891. if (!rc) {
  1892. /* set up a none zero myDID for loopback test */
  1893. phba->pport->fc_myDID = 1;
  1894. rc = lpfc_sli4_diag_fcport_reg_setup(phba);
  1895. } else
  1896. goto loopback_mode_exit;
  1897. if (!rc) {
  1898. /* wait for the port ready */
  1899. msleep(100);
  1900. i = 0;
  1901. while (phba->link_state != LPFC_HBA_READY) {
  1902. if (i++ > timeout) {
  1903. rc = -ETIMEDOUT;
  1904. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  1905. "3133 Timeout waiting for port "
  1906. "loopback mode ready, timeout:%d ms\n",
  1907. timeout * 10);
  1908. break;
  1909. }
  1910. msleep(10);
  1911. }
  1912. }
  1913. loopback_mode_exit:
  1914. /* clear loopback diagnostic mode */
  1915. if (rc) {
  1916. spin_lock_irq(&phba->hbalock);
  1917. phba->link_flag &= ~LS_LOOPBACK_MODE;
  1918. spin_unlock_irq(&phba->hbalock);
  1919. }
  1920. lpfc_bsg_diag_mode_exit(phba);
  1921. job_error:
  1922. /* make error code available to userspace */
  1923. job->reply->result = rc;
  1924. /* complete the job back to userspace if no error */
  1925. if (rc == 0)
  1926. job->job_done(job);
  1927. return rc;
  1928. }
  1929. /**
  1930. * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
  1931. * @job: LPFC_BSG_VENDOR_DIAG_MODE
  1932. *
  1933. * This function is responsible for responding to check and dispatch bsg diag
  1934. * command from the user to proper driver action routines.
  1935. */
  1936. static int
  1937. lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
  1938. {
  1939. struct Scsi_Host *shost;
  1940. struct lpfc_vport *vport;
  1941. struct lpfc_hba *phba;
  1942. int rc;
  1943. shost = job->shost;
  1944. if (!shost)
  1945. return -ENODEV;
  1946. vport = (struct lpfc_vport *)job->shost->hostdata;
  1947. if (!vport)
  1948. return -ENODEV;
  1949. phba = vport->phba;
  1950. if (!phba)
  1951. return -ENODEV;
  1952. if (phba->sli_rev < LPFC_SLI_REV4)
  1953. rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
  1954. else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  1955. LPFC_SLI_INTF_IF_TYPE_2)
  1956. rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
  1957. else
  1958. rc = -ENODEV;
  1959. return rc;
  1960. }
  1961. /**
  1962. * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
  1963. * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
  1964. *
  1965. * This function is responsible for responding to check and dispatch bsg diag
  1966. * command from the user to proper driver action routines.
  1967. */
  1968. static int
  1969. lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
  1970. {
  1971. struct Scsi_Host *shost;
  1972. struct lpfc_vport *vport;
  1973. struct lpfc_hba *phba;
  1974. struct diag_mode_set *loopback_mode_end_cmd;
  1975. uint32_t timeout;
  1976. int rc, i;
  1977. shost = job->shost;
  1978. if (!shost)
  1979. return -ENODEV;
  1980. vport = (struct lpfc_vport *)job->shost->hostdata;
  1981. if (!vport)
  1982. return -ENODEV;
  1983. phba = vport->phba;
  1984. if (!phba)
  1985. return -ENODEV;
  1986. if (phba->sli_rev < LPFC_SLI_REV4)
  1987. return -ENODEV;
  1988. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  1989. LPFC_SLI_INTF_IF_TYPE_2)
  1990. return -ENODEV;
  1991. /* clear loopback diagnostic mode */
  1992. spin_lock_irq(&phba->hbalock);
  1993. phba->link_flag &= ~LS_LOOPBACK_MODE;
  1994. spin_unlock_irq(&phba->hbalock);
  1995. loopback_mode_end_cmd = (struct diag_mode_set *)
  1996. job->request->rqst_data.h_vendor.vendor_cmd;
  1997. timeout = loopback_mode_end_cmd->timeout * 100;
  1998. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
  1999. if (rc) {
  2000. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2001. "3139 Failed to bring link to diagnostic "
  2002. "state, rc:x%x\n", rc);
  2003. goto loopback_mode_end_exit;
  2004. }
  2005. /* wait for link down before proceeding */
  2006. i = 0;
  2007. while (phba->link_state != LPFC_LINK_DOWN) {
  2008. if (i++ > timeout) {
  2009. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  2010. "3140 Timeout waiting for link to "
  2011. "diagnostic mode_end, timeout:%d ms\n",
  2012. timeout * 10);
  2013. /* there is nothing much we can do here */
  2014. break;
  2015. }
  2016. msleep(10);
  2017. }
  2018. /* reset port resource registrations */
  2019. rc = lpfc_selective_reset(phba);
  2020. phba->pport->fc_myDID = 0;
  2021. loopback_mode_end_exit:
  2022. /* make return code available to userspace */
  2023. job->reply->result = rc;
  2024. /* complete the job back to userspace if no error */
  2025. if (rc == 0)
  2026. job->job_done(job);
  2027. return rc;
  2028. }
  2029. /**
  2030. * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
  2031. * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
  2032. *
  2033. * This function is to perform SLI4 diag link test request from the user
  2034. * applicaiton.
  2035. */
  2036. static int
  2037. lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
  2038. {
  2039. struct Scsi_Host *shost;
  2040. struct lpfc_vport *vport;
  2041. struct lpfc_hba *phba;
  2042. LPFC_MBOXQ_t *pmboxq;
  2043. struct sli4_link_diag *link_diag_test_cmd;
  2044. uint32_t req_len, alloc_len;
  2045. struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
  2046. union lpfc_sli4_cfg_shdr *shdr;
  2047. uint32_t shdr_status, shdr_add_status;
  2048. struct diag_status *diag_status_reply;
  2049. int mbxstatus, rc = 0;
  2050. shost = job->shost;
  2051. if (!shost) {
  2052. rc = -ENODEV;
  2053. goto job_error;
  2054. }
  2055. vport = (struct lpfc_vport *)job->shost->hostdata;
  2056. if (!vport) {
  2057. rc = -ENODEV;
  2058. goto job_error;
  2059. }
  2060. phba = vport->phba;
  2061. if (!phba) {
  2062. rc = -ENODEV;
  2063. goto job_error;
  2064. }
  2065. if (phba->sli_rev < LPFC_SLI_REV4) {
  2066. rc = -ENODEV;
  2067. goto job_error;
  2068. }
  2069. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  2070. LPFC_SLI_INTF_IF_TYPE_2) {
  2071. rc = -ENODEV;
  2072. goto job_error;
  2073. }
  2074. if (job->request_len < sizeof(struct fc_bsg_request) +
  2075. sizeof(struct sli4_link_diag)) {
  2076. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2077. "3013 Received LINK DIAG TEST request "
  2078. " size:%d below the minimum size:%d\n",
  2079. job->request_len,
  2080. (int)(sizeof(struct fc_bsg_request) +
  2081. sizeof(struct sli4_link_diag)));
  2082. rc = -EINVAL;
  2083. goto job_error;
  2084. }
  2085. rc = lpfc_bsg_diag_mode_enter(phba);
  2086. if (rc)
  2087. goto job_error;
  2088. link_diag_test_cmd = (struct sli4_link_diag *)
  2089. job->request->rqst_data.h_vendor.vendor_cmd;
  2090. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
  2091. if (rc)
  2092. goto job_error;
  2093. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2094. if (!pmboxq) {
  2095. rc = -ENOMEM;
  2096. goto link_diag_test_exit;
  2097. }
  2098. req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
  2099. sizeof(struct lpfc_sli4_cfg_mhdr));
  2100. alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  2101. LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
  2102. req_len, LPFC_SLI4_MBX_EMBED);
  2103. if (alloc_len != req_len) {
  2104. rc = -ENOMEM;
  2105. goto link_diag_test_exit;
  2106. }
  2107. run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
  2108. bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
  2109. phba->sli4_hba.lnk_info.lnk_no);
  2110. bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
  2111. phba->sli4_hba.lnk_info.lnk_tp);
  2112. bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
  2113. link_diag_test_cmd->test_id);
  2114. bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
  2115. link_diag_test_cmd->loops);
  2116. bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
  2117. link_diag_test_cmd->test_version);
  2118. bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
  2119. link_diag_test_cmd->error_action);
  2120. mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
  2121. shdr = (union lpfc_sli4_cfg_shdr *)
  2122. &pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
  2123. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  2124. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  2125. if (shdr_status || shdr_add_status || mbxstatus) {
  2126. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2127. "3010 Run link diag test mailbox failed with "
  2128. "mbx_status x%x status x%x, add_status x%x\n",
  2129. mbxstatus, shdr_status, shdr_add_status);
  2130. }
  2131. diag_status_reply = (struct diag_status *)
  2132. job->reply->reply_data.vendor_reply.vendor_rsp;
  2133. if (job->reply_len <
  2134. sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
  2135. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2136. "3012 Received Run link diag test reply "
  2137. "below minimum size (%d): reply_len:%d\n",
  2138. (int)(sizeof(struct fc_bsg_request) +
  2139. sizeof(struct diag_status)),
  2140. job->reply_len);
  2141. rc = -EINVAL;
  2142. goto job_error;
  2143. }
  2144. diag_status_reply->mbox_status = mbxstatus;
  2145. diag_status_reply->shdr_status = shdr_status;
  2146. diag_status_reply->shdr_add_status = shdr_add_status;
  2147. link_diag_test_exit:
  2148. rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
  2149. if (pmboxq)
  2150. mempool_free(pmboxq, phba->mbox_mem_pool);
  2151. lpfc_bsg_diag_mode_exit(phba);
  2152. job_error:
  2153. /* make error code available to userspace */
  2154. job->reply->result = rc;
  2155. /* complete the job back to userspace if no error */
  2156. if (rc == 0)
  2157. job->job_done(job);
  2158. return rc;
  2159. }
  2160. /**
  2161. * lpfcdiag_loop_self_reg - obtains a remote port login id
  2162. * @phba: Pointer to HBA context object
  2163. * @rpi: Pointer to a remote port login id
  2164. *
  2165. * This function obtains a remote port login id so the diag loopback test
  2166. * can send and receive its own unsolicited CT command.
  2167. **/
  2168. static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
  2169. {
  2170. LPFC_MBOXQ_t *mbox;
  2171. struct lpfc_dmabuf *dmabuff;
  2172. int status;
  2173. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2174. if (!mbox)
  2175. return -ENOMEM;
  2176. if (phba->sli_rev < LPFC_SLI_REV4)
  2177. status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
  2178. (uint8_t *)&phba->pport->fc_sparam,
  2179. mbox, *rpi);
  2180. else {
  2181. *rpi = lpfc_sli4_alloc_rpi(phba);
  2182. status = lpfc_reg_rpi(phba, phba->pport->vpi,
  2183. phba->pport->fc_myDID,
  2184. (uint8_t *)&phba->pport->fc_sparam,
  2185. mbox, *rpi);
  2186. }
  2187. if (status) {
  2188. mempool_free(mbox, phba->mbox_mem_pool);
  2189. if (phba->sli_rev == LPFC_SLI_REV4)
  2190. lpfc_sli4_free_rpi(phba, *rpi);
  2191. return -ENOMEM;
  2192. }
  2193. dmabuff = (struct lpfc_dmabuf *) mbox->context1;
  2194. mbox->context1 = NULL;
  2195. mbox->context2 = NULL;
  2196. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2197. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  2198. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  2199. kfree(dmabuff);
  2200. if (status != MBX_TIMEOUT)
  2201. mempool_free(mbox, phba->mbox_mem_pool);
  2202. if (phba->sli_rev == LPFC_SLI_REV4)
  2203. lpfc_sli4_free_rpi(phba, *rpi);
  2204. return -ENODEV;
  2205. }
  2206. if (phba->sli_rev < LPFC_SLI_REV4)
  2207. *rpi = mbox->u.mb.un.varWords[0];
  2208. lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
  2209. kfree(dmabuff);
  2210. mempool_free(mbox, phba->mbox_mem_pool);
  2211. return 0;
  2212. }
  2213. /**
  2214. * lpfcdiag_loop_self_unreg - unregs from the rpi
  2215. * @phba: Pointer to HBA context object
  2216. * @rpi: Remote port login id
  2217. *
  2218. * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
  2219. **/
  2220. static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
  2221. {
  2222. LPFC_MBOXQ_t *mbox;
  2223. int status;
  2224. /* Allocate mboxq structure */
  2225. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2226. if (mbox == NULL)
  2227. return -ENOMEM;
  2228. if (phba->sli_rev < LPFC_SLI_REV4)
  2229. lpfc_unreg_login(phba, 0, rpi, mbox);
  2230. else
  2231. lpfc_unreg_login(phba, phba->pport->vpi,
  2232. phba->sli4_hba.rpi_ids[rpi], mbox);
  2233. status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2234. if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
  2235. if (status != MBX_TIMEOUT)
  2236. mempool_free(mbox, phba->mbox_mem_pool);
  2237. return -EIO;
  2238. }
  2239. mempool_free(mbox, phba->mbox_mem_pool);
  2240. if (phba->sli_rev == LPFC_SLI_REV4)
  2241. lpfc_sli4_free_rpi(phba, rpi);
  2242. return 0;
  2243. }
  2244. /**
  2245. * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
  2246. * @phba: Pointer to HBA context object
  2247. * @rpi: Remote port login id
  2248. * @txxri: Pointer to transmit exchange id
  2249. * @rxxri: Pointer to response exchabge id
  2250. *
  2251. * This function obtains the transmit and receive ids required to send
  2252. * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
  2253. * flags are used to the unsolicted response handler is able to process
  2254. * the ct command sent on the same port.
  2255. **/
  2256. static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
  2257. uint16_t *txxri, uint16_t * rxxri)
  2258. {
  2259. struct lpfc_bsg_event *evt;
  2260. struct lpfc_iocbq *cmdiocbq, *rspiocbq;
  2261. IOCB_t *cmd, *rsp;
  2262. struct lpfc_dmabuf *dmabuf;
  2263. struct ulp_bde64 *bpl = NULL;
  2264. struct lpfc_sli_ct_request *ctreq = NULL;
  2265. int ret_val = 0;
  2266. int time_left;
  2267. int iocb_stat = IOCB_SUCCESS;
  2268. unsigned long flags;
  2269. *txxri = 0;
  2270. *rxxri = 0;
  2271. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  2272. SLI_CT_ELX_LOOPBACK);
  2273. if (!evt)
  2274. return -ENOMEM;
  2275. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2276. list_add(&evt->node, &phba->ct_ev_waiters);
  2277. lpfc_bsg_event_ref(evt);
  2278. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2279. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2280. rspiocbq = lpfc_sli_get_iocbq(phba);
  2281. dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2282. if (dmabuf) {
  2283. dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
  2284. if (dmabuf->virt) {
  2285. INIT_LIST_HEAD(&dmabuf->list);
  2286. bpl = (struct ulp_bde64 *) dmabuf->virt;
  2287. memset(bpl, 0, sizeof(*bpl));
  2288. ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
  2289. bpl->addrHigh =
  2290. le32_to_cpu(putPaddrHigh(dmabuf->phys +
  2291. sizeof(*bpl)));
  2292. bpl->addrLow =
  2293. le32_to_cpu(putPaddrLow(dmabuf->phys +
  2294. sizeof(*bpl)));
  2295. bpl->tus.f.bdeFlags = 0;
  2296. bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
  2297. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  2298. }
  2299. }
  2300. if (cmdiocbq == NULL || rspiocbq == NULL ||
  2301. dmabuf == NULL || bpl == NULL || ctreq == NULL ||
  2302. dmabuf->virt == NULL) {
  2303. ret_val = -ENOMEM;
  2304. goto err_get_xri_exit;
  2305. }
  2306. cmd = &cmdiocbq->iocb;
  2307. rsp = &rspiocbq->iocb;
  2308. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  2309. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  2310. ctreq->RevisionId.bits.InId = 0;
  2311. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  2312. ctreq->FsSubType = 0;
  2313. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
  2314. ctreq->CommandResponse.bits.Size = 0;
  2315. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
  2316. cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
  2317. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  2318. cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
  2319. cmd->un.xseq64.w5.hcsw.Fctl = LA;
  2320. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  2321. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  2322. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  2323. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
  2324. cmd->ulpBdeCount = 1;
  2325. cmd->ulpLe = 1;
  2326. cmd->ulpClass = CLASS3;
  2327. cmd->ulpContext = rpi;
  2328. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  2329. cmdiocbq->vport = phba->pport;
  2330. cmdiocbq->iocb_cmpl = NULL;
  2331. iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  2332. rspiocbq,
  2333. (phba->fc_ratov * 2)
  2334. + LPFC_DRVR_TIMEOUT);
  2335. if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
  2336. ret_val = -EIO;
  2337. goto err_get_xri_exit;
  2338. }
  2339. *txxri = rsp->ulpContext;
  2340. evt->waiting = 1;
  2341. evt->wait_time_stamp = jiffies;
  2342. time_left = wait_event_interruptible_timeout(
  2343. evt->wq, !list_empty(&evt->events_to_see),
  2344. msecs_to_jiffies(1000 *
  2345. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
  2346. if (list_empty(&evt->events_to_see))
  2347. ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
  2348. else {
  2349. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2350. list_move(evt->events_to_see.prev, &evt->events_to_get);
  2351. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2352. *rxxri = (list_entry(evt->events_to_get.prev,
  2353. typeof(struct event_data),
  2354. node))->immed_dat;
  2355. }
  2356. evt->waiting = 0;
  2357. err_get_xri_exit:
  2358. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2359. lpfc_bsg_event_unref(evt); /* release ref */
  2360. lpfc_bsg_event_unref(evt); /* delete */
  2361. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2362. if (dmabuf) {
  2363. if (dmabuf->virt)
  2364. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  2365. kfree(dmabuf);
  2366. }
  2367. if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
  2368. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2369. if (rspiocbq)
  2370. lpfc_sli_release_iocbq(phba, rspiocbq);
  2371. return ret_val;
  2372. }
  2373. /**
  2374. * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
  2375. * @phba: Pointer to HBA context object
  2376. *
  2377. * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
  2378. * returns the pointer to the buffer.
  2379. **/
  2380. static struct lpfc_dmabuf *
  2381. lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
  2382. {
  2383. struct lpfc_dmabuf *dmabuf;
  2384. struct pci_dev *pcidev = phba->pcidev;
  2385. /* allocate dma buffer struct */
  2386. dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2387. if (!dmabuf)
  2388. return NULL;
  2389. INIT_LIST_HEAD(&dmabuf->list);
  2390. /* now, allocate dma buffer */
  2391. dmabuf->virt = dma_zalloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
  2392. &(dmabuf->phys), GFP_KERNEL);
  2393. if (!dmabuf->virt) {
  2394. kfree(dmabuf);
  2395. return NULL;
  2396. }
  2397. return dmabuf;
  2398. }
  2399. /**
  2400. * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
  2401. * @phba: Pointer to HBA context object.
  2402. * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
  2403. *
  2404. * This routine just simply frees a dma buffer and its associated buffer
  2405. * descriptor referred by @dmabuf.
  2406. **/
  2407. static void
  2408. lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
  2409. {
  2410. struct pci_dev *pcidev = phba->pcidev;
  2411. if (!dmabuf)
  2412. return;
  2413. if (dmabuf->virt)
  2414. dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
  2415. dmabuf->virt, dmabuf->phys);
  2416. kfree(dmabuf);
  2417. return;
  2418. }
  2419. /**
  2420. * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
  2421. * @phba: Pointer to HBA context object.
  2422. * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
  2423. *
  2424. * This routine just simply frees all dma buffers and their associated buffer
  2425. * descriptors referred by @dmabuf_list.
  2426. **/
  2427. static void
  2428. lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
  2429. struct list_head *dmabuf_list)
  2430. {
  2431. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  2432. if (list_empty(dmabuf_list))
  2433. return;
  2434. list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
  2435. list_del_init(&dmabuf->list);
  2436. lpfc_bsg_dma_page_free(phba, dmabuf);
  2437. }
  2438. return;
  2439. }
  2440. /**
  2441. * diag_cmd_data_alloc - fills in a bde struct with dma buffers
  2442. * @phba: Pointer to HBA context object
  2443. * @bpl: Pointer to 64 bit bde structure
  2444. * @size: Number of bytes to process
  2445. * @nocopydata: Flag to copy user data into the allocated buffer
  2446. *
  2447. * This function allocates page size buffers and populates an lpfc_dmabufext.
  2448. * If allowed the user data pointed to with indataptr is copied into the kernel
  2449. * memory. The chained list of page size buffers is returned.
  2450. **/
  2451. static struct lpfc_dmabufext *
  2452. diag_cmd_data_alloc(struct lpfc_hba *phba,
  2453. struct ulp_bde64 *bpl, uint32_t size,
  2454. int nocopydata)
  2455. {
  2456. struct lpfc_dmabufext *mlist = NULL;
  2457. struct lpfc_dmabufext *dmp;
  2458. int cnt, offset = 0, i = 0;
  2459. struct pci_dev *pcidev;
  2460. pcidev = phba->pcidev;
  2461. while (size) {
  2462. /* We get chunks of 4K */
  2463. if (size > BUF_SZ_4K)
  2464. cnt = BUF_SZ_4K;
  2465. else
  2466. cnt = size;
  2467. /* allocate struct lpfc_dmabufext buffer header */
  2468. dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
  2469. if (!dmp)
  2470. goto out;
  2471. INIT_LIST_HEAD(&dmp->dma.list);
  2472. /* Queue it to a linked list */
  2473. if (mlist)
  2474. list_add_tail(&dmp->dma.list, &mlist->dma.list);
  2475. else
  2476. mlist = dmp;
  2477. /* allocate buffer */
  2478. dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
  2479. cnt,
  2480. &(dmp->dma.phys),
  2481. GFP_KERNEL);
  2482. if (!dmp->dma.virt)
  2483. goto out;
  2484. dmp->size = cnt;
  2485. if (nocopydata) {
  2486. bpl->tus.f.bdeFlags = 0;
  2487. pci_dma_sync_single_for_device(phba->pcidev,
  2488. dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
  2489. } else {
  2490. memset((uint8_t *)dmp->dma.virt, 0, cnt);
  2491. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  2492. }
  2493. /* build buffer ptr list for IOCB */
  2494. bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
  2495. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
  2496. bpl->tus.f.bdeSize = (ushort) cnt;
  2497. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  2498. bpl++;
  2499. i++;
  2500. offset += cnt;
  2501. size -= cnt;
  2502. }
  2503. if (mlist) {
  2504. mlist->flag = i;
  2505. return mlist;
  2506. }
  2507. out:
  2508. diag_cmd_data_free(phba, mlist);
  2509. return NULL;
  2510. }
  2511. /**
  2512. * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
  2513. * @phba: Pointer to HBA context object
  2514. * @rxxri: Receive exchange id
  2515. * @len: Number of data bytes
  2516. *
  2517. * This function allocates and posts a data buffer of sufficient size to receive
  2518. * an unsolicted CT command.
  2519. **/
  2520. static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
  2521. size_t len)
  2522. {
  2523. struct lpfc_sli *psli = &phba->sli;
  2524. struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
  2525. struct lpfc_iocbq *cmdiocbq;
  2526. IOCB_t *cmd = NULL;
  2527. struct list_head head, *curr, *next;
  2528. struct lpfc_dmabuf *rxbmp;
  2529. struct lpfc_dmabuf *dmp;
  2530. struct lpfc_dmabuf *mp[2] = {NULL, NULL};
  2531. struct ulp_bde64 *rxbpl = NULL;
  2532. uint32_t num_bde;
  2533. struct lpfc_dmabufext *rxbuffer = NULL;
  2534. int ret_val = 0;
  2535. int iocb_stat;
  2536. int i = 0;
  2537. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2538. rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2539. if (rxbmp != NULL) {
  2540. rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
  2541. if (rxbmp->virt) {
  2542. INIT_LIST_HEAD(&rxbmp->list);
  2543. rxbpl = (struct ulp_bde64 *) rxbmp->virt;
  2544. rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
  2545. }
  2546. }
  2547. if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
  2548. ret_val = -ENOMEM;
  2549. goto err_post_rxbufs_exit;
  2550. }
  2551. /* Queue buffers for the receive exchange */
  2552. num_bde = (uint32_t)rxbuffer->flag;
  2553. dmp = &rxbuffer->dma;
  2554. cmd = &cmdiocbq->iocb;
  2555. i = 0;
  2556. INIT_LIST_HEAD(&head);
  2557. list_add_tail(&head, &dmp->list);
  2558. list_for_each_safe(curr, next, &head) {
  2559. mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
  2560. list_del(curr);
  2561. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2562. mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
  2563. cmd->un.quexri64cx.buff.bde.addrHigh =
  2564. putPaddrHigh(mp[i]->phys);
  2565. cmd->un.quexri64cx.buff.bde.addrLow =
  2566. putPaddrLow(mp[i]->phys);
  2567. cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
  2568. ((struct lpfc_dmabufext *)mp[i])->size;
  2569. cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
  2570. cmd->ulpCommand = CMD_QUE_XRI64_CX;
  2571. cmd->ulpPU = 0;
  2572. cmd->ulpLe = 1;
  2573. cmd->ulpBdeCount = 1;
  2574. cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
  2575. } else {
  2576. cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
  2577. cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
  2578. cmd->un.cont64[i].tus.f.bdeSize =
  2579. ((struct lpfc_dmabufext *)mp[i])->size;
  2580. cmd->ulpBdeCount = ++i;
  2581. if ((--num_bde > 0) && (i < 2))
  2582. continue;
  2583. cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
  2584. cmd->ulpLe = 1;
  2585. }
  2586. cmd->ulpClass = CLASS3;
  2587. cmd->ulpContext = rxxri;
  2588. iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
  2589. 0);
  2590. if (iocb_stat == IOCB_ERROR) {
  2591. diag_cmd_data_free(phba,
  2592. (struct lpfc_dmabufext *)mp[0]);
  2593. if (mp[1])
  2594. diag_cmd_data_free(phba,
  2595. (struct lpfc_dmabufext *)mp[1]);
  2596. dmp = list_entry(next, struct lpfc_dmabuf, list);
  2597. ret_val = -EIO;
  2598. goto err_post_rxbufs_exit;
  2599. }
  2600. lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
  2601. if (mp[1]) {
  2602. lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
  2603. mp[1] = NULL;
  2604. }
  2605. /* The iocb was freed by lpfc_sli_issue_iocb */
  2606. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2607. if (!cmdiocbq) {
  2608. dmp = list_entry(next, struct lpfc_dmabuf, list);
  2609. ret_val = -EIO;
  2610. goto err_post_rxbufs_exit;
  2611. }
  2612. cmd = &cmdiocbq->iocb;
  2613. i = 0;
  2614. }
  2615. list_del(&head);
  2616. err_post_rxbufs_exit:
  2617. if (rxbmp) {
  2618. if (rxbmp->virt)
  2619. lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
  2620. kfree(rxbmp);
  2621. }
  2622. if (cmdiocbq)
  2623. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2624. return ret_val;
  2625. }
  2626. /**
  2627. * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
  2628. * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
  2629. *
  2630. * This function receives a user data buffer to be transmitted and received on
  2631. * the same port, the link must be up and in loopback mode prior
  2632. * to being called.
  2633. * 1. A kernel buffer is allocated to copy the user data into.
  2634. * 2. The port registers with "itself".
  2635. * 3. The transmit and receive exchange ids are obtained.
  2636. * 4. The receive exchange id is posted.
  2637. * 5. A new els loopback event is created.
  2638. * 6. The command and response iocbs are allocated.
  2639. * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
  2640. *
  2641. * This function is meant to be called n times while the port is in loopback
  2642. * so it is the apps responsibility to issue a reset to take the port out
  2643. * of loopback mode.
  2644. **/
  2645. static int
  2646. lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
  2647. {
  2648. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2649. struct lpfc_hba *phba = vport->phba;
  2650. struct diag_mode_test *diag_mode;
  2651. struct lpfc_bsg_event *evt;
  2652. struct event_data *evdat;
  2653. struct lpfc_sli *psli = &phba->sli;
  2654. uint32_t size;
  2655. uint32_t full_size;
  2656. size_t segment_len = 0, segment_offset = 0, current_offset = 0;
  2657. uint16_t rpi = 0;
  2658. struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
  2659. IOCB_t *cmd, *rsp = NULL;
  2660. struct lpfc_sli_ct_request *ctreq;
  2661. struct lpfc_dmabuf *txbmp;
  2662. struct ulp_bde64 *txbpl = NULL;
  2663. struct lpfc_dmabufext *txbuffer = NULL;
  2664. struct list_head head;
  2665. struct lpfc_dmabuf *curr;
  2666. uint16_t txxri = 0, rxxri;
  2667. uint32_t num_bde;
  2668. uint8_t *ptr = NULL, *rx_databuf = NULL;
  2669. int rc = 0;
  2670. int time_left;
  2671. int iocb_stat = IOCB_SUCCESS;
  2672. unsigned long flags;
  2673. void *dataout = NULL;
  2674. uint32_t total_mem;
  2675. /* in case no data is returned return just the return code */
  2676. job->reply->reply_payload_rcv_len = 0;
  2677. if (job->request_len <
  2678. sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
  2679. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2680. "2739 Received DIAG TEST request below minimum "
  2681. "size\n");
  2682. rc = -EINVAL;
  2683. goto loopback_test_exit;
  2684. }
  2685. if (job->request_payload.payload_len !=
  2686. job->reply_payload.payload_len) {
  2687. rc = -EINVAL;
  2688. goto loopback_test_exit;
  2689. }
  2690. diag_mode = (struct diag_mode_test *)
  2691. job->request->rqst_data.h_vendor.vendor_cmd;
  2692. if ((phba->link_state == LPFC_HBA_ERROR) ||
  2693. (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
  2694. (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
  2695. rc = -EACCES;
  2696. goto loopback_test_exit;
  2697. }
  2698. if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
  2699. rc = -EACCES;
  2700. goto loopback_test_exit;
  2701. }
  2702. size = job->request_payload.payload_len;
  2703. full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
  2704. if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
  2705. rc = -ERANGE;
  2706. goto loopback_test_exit;
  2707. }
  2708. if (full_size >= BUF_SZ_4K) {
  2709. /*
  2710. * Allocate memory for ioctl data. If buffer is bigger than 64k,
  2711. * then we allocate 64k and re-use that buffer over and over to
  2712. * xfer the whole block. This is because Linux kernel has a
  2713. * problem allocating more than 120k of kernel space memory. Saw
  2714. * problem with GET_FCPTARGETMAPPING...
  2715. */
  2716. if (size <= (64 * 1024))
  2717. total_mem = full_size;
  2718. else
  2719. total_mem = 64 * 1024;
  2720. } else
  2721. /* Allocate memory for ioctl data */
  2722. total_mem = BUF_SZ_4K;
  2723. dataout = kmalloc(total_mem, GFP_KERNEL);
  2724. if (dataout == NULL) {
  2725. rc = -ENOMEM;
  2726. goto loopback_test_exit;
  2727. }
  2728. ptr = dataout;
  2729. ptr += ELX_LOOPBACK_HEADER_SZ;
  2730. sg_copy_to_buffer(job->request_payload.sg_list,
  2731. job->request_payload.sg_cnt,
  2732. ptr, size);
  2733. rc = lpfcdiag_loop_self_reg(phba, &rpi);
  2734. if (rc)
  2735. goto loopback_test_exit;
  2736. if (phba->sli_rev < LPFC_SLI_REV4) {
  2737. rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
  2738. if (rc) {
  2739. lpfcdiag_loop_self_unreg(phba, rpi);
  2740. goto loopback_test_exit;
  2741. }
  2742. rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
  2743. if (rc) {
  2744. lpfcdiag_loop_self_unreg(phba, rpi);
  2745. goto loopback_test_exit;
  2746. }
  2747. }
  2748. evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
  2749. SLI_CT_ELX_LOOPBACK);
  2750. if (!evt) {
  2751. lpfcdiag_loop_self_unreg(phba, rpi);
  2752. rc = -ENOMEM;
  2753. goto loopback_test_exit;
  2754. }
  2755. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2756. list_add(&evt->node, &phba->ct_ev_waiters);
  2757. lpfc_bsg_event_ref(evt);
  2758. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2759. cmdiocbq = lpfc_sli_get_iocbq(phba);
  2760. if (phba->sli_rev < LPFC_SLI_REV4)
  2761. rspiocbq = lpfc_sli_get_iocbq(phba);
  2762. txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  2763. if (txbmp) {
  2764. txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
  2765. if (txbmp->virt) {
  2766. INIT_LIST_HEAD(&txbmp->list);
  2767. txbpl = (struct ulp_bde64 *) txbmp->virt;
  2768. txbuffer = diag_cmd_data_alloc(phba,
  2769. txbpl, full_size, 0);
  2770. }
  2771. }
  2772. if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
  2773. rc = -ENOMEM;
  2774. goto err_loopback_test_exit;
  2775. }
  2776. if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
  2777. rc = -ENOMEM;
  2778. goto err_loopback_test_exit;
  2779. }
  2780. cmd = &cmdiocbq->iocb;
  2781. if (phba->sli_rev < LPFC_SLI_REV4)
  2782. rsp = &rspiocbq->iocb;
  2783. INIT_LIST_HEAD(&head);
  2784. list_add_tail(&head, &txbuffer->dma.list);
  2785. list_for_each_entry(curr, &head, list) {
  2786. segment_len = ((struct lpfc_dmabufext *)curr)->size;
  2787. if (current_offset == 0) {
  2788. ctreq = curr->virt;
  2789. memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
  2790. ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
  2791. ctreq->RevisionId.bits.InId = 0;
  2792. ctreq->FsType = SLI_CT_ELX_LOOPBACK;
  2793. ctreq->FsSubType = 0;
  2794. ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
  2795. ctreq->CommandResponse.bits.Size = size;
  2796. segment_offset = ELX_LOOPBACK_HEADER_SZ;
  2797. } else
  2798. segment_offset = 0;
  2799. BUG_ON(segment_offset >= segment_len);
  2800. memcpy(curr->virt + segment_offset,
  2801. ptr + current_offset,
  2802. segment_len - segment_offset);
  2803. current_offset += segment_len - segment_offset;
  2804. BUG_ON(current_offset > size);
  2805. }
  2806. list_del(&head);
  2807. /* Build the XMIT_SEQUENCE iocb */
  2808. num_bde = (uint32_t)txbuffer->flag;
  2809. cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
  2810. cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
  2811. cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  2812. cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
  2813. cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
  2814. cmd->un.xseq64.w5.hcsw.Dfctl = 0;
  2815. cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  2816. cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
  2817. cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
  2818. cmd->ulpBdeCount = 1;
  2819. cmd->ulpLe = 1;
  2820. cmd->ulpClass = CLASS3;
  2821. if (phba->sli_rev < LPFC_SLI_REV4) {
  2822. cmd->ulpContext = txxri;
  2823. } else {
  2824. cmd->un.xseq64.bdl.ulpIoTag32 = 0;
  2825. cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
  2826. cmdiocbq->context3 = txbmp;
  2827. cmdiocbq->sli4_xritag = NO_XRI;
  2828. cmd->unsli3.rcvsli3.ox_id = 0xffff;
  2829. }
  2830. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  2831. cmdiocbq->vport = phba->pport;
  2832. cmdiocbq->iocb_cmpl = NULL;
  2833. iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  2834. rspiocbq, (phba->fc_ratov * 2) +
  2835. LPFC_DRVR_TIMEOUT);
  2836. if ((iocb_stat != IOCB_SUCCESS) ||
  2837. ((phba->sli_rev < LPFC_SLI_REV4) &&
  2838. (rsp->ulpStatus != IOSTAT_SUCCESS))) {
  2839. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2840. "3126 Failed loopback test issue iocb: "
  2841. "iocb_stat:x%x\n", iocb_stat);
  2842. rc = -EIO;
  2843. goto err_loopback_test_exit;
  2844. }
  2845. evt->waiting = 1;
  2846. time_left = wait_event_interruptible_timeout(
  2847. evt->wq, !list_empty(&evt->events_to_see),
  2848. msecs_to_jiffies(1000 *
  2849. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
  2850. evt->waiting = 0;
  2851. if (list_empty(&evt->events_to_see)) {
  2852. rc = (time_left) ? -EINTR : -ETIMEDOUT;
  2853. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2854. "3125 Not receiving unsolicited event, "
  2855. "rc:x%x\n", rc);
  2856. } else {
  2857. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2858. list_move(evt->events_to_see.prev, &evt->events_to_get);
  2859. evdat = list_entry(evt->events_to_get.prev,
  2860. typeof(*evdat), node);
  2861. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2862. rx_databuf = evdat->data;
  2863. if (evdat->len != full_size) {
  2864. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2865. "1603 Loopback test did not receive expected "
  2866. "data length. actual length 0x%x expected "
  2867. "length 0x%x\n",
  2868. evdat->len, full_size);
  2869. rc = -EIO;
  2870. } else if (rx_databuf == NULL)
  2871. rc = -EIO;
  2872. else {
  2873. rc = IOCB_SUCCESS;
  2874. /* skip over elx loopback header */
  2875. rx_databuf += ELX_LOOPBACK_HEADER_SZ;
  2876. job->reply->reply_payload_rcv_len =
  2877. sg_copy_from_buffer(job->reply_payload.sg_list,
  2878. job->reply_payload.sg_cnt,
  2879. rx_databuf, size);
  2880. job->reply->reply_payload_rcv_len = size;
  2881. }
  2882. }
  2883. err_loopback_test_exit:
  2884. lpfcdiag_loop_self_unreg(phba, rpi);
  2885. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2886. lpfc_bsg_event_unref(evt); /* release ref */
  2887. lpfc_bsg_event_unref(evt); /* delete */
  2888. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2889. if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
  2890. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2891. if (rspiocbq != NULL)
  2892. lpfc_sli_release_iocbq(phba, rspiocbq);
  2893. if (txbmp != NULL) {
  2894. if (txbpl != NULL) {
  2895. if (txbuffer != NULL)
  2896. diag_cmd_data_free(phba, txbuffer);
  2897. lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
  2898. }
  2899. kfree(txbmp);
  2900. }
  2901. loopback_test_exit:
  2902. kfree(dataout);
  2903. /* make error code available to userspace */
  2904. job->reply->result = rc;
  2905. job->dd_data = NULL;
  2906. /* complete the job back to userspace if no error */
  2907. if (rc == IOCB_SUCCESS)
  2908. job->job_done(job);
  2909. return rc;
  2910. }
  2911. /**
  2912. * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
  2913. * @job: GET_DFC_REV fc_bsg_job
  2914. **/
  2915. static int
  2916. lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
  2917. {
  2918. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2919. struct lpfc_hba *phba = vport->phba;
  2920. struct get_mgmt_rev *event_req;
  2921. struct get_mgmt_rev_reply *event_reply;
  2922. int rc = 0;
  2923. if (job->request_len <
  2924. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
  2925. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2926. "2740 Received GET_DFC_REV request below "
  2927. "minimum size\n");
  2928. rc = -EINVAL;
  2929. goto job_error;
  2930. }
  2931. event_req = (struct get_mgmt_rev *)
  2932. job->request->rqst_data.h_vendor.vendor_cmd;
  2933. event_reply = (struct get_mgmt_rev_reply *)
  2934. job->reply->reply_data.vendor_reply.vendor_rsp;
  2935. if (job->reply_len <
  2936. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
  2937. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2938. "2741 Received GET_DFC_REV reply below "
  2939. "minimum size\n");
  2940. rc = -EINVAL;
  2941. goto job_error;
  2942. }
  2943. event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
  2944. event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
  2945. job_error:
  2946. job->reply->result = rc;
  2947. if (rc == 0)
  2948. job->job_done(job);
  2949. return rc;
  2950. }
  2951. /**
  2952. * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
  2953. * @phba: Pointer to HBA context object.
  2954. * @pmboxq: Pointer to mailbox command.
  2955. *
  2956. * This is completion handler function for mailbox commands issued from
  2957. * lpfc_bsg_issue_mbox function. This function is called by the
  2958. * mailbox event handler function with no lock held. This function
  2959. * will wake up thread waiting on the wait queue pointed by context1
  2960. * of the mailbox.
  2961. **/
  2962. static void
  2963. lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  2964. {
  2965. struct bsg_job_data *dd_data;
  2966. struct fc_bsg_job *job;
  2967. uint32_t size;
  2968. unsigned long flags;
  2969. uint8_t *pmb, *pmb_buf;
  2970. dd_data = pmboxq->context1;
  2971. /*
  2972. * The outgoing buffer is readily referred from the dma buffer,
  2973. * just need to get header part from mailboxq structure.
  2974. */
  2975. pmb = (uint8_t *)&pmboxq->u.mb;
  2976. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  2977. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  2978. /* Determine if job has been aborted */
  2979. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2980. job = dd_data->set_job;
  2981. if (job) {
  2982. /* Prevent timeout handling from trying to abort job */
  2983. job->dd_data = NULL;
  2984. }
  2985. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2986. /* Copy the mailbox data to the job if it is still active */
  2987. if (job) {
  2988. size = job->reply_payload.payload_len;
  2989. job->reply->reply_payload_rcv_len =
  2990. sg_copy_from_buffer(job->reply_payload.sg_list,
  2991. job->reply_payload.sg_cnt,
  2992. pmb_buf, size);
  2993. }
  2994. dd_data->set_job = NULL;
  2995. mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
  2996. lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
  2997. kfree(dd_data);
  2998. /* Complete the job if the job is still active */
  2999. if (job) {
  3000. job->reply->result = 0;
  3001. job->job_done(job);
  3002. }
  3003. return;
  3004. }
  3005. /**
  3006. * lpfc_bsg_check_cmd_access - test for a supported mailbox command
  3007. * @phba: Pointer to HBA context object.
  3008. * @mb: Pointer to a mailbox object.
  3009. * @vport: Pointer to a vport object.
  3010. *
  3011. * Some commands require the port to be offline, some may not be called from
  3012. * the application.
  3013. **/
  3014. static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
  3015. MAILBOX_t *mb, struct lpfc_vport *vport)
  3016. {
  3017. /* return negative error values for bsg job */
  3018. switch (mb->mbxCommand) {
  3019. /* Offline only */
  3020. case MBX_INIT_LINK:
  3021. case MBX_DOWN_LINK:
  3022. case MBX_CONFIG_LINK:
  3023. case MBX_CONFIG_RING:
  3024. case MBX_RESET_RING:
  3025. case MBX_UNREG_LOGIN:
  3026. case MBX_CLEAR_LA:
  3027. case MBX_DUMP_CONTEXT:
  3028. case MBX_RUN_DIAGS:
  3029. case MBX_RESTART:
  3030. case MBX_SET_MASK:
  3031. if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
  3032. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  3033. "2743 Command 0x%x is illegal in on-line "
  3034. "state\n",
  3035. mb->mbxCommand);
  3036. return -EPERM;
  3037. }
  3038. case MBX_WRITE_NV:
  3039. case MBX_WRITE_VPARMS:
  3040. case MBX_LOAD_SM:
  3041. case MBX_READ_NV:
  3042. case MBX_READ_CONFIG:
  3043. case MBX_READ_RCONFIG:
  3044. case MBX_READ_STATUS:
  3045. case MBX_READ_XRI:
  3046. case MBX_READ_REV:
  3047. case MBX_READ_LNK_STAT:
  3048. case MBX_DUMP_MEMORY:
  3049. case MBX_DOWN_LOAD:
  3050. case MBX_UPDATE_CFG:
  3051. case MBX_KILL_BOARD:
  3052. case MBX_READ_TOPOLOGY:
  3053. case MBX_LOAD_AREA:
  3054. case MBX_LOAD_EXP_ROM:
  3055. case MBX_BEACON:
  3056. case MBX_DEL_LD_ENTRY:
  3057. case MBX_SET_DEBUG:
  3058. case MBX_WRITE_WWN:
  3059. case MBX_SLI4_CONFIG:
  3060. case MBX_READ_EVENT_LOG:
  3061. case MBX_READ_EVENT_LOG_STATUS:
  3062. case MBX_WRITE_EVENT_LOG:
  3063. case MBX_PORT_CAPABILITIES:
  3064. case MBX_PORT_IOV_CONTROL:
  3065. case MBX_RUN_BIU_DIAG64:
  3066. break;
  3067. case MBX_SET_VARIABLE:
  3068. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3069. "1226 mbox: set_variable 0x%x, 0x%x\n",
  3070. mb->un.varWords[0],
  3071. mb->un.varWords[1]);
  3072. if ((mb->un.varWords[0] == SETVAR_MLOMNT)
  3073. && (mb->un.varWords[1] == 1)) {
  3074. phba->wait_4_mlo_maint_flg = 1;
  3075. } else if (mb->un.varWords[0] == SETVAR_MLORST) {
  3076. spin_lock_irq(&phba->hbalock);
  3077. phba->link_flag &= ~LS_LOOPBACK_MODE;
  3078. spin_unlock_irq(&phba->hbalock);
  3079. phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
  3080. }
  3081. break;
  3082. case MBX_READ_SPARM64:
  3083. case MBX_REG_LOGIN:
  3084. case MBX_REG_LOGIN64:
  3085. case MBX_CONFIG_PORT:
  3086. case MBX_RUN_BIU_DIAG:
  3087. default:
  3088. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  3089. "2742 Unknown Command 0x%x\n",
  3090. mb->mbxCommand);
  3091. return -EPERM;
  3092. }
  3093. return 0; /* ok */
  3094. }
  3095. /**
  3096. * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
  3097. * @phba: Pointer to HBA context object.
  3098. *
  3099. * This is routine clean up and reset BSG handling of multi-buffer mbox
  3100. * command session.
  3101. **/
  3102. static void
  3103. lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
  3104. {
  3105. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
  3106. return;
  3107. /* free all memory, including dma buffers */
  3108. lpfc_bsg_dma_page_list_free(phba,
  3109. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3110. lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
  3111. /* multi-buffer write mailbox command pass-through complete */
  3112. memset((char *)&phba->mbox_ext_buf_ctx, 0,
  3113. sizeof(struct lpfc_mbox_ext_buf_ctx));
  3114. INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3115. return;
  3116. }
  3117. /**
  3118. * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
  3119. * @phba: Pointer to HBA context object.
  3120. * @pmboxq: Pointer to mailbox command.
  3121. *
  3122. * This is routine handles BSG job for mailbox commands completions with
  3123. * multiple external buffers.
  3124. **/
  3125. static struct fc_bsg_job *
  3126. lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3127. {
  3128. struct bsg_job_data *dd_data;
  3129. struct fc_bsg_job *job;
  3130. uint8_t *pmb, *pmb_buf;
  3131. unsigned long flags;
  3132. uint32_t size;
  3133. int rc = 0;
  3134. struct lpfc_dmabuf *dmabuf;
  3135. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3136. uint8_t *pmbx;
  3137. dd_data = pmboxq->context1;
  3138. /* Determine if job has been aborted */
  3139. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  3140. job = dd_data->set_job;
  3141. if (job) {
  3142. /* Prevent timeout handling from trying to abort job */
  3143. job->dd_data = NULL;
  3144. }
  3145. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  3146. /*
  3147. * The outgoing buffer is readily referred from the dma buffer,
  3148. * just need to get header part from mailboxq structure.
  3149. */
  3150. pmb = (uint8_t *)&pmboxq->u.mb;
  3151. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  3152. /* Copy the byte swapped response mailbox back to the user */
  3153. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  3154. /* if there is any non-embedded extended data copy that too */
  3155. dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
  3156. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3157. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3158. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3159. pmbx = (uint8_t *)dmabuf->virt;
  3160. /* byte swap the extended data following the mailbox command */
  3161. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3162. &pmbx[sizeof(MAILBOX_t)],
  3163. sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
  3164. }
  3165. /* Complete the job if the job is still active */
  3166. if (job) {
  3167. size = job->reply_payload.payload_len;
  3168. job->reply->reply_payload_rcv_len =
  3169. sg_copy_from_buffer(job->reply_payload.sg_list,
  3170. job->reply_payload.sg_cnt,
  3171. pmb_buf, size);
  3172. /* result for successful */
  3173. job->reply->result = 0;
  3174. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3175. "2937 SLI_CONFIG ext-buffer maibox command "
  3176. "(x%x/x%x) complete bsg job done, bsize:%d\n",
  3177. phba->mbox_ext_buf_ctx.nembType,
  3178. phba->mbox_ext_buf_ctx.mboxType, size);
  3179. lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
  3180. phba->mbox_ext_buf_ctx.nembType,
  3181. phba->mbox_ext_buf_ctx.mboxType,
  3182. dma_ebuf, sta_pos_addr,
  3183. phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
  3184. } else {
  3185. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3186. "2938 SLI_CONFIG ext-buffer maibox "
  3187. "command (x%x/x%x) failure, rc:x%x\n",
  3188. phba->mbox_ext_buf_ctx.nembType,
  3189. phba->mbox_ext_buf_ctx.mboxType, rc);
  3190. }
  3191. /* state change */
  3192. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
  3193. kfree(dd_data);
  3194. return job;
  3195. }
  3196. /**
  3197. * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
  3198. * @phba: Pointer to HBA context object.
  3199. * @pmboxq: Pointer to mailbox command.
  3200. *
  3201. * This is completion handler function for mailbox read commands with multiple
  3202. * external buffers.
  3203. **/
  3204. static void
  3205. lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3206. {
  3207. struct fc_bsg_job *job;
  3208. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3209. /* handle the BSG job with mailbox command */
  3210. if (!job)
  3211. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3212. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3213. "2939 SLI_CONFIG ext-buffer rd maibox command "
  3214. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3215. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3216. if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
  3217. lpfc_bsg_mbox_ext_session_reset(phba);
  3218. /* free base driver mailbox structure memory */
  3219. mempool_free(pmboxq, phba->mbox_mem_pool);
  3220. /* if the job is still active, call job done */
  3221. if (job)
  3222. job->job_done(job);
  3223. return;
  3224. }
  3225. /**
  3226. * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
  3227. * @phba: Pointer to HBA context object.
  3228. * @pmboxq: Pointer to mailbox command.
  3229. *
  3230. * This is completion handler function for mailbox write commands with multiple
  3231. * external buffers.
  3232. **/
  3233. static void
  3234. lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3235. {
  3236. struct fc_bsg_job *job;
  3237. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3238. /* handle the BSG job with the mailbox command */
  3239. if (!job)
  3240. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3241. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3242. "2940 SLI_CONFIG ext-buffer wr maibox command "
  3243. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3244. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3245. /* free all memory, including dma buffers */
  3246. mempool_free(pmboxq, phba->mbox_mem_pool);
  3247. lpfc_bsg_mbox_ext_session_reset(phba);
  3248. /* if the job is still active, call job done */
  3249. if (job)
  3250. job->job_done(job);
  3251. return;
  3252. }
  3253. static void
  3254. lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
  3255. uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
  3256. struct lpfc_dmabuf *ext_dmabuf)
  3257. {
  3258. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3259. /* pointer to the start of mailbox command */
  3260. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
  3261. if (nemb_tp == nemb_mse) {
  3262. if (index == 0) {
  3263. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3264. mse[index].pa_hi =
  3265. putPaddrHigh(mbx_dmabuf->phys +
  3266. sizeof(MAILBOX_t));
  3267. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3268. mse[index].pa_lo =
  3269. putPaddrLow(mbx_dmabuf->phys +
  3270. sizeof(MAILBOX_t));
  3271. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3272. "2943 SLI_CONFIG(mse)[%d], "
  3273. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3274. index,
  3275. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3276. mse[index].buf_len,
  3277. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3278. mse[index].pa_hi,
  3279. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3280. mse[index].pa_lo);
  3281. } else {
  3282. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3283. mse[index].pa_hi =
  3284. putPaddrHigh(ext_dmabuf->phys);
  3285. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3286. mse[index].pa_lo =
  3287. putPaddrLow(ext_dmabuf->phys);
  3288. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3289. "2944 SLI_CONFIG(mse)[%d], "
  3290. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3291. index,
  3292. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3293. mse[index].buf_len,
  3294. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3295. mse[index].pa_hi,
  3296. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3297. mse[index].pa_lo);
  3298. }
  3299. } else {
  3300. if (index == 0) {
  3301. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3302. hbd[index].pa_hi =
  3303. putPaddrHigh(mbx_dmabuf->phys +
  3304. sizeof(MAILBOX_t));
  3305. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3306. hbd[index].pa_lo =
  3307. putPaddrLow(mbx_dmabuf->phys +
  3308. sizeof(MAILBOX_t));
  3309. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3310. "3007 SLI_CONFIG(hbd)[%d], "
  3311. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3312. index,
  3313. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3314. &sli_cfg_mbx->un.
  3315. sli_config_emb1_subsys.hbd[index]),
  3316. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3317. hbd[index].pa_hi,
  3318. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3319. hbd[index].pa_lo);
  3320. } else {
  3321. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3322. hbd[index].pa_hi =
  3323. putPaddrHigh(ext_dmabuf->phys);
  3324. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3325. hbd[index].pa_lo =
  3326. putPaddrLow(ext_dmabuf->phys);
  3327. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3328. "3008 SLI_CONFIG(hbd)[%d], "
  3329. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3330. index,
  3331. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3332. &sli_cfg_mbx->un.
  3333. sli_config_emb1_subsys.hbd[index]),
  3334. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3335. hbd[index].pa_hi,
  3336. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3337. hbd[index].pa_lo);
  3338. }
  3339. }
  3340. return;
  3341. }
  3342. /**
  3343. * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
  3344. * @phba: Pointer to HBA context object.
  3345. * @mb: Pointer to a BSG mailbox object.
  3346. * @nemb_tp: Enumerate of non-embedded mailbox command type.
  3347. * @dmabuff: Pointer to a DMA buffer descriptor.
  3348. *
  3349. * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
  3350. * non-embedded external bufffers.
  3351. **/
  3352. static int
  3353. lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3354. enum nemb_type nemb_tp,
  3355. struct lpfc_dmabuf *dmabuf)
  3356. {
  3357. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3358. struct dfc_mbox_req *mbox_req;
  3359. struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
  3360. uint32_t ext_buf_cnt, ext_buf_index;
  3361. struct lpfc_dmabuf *ext_dmabuf = NULL;
  3362. struct bsg_job_data *dd_data = NULL;
  3363. LPFC_MBOXQ_t *pmboxq = NULL;
  3364. MAILBOX_t *pmb;
  3365. uint8_t *pmbx;
  3366. int rc, i;
  3367. mbox_req =
  3368. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3369. /* pointer to the start of mailbox command */
  3370. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3371. if (nemb_tp == nemb_mse) {
  3372. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3373. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3374. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3375. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3376. "2945 Handled SLI_CONFIG(mse) rd, "
  3377. "ext_buf_cnt(%d) out of range(%d)\n",
  3378. ext_buf_cnt,
  3379. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3380. rc = -ERANGE;
  3381. goto job_error;
  3382. }
  3383. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3384. "2941 Handled SLI_CONFIG(mse) rd, "
  3385. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3386. } else {
  3387. /* sanity check on interface type for support */
  3388. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3389. LPFC_SLI_INTF_IF_TYPE_2) {
  3390. rc = -ENODEV;
  3391. goto job_error;
  3392. }
  3393. /* nemb_tp == nemb_hbd */
  3394. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3395. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3396. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3397. "2946 Handled SLI_CONFIG(hbd) rd, "
  3398. "ext_buf_cnt(%d) out of range(%d)\n",
  3399. ext_buf_cnt,
  3400. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3401. rc = -ERANGE;
  3402. goto job_error;
  3403. }
  3404. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3405. "2942 Handled SLI_CONFIG(hbd) rd, "
  3406. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3407. }
  3408. /* before dma descriptor setup */
  3409. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3410. sta_pre_addr, dmabuf, ext_buf_cnt);
  3411. /* reject non-embedded mailbox command with none external buffer */
  3412. if (ext_buf_cnt == 0) {
  3413. rc = -EPERM;
  3414. goto job_error;
  3415. } else if (ext_buf_cnt > 1) {
  3416. /* additional external read buffers */
  3417. for (i = 1; i < ext_buf_cnt; i++) {
  3418. ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
  3419. if (!ext_dmabuf) {
  3420. rc = -ENOMEM;
  3421. goto job_error;
  3422. }
  3423. list_add_tail(&ext_dmabuf->list,
  3424. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3425. }
  3426. }
  3427. /* bsg tracking structure */
  3428. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3429. if (!dd_data) {
  3430. rc = -ENOMEM;
  3431. goto job_error;
  3432. }
  3433. /* mailbox command structure for base driver */
  3434. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3435. if (!pmboxq) {
  3436. rc = -ENOMEM;
  3437. goto job_error;
  3438. }
  3439. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3440. /* for the first external buffer */
  3441. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3442. /* for the rest of external buffer descriptors if any */
  3443. if (ext_buf_cnt > 1) {
  3444. ext_buf_index = 1;
  3445. list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
  3446. &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
  3447. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
  3448. ext_buf_index, dmabuf,
  3449. curr_dmabuf);
  3450. ext_buf_index++;
  3451. }
  3452. }
  3453. /* after dma descriptor setup */
  3454. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3455. sta_pos_addr, dmabuf, ext_buf_cnt);
  3456. /* construct base driver mbox command */
  3457. pmb = &pmboxq->u.mb;
  3458. pmbx = (uint8_t *)dmabuf->virt;
  3459. memcpy(pmb, pmbx, sizeof(*pmb));
  3460. pmb->mbxOwner = OWN_HOST;
  3461. pmboxq->vport = phba->pport;
  3462. /* multi-buffer handling context */
  3463. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3464. phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
  3465. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3466. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3467. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3468. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3469. /* callback for multi-buffer read mailbox command */
  3470. pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
  3471. /* context fields to callback function */
  3472. pmboxq->context1 = dd_data;
  3473. dd_data->type = TYPE_MBOX;
  3474. dd_data->set_job = job;
  3475. dd_data->context_un.mbox.pmboxq = pmboxq;
  3476. dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
  3477. job->dd_data = dd_data;
  3478. /* state change */
  3479. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3480. /*
  3481. * Non-embedded mailbox subcommand data gets byte swapped here because
  3482. * the lower level driver code only does the first 64 mailbox words.
  3483. */
  3484. if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
  3485. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
  3486. (nemb_tp == nemb_mse))
  3487. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3488. &pmbx[sizeof(MAILBOX_t)],
  3489. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3490. mse[0].buf_len);
  3491. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3492. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3493. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3494. "2947 Issued SLI_CONFIG ext-buffer "
  3495. "maibox command, rc:x%x\n", rc);
  3496. return SLI_CONFIG_HANDLED;
  3497. }
  3498. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3499. "2948 Failed to issue SLI_CONFIG ext-buffer "
  3500. "maibox command, rc:x%x\n", rc);
  3501. rc = -EPIPE;
  3502. job_error:
  3503. if (pmboxq)
  3504. mempool_free(pmboxq, phba->mbox_mem_pool);
  3505. lpfc_bsg_dma_page_list_free(phba,
  3506. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3507. kfree(dd_data);
  3508. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
  3509. return rc;
  3510. }
  3511. /**
  3512. * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
  3513. * @phba: Pointer to HBA context object.
  3514. * @mb: Pointer to a BSG mailbox object.
  3515. * @dmabuff: Pointer to a DMA buffer descriptor.
  3516. *
  3517. * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
  3518. * non-embedded external bufffers.
  3519. **/
  3520. static int
  3521. lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3522. enum nemb_type nemb_tp,
  3523. struct lpfc_dmabuf *dmabuf)
  3524. {
  3525. struct dfc_mbox_req *mbox_req;
  3526. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3527. uint32_t ext_buf_cnt;
  3528. struct bsg_job_data *dd_data = NULL;
  3529. LPFC_MBOXQ_t *pmboxq = NULL;
  3530. MAILBOX_t *pmb;
  3531. uint8_t *mbx;
  3532. int rc = SLI_CONFIG_NOT_HANDLED, i;
  3533. mbox_req =
  3534. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3535. /* pointer to the start of mailbox command */
  3536. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3537. if (nemb_tp == nemb_mse) {
  3538. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3539. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3540. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3541. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3542. "2953 Failed SLI_CONFIG(mse) wr, "
  3543. "ext_buf_cnt(%d) out of range(%d)\n",
  3544. ext_buf_cnt,
  3545. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3546. return -ERANGE;
  3547. }
  3548. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3549. "2949 Handled SLI_CONFIG(mse) wr, "
  3550. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3551. } else {
  3552. /* sanity check on interface type for support */
  3553. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3554. LPFC_SLI_INTF_IF_TYPE_2)
  3555. return -ENODEV;
  3556. /* nemb_tp == nemb_hbd */
  3557. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3558. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3559. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3560. "2954 Failed SLI_CONFIG(hbd) wr, "
  3561. "ext_buf_cnt(%d) out of range(%d)\n",
  3562. ext_buf_cnt,
  3563. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3564. return -ERANGE;
  3565. }
  3566. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3567. "2950 Handled SLI_CONFIG(hbd) wr, "
  3568. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3569. }
  3570. /* before dma buffer descriptor setup */
  3571. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3572. sta_pre_addr, dmabuf, ext_buf_cnt);
  3573. if (ext_buf_cnt == 0)
  3574. return -EPERM;
  3575. /* for the first external buffer */
  3576. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3577. /* after dma descriptor setup */
  3578. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3579. sta_pos_addr, dmabuf, ext_buf_cnt);
  3580. /* log for looking forward */
  3581. for (i = 1; i < ext_buf_cnt; i++) {
  3582. if (nemb_tp == nemb_mse)
  3583. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3584. "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
  3585. i, sli_cfg_mbx->un.sli_config_emb0_subsys.
  3586. mse[i].buf_len);
  3587. else
  3588. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3589. "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
  3590. i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3591. &sli_cfg_mbx->un.sli_config_emb1_subsys.
  3592. hbd[i]));
  3593. }
  3594. /* multi-buffer handling context */
  3595. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3596. phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
  3597. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3598. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3599. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3600. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3601. if (ext_buf_cnt == 1) {
  3602. /* bsg tracking structure */
  3603. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3604. if (!dd_data) {
  3605. rc = -ENOMEM;
  3606. goto job_error;
  3607. }
  3608. /* mailbox command structure for base driver */
  3609. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3610. if (!pmboxq) {
  3611. rc = -ENOMEM;
  3612. goto job_error;
  3613. }
  3614. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3615. pmb = &pmboxq->u.mb;
  3616. mbx = (uint8_t *)dmabuf->virt;
  3617. memcpy(pmb, mbx, sizeof(*pmb));
  3618. pmb->mbxOwner = OWN_HOST;
  3619. pmboxq->vport = phba->pport;
  3620. /* callback for multi-buffer read mailbox command */
  3621. pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
  3622. /* context fields to callback function */
  3623. pmboxq->context1 = dd_data;
  3624. dd_data->type = TYPE_MBOX;
  3625. dd_data->set_job = job;
  3626. dd_data->context_un.mbox.pmboxq = pmboxq;
  3627. dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
  3628. job->dd_data = dd_data;
  3629. /* state change */
  3630. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3631. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3632. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3633. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3634. "2955 Issued SLI_CONFIG ext-buffer "
  3635. "maibox command, rc:x%x\n", rc);
  3636. return SLI_CONFIG_HANDLED;
  3637. }
  3638. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3639. "2956 Failed to issue SLI_CONFIG ext-buffer "
  3640. "maibox command, rc:x%x\n", rc);
  3641. rc = -EPIPE;
  3642. goto job_error;
  3643. }
  3644. /* wait for additoinal external buffers */
  3645. job->reply->result = 0;
  3646. job->job_done(job);
  3647. return SLI_CONFIG_HANDLED;
  3648. job_error:
  3649. if (pmboxq)
  3650. mempool_free(pmboxq, phba->mbox_mem_pool);
  3651. kfree(dd_data);
  3652. return rc;
  3653. }
  3654. /**
  3655. * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
  3656. * @phba: Pointer to HBA context object.
  3657. * @mb: Pointer to a BSG mailbox object.
  3658. * @dmabuff: Pointer to a DMA buffer descriptor.
  3659. *
  3660. * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
  3661. * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
  3662. * with embedded sussystem 0x1 and opcodes with external HBDs.
  3663. **/
  3664. static int
  3665. lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3666. struct lpfc_dmabuf *dmabuf)
  3667. {
  3668. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3669. uint32_t subsys;
  3670. uint32_t opcode;
  3671. int rc = SLI_CONFIG_NOT_HANDLED;
  3672. /* state change on new multi-buffer pass-through mailbox command */
  3673. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
  3674. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3675. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3676. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3677. subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
  3678. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3679. opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
  3680. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3681. if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
  3682. switch (opcode) {
  3683. case FCOE_OPCODE_READ_FCF:
  3684. case FCOE_OPCODE_GET_DPORT_RESULTS:
  3685. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3686. "2957 Handled SLI_CONFIG "
  3687. "subsys_fcoe, opcode:x%x\n",
  3688. opcode);
  3689. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3690. nemb_mse, dmabuf);
  3691. break;
  3692. case FCOE_OPCODE_ADD_FCF:
  3693. case FCOE_OPCODE_SET_DPORT_MODE:
  3694. case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
  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 int
  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. }