lpfc_bsg.c 146 KB

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