lpfc_scsi.c 164 KB

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