lpfc_bsg.c 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2009-2015 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *******************************************************************/
  20. #include <linux/interrupt.h>
  21. #include <linux/mempool.h>
  22. #include <linux/pci.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/list.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_transport_fc.h>
  29. #include <scsi/scsi_bsg_fc.h>
  30. #include <scsi/fc/fc_fs.h>
  31. #include "lpfc_hw4.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_nl.h"
  36. #include "lpfc_bsg.h"
  37. #include "lpfc_disc.h"
  38. #include "lpfc_scsi.h"
  39. #include "lpfc.h"
  40. #include "lpfc_logmsg.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_debugfs.h"
  43. #include "lpfc_vport.h"
  44. #include "lpfc_version.h"
  45. struct lpfc_bsg_event {
  46. struct list_head node;
  47. struct kref kref;
  48. wait_queue_head_t wq;
  49. /* Event type and waiter identifiers */
  50. uint32_t type_mask;
  51. uint32_t req_id;
  52. uint32_t reg_id;
  53. /* next two flags are here for the auto-delete logic */
  54. unsigned long wait_time_stamp;
  55. int waiting;
  56. /* seen and not seen events */
  57. struct list_head events_to_get;
  58. struct list_head events_to_see;
  59. /* driver data associated with the job */
  60. void *dd_data;
  61. };
  62. struct lpfc_bsg_iocb {
  63. struct lpfc_iocbq *cmdiocbq;
  64. struct lpfc_dmabuf *rmp;
  65. struct lpfc_nodelist *ndlp;
  66. };
  67. struct lpfc_bsg_mbox {
  68. LPFC_MBOXQ_t *pmboxq;
  69. MAILBOX_t *mb;
  70. struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
  71. uint8_t *ext; /* extended mailbox data */
  72. uint32_t mbOffset; /* from app */
  73. uint32_t inExtWLen; /* from app */
  74. uint32_t outExtWLen; /* from app */
  75. };
  76. #define MENLO_DID 0x0000FC0E
  77. struct lpfc_bsg_menlo {
  78. struct lpfc_iocbq *cmdiocbq;
  79. struct lpfc_dmabuf *rmp;
  80. };
  81. #define TYPE_EVT 1
  82. #define TYPE_IOCB 2
  83. #define TYPE_MBOX 3
  84. #define TYPE_MENLO 4
  85. struct bsg_job_data {
  86. uint32_t type;
  87. struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
  88. union {
  89. struct lpfc_bsg_event *evt;
  90. struct lpfc_bsg_iocb iocb;
  91. struct lpfc_bsg_mbox mbox;
  92. struct lpfc_bsg_menlo menlo;
  93. } context_un;
  94. };
  95. struct event_data {
  96. struct list_head node;
  97. uint32_t type;
  98. uint32_t immed_dat;
  99. void *data;
  100. uint32_t len;
  101. };
  102. #define BUF_SZ_4K 4096
  103. #define SLI_CT_ELX_LOOPBACK 0x10
  104. enum ELX_LOOPBACK_CMD {
  105. ELX_LOOPBACK_XRI_SETUP,
  106. ELX_LOOPBACK_DATA,
  107. };
  108. #define ELX_LOOPBACK_HEADER_SZ \
  109. (size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
  110. struct lpfc_dmabufext {
  111. struct lpfc_dmabuf dma;
  112. uint32_t size;
  113. uint32_t flag;
  114. };
  115. static void
  116. lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
  117. {
  118. struct lpfc_dmabuf *mlast, *next_mlast;
  119. if (mlist) {
  120. list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
  121. list) {
  122. lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
  123. list_del(&mlast->list);
  124. kfree(mlast);
  125. }
  126. lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
  127. kfree(mlist);
  128. }
  129. return;
  130. }
  131. static struct lpfc_dmabuf *
  132. lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
  133. int outbound_buffers, struct ulp_bde64 *bpl,
  134. int *bpl_entries)
  135. {
  136. struct lpfc_dmabuf *mlist = NULL;
  137. struct lpfc_dmabuf *mp;
  138. unsigned int bytes_left = size;
  139. /* Verify we can support the size specified */
  140. if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
  141. return NULL;
  142. /* Determine the number of dma buffers to allocate */
  143. *bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
  144. size/LPFC_BPL_SIZE);
  145. /* Allocate dma buffer and place in BPL passed */
  146. while (bytes_left) {
  147. /* Allocate dma buffer */
  148. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  149. if (!mp) {
  150. if (mlist)
  151. lpfc_free_bsg_buffers(phba, mlist);
  152. return NULL;
  153. }
  154. INIT_LIST_HEAD(&mp->list);
  155. mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
  156. if (!mp->virt) {
  157. kfree(mp);
  158. if (mlist)
  159. lpfc_free_bsg_buffers(phba, mlist);
  160. return NULL;
  161. }
  162. /* Queue it to a linked list */
  163. if (!mlist)
  164. mlist = mp;
  165. else
  166. list_add_tail(&mp->list, &mlist->list);
  167. /* Add buffer to buffer pointer list */
  168. if (outbound_buffers)
  169. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  170. else
  171. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  172. bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
  173. bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
  174. bpl->tus.f.bdeSize = (uint16_t)
  175. (bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
  176. bytes_left);
  177. bytes_left -= bpl->tus.f.bdeSize;
  178. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  179. bpl++;
  180. }
  181. return mlist;
  182. }
  183. static unsigned int
  184. lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
  185. struct fc_bsg_buffer *bsg_buffers,
  186. unsigned int bytes_to_transfer, int to_buffers)
  187. {
  188. struct lpfc_dmabuf *mp;
  189. unsigned int transfer_bytes, bytes_copied = 0;
  190. unsigned int sg_offset, dma_offset;
  191. unsigned char *dma_address, *sg_address;
  192. LIST_HEAD(temp_list);
  193. struct sg_mapping_iter miter;
  194. unsigned long flags;
  195. unsigned int sg_flags = SG_MITER_ATOMIC;
  196. bool sg_valid;
  197. list_splice_init(&dma_buffers->list, &temp_list);
  198. list_add(&dma_buffers->list, &temp_list);
  199. sg_offset = 0;
  200. if (to_buffers)
  201. sg_flags |= SG_MITER_FROM_SG;
  202. else
  203. sg_flags |= SG_MITER_TO_SG;
  204. sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
  205. sg_flags);
  206. local_irq_save(flags);
  207. sg_valid = sg_miter_next(&miter);
  208. list_for_each_entry(mp, &temp_list, list) {
  209. dma_offset = 0;
  210. while (bytes_to_transfer && sg_valid &&
  211. (dma_offset < LPFC_BPL_SIZE)) {
  212. dma_address = mp->virt + dma_offset;
  213. if (sg_offset) {
  214. /* Continue previous partial transfer of sg */
  215. sg_address = miter.addr + sg_offset;
  216. transfer_bytes = miter.length - sg_offset;
  217. } else {
  218. sg_address = miter.addr;
  219. transfer_bytes = miter.length;
  220. }
  221. if (bytes_to_transfer < transfer_bytes)
  222. transfer_bytes = bytes_to_transfer;
  223. if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
  224. transfer_bytes = LPFC_BPL_SIZE - dma_offset;
  225. if (to_buffers)
  226. memcpy(dma_address, sg_address, transfer_bytes);
  227. else
  228. memcpy(sg_address, dma_address, transfer_bytes);
  229. dma_offset += transfer_bytes;
  230. sg_offset += transfer_bytes;
  231. bytes_to_transfer -= transfer_bytes;
  232. bytes_copied += transfer_bytes;
  233. if (sg_offset >= miter.length) {
  234. sg_offset = 0;
  235. sg_valid = sg_miter_next(&miter);
  236. }
  237. }
  238. }
  239. sg_miter_stop(&miter);
  240. local_irq_restore(flags);
  241. list_del_init(&dma_buffers->list);
  242. list_splice(&temp_list, &dma_buffers->list);
  243. return bytes_copied;
  244. }
  245. /**
  246. * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
  247. * @phba: Pointer to HBA context object.
  248. * @cmdiocbq: Pointer to command iocb.
  249. * @rspiocbq: Pointer to response iocb.
  250. *
  251. * This function is the completion handler for iocbs issued using
  252. * lpfc_bsg_send_mgmt_cmd function. This function is called by the
  253. * ring event handler function without any lock held. This function
  254. * can be called from both worker thread context and interrupt
  255. * context. This function also can be called from another thread which
  256. * cleans up the SLI layer objects.
  257. * This function copies the contents of the response iocb to the
  258. * response iocb memory object provided by the caller of
  259. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  260. * sleeps for the iocb completion.
  261. **/
  262. static void
  263. lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
  264. struct lpfc_iocbq *cmdiocbq,
  265. struct lpfc_iocbq *rspiocbq)
  266. {
  267. struct bsg_job_data *dd_data;
  268. struct fc_bsg_job *job;
  269. IOCB_t *rsp;
  270. struct lpfc_dmabuf *bmp, *cmp, *rmp;
  271. struct lpfc_nodelist *ndlp;
  272. struct lpfc_bsg_iocb *iocb;
  273. unsigned long flags;
  274. unsigned int rsp_size;
  275. int rc = 0;
  276. dd_data = cmdiocbq->context1;
  277. /* Determine if job has been aborted */
  278. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  279. job = dd_data->set_job;
  280. if (job) {
  281. /* Prevent timeout handling from trying to abort job */
  282. job->dd_data = NULL;
  283. }
  284. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  285. /* Close the timeout handler abort window */
  286. spin_lock_irqsave(&phba->hbalock, flags);
  287. cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
  288. spin_unlock_irqrestore(&phba->hbalock, flags);
  289. iocb = &dd_data->context_un.iocb;
  290. ndlp = iocb->ndlp;
  291. rmp = iocb->rmp;
  292. cmp = cmdiocbq->context2;
  293. bmp = cmdiocbq->context3;
  294. rsp = &rspiocbq->iocb;
  295. /* Copy the completed data or set the error status */
  296. if (job) {
  297. if (rsp->ulpStatus) {
  298. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  299. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  300. case IOERR_SEQUENCE_TIMEOUT:
  301. rc = -ETIMEDOUT;
  302. break;
  303. case IOERR_INVALID_RPI:
  304. rc = -EFAULT;
  305. break;
  306. default:
  307. rc = -EACCES;
  308. break;
  309. }
  310. } else {
  311. rc = -EACCES;
  312. }
  313. } else {
  314. rsp_size = rsp->un.genreq64.bdl.bdeSize;
  315. job->reply->reply_payload_rcv_len =
  316. lpfc_bsg_copy_data(rmp, &job->reply_payload,
  317. rsp_size, 0);
  318. }
  319. }
  320. lpfc_free_bsg_buffers(phba, cmp);
  321. lpfc_free_bsg_buffers(phba, rmp);
  322. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  323. kfree(bmp);
  324. lpfc_sli_release_iocbq(phba, cmdiocbq);
  325. lpfc_nlp_put(ndlp);
  326. kfree(dd_data);
  327. /* Complete the job if the job is still active */
  328. if (job) {
  329. job->reply->result = rc;
  330. job->job_done(job);
  331. }
  332. return;
  333. }
  334. /**
  335. * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
  336. * @job: fc_bsg_job to handle
  337. **/
  338. static int
  339. lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
  340. {
  341. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  342. struct lpfc_hba *phba = vport->phba;
  343. struct lpfc_rport_data *rdata = job->rport->dd_data;
  344. struct lpfc_nodelist *ndlp = rdata->pnode;
  345. struct ulp_bde64 *bpl = NULL;
  346. uint32_t timeout;
  347. struct lpfc_iocbq *cmdiocbq = NULL;
  348. IOCB_t *cmd;
  349. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
  350. int request_nseg;
  351. int reply_nseg;
  352. struct bsg_job_data *dd_data;
  353. unsigned long flags;
  354. uint32_t creg_val;
  355. int rc = 0;
  356. int iocb_stat;
  357. /* in case no data is transferred */
  358. job->reply->reply_payload_rcv_len = 0;
  359. /* allocate our bsg tracking structure */
  360. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  361. if (!dd_data) {
  362. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  363. "2733 Failed allocation of dd_data\n");
  364. rc = -ENOMEM;
  365. goto no_dd_data;
  366. }
  367. if (!lpfc_nlp_get(ndlp)) {
  368. rc = -ENODEV;
  369. goto no_ndlp;
  370. }
  371. if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
  372. rc = -ENODEV;
  373. goto free_ndlp;
  374. }
  375. cmdiocbq = lpfc_sli_get_iocbq(phba);
  376. if (!cmdiocbq) {
  377. rc = -ENOMEM;
  378. goto free_ndlp;
  379. }
  380. cmd = &cmdiocbq->iocb;
  381. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  382. if (!bmp) {
  383. rc = -ENOMEM;
  384. goto free_cmdiocbq;
  385. }
  386. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  387. if (!bmp->virt) {
  388. rc = -ENOMEM;
  389. goto free_bmp;
  390. }
  391. INIT_LIST_HEAD(&bmp->list);
  392. bpl = (struct ulp_bde64 *) bmp->virt;
  393. request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
  394. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  395. 1, bpl, &request_nseg);
  396. if (!cmp) {
  397. rc = -ENOMEM;
  398. goto free_bmp;
  399. }
  400. lpfc_bsg_copy_data(cmp, &job->request_payload,
  401. job->request_payload.payload_len, 1);
  402. bpl += request_nseg;
  403. reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
  404. rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
  405. bpl, &reply_nseg);
  406. if (!rmp) {
  407. rc = -ENOMEM;
  408. goto free_cmp;
  409. }
  410. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  411. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  412. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  413. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  414. cmd->un.genreq64.bdl.bdeSize =
  415. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  416. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  417. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  418. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  419. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
  420. cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
  421. cmd->ulpBdeCount = 1;
  422. cmd->ulpLe = 1;
  423. cmd->ulpClass = CLASS3;
  424. cmd->ulpContext = ndlp->nlp_rpi;
  425. if (phba->sli_rev == LPFC_SLI_REV4)
  426. cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  427. cmd->ulpOwner = OWN_CHIP;
  428. cmdiocbq->vport = phba->pport;
  429. cmdiocbq->context3 = bmp;
  430. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  431. timeout = phba->fc_ratov * 2;
  432. cmd->ulpTimeout = timeout;
  433. cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
  434. cmdiocbq->context1 = dd_data;
  435. cmdiocbq->context2 = cmp;
  436. cmdiocbq->context3 = bmp;
  437. cmdiocbq->context_un.ndlp = ndlp;
  438. dd_data->type = TYPE_IOCB;
  439. dd_data->set_job = job;
  440. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  441. dd_data->context_un.iocb.ndlp = ndlp;
  442. dd_data->context_un.iocb.rmp = rmp;
  443. job->dd_data = dd_data;
  444. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  445. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  446. rc = -EIO ;
  447. goto free_rmp;
  448. }
  449. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  450. writel(creg_val, phba->HCregaddr);
  451. readl(phba->HCregaddr); /* flush */
  452. }
  453. iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  454. if (iocb_stat == IOCB_SUCCESS) {
  455. spin_lock_irqsave(&phba->hbalock, flags);
  456. /* make sure the I/O had not been completed yet */
  457. if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
  458. /* open up abort window to timeout handler */
  459. cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
  460. }
  461. spin_unlock_irqrestore(&phba->hbalock, flags);
  462. return 0; /* done for now */
  463. } else if (iocb_stat == IOCB_BUSY) {
  464. rc = -EAGAIN;
  465. } else {
  466. rc = -EIO;
  467. }
  468. /* iocb failed so cleanup */
  469. job->dd_data = NULL;
  470. free_rmp:
  471. lpfc_free_bsg_buffers(phba, rmp);
  472. free_cmp:
  473. lpfc_free_bsg_buffers(phba, cmp);
  474. free_bmp:
  475. if (bmp->virt)
  476. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  477. kfree(bmp);
  478. free_cmdiocbq:
  479. lpfc_sli_release_iocbq(phba, cmdiocbq);
  480. free_ndlp:
  481. lpfc_nlp_put(ndlp);
  482. no_ndlp:
  483. kfree(dd_data);
  484. no_dd_data:
  485. /* make error code available to userspace */
  486. job->reply->result = rc;
  487. job->dd_data = NULL;
  488. return rc;
  489. }
  490. /**
  491. * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
  492. * @phba: Pointer to HBA context object.
  493. * @cmdiocbq: Pointer to command iocb.
  494. * @rspiocbq: Pointer to response iocb.
  495. *
  496. * This function is the completion handler for iocbs issued using
  497. * lpfc_bsg_rport_els_cmp function. This function is called by the
  498. * ring event handler function without any lock held. This function
  499. * can be called from both worker thread context and interrupt
  500. * context. This function also can be called from other thread which
  501. * cleans up the SLI layer objects.
  502. * This function copies the contents of the response iocb to the
  503. * response iocb memory object provided by the caller of
  504. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  505. * sleeps for the iocb completion.
  506. **/
  507. static void
  508. lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
  509. struct lpfc_iocbq *cmdiocbq,
  510. struct lpfc_iocbq *rspiocbq)
  511. {
  512. struct bsg_job_data *dd_data;
  513. struct fc_bsg_job *job;
  514. IOCB_t *rsp;
  515. struct lpfc_nodelist *ndlp;
  516. struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
  517. struct fc_bsg_ctels_reply *els_reply;
  518. uint8_t *rjt_data;
  519. unsigned long flags;
  520. unsigned int rsp_size;
  521. int rc = 0;
  522. dd_data = cmdiocbq->context1;
  523. ndlp = dd_data->context_un.iocb.ndlp;
  524. cmdiocbq->context1 = ndlp;
  525. /* Determine if job has been aborted */
  526. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  527. job = dd_data->set_job;
  528. if (job) {
  529. /* Prevent timeout handling from trying to abort job */
  530. job->dd_data = NULL;
  531. }
  532. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  533. /* Close the timeout handler abort window */
  534. spin_lock_irqsave(&phba->hbalock, flags);
  535. cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
  536. spin_unlock_irqrestore(&phba->hbalock, flags);
  537. rsp = &rspiocbq->iocb;
  538. pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
  539. prsp = (struct lpfc_dmabuf *)pcmd->list.next;
  540. /* Copy the completed job data or determine the job status if job is
  541. * still active
  542. */
  543. if (job) {
  544. if (rsp->ulpStatus == IOSTAT_SUCCESS) {
  545. rsp_size = rsp->un.elsreq64.bdl.bdeSize;
  546. job->reply->reply_payload_rcv_len =
  547. sg_copy_from_buffer(job->reply_payload.sg_list,
  548. job->reply_payload.sg_cnt,
  549. prsp->virt,
  550. rsp_size);
  551. } else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
  552. job->reply->reply_payload_rcv_len =
  553. sizeof(struct fc_bsg_ctels_reply);
  554. /* LS_RJT data returned in word 4 */
  555. rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
  556. els_reply = &job->reply->reply_data.ctels_reply;
  557. els_reply->status = FC_CTELS_STATUS_REJECT;
  558. els_reply->rjt_data.action = rjt_data[3];
  559. els_reply->rjt_data.reason_code = rjt_data[2];
  560. els_reply->rjt_data.reason_explanation = rjt_data[1];
  561. els_reply->rjt_data.vendor_unique = rjt_data[0];
  562. } else {
  563. rc = -EIO;
  564. }
  565. }
  566. lpfc_nlp_put(ndlp);
  567. lpfc_els_free_iocb(phba, cmdiocbq);
  568. kfree(dd_data);
  569. /* Complete the job if the job is still active */
  570. if (job) {
  571. job->reply->result = rc;
  572. job->job_done(job);
  573. }
  574. return;
  575. }
  576. /**
  577. * lpfc_bsg_rport_els - send an ELS command from a bsg request
  578. * @job: fc_bsg_job to handle
  579. **/
  580. static int
  581. lpfc_bsg_rport_els(struct fc_bsg_job *job)
  582. {
  583. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  584. struct lpfc_hba *phba = vport->phba;
  585. struct lpfc_rport_data *rdata = job->rport->dd_data;
  586. struct lpfc_nodelist *ndlp = rdata->pnode;
  587. uint32_t elscmd;
  588. uint32_t cmdsize;
  589. struct lpfc_iocbq *cmdiocbq;
  590. uint16_t rpi = 0;
  591. struct bsg_job_data *dd_data;
  592. unsigned long flags;
  593. uint32_t creg_val;
  594. int rc = 0;
  595. /* in case no data is transferred */
  596. job->reply->reply_payload_rcv_len = 0;
  597. /* verify the els command is not greater than the
  598. * maximum ELS transfer size.
  599. */
  600. if (job->request_payload.payload_len > FCELSSIZE) {
  601. rc = -EINVAL;
  602. goto no_dd_data;
  603. }
  604. /* allocate our bsg tracking structure */
  605. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  606. if (!dd_data) {
  607. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  608. "2735 Failed allocation of dd_data\n");
  609. rc = -ENOMEM;
  610. goto no_dd_data;
  611. }
  612. elscmd = job->request->rqst_data.r_els.els_code;
  613. cmdsize = job->request_payload.payload_len;
  614. if (!lpfc_nlp_get(ndlp)) {
  615. rc = -ENODEV;
  616. goto free_dd_data;
  617. }
  618. /* We will use the allocated dma buffers by prep els iocb for command
  619. * and response to ensure if the job times out and the request is freed,
  620. * we won't be dma into memory that is no longer allocated to for the
  621. * request.
  622. */
  623. cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
  624. ndlp->nlp_DID, elscmd);
  625. if (!cmdiocbq) {
  626. rc = -EIO;
  627. goto release_ndlp;
  628. }
  629. rpi = ndlp->nlp_rpi;
  630. /* Transfer the request payload to allocated command dma buffer */
  631. sg_copy_to_buffer(job->request_payload.sg_list,
  632. job->request_payload.sg_cnt,
  633. ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
  634. cmdsize);
  635. if (phba->sli_rev == LPFC_SLI_REV4)
  636. cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
  637. else
  638. cmdiocbq->iocb.ulpContext = rpi;
  639. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  640. cmdiocbq->context1 = dd_data;
  641. cmdiocbq->context_un.ndlp = ndlp;
  642. cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
  643. dd_data->type = TYPE_IOCB;
  644. dd_data->set_job = job;
  645. dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
  646. dd_data->context_un.iocb.ndlp = ndlp;
  647. dd_data->context_un.iocb.rmp = NULL;
  648. job->dd_data = dd_data;
  649. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  650. if (lpfc_readl(phba->HCregaddr, &creg_val)) {
  651. rc = -EIO;
  652. goto linkdown_err;
  653. }
  654. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  655. writel(creg_val, phba->HCregaddr);
  656. readl(phba->HCregaddr); /* flush */
  657. }
  658. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
  659. if (rc == IOCB_SUCCESS) {
  660. spin_lock_irqsave(&phba->hbalock, flags);
  661. /* make sure the I/O had not been completed/released */
  662. if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
  663. /* open up abort window to timeout handler */
  664. cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
  665. }
  666. spin_unlock_irqrestore(&phba->hbalock, flags);
  667. return 0; /* done for now */
  668. } else if (rc == IOCB_BUSY) {
  669. rc = -EAGAIN;
  670. } else {
  671. rc = -EIO;
  672. }
  673. /* iocb failed so cleanup */
  674. job->dd_data = NULL;
  675. linkdown_err:
  676. cmdiocbq->context1 = ndlp;
  677. lpfc_els_free_iocb(phba, cmdiocbq);
  678. release_ndlp:
  679. lpfc_nlp_put(ndlp);
  680. free_dd_data:
  681. kfree(dd_data);
  682. no_dd_data:
  683. /* make error code available to userspace */
  684. job->reply->result = rc;
  685. job->dd_data = NULL;
  686. return rc;
  687. }
  688. /**
  689. * lpfc_bsg_event_free - frees an allocated event structure
  690. * @kref: Pointer to a kref.
  691. *
  692. * Called from kref_put. Back cast the kref into an event structure address.
  693. * Free any events to get, delete associated nodes, free any events to see,
  694. * free any data then free the event itself.
  695. **/
  696. static void
  697. lpfc_bsg_event_free(struct kref *kref)
  698. {
  699. struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
  700. kref);
  701. struct event_data *ed;
  702. list_del(&evt->node);
  703. while (!list_empty(&evt->events_to_get)) {
  704. ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
  705. list_del(&ed->node);
  706. kfree(ed->data);
  707. kfree(ed);
  708. }
  709. while (!list_empty(&evt->events_to_see)) {
  710. ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
  711. list_del(&ed->node);
  712. kfree(ed->data);
  713. kfree(ed);
  714. }
  715. kfree(evt->dd_data);
  716. kfree(evt);
  717. }
  718. /**
  719. * lpfc_bsg_event_ref - increments the kref for an event
  720. * @evt: Pointer to an event structure.
  721. **/
  722. static inline void
  723. lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
  724. {
  725. kref_get(&evt->kref);
  726. }
  727. /**
  728. * lpfc_bsg_event_unref - Uses kref_put to free an event structure
  729. * @evt: Pointer to an event structure.
  730. **/
  731. static inline void
  732. lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
  733. {
  734. kref_put(&evt->kref, lpfc_bsg_event_free);
  735. }
  736. /**
  737. * lpfc_bsg_event_new - allocate and initialize a event structure
  738. * @ev_mask: Mask of events.
  739. * @ev_reg_id: Event reg id.
  740. * @ev_req_id: Event request id.
  741. **/
  742. static struct lpfc_bsg_event *
  743. lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
  744. {
  745. struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  746. if (!evt)
  747. return NULL;
  748. INIT_LIST_HEAD(&evt->events_to_get);
  749. INIT_LIST_HEAD(&evt->events_to_see);
  750. evt->type_mask = ev_mask;
  751. evt->req_id = ev_req_id;
  752. evt->reg_id = ev_reg_id;
  753. evt->wait_time_stamp = jiffies;
  754. evt->dd_data = NULL;
  755. init_waitqueue_head(&evt->wq);
  756. kref_init(&evt->kref);
  757. return evt;
  758. }
  759. /**
  760. * diag_cmd_data_free - Frees an lpfc dma buffer extension
  761. * @phba: Pointer to HBA context object.
  762. * @mlist: Pointer to an lpfc dma buffer extension.
  763. **/
  764. static int
  765. diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
  766. {
  767. struct lpfc_dmabufext *mlast;
  768. struct pci_dev *pcidev;
  769. struct list_head head, *curr, *next;
  770. if ((!mlist) || (!lpfc_is_link_up(phba) &&
  771. (phba->link_flag & LS_LOOPBACK_MODE))) {
  772. return 0;
  773. }
  774. pcidev = phba->pcidev;
  775. list_add_tail(&head, &mlist->dma.list);
  776. list_for_each_safe(curr, next, &head) {
  777. mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
  778. if (mlast->dma.virt)
  779. dma_free_coherent(&pcidev->dev,
  780. mlast->size,
  781. mlast->dma.virt,
  782. mlast->dma.phys);
  783. kfree(mlast);
  784. }
  785. return 0;
  786. }
  787. /**
  788. * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
  789. * @phba:
  790. * @pring:
  791. * @piocbq:
  792. *
  793. * This function is called when an unsolicited CT command is received. It
  794. * forwards the event to any processes registered to receive CT events.
  795. **/
  796. int
  797. lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  798. struct lpfc_iocbq *piocbq)
  799. {
  800. uint32_t evt_req_id = 0;
  801. uint32_t cmd;
  802. 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->iocb_flag |= LPFC_IO_LOOPBACK;
  2832. cmdiocbq->vport = phba->pport;
  2833. cmdiocbq->iocb_cmpl = NULL;
  2834. iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
  2835. rspiocbq, (phba->fc_ratov * 2) +
  2836. LPFC_DRVR_TIMEOUT);
  2837. if ((iocb_stat != IOCB_SUCCESS) ||
  2838. ((phba->sli_rev < LPFC_SLI_REV4) &&
  2839. (rsp->ulpStatus != IOSTAT_SUCCESS))) {
  2840. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2841. "3126 Failed loopback test issue iocb: "
  2842. "iocb_stat:x%x\n", iocb_stat);
  2843. rc = -EIO;
  2844. goto err_loopback_test_exit;
  2845. }
  2846. evt->waiting = 1;
  2847. time_left = wait_event_interruptible_timeout(
  2848. evt->wq, !list_empty(&evt->events_to_see),
  2849. msecs_to_jiffies(1000 *
  2850. ((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
  2851. evt->waiting = 0;
  2852. if (list_empty(&evt->events_to_see)) {
  2853. rc = (time_left) ? -EINTR : -ETIMEDOUT;
  2854. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2855. "3125 Not receiving unsolicited event, "
  2856. "rc:x%x\n", rc);
  2857. } else {
  2858. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2859. list_move(evt->events_to_see.prev, &evt->events_to_get);
  2860. evdat = list_entry(evt->events_to_get.prev,
  2861. typeof(*evdat), node);
  2862. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2863. rx_databuf = evdat->data;
  2864. if (evdat->len != full_size) {
  2865. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  2866. "1603 Loopback test did not receive expected "
  2867. "data length. actual length 0x%x expected "
  2868. "length 0x%x\n",
  2869. evdat->len, full_size);
  2870. rc = -EIO;
  2871. } else if (rx_databuf == NULL)
  2872. rc = -EIO;
  2873. else {
  2874. rc = IOCB_SUCCESS;
  2875. /* skip over elx loopback header */
  2876. rx_databuf += ELX_LOOPBACK_HEADER_SZ;
  2877. job->reply->reply_payload_rcv_len =
  2878. sg_copy_from_buffer(job->reply_payload.sg_list,
  2879. job->reply_payload.sg_cnt,
  2880. rx_databuf, size);
  2881. job->reply->reply_payload_rcv_len = size;
  2882. }
  2883. }
  2884. err_loopback_test_exit:
  2885. lpfcdiag_loop_self_unreg(phba, rpi);
  2886. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2887. lpfc_bsg_event_unref(evt); /* release ref */
  2888. lpfc_bsg_event_unref(evt); /* delete */
  2889. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2890. if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
  2891. lpfc_sli_release_iocbq(phba, cmdiocbq);
  2892. if (rspiocbq != NULL)
  2893. lpfc_sli_release_iocbq(phba, rspiocbq);
  2894. if (txbmp != NULL) {
  2895. if (txbpl != NULL) {
  2896. if (txbuffer != NULL)
  2897. diag_cmd_data_free(phba, txbuffer);
  2898. lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
  2899. }
  2900. kfree(txbmp);
  2901. }
  2902. loopback_test_exit:
  2903. kfree(dataout);
  2904. /* make error code available to userspace */
  2905. job->reply->result = rc;
  2906. job->dd_data = NULL;
  2907. /* complete the job back to userspace if no error */
  2908. if (rc == IOCB_SUCCESS)
  2909. job->job_done(job);
  2910. return rc;
  2911. }
  2912. /**
  2913. * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
  2914. * @job: GET_DFC_REV fc_bsg_job
  2915. **/
  2916. static int
  2917. lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
  2918. {
  2919. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  2920. struct lpfc_hba *phba = vport->phba;
  2921. struct get_mgmt_rev *event_req;
  2922. struct get_mgmt_rev_reply *event_reply;
  2923. int rc = 0;
  2924. if (job->request_len <
  2925. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
  2926. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2927. "2740 Received GET_DFC_REV request below "
  2928. "minimum size\n");
  2929. rc = -EINVAL;
  2930. goto job_error;
  2931. }
  2932. event_req = (struct get_mgmt_rev *)
  2933. job->request->rqst_data.h_vendor.vendor_cmd;
  2934. event_reply = (struct get_mgmt_rev_reply *)
  2935. job->reply->reply_data.vendor_reply.vendor_rsp;
  2936. if (job->reply_len <
  2937. sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
  2938. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  2939. "2741 Received GET_DFC_REV reply below "
  2940. "minimum size\n");
  2941. rc = -EINVAL;
  2942. goto job_error;
  2943. }
  2944. event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
  2945. event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
  2946. job_error:
  2947. job->reply->result = rc;
  2948. if (rc == 0)
  2949. job->job_done(job);
  2950. return rc;
  2951. }
  2952. /**
  2953. * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
  2954. * @phba: Pointer to HBA context object.
  2955. * @pmboxq: Pointer to mailbox command.
  2956. *
  2957. * This is completion handler function for mailbox commands issued from
  2958. * lpfc_bsg_issue_mbox function. This function is called by the
  2959. * mailbox event handler function with no lock held. This function
  2960. * will wake up thread waiting on the wait queue pointed by context1
  2961. * of the mailbox.
  2962. **/
  2963. static void
  2964. lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  2965. {
  2966. struct bsg_job_data *dd_data;
  2967. struct fc_bsg_job *job;
  2968. uint32_t size;
  2969. unsigned long flags;
  2970. uint8_t *pmb, *pmb_buf;
  2971. dd_data = pmboxq->context1;
  2972. /*
  2973. * The outgoing buffer is readily referred from the dma buffer,
  2974. * just need to get header part from mailboxq structure.
  2975. */
  2976. pmb = (uint8_t *)&pmboxq->u.mb;
  2977. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  2978. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  2979. /* Determine if job has been aborted */
  2980. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  2981. job = dd_data->set_job;
  2982. if (job) {
  2983. /* Prevent timeout handling from trying to abort job */
  2984. job->dd_data = NULL;
  2985. }
  2986. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  2987. /* Copy the mailbox data to the job if it is still active */
  2988. if (job) {
  2989. size = job->reply_payload.payload_len;
  2990. job->reply->reply_payload_rcv_len =
  2991. sg_copy_from_buffer(job->reply_payload.sg_list,
  2992. job->reply_payload.sg_cnt,
  2993. pmb_buf, size);
  2994. }
  2995. dd_data->set_job = NULL;
  2996. mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
  2997. lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
  2998. kfree(dd_data);
  2999. /* Complete the job if the job is still active */
  3000. if (job) {
  3001. job->reply->result = 0;
  3002. job->job_done(job);
  3003. }
  3004. return;
  3005. }
  3006. /**
  3007. * lpfc_bsg_check_cmd_access - test for a supported mailbox command
  3008. * @phba: Pointer to HBA context object.
  3009. * @mb: Pointer to a mailbox object.
  3010. * @vport: Pointer to a vport object.
  3011. *
  3012. * Some commands require the port to be offline, some may not be called from
  3013. * the application.
  3014. **/
  3015. static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
  3016. MAILBOX_t *mb, struct lpfc_vport *vport)
  3017. {
  3018. /* return negative error values for bsg job */
  3019. switch (mb->mbxCommand) {
  3020. /* Offline only */
  3021. case MBX_INIT_LINK:
  3022. case MBX_DOWN_LINK:
  3023. case MBX_CONFIG_LINK:
  3024. case MBX_CONFIG_RING:
  3025. case MBX_RESET_RING:
  3026. case MBX_UNREG_LOGIN:
  3027. case MBX_CLEAR_LA:
  3028. case MBX_DUMP_CONTEXT:
  3029. case MBX_RUN_DIAGS:
  3030. case MBX_RESTART:
  3031. case MBX_SET_MASK:
  3032. if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
  3033. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  3034. "2743 Command 0x%x is illegal in on-line "
  3035. "state\n",
  3036. mb->mbxCommand);
  3037. return -EPERM;
  3038. }
  3039. case MBX_WRITE_NV:
  3040. case MBX_WRITE_VPARMS:
  3041. case MBX_LOAD_SM:
  3042. case MBX_READ_NV:
  3043. case MBX_READ_CONFIG:
  3044. case MBX_READ_RCONFIG:
  3045. case MBX_READ_STATUS:
  3046. case MBX_READ_XRI:
  3047. case MBX_READ_REV:
  3048. case MBX_READ_LNK_STAT:
  3049. case MBX_DUMP_MEMORY:
  3050. case MBX_DOWN_LOAD:
  3051. case MBX_UPDATE_CFG:
  3052. case MBX_KILL_BOARD:
  3053. case MBX_READ_TOPOLOGY:
  3054. case MBX_LOAD_AREA:
  3055. case MBX_LOAD_EXP_ROM:
  3056. case MBX_BEACON:
  3057. case MBX_DEL_LD_ENTRY:
  3058. case MBX_SET_DEBUG:
  3059. case MBX_WRITE_WWN:
  3060. case MBX_SLI4_CONFIG:
  3061. case MBX_READ_EVENT_LOG:
  3062. case MBX_READ_EVENT_LOG_STATUS:
  3063. case MBX_WRITE_EVENT_LOG:
  3064. case MBX_PORT_CAPABILITIES:
  3065. case MBX_PORT_IOV_CONTROL:
  3066. case MBX_RUN_BIU_DIAG64:
  3067. break;
  3068. case MBX_SET_VARIABLE:
  3069. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3070. "1226 mbox: set_variable 0x%x, 0x%x\n",
  3071. mb->un.varWords[0],
  3072. mb->un.varWords[1]);
  3073. if ((mb->un.varWords[0] == SETVAR_MLOMNT)
  3074. && (mb->un.varWords[1] == 1)) {
  3075. phba->wait_4_mlo_maint_flg = 1;
  3076. } else if (mb->un.varWords[0] == SETVAR_MLORST) {
  3077. spin_lock_irq(&phba->hbalock);
  3078. phba->link_flag &= ~LS_LOOPBACK_MODE;
  3079. spin_unlock_irq(&phba->hbalock);
  3080. phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
  3081. }
  3082. break;
  3083. case MBX_READ_SPARM64:
  3084. case MBX_REG_LOGIN:
  3085. case MBX_REG_LOGIN64:
  3086. case MBX_CONFIG_PORT:
  3087. case MBX_RUN_BIU_DIAG:
  3088. default:
  3089. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  3090. "2742 Unknown Command 0x%x\n",
  3091. mb->mbxCommand);
  3092. return -EPERM;
  3093. }
  3094. return 0; /* ok */
  3095. }
  3096. /**
  3097. * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
  3098. * @phba: Pointer to HBA context object.
  3099. *
  3100. * This is routine clean up and reset BSG handling of multi-buffer mbox
  3101. * command session.
  3102. **/
  3103. static void
  3104. lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
  3105. {
  3106. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
  3107. return;
  3108. /* free all memory, including dma buffers */
  3109. lpfc_bsg_dma_page_list_free(phba,
  3110. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3111. lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
  3112. /* multi-buffer write mailbox command pass-through complete */
  3113. memset((char *)&phba->mbox_ext_buf_ctx, 0,
  3114. sizeof(struct lpfc_mbox_ext_buf_ctx));
  3115. INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3116. return;
  3117. }
  3118. /**
  3119. * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
  3120. * @phba: Pointer to HBA context object.
  3121. * @pmboxq: Pointer to mailbox command.
  3122. *
  3123. * This is routine handles BSG job for mailbox commands completions with
  3124. * multiple external buffers.
  3125. **/
  3126. static struct fc_bsg_job *
  3127. lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3128. {
  3129. struct bsg_job_data *dd_data;
  3130. struct fc_bsg_job *job;
  3131. uint8_t *pmb, *pmb_buf;
  3132. unsigned long flags;
  3133. uint32_t size;
  3134. int rc = 0;
  3135. struct lpfc_dmabuf *dmabuf;
  3136. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3137. uint8_t *pmbx;
  3138. dd_data = pmboxq->context1;
  3139. /* Determine if job has been aborted */
  3140. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  3141. job = dd_data->set_job;
  3142. if (job) {
  3143. /* Prevent timeout handling from trying to abort job */
  3144. job->dd_data = NULL;
  3145. }
  3146. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  3147. /*
  3148. * The outgoing buffer is readily referred from the dma buffer,
  3149. * just need to get header part from mailboxq structure.
  3150. */
  3151. pmb = (uint8_t *)&pmboxq->u.mb;
  3152. pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
  3153. /* Copy the byte swapped response mailbox back to the user */
  3154. memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
  3155. /* if there is any non-embedded extended data copy that too */
  3156. dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
  3157. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3158. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3159. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3160. pmbx = (uint8_t *)dmabuf->virt;
  3161. /* byte swap the extended data following the mailbox command */
  3162. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3163. &pmbx[sizeof(MAILBOX_t)],
  3164. sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
  3165. }
  3166. /* Complete the job if the job is still active */
  3167. if (job) {
  3168. size = job->reply_payload.payload_len;
  3169. job->reply->reply_payload_rcv_len =
  3170. sg_copy_from_buffer(job->reply_payload.sg_list,
  3171. job->reply_payload.sg_cnt,
  3172. pmb_buf, size);
  3173. /* result for successful */
  3174. job->reply->result = 0;
  3175. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3176. "2937 SLI_CONFIG ext-buffer maibox command "
  3177. "(x%x/x%x) complete bsg job done, bsize:%d\n",
  3178. phba->mbox_ext_buf_ctx.nembType,
  3179. phba->mbox_ext_buf_ctx.mboxType, size);
  3180. lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
  3181. phba->mbox_ext_buf_ctx.nembType,
  3182. phba->mbox_ext_buf_ctx.mboxType,
  3183. dma_ebuf, sta_pos_addr,
  3184. phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
  3185. } else {
  3186. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3187. "2938 SLI_CONFIG ext-buffer maibox "
  3188. "command (x%x/x%x) failure, rc:x%x\n",
  3189. phba->mbox_ext_buf_ctx.nembType,
  3190. phba->mbox_ext_buf_ctx.mboxType, rc);
  3191. }
  3192. /* state change */
  3193. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
  3194. kfree(dd_data);
  3195. return job;
  3196. }
  3197. /**
  3198. * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
  3199. * @phba: Pointer to HBA context object.
  3200. * @pmboxq: Pointer to mailbox command.
  3201. *
  3202. * This is completion handler function for mailbox read commands with multiple
  3203. * external buffers.
  3204. **/
  3205. static void
  3206. lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3207. {
  3208. struct fc_bsg_job *job;
  3209. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3210. /* handle the BSG job with mailbox command */
  3211. if (!job)
  3212. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3213. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3214. "2939 SLI_CONFIG ext-buffer rd maibox command "
  3215. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3216. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3217. if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
  3218. lpfc_bsg_mbox_ext_session_reset(phba);
  3219. /* free base driver mailbox structure memory */
  3220. mempool_free(pmboxq, phba->mbox_mem_pool);
  3221. /* if the job is still active, call job done */
  3222. if (job)
  3223. job->job_done(job);
  3224. return;
  3225. }
  3226. /**
  3227. * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
  3228. * @phba: Pointer to HBA context object.
  3229. * @pmboxq: Pointer to mailbox command.
  3230. *
  3231. * This is completion handler function for mailbox write commands with multiple
  3232. * external buffers.
  3233. **/
  3234. static void
  3235. lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  3236. {
  3237. struct fc_bsg_job *job;
  3238. job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
  3239. /* handle the BSG job with the mailbox command */
  3240. if (!job)
  3241. pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
  3242. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3243. "2940 SLI_CONFIG ext-buffer wr maibox command "
  3244. "complete, ctxState:x%x, mbxStatus:x%x\n",
  3245. phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
  3246. /* free all memory, including dma buffers */
  3247. mempool_free(pmboxq, phba->mbox_mem_pool);
  3248. lpfc_bsg_mbox_ext_session_reset(phba);
  3249. /* if the job is still active, call job done */
  3250. if (job)
  3251. job->job_done(job);
  3252. return;
  3253. }
  3254. static void
  3255. lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
  3256. uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
  3257. struct lpfc_dmabuf *ext_dmabuf)
  3258. {
  3259. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3260. /* pointer to the start of mailbox command */
  3261. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
  3262. if (nemb_tp == nemb_mse) {
  3263. if (index == 0) {
  3264. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3265. mse[index].pa_hi =
  3266. putPaddrHigh(mbx_dmabuf->phys +
  3267. sizeof(MAILBOX_t));
  3268. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3269. mse[index].pa_lo =
  3270. putPaddrLow(mbx_dmabuf->phys +
  3271. sizeof(MAILBOX_t));
  3272. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3273. "2943 SLI_CONFIG(mse)[%d], "
  3274. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3275. index,
  3276. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3277. mse[index].buf_len,
  3278. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3279. mse[index].pa_hi,
  3280. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3281. mse[index].pa_lo);
  3282. } else {
  3283. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3284. mse[index].pa_hi =
  3285. putPaddrHigh(ext_dmabuf->phys);
  3286. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3287. mse[index].pa_lo =
  3288. putPaddrLow(ext_dmabuf->phys);
  3289. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3290. "2944 SLI_CONFIG(mse)[%d], "
  3291. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3292. index,
  3293. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3294. mse[index].buf_len,
  3295. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3296. mse[index].pa_hi,
  3297. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3298. mse[index].pa_lo);
  3299. }
  3300. } else {
  3301. if (index == 0) {
  3302. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3303. hbd[index].pa_hi =
  3304. putPaddrHigh(mbx_dmabuf->phys +
  3305. sizeof(MAILBOX_t));
  3306. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3307. hbd[index].pa_lo =
  3308. putPaddrLow(mbx_dmabuf->phys +
  3309. sizeof(MAILBOX_t));
  3310. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3311. "3007 SLI_CONFIG(hbd)[%d], "
  3312. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3313. index,
  3314. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3315. &sli_cfg_mbx->un.
  3316. sli_config_emb1_subsys.hbd[index]),
  3317. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3318. hbd[index].pa_hi,
  3319. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3320. hbd[index].pa_lo);
  3321. } else {
  3322. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3323. hbd[index].pa_hi =
  3324. putPaddrHigh(ext_dmabuf->phys);
  3325. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3326. hbd[index].pa_lo =
  3327. putPaddrLow(ext_dmabuf->phys);
  3328. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3329. "3008 SLI_CONFIG(hbd)[%d], "
  3330. "bufLen:%d, addrHi:x%x, addrLo:x%x\n",
  3331. index,
  3332. bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3333. &sli_cfg_mbx->un.
  3334. sli_config_emb1_subsys.hbd[index]),
  3335. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3336. hbd[index].pa_hi,
  3337. sli_cfg_mbx->un.sli_config_emb1_subsys.
  3338. hbd[index].pa_lo);
  3339. }
  3340. }
  3341. return;
  3342. }
  3343. /**
  3344. * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
  3345. * @phba: Pointer to HBA context object.
  3346. * @mb: Pointer to a BSG mailbox object.
  3347. * @nemb_tp: Enumerate of non-embedded mailbox command type.
  3348. * @dmabuff: Pointer to a DMA buffer descriptor.
  3349. *
  3350. * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
  3351. * non-embedded external bufffers.
  3352. **/
  3353. static int
  3354. lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3355. enum nemb_type nemb_tp,
  3356. struct lpfc_dmabuf *dmabuf)
  3357. {
  3358. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3359. struct dfc_mbox_req *mbox_req;
  3360. struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
  3361. uint32_t ext_buf_cnt, ext_buf_index;
  3362. struct lpfc_dmabuf *ext_dmabuf = NULL;
  3363. struct bsg_job_data *dd_data = NULL;
  3364. LPFC_MBOXQ_t *pmboxq = NULL;
  3365. MAILBOX_t *pmb;
  3366. uint8_t *pmbx;
  3367. int rc, i;
  3368. mbox_req =
  3369. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3370. /* pointer to the start of mailbox command */
  3371. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3372. if (nemb_tp == nemb_mse) {
  3373. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3374. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3375. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3376. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3377. "2945 Handled SLI_CONFIG(mse) rd, "
  3378. "ext_buf_cnt(%d) out of range(%d)\n",
  3379. ext_buf_cnt,
  3380. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3381. rc = -ERANGE;
  3382. goto job_error;
  3383. }
  3384. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3385. "2941 Handled SLI_CONFIG(mse) rd, "
  3386. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3387. } else {
  3388. /* sanity check on interface type for support */
  3389. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3390. LPFC_SLI_INTF_IF_TYPE_2) {
  3391. rc = -ENODEV;
  3392. goto job_error;
  3393. }
  3394. /* nemb_tp == nemb_hbd */
  3395. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3396. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3397. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3398. "2946 Handled SLI_CONFIG(hbd) rd, "
  3399. "ext_buf_cnt(%d) out of range(%d)\n",
  3400. ext_buf_cnt,
  3401. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3402. rc = -ERANGE;
  3403. goto job_error;
  3404. }
  3405. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3406. "2942 Handled SLI_CONFIG(hbd) rd, "
  3407. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3408. }
  3409. /* before dma descriptor setup */
  3410. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3411. sta_pre_addr, dmabuf, ext_buf_cnt);
  3412. /* reject non-embedded mailbox command with none external buffer */
  3413. if (ext_buf_cnt == 0) {
  3414. rc = -EPERM;
  3415. goto job_error;
  3416. } else if (ext_buf_cnt > 1) {
  3417. /* additional external read buffers */
  3418. for (i = 1; i < ext_buf_cnt; i++) {
  3419. ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
  3420. if (!ext_dmabuf) {
  3421. rc = -ENOMEM;
  3422. goto job_error;
  3423. }
  3424. list_add_tail(&ext_dmabuf->list,
  3425. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3426. }
  3427. }
  3428. /* bsg tracking structure */
  3429. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3430. if (!dd_data) {
  3431. rc = -ENOMEM;
  3432. goto job_error;
  3433. }
  3434. /* mailbox command structure for base driver */
  3435. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3436. if (!pmboxq) {
  3437. rc = -ENOMEM;
  3438. goto job_error;
  3439. }
  3440. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3441. /* for the first external buffer */
  3442. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3443. /* for the rest of external buffer descriptors if any */
  3444. if (ext_buf_cnt > 1) {
  3445. ext_buf_index = 1;
  3446. list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
  3447. &phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
  3448. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
  3449. ext_buf_index, dmabuf,
  3450. curr_dmabuf);
  3451. ext_buf_index++;
  3452. }
  3453. }
  3454. /* after dma descriptor setup */
  3455. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
  3456. sta_pos_addr, dmabuf, ext_buf_cnt);
  3457. /* construct base driver mbox command */
  3458. pmb = &pmboxq->u.mb;
  3459. pmbx = (uint8_t *)dmabuf->virt;
  3460. memcpy(pmb, pmbx, sizeof(*pmb));
  3461. pmb->mbxOwner = OWN_HOST;
  3462. pmboxq->vport = phba->pport;
  3463. /* multi-buffer handling context */
  3464. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3465. phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
  3466. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3467. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3468. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3469. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3470. /* callback for multi-buffer read mailbox command */
  3471. pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
  3472. /* context fields to callback function */
  3473. pmboxq->context1 = dd_data;
  3474. dd_data->type = TYPE_MBOX;
  3475. dd_data->set_job = job;
  3476. dd_data->context_un.mbox.pmboxq = pmboxq;
  3477. dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
  3478. job->dd_data = dd_data;
  3479. /* state change */
  3480. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3481. /*
  3482. * Non-embedded mailbox subcommand data gets byte swapped here because
  3483. * the lower level driver code only does the first 64 mailbox words.
  3484. */
  3485. if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
  3486. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
  3487. (nemb_tp == nemb_mse))
  3488. lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
  3489. &pmbx[sizeof(MAILBOX_t)],
  3490. sli_cfg_mbx->un.sli_config_emb0_subsys.
  3491. mse[0].buf_len);
  3492. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3493. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3494. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3495. "2947 Issued SLI_CONFIG ext-buffer "
  3496. "maibox command, rc:x%x\n", rc);
  3497. return SLI_CONFIG_HANDLED;
  3498. }
  3499. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3500. "2948 Failed to issue SLI_CONFIG ext-buffer "
  3501. "maibox command, rc:x%x\n", rc);
  3502. rc = -EPIPE;
  3503. job_error:
  3504. if (pmboxq)
  3505. mempool_free(pmboxq, phba->mbox_mem_pool);
  3506. lpfc_bsg_dma_page_list_free(phba,
  3507. &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3508. kfree(dd_data);
  3509. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
  3510. return rc;
  3511. }
  3512. /**
  3513. * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
  3514. * @phba: Pointer to HBA context object.
  3515. * @mb: Pointer to a BSG mailbox object.
  3516. * @dmabuff: Pointer to a DMA buffer descriptor.
  3517. *
  3518. * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
  3519. * non-embedded external bufffers.
  3520. **/
  3521. static int
  3522. lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3523. enum nemb_type nemb_tp,
  3524. struct lpfc_dmabuf *dmabuf)
  3525. {
  3526. struct dfc_mbox_req *mbox_req;
  3527. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3528. uint32_t ext_buf_cnt;
  3529. struct bsg_job_data *dd_data = NULL;
  3530. LPFC_MBOXQ_t *pmboxq = NULL;
  3531. MAILBOX_t *pmb;
  3532. uint8_t *mbx;
  3533. int rc = SLI_CONFIG_NOT_HANDLED, i;
  3534. mbox_req =
  3535. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  3536. /* pointer to the start of mailbox command */
  3537. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3538. if (nemb_tp == nemb_mse) {
  3539. ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
  3540. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
  3541. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
  3542. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3543. "2953 Failed SLI_CONFIG(mse) wr, "
  3544. "ext_buf_cnt(%d) out of range(%d)\n",
  3545. ext_buf_cnt,
  3546. LPFC_MBX_SLI_CONFIG_MAX_MSE);
  3547. return -ERANGE;
  3548. }
  3549. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3550. "2949 Handled SLI_CONFIG(mse) wr, "
  3551. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3552. } else {
  3553. /* sanity check on interface type for support */
  3554. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
  3555. LPFC_SLI_INTF_IF_TYPE_2)
  3556. return -ENODEV;
  3557. /* nemb_tp == nemb_hbd */
  3558. ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
  3559. if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
  3560. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3561. "2954 Failed SLI_CONFIG(hbd) wr, "
  3562. "ext_buf_cnt(%d) out of range(%d)\n",
  3563. ext_buf_cnt,
  3564. LPFC_MBX_SLI_CONFIG_MAX_HBD);
  3565. return -ERANGE;
  3566. }
  3567. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3568. "2950 Handled SLI_CONFIG(hbd) wr, "
  3569. "ext_buf_cnt:%d\n", ext_buf_cnt);
  3570. }
  3571. /* before dma buffer descriptor setup */
  3572. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3573. sta_pre_addr, dmabuf, ext_buf_cnt);
  3574. if (ext_buf_cnt == 0)
  3575. return -EPERM;
  3576. /* for the first external buffer */
  3577. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
  3578. /* after dma descriptor setup */
  3579. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
  3580. sta_pos_addr, dmabuf, ext_buf_cnt);
  3581. /* log for looking forward */
  3582. for (i = 1; i < ext_buf_cnt; i++) {
  3583. if (nemb_tp == nemb_mse)
  3584. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3585. "2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
  3586. i, sli_cfg_mbx->un.sli_config_emb0_subsys.
  3587. mse[i].buf_len);
  3588. else
  3589. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3590. "2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
  3591. i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3592. &sli_cfg_mbx->un.sli_config_emb1_subsys.
  3593. hbd[i]));
  3594. }
  3595. /* multi-buffer handling context */
  3596. phba->mbox_ext_buf_ctx.nembType = nemb_tp;
  3597. phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
  3598. phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
  3599. phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
  3600. phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
  3601. phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
  3602. if (ext_buf_cnt == 1) {
  3603. /* bsg tracking structure */
  3604. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3605. if (!dd_data) {
  3606. rc = -ENOMEM;
  3607. goto job_error;
  3608. }
  3609. /* mailbox command structure for base driver */
  3610. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3611. if (!pmboxq) {
  3612. rc = -ENOMEM;
  3613. goto job_error;
  3614. }
  3615. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3616. pmb = &pmboxq->u.mb;
  3617. mbx = (uint8_t *)dmabuf->virt;
  3618. memcpy(pmb, mbx, sizeof(*pmb));
  3619. pmb->mbxOwner = OWN_HOST;
  3620. pmboxq->vport = phba->pport;
  3621. /* callback for multi-buffer read mailbox command */
  3622. pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
  3623. /* context fields to callback function */
  3624. pmboxq->context1 = dd_data;
  3625. dd_data->type = TYPE_MBOX;
  3626. dd_data->set_job = job;
  3627. dd_data->context_un.mbox.pmboxq = pmboxq;
  3628. dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
  3629. job->dd_data = dd_data;
  3630. /* state change */
  3631. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3632. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3633. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3634. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3635. "2955 Issued SLI_CONFIG ext-buffer "
  3636. "maibox command, rc:x%x\n", rc);
  3637. return SLI_CONFIG_HANDLED;
  3638. }
  3639. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3640. "2956 Failed to issue SLI_CONFIG ext-buffer "
  3641. "maibox command, rc:x%x\n", rc);
  3642. rc = -EPIPE;
  3643. goto job_error;
  3644. }
  3645. /* wait for additoinal external buffers */
  3646. job->reply->result = 0;
  3647. job->job_done(job);
  3648. return SLI_CONFIG_HANDLED;
  3649. job_error:
  3650. if (pmboxq)
  3651. mempool_free(pmboxq, phba->mbox_mem_pool);
  3652. kfree(dd_data);
  3653. return rc;
  3654. }
  3655. /**
  3656. * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
  3657. * @phba: Pointer to HBA context object.
  3658. * @mb: Pointer to a BSG mailbox object.
  3659. * @dmabuff: Pointer to a DMA buffer descriptor.
  3660. *
  3661. * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
  3662. * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
  3663. * with embedded sussystem 0x1 and opcodes with external HBDs.
  3664. **/
  3665. static int
  3666. lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3667. struct lpfc_dmabuf *dmabuf)
  3668. {
  3669. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3670. uint32_t subsys;
  3671. uint32_t opcode;
  3672. int rc = SLI_CONFIG_NOT_HANDLED;
  3673. /* state change on new multi-buffer pass-through mailbox command */
  3674. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
  3675. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
  3676. if (!bsg_bf_get(lpfc_mbox_hdr_emb,
  3677. &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
  3678. subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
  3679. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3680. opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
  3681. &sli_cfg_mbx->un.sli_config_emb0_subsys);
  3682. if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
  3683. switch (opcode) {
  3684. case FCOE_OPCODE_READ_FCF:
  3685. case FCOE_OPCODE_GET_DPORT_RESULTS:
  3686. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3687. "2957 Handled SLI_CONFIG "
  3688. "subsys_fcoe, opcode:x%x\n",
  3689. opcode);
  3690. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3691. nemb_mse, dmabuf);
  3692. break;
  3693. case FCOE_OPCODE_ADD_FCF:
  3694. case FCOE_OPCODE_SET_DPORT_MODE:
  3695. case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
  3696. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3697. "2958 Handled SLI_CONFIG "
  3698. "subsys_fcoe, opcode:x%x\n",
  3699. opcode);
  3700. rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
  3701. nemb_mse, dmabuf);
  3702. break;
  3703. default:
  3704. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3705. "2959 Reject SLI_CONFIG "
  3706. "subsys_fcoe, opcode:x%x\n",
  3707. opcode);
  3708. rc = -EPERM;
  3709. break;
  3710. }
  3711. } else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
  3712. switch (opcode) {
  3713. case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
  3714. case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
  3715. case COMN_OPCODE_GET_PROFILE_CONFIG:
  3716. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3717. "3106 Handled SLI_CONFIG "
  3718. "subsys_comn, opcode:x%x\n",
  3719. opcode);
  3720. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3721. nemb_mse, dmabuf);
  3722. break;
  3723. default:
  3724. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3725. "3107 Reject SLI_CONFIG "
  3726. "subsys_comn, opcode:x%x\n",
  3727. opcode);
  3728. rc = -EPERM;
  3729. break;
  3730. }
  3731. } else {
  3732. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3733. "2977 Reject SLI_CONFIG "
  3734. "subsys:x%d, opcode:x%x\n",
  3735. subsys, opcode);
  3736. rc = -EPERM;
  3737. }
  3738. } else {
  3739. subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
  3740. &sli_cfg_mbx->un.sli_config_emb1_subsys);
  3741. opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
  3742. &sli_cfg_mbx->un.sli_config_emb1_subsys);
  3743. if (subsys == SLI_CONFIG_SUBSYS_COMN) {
  3744. switch (opcode) {
  3745. case COMN_OPCODE_READ_OBJECT:
  3746. case COMN_OPCODE_READ_OBJECT_LIST:
  3747. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3748. "2960 Handled SLI_CONFIG "
  3749. "subsys_comn, opcode:x%x\n",
  3750. opcode);
  3751. rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
  3752. nemb_hbd, dmabuf);
  3753. break;
  3754. case COMN_OPCODE_WRITE_OBJECT:
  3755. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3756. "2961 Handled SLI_CONFIG "
  3757. "subsys_comn, opcode:x%x\n",
  3758. opcode);
  3759. rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
  3760. nemb_hbd, dmabuf);
  3761. break;
  3762. default:
  3763. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3764. "2962 Not handled SLI_CONFIG "
  3765. "subsys_comn, opcode:x%x\n",
  3766. opcode);
  3767. rc = SLI_CONFIG_NOT_HANDLED;
  3768. break;
  3769. }
  3770. } else {
  3771. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3772. "2978 Not handled SLI_CONFIG "
  3773. "subsys:x%d, opcode:x%x\n",
  3774. subsys, opcode);
  3775. rc = SLI_CONFIG_NOT_HANDLED;
  3776. }
  3777. }
  3778. /* state reset on not handled new multi-buffer mailbox command */
  3779. if (rc != SLI_CONFIG_HANDLED)
  3780. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
  3781. return rc;
  3782. }
  3783. /**
  3784. * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
  3785. * @phba: Pointer to HBA context object.
  3786. *
  3787. * This routine is for requesting to abort a pass-through mailbox command with
  3788. * multiple external buffers due to error condition.
  3789. **/
  3790. static void
  3791. lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
  3792. {
  3793. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
  3794. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
  3795. else
  3796. lpfc_bsg_mbox_ext_session_reset(phba);
  3797. return;
  3798. }
  3799. /**
  3800. * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
  3801. * @phba: Pointer to HBA context object.
  3802. * @dmabuf: Pointer to a DMA buffer descriptor.
  3803. *
  3804. * This routine extracts the next mailbox read external buffer back to
  3805. * user space through BSG.
  3806. **/
  3807. static int
  3808. lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
  3809. {
  3810. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3811. struct lpfc_dmabuf *dmabuf;
  3812. uint8_t *pbuf;
  3813. uint32_t size;
  3814. uint32_t index;
  3815. index = phba->mbox_ext_buf_ctx.seqNum;
  3816. phba->mbox_ext_buf_ctx.seqNum++;
  3817. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
  3818. phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3819. if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
  3820. size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
  3821. &sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
  3822. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3823. "2963 SLI_CONFIG (mse) ext-buffer rd get "
  3824. "buffer[%d], size:%d\n", index, size);
  3825. } else {
  3826. size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
  3827. &sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
  3828. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3829. "2964 SLI_CONFIG (hbd) ext-buffer rd get "
  3830. "buffer[%d], size:%d\n", index, size);
  3831. }
  3832. if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
  3833. return -EPIPE;
  3834. dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
  3835. struct lpfc_dmabuf, list);
  3836. list_del_init(&dmabuf->list);
  3837. /* after dma buffer descriptor setup */
  3838. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
  3839. mbox_rd, dma_ebuf, sta_pos_addr,
  3840. dmabuf, index);
  3841. pbuf = (uint8_t *)dmabuf->virt;
  3842. job->reply->reply_payload_rcv_len =
  3843. sg_copy_from_buffer(job->reply_payload.sg_list,
  3844. job->reply_payload.sg_cnt,
  3845. pbuf, size);
  3846. lpfc_bsg_dma_page_free(phba, dmabuf);
  3847. if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
  3848. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3849. "2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
  3850. "command session done\n");
  3851. lpfc_bsg_mbox_ext_session_reset(phba);
  3852. }
  3853. job->reply->result = 0;
  3854. job->job_done(job);
  3855. return SLI_CONFIG_HANDLED;
  3856. }
  3857. /**
  3858. * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
  3859. * @phba: Pointer to HBA context object.
  3860. * @dmabuf: Pointer to a DMA buffer descriptor.
  3861. *
  3862. * This routine sets up the next mailbox read external buffer obtained
  3863. * from user space through BSG.
  3864. **/
  3865. static int
  3866. lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3867. struct lpfc_dmabuf *dmabuf)
  3868. {
  3869. struct lpfc_sli_config_mbox *sli_cfg_mbx;
  3870. struct bsg_job_data *dd_data = NULL;
  3871. LPFC_MBOXQ_t *pmboxq = NULL;
  3872. MAILBOX_t *pmb;
  3873. enum nemb_type nemb_tp;
  3874. uint8_t *pbuf;
  3875. uint32_t size;
  3876. uint32_t index;
  3877. int rc;
  3878. index = phba->mbox_ext_buf_ctx.seqNum;
  3879. phba->mbox_ext_buf_ctx.seqNum++;
  3880. nemb_tp = phba->mbox_ext_buf_ctx.nembType;
  3881. sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
  3882. phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3883. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  3884. if (!dd_data) {
  3885. rc = -ENOMEM;
  3886. goto job_error;
  3887. }
  3888. pbuf = (uint8_t *)dmabuf->virt;
  3889. size = job->request_payload.payload_len;
  3890. sg_copy_to_buffer(job->request_payload.sg_list,
  3891. job->request_payload.sg_cnt,
  3892. pbuf, size);
  3893. if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
  3894. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3895. "2966 SLI_CONFIG (mse) ext-buffer wr set "
  3896. "buffer[%d], size:%d\n",
  3897. phba->mbox_ext_buf_ctx.seqNum, size);
  3898. } else {
  3899. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3900. "2967 SLI_CONFIG (hbd) ext-buffer wr set "
  3901. "buffer[%d], size:%d\n",
  3902. phba->mbox_ext_buf_ctx.seqNum, size);
  3903. }
  3904. /* set up external buffer descriptor and add to external buffer list */
  3905. lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
  3906. phba->mbox_ext_buf_ctx.mbx_dmabuf,
  3907. dmabuf);
  3908. list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
  3909. /* after write dma buffer */
  3910. lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
  3911. mbox_wr, dma_ebuf, sta_pos_addr,
  3912. dmabuf, index);
  3913. if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
  3914. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3915. "2968 SLI_CONFIG ext-buffer wr all %d "
  3916. "ebuffers received\n",
  3917. phba->mbox_ext_buf_ctx.numBuf);
  3918. /* mailbox command structure for base driver */
  3919. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3920. if (!pmboxq) {
  3921. rc = -ENOMEM;
  3922. goto job_error;
  3923. }
  3924. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  3925. pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
  3926. pmb = &pmboxq->u.mb;
  3927. memcpy(pmb, pbuf, sizeof(*pmb));
  3928. pmb->mbxOwner = OWN_HOST;
  3929. pmboxq->vport = phba->pport;
  3930. /* callback for multi-buffer write mailbox command */
  3931. pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
  3932. /* context fields to callback function */
  3933. pmboxq->context1 = dd_data;
  3934. dd_data->type = TYPE_MBOX;
  3935. dd_data->set_job = job;
  3936. dd_data->context_un.mbox.pmboxq = pmboxq;
  3937. dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
  3938. job->dd_data = dd_data;
  3939. /* state change */
  3940. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
  3941. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  3942. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
  3943. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3944. "2969 Issued SLI_CONFIG ext-buffer "
  3945. "maibox command, rc:x%x\n", rc);
  3946. return SLI_CONFIG_HANDLED;
  3947. }
  3948. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3949. "2970 Failed to issue SLI_CONFIG ext-buffer "
  3950. "maibox command, rc:x%x\n", rc);
  3951. rc = -EPIPE;
  3952. goto job_error;
  3953. }
  3954. /* wait for additoinal external buffers */
  3955. job->reply->result = 0;
  3956. job->job_done(job);
  3957. return SLI_CONFIG_HANDLED;
  3958. job_error:
  3959. lpfc_bsg_dma_page_free(phba, dmabuf);
  3960. kfree(dd_data);
  3961. return rc;
  3962. }
  3963. /**
  3964. * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
  3965. * @phba: Pointer to HBA context object.
  3966. * @mb: Pointer to a BSG mailbox object.
  3967. * @dmabuff: Pointer to a DMA buffer descriptor.
  3968. *
  3969. * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
  3970. * command with multiple non-embedded external buffers.
  3971. **/
  3972. static int
  3973. lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
  3974. struct lpfc_dmabuf *dmabuf)
  3975. {
  3976. int rc;
  3977. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  3978. "2971 SLI_CONFIG buffer (type:x%x)\n",
  3979. phba->mbox_ext_buf_ctx.mboxType);
  3980. if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
  3981. if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
  3982. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3983. "2972 SLI_CONFIG rd buffer state "
  3984. "mismatch:x%x\n",
  3985. phba->mbox_ext_buf_ctx.state);
  3986. lpfc_bsg_mbox_ext_abort(phba);
  3987. return -EPIPE;
  3988. }
  3989. rc = lpfc_bsg_read_ebuf_get(phba, job);
  3990. if (rc == SLI_CONFIG_HANDLED)
  3991. lpfc_bsg_dma_page_free(phba, dmabuf);
  3992. } else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
  3993. if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
  3994. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  3995. "2973 SLI_CONFIG wr buffer state "
  3996. "mismatch:x%x\n",
  3997. phba->mbox_ext_buf_ctx.state);
  3998. lpfc_bsg_mbox_ext_abort(phba);
  3999. return -EPIPE;
  4000. }
  4001. rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
  4002. }
  4003. return rc;
  4004. }
  4005. /**
  4006. * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
  4007. * @phba: Pointer to HBA context object.
  4008. * @mb: Pointer to a BSG mailbox object.
  4009. * @dmabuff: Pointer to a DMA buffer descriptor.
  4010. *
  4011. * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
  4012. * (0x9B) mailbox commands and external buffers.
  4013. **/
  4014. static int
  4015. lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
  4016. struct lpfc_dmabuf *dmabuf)
  4017. {
  4018. struct dfc_mbox_req *mbox_req;
  4019. int rc = SLI_CONFIG_NOT_HANDLED;
  4020. mbox_req =
  4021. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  4022. /* mbox command with/without single external buffer */
  4023. if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
  4024. return rc;
  4025. /* mbox command and first external buffer */
  4026. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
  4027. if (mbox_req->extSeqNum == 1) {
  4028. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  4029. "2974 SLI_CONFIG mailbox: tag:%d, "
  4030. "seq:%d\n", mbox_req->extMboxTag,
  4031. mbox_req->extSeqNum);
  4032. rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
  4033. return rc;
  4034. } else
  4035. goto sli_cfg_ext_error;
  4036. }
  4037. /*
  4038. * handle additional external buffers
  4039. */
  4040. /* check broken pipe conditions */
  4041. if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
  4042. goto sli_cfg_ext_error;
  4043. if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
  4044. goto sli_cfg_ext_error;
  4045. if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
  4046. goto sli_cfg_ext_error;
  4047. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  4048. "2975 SLI_CONFIG mailbox external buffer: "
  4049. "extSta:x%x, tag:%d, seq:%d\n",
  4050. phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
  4051. mbox_req->extSeqNum);
  4052. rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
  4053. return rc;
  4054. sli_cfg_ext_error:
  4055. /* all other cases, broken pipe */
  4056. lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
  4057. "2976 SLI_CONFIG mailbox broken pipe: "
  4058. "ctxSta:x%x, ctxNumBuf:%d "
  4059. "ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
  4060. phba->mbox_ext_buf_ctx.state,
  4061. phba->mbox_ext_buf_ctx.numBuf,
  4062. phba->mbox_ext_buf_ctx.mbxTag,
  4063. phba->mbox_ext_buf_ctx.seqNum,
  4064. mbox_req->extMboxTag, mbox_req->extSeqNum);
  4065. lpfc_bsg_mbox_ext_session_reset(phba);
  4066. return -EPIPE;
  4067. }
  4068. /**
  4069. * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
  4070. * @phba: Pointer to HBA context object.
  4071. * @mb: Pointer to a mailbox object.
  4072. * @vport: Pointer to a vport object.
  4073. *
  4074. * Allocate a tracking object, mailbox command memory, get a mailbox
  4075. * from the mailbox pool, copy the caller mailbox command.
  4076. *
  4077. * If offline and the sli is active we need to poll for the command (port is
  4078. * being reset) and com-plete the job, otherwise issue the mailbox command and
  4079. * let our completion handler finish the command.
  4080. **/
  4081. static int
  4082. lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
  4083. struct lpfc_vport *vport)
  4084. {
  4085. LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
  4086. MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
  4087. /* a 4k buffer to hold the mb and extended data from/to the bsg */
  4088. uint8_t *pmbx = NULL;
  4089. struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
  4090. struct lpfc_dmabuf *dmabuf = NULL;
  4091. struct dfc_mbox_req *mbox_req;
  4092. struct READ_EVENT_LOG_VAR *rdEventLog;
  4093. uint32_t transmit_length, receive_length, mode;
  4094. struct lpfc_mbx_sli4_config *sli4_config;
  4095. struct lpfc_mbx_nembed_cmd *nembed_sge;
  4096. struct mbox_header *header;
  4097. struct ulp_bde64 *bde;
  4098. uint8_t *ext = NULL;
  4099. int rc = 0;
  4100. uint8_t *from;
  4101. uint32_t size;
  4102. /* in case no data is transferred */
  4103. job->reply->reply_payload_rcv_len = 0;
  4104. /* sanity check to protect driver */
  4105. if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
  4106. job->request_payload.payload_len > BSG_MBOX_SIZE) {
  4107. rc = -ERANGE;
  4108. goto job_done;
  4109. }
  4110. /*
  4111. * Don't allow mailbox commands to be sent when blocked or when in
  4112. * the middle of discovery
  4113. */
  4114. if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
  4115. rc = -EAGAIN;
  4116. goto job_done;
  4117. }
  4118. mbox_req =
  4119. (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
  4120. /* check if requested extended data lengths are valid */
  4121. if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
  4122. (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
  4123. rc = -ERANGE;
  4124. goto job_done;
  4125. }
  4126. dmabuf = lpfc_bsg_dma_page_alloc(phba);
  4127. if (!dmabuf || !dmabuf->virt) {
  4128. rc = -ENOMEM;
  4129. goto job_done;
  4130. }
  4131. /* Get the mailbox command or external buffer from BSG */
  4132. pmbx = (uint8_t *)dmabuf->virt;
  4133. size = job->request_payload.payload_len;
  4134. sg_copy_to_buffer(job->request_payload.sg_list,
  4135. job->request_payload.sg_cnt, pmbx, size);
  4136. /* Handle possible SLI_CONFIG with non-embedded payloads */
  4137. if (phba->sli_rev == LPFC_SLI_REV4) {
  4138. rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
  4139. if (rc == SLI_CONFIG_HANDLED)
  4140. goto job_cont;
  4141. if (rc)
  4142. goto job_done;
  4143. /* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
  4144. }
  4145. rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
  4146. if (rc != 0)
  4147. goto job_done; /* must be negative */
  4148. /* allocate our bsg tracking structure */
  4149. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  4150. if (!dd_data) {
  4151. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4152. "2727 Failed allocation of dd_data\n");
  4153. rc = -ENOMEM;
  4154. goto job_done;
  4155. }
  4156. pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4157. if (!pmboxq) {
  4158. rc = -ENOMEM;
  4159. goto job_done;
  4160. }
  4161. memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
  4162. pmb = &pmboxq->u.mb;
  4163. memcpy(pmb, pmbx, sizeof(*pmb));
  4164. pmb->mbxOwner = OWN_HOST;
  4165. pmboxq->vport = vport;
  4166. /* If HBA encountered an error attention, allow only DUMP
  4167. * or RESTART mailbox commands until the HBA is restarted.
  4168. */
  4169. if (phba->pport->stopped &&
  4170. pmb->mbxCommand != MBX_DUMP_MEMORY &&
  4171. pmb->mbxCommand != MBX_RESTART &&
  4172. pmb->mbxCommand != MBX_WRITE_VPARMS &&
  4173. pmb->mbxCommand != MBX_WRITE_WWN)
  4174. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
  4175. "2797 mbox: Issued mailbox cmd "
  4176. "0x%x while in stopped state.\n",
  4177. pmb->mbxCommand);
  4178. /* extended mailbox commands will need an extended buffer */
  4179. if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
  4180. from = pmbx;
  4181. ext = from + sizeof(MAILBOX_t);
  4182. pmboxq->context2 = ext;
  4183. pmboxq->in_ext_byte_len =
  4184. mbox_req->inExtWLen * sizeof(uint32_t);
  4185. pmboxq->out_ext_byte_len =
  4186. mbox_req->outExtWLen * sizeof(uint32_t);
  4187. pmboxq->mbox_offset_word = mbox_req->mbOffset;
  4188. }
  4189. /* biu diag will need a kernel buffer to transfer the data
  4190. * allocate our own buffer and setup the mailbox command to
  4191. * use ours
  4192. */
  4193. if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
  4194. transmit_length = pmb->un.varWords[1];
  4195. receive_length = pmb->un.varWords[4];
  4196. /* transmit length cannot be greater than receive length or
  4197. * mailbox extension size
  4198. */
  4199. if ((transmit_length > receive_length) ||
  4200. (transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
  4201. rc = -ERANGE;
  4202. goto job_done;
  4203. }
  4204. pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
  4205. putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
  4206. pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
  4207. putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
  4208. pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
  4209. putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
  4210. + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
  4211. pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
  4212. putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
  4213. + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
  4214. } else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
  4215. rdEventLog = &pmb->un.varRdEventLog;
  4216. receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
  4217. mode = bf_get(lpfc_event_log, rdEventLog);
  4218. /* receive length cannot be greater than mailbox
  4219. * extension size
  4220. */
  4221. if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
  4222. rc = -ERANGE;
  4223. goto job_done;
  4224. }
  4225. /* mode zero uses a bde like biu diags command */
  4226. if (mode == 0) {
  4227. pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
  4228. + sizeof(MAILBOX_t));
  4229. pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
  4230. + sizeof(MAILBOX_t));
  4231. }
  4232. } else if (phba->sli_rev == LPFC_SLI_REV4) {
  4233. /* Let type 4 (well known data) through because the data is
  4234. * returned in varwords[4-8]
  4235. * otherwise check the recieve length and fetch the buffer addr
  4236. */
  4237. if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
  4238. (pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
  4239. /* rebuild the command for sli4 using our own buffers
  4240. * like we do for biu diags
  4241. */
  4242. receive_length = pmb->un.varWords[2];
  4243. /* receive length cannot be greater than mailbox
  4244. * extension size
  4245. */
  4246. if (receive_length == 0) {
  4247. rc = -ERANGE;
  4248. goto job_done;
  4249. }
  4250. pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
  4251. + sizeof(MAILBOX_t));
  4252. pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
  4253. + sizeof(MAILBOX_t));
  4254. } else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
  4255. pmb->un.varUpdateCfg.co) {
  4256. bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
  4257. /* bde size cannot be greater than mailbox ext size */
  4258. if (bde->tus.f.bdeSize >
  4259. BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
  4260. rc = -ERANGE;
  4261. goto job_done;
  4262. }
  4263. bde->addrHigh = putPaddrHigh(dmabuf->phys
  4264. + sizeof(MAILBOX_t));
  4265. bde->addrLow = putPaddrLow(dmabuf->phys
  4266. + sizeof(MAILBOX_t));
  4267. } else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
  4268. /* Handling non-embedded SLI_CONFIG mailbox command */
  4269. sli4_config = &pmboxq->u.mqe.un.sli4_config;
  4270. if (!bf_get(lpfc_mbox_hdr_emb,
  4271. &sli4_config->header.cfg_mhdr)) {
  4272. /* rebuild the command for sli4 using our
  4273. * own buffers like we do for biu diags
  4274. */
  4275. header = (struct mbox_header *)
  4276. &pmb->un.varWords[0];
  4277. nembed_sge = (struct lpfc_mbx_nembed_cmd *)
  4278. &pmb->un.varWords[0];
  4279. receive_length = nembed_sge->sge[0].length;
  4280. /* receive length cannot be greater than
  4281. * mailbox extension size
  4282. */
  4283. if ((receive_length == 0) ||
  4284. (receive_length >
  4285. BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
  4286. rc = -ERANGE;
  4287. goto job_done;
  4288. }
  4289. nembed_sge->sge[0].pa_hi =
  4290. putPaddrHigh(dmabuf->phys
  4291. + sizeof(MAILBOX_t));
  4292. nembed_sge->sge[0].pa_lo =
  4293. putPaddrLow(dmabuf->phys
  4294. + sizeof(MAILBOX_t));
  4295. }
  4296. }
  4297. }
  4298. dd_data->context_un.mbox.dmabuffers = dmabuf;
  4299. /* setup wake call as IOCB callback */
  4300. pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
  4301. /* setup context field to pass wait_queue pointer to wake function */
  4302. pmboxq->context1 = dd_data;
  4303. dd_data->type = TYPE_MBOX;
  4304. dd_data->set_job = job;
  4305. dd_data->context_un.mbox.pmboxq = pmboxq;
  4306. dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
  4307. dd_data->context_un.mbox.ext = ext;
  4308. dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
  4309. dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
  4310. dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
  4311. job->dd_data = dd_data;
  4312. if ((vport->fc_flag & FC_OFFLINE_MODE) ||
  4313. (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
  4314. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
  4315. if (rc != MBX_SUCCESS) {
  4316. rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
  4317. goto job_done;
  4318. }
  4319. /* job finished, copy the data */
  4320. memcpy(pmbx, pmb, sizeof(*pmb));
  4321. job->reply->reply_payload_rcv_len =
  4322. sg_copy_from_buffer(job->reply_payload.sg_list,
  4323. job->reply_payload.sg_cnt,
  4324. pmbx, size);
  4325. /* not waiting mbox already done */
  4326. rc = 0;
  4327. goto job_done;
  4328. }
  4329. rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  4330. if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
  4331. return 1; /* job started */
  4332. job_done:
  4333. /* common exit for error or job completed inline */
  4334. if (pmboxq)
  4335. mempool_free(pmboxq, phba->mbox_mem_pool);
  4336. lpfc_bsg_dma_page_free(phba, dmabuf);
  4337. kfree(dd_data);
  4338. job_cont:
  4339. return rc;
  4340. }
  4341. /**
  4342. * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
  4343. * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
  4344. **/
  4345. static int
  4346. lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
  4347. {
  4348. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4349. struct lpfc_hba *phba = vport->phba;
  4350. struct dfc_mbox_req *mbox_req;
  4351. int rc = 0;
  4352. /* mix-and-match backward compatibility */
  4353. job->reply->reply_payload_rcv_len = 0;
  4354. if (job->request_len <
  4355. sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
  4356. lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
  4357. "2737 Mix-and-match backward compatibility "
  4358. "between MBOX_REQ old size:%d and "
  4359. "new request size:%d\n",
  4360. (int)(job->request_len -
  4361. sizeof(struct fc_bsg_request)),
  4362. (int)sizeof(struct dfc_mbox_req));
  4363. mbox_req = (struct dfc_mbox_req *)
  4364. job->request->rqst_data.h_vendor.vendor_cmd;
  4365. mbox_req->extMboxTag = 0;
  4366. mbox_req->extSeqNum = 0;
  4367. }
  4368. rc = lpfc_bsg_issue_mbox(phba, job, vport);
  4369. if (rc == 0) {
  4370. /* job done */
  4371. job->reply->result = 0;
  4372. job->dd_data = NULL;
  4373. job->job_done(job);
  4374. } else if (rc == 1)
  4375. /* job submitted, will complete later*/
  4376. rc = 0; /* return zero, no error */
  4377. else {
  4378. /* some error occurred */
  4379. job->reply->result = rc;
  4380. job->dd_data = NULL;
  4381. }
  4382. return rc;
  4383. }
  4384. /**
  4385. * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
  4386. * @phba: Pointer to HBA context object.
  4387. * @cmdiocbq: Pointer to command iocb.
  4388. * @rspiocbq: Pointer to response iocb.
  4389. *
  4390. * This function is the completion handler for iocbs issued using
  4391. * lpfc_menlo_cmd function. This function is called by the
  4392. * ring event handler function without any lock held. This function
  4393. * can be called from both worker thread context and interrupt
  4394. * context. This function also can be called from another thread which
  4395. * cleans up the SLI layer objects.
  4396. * This function copies the contents of the response iocb to the
  4397. * response iocb memory object provided by the caller of
  4398. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  4399. * sleeps for the iocb completion.
  4400. **/
  4401. static void
  4402. lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
  4403. struct lpfc_iocbq *cmdiocbq,
  4404. struct lpfc_iocbq *rspiocbq)
  4405. {
  4406. struct bsg_job_data *dd_data;
  4407. struct fc_bsg_job *job;
  4408. IOCB_t *rsp;
  4409. struct lpfc_dmabuf *bmp, *cmp, *rmp;
  4410. struct lpfc_bsg_menlo *menlo;
  4411. unsigned long flags;
  4412. struct menlo_response *menlo_resp;
  4413. unsigned int rsp_size;
  4414. int rc = 0;
  4415. dd_data = cmdiocbq->context1;
  4416. cmp = cmdiocbq->context2;
  4417. bmp = cmdiocbq->context3;
  4418. menlo = &dd_data->context_un.menlo;
  4419. rmp = menlo->rmp;
  4420. rsp = &rspiocbq->iocb;
  4421. /* Determine if job has been aborted */
  4422. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  4423. job = dd_data->set_job;
  4424. if (job) {
  4425. /* Prevent timeout handling from trying to abort job */
  4426. job->dd_data = NULL;
  4427. }
  4428. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4429. /* Copy the job data or set the failing status for the job */
  4430. if (job) {
  4431. /* always return the xri, this would be used in the case
  4432. * of a menlo download to allow the data to be sent as a
  4433. * continuation of the exchange.
  4434. */
  4435. menlo_resp = (struct menlo_response *)
  4436. job->reply->reply_data.vendor_reply.vendor_rsp;
  4437. menlo_resp->xri = rsp->ulpContext;
  4438. if (rsp->ulpStatus) {
  4439. if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  4440. switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
  4441. case IOERR_SEQUENCE_TIMEOUT:
  4442. rc = -ETIMEDOUT;
  4443. break;
  4444. case IOERR_INVALID_RPI:
  4445. rc = -EFAULT;
  4446. break;
  4447. default:
  4448. rc = -EACCES;
  4449. break;
  4450. }
  4451. } else {
  4452. rc = -EACCES;
  4453. }
  4454. } else {
  4455. rsp_size = rsp->un.genreq64.bdl.bdeSize;
  4456. job->reply->reply_payload_rcv_len =
  4457. lpfc_bsg_copy_data(rmp, &job->reply_payload,
  4458. rsp_size, 0);
  4459. }
  4460. }
  4461. lpfc_sli_release_iocbq(phba, cmdiocbq);
  4462. lpfc_free_bsg_buffers(phba, cmp);
  4463. lpfc_free_bsg_buffers(phba, rmp);
  4464. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  4465. kfree(bmp);
  4466. kfree(dd_data);
  4467. /* Complete the job if active */
  4468. if (job) {
  4469. job->reply->result = rc;
  4470. job->job_done(job);
  4471. }
  4472. return;
  4473. }
  4474. /**
  4475. * lpfc_menlo_cmd - send an ioctl for menlo hardware
  4476. * @job: fc_bsg_job to handle
  4477. *
  4478. * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
  4479. * all the command completions will return the xri for the command.
  4480. * For menlo data requests a gen request 64 CX is used to continue the exchange
  4481. * supplied in the menlo request header xri field.
  4482. **/
  4483. static int
  4484. lpfc_menlo_cmd(struct fc_bsg_job *job)
  4485. {
  4486. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4487. struct lpfc_hba *phba = vport->phba;
  4488. struct lpfc_iocbq *cmdiocbq;
  4489. IOCB_t *cmd;
  4490. int rc = 0;
  4491. struct menlo_command *menlo_cmd;
  4492. struct menlo_response *menlo_resp;
  4493. struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
  4494. int request_nseg;
  4495. int reply_nseg;
  4496. struct bsg_job_data *dd_data;
  4497. struct ulp_bde64 *bpl = NULL;
  4498. /* in case no data is returned return just the return code */
  4499. job->reply->reply_payload_rcv_len = 0;
  4500. if (job->request_len <
  4501. sizeof(struct fc_bsg_request) +
  4502. sizeof(struct menlo_command)) {
  4503. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4504. "2784 Received MENLO_CMD request below "
  4505. "minimum size\n");
  4506. rc = -ERANGE;
  4507. goto no_dd_data;
  4508. }
  4509. if (job->reply_len <
  4510. sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
  4511. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4512. "2785 Received MENLO_CMD reply below "
  4513. "minimum size\n");
  4514. rc = -ERANGE;
  4515. goto no_dd_data;
  4516. }
  4517. if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
  4518. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4519. "2786 Adapter does not support menlo "
  4520. "commands\n");
  4521. rc = -EPERM;
  4522. goto no_dd_data;
  4523. }
  4524. menlo_cmd = (struct menlo_command *)
  4525. job->request->rqst_data.h_vendor.vendor_cmd;
  4526. menlo_resp = (struct menlo_response *)
  4527. job->reply->reply_data.vendor_reply.vendor_rsp;
  4528. /* allocate our bsg tracking structure */
  4529. dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
  4530. if (!dd_data) {
  4531. lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
  4532. "2787 Failed allocation of dd_data\n");
  4533. rc = -ENOMEM;
  4534. goto no_dd_data;
  4535. }
  4536. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4537. if (!bmp) {
  4538. rc = -ENOMEM;
  4539. goto free_dd;
  4540. }
  4541. bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
  4542. if (!bmp->virt) {
  4543. rc = -ENOMEM;
  4544. goto free_bmp;
  4545. }
  4546. INIT_LIST_HEAD(&bmp->list);
  4547. bpl = (struct ulp_bde64 *)bmp->virt;
  4548. request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
  4549. cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
  4550. 1, bpl, &request_nseg);
  4551. if (!cmp) {
  4552. rc = -ENOMEM;
  4553. goto free_bmp;
  4554. }
  4555. lpfc_bsg_copy_data(cmp, &job->request_payload,
  4556. job->request_payload.payload_len, 1);
  4557. bpl += request_nseg;
  4558. reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
  4559. rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
  4560. bpl, &reply_nseg);
  4561. if (!rmp) {
  4562. rc = -ENOMEM;
  4563. goto free_cmp;
  4564. }
  4565. cmdiocbq = lpfc_sli_get_iocbq(phba);
  4566. if (!cmdiocbq) {
  4567. rc = -ENOMEM;
  4568. goto free_rmp;
  4569. }
  4570. cmd = &cmdiocbq->iocb;
  4571. cmd->un.genreq64.bdl.ulpIoTag32 = 0;
  4572. cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
  4573. cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
  4574. cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  4575. cmd->un.genreq64.bdl.bdeSize =
  4576. (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
  4577. cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
  4578. cmd->un.genreq64.w5.hcsw.Dfctl = 0;
  4579. cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
  4580. cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
  4581. cmd->ulpBdeCount = 1;
  4582. cmd->ulpClass = CLASS3;
  4583. cmd->ulpOwner = OWN_CHIP;
  4584. cmd->ulpLe = 1; /* Limited Edition */
  4585. cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
  4586. cmdiocbq->vport = phba->pport;
  4587. /* We want the firmware to timeout before we do */
  4588. cmd->ulpTimeout = MENLO_TIMEOUT - 5;
  4589. cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
  4590. cmdiocbq->context1 = dd_data;
  4591. cmdiocbq->context2 = cmp;
  4592. cmdiocbq->context3 = bmp;
  4593. if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
  4594. cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
  4595. cmd->ulpPU = MENLO_PU; /* 3 */
  4596. cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
  4597. cmd->ulpContext = MENLO_CONTEXT; /* 0 */
  4598. } else {
  4599. cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
  4600. cmd->ulpPU = 1;
  4601. cmd->un.ulpWord[4] = 0;
  4602. cmd->ulpContext = menlo_cmd->xri;
  4603. }
  4604. dd_data->type = TYPE_MENLO;
  4605. dd_data->set_job = job;
  4606. dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
  4607. dd_data->context_un.menlo.rmp = rmp;
  4608. job->dd_data = dd_data;
  4609. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
  4610. MENLO_TIMEOUT - 5);
  4611. if (rc == IOCB_SUCCESS)
  4612. return 0; /* done for now */
  4613. lpfc_sli_release_iocbq(phba, cmdiocbq);
  4614. free_rmp:
  4615. lpfc_free_bsg_buffers(phba, rmp);
  4616. free_cmp:
  4617. lpfc_free_bsg_buffers(phba, cmp);
  4618. free_bmp:
  4619. if (bmp->virt)
  4620. lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
  4621. kfree(bmp);
  4622. free_dd:
  4623. kfree(dd_data);
  4624. no_dd_data:
  4625. /* make error code available to userspace */
  4626. job->reply->result = rc;
  4627. job->dd_data = NULL;
  4628. return rc;
  4629. }
  4630. /**
  4631. * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
  4632. * @job: fc_bsg_job to handle
  4633. **/
  4634. static int
  4635. lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
  4636. {
  4637. int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
  4638. int rc;
  4639. switch (command) {
  4640. case LPFC_BSG_VENDOR_SET_CT_EVENT:
  4641. rc = lpfc_bsg_hba_set_event(job);
  4642. break;
  4643. case LPFC_BSG_VENDOR_GET_CT_EVENT:
  4644. rc = lpfc_bsg_hba_get_event(job);
  4645. break;
  4646. case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
  4647. rc = lpfc_bsg_send_mgmt_rsp(job);
  4648. break;
  4649. case LPFC_BSG_VENDOR_DIAG_MODE:
  4650. rc = lpfc_bsg_diag_loopback_mode(job);
  4651. break;
  4652. case LPFC_BSG_VENDOR_DIAG_MODE_END:
  4653. rc = lpfc_sli4_bsg_diag_mode_end(job);
  4654. break;
  4655. case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
  4656. rc = lpfc_bsg_diag_loopback_run(job);
  4657. break;
  4658. case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
  4659. rc = lpfc_sli4_bsg_link_diag_test(job);
  4660. break;
  4661. case LPFC_BSG_VENDOR_GET_MGMT_REV:
  4662. rc = lpfc_bsg_get_dfc_rev(job);
  4663. break;
  4664. case LPFC_BSG_VENDOR_MBOX:
  4665. rc = lpfc_bsg_mbox_cmd(job);
  4666. break;
  4667. case LPFC_BSG_VENDOR_MENLO_CMD:
  4668. case LPFC_BSG_VENDOR_MENLO_DATA:
  4669. rc = lpfc_menlo_cmd(job);
  4670. break;
  4671. default:
  4672. rc = -EINVAL;
  4673. job->reply->reply_payload_rcv_len = 0;
  4674. /* make error code available to userspace */
  4675. job->reply->result = rc;
  4676. break;
  4677. }
  4678. return rc;
  4679. }
  4680. /**
  4681. * lpfc_bsg_request - handle a bsg request from the FC transport
  4682. * @job: fc_bsg_job to handle
  4683. **/
  4684. int
  4685. lpfc_bsg_request(struct fc_bsg_job *job)
  4686. {
  4687. uint32_t msgcode;
  4688. int rc;
  4689. msgcode = job->request->msgcode;
  4690. switch (msgcode) {
  4691. case FC_BSG_HST_VENDOR:
  4692. rc = lpfc_bsg_hst_vendor(job);
  4693. break;
  4694. case FC_BSG_RPT_ELS:
  4695. rc = lpfc_bsg_rport_els(job);
  4696. break;
  4697. case FC_BSG_RPT_CT:
  4698. rc = lpfc_bsg_send_mgmt_cmd(job);
  4699. break;
  4700. default:
  4701. rc = -EINVAL;
  4702. job->reply->reply_payload_rcv_len = 0;
  4703. /* make error code available to userspace */
  4704. job->reply->result = rc;
  4705. break;
  4706. }
  4707. return rc;
  4708. }
  4709. /**
  4710. * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
  4711. * @job: fc_bsg_job that has timed out
  4712. *
  4713. * This function just aborts the job's IOCB. The aborted IOCB will return to
  4714. * the waiting function which will handle passing the error back to userspace
  4715. **/
  4716. int
  4717. lpfc_bsg_timeout(struct fc_bsg_job *job)
  4718. {
  4719. struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
  4720. struct lpfc_hba *phba = vport->phba;
  4721. struct lpfc_iocbq *cmdiocb;
  4722. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  4723. struct bsg_job_data *dd_data;
  4724. unsigned long flags;
  4725. int rc = 0;
  4726. LIST_HEAD(completions);
  4727. struct lpfc_iocbq *check_iocb, *next_iocb;
  4728. /* if job's driver data is NULL, the command completed or is in the
  4729. * the process of completing. In this case, return status to request
  4730. * so the timeout is retried. This avoids double completion issues
  4731. * and the request will be pulled off the timer queue when the
  4732. * command's completion handler executes. Otherwise, prevent the
  4733. * command's completion handler from executing the job done callback
  4734. * and continue processing to abort the outstanding the command.
  4735. */
  4736. spin_lock_irqsave(&phba->ct_ev_lock, flags);
  4737. dd_data = (struct bsg_job_data *)job->dd_data;
  4738. if (dd_data) {
  4739. dd_data->set_job = NULL;
  4740. job->dd_data = NULL;
  4741. } else {
  4742. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4743. return -EAGAIN;
  4744. }
  4745. switch (dd_data->type) {
  4746. case TYPE_IOCB:
  4747. /* Check to see if IOCB was issued to the port or not. If not,
  4748. * remove it from the txq queue and call cancel iocbs.
  4749. * Otherwise, call abort iotag
  4750. */
  4751. cmdiocb = dd_data->context_un.iocb.cmdiocbq;
  4752. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4753. spin_lock_irqsave(&phba->hbalock, flags);
  4754. /* make sure the I/O abort window is still open */
  4755. if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
  4756. spin_unlock_irqrestore(&phba->hbalock, flags);
  4757. return -EAGAIN;
  4758. }
  4759. list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
  4760. list) {
  4761. if (check_iocb == cmdiocb) {
  4762. list_move_tail(&check_iocb->list, &completions);
  4763. break;
  4764. }
  4765. }
  4766. if (list_empty(&completions))
  4767. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  4768. spin_unlock_irqrestore(&phba->hbalock, flags);
  4769. if (!list_empty(&completions)) {
  4770. lpfc_sli_cancel_iocbs(phba, &completions,
  4771. IOSTAT_LOCAL_REJECT,
  4772. IOERR_SLI_ABORTED);
  4773. }
  4774. break;
  4775. case TYPE_EVT:
  4776. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4777. break;
  4778. case TYPE_MBOX:
  4779. /* Update the ext buf ctx state if needed */
  4780. if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
  4781. phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
  4782. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4783. break;
  4784. case TYPE_MENLO:
  4785. /* Check to see if IOCB was issued to the port or not. If not,
  4786. * remove it from the txq queue and call cancel iocbs.
  4787. * Otherwise, call abort iotag.
  4788. */
  4789. cmdiocb = dd_data->context_un.menlo.cmdiocbq;
  4790. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4791. spin_lock_irqsave(&phba->hbalock, flags);
  4792. list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
  4793. list) {
  4794. if (check_iocb == cmdiocb) {
  4795. list_move_tail(&check_iocb->list, &completions);
  4796. break;
  4797. }
  4798. }
  4799. if (list_empty(&completions))
  4800. lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
  4801. spin_unlock_irqrestore(&phba->hbalock, flags);
  4802. if (!list_empty(&completions)) {
  4803. lpfc_sli_cancel_iocbs(phba, &completions,
  4804. IOSTAT_LOCAL_REJECT,
  4805. IOERR_SLI_ABORTED);
  4806. }
  4807. break;
  4808. default:
  4809. spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
  4810. break;
  4811. }
  4812. /* scsi transport fc fc_bsg_job_timeout expects a zero return code,
  4813. * otherwise an error message will be displayed on the console
  4814. * so always return success (zero)
  4815. */
  4816. return rc;
  4817. }