lpfc_bsg.c 137 KB

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