lpfc_scsi.c 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/export.h>
  25. #include <linux/delay.h>
  26. #include <asm/unaligned.h>
  27. #include <linux/crc-t10dif.h>
  28. #include <net/checksum.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_transport_fc.h>
  35. #include "lpfc_version.h"
  36. #include "lpfc_hw4.h"
  37. #include "lpfc_hw.h"
  38. #include "lpfc_sli.h"
  39. #include "lpfc_sli4.h"
  40. #include "lpfc_nl.h"
  41. #include "lpfc_disc.h"
  42. #include "lpfc.h"
  43. #include "lpfc_scsi.h"
  44. #include "lpfc_logmsg.h"
  45. #include "lpfc_crtn.h"
  46. #include "lpfc_vport.h"
  47. #define LPFC_RESET_WAIT 2
  48. #define LPFC_ABORT_WAIT 2
  49. int _dump_buf_done = 1;
  50. static char *dif_op_str[] = {
  51. "PROT_NORMAL",
  52. "PROT_READ_INSERT",
  53. "PROT_WRITE_STRIP",
  54. "PROT_READ_STRIP",
  55. "PROT_WRITE_INSERT",
  56. "PROT_READ_PASS",
  57. "PROT_WRITE_PASS",
  58. };
  59. struct scsi_dif_tuple {
  60. __be16 guard_tag; /* Checksum */
  61. __be16 app_tag; /* Opaque storage */
  62. __be32 ref_tag; /* Target LBA or indirect LBA */
  63. };
  64. static struct lpfc_rport_data *
  65. lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
  66. {
  67. struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
  68. if (vport->phba->cfg_fof)
  69. return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
  70. else
  71. return (struct lpfc_rport_data *)sdev->hostdata;
  72. }
  73. static void
  74. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  75. static void
  76. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  77. static int
  78. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
  79. static void
  80. lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  81. {
  82. void *src, *dst;
  83. struct scatterlist *sgde = scsi_sglist(cmnd);
  84. if (!_dump_buf_data) {
  85. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  86. "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  87. __func__);
  88. return;
  89. }
  90. if (!sgde) {
  91. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  92. "9051 BLKGRD: ERROR: data scatterlist is null\n");
  93. return;
  94. }
  95. dst = (void *) _dump_buf_data;
  96. while (sgde) {
  97. src = sg_virt(sgde);
  98. memcpy(dst, src, sgde->length);
  99. dst += sgde->length;
  100. sgde = sg_next(sgde);
  101. }
  102. }
  103. static void
  104. lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  105. {
  106. void *src, *dst;
  107. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  108. if (!_dump_buf_dif) {
  109. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  110. "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  111. __func__);
  112. return;
  113. }
  114. if (!sgde) {
  115. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  116. "9053 BLKGRD: ERROR: prot scatterlist is null\n");
  117. return;
  118. }
  119. dst = _dump_buf_dif;
  120. while (sgde) {
  121. src = sg_virt(sgde);
  122. memcpy(dst, src, sgde->length);
  123. dst += sgde->length;
  124. sgde = sg_next(sgde);
  125. }
  126. }
  127. static inline unsigned
  128. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  129. {
  130. return sc->device->sector_size;
  131. }
  132. #define LPFC_CHECK_PROTECT_GUARD 1
  133. #define LPFC_CHECK_PROTECT_REF 2
  134. static inline unsigned
  135. lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
  136. {
  137. return 1;
  138. }
  139. static inline unsigned
  140. lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
  141. {
  142. if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
  143. return 0;
  144. if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
  145. return 1;
  146. return 0;
  147. }
  148. /**
  149. * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
  150. * @phba: Pointer to HBA object.
  151. * @lpfc_cmd: lpfc scsi command object pointer.
  152. *
  153. * This function is called from the lpfc_prep_task_mgmt_cmd function to
  154. * set the last bit in the response sge entry.
  155. **/
  156. static void
  157. lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
  158. struct lpfc_scsi_buf *lpfc_cmd)
  159. {
  160. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  161. if (sgl) {
  162. sgl += 1;
  163. sgl->word2 = le32_to_cpu(sgl->word2);
  164. bf_set(lpfc_sli4_sge_last, sgl, 1);
  165. sgl->word2 = cpu_to_le32(sgl->word2);
  166. }
  167. }
  168. /**
  169. * lpfc_update_stats - Update statistical data for the command completion
  170. * @phba: Pointer to HBA object.
  171. * @lpfc_cmd: lpfc scsi command object pointer.
  172. *
  173. * This function is called when there is a command completion and this
  174. * function updates the statistical data for the command completion.
  175. **/
  176. static void
  177. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  178. {
  179. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  180. struct lpfc_nodelist *pnode = rdata->pnode;
  181. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  182. unsigned long flags;
  183. struct Scsi_Host *shost = cmd->device->host;
  184. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  185. unsigned long latency;
  186. int i;
  187. if (cmd->result)
  188. return;
  189. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  190. spin_lock_irqsave(shost->host_lock, flags);
  191. if (!vport->stat_data_enabled ||
  192. vport->stat_data_blocked ||
  193. !pnode ||
  194. !pnode->lat_data ||
  195. (phba->bucket_type == LPFC_NO_BUCKET)) {
  196. spin_unlock_irqrestore(shost->host_lock, flags);
  197. return;
  198. }
  199. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  200. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  201. phba->bucket_step;
  202. /* check array subscript bounds */
  203. if (i < 0)
  204. i = 0;
  205. else if (i >= LPFC_MAX_BUCKET_COUNT)
  206. i = LPFC_MAX_BUCKET_COUNT - 1;
  207. } else {
  208. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  209. if (latency <= (phba->bucket_base +
  210. ((1<<i)*phba->bucket_step)))
  211. break;
  212. }
  213. pnode->lat_data[i].cmd_count++;
  214. spin_unlock_irqrestore(shost->host_lock, flags);
  215. }
  216. /**
  217. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  218. * @phba: The Hba for which this call is being executed.
  219. *
  220. * This routine is called when there is resource error in driver or firmware.
  221. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  222. * posts at most 1 event each second. This routine wakes up worker thread of
  223. * @phba to process WORKER_RAM_DOWN_EVENT event.
  224. *
  225. * This routine should be called with no lock held.
  226. **/
  227. void
  228. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  229. {
  230. unsigned long flags;
  231. uint32_t evt_posted;
  232. unsigned long expires;
  233. spin_lock_irqsave(&phba->hbalock, flags);
  234. atomic_inc(&phba->num_rsrc_err);
  235. phba->last_rsrc_error_time = jiffies;
  236. expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
  237. if (time_after(expires, jiffies)) {
  238. spin_unlock_irqrestore(&phba->hbalock, flags);
  239. return;
  240. }
  241. phba->last_ramp_down_time = jiffies;
  242. spin_unlock_irqrestore(&phba->hbalock, flags);
  243. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  244. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  245. if (!evt_posted)
  246. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  247. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  248. if (!evt_posted)
  249. lpfc_worker_wake_up(phba);
  250. return;
  251. }
  252. /**
  253. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  254. * @phba: The Hba for which this call is being executed.
  255. *
  256. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  257. * thread.This routine reduces queue depth for all scsi device on each vport
  258. * associated with @phba.
  259. **/
  260. void
  261. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  262. {
  263. struct lpfc_vport **vports;
  264. struct Scsi_Host *shost;
  265. struct scsi_device *sdev;
  266. unsigned long new_queue_depth;
  267. unsigned long num_rsrc_err, num_cmd_success;
  268. int i;
  269. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  270. num_cmd_success = atomic_read(&phba->num_cmd_success);
  271. /*
  272. * The error and success command counters are global per
  273. * driver instance. If another handler has already
  274. * operated on this error event, just exit.
  275. */
  276. if (num_rsrc_err == 0)
  277. return;
  278. vports = lpfc_create_vport_work_array(phba);
  279. if (vports != NULL)
  280. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  281. shost = lpfc_shost_from_vport(vports[i]);
  282. shost_for_each_device(sdev, shost) {
  283. new_queue_depth =
  284. sdev->queue_depth * num_rsrc_err /
  285. (num_rsrc_err + num_cmd_success);
  286. if (!new_queue_depth)
  287. new_queue_depth = sdev->queue_depth - 1;
  288. else
  289. new_queue_depth = sdev->queue_depth -
  290. new_queue_depth;
  291. scsi_change_queue_depth(sdev, new_queue_depth);
  292. }
  293. }
  294. lpfc_destroy_vport_work_array(phba, vports);
  295. atomic_set(&phba->num_rsrc_err, 0);
  296. atomic_set(&phba->num_cmd_success, 0);
  297. }
  298. /**
  299. * lpfc_scsi_dev_block - set all scsi hosts to block state
  300. * @phba: Pointer to HBA context object.
  301. *
  302. * This function walks vport list and set each SCSI host to block state
  303. * by invoking fc_remote_port_delete() routine. This function is invoked
  304. * with EEH when device's PCI slot has been permanently disabled.
  305. **/
  306. void
  307. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  308. {
  309. struct lpfc_vport **vports;
  310. struct Scsi_Host *shost;
  311. struct scsi_device *sdev;
  312. struct fc_rport *rport;
  313. int i;
  314. vports = lpfc_create_vport_work_array(phba);
  315. if (vports != NULL)
  316. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  317. shost = lpfc_shost_from_vport(vports[i]);
  318. shost_for_each_device(sdev, shost) {
  319. rport = starget_to_rport(scsi_target(sdev));
  320. fc_remote_port_delete(rport);
  321. }
  322. }
  323. lpfc_destroy_vport_work_array(phba, vports);
  324. }
  325. /**
  326. * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
  327. * @vport: The virtual port for which this call being executed.
  328. * @num_to_allocate: The requested number of buffers to allocate.
  329. *
  330. * This routine allocates a scsi buffer for device with SLI-3 interface spec,
  331. * the scsi buffer contains all the necessary information needed to initiate
  332. * a SCSI I/O. The non-DMAable buffer region contains information to build
  333. * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
  334. * and the initial BPL. In addition to allocating memory, the FCP CMND and
  335. * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
  336. *
  337. * Return codes:
  338. * int - number of scsi buffers that were allocated.
  339. * 0 = failure, less than num_to_alloc is a partial failure.
  340. **/
  341. static int
  342. lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
  343. {
  344. struct lpfc_hba *phba = vport->phba;
  345. struct lpfc_scsi_buf *psb;
  346. struct ulp_bde64 *bpl;
  347. IOCB_t *iocb;
  348. dma_addr_t pdma_phys_fcp_cmd;
  349. dma_addr_t pdma_phys_fcp_rsp;
  350. dma_addr_t pdma_phys_bpl;
  351. uint16_t iotag;
  352. int bcnt, bpl_size;
  353. bpl_size = phba->cfg_sg_dma_buf_size -
  354. (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  355. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  356. "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
  357. num_to_alloc, phba->cfg_sg_dma_buf_size,
  358. (int)sizeof(struct fcp_cmnd),
  359. (int)sizeof(struct fcp_rsp), bpl_size);
  360. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  361. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  362. if (!psb)
  363. break;
  364. /*
  365. * Get memory from the pci pool to map the virt space to pci
  366. * bus space for an I/O. The DMA buffer includes space for the
  367. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  368. * necessary to support the sg_tablesize.
  369. */
  370. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  371. GFP_KERNEL, &psb->dma_handle);
  372. if (!psb->data) {
  373. kfree(psb);
  374. break;
  375. }
  376. /* Initialize virtual ptrs to dma_buf region. */
  377. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  378. /* Allocate iotag for psb->cur_iocbq. */
  379. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  380. if (iotag == 0) {
  381. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  382. psb->data, psb->dma_handle);
  383. kfree(psb);
  384. break;
  385. }
  386. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  387. psb->fcp_cmnd = psb->data;
  388. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  389. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  390. sizeof(struct fcp_rsp);
  391. /* Initialize local short-hand pointers. */
  392. bpl = psb->fcp_bpl;
  393. pdma_phys_fcp_cmd = psb->dma_handle;
  394. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  395. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  396. sizeof(struct fcp_rsp);
  397. /*
  398. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  399. * are sg list bdes. Initialize the first two and leave the
  400. * rest for queuecommand.
  401. */
  402. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  403. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  404. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  405. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  406. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  407. /* Setup the physical region for the FCP RSP */
  408. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  409. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  410. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  411. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  412. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  413. /*
  414. * Since the IOCB for the FCP I/O is built into this
  415. * lpfc_scsi_buf, initialize it with all known data now.
  416. */
  417. iocb = &psb->cur_iocbq.iocb;
  418. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  419. if ((phba->sli_rev == 3) &&
  420. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  421. /* fill in immediate fcp command BDE */
  422. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  423. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  424. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  425. unsli3.fcp_ext.icd);
  426. iocb->un.fcpi64.bdl.addrHigh = 0;
  427. iocb->ulpBdeCount = 0;
  428. iocb->ulpLe = 0;
  429. /* fill in response BDE */
  430. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
  431. BUFF_TYPE_BDE_64;
  432. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  433. sizeof(struct fcp_rsp);
  434. iocb->unsli3.fcp_ext.rbde.addrLow =
  435. putPaddrLow(pdma_phys_fcp_rsp);
  436. iocb->unsli3.fcp_ext.rbde.addrHigh =
  437. putPaddrHigh(pdma_phys_fcp_rsp);
  438. } else {
  439. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  440. iocb->un.fcpi64.bdl.bdeSize =
  441. (2 * sizeof(struct ulp_bde64));
  442. iocb->un.fcpi64.bdl.addrLow =
  443. putPaddrLow(pdma_phys_bpl);
  444. iocb->un.fcpi64.bdl.addrHigh =
  445. putPaddrHigh(pdma_phys_bpl);
  446. iocb->ulpBdeCount = 1;
  447. iocb->ulpLe = 1;
  448. }
  449. iocb->ulpClass = CLASS3;
  450. psb->status = IOSTAT_SUCCESS;
  451. /* Put it back into the SCSI buffer list */
  452. psb->cur_iocbq.context1 = psb;
  453. lpfc_release_scsi_buf_s3(phba, psb);
  454. }
  455. return bcnt;
  456. }
  457. /**
  458. * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
  459. * @vport: pointer to lpfc vport data structure.
  460. *
  461. * This routine is invoked by the vport cleanup for deletions and the cleanup
  462. * for an ndlp on removal.
  463. **/
  464. void
  465. lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
  466. {
  467. struct lpfc_hba *phba = vport->phba;
  468. struct lpfc_scsi_buf *psb, *next_psb;
  469. unsigned long iflag = 0;
  470. spin_lock_irqsave(&phba->hbalock, iflag);
  471. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  472. list_for_each_entry_safe(psb, next_psb,
  473. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  474. if (psb->rdata && psb->rdata->pnode
  475. && psb->rdata->pnode->vport == vport)
  476. psb->rdata = NULL;
  477. }
  478. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  479. spin_unlock_irqrestore(&phba->hbalock, iflag);
  480. }
  481. /**
  482. * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
  483. * @phba: pointer to lpfc hba data structure.
  484. * @axri: pointer to the fcp xri abort wcqe structure.
  485. *
  486. * This routine is invoked by the worker thread to process a SLI4 fast-path
  487. * FCP aborted xri.
  488. **/
  489. void
  490. lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
  491. struct sli4_wcqe_xri_aborted *axri)
  492. {
  493. uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
  494. uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
  495. struct lpfc_scsi_buf *psb, *next_psb;
  496. unsigned long iflag = 0;
  497. struct lpfc_iocbq *iocbq;
  498. int i;
  499. struct lpfc_nodelist *ndlp;
  500. int rrq_empty = 0;
  501. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  502. spin_lock_irqsave(&phba->hbalock, iflag);
  503. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  504. list_for_each_entry_safe(psb, next_psb,
  505. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  506. if (psb->cur_iocbq.sli4_xritag == xri) {
  507. list_del(&psb->list);
  508. psb->exch_busy = 0;
  509. psb->status = IOSTAT_SUCCESS;
  510. spin_unlock(
  511. &phba->sli4_hba.abts_scsi_buf_list_lock);
  512. if (psb->rdata && psb->rdata->pnode)
  513. ndlp = psb->rdata->pnode;
  514. else
  515. ndlp = NULL;
  516. rrq_empty = list_empty(&phba->active_rrq_list);
  517. spin_unlock_irqrestore(&phba->hbalock, iflag);
  518. if (ndlp) {
  519. lpfc_set_rrq_active(phba, ndlp,
  520. psb->cur_iocbq.sli4_lxritag, rxid, 1);
  521. lpfc_sli4_abts_err_handler(phba, ndlp, axri);
  522. }
  523. lpfc_release_scsi_buf_s4(phba, psb);
  524. if (rrq_empty)
  525. lpfc_worker_wake_up(phba);
  526. return;
  527. }
  528. }
  529. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  530. for (i = 1; i <= phba->sli.last_iotag; i++) {
  531. iocbq = phba->sli.iocbq_lookup[i];
  532. if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
  533. (iocbq->iocb_flag & LPFC_IO_LIBDFC))
  534. continue;
  535. if (iocbq->sli4_xritag != xri)
  536. continue;
  537. psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  538. psb->exch_busy = 0;
  539. spin_unlock_irqrestore(&phba->hbalock, iflag);
  540. if (!list_empty(&pring->txq))
  541. lpfc_worker_wake_up(phba);
  542. return;
  543. }
  544. spin_unlock_irqrestore(&phba->hbalock, iflag);
  545. }
  546. /**
  547. * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
  548. * @phba: pointer to lpfc hba data structure.
  549. * @post_sblist: pointer to the scsi buffer list.
  550. *
  551. * This routine walks a list of scsi buffers that was passed in. It attempts
  552. * to construct blocks of scsi buffer sgls which contains contiguous xris and
  553. * uses the non-embedded SGL block post mailbox commands to post to the port.
  554. * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
  555. * embedded SGL post mailbox command for posting. The @post_sblist passed in
  556. * must be local list, thus no lock is needed when manipulate the list.
  557. *
  558. * Returns: 0 = failure, non-zero number of successfully posted buffers.
  559. **/
  560. static int
  561. lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
  562. struct list_head *post_sblist, int sb_count)
  563. {
  564. struct lpfc_scsi_buf *psb, *psb_next;
  565. int status, sgl_size;
  566. int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
  567. dma_addr_t pdma_phys_bpl1;
  568. int last_xritag = NO_XRI;
  569. LIST_HEAD(prep_sblist);
  570. LIST_HEAD(blck_sblist);
  571. LIST_HEAD(scsi_sblist);
  572. /* sanity check */
  573. if (sb_count <= 0)
  574. return -EINVAL;
  575. sgl_size = phba->cfg_sg_dma_buf_size -
  576. (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  577. list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
  578. list_del_init(&psb->list);
  579. block_cnt++;
  580. if ((last_xritag != NO_XRI) &&
  581. (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
  582. /* a hole in xri block, form a sgl posting block */
  583. list_splice_init(&prep_sblist, &blck_sblist);
  584. post_cnt = block_cnt - 1;
  585. /* prepare list for next posting block */
  586. list_add_tail(&psb->list, &prep_sblist);
  587. block_cnt = 1;
  588. } else {
  589. /* prepare list for next posting block */
  590. list_add_tail(&psb->list, &prep_sblist);
  591. /* enough sgls for non-embed sgl mbox command */
  592. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  593. list_splice_init(&prep_sblist, &blck_sblist);
  594. post_cnt = block_cnt;
  595. block_cnt = 0;
  596. }
  597. }
  598. num_posting++;
  599. last_xritag = psb->cur_iocbq.sli4_xritag;
  600. /* end of repost sgl list condition for SCSI buffers */
  601. if (num_posting == sb_count) {
  602. if (post_cnt == 0) {
  603. /* last sgl posting block */
  604. list_splice_init(&prep_sblist, &blck_sblist);
  605. post_cnt = block_cnt;
  606. } else if (block_cnt == 1) {
  607. /* last single sgl with non-contiguous xri */
  608. if (sgl_size > SGL_PAGE_SIZE)
  609. pdma_phys_bpl1 = psb->dma_phys_bpl +
  610. SGL_PAGE_SIZE;
  611. else
  612. pdma_phys_bpl1 = 0;
  613. status = lpfc_sli4_post_sgl(phba,
  614. psb->dma_phys_bpl,
  615. pdma_phys_bpl1,
  616. psb->cur_iocbq.sli4_xritag);
  617. if (status) {
  618. /* failure, put on abort scsi list */
  619. psb->exch_busy = 1;
  620. } else {
  621. /* success, put on SCSI buffer list */
  622. psb->exch_busy = 0;
  623. psb->status = IOSTAT_SUCCESS;
  624. num_posted++;
  625. }
  626. /* success, put on SCSI buffer sgl list */
  627. list_add_tail(&psb->list, &scsi_sblist);
  628. }
  629. }
  630. /* continue until a nembed page worth of sgls */
  631. if (post_cnt == 0)
  632. continue;
  633. /* post block of SCSI buffer list sgls */
  634. status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
  635. post_cnt);
  636. /* don't reset xirtag due to hole in xri block */
  637. if (block_cnt == 0)
  638. last_xritag = NO_XRI;
  639. /* reset SCSI buffer post count for next round of posting */
  640. post_cnt = 0;
  641. /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
  642. while (!list_empty(&blck_sblist)) {
  643. list_remove_head(&blck_sblist, psb,
  644. struct lpfc_scsi_buf, list);
  645. if (status) {
  646. /* failure, put on abort scsi list */
  647. psb->exch_busy = 1;
  648. } else {
  649. /* success, put on SCSI buffer list */
  650. psb->exch_busy = 0;
  651. psb->status = IOSTAT_SUCCESS;
  652. num_posted++;
  653. }
  654. list_add_tail(&psb->list, &scsi_sblist);
  655. }
  656. }
  657. /* Push SCSI buffers with sgl posted to the availble list */
  658. while (!list_empty(&scsi_sblist)) {
  659. list_remove_head(&scsi_sblist, psb,
  660. struct lpfc_scsi_buf, list);
  661. lpfc_release_scsi_buf_s4(phba, psb);
  662. }
  663. return num_posted;
  664. }
  665. /**
  666. * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
  667. * @phba: pointer to lpfc hba data structure.
  668. *
  669. * This routine walks the list of scsi buffers that have been allocated and
  670. * repost them to the port by using SGL block post. This is needed after a
  671. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  672. * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
  673. * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
  674. *
  675. * Returns: 0 = success, non-zero failure.
  676. **/
  677. int
  678. lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
  679. {
  680. LIST_HEAD(post_sblist);
  681. int num_posted, rc = 0;
  682. /* get all SCSI buffers need to repost to a local list */
  683. spin_lock_irq(&phba->scsi_buf_list_get_lock);
  684. spin_lock(&phba->scsi_buf_list_put_lock);
  685. list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
  686. list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
  687. spin_unlock(&phba->scsi_buf_list_put_lock);
  688. spin_unlock_irq(&phba->scsi_buf_list_get_lock);
  689. /* post the list of scsi buffer sgls to port if available */
  690. if (!list_empty(&post_sblist)) {
  691. num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
  692. phba->sli4_hba.scsi_xri_cnt);
  693. /* failed to post any scsi buffer, return error */
  694. if (num_posted == 0)
  695. rc = -EIO;
  696. }
  697. return rc;
  698. }
  699. /**
  700. * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
  701. * @vport: The virtual port for which this call being executed.
  702. * @num_to_allocate: The requested number of buffers to allocate.
  703. *
  704. * This routine allocates scsi buffers for device with SLI-4 interface spec,
  705. * the scsi buffer contains all the necessary information needed to initiate
  706. * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
  707. * them on a list, it post them to the port by using SGL block post.
  708. *
  709. * Return codes:
  710. * int - number of scsi buffers that were allocated and posted.
  711. * 0 = failure, less than num_to_alloc is a partial failure.
  712. **/
  713. static int
  714. lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
  715. {
  716. struct lpfc_hba *phba = vport->phba;
  717. struct lpfc_scsi_buf *psb;
  718. struct sli4_sge *sgl;
  719. IOCB_t *iocb;
  720. dma_addr_t pdma_phys_fcp_cmd;
  721. dma_addr_t pdma_phys_fcp_rsp;
  722. dma_addr_t pdma_phys_bpl;
  723. uint16_t iotag, lxri = 0;
  724. int bcnt, num_posted, sgl_size;
  725. LIST_HEAD(prep_sblist);
  726. LIST_HEAD(post_sblist);
  727. LIST_HEAD(scsi_sblist);
  728. sgl_size = phba->cfg_sg_dma_buf_size -
  729. (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  730. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  731. "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
  732. num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
  733. (int)sizeof(struct fcp_cmnd),
  734. (int)sizeof(struct fcp_rsp));
  735. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  736. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  737. if (!psb)
  738. break;
  739. /*
  740. * Get memory from the pci pool to map the virt space to
  741. * pci bus space for an I/O. The DMA buffer includes space
  742. * for the struct fcp_cmnd, struct fcp_rsp and the number
  743. * of bde's necessary to support the sg_tablesize.
  744. */
  745. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  746. GFP_KERNEL, &psb->dma_handle);
  747. if (!psb->data) {
  748. kfree(psb);
  749. break;
  750. }
  751. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  752. /*
  753. * 4K Page alignment is CRITICAL to BlockGuard, double check
  754. * to be sure.
  755. */
  756. if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
  757. (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
  758. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  759. psb->data, psb->dma_handle);
  760. kfree(psb);
  761. break;
  762. }
  763. lxri = lpfc_sli4_next_xritag(phba);
  764. if (lxri == NO_XRI) {
  765. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  766. psb->data, psb->dma_handle);
  767. kfree(psb);
  768. break;
  769. }
  770. /* Allocate iotag for psb->cur_iocbq. */
  771. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  772. if (iotag == 0) {
  773. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  774. psb->data, psb->dma_handle);
  775. kfree(psb);
  776. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  777. "3368 Failed to allocated IOTAG for"
  778. " XRI:0x%x\n", lxri);
  779. lpfc_sli4_free_xri(phba, lxri);
  780. break;
  781. }
  782. psb->cur_iocbq.sli4_lxritag = lxri;
  783. psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  784. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  785. psb->fcp_bpl = psb->data;
  786. psb->fcp_cmnd = (psb->data + sgl_size);
  787. psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
  788. sizeof(struct fcp_cmnd));
  789. /* Initialize local short-hand pointers. */
  790. sgl = (struct sli4_sge *)psb->fcp_bpl;
  791. pdma_phys_bpl = psb->dma_handle;
  792. pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
  793. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  794. /*
  795. * The first two bdes are the FCP_CMD and FCP_RSP.
  796. * The balance are sg list bdes. Initialize the
  797. * first two and leave the rest for queuecommand.
  798. */
  799. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  800. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  801. sgl->word2 = le32_to_cpu(sgl->word2);
  802. bf_set(lpfc_sli4_sge_last, sgl, 0);
  803. sgl->word2 = cpu_to_le32(sgl->word2);
  804. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
  805. sgl++;
  806. /* Setup the physical region for the FCP RSP */
  807. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  808. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  809. sgl->word2 = le32_to_cpu(sgl->word2);
  810. bf_set(lpfc_sli4_sge_last, sgl, 1);
  811. sgl->word2 = cpu_to_le32(sgl->word2);
  812. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
  813. /*
  814. * Since the IOCB for the FCP I/O is built into this
  815. * lpfc_scsi_buf, initialize it with all known data now.
  816. */
  817. iocb = &psb->cur_iocbq.iocb;
  818. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  819. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
  820. /* setting the BLP size to 2 * sizeof BDE may not be correct.
  821. * We are setting the bpl to point to out sgl. An sgl's
  822. * entries are 16 bytes, a bpl entries are 12 bytes.
  823. */
  824. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  825. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
  826. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
  827. iocb->ulpBdeCount = 1;
  828. iocb->ulpLe = 1;
  829. iocb->ulpClass = CLASS3;
  830. psb->cur_iocbq.context1 = psb;
  831. psb->dma_phys_bpl = pdma_phys_bpl;
  832. /* add the scsi buffer to a post list */
  833. list_add_tail(&psb->list, &post_sblist);
  834. spin_lock_irq(&phba->scsi_buf_list_get_lock);
  835. phba->sli4_hba.scsi_xri_cnt++;
  836. spin_unlock_irq(&phba->scsi_buf_list_get_lock);
  837. }
  838. lpfc_printf_log(phba, KERN_INFO, LOG_BG,
  839. "3021 Allocate %d out of %d requested new SCSI "
  840. "buffers\n", bcnt, num_to_alloc);
  841. /* post the list of scsi buffer sgls to port if available */
  842. if (!list_empty(&post_sblist))
  843. num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
  844. &post_sblist, bcnt);
  845. else
  846. num_posted = 0;
  847. return num_posted;
  848. }
  849. /**
  850. * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
  851. * @vport: The virtual port for which this call being executed.
  852. * @num_to_allocate: The requested number of buffers to allocate.
  853. *
  854. * This routine wraps the actual SCSI buffer allocator function pointer from
  855. * the lpfc_hba struct.
  856. *
  857. * Return codes:
  858. * int - number of scsi buffers that were allocated.
  859. * 0 = failure, less than num_to_alloc is a partial failure.
  860. **/
  861. static inline int
  862. lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
  863. {
  864. return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
  865. }
  866. /**
  867. * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  868. * @phba: The HBA for which this call is being executed.
  869. *
  870. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  871. * and returns to caller.
  872. *
  873. * Return codes:
  874. * NULL - Error
  875. * Pointer to lpfc_scsi_buf - Success
  876. **/
  877. static struct lpfc_scsi_buf*
  878. lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  879. {
  880. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  881. struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
  882. unsigned long iflag = 0;
  883. spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
  884. list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
  885. list);
  886. if (!lpfc_cmd) {
  887. spin_lock(&phba->scsi_buf_list_put_lock);
  888. list_splice(&phba->lpfc_scsi_buf_list_put,
  889. &phba->lpfc_scsi_buf_list_get);
  890. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
  891. list_remove_head(scsi_buf_list_get, lpfc_cmd,
  892. struct lpfc_scsi_buf, list);
  893. spin_unlock(&phba->scsi_buf_list_put_lock);
  894. }
  895. spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
  896. return lpfc_cmd;
  897. }
  898. /**
  899. * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  900. * @phba: The HBA for which this call is being executed.
  901. *
  902. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  903. * and returns to caller.
  904. *
  905. * Return codes:
  906. * NULL - Error
  907. * Pointer to lpfc_scsi_buf - Success
  908. **/
  909. static struct lpfc_scsi_buf*
  910. lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  911. {
  912. struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
  913. unsigned long iflag = 0;
  914. int found = 0;
  915. spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
  916. list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
  917. &phba->lpfc_scsi_buf_list_get, list) {
  918. if (lpfc_test_rrq_active(phba, ndlp,
  919. lpfc_cmd->cur_iocbq.sli4_lxritag))
  920. continue;
  921. list_del(&lpfc_cmd->list);
  922. found = 1;
  923. break;
  924. }
  925. if (!found) {
  926. spin_lock(&phba->scsi_buf_list_put_lock);
  927. list_splice(&phba->lpfc_scsi_buf_list_put,
  928. &phba->lpfc_scsi_buf_list_get);
  929. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
  930. spin_unlock(&phba->scsi_buf_list_put_lock);
  931. list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
  932. &phba->lpfc_scsi_buf_list_get, list) {
  933. if (lpfc_test_rrq_active(
  934. phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
  935. continue;
  936. list_del(&lpfc_cmd->list);
  937. found = 1;
  938. break;
  939. }
  940. }
  941. spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
  942. if (!found)
  943. return NULL;
  944. return lpfc_cmd;
  945. }
  946. /**
  947. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  948. * @phba: The HBA for which this call is being executed.
  949. *
  950. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  951. * and returns to caller.
  952. *
  953. * Return codes:
  954. * NULL - Error
  955. * Pointer to lpfc_scsi_buf - Success
  956. **/
  957. static struct lpfc_scsi_buf*
  958. lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  959. {
  960. return phba->lpfc_get_scsi_buf(phba, ndlp);
  961. }
  962. /**
  963. * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
  964. * @phba: The Hba for which this call is being executed.
  965. * @psb: The scsi buffer which is being released.
  966. *
  967. * This routine releases @psb scsi buffer by adding it to tail of @phba
  968. * lpfc_scsi_buf_list list.
  969. **/
  970. static void
  971. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  972. {
  973. unsigned long iflag = 0;
  974. psb->seg_cnt = 0;
  975. psb->nonsg_phys = 0;
  976. psb->prot_seg_cnt = 0;
  977. spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
  978. psb->pCmd = NULL;
  979. psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
  980. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
  981. spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
  982. }
  983. /**
  984. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  985. * @phba: The Hba for which this call is being executed.
  986. * @psb: The scsi buffer which is being released.
  987. *
  988. * This routine releases @psb scsi buffer by adding it to tail of @phba
  989. * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  990. * and cannot be reused for at least RA_TOV amount of time if it was
  991. * aborted.
  992. **/
  993. static void
  994. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  995. {
  996. unsigned long iflag = 0;
  997. psb->seg_cnt = 0;
  998. psb->nonsg_phys = 0;
  999. psb->prot_seg_cnt = 0;
  1000. if (psb->exch_busy) {
  1001. spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1002. iflag);
  1003. psb->pCmd = NULL;
  1004. list_add_tail(&psb->list,
  1005. &phba->sli4_hba.lpfc_abts_scsi_buf_list);
  1006. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1007. iflag);
  1008. } else {
  1009. psb->pCmd = NULL;
  1010. psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
  1011. spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
  1012. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
  1013. spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
  1014. }
  1015. }
  1016. /**
  1017. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  1018. * @phba: The Hba for which this call is being executed.
  1019. * @psb: The scsi buffer which is being released.
  1020. *
  1021. * This routine releases @psb scsi buffer by adding it to tail of @phba
  1022. * lpfc_scsi_buf_list list.
  1023. **/
  1024. static void
  1025. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1026. {
  1027. phba->lpfc_release_scsi_buf(phba, psb);
  1028. }
  1029. /**
  1030. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  1031. * @phba: The Hba for which this call is being executed.
  1032. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1033. *
  1034. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1035. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  1036. * through sg elements and format the bdea. This routine also initializes all
  1037. * IOCB fields which are dependent on scsi command request buffer.
  1038. *
  1039. * Return codes:
  1040. * 1 - Error
  1041. * 0 - Success
  1042. **/
  1043. static int
  1044. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1045. {
  1046. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1047. struct scatterlist *sgel = NULL;
  1048. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1049. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1050. struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
  1051. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1052. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  1053. dma_addr_t physaddr;
  1054. uint32_t num_bde = 0;
  1055. int nseg, datadir = scsi_cmnd->sc_data_direction;
  1056. /*
  1057. * There are three possibilities here - use scatter-gather segment, use
  1058. * the single mapping, or neither. Start the lpfc command prep by
  1059. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1060. * data bde entry.
  1061. */
  1062. bpl += 2;
  1063. if (scsi_sg_count(scsi_cmnd)) {
  1064. /*
  1065. * The driver stores the segment count returned from pci_map_sg
  1066. * because this a count of dma-mappings used to map the use_sg
  1067. * pages. They are not guaranteed to be the same for those
  1068. * architectures that implement an IOMMU.
  1069. */
  1070. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  1071. scsi_sg_count(scsi_cmnd), datadir);
  1072. if (unlikely(!nseg))
  1073. return 1;
  1074. lpfc_cmd->seg_cnt = nseg;
  1075. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1076. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1077. "9064 BLKGRD: %s: Too many sg segments from "
  1078. "dma_map_sg. Config %d, seg_cnt %d\n",
  1079. __func__, phba->cfg_sg_seg_cnt,
  1080. lpfc_cmd->seg_cnt);
  1081. lpfc_cmd->seg_cnt = 0;
  1082. scsi_dma_unmap(scsi_cmnd);
  1083. return 1;
  1084. }
  1085. /*
  1086. * The driver established a maximum scatter-gather segment count
  1087. * during probe that limits the number of sg elements in any
  1088. * single scsi command. Just run through the seg_cnt and format
  1089. * the bde's.
  1090. * When using SLI-3 the driver will try to fit all the BDEs into
  1091. * the IOCB. If it can't then the BDEs get added to a BPL as it
  1092. * does for SLI-2 mode.
  1093. */
  1094. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  1095. physaddr = sg_dma_address(sgel);
  1096. if (phba->sli_rev == 3 &&
  1097. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1098. !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
  1099. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  1100. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1101. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  1102. data_bde->addrLow = putPaddrLow(physaddr);
  1103. data_bde->addrHigh = putPaddrHigh(physaddr);
  1104. data_bde++;
  1105. } else {
  1106. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1107. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  1108. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1109. bpl->addrLow =
  1110. le32_to_cpu(putPaddrLow(physaddr));
  1111. bpl->addrHigh =
  1112. le32_to_cpu(putPaddrHigh(physaddr));
  1113. bpl++;
  1114. }
  1115. }
  1116. }
  1117. /*
  1118. * Finish initializing those IOCB fields that are dependent on the
  1119. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  1120. * explicitly reinitialized and for SLI-3 the extended bde count is
  1121. * explicitly reinitialized since all iocb memory resources are reused.
  1122. */
  1123. if (phba->sli_rev == 3 &&
  1124. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1125. !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
  1126. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  1127. /*
  1128. * The extended IOCB format can only fit 3 BDE or a BPL.
  1129. * This I/O has more than 3 BDE so the 1st data bde will
  1130. * be a BPL that is filled in here.
  1131. */
  1132. physaddr = lpfc_cmd->dma_handle;
  1133. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  1134. data_bde->tus.f.bdeSize = (num_bde *
  1135. sizeof(struct ulp_bde64));
  1136. physaddr += (sizeof(struct fcp_cmnd) +
  1137. sizeof(struct fcp_rsp) +
  1138. (2 * sizeof(struct ulp_bde64)));
  1139. data_bde->addrHigh = putPaddrHigh(physaddr);
  1140. data_bde->addrLow = putPaddrLow(physaddr);
  1141. /* ebde count includes the response bde and data bpl */
  1142. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  1143. } else {
  1144. /* ebde count includes the response bde and data bdes */
  1145. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1146. }
  1147. } else {
  1148. iocb_cmd->un.fcpi64.bdl.bdeSize =
  1149. ((num_bde + 2) * sizeof(struct ulp_bde64));
  1150. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1151. }
  1152. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1153. /*
  1154. * Due to difference in data length between DIF/non-DIF paths,
  1155. * we need to set word 4 of IOCB here
  1156. */
  1157. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1158. return 0;
  1159. }
  1160. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1161. /* Return if if error injection is detected by Initiator */
  1162. #define BG_ERR_INIT 0x1
  1163. /* Return if if error injection is detected by Target */
  1164. #define BG_ERR_TGT 0x2
  1165. /* Return if if swapping CSUM<-->CRC is required for error injection */
  1166. #define BG_ERR_SWAP 0x10
  1167. /* Return if disabling Guard/Ref/App checking is required for error injection */
  1168. #define BG_ERR_CHECK 0x20
  1169. /**
  1170. * lpfc_bg_err_inject - Determine if we should inject an error
  1171. * @phba: The Hba for which this call is being executed.
  1172. * @sc: The SCSI command to examine
  1173. * @reftag: (out) BlockGuard reference tag for transmitted data
  1174. * @apptag: (out) BlockGuard application tag for transmitted data
  1175. * @new_guard (in) Value to replace CRC with if needed
  1176. *
  1177. * Returns BG_ERR_* bit mask or 0 if request ignored
  1178. **/
  1179. static int
  1180. lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1181. uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
  1182. {
  1183. struct scatterlist *sgpe; /* s/g prot entry */
  1184. struct lpfc_scsi_buf *lpfc_cmd = NULL;
  1185. struct scsi_dif_tuple *src = NULL;
  1186. struct lpfc_nodelist *ndlp;
  1187. struct lpfc_rport_data *rdata;
  1188. uint32_t op = scsi_get_prot_op(sc);
  1189. uint32_t blksize;
  1190. uint32_t numblks;
  1191. sector_t lba;
  1192. int rc = 0;
  1193. int blockoff = 0;
  1194. if (op == SCSI_PROT_NORMAL)
  1195. return 0;
  1196. sgpe = scsi_prot_sglist(sc);
  1197. lba = scsi_get_lba(sc);
  1198. /* First check if we need to match the LBA */
  1199. if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
  1200. blksize = lpfc_cmd_blksize(sc);
  1201. numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
  1202. /* Make sure we have the right LBA if one is specified */
  1203. if ((phba->lpfc_injerr_lba < lba) ||
  1204. (phba->lpfc_injerr_lba >= (lba + numblks)))
  1205. return 0;
  1206. if (sgpe) {
  1207. blockoff = phba->lpfc_injerr_lba - lba;
  1208. numblks = sg_dma_len(sgpe) /
  1209. sizeof(struct scsi_dif_tuple);
  1210. if (numblks < blockoff)
  1211. blockoff = numblks;
  1212. }
  1213. }
  1214. /* Next check if we need to match the remote NPortID or WWPN */
  1215. rdata = lpfc_rport_data_from_scsi_device(sc->device);
  1216. if (rdata && rdata->pnode) {
  1217. ndlp = rdata->pnode;
  1218. /* Make sure we have the right NPortID if one is specified */
  1219. if (phba->lpfc_injerr_nportid &&
  1220. (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
  1221. return 0;
  1222. /*
  1223. * Make sure we have the right WWPN if one is specified.
  1224. * wwn[0] should be a non-zero NAA in a good WWPN.
  1225. */
  1226. if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
  1227. (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
  1228. sizeof(struct lpfc_name)) != 0))
  1229. return 0;
  1230. }
  1231. /* Setup a ptr to the protection data if the SCSI host provides it */
  1232. if (sgpe) {
  1233. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  1234. src += blockoff;
  1235. lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
  1236. }
  1237. /* Should we change the Reference Tag */
  1238. if (reftag) {
  1239. if (phba->lpfc_injerr_wref_cnt) {
  1240. switch (op) {
  1241. case SCSI_PROT_WRITE_PASS:
  1242. if (src) {
  1243. /*
  1244. * For WRITE_PASS, force the error
  1245. * to be sent on the wire. It should
  1246. * be detected by the Target.
  1247. * If blockoff != 0 error will be
  1248. * inserted in middle of the IO.
  1249. */
  1250. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1251. "9076 BLKGRD: Injecting reftag error: "
  1252. "write lba x%lx + x%x oldrefTag x%x\n",
  1253. (unsigned long)lba, blockoff,
  1254. be32_to_cpu(src->ref_tag));
  1255. /*
  1256. * Save the old ref_tag so we can
  1257. * restore it on completion.
  1258. */
  1259. if (lpfc_cmd) {
  1260. lpfc_cmd->prot_data_type =
  1261. LPFC_INJERR_REFTAG;
  1262. lpfc_cmd->prot_data_segment =
  1263. src;
  1264. lpfc_cmd->prot_data =
  1265. src->ref_tag;
  1266. }
  1267. src->ref_tag = cpu_to_be32(0xDEADBEEF);
  1268. phba->lpfc_injerr_wref_cnt--;
  1269. if (phba->lpfc_injerr_wref_cnt == 0) {
  1270. phba->lpfc_injerr_nportid = 0;
  1271. phba->lpfc_injerr_lba =
  1272. LPFC_INJERR_LBA_OFF;
  1273. memset(&phba->lpfc_injerr_wwpn,
  1274. 0, sizeof(struct lpfc_name));
  1275. }
  1276. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1277. break;
  1278. }
  1279. /* Drop thru */
  1280. case SCSI_PROT_WRITE_INSERT:
  1281. /*
  1282. * For WRITE_INSERT, force the error
  1283. * to be sent on the wire. It should be
  1284. * detected by the Target.
  1285. */
  1286. /* DEADBEEF will be the reftag on the wire */
  1287. *reftag = 0xDEADBEEF;
  1288. phba->lpfc_injerr_wref_cnt--;
  1289. if (phba->lpfc_injerr_wref_cnt == 0) {
  1290. phba->lpfc_injerr_nportid = 0;
  1291. phba->lpfc_injerr_lba =
  1292. LPFC_INJERR_LBA_OFF;
  1293. memset(&phba->lpfc_injerr_wwpn,
  1294. 0, sizeof(struct lpfc_name));
  1295. }
  1296. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1297. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1298. "9078 BLKGRD: Injecting reftag error: "
  1299. "write lba x%lx\n", (unsigned long)lba);
  1300. break;
  1301. case SCSI_PROT_WRITE_STRIP:
  1302. /*
  1303. * For WRITE_STRIP and WRITE_PASS,
  1304. * force the error on data
  1305. * being copied from SLI-Host to SLI-Port.
  1306. */
  1307. *reftag = 0xDEADBEEF;
  1308. phba->lpfc_injerr_wref_cnt--;
  1309. if (phba->lpfc_injerr_wref_cnt == 0) {
  1310. phba->lpfc_injerr_nportid = 0;
  1311. phba->lpfc_injerr_lba =
  1312. LPFC_INJERR_LBA_OFF;
  1313. memset(&phba->lpfc_injerr_wwpn,
  1314. 0, sizeof(struct lpfc_name));
  1315. }
  1316. rc = BG_ERR_INIT;
  1317. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1318. "9077 BLKGRD: Injecting reftag error: "
  1319. "write lba x%lx\n", (unsigned long)lba);
  1320. break;
  1321. }
  1322. }
  1323. if (phba->lpfc_injerr_rref_cnt) {
  1324. switch (op) {
  1325. case SCSI_PROT_READ_INSERT:
  1326. case SCSI_PROT_READ_STRIP:
  1327. case SCSI_PROT_READ_PASS:
  1328. /*
  1329. * For READ_STRIP and READ_PASS, force the
  1330. * error on data being read off the wire. It
  1331. * should force an IO error to the driver.
  1332. */
  1333. *reftag = 0xDEADBEEF;
  1334. phba->lpfc_injerr_rref_cnt--;
  1335. if (phba->lpfc_injerr_rref_cnt == 0) {
  1336. phba->lpfc_injerr_nportid = 0;
  1337. phba->lpfc_injerr_lba =
  1338. LPFC_INJERR_LBA_OFF;
  1339. memset(&phba->lpfc_injerr_wwpn,
  1340. 0, sizeof(struct lpfc_name));
  1341. }
  1342. rc = BG_ERR_INIT;
  1343. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1344. "9079 BLKGRD: Injecting reftag error: "
  1345. "read lba x%lx\n", (unsigned long)lba);
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. /* Should we change the Application Tag */
  1351. if (apptag) {
  1352. if (phba->lpfc_injerr_wapp_cnt) {
  1353. switch (op) {
  1354. case SCSI_PROT_WRITE_PASS:
  1355. if (src) {
  1356. /*
  1357. * For WRITE_PASS, force the error
  1358. * to be sent on the wire. It should
  1359. * be detected by the Target.
  1360. * If blockoff != 0 error will be
  1361. * inserted in middle of the IO.
  1362. */
  1363. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1364. "9080 BLKGRD: Injecting apptag error: "
  1365. "write lba x%lx + x%x oldappTag x%x\n",
  1366. (unsigned long)lba, blockoff,
  1367. be16_to_cpu(src->app_tag));
  1368. /*
  1369. * Save the old app_tag so we can
  1370. * restore it on completion.
  1371. */
  1372. if (lpfc_cmd) {
  1373. lpfc_cmd->prot_data_type =
  1374. LPFC_INJERR_APPTAG;
  1375. lpfc_cmd->prot_data_segment =
  1376. src;
  1377. lpfc_cmd->prot_data =
  1378. src->app_tag;
  1379. }
  1380. src->app_tag = cpu_to_be16(0xDEAD);
  1381. phba->lpfc_injerr_wapp_cnt--;
  1382. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1383. phba->lpfc_injerr_nportid = 0;
  1384. phba->lpfc_injerr_lba =
  1385. LPFC_INJERR_LBA_OFF;
  1386. memset(&phba->lpfc_injerr_wwpn,
  1387. 0, sizeof(struct lpfc_name));
  1388. }
  1389. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1390. break;
  1391. }
  1392. /* Drop thru */
  1393. case SCSI_PROT_WRITE_INSERT:
  1394. /*
  1395. * For WRITE_INSERT, force the
  1396. * error to be sent on the wire. It should be
  1397. * detected by the Target.
  1398. */
  1399. /* DEAD will be the apptag on the wire */
  1400. *apptag = 0xDEAD;
  1401. phba->lpfc_injerr_wapp_cnt--;
  1402. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1403. phba->lpfc_injerr_nportid = 0;
  1404. phba->lpfc_injerr_lba =
  1405. LPFC_INJERR_LBA_OFF;
  1406. memset(&phba->lpfc_injerr_wwpn,
  1407. 0, sizeof(struct lpfc_name));
  1408. }
  1409. rc = BG_ERR_TGT | BG_ERR_CHECK;
  1410. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1411. "0813 BLKGRD: Injecting apptag error: "
  1412. "write lba x%lx\n", (unsigned long)lba);
  1413. break;
  1414. case SCSI_PROT_WRITE_STRIP:
  1415. /*
  1416. * For WRITE_STRIP and WRITE_PASS,
  1417. * force the error on data
  1418. * being copied from SLI-Host to SLI-Port.
  1419. */
  1420. *apptag = 0xDEAD;
  1421. phba->lpfc_injerr_wapp_cnt--;
  1422. if (phba->lpfc_injerr_wapp_cnt == 0) {
  1423. phba->lpfc_injerr_nportid = 0;
  1424. phba->lpfc_injerr_lba =
  1425. LPFC_INJERR_LBA_OFF;
  1426. memset(&phba->lpfc_injerr_wwpn,
  1427. 0, sizeof(struct lpfc_name));
  1428. }
  1429. rc = BG_ERR_INIT;
  1430. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1431. "0812 BLKGRD: Injecting apptag error: "
  1432. "write lba x%lx\n", (unsigned long)lba);
  1433. break;
  1434. }
  1435. }
  1436. if (phba->lpfc_injerr_rapp_cnt) {
  1437. switch (op) {
  1438. case SCSI_PROT_READ_INSERT:
  1439. case SCSI_PROT_READ_STRIP:
  1440. case SCSI_PROT_READ_PASS:
  1441. /*
  1442. * For READ_STRIP and READ_PASS, force the
  1443. * error on data being read off the wire. It
  1444. * should force an IO error to the driver.
  1445. */
  1446. *apptag = 0xDEAD;
  1447. phba->lpfc_injerr_rapp_cnt--;
  1448. if (phba->lpfc_injerr_rapp_cnt == 0) {
  1449. phba->lpfc_injerr_nportid = 0;
  1450. phba->lpfc_injerr_lba =
  1451. LPFC_INJERR_LBA_OFF;
  1452. memset(&phba->lpfc_injerr_wwpn,
  1453. 0, sizeof(struct lpfc_name));
  1454. }
  1455. rc = BG_ERR_INIT;
  1456. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1457. "0814 BLKGRD: Injecting apptag error: "
  1458. "read lba x%lx\n", (unsigned long)lba);
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. /* Should we change the Guard Tag */
  1464. if (new_guard) {
  1465. if (phba->lpfc_injerr_wgrd_cnt) {
  1466. switch (op) {
  1467. case SCSI_PROT_WRITE_PASS:
  1468. rc = BG_ERR_CHECK;
  1469. /* Drop thru */
  1470. case SCSI_PROT_WRITE_INSERT:
  1471. /*
  1472. * For WRITE_INSERT, force the
  1473. * error to be sent on the wire. It should be
  1474. * detected by the Target.
  1475. */
  1476. phba->lpfc_injerr_wgrd_cnt--;
  1477. if (phba->lpfc_injerr_wgrd_cnt == 0) {
  1478. phba->lpfc_injerr_nportid = 0;
  1479. phba->lpfc_injerr_lba =
  1480. LPFC_INJERR_LBA_OFF;
  1481. memset(&phba->lpfc_injerr_wwpn,
  1482. 0, sizeof(struct lpfc_name));
  1483. }
  1484. rc |= BG_ERR_TGT | BG_ERR_SWAP;
  1485. /* Signals the caller to swap CRC->CSUM */
  1486. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1487. "0817 BLKGRD: Injecting guard error: "
  1488. "write lba x%lx\n", (unsigned long)lba);
  1489. break;
  1490. case SCSI_PROT_WRITE_STRIP:
  1491. /*
  1492. * For WRITE_STRIP and WRITE_PASS,
  1493. * force the error on data
  1494. * being copied from SLI-Host to SLI-Port.
  1495. */
  1496. phba->lpfc_injerr_wgrd_cnt--;
  1497. if (phba->lpfc_injerr_wgrd_cnt == 0) {
  1498. phba->lpfc_injerr_nportid = 0;
  1499. phba->lpfc_injerr_lba =
  1500. LPFC_INJERR_LBA_OFF;
  1501. memset(&phba->lpfc_injerr_wwpn,
  1502. 0, sizeof(struct lpfc_name));
  1503. }
  1504. rc = BG_ERR_INIT | BG_ERR_SWAP;
  1505. /* Signals the caller to swap CRC->CSUM */
  1506. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1507. "0816 BLKGRD: Injecting guard error: "
  1508. "write lba x%lx\n", (unsigned long)lba);
  1509. break;
  1510. }
  1511. }
  1512. if (phba->lpfc_injerr_rgrd_cnt) {
  1513. switch (op) {
  1514. case SCSI_PROT_READ_INSERT:
  1515. case SCSI_PROT_READ_STRIP:
  1516. case SCSI_PROT_READ_PASS:
  1517. /*
  1518. * For READ_STRIP and READ_PASS, force the
  1519. * error on data being read off the wire. It
  1520. * should force an IO error to the driver.
  1521. */
  1522. phba->lpfc_injerr_rgrd_cnt--;
  1523. if (phba->lpfc_injerr_rgrd_cnt == 0) {
  1524. phba->lpfc_injerr_nportid = 0;
  1525. phba->lpfc_injerr_lba =
  1526. LPFC_INJERR_LBA_OFF;
  1527. memset(&phba->lpfc_injerr_wwpn,
  1528. 0, sizeof(struct lpfc_name));
  1529. }
  1530. rc = BG_ERR_INIT | BG_ERR_SWAP;
  1531. /* Signals the caller to swap CRC->CSUM */
  1532. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1533. "0818 BLKGRD: Injecting guard error: "
  1534. "read lba x%lx\n", (unsigned long)lba);
  1535. }
  1536. }
  1537. }
  1538. return rc;
  1539. }
  1540. #endif
  1541. /**
  1542. * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
  1543. * the specified SCSI command.
  1544. * @phba: The Hba for which this call is being executed.
  1545. * @sc: The SCSI command to examine
  1546. * @txopt: (out) BlockGuard operation for transmitted data
  1547. * @rxopt: (out) BlockGuard operation for received data
  1548. *
  1549. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1550. *
  1551. **/
  1552. static int
  1553. lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1554. uint8_t *txop, uint8_t *rxop)
  1555. {
  1556. uint8_t ret = 0;
  1557. if (lpfc_cmd_guard_csum(sc)) {
  1558. switch (scsi_get_prot_op(sc)) {
  1559. case SCSI_PROT_READ_INSERT:
  1560. case SCSI_PROT_WRITE_STRIP:
  1561. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1562. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1563. break;
  1564. case SCSI_PROT_READ_STRIP:
  1565. case SCSI_PROT_WRITE_INSERT:
  1566. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1567. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1568. break;
  1569. case SCSI_PROT_READ_PASS:
  1570. case SCSI_PROT_WRITE_PASS:
  1571. *rxop = BG_OP_IN_CRC_OUT_CSUM;
  1572. *txop = BG_OP_IN_CSUM_OUT_CRC;
  1573. break;
  1574. case SCSI_PROT_NORMAL:
  1575. default:
  1576. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1577. "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
  1578. scsi_get_prot_op(sc));
  1579. ret = 1;
  1580. break;
  1581. }
  1582. } else {
  1583. switch (scsi_get_prot_op(sc)) {
  1584. case SCSI_PROT_READ_STRIP:
  1585. case SCSI_PROT_WRITE_INSERT:
  1586. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1587. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1588. break;
  1589. case SCSI_PROT_READ_PASS:
  1590. case SCSI_PROT_WRITE_PASS:
  1591. *rxop = BG_OP_IN_CRC_OUT_CRC;
  1592. *txop = BG_OP_IN_CRC_OUT_CRC;
  1593. break;
  1594. case SCSI_PROT_READ_INSERT:
  1595. case SCSI_PROT_WRITE_STRIP:
  1596. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1597. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1598. break;
  1599. case SCSI_PROT_NORMAL:
  1600. default:
  1601. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1602. "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
  1603. scsi_get_prot_op(sc));
  1604. ret = 1;
  1605. break;
  1606. }
  1607. }
  1608. return ret;
  1609. }
  1610. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1611. /**
  1612. * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
  1613. * the specified SCSI command in order to force a guard tag error.
  1614. * @phba: The Hba for which this call is being executed.
  1615. * @sc: The SCSI command to examine
  1616. * @txopt: (out) BlockGuard operation for transmitted data
  1617. * @rxopt: (out) BlockGuard operation for received data
  1618. *
  1619. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1620. *
  1621. **/
  1622. static int
  1623. lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1624. uint8_t *txop, uint8_t *rxop)
  1625. {
  1626. uint8_t ret = 0;
  1627. if (lpfc_cmd_guard_csum(sc)) {
  1628. switch (scsi_get_prot_op(sc)) {
  1629. case SCSI_PROT_READ_INSERT:
  1630. case SCSI_PROT_WRITE_STRIP:
  1631. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1632. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1633. break;
  1634. case SCSI_PROT_READ_STRIP:
  1635. case SCSI_PROT_WRITE_INSERT:
  1636. *rxop = BG_OP_IN_CSUM_OUT_NODIF;
  1637. *txop = BG_OP_IN_NODIF_OUT_CSUM;
  1638. break;
  1639. case SCSI_PROT_READ_PASS:
  1640. case SCSI_PROT_WRITE_PASS:
  1641. *rxop = BG_OP_IN_CSUM_OUT_CRC;
  1642. *txop = BG_OP_IN_CRC_OUT_CSUM;
  1643. break;
  1644. case SCSI_PROT_NORMAL:
  1645. default:
  1646. break;
  1647. }
  1648. } else {
  1649. switch (scsi_get_prot_op(sc)) {
  1650. case SCSI_PROT_READ_STRIP:
  1651. case SCSI_PROT_WRITE_INSERT:
  1652. *rxop = BG_OP_IN_CSUM_OUT_NODIF;
  1653. *txop = BG_OP_IN_NODIF_OUT_CSUM;
  1654. break;
  1655. case SCSI_PROT_READ_PASS:
  1656. case SCSI_PROT_WRITE_PASS:
  1657. *rxop = BG_OP_IN_CSUM_OUT_CSUM;
  1658. *txop = BG_OP_IN_CSUM_OUT_CSUM;
  1659. break;
  1660. case SCSI_PROT_READ_INSERT:
  1661. case SCSI_PROT_WRITE_STRIP:
  1662. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1663. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1664. break;
  1665. case SCSI_PROT_NORMAL:
  1666. default:
  1667. break;
  1668. }
  1669. }
  1670. return ret;
  1671. }
  1672. #endif
  1673. /**
  1674. * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
  1675. * @phba: The Hba for which this call is being executed.
  1676. * @sc: pointer to scsi command we're working on
  1677. * @bpl: pointer to buffer list for protection groups
  1678. * @datacnt: number of segments of data that have been dma mapped
  1679. *
  1680. * This function sets up BPL buffer list for protection groups of
  1681. * type LPFC_PG_TYPE_NO_DIF
  1682. *
  1683. * This is usually used when the HBA is instructed to generate
  1684. * DIFs and insert them into data stream (or strip DIF from
  1685. * incoming data stream)
  1686. *
  1687. * The buffer list consists of just one protection group described
  1688. * below:
  1689. * +-------------------------+
  1690. * start of prot group --> | PDE_5 |
  1691. * +-------------------------+
  1692. * | PDE_6 |
  1693. * +-------------------------+
  1694. * | Data BDE |
  1695. * +-------------------------+
  1696. * |more Data BDE's ... (opt)|
  1697. * +-------------------------+
  1698. *
  1699. *
  1700. * Note: Data s/g buffers have been dma mapped
  1701. *
  1702. * Returns the number of BDEs added to the BPL.
  1703. **/
  1704. static int
  1705. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1706. struct ulp_bde64 *bpl, int datasegcnt)
  1707. {
  1708. struct scatterlist *sgde = NULL; /* s/g data entry */
  1709. struct lpfc_pde5 *pde5 = NULL;
  1710. struct lpfc_pde6 *pde6 = NULL;
  1711. dma_addr_t physaddr;
  1712. int i = 0, num_bde = 0, status;
  1713. int datadir = sc->sc_data_direction;
  1714. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1715. uint32_t rc;
  1716. #endif
  1717. uint32_t checking = 1;
  1718. uint32_t reftag;
  1719. uint8_t txop, rxop;
  1720. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1721. if (status)
  1722. goto out;
  1723. /* extract some info from the scsi command for pde*/
  1724. reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
  1725. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1726. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1727. if (rc) {
  1728. if (rc & BG_ERR_SWAP)
  1729. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1730. if (rc & BG_ERR_CHECK)
  1731. checking = 0;
  1732. }
  1733. #endif
  1734. /* setup PDE5 with what we have */
  1735. pde5 = (struct lpfc_pde5 *) bpl;
  1736. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1737. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1738. /* Endianness conversion if necessary for PDE5 */
  1739. pde5->word0 = cpu_to_le32(pde5->word0);
  1740. pde5->reftag = cpu_to_le32(reftag);
  1741. /* advance bpl and increment bde count */
  1742. num_bde++;
  1743. bpl++;
  1744. pde6 = (struct lpfc_pde6 *) bpl;
  1745. /* setup PDE6 with the rest of the info */
  1746. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1747. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1748. bf_set(pde6_optx, pde6, txop);
  1749. bf_set(pde6_oprx, pde6, rxop);
  1750. /*
  1751. * We only need to check the data on READs, for WRITEs
  1752. * protection data is automatically generated, not checked.
  1753. */
  1754. if (datadir == DMA_FROM_DEVICE) {
  1755. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
  1756. bf_set(pde6_ce, pde6, checking);
  1757. else
  1758. bf_set(pde6_ce, pde6, 0);
  1759. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
  1760. bf_set(pde6_re, pde6, checking);
  1761. else
  1762. bf_set(pde6_re, pde6, 0);
  1763. }
  1764. bf_set(pde6_ai, pde6, 1);
  1765. bf_set(pde6_ae, pde6, 0);
  1766. bf_set(pde6_apptagval, pde6, 0);
  1767. /* Endianness conversion if necessary for PDE6 */
  1768. pde6->word0 = cpu_to_le32(pde6->word0);
  1769. pde6->word1 = cpu_to_le32(pde6->word1);
  1770. pde6->word2 = cpu_to_le32(pde6->word2);
  1771. /* advance bpl and increment bde count */
  1772. num_bde++;
  1773. bpl++;
  1774. /* assumption: caller has already run dma_map_sg on command data */
  1775. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1776. physaddr = sg_dma_address(sgde);
  1777. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1778. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1779. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1780. if (datadir == DMA_TO_DEVICE)
  1781. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1782. else
  1783. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1784. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1785. bpl++;
  1786. num_bde++;
  1787. }
  1788. out:
  1789. return num_bde;
  1790. }
  1791. /**
  1792. * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
  1793. * @phba: The Hba for which this call is being executed.
  1794. * @sc: pointer to scsi command we're working on
  1795. * @bpl: pointer to buffer list for protection groups
  1796. * @datacnt: number of segments of data that have been dma mapped
  1797. * @protcnt: number of segment of protection data that have been dma mapped
  1798. *
  1799. * This function sets up BPL buffer list for protection groups of
  1800. * type LPFC_PG_TYPE_DIF
  1801. *
  1802. * This is usually used when DIFs are in their own buffers,
  1803. * separate from the data. The HBA can then by instructed
  1804. * to place the DIFs in the outgoing stream. For read operations,
  1805. * The HBA could extract the DIFs and place it in DIF buffers.
  1806. *
  1807. * The buffer list for this type consists of one or more of the
  1808. * protection groups described below:
  1809. * +-------------------------+
  1810. * start of first prot group --> | PDE_5 |
  1811. * +-------------------------+
  1812. * | PDE_6 |
  1813. * +-------------------------+
  1814. * | PDE_7 (Prot BDE) |
  1815. * +-------------------------+
  1816. * | Data BDE |
  1817. * +-------------------------+
  1818. * |more Data BDE's ... (opt)|
  1819. * +-------------------------+
  1820. * start of new prot group --> | PDE_5 |
  1821. * +-------------------------+
  1822. * | ... |
  1823. * +-------------------------+
  1824. *
  1825. * Note: It is assumed that both data and protection s/g buffers have been
  1826. * mapped for DMA
  1827. *
  1828. * Returns the number of BDEs added to the BPL.
  1829. **/
  1830. static int
  1831. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1832. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1833. {
  1834. struct scatterlist *sgde = NULL; /* s/g data entry */
  1835. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1836. struct lpfc_pde5 *pde5 = NULL;
  1837. struct lpfc_pde6 *pde6 = NULL;
  1838. struct lpfc_pde7 *pde7 = NULL;
  1839. dma_addr_t dataphysaddr, protphysaddr;
  1840. unsigned short curr_data = 0, curr_prot = 0;
  1841. unsigned int split_offset;
  1842. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  1843. unsigned int protgrp_blks, protgrp_bytes;
  1844. unsigned int remainder, subtotal;
  1845. int status;
  1846. int datadir = sc->sc_data_direction;
  1847. unsigned char pgdone = 0, alldone = 0;
  1848. unsigned blksize;
  1849. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1850. uint32_t rc;
  1851. #endif
  1852. uint32_t checking = 1;
  1853. uint32_t reftag;
  1854. uint8_t txop, rxop;
  1855. int num_bde = 0;
  1856. sgpe = scsi_prot_sglist(sc);
  1857. sgde = scsi_sglist(sc);
  1858. if (!sgpe || !sgde) {
  1859. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1860. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  1861. sgpe, sgde);
  1862. return 0;
  1863. }
  1864. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1865. if (status)
  1866. goto out;
  1867. /* extract some info from the scsi command */
  1868. blksize = lpfc_cmd_blksize(sc);
  1869. reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
  1870. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1871. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  1872. if (rc) {
  1873. if (rc & BG_ERR_SWAP)
  1874. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  1875. if (rc & BG_ERR_CHECK)
  1876. checking = 0;
  1877. }
  1878. #endif
  1879. split_offset = 0;
  1880. do {
  1881. /* Check to see if we ran out of space */
  1882. if (num_bde >= (phba->cfg_total_seg_cnt - 2))
  1883. return num_bde + 3;
  1884. /* setup PDE5 with what we have */
  1885. pde5 = (struct lpfc_pde5 *) bpl;
  1886. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1887. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1888. /* Endianness conversion if necessary for PDE5 */
  1889. pde5->word0 = cpu_to_le32(pde5->word0);
  1890. pde5->reftag = cpu_to_le32(reftag);
  1891. /* advance bpl and increment bde count */
  1892. num_bde++;
  1893. bpl++;
  1894. pde6 = (struct lpfc_pde6 *) bpl;
  1895. /* setup PDE6 with the rest of the info */
  1896. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1897. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1898. bf_set(pde6_optx, pde6, txop);
  1899. bf_set(pde6_oprx, pde6, rxop);
  1900. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
  1901. bf_set(pde6_ce, pde6, checking);
  1902. else
  1903. bf_set(pde6_ce, pde6, 0);
  1904. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
  1905. bf_set(pde6_re, pde6, checking);
  1906. else
  1907. bf_set(pde6_re, pde6, 0);
  1908. bf_set(pde6_ai, pde6, 1);
  1909. bf_set(pde6_ae, pde6, 0);
  1910. bf_set(pde6_apptagval, pde6, 0);
  1911. /* Endianness conversion if necessary for PDE6 */
  1912. pde6->word0 = cpu_to_le32(pde6->word0);
  1913. pde6->word1 = cpu_to_le32(pde6->word1);
  1914. pde6->word2 = cpu_to_le32(pde6->word2);
  1915. /* advance bpl and increment bde count */
  1916. num_bde++;
  1917. bpl++;
  1918. /* setup the first BDE that points to protection buffer */
  1919. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  1920. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  1921. /* must be integer multiple of the DIF block length */
  1922. BUG_ON(protgroup_len % 8);
  1923. pde7 = (struct lpfc_pde7 *) bpl;
  1924. memset(pde7, 0, sizeof(struct lpfc_pde7));
  1925. bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
  1926. pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1927. pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1928. protgrp_blks = protgroup_len / 8;
  1929. protgrp_bytes = protgrp_blks * blksize;
  1930. /* check if this pde is crossing the 4K boundary; if so split */
  1931. if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
  1932. protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
  1933. protgroup_offset += protgroup_remainder;
  1934. protgrp_blks = protgroup_remainder / 8;
  1935. protgrp_bytes = protgrp_blks * blksize;
  1936. } else {
  1937. protgroup_offset = 0;
  1938. curr_prot++;
  1939. }
  1940. num_bde++;
  1941. /* setup BDE's for data blocks associated with DIF data */
  1942. pgdone = 0;
  1943. subtotal = 0; /* total bytes processed for current prot grp */
  1944. while (!pgdone) {
  1945. /* Check to see if we ran out of space */
  1946. if (num_bde >= phba->cfg_total_seg_cnt)
  1947. return num_bde + 1;
  1948. if (!sgde) {
  1949. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1950. "9065 BLKGRD:%s Invalid data segment\n",
  1951. __func__);
  1952. return 0;
  1953. }
  1954. bpl++;
  1955. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1956. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1957. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1958. remainder = sg_dma_len(sgde) - split_offset;
  1959. if ((subtotal + remainder) <= protgrp_bytes) {
  1960. /* we can use this whole buffer */
  1961. bpl->tus.f.bdeSize = remainder;
  1962. split_offset = 0;
  1963. if ((subtotal + remainder) == protgrp_bytes)
  1964. pgdone = 1;
  1965. } else {
  1966. /* must split this buffer with next prot grp */
  1967. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1968. split_offset += bpl->tus.f.bdeSize;
  1969. }
  1970. subtotal += bpl->tus.f.bdeSize;
  1971. if (datadir == DMA_TO_DEVICE)
  1972. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1973. else
  1974. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1975. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1976. num_bde++;
  1977. curr_data++;
  1978. if (split_offset)
  1979. break;
  1980. /* Move to the next s/g segment if possible */
  1981. sgde = sg_next(sgde);
  1982. }
  1983. if (protgroup_offset) {
  1984. /* update the reference tag */
  1985. reftag += protgrp_blks;
  1986. bpl++;
  1987. continue;
  1988. }
  1989. /* are we done ? */
  1990. if (curr_prot == protcnt) {
  1991. alldone = 1;
  1992. } else if (curr_prot < protcnt) {
  1993. /* advance to next prot buffer */
  1994. sgpe = sg_next(sgpe);
  1995. bpl++;
  1996. /* update the reference tag */
  1997. reftag += protgrp_blks;
  1998. } else {
  1999. /* if we're here, we have a bug */
  2000. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2001. "9054 BLKGRD: bug in %s\n", __func__);
  2002. }
  2003. } while (!alldone);
  2004. out:
  2005. return num_bde;
  2006. }
  2007. /**
  2008. * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
  2009. * @phba: The Hba for which this call is being executed.
  2010. * @sc: pointer to scsi command we're working on
  2011. * @sgl: pointer to buffer list for protection groups
  2012. * @datacnt: number of segments of data that have been dma mapped
  2013. *
  2014. * This function sets up SGL buffer list for protection groups of
  2015. * type LPFC_PG_TYPE_NO_DIF
  2016. *
  2017. * This is usually used when the HBA is instructed to generate
  2018. * DIFs and insert them into data stream (or strip DIF from
  2019. * incoming data stream)
  2020. *
  2021. * The buffer list consists of just one protection group described
  2022. * below:
  2023. * +-------------------------+
  2024. * start of prot group --> | DI_SEED |
  2025. * +-------------------------+
  2026. * | Data SGE |
  2027. * +-------------------------+
  2028. * |more Data SGE's ... (opt)|
  2029. * +-------------------------+
  2030. *
  2031. *
  2032. * Note: Data s/g buffers have been dma mapped
  2033. *
  2034. * Returns the number of SGEs added to the SGL.
  2035. **/
  2036. static int
  2037. lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  2038. struct sli4_sge *sgl, int datasegcnt)
  2039. {
  2040. struct scatterlist *sgde = NULL; /* s/g data entry */
  2041. struct sli4_sge_diseed *diseed = NULL;
  2042. dma_addr_t physaddr;
  2043. int i = 0, num_sge = 0, status;
  2044. uint32_t reftag;
  2045. uint8_t txop, rxop;
  2046. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  2047. uint32_t rc;
  2048. #endif
  2049. uint32_t checking = 1;
  2050. uint32_t dma_len;
  2051. uint32_t dma_offset = 0;
  2052. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  2053. if (status)
  2054. goto out;
  2055. /* extract some info from the scsi command for pde*/
  2056. reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
  2057. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  2058. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  2059. if (rc) {
  2060. if (rc & BG_ERR_SWAP)
  2061. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  2062. if (rc & BG_ERR_CHECK)
  2063. checking = 0;
  2064. }
  2065. #endif
  2066. /* setup DISEED with what we have */
  2067. diseed = (struct sli4_sge_diseed *) sgl;
  2068. memset(diseed, 0, sizeof(struct sli4_sge_diseed));
  2069. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
  2070. /* Endianness conversion if necessary */
  2071. diseed->ref_tag = cpu_to_le32(reftag);
  2072. diseed->ref_tag_tran = diseed->ref_tag;
  2073. /*
  2074. * We only need to check the data on READs, for WRITEs
  2075. * protection data is automatically generated, not checked.
  2076. */
  2077. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  2078. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
  2079. bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
  2080. else
  2081. bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
  2082. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
  2083. bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
  2084. else
  2085. bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
  2086. }
  2087. /* setup DISEED with the rest of the info */
  2088. bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
  2089. bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
  2090. bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
  2091. bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
  2092. /* Endianness conversion if necessary for DISEED */
  2093. diseed->word2 = cpu_to_le32(diseed->word2);
  2094. diseed->word3 = cpu_to_le32(diseed->word3);
  2095. /* advance bpl and increment sge count */
  2096. num_sge++;
  2097. sgl++;
  2098. /* assumption: caller has already run dma_map_sg on command data */
  2099. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  2100. physaddr = sg_dma_address(sgde);
  2101. dma_len = sg_dma_len(sgde);
  2102. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  2103. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  2104. if ((i + 1) == datasegcnt)
  2105. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2106. else
  2107. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2108. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2109. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  2110. sgl->sge_len = cpu_to_le32(dma_len);
  2111. dma_offset += dma_len;
  2112. sgl++;
  2113. num_sge++;
  2114. }
  2115. out:
  2116. return num_sge;
  2117. }
  2118. /**
  2119. * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
  2120. * @phba: The Hba for which this call is being executed.
  2121. * @sc: pointer to scsi command we're working on
  2122. * @sgl: pointer to buffer list for protection groups
  2123. * @datacnt: number of segments of data that have been dma mapped
  2124. * @protcnt: number of segment of protection data that have been dma mapped
  2125. *
  2126. * This function sets up SGL buffer list for protection groups of
  2127. * type LPFC_PG_TYPE_DIF
  2128. *
  2129. * This is usually used when DIFs are in their own buffers,
  2130. * separate from the data. The HBA can then by instructed
  2131. * to place the DIFs in the outgoing stream. For read operations,
  2132. * The HBA could extract the DIFs and place it in DIF buffers.
  2133. *
  2134. * The buffer list for this type consists of one or more of the
  2135. * protection groups described below:
  2136. * +-------------------------+
  2137. * start of first prot group --> | DISEED |
  2138. * +-------------------------+
  2139. * | DIF (Prot SGE) |
  2140. * +-------------------------+
  2141. * | Data SGE |
  2142. * +-------------------------+
  2143. * |more Data SGE's ... (opt)|
  2144. * +-------------------------+
  2145. * start of new prot group --> | DISEED |
  2146. * +-------------------------+
  2147. * | ... |
  2148. * +-------------------------+
  2149. *
  2150. * Note: It is assumed that both data and protection s/g buffers have been
  2151. * mapped for DMA
  2152. *
  2153. * Returns the number of SGEs added to the SGL.
  2154. **/
  2155. static int
  2156. lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  2157. struct sli4_sge *sgl, int datacnt, int protcnt)
  2158. {
  2159. struct scatterlist *sgde = NULL; /* s/g data entry */
  2160. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  2161. struct sli4_sge_diseed *diseed = NULL;
  2162. dma_addr_t dataphysaddr, protphysaddr;
  2163. unsigned short curr_data = 0, curr_prot = 0;
  2164. unsigned int split_offset;
  2165. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  2166. unsigned int protgrp_blks, protgrp_bytes;
  2167. unsigned int remainder, subtotal;
  2168. int status;
  2169. unsigned char pgdone = 0, alldone = 0;
  2170. unsigned blksize;
  2171. uint32_t reftag;
  2172. uint8_t txop, rxop;
  2173. uint32_t dma_len;
  2174. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  2175. uint32_t rc;
  2176. #endif
  2177. uint32_t checking = 1;
  2178. uint32_t dma_offset = 0;
  2179. int num_sge = 0;
  2180. sgpe = scsi_prot_sglist(sc);
  2181. sgde = scsi_sglist(sc);
  2182. if (!sgpe || !sgde) {
  2183. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  2184. "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
  2185. sgpe, sgde);
  2186. return 0;
  2187. }
  2188. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  2189. if (status)
  2190. goto out;
  2191. /* extract some info from the scsi command */
  2192. blksize = lpfc_cmd_blksize(sc);
  2193. reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
  2194. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  2195. rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
  2196. if (rc) {
  2197. if (rc & BG_ERR_SWAP)
  2198. lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
  2199. if (rc & BG_ERR_CHECK)
  2200. checking = 0;
  2201. }
  2202. #endif
  2203. split_offset = 0;
  2204. do {
  2205. /* Check to see if we ran out of space */
  2206. if (num_sge >= (phba->cfg_total_seg_cnt - 2))
  2207. return num_sge + 3;
  2208. /* setup DISEED with what we have */
  2209. diseed = (struct sli4_sge_diseed *) sgl;
  2210. memset(diseed, 0, sizeof(struct sli4_sge_diseed));
  2211. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
  2212. /* Endianness conversion if necessary */
  2213. diseed->ref_tag = cpu_to_le32(reftag);
  2214. diseed->ref_tag_tran = diseed->ref_tag;
  2215. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
  2216. bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
  2217. } else {
  2218. bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
  2219. /*
  2220. * When in this mode, the hardware will replace
  2221. * the guard tag from the host with a
  2222. * newly generated good CRC for the wire.
  2223. * Switch to raw mode here to avoid this
  2224. * behavior. What the host sends gets put on the wire.
  2225. */
  2226. if (txop == BG_OP_IN_CRC_OUT_CRC) {
  2227. txop = BG_OP_RAW_MODE;
  2228. rxop = BG_OP_RAW_MODE;
  2229. }
  2230. }
  2231. if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
  2232. bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
  2233. else
  2234. bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
  2235. /* setup DISEED with the rest of the info */
  2236. bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
  2237. bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
  2238. bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
  2239. bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
  2240. /* Endianness conversion if necessary for DISEED */
  2241. diseed->word2 = cpu_to_le32(diseed->word2);
  2242. diseed->word3 = cpu_to_le32(diseed->word3);
  2243. /* advance sgl and increment bde count */
  2244. num_sge++;
  2245. sgl++;
  2246. /* setup the first BDE that points to protection buffer */
  2247. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  2248. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  2249. /* must be integer multiple of the DIF block length */
  2250. BUG_ON(protgroup_len % 8);
  2251. /* Now setup DIF SGE */
  2252. sgl->word2 = 0;
  2253. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
  2254. sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
  2255. sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
  2256. sgl->word2 = cpu_to_le32(sgl->word2);
  2257. protgrp_blks = protgroup_len / 8;
  2258. protgrp_bytes = protgrp_blks * blksize;
  2259. /* check if DIF SGE is crossing the 4K boundary; if so split */
  2260. if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
  2261. protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
  2262. protgroup_offset += protgroup_remainder;
  2263. protgrp_blks = protgroup_remainder / 8;
  2264. protgrp_bytes = protgrp_blks * blksize;
  2265. } else {
  2266. protgroup_offset = 0;
  2267. curr_prot++;
  2268. }
  2269. num_sge++;
  2270. /* setup SGE's for data blocks associated with DIF data */
  2271. pgdone = 0;
  2272. subtotal = 0; /* total bytes processed for current prot grp */
  2273. while (!pgdone) {
  2274. /* Check to see if we ran out of space */
  2275. if (num_sge >= phba->cfg_total_seg_cnt)
  2276. return num_sge + 1;
  2277. if (!sgde) {
  2278. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2279. "9086 BLKGRD:%s Invalid data segment\n",
  2280. __func__);
  2281. return 0;
  2282. }
  2283. sgl++;
  2284. dataphysaddr = sg_dma_address(sgde) + split_offset;
  2285. remainder = sg_dma_len(sgde) - split_offset;
  2286. if ((subtotal + remainder) <= protgrp_bytes) {
  2287. /* we can use this whole buffer */
  2288. dma_len = remainder;
  2289. split_offset = 0;
  2290. if ((subtotal + remainder) == protgrp_bytes)
  2291. pgdone = 1;
  2292. } else {
  2293. /* must split this buffer with next prot grp */
  2294. dma_len = protgrp_bytes - subtotal;
  2295. split_offset += dma_len;
  2296. }
  2297. subtotal += dma_len;
  2298. sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
  2299. sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
  2300. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2301. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2302. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  2303. sgl->sge_len = cpu_to_le32(dma_len);
  2304. dma_offset += dma_len;
  2305. num_sge++;
  2306. curr_data++;
  2307. if (split_offset)
  2308. break;
  2309. /* Move to the next s/g segment if possible */
  2310. sgde = sg_next(sgde);
  2311. }
  2312. if (protgroup_offset) {
  2313. /* update the reference tag */
  2314. reftag += protgrp_blks;
  2315. sgl++;
  2316. continue;
  2317. }
  2318. /* are we done ? */
  2319. if (curr_prot == protcnt) {
  2320. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2321. alldone = 1;
  2322. } else if (curr_prot < protcnt) {
  2323. /* advance to next prot buffer */
  2324. sgpe = sg_next(sgpe);
  2325. sgl++;
  2326. /* update the reference tag */
  2327. reftag += protgrp_blks;
  2328. } else {
  2329. /* if we're here, we have a bug */
  2330. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2331. "9085 BLKGRD: bug in %s\n", __func__);
  2332. }
  2333. } while (!alldone);
  2334. out:
  2335. return num_sge;
  2336. }
  2337. /**
  2338. * lpfc_prot_group_type - Get prtotection group type of SCSI command
  2339. * @phba: The Hba for which this call is being executed.
  2340. * @sc: pointer to scsi command we're working on
  2341. *
  2342. * Given a SCSI command that supports DIF, determine composition of protection
  2343. * groups involved in setting up buffer lists
  2344. *
  2345. * Returns: Protection group type (with or without DIF)
  2346. *
  2347. **/
  2348. static int
  2349. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  2350. {
  2351. int ret = LPFC_PG_TYPE_INVALID;
  2352. unsigned char op = scsi_get_prot_op(sc);
  2353. switch (op) {
  2354. case SCSI_PROT_READ_STRIP:
  2355. case SCSI_PROT_WRITE_INSERT:
  2356. ret = LPFC_PG_TYPE_NO_DIF;
  2357. break;
  2358. case SCSI_PROT_READ_INSERT:
  2359. case SCSI_PROT_WRITE_STRIP:
  2360. case SCSI_PROT_READ_PASS:
  2361. case SCSI_PROT_WRITE_PASS:
  2362. ret = LPFC_PG_TYPE_DIF_BUF;
  2363. break;
  2364. default:
  2365. if (phba)
  2366. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  2367. "9021 Unsupported protection op:%d\n",
  2368. op);
  2369. break;
  2370. }
  2371. return ret;
  2372. }
  2373. /**
  2374. * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
  2375. * @phba: The Hba for which this call is being executed.
  2376. * @lpfc_cmd: The scsi buffer which is going to be adjusted.
  2377. *
  2378. * Adjust the data length to account for how much data
  2379. * is actually on the wire.
  2380. *
  2381. * returns the adjusted data length
  2382. **/
  2383. static int
  2384. lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
  2385. struct lpfc_scsi_buf *lpfc_cmd)
  2386. {
  2387. struct scsi_cmnd *sc = lpfc_cmd->pCmd;
  2388. int fcpdl;
  2389. fcpdl = scsi_bufflen(sc);
  2390. /* Check if there is protection data on the wire */
  2391. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  2392. /* Read check for protection data */
  2393. if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
  2394. return fcpdl;
  2395. } else {
  2396. /* Write check for protection data */
  2397. if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
  2398. return fcpdl;
  2399. }
  2400. /*
  2401. * If we are in DIF Type 1 mode every data block has a 8 byte
  2402. * DIF (trailer) attached to it. Must ajust FCP data length
  2403. * to account for the protection data.
  2404. */
  2405. fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
  2406. return fcpdl;
  2407. }
  2408. /**
  2409. * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  2410. * @phba: The Hba for which this call is being executed.
  2411. * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
  2412. *
  2413. * This is the protection/DIF aware version of
  2414. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  2415. * two functions eventually, but for now, it's here
  2416. **/
  2417. static int
  2418. lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
  2419. struct lpfc_scsi_buf *lpfc_cmd)
  2420. {
  2421. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2422. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2423. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  2424. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2425. uint32_t num_bde = 0;
  2426. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  2427. int prot_group_type = 0;
  2428. int fcpdl;
  2429. /*
  2430. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  2431. * fcp_rsp regions to the first data bde entry
  2432. */
  2433. bpl += 2;
  2434. if (scsi_sg_count(scsi_cmnd)) {
  2435. /*
  2436. * The driver stores the segment count returned from pci_map_sg
  2437. * because this a count of dma-mappings used to map the use_sg
  2438. * pages. They are not guaranteed to be the same for those
  2439. * architectures that implement an IOMMU.
  2440. */
  2441. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  2442. scsi_sglist(scsi_cmnd),
  2443. scsi_sg_count(scsi_cmnd), datadir);
  2444. if (unlikely(!datasegcnt))
  2445. return 1;
  2446. lpfc_cmd->seg_cnt = datasegcnt;
  2447. /* First check if data segment count from SCSI Layer is good */
  2448. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
  2449. goto err;
  2450. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  2451. switch (prot_group_type) {
  2452. case LPFC_PG_TYPE_NO_DIF:
  2453. /* Here we need to add a PDE5 and PDE6 to the count */
  2454. if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
  2455. goto err;
  2456. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  2457. datasegcnt);
  2458. /* we should have 2 or more entries in buffer list */
  2459. if (num_bde < 2)
  2460. goto err;
  2461. break;
  2462. case LPFC_PG_TYPE_DIF_BUF:
  2463. /*
  2464. * This type indicates that protection buffers are
  2465. * passed to the driver, so that needs to be prepared
  2466. * for DMA
  2467. */
  2468. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  2469. scsi_prot_sglist(scsi_cmnd),
  2470. scsi_prot_sg_count(scsi_cmnd), datadir);
  2471. if (unlikely(!protsegcnt)) {
  2472. scsi_dma_unmap(scsi_cmnd);
  2473. return 1;
  2474. }
  2475. lpfc_cmd->prot_seg_cnt = protsegcnt;
  2476. /*
  2477. * There is a minimun of 4 BPLs used for every
  2478. * protection data segment.
  2479. */
  2480. if ((lpfc_cmd->prot_seg_cnt * 4) >
  2481. (phba->cfg_total_seg_cnt - 2))
  2482. goto err;
  2483. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  2484. datasegcnt, protsegcnt);
  2485. /* we should have 3 or more entries in buffer list */
  2486. if ((num_bde < 3) ||
  2487. (num_bde > phba->cfg_total_seg_cnt))
  2488. goto err;
  2489. break;
  2490. case LPFC_PG_TYPE_INVALID:
  2491. default:
  2492. scsi_dma_unmap(scsi_cmnd);
  2493. lpfc_cmd->seg_cnt = 0;
  2494. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  2495. "9022 Unexpected protection group %i\n",
  2496. prot_group_type);
  2497. return 1;
  2498. }
  2499. }
  2500. /*
  2501. * Finish initializing those IOCB fields that are dependent on the
  2502. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  2503. * reinitialized since all iocb memory resources are used many times
  2504. * for transmit, receive, and continuation bpl's.
  2505. */
  2506. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  2507. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  2508. iocb_cmd->ulpBdeCount = 1;
  2509. iocb_cmd->ulpLe = 1;
  2510. fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
  2511. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  2512. /*
  2513. * Due to difference in data length between DIF/non-DIF paths,
  2514. * we need to set word 4 of IOCB here
  2515. */
  2516. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  2517. return 0;
  2518. err:
  2519. if (lpfc_cmd->seg_cnt)
  2520. scsi_dma_unmap(scsi_cmnd);
  2521. if (lpfc_cmd->prot_seg_cnt)
  2522. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
  2523. scsi_prot_sg_count(scsi_cmnd),
  2524. scsi_cmnd->sc_data_direction);
  2525. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  2526. "9023 Cannot setup S/G List for HBA"
  2527. "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
  2528. lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
  2529. phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
  2530. prot_group_type, num_bde);
  2531. lpfc_cmd->seg_cnt = 0;
  2532. lpfc_cmd->prot_seg_cnt = 0;
  2533. return 1;
  2534. }
  2535. /*
  2536. * This function calcuates the T10 DIF guard tag
  2537. * on the specified data using a CRC algorithmn
  2538. * using crc_t10dif.
  2539. */
  2540. static uint16_t
  2541. lpfc_bg_crc(uint8_t *data, int count)
  2542. {
  2543. uint16_t crc = 0;
  2544. uint16_t x;
  2545. crc = crc_t10dif(data, count);
  2546. x = cpu_to_be16(crc);
  2547. return x;
  2548. }
  2549. /*
  2550. * This function calcuates the T10 DIF guard tag
  2551. * on the specified data using a CSUM algorithmn
  2552. * using ip_compute_csum.
  2553. */
  2554. static uint16_t
  2555. lpfc_bg_csum(uint8_t *data, int count)
  2556. {
  2557. uint16_t ret;
  2558. ret = ip_compute_csum(data, count);
  2559. return ret;
  2560. }
  2561. /*
  2562. * This function examines the protection data to try to determine
  2563. * what type of T10-DIF error occurred.
  2564. */
  2565. static void
  2566. lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  2567. {
  2568. struct scatterlist *sgpe; /* s/g prot entry */
  2569. struct scatterlist *sgde; /* s/g data entry */
  2570. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  2571. struct scsi_dif_tuple *src = NULL;
  2572. uint8_t *data_src = NULL;
  2573. uint16_t guard_tag;
  2574. uint16_t start_app_tag, app_tag;
  2575. uint32_t start_ref_tag, ref_tag;
  2576. int prot, protsegcnt;
  2577. int err_type, len, data_len;
  2578. int chk_ref, chk_app, chk_guard;
  2579. uint16_t sum;
  2580. unsigned blksize;
  2581. err_type = BGS_GUARD_ERR_MASK;
  2582. sum = 0;
  2583. guard_tag = 0;
  2584. /* First check to see if there is protection data to examine */
  2585. prot = scsi_get_prot_op(cmd);
  2586. if ((prot == SCSI_PROT_READ_STRIP) ||
  2587. (prot == SCSI_PROT_WRITE_INSERT) ||
  2588. (prot == SCSI_PROT_NORMAL))
  2589. goto out;
  2590. /* Currently the driver just supports ref_tag and guard_tag checking */
  2591. chk_ref = 1;
  2592. chk_app = 0;
  2593. chk_guard = 0;
  2594. /* Setup a ptr to the protection data provided by the SCSI host */
  2595. sgpe = scsi_prot_sglist(cmd);
  2596. protsegcnt = lpfc_cmd->prot_seg_cnt;
  2597. if (sgpe && protsegcnt) {
  2598. /*
  2599. * We will only try to verify guard tag if the segment
  2600. * data length is a multiple of the blksize.
  2601. */
  2602. sgde = scsi_sglist(cmd);
  2603. blksize = lpfc_cmd_blksize(cmd);
  2604. data_src = (uint8_t *)sg_virt(sgde);
  2605. data_len = sgde->length;
  2606. if ((data_len & (blksize - 1)) == 0)
  2607. chk_guard = 1;
  2608. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  2609. start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
  2610. start_app_tag = src->app_tag;
  2611. len = sgpe->length;
  2612. while (src && protsegcnt) {
  2613. while (len) {
  2614. /*
  2615. * First check to see if a protection data
  2616. * check is valid
  2617. */
  2618. if ((src->ref_tag == 0xffffffff) ||
  2619. (src->app_tag == 0xffff)) {
  2620. start_ref_tag++;
  2621. goto skipit;
  2622. }
  2623. /* First Guard Tag checking */
  2624. if (chk_guard) {
  2625. guard_tag = src->guard_tag;
  2626. if (lpfc_cmd_guard_csum(cmd))
  2627. sum = lpfc_bg_csum(data_src,
  2628. blksize);
  2629. else
  2630. sum = lpfc_bg_crc(data_src,
  2631. blksize);
  2632. if ((guard_tag != sum)) {
  2633. err_type = BGS_GUARD_ERR_MASK;
  2634. goto out;
  2635. }
  2636. }
  2637. /* Reference Tag checking */
  2638. ref_tag = be32_to_cpu(src->ref_tag);
  2639. if (chk_ref && (ref_tag != start_ref_tag)) {
  2640. err_type = BGS_REFTAG_ERR_MASK;
  2641. goto out;
  2642. }
  2643. start_ref_tag++;
  2644. /* App Tag checking */
  2645. app_tag = src->app_tag;
  2646. if (chk_app && (app_tag != start_app_tag)) {
  2647. err_type = BGS_APPTAG_ERR_MASK;
  2648. goto out;
  2649. }
  2650. skipit:
  2651. len -= sizeof(struct scsi_dif_tuple);
  2652. if (len < 0)
  2653. len = 0;
  2654. src++;
  2655. data_src += blksize;
  2656. data_len -= blksize;
  2657. /*
  2658. * Are we at the end of the Data segment?
  2659. * The data segment is only used for Guard
  2660. * tag checking.
  2661. */
  2662. if (chk_guard && (data_len == 0)) {
  2663. chk_guard = 0;
  2664. sgde = sg_next(sgde);
  2665. if (!sgde)
  2666. goto out;
  2667. data_src = (uint8_t *)sg_virt(sgde);
  2668. data_len = sgde->length;
  2669. if ((data_len & (blksize - 1)) == 0)
  2670. chk_guard = 1;
  2671. }
  2672. }
  2673. /* Goto the next Protection data segment */
  2674. sgpe = sg_next(sgpe);
  2675. if (sgpe) {
  2676. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  2677. len = sgpe->length;
  2678. } else {
  2679. src = NULL;
  2680. }
  2681. protsegcnt--;
  2682. }
  2683. }
  2684. out:
  2685. if (err_type == BGS_GUARD_ERR_MASK) {
  2686. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2687. 0x10, 0x1);
  2688. cmd->result = DRIVER_SENSE << 24
  2689. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2690. phba->bg_guard_err_cnt++;
  2691. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2692. "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
  2693. (unsigned long)scsi_get_lba(cmd),
  2694. sum, guard_tag);
  2695. } else if (err_type == BGS_REFTAG_ERR_MASK) {
  2696. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2697. 0x10, 0x3);
  2698. cmd->result = DRIVER_SENSE << 24
  2699. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2700. phba->bg_reftag_err_cnt++;
  2701. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2702. "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
  2703. (unsigned long)scsi_get_lba(cmd),
  2704. ref_tag, start_ref_tag);
  2705. } else if (err_type == BGS_APPTAG_ERR_MASK) {
  2706. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2707. 0x10, 0x2);
  2708. cmd->result = DRIVER_SENSE << 24
  2709. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2710. phba->bg_apptag_err_cnt++;
  2711. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2712. "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
  2713. (unsigned long)scsi_get_lba(cmd),
  2714. app_tag, start_app_tag);
  2715. }
  2716. }
  2717. /*
  2718. * This function checks for BlockGuard errors detected by
  2719. * the HBA. In case of errors, the ASC/ASCQ fields in the
  2720. * sense buffer will be set accordingly, paired with
  2721. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  2722. * detected corruption.
  2723. *
  2724. * Returns:
  2725. * 0 - No error found
  2726. * 1 - BlockGuard error found
  2727. * -1 - Internal error (bad profile, ...etc)
  2728. */
  2729. static int
  2730. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  2731. struct lpfc_iocbq *pIocbOut)
  2732. {
  2733. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  2734. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  2735. int ret = 0;
  2736. uint32_t bghm = bgf->bghm;
  2737. uint32_t bgstat = bgf->bgstat;
  2738. uint64_t failing_sector = 0;
  2739. spin_lock(&_dump_buf_lock);
  2740. if (!_dump_buf_done) {
  2741. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
  2742. " Data for %u blocks to debugfs\n",
  2743. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  2744. lpfc_debug_save_data(phba, cmd);
  2745. /* If we have a prot sgl, save the DIF buffer */
  2746. if (lpfc_prot_group_type(phba, cmd) ==
  2747. LPFC_PG_TYPE_DIF_BUF) {
  2748. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
  2749. "Saving DIF for %u blocks to debugfs\n",
  2750. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  2751. lpfc_debug_save_dif(phba, cmd);
  2752. }
  2753. _dump_buf_done = 1;
  2754. }
  2755. spin_unlock(&_dump_buf_lock);
  2756. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  2757. cmd->result = ScsiResult(DID_ERROR, 0);
  2758. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2759. "9072 BLKGRD: Invalid BG Profile in cmd"
  2760. " 0x%x lba 0x%llx blk cnt 0x%x "
  2761. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2762. (unsigned long long)scsi_get_lba(cmd),
  2763. blk_rq_sectors(cmd->request), bgstat, bghm);
  2764. ret = (-1);
  2765. goto out;
  2766. }
  2767. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  2768. cmd->result = ScsiResult(DID_ERROR, 0);
  2769. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2770. "9073 BLKGRD: Invalid BG PDIF Block in cmd"
  2771. " 0x%x lba 0x%llx blk cnt 0x%x "
  2772. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2773. (unsigned long long)scsi_get_lba(cmd),
  2774. blk_rq_sectors(cmd->request), bgstat, bghm);
  2775. ret = (-1);
  2776. goto out;
  2777. }
  2778. if (lpfc_bgs_get_guard_err(bgstat)) {
  2779. ret = 1;
  2780. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2781. 0x10, 0x1);
  2782. cmd->result = DRIVER_SENSE << 24
  2783. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2784. phba->bg_guard_err_cnt++;
  2785. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2786. "9055 BLKGRD: Guard Tag error in cmd"
  2787. " 0x%x lba 0x%llx blk cnt 0x%x "
  2788. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2789. (unsigned long long)scsi_get_lba(cmd),
  2790. blk_rq_sectors(cmd->request), bgstat, bghm);
  2791. }
  2792. if (lpfc_bgs_get_reftag_err(bgstat)) {
  2793. ret = 1;
  2794. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2795. 0x10, 0x3);
  2796. cmd->result = DRIVER_SENSE << 24
  2797. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2798. phba->bg_reftag_err_cnt++;
  2799. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2800. "9056 BLKGRD: Ref Tag error in cmd"
  2801. " 0x%x lba 0x%llx blk cnt 0x%x "
  2802. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2803. (unsigned long long)scsi_get_lba(cmd),
  2804. blk_rq_sectors(cmd->request), bgstat, bghm);
  2805. }
  2806. if (lpfc_bgs_get_apptag_err(bgstat)) {
  2807. ret = 1;
  2808. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  2809. 0x10, 0x2);
  2810. cmd->result = DRIVER_SENSE << 24
  2811. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  2812. phba->bg_apptag_err_cnt++;
  2813. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2814. "9061 BLKGRD: App Tag error in cmd"
  2815. " 0x%x lba 0x%llx blk cnt 0x%x "
  2816. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2817. (unsigned long long)scsi_get_lba(cmd),
  2818. blk_rq_sectors(cmd->request), bgstat, bghm);
  2819. }
  2820. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  2821. /*
  2822. * setup sense data descriptor 0 per SPC-4 as an information
  2823. * field, and put the failing LBA in it.
  2824. * This code assumes there was also a guard/app/ref tag error
  2825. * indication.
  2826. */
  2827. cmd->sense_buffer[7] = 0xc; /* Additional sense length */
  2828. cmd->sense_buffer[8] = 0; /* Information descriptor type */
  2829. cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
  2830. cmd->sense_buffer[10] = 0x80; /* Validity bit */
  2831. /* bghm is a "on the wire" FC frame based count */
  2832. switch (scsi_get_prot_op(cmd)) {
  2833. case SCSI_PROT_READ_INSERT:
  2834. case SCSI_PROT_WRITE_STRIP:
  2835. bghm /= cmd->device->sector_size;
  2836. break;
  2837. case SCSI_PROT_READ_STRIP:
  2838. case SCSI_PROT_WRITE_INSERT:
  2839. case SCSI_PROT_READ_PASS:
  2840. case SCSI_PROT_WRITE_PASS:
  2841. bghm /= (cmd->device->sector_size +
  2842. sizeof(struct scsi_dif_tuple));
  2843. break;
  2844. }
  2845. failing_sector = scsi_get_lba(cmd);
  2846. failing_sector += bghm;
  2847. /* Descriptor Information */
  2848. put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
  2849. }
  2850. if (!ret) {
  2851. /* No error was reported - problem in FW? */
  2852. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
  2853. "9057 BLKGRD: Unknown error in cmd"
  2854. " 0x%x lba 0x%llx blk cnt 0x%x "
  2855. "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
  2856. (unsigned long long)scsi_get_lba(cmd),
  2857. blk_rq_sectors(cmd->request), bgstat, bghm);
  2858. /* Calcuate what type of error it was */
  2859. lpfc_calc_bg_err(phba, lpfc_cmd);
  2860. }
  2861. out:
  2862. return ret;
  2863. }
  2864. /**
  2865. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  2866. * @phba: The Hba for which this call is being executed.
  2867. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2868. *
  2869. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  2870. * field of @lpfc_cmd for device with SLI-4 interface spec.
  2871. *
  2872. * Return codes:
  2873. * 1 - Error
  2874. * 0 - Success
  2875. **/
  2876. static int
  2877. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  2878. {
  2879. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2880. struct scatterlist *sgel = NULL;
  2881. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2882. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  2883. struct sli4_sge *first_data_sgl;
  2884. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2885. dma_addr_t physaddr;
  2886. uint32_t num_bde = 0;
  2887. uint32_t dma_len;
  2888. uint32_t dma_offset = 0;
  2889. int nseg;
  2890. struct ulp_bde64 *bde;
  2891. /*
  2892. * There are three possibilities here - use scatter-gather segment, use
  2893. * the single mapping, or neither. Start the lpfc command prep by
  2894. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2895. * data bde entry.
  2896. */
  2897. if (scsi_sg_count(scsi_cmnd)) {
  2898. /*
  2899. * The driver stores the segment count returned from pci_map_sg
  2900. * because this a count of dma-mappings used to map the use_sg
  2901. * pages. They are not guaranteed to be the same for those
  2902. * architectures that implement an IOMMU.
  2903. */
  2904. nseg = scsi_dma_map(scsi_cmnd);
  2905. if (unlikely(nseg <= 0))
  2906. return 1;
  2907. sgl += 1;
  2908. /* clear the last flag in the fcp_rsp map entry */
  2909. sgl->word2 = le32_to_cpu(sgl->word2);
  2910. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2911. sgl->word2 = cpu_to_le32(sgl->word2);
  2912. sgl += 1;
  2913. first_data_sgl = sgl;
  2914. lpfc_cmd->seg_cnt = nseg;
  2915. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  2916. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
  2917. " %s: Too many sg segments from "
  2918. "dma_map_sg. Config %d, seg_cnt %d\n",
  2919. __func__, phba->cfg_sg_seg_cnt,
  2920. lpfc_cmd->seg_cnt);
  2921. lpfc_cmd->seg_cnt = 0;
  2922. scsi_dma_unmap(scsi_cmnd);
  2923. return 1;
  2924. }
  2925. /*
  2926. * The driver established a maximum scatter-gather segment count
  2927. * during probe that limits the number of sg elements in any
  2928. * single scsi command. Just run through the seg_cnt and format
  2929. * the sge's.
  2930. * When using SLI-3 the driver will try to fit all the BDEs into
  2931. * the IOCB. If it can't then the BDEs get added to a BPL as it
  2932. * does for SLI-2 mode.
  2933. */
  2934. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  2935. physaddr = sg_dma_address(sgel);
  2936. dma_len = sg_dma_len(sgel);
  2937. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  2938. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  2939. sgl->word2 = le32_to_cpu(sgl->word2);
  2940. if ((num_bde + 1) == nseg)
  2941. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2942. else
  2943. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2944. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2945. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  2946. sgl->word2 = cpu_to_le32(sgl->word2);
  2947. sgl->sge_len = cpu_to_le32(dma_len);
  2948. dma_offset += dma_len;
  2949. sgl++;
  2950. }
  2951. /* setup the performance hint (first data BDE) if enabled */
  2952. if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
  2953. bde = (struct ulp_bde64 *)
  2954. &(iocb_cmd->unsli3.sli3Words[5]);
  2955. bde->addrLow = first_data_sgl->addr_lo;
  2956. bde->addrHigh = first_data_sgl->addr_hi;
  2957. bde->tus.f.bdeSize =
  2958. le32_to_cpu(first_data_sgl->sge_len);
  2959. bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  2960. bde->tus.w = cpu_to_le32(bde->tus.w);
  2961. }
  2962. } else {
  2963. sgl += 1;
  2964. /* clear the last flag in the fcp_rsp map entry */
  2965. sgl->word2 = le32_to_cpu(sgl->word2);
  2966. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2967. sgl->word2 = cpu_to_le32(sgl->word2);
  2968. }
  2969. /*
  2970. * Finish initializing those IOCB fields that are dependent on the
  2971. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  2972. * explicitly reinitialized.
  2973. * all iocb memory resources are reused.
  2974. */
  2975. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  2976. /*
  2977. * Due to difference in data length between DIF/non-DIF paths,
  2978. * we need to set word 4 of IOCB here
  2979. */
  2980. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  2981. /*
  2982. * If the OAS driver feature is enabled and the lun is enabled for
  2983. * OAS, set the oas iocb related flags.
  2984. */
  2985. if ((phba->cfg_fof) && ((struct lpfc_device_data *)
  2986. scsi_cmnd->device->hostdata)->oas_enabled) {
  2987. lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
  2988. lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
  2989. scsi_cmnd->device->hostdata)->priority;
  2990. }
  2991. return 0;
  2992. }
  2993. /**
  2994. * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  2995. * @phba: The Hba for which this call is being executed.
  2996. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2997. *
  2998. * This is the protection/DIF aware version of
  2999. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  3000. * two functions eventually, but for now, it's here
  3001. **/
  3002. static int
  3003. lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
  3004. struct lpfc_scsi_buf *lpfc_cmd)
  3005. {
  3006. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  3007. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  3008. struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
  3009. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  3010. uint32_t num_sge = 0;
  3011. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  3012. int prot_group_type = 0;
  3013. int fcpdl;
  3014. /*
  3015. * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
  3016. * fcp_rsp regions to the first data sge entry
  3017. */
  3018. if (scsi_sg_count(scsi_cmnd)) {
  3019. /*
  3020. * The driver stores the segment count returned from pci_map_sg
  3021. * because this a count of dma-mappings used to map the use_sg
  3022. * pages. They are not guaranteed to be the same for those
  3023. * architectures that implement an IOMMU.
  3024. */
  3025. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  3026. scsi_sglist(scsi_cmnd),
  3027. scsi_sg_count(scsi_cmnd), datadir);
  3028. if (unlikely(!datasegcnt))
  3029. return 1;
  3030. sgl += 1;
  3031. /* clear the last flag in the fcp_rsp map entry */
  3032. sgl->word2 = le32_to_cpu(sgl->word2);
  3033. bf_set(lpfc_sli4_sge_last, sgl, 0);
  3034. sgl->word2 = cpu_to_le32(sgl->word2);
  3035. sgl += 1;
  3036. lpfc_cmd->seg_cnt = datasegcnt;
  3037. /* First check if data segment count from SCSI Layer is good */
  3038. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
  3039. goto err;
  3040. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  3041. switch (prot_group_type) {
  3042. case LPFC_PG_TYPE_NO_DIF:
  3043. /* Here we need to add a DISEED to the count */
  3044. if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
  3045. goto err;
  3046. num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
  3047. datasegcnt);
  3048. /* we should have 2 or more entries in buffer list */
  3049. if (num_sge < 2)
  3050. goto err;
  3051. break;
  3052. case LPFC_PG_TYPE_DIF_BUF:
  3053. /*
  3054. * This type indicates that protection buffers are
  3055. * passed to the driver, so that needs to be prepared
  3056. * for DMA
  3057. */
  3058. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  3059. scsi_prot_sglist(scsi_cmnd),
  3060. scsi_prot_sg_count(scsi_cmnd), datadir);
  3061. if (unlikely(!protsegcnt)) {
  3062. scsi_dma_unmap(scsi_cmnd);
  3063. return 1;
  3064. }
  3065. lpfc_cmd->prot_seg_cnt = protsegcnt;
  3066. /*
  3067. * There is a minimun of 3 SGEs used for every
  3068. * protection data segment.
  3069. */
  3070. if ((lpfc_cmd->prot_seg_cnt * 3) >
  3071. (phba->cfg_total_seg_cnt - 2))
  3072. goto err;
  3073. num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
  3074. datasegcnt, protsegcnt);
  3075. /* we should have 3 or more entries in buffer list */
  3076. if ((num_sge < 3) ||
  3077. (num_sge > phba->cfg_total_seg_cnt))
  3078. goto err;
  3079. break;
  3080. case LPFC_PG_TYPE_INVALID:
  3081. default:
  3082. scsi_dma_unmap(scsi_cmnd);
  3083. lpfc_cmd->seg_cnt = 0;
  3084. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  3085. "9083 Unexpected protection group %i\n",
  3086. prot_group_type);
  3087. return 1;
  3088. }
  3089. }
  3090. switch (scsi_get_prot_op(scsi_cmnd)) {
  3091. case SCSI_PROT_WRITE_STRIP:
  3092. case SCSI_PROT_READ_STRIP:
  3093. lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
  3094. break;
  3095. case SCSI_PROT_WRITE_INSERT:
  3096. case SCSI_PROT_READ_INSERT:
  3097. lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
  3098. break;
  3099. case SCSI_PROT_WRITE_PASS:
  3100. case SCSI_PROT_READ_PASS:
  3101. lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
  3102. break;
  3103. }
  3104. fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
  3105. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  3106. /*
  3107. * Due to difference in data length between DIF/non-DIF paths,
  3108. * we need to set word 4 of IOCB here
  3109. */
  3110. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  3111. /*
  3112. * If the OAS driver feature is enabled and the lun is enabled for
  3113. * OAS, set the oas iocb related flags.
  3114. */
  3115. if ((phba->cfg_fof) && ((struct lpfc_device_data *)
  3116. scsi_cmnd->device->hostdata)->oas_enabled)
  3117. lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
  3118. return 0;
  3119. err:
  3120. if (lpfc_cmd->seg_cnt)
  3121. scsi_dma_unmap(scsi_cmnd);
  3122. if (lpfc_cmd->prot_seg_cnt)
  3123. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
  3124. scsi_prot_sg_count(scsi_cmnd),
  3125. scsi_cmnd->sc_data_direction);
  3126. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  3127. "9084 Cannot setup S/G List for HBA"
  3128. "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
  3129. lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
  3130. phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
  3131. prot_group_type, num_sge);
  3132. lpfc_cmd->seg_cnt = 0;
  3133. lpfc_cmd->prot_seg_cnt = 0;
  3134. return 1;
  3135. }
  3136. /**
  3137. * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  3138. * @phba: The Hba for which this call is being executed.
  3139. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  3140. *
  3141. * This routine wraps the actual DMA mapping function pointer from the
  3142. * lpfc_hba struct.
  3143. *
  3144. * Return codes:
  3145. * 1 - Error
  3146. * 0 - Success
  3147. **/
  3148. static inline int
  3149. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  3150. {
  3151. return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  3152. }
  3153. /**
  3154. * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  3155. * using BlockGuard.
  3156. * @phba: The Hba for which this call is being executed.
  3157. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  3158. *
  3159. * This routine wraps the actual DMA mapping function pointer from the
  3160. * lpfc_hba struct.
  3161. *
  3162. * Return codes:
  3163. * 1 - Error
  3164. * 0 - Success
  3165. **/
  3166. static inline int
  3167. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  3168. {
  3169. return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  3170. }
  3171. /**
  3172. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  3173. * @phba: Pointer to hba context object.
  3174. * @vport: Pointer to vport object.
  3175. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  3176. * @rsp_iocb: Pointer to response iocb object which reported error.
  3177. *
  3178. * This function posts an event when there is a SCSI command reporting
  3179. * error from the scsi device.
  3180. **/
  3181. static void
  3182. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  3183. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  3184. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  3185. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  3186. uint32_t resp_info = fcprsp->rspStatus2;
  3187. uint32_t scsi_status = fcprsp->rspStatus3;
  3188. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  3189. struct lpfc_fast_path_event *fast_path_evt = NULL;
  3190. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  3191. unsigned long flags;
  3192. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3193. return;
  3194. /* If there is queuefull or busy condition send a scsi event */
  3195. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  3196. (cmnd->result == SAM_STAT_BUSY)) {
  3197. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3198. if (!fast_path_evt)
  3199. return;
  3200. fast_path_evt->un.scsi_evt.event_type =
  3201. FC_REG_SCSI_EVENT;
  3202. fast_path_evt->un.scsi_evt.subcategory =
  3203. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  3204. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  3205. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  3206. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  3207. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3208. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  3209. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3210. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  3211. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  3212. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3213. if (!fast_path_evt)
  3214. return;
  3215. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  3216. FC_REG_SCSI_EVENT;
  3217. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  3218. LPFC_EVENT_CHECK_COND;
  3219. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  3220. cmnd->device->lun;
  3221. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  3222. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3223. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  3224. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3225. fast_path_evt->un.check_cond_evt.sense_key =
  3226. cmnd->sense_buffer[2] & 0xf;
  3227. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  3228. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  3229. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  3230. fcpi_parm &&
  3231. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  3232. ((scsi_status == SAM_STAT_GOOD) &&
  3233. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  3234. /*
  3235. * If status is good or resid does not match with fcp_param and
  3236. * there is valid fcpi_parm, then there is a read_check error
  3237. */
  3238. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3239. if (!fast_path_evt)
  3240. return;
  3241. fast_path_evt->un.read_check_error.header.event_type =
  3242. FC_REG_FABRIC_EVENT;
  3243. fast_path_evt->un.read_check_error.header.subcategory =
  3244. LPFC_EVENT_FCPRDCHKERR;
  3245. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  3246. &pnode->nlp_portname, sizeof(struct lpfc_name));
  3247. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  3248. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3249. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  3250. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  3251. fast_path_evt->un.read_check_error.fcpiparam =
  3252. fcpi_parm;
  3253. } else
  3254. return;
  3255. fast_path_evt->vport = vport;
  3256. spin_lock_irqsave(&phba->hbalock, flags);
  3257. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  3258. spin_unlock_irqrestore(&phba->hbalock, flags);
  3259. lpfc_worker_wake_up(phba);
  3260. return;
  3261. }
  3262. /**
  3263. * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
  3264. * @phba: The HBA for which this call is being executed.
  3265. * @psb: The scsi buffer which is going to be un-mapped.
  3266. *
  3267. * This routine does DMA un-mapping of scatter gather list of scsi command
  3268. * field of @lpfc_cmd for device with SLI-3 interface spec.
  3269. **/
  3270. static void
  3271. lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  3272. {
  3273. /*
  3274. * There are only two special cases to consider. (1) the scsi command
  3275. * requested scatter-gather usage or (2) the scsi command allocated
  3276. * a request buffer, but did not request use_sg. There is a third
  3277. * case, but it does not require resource deallocation.
  3278. */
  3279. if (psb->seg_cnt > 0)
  3280. scsi_dma_unmap(psb->pCmd);
  3281. if (psb->prot_seg_cnt > 0)
  3282. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  3283. scsi_prot_sg_count(psb->pCmd),
  3284. psb->pCmd->sc_data_direction);
  3285. }
  3286. /**
  3287. * lpfc_handler_fcp_err - FCP response handler
  3288. * @vport: The virtual port for which this call is being executed.
  3289. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  3290. * @rsp_iocb: The response IOCB which contains FCP error.
  3291. *
  3292. * This routine is called to process response IOCB with status field
  3293. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  3294. * based upon SCSI and FCP error.
  3295. **/
  3296. static void
  3297. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  3298. struct lpfc_iocbq *rsp_iocb)
  3299. {
  3300. struct lpfc_hba *phba = vport->phba;
  3301. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  3302. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  3303. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  3304. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  3305. uint32_t resp_info = fcprsp->rspStatus2;
  3306. uint32_t scsi_status = fcprsp->rspStatus3;
  3307. uint32_t *lp;
  3308. uint32_t host_status = DID_OK;
  3309. uint32_t rsplen = 0;
  3310. uint32_t fcpDl;
  3311. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  3312. /*
  3313. * If this is a task management command, there is no
  3314. * scsi packet associated with this lpfc_cmd. The driver
  3315. * consumes it.
  3316. */
  3317. if (fcpcmd->fcpCntl2) {
  3318. scsi_status = 0;
  3319. goto out;
  3320. }
  3321. if (resp_info & RSP_LEN_VALID) {
  3322. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  3323. if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
  3324. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3325. "2719 Invalid response length: "
  3326. "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
  3327. cmnd->device->id,
  3328. cmnd->device->lun, cmnd->cmnd[0],
  3329. rsplen);
  3330. host_status = DID_ERROR;
  3331. goto out;
  3332. }
  3333. if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
  3334. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3335. "2757 Protocol failure detected during "
  3336. "processing of FCP I/O op: "
  3337. "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
  3338. cmnd->device->id,
  3339. cmnd->device->lun, cmnd->cmnd[0],
  3340. fcprsp->rspInfo3);
  3341. host_status = DID_ERROR;
  3342. goto out;
  3343. }
  3344. }
  3345. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  3346. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  3347. if (snslen > SCSI_SENSE_BUFFERSIZE)
  3348. snslen = SCSI_SENSE_BUFFERSIZE;
  3349. if (resp_info & RSP_LEN_VALID)
  3350. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  3351. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  3352. }
  3353. lp = (uint32_t *)cmnd->sense_buffer;
  3354. /* special handling for under run conditions */
  3355. if (!scsi_status && (resp_info & RESID_UNDER)) {
  3356. /* don't log under runs if fcp set... */
  3357. if (vport->cfg_log_verbose & LOG_FCP)
  3358. logit = LOG_FCP_ERROR;
  3359. /* unless operator says so */
  3360. if (vport->cfg_log_verbose & LOG_FCP_UNDER)
  3361. logit = LOG_FCP_UNDER;
  3362. }
  3363. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3364. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  3365. "Data: x%x x%x x%x x%x x%x\n",
  3366. cmnd->cmnd[0], scsi_status,
  3367. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  3368. be32_to_cpu(fcprsp->rspResId),
  3369. be32_to_cpu(fcprsp->rspSnsLen),
  3370. be32_to_cpu(fcprsp->rspRspLen),
  3371. fcprsp->rspInfo3);
  3372. scsi_set_resid(cmnd, 0);
  3373. fcpDl = be32_to_cpu(fcpcmd->fcpDl);
  3374. if (resp_info & RESID_UNDER) {
  3375. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  3376. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
  3377. "9025 FCP Read Underrun, expected %d, "
  3378. "residual %d Data: x%x x%x x%x\n",
  3379. fcpDl,
  3380. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  3381. cmnd->underflow);
  3382. /*
  3383. * If there is an under run check if under run reported by
  3384. * storage array is same as the under run reported by HBA.
  3385. * If this is not same, there is a dropped frame.
  3386. */
  3387. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  3388. fcpi_parm &&
  3389. (scsi_get_resid(cmnd) != fcpi_parm)) {
  3390. lpfc_printf_vlog(vport, KERN_WARNING,
  3391. LOG_FCP | LOG_FCP_ERROR,
  3392. "9026 FCP Read Check Error "
  3393. "and Underrun Data: x%x x%x x%x x%x\n",
  3394. fcpDl,
  3395. scsi_get_resid(cmnd), fcpi_parm,
  3396. cmnd->cmnd[0]);
  3397. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  3398. host_status = DID_ERROR;
  3399. }
  3400. /*
  3401. * The cmnd->underflow is the minimum number of bytes that must
  3402. * be transferred for this command. Provided a sense condition
  3403. * is not present, make sure the actual amount transferred is at
  3404. * least the underflow value or fail.
  3405. */
  3406. if (!(resp_info & SNS_LEN_VALID) &&
  3407. (scsi_status == SAM_STAT_GOOD) &&
  3408. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  3409. < cmnd->underflow)) {
  3410. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3411. "9027 FCP command x%x residual "
  3412. "underrun converted to error "
  3413. "Data: x%x x%x x%x\n",
  3414. cmnd->cmnd[0], scsi_bufflen(cmnd),
  3415. scsi_get_resid(cmnd), cmnd->underflow);
  3416. host_status = DID_ERROR;
  3417. }
  3418. } else if (resp_info & RESID_OVER) {
  3419. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3420. "9028 FCP command x%x residual overrun error. "
  3421. "Data: x%x x%x\n", cmnd->cmnd[0],
  3422. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  3423. host_status = DID_ERROR;
  3424. /*
  3425. * Check SLI validation that all the transfer was actually done
  3426. * (fcpi_parm should be zero). Apply check only to reads.
  3427. */
  3428. } else if (fcpi_parm) {
  3429. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  3430. "9029 FCP %s Check Error xri x%x Data: "
  3431. "x%x x%x x%x x%x x%x\n",
  3432. ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
  3433. "Read" : "Write"),
  3434. ((phba->sli_rev == LPFC_SLI_REV4) ?
  3435. lpfc_cmd->cur_iocbq.sli4_xritag :
  3436. rsp_iocb->iocb.ulpContext),
  3437. fcpDl, be32_to_cpu(fcprsp->rspResId),
  3438. fcpi_parm, cmnd->cmnd[0], scsi_status);
  3439. /* There is some issue with the LPe12000 that causes it
  3440. * to miscalculate the fcpi_parm and falsely trip this
  3441. * recovery logic. Detect this case and don't error when true.
  3442. */
  3443. if (fcpi_parm > fcpDl)
  3444. goto out;
  3445. switch (scsi_status) {
  3446. case SAM_STAT_GOOD:
  3447. case SAM_STAT_CHECK_CONDITION:
  3448. /* Fabric dropped a data frame. Fail any successful
  3449. * command in which we detected dropped frames.
  3450. * A status of good or some check conditions could
  3451. * be considered a successful command.
  3452. */
  3453. host_status = DID_ERROR;
  3454. break;
  3455. }
  3456. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  3457. }
  3458. out:
  3459. cmnd->result = ScsiResult(host_status, scsi_status);
  3460. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  3461. }
  3462. /**
  3463. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  3464. * @phba: Pointer to HBA context object.
  3465. *
  3466. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  3467. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  3468. * held.
  3469. * If scsi-mq is enabled, get the default block layer mapping of software queues
  3470. * to hardware queues. This information is saved in request tag.
  3471. *
  3472. * Return: index into SLI4 fast-path FCP queue index.
  3473. **/
  3474. int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
  3475. struct lpfc_scsi_buf *lpfc_cmd)
  3476. {
  3477. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  3478. struct lpfc_vector_map_info *cpup;
  3479. int chann, cpu;
  3480. uint32_t tag;
  3481. uint16_t hwq;
  3482. if (cmnd && shost_use_blk_mq(cmnd->device->host)) {
  3483. tag = blk_mq_unique_tag(cmnd->request);
  3484. hwq = blk_mq_unique_tag_to_hwq(tag);
  3485. return hwq;
  3486. }
  3487. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
  3488. && phba->cfg_fcp_io_channel > 1) {
  3489. cpu = smp_processor_id();
  3490. if (cpu < phba->sli4_hba.num_present_cpu) {
  3491. cpup = phba->sli4_hba.cpu_map;
  3492. cpup += cpu;
  3493. return cpup->channel_id;
  3494. }
  3495. }
  3496. chann = atomic_add_return(1, &phba->fcp_qidx);
  3497. chann = (chann % phba->cfg_fcp_io_channel);
  3498. return chann;
  3499. }
  3500. /**
  3501. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  3502. * @phba: The Hba for which this call is being executed.
  3503. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  3504. * @pIocbOut: The response IOCBQ for the scsi cmnd.
  3505. *
  3506. * This routine assigns scsi command result by looking into response IOCB
  3507. * status field appropriately. This routine handles QUEUE FULL condition as
  3508. * well by ramping down device queue depth.
  3509. **/
  3510. static void
  3511. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  3512. struct lpfc_iocbq *pIocbOut)
  3513. {
  3514. struct lpfc_scsi_buf *lpfc_cmd =
  3515. (struct lpfc_scsi_buf *) pIocbIn->context1;
  3516. struct lpfc_vport *vport = pIocbIn->vport;
  3517. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  3518. struct lpfc_nodelist *pnode = rdata->pnode;
  3519. struct scsi_cmnd *cmd;
  3520. int depth;
  3521. unsigned long flags;
  3522. struct lpfc_fast_path_event *fast_path_evt;
  3523. struct Scsi_Host *shost;
  3524. uint32_t logit = LOG_FCP;
  3525. /* Sanity check on return of outstanding command */
  3526. cmd = lpfc_cmd->pCmd;
  3527. if (!cmd)
  3528. return;
  3529. shost = cmd->device->host;
  3530. lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
  3531. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  3532. /* pick up SLI4 exhange busy status from HBA */
  3533. lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
  3534. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  3535. if (lpfc_cmd->prot_data_type) {
  3536. struct scsi_dif_tuple *src = NULL;
  3537. src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
  3538. /*
  3539. * Used to restore any changes to protection
  3540. * data for error injection.
  3541. */
  3542. switch (lpfc_cmd->prot_data_type) {
  3543. case LPFC_INJERR_REFTAG:
  3544. src->ref_tag =
  3545. lpfc_cmd->prot_data;
  3546. break;
  3547. case LPFC_INJERR_APPTAG:
  3548. src->app_tag =
  3549. (uint16_t)lpfc_cmd->prot_data;
  3550. break;
  3551. case LPFC_INJERR_GUARD:
  3552. src->guard_tag =
  3553. (uint16_t)lpfc_cmd->prot_data;
  3554. break;
  3555. default:
  3556. break;
  3557. }
  3558. lpfc_cmd->prot_data = 0;
  3559. lpfc_cmd->prot_data_type = 0;
  3560. lpfc_cmd->prot_data_segment = NULL;
  3561. }
  3562. #endif
  3563. if (pnode && NLP_CHK_NODE_ACT(pnode))
  3564. atomic_dec(&pnode->cmd_pending);
  3565. if (lpfc_cmd->status) {
  3566. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  3567. (lpfc_cmd->result & IOERR_DRVR_MASK))
  3568. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3569. else if (lpfc_cmd->status >= IOSTAT_CNT)
  3570. lpfc_cmd->status = IOSTAT_DEFAULT;
  3571. if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
  3572. !lpfc_cmd->fcp_rsp->rspStatus3 &&
  3573. (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
  3574. !(vport->cfg_log_verbose & LOG_FCP_UNDER))
  3575. logit = 0;
  3576. else
  3577. logit = LOG_FCP | LOG_FCP_UNDER;
  3578. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  3579. "9030 FCP cmd x%x failed <%d/%lld> "
  3580. "status: x%x result: x%x "
  3581. "sid: x%x did: x%x oxid: x%x "
  3582. "Data: x%x x%x\n",
  3583. cmd->cmnd[0],
  3584. cmd->device ? cmd->device->id : 0xffff,
  3585. cmd->device ? cmd->device->lun : 0xffff,
  3586. lpfc_cmd->status, lpfc_cmd->result,
  3587. vport->fc_myDID,
  3588. (pnode) ? pnode->nlp_DID : 0,
  3589. phba->sli_rev == LPFC_SLI_REV4 ?
  3590. lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
  3591. pIocbOut->iocb.ulpContext,
  3592. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  3593. switch (lpfc_cmd->status) {
  3594. case IOSTAT_FCP_RSP_ERROR:
  3595. /* Call FCP RSP handler to determine result */
  3596. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  3597. break;
  3598. case IOSTAT_NPORT_BSY:
  3599. case IOSTAT_FABRIC_BSY:
  3600. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  3601. fast_path_evt = lpfc_alloc_fast_evt(phba);
  3602. if (!fast_path_evt)
  3603. break;
  3604. fast_path_evt->un.fabric_evt.event_type =
  3605. FC_REG_FABRIC_EVENT;
  3606. fast_path_evt->un.fabric_evt.subcategory =
  3607. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  3608. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  3609. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  3610. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  3611. &pnode->nlp_portname,
  3612. sizeof(struct lpfc_name));
  3613. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  3614. &pnode->nlp_nodename,
  3615. sizeof(struct lpfc_name));
  3616. }
  3617. fast_path_evt->vport = vport;
  3618. fast_path_evt->work_evt.evt =
  3619. LPFC_EVT_FASTPATH_MGMT_EVT;
  3620. spin_lock_irqsave(&phba->hbalock, flags);
  3621. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  3622. &phba->work_list);
  3623. spin_unlock_irqrestore(&phba->hbalock, flags);
  3624. lpfc_worker_wake_up(phba);
  3625. break;
  3626. case IOSTAT_LOCAL_REJECT:
  3627. case IOSTAT_REMOTE_STOP:
  3628. if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
  3629. lpfc_cmd->result ==
  3630. IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
  3631. lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
  3632. lpfc_cmd->result ==
  3633. IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
  3634. cmd->result = ScsiResult(DID_NO_CONNECT, 0);
  3635. break;
  3636. }
  3637. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  3638. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  3639. lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
  3640. lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
  3641. cmd->result = ScsiResult(DID_REQUEUE, 0);
  3642. break;
  3643. }
  3644. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  3645. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  3646. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  3647. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  3648. /*
  3649. * This is a response for a BG enabled
  3650. * cmd. Parse BG error
  3651. */
  3652. lpfc_parse_bg_err(phba, lpfc_cmd,
  3653. pIocbOut);
  3654. break;
  3655. } else {
  3656. lpfc_printf_vlog(vport, KERN_WARNING,
  3657. LOG_BG,
  3658. "9031 non-zero BGSTAT "
  3659. "on unprotected cmd\n");
  3660. }
  3661. }
  3662. if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
  3663. && (phba->sli_rev == LPFC_SLI_REV4)
  3664. && (pnode && NLP_CHK_NODE_ACT(pnode))) {
  3665. /* This IO was aborted by the target, we don't
  3666. * know the rxid and because we did not send the
  3667. * ABTS we cannot generate and RRQ.
  3668. */
  3669. lpfc_set_rrq_active(phba, pnode,
  3670. lpfc_cmd->cur_iocbq.sli4_lxritag,
  3671. 0, 0);
  3672. }
  3673. /* else: fall through */
  3674. default:
  3675. cmd->result = ScsiResult(DID_ERROR, 0);
  3676. break;
  3677. }
  3678. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  3679. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  3680. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  3681. SAM_STAT_BUSY);
  3682. } else
  3683. cmd->result = ScsiResult(DID_OK, 0);
  3684. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  3685. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  3686. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3687. "0710 Iodone <%d/%llu> cmd %p, error "
  3688. "x%x SNS x%x x%x Data: x%x x%x\n",
  3689. cmd->device->id, cmd->device->lun, cmd,
  3690. cmd->result, *lp, *(lp + 3), cmd->retries,
  3691. scsi_get_resid(cmd));
  3692. }
  3693. lpfc_update_stats(phba, lpfc_cmd);
  3694. if (vport->cfg_max_scsicmpl_time &&
  3695. time_after(jiffies, lpfc_cmd->start_time +
  3696. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  3697. spin_lock_irqsave(shost->host_lock, flags);
  3698. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  3699. if (pnode->cmd_qdepth >
  3700. atomic_read(&pnode->cmd_pending) &&
  3701. (atomic_read(&pnode->cmd_pending) >
  3702. LPFC_MIN_TGT_QDEPTH) &&
  3703. ((cmd->cmnd[0] == READ_10) ||
  3704. (cmd->cmnd[0] == WRITE_10)))
  3705. pnode->cmd_qdepth =
  3706. atomic_read(&pnode->cmd_pending);
  3707. pnode->last_change_time = jiffies;
  3708. }
  3709. spin_unlock_irqrestore(shost->host_lock, flags);
  3710. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  3711. if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
  3712. time_after(jiffies, pnode->last_change_time +
  3713. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  3714. spin_lock_irqsave(shost->host_lock, flags);
  3715. depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
  3716. / 100;
  3717. depth = depth ? depth : 1;
  3718. pnode->cmd_qdepth += depth;
  3719. if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
  3720. pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
  3721. pnode->last_change_time = jiffies;
  3722. spin_unlock_irqrestore(shost->host_lock, flags);
  3723. }
  3724. }
  3725. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  3726. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  3727. cmd->scsi_done(cmd);
  3728. spin_lock_irqsave(&phba->hbalock, flags);
  3729. lpfc_cmd->pCmd = NULL;
  3730. spin_unlock_irqrestore(&phba->hbalock, flags);
  3731. /*
  3732. * If there is a thread waiting for command completion
  3733. * wake up the thread.
  3734. */
  3735. spin_lock_irqsave(shost->host_lock, flags);
  3736. if (lpfc_cmd->waitq)
  3737. wake_up(lpfc_cmd->waitq);
  3738. spin_unlock_irqrestore(shost->host_lock, flags);
  3739. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3740. }
  3741. /**
  3742. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  3743. * @data: A pointer to the immediate command data portion of the IOCB.
  3744. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  3745. *
  3746. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  3747. * byte swapping the data to big endian format for transmission on the wire.
  3748. **/
  3749. static void
  3750. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  3751. {
  3752. int i, j;
  3753. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  3754. i += sizeof(uint32_t), j++) {
  3755. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  3756. }
  3757. }
  3758. /**
  3759. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  3760. * @vport: The virtual port for which this call is being executed.
  3761. * @lpfc_cmd: The scsi command which needs to send.
  3762. * @pnode: Pointer to lpfc_nodelist.
  3763. *
  3764. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  3765. * to transfer for device with SLI3 interface spec.
  3766. **/
  3767. static void
  3768. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  3769. struct lpfc_nodelist *pnode)
  3770. {
  3771. struct lpfc_hba *phba = vport->phba;
  3772. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  3773. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  3774. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  3775. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  3776. int datadir = scsi_cmnd->sc_data_direction;
  3777. uint8_t *ptr;
  3778. bool sli4;
  3779. uint32_t fcpdl;
  3780. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3781. return;
  3782. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  3783. /* clear task management bits */
  3784. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  3785. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  3786. &lpfc_cmd->fcp_cmnd->fcp_lun);
  3787. ptr = &fcp_cmnd->fcpCdb[0];
  3788. memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
  3789. if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
  3790. ptr += scsi_cmnd->cmd_len;
  3791. memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
  3792. }
  3793. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  3794. sli4 = (phba->sli_rev == LPFC_SLI_REV4);
  3795. piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
  3796. /*
  3797. * There are three possibilities here - use scatter-gather segment, use
  3798. * the single mapping, or neither. Start the lpfc command prep by
  3799. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  3800. * data bde entry.
  3801. */
  3802. if (scsi_sg_count(scsi_cmnd)) {
  3803. if (datadir == DMA_TO_DEVICE) {
  3804. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  3805. iocb_cmd->ulpPU = PARM_READ_CHECK;
  3806. if (vport->cfg_first_burst_size &&
  3807. (pnode->nlp_flag & NLP_FIRSTBURST)) {
  3808. fcpdl = scsi_bufflen(scsi_cmnd);
  3809. if (fcpdl < vport->cfg_first_burst_size)
  3810. piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
  3811. else
  3812. piocbq->iocb.un.fcpi.fcpi_XRdy =
  3813. vport->cfg_first_burst_size;
  3814. }
  3815. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  3816. phba->fc4OutputRequests++;
  3817. } else {
  3818. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  3819. iocb_cmd->ulpPU = PARM_READ_CHECK;
  3820. fcp_cmnd->fcpCntl3 = READ_DATA;
  3821. phba->fc4InputRequests++;
  3822. }
  3823. } else {
  3824. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  3825. iocb_cmd->un.fcpi.fcpi_parm = 0;
  3826. iocb_cmd->ulpPU = 0;
  3827. fcp_cmnd->fcpCntl3 = 0;
  3828. phba->fc4ControlRequests++;
  3829. }
  3830. if (phba->sli_rev == 3 &&
  3831. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  3832. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  3833. /*
  3834. * Finish initializing those IOCB fields that are independent
  3835. * of the scsi_cmnd request_buffer
  3836. */
  3837. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  3838. if (sli4)
  3839. piocbq->iocb.ulpContext =
  3840. phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
  3841. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  3842. piocbq->iocb.ulpFCP2Rcvy = 1;
  3843. else
  3844. piocbq->iocb.ulpFCP2Rcvy = 0;
  3845. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  3846. piocbq->context1 = lpfc_cmd;
  3847. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  3848. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  3849. piocbq->vport = vport;
  3850. }
  3851. /**
  3852. * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
  3853. * @vport: The virtual port for which this call is being executed.
  3854. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  3855. * @lun: Logical unit number.
  3856. * @task_mgmt_cmd: SCSI task management command.
  3857. *
  3858. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  3859. * for device with SLI-3 interface spec.
  3860. *
  3861. * Return codes:
  3862. * 0 - Error
  3863. * 1 - Success
  3864. **/
  3865. static int
  3866. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  3867. struct lpfc_scsi_buf *lpfc_cmd,
  3868. uint64_t lun,
  3869. uint8_t task_mgmt_cmd)
  3870. {
  3871. struct lpfc_iocbq *piocbq;
  3872. IOCB_t *piocb;
  3873. struct fcp_cmnd *fcp_cmnd;
  3874. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  3875. struct lpfc_nodelist *ndlp = rdata->pnode;
  3876. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  3877. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  3878. return 0;
  3879. piocbq = &(lpfc_cmd->cur_iocbq);
  3880. piocbq->vport = vport;
  3881. piocb = &piocbq->iocb;
  3882. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  3883. /* Clear out any old data in the FCP command area */
  3884. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  3885. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  3886. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  3887. if (vport->phba->sli_rev == 3 &&
  3888. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  3889. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  3890. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  3891. piocb->ulpContext = ndlp->nlp_rpi;
  3892. if (vport->phba->sli_rev == LPFC_SLI_REV4) {
  3893. piocb->ulpContext =
  3894. vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  3895. }
  3896. piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
  3897. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  3898. piocb->ulpPU = 0;
  3899. piocb->un.fcpi.fcpi_parm = 0;
  3900. /* ulpTimeout is only one byte */
  3901. if (lpfc_cmd->timeout > 0xff) {
  3902. /*
  3903. * Do not timeout the command at the firmware level.
  3904. * The driver will provide the timeout mechanism.
  3905. */
  3906. piocb->ulpTimeout = 0;
  3907. } else
  3908. piocb->ulpTimeout = lpfc_cmd->timeout;
  3909. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  3910. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  3911. return 1;
  3912. }
  3913. /**
  3914. * lpfc_scsi_api_table_setup - Set up scsi api function jump table
  3915. * @phba: The hba struct for which this call is being executed.
  3916. * @dev_grp: The HBA PCI-Device group number.
  3917. *
  3918. * This routine sets up the SCSI interface API function jump table in @phba
  3919. * struct.
  3920. * Returns: 0 - success, -ENODEV - failure.
  3921. **/
  3922. int
  3923. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  3924. {
  3925. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  3926. phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
  3927. switch (dev_grp) {
  3928. case LPFC_PCI_DEV_LP:
  3929. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
  3930. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  3931. phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
  3932. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  3933. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
  3934. break;
  3935. case LPFC_PCI_DEV_OC:
  3936. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
  3937. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  3938. phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
  3939. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  3940. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
  3941. break;
  3942. default:
  3943. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3944. "1418 Invalid HBA PCI-device group: 0x%x\n",
  3945. dev_grp);
  3946. return -ENODEV;
  3947. break;
  3948. }
  3949. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  3950. phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  3951. return 0;
  3952. }
  3953. /**
  3954. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  3955. * @phba: The Hba for which this call is being executed.
  3956. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  3957. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  3958. *
  3959. * This routine is IOCB completion routine for device reset and target reset
  3960. * routine. This routine release scsi buffer associated with lpfc_cmd.
  3961. **/
  3962. static void
  3963. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  3964. struct lpfc_iocbq *cmdiocbq,
  3965. struct lpfc_iocbq *rspiocbq)
  3966. {
  3967. struct lpfc_scsi_buf *lpfc_cmd =
  3968. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  3969. if (lpfc_cmd)
  3970. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3971. return;
  3972. }
  3973. /**
  3974. * lpfc_info - Info entry point of scsi_host_template data structure
  3975. * @host: The scsi host for which this call is being executed.
  3976. *
  3977. * This routine provides module information about hba.
  3978. *
  3979. * Reutrn code:
  3980. * Pointer to char - Success.
  3981. **/
  3982. const char *
  3983. lpfc_info(struct Scsi_Host *host)
  3984. {
  3985. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  3986. struct lpfc_hba *phba = vport->phba;
  3987. int len, link_speed = 0;
  3988. static char lpfcinfobuf[384];
  3989. memset(lpfcinfobuf,0,384);
  3990. if (phba && phba->pcidev){
  3991. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  3992. len = strlen(lpfcinfobuf);
  3993. snprintf(lpfcinfobuf + len,
  3994. 384-len,
  3995. " on PCI bus %02x device %02x irq %d",
  3996. phba->pcidev->bus->number,
  3997. phba->pcidev->devfn,
  3998. phba->pcidev->irq);
  3999. len = strlen(lpfcinfobuf);
  4000. if (phba->Port[0]) {
  4001. snprintf(lpfcinfobuf + len,
  4002. 384-len,
  4003. " port %s",
  4004. phba->Port);
  4005. }
  4006. len = strlen(lpfcinfobuf);
  4007. link_speed = lpfc_sli_port_speed_get(phba);
  4008. if (link_speed != 0)
  4009. snprintf(lpfcinfobuf + len, 384-len,
  4010. " Logical Link Speed: %d Mbps", link_speed);
  4011. }
  4012. return lpfcinfobuf;
  4013. }
  4014. /**
  4015. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  4016. * @phba: The Hba for which this call is being executed.
  4017. *
  4018. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  4019. * The default value of cfg_poll_tmo is 10 milliseconds.
  4020. **/
  4021. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  4022. {
  4023. unsigned long poll_tmo_expires =
  4024. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  4025. if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
  4026. mod_timer(&phba->fcp_poll_timer,
  4027. poll_tmo_expires);
  4028. }
  4029. /**
  4030. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  4031. * @phba: The Hba for which this call is being executed.
  4032. *
  4033. * This routine starts the fcp_poll_timer of @phba.
  4034. **/
  4035. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  4036. {
  4037. lpfc_poll_rearm_timer(phba);
  4038. }
  4039. /**
  4040. * lpfc_poll_timeout - Restart polling timer
  4041. * @ptr: Map to lpfc_hba data structure pointer.
  4042. *
  4043. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  4044. * and FCP Ring interrupt is disable.
  4045. **/
  4046. void lpfc_poll_timeout(unsigned long ptr)
  4047. {
  4048. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  4049. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  4050. lpfc_sli_handle_fast_ring_event(phba,
  4051. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4052. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4053. lpfc_poll_rearm_timer(phba);
  4054. }
  4055. }
  4056. /**
  4057. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  4058. * @cmnd: Pointer to scsi_cmnd data structure.
  4059. * @done: Pointer to done routine.
  4060. *
  4061. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  4062. * This routine prepares an IOCB from scsi command and provides to firmware.
  4063. * The @done callback is invoked after driver finished processing the command.
  4064. *
  4065. * Return value :
  4066. * 0 - Success
  4067. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  4068. **/
  4069. static int
  4070. lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
  4071. {
  4072. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4073. struct lpfc_hba *phba = vport->phba;
  4074. struct lpfc_rport_data *rdata;
  4075. struct lpfc_nodelist *ndlp;
  4076. struct lpfc_scsi_buf *lpfc_cmd;
  4077. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  4078. int err;
  4079. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4080. err = fc_remote_port_chkready(rport);
  4081. if (err) {
  4082. cmnd->result = err;
  4083. goto out_fail_command;
  4084. }
  4085. ndlp = rdata->pnode;
  4086. if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
  4087. (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
  4088. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  4089. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  4090. " op:%02x str=%s without registering for"
  4091. " BlockGuard - Rejecting command\n",
  4092. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  4093. dif_op_str[scsi_get_prot_op(cmnd)]);
  4094. goto out_fail_command;
  4095. }
  4096. /*
  4097. * Catch race where our node has transitioned, but the
  4098. * transport is still transitioning.
  4099. */
  4100. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  4101. goto out_tgt_busy;
  4102. if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
  4103. goto out_tgt_busy;
  4104. lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
  4105. if (lpfc_cmd == NULL) {
  4106. lpfc_rampdown_queue_depth(phba);
  4107. lpfc_printf_vlog(vport, KERN_INFO, LOG_MISC,
  4108. "0707 driver's buffer pool is empty, "
  4109. "IO busied\n");
  4110. goto out_host_busy;
  4111. }
  4112. /*
  4113. * Store the midlayer's command structure for the completion phase
  4114. * and complete the command initialization.
  4115. */
  4116. lpfc_cmd->pCmd = cmnd;
  4117. lpfc_cmd->rdata = rdata;
  4118. lpfc_cmd->timeout = 0;
  4119. lpfc_cmd->start_time = jiffies;
  4120. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  4121. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  4122. if (vport->phba->cfg_enable_bg) {
  4123. lpfc_printf_vlog(vport,
  4124. KERN_INFO, LOG_SCSI_CMD,
  4125. "9033 BLKGRD: rcvd %s cmd:x%x "
  4126. "sector x%llx cnt %u pt %x\n",
  4127. dif_op_str[scsi_get_prot_op(cmnd)],
  4128. cmnd->cmnd[0],
  4129. (unsigned long long)scsi_get_lba(cmnd),
  4130. blk_rq_sectors(cmnd->request),
  4131. (cmnd->cmnd[1]>>5));
  4132. }
  4133. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  4134. } else {
  4135. if (vport->phba->cfg_enable_bg) {
  4136. lpfc_printf_vlog(vport,
  4137. KERN_INFO, LOG_SCSI_CMD,
  4138. "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
  4139. "x%x sector x%llx cnt %u pt %x\n",
  4140. cmnd->cmnd[0],
  4141. (unsigned long long)scsi_get_lba(cmnd),
  4142. blk_rq_sectors(cmnd->request),
  4143. (cmnd->cmnd[1]>>5));
  4144. }
  4145. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  4146. }
  4147. if (err)
  4148. goto out_host_busy_free_buf;
  4149. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  4150. atomic_inc(&ndlp->cmd_pending);
  4151. err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  4152. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  4153. if (err) {
  4154. atomic_dec(&ndlp->cmd_pending);
  4155. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4156. "3376 FCP could not issue IOCB err %x"
  4157. "FCP cmd x%x <%d/%llu> "
  4158. "sid: x%x did: x%x oxid: x%x "
  4159. "Data: x%x x%x x%x x%x\n",
  4160. err, cmnd->cmnd[0],
  4161. cmnd->device ? cmnd->device->id : 0xffff,
  4162. cmnd->device ? cmnd->device->lun : (u64) -1,
  4163. vport->fc_myDID, ndlp->nlp_DID,
  4164. phba->sli_rev == LPFC_SLI_REV4 ?
  4165. lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
  4166. lpfc_cmd->cur_iocbq.iocb.ulpContext,
  4167. lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
  4168. lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
  4169. (uint32_t)
  4170. (cmnd->request->timeout / 1000));
  4171. goto out_host_busy_free_buf;
  4172. }
  4173. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  4174. lpfc_sli_handle_fast_ring_event(phba,
  4175. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4176. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4177. lpfc_poll_rearm_timer(phba);
  4178. }
  4179. return 0;
  4180. out_host_busy_free_buf:
  4181. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  4182. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4183. out_host_busy:
  4184. return SCSI_MLQUEUE_HOST_BUSY;
  4185. out_tgt_busy:
  4186. return SCSI_MLQUEUE_TARGET_BUSY;
  4187. out_fail_command:
  4188. cmnd->scsi_done(cmnd);
  4189. return 0;
  4190. }
  4191. /**
  4192. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  4193. * @cmnd: Pointer to scsi_cmnd data structure.
  4194. *
  4195. * This routine aborts @cmnd pending in base driver.
  4196. *
  4197. * Return code :
  4198. * 0x2003 - Error
  4199. * 0x2002 - Success
  4200. **/
  4201. static int
  4202. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  4203. {
  4204. struct Scsi_Host *shost = cmnd->device->host;
  4205. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4206. struct lpfc_hba *phba = vport->phba;
  4207. struct lpfc_iocbq *iocb;
  4208. struct lpfc_iocbq *abtsiocb;
  4209. struct lpfc_scsi_buf *lpfc_cmd;
  4210. IOCB_t *cmd, *icmd;
  4211. int ret = SUCCESS, status = 0;
  4212. struct lpfc_sli_ring *pring_s4;
  4213. int ring_number, ret_val;
  4214. unsigned long flags, iflags;
  4215. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  4216. status = fc_block_scsi_eh(cmnd);
  4217. if (status != 0 && status != SUCCESS)
  4218. return status;
  4219. spin_lock_irqsave(&phba->hbalock, flags);
  4220. /* driver queued commands are in process of being flushed */
  4221. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
  4222. spin_unlock_irqrestore(&phba->hbalock, flags);
  4223. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4224. "3168 SCSI Layer abort requested I/O has been "
  4225. "flushed by LLD.\n");
  4226. return FAILED;
  4227. }
  4228. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  4229. if (!lpfc_cmd || !lpfc_cmd->pCmd) {
  4230. spin_unlock_irqrestore(&phba->hbalock, flags);
  4231. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4232. "2873 SCSI Layer I/O Abort Request IO CMPL Status "
  4233. "x%x ID %d LUN %llu\n",
  4234. SUCCESS, cmnd->device->id, cmnd->device->lun);
  4235. return SUCCESS;
  4236. }
  4237. iocb = &lpfc_cmd->cur_iocbq;
  4238. /* the command is in process of being cancelled */
  4239. if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
  4240. spin_unlock_irqrestore(&phba->hbalock, flags);
  4241. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4242. "3169 SCSI Layer abort requested I/O has been "
  4243. "cancelled by LLD.\n");
  4244. return FAILED;
  4245. }
  4246. /*
  4247. * If pCmd field of the corresponding lpfc_scsi_buf structure
  4248. * points to a different SCSI command, then the driver has
  4249. * already completed this command, but the midlayer did not
  4250. * see the completion before the eh fired. Just return SUCCESS.
  4251. */
  4252. if (lpfc_cmd->pCmd != cmnd) {
  4253. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4254. "3170 SCSI Layer abort requested I/O has been "
  4255. "completed by LLD.\n");
  4256. goto out_unlock;
  4257. }
  4258. BUG_ON(iocb->context1 != lpfc_cmd);
  4259. /* abort issued in recovery is still in progress */
  4260. if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
  4261. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4262. "3389 SCSI Layer I/O Abort Request is pending\n");
  4263. spin_unlock_irqrestore(&phba->hbalock, flags);
  4264. goto wait_for_cmpl;
  4265. }
  4266. abtsiocb = __lpfc_sli_get_iocbq(phba);
  4267. if (abtsiocb == NULL) {
  4268. ret = FAILED;
  4269. goto out_unlock;
  4270. }
  4271. /* Indicate the IO is being aborted by the driver. */
  4272. iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  4273. /*
  4274. * The scsi command can not be in txq and it is in flight because the
  4275. * pCmd is still pointig at the SCSI command we have to abort. There
  4276. * is no need to search the txcmplq. Just send an abort to the FW.
  4277. */
  4278. cmd = &iocb->iocb;
  4279. icmd = &abtsiocb->iocb;
  4280. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  4281. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  4282. if (phba->sli_rev == LPFC_SLI_REV4)
  4283. icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
  4284. else
  4285. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  4286. icmd->ulpLe = 1;
  4287. icmd->ulpClass = cmd->ulpClass;
  4288. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  4289. abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
  4290. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  4291. if (iocb->iocb_flag & LPFC_IO_FOF)
  4292. abtsiocb->iocb_flag |= LPFC_IO_FOF;
  4293. if (lpfc_is_link_up(phba))
  4294. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  4295. else
  4296. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  4297. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  4298. abtsiocb->vport = vport;
  4299. if (phba->sli_rev == LPFC_SLI_REV4) {
  4300. ring_number = MAX_SLI3_CONFIGURED_RINGS + iocb->fcp_wqidx;
  4301. pring_s4 = &phba->sli.ring[ring_number];
  4302. /* Note: both hbalock and ring_lock must be set here */
  4303. spin_lock_irqsave(&pring_s4->ring_lock, iflags);
  4304. ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  4305. abtsiocb, 0);
  4306. spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
  4307. } else {
  4308. ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  4309. abtsiocb, 0);
  4310. }
  4311. /* no longer need the lock after this point */
  4312. spin_unlock_irqrestore(&phba->hbalock, flags);
  4313. if (ret_val == IOCB_ERROR) {
  4314. lpfc_sli_release_iocbq(phba, abtsiocb);
  4315. ret = FAILED;
  4316. goto out;
  4317. }
  4318. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4319. lpfc_sli_handle_fast_ring_event(phba,
  4320. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4321. wait_for_cmpl:
  4322. lpfc_cmd->waitq = &waitq;
  4323. /* Wait for abort to complete */
  4324. wait_event_timeout(waitq,
  4325. (lpfc_cmd->pCmd != cmnd),
  4326. msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
  4327. spin_lock_irqsave(shost->host_lock, flags);
  4328. lpfc_cmd->waitq = NULL;
  4329. spin_unlock_irqrestore(shost->host_lock, flags);
  4330. if (lpfc_cmd->pCmd == cmnd) {
  4331. ret = FAILED;
  4332. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4333. "0748 abort handler timed out waiting "
  4334. "for abortng I/O (xri:x%x) to complete: "
  4335. "ret %#x, ID %d, LUN %llu\n",
  4336. iocb->sli4_xritag, ret,
  4337. cmnd->device->id, cmnd->device->lun);
  4338. }
  4339. goto out;
  4340. out_unlock:
  4341. spin_unlock_irqrestore(&phba->hbalock, flags);
  4342. out:
  4343. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4344. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  4345. "LUN %llu\n", ret, cmnd->device->id,
  4346. cmnd->device->lun);
  4347. return ret;
  4348. }
  4349. static char *
  4350. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  4351. {
  4352. switch (task_mgmt_cmd) {
  4353. case FCP_ABORT_TASK_SET:
  4354. return "ABORT_TASK_SET";
  4355. case FCP_CLEAR_TASK_SET:
  4356. return "FCP_CLEAR_TASK_SET";
  4357. case FCP_BUS_RESET:
  4358. return "FCP_BUS_RESET";
  4359. case FCP_LUN_RESET:
  4360. return "FCP_LUN_RESET";
  4361. case FCP_TARGET_RESET:
  4362. return "FCP_TARGET_RESET";
  4363. case FCP_CLEAR_ACA:
  4364. return "FCP_CLEAR_ACA";
  4365. case FCP_TERMINATE_TASK:
  4366. return "FCP_TERMINATE_TASK";
  4367. default:
  4368. return "unknown";
  4369. }
  4370. }
  4371. /**
  4372. * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
  4373. * @vport: The virtual port for which this call is being executed.
  4374. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  4375. *
  4376. * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
  4377. *
  4378. * Return code :
  4379. * 0x2003 - Error
  4380. * 0x2002 - Success
  4381. **/
  4382. static int
  4383. lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
  4384. {
  4385. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  4386. uint32_t rsp_info;
  4387. uint32_t rsp_len;
  4388. uint8_t rsp_info_code;
  4389. int ret = FAILED;
  4390. if (fcprsp == NULL)
  4391. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4392. "0703 fcp_rsp is missing\n");
  4393. else {
  4394. rsp_info = fcprsp->rspStatus2;
  4395. rsp_len = be32_to_cpu(fcprsp->rspRspLen);
  4396. rsp_info_code = fcprsp->rspInfo3;
  4397. lpfc_printf_vlog(vport, KERN_INFO,
  4398. LOG_FCP,
  4399. "0706 fcp_rsp valid 0x%x,"
  4400. " rsp len=%d code 0x%x\n",
  4401. rsp_info,
  4402. rsp_len, rsp_info_code);
  4403. if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
  4404. switch (rsp_info_code) {
  4405. case RSP_NO_FAILURE:
  4406. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4407. "0715 Task Mgmt No Failure\n");
  4408. ret = SUCCESS;
  4409. break;
  4410. case RSP_TM_NOT_SUPPORTED: /* TM rejected */
  4411. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4412. "0716 Task Mgmt Target "
  4413. "reject\n");
  4414. break;
  4415. case RSP_TM_NOT_COMPLETED: /* TM failed */
  4416. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4417. "0717 Task Mgmt Target "
  4418. "failed TM\n");
  4419. break;
  4420. case RSP_TM_INVALID_LU: /* TM to invalid LU! */
  4421. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4422. "0718 Task Mgmt to invalid "
  4423. "LUN\n");
  4424. break;
  4425. }
  4426. }
  4427. }
  4428. return ret;
  4429. }
  4430. /**
  4431. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  4432. * @vport: The virtual port for which this call is being executed.
  4433. * @rdata: Pointer to remote port local data
  4434. * @tgt_id: Target ID of remote device.
  4435. * @lun_id: Lun number for the TMF
  4436. * @task_mgmt_cmd: type of TMF to send
  4437. *
  4438. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  4439. * a remote port.
  4440. *
  4441. * Return Code:
  4442. * 0x2003 - Error
  4443. * 0x2002 - Success.
  4444. **/
  4445. static int
  4446. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
  4447. unsigned tgt_id, uint64_t lun_id,
  4448. uint8_t task_mgmt_cmd)
  4449. {
  4450. struct lpfc_hba *phba = vport->phba;
  4451. struct lpfc_scsi_buf *lpfc_cmd;
  4452. struct lpfc_iocbq *iocbq;
  4453. struct lpfc_iocbq *iocbqrsp;
  4454. struct lpfc_nodelist *pnode = rdata->pnode;
  4455. int ret;
  4456. int status;
  4457. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  4458. return FAILED;
  4459. lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
  4460. if (lpfc_cmd == NULL)
  4461. return FAILED;
  4462. lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
  4463. lpfc_cmd->rdata = rdata;
  4464. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  4465. task_mgmt_cmd);
  4466. if (!status) {
  4467. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4468. return FAILED;
  4469. }
  4470. iocbq = &lpfc_cmd->cur_iocbq;
  4471. iocbqrsp = lpfc_sli_get_iocbq(phba);
  4472. if (iocbqrsp == NULL) {
  4473. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4474. return FAILED;
  4475. }
  4476. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  4477. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4478. "0702 Issue %s to TGT %d LUN %llu "
  4479. "rpi x%x nlp_flag x%x Data: x%x x%x\n",
  4480. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  4481. pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
  4482. iocbq->iocb_flag);
  4483. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  4484. iocbq, iocbqrsp, lpfc_cmd->timeout);
  4485. if ((status != IOCB_SUCCESS) ||
  4486. (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
  4487. if (status != IOCB_SUCCESS ||
  4488. iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
  4489. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4490. "0727 TMF %s to TGT %d LUN %llu "
  4491. "failed (%d, %d) iocb_flag x%x\n",
  4492. lpfc_taskmgmt_name(task_mgmt_cmd),
  4493. tgt_id, lun_id,
  4494. iocbqrsp->iocb.ulpStatus,
  4495. iocbqrsp->iocb.un.ulpWord[4],
  4496. iocbq->iocb_flag);
  4497. /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
  4498. if (status == IOCB_SUCCESS) {
  4499. if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  4500. /* Something in the FCP_RSP was invalid.
  4501. * Check conditions */
  4502. ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
  4503. else
  4504. ret = FAILED;
  4505. } else if (status == IOCB_TIMEDOUT) {
  4506. ret = TIMEOUT_ERROR;
  4507. } else {
  4508. ret = FAILED;
  4509. }
  4510. } else
  4511. ret = SUCCESS;
  4512. lpfc_sli_release_iocbq(phba, iocbqrsp);
  4513. if (ret != TIMEOUT_ERROR)
  4514. lpfc_release_scsi_buf(phba, lpfc_cmd);
  4515. return ret;
  4516. }
  4517. /**
  4518. * lpfc_chk_tgt_mapped -
  4519. * @vport: The virtual port to check on
  4520. * @cmnd: Pointer to scsi_cmnd data structure.
  4521. *
  4522. * This routine delays until the scsi target (aka rport) for the
  4523. * command exists (is present and logged in) or we declare it non-existent.
  4524. *
  4525. * Return code :
  4526. * 0x2003 - Error
  4527. * 0x2002 - Success
  4528. **/
  4529. static int
  4530. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
  4531. {
  4532. struct lpfc_rport_data *rdata;
  4533. struct lpfc_nodelist *pnode;
  4534. unsigned long later;
  4535. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4536. if (!rdata) {
  4537. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  4538. "0797 Tgt Map rport failure: rdata x%p\n", rdata);
  4539. return FAILED;
  4540. }
  4541. pnode = rdata->pnode;
  4542. /*
  4543. * If target is not in a MAPPED state, delay until
  4544. * target is rediscovered or devloss timeout expires.
  4545. */
  4546. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  4547. while (time_after(later, jiffies)) {
  4548. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  4549. return FAILED;
  4550. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  4551. return SUCCESS;
  4552. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  4553. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4554. if (!rdata)
  4555. return FAILED;
  4556. pnode = rdata->pnode;
  4557. }
  4558. if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
  4559. (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  4560. return FAILED;
  4561. return SUCCESS;
  4562. }
  4563. /**
  4564. * lpfc_reset_flush_io_context -
  4565. * @vport: The virtual port (scsi_host) for the flush context
  4566. * @tgt_id: If aborting by Target contect - specifies the target id
  4567. * @lun_id: If aborting by Lun context - specifies the lun id
  4568. * @context: specifies the context level to flush at.
  4569. *
  4570. * After a reset condition via TMF, we need to flush orphaned i/o
  4571. * contexts from the adapter. This routine aborts any contexts
  4572. * outstanding, then waits for their completions. The wait is
  4573. * bounded by devloss_tmo though.
  4574. *
  4575. * Return code :
  4576. * 0x2003 - Error
  4577. * 0x2002 - Success
  4578. **/
  4579. static int
  4580. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  4581. uint64_t lun_id, lpfc_ctx_cmd context)
  4582. {
  4583. struct lpfc_hba *phba = vport->phba;
  4584. unsigned long later;
  4585. int cnt;
  4586. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  4587. if (cnt)
  4588. lpfc_sli_abort_taskmgmt(vport,
  4589. &phba->sli.ring[phba->sli.fcp_ring],
  4590. tgt_id, lun_id, context);
  4591. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  4592. while (time_after(later, jiffies) && cnt) {
  4593. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  4594. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  4595. }
  4596. if (cnt) {
  4597. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4598. "0724 I/O flush failure for context %s : cnt x%x\n",
  4599. ((context == LPFC_CTX_LUN) ? "LUN" :
  4600. ((context == LPFC_CTX_TGT) ? "TGT" :
  4601. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  4602. cnt);
  4603. return FAILED;
  4604. }
  4605. return SUCCESS;
  4606. }
  4607. /**
  4608. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  4609. * @cmnd: Pointer to scsi_cmnd data structure.
  4610. *
  4611. * This routine does a device reset by sending a LUN_RESET task management
  4612. * command.
  4613. *
  4614. * Return code :
  4615. * 0x2003 - Error
  4616. * 0x2002 - Success
  4617. **/
  4618. static int
  4619. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  4620. {
  4621. struct Scsi_Host *shost = cmnd->device->host;
  4622. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4623. struct lpfc_rport_data *rdata;
  4624. struct lpfc_nodelist *pnode;
  4625. unsigned tgt_id = cmnd->device->id;
  4626. uint64_t lun_id = cmnd->device->lun;
  4627. struct lpfc_scsi_event_header scsi_event;
  4628. int status;
  4629. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4630. if (!rdata || !rdata->pnode) {
  4631. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4632. "0798 Device Reset rport failure: rdata x%p\n",
  4633. rdata);
  4634. return FAILED;
  4635. }
  4636. pnode = rdata->pnode;
  4637. status = fc_block_scsi_eh(cmnd);
  4638. if (status != 0 && status != SUCCESS)
  4639. return status;
  4640. status = lpfc_chk_tgt_mapped(vport, cmnd);
  4641. if (status == FAILED) {
  4642. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4643. "0721 Device Reset rport failure: rdata x%p\n", rdata);
  4644. return FAILED;
  4645. }
  4646. scsi_event.event_type = FC_REG_SCSI_EVENT;
  4647. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  4648. scsi_event.lun = lun_id;
  4649. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  4650. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  4651. fc_host_post_vendor_event(shost, fc_get_event_number(),
  4652. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  4653. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  4654. FCP_LUN_RESET);
  4655. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4656. "0713 SCSI layer issued Device Reset (%d, %llu) "
  4657. "return x%x\n", tgt_id, lun_id, status);
  4658. /*
  4659. * We have to clean up i/o as : they may be orphaned by the TMF;
  4660. * or if the TMF failed, they may be in an indeterminate state.
  4661. * So, continue on.
  4662. * We will report success if all the i/o aborts successfully.
  4663. */
  4664. if (status == SUCCESS)
  4665. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  4666. LPFC_CTX_LUN);
  4667. return status;
  4668. }
  4669. /**
  4670. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  4671. * @cmnd: Pointer to scsi_cmnd data structure.
  4672. *
  4673. * This routine does a target reset by sending a TARGET_RESET task management
  4674. * command.
  4675. *
  4676. * Return code :
  4677. * 0x2003 - Error
  4678. * 0x2002 - Success
  4679. **/
  4680. static int
  4681. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  4682. {
  4683. struct Scsi_Host *shost = cmnd->device->host;
  4684. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4685. struct lpfc_rport_data *rdata;
  4686. struct lpfc_nodelist *pnode;
  4687. unsigned tgt_id = cmnd->device->id;
  4688. uint64_t lun_id = cmnd->device->lun;
  4689. struct lpfc_scsi_event_header scsi_event;
  4690. int status;
  4691. rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
  4692. if (!rdata) {
  4693. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4694. "0799 Target Reset rport failure: rdata x%p\n", rdata);
  4695. return FAILED;
  4696. }
  4697. pnode = rdata->pnode;
  4698. status = fc_block_scsi_eh(cmnd);
  4699. if (status != 0 && status != SUCCESS)
  4700. return status;
  4701. status = lpfc_chk_tgt_mapped(vport, cmnd);
  4702. if (status == FAILED) {
  4703. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4704. "0722 Target Reset rport failure: rdata x%p\n", rdata);
  4705. if (pnode) {
  4706. spin_lock_irq(shost->host_lock);
  4707. pnode->nlp_flag &= ~NLP_NPR_ADISC;
  4708. pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  4709. spin_unlock_irq(shost->host_lock);
  4710. }
  4711. lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  4712. LPFC_CTX_TGT);
  4713. return FAST_IO_FAIL;
  4714. }
  4715. scsi_event.event_type = FC_REG_SCSI_EVENT;
  4716. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  4717. scsi_event.lun = 0;
  4718. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  4719. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  4720. fc_host_post_vendor_event(shost, fc_get_event_number(),
  4721. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  4722. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  4723. FCP_TARGET_RESET);
  4724. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4725. "0723 SCSI layer issued Target Reset (%d, %llu) "
  4726. "return x%x\n", tgt_id, lun_id, status);
  4727. /*
  4728. * We have to clean up i/o as : they may be orphaned by the TMF;
  4729. * or if the TMF failed, they may be in an indeterminate state.
  4730. * So, continue on.
  4731. * We will report success if all the i/o aborts successfully.
  4732. */
  4733. if (status == SUCCESS)
  4734. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  4735. LPFC_CTX_TGT);
  4736. return status;
  4737. }
  4738. /**
  4739. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  4740. * @cmnd: Pointer to scsi_cmnd data structure.
  4741. *
  4742. * This routine does target reset to all targets on @cmnd->device->host.
  4743. * This emulates Parallel SCSI Bus Reset Semantics.
  4744. *
  4745. * Return code :
  4746. * 0x2003 - Error
  4747. * 0x2002 - Success
  4748. **/
  4749. static int
  4750. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  4751. {
  4752. struct Scsi_Host *shost = cmnd->device->host;
  4753. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4754. struct lpfc_nodelist *ndlp = NULL;
  4755. struct lpfc_scsi_event_header scsi_event;
  4756. int match;
  4757. int ret = SUCCESS, status, i;
  4758. scsi_event.event_type = FC_REG_SCSI_EVENT;
  4759. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  4760. scsi_event.lun = 0;
  4761. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  4762. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  4763. fc_host_post_vendor_event(shost, fc_get_event_number(),
  4764. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  4765. status = fc_block_scsi_eh(cmnd);
  4766. if (status != 0 && status != SUCCESS)
  4767. return status;
  4768. /*
  4769. * Since the driver manages a single bus device, reset all
  4770. * targets known to the driver. Should any target reset
  4771. * fail, this routine returns failure to the midlayer.
  4772. */
  4773. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  4774. /* Search for mapped node by target ID */
  4775. match = 0;
  4776. spin_lock_irq(shost->host_lock);
  4777. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  4778. if (!NLP_CHK_NODE_ACT(ndlp))
  4779. continue;
  4780. if (vport->phba->cfg_fcp2_no_tgt_reset &&
  4781. (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
  4782. continue;
  4783. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  4784. ndlp->nlp_sid == i &&
  4785. ndlp->rport) {
  4786. match = 1;
  4787. break;
  4788. }
  4789. }
  4790. spin_unlock_irq(shost->host_lock);
  4791. if (!match)
  4792. continue;
  4793. status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
  4794. i, 0, FCP_TARGET_RESET);
  4795. if (status != SUCCESS) {
  4796. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4797. "0700 Bus Reset on target %d failed\n",
  4798. i);
  4799. ret = FAILED;
  4800. }
  4801. }
  4802. /*
  4803. * We have to clean up i/o as : they may be orphaned by the TMFs
  4804. * above; or if any of the TMFs failed, they may be in an
  4805. * indeterminate state.
  4806. * We will report success if all the i/o aborts successfully.
  4807. */
  4808. status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
  4809. if (status != SUCCESS)
  4810. ret = FAILED;
  4811. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4812. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  4813. return ret;
  4814. }
  4815. /**
  4816. * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
  4817. * @cmnd: Pointer to scsi_cmnd data structure.
  4818. *
  4819. * This routine does host reset to the adaptor port. It brings the HBA
  4820. * offline, performs a board restart, and then brings the board back online.
  4821. * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
  4822. * reject all outstanding SCSI commands to the host and error returned
  4823. * back to SCSI mid-level. As this will be SCSI mid-level's last resort
  4824. * of error handling, it will only return error if resetting of the adapter
  4825. * is not successful; in all other cases, will return success.
  4826. *
  4827. * Return code :
  4828. * 0x2003 - Error
  4829. * 0x2002 - Success
  4830. **/
  4831. static int
  4832. lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
  4833. {
  4834. struct Scsi_Host *shost = cmnd->device->host;
  4835. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  4836. struct lpfc_hba *phba = vport->phba;
  4837. int rc, ret = SUCCESS;
  4838. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4839. "3172 SCSI layer issued Host Reset Data:\n");
  4840. lpfc_offline_prep(phba, LPFC_MBX_WAIT);
  4841. lpfc_offline(phba);
  4842. rc = lpfc_sli_brdrestart(phba);
  4843. if (rc)
  4844. ret = FAILED;
  4845. rc = lpfc_online(phba);
  4846. if (rc)
  4847. ret = FAILED;
  4848. lpfc_unblock_mgmt_io(phba);
  4849. if (ret == FAILED) {
  4850. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4851. "3323 Failed host reset, bring it offline\n");
  4852. lpfc_sli4_offline_eratt(phba);
  4853. }
  4854. return ret;
  4855. }
  4856. /**
  4857. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  4858. * @sdev: Pointer to scsi_device.
  4859. *
  4860. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  4861. * globally available list of scsi buffers. This routine also makes sure scsi
  4862. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  4863. * of scsi buffer exists for the lifetime of the driver.
  4864. *
  4865. * Return codes:
  4866. * non-0 - Error
  4867. * 0 - Success
  4868. **/
  4869. static int
  4870. lpfc_slave_alloc(struct scsi_device *sdev)
  4871. {
  4872. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  4873. struct lpfc_hba *phba = vport->phba;
  4874. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  4875. uint32_t total = 0;
  4876. uint32_t num_to_alloc = 0;
  4877. int num_allocated = 0;
  4878. uint32_t sdev_cnt;
  4879. struct lpfc_device_data *device_data;
  4880. unsigned long flags;
  4881. struct lpfc_name target_wwpn;
  4882. if (!rport || fc_remote_port_chkready(rport))
  4883. return -ENXIO;
  4884. if (phba->cfg_fof) {
  4885. /*
  4886. * Check to see if the device data structure for the lun
  4887. * exists. If not, create one.
  4888. */
  4889. u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
  4890. spin_lock_irqsave(&phba->devicelock, flags);
  4891. device_data = __lpfc_get_device_data(phba,
  4892. &phba->luns,
  4893. &vport->fc_portname,
  4894. &target_wwpn,
  4895. sdev->lun);
  4896. if (!device_data) {
  4897. spin_unlock_irqrestore(&phba->devicelock, flags);
  4898. device_data = lpfc_create_device_data(phba,
  4899. &vport->fc_portname,
  4900. &target_wwpn,
  4901. sdev->lun, true);
  4902. if (!device_data)
  4903. return -ENOMEM;
  4904. spin_lock_irqsave(&phba->devicelock, flags);
  4905. list_add_tail(&device_data->listentry, &phba->luns);
  4906. }
  4907. device_data->rport_data = rport->dd_data;
  4908. device_data->available = true;
  4909. spin_unlock_irqrestore(&phba->devicelock, flags);
  4910. sdev->hostdata = device_data;
  4911. } else {
  4912. sdev->hostdata = rport->dd_data;
  4913. }
  4914. sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
  4915. /*
  4916. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  4917. * available list of scsi buffers. Don't allocate more than the
  4918. * HBA limit conveyed to the midlayer via the host structure. The
  4919. * formula accounts for the lun_queue_depth + error handlers + 1
  4920. * extra. This list of scsi bufs exists for the lifetime of the driver.
  4921. */
  4922. total = phba->total_scsi_bufs;
  4923. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  4924. /* If allocated buffers are enough do nothing */
  4925. if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
  4926. return 0;
  4927. /* Allow some exchanges to be available always to complete discovery */
  4928. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  4929. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4930. "0704 At limitation of %d preallocated "
  4931. "command buffers\n", total);
  4932. return 0;
  4933. /* Allow some exchanges to be available always to complete discovery */
  4934. } else if (total + num_to_alloc >
  4935. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  4936. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  4937. "0705 Allocation request of %d "
  4938. "command buffers will exceed max of %d. "
  4939. "Reducing allocation request to %d.\n",
  4940. num_to_alloc, phba->cfg_hba_queue_depth,
  4941. (phba->cfg_hba_queue_depth - total));
  4942. num_to_alloc = phba->cfg_hba_queue_depth - total;
  4943. }
  4944. num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
  4945. if (num_to_alloc != num_allocated) {
  4946. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  4947. "0708 Allocation request of %d "
  4948. "command buffers did not succeed. "
  4949. "Allocated %d buffers.\n",
  4950. num_to_alloc, num_allocated);
  4951. }
  4952. if (num_allocated > 0)
  4953. phba->total_scsi_bufs += num_allocated;
  4954. return 0;
  4955. }
  4956. /**
  4957. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  4958. * @sdev: Pointer to scsi_device.
  4959. *
  4960. * This routine configures following items
  4961. * - Tag command queuing support for @sdev if supported.
  4962. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  4963. *
  4964. * Return codes:
  4965. * 0 - Success
  4966. **/
  4967. static int
  4968. lpfc_slave_configure(struct scsi_device *sdev)
  4969. {
  4970. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  4971. struct lpfc_hba *phba = vport->phba;
  4972. scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
  4973. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  4974. lpfc_sli_handle_fast_ring_event(phba,
  4975. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  4976. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  4977. lpfc_poll_rearm_timer(phba);
  4978. }
  4979. return 0;
  4980. }
  4981. /**
  4982. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  4983. * @sdev: Pointer to scsi_device.
  4984. *
  4985. * This routine sets @sdev hostatdata filed to null.
  4986. **/
  4987. static void
  4988. lpfc_slave_destroy(struct scsi_device *sdev)
  4989. {
  4990. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  4991. struct lpfc_hba *phba = vport->phba;
  4992. unsigned long flags;
  4993. struct lpfc_device_data *device_data = sdev->hostdata;
  4994. atomic_dec(&phba->sdev_cnt);
  4995. if ((phba->cfg_fof) && (device_data)) {
  4996. spin_lock_irqsave(&phba->devicelock, flags);
  4997. device_data->available = false;
  4998. if (!device_data->oas_enabled)
  4999. lpfc_delete_device_data(phba, device_data);
  5000. spin_unlock_irqrestore(&phba->devicelock, flags);
  5001. }
  5002. sdev->hostdata = NULL;
  5003. return;
  5004. }
  5005. /**
  5006. * lpfc_create_device_data - creates and initializes device data structure for OAS
  5007. * @pha: Pointer to host bus adapter structure.
  5008. * @vport_wwpn: Pointer to vport's wwpn information
  5009. * @target_wwpn: Pointer to target's wwpn information
  5010. * @lun: Lun on target
  5011. * @atomic_create: Flag to indicate if memory should be allocated using the
  5012. * GFP_ATOMIC flag or not.
  5013. *
  5014. * This routine creates a device data structure which will contain identifying
  5015. * information for the device (host wwpn, target wwpn, lun), state of OAS,
  5016. * whether or not the corresponding lun is available by the system,
  5017. * and pointer to the rport data.
  5018. *
  5019. * Return codes:
  5020. * NULL - Error
  5021. * Pointer to lpfc_device_data - Success
  5022. **/
  5023. struct lpfc_device_data*
  5024. lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5025. struct lpfc_name *target_wwpn, uint64_t lun,
  5026. bool atomic_create)
  5027. {
  5028. struct lpfc_device_data *lun_info;
  5029. int memory_flags;
  5030. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5031. !(phba->cfg_fof))
  5032. return NULL;
  5033. /* Attempt to create the device data to contain lun info */
  5034. if (atomic_create)
  5035. memory_flags = GFP_ATOMIC;
  5036. else
  5037. memory_flags = GFP_KERNEL;
  5038. lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
  5039. if (!lun_info)
  5040. return NULL;
  5041. INIT_LIST_HEAD(&lun_info->listentry);
  5042. lun_info->rport_data = NULL;
  5043. memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5044. sizeof(struct lpfc_name));
  5045. memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
  5046. sizeof(struct lpfc_name));
  5047. lun_info->device_id.lun = lun;
  5048. lun_info->oas_enabled = false;
  5049. lun_info->priority = phba->cfg_XLanePriority;
  5050. lun_info->available = false;
  5051. return lun_info;
  5052. }
  5053. /**
  5054. * lpfc_delete_device_data - frees a device data structure for OAS
  5055. * @pha: Pointer to host bus adapter structure.
  5056. * @lun_info: Pointer to device data structure to free.
  5057. *
  5058. * This routine frees the previously allocated device data structure passed.
  5059. *
  5060. **/
  5061. void
  5062. lpfc_delete_device_data(struct lpfc_hba *phba,
  5063. struct lpfc_device_data *lun_info)
  5064. {
  5065. if (unlikely(!phba) || !lun_info ||
  5066. !(phba->cfg_fof))
  5067. return;
  5068. if (!list_empty(&lun_info->listentry))
  5069. list_del(&lun_info->listentry);
  5070. mempool_free(lun_info, phba->device_data_mem_pool);
  5071. return;
  5072. }
  5073. /**
  5074. * __lpfc_get_device_data - returns the device data for the specified lun
  5075. * @pha: Pointer to host bus adapter structure.
  5076. * @list: Point to list to search.
  5077. * @vport_wwpn: Pointer to vport's wwpn information
  5078. * @target_wwpn: Pointer to target's wwpn information
  5079. * @lun: Lun on target
  5080. *
  5081. * This routine searches the list passed for the specified lun's device data.
  5082. * This function does not hold locks, it is the responsibility of the caller
  5083. * to ensure the proper lock is held before calling the function.
  5084. *
  5085. * Return codes:
  5086. * NULL - Error
  5087. * Pointer to lpfc_device_data - Success
  5088. **/
  5089. struct lpfc_device_data*
  5090. __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
  5091. struct lpfc_name *vport_wwpn,
  5092. struct lpfc_name *target_wwpn, uint64_t lun)
  5093. {
  5094. struct lpfc_device_data *lun_info;
  5095. if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
  5096. !phba->cfg_fof)
  5097. return NULL;
  5098. /* Check to see if the lun is already enabled for OAS. */
  5099. list_for_each_entry(lun_info, list, listentry) {
  5100. if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5101. sizeof(struct lpfc_name)) == 0) &&
  5102. (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
  5103. sizeof(struct lpfc_name)) == 0) &&
  5104. (lun_info->device_id.lun == lun))
  5105. return lun_info;
  5106. }
  5107. return NULL;
  5108. }
  5109. /**
  5110. * lpfc_find_next_oas_lun - searches for the next oas lun
  5111. * @pha: Pointer to host bus adapter structure.
  5112. * @vport_wwpn: Pointer to vport's wwpn information
  5113. * @target_wwpn: Pointer to target's wwpn information
  5114. * @starting_lun: Pointer to the lun to start searching for
  5115. * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
  5116. * @found_target_wwpn: Pointer to the found lun's target wwpn information
  5117. * @found_lun: Pointer to the found lun.
  5118. * @found_lun_status: Pointer to status of the found lun.
  5119. *
  5120. * This routine searches the luns list for the specified lun
  5121. * or the first lun for the vport/target. If the vport wwpn contains
  5122. * a zero value then a specific vport is not specified. In this case
  5123. * any vport which contains the lun will be considered a match. If the
  5124. * target wwpn contains a zero value then a specific target is not specified.
  5125. * In this case any target which contains the lun will be considered a
  5126. * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
  5127. * are returned. The function will also return the next lun if available.
  5128. * If the next lun is not found, starting_lun parameter will be set to
  5129. * NO_MORE_OAS_LUN.
  5130. *
  5131. * Return codes:
  5132. * non-0 - Error
  5133. * 0 - Success
  5134. **/
  5135. bool
  5136. lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5137. struct lpfc_name *target_wwpn, uint64_t *starting_lun,
  5138. struct lpfc_name *found_vport_wwpn,
  5139. struct lpfc_name *found_target_wwpn,
  5140. uint64_t *found_lun,
  5141. uint32_t *found_lun_status)
  5142. {
  5143. unsigned long flags;
  5144. struct lpfc_device_data *lun_info;
  5145. struct lpfc_device_id *device_id;
  5146. uint64_t lun;
  5147. bool found = false;
  5148. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5149. !starting_lun || !found_vport_wwpn ||
  5150. !found_target_wwpn || !found_lun || !found_lun_status ||
  5151. (*starting_lun == NO_MORE_OAS_LUN) ||
  5152. !phba->cfg_fof)
  5153. return false;
  5154. lun = *starting_lun;
  5155. *found_lun = NO_MORE_OAS_LUN;
  5156. *starting_lun = NO_MORE_OAS_LUN;
  5157. /* Search for lun or the lun closet in value */
  5158. spin_lock_irqsave(&phba->devicelock, flags);
  5159. list_for_each_entry(lun_info, &phba->luns, listentry) {
  5160. if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
  5161. (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
  5162. sizeof(struct lpfc_name)) == 0)) &&
  5163. ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
  5164. (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
  5165. sizeof(struct lpfc_name)) == 0)) &&
  5166. (lun_info->oas_enabled)) {
  5167. device_id = &lun_info->device_id;
  5168. if ((!found) &&
  5169. ((lun == FIND_FIRST_OAS_LUN) ||
  5170. (device_id->lun == lun))) {
  5171. *found_lun = device_id->lun;
  5172. memcpy(found_vport_wwpn,
  5173. &device_id->vport_wwpn,
  5174. sizeof(struct lpfc_name));
  5175. memcpy(found_target_wwpn,
  5176. &device_id->target_wwpn,
  5177. sizeof(struct lpfc_name));
  5178. if (lun_info->available)
  5179. *found_lun_status =
  5180. OAS_LUN_STATUS_EXISTS;
  5181. else
  5182. *found_lun_status = 0;
  5183. if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
  5184. memset(vport_wwpn, 0x0,
  5185. sizeof(struct lpfc_name));
  5186. if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
  5187. memset(target_wwpn, 0x0,
  5188. sizeof(struct lpfc_name));
  5189. found = true;
  5190. } else if (found) {
  5191. *starting_lun = device_id->lun;
  5192. memcpy(vport_wwpn, &device_id->vport_wwpn,
  5193. sizeof(struct lpfc_name));
  5194. memcpy(target_wwpn, &device_id->target_wwpn,
  5195. sizeof(struct lpfc_name));
  5196. break;
  5197. }
  5198. }
  5199. }
  5200. spin_unlock_irqrestore(&phba->devicelock, flags);
  5201. return found;
  5202. }
  5203. /**
  5204. * lpfc_enable_oas_lun - enables a lun for OAS operations
  5205. * @pha: Pointer to host bus adapter structure.
  5206. * @vport_wwpn: Pointer to vport's wwpn information
  5207. * @target_wwpn: Pointer to target's wwpn information
  5208. * @lun: Lun
  5209. *
  5210. * This routine enables a lun for oas operations. The routines does so by
  5211. * doing the following :
  5212. *
  5213. * 1) Checks to see if the device data for the lun has been created.
  5214. * 2) If found, sets the OAS enabled flag if not set and returns.
  5215. * 3) Otherwise, creates a device data structure.
  5216. * 4) If successfully created, indicates the device data is for an OAS lun,
  5217. * indicates the lun is not available and add to the list of luns.
  5218. *
  5219. * Return codes:
  5220. * false - Error
  5221. * true - Success
  5222. **/
  5223. bool
  5224. lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5225. struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
  5226. {
  5227. struct lpfc_device_data *lun_info;
  5228. unsigned long flags;
  5229. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5230. !phba->cfg_fof)
  5231. return false;
  5232. spin_lock_irqsave(&phba->devicelock, flags);
  5233. /* Check to see if the device data for the lun has been created */
  5234. lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
  5235. target_wwpn, lun);
  5236. if (lun_info) {
  5237. if (!lun_info->oas_enabled)
  5238. lun_info->oas_enabled = true;
  5239. spin_unlock_irqrestore(&phba->devicelock, flags);
  5240. return true;
  5241. }
  5242. /* Create an lun info structure and add to list of luns */
  5243. lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
  5244. false);
  5245. if (lun_info) {
  5246. lun_info->oas_enabled = true;
  5247. lun_info->priority = pri;
  5248. lun_info->available = false;
  5249. list_add_tail(&lun_info->listentry, &phba->luns);
  5250. spin_unlock_irqrestore(&phba->devicelock, flags);
  5251. return true;
  5252. }
  5253. spin_unlock_irqrestore(&phba->devicelock, flags);
  5254. return false;
  5255. }
  5256. /**
  5257. * lpfc_disable_oas_lun - disables a lun for OAS operations
  5258. * @pha: Pointer to host bus adapter structure.
  5259. * @vport_wwpn: Pointer to vport's wwpn information
  5260. * @target_wwpn: Pointer to target's wwpn information
  5261. * @lun: Lun
  5262. *
  5263. * This routine disables a lun for oas operations. The routines does so by
  5264. * doing the following :
  5265. *
  5266. * 1) Checks to see if the device data for the lun is created.
  5267. * 2) If present, clears the flag indicating this lun is for OAS.
  5268. * 3) If the lun is not available by the system, the device data is
  5269. * freed.
  5270. *
  5271. * Return codes:
  5272. * false - Error
  5273. * true - Success
  5274. **/
  5275. bool
  5276. lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
  5277. struct lpfc_name *target_wwpn, uint64_t lun)
  5278. {
  5279. struct lpfc_device_data *lun_info;
  5280. unsigned long flags;
  5281. if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
  5282. !phba->cfg_fof)
  5283. return false;
  5284. spin_lock_irqsave(&phba->devicelock, flags);
  5285. /* Check to see if the lun is available. */
  5286. lun_info = __lpfc_get_device_data(phba,
  5287. &phba->luns, vport_wwpn,
  5288. target_wwpn, lun);
  5289. if (lun_info) {
  5290. lun_info->oas_enabled = false;
  5291. if (!lun_info->available)
  5292. lpfc_delete_device_data(phba, lun_info);
  5293. spin_unlock_irqrestore(&phba->devicelock, flags);
  5294. return true;
  5295. }
  5296. spin_unlock_irqrestore(&phba->devicelock, flags);
  5297. return false;
  5298. }
  5299. struct scsi_host_template lpfc_template_s3 = {
  5300. .module = THIS_MODULE,
  5301. .name = LPFC_DRIVER_NAME,
  5302. .proc_name = LPFC_DRIVER_NAME,
  5303. .info = lpfc_info,
  5304. .queuecommand = lpfc_queuecommand,
  5305. .eh_abort_handler = lpfc_abort_handler,
  5306. .eh_device_reset_handler = lpfc_device_reset_handler,
  5307. .eh_target_reset_handler = lpfc_target_reset_handler,
  5308. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  5309. .slave_alloc = lpfc_slave_alloc,
  5310. .slave_configure = lpfc_slave_configure,
  5311. .slave_destroy = lpfc_slave_destroy,
  5312. .scan_finished = lpfc_scan_finished,
  5313. .this_id = -1,
  5314. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  5315. .cmd_per_lun = LPFC_CMD_PER_LUN,
  5316. .use_clustering = ENABLE_CLUSTERING,
  5317. .shost_attrs = lpfc_hba_attrs,
  5318. .max_sectors = 0xFFFF,
  5319. .vendor_id = LPFC_NL_VENDOR_ID,
  5320. .change_queue_depth = scsi_change_queue_depth,
  5321. .track_queue_depth = 1,
  5322. };
  5323. struct scsi_host_template lpfc_template = {
  5324. .module = THIS_MODULE,
  5325. .name = LPFC_DRIVER_NAME,
  5326. .proc_name = LPFC_DRIVER_NAME,
  5327. .info = lpfc_info,
  5328. .queuecommand = lpfc_queuecommand,
  5329. .eh_abort_handler = lpfc_abort_handler,
  5330. .eh_device_reset_handler = lpfc_device_reset_handler,
  5331. .eh_target_reset_handler = lpfc_target_reset_handler,
  5332. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  5333. .eh_host_reset_handler = lpfc_host_reset_handler,
  5334. .slave_alloc = lpfc_slave_alloc,
  5335. .slave_configure = lpfc_slave_configure,
  5336. .slave_destroy = lpfc_slave_destroy,
  5337. .scan_finished = lpfc_scan_finished,
  5338. .this_id = -1,
  5339. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  5340. .cmd_per_lun = LPFC_CMD_PER_LUN,
  5341. .use_clustering = ENABLE_CLUSTERING,
  5342. .shost_attrs = lpfc_hba_attrs,
  5343. .max_sectors = 0xFFFF,
  5344. .vendor_id = LPFC_NL_VENDOR_ID,
  5345. .change_queue_depth = scsi_change_queue_depth,
  5346. .track_queue_depth = 1,
  5347. };
  5348. struct scsi_host_template lpfc_vport_template = {
  5349. .module = THIS_MODULE,
  5350. .name = LPFC_DRIVER_NAME,
  5351. .proc_name = LPFC_DRIVER_NAME,
  5352. .info = lpfc_info,
  5353. .queuecommand = lpfc_queuecommand,
  5354. .eh_abort_handler = lpfc_abort_handler,
  5355. .eh_device_reset_handler = lpfc_device_reset_handler,
  5356. .eh_target_reset_handler = lpfc_target_reset_handler,
  5357. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  5358. .slave_alloc = lpfc_slave_alloc,
  5359. .slave_configure = lpfc_slave_configure,
  5360. .slave_destroy = lpfc_slave_destroy,
  5361. .scan_finished = lpfc_scan_finished,
  5362. .this_id = -1,
  5363. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  5364. .cmd_per_lun = LPFC_CMD_PER_LUN,
  5365. .use_clustering = ENABLE_CLUSTERING,
  5366. .shost_attrs = lpfc_vport_attrs,
  5367. .max_sectors = 0xFFFF,
  5368. .change_queue_depth = scsi_change_queue_depth,
  5369. .track_queue_depth = 1,
  5370. };