lpfc_sli.c 156 KB

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