lpfc_bsg.c 154 KB

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