lpfc_scsi.c 165 KB

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