lpfc_scsi.c 152 KB

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