lpfc_scsi.c 167 KB

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