lpfc_scsi.c 165 KB

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