lpfc_scsi.c 165 KB

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