lpfc_scsi.c 165 KB

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