lpfc_sli.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_nl.h"
  33. #include "lpfc_disc.h"
  34. #include "lpfc_scsi.h"
  35. #include "lpfc.h"
  36. #include "lpfc_crtn.h"
  37. #include "lpfc_logmsg.h"
  38. #include "lpfc_compat.h"
  39. #include "lpfc_debugfs.h"
  40. /*
  41. * Define macro to log: Mailbox command x%x cannot issue Data
  42. * This allows multiple uses of lpfc_msgBlk0311
  43. * w/o perturbing log msg utility.
  44. */
  45. #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
  46. lpfc_printf_log(phba, \
  47. KERN_INFO, \
  48. LOG_MBOX | LOG_SLI, \
  49. "(%d):0311 Mailbox command x%x cannot " \
  50. "issue Data: x%x x%x x%x\n", \
  51. pmbox->vport ? pmbox->vport->vpi : 0, \
  52. pmbox->mb.mbxCommand, \
  53. phba->pport->port_state, \
  54. psli->sli_flag, \
  55. flag)
  56. /* There are only four IOCB completion types. */
  57. typedef enum _lpfc_iocb_type {
  58. LPFC_UNKNOWN_IOCB,
  59. LPFC_UNSOL_IOCB,
  60. LPFC_SOL_IOCB,
  61. LPFC_ABORT_IOCB
  62. } lpfc_iocb_type;
  63. /**
  64. * lpfc_cmd_iocb: Get next command iocb entry in the ring.
  65. * @phba: Pointer to HBA context object.
  66. * @pring: Pointer to driver SLI ring object.
  67. *
  68. * This function returns pointer to next command iocb entry
  69. * in the command ring. The caller must hold hbalock to prevent
  70. * other threads consume the next command iocb.
  71. * SLI-2/SLI-3 provide different sized iocbs.
  72. **/
  73. static inline IOCB_t *
  74. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  75. {
  76. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  77. pring->cmdidx * phba->iocb_cmd_size);
  78. }
  79. /**
  80. * lpfc_resp_iocb: Get next response iocb entry in the ring.
  81. * @phba: Pointer to HBA context object.
  82. * @pring: Pointer to driver SLI ring object.
  83. *
  84. * This function returns pointer to next response iocb entry
  85. * in the response ring. The caller must hold hbalock to make sure
  86. * that no other thread consume the next response iocb.
  87. * SLI-2/SLI-3 provide different sized iocbs.
  88. **/
  89. static inline IOCB_t *
  90. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  91. {
  92. return (IOCB_t *) (((char *) pring->rspringaddr) +
  93. pring->rspidx * phba->iocb_rsp_size);
  94. }
  95. /**
  96. * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  97. * @phba: Pointer to HBA context object.
  98. *
  99. * This function is called with hbalock held. This function
  100. * allocates a new driver iocb object from the iocb pool. If the
  101. * allocation is successful, it returns pointer to the newly
  102. * allocated iocb object else it returns NULL.
  103. **/
  104. static struct lpfc_iocbq *
  105. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  106. {
  107. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  108. struct lpfc_iocbq * iocbq = NULL;
  109. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  110. return iocbq;
  111. }
  112. /**
  113. * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  114. * @phba: Pointer to HBA context object.
  115. *
  116. * This function is called with no lock held. This function
  117. * allocates a new driver iocb object from the iocb pool. If the
  118. * allocation is successful, it returns pointer to the newly
  119. * allocated iocb object else it returns NULL.
  120. **/
  121. struct lpfc_iocbq *
  122. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  123. {
  124. struct lpfc_iocbq * iocbq = NULL;
  125. unsigned long iflags;
  126. spin_lock_irqsave(&phba->hbalock, iflags);
  127. iocbq = __lpfc_sli_get_iocbq(phba);
  128. spin_unlock_irqrestore(&phba->hbalock, iflags);
  129. return iocbq;
  130. }
  131. /**
  132. * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  133. * @phba: Pointer to HBA context object.
  134. * @iocbq: Pointer to driver iocb object.
  135. *
  136. * This function is called with hbalock held to release driver
  137. * iocb object to the iocb pool. The iotag in the iocb object
  138. * does not change for each use of the iocb object. This function
  139. * clears all other fields of the iocb object when it is freed.
  140. **/
  141. static void
  142. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  143. {
  144. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  145. /*
  146. * Clean all volatile data fields, preserve iotag and node struct.
  147. */
  148. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  149. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  150. }
  151. /**
  152. * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  153. * @phba: Pointer to HBA context object.
  154. * @iocbq: Pointer to driver iocb object.
  155. *
  156. * This function is called with no lock held to release the iocb to
  157. * iocb pool.
  158. **/
  159. void
  160. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  161. {
  162. unsigned long iflags;
  163. /*
  164. * Clean all volatile data fields, preserve iotag and node struct.
  165. */
  166. spin_lock_irqsave(&phba->hbalock, iflags);
  167. __lpfc_sli_release_iocbq(phba, iocbq);
  168. spin_unlock_irqrestore(&phba->hbalock, iflags);
  169. }
  170. /**
  171. * lpfc_sli_iocb_cmd_type: Get the iocb type.
  172. * @iocb_cmnd : iocb command code.
  173. *
  174. * This function is called by ring event handler function to get the iocb type.
  175. * This function translates the iocb command to an iocb command type used to
  176. * decide the final disposition of each completed IOCB.
  177. * The function returns
  178. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  179. * LPFC_SOL_IOCB if it is a solicited iocb completion
  180. * LPFC_ABORT_IOCB if it is an abort iocb
  181. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  182. *
  183. * The caller is not required to hold any lock.
  184. **/
  185. static lpfc_iocb_type
  186. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  187. {
  188. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  189. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  190. return 0;
  191. switch (iocb_cmnd) {
  192. case CMD_XMIT_SEQUENCE_CR:
  193. case CMD_XMIT_SEQUENCE_CX:
  194. case CMD_XMIT_BCAST_CN:
  195. case CMD_XMIT_BCAST_CX:
  196. case CMD_ELS_REQUEST_CR:
  197. case CMD_ELS_REQUEST_CX:
  198. case CMD_CREATE_XRI_CR:
  199. case CMD_CREATE_XRI_CX:
  200. case CMD_GET_RPI_CN:
  201. case CMD_XMIT_ELS_RSP_CX:
  202. case CMD_GET_RPI_CR:
  203. case CMD_FCP_IWRITE_CR:
  204. case CMD_FCP_IWRITE_CX:
  205. case CMD_FCP_IREAD_CR:
  206. case CMD_FCP_IREAD_CX:
  207. case CMD_FCP_ICMND_CR:
  208. case CMD_FCP_ICMND_CX:
  209. case CMD_FCP_TSEND_CX:
  210. case CMD_FCP_TRSP_CX:
  211. case CMD_FCP_TRECEIVE_CX:
  212. case CMD_FCP_AUTO_TRSP_CX:
  213. case CMD_ADAPTER_MSG:
  214. case CMD_ADAPTER_DUMP:
  215. case CMD_XMIT_SEQUENCE64_CR:
  216. case CMD_XMIT_SEQUENCE64_CX:
  217. case CMD_XMIT_BCAST64_CN:
  218. case CMD_XMIT_BCAST64_CX:
  219. case CMD_ELS_REQUEST64_CR:
  220. case CMD_ELS_REQUEST64_CX:
  221. case CMD_FCP_IWRITE64_CR:
  222. case CMD_FCP_IWRITE64_CX:
  223. case CMD_FCP_IREAD64_CR:
  224. case CMD_FCP_IREAD64_CX:
  225. case CMD_FCP_ICMND64_CR:
  226. case CMD_FCP_ICMND64_CX:
  227. case CMD_FCP_TSEND64_CX:
  228. case CMD_FCP_TRSP64_CX:
  229. case CMD_FCP_TRECEIVE64_CX:
  230. case CMD_GEN_REQUEST64_CR:
  231. case CMD_GEN_REQUEST64_CX:
  232. case CMD_XMIT_ELS_RSP64_CX:
  233. type = LPFC_SOL_IOCB;
  234. break;
  235. case CMD_ABORT_XRI_CN:
  236. case CMD_ABORT_XRI_CX:
  237. case CMD_CLOSE_XRI_CN:
  238. case CMD_CLOSE_XRI_CX:
  239. case CMD_XRI_ABORTED_CX:
  240. case CMD_ABORT_MXRI64_CN:
  241. type = LPFC_ABORT_IOCB;
  242. break;
  243. case CMD_RCV_SEQUENCE_CX:
  244. case CMD_RCV_ELS_REQ_CX:
  245. case CMD_RCV_SEQUENCE64_CX:
  246. case CMD_RCV_ELS_REQ64_CX:
  247. case CMD_ASYNC_STATUS:
  248. case CMD_IOCB_RCV_SEQ64_CX:
  249. case CMD_IOCB_RCV_ELS64_CX:
  250. case CMD_IOCB_RCV_CONT64_CX:
  251. case CMD_IOCB_RET_XRI64_CX:
  252. type = LPFC_UNSOL_IOCB;
  253. break;
  254. case CMD_IOCB_XMIT_MSEQ64_CR:
  255. case CMD_IOCB_XMIT_MSEQ64_CX:
  256. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  257. case CMD_IOCB_RCV_ELS_LIST64_CX:
  258. case CMD_IOCB_CLOSE_EXTENDED_CN:
  259. case CMD_IOCB_ABORT_EXTENDED_CN:
  260. case CMD_IOCB_RET_HBQE64_CN:
  261. case CMD_IOCB_FCP_IBIDIR64_CR:
  262. case CMD_IOCB_FCP_IBIDIR64_CX:
  263. case CMD_IOCB_FCP_ITASKMGT64_CX:
  264. case CMD_IOCB_LOGENTRY_CN:
  265. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  266. printk("%s - Unhandled SLI-3 Command x%x\n",
  267. __func__, iocb_cmnd);
  268. type = LPFC_UNKNOWN_IOCB;
  269. break;
  270. default:
  271. type = LPFC_UNKNOWN_IOCB;
  272. break;
  273. }
  274. return type;
  275. }
  276. /**
  277. * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
  278. * @phba: Pointer to HBA context object.
  279. *
  280. * This function is called from SLI initialization code
  281. * to configure every ring of the HBA's SLI interface. The
  282. * caller is not required to hold any lock. This function issues
  283. * a config_ring mailbox command for each ring.
  284. * This function returns zero if successful else returns a negative
  285. * error code.
  286. **/
  287. static int
  288. lpfc_sli_ring_map(struct lpfc_hba *phba)
  289. {
  290. struct lpfc_sli *psli = &phba->sli;
  291. LPFC_MBOXQ_t *pmb;
  292. MAILBOX_t *pmbox;
  293. int i, rc, ret = 0;
  294. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  295. if (!pmb)
  296. return -ENOMEM;
  297. pmbox = &pmb->mb;
  298. phba->link_state = LPFC_INIT_MBX_CMDS;
  299. for (i = 0; i < psli->num_rings; i++) {
  300. lpfc_config_ring(phba, i, pmb);
  301. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  302. if (rc != MBX_SUCCESS) {
  303. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  304. "0446 Adapter failed to init (%d), "
  305. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  306. "ring %d\n",
  307. rc, pmbox->mbxCommand,
  308. pmbox->mbxStatus, i);
  309. phba->link_state = LPFC_HBA_ERROR;
  310. ret = -ENXIO;
  311. break;
  312. }
  313. }
  314. mempool_free(pmb, phba->mbox_mem_pool);
  315. return ret;
  316. }
  317. /**
  318. * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
  319. * @phba: Pointer to HBA context object.
  320. * @pring: Pointer to driver SLI ring object.
  321. * @piocb: Pointer to the driver iocb object.
  322. *
  323. * This function is called with hbalock held. The function adds the
  324. * new iocb to txcmplq of the given ring. This function always returns
  325. * 0. If this function is called for ELS ring, this function checks if
  326. * there is a vport associated with the ELS command. This function also
  327. * starts els_tmofunc timer if this is an ELS command.
  328. **/
  329. static int
  330. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  331. struct lpfc_iocbq *piocb)
  332. {
  333. list_add_tail(&piocb->list, &pring->txcmplq);
  334. pring->txcmplq_cnt++;
  335. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  336. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  337. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  338. if (!piocb->vport)
  339. BUG();
  340. else
  341. mod_timer(&piocb->vport->els_tmofunc,
  342. jiffies + HZ * (phba->fc_ratov << 1));
  343. }
  344. return 0;
  345. }
  346. /**
  347. * lpfc_sli_ringtx_get: Get first element of the txq.
  348. * @phba: Pointer to HBA context object.
  349. * @pring: Pointer to driver SLI ring object.
  350. *
  351. * This function is called with hbalock held to get next
  352. * iocb in txq of the given ring. If there is any iocb in
  353. * the txq, the function returns first iocb in the list after
  354. * removing the iocb from the list, else it returns NULL.
  355. **/
  356. static struct lpfc_iocbq *
  357. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  358. {
  359. struct lpfc_iocbq *cmd_iocb;
  360. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  361. if (cmd_iocb != NULL)
  362. pring->txq_cnt--;
  363. return cmd_iocb;
  364. }
  365. /**
  366. * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
  367. * @phba: Pointer to HBA context object.
  368. * @pring: Pointer to driver SLI ring object.
  369. *
  370. * This function is called with hbalock held and the caller must post the
  371. * iocb without releasing the lock. If the caller releases the lock,
  372. * iocb slot returned by the function is not guaranteed to be available.
  373. * The function returns pointer to the next available iocb slot if there
  374. * is available slot in the ring, else it returns NULL.
  375. * If the get index of the ring is ahead of the put index, the function
  376. * will post an error attention event to the worker thread to take the
  377. * HBA to offline state.
  378. **/
  379. static IOCB_t *
  380. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  381. {
  382. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  383. uint32_t max_cmd_idx = pring->numCiocb;
  384. if ((pring->next_cmdidx == pring->cmdidx) &&
  385. (++pring->next_cmdidx >= max_cmd_idx))
  386. pring->next_cmdidx = 0;
  387. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  388. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  389. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  391. "0315 Ring %d issue: portCmdGet %d "
  392. "is bigger then cmd ring %d\n",
  393. pring->ringno,
  394. pring->local_getidx, max_cmd_idx);
  395. phba->link_state = LPFC_HBA_ERROR;
  396. /*
  397. * All error attention handlers are posted to
  398. * worker thread
  399. */
  400. phba->work_ha |= HA_ERATT;
  401. phba->work_hs = HS_FFER3;
  402. lpfc_worker_wake_up(phba);
  403. return NULL;
  404. }
  405. if (pring->local_getidx == pring->next_cmdidx)
  406. return NULL;
  407. }
  408. return lpfc_cmd_iocb(phba, pring);
  409. }
  410. /**
  411. * lpfc_sli_next_iotag: Get an iotag for the iocb.
  412. * @phba: Pointer to HBA context object.
  413. * @iocbq: Pointer to driver iocb object.
  414. *
  415. * This function gets an iotag for the iocb. If there is no unused iotag and
  416. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  417. * array and assigns a new iotag.
  418. * The function returns the allocated iotag if successful, else returns zero.
  419. * Zero is not a valid iotag.
  420. * The caller is not required to hold any lock.
  421. **/
  422. uint16_t
  423. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  424. {
  425. struct lpfc_iocbq **new_arr;
  426. struct lpfc_iocbq **old_arr;
  427. size_t new_len;
  428. struct lpfc_sli *psli = &phba->sli;
  429. uint16_t iotag;
  430. spin_lock_irq(&phba->hbalock);
  431. iotag = psli->last_iotag;
  432. if(++iotag < psli->iocbq_lookup_len) {
  433. psli->last_iotag = iotag;
  434. psli->iocbq_lookup[iotag] = iocbq;
  435. spin_unlock_irq(&phba->hbalock);
  436. iocbq->iotag = iotag;
  437. return iotag;
  438. } else if (psli->iocbq_lookup_len < (0xffff
  439. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  440. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  441. spin_unlock_irq(&phba->hbalock);
  442. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  443. GFP_KERNEL);
  444. if (new_arr) {
  445. spin_lock_irq(&phba->hbalock);
  446. old_arr = psli->iocbq_lookup;
  447. if (new_len <= psli->iocbq_lookup_len) {
  448. /* highly unprobable case */
  449. kfree(new_arr);
  450. iotag = psli->last_iotag;
  451. if(++iotag < psli->iocbq_lookup_len) {
  452. psli->last_iotag = iotag;
  453. psli->iocbq_lookup[iotag] = iocbq;
  454. spin_unlock_irq(&phba->hbalock);
  455. iocbq->iotag = iotag;
  456. return iotag;
  457. }
  458. spin_unlock_irq(&phba->hbalock);
  459. return 0;
  460. }
  461. if (psli->iocbq_lookup)
  462. memcpy(new_arr, old_arr,
  463. ((psli->last_iotag + 1) *
  464. sizeof (struct lpfc_iocbq *)));
  465. psli->iocbq_lookup = new_arr;
  466. psli->iocbq_lookup_len = new_len;
  467. psli->last_iotag = iotag;
  468. psli->iocbq_lookup[iotag] = iocbq;
  469. spin_unlock_irq(&phba->hbalock);
  470. iocbq->iotag = iotag;
  471. kfree(old_arr);
  472. return iotag;
  473. }
  474. } else
  475. spin_unlock_irq(&phba->hbalock);
  476. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  477. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  478. psli->last_iotag);
  479. return 0;
  480. }
  481. /**
  482. * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
  483. * @phba: Pointer to HBA context object.
  484. * @pring: Pointer to driver SLI ring object.
  485. * @iocb: Pointer to iocb slot in the ring.
  486. * @nextiocb: Pointer to driver iocb object which need to be
  487. * posted to firmware.
  488. *
  489. * This function is called with hbalock held to post a new iocb to
  490. * the firmware. This function copies the new iocb to ring iocb slot and
  491. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  492. * a completion call back for this iocb else the function will free the
  493. * iocb object.
  494. **/
  495. static void
  496. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  497. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  498. {
  499. /*
  500. * Set up an iotag
  501. */
  502. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  503. if (pring->ringno == LPFC_ELS_RING) {
  504. lpfc_debugfs_slow_ring_trc(phba,
  505. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  506. *(((uint32_t *) &nextiocb->iocb) + 4),
  507. *(((uint32_t *) &nextiocb->iocb) + 6),
  508. *(((uint32_t *) &nextiocb->iocb) + 7));
  509. }
  510. /*
  511. * Issue iocb command to adapter
  512. */
  513. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  514. wmb();
  515. pring->stats.iocb_cmd++;
  516. /*
  517. * If there is no completion routine to call, we can release the
  518. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  519. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  520. */
  521. if (nextiocb->iocb_cmpl)
  522. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  523. else
  524. __lpfc_sli_release_iocbq(phba, nextiocb);
  525. /*
  526. * Let the HBA know what IOCB slot will be the next one the
  527. * driver will put a command into.
  528. */
  529. pring->cmdidx = pring->next_cmdidx;
  530. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  531. }
  532. /**
  533. * lpfc_sli_update_full_ring: Update the chip attention register.
  534. * @phba: Pointer to HBA context object.
  535. * @pring: Pointer to driver SLI ring object.
  536. *
  537. * The caller is not required to hold any lock for calling this function.
  538. * This function updates the chip attention bits for the ring to inform firmware
  539. * that there are pending work to be done for this ring and requests an
  540. * interrupt when there is space available in the ring. This function is
  541. * called when the driver is unable to post more iocbs to the ring due
  542. * to unavailability of space in the ring.
  543. **/
  544. static void
  545. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  546. {
  547. int ringno = pring->ringno;
  548. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  549. wmb();
  550. /*
  551. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  552. * The HBA will tell us when an IOCB entry is available.
  553. */
  554. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  555. readl(phba->CAregaddr); /* flush */
  556. pring->stats.iocb_cmd_full++;
  557. }
  558. /**
  559. * lpfc_sli_update_ring: Update chip attention register.
  560. * @phba: Pointer to HBA context object.
  561. * @pring: Pointer to driver SLI ring object.
  562. *
  563. * This function updates the chip attention register bit for the
  564. * given ring to inform HBA that there is more work to be done
  565. * in this ring. The caller is not required to hold any lock.
  566. **/
  567. static void
  568. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  569. {
  570. int ringno = pring->ringno;
  571. /*
  572. * Tell the HBA that there is work to do in this ring.
  573. */
  574. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  575. wmb();
  576. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  577. readl(phba->CAregaddr); /* flush */
  578. }
  579. }
  580. /**
  581. * lpfc_sli_resume_iocb: Process iocbs in the txq.
  582. * @phba: Pointer to HBA context object.
  583. * @pring: Pointer to driver SLI ring object.
  584. *
  585. * This function is called with hbalock held to post pending iocbs
  586. * in the txq to the firmware. This function is called when driver
  587. * detects space available in the ring.
  588. **/
  589. static void
  590. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  591. {
  592. IOCB_t *iocb;
  593. struct lpfc_iocbq *nextiocb;
  594. /*
  595. * Check to see if:
  596. * (a) there is anything on the txq to send
  597. * (b) link is up
  598. * (c) link attention events can be processed (fcp ring only)
  599. * (d) IOCB processing is not blocked by the outstanding mbox command.
  600. */
  601. if (pring->txq_cnt &&
  602. lpfc_is_link_up(phba) &&
  603. (pring->ringno != phba->sli.fcp_ring ||
  604. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  605. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  606. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  607. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  608. if (iocb)
  609. lpfc_sli_update_ring(phba, pring);
  610. else
  611. lpfc_sli_update_full_ring(phba, pring);
  612. }
  613. return;
  614. }
  615. /**
  616. * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
  617. * @phba: Pointer to HBA context object.
  618. * @hbqno: HBQ number.
  619. *
  620. * This function is called with hbalock held to get the next
  621. * available slot for the given HBQ. If there is free slot
  622. * available for the HBQ it will return pointer to the next available
  623. * HBQ entry else it will return NULL.
  624. **/
  625. static struct lpfc_hbq_entry *
  626. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  627. {
  628. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  629. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  630. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  631. hbqp->next_hbqPutIdx = 0;
  632. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  633. uint32_t raw_index = phba->hbq_get[hbqno];
  634. uint32_t getidx = le32_to_cpu(raw_index);
  635. hbqp->local_hbqGetIdx = getidx;
  636. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  637. lpfc_printf_log(phba, KERN_ERR,
  638. LOG_SLI | LOG_VPORT,
  639. "1802 HBQ %d: local_hbqGetIdx "
  640. "%u is > than hbqp->entry_count %u\n",
  641. hbqno, hbqp->local_hbqGetIdx,
  642. hbqp->entry_count);
  643. phba->link_state = LPFC_HBA_ERROR;
  644. return NULL;
  645. }
  646. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  647. return NULL;
  648. }
  649. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  650. hbqp->hbqPutIdx;
  651. }
  652. /**
  653. * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
  654. * @phba: Pointer to HBA context object.
  655. *
  656. * This function is called with no lock held to free all the
  657. * hbq buffers while uninitializing the SLI interface. It also
  658. * frees the HBQ buffers returned by the firmware but not yet
  659. * processed by the upper layers.
  660. **/
  661. void
  662. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  663. {
  664. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  665. struct hbq_dmabuf *hbq_buf;
  666. unsigned long flags;
  667. int i, hbq_count;
  668. uint32_t hbqno;
  669. hbq_count = lpfc_sli_hbq_count();
  670. /* Return all memory used by all HBQs */
  671. spin_lock_irqsave(&phba->hbalock, flags);
  672. for (i = 0; i < hbq_count; ++i) {
  673. list_for_each_entry_safe(dmabuf, next_dmabuf,
  674. &phba->hbqs[i].hbq_buffer_list, list) {
  675. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  676. list_del(&hbq_buf->dbuf.list);
  677. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  678. }
  679. phba->hbqs[i].buffer_count = 0;
  680. }
  681. /* Return all HBQ buffer that are in-fly */
  682. list_for_each_entry_safe(dmabuf, next_dmabuf,
  683. &phba->hbqbuf_in_list, list) {
  684. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  685. list_del(&hbq_buf->dbuf.list);
  686. if (hbq_buf->tag == -1) {
  687. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  688. (phba, hbq_buf);
  689. } else {
  690. hbqno = hbq_buf->tag >> 16;
  691. if (hbqno >= LPFC_MAX_HBQS)
  692. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  693. (phba, hbq_buf);
  694. else
  695. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  696. hbq_buf);
  697. }
  698. }
  699. /* Mark the HBQs not in use */
  700. phba->hbq_in_use = 0;
  701. spin_unlock_irqrestore(&phba->hbalock, flags);
  702. }
  703. /**
  704. * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
  705. * @phba: Pointer to HBA context object.
  706. * @hbqno: HBQ number.
  707. * @hbq_buf: Pointer to HBQ buffer.
  708. *
  709. * This function is called with the hbalock held to post a
  710. * hbq buffer to the firmware. If the function finds an empty
  711. * slot in the HBQ, it will post the buffer. The function will return
  712. * pointer to the hbq entry if it successfully post the buffer
  713. * else it will return NULL.
  714. **/
  715. static struct lpfc_hbq_entry *
  716. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  717. struct hbq_dmabuf *hbq_buf)
  718. {
  719. struct lpfc_hbq_entry *hbqe;
  720. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  721. /* Get next HBQ entry slot to use */
  722. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  723. if (hbqe) {
  724. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  725. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  726. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  727. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  728. hbqe->bde.tus.f.bdeFlags = 0;
  729. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  730. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  731. /* Sync SLIM */
  732. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  733. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  734. /* flush */
  735. readl(phba->hbq_put + hbqno);
  736. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  737. }
  738. return hbqe;
  739. }
  740. /* HBQ for ELS and CT traffic. */
  741. static struct lpfc_hbq_init lpfc_els_hbq = {
  742. .rn = 1,
  743. .entry_count = 200,
  744. .mask_count = 0,
  745. .profile = 0,
  746. .ring_mask = (1 << LPFC_ELS_RING),
  747. .buffer_count = 0,
  748. .init_count = 20,
  749. .add_count = 5,
  750. };
  751. /* HBQ for the extra ring if needed */
  752. static struct lpfc_hbq_init lpfc_extra_hbq = {
  753. .rn = 1,
  754. .entry_count = 200,
  755. .mask_count = 0,
  756. .profile = 0,
  757. .ring_mask = (1 << LPFC_EXTRA_RING),
  758. .buffer_count = 0,
  759. .init_count = 0,
  760. .add_count = 5,
  761. };
  762. /* Array of HBQs */
  763. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  764. &lpfc_els_hbq,
  765. &lpfc_extra_hbq,
  766. };
  767. /**
  768. * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
  769. * @phba: Pointer to HBA context object.
  770. * @hbqno: HBQ number.
  771. * @count: Number of HBQ buffers to be posted.
  772. *
  773. * This function is called with no lock held to post more hbq buffers to the
  774. * given HBQ. The function returns the number of HBQ buffers successfully
  775. * posted.
  776. **/
  777. static int
  778. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  779. {
  780. uint32_t i, posted = 0;
  781. unsigned long flags;
  782. struct hbq_dmabuf *hbq_buffer;
  783. LIST_HEAD(hbq_buf_list);
  784. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  785. return 0;
  786. if ((phba->hbqs[hbqno].buffer_count + count) >
  787. lpfc_hbq_defs[hbqno]->entry_count)
  788. count = lpfc_hbq_defs[hbqno]->entry_count -
  789. phba->hbqs[hbqno].buffer_count;
  790. if (!count)
  791. return 0;
  792. /* Allocate HBQ entries */
  793. for (i = 0; i < count; i++) {
  794. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  795. if (!hbq_buffer)
  796. break;
  797. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  798. }
  799. /* Check whether HBQ is still in use */
  800. spin_lock_irqsave(&phba->hbalock, flags);
  801. if (!phba->hbq_in_use)
  802. goto err;
  803. while (!list_empty(&hbq_buf_list)) {
  804. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  805. dbuf.list);
  806. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  807. (hbqno << 16));
  808. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  809. phba->hbqs[hbqno].buffer_count++;
  810. posted++;
  811. } else
  812. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  813. }
  814. spin_unlock_irqrestore(&phba->hbalock, flags);
  815. return posted;
  816. err:
  817. spin_unlock_irqrestore(&phba->hbalock, flags);
  818. while (!list_empty(&hbq_buf_list)) {
  819. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  820. dbuf.list);
  821. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  822. }
  823. return 0;
  824. }
  825. /**
  826. * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
  827. * @phba: Pointer to HBA context object.
  828. * @qno: HBQ number.
  829. *
  830. * This function posts more buffers to the HBQ. This function
  831. * is called with no lock held. The function returns the number of HBQ entries
  832. * successfully allocated.
  833. **/
  834. int
  835. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  836. {
  837. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  838. lpfc_hbq_defs[qno]->add_count));
  839. }
  840. /**
  841. * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
  842. * @phba: Pointer to HBA context object.
  843. * @qno: HBQ queue number.
  844. *
  845. * This function is called from SLI initialization code path with
  846. * no lock held to post initial HBQ buffers to firmware. The
  847. * function returns the number of HBQ entries successfully allocated.
  848. **/
  849. static int
  850. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  851. {
  852. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  853. lpfc_hbq_defs[qno]->init_count));
  854. }
  855. /**
  856. * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
  857. * @phba: Pointer to HBA context object.
  858. * @tag: Tag of the hbq buffer.
  859. *
  860. * This function is called with hbalock held. This function searches
  861. * for the hbq buffer associated with the given tag in the hbq buffer
  862. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  863. * it returns NULL.
  864. **/
  865. static struct hbq_dmabuf *
  866. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  867. {
  868. struct lpfc_dmabuf *d_buf;
  869. struct hbq_dmabuf *hbq_buf;
  870. uint32_t hbqno;
  871. hbqno = tag >> 16;
  872. if (hbqno >= LPFC_MAX_HBQS)
  873. return NULL;
  874. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  875. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  876. if (hbq_buf->tag == tag) {
  877. return hbq_buf;
  878. }
  879. }
  880. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  881. "1803 Bad hbq tag. Data: x%x x%x\n",
  882. tag, phba->hbqs[tag >> 16].buffer_count);
  883. return NULL;
  884. }
  885. /**
  886. * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
  887. * @phba: Pointer to HBA context object.
  888. * @hbq_buffer: Pointer to HBQ buffer.
  889. *
  890. * This function is called with hbalock. This function gives back
  891. * the hbq buffer to firmware. If the HBQ does not have space to
  892. * post the buffer, it will free the buffer.
  893. **/
  894. void
  895. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  896. {
  897. uint32_t hbqno;
  898. if (hbq_buffer) {
  899. hbqno = hbq_buffer->tag >> 16;
  900. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  901. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  902. }
  903. }
  904. }
  905. /**
  906. * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
  907. * @mbxCommand: mailbox command code.
  908. *
  909. * This function is called by the mailbox event handler function to verify
  910. * that the completed mailbox command is a legitimate mailbox command. If the
  911. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  912. * and the mailbox event handler will take the HBA offline.
  913. **/
  914. static int
  915. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  916. {
  917. uint8_t ret;
  918. switch (mbxCommand) {
  919. case MBX_LOAD_SM:
  920. case MBX_READ_NV:
  921. case MBX_WRITE_NV:
  922. case MBX_WRITE_VPARMS:
  923. case MBX_RUN_BIU_DIAG:
  924. case MBX_INIT_LINK:
  925. case MBX_DOWN_LINK:
  926. case MBX_CONFIG_LINK:
  927. case MBX_CONFIG_RING:
  928. case MBX_RESET_RING:
  929. case MBX_READ_CONFIG:
  930. case MBX_READ_RCONFIG:
  931. case MBX_READ_SPARM:
  932. case MBX_READ_STATUS:
  933. case MBX_READ_RPI:
  934. case MBX_READ_XRI:
  935. case MBX_READ_REV:
  936. case MBX_READ_LNK_STAT:
  937. case MBX_REG_LOGIN:
  938. case MBX_UNREG_LOGIN:
  939. case MBX_READ_LA:
  940. case MBX_CLEAR_LA:
  941. case MBX_DUMP_MEMORY:
  942. case MBX_DUMP_CONTEXT:
  943. case MBX_RUN_DIAGS:
  944. case MBX_RESTART:
  945. case MBX_UPDATE_CFG:
  946. case MBX_DOWN_LOAD:
  947. case MBX_DEL_LD_ENTRY:
  948. case MBX_RUN_PROGRAM:
  949. case MBX_SET_MASK:
  950. case MBX_SET_VARIABLE:
  951. case MBX_UNREG_D_ID:
  952. case MBX_KILL_BOARD:
  953. case MBX_CONFIG_FARP:
  954. case MBX_BEACON:
  955. case MBX_LOAD_AREA:
  956. case MBX_RUN_BIU_DIAG64:
  957. case MBX_CONFIG_PORT:
  958. case MBX_READ_SPARM64:
  959. case MBX_READ_RPI64:
  960. case MBX_REG_LOGIN64:
  961. case MBX_READ_LA64:
  962. case MBX_WRITE_WWN:
  963. case MBX_SET_DEBUG:
  964. case MBX_LOAD_EXP_ROM:
  965. case MBX_ASYNCEVT_ENABLE:
  966. case MBX_REG_VPI:
  967. case MBX_UNREG_VPI:
  968. case MBX_HEARTBEAT:
  969. case MBX_PORT_CAPABILITIES:
  970. case MBX_PORT_IOV_CONTROL:
  971. ret = mbxCommand;
  972. break;
  973. default:
  974. ret = MBX_SHUTDOWN;
  975. break;
  976. }
  977. return ret;
  978. }
  979. /**
  980. * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
  981. * lpfc_sli_issue_mbox_wait.
  982. * @phba: Pointer to HBA context object.
  983. * @pmboxq: Pointer to mailbox command.
  984. *
  985. * This is completion handler function for mailbox commands issued from
  986. * lpfc_sli_issue_mbox_wait function. This function is called by the
  987. * mailbox event handler function with no lock held. This function
  988. * will wake up thread waiting on the wait queue pointed by context1
  989. * of the mailbox.
  990. **/
  991. static void
  992. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  993. {
  994. wait_queue_head_t *pdone_q;
  995. unsigned long drvr_flag;
  996. /*
  997. * If pdone_q is empty, the driver thread gave up waiting and
  998. * continued running.
  999. */
  1000. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1001. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1002. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1003. if (pdone_q)
  1004. wake_up_interruptible(pdone_q);
  1005. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1006. return;
  1007. }
  1008. /**
  1009. * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
  1010. * @phba: Pointer to HBA context object.
  1011. * @pmb: Pointer to mailbox object.
  1012. *
  1013. * This function is the default mailbox completion handler. It
  1014. * frees the memory resources associated with the completed mailbox
  1015. * command. If the completed command is a REG_LOGIN mailbox command,
  1016. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1017. **/
  1018. void
  1019. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1020. {
  1021. struct lpfc_dmabuf *mp;
  1022. uint16_t rpi;
  1023. int rc;
  1024. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1025. if (mp) {
  1026. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1027. kfree(mp);
  1028. }
  1029. /*
  1030. * If a REG_LOGIN succeeded after node is destroyed or node
  1031. * is in re-discovery driver need to cleanup the RPI.
  1032. */
  1033. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1034. pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
  1035. !pmb->mb.mbxStatus) {
  1036. rpi = pmb->mb.un.varWords[0];
  1037. lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
  1038. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1039. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1040. if (rc != MBX_NOT_FINISHED)
  1041. return;
  1042. }
  1043. mempool_free(pmb, phba->mbox_mem_pool);
  1044. return;
  1045. }
  1046. /**
  1047. * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
  1048. * @phba: Pointer to HBA context object.
  1049. *
  1050. * This function is called with no lock held. This function processes all
  1051. * the completed mailbox commands and gives it to upper layers. The interrupt
  1052. * service routine processes mailbox completion interrupt and adds completed
  1053. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1054. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1055. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1056. * function returns the mailbox commands to the upper layer by calling the
  1057. * completion handler function of each mailbox.
  1058. **/
  1059. int
  1060. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1061. {
  1062. MAILBOX_t *pmbox;
  1063. LPFC_MBOXQ_t *pmb;
  1064. int rc;
  1065. LIST_HEAD(cmplq);
  1066. phba->sli.slistat.mbox_event++;
  1067. /* Get all completed mailboxe buffers into the cmplq */
  1068. spin_lock_irq(&phba->hbalock);
  1069. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1070. spin_unlock_irq(&phba->hbalock);
  1071. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1072. do {
  1073. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1074. if (pmb == NULL)
  1075. break;
  1076. pmbox = &pmb->mb;
  1077. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1078. if (pmb->vport) {
  1079. lpfc_debugfs_disc_trc(pmb->vport,
  1080. LPFC_DISC_TRC_MBOX_VPORT,
  1081. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1082. (uint32_t)pmbox->mbxCommand,
  1083. pmbox->un.varWords[0],
  1084. pmbox->un.varWords[1]);
  1085. }
  1086. else {
  1087. lpfc_debugfs_disc_trc(phba->pport,
  1088. LPFC_DISC_TRC_MBOX,
  1089. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  1090. (uint32_t)pmbox->mbxCommand,
  1091. pmbox->un.varWords[0],
  1092. pmbox->un.varWords[1]);
  1093. }
  1094. }
  1095. /*
  1096. * It is a fatal error if unknown mbox command completion.
  1097. */
  1098. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  1099. MBX_SHUTDOWN) {
  1100. /* Unknow mailbox command compl */
  1101. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1102. "(%d):0323 Unknown Mailbox command "
  1103. "%x Cmpl\n",
  1104. pmb->vport ? pmb->vport->vpi : 0,
  1105. pmbox->mbxCommand);
  1106. phba->link_state = LPFC_HBA_ERROR;
  1107. phba->work_hs = HS_FFER3;
  1108. lpfc_handle_eratt(phba);
  1109. continue;
  1110. }
  1111. if (pmbox->mbxStatus) {
  1112. phba->sli.slistat.mbox_stat_err++;
  1113. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  1114. /* Mbox cmd cmpl error - RETRYing */
  1115. lpfc_printf_log(phba, KERN_INFO,
  1116. LOG_MBOX | LOG_SLI,
  1117. "(%d):0305 Mbox cmd cmpl "
  1118. "error - RETRYing Data: x%x "
  1119. "x%x x%x x%x\n",
  1120. pmb->vport ? pmb->vport->vpi :0,
  1121. pmbox->mbxCommand,
  1122. pmbox->mbxStatus,
  1123. pmbox->un.varWords[0],
  1124. pmb->vport->port_state);
  1125. pmbox->mbxStatus = 0;
  1126. pmbox->mbxOwner = OWN_HOST;
  1127. spin_lock_irq(&phba->hbalock);
  1128. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1129. spin_unlock_irq(&phba->hbalock);
  1130. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1131. if (rc == MBX_SUCCESS)
  1132. continue;
  1133. }
  1134. }
  1135. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  1136. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  1137. "(%d):0307 Mailbox cmd x%x Cmpl x%p "
  1138. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1139. pmb->vport ? pmb->vport->vpi : 0,
  1140. pmbox->mbxCommand,
  1141. pmb->mbox_cmpl,
  1142. *((uint32_t *) pmbox),
  1143. pmbox->un.varWords[0],
  1144. pmbox->un.varWords[1],
  1145. pmbox->un.varWords[2],
  1146. pmbox->un.varWords[3],
  1147. pmbox->un.varWords[4],
  1148. pmbox->un.varWords[5],
  1149. pmbox->un.varWords[6],
  1150. pmbox->un.varWords[7]);
  1151. if (pmb->mbox_cmpl)
  1152. pmb->mbox_cmpl(phba,pmb);
  1153. } while (1);
  1154. return 0;
  1155. }
  1156. /**
  1157. * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
  1158. * @phba: Pointer to HBA context object.
  1159. * @pring: Pointer to driver SLI ring object.
  1160. * @tag: buffer tag.
  1161. *
  1162. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  1163. * is set in the tag the buffer is posted for a particular exchange,
  1164. * the function will return the buffer without replacing the buffer.
  1165. * If the buffer is for unsolicited ELS or CT traffic, this function
  1166. * returns the buffer and also posts another buffer to the firmware.
  1167. **/
  1168. static struct lpfc_dmabuf *
  1169. lpfc_sli_get_buff(struct lpfc_hba *phba,
  1170. struct lpfc_sli_ring *pring,
  1171. uint32_t tag)
  1172. {
  1173. struct hbq_dmabuf *hbq_entry;
  1174. if (tag & QUE_BUFTAG_BIT)
  1175. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  1176. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  1177. if (!hbq_entry)
  1178. return NULL;
  1179. return &hbq_entry->dbuf;
  1180. }
  1181. /**
  1182. * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
  1183. * @phba: Pointer to HBA context object.
  1184. * @pring: Pointer to driver SLI ring object.
  1185. * @saveq: Pointer to the unsolicited iocb.
  1186. *
  1187. * This function is called with no lock held by the ring event handler
  1188. * when there is an unsolicited iocb posted to the response ring by the
  1189. * firmware. This function gets the buffer associated with the iocbs
  1190. * and calls the event handler for the ring. This function handles both
  1191. * qring buffers and hbq buffers.
  1192. * When the function returns 1 the caller can free the iocb object otherwise
  1193. * upper layer functions will free the iocb objects.
  1194. **/
  1195. static int
  1196. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1197. struct lpfc_iocbq *saveq)
  1198. {
  1199. IOCB_t * irsp;
  1200. WORD5 * w5p;
  1201. uint32_t Rctl, Type;
  1202. uint32_t match, i;
  1203. struct lpfc_iocbq *iocbq;
  1204. struct lpfc_dmabuf *dmzbuf;
  1205. match = 0;
  1206. irsp = &(saveq->iocb);
  1207. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  1208. if (pring->lpfc_sli_rcv_async_status)
  1209. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  1210. else
  1211. lpfc_printf_log(phba,
  1212. KERN_WARNING,
  1213. LOG_SLI,
  1214. "0316 Ring %d handler: unexpected "
  1215. "ASYNC_STATUS iocb received evt_code "
  1216. "0x%x\n",
  1217. pring->ringno,
  1218. irsp->un.asyncstat.evt_code);
  1219. return 1;
  1220. }
  1221. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  1222. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  1223. if (irsp->ulpBdeCount > 0) {
  1224. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1225. irsp->un.ulpWord[3]);
  1226. lpfc_in_buf_free(phba, dmzbuf);
  1227. }
  1228. if (irsp->ulpBdeCount > 1) {
  1229. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1230. irsp->unsli3.sli3Words[3]);
  1231. lpfc_in_buf_free(phba, dmzbuf);
  1232. }
  1233. if (irsp->ulpBdeCount > 2) {
  1234. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1235. irsp->unsli3.sli3Words[7]);
  1236. lpfc_in_buf_free(phba, dmzbuf);
  1237. }
  1238. return 1;
  1239. }
  1240. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1241. if (irsp->ulpBdeCount != 0) {
  1242. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  1243. irsp->un.ulpWord[3]);
  1244. if (!saveq->context2)
  1245. lpfc_printf_log(phba,
  1246. KERN_ERR,
  1247. LOG_SLI,
  1248. "0341 Ring %d Cannot find buffer for "
  1249. "an unsolicited iocb. tag 0x%x\n",
  1250. pring->ringno,
  1251. irsp->un.ulpWord[3]);
  1252. }
  1253. if (irsp->ulpBdeCount == 2) {
  1254. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  1255. irsp->unsli3.sli3Words[7]);
  1256. if (!saveq->context3)
  1257. lpfc_printf_log(phba,
  1258. KERN_ERR,
  1259. LOG_SLI,
  1260. "0342 Ring %d Cannot find buffer for an"
  1261. " unsolicited iocb. tag 0x%x\n",
  1262. pring->ringno,
  1263. irsp->unsli3.sli3Words[7]);
  1264. }
  1265. list_for_each_entry(iocbq, &saveq->list, list) {
  1266. irsp = &(iocbq->iocb);
  1267. if (irsp->ulpBdeCount != 0) {
  1268. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  1269. irsp->un.ulpWord[3]);
  1270. if (!iocbq->context2)
  1271. lpfc_printf_log(phba,
  1272. KERN_ERR,
  1273. LOG_SLI,
  1274. "0343 Ring %d Cannot find "
  1275. "buffer for an unsolicited iocb"
  1276. ". tag 0x%x\n", pring->ringno,
  1277. irsp->un.ulpWord[3]);
  1278. }
  1279. if (irsp->ulpBdeCount == 2) {
  1280. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  1281. irsp->unsli3.sli3Words[7]);
  1282. if (!iocbq->context3)
  1283. lpfc_printf_log(phba,
  1284. KERN_ERR,
  1285. LOG_SLI,
  1286. "0344 Ring %d Cannot find "
  1287. "buffer for an unsolicited "
  1288. "iocb. tag 0x%x\n",
  1289. pring->ringno,
  1290. irsp->unsli3.sli3Words[7]);
  1291. }
  1292. }
  1293. }
  1294. if (irsp->ulpBdeCount != 0 &&
  1295. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  1296. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  1297. int found = 0;
  1298. /* search continue save q for same XRI */
  1299. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  1300. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  1301. list_add_tail(&saveq->list, &iocbq->list);
  1302. found = 1;
  1303. break;
  1304. }
  1305. }
  1306. if (!found)
  1307. list_add_tail(&saveq->clist,
  1308. &pring->iocb_continue_saveq);
  1309. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  1310. list_del_init(&iocbq->clist);
  1311. saveq = iocbq;
  1312. irsp = &(saveq->iocb);
  1313. } else
  1314. return 0;
  1315. }
  1316. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  1317. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  1318. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  1319. Rctl = FC_ELS_REQ;
  1320. Type = FC_ELS_DATA;
  1321. } else {
  1322. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  1323. Rctl = w5p->hcsw.Rctl;
  1324. Type = w5p->hcsw.Type;
  1325. /* Firmware Workaround */
  1326. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  1327. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  1328. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  1329. Rctl = FC_ELS_REQ;
  1330. Type = FC_ELS_DATA;
  1331. w5p->hcsw.Rctl = Rctl;
  1332. w5p->hcsw.Type = Type;
  1333. }
  1334. }
  1335. /* unSolicited Responses */
  1336. if (pring->prt[0].profile) {
  1337. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  1338. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  1339. saveq);
  1340. match = 1;
  1341. } else {
  1342. /* We must search, based on rctl / type
  1343. for the right routine */
  1344. for (i = 0; i < pring->num_mask; i++) {
  1345. if ((pring->prt[i].rctl == Rctl)
  1346. && (pring->prt[i].type == Type)) {
  1347. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1348. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1349. (phba, pring, saveq);
  1350. match = 1;
  1351. break;
  1352. }
  1353. }
  1354. }
  1355. if (match == 0) {
  1356. /* Unexpected Rctl / Type received */
  1357. /* Ring <ringno> handler: unexpected
  1358. Rctl <Rctl> Type <Type> received */
  1359. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1360. "0313 Ring %d handler: unexpected Rctl x%x "
  1361. "Type x%x received\n",
  1362. pring->ringno, Rctl, Type);
  1363. }
  1364. return 1;
  1365. }
  1366. /**
  1367. * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
  1368. * @phba: Pointer to HBA context object.
  1369. * @pring: Pointer to driver SLI ring object.
  1370. * @prspiocb: Pointer to response iocb object.
  1371. *
  1372. * This function looks up the iocb_lookup table to get the command iocb
  1373. * corresponding to the given response iocb using the iotag of the
  1374. * response iocb. This function is called with the hbalock held.
  1375. * This function returns the command iocb object if it finds the command
  1376. * iocb else returns NULL.
  1377. **/
  1378. static struct lpfc_iocbq *
  1379. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  1380. struct lpfc_sli_ring *pring,
  1381. struct lpfc_iocbq *prspiocb)
  1382. {
  1383. struct lpfc_iocbq *cmd_iocb = NULL;
  1384. uint16_t iotag;
  1385. iotag = prspiocb->iocb.ulpIoTag;
  1386. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1387. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1388. list_del_init(&cmd_iocb->list);
  1389. pring->txcmplq_cnt--;
  1390. return cmd_iocb;
  1391. }
  1392. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1393. "0317 iotag x%x is out off "
  1394. "range: max iotag x%x wd0 x%x\n",
  1395. iotag, phba->sli.last_iotag,
  1396. *(((uint32_t *) &prspiocb->iocb) + 7));
  1397. return NULL;
  1398. }
  1399. /**
  1400. * lpfc_sli_process_sol_iocb: process solicited iocb completion.
  1401. * @phba: Pointer to HBA context object.
  1402. * @pring: Pointer to driver SLI ring object.
  1403. * @saveq: Pointer to the response iocb to be processed.
  1404. *
  1405. * This function is called by the ring event handler for non-fcp
  1406. * rings when there is a new response iocb in the response ring.
  1407. * The caller is not required to hold any locks. This function
  1408. * gets the command iocb associated with the response iocb and
  1409. * calls the completion handler for the command iocb. If there
  1410. * is no completion handler, the function will free the resources
  1411. * associated with command iocb. If the response iocb is for
  1412. * an already aborted command iocb, the status of the completion
  1413. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  1414. * This function always returns 1.
  1415. **/
  1416. static int
  1417. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1418. struct lpfc_iocbq *saveq)
  1419. {
  1420. struct lpfc_iocbq *cmdiocbp;
  1421. int rc = 1;
  1422. unsigned long iflag;
  1423. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  1424. spin_lock_irqsave(&phba->hbalock, iflag);
  1425. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  1426. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1427. if (cmdiocbp) {
  1428. if (cmdiocbp->iocb_cmpl) {
  1429. /*
  1430. * If an ELS command failed send an event to mgmt
  1431. * application.
  1432. */
  1433. if (saveq->iocb.ulpStatus &&
  1434. (pring->ringno == LPFC_ELS_RING) &&
  1435. (cmdiocbp->iocb.ulpCommand ==
  1436. CMD_ELS_REQUEST64_CR))
  1437. lpfc_send_els_failure_event(phba,
  1438. cmdiocbp, saveq);
  1439. /*
  1440. * Post all ELS completions to the worker thread.
  1441. * All other are passed to the completion callback.
  1442. */
  1443. if (pring->ringno == LPFC_ELS_RING) {
  1444. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  1445. cmdiocbp->iocb_flag &=
  1446. ~LPFC_DRIVER_ABORTED;
  1447. saveq->iocb.ulpStatus =
  1448. IOSTAT_LOCAL_REJECT;
  1449. saveq->iocb.un.ulpWord[4] =
  1450. IOERR_SLI_ABORTED;
  1451. /* Firmware could still be in progress
  1452. * of DMAing payload, so don't free data
  1453. * buffer till after a hbeat.
  1454. */
  1455. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  1456. }
  1457. }
  1458. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  1459. } else
  1460. lpfc_sli_release_iocbq(phba, cmdiocbp);
  1461. } else {
  1462. /*
  1463. * Unknown initiating command based on the response iotag.
  1464. * This could be the case on the ELS ring because of
  1465. * lpfc_els_abort().
  1466. */
  1467. if (pring->ringno != LPFC_ELS_RING) {
  1468. /*
  1469. * Ring <ringno> handler: unexpected completion IoTag
  1470. * <IoTag>
  1471. */
  1472. lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
  1473. "0322 Ring %d handler: "
  1474. "unexpected completion IoTag x%x "
  1475. "Data: x%x x%x x%x x%x\n",
  1476. pring->ringno,
  1477. saveq->iocb.ulpIoTag,
  1478. saveq->iocb.ulpStatus,
  1479. saveq->iocb.un.ulpWord[4],
  1480. saveq->iocb.ulpCommand,
  1481. saveq->iocb.ulpContext);
  1482. }
  1483. }
  1484. return rc;
  1485. }
  1486. /**
  1487. * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
  1488. * @phba: Pointer to HBA context object.
  1489. * @pring: Pointer to driver SLI ring object.
  1490. *
  1491. * This function is called from the iocb ring event handlers when
  1492. * put pointer is ahead of the get pointer for a ring. This function signal
  1493. * an error attention condition to the worker thread and the worker
  1494. * thread will transition the HBA to offline state.
  1495. **/
  1496. static void
  1497. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1498. {
  1499. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1500. /*
  1501. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1502. * rsp ring <portRspMax>
  1503. */
  1504. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1505. "0312 Ring %d handler: portRspPut %d "
  1506. "is bigger then rsp ring %d\n",
  1507. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  1508. pring->numRiocb);
  1509. phba->link_state = LPFC_HBA_ERROR;
  1510. /*
  1511. * All error attention handlers are posted to
  1512. * worker thread
  1513. */
  1514. phba->work_ha |= HA_ERATT;
  1515. phba->work_hs = HS_FFER3;
  1516. lpfc_worker_wake_up(phba);
  1517. return;
  1518. }
  1519. /**
  1520. * lpfc_poll_eratt: Error attention polling timer timeout handler.
  1521. * @ptr: Pointer to address of HBA context object.
  1522. *
  1523. * This function is invoked by the Error Attention polling timer when the
  1524. * timer times out. It will check the SLI Error Attention register for
  1525. * possible attention events. If so, it will post an Error Attention event
  1526. * and wake up worker thread to process it. Otherwise, it will set up the
  1527. * Error Attention polling timer for the next poll.
  1528. **/
  1529. void lpfc_poll_eratt(unsigned long ptr)
  1530. {
  1531. struct lpfc_hba *phba;
  1532. uint32_t eratt = 0;
  1533. phba = (struct lpfc_hba *)ptr;
  1534. /* Check chip HA register for error event */
  1535. eratt = lpfc_sli_check_eratt(phba);
  1536. if (eratt)
  1537. /* Tell the worker thread there is work to do */
  1538. lpfc_worker_wake_up(phba);
  1539. else
  1540. /* Restart the timer for next eratt poll */
  1541. mod_timer(&phba->eratt_poll, jiffies +
  1542. HZ * LPFC_ERATT_POLL_INTERVAL);
  1543. return;
  1544. }
  1545. /**
  1546. * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
  1547. * @phba: Pointer to HBA context object.
  1548. *
  1549. * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
  1550. * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
  1551. * is enabled.
  1552. *
  1553. * The caller does not hold any lock.
  1554. * The function processes each response iocb in the response ring until it
  1555. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1556. * LE bit set. The function will call the completion handler of the command iocb
  1557. * if the response iocb indicates a completion for a command iocb or it is
  1558. * an abort completion.
  1559. **/
  1560. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  1561. {
  1562. struct lpfc_sli *psli = &phba->sli;
  1563. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  1564. IOCB_t *irsp = NULL;
  1565. IOCB_t *entry = NULL;
  1566. struct lpfc_iocbq *cmdiocbq = NULL;
  1567. struct lpfc_iocbq rspiocbq;
  1568. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1569. uint32_t status;
  1570. uint32_t portRspPut, portRspMax;
  1571. int type;
  1572. uint32_t rsp_cmpl = 0;
  1573. uint32_t ha_copy;
  1574. unsigned long iflags;
  1575. pring->stats.iocb_event++;
  1576. /*
  1577. * The next available response entry should never exceed the maximum
  1578. * entries. If it does, treat it as an adapter hardware error.
  1579. */
  1580. portRspMax = pring->numRiocb;
  1581. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1582. if (unlikely(portRspPut >= portRspMax)) {
  1583. lpfc_sli_rsp_pointers_error(phba, pring);
  1584. return;
  1585. }
  1586. rmb();
  1587. while (pring->rspidx != portRspPut) {
  1588. entry = lpfc_resp_iocb(phba, pring);
  1589. if (++pring->rspidx >= portRspMax)
  1590. pring->rspidx = 0;
  1591. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1592. (uint32_t *) &rspiocbq.iocb,
  1593. phba->iocb_rsp_size);
  1594. irsp = &rspiocbq.iocb;
  1595. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1596. pring->stats.iocb_rsp++;
  1597. rsp_cmpl++;
  1598. if (unlikely(irsp->ulpStatus)) {
  1599. /* Rsp ring <ringno> error: IOCB */
  1600. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1601. "0326 Rsp Ring %d error: IOCB Data: "
  1602. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1603. pring->ringno,
  1604. irsp->un.ulpWord[0],
  1605. irsp->un.ulpWord[1],
  1606. irsp->un.ulpWord[2],
  1607. irsp->un.ulpWord[3],
  1608. irsp->un.ulpWord[4],
  1609. irsp->un.ulpWord[5],
  1610. *(uint32_t *)&irsp->un1,
  1611. *((uint32_t *)&irsp->un1 + 1));
  1612. }
  1613. switch (type) {
  1614. case LPFC_ABORT_IOCB:
  1615. case LPFC_SOL_IOCB:
  1616. /*
  1617. * Idle exchange closed via ABTS from port. No iocb
  1618. * resources need to be recovered.
  1619. */
  1620. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1621. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1622. "0314 IOCB cmd 0x%x "
  1623. "processed. Skipping "
  1624. "completion",
  1625. irsp->ulpCommand);
  1626. break;
  1627. }
  1628. spin_lock_irqsave(&phba->hbalock, iflags);
  1629. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1630. &rspiocbq);
  1631. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1632. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1633. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1634. &rspiocbq);
  1635. }
  1636. break;
  1637. default:
  1638. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1639. char adaptermsg[LPFC_MAX_ADPTMSG];
  1640. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1641. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1642. MAX_MSG_DATA);
  1643. dev_warn(&((phba->pcidev)->dev),
  1644. "lpfc%d: %s\n",
  1645. phba->brd_no, adaptermsg);
  1646. } else {
  1647. /* Unknown IOCB command */
  1648. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1649. "0321 Unknown IOCB command "
  1650. "Data: x%x, x%x x%x x%x x%x\n",
  1651. type, irsp->ulpCommand,
  1652. irsp->ulpStatus,
  1653. irsp->ulpIoTag,
  1654. irsp->ulpContext);
  1655. }
  1656. break;
  1657. }
  1658. /*
  1659. * The response IOCB has been processed. Update the ring
  1660. * pointer in SLIM. If the port response put pointer has not
  1661. * been updated, sync the pgp->rspPutInx and fetch the new port
  1662. * response put pointer.
  1663. */
  1664. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1665. if (pring->rspidx == portRspPut)
  1666. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1667. }
  1668. ha_copy = readl(phba->HAregaddr);
  1669. ha_copy >>= (LPFC_FCP_RING * 4);
  1670. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  1671. spin_lock_irqsave(&phba->hbalock, iflags);
  1672. pring->stats.iocb_rsp_full++;
  1673. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  1674. writel(status, phba->CAregaddr);
  1675. readl(phba->CAregaddr);
  1676. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1677. }
  1678. if ((ha_copy & HA_R0CE_RSP) &&
  1679. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1680. spin_lock_irqsave(&phba->hbalock, iflags);
  1681. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1682. pring->stats.iocb_cmd_empty++;
  1683. /* Force update of the local copy of cmdGetInx */
  1684. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1685. lpfc_sli_resume_iocb(phba, pring);
  1686. if ((pring->lpfc_sli_cmd_available))
  1687. (pring->lpfc_sli_cmd_available) (phba, pring);
  1688. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1689. }
  1690. return;
  1691. }
  1692. /**
  1693. * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
  1694. * @phba: Pointer to HBA context object.
  1695. * @pring: Pointer to driver SLI ring object.
  1696. * @mask: Host attention register mask for this ring.
  1697. *
  1698. * This function is called from the interrupt context when there is a ring
  1699. * event for the fcp ring. The caller does not hold any lock.
  1700. * The function processes each response iocb in the response ring until it
  1701. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1702. * LE bit set. The function will call the completion handler of the command iocb
  1703. * if the response iocb indicates a completion for a command iocb or it is
  1704. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  1705. * function if this is an unsolicited iocb.
  1706. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  1707. * to check it explicitly. This function always returns 1.
  1708. **/
  1709. static int
  1710. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  1711. struct lpfc_sli_ring *pring, uint32_t mask)
  1712. {
  1713. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1714. IOCB_t *irsp = NULL;
  1715. IOCB_t *entry = NULL;
  1716. struct lpfc_iocbq *cmdiocbq = NULL;
  1717. struct lpfc_iocbq rspiocbq;
  1718. uint32_t status;
  1719. uint32_t portRspPut, portRspMax;
  1720. int rc = 1;
  1721. lpfc_iocb_type type;
  1722. unsigned long iflag;
  1723. uint32_t rsp_cmpl = 0;
  1724. spin_lock_irqsave(&phba->hbalock, iflag);
  1725. pring->stats.iocb_event++;
  1726. /*
  1727. * The next available response entry should never exceed the maximum
  1728. * entries. If it does, treat it as an adapter hardware error.
  1729. */
  1730. portRspMax = pring->numRiocb;
  1731. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1732. if (unlikely(portRspPut >= portRspMax)) {
  1733. lpfc_sli_rsp_pointers_error(phba, pring);
  1734. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1735. return 1;
  1736. }
  1737. rmb();
  1738. while (pring->rspidx != portRspPut) {
  1739. /*
  1740. * Fetch an entry off the ring and copy it into a local data
  1741. * structure. The copy involves a byte-swap since the
  1742. * network byte order and pci byte orders are different.
  1743. */
  1744. entry = lpfc_resp_iocb(phba, pring);
  1745. phba->last_completion_time = jiffies;
  1746. if (++pring->rspidx >= portRspMax)
  1747. pring->rspidx = 0;
  1748. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1749. (uint32_t *) &rspiocbq.iocb,
  1750. phba->iocb_rsp_size);
  1751. INIT_LIST_HEAD(&(rspiocbq.list));
  1752. irsp = &rspiocbq.iocb;
  1753. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1754. pring->stats.iocb_rsp++;
  1755. rsp_cmpl++;
  1756. if (unlikely(irsp->ulpStatus)) {
  1757. /*
  1758. * If resource errors reported from HBA, reduce
  1759. * queuedepths of the SCSI device.
  1760. */
  1761. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1762. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1763. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1764. lpfc_rampdown_queue_depth(phba);
  1765. spin_lock_irqsave(&phba->hbalock, iflag);
  1766. }
  1767. /* Rsp ring <ringno> error: IOCB */
  1768. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1769. "0336 Rsp Ring %d error: IOCB Data: "
  1770. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1771. pring->ringno,
  1772. irsp->un.ulpWord[0],
  1773. irsp->un.ulpWord[1],
  1774. irsp->un.ulpWord[2],
  1775. irsp->un.ulpWord[3],
  1776. irsp->un.ulpWord[4],
  1777. irsp->un.ulpWord[5],
  1778. *(uint32_t *)&irsp->un1,
  1779. *((uint32_t *)&irsp->un1 + 1));
  1780. }
  1781. switch (type) {
  1782. case LPFC_ABORT_IOCB:
  1783. case LPFC_SOL_IOCB:
  1784. /*
  1785. * Idle exchange closed via ABTS from port. No iocb
  1786. * resources need to be recovered.
  1787. */
  1788. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1789. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1790. "0333 IOCB cmd 0x%x"
  1791. " processed. Skipping"
  1792. " completion\n",
  1793. irsp->ulpCommand);
  1794. break;
  1795. }
  1796. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1797. &rspiocbq);
  1798. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1799. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1800. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1801. &rspiocbq);
  1802. } else {
  1803. spin_unlock_irqrestore(&phba->hbalock,
  1804. iflag);
  1805. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1806. &rspiocbq);
  1807. spin_lock_irqsave(&phba->hbalock,
  1808. iflag);
  1809. }
  1810. }
  1811. break;
  1812. case LPFC_UNSOL_IOCB:
  1813. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1814. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  1815. spin_lock_irqsave(&phba->hbalock, iflag);
  1816. break;
  1817. default:
  1818. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1819. char adaptermsg[LPFC_MAX_ADPTMSG];
  1820. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1821. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1822. MAX_MSG_DATA);
  1823. dev_warn(&((phba->pcidev)->dev),
  1824. "lpfc%d: %s\n",
  1825. phba->brd_no, adaptermsg);
  1826. } else {
  1827. /* Unknown IOCB command */
  1828. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1829. "0334 Unknown IOCB command "
  1830. "Data: x%x, x%x x%x x%x x%x\n",
  1831. type, irsp->ulpCommand,
  1832. irsp->ulpStatus,
  1833. irsp->ulpIoTag,
  1834. irsp->ulpContext);
  1835. }
  1836. break;
  1837. }
  1838. /*
  1839. * The response IOCB has been processed. Update the ring
  1840. * pointer in SLIM. If the port response put pointer has not
  1841. * been updated, sync the pgp->rspPutInx and fetch the new port
  1842. * response put pointer.
  1843. */
  1844. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1845. if (pring->rspidx == portRspPut)
  1846. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1847. }
  1848. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  1849. pring->stats.iocb_rsp_full++;
  1850. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1851. writel(status, phba->CAregaddr);
  1852. readl(phba->CAregaddr);
  1853. }
  1854. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1855. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1856. pring->stats.iocb_cmd_empty++;
  1857. /* Force update of the local copy of cmdGetInx */
  1858. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1859. lpfc_sli_resume_iocb(phba, pring);
  1860. if ((pring->lpfc_sli_cmd_available))
  1861. (pring->lpfc_sli_cmd_available) (phba, pring);
  1862. }
  1863. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1864. return rc;
  1865. }
  1866. /**
  1867. * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
  1868. * @phba: Pointer to HBA context object.
  1869. * @pring: Pointer to driver SLI ring object.
  1870. * @mask: Host attention register mask for this ring.
  1871. *
  1872. * This function is called from the worker thread when there is a ring
  1873. * event for non-fcp rings. The caller does not hold any lock .
  1874. * The function processes each response iocb in the response ring until it
  1875. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1876. * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
  1877. * response iocb indicates a completion of a command iocb. The function
  1878. * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
  1879. * iocb. The function frees the resources or calls the completion handler if
  1880. * this iocb is an abort completion. The function returns 0 when the allocated
  1881. * iocbs are not freed, otherwise returns 1.
  1882. **/
  1883. int
  1884. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  1885. struct lpfc_sli_ring *pring, uint32_t mask)
  1886. {
  1887. struct lpfc_pgp *pgp;
  1888. IOCB_t *entry;
  1889. IOCB_t *irsp = NULL;
  1890. struct lpfc_iocbq *rspiocbp = NULL;
  1891. struct lpfc_iocbq *next_iocb;
  1892. struct lpfc_iocbq *cmdiocbp;
  1893. struct lpfc_iocbq *saveq;
  1894. uint8_t iocb_cmd_type;
  1895. lpfc_iocb_type type;
  1896. uint32_t status, free_saveq;
  1897. uint32_t portRspPut, portRspMax;
  1898. int rc = 1;
  1899. unsigned long iflag;
  1900. pgp = &phba->port_gp[pring->ringno];
  1901. spin_lock_irqsave(&phba->hbalock, iflag);
  1902. pring->stats.iocb_event++;
  1903. /*
  1904. * The next available response entry should never exceed the maximum
  1905. * entries. If it does, treat it as an adapter hardware error.
  1906. */
  1907. portRspMax = pring->numRiocb;
  1908. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1909. if (portRspPut >= portRspMax) {
  1910. /*
  1911. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1912. * rsp ring <portRspMax>
  1913. */
  1914. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1915. "0303 Ring %d handler: portRspPut %d "
  1916. "is bigger then rsp ring %d\n",
  1917. pring->ringno, portRspPut, portRspMax);
  1918. phba->link_state = LPFC_HBA_ERROR;
  1919. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1920. phba->work_hs = HS_FFER3;
  1921. lpfc_handle_eratt(phba);
  1922. return 1;
  1923. }
  1924. rmb();
  1925. while (pring->rspidx != portRspPut) {
  1926. /*
  1927. * Build a completion list and call the appropriate handler.
  1928. * The process is to get the next available response iocb, get
  1929. * a free iocb from the list, copy the response data into the
  1930. * free iocb, insert to the continuation list, and update the
  1931. * next response index to slim. This process makes response
  1932. * iocb's in the ring available to DMA as fast as possible but
  1933. * pays a penalty for a copy operation. Since the iocb is
  1934. * only 32 bytes, this penalty is considered small relative to
  1935. * the PCI reads for register values and a slim write. When
  1936. * the ulpLe field is set, the entire Command has been
  1937. * received.
  1938. */
  1939. entry = lpfc_resp_iocb(phba, pring);
  1940. phba->last_completion_time = jiffies;
  1941. rspiocbp = __lpfc_sli_get_iocbq(phba);
  1942. if (rspiocbp == NULL) {
  1943. printk(KERN_ERR "%s: out of buffers! Failing "
  1944. "completion.\n", __func__);
  1945. break;
  1946. }
  1947. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  1948. phba->iocb_rsp_size);
  1949. irsp = &rspiocbp->iocb;
  1950. if (++pring->rspidx >= portRspMax)
  1951. pring->rspidx = 0;
  1952. if (pring->ringno == LPFC_ELS_RING) {
  1953. lpfc_debugfs_slow_ring_trc(phba,
  1954. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1955. *(((uint32_t *) irsp) + 4),
  1956. *(((uint32_t *) irsp) + 6),
  1957. *(((uint32_t *) irsp) + 7));
  1958. }
  1959. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1960. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  1961. pring->iocb_continueq_cnt++;
  1962. if (irsp->ulpLe) {
  1963. /*
  1964. * By default, the driver expects to free all resources
  1965. * associated with this iocb completion.
  1966. */
  1967. free_saveq = 1;
  1968. saveq = list_get_first(&pring->iocb_continueq,
  1969. struct lpfc_iocbq, list);
  1970. irsp = &(saveq->iocb);
  1971. list_del_init(&pring->iocb_continueq);
  1972. pring->iocb_continueq_cnt = 0;
  1973. pring->stats.iocb_rsp++;
  1974. /*
  1975. * If resource errors reported from HBA, reduce
  1976. * queuedepths of the SCSI device.
  1977. */
  1978. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1979. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1980. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1981. lpfc_rampdown_queue_depth(phba);
  1982. spin_lock_irqsave(&phba->hbalock, iflag);
  1983. }
  1984. if (irsp->ulpStatus) {
  1985. /* Rsp ring <ringno> error: IOCB */
  1986. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1987. "0328 Rsp Ring %d error: "
  1988. "IOCB Data: "
  1989. "x%x x%x x%x x%x "
  1990. "x%x x%x x%x x%x "
  1991. "x%x x%x x%x x%x "
  1992. "x%x x%x x%x x%x\n",
  1993. pring->ringno,
  1994. irsp->un.ulpWord[0],
  1995. irsp->un.ulpWord[1],
  1996. irsp->un.ulpWord[2],
  1997. irsp->un.ulpWord[3],
  1998. irsp->un.ulpWord[4],
  1999. irsp->un.ulpWord[5],
  2000. *(((uint32_t *) irsp) + 6),
  2001. *(((uint32_t *) irsp) + 7),
  2002. *(((uint32_t *) irsp) + 8),
  2003. *(((uint32_t *) irsp) + 9),
  2004. *(((uint32_t *) irsp) + 10),
  2005. *(((uint32_t *) irsp) + 11),
  2006. *(((uint32_t *) irsp) + 12),
  2007. *(((uint32_t *) irsp) + 13),
  2008. *(((uint32_t *) irsp) + 14),
  2009. *(((uint32_t *) irsp) + 15));
  2010. }
  2011. /*
  2012. * Fetch the IOCB command type and call the correct
  2013. * completion routine. Solicited and Unsolicited
  2014. * IOCBs on the ELS ring get freed back to the
  2015. * lpfc_iocb_list by the discovery kernel thread.
  2016. */
  2017. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2018. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2019. if (type == LPFC_SOL_IOCB) {
  2020. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2021. rc = lpfc_sli_process_sol_iocb(phba, pring,
  2022. saveq);
  2023. spin_lock_irqsave(&phba->hbalock, iflag);
  2024. } else if (type == LPFC_UNSOL_IOCB) {
  2025. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2026. rc = lpfc_sli_process_unsol_iocb(phba, pring,
  2027. saveq);
  2028. spin_lock_irqsave(&phba->hbalock, iflag);
  2029. if (!rc)
  2030. free_saveq = 0;
  2031. } else if (type == LPFC_ABORT_IOCB) {
  2032. if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
  2033. ((cmdiocbp =
  2034. lpfc_sli_iocbq_lookup(phba, pring,
  2035. saveq)))) {
  2036. /* Call the specified completion
  2037. routine */
  2038. if (cmdiocbp->iocb_cmpl) {
  2039. spin_unlock_irqrestore(
  2040. &phba->hbalock,
  2041. iflag);
  2042. (cmdiocbp->iocb_cmpl) (phba,
  2043. cmdiocbp, saveq);
  2044. spin_lock_irqsave(
  2045. &phba->hbalock,
  2046. iflag);
  2047. } else
  2048. __lpfc_sli_release_iocbq(phba,
  2049. cmdiocbp);
  2050. }
  2051. } else if (type == LPFC_UNKNOWN_IOCB) {
  2052. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2053. char adaptermsg[LPFC_MAX_ADPTMSG];
  2054. memset(adaptermsg, 0,
  2055. LPFC_MAX_ADPTMSG);
  2056. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2057. MAX_MSG_DATA);
  2058. dev_warn(&((phba->pcidev)->dev),
  2059. "lpfc%d: %s\n",
  2060. phba->brd_no, adaptermsg);
  2061. } else {
  2062. /* Unknown IOCB command */
  2063. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2064. "0335 Unknown IOCB "
  2065. "command Data: x%x "
  2066. "x%x x%x x%x\n",
  2067. irsp->ulpCommand,
  2068. irsp->ulpStatus,
  2069. irsp->ulpIoTag,
  2070. irsp->ulpContext);
  2071. }
  2072. }
  2073. if (free_saveq) {
  2074. list_for_each_entry_safe(rspiocbp, next_iocb,
  2075. &saveq->list, list) {
  2076. list_del(&rspiocbp->list);
  2077. __lpfc_sli_release_iocbq(phba,
  2078. rspiocbp);
  2079. }
  2080. __lpfc_sli_release_iocbq(phba, saveq);
  2081. }
  2082. rspiocbp = NULL;
  2083. }
  2084. /*
  2085. * If the port response put pointer has not been updated, sync
  2086. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  2087. * response put pointer.
  2088. */
  2089. if (pring->rspidx == portRspPut) {
  2090. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2091. }
  2092. } /* while (pring->rspidx != portRspPut) */
  2093. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  2094. /* At least one response entry has been freed */
  2095. pring->stats.iocb_rsp_full++;
  2096. /* SET RxRE_RSP in Chip Att register */
  2097. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2098. writel(status, phba->CAregaddr);
  2099. readl(phba->CAregaddr); /* flush */
  2100. }
  2101. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2102. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2103. pring->stats.iocb_cmd_empty++;
  2104. /* Force update of the local copy of cmdGetInx */
  2105. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2106. lpfc_sli_resume_iocb(phba, pring);
  2107. if ((pring->lpfc_sli_cmd_available))
  2108. (pring->lpfc_sli_cmd_available) (phba, pring);
  2109. }
  2110. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2111. return rc;
  2112. }
  2113. /**
  2114. * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
  2115. * @phba: Pointer to HBA context object.
  2116. * @pring: Pointer to driver SLI ring object.
  2117. *
  2118. * This function aborts all iocbs in the given ring and frees all the iocb
  2119. * objects in txq. This function issues an abort iocb for all the iocb commands
  2120. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  2121. * the return of this function. The caller is not required to hold any locks.
  2122. **/
  2123. void
  2124. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2125. {
  2126. LIST_HEAD(completions);
  2127. struct lpfc_iocbq *iocb, *next_iocb;
  2128. IOCB_t *cmd = NULL;
  2129. if (pring->ringno == LPFC_ELS_RING) {
  2130. lpfc_fabric_abort_hba(phba);
  2131. }
  2132. /* Error everything on txq and txcmplq
  2133. * First do the txq.
  2134. */
  2135. spin_lock_irq(&phba->hbalock);
  2136. list_splice_init(&pring->txq, &completions);
  2137. pring->txq_cnt = 0;
  2138. /* Next issue ABTS for everything on the txcmplq */
  2139. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  2140. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2141. spin_unlock_irq(&phba->hbalock);
  2142. while (!list_empty(&completions)) {
  2143. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  2144. cmd = &iocb->iocb;
  2145. list_del_init(&iocb->list);
  2146. if (!iocb->iocb_cmpl)
  2147. lpfc_sli_release_iocbq(phba, iocb);
  2148. else {
  2149. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2150. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  2151. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2152. }
  2153. }
  2154. }
  2155. /**
  2156. * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
  2157. * @phba: Pointer to HBA context object.
  2158. *
  2159. * This function flushes all iocbs in the fcp ring and frees all the iocb
  2160. * objects in txq and txcmplq. This function will not issue abort iocbs
  2161. * for all the iocb commands in txcmplq, they will just be returned with
  2162. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2163. * slot has been permanently disabled.
  2164. **/
  2165. void
  2166. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  2167. {
  2168. LIST_HEAD(txq);
  2169. LIST_HEAD(txcmplq);
  2170. struct lpfc_iocbq *iocb;
  2171. IOCB_t *cmd = NULL;
  2172. struct lpfc_sli *psli = &phba->sli;
  2173. struct lpfc_sli_ring *pring;
  2174. /* Currently, only one fcp ring */
  2175. pring = &psli->ring[psli->fcp_ring];
  2176. spin_lock_irq(&phba->hbalock);
  2177. /* Retrieve everything on txq */
  2178. list_splice_init(&pring->txq, &txq);
  2179. pring->txq_cnt = 0;
  2180. /* Retrieve everything on the txcmplq */
  2181. list_splice_init(&pring->txcmplq, &txcmplq);
  2182. pring->txcmplq_cnt = 0;
  2183. spin_unlock_irq(&phba->hbalock);
  2184. /* Flush the txq */
  2185. while (!list_empty(&txq)) {
  2186. iocb = list_get_first(&txq, struct lpfc_iocbq, list);
  2187. cmd = &iocb->iocb;
  2188. list_del_init(&iocb->list);
  2189. if (!iocb->iocb_cmpl)
  2190. lpfc_sli_release_iocbq(phba, iocb);
  2191. else {
  2192. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2193. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2194. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2195. }
  2196. }
  2197. /* Flush the txcmpq */
  2198. while (!list_empty(&txcmplq)) {
  2199. iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
  2200. cmd = &iocb->iocb;
  2201. list_del_init(&iocb->list);
  2202. if (!iocb->iocb_cmpl)
  2203. lpfc_sli_release_iocbq(phba, iocb);
  2204. else {
  2205. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2206. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2207. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2208. }
  2209. }
  2210. }
  2211. /**
  2212. * lpfc_sli_brdready: Check for host status bits.
  2213. * @phba: Pointer to HBA context object.
  2214. * @mask: Bit mask to be checked.
  2215. *
  2216. * This function reads the host status register and compares
  2217. * with the provided bit mask to check if HBA completed
  2218. * the restart. This function will wait in a loop for the
  2219. * HBA to complete restart. If the HBA does not restart within
  2220. * 15 iterations, the function will reset the HBA again. The
  2221. * function returns 1 when HBA fail to restart otherwise returns
  2222. * zero.
  2223. **/
  2224. int
  2225. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  2226. {
  2227. uint32_t status;
  2228. int i = 0;
  2229. int retval = 0;
  2230. /* Read the HBA Host Status Register */
  2231. status = readl(phba->HSregaddr);
  2232. /*
  2233. * Check status register every 100ms for 5 retries, then every
  2234. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  2235. * every 2.5 sec for 4.
  2236. * Break our of the loop if errors occurred during init.
  2237. */
  2238. while (((status & mask) != mask) &&
  2239. !(status & HS_FFERM) &&
  2240. i++ < 20) {
  2241. if (i <= 5)
  2242. msleep(10);
  2243. else if (i <= 10)
  2244. msleep(500);
  2245. else
  2246. msleep(2500);
  2247. if (i == 15) {
  2248. /* Do post */
  2249. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2250. lpfc_sli_brdrestart(phba);
  2251. }
  2252. /* Read the HBA Host Status Register */
  2253. status = readl(phba->HSregaddr);
  2254. }
  2255. /* Check to see if any errors occurred during init */
  2256. if ((status & HS_FFERM) || (i >= 20)) {
  2257. phba->link_state = LPFC_HBA_ERROR;
  2258. retval = 1;
  2259. }
  2260. return retval;
  2261. }
  2262. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  2263. /**
  2264. * lpfc_reset_barrier: Make HBA ready for HBA reset.
  2265. * @phba: Pointer to HBA context object.
  2266. *
  2267. * This function is called before resetting an HBA. This
  2268. * function requests HBA to quiesce DMAs before a reset.
  2269. **/
  2270. void lpfc_reset_barrier(struct lpfc_hba *phba)
  2271. {
  2272. uint32_t __iomem *resp_buf;
  2273. uint32_t __iomem *mbox_buf;
  2274. volatile uint32_t mbox;
  2275. uint32_t hc_copy;
  2276. int i;
  2277. uint8_t hdrtype;
  2278. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  2279. if (hdrtype != 0x80 ||
  2280. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  2281. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  2282. return;
  2283. /*
  2284. * Tell the other part of the chip to suspend temporarily all
  2285. * its DMA activity.
  2286. */
  2287. resp_buf = phba->MBslimaddr;
  2288. /* Disable the error attention */
  2289. hc_copy = readl(phba->HCregaddr);
  2290. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  2291. readl(phba->HCregaddr); /* flush */
  2292. phba->link_flag |= LS_IGNORE_ERATT;
  2293. if (readl(phba->HAregaddr) & HA_ERATT) {
  2294. /* Clear Chip error bit */
  2295. writel(HA_ERATT, phba->HAregaddr);
  2296. phba->pport->stopped = 1;
  2297. }
  2298. mbox = 0;
  2299. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  2300. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  2301. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  2302. mbox_buf = phba->MBslimaddr;
  2303. writel(mbox, mbox_buf);
  2304. for (i = 0;
  2305. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  2306. mdelay(1);
  2307. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  2308. if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
  2309. phba->pport->stopped)
  2310. goto restore_hc;
  2311. else
  2312. goto clear_errat;
  2313. }
  2314. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  2315. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  2316. mdelay(1);
  2317. clear_errat:
  2318. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  2319. mdelay(1);
  2320. if (readl(phba->HAregaddr) & HA_ERATT) {
  2321. writel(HA_ERATT, phba->HAregaddr);
  2322. phba->pport->stopped = 1;
  2323. }
  2324. restore_hc:
  2325. phba->link_flag &= ~LS_IGNORE_ERATT;
  2326. writel(hc_copy, phba->HCregaddr);
  2327. readl(phba->HCregaddr); /* flush */
  2328. }
  2329. /**
  2330. * lpfc_sli_brdkill: Issue a kill_board mailbox command.
  2331. * @phba: Pointer to HBA context object.
  2332. *
  2333. * This function issues a kill_board mailbox command and waits for
  2334. * the error attention interrupt. This function is called for stopping
  2335. * the firmware processing. The caller is not required to hold any
  2336. * locks. This function calls lpfc_hba_down_post function to free
  2337. * any pending commands after the kill. The function will return 1 when it
  2338. * fails to kill the board else will return 0.
  2339. **/
  2340. int
  2341. lpfc_sli_brdkill(struct lpfc_hba *phba)
  2342. {
  2343. struct lpfc_sli *psli;
  2344. LPFC_MBOXQ_t *pmb;
  2345. uint32_t status;
  2346. uint32_t ha_copy;
  2347. int retval;
  2348. int i = 0;
  2349. psli = &phba->sli;
  2350. /* Kill HBA */
  2351. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2352. "0329 Kill HBA Data: x%x x%x\n",
  2353. phba->pport->port_state, psli->sli_flag);
  2354. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2355. if (!pmb)
  2356. return 1;
  2357. /* Disable the error attention */
  2358. spin_lock_irq(&phba->hbalock);
  2359. status = readl(phba->HCregaddr);
  2360. status &= ~HC_ERINT_ENA;
  2361. writel(status, phba->HCregaddr);
  2362. readl(phba->HCregaddr); /* flush */
  2363. phba->link_flag |= LS_IGNORE_ERATT;
  2364. spin_unlock_irq(&phba->hbalock);
  2365. lpfc_kill_board(phba, pmb);
  2366. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2367. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2368. if (retval != MBX_SUCCESS) {
  2369. if (retval != MBX_BUSY)
  2370. mempool_free(pmb, phba->mbox_mem_pool);
  2371. spin_lock_irq(&phba->hbalock);
  2372. phba->link_flag &= ~LS_IGNORE_ERATT;
  2373. spin_unlock_irq(&phba->hbalock);
  2374. return 1;
  2375. }
  2376. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2377. mempool_free(pmb, phba->mbox_mem_pool);
  2378. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  2379. * attention every 100ms for 3 seconds. If we don't get ERATT after
  2380. * 3 seconds we still set HBA_ERROR state because the status of the
  2381. * board is now undefined.
  2382. */
  2383. ha_copy = readl(phba->HAregaddr);
  2384. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  2385. mdelay(100);
  2386. ha_copy = readl(phba->HAregaddr);
  2387. }
  2388. del_timer_sync(&psli->mbox_tmo);
  2389. if (ha_copy & HA_ERATT) {
  2390. writel(HA_ERATT, phba->HAregaddr);
  2391. phba->pport->stopped = 1;
  2392. }
  2393. spin_lock_irq(&phba->hbalock);
  2394. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2395. phba->link_flag &= ~LS_IGNORE_ERATT;
  2396. spin_unlock_irq(&phba->hbalock);
  2397. psli->mbox_active = NULL;
  2398. lpfc_hba_down_post(phba);
  2399. phba->link_state = LPFC_HBA_ERROR;
  2400. return ha_copy & HA_ERATT ? 0 : 1;
  2401. }
  2402. /**
  2403. * lpfc_sli_brdreset: Reset the HBA.
  2404. * @phba: Pointer to HBA context object.
  2405. *
  2406. * This function resets the HBA by writing HC_INITFF to the control
  2407. * register. After the HBA resets, this function resets all the iocb ring
  2408. * indices. This function disables PCI layer parity checking during
  2409. * the reset.
  2410. * This function returns 0 always.
  2411. * The caller is not required to hold any locks.
  2412. **/
  2413. int
  2414. lpfc_sli_brdreset(struct lpfc_hba *phba)
  2415. {
  2416. struct lpfc_sli *psli;
  2417. struct lpfc_sli_ring *pring;
  2418. uint16_t cfg_value;
  2419. int i;
  2420. psli = &phba->sli;
  2421. /* Reset HBA */
  2422. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2423. "0325 Reset HBA Data: x%x x%x\n",
  2424. phba->pport->port_state, psli->sli_flag);
  2425. /* perform board reset */
  2426. phba->fc_eventTag = 0;
  2427. phba->pport->fc_myDID = 0;
  2428. phba->pport->fc_prevDID = 0;
  2429. /* Turn off parity checking and serr during the physical reset */
  2430. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  2431. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  2432. (cfg_value &
  2433. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  2434. psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
  2435. /* Now toggle INITFF bit in the Host Control Register */
  2436. writel(HC_INITFF, phba->HCregaddr);
  2437. mdelay(1);
  2438. readl(phba->HCregaddr); /* flush */
  2439. writel(0, phba->HCregaddr);
  2440. readl(phba->HCregaddr); /* flush */
  2441. /* Restore PCI cmd register */
  2442. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  2443. /* Initialize relevant SLI info */
  2444. for (i = 0; i < psli->num_rings; i++) {
  2445. pring = &psli->ring[i];
  2446. pring->flag = 0;
  2447. pring->rspidx = 0;
  2448. pring->next_cmdidx = 0;
  2449. pring->local_getidx = 0;
  2450. pring->cmdidx = 0;
  2451. pring->missbufcnt = 0;
  2452. }
  2453. phba->link_state = LPFC_WARM_START;
  2454. return 0;
  2455. }
  2456. /**
  2457. * lpfc_sli_brdrestart: Restart the HBA.
  2458. * @phba: Pointer to HBA context object.
  2459. *
  2460. * This function is called in the SLI initialization code path to
  2461. * restart the HBA. The caller is not required to hold any lock.
  2462. * This function writes MBX_RESTART mailbox command to the SLIM and
  2463. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  2464. * function to free any pending commands. The function enables
  2465. * POST only during the first initialization. The function returns zero.
  2466. * The function does not guarantee completion of MBX_RESTART mailbox
  2467. * command before the return of this function.
  2468. **/
  2469. int
  2470. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  2471. {
  2472. MAILBOX_t *mb;
  2473. struct lpfc_sli *psli;
  2474. volatile uint32_t word0;
  2475. void __iomem *to_slim;
  2476. spin_lock_irq(&phba->hbalock);
  2477. psli = &phba->sli;
  2478. /* Restart HBA */
  2479. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2480. "0337 Restart HBA Data: x%x x%x\n",
  2481. phba->pport->port_state, psli->sli_flag);
  2482. word0 = 0;
  2483. mb = (MAILBOX_t *) &word0;
  2484. mb->mbxCommand = MBX_RESTART;
  2485. mb->mbxHc = 1;
  2486. lpfc_reset_barrier(phba);
  2487. to_slim = phba->MBslimaddr;
  2488. writel(*(uint32_t *) mb, to_slim);
  2489. readl(to_slim); /* flush */
  2490. /* Only skip post after fc_ffinit is completed */
  2491. if (phba->pport->port_state)
  2492. word0 = 1; /* This is really setting up word1 */
  2493. else
  2494. word0 = 0; /* This is really setting up word1 */
  2495. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  2496. writel(*(uint32_t *) mb, to_slim);
  2497. readl(to_slim); /* flush */
  2498. lpfc_sli_brdreset(phba);
  2499. phba->pport->stopped = 0;
  2500. phba->link_state = LPFC_INIT_START;
  2501. spin_unlock_irq(&phba->hbalock);
  2502. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  2503. psli->stats_start = get_seconds();
  2504. /* Give the INITFF and Post time to settle. */
  2505. mdelay(100);
  2506. lpfc_hba_down_post(phba);
  2507. return 0;
  2508. }
  2509. /**
  2510. * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
  2511. * @phba: Pointer to HBA context object.
  2512. *
  2513. * This function is called after a HBA restart to wait for successful
  2514. * restart of the HBA. Successful restart of the HBA is indicated by
  2515. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  2516. * iteration, the function will restart the HBA again. The function returns
  2517. * zero if HBA successfully restarted else returns negative error code.
  2518. **/
  2519. static int
  2520. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  2521. {
  2522. uint32_t status, i = 0;
  2523. /* Read the HBA Host Status Register */
  2524. status = readl(phba->HSregaddr);
  2525. /* Check status register to see what current state is */
  2526. i = 0;
  2527. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  2528. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  2529. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  2530. * 4.
  2531. */
  2532. if (i++ >= 20) {
  2533. /* Adapter failed to init, timeout, status reg
  2534. <status> */
  2535. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2536. "0436 Adapter failed to init, "
  2537. "timeout, status reg x%x, "
  2538. "FW Data: A8 x%x AC x%x\n", status,
  2539. readl(phba->MBslimaddr + 0xa8),
  2540. readl(phba->MBslimaddr + 0xac));
  2541. phba->link_state = LPFC_HBA_ERROR;
  2542. return -ETIMEDOUT;
  2543. }
  2544. /* Check to see if any errors occurred during init */
  2545. if (status & HS_FFERM) {
  2546. /* ERROR: During chipset initialization */
  2547. /* Adapter failed to init, chipset, status reg
  2548. <status> */
  2549. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2550. "0437 Adapter failed to init, "
  2551. "chipset, status reg x%x, "
  2552. "FW Data: A8 x%x AC x%x\n", status,
  2553. readl(phba->MBslimaddr + 0xa8),
  2554. readl(phba->MBslimaddr + 0xac));
  2555. phba->link_state = LPFC_HBA_ERROR;
  2556. return -EIO;
  2557. }
  2558. if (i <= 5) {
  2559. msleep(10);
  2560. } else if (i <= 10) {
  2561. msleep(500);
  2562. } else {
  2563. msleep(2500);
  2564. }
  2565. if (i == 15) {
  2566. /* Do post */
  2567. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2568. lpfc_sli_brdrestart(phba);
  2569. }
  2570. /* Read the HBA Host Status Register */
  2571. status = readl(phba->HSregaddr);
  2572. }
  2573. /* Check to see if any errors occurred during init */
  2574. if (status & HS_FFERM) {
  2575. /* ERROR: During chipset initialization */
  2576. /* Adapter failed to init, chipset, status reg <status> */
  2577. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2578. "0438 Adapter failed to init, chipset, "
  2579. "status reg x%x, "
  2580. "FW Data: A8 x%x AC x%x\n", status,
  2581. readl(phba->MBslimaddr + 0xa8),
  2582. readl(phba->MBslimaddr + 0xac));
  2583. phba->link_state = LPFC_HBA_ERROR;
  2584. return -EIO;
  2585. }
  2586. /* Clear all interrupt enable conditions */
  2587. writel(0, phba->HCregaddr);
  2588. readl(phba->HCregaddr); /* flush */
  2589. /* setup host attn register */
  2590. writel(0xffffffff, phba->HAregaddr);
  2591. readl(phba->HAregaddr); /* flush */
  2592. return 0;
  2593. }
  2594. /**
  2595. * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
  2596. *
  2597. * This function calculates and returns the number of HBQs required to be
  2598. * configured.
  2599. **/
  2600. int
  2601. lpfc_sli_hbq_count(void)
  2602. {
  2603. return ARRAY_SIZE(lpfc_hbq_defs);
  2604. }
  2605. /**
  2606. * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
  2607. *
  2608. * This function adds the number of hbq entries in every HBQ to get
  2609. * the total number of hbq entries required for the HBA and returns
  2610. * the total count.
  2611. **/
  2612. static int
  2613. lpfc_sli_hbq_entry_count(void)
  2614. {
  2615. int hbq_count = lpfc_sli_hbq_count();
  2616. int count = 0;
  2617. int i;
  2618. for (i = 0; i < hbq_count; ++i)
  2619. count += lpfc_hbq_defs[i]->entry_count;
  2620. return count;
  2621. }
  2622. /**
  2623. * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
  2624. *
  2625. * This function calculates amount of memory required for all hbq entries
  2626. * to be configured and returns the total memory required.
  2627. **/
  2628. int
  2629. lpfc_sli_hbq_size(void)
  2630. {
  2631. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  2632. }
  2633. /**
  2634. * lpfc_sli_hbq_setup: configure and initialize HBQs.
  2635. * @phba: Pointer to HBA context object.
  2636. *
  2637. * This function is called during the SLI initialization to configure
  2638. * all the HBQs and post buffers to the HBQ. The caller is not
  2639. * required to hold any locks. This function will return zero if successful
  2640. * else it will return negative error code.
  2641. **/
  2642. static int
  2643. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  2644. {
  2645. int hbq_count = lpfc_sli_hbq_count();
  2646. LPFC_MBOXQ_t *pmb;
  2647. MAILBOX_t *pmbox;
  2648. uint32_t hbqno;
  2649. uint32_t hbq_entry_index;
  2650. /* Get a Mailbox buffer to setup mailbox
  2651. * commands for HBA initialization
  2652. */
  2653. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2654. if (!pmb)
  2655. return -ENOMEM;
  2656. pmbox = &pmb->mb;
  2657. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  2658. phba->link_state = LPFC_INIT_MBX_CMDS;
  2659. phba->hbq_in_use = 1;
  2660. hbq_entry_index = 0;
  2661. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  2662. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  2663. phba->hbqs[hbqno].hbqPutIdx = 0;
  2664. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  2665. phba->hbqs[hbqno].entry_count =
  2666. lpfc_hbq_defs[hbqno]->entry_count;
  2667. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  2668. hbq_entry_index, pmb);
  2669. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  2670. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  2671. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  2672. mbxStatus <status>, ring <num> */
  2673. lpfc_printf_log(phba, KERN_ERR,
  2674. LOG_SLI | LOG_VPORT,
  2675. "1805 Adapter failed to init. "
  2676. "Data: x%x x%x x%x\n",
  2677. pmbox->mbxCommand,
  2678. pmbox->mbxStatus, hbqno);
  2679. phba->link_state = LPFC_HBA_ERROR;
  2680. mempool_free(pmb, phba->mbox_mem_pool);
  2681. return ENXIO;
  2682. }
  2683. }
  2684. phba->hbq_count = hbq_count;
  2685. mempool_free(pmb, phba->mbox_mem_pool);
  2686. /* Initially populate or replenish the HBQs */
  2687. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  2688. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  2689. return 0;
  2690. }
  2691. /**
  2692. * lpfc_sli_config_port: Issue config port mailbox command.
  2693. * @phba: Pointer to HBA context object.
  2694. * @sli_mode: sli mode - 2/3
  2695. *
  2696. * This function is called by the sli intialization code path
  2697. * to issue config_port mailbox command. This function restarts the
  2698. * HBA firmware and issues a config_port mailbox command to configure
  2699. * the SLI interface in the sli mode specified by sli_mode
  2700. * variable. The caller is not required to hold any locks.
  2701. * The function returns 0 if successful, else returns negative error
  2702. * code.
  2703. **/
  2704. int
  2705. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  2706. {
  2707. LPFC_MBOXQ_t *pmb;
  2708. uint32_t resetcount = 0, rc = 0, done = 0;
  2709. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2710. if (!pmb) {
  2711. phba->link_state = LPFC_HBA_ERROR;
  2712. return -ENOMEM;
  2713. }
  2714. phba->sli_rev = sli_mode;
  2715. while (resetcount < 2 && !done) {
  2716. spin_lock_irq(&phba->hbalock);
  2717. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  2718. spin_unlock_irq(&phba->hbalock);
  2719. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2720. lpfc_sli_brdrestart(phba);
  2721. rc = lpfc_sli_chipset_init(phba);
  2722. if (rc)
  2723. break;
  2724. spin_lock_irq(&phba->hbalock);
  2725. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2726. spin_unlock_irq(&phba->hbalock);
  2727. resetcount++;
  2728. /* Call pre CONFIG_PORT mailbox command initialization. A
  2729. * value of 0 means the call was successful. Any other
  2730. * nonzero value is a failure, but if ERESTART is returned,
  2731. * the driver may reset the HBA and try again.
  2732. */
  2733. rc = lpfc_config_port_prep(phba);
  2734. if (rc == -ERESTART) {
  2735. phba->link_state = LPFC_LINK_UNKNOWN;
  2736. continue;
  2737. } else if (rc)
  2738. break;
  2739. phba->link_state = LPFC_INIT_MBX_CMDS;
  2740. lpfc_config_port(phba, pmb);
  2741. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  2742. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  2743. LPFC_SLI3_HBQ_ENABLED |
  2744. LPFC_SLI3_CRP_ENABLED |
  2745. LPFC_SLI3_INB_ENABLED |
  2746. LPFC_SLI3_BG_ENABLED);
  2747. if (rc != MBX_SUCCESS) {
  2748. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2749. "0442 Adapter failed to init, mbxCmd x%x "
  2750. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  2751. pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
  2752. spin_lock_irq(&phba->hbalock);
  2753. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  2754. spin_unlock_irq(&phba->hbalock);
  2755. rc = -ENXIO;
  2756. } else
  2757. done = 1;
  2758. }
  2759. if (!done) {
  2760. rc = -EINVAL;
  2761. goto do_prep_failed;
  2762. }
  2763. if (pmb->mb.un.varCfgPort.sli_mode == 3) {
  2764. if (!pmb->mb.un.varCfgPort.cMA) {
  2765. rc = -ENXIO;
  2766. goto do_prep_failed;
  2767. }
  2768. if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
  2769. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2770. phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
  2771. } else
  2772. phba->max_vpi = 0;
  2773. if (pmb->mb.un.varCfgPort.gerbm)
  2774. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  2775. if (pmb->mb.un.varCfgPort.gcrp)
  2776. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  2777. if (pmb->mb.un.varCfgPort.ginb) {
  2778. phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
  2779. phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
  2780. phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
  2781. phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
  2782. phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
  2783. phba->inb_last_counter =
  2784. phba->mbox->us.s3_inb_pgp.counter;
  2785. } else {
  2786. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  2787. phba->port_gp = phba->mbox->us.s3_pgp.port;
  2788. phba->inb_ha_copy = NULL;
  2789. phba->inb_counter = NULL;
  2790. }
  2791. if (phba->cfg_enable_bg) {
  2792. if (pmb->mb.un.varCfgPort.gbg)
  2793. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  2794. else
  2795. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2796. "0443 Adapter did not grant "
  2797. "BlockGuard\n");
  2798. }
  2799. } else {
  2800. phba->hbq_get = NULL;
  2801. phba->port_gp = phba->mbox->us.s2.port;
  2802. phba->inb_ha_copy = NULL;
  2803. phba->inb_counter = NULL;
  2804. phba->max_vpi = 0;
  2805. }
  2806. do_prep_failed:
  2807. mempool_free(pmb, phba->mbox_mem_pool);
  2808. return rc;
  2809. }
  2810. /**
  2811. * lpfc_sli_hba_setup: SLI intialization function.
  2812. * @phba: Pointer to HBA context object.
  2813. *
  2814. * This function is the main SLI intialization function. This function
  2815. * is called by the HBA intialization code, HBA reset code and HBA
  2816. * error attention handler code. Caller is not required to hold any
  2817. * locks. This function issues config_port mailbox command to configure
  2818. * the SLI, setup iocb rings and HBQ rings. In the end the function
  2819. * calls the config_port_post function to issue init_link mailbox
  2820. * command and to start the discovery. The function will return zero
  2821. * if successful, else it will return negative error code.
  2822. **/
  2823. int
  2824. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  2825. {
  2826. uint32_t rc;
  2827. int mode = 3;
  2828. switch (lpfc_sli_mode) {
  2829. case 2:
  2830. if (phba->cfg_enable_npiv) {
  2831. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2832. "1824 NPIV enabled: Override lpfc_sli_mode "
  2833. "parameter (%d) to auto (0).\n",
  2834. lpfc_sli_mode);
  2835. break;
  2836. }
  2837. mode = 2;
  2838. break;
  2839. case 0:
  2840. case 3:
  2841. break;
  2842. default:
  2843. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2844. "1819 Unrecognized lpfc_sli_mode "
  2845. "parameter: %d.\n", lpfc_sli_mode);
  2846. break;
  2847. }
  2848. rc = lpfc_sli_config_port(phba, mode);
  2849. if (rc && lpfc_sli_mode == 3)
  2850. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2851. "1820 Unable to select SLI-3. "
  2852. "Not supported by adapter.\n");
  2853. if (rc && mode != 2)
  2854. rc = lpfc_sli_config_port(phba, 2);
  2855. if (rc)
  2856. goto lpfc_sli_hba_setup_error;
  2857. if (phba->sli_rev == 3) {
  2858. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  2859. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  2860. } else {
  2861. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  2862. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  2863. phba->sli3_options = 0;
  2864. }
  2865. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2866. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  2867. phba->sli_rev, phba->max_vpi);
  2868. rc = lpfc_sli_ring_map(phba);
  2869. if (rc)
  2870. goto lpfc_sli_hba_setup_error;
  2871. /* Init HBQs */
  2872. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2873. rc = lpfc_sli_hbq_setup(phba);
  2874. if (rc)
  2875. goto lpfc_sli_hba_setup_error;
  2876. }
  2877. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  2878. rc = lpfc_config_port_post(phba);
  2879. if (rc)
  2880. goto lpfc_sli_hba_setup_error;
  2881. return rc;
  2882. lpfc_sli_hba_setup_error:
  2883. phba->link_state = LPFC_HBA_ERROR;
  2884. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2885. "0445 Firmware initialization failed\n");
  2886. return rc;
  2887. }
  2888. /**
  2889. * lpfc_mbox_timeout: Timeout call back function for mbox timer.
  2890. * @ptr: context object - pointer to hba structure.
  2891. *
  2892. * This is the callback function for mailbox timer. The mailbox
  2893. * timer is armed when a new mailbox command is issued and the timer
  2894. * is deleted when the mailbox complete. The function is called by
  2895. * the kernel timer code when a mailbox does not complete within
  2896. * expected time. This function wakes up the worker thread to
  2897. * process the mailbox timeout and returns. All the processing is
  2898. * done by the worker thread function lpfc_mbox_timeout_handler.
  2899. **/
  2900. void
  2901. lpfc_mbox_timeout(unsigned long ptr)
  2902. {
  2903. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2904. unsigned long iflag;
  2905. uint32_t tmo_posted;
  2906. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  2907. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  2908. if (!tmo_posted)
  2909. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  2910. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  2911. if (!tmo_posted)
  2912. lpfc_worker_wake_up(phba);
  2913. return;
  2914. }
  2915. /**
  2916. * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
  2917. * @phba: Pointer to HBA context object.
  2918. *
  2919. * This function is called from worker thread when a mailbox command times out.
  2920. * The caller is not required to hold any locks. This function will reset the
  2921. * HBA and recover all the pending commands.
  2922. **/
  2923. void
  2924. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  2925. {
  2926. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  2927. MAILBOX_t *mb = &pmbox->mb;
  2928. struct lpfc_sli *psli = &phba->sli;
  2929. struct lpfc_sli_ring *pring;
  2930. /* Mbox cmd <mbxCommand> timeout */
  2931. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2932. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  2933. mb->mbxCommand,
  2934. phba->pport->port_state,
  2935. phba->sli.sli_flag,
  2936. phba->sli.mbox_active);
  2937. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  2938. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  2939. * it to fail all oustanding SCSI IO.
  2940. */
  2941. spin_lock_irq(&phba->pport->work_port_lock);
  2942. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  2943. spin_unlock_irq(&phba->pport->work_port_lock);
  2944. spin_lock_irq(&phba->hbalock);
  2945. phba->link_state = LPFC_LINK_UNKNOWN;
  2946. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2947. spin_unlock_irq(&phba->hbalock);
  2948. pring = &psli->ring[psli->fcp_ring];
  2949. lpfc_sli_abort_iocb_ring(phba, pring);
  2950. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2951. "0345 Resetting board due to mailbox timeout\n");
  2952. /*
  2953. * lpfc_offline calls lpfc_sli_hba_down which will clean up
  2954. * on oustanding mailbox commands.
  2955. */
  2956. /* If resets are disabled then set error state and return. */
  2957. if (!phba->cfg_enable_hba_reset) {
  2958. phba->link_state = LPFC_HBA_ERROR;
  2959. return;
  2960. }
  2961. lpfc_offline_prep(phba);
  2962. lpfc_offline(phba);
  2963. lpfc_sli_brdrestart(phba);
  2964. lpfc_online(phba);
  2965. lpfc_unblock_mgmt_io(phba);
  2966. return;
  2967. }
  2968. /**
  2969. * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
  2970. * @phba: Pointer to HBA context object.
  2971. * @pmbox: Pointer to mailbox object.
  2972. * @flag: Flag indicating how the mailbox need to be processed.
  2973. *
  2974. * This function is called by discovery code and HBA management code
  2975. * to submit a mailbox command to firmware. This function gets the
  2976. * hbalock to protect the data structures.
  2977. * The mailbox command can be submitted in polling mode, in which case
  2978. * this function will wait in a polling loop for the completion of the
  2979. * mailbox.
  2980. * If the mailbox is submitted in no_wait mode (not polling) the
  2981. * function will submit the command and returns immediately without waiting
  2982. * for the mailbox completion. The no_wait is supported only when HBA
  2983. * is in SLI2/SLI3 mode - interrupts are enabled.
  2984. * The SLI interface allows only one mailbox pending at a time. If the
  2985. * mailbox is issued in polling mode and there is already a mailbox
  2986. * pending, then the function will return an error. If the mailbox is issued
  2987. * in NO_WAIT mode and there is a mailbox pending already, the function
  2988. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  2989. * The sli layer owns the mailbox object until the completion of mailbox
  2990. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  2991. * return codes the caller owns the mailbox command after the return of
  2992. * the function.
  2993. **/
  2994. int
  2995. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  2996. {
  2997. MAILBOX_t *mb;
  2998. struct lpfc_sli *psli = &phba->sli;
  2999. uint32_t status, evtctr;
  3000. uint32_t ha_copy;
  3001. int i;
  3002. unsigned long timeout;
  3003. unsigned long drvr_flag = 0;
  3004. uint32_t word0, ldata;
  3005. void __iomem *to_slim;
  3006. int processing_queue = 0;
  3007. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3008. if (!pmbox) {
  3009. /* processing mbox queue from intr_handler */
  3010. processing_queue = 1;
  3011. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3012. pmbox = lpfc_mbox_get(phba);
  3013. if (!pmbox) {
  3014. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3015. return MBX_SUCCESS;
  3016. }
  3017. }
  3018. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  3019. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  3020. if(!pmbox->vport) {
  3021. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3022. lpfc_printf_log(phba, KERN_ERR,
  3023. LOG_MBOX | LOG_VPORT,
  3024. "1806 Mbox x%x failed. No vport\n",
  3025. pmbox->mb.mbxCommand);
  3026. dump_stack();
  3027. goto out_not_finished;
  3028. }
  3029. }
  3030. /* If the PCI channel is in offline state, do not post mbox. */
  3031. if (unlikely(pci_channel_offline(phba->pcidev))) {
  3032. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3033. goto out_not_finished;
  3034. }
  3035. psli = &phba->sli;
  3036. mb = &pmbox->mb;
  3037. status = MBX_SUCCESS;
  3038. if (phba->link_state == LPFC_HBA_ERROR) {
  3039. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3040. /* Mbox command <mbxCommand> cannot issue */
  3041. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3042. goto out_not_finished;
  3043. }
  3044. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  3045. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  3046. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3047. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3048. goto out_not_finished;
  3049. }
  3050. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  3051. /* Polling for a mbox command when another one is already active
  3052. * is not allowed in SLI. Also, the driver must have established
  3053. * SLI2 mode to queue and process multiple mbox commands.
  3054. */
  3055. if (flag & MBX_POLL) {
  3056. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3057. /* Mbox command <mbxCommand> cannot issue */
  3058. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3059. goto out_not_finished;
  3060. }
  3061. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  3062. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3063. /* Mbox command <mbxCommand> cannot issue */
  3064. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3065. goto out_not_finished;
  3066. }
  3067. /* Another mailbox command is still being processed, queue this
  3068. * command to be processed later.
  3069. */
  3070. lpfc_mbox_put(phba, pmbox);
  3071. /* Mbox cmd issue - BUSY */
  3072. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3073. "(%d):0308 Mbox cmd issue - BUSY Data: "
  3074. "x%x x%x x%x x%x\n",
  3075. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  3076. mb->mbxCommand, phba->pport->port_state,
  3077. psli->sli_flag, flag);
  3078. psli->slistat.mbox_busy++;
  3079. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3080. if (pmbox->vport) {
  3081. lpfc_debugfs_disc_trc(pmbox->vport,
  3082. LPFC_DISC_TRC_MBOX_VPORT,
  3083. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  3084. (uint32_t)mb->mbxCommand,
  3085. mb->un.varWords[0], mb->un.varWords[1]);
  3086. }
  3087. else {
  3088. lpfc_debugfs_disc_trc(phba->pport,
  3089. LPFC_DISC_TRC_MBOX,
  3090. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  3091. (uint32_t)mb->mbxCommand,
  3092. mb->un.varWords[0], mb->un.varWords[1]);
  3093. }
  3094. return MBX_BUSY;
  3095. }
  3096. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3097. /* If we are not polling, we MUST be in SLI2 mode */
  3098. if (flag != MBX_POLL) {
  3099. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
  3100. (mb->mbxCommand != MBX_KILL_BOARD)) {
  3101. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3102. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3103. /* Mbox command <mbxCommand> cannot issue */
  3104. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3105. goto out_not_finished;
  3106. }
  3107. /* timeout active mbox command */
  3108. mod_timer(&psli->mbox_tmo, (jiffies +
  3109. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  3110. }
  3111. /* Mailbox cmd <cmd> issue */
  3112. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3113. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  3114. "x%x\n",
  3115. pmbox->vport ? pmbox->vport->vpi : 0,
  3116. mb->mbxCommand, phba->pport->port_state,
  3117. psli->sli_flag, flag);
  3118. if (mb->mbxCommand != MBX_HEARTBEAT) {
  3119. if (pmbox->vport) {
  3120. lpfc_debugfs_disc_trc(pmbox->vport,
  3121. LPFC_DISC_TRC_MBOX_VPORT,
  3122. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  3123. (uint32_t)mb->mbxCommand,
  3124. mb->un.varWords[0], mb->un.varWords[1]);
  3125. }
  3126. else {
  3127. lpfc_debugfs_disc_trc(phba->pport,
  3128. LPFC_DISC_TRC_MBOX,
  3129. "MBOX Send: cmd:x%x mb:x%x x%x",
  3130. (uint32_t)mb->mbxCommand,
  3131. mb->un.varWords[0], mb->un.varWords[1]);
  3132. }
  3133. }
  3134. psli->slistat.mbox_cmd++;
  3135. evtctr = psli->slistat.mbox_event;
  3136. /* next set own bit for the adapter and copy over command word */
  3137. mb->mbxOwner = OWN_CHIP;
  3138. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3139. /* First copy command data to host SLIM area */
  3140. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3141. } else {
  3142. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3143. /* copy command data into host mbox for cmpl */
  3144. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3145. }
  3146. /* First copy mbox command data to HBA SLIM, skip past first
  3147. word */
  3148. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3149. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  3150. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  3151. /* Next copy over first word, with mbxOwner set */
  3152. ldata = *((uint32_t *)mb);
  3153. to_slim = phba->MBslimaddr;
  3154. writel(ldata, to_slim);
  3155. readl(to_slim); /* flush */
  3156. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3157. /* switch over to host mailbox */
  3158. psli->sli_flag |= LPFC_SLI2_ACTIVE;
  3159. }
  3160. }
  3161. wmb();
  3162. switch (flag) {
  3163. case MBX_NOWAIT:
  3164. /* Set up reference to mailbox command */
  3165. psli->mbox_active = pmbox;
  3166. /* Interrupt board to do it */
  3167. writel(CA_MBATT, phba->CAregaddr);
  3168. readl(phba->CAregaddr); /* flush */
  3169. /* Don't wait for it to finish, just return */
  3170. break;
  3171. case MBX_POLL:
  3172. /* Set up null reference to mailbox command */
  3173. psli->mbox_active = NULL;
  3174. /* Interrupt board to do it */
  3175. writel(CA_MBATT, phba->CAregaddr);
  3176. readl(phba->CAregaddr); /* flush */
  3177. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3178. /* First read mbox status word */
  3179. word0 = *((uint32_t *)phba->mbox);
  3180. word0 = le32_to_cpu(word0);
  3181. } else {
  3182. /* First read mbox status word */
  3183. word0 = readl(phba->MBslimaddr);
  3184. }
  3185. /* Read the HBA Host Attention Register */
  3186. ha_copy = readl(phba->HAregaddr);
  3187. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  3188. mb->mbxCommand) *
  3189. 1000) + jiffies;
  3190. i = 0;
  3191. /* Wait for command to complete */
  3192. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  3193. (!(ha_copy & HA_MBATT) &&
  3194. (phba->link_state > LPFC_WARM_START))) {
  3195. if (time_after(jiffies, timeout)) {
  3196. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3197. spin_unlock_irqrestore(&phba->hbalock,
  3198. drvr_flag);
  3199. goto out_not_finished;
  3200. }
  3201. /* Check if we took a mbox interrupt while we were
  3202. polling */
  3203. if (((word0 & OWN_CHIP) != OWN_CHIP)
  3204. && (evtctr != psli->slistat.mbox_event))
  3205. break;
  3206. if (i++ > 10) {
  3207. spin_unlock_irqrestore(&phba->hbalock,
  3208. drvr_flag);
  3209. msleep(1);
  3210. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3211. }
  3212. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3213. /* First copy command data */
  3214. word0 = *((uint32_t *)phba->mbox);
  3215. word0 = le32_to_cpu(word0);
  3216. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3217. MAILBOX_t *slimmb;
  3218. uint32_t slimword0;
  3219. /* Check real SLIM for any errors */
  3220. slimword0 = readl(phba->MBslimaddr);
  3221. slimmb = (MAILBOX_t *) & slimword0;
  3222. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  3223. && slimmb->mbxStatus) {
  3224. psli->sli_flag &=
  3225. ~LPFC_SLI2_ACTIVE;
  3226. word0 = slimword0;
  3227. }
  3228. }
  3229. } else {
  3230. /* First copy command data */
  3231. word0 = readl(phba->MBslimaddr);
  3232. }
  3233. /* Read the HBA Host Attention Register */
  3234. ha_copy = readl(phba->HAregaddr);
  3235. }
  3236. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3237. /* copy results back to user */
  3238. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  3239. } else {
  3240. /* First copy command data */
  3241. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  3242. MAILBOX_CMD_SIZE);
  3243. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  3244. pmbox->context2) {
  3245. lpfc_memcpy_from_slim((void *)pmbox->context2,
  3246. phba->MBslimaddr + DMP_RSP_OFFSET,
  3247. mb->un.varDmp.word_cnt);
  3248. }
  3249. }
  3250. writel(HA_MBATT, phba->HAregaddr);
  3251. readl(phba->HAregaddr); /* flush */
  3252. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3253. status = mb->mbxStatus;
  3254. }
  3255. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3256. return status;
  3257. out_not_finished:
  3258. if (processing_queue) {
  3259. pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
  3260. lpfc_mbox_cmpl_put(phba, pmbox);
  3261. }
  3262. return MBX_NOT_FINISHED;
  3263. }
  3264. /**
  3265. * __lpfc_sli_ringtx_put: Add an iocb to the txq.
  3266. * @phba: Pointer to HBA context object.
  3267. * @pring: Pointer to driver SLI ring object.
  3268. * @piocb: Pointer to address of newly added command iocb.
  3269. *
  3270. * This function is called with hbalock held to add a command
  3271. * iocb to the txq when SLI layer cannot submit the command iocb
  3272. * to the ring.
  3273. **/
  3274. static void
  3275. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3276. struct lpfc_iocbq *piocb)
  3277. {
  3278. /* Insert the caller's iocb in the txq tail for later processing. */
  3279. list_add_tail(&piocb->list, &pring->txq);
  3280. pring->txq_cnt++;
  3281. }
  3282. /**
  3283. * lpfc_sli_next_iocb: Get the next iocb in the txq.
  3284. * @phba: Pointer to HBA context object.
  3285. * @pring: Pointer to driver SLI ring object.
  3286. * @piocb: Pointer to address of newly added command iocb.
  3287. *
  3288. * This function is called with hbalock held before a new
  3289. * iocb is submitted to the firmware. This function checks
  3290. * txq to flush the iocbs in txq to Firmware before
  3291. * submitting new iocbs to the Firmware.
  3292. * If there are iocbs in the txq which need to be submitted
  3293. * to firmware, lpfc_sli_next_iocb returns the first element
  3294. * of the txq after dequeuing it from txq.
  3295. * If there is no iocb in the txq then the function will return
  3296. * *piocb and *piocb is set to NULL. Caller needs to check
  3297. * *piocb to find if there are more commands in the txq.
  3298. **/
  3299. static struct lpfc_iocbq *
  3300. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3301. struct lpfc_iocbq **piocb)
  3302. {
  3303. struct lpfc_iocbq * nextiocb;
  3304. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  3305. if (!nextiocb) {
  3306. nextiocb = *piocb;
  3307. *piocb = NULL;
  3308. }
  3309. return nextiocb;
  3310. }
  3311. /**
  3312. * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
  3313. * @phba: Pointer to HBA context object.
  3314. * @pring: Pointer to driver SLI ring object.
  3315. * @piocb: Pointer to command iocb.
  3316. * @flag: Flag indicating if this command can be put into txq.
  3317. *
  3318. * __lpfc_sli_issue_iocb is used by other functions in the driver
  3319. * to issue an iocb command to the HBA. If the PCI slot is recovering
  3320. * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  3321. * flag is turned on, the function returns IOCB_ERROR.
  3322. * When the link is down, this function allows only iocbs for
  3323. * posting buffers.
  3324. * This function finds next available slot in the command ring and
  3325. * posts the command to the available slot and writes the port
  3326. * attention register to request HBA start processing new iocb.
  3327. * If there is no slot available in the ring and
  3328. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
  3329. * txq, otherwise the function returns IOCB_BUSY.
  3330. *
  3331. * This function is called with hbalock held.
  3332. * The function will return success after it successfully submit the
  3333. * iocb to firmware or after adding to the txq.
  3334. **/
  3335. static int
  3336. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3337. struct lpfc_iocbq *piocb, uint32_t flag)
  3338. {
  3339. struct lpfc_iocbq *nextiocb;
  3340. IOCB_t *iocb;
  3341. if (piocb->iocb_cmpl && (!piocb->vport) &&
  3342. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  3343. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  3344. lpfc_printf_log(phba, KERN_ERR,
  3345. LOG_SLI | LOG_VPORT,
  3346. "1807 IOCB x%x failed. No vport\n",
  3347. piocb->iocb.ulpCommand);
  3348. dump_stack();
  3349. return IOCB_ERROR;
  3350. }
  3351. /* If the PCI channel is in offline state, do not post iocbs. */
  3352. if (unlikely(pci_channel_offline(phba->pcidev)))
  3353. return IOCB_ERROR;
  3354. /*
  3355. * We should never get an IOCB if we are in a < LINK_DOWN state
  3356. */
  3357. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  3358. return IOCB_ERROR;
  3359. /*
  3360. * Check to see if we are blocking IOCB processing because of a
  3361. * outstanding event.
  3362. */
  3363. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  3364. goto iocb_busy;
  3365. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  3366. /*
  3367. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  3368. * can be issued if the link is not up.
  3369. */
  3370. switch (piocb->iocb.ulpCommand) {
  3371. case CMD_GEN_REQUEST64_CR:
  3372. case CMD_GEN_REQUEST64_CX:
  3373. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  3374. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  3375. FC_FCP_CMND) ||
  3376. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  3377. MENLO_TRANSPORT_TYPE))
  3378. goto iocb_busy;
  3379. break;
  3380. case CMD_QUE_RING_BUF_CN:
  3381. case CMD_QUE_RING_BUF64_CN:
  3382. /*
  3383. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  3384. * completion, iocb_cmpl MUST be 0.
  3385. */
  3386. if (piocb->iocb_cmpl)
  3387. piocb->iocb_cmpl = NULL;
  3388. /*FALLTHROUGH*/
  3389. case CMD_CREATE_XRI_CR:
  3390. case CMD_CLOSE_XRI_CN:
  3391. case CMD_CLOSE_XRI_CX:
  3392. break;
  3393. default:
  3394. goto iocb_busy;
  3395. }
  3396. /*
  3397. * For FCP commands, we must be in a state where we can process link
  3398. * attention events.
  3399. */
  3400. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  3401. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  3402. goto iocb_busy;
  3403. }
  3404. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  3405. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  3406. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  3407. if (iocb)
  3408. lpfc_sli_update_ring(phba, pring);
  3409. else
  3410. lpfc_sli_update_full_ring(phba, pring);
  3411. if (!piocb)
  3412. return IOCB_SUCCESS;
  3413. goto out_busy;
  3414. iocb_busy:
  3415. pring->stats.iocb_cmd_delay++;
  3416. out_busy:
  3417. if (!(flag & SLI_IOCB_RET_IOCB)) {
  3418. __lpfc_sli_ringtx_put(phba, pring, piocb);
  3419. return IOCB_SUCCESS;
  3420. }
  3421. return IOCB_BUSY;
  3422. }
  3423. /**
  3424. * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
  3425. * @phba: Pointer to HBA context object.
  3426. * @pring: Pointer to driver SLI ring object.
  3427. * @piocb: Pointer to command iocb.
  3428. * @flag: Flag indicating if this command can be put into txq.
  3429. *
  3430. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  3431. * function. This function gets the hbalock and calls
  3432. * __lpfc_sli_issue_iocb function and will return the error returned
  3433. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  3434. * functions which do not hold hbalock.
  3435. **/
  3436. int
  3437. lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3438. struct lpfc_iocbq *piocb, uint32_t flag)
  3439. {
  3440. unsigned long iflags;
  3441. int rc;
  3442. spin_lock_irqsave(&phba->hbalock, iflags);
  3443. rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
  3444. spin_unlock_irqrestore(&phba->hbalock, iflags);
  3445. return rc;
  3446. }
  3447. /**
  3448. * lpfc_extra_ring_setup: Extra ring setup function.
  3449. * @phba: Pointer to HBA context object.
  3450. *
  3451. * This function is called while driver attaches with the
  3452. * HBA to setup the extra ring. The extra ring is used
  3453. * only when driver needs to support target mode functionality
  3454. * or IP over FC functionalities.
  3455. *
  3456. * This function is called with no lock held.
  3457. **/
  3458. static int
  3459. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  3460. {
  3461. struct lpfc_sli *psli;
  3462. struct lpfc_sli_ring *pring;
  3463. psli = &phba->sli;
  3464. /* Adjust cmd/rsp ring iocb entries more evenly */
  3465. /* Take some away from the FCP ring */
  3466. pring = &psli->ring[psli->fcp_ring];
  3467. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3468. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3469. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3470. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3471. /* and give them to the extra ring */
  3472. pring = &psli->ring[psli->extra_ring];
  3473. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3474. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3475. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3476. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3477. /* Setup default profile for this ring */
  3478. pring->iotag_max = 4096;
  3479. pring->num_mask = 1;
  3480. pring->prt[0].profile = 0; /* Mask 0 */
  3481. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  3482. pring->prt[0].type = phba->cfg_multi_ring_type;
  3483. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  3484. return 0;
  3485. }
  3486. /**
  3487. * lpfc_sli_async_event_handler: ASYNC iocb handler function.
  3488. * @phba: Pointer to HBA context object.
  3489. * @pring: Pointer to driver SLI ring object.
  3490. * @iocbq: Pointer to iocb object.
  3491. *
  3492. * This function is called by the slow ring event handler
  3493. * function when there is an ASYNC event iocb in the ring.
  3494. * This function is called with no lock held.
  3495. * Currently this function handles only temperature related
  3496. * ASYNC events. The function decodes the temperature sensor
  3497. * event message and posts events for the management applications.
  3498. **/
  3499. static void
  3500. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  3501. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  3502. {
  3503. IOCB_t *icmd;
  3504. uint16_t evt_code;
  3505. uint16_t temp;
  3506. struct temp_event temp_event_data;
  3507. struct Scsi_Host *shost;
  3508. icmd = &iocbq->iocb;
  3509. evt_code = icmd->un.asyncstat.evt_code;
  3510. temp = icmd->ulpContext;
  3511. if ((evt_code != ASYNC_TEMP_WARN) &&
  3512. (evt_code != ASYNC_TEMP_SAFE)) {
  3513. lpfc_printf_log(phba,
  3514. KERN_ERR,
  3515. LOG_SLI,
  3516. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  3517. " evt_code 0x%x\n",
  3518. pring->ringno,
  3519. icmd->un.asyncstat.evt_code);
  3520. return;
  3521. }
  3522. temp_event_data.data = (uint32_t)temp;
  3523. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  3524. if (evt_code == ASYNC_TEMP_WARN) {
  3525. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  3526. lpfc_printf_log(phba,
  3527. KERN_ERR,
  3528. LOG_TEMP,
  3529. "0347 Adapter is very hot, please take "
  3530. "corrective action. temperature : %d Celsius\n",
  3531. temp);
  3532. }
  3533. if (evt_code == ASYNC_TEMP_SAFE) {
  3534. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  3535. lpfc_printf_log(phba,
  3536. KERN_ERR,
  3537. LOG_TEMP,
  3538. "0340 Adapter temperature is OK now. "
  3539. "temperature : %d Celsius\n",
  3540. temp);
  3541. }
  3542. /* Send temperature change event to applications */
  3543. shost = lpfc_shost_from_vport(phba->pport);
  3544. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3545. sizeof(temp_event_data), (char *) &temp_event_data,
  3546. LPFC_NL_VENDOR_ID);
  3547. }
  3548. /**
  3549. * lpfc_sli_setup: SLI ring setup function.
  3550. * @phba: Pointer to HBA context object.
  3551. *
  3552. * lpfc_sli_setup sets up rings of the SLI interface with
  3553. * number of iocbs per ring and iotags. This function is
  3554. * called while driver attach to the HBA and before the
  3555. * interrupts are enabled. So there is no need for locking.
  3556. *
  3557. * This function always returns 0.
  3558. **/
  3559. int
  3560. lpfc_sli_setup(struct lpfc_hba *phba)
  3561. {
  3562. int i, totiocbsize = 0;
  3563. struct lpfc_sli *psli = &phba->sli;
  3564. struct lpfc_sli_ring *pring;
  3565. psli->num_rings = MAX_CONFIGURED_RINGS;
  3566. psli->sli_flag = 0;
  3567. psli->fcp_ring = LPFC_FCP_RING;
  3568. psli->next_ring = LPFC_FCP_NEXT_RING;
  3569. psli->extra_ring = LPFC_EXTRA_RING;
  3570. psli->iocbq_lookup = NULL;
  3571. psli->iocbq_lookup_len = 0;
  3572. psli->last_iotag = 0;
  3573. for (i = 0; i < psli->num_rings; i++) {
  3574. pring = &psli->ring[i];
  3575. switch (i) {
  3576. case LPFC_FCP_RING: /* ring 0 - FCP */
  3577. /* numCiocb and numRiocb are used in config_port */
  3578. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  3579. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  3580. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3581. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3582. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3583. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3584. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3585. SLI3_IOCB_CMD_SIZE :
  3586. SLI2_IOCB_CMD_SIZE;
  3587. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3588. SLI3_IOCB_RSP_SIZE :
  3589. SLI2_IOCB_RSP_SIZE;
  3590. pring->iotag_ctr = 0;
  3591. pring->iotag_max =
  3592. (phba->cfg_hba_queue_depth * 2);
  3593. pring->fast_iotag = pring->iotag_max;
  3594. pring->num_mask = 0;
  3595. break;
  3596. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  3597. /* numCiocb and numRiocb are used in config_port */
  3598. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  3599. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  3600. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3601. SLI3_IOCB_CMD_SIZE :
  3602. SLI2_IOCB_CMD_SIZE;
  3603. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3604. SLI3_IOCB_RSP_SIZE :
  3605. SLI2_IOCB_RSP_SIZE;
  3606. pring->iotag_max = phba->cfg_hba_queue_depth;
  3607. pring->num_mask = 0;
  3608. break;
  3609. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  3610. /* numCiocb and numRiocb are used in config_port */
  3611. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  3612. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  3613. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3614. SLI3_IOCB_CMD_SIZE :
  3615. SLI2_IOCB_CMD_SIZE;
  3616. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3617. SLI3_IOCB_RSP_SIZE :
  3618. SLI2_IOCB_RSP_SIZE;
  3619. pring->fast_iotag = 0;
  3620. pring->iotag_ctr = 0;
  3621. pring->iotag_max = 4096;
  3622. pring->lpfc_sli_rcv_async_status =
  3623. lpfc_sli_async_event_handler;
  3624. pring->num_mask = 4;
  3625. pring->prt[0].profile = 0; /* Mask 0 */
  3626. pring->prt[0].rctl = FC_ELS_REQ;
  3627. pring->prt[0].type = FC_ELS_DATA;
  3628. pring->prt[0].lpfc_sli_rcv_unsol_event =
  3629. lpfc_els_unsol_event;
  3630. pring->prt[1].profile = 0; /* Mask 1 */
  3631. pring->prt[1].rctl = FC_ELS_RSP;
  3632. pring->prt[1].type = FC_ELS_DATA;
  3633. pring->prt[1].lpfc_sli_rcv_unsol_event =
  3634. lpfc_els_unsol_event;
  3635. pring->prt[2].profile = 0; /* Mask 2 */
  3636. /* NameServer Inquiry */
  3637. pring->prt[2].rctl = FC_UNSOL_CTL;
  3638. /* NameServer */
  3639. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  3640. pring->prt[2].lpfc_sli_rcv_unsol_event =
  3641. lpfc_ct_unsol_event;
  3642. pring->prt[3].profile = 0; /* Mask 3 */
  3643. /* NameServer response */
  3644. pring->prt[3].rctl = FC_SOL_CTL;
  3645. /* NameServer */
  3646. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  3647. pring->prt[3].lpfc_sli_rcv_unsol_event =
  3648. lpfc_ct_unsol_event;
  3649. break;
  3650. }
  3651. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  3652. (pring->numRiocb * pring->sizeRiocb);
  3653. }
  3654. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  3655. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  3656. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  3657. "SLI2 SLIM Data: x%x x%lx\n",
  3658. phba->brd_no, totiocbsize,
  3659. (unsigned long) MAX_SLIM_IOCB_SIZE);
  3660. }
  3661. if (phba->cfg_multi_ring_support == 2)
  3662. lpfc_extra_ring_setup(phba);
  3663. return 0;
  3664. }
  3665. /**
  3666. * lpfc_sli_queue_setup: Queue initialization function.
  3667. * @phba: Pointer to HBA context object.
  3668. *
  3669. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  3670. * ring. This function also initializes ring indices of each ring.
  3671. * This function is called during the initialization of the SLI
  3672. * interface of an HBA.
  3673. * This function is called with no lock held and always returns
  3674. * 1.
  3675. **/
  3676. int
  3677. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  3678. {
  3679. struct lpfc_sli *psli;
  3680. struct lpfc_sli_ring *pring;
  3681. int i;
  3682. psli = &phba->sli;
  3683. spin_lock_irq(&phba->hbalock);
  3684. INIT_LIST_HEAD(&psli->mboxq);
  3685. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  3686. /* Initialize list headers for txq and txcmplq as double linked lists */
  3687. for (i = 0; i < psli->num_rings; i++) {
  3688. pring = &psli->ring[i];
  3689. pring->ringno = i;
  3690. pring->next_cmdidx = 0;
  3691. pring->local_getidx = 0;
  3692. pring->cmdidx = 0;
  3693. INIT_LIST_HEAD(&pring->txq);
  3694. INIT_LIST_HEAD(&pring->txcmplq);
  3695. INIT_LIST_HEAD(&pring->iocb_continueq);
  3696. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  3697. INIT_LIST_HEAD(&pring->postbufq);
  3698. }
  3699. spin_unlock_irq(&phba->hbalock);
  3700. return 1;
  3701. }
  3702. /**
  3703. * lpfc_sli_host_down: Vport cleanup function.
  3704. * @vport: Pointer to virtual port object.
  3705. *
  3706. * lpfc_sli_host_down is called to clean up the resources
  3707. * associated with a vport before destroying virtual
  3708. * port data structures.
  3709. * This function does following operations:
  3710. * - Free discovery resources associated with this virtual
  3711. * port.
  3712. * - Free iocbs associated with this virtual port in
  3713. * the txq.
  3714. * - Send abort for all iocb commands associated with this
  3715. * vport in txcmplq.
  3716. *
  3717. * This function is called with no lock held and always returns 1.
  3718. **/
  3719. int
  3720. lpfc_sli_host_down(struct lpfc_vport *vport)
  3721. {
  3722. LIST_HEAD(completions);
  3723. struct lpfc_hba *phba = vport->phba;
  3724. struct lpfc_sli *psli = &phba->sli;
  3725. struct lpfc_sli_ring *pring;
  3726. struct lpfc_iocbq *iocb, *next_iocb;
  3727. int i;
  3728. unsigned long flags = 0;
  3729. uint16_t prev_pring_flag;
  3730. lpfc_cleanup_discovery_resources(vport);
  3731. spin_lock_irqsave(&phba->hbalock, flags);
  3732. for (i = 0; i < psli->num_rings; i++) {
  3733. pring = &psli->ring[i];
  3734. prev_pring_flag = pring->flag;
  3735. /* Only slow rings */
  3736. if (pring->ringno == LPFC_ELS_RING) {
  3737. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3738. /* Set the lpfc data pending flag */
  3739. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3740. }
  3741. /*
  3742. * Error everything on the txq since these iocbs have not been
  3743. * given to the FW yet.
  3744. */
  3745. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  3746. if (iocb->vport != vport)
  3747. continue;
  3748. list_move_tail(&iocb->list, &completions);
  3749. pring->txq_cnt--;
  3750. }
  3751. /* Next issue ABTS for everything on the txcmplq */
  3752. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  3753. list) {
  3754. if (iocb->vport != vport)
  3755. continue;
  3756. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3757. }
  3758. pring->flag = prev_pring_flag;
  3759. }
  3760. spin_unlock_irqrestore(&phba->hbalock, flags);
  3761. while (!list_empty(&completions)) {
  3762. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3763. if (!iocb->iocb_cmpl)
  3764. lpfc_sli_release_iocbq(phba, iocb);
  3765. else {
  3766. iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  3767. iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
  3768. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3769. }
  3770. }
  3771. return 1;
  3772. }
  3773. /**
  3774. * lpfc_sli_hba_down: Resource cleanup function for the HBA.
  3775. * @phba: Pointer to HBA context object.
  3776. *
  3777. * This function cleans up all iocb, buffers, mailbox commands
  3778. * while shutting down the HBA. This function is called with no
  3779. * lock held and always returns 1.
  3780. * This function does the following to cleanup driver resources:
  3781. * - Free discovery resources for each virtual port
  3782. * - Cleanup any pending fabric iocbs
  3783. * - Iterate through the iocb txq and free each entry
  3784. * in the list.
  3785. * - Free up any buffer posted to the HBA
  3786. * - Free mailbox commands in the mailbox queue.
  3787. **/
  3788. int
  3789. lpfc_sli_hba_down(struct lpfc_hba *phba)
  3790. {
  3791. LIST_HEAD(completions);
  3792. struct lpfc_sli *psli = &phba->sli;
  3793. struct lpfc_sli_ring *pring;
  3794. struct lpfc_dmabuf *buf_ptr;
  3795. LPFC_MBOXQ_t *pmb;
  3796. struct lpfc_iocbq *iocb;
  3797. IOCB_t *cmd = NULL;
  3798. int i;
  3799. unsigned long flags = 0;
  3800. lpfc_hba_down_prep(phba);
  3801. lpfc_fabric_abort_hba(phba);
  3802. spin_lock_irqsave(&phba->hbalock, flags);
  3803. for (i = 0; i < psli->num_rings; i++) {
  3804. pring = &psli->ring[i];
  3805. /* Only slow rings */
  3806. if (pring->ringno == LPFC_ELS_RING) {
  3807. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3808. /* Set the lpfc data pending flag */
  3809. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3810. }
  3811. /*
  3812. * Error everything on the txq since these iocbs have not been
  3813. * given to the FW yet.
  3814. */
  3815. list_splice_init(&pring->txq, &completions);
  3816. pring->txq_cnt = 0;
  3817. }
  3818. spin_unlock_irqrestore(&phba->hbalock, flags);
  3819. while (!list_empty(&completions)) {
  3820. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3821. cmd = &iocb->iocb;
  3822. if (!iocb->iocb_cmpl)
  3823. lpfc_sli_release_iocbq(phba, iocb);
  3824. else {
  3825. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  3826. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  3827. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3828. }
  3829. }
  3830. spin_lock_irqsave(&phba->hbalock, flags);
  3831. list_splice_init(&phba->elsbuf, &completions);
  3832. phba->elsbuf_cnt = 0;
  3833. phba->elsbuf_prev_cnt = 0;
  3834. spin_unlock_irqrestore(&phba->hbalock, flags);
  3835. while (!list_empty(&completions)) {
  3836. list_remove_head(&completions, buf_ptr,
  3837. struct lpfc_dmabuf, list);
  3838. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  3839. kfree(buf_ptr);
  3840. }
  3841. /* Return any active mbox cmds */
  3842. del_timer_sync(&psli->mbox_tmo);
  3843. spin_lock_irqsave(&phba->hbalock, flags);
  3844. spin_lock(&phba->pport->work_port_lock);
  3845. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  3846. spin_unlock(&phba->pport->work_port_lock);
  3847. /* Return any pending or completed mbox cmds */
  3848. list_splice_init(&phba->sli.mboxq, &completions);
  3849. if (psli->mbox_active) {
  3850. list_add_tail(&psli->mbox_active->list, &completions);
  3851. psli->mbox_active = NULL;
  3852. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3853. }
  3854. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  3855. spin_unlock_irqrestore(&phba->hbalock, flags);
  3856. while (!list_empty(&completions)) {
  3857. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  3858. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  3859. if (pmb->mbox_cmpl)
  3860. pmb->mbox_cmpl(phba,pmb);
  3861. }
  3862. return 1;
  3863. }
  3864. /**
  3865. * lpfc_sli_pcimem_bcopy: SLI memory copy function.
  3866. * @srcp: Source memory pointer.
  3867. * @destp: Destination memory pointer.
  3868. * @cnt: Number of words required to be copied.
  3869. *
  3870. * This function is used for copying data between driver memory
  3871. * and the SLI memory. This function also changes the endianness
  3872. * of each word if native endianness is different from SLI
  3873. * endianness. This function can be called with or without
  3874. * lock.
  3875. **/
  3876. void
  3877. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  3878. {
  3879. uint32_t *src = srcp;
  3880. uint32_t *dest = destp;
  3881. uint32_t ldata;
  3882. int i;
  3883. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  3884. ldata = *src;
  3885. ldata = le32_to_cpu(ldata);
  3886. *dest = ldata;
  3887. src++;
  3888. dest++;
  3889. }
  3890. }
  3891. /**
  3892. * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
  3893. * @phba: Pointer to HBA context object.
  3894. * @pring: Pointer to driver SLI ring object.
  3895. * @mp: Pointer to driver buffer object.
  3896. *
  3897. * This function is called with no lock held.
  3898. * It always return zero after adding the buffer to the postbufq
  3899. * buffer list.
  3900. **/
  3901. int
  3902. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3903. struct lpfc_dmabuf *mp)
  3904. {
  3905. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  3906. later */
  3907. spin_lock_irq(&phba->hbalock);
  3908. list_add_tail(&mp->list, &pring->postbufq);
  3909. pring->postbufq_cnt++;
  3910. spin_unlock_irq(&phba->hbalock);
  3911. return 0;
  3912. }
  3913. /**
  3914. * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
  3915. * using CMD_QUE_XRI64_CX iocb.
  3916. * @phba: Pointer to HBA context object.
  3917. *
  3918. * When HBQ is enabled, buffers are searched based on tags. This function
  3919. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  3920. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  3921. * does not conflict with tags of buffer posted for unsolicited events.
  3922. * The function returns the allocated tag. The function is called with
  3923. * no locks held.
  3924. **/
  3925. uint32_t
  3926. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  3927. {
  3928. spin_lock_irq(&phba->hbalock);
  3929. phba->buffer_tag_count++;
  3930. /*
  3931. * Always set the QUE_BUFTAG_BIT to distiguish between
  3932. * a tag assigned by HBQ.
  3933. */
  3934. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  3935. spin_unlock_irq(&phba->hbalock);
  3936. return phba->buffer_tag_count;
  3937. }
  3938. /**
  3939. * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
  3940. * posted using CMD_QUE_XRI64_CX iocb.
  3941. * @phba: Pointer to HBA context object.
  3942. * @pring: Pointer to driver SLI ring object.
  3943. * @tag: Buffer tag.
  3944. *
  3945. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  3946. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  3947. * iocb is posted to the response ring with the tag of the buffer.
  3948. * This function searches the pring->postbufq list using the tag
  3949. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  3950. * iocb. If the buffer is found then lpfc_dmabuf object of the
  3951. * buffer is returned to the caller else NULL is returned.
  3952. * This function is called with no lock held.
  3953. **/
  3954. struct lpfc_dmabuf *
  3955. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3956. uint32_t tag)
  3957. {
  3958. struct lpfc_dmabuf *mp, *next_mp;
  3959. struct list_head *slp = &pring->postbufq;
  3960. /* Search postbufq, from the begining, looking for a match on tag */
  3961. spin_lock_irq(&phba->hbalock);
  3962. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  3963. if (mp->buffer_tag == tag) {
  3964. list_del_init(&mp->list);
  3965. pring->postbufq_cnt--;
  3966. spin_unlock_irq(&phba->hbalock);
  3967. return mp;
  3968. }
  3969. }
  3970. spin_unlock_irq(&phba->hbalock);
  3971. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3972. "0402 Cannot find virtual addr for buffer tag on "
  3973. "ring %d Data x%lx x%p x%p x%x\n",
  3974. pring->ringno, (unsigned long) tag,
  3975. slp->next, slp->prev, pring->postbufq_cnt);
  3976. return NULL;
  3977. }
  3978. /**
  3979. * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
  3980. * unsolicited ct and els events.
  3981. * @phba: Pointer to HBA context object.
  3982. * @pring: Pointer to driver SLI ring object.
  3983. * @phys: DMA address of the buffer.
  3984. *
  3985. * This function searches the buffer list using the dma_address
  3986. * of unsolicited event to find the driver's lpfc_dmabuf object
  3987. * corresponding to the dma_address. The function returns the
  3988. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  3989. * This function is called by the ct and els unsolicited event
  3990. * handlers to get the buffer associated with the unsolicited
  3991. * event.
  3992. *
  3993. * This function is called with no lock held.
  3994. **/
  3995. struct lpfc_dmabuf *
  3996. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3997. dma_addr_t phys)
  3998. {
  3999. struct lpfc_dmabuf *mp, *next_mp;
  4000. struct list_head *slp = &pring->postbufq;
  4001. /* Search postbufq, from the begining, looking for a match on phys */
  4002. spin_lock_irq(&phba->hbalock);
  4003. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  4004. if (mp->phys == phys) {
  4005. list_del_init(&mp->list);
  4006. pring->postbufq_cnt--;
  4007. spin_unlock_irq(&phba->hbalock);
  4008. return mp;
  4009. }
  4010. }
  4011. spin_unlock_irq(&phba->hbalock);
  4012. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4013. "0410 Cannot find virtual addr for mapped buf on "
  4014. "ring %d Data x%llx x%p x%p x%x\n",
  4015. pring->ringno, (unsigned long long)phys,
  4016. slp->next, slp->prev, pring->postbufq_cnt);
  4017. return NULL;
  4018. }
  4019. /**
  4020. * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
  4021. * @phba: Pointer to HBA context object.
  4022. * @cmdiocb: Pointer to driver command iocb object.
  4023. * @rspiocb: Pointer to driver response iocb object.
  4024. *
  4025. * This function is the completion handler for the abort iocbs for
  4026. * ELS commands. This function is called from the ELS ring event
  4027. * handler with no lock held. This function frees memory resources
  4028. * associated with the abort iocb.
  4029. **/
  4030. static void
  4031. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4032. struct lpfc_iocbq *rspiocb)
  4033. {
  4034. IOCB_t *irsp = &rspiocb->iocb;
  4035. uint16_t abort_iotag, abort_context;
  4036. struct lpfc_iocbq *abort_iocb;
  4037. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  4038. abort_iocb = NULL;
  4039. if (irsp->ulpStatus) {
  4040. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  4041. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  4042. spin_lock_irq(&phba->hbalock);
  4043. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  4044. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  4045. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  4046. "0327 Cannot abort els iocb %p "
  4047. "with tag %x context %x, abort status %x, "
  4048. "abort code %x\n",
  4049. abort_iocb, abort_iotag, abort_context,
  4050. irsp->ulpStatus, irsp->un.ulpWord[4]);
  4051. /*
  4052. * If the iocb is not found in Firmware queue the iocb
  4053. * might have completed already. Do not free it again.
  4054. */
  4055. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  4056. spin_unlock_irq(&phba->hbalock);
  4057. lpfc_sli_release_iocbq(phba, cmdiocb);
  4058. return;
  4059. }
  4060. /*
  4061. * make sure we have the right iocbq before taking it
  4062. * off the txcmplq and try to call completion routine.
  4063. */
  4064. if (!abort_iocb ||
  4065. abort_iocb->iocb.ulpContext != abort_context ||
  4066. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  4067. spin_unlock_irq(&phba->hbalock);
  4068. else {
  4069. list_del_init(&abort_iocb->list);
  4070. pring->txcmplq_cnt--;
  4071. spin_unlock_irq(&phba->hbalock);
  4072. /* Firmware could still be in progress of DMAing
  4073. * payload, so don't free data buffer till after
  4074. * a hbeat.
  4075. */
  4076. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  4077. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  4078. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  4079. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  4080. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  4081. }
  4082. }
  4083. lpfc_sli_release_iocbq(phba, cmdiocb);
  4084. return;
  4085. }
  4086. /**
  4087. * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
  4088. * @phba: Pointer to HBA context object.
  4089. * @cmdiocb: Pointer to driver command iocb object.
  4090. * @rspiocb: Pointer to driver response iocb object.
  4091. *
  4092. * The function is called from SLI ring event handler with no
  4093. * lock held. This function is the completion handler for ELS commands
  4094. * which are aborted. The function frees memory resources used for
  4095. * the aborted ELS commands.
  4096. **/
  4097. static void
  4098. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4099. struct lpfc_iocbq *rspiocb)
  4100. {
  4101. IOCB_t *irsp = &rspiocb->iocb;
  4102. /* ELS cmd tag <ulpIoTag> completes */
  4103. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  4104. "0139 Ignoring ELS cmd tag x%x completion Data: "
  4105. "x%x x%x x%x\n",
  4106. irsp->ulpIoTag, irsp->ulpStatus,
  4107. irsp->un.ulpWord[4], irsp->ulpTimeout);
  4108. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  4109. lpfc_ct_free_iocb(phba, cmdiocb);
  4110. else
  4111. lpfc_els_free_iocb(phba, cmdiocb);
  4112. return;
  4113. }
  4114. /**
  4115. * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
  4116. * @phba: Pointer to HBA context object.
  4117. * @pring: Pointer to driver SLI ring object.
  4118. * @cmdiocb: Pointer to driver command iocb object.
  4119. *
  4120. * This function issues an abort iocb for the provided command
  4121. * iocb. This function is called with hbalock held.
  4122. * The function returns 0 when it fails due to memory allocation
  4123. * failure or when the command iocb is an abort request.
  4124. **/
  4125. int
  4126. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4127. struct lpfc_iocbq *cmdiocb)
  4128. {
  4129. struct lpfc_vport *vport = cmdiocb->vport;
  4130. struct lpfc_iocbq *abtsiocbp;
  4131. IOCB_t *icmd = NULL;
  4132. IOCB_t *iabt = NULL;
  4133. int retval = IOCB_ERROR;
  4134. /*
  4135. * There are certain command types we don't want to abort. And we
  4136. * don't want to abort commands that are already in the process of
  4137. * being aborted.
  4138. */
  4139. icmd = &cmdiocb->iocb;
  4140. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  4141. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  4142. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  4143. return 0;
  4144. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  4145. * callback so that nothing happens when it finishes.
  4146. */
  4147. if ((vport->load_flag & FC_UNLOADING) &&
  4148. (pring->ringno == LPFC_ELS_RING)) {
  4149. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  4150. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  4151. else
  4152. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  4153. goto abort_iotag_exit;
  4154. }
  4155. /* issue ABTS for this IOCB based on iotag */
  4156. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  4157. if (abtsiocbp == NULL)
  4158. return 0;
  4159. /* This signals the response to set the correct status
  4160. * before calling the completion handler.
  4161. */
  4162. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  4163. iabt = &abtsiocbp->iocb;
  4164. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  4165. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  4166. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  4167. iabt->ulpLe = 1;
  4168. iabt->ulpClass = icmd->ulpClass;
  4169. if (phba->link_state >= LPFC_LINK_UP)
  4170. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  4171. else
  4172. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  4173. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  4174. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  4175. "0339 Abort xri x%x, original iotag x%x, "
  4176. "abort cmd iotag x%x\n",
  4177. iabt->un.acxri.abortContextTag,
  4178. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  4179. retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
  4180. if (retval)
  4181. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  4182. abort_iotag_exit:
  4183. /*
  4184. * Caller to this routine should check for IOCB_ERROR
  4185. * and handle it properly. This routine no longer removes
  4186. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  4187. */
  4188. return retval;
  4189. }
  4190. /**
  4191. * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
  4192. * associated with a vport/SCSI target/lun.
  4193. * @iocbq: Pointer to driver iocb object.
  4194. * @vport: Pointer to driver virtual port object.
  4195. * @tgt_id: SCSI ID of the target.
  4196. * @lun_id: LUN ID of the scsi device.
  4197. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  4198. *
  4199. * This function acts as iocb filter for functions which abort or count
  4200. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  4201. * 0 if the filtering criteria is met for the given iocb and will return
  4202. * 1 if the filtering criteria is not met.
  4203. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  4204. * given iocb is for the SCSI device specified by vport, tgt_id and
  4205. * lun_id parameter.
  4206. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  4207. * given iocb is for the SCSI target specified by vport and tgt_id
  4208. * parameters.
  4209. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  4210. * given iocb is for the SCSI host associated with the given vport.
  4211. * This function is called with no locks held.
  4212. **/
  4213. static int
  4214. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  4215. uint16_t tgt_id, uint64_t lun_id,
  4216. lpfc_ctx_cmd ctx_cmd)
  4217. {
  4218. struct lpfc_scsi_buf *lpfc_cmd;
  4219. int rc = 1;
  4220. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  4221. return rc;
  4222. if (iocbq->vport != vport)
  4223. return rc;
  4224. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  4225. if (lpfc_cmd->pCmd == NULL)
  4226. return rc;
  4227. switch (ctx_cmd) {
  4228. case LPFC_CTX_LUN:
  4229. if ((lpfc_cmd->rdata->pnode) &&
  4230. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  4231. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  4232. rc = 0;
  4233. break;
  4234. case LPFC_CTX_TGT:
  4235. if ((lpfc_cmd->rdata->pnode) &&
  4236. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  4237. rc = 0;
  4238. break;
  4239. case LPFC_CTX_HOST:
  4240. rc = 0;
  4241. break;
  4242. default:
  4243. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  4244. __func__, ctx_cmd);
  4245. break;
  4246. }
  4247. return rc;
  4248. }
  4249. /**
  4250. * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
  4251. * @vport: Pointer to virtual port.
  4252. * @tgt_id: SCSI ID of the target.
  4253. * @lun_id: LUN ID of the scsi device.
  4254. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4255. *
  4256. * This function returns number of FCP commands pending for the vport.
  4257. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  4258. * commands pending on the vport associated with SCSI device specified
  4259. * by tgt_id and lun_id parameters.
  4260. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  4261. * commands pending on the vport associated with SCSI target specified
  4262. * by tgt_id parameter.
  4263. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  4264. * commands pending on the vport.
  4265. * This function returns the number of iocbs which satisfy the filter.
  4266. * This function is called without any lock held.
  4267. **/
  4268. int
  4269. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  4270. lpfc_ctx_cmd ctx_cmd)
  4271. {
  4272. struct lpfc_hba *phba = vport->phba;
  4273. struct lpfc_iocbq *iocbq;
  4274. int sum, i;
  4275. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  4276. iocbq = phba->sli.iocbq_lookup[i];
  4277. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  4278. ctx_cmd) == 0)
  4279. sum++;
  4280. }
  4281. return sum;
  4282. }
  4283. /**
  4284. * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
  4285. * FCP iocb.
  4286. * @phba: Pointer to HBA context object
  4287. * @cmdiocb: Pointer to command iocb object.
  4288. * @rspiocb: Pointer to response iocb object.
  4289. *
  4290. * This function is called when an aborted FCP iocb completes. This
  4291. * function is called by the ring event handler with no lock held.
  4292. * This function frees the iocb.
  4293. **/
  4294. void
  4295. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4296. struct lpfc_iocbq *rspiocb)
  4297. {
  4298. lpfc_sli_release_iocbq(phba, cmdiocb);
  4299. return;
  4300. }
  4301. /**
  4302. * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
  4303. * pending on a SCSI host(vport)/target/lun.
  4304. * @vport: Pointer to virtual port.
  4305. * @pring: Pointer to driver SLI ring object.
  4306. * @tgt_id: SCSI ID of the target.
  4307. * @lun_id: LUN ID of the scsi device.
  4308. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4309. *
  4310. * This function sends an abort command for every SCSI command
  4311. * associated with the given virtual port pending on the ring
  4312. * filtered by lpfc_sli_validate_fcp_iocb function.
  4313. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  4314. * FCP iocbs associated with lun specified by tgt_id and lun_id
  4315. * parameters
  4316. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  4317. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  4318. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  4319. * FCP iocbs associated with virtual port.
  4320. * This function returns number of iocbs it failed to abort.
  4321. * This function is called with no locks held.
  4322. **/
  4323. int
  4324. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  4325. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  4326. {
  4327. struct lpfc_hba *phba = vport->phba;
  4328. struct lpfc_iocbq *iocbq;
  4329. struct lpfc_iocbq *abtsiocb;
  4330. IOCB_t *cmd = NULL;
  4331. int errcnt = 0, ret_val = 0;
  4332. int i;
  4333. for (i = 1; i <= phba->sli.last_iotag; i++) {
  4334. iocbq = phba->sli.iocbq_lookup[i];
  4335. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  4336. abort_cmd) != 0)
  4337. continue;
  4338. /* issue ABTS for this IOCB based on iotag */
  4339. abtsiocb = lpfc_sli_get_iocbq(phba);
  4340. if (abtsiocb == NULL) {
  4341. errcnt++;
  4342. continue;
  4343. }
  4344. cmd = &iocbq->iocb;
  4345. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  4346. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  4347. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  4348. abtsiocb->iocb.ulpLe = 1;
  4349. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  4350. abtsiocb->vport = phba->pport;
  4351. if (lpfc_is_link_up(phba))
  4352. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  4353. else
  4354. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  4355. /* Setup callback routine and issue the command. */
  4356. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  4357. ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
  4358. if (ret_val == IOCB_ERROR) {
  4359. lpfc_sli_release_iocbq(phba, abtsiocb);
  4360. errcnt++;
  4361. continue;
  4362. }
  4363. }
  4364. return errcnt;
  4365. }
  4366. /**
  4367. * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
  4368. * lpfc_sli_issue_iocb_wait.
  4369. * @phba: Pointer to HBA context object.
  4370. * @cmdiocbq: Pointer to command iocb.
  4371. * @rspiocbq: Pointer to response iocb.
  4372. *
  4373. * This function is the completion handler for iocbs issued using
  4374. * lpfc_sli_issue_iocb_wait function. This function is called by the
  4375. * ring event handler function without any lock held. This function
  4376. * can be called from both worker thread context and interrupt
  4377. * context. This function also can be called from other thread which
  4378. * cleans up the SLI layer objects.
  4379. * This function copy the contents of the response iocb to the
  4380. * response iocb memory object provided by the caller of
  4381. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  4382. * sleeps for the iocb completion.
  4383. **/
  4384. static void
  4385. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  4386. struct lpfc_iocbq *cmdiocbq,
  4387. struct lpfc_iocbq *rspiocbq)
  4388. {
  4389. wait_queue_head_t *pdone_q;
  4390. unsigned long iflags;
  4391. spin_lock_irqsave(&phba->hbalock, iflags);
  4392. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  4393. if (cmdiocbq->context2 && rspiocbq)
  4394. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  4395. &rspiocbq->iocb, sizeof(IOCB_t));
  4396. pdone_q = cmdiocbq->context_un.wait_queue;
  4397. if (pdone_q)
  4398. wake_up(pdone_q);
  4399. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4400. return;
  4401. }
  4402. /**
  4403. * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
  4404. * @phba: Pointer to HBA context object..
  4405. * @pring: Pointer to sli ring.
  4406. * @piocb: Pointer to command iocb.
  4407. * @prspiocbq: Pointer to response iocb.
  4408. * @timeout: Timeout in number of seconds.
  4409. *
  4410. * This function issues the iocb to firmware and waits for the
  4411. * iocb to complete. If the iocb command is not
  4412. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  4413. * Caller should not free the iocb resources if this function
  4414. * returns IOCB_TIMEDOUT.
  4415. * The function waits for the iocb completion using an
  4416. * non-interruptible wait.
  4417. * This function will sleep while waiting for iocb completion.
  4418. * So, this function should not be called from any context which
  4419. * does not allow sleeping. Due to the same reason, this function
  4420. * cannot be called with interrupt disabled.
  4421. * This function assumes that the iocb completions occur while
  4422. * this function sleep. So, this function cannot be called from
  4423. * the thread which process iocb completion for this ring.
  4424. * This function clears the iocb_flag of the iocb object before
  4425. * issuing the iocb and the iocb completion handler sets this
  4426. * flag and wakes this thread when the iocb completes.
  4427. * The contents of the response iocb will be copied to prspiocbq
  4428. * by the completion handler when the command completes.
  4429. * This function returns IOCB_SUCCESS when success.
  4430. * This function is called with no lock held.
  4431. **/
  4432. int
  4433. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  4434. struct lpfc_sli_ring *pring,
  4435. struct lpfc_iocbq *piocb,
  4436. struct lpfc_iocbq *prspiocbq,
  4437. uint32_t timeout)
  4438. {
  4439. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4440. long timeleft, timeout_req = 0;
  4441. int retval = IOCB_SUCCESS;
  4442. uint32_t creg_val;
  4443. /*
  4444. * If the caller has provided a response iocbq buffer, then context2
  4445. * is NULL or its an error.
  4446. */
  4447. if (prspiocbq) {
  4448. if (piocb->context2)
  4449. return IOCB_ERROR;
  4450. piocb->context2 = prspiocbq;
  4451. }
  4452. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  4453. piocb->context_un.wait_queue = &done_q;
  4454. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  4455. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4456. creg_val = readl(phba->HCregaddr);
  4457. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  4458. writel(creg_val, phba->HCregaddr);
  4459. readl(phba->HCregaddr); /* flush */
  4460. }
  4461. retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
  4462. if (retval == IOCB_SUCCESS) {
  4463. timeout_req = timeout * HZ;
  4464. timeleft = wait_event_timeout(done_q,
  4465. piocb->iocb_flag & LPFC_IO_WAKE,
  4466. timeout_req);
  4467. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  4468. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4469. "0331 IOCB wake signaled\n");
  4470. } else if (timeleft == 0) {
  4471. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4472. "0338 IOCB wait timeout error - no "
  4473. "wake response Data x%x\n", timeout);
  4474. retval = IOCB_TIMEDOUT;
  4475. } else {
  4476. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4477. "0330 IOCB wake NOT set, "
  4478. "Data x%x x%lx\n",
  4479. timeout, (timeleft / jiffies));
  4480. retval = IOCB_TIMEDOUT;
  4481. }
  4482. } else {
  4483. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4484. "0332 IOCB wait issue failed, Data x%x\n",
  4485. retval);
  4486. retval = IOCB_ERROR;
  4487. }
  4488. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4489. creg_val = readl(phba->HCregaddr);
  4490. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  4491. writel(creg_val, phba->HCregaddr);
  4492. readl(phba->HCregaddr); /* flush */
  4493. }
  4494. if (prspiocbq)
  4495. piocb->context2 = NULL;
  4496. piocb->context_un.wait_queue = NULL;
  4497. piocb->iocb_cmpl = NULL;
  4498. return retval;
  4499. }
  4500. /**
  4501. * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
  4502. * @phba: Pointer to HBA context object.
  4503. * @pmboxq: Pointer to driver mailbox object.
  4504. * @timeout: Timeout in number of seconds.
  4505. *
  4506. * This function issues the mailbox to firmware and waits for the
  4507. * mailbox command to complete. If the mailbox command is not
  4508. * completed within timeout seconds, it returns MBX_TIMEOUT.
  4509. * The function waits for the mailbox completion using an
  4510. * interruptible wait. If the thread is woken up due to a
  4511. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  4512. * should not free the mailbox resources, if this function returns
  4513. * MBX_TIMEOUT.
  4514. * This function will sleep while waiting for mailbox completion.
  4515. * So, this function should not be called from any context which
  4516. * does not allow sleeping. Due to the same reason, this function
  4517. * cannot be called with interrupt disabled.
  4518. * This function assumes that the mailbox completion occurs while
  4519. * this function sleep. So, this function cannot be called from
  4520. * the worker thread which processes mailbox completion.
  4521. * This function is called in the context of HBA management
  4522. * applications.
  4523. * This function returns MBX_SUCCESS when successful.
  4524. * This function is called with no lock held.
  4525. **/
  4526. int
  4527. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  4528. uint32_t timeout)
  4529. {
  4530. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4531. int retval;
  4532. unsigned long flag;
  4533. /* The caller must leave context1 empty. */
  4534. if (pmboxq->context1)
  4535. return MBX_NOT_FINISHED;
  4536. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  4537. /* setup wake call as IOCB callback */
  4538. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  4539. /* setup context field to pass wait_queue pointer to wake function */
  4540. pmboxq->context1 = &done_q;
  4541. /* now issue the command */
  4542. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  4543. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  4544. wait_event_interruptible_timeout(done_q,
  4545. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  4546. timeout * HZ);
  4547. spin_lock_irqsave(&phba->hbalock, flag);
  4548. pmboxq->context1 = NULL;
  4549. /*
  4550. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  4551. * else do not free the resources.
  4552. */
  4553. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  4554. retval = MBX_SUCCESS;
  4555. else {
  4556. retval = MBX_TIMEOUT;
  4557. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4558. }
  4559. spin_unlock_irqrestore(&phba->hbalock, flag);
  4560. }
  4561. return retval;
  4562. }
  4563. /**
  4564. * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
  4565. * @phba: Pointer to HBA context.
  4566. *
  4567. * This function is called to cleanup any pending mailbox
  4568. * objects in the driver queue before bringing the HBA offline.
  4569. * This function is called while resetting the HBA.
  4570. * The function is called without any lock held. The function
  4571. * takes hbalock to update SLI data structure.
  4572. * This function returns 1 when there is an active mailbox
  4573. * command pending else returns 0.
  4574. **/
  4575. int
  4576. lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
  4577. {
  4578. struct lpfc_vport *vport = phba->pport;
  4579. int i = 0;
  4580. uint32_t ha_copy;
  4581. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
  4582. if (i++ > LPFC_MBOX_TMO * 1000)
  4583. return 1;
  4584. /*
  4585. * Call lpfc_sli_handle_mb_event only if a mailbox cmd
  4586. * did finish. This way we won't get the misleading
  4587. * "Stray Mailbox Interrupt" message.
  4588. */
  4589. spin_lock_irq(&phba->hbalock);
  4590. ha_copy = phba->work_ha;
  4591. phba->work_ha &= ~HA_MBATT;
  4592. spin_unlock_irq(&phba->hbalock);
  4593. if (ha_copy & HA_MBATT)
  4594. if (lpfc_sli_handle_mb_event(phba) == 0)
  4595. i = 0;
  4596. msleep(1);
  4597. }
  4598. return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
  4599. }
  4600. /**
  4601. * lpfc_sli_check_eratt: check error attention events
  4602. * @phba: Pointer to HBA context.
  4603. *
  4604. * This function is called form timer soft interrupt context to check HBA's
  4605. * error attention register bit for error attention events.
  4606. *
  4607. * This fucntion returns 1 when there is Error Attention in the Host Attention
  4608. * Register and returns 0 otherwise.
  4609. **/
  4610. int
  4611. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  4612. {
  4613. uint32_t ha_copy;
  4614. /* If PCI channel is offline, don't process it */
  4615. if (unlikely(pci_channel_offline(phba->pcidev)))
  4616. return 0;
  4617. /* If somebody is waiting to handle an eratt, don't process it
  4618. * here. The brdkill function will do this.
  4619. */
  4620. if (phba->link_flag & LS_IGNORE_ERATT)
  4621. return 0;
  4622. /* Check if interrupt handler handles this ERATT */
  4623. spin_lock_irq(&phba->hbalock);
  4624. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  4625. /* Interrupt handler has handled ERATT */
  4626. spin_unlock_irq(&phba->hbalock);
  4627. return 0;
  4628. }
  4629. /* Read chip Host Attention (HA) register */
  4630. ha_copy = readl(phba->HAregaddr);
  4631. if (ha_copy & HA_ERATT) {
  4632. /* Read host status register to retrieve error event */
  4633. lpfc_sli_read_hs(phba);
  4634. /* Set the driver HA work bitmap */
  4635. phba->work_ha |= HA_ERATT;
  4636. /* Indicate polling handles this ERATT */
  4637. phba->hba_flag |= HBA_ERATT_HANDLED;
  4638. spin_unlock_irq(&phba->hbalock);
  4639. return 1;
  4640. }
  4641. spin_unlock_irq(&phba->hbalock);
  4642. return 0;
  4643. }
  4644. /**
  4645. * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
  4646. * @irq: Interrupt number.
  4647. * @dev_id: The device context pointer.
  4648. *
  4649. * This function is directly called from the PCI layer as an interrupt
  4650. * service routine when the device is enabled with MSI-X multi-message
  4651. * interrupt mode and there are slow-path events in the HBA. However,
  4652. * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
  4653. * this function is called as part of the device-level interrupt handler.
  4654. * When the PCI slot is in error recovery or the HBA is undergoing
  4655. * initialization, the interrupt handler will not process the interrupt.
  4656. * The link attention and ELS ring attention events are handled by the
  4657. * worker thread. The interrupt handler signals the worker thread and
  4658. * and returns for these events. This function is called without any
  4659. * lock held. It gets the hbalock to access and update SLI data
  4660. * structures.
  4661. *
  4662. * This function returns IRQ_HANDLED when interrupt is handled else it
  4663. * returns IRQ_NONE.
  4664. **/
  4665. irqreturn_t
  4666. lpfc_sp_intr_handler(int irq, void *dev_id)
  4667. {
  4668. struct lpfc_hba *phba;
  4669. uint32_t ha_copy;
  4670. uint32_t work_ha_copy;
  4671. unsigned long status;
  4672. unsigned long iflag;
  4673. uint32_t control;
  4674. MAILBOX_t *mbox, *pmbox;
  4675. struct lpfc_vport *vport;
  4676. struct lpfc_nodelist *ndlp;
  4677. struct lpfc_dmabuf *mp;
  4678. LPFC_MBOXQ_t *pmb;
  4679. int rc;
  4680. /*
  4681. * Get the driver's phba structure from the dev_id and
  4682. * assume the HBA is not interrupting.
  4683. */
  4684. phba = (struct lpfc_hba *)dev_id;
  4685. if (unlikely(!phba))
  4686. return IRQ_NONE;
  4687. /*
  4688. * Stuff needs to be attented to when this function is invoked as an
  4689. * individual interrupt handler in MSI-X multi-message interrupt mode
  4690. */
  4691. if (phba->intr_type == MSIX) {
  4692. /* If the pci channel is offline, ignore all the interrupts */
  4693. if (unlikely(pci_channel_offline(phba->pcidev)))
  4694. return IRQ_NONE;
  4695. /* Update device-level interrupt statistics */
  4696. phba->sli.slistat.sli_intr++;
  4697. /* Ignore all interrupts during initialization. */
  4698. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4699. return IRQ_NONE;
  4700. /* Need to read HA REG for slow-path events */
  4701. spin_lock_irqsave(&phba->hbalock, iflag);
  4702. ha_copy = readl(phba->HAregaddr);
  4703. /* If somebody is waiting to handle an eratt don't process it
  4704. * here. The brdkill function will do this.
  4705. */
  4706. if (phba->link_flag & LS_IGNORE_ERATT)
  4707. ha_copy &= ~HA_ERATT;
  4708. /* Check the need for handling ERATT in interrupt handler */
  4709. if (ha_copy & HA_ERATT) {
  4710. if (phba->hba_flag & HBA_ERATT_HANDLED)
  4711. /* ERATT polling has handled ERATT */
  4712. ha_copy &= ~HA_ERATT;
  4713. else
  4714. /* Indicate interrupt handler handles ERATT */
  4715. phba->hba_flag |= HBA_ERATT_HANDLED;
  4716. }
  4717. /* Clear up only attention source related to slow-path */
  4718. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  4719. phba->HAregaddr);
  4720. readl(phba->HAregaddr); /* flush */
  4721. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4722. } else
  4723. ha_copy = phba->ha_copy;
  4724. work_ha_copy = ha_copy & phba->work_ha_mask;
  4725. if (work_ha_copy) {
  4726. if (work_ha_copy & HA_LATT) {
  4727. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  4728. /*
  4729. * Turn off Link Attention interrupts
  4730. * until CLEAR_LA done
  4731. */
  4732. spin_lock_irqsave(&phba->hbalock, iflag);
  4733. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  4734. control = readl(phba->HCregaddr);
  4735. control &= ~HC_LAINT_ENA;
  4736. writel(control, phba->HCregaddr);
  4737. readl(phba->HCregaddr); /* flush */
  4738. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4739. }
  4740. else
  4741. work_ha_copy &= ~HA_LATT;
  4742. }
  4743. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  4744. /*
  4745. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  4746. * the only slow ring.
  4747. */
  4748. status = (work_ha_copy &
  4749. (HA_RXMASK << (4*LPFC_ELS_RING)));
  4750. status >>= (4*LPFC_ELS_RING);
  4751. if (status & HA_RXMASK) {
  4752. spin_lock_irqsave(&phba->hbalock, iflag);
  4753. control = readl(phba->HCregaddr);
  4754. lpfc_debugfs_slow_ring_trc(phba,
  4755. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  4756. control, status,
  4757. (uint32_t)phba->sli.slistat.sli_intr);
  4758. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  4759. lpfc_debugfs_slow_ring_trc(phba,
  4760. "ISR Disable ring:"
  4761. "pwork:x%x hawork:x%x wait:x%x",
  4762. phba->work_ha, work_ha_copy,
  4763. (uint32_t)((unsigned long)
  4764. &phba->work_waitq));
  4765. control &=
  4766. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  4767. writel(control, phba->HCregaddr);
  4768. readl(phba->HCregaddr); /* flush */
  4769. }
  4770. else {
  4771. lpfc_debugfs_slow_ring_trc(phba,
  4772. "ISR slow ring: pwork:"
  4773. "x%x hawork:x%x wait:x%x",
  4774. phba->work_ha, work_ha_copy,
  4775. (uint32_t)((unsigned long)
  4776. &phba->work_waitq));
  4777. }
  4778. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4779. }
  4780. }
  4781. spin_lock_irqsave(&phba->hbalock, iflag);
  4782. if (work_ha_copy & HA_ERATT)
  4783. lpfc_sli_read_hs(phba);
  4784. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  4785. pmb = phba->sli.mbox_active;
  4786. pmbox = &pmb->mb;
  4787. mbox = phba->mbox;
  4788. vport = pmb->vport;
  4789. /* First check out the status word */
  4790. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  4791. if (pmbox->mbxOwner != OWN_HOST) {
  4792. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4793. /*
  4794. * Stray Mailbox Interrupt, mbxCommand <cmd>
  4795. * mbxStatus <status>
  4796. */
  4797. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4798. LOG_SLI,
  4799. "(%d):0304 Stray Mailbox "
  4800. "Interrupt mbxCommand x%x "
  4801. "mbxStatus x%x\n",
  4802. (vport ? vport->vpi : 0),
  4803. pmbox->mbxCommand,
  4804. pmbox->mbxStatus);
  4805. /* clear mailbox attention bit */
  4806. work_ha_copy &= ~HA_MBATT;
  4807. } else {
  4808. phba->sli.mbox_active = NULL;
  4809. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4810. phba->last_completion_time = jiffies;
  4811. del_timer(&phba->sli.mbox_tmo);
  4812. if (pmb->mbox_cmpl) {
  4813. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  4814. MAILBOX_CMD_SIZE);
  4815. }
  4816. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  4817. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  4818. lpfc_debugfs_disc_trc(vport,
  4819. LPFC_DISC_TRC_MBOX_VPORT,
  4820. "MBOX dflt rpi: : "
  4821. "status:x%x rpi:x%x",
  4822. (uint32_t)pmbox->mbxStatus,
  4823. pmbox->un.varWords[0], 0);
  4824. if (!pmbox->mbxStatus) {
  4825. mp = (struct lpfc_dmabuf *)
  4826. (pmb->context1);
  4827. ndlp = (struct lpfc_nodelist *)
  4828. pmb->context2;
  4829. /* Reg_LOGIN of dflt RPI was
  4830. * successful. new lets get
  4831. * rid of the RPI using the
  4832. * same mbox buffer.
  4833. */
  4834. lpfc_unreg_login(phba,
  4835. vport->vpi,
  4836. pmbox->un.varWords[0],
  4837. pmb);
  4838. pmb->mbox_cmpl =
  4839. lpfc_mbx_cmpl_dflt_rpi;
  4840. pmb->context1 = mp;
  4841. pmb->context2 = ndlp;
  4842. pmb->vport = vport;
  4843. rc = lpfc_sli_issue_mbox(phba,
  4844. pmb,
  4845. MBX_NOWAIT);
  4846. if (rc != MBX_BUSY)
  4847. lpfc_printf_log(phba,
  4848. KERN_ERR,
  4849. LOG_MBOX | LOG_SLI,
  4850. "0350 rc should have"
  4851. "been MBX_BUSY");
  4852. goto send_current_mbox;
  4853. }
  4854. }
  4855. spin_lock_irqsave(
  4856. &phba->pport->work_port_lock,
  4857. iflag);
  4858. phba->pport->work_port_events &=
  4859. ~WORKER_MBOX_TMO;
  4860. spin_unlock_irqrestore(
  4861. &phba->pport->work_port_lock,
  4862. iflag);
  4863. lpfc_mbox_cmpl_put(phba, pmb);
  4864. }
  4865. } else
  4866. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4867. if ((work_ha_copy & HA_MBATT) &&
  4868. (phba->sli.mbox_active == NULL)) {
  4869. send_current_mbox:
  4870. /* Process next mailbox command if there is one */
  4871. do {
  4872. rc = lpfc_sli_issue_mbox(phba, NULL,
  4873. MBX_NOWAIT);
  4874. } while (rc == MBX_NOT_FINISHED);
  4875. if (rc != MBX_SUCCESS)
  4876. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4877. LOG_SLI, "0349 rc should be "
  4878. "MBX_SUCCESS");
  4879. }
  4880. spin_lock_irqsave(&phba->hbalock, iflag);
  4881. phba->work_ha |= work_ha_copy;
  4882. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4883. lpfc_worker_wake_up(phba);
  4884. }
  4885. return IRQ_HANDLED;
  4886. } /* lpfc_sp_intr_handler */
  4887. /**
  4888. * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
  4889. * @irq: Interrupt number.
  4890. * @dev_id: The device context pointer.
  4891. *
  4892. * This function is directly called from the PCI layer as an interrupt
  4893. * service routine when the device is enabled with MSI-X multi-message
  4894. * interrupt mode and there is a fast-path FCP IOCB ring event in the
  4895. * HBA. However, when the device is enabled with either MSI or Pin-IRQ
  4896. * interrupt mode, this function is called as part of the device-level
  4897. * interrupt handler. When the PCI slot is in error recovery or the HBA
  4898. * is undergoing initialization, the interrupt handler will not process
  4899. * the interrupt. The SCSI FCP fast-path ring event are handled in the
  4900. * intrrupt context. This function is called without any lock held. It
  4901. * gets the hbalock to access and update SLI data structures.
  4902. *
  4903. * This function returns IRQ_HANDLED when interrupt is handled else it
  4904. * returns IRQ_NONE.
  4905. **/
  4906. irqreturn_t
  4907. lpfc_fp_intr_handler(int irq, void *dev_id)
  4908. {
  4909. struct lpfc_hba *phba;
  4910. uint32_t ha_copy;
  4911. unsigned long status;
  4912. unsigned long iflag;
  4913. /* Get the driver's phba structure from the dev_id and
  4914. * assume the HBA is not interrupting.
  4915. */
  4916. phba = (struct lpfc_hba *) dev_id;
  4917. if (unlikely(!phba))
  4918. return IRQ_NONE;
  4919. /*
  4920. * Stuff needs to be attented to when this function is invoked as an
  4921. * individual interrupt handler in MSI-X multi-message interrupt mode
  4922. */
  4923. if (phba->intr_type == MSIX) {
  4924. /* If pci channel is offline, ignore all the interrupts */
  4925. if (unlikely(pci_channel_offline(phba->pcidev)))
  4926. return IRQ_NONE;
  4927. /* Update device-level interrupt statistics */
  4928. phba->sli.slistat.sli_intr++;
  4929. /* Ignore all interrupts during initialization. */
  4930. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4931. return IRQ_NONE;
  4932. /* Need to read HA REG for FCP ring and other ring events */
  4933. ha_copy = readl(phba->HAregaddr);
  4934. /* Clear up only attention source related to fast-path */
  4935. spin_lock_irqsave(&phba->hbalock, iflag);
  4936. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  4937. phba->HAregaddr);
  4938. readl(phba->HAregaddr); /* flush */
  4939. spin_unlock_irqrestore(&phba->hbalock, iflag);
  4940. } else
  4941. ha_copy = phba->ha_copy;
  4942. /*
  4943. * Process all events on FCP ring. Take the optimized path for FCP IO.
  4944. */
  4945. ha_copy &= ~(phba->work_ha_mask);
  4946. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  4947. status >>= (4*LPFC_FCP_RING);
  4948. if (status & HA_RXMASK)
  4949. lpfc_sli_handle_fast_ring_event(phba,
  4950. &phba->sli.ring[LPFC_FCP_RING],
  4951. status);
  4952. if (phba->cfg_multi_ring_support == 2) {
  4953. /*
  4954. * Process all events on extra ring. Take the optimized path
  4955. * for extra ring IO.
  4956. */
  4957. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  4958. status >>= (4*LPFC_EXTRA_RING);
  4959. if (status & HA_RXMASK) {
  4960. lpfc_sli_handle_fast_ring_event(phba,
  4961. &phba->sli.ring[LPFC_EXTRA_RING],
  4962. status);
  4963. }
  4964. }
  4965. return IRQ_HANDLED;
  4966. } /* lpfc_fp_intr_handler */
  4967. /**
  4968. * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
  4969. * @irq: Interrupt number.
  4970. * @dev_id: The device context pointer.
  4971. *
  4972. * This function is the device-level interrupt handler called from the PCI
  4973. * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
  4974. * an event in the HBA which requires driver attention. This function
  4975. * invokes the slow-path interrupt attention handling function and fast-path
  4976. * interrupt attention handling function in turn to process the relevant
  4977. * HBA attention events. This function is called without any lock held. It
  4978. * gets the hbalock to access and update SLI data structures.
  4979. *
  4980. * This function returns IRQ_HANDLED when interrupt is handled, else it
  4981. * returns IRQ_NONE.
  4982. **/
  4983. irqreturn_t
  4984. lpfc_intr_handler(int irq, void *dev_id)
  4985. {
  4986. struct lpfc_hba *phba;
  4987. irqreturn_t sp_irq_rc, fp_irq_rc;
  4988. unsigned long status1, status2;
  4989. /*
  4990. * Get the driver's phba structure from the dev_id and
  4991. * assume the HBA is not interrupting.
  4992. */
  4993. phba = (struct lpfc_hba *) dev_id;
  4994. if (unlikely(!phba))
  4995. return IRQ_NONE;
  4996. /* If the pci channel is offline, ignore all the interrupts. */
  4997. if (unlikely(pci_channel_offline(phba->pcidev)))
  4998. return IRQ_NONE;
  4999. /* Update device level interrupt statistics */
  5000. phba->sli.slistat.sli_intr++;
  5001. /* Ignore all interrupts during initialization. */
  5002. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  5003. return IRQ_NONE;
  5004. spin_lock(&phba->hbalock);
  5005. phba->ha_copy = readl(phba->HAregaddr);
  5006. if (unlikely(!phba->ha_copy)) {
  5007. spin_unlock(&phba->hbalock);
  5008. return IRQ_NONE;
  5009. } else if (phba->ha_copy & HA_ERATT) {
  5010. if (phba->hba_flag & HBA_ERATT_HANDLED)
  5011. /* ERATT polling has handled ERATT */
  5012. phba->ha_copy &= ~HA_ERATT;
  5013. else
  5014. /* Indicate interrupt handler handles ERATT */
  5015. phba->hba_flag |= HBA_ERATT_HANDLED;
  5016. }
  5017. /* Clear attention sources except link and error attentions */
  5018. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  5019. readl(phba->HAregaddr); /* flush */
  5020. spin_unlock(&phba->hbalock);
  5021. /*
  5022. * Invokes slow-path host attention interrupt handling as appropriate.
  5023. */
  5024. /* status of events with mailbox and link attention */
  5025. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  5026. /* status of events with ELS ring */
  5027. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  5028. status2 >>= (4*LPFC_ELS_RING);
  5029. if (status1 || (status2 & HA_RXMASK))
  5030. sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
  5031. else
  5032. sp_irq_rc = IRQ_NONE;
  5033. /*
  5034. * Invoke fast-path host attention interrupt handling as appropriate.
  5035. */
  5036. /* status of events with FCP ring */
  5037. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  5038. status1 >>= (4*LPFC_FCP_RING);
  5039. /* status of events with extra ring */
  5040. if (phba->cfg_multi_ring_support == 2) {
  5041. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  5042. status2 >>= (4*LPFC_EXTRA_RING);
  5043. } else
  5044. status2 = 0;
  5045. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  5046. fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
  5047. else
  5048. fp_irq_rc = IRQ_NONE;
  5049. /* Return device-level interrupt handling status */
  5050. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  5051. } /* lpfc_intr_handler */