lpfc_scsi.c 160 KB

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