lpfc_scsi.c 154 KB

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