be_main.c 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971
  1. /**
  2. * Copyright (C) 2005 - 2016 Broadcom
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Written by: Jayamohan Kallickal (jayamohan.kallickal@broadcom.com)
  11. *
  12. * Contact Information:
  13. * linux-drivers@broadcom.com
  14. *
  15. * Emulex
  16. * 3333 Susan Street
  17. * Costa Mesa, CA 92626
  18. */
  19. #include <linux/reboot.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/pci.h>
  25. #include <linux/string.h>
  26. #include <linux/kernel.h>
  27. #include <linux/semaphore.h>
  28. #include <linux/iscsi_boot_sysfs.h>
  29. #include <linux/module.h>
  30. #include <linux/bsg-lib.h>
  31. #include <linux/irq_poll.h>
  32. #include <scsi/libiscsi.h>
  33. #include <scsi/scsi_bsg_iscsi.h>
  34. #include <scsi/scsi_netlink.h>
  35. #include <scsi/scsi_transport_iscsi.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi.h>
  41. #include "be_main.h"
  42. #include "be_iscsi.h"
  43. #include "be_mgmt.h"
  44. #include "be_cmds.h"
  45. static unsigned int be_iopoll_budget = 10;
  46. static unsigned int be_max_phys_size = 64;
  47. static unsigned int enable_msix = 1;
  48. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  49. MODULE_VERSION(BUILD_STR);
  50. MODULE_AUTHOR("Emulex Corporation");
  51. MODULE_LICENSE("GPL");
  52. module_param(be_iopoll_budget, int, 0);
  53. module_param(enable_msix, int, 0);
  54. module_param(be_max_phys_size, uint, S_IRUGO);
  55. MODULE_PARM_DESC(be_max_phys_size,
  56. "Maximum Size (In Kilobytes) of physically contiguous "
  57. "memory that can be allocated. Range is 16 - 128");
  58. #define beiscsi_disp_param(_name)\
  59. ssize_t \
  60. beiscsi_##_name##_disp(struct device *dev,\
  61. struct device_attribute *attrib, char *buf) \
  62. { \
  63. struct Scsi_Host *shost = class_to_shost(dev);\
  64. struct beiscsi_hba *phba = iscsi_host_priv(shost); \
  65. uint32_t param_val = 0; \
  66. param_val = phba->attr_##_name;\
  67. return snprintf(buf, PAGE_SIZE, "%d\n",\
  68. phba->attr_##_name);\
  69. }
  70. #define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
  71. int \
  72. beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
  73. {\
  74. if (val >= _minval && val <= _maxval) {\
  75. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  76. "BA_%d : beiscsi_"#_name" updated "\
  77. "from 0x%x ==> 0x%x\n",\
  78. phba->attr_##_name, val); \
  79. phba->attr_##_name = val;\
  80. return 0;\
  81. } \
  82. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
  83. "BA_%d beiscsi_"#_name" attribute "\
  84. "cannot be updated to 0x%x, "\
  85. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  86. return -EINVAL;\
  87. }
  88. #define beiscsi_store_param(_name) \
  89. ssize_t \
  90. beiscsi_##_name##_store(struct device *dev,\
  91. struct device_attribute *attr, const char *buf,\
  92. size_t count) \
  93. { \
  94. struct Scsi_Host *shost = class_to_shost(dev);\
  95. struct beiscsi_hba *phba = iscsi_host_priv(shost);\
  96. uint32_t param_val = 0;\
  97. if (!isdigit(buf[0]))\
  98. return -EINVAL;\
  99. if (sscanf(buf, "%i", &param_val) != 1)\
  100. return -EINVAL;\
  101. if (beiscsi_##_name##_change(phba, param_val) == 0) \
  102. return strlen(buf);\
  103. else \
  104. return -EINVAL;\
  105. }
  106. #define beiscsi_init_param(_name, _minval, _maxval, _defval) \
  107. int \
  108. beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
  109. { \
  110. if (val >= _minval && val <= _maxval) {\
  111. phba->attr_##_name = val;\
  112. return 0;\
  113. } \
  114. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  115. "BA_%d beiscsi_"#_name" attribute " \
  116. "cannot be updated to 0x%x, "\
  117. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  118. phba->attr_##_name = _defval;\
  119. return -EINVAL;\
  120. }
  121. #define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
  122. static uint beiscsi_##_name = _defval;\
  123. module_param(beiscsi_##_name, uint, S_IRUGO);\
  124. MODULE_PARM_DESC(beiscsi_##_name, _descp);\
  125. beiscsi_disp_param(_name)\
  126. beiscsi_change_param(_name, _minval, _maxval, _defval)\
  127. beiscsi_store_param(_name)\
  128. beiscsi_init_param(_name, _minval, _maxval, _defval)\
  129. DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
  130. beiscsi_##_name##_disp, beiscsi_##_name##_store)
  131. /*
  132. * When new log level added update the
  133. * the MAX allowed value for log_enable
  134. */
  135. BEISCSI_RW_ATTR(log_enable, 0x00,
  136. 0xFF, 0x00, "Enable logging Bit Mask\n"
  137. "\t\t\t\tInitialization Events : 0x01\n"
  138. "\t\t\t\tMailbox Events : 0x02\n"
  139. "\t\t\t\tMiscellaneous Events : 0x04\n"
  140. "\t\t\t\tError Handling : 0x08\n"
  141. "\t\t\t\tIO Path Events : 0x10\n"
  142. "\t\t\t\tConfiguration Path : 0x20\n"
  143. "\t\t\t\tiSCSI Protocol : 0x40\n");
  144. DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
  145. DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
  146. DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
  147. DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
  148. DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
  149. beiscsi_active_session_disp, NULL);
  150. DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
  151. beiscsi_free_session_disp, NULL);
  152. struct device_attribute *beiscsi_attrs[] = {
  153. &dev_attr_beiscsi_log_enable,
  154. &dev_attr_beiscsi_drvr_ver,
  155. &dev_attr_beiscsi_adapter_family,
  156. &dev_attr_beiscsi_fw_ver,
  157. &dev_attr_beiscsi_active_session_count,
  158. &dev_attr_beiscsi_free_session_count,
  159. &dev_attr_beiscsi_phys_port,
  160. NULL,
  161. };
  162. static char const *cqe_desc[] = {
  163. "RESERVED_DESC",
  164. "SOL_CMD_COMPLETE",
  165. "SOL_CMD_KILLED_DATA_DIGEST_ERR",
  166. "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
  167. "CXN_KILLED_BURST_LEN_MISMATCH",
  168. "CXN_KILLED_AHS_RCVD",
  169. "CXN_KILLED_HDR_DIGEST_ERR",
  170. "CXN_KILLED_UNKNOWN_HDR",
  171. "CXN_KILLED_STALE_ITT_TTT_RCVD",
  172. "CXN_KILLED_INVALID_ITT_TTT_RCVD",
  173. "CXN_KILLED_RST_RCVD",
  174. "CXN_KILLED_TIMED_OUT",
  175. "CXN_KILLED_RST_SENT",
  176. "CXN_KILLED_FIN_RCVD",
  177. "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
  178. "CXN_KILLED_BAD_WRB_INDEX_ERROR",
  179. "CXN_KILLED_OVER_RUN_RESIDUAL",
  180. "CXN_KILLED_UNDER_RUN_RESIDUAL",
  181. "CMD_KILLED_INVALID_STATSN_RCVD",
  182. "CMD_KILLED_INVALID_R2T_RCVD",
  183. "CMD_CXN_KILLED_LUN_INVALID",
  184. "CMD_CXN_KILLED_ICD_INVALID",
  185. "CMD_CXN_KILLED_ITT_INVALID",
  186. "CMD_CXN_KILLED_SEQ_OUTOFORDER",
  187. "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
  188. "CXN_INVALIDATE_NOTIFY",
  189. "CXN_INVALIDATE_INDEX_NOTIFY",
  190. "CMD_INVALIDATED_NOTIFY",
  191. "UNSOL_HDR_NOTIFY",
  192. "UNSOL_DATA_NOTIFY",
  193. "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
  194. "DRIVERMSG_NOTIFY",
  195. "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
  196. "SOL_CMD_KILLED_DIF_ERR",
  197. "CXN_KILLED_SYN_RCVD",
  198. "CXN_KILLED_IMM_DATA_RCVD"
  199. };
  200. static int beiscsi_slave_configure(struct scsi_device *sdev)
  201. {
  202. blk_queue_max_segment_size(sdev->request_queue, 65536);
  203. return 0;
  204. }
  205. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  206. {
  207. struct iscsi_cls_session *cls_session;
  208. struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
  209. struct beiscsi_io_task *aborted_io_task;
  210. struct iscsi_conn *conn;
  211. struct beiscsi_conn *beiscsi_conn;
  212. struct beiscsi_hba *phba;
  213. struct iscsi_session *session;
  214. struct invalidate_command_table *inv_tbl;
  215. struct be_dma_mem nonemb_cmd;
  216. unsigned int cid, tag, num_invalidate;
  217. int rc;
  218. cls_session = starget_to_session(scsi_target(sc->device));
  219. session = cls_session->dd_data;
  220. spin_lock_bh(&session->frwd_lock);
  221. if (!aborted_task || !aborted_task->sc) {
  222. /* we raced */
  223. spin_unlock_bh(&session->frwd_lock);
  224. return SUCCESS;
  225. }
  226. aborted_io_task = aborted_task->dd_data;
  227. if (!aborted_io_task->scsi_cmnd) {
  228. /* raced or invalid command */
  229. spin_unlock_bh(&session->frwd_lock);
  230. return SUCCESS;
  231. }
  232. spin_unlock_bh(&session->frwd_lock);
  233. /* Invalidate WRB Posted for this Task */
  234. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  235. aborted_io_task->pwrb_handle->pwrb,
  236. 1);
  237. conn = aborted_task->conn;
  238. beiscsi_conn = conn->dd_data;
  239. phba = beiscsi_conn->phba;
  240. /* invalidate iocb */
  241. cid = beiscsi_conn->beiscsi_conn_cid;
  242. inv_tbl = phba->inv_tbl;
  243. memset(inv_tbl, 0x0, sizeof(*inv_tbl));
  244. inv_tbl->cid = cid;
  245. inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
  246. num_invalidate = 1;
  247. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  248. sizeof(struct invalidate_commands_params_in),
  249. &nonemb_cmd.dma);
  250. if (nonemb_cmd.va == NULL) {
  251. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  252. "BM_%d : Failed to allocate memory for"
  253. "mgmt_invalidate_icds\n");
  254. return FAILED;
  255. }
  256. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  257. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  258. cid, &nonemb_cmd);
  259. if (!tag) {
  260. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  261. "BM_%d : mgmt_invalidate_icds could not be"
  262. "submitted\n");
  263. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  264. nonemb_cmd.va, nonemb_cmd.dma);
  265. return FAILED;
  266. }
  267. rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
  268. if (rc != -EBUSY)
  269. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  270. nonemb_cmd.va, nonemb_cmd.dma);
  271. return iscsi_eh_abort(sc);
  272. }
  273. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  274. {
  275. struct iscsi_task *abrt_task;
  276. struct beiscsi_io_task *abrt_io_task;
  277. struct iscsi_conn *conn;
  278. struct beiscsi_conn *beiscsi_conn;
  279. struct beiscsi_hba *phba;
  280. struct iscsi_session *session;
  281. struct iscsi_cls_session *cls_session;
  282. struct invalidate_command_table *inv_tbl;
  283. struct be_dma_mem nonemb_cmd;
  284. unsigned int cid, tag, i, num_invalidate;
  285. int rc;
  286. /* invalidate iocbs */
  287. cls_session = starget_to_session(scsi_target(sc->device));
  288. session = cls_session->dd_data;
  289. spin_lock_bh(&session->frwd_lock);
  290. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
  291. spin_unlock_bh(&session->frwd_lock);
  292. return FAILED;
  293. }
  294. conn = session->leadconn;
  295. beiscsi_conn = conn->dd_data;
  296. phba = beiscsi_conn->phba;
  297. cid = beiscsi_conn->beiscsi_conn_cid;
  298. inv_tbl = phba->inv_tbl;
  299. memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
  300. num_invalidate = 0;
  301. for (i = 0; i < conn->session->cmds_max; i++) {
  302. abrt_task = conn->session->cmds[i];
  303. abrt_io_task = abrt_task->dd_data;
  304. if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
  305. continue;
  306. if (sc->device->lun != abrt_task->sc->device->lun)
  307. continue;
  308. /* Invalidate WRB Posted for this Task */
  309. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  310. abrt_io_task->pwrb_handle->pwrb,
  311. 1);
  312. inv_tbl->cid = cid;
  313. inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
  314. num_invalidate++;
  315. inv_tbl++;
  316. }
  317. spin_unlock_bh(&session->frwd_lock);
  318. inv_tbl = phba->inv_tbl;
  319. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  320. sizeof(struct invalidate_commands_params_in),
  321. &nonemb_cmd.dma);
  322. if (nonemb_cmd.va == NULL) {
  323. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  324. "BM_%d : Failed to allocate memory for"
  325. "mgmt_invalidate_icds\n");
  326. return FAILED;
  327. }
  328. nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
  329. memset(nonemb_cmd.va, 0, nonemb_cmd.size);
  330. tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
  331. cid, &nonemb_cmd);
  332. if (!tag) {
  333. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  334. "BM_%d : mgmt_invalidate_icds could not be"
  335. " submitted\n");
  336. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  337. nonemb_cmd.va, nonemb_cmd.dma);
  338. return FAILED;
  339. }
  340. rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
  341. if (rc != -EBUSY)
  342. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  343. nonemb_cmd.va, nonemb_cmd.dma);
  344. return iscsi_eh_device_reset(sc);
  345. }
  346. /*------------------- PCI Driver operations and data ----------------- */
  347. static const struct pci_device_id beiscsi_pci_id_table[] = {
  348. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  349. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  350. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  351. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  352. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  353. { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
  354. { 0 }
  355. };
  356. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  357. static struct scsi_host_template beiscsi_sht = {
  358. .module = THIS_MODULE,
  359. .name = "Emulex 10Gbe open-iscsi Initiator Driver",
  360. .proc_name = DRV_NAME,
  361. .queuecommand = iscsi_queuecommand,
  362. .change_queue_depth = scsi_change_queue_depth,
  363. .slave_configure = beiscsi_slave_configure,
  364. .target_alloc = iscsi_target_alloc,
  365. .eh_abort_handler = beiscsi_eh_abort,
  366. .eh_device_reset_handler = beiscsi_eh_device_reset,
  367. .eh_target_reset_handler = iscsi_eh_session_reset,
  368. .shost_attrs = beiscsi_attrs,
  369. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  370. .can_queue = BE2_IO_DEPTH,
  371. .this_id = -1,
  372. .max_sectors = BEISCSI_MAX_SECTORS,
  373. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  374. .use_clustering = ENABLE_CLUSTERING,
  375. .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
  376. .track_queue_depth = 1,
  377. };
  378. static struct scsi_transport_template *beiscsi_scsi_transport;
  379. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  380. {
  381. struct beiscsi_hba *phba;
  382. struct Scsi_Host *shost;
  383. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  384. if (!shost) {
  385. dev_err(&pcidev->dev,
  386. "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
  387. return NULL;
  388. }
  389. shost->max_id = BE2_MAX_SESSIONS;
  390. shost->max_channel = 0;
  391. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  392. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  393. shost->transportt = beiscsi_scsi_transport;
  394. phba = iscsi_host_priv(shost);
  395. memset(phba, 0, sizeof(*phba));
  396. phba->shost = shost;
  397. phba->pcidev = pci_dev_get(pcidev);
  398. pci_set_drvdata(pcidev, phba);
  399. phba->interface_handle = 0xFFFFFFFF;
  400. return phba;
  401. }
  402. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  403. {
  404. if (phba->csr_va) {
  405. iounmap(phba->csr_va);
  406. phba->csr_va = NULL;
  407. }
  408. if (phba->db_va) {
  409. iounmap(phba->db_va);
  410. phba->db_va = NULL;
  411. }
  412. if (phba->pci_va) {
  413. iounmap(phba->pci_va);
  414. phba->pci_va = NULL;
  415. }
  416. }
  417. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  418. struct pci_dev *pcidev)
  419. {
  420. u8 __iomem *addr;
  421. int pcicfg_reg;
  422. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  423. pci_resource_len(pcidev, 2));
  424. if (addr == NULL)
  425. return -ENOMEM;
  426. phba->ctrl.csr = addr;
  427. phba->csr_va = addr;
  428. phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
  429. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  430. if (addr == NULL)
  431. goto pci_map_err;
  432. phba->ctrl.db = addr;
  433. phba->db_va = addr;
  434. phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
  435. if (phba->generation == BE_GEN2)
  436. pcicfg_reg = 1;
  437. else
  438. pcicfg_reg = 0;
  439. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  440. pci_resource_len(pcidev, pcicfg_reg));
  441. if (addr == NULL)
  442. goto pci_map_err;
  443. phba->ctrl.pcicfg = addr;
  444. phba->pci_va = addr;
  445. phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
  446. return 0;
  447. pci_map_err:
  448. beiscsi_unmap_pci_function(phba);
  449. return -ENOMEM;
  450. }
  451. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  452. {
  453. int ret;
  454. ret = pci_enable_device(pcidev);
  455. if (ret) {
  456. dev_err(&pcidev->dev,
  457. "beiscsi_enable_pci - enable device failed\n");
  458. return ret;
  459. }
  460. ret = pci_request_regions(pcidev, DRV_NAME);
  461. if (ret) {
  462. dev_err(&pcidev->dev,
  463. "beiscsi_enable_pci - request region failed\n");
  464. goto pci_dev_disable;
  465. }
  466. pci_set_master(pcidev);
  467. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
  468. if (ret) {
  469. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
  470. if (ret) {
  471. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  472. goto pci_region_release;
  473. } else {
  474. ret = pci_set_consistent_dma_mask(pcidev,
  475. DMA_BIT_MASK(32));
  476. }
  477. } else {
  478. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
  479. if (ret) {
  480. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  481. goto pci_region_release;
  482. }
  483. }
  484. return 0;
  485. pci_region_release:
  486. pci_release_regions(pcidev);
  487. pci_dev_disable:
  488. pci_disable_device(pcidev);
  489. return ret;
  490. }
  491. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  492. {
  493. struct be_ctrl_info *ctrl = &phba->ctrl;
  494. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  495. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  496. int status = 0;
  497. ctrl->pdev = pdev;
  498. status = beiscsi_map_pci_bars(phba, pdev);
  499. if (status)
  500. return status;
  501. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  502. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  503. mbox_mem_alloc->size,
  504. &mbox_mem_alloc->dma);
  505. if (!mbox_mem_alloc->va) {
  506. beiscsi_unmap_pci_function(phba);
  507. return -ENOMEM;
  508. }
  509. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  510. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  511. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  512. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  513. mutex_init(&ctrl->mbox_lock);
  514. spin_lock_init(&phba->ctrl.mcc_lock);
  515. return status;
  516. }
  517. /**
  518. * beiscsi_get_params()- Set the config paramters
  519. * @phba: ptr device priv structure
  520. **/
  521. static void beiscsi_get_params(struct beiscsi_hba *phba)
  522. {
  523. uint32_t total_cid_count = 0;
  524. uint32_t total_icd_count = 0;
  525. uint8_t ulp_num = 0;
  526. total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
  527. BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
  528. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  529. uint32_t align_mask = 0;
  530. uint32_t icd_post_per_page = 0;
  531. uint32_t icd_count_unavailable = 0;
  532. uint32_t icd_start = 0, icd_count = 0;
  533. uint32_t icd_start_align = 0, icd_count_align = 0;
  534. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  535. icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  536. icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  537. /* Get ICD count that can be posted on each page */
  538. icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
  539. sizeof(struct iscsi_sge)));
  540. align_mask = (icd_post_per_page - 1);
  541. /* Check if icd_start is aligned ICD per page posting */
  542. if (icd_start % icd_post_per_page) {
  543. icd_start_align = ((icd_start +
  544. icd_post_per_page) &
  545. ~(align_mask));
  546. phba->fw_config.
  547. iscsi_icd_start[ulp_num] =
  548. icd_start_align;
  549. }
  550. icd_count_align = (icd_count & ~align_mask);
  551. /* ICD discarded in the process of alignment */
  552. if (icd_start_align)
  553. icd_count_unavailable = ((icd_start_align -
  554. icd_start) +
  555. (icd_count -
  556. icd_count_align));
  557. /* Updated ICD count available */
  558. phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
  559. icd_count_unavailable);
  560. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  561. "BM_%d : Aligned ICD values\n"
  562. "\t ICD Start : %d\n"
  563. "\t ICD Count : %d\n"
  564. "\t ICD Discarded : %d\n",
  565. phba->fw_config.
  566. iscsi_icd_start[ulp_num],
  567. phba->fw_config.
  568. iscsi_icd_count[ulp_num],
  569. icd_count_unavailable);
  570. break;
  571. }
  572. }
  573. total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  574. phba->params.ios_per_ctrl = (total_icd_count -
  575. (total_cid_count +
  576. BE2_TMFS + BE2_NOPOUT_REQ));
  577. phba->params.cxns_per_ctrl = total_cid_count;
  578. phba->params.asyncpdus_per_ctrl = total_cid_count;
  579. phba->params.icds_per_ctrl = total_icd_count;
  580. phba->params.num_sge_per_io = BE2_SGE;
  581. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  582. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  583. phba->params.eq_timer = 64;
  584. phba->params.num_eq_entries = 1024;
  585. phba->params.num_cq_entries = 1024;
  586. phba->params.wrbs_per_cxn = 256;
  587. }
  588. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  589. unsigned int id, unsigned int clr_interrupt,
  590. unsigned int num_processed,
  591. unsigned char rearm, unsigned char event)
  592. {
  593. u32 val = 0;
  594. if (rearm)
  595. val |= 1 << DB_EQ_REARM_SHIFT;
  596. if (clr_interrupt)
  597. val |= 1 << DB_EQ_CLR_SHIFT;
  598. if (event)
  599. val |= 1 << DB_EQ_EVNT_SHIFT;
  600. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  601. /* Setting lower order EQ_ID Bits */
  602. val |= (id & DB_EQ_RING_ID_LOW_MASK);
  603. /* Setting Higher order EQ_ID Bits */
  604. val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
  605. DB_EQ_RING_ID_HIGH_MASK)
  606. << DB_EQ_HIGH_SET_SHIFT);
  607. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  608. }
  609. /**
  610. * be_isr_mcc - The isr routine of the driver.
  611. * @irq: Not used
  612. * @dev_id: Pointer to host adapter structure
  613. */
  614. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  615. {
  616. struct beiscsi_hba *phba;
  617. struct be_eq_entry *eqe;
  618. struct be_queue_info *eq;
  619. struct be_queue_info *mcc;
  620. unsigned int mcc_events;
  621. struct be_eq_obj *pbe_eq;
  622. pbe_eq = dev_id;
  623. eq = &pbe_eq->q;
  624. phba = pbe_eq->phba;
  625. mcc = &phba->ctrl.mcc_obj.cq;
  626. eqe = queue_tail_node(eq);
  627. mcc_events = 0;
  628. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  629. & EQE_VALID_MASK) {
  630. if (((eqe->dw[offsetof(struct amap_eq_entry,
  631. resource_id) / 32] &
  632. EQE_RESID_MASK) >> 16) == mcc->id) {
  633. mcc_events++;
  634. }
  635. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  636. queue_tail_inc(eq);
  637. eqe = queue_tail_node(eq);
  638. }
  639. if (mcc_events) {
  640. queue_work(phba->wq, &pbe_eq->mcc_work);
  641. hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
  642. }
  643. return IRQ_HANDLED;
  644. }
  645. /**
  646. * be_isr_msix - The isr routine of the driver.
  647. * @irq: Not used
  648. * @dev_id: Pointer to host adapter structure
  649. */
  650. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  651. {
  652. struct beiscsi_hba *phba;
  653. struct be_queue_info *eq;
  654. struct be_eq_obj *pbe_eq;
  655. pbe_eq = dev_id;
  656. eq = &pbe_eq->q;
  657. phba = pbe_eq->phba;
  658. /* disable interrupt till iopoll completes */
  659. hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
  660. irq_poll_sched(&pbe_eq->iopoll);
  661. return IRQ_HANDLED;
  662. }
  663. /**
  664. * be_isr - The isr routine of the driver.
  665. * @irq: Not used
  666. * @dev_id: Pointer to host adapter structure
  667. */
  668. static irqreturn_t be_isr(int irq, void *dev_id)
  669. {
  670. struct beiscsi_hba *phba;
  671. struct hwi_controller *phwi_ctrlr;
  672. struct hwi_context_memory *phwi_context;
  673. struct be_eq_entry *eqe;
  674. struct be_queue_info *eq;
  675. struct be_queue_info *mcc;
  676. unsigned int mcc_events, io_events;
  677. struct be_ctrl_info *ctrl;
  678. struct be_eq_obj *pbe_eq;
  679. int isr, rearm;
  680. phba = dev_id;
  681. ctrl = &phba->ctrl;
  682. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  683. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  684. if (!isr)
  685. return IRQ_NONE;
  686. phwi_ctrlr = phba->phwi_ctrlr;
  687. phwi_context = phwi_ctrlr->phwi_ctxt;
  688. pbe_eq = &phwi_context->be_eq[0];
  689. eq = &phwi_context->be_eq[0].q;
  690. mcc = &phba->ctrl.mcc_obj.cq;
  691. eqe = queue_tail_node(eq);
  692. io_events = 0;
  693. mcc_events = 0;
  694. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  695. & EQE_VALID_MASK) {
  696. if (((eqe->dw[offsetof(struct amap_eq_entry,
  697. resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
  698. mcc_events++;
  699. else
  700. io_events++;
  701. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  702. queue_tail_inc(eq);
  703. eqe = queue_tail_node(eq);
  704. }
  705. if (!io_events && !mcc_events)
  706. return IRQ_NONE;
  707. /* no need to rearm if interrupt is only for IOs */
  708. rearm = 0;
  709. if (mcc_events) {
  710. queue_work(phba->wq, &pbe_eq->mcc_work);
  711. /* rearm for MCCQ */
  712. rearm = 1;
  713. }
  714. if (io_events)
  715. irq_poll_sched(&pbe_eq->iopoll);
  716. hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
  717. return IRQ_HANDLED;
  718. }
  719. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  720. {
  721. struct pci_dev *pcidev = phba->pcidev;
  722. struct hwi_controller *phwi_ctrlr;
  723. struct hwi_context_memory *phwi_context;
  724. int ret, msix_vec, i, j;
  725. phwi_ctrlr = phba->phwi_ctrlr;
  726. phwi_context = phwi_ctrlr->phwi_ctxt;
  727. if (phba->msix_enabled) {
  728. for (i = 0; i < phba->num_cpus; i++) {
  729. phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
  730. GFP_KERNEL);
  731. if (!phba->msi_name[i]) {
  732. ret = -ENOMEM;
  733. goto free_msix_irqs;
  734. }
  735. sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
  736. phba->shost->host_no, i);
  737. msix_vec = phba->msix_entries[i].vector;
  738. ret = request_irq(msix_vec, be_isr_msix, 0,
  739. phba->msi_name[i],
  740. &phwi_context->be_eq[i]);
  741. if (ret) {
  742. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  743. "BM_%d : beiscsi_init_irqs-Failed to"
  744. "register msix for i = %d\n",
  745. i);
  746. kfree(phba->msi_name[i]);
  747. goto free_msix_irqs;
  748. }
  749. }
  750. phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
  751. if (!phba->msi_name[i]) {
  752. ret = -ENOMEM;
  753. goto free_msix_irqs;
  754. }
  755. sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
  756. phba->shost->host_no);
  757. msix_vec = phba->msix_entries[i].vector;
  758. ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
  759. &phwi_context->be_eq[i]);
  760. if (ret) {
  761. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
  762. "BM_%d : beiscsi_init_irqs-"
  763. "Failed to register beiscsi_msix_mcc\n");
  764. kfree(phba->msi_name[i]);
  765. goto free_msix_irqs;
  766. }
  767. } else {
  768. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  769. "beiscsi", phba);
  770. if (ret) {
  771. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  772. "BM_%d : beiscsi_init_irqs-"
  773. "Failed to register irq\\n");
  774. return ret;
  775. }
  776. }
  777. return 0;
  778. free_msix_irqs:
  779. for (j = i - 1; j >= 0; j--) {
  780. kfree(phba->msi_name[j]);
  781. msix_vec = phba->msix_entries[j].vector;
  782. free_irq(msix_vec, &phwi_context->be_eq[j]);
  783. }
  784. return ret;
  785. }
  786. void hwi_ring_cq_db(struct beiscsi_hba *phba,
  787. unsigned int id, unsigned int num_processed,
  788. unsigned char rearm)
  789. {
  790. u32 val = 0;
  791. if (rearm)
  792. val |= 1 << DB_CQ_REARM_SHIFT;
  793. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  794. /* Setting lower order CQ_ID Bits */
  795. val |= (id & DB_CQ_RING_ID_LOW_MASK);
  796. /* Setting Higher order CQ_ID Bits */
  797. val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
  798. DB_CQ_RING_ID_HIGH_MASK)
  799. << DB_CQ_HIGH_SET_SHIFT);
  800. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  801. }
  802. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  803. {
  804. struct sgl_handle *psgl_handle;
  805. spin_lock_bh(&phba->io_sgl_lock);
  806. if (phba->io_sgl_hndl_avbl) {
  807. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  808. "BM_%d : In alloc_io_sgl_handle,"
  809. " io_sgl_alloc_index=%d\n",
  810. phba->io_sgl_alloc_index);
  811. psgl_handle = phba->io_sgl_hndl_base[phba->
  812. io_sgl_alloc_index];
  813. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  814. phba->io_sgl_hndl_avbl--;
  815. if (phba->io_sgl_alloc_index == (phba->params.
  816. ios_per_ctrl - 1))
  817. phba->io_sgl_alloc_index = 0;
  818. else
  819. phba->io_sgl_alloc_index++;
  820. } else
  821. psgl_handle = NULL;
  822. spin_unlock_bh(&phba->io_sgl_lock);
  823. return psgl_handle;
  824. }
  825. static void
  826. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  827. {
  828. spin_lock_bh(&phba->io_sgl_lock);
  829. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  830. "BM_%d : In free_,io_sgl_free_index=%d\n",
  831. phba->io_sgl_free_index);
  832. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  833. /*
  834. * this can happen if clean_task is called on a task that
  835. * failed in xmit_task or alloc_pdu.
  836. */
  837. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  838. "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
  839. "value there=%p\n", phba->io_sgl_free_index,
  840. phba->io_sgl_hndl_base
  841. [phba->io_sgl_free_index]);
  842. spin_unlock_bh(&phba->io_sgl_lock);
  843. return;
  844. }
  845. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  846. phba->io_sgl_hndl_avbl++;
  847. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  848. phba->io_sgl_free_index = 0;
  849. else
  850. phba->io_sgl_free_index++;
  851. spin_unlock_bh(&phba->io_sgl_lock);
  852. }
  853. static inline struct wrb_handle *
  854. beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
  855. unsigned int wrbs_per_cxn)
  856. {
  857. struct wrb_handle *pwrb_handle;
  858. spin_lock_bh(&pwrb_context->wrb_lock);
  859. pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
  860. pwrb_context->wrb_handles_available--;
  861. if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
  862. pwrb_context->alloc_index = 0;
  863. else
  864. pwrb_context->alloc_index++;
  865. spin_unlock_bh(&pwrb_context->wrb_lock);
  866. if (pwrb_handle)
  867. memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
  868. return pwrb_handle;
  869. }
  870. /**
  871. * alloc_wrb_handle - To allocate a wrb handle
  872. * @phba: The hba pointer
  873. * @cid: The cid to use for allocation
  874. * @pwrb_context: ptr to ptr to wrb context
  875. *
  876. * This happens under session_lock until submission to chip
  877. */
  878. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
  879. struct hwi_wrb_context **pcontext)
  880. {
  881. struct hwi_wrb_context *pwrb_context;
  882. struct hwi_controller *phwi_ctrlr;
  883. uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
  884. phwi_ctrlr = phba->phwi_ctrlr;
  885. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  886. /* return the context address */
  887. *pcontext = pwrb_context;
  888. return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
  889. }
  890. static inline void
  891. beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
  892. struct wrb_handle *pwrb_handle,
  893. unsigned int wrbs_per_cxn)
  894. {
  895. spin_lock_bh(&pwrb_context->wrb_lock);
  896. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  897. pwrb_context->wrb_handles_available++;
  898. if (pwrb_context->free_index == (wrbs_per_cxn - 1))
  899. pwrb_context->free_index = 0;
  900. else
  901. pwrb_context->free_index++;
  902. spin_unlock_bh(&pwrb_context->wrb_lock);
  903. }
  904. /**
  905. * free_wrb_handle - To free the wrb handle back to pool
  906. * @phba: The hba pointer
  907. * @pwrb_context: The context to free from
  908. * @pwrb_handle: The wrb_handle to free
  909. *
  910. * This happens under session_lock until submission to chip
  911. */
  912. static void
  913. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  914. struct wrb_handle *pwrb_handle)
  915. {
  916. beiscsi_put_wrb_handle(pwrb_context,
  917. pwrb_handle,
  918. phba->params.wrbs_per_cxn);
  919. beiscsi_log(phba, KERN_INFO,
  920. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  921. "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
  922. "wrb_handles_available=%d\n",
  923. pwrb_handle, pwrb_context->free_index,
  924. pwrb_context->wrb_handles_available);
  925. }
  926. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  927. {
  928. struct sgl_handle *psgl_handle;
  929. spin_lock_bh(&phba->mgmt_sgl_lock);
  930. if (phba->eh_sgl_hndl_avbl) {
  931. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  932. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  933. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  934. "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
  935. phba->eh_sgl_alloc_index,
  936. phba->eh_sgl_alloc_index);
  937. phba->eh_sgl_hndl_avbl--;
  938. if (phba->eh_sgl_alloc_index ==
  939. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  940. 1))
  941. phba->eh_sgl_alloc_index = 0;
  942. else
  943. phba->eh_sgl_alloc_index++;
  944. } else
  945. psgl_handle = NULL;
  946. spin_unlock_bh(&phba->mgmt_sgl_lock);
  947. return psgl_handle;
  948. }
  949. void
  950. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  951. {
  952. spin_lock_bh(&phba->mgmt_sgl_lock);
  953. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  954. "BM_%d : In free_mgmt_sgl_handle,"
  955. "eh_sgl_free_index=%d\n",
  956. phba->eh_sgl_free_index);
  957. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  958. /*
  959. * this can happen if clean_task is called on a task that
  960. * failed in xmit_task or alloc_pdu.
  961. */
  962. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
  963. "BM_%d : Double Free in eh SGL ,"
  964. "eh_sgl_free_index=%d\n",
  965. phba->eh_sgl_free_index);
  966. spin_unlock_bh(&phba->mgmt_sgl_lock);
  967. return;
  968. }
  969. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  970. phba->eh_sgl_hndl_avbl++;
  971. if (phba->eh_sgl_free_index ==
  972. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  973. phba->eh_sgl_free_index = 0;
  974. else
  975. phba->eh_sgl_free_index++;
  976. spin_unlock_bh(&phba->mgmt_sgl_lock);
  977. }
  978. static void
  979. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  980. struct iscsi_task *task,
  981. struct common_sol_cqe *csol_cqe)
  982. {
  983. struct beiscsi_io_task *io_task = task->dd_data;
  984. struct be_status_bhs *sts_bhs =
  985. (struct be_status_bhs *)io_task->cmd_bhs;
  986. struct iscsi_conn *conn = beiscsi_conn->conn;
  987. unsigned char *sense;
  988. u32 resid = 0, exp_cmdsn, max_cmdsn;
  989. u8 rsp, status, flags;
  990. exp_cmdsn = csol_cqe->exp_cmdsn;
  991. max_cmdsn = (csol_cqe->exp_cmdsn +
  992. csol_cqe->cmd_wnd - 1);
  993. rsp = csol_cqe->i_resp;
  994. status = csol_cqe->i_sts;
  995. flags = csol_cqe->i_flags;
  996. resid = csol_cqe->res_cnt;
  997. if (!task->sc) {
  998. if (io_task->scsi_cmnd) {
  999. scsi_dma_unmap(io_task->scsi_cmnd);
  1000. io_task->scsi_cmnd = NULL;
  1001. }
  1002. return;
  1003. }
  1004. task->sc->result = (DID_OK << 16) | status;
  1005. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  1006. task->sc->result = DID_ERROR << 16;
  1007. goto unmap;
  1008. }
  1009. /* bidi not initially supported */
  1010. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  1011. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  1012. task->sc->result = DID_ERROR << 16;
  1013. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  1014. scsi_set_resid(task->sc, resid);
  1015. if (!status && (scsi_bufflen(task->sc) - resid <
  1016. task->sc->underflow))
  1017. task->sc->result = DID_ERROR << 16;
  1018. }
  1019. }
  1020. if (status == SAM_STAT_CHECK_CONDITION) {
  1021. u16 sense_len;
  1022. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  1023. sense = sts_bhs->sense_info + sizeof(unsigned short);
  1024. sense_len = be16_to_cpu(*slen);
  1025. memcpy(task->sc->sense_buffer, sense,
  1026. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  1027. }
  1028. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
  1029. conn->rxdata_octets += resid;
  1030. unmap:
  1031. if (io_task->scsi_cmnd) {
  1032. scsi_dma_unmap(io_task->scsi_cmnd);
  1033. io_task->scsi_cmnd = NULL;
  1034. }
  1035. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  1036. }
  1037. static void
  1038. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  1039. struct iscsi_task *task,
  1040. struct common_sol_cqe *csol_cqe)
  1041. {
  1042. struct iscsi_logout_rsp *hdr;
  1043. struct beiscsi_io_task *io_task = task->dd_data;
  1044. struct iscsi_conn *conn = beiscsi_conn->conn;
  1045. hdr = (struct iscsi_logout_rsp *)task->hdr;
  1046. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  1047. hdr->t2wait = 5;
  1048. hdr->t2retain = 0;
  1049. hdr->flags = csol_cqe->i_flags;
  1050. hdr->response = csol_cqe->i_resp;
  1051. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1052. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1053. csol_cqe->cmd_wnd - 1);
  1054. hdr->dlength[0] = 0;
  1055. hdr->dlength[1] = 0;
  1056. hdr->dlength[2] = 0;
  1057. hdr->hlength = 0;
  1058. hdr->itt = io_task->libiscsi_itt;
  1059. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1060. }
  1061. static void
  1062. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  1063. struct iscsi_task *task,
  1064. struct common_sol_cqe *csol_cqe)
  1065. {
  1066. struct iscsi_tm_rsp *hdr;
  1067. struct iscsi_conn *conn = beiscsi_conn->conn;
  1068. struct beiscsi_io_task *io_task = task->dd_data;
  1069. hdr = (struct iscsi_tm_rsp *)task->hdr;
  1070. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  1071. hdr->flags = csol_cqe->i_flags;
  1072. hdr->response = csol_cqe->i_resp;
  1073. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1074. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1075. csol_cqe->cmd_wnd - 1);
  1076. hdr->itt = io_task->libiscsi_itt;
  1077. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1078. }
  1079. static void
  1080. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  1081. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1082. {
  1083. struct hwi_wrb_context *pwrb_context;
  1084. uint16_t wrb_index, cid, cri_index;
  1085. struct hwi_controller *phwi_ctrlr;
  1086. struct wrb_handle *pwrb_handle;
  1087. struct iscsi_task *task;
  1088. phwi_ctrlr = phba->phwi_ctrlr;
  1089. if (is_chip_be2_be3r(phba)) {
  1090. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1091. wrb_idx, psol);
  1092. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1093. cid, psol);
  1094. } else {
  1095. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1096. wrb_idx, psol);
  1097. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1098. cid, psol);
  1099. }
  1100. cri_index = BE_GET_CRI_FROM_CID(cid);
  1101. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1102. pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
  1103. task = pwrb_handle->pio_handle;
  1104. iscsi_put_task(task);
  1105. }
  1106. static void
  1107. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  1108. struct iscsi_task *task,
  1109. struct common_sol_cqe *csol_cqe)
  1110. {
  1111. struct iscsi_nopin *hdr;
  1112. struct iscsi_conn *conn = beiscsi_conn->conn;
  1113. struct beiscsi_io_task *io_task = task->dd_data;
  1114. hdr = (struct iscsi_nopin *)task->hdr;
  1115. hdr->flags = csol_cqe->i_flags;
  1116. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1117. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1118. csol_cqe->cmd_wnd - 1);
  1119. hdr->opcode = ISCSI_OP_NOOP_IN;
  1120. hdr->itt = io_task->libiscsi_itt;
  1121. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1122. }
  1123. static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
  1124. struct sol_cqe *psol,
  1125. struct common_sol_cqe *csol_cqe)
  1126. {
  1127. if (is_chip_be2_be3r(phba)) {
  1128. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
  1129. i_exp_cmd_sn, psol);
  1130. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
  1131. i_res_cnt, psol);
  1132. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
  1133. i_cmd_wnd, psol);
  1134. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
  1135. wrb_index, psol);
  1136. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
  1137. cid, psol);
  1138. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1139. hw_sts, psol);
  1140. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
  1141. i_resp, psol);
  1142. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1143. i_sts, psol);
  1144. csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
  1145. i_flags, psol);
  1146. } else {
  1147. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1148. i_exp_cmd_sn, psol);
  1149. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1150. i_res_cnt, psol);
  1151. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1152. wrb_index, psol);
  1153. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1154. cid, psol);
  1155. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1156. hw_sts, psol);
  1157. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1158. i_cmd_wnd, psol);
  1159. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1160. cmd_cmpl, psol))
  1161. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1162. i_sts, psol);
  1163. else
  1164. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1165. i_sts, psol);
  1166. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1167. u, psol))
  1168. csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
  1169. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1170. o, psol))
  1171. csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
  1172. }
  1173. }
  1174. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  1175. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1176. {
  1177. struct hwi_wrb_context *pwrb_context;
  1178. struct wrb_handle *pwrb_handle;
  1179. struct iscsi_wrb *pwrb = NULL;
  1180. struct hwi_controller *phwi_ctrlr;
  1181. struct iscsi_task *task;
  1182. unsigned int type;
  1183. struct iscsi_conn *conn = beiscsi_conn->conn;
  1184. struct iscsi_session *session = conn->session;
  1185. struct common_sol_cqe csol_cqe = {0};
  1186. uint16_t cri_index = 0;
  1187. phwi_ctrlr = phba->phwi_ctrlr;
  1188. /* Copy the elements to a common structure */
  1189. adapter_get_sol_cqe(phba, psol, &csol_cqe);
  1190. cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
  1191. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1192. pwrb_handle = pwrb_context->pwrb_handle_basestd[
  1193. csol_cqe.wrb_index];
  1194. task = pwrb_handle->pio_handle;
  1195. pwrb = pwrb_handle->pwrb;
  1196. type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
  1197. spin_lock_bh(&session->back_lock);
  1198. switch (type) {
  1199. case HWH_TYPE_IO:
  1200. case HWH_TYPE_IO_RD:
  1201. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1202. ISCSI_OP_NOOP_OUT)
  1203. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1204. else
  1205. be_complete_io(beiscsi_conn, task, &csol_cqe);
  1206. break;
  1207. case HWH_TYPE_LOGOUT:
  1208. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1209. be_complete_logout(beiscsi_conn, task, &csol_cqe);
  1210. else
  1211. be_complete_tmf(beiscsi_conn, task, &csol_cqe);
  1212. break;
  1213. case HWH_TYPE_LOGIN:
  1214. beiscsi_log(phba, KERN_ERR,
  1215. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1216. "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
  1217. " hwi_complete_cmd- Solicited path\n");
  1218. break;
  1219. case HWH_TYPE_NOP:
  1220. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1221. break;
  1222. default:
  1223. beiscsi_log(phba, KERN_WARNING,
  1224. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1225. "BM_%d : In hwi_complete_cmd, unknown type = %d"
  1226. "wrb_index 0x%x CID 0x%x\n", type,
  1227. csol_cqe.wrb_index,
  1228. csol_cqe.cid);
  1229. break;
  1230. }
  1231. spin_unlock_bh(&session->back_lock);
  1232. }
  1233. /**
  1234. * ASYNC PDUs include
  1235. * a. Unsolicited NOP-In (target initiated NOP-In)
  1236. * b. ASYNC Messages
  1237. * c. Reject PDU
  1238. * d. Login response
  1239. * These headers arrive unprocessed by the EP firmware.
  1240. * iSCSI layer processes them.
  1241. */
  1242. static unsigned int
  1243. beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
  1244. struct pdu_base *phdr, void *pdata, unsigned int dlen)
  1245. {
  1246. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1247. struct iscsi_conn *conn = beiscsi_conn->conn;
  1248. struct beiscsi_io_task *io_task;
  1249. struct iscsi_hdr *login_hdr;
  1250. struct iscsi_task *task;
  1251. u8 code;
  1252. code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
  1253. switch (code) {
  1254. case ISCSI_OP_NOOP_IN:
  1255. pdata = NULL;
  1256. dlen = 0;
  1257. break;
  1258. case ISCSI_OP_ASYNC_EVENT:
  1259. break;
  1260. case ISCSI_OP_REJECT:
  1261. WARN_ON(!pdata);
  1262. WARN_ON(!(dlen == 48));
  1263. beiscsi_log(phba, KERN_ERR,
  1264. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1265. "BM_%d : In ISCSI_OP_REJECT\n");
  1266. break;
  1267. case ISCSI_OP_LOGIN_RSP:
  1268. case ISCSI_OP_TEXT_RSP:
  1269. task = conn->login_task;
  1270. io_task = task->dd_data;
  1271. login_hdr = (struct iscsi_hdr *)phdr;
  1272. login_hdr->itt = io_task->libiscsi_itt;
  1273. break;
  1274. default:
  1275. beiscsi_log(phba, KERN_WARNING,
  1276. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1277. "BM_%d : unrecognized async PDU opcode 0x%x\n",
  1278. code);
  1279. return 1;
  1280. }
  1281. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
  1282. return 0;
  1283. }
  1284. static inline void
  1285. beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
  1286. struct hd_async_handle *pasync_handle)
  1287. {
  1288. if (pasync_handle->is_header) {
  1289. list_add_tail(&pasync_handle->link,
  1290. &pasync_ctx->async_header.free_list);
  1291. pasync_ctx->async_header.free_entries++;
  1292. } else {
  1293. list_add_tail(&pasync_handle->link,
  1294. &pasync_ctx->async_data.free_list);
  1295. pasync_ctx->async_data.free_entries++;
  1296. }
  1297. }
  1298. static struct hd_async_handle *
  1299. beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
  1300. struct hd_async_context *pasync_ctx,
  1301. struct i_t_dpdu_cqe *pdpdu_cqe)
  1302. {
  1303. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1304. struct hd_async_handle *pasync_handle;
  1305. struct be_bus_address phys_addr;
  1306. u8 final, error = 0;
  1307. u16 cid, code, ci;
  1308. u32 dpl;
  1309. cid = beiscsi_conn->beiscsi_conn_cid;
  1310. /**
  1311. * This function is invoked to get the right async_handle structure
  1312. * from a given DEF PDU CQ entry.
  1313. *
  1314. * - index in CQ entry gives the vertical index
  1315. * - address in CQ entry is the offset where the DMA last ended
  1316. * - final - no more notifications for this PDU
  1317. */
  1318. if (is_chip_be2_be3r(phba)) {
  1319. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1320. dpl, pdpdu_cqe);
  1321. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1322. index, pdpdu_cqe);
  1323. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1324. final, pdpdu_cqe);
  1325. } else {
  1326. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1327. dpl, pdpdu_cqe);
  1328. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1329. index, pdpdu_cqe);
  1330. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1331. final, pdpdu_cqe);
  1332. }
  1333. /**
  1334. * DB addr Hi/Lo is same for BE and SKH.
  1335. * Subtract the dataplacementlength to get to the base.
  1336. */
  1337. phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1338. db_addr_lo, pdpdu_cqe);
  1339. phys_addr.u.a32.address_lo -= dpl;
  1340. phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1341. db_addr_hi, pdpdu_cqe);
  1342. code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
  1343. switch (code) {
  1344. case UNSOL_HDR_NOTIFY:
  1345. pasync_handle = pasync_ctx->async_entry[ci].header;
  1346. break;
  1347. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1348. error = 1;
  1349. case UNSOL_DATA_NOTIFY:
  1350. pasync_handle = pasync_ctx->async_entry[ci].data;
  1351. break;
  1352. /* called only for above codes */
  1353. default:
  1354. pasync_handle = NULL;
  1355. break;
  1356. }
  1357. if (!pasync_handle) {
  1358. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1359. "BM_%d : cid %d async PDU handle not found - code %d ci %d addr %llx\n",
  1360. cid, code, ci, phys_addr.u.a64.address);
  1361. return pasync_handle;
  1362. }
  1363. if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
  1364. pasync_handle->index != ci) {
  1365. /* driver bug - if ci does not match async handle index */
  1366. error = 1;
  1367. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1368. "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
  1369. cid, pasync_handle->is_header ? 'H' : 'D',
  1370. pasync_handle->pa.u.a64.address,
  1371. pasync_handle->index,
  1372. phys_addr.u.a64.address, ci);
  1373. /* FW has stale address - attempt continuing by dropping */
  1374. }
  1375. /**
  1376. * Each CID is associated with unique CRI.
  1377. * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
  1378. **/
  1379. pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
  1380. pasync_handle->is_final = final;
  1381. pasync_handle->buffer_len = dpl;
  1382. /* empty the slot */
  1383. if (pasync_handle->is_header)
  1384. pasync_ctx->async_entry[ci].header = NULL;
  1385. else
  1386. pasync_ctx->async_entry[ci].data = NULL;
  1387. /**
  1388. * DEF PDU header and data buffers with errors should be simply
  1389. * dropped as there are no consumers for it.
  1390. */
  1391. if (error) {
  1392. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1393. pasync_handle = NULL;
  1394. }
  1395. return pasync_handle;
  1396. }
  1397. static void
  1398. beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
  1399. struct hd_async_context *pasync_ctx,
  1400. u16 cri)
  1401. {
  1402. struct hd_async_handle *pasync_handle, *tmp_handle;
  1403. struct list_head *plist;
  1404. plist = &pasync_ctx->async_entry[cri].wq.list;
  1405. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
  1406. list_del(&pasync_handle->link);
  1407. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1408. }
  1409. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
  1410. pasync_ctx->async_entry[cri].wq.hdr_len = 0;
  1411. pasync_ctx->async_entry[cri].wq.bytes_received = 0;
  1412. pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
  1413. }
  1414. static unsigned int
  1415. beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
  1416. struct hd_async_context *pasync_ctx,
  1417. u16 cri)
  1418. {
  1419. struct iscsi_session *session = beiscsi_conn->conn->session;
  1420. struct hd_async_handle *pasync_handle, *plast_handle;
  1421. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1422. void *phdr = NULL, *pdata = NULL;
  1423. u32 dlen = 0, status = 0;
  1424. struct list_head *plist;
  1425. plist = &pasync_ctx->async_entry[cri].wq.list;
  1426. plast_handle = NULL;
  1427. list_for_each_entry(pasync_handle, plist, link) {
  1428. plast_handle = pasync_handle;
  1429. /* get the header, the first entry */
  1430. if (!phdr) {
  1431. phdr = pasync_handle->pbuffer;
  1432. continue;
  1433. }
  1434. /* use first buffer to collect all the data */
  1435. if (!pdata) {
  1436. pdata = pasync_handle->pbuffer;
  1437. dlen = pasync_handle->buffer_len;
  1438. continue;
  1439. }
  1440. memcpy(pdata + dlen, pasync_handle->pbuffer,
  1441. pasync_handle->buffer_len);
  1442. dlen += pasync_handle->buffer_len;
  1443. }
  1444. if (!plast_handle->is_final) {
  1445. /* last handle should have final PDU notification from FW */
  1446. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1447. "BM_%d : cid %u %p fwd async PDU with last handle missing - HL%u:DN%u:DR%u\n",
  1448. beiscsi_conn->beiscsi_conn_cid, plast_handle,
  1449. pasync_ctx->async_entry[cri].wq.hdr_len,
  1450. pasync_ctx->async_entry[cri].wq.bytes_needed,
  1451. pasync_ctx->async_entry[cri].wq.bytes_received);
  1452. }
  1453. spin_lock_bh(&session->back_lock);
  1454. status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
  1455. spin_unlock_bh(&session->back_lock);
  1456. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1457. return status;
  1458. }
  1459. static unsigned int
  1460. beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
  1461. struct hd_async_context *pasync_ctx,
  1462. struct hd_async_handle *pasync_handle)
  1463. {
  1464. unsigned int bytes_needed = 0, status = 0;
  1465. u16 cri = pasync_handle->cri;
  1466. struct cri_wait_queue *wq;
  1467. struct beiscsi_hba *phba;
  1468. struct pdu_base *ppdu;
  1469. char *err = "";
  1470. phba = beiscsi_conn->phba;
  1471. wq = &pasync_ctx->async_entry[cri].wq;
  1472. if (pasync_handle->is_header) {
  1473. /* check if PDU hdr is rcv'd when old hdr not completed */
  1474. if (wq->hdr_len) {
  1475. err = "incomplete";
  1476. goto drop_pdu;
  1477. }
  1478. ppdu = pasync_handle->pbuffer;
  1479. bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
  1480. data_len_hi, ppdu);
  1481. bytes_needed <<= 16;
  1482. bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
  1483. data_len_lo, ppdu));
  1484. wq->hdr_len = pasync_handle->buffer_len;
  1485. wq->bytes_received = 0;
  1486. wq->bytes_needed = bytes_needed;
  1487. list_add_tail(&pasync_handle->link, &wq->list);
  1488. if (!bytes_needed)
  1489. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1490. pasync_ctx, cri);
  1491. } else {
  1492. /* check if data received has header and is needed */
  1493. if (!wq->hdr_len || !wq->bytes_needed) {
  1494. err = "header less";
  1495. goto drop_pdu;
  1496. }
  1497. wq->bytes_received += pasync_handle->buffer_len;
  1498. /* Something got overwritten? Better catch it here. */
  1499. if (wq->bytes_received > wq->bytes_needed) {
  1500. err = "overflow";
  1501. goto drop_pdu;
  1502. }
  1503. list_add_tail(&pasync_handle->link, &wq->list);
  1504. if (wq->bytes_received == wq->bytes_needed)
  1505. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1506. pasync_ctx, cri);
  1507. }
  1508. return status;
  1509. drop_pdu:
  1510. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1511. "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
  1512. beiscsi_conn->beiscsi_conn_cid, err,
  1513. pasync_handle->is_header ? 'H' : 'D',
  1514. wq->hdr_len, wq->bytes_needed,
  1515. pasync_handle->buffer_len);
  1516. /* discard this handle */
  1517. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1518. /* free all the other handles in cri_wait_queue */
  1519. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1520. /* try continuing */
  1521. return status;
  1522. }
  1523. static void
  1524. beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
  1525. u8 header, u8 ulp_num)
  1526. {
  1527. struct hd_async_handle *pasync_handle, *tmp, **slot;
  1528. struct hd_async_context *pasync_ctx;
  1529. struct hwi_controller *phwi_ctrlr;
  1530. struct list_head *hfree_list;
  1531. struct phys_addr *pasync_sge;
  1532. u32 ring_id, doorbell = 0;
  1533. u16 index, num_entries;
  1534. u32 doorbell_offset;
  1535. u16 prod = 0, cons;
  1536. phwi_ctrlr = phba->phwi_ctrlr;
  1537. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1538. num_entries = pasync_ctx->num_entries;
  1539. if (header) {
  1540. cons = pasync_ctx->async_header.free_entries;
  1541. hfree_list = &pasync_ctx->async_header.free_list;
  1542. ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
  1543. doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
  1544. doorbell_offset;
  1545. } else {
  1546. cons = pasync_ctx->async_data.free_entries;
  1547. hfree_list = &pasync_ctx->async_data.free_list;
  1548. ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
  1549. doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
  1550. doorbell_offset;
  1551. }
  1552. /* number of entries posted must be in multiples of 8 */
  1553. if (cons % 8)
  1554. return;
  1555. list_for_each_entry_safe(pasync_handle, tmp, hfree_list, link) {
  1556. list_del_init(&pasync_handle->link);
  1557. pasync_handle->is_final = 0;
  1558. pasync_handle->buffer_len = 0;
  1559. /* handles can be consumed out of order, use index in handle */
  1560. index = pasync_handle->index;
  1561. WARN_ON(pasync_handle->is_header != header);
  1562. if (header)
  1563. slot = &pasync_ctx->async_entry[index].header;
  1564. else
  1565. slot = &pasync_ctx->async_entry[index].data;
  1566. /**
  1567. * The slot just tracks handle's hold and release, so
  1568. * overwriting at the same index won't do any harm but
  1569. * needs to be caught.
  1570. */
  1571. if (*slot != NULL) {
  1572. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1573. "BM_%d : async PDU %s slot at %u not empty\n",
  1574. header ? "header" : "data", index);
  1575. }
  1576. /**
  1577. * We use same freed index as in completion to post so this
  1578. * operation is not required for refills. Its required only
  1579. * for ring creation.
  1580. */
  1581. if (header)
  1582. pasync_sge = pasync_ctx->async_header.ring_base;
  1583. else
  1584. pasync_sge = pasync_ctx->async_data.ring_base;
  1585. pasync_sge += index;
  1586. /* if its a refill then address is same; hi is lo */
  1587. WARN_ON(pasync_sge->hi &&
  1588. pasync_sge->hi != pasync_handle->pa.u.a32.address_lo);
  1589. WARN_ON(pasync_sge->lo &&
  1590. pasync_sge->lo != pasync_handle->pa.u.a32.address_hi);
  1591. pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
  1592. pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
  1593. *slot = pasync_handle;
  1594. if (++prod == cons)
  1595. break;
  1596. }
  1597. if (header)
  1598. pasync_ctx->async_header.free_entries -= prod;
  1599. else
  1600. pasync_ctx->async_data.free_entries -= prod;
  1601. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1602. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1603. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1604. doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
  1605. iowrite32(doorbell, phba->db_va + doorbell_offset);
  1606. }
  1607. static void
  1608. beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
  1609. struct i_t_dpdu_cqe *pdpdu_cqe)
  1610. {
  1611. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1612. struct hd_async_handle *pasync_handle = NULL;
  1613. struct hd_async_context *pasync_ctx;
  1614. struct hwi_controller *phwi_ctrlr;
  1615. u16 cid_cri;
  1616. u8 ulp_num;
  1617. phwi_ctrlr = phba->phwi_ctrlr;
  1618. cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
  1619. ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
  1620. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1621. pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
  1622. pdpdu_cqe);
  1623. if (!pasync_handle)
  1624. return;
  1625. beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
  1626. beiscsi_hdq_post_handles(phba, pasync_handle->is_header, ulp_num);
  1627. }
  1628. void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
  1629. {
  1630. struct be_queue_info *mcc_cq;
  1631. struct be_mcc_compl *mcc_compl;
  1632. unsigned int num_processed = 0;
  1633. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1634. mcc_compl = queue_tail_node(mcc_cq);
  1635. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1636. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1637. if (beiscsi_hba_in_error(phba))
  1638. return;
  1639. if (num_processed >= 32) {
  1640. hwi_ring_cq_db(phba, mcc_cq->id,
  1641. num_processed, 0);
  1642. num_processed = 0;
  1643. }
  1644. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1645. beiscsi_process_async_event(phba, mcc_compl);
  1646. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1647. beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
  1648. }
  1649. mcc_compl->flags = 0;
  1650. queue_tail_inc(mcc_cq);
  1651. mcc_compl = queue_tail_node(mcc_cq);
  1652. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1653. num_processed++;
  1654. }
  1655. if (num_processed > 0)
  1656. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
  1657. }
  1658. static void beiscsi_mcc_work(struct work_struct *work)
  1659. {
  1660. struct be_eq_obj *pbe_eq;
  1661. struct beiscsi_hba *phba;
  1662. pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
  1663. phba = pbe_eq->phba;
  1664. beiscsi_process_mcc_cq(phba);
  1665. /* rearm EQ for further interrupts */
  1666. if (!beiscsi_hba_in_error(phba))
  1667. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1668. }
  1669. /**
  1670. * beiscsi_process_cq()- Process the Completion Queue
  1671. * @pbe_eq: Event Q on which the Completion has come
  1672. * @budget: Max number of events to processed
  1673. *
  1674. * return
  1675. * Number of Completion Entries processed.
  1676. **/
  1677. unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
  1678. {
  1679. struct be_queue_info *cq;
  1680. struct sol_cqe *sol;
  1681. struct dmsg_cqe *dmsg;
  1682. unsigned int total = 0;
  1683. unsigned int num_processed = 0;
  1684. unsigned short code = 0, cid = 0;
  1685. uint16_t cri_index = 0;
  1686. struct beiscsi_conn *beiscsi_conn;
  1687. struct beiscsi_endpoint *beiscsi_ep;
  1688. struct iscsi_endpoint *ep;
  1689. struct beiscsi_hba *phba;
  1690. cq = pbe_eq->cq;
  1691. sol = queue_tail_node(cq);
  1692. phba = pbe_eq->phba;
  1693. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1694. CQE_VALID_MASK) {
  1695. if (beiscsi_hba_in_error(phba))
  1696. return 0;
  1697. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1698. code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
  1699. 32] & CQE_CODE_MASK);
  1700. /* Get the CID */
  1701. if (is_chip_be2_be3r(phba)) {
  1702. cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
  1703. } else {
  1704. if ((code == DRIVERMSG_NOTIFY) ||
  1705. (code == UNSOL_HDR_NOTIFY) ||
  1706. (code == UNSOL_DATA_NOTIFY))
  1707. cid = AMAP_GET_BITS(
  1708. struct amap_i_t_dpdu_cqe_v2,
  1709. cid, sol);
  1710. else
  1711. cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1712. cid, sol);
  1713. }
  1714. cri_index = BE_GET_CRI_FROM_CID(cid);
  1715. ep = phba->ep_array[cri_index];
  1716. if (ep == NULL) {
  1717. /* connection has already been freed
  1718. * just move on to next one
  1719. */
  1720. beiscsi_log(phba, KERN_WARNING,
  1721. BEISCSI_LOG_INIT,
  1722. "BM_%d : proc cqe of disconn ep: cid %d\n",
  1723. cid);
  1724. goto proc_next_cqe;
  1725. }
  1726. beiscsi_ep = ep->dd_data;
  1727. beiscsi_conn = beiscsi_ep->conn;
  1728. /* replenish cq */
  1729. if (num_processed == 32) {
  1730. hwi_ring_cq_db(phba, cq->id, 32, 0);
  1731. num_processed = 0;
  1732. }
  1733. total++;
  1734. switch (code) {
  1735. case SOL_CMD_COMPLETE:
  1736. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1737. break;
  1738. case DRIVERMSG_NOTIFY:
  1739. beiscsi_log(phba, KERN_INFO,
  1740. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1741. "BM_%d : Received %s[%d] on CID : %d\n",
  1742. cqe_desc[code], code, cid);
  1743. dmsg = (struct dmsg_cqe *)sol;
  1744. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1745. break;
  1746. case UNSOL_HDR_NOTIFY:
  1747. beiscsi_log(phba, KERN_INFO,
  1748. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1749. "BM_%d : Received %s[%d] on CID : %d\n",
  1750. cqe_desc[code], code, cid);
  1751. spin_lock_bh(&phba->async_pdu_lock);
  1752. beiscsi_hdq_process_compl(beiscsi_conn,
  1753. (struct i_t_dpdu_cqe *)sol);
  1754. spin_unlock_bh(&phba->async_pdu_lock);
  1755. break;
  1756. case UNSOL_DATA_NOTIFY:
  1757. beiscsi_log(phba, KERN_INFO,
  1758. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1759. "BM_%d : Received %s[%d] on CID : %d\n",
  1760. cqe_desc[code], code, cid);
  1761. spin_lock_bh(&phba->async_pdu_lock);
  1762. beiscsi_hdq_process_compl(beiscsi_conn,
  1763. (struct i_t_dpdu_cqe *)sol);
  1764. spin_unlock_bh(&phba->async_pdu_lock);
  1765. break;
  1766. case CXN_INVALIDATE_INDEX_NOTIFY:
  1767. case CMD_INVALIDATED_NOTIFY:
  1768. case CXN_INVALIDATE_NOTIFY:
  1769. beiscsi_log(phba, KERN_ERR,
  1770. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1771. "BM_%d : Ignoring %s[%d] on CID : %d\n",
  1772. cqe_desc[code], code, cid);
  1773. break;
  1774. case CXN_KILLED_HDR_DIGEST_ERR:
  1775. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1776. beiscsi_log(phba, KERN_ERR,
  1777. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1778. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1779. cqe_desc[code], code, cid);
  1780. break;
  1781. case CMD_KILLED_INVALID_STATSN_RCVD:
  1782. case CMD_KILLED_INVALID_R2T_RCVD:
  1783. case CMD_CXN_KILLED_LUN_INVALID:
  1784. case CMD_CXN_KILLED_ICD_INVALID:
  1785. case CMD_CXN_KILLED_ITT_INVALID:
  1786. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1787. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1788. beiscsi_log(phba, KERN_ERR,
  1789. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1790. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1791. cqe_desc[code], code, cid);
  1792. break;
  1793. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1794. beiscsi_log(phba, KERN_ERR,
  1795. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1796. "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
  1797. cqe_desc[code], code, cid);
  1798. spin_lock_bh(&phba->async_pdu_lock);
  1799. /* driver consumes the entry and drops the contents */
  1800. beiscsi_hdq_process_compl(beiscsi_conn,
  1801. (struct i_t_dpdu_cqe *)sol);
  1802. spin_unlock_bh(&phba->async_pdu_lock);
  1803. break;
  1804. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1805. case CXN_KILLED_BURST_LEN_MISMATCH:
  1806. case CXN_KILLED_AHS_RCVD:
  1807. case CXN_KILLED_UNKNOWN_HDR:
  1808. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1809. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1810. case CXN_KILLED_TIMED_OUT:
  1811. case CXN_KILLED_FIN_RCVD:
  1812. case CXN_KILLED_RST_SENT:
  1813. case CXN_KILLED_RST_RCVD:
  1814. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1815. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1816. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1817. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1818. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1819. beiscsi_log(phba, KERN_ERR,
  1820. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1821. "BM_%d : Event %s[%d] received on CID : %d\n",
  1822. cqe_desc[code], code, cid);
  1823. if (beiscsi_conn)
  1824. iscsi_conn_failure(beiscsi_conn->conn,
  1825. ISCSI_ERR_CONN_FAILED);
  1826. break;
  1827. default:
  1828. beiscsi_log(phba, KERN_ERR,
  1829. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1830. "BM_%d : Invalid CQE Event Received Code : %d"
  1831. "CID 0x%x...\n",
  1832. code, cid);
  1833. break;
  1834. }
  1835. proc_next_cqe:
  1836. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1837. queue_tail_inc(cq);
  1838. sol = queue_tail_node(cq);
  1839. num_processed++;
  1840. if (total == budget)
  1841. break;
  1842. }
  1843. hwi_ring_cq_db(phba, cq->id, num_processed, 1);
  1844. return total;
  1845. }
  1846. static int be_iopoll(struct irq_poll *iop, int budget)
  1847. {
  1848. unsigned int ret, io_events;
  1849. struct beiscsi_hba *phba;
  1850. struct be_eq_obj *pbe_eq;
  1851. struct be_eq_entry *eqe = NULL;
  1852. struct be_queue_info *eq;
  1853. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1854. phba = pbe_eq->phba;
  1855. if (beiscsi_hba_in_error(phba)) {
  1856. irq_poll_complete(iop);
  1857. return 0;
  1858. }
  1859. io_events = 0;
  1860. eq = &pbe_eq->q;
  1861. eqe = queue_tail_node(eq);
  1862. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
  1863. EQE_VALID_MASK) {
  1864. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  1865. queue_tail_inc(eq);
  1866. eqe = queue_tail_node(eq);
  1867. io_events++;
  1868. }
  1869. hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
  1870. ret = beiscsi_process_cq(pbe_eq, budget);
  1871. pbe_eq->cq_count += ret;
  1872. if (ret < budget) {
  1873. irq_poll_complete(iop);
  1874. beiscsi_log(phba, KERN_INFO,
  1875. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1876. "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
  1877. pbe_eq->q.id, ret);
  1878. if (!beiscsi_hba_in_error(phba))
  1879. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1880. }
  1881. return ret;
  1882. }
  1883. static void
  1884. hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1885. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1886. {
  1887. struct iscsi_sge *psgl;
  1888. unsigned int sg_len, index;
  1889. unsigned int sge_len = 0;
  1890. unsigned long long addr;
  1891. struct scatterlist *l_sg;
  1892. unsigned int offset;
  1893. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
  1894. io_task->bhs_pa.u.a32.address_lo);
  1895. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
  1896. io_task->bhs_pa.u.a32.address_hi);
  1897. l_sg = sg;
  1898. for (index = 0; (index < num_sg) && (index < 2); index++,
  1899. sg = sg_next(sg)) {
  1900. if (index == 0) {
  1901. sg_len = sg_dma_len(sg);
  1902. addr = (u64) sg_dma_address(sg);
  1903. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1904. sge0_addr_lo, pwrb,
  1905. lower_32_bits(addr));
  1906. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1907. sge0_addr_hi, pwrb,
  1908. upper_32_bits(addr));
  1909. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1910. sge0_len, pwrb,
  1911. sg_len);
  1912. sge_len = sg_len;
  1913. } else {
  1914. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
  1915. pwrb, sge_len);
  1916. sg_len = sg_dma_len(sg);
  1917. addr = (u64) sg_dma_address(sg);
  1918. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1919. sge1_addr_lo, pwrb,
  1920. lower_32_bits(addr));
  1921. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1922. sge1_addr_hi, pwrb,
  1923. upper_32_bits(addr));
  1924. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1925. sge1_len, pwrb,
  1926. sg_len);
  1927. }
  1928. }
  1929. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1930. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1931. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1932. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1933. io_task->bhs_pa.u.a32.address_hi);
  1934. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1935. io_task->bhs_pa.u.a32.address_lo);
  1936. if (num_sg == 1) {
  1937. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1938. 1);
  1939. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1940. 0);
  1941. } else if (num_sg == 2) {
  1942. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1943. 0);
  1944. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1945. 1);
  1946. } else {
  1947. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1948. 0);
  1949. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1950. 0);
  1951. }
  1952. sg = l_sg;
  1953. psgl++;
  1954. psgl++;
  1955. offset = 0;
  1956. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1957. sg_len = sg_dma_len(sg);
  1958. addr = (u64) sg_dma_address(sg);
  1959. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1960. lower_32_bits(addr));
  1961. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1962. upper_32_bits(addr));
  1963. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1964. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1965. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1966. offset += sg_len;
  1967. }
  1968. psgl--;
  1969. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1970. }
  1971. static void
  1972. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1973. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1974. {
  1975. struct iscsi_sge *psgl;
  1976. unsigned int sg_len, index;
  1977. unsigned int sge_len = 0;
  1978. unsigned long long addr;
  1979. struct scatterlist *l_sg;
  1980. unsigned int offset;
  1981. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1982. io_task->bhs_pa.u.a32.address_lo);
  1983. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1984. io_task->bhs_pa.u.a32.address_hi);
  1985. l_sg = sg;
  1986. for (index = 0; (index < num_sg) && (index < 2); index++,
  1987. sg = sg_next(sg)) {
  1988. if (index == 0) {
  1989. sg_len = sg_dma_len(sg);
  1990. addr = (u64) sg_dma_address(sg);
  1991. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1992. ((u32)(addr & 0xFFFFFFFF)));
  1993. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1994. ((u32)(addr >> 32)));
  1995. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1996. sg_len);
  1997. sge_len = sg_len;
  1998. } else {
  1999. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  2000. pwrb, sge_len);
  2001. sg_len = sg_dma_len(sg);
  2002. addr = (u64) sg_dma_address(sg);
  2003. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  2004. ((u32)(addr & 0xFFFFFFFF)));
  2005. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  2006. ((u32)(addr >> 32)));
  2007. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  2008. sg_len);
  2009. }
  2010. }
  2011. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2012. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  2013. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  2014. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2015. io_task->bhs_pa.u.a32.address_hi);
  2016. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2017. io_task->bhs_pa.u.a32.address_lo);
  2018. if (num_sg == 1) {
  2019. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2020. 1);
  2021. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2022. 0);
  2023. } else if (num_sg == 2) {
  2024. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2025. 0);
  2026. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2027. 1);
  2028. } else {
  2029. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2030. 0);
  2031. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2032. 0);
  2033. }
  2034. sg = l_sg;
  2035. psgl++;
  2036. psgl++;
  2037. offset = 0;
  2038. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  2039. sg_len = sg_dma_len(sg);
  2040. addr = (u64) sg_dma_address(sg);
  2041. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2042. (addr & 0xFFFFFFFF));
  2043. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2044. (addr >> 32));
  2045. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  2046. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  2047. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2048. offset += sg_len;
  2049. }
  2050. psgl--;
  2051. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2052. }
  2053. /**
  2054. * hwi_write_buffer()- Populate the WRB with task info
  2055. * @pwrb: ptr to the WRB entry
  2056. * @task: iscsi task which is to be executed
  2057. **/
  2058. static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  2059. {
  2060. struct iscsi_sge *psgl;
  2061. struct beiscsi_io_task *io_task = task->dd_data;
  2062. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  2063. struct beiscsi_hba *phba = beiscsi_conn->phba;
  2064. uint8_t dsp_value = 0;
  2065. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  2066. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  2067. io_task->bhs_pa.u.a32.address_lo);
  2068. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  2069. io_task->bhs_pa.u.a32.address_hi);
  2070. if (task->data) {
  2071. /* Check for the data_count */
  2072. dsp_value = (task->data_count) ? 1 : 0;
  2073. if (is_chip_be2_be3r(phba))
  2074. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
  2075. pwrb, dsp_value);
  2076. else
  2077. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
  2078. pwrb, dsp_value);
  2079. /* Map addr only if there is data_count */
  2080. if (dsp_value) {
  2081. io_task->mtask_addr = pci_map_single(phba->pcidev,
  2082. task->data,
  2083. task->data_count,
  2084. PCI_DMA_TODEVICE);
  2085. if (pci_dma_mapping_error(phba->pcidev,
  2086. io_task->mtask_addr))
  2087. return -ENOMEM;
  2088. io_task->mtask_data_count = task->data_count;
  2089. } else
  2090. io_task->mtask_addr = 0;
  2091. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  2092. lower_32_bits(io_task->mtask_addr));
  2093. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  2094. upper_32_bits(io_task->mtask_addr));
  2095. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  2096. task->data_count);
  2097. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  2098. } else {
  2099. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  2100. io_task->mtask_addr = 0;
  2101. }
  2102. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2103. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  2104. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2105. io_task->bhs_pa.u.a32.address_hi);
  2106. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2107. io_task->bhs_pa.u.a32.address_lo);
  2108. if (task->data) {
  2109. psgl++;
  2110. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  2111. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  2112. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  2113. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  2114. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  2115. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2116. psgl++;
  2117. if (task->data) {
  2118. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2119. lower_32_bits(io_task->mtask_addr));
  2120. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2121. upper_32_bits(io_task->mtask_addr));
  2122. }
  2123. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  2124. }
  2125. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2126. return 0;
  2127. }
  2128. /**
  2129. * beiscsi_find_mem_req()- Find mem needed
  2130. * @phba: ptr to HBA struct
  2131. **/
  2132. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  2133. {
  2134. uint8_t mem_descr_index, ulp_num;
  2135. unsigned int num_cq_pages, num_async_pdu_buf_pages;
  2136. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  2137. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  2138. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2139. sizeof(struct sol_cqe));
  2140. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  2141. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  2142. BE_ISCSI_PDU_HEADER_SIZE;
  2143. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  2144. sizeof(struct hwi_context_memory);
  2145. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  2146. * (phba->params.wrbs_per_cxn)
  2147. * phba->params.cxns_per_ctrl;
  2148. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  2149. (phba->params.wrbs_per_cxn);
  2150. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  2151. phba->params.cxns_per_ctrl);
  2152. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  2153. phba->params.icds_per_ctrl;
  2154. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  2155. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  2156. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2157. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2158. num_async_pdu_buf_sgl_pages =
  2159. PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
  2160. phba, ulp_num) *
  2161. sizeof(struct phys_addr));
  2162. num_async_pdu_buf_pages =
  2163. PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
  2164. phba, ulp_num) *
  2165. phba->params.defpdu_hdr_sz);
  2166. num_async_pdu_data_pages =
  2167. PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
  2168. phba, ulp_num) *
  2169. phba->params.defpdu_data_sz);
  2170. num_async_pdu_data_sgl_pages =
  2171. PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
  2172. phba, ulp_num) *
  2173. sizeof(struct phys_addr));
  2174. mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
  2175. (ulp_num * MEM_DESCR_OFFSET));
  2176. phba->mem_req[mem_descr_index] =
  2177. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2178. BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
  2179. mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2180. (ulp_num * MEM_DESCR_OFFSET));
  2181. phba->mem_req[mem_descr_index] =
  2182. num_async_pdu_buf_pages *
  2183. PAGE_SIZE;
  2184. mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2185. (ulp_num * MEM_DESCR_OFFSET));
  2186. phba->mem_req[mem_descr_index] =
  2187. num_async_pdu_data_pages *
  2188. PAGE_SIZE;
  2189. mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2190. (ulp_num * MEM_DESCR_OFFSET));
  2191. phba->mem_req[mem_descr_index] =
  2192. num_async_pdu_buf_sgl_pages *
  2193. PAGE_SIZE;
  2194. mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2195. (ulp_num * MEM_DESCR_OFFSET));
  2196. phba->mem_req[mem_descr_index] =
  2197. num_async_pdu_data_sgl_pages *
  2198. PAGE_SIZE;
  2199. mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2200. (ulp_num * MEM_DESCR_OFFSET));
  2201. phba->mem_req[mem_descr_index] =
  2202. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2203. sizeof(struct hd_async_handle);
  2204. mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2205. (ulp_num * MEM_DESCR_OFFSET));
  2206. phba->mem_req[mem_descr_index] =
  2207. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2208. sizeof(struct hd_async_handle);
  2209. mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2210. (ulp_num * MEM_DESCR_OFFSET));
  2211. phba->mem_req[mem_descr_index] =
  2212. sizeof(struct hd_async_context) +
  2213. (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2214. sizeof(struct hd_async_entry));
  2215. }
  2216. }
  2217. }
  2218. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  2219. {
  2220. dma_addr_t bus_add;
  2221. struct hwi_controller *phwi_ctrlr;
  2222. struct be_mem_descriptor *mem_descr;
  2223. struct mem_array *mem_arr, *mem_arr_orig;
  2224. unsigned int i, j, alloc_size, curr_alloc_size;
  2225. phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  2226. if (!phba->phwi_ctrlr)
  2227. return -ENOMEM;
  2228. /* Allocate memory for wrb_context */
  2229. phwi_ctrlr = phba->phwi_ctrlr;
  2230. phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
  2231. phba->params.cxns_per_ctrl,
  2232. GFP_KERNEL);
  2233. if (!phwi_ctrlr->wrb_context) {
  2234. kfree(phba->phwi_ctrlr);
  2235. return -ENOMEM;
  2236. }
  2237. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  2238. GFP_KERNEL);
  2239. if (!phba->init_mem) {
  2240. kfree(phwi_ctrlr->wrb_context);
  2241. kfree(phba->phwi_ctrlr);
  2242. return -ENOMEM;
  2243. }
  2244. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  2245. GFP_KERNEL);
  2246. if (!mem_arr_orig) {
  2247. kfree(phba->init_mem);
  2248. kfree(phwi_ctrlr->wrb_context);
  2249. kfree(phba->phwi_ctrlr);
  2250. return -ENOMEM;
  2251. }
  2252. mem_descr = phba->init_mem;
  2253. for (i = 0; i < SE_MEM_MAX; i++) {
  2254. if (!phba->mem_req[i]) {
  2255. mem_descr->mem_array = NULL;
  2256. mem_descr++;
  2257. continue;
  2258. }
  2259. j = 0;
  2260. mem_arr = mem_arr_orig;
  2261. alloc_size = phba->mem_req[i];
  2262. memset(mem_arr, 0, sizeof(struct mem_array) *
  2263. BEISCSI_MAX_FRAGS_INIT);
  2264. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  2265. do {
  2266. mem_arr->virtual_address = pci_alloc_consistent(
  2267. phba->pcidev,
  2268. curr_alloc_size,
  2269. &bus_add);
  2270. if (!mem_arr->virtual_address) {
  2271. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  2272. goto free_mem;
  2273. if (curr_alloc_size -
  2274. rounddown_pow_of_two(curr_alloc_size))
  2275. curr_alloc_size = rounddown_pow_of_two
  2276. (curr_alloc_size);
  2277. else
  2278. curr_alloc_size = curr_alloc_size / 2;
  2279. } else {
  2280. mem_arr->bus_address.u.
  2281. a64.address = (__u64) bus_add;
  2282. mem_arr->size = curr_alloc_size;
  2283. alloc_size -= curr_alloc_size;
  2284. curr_alloc_size = min(be_max_phys_size *
  2285. 1024, alloc_size);
  2286. j++;
  2287. mem_arr++;
  2288. }
  2289. } while (alloc_size);
  2290. mem_descr->num_elements = j;
  2291. mem_descr->size_in_bytes = phba->mem_req[i];
  2292. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  2293. GFP_KERNEL);
  2294. if (!mem_descr->mem_array)
  2295. goto free_mem;
  2296. memcpy(mem_descr->mem_array, mem_arr_orig,
  2297. sizeof(struct mem_array) * j);
  2298. mem_descr++;
  2299. }
  2300. kfree(mem_arr_orig);
  2301. return 0;
  2302. free_mem:
  2303. mem_descr->num_elements = j;
  2304. while ((i) || (j)) {
  2305. for (j = mem_descr->num_elements; j > 0; j--) {
  2306. pci_free_consistent(phba->pcidev,
  2307. mem_descr->mem_array[j - 1].size,
  2308. mem_descr->mem_array[j - 1].
  2309. virtual_address,
  2310. (unsigned long)mem_descr->
  2311. mem_array[j - 1].
  2312. bus_address.u.a64.address);
  2313. }
  2314. if (i) {
  2315. i--;
  2316. kfree(mem_descr->mem_array);
  2317. mem_descr--;
  2318. }
  2319. }
  2320. kfree(mem_arr_orig);
  2321. kfree(phba->init_mem);
  2322. kfree(phba->phwi_ctrlr->wrb_context);
  2323. kfree(phba->phwi_ctrlr);
  2324. return -ENOMEM;
  2325. }
  2326. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  2327. {
  2328. beiscsi_find_mem_req(phba);
  2329. return beiscsi_alloc_mem(phba);
  2330. }
  2331. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  2332. {
  2333. struct pdu_data_out *pdata_out;
  2334. struct pdu_nop_out *pnop_out;
  2335. struct be_mem_descriptor *mem_descr;
  2336. mem_descr = phba->init_mem;
  2337. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  2338. pdata_out =
  2339. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  2340. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2341. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  2342. IIOC_SCSI_DATA);
  2343. pnop_out =
  2344. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  2345. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  2346. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2347. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  2348. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  2349. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  2350. }
  2351. static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  2352. {
  2353. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  2354. struct hwi_context_memory *phwi_ctxt;
  2355. struct wrb_handle *pwrb_handle = NULL;
  2356. struct hwi_controller *phwi_ctrlr;
  2357. struct hwi_wrb_context *pwrb_context;
  2358. struct iscsi_wrb *pwrb = NULL;
  2359. unsigned int num_cxn_wrbh = 0;
  2360. unsigned int num_cxn_wrb = 0, j, idx = 0, index;
  2361. mem_descr_wrbh = phba->init_mem;
  2362. mem_descr_wrbh += HWI_MEM_WRBH;
  2363. mem_descr_wrb = phba->init_mem;
  2364. mem_descr_wrb += HWI_MEM_WRB;
  2365. phwi_ctrlr = phba->phwi_ctrlr;
  2366. /* Allocate memory for WRBQ */
  2367. phwi_ctxt = phwi_ctrlr->phwi_ctxt;
  2368. phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
  2369. phba->params.cxns_per_ctrl,
  2370. GFP_KERNEL);
  2371. if (!phwi_ctxt->be_wrbq) {
  2372. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2373. "BM_%d : WRBQ Mem Alloc Failed\n");
  2374. return -ENOMEM;
  2375. }
  2376. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2377. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2378. pwrb_context->pwrb_handle_base =
  2379. kzalloc(sizeof(struct wrb_handle *) *
  2380. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2381. if (!pwrb_context->pwrb_handle_base) {
  2382. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2383. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2384. goto init_wrb_hndl_failed;
  2385. }
  2386. pwrb_context->pwrb_handle_basestd =
  2387. kzalloc(sizeof(struct wrb_handle *) *
  2388. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2389. if (!pwrb_context->pwrb_handle_basestd) {
  2390. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2391. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2392. goto init_wrb_hndl_failed;
  2393. }
  2394. if (!num_cxn_wrbh) {
  2395. pwrb_handle =
  2396. mem_descr_wrbh->mem_array[idx].virtual_address;
  2397. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  2398. ((sizeof(struct wrb_handle)) *
  2399. phba->params.wrbs_per_cxn));
  2400. idx++;
  2401. }
  2402. pwrb_context->alloc_index = 0;
  2403. pwrb_context->wrb_handles_available = 0;
  2404. pwrb_context->free_index = 0;
  2405. if (num_cxn_wrbh) {
  2406. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2407. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2408. pwrb_context->pwrb_handle_basestd[j] =
  2409. pwrb_handle;
  2410. pwrb_context->wrb_handles_available++;
  2411. pwrb_handle->wrb_index = j;
  2412. pwrb_handle++;
  2413. }
  2414. num_cxn_wrbh--;
  2415. }
  2416. spin_lock_init(&pwrb_context->wrb_lock);
  2417. }
  2418. idx = 0;
  2419. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2420. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2421. if (!num_cxn_wrb) {
  2422. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2423. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2424. ((sizeof(struct iscsi_wrb) *
  2425. phba->params.wrbs_per_cxn));
  2426. idx++;
  2427. }
  2428. if (num_cxn_wrb) {
  2429. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2430. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2431. pwrb_handle->pwrb = pwrb;
  2432. pwrb++;
  2433. }
  2434. num_cxn_wrb--;
  2435. }
  2436. }
  2437. return 0;
  2438. init_wrb_hndl_failed:
  2439. for (j = index; j > 0; j--) {
  2440. pwrb_context = &phwi_ctrlr->wrb_context[j];
  2441. kfree(pwrb_context->pwrb_handle_base);
  2442. kfree(pwrb_context->pwrb_handle_basestd);
  2443. }
  2444. return -ENOMEM;
  2445. }
  2446. static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2447. {
  2448. uint8_t ulp_num;
  2449. struct hwi_controller *phwi_ctrlr;
  2450. struct hba_parameters *p = &phba->params;
  2451. struct hd_async_context *pasync_ctx;
  2452. struct hd_async_handle *pasync_header_h, *pasync_data_h;
  2453. unsigned int index, idx, num_per_mem, num_async_data;
  2454. struct be_mem_descriptor *mem_descr;
  2455. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2456. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2457. /* get async_ctx for each ULP */
  2458. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2459. mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2460. (ulp_num * MEM_DESCR_OFFSET));
  2461. phwi_ctrlr = phba->phwi_ctrlr;
  2462. phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
  2463. (struct hd_async_context *)
  2464. mem_descr->mem_array[0].virtual_address;
  2465. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
  2466. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2467. pasync_ctx->async_entry =
  2468. (struct hd_async_entry *)
  2469. ((long unsigned int)pasync_ctx +
  2470. sizeof(struct hd_async_context));
  2471. pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
  2472. ulp_num);
  2473. /* setup header buffers */
  2474. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2475. mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2476. (ulp_num * MEM_DESCR_OFFSET);
  2477. if (mem_descr->mem_array[0].virtual_address) {
  2478. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2479. "BM_%d : hwi_init_async_pdu_ctx"
  2480. " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
  2481. ulp_num,
  2482. mem_descr->mem_array[0].
  2483. virtual_address);
  2484. } else
  2485. beiscsi_log(phba, KERN_WARNING,
  2486. BEISCSI_LOG_INIT,
  2487. "BM_%d : No Virtual address for ULP : %d\n",
  2488. ulp_num);
  2489. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  2490. pasync_ctx->async_header.va_base =
  2491. mem_descr->mem_array[0].virtual_address;
  2492. pasync_ctx->async_header.pa_base.u.a64.address =
  2493. mem_descr->mem_array[0].
  2494. bus_address.u.a64.address;
  2495. /* setup header buffer sgls */
  2496. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2497. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2498. (ulp_num * MEM_DESCR_OFFSET);
  2499. if (mem_descr->mem_array[0].virtual_address) {
  2500. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2501. "BM_%d : hwi_init_async_pdu_ctx"
  2502. " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
  2503. ulp_num,
  2504. mem_descr->mem_array[0].
  2505. virtual_address);
  2506. } else
  2507. beiscsi_log(phba, KERN_WARNING,
  2508. BEISCSI_LOG_INIT,
  2509. "BM_%d : No Virtual address for ULP : %d\n",
  2510. ulp_num);
  2511. pasync_ctx->async_header.ring_base =
  2512. mem_descr->mem_array[0].virtual_address;
  2513. /* setup header buffer handles */
  2514. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2515. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2516. (ulp_num * MEM_DESCR_OFFSET);
  2517. if (mem_descr->mem_array[0].virtual_address) {
  2518. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2519. "BM_%d : hwi_init_async_pdu_ctx"
  2520. " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
  2521. ulp_num,
  2522. mem_descr->mem_array[0].
  2523. virtual_address);
  2524. } else
  2525. beiscsi_log(phba, KERN_WARNING,
  2526. BEISCSI_LOG_INIT,
  2527. "BM_%d : No Virtual address for ULP : %d\n",
  2528. ulp_num);
  2529. pasync_ctx->async_header.handle_base =
  2530. mem_descr->mem_array[0].virtual_address;
  2531. INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
  2532. /* setup data buffer sgls */
  2533. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2534. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2535. (ulp_num * MEM_DESCR_OFFSET);
  2536. if (mem_descr->mem_array[0].virtual_address) {
  2537. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2538. "BM_%d : hwi_init_async_pdu_ctx"
  2539. " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
  2540. ulp_num,
  2541. mem_descr->mem_array[0].
  2542. virtual_address);
  2543. } else
  2544. beiscsi_log(phba, KERN_WARNING,
  2545. BEISCSI_LOG_INIT,
  2546. "BM_%d : No Virtual address for ULP : %d\n",
  2547. ulp_num);
  2548. pasync_ctx->async_data.ring_base =
  2549. mem_descr->mem_array[0].virtual_address;
  2550. /* setup data buffer handles */
  2551. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2552. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2553. (ulp_num * MEM_DESCR_OFFSET);
  2554. if (!mem_descr->mem_array[0].virtual_address)
  2555. beiscsi_log(phba, KERN_WARNING,
  2556. BEISCSI_LOG_INIT,
  2557. "BM_%d : No Virtual address for ULP : %d\n",
  2558. ulp_num);
  2559. pasync_ctx->async_data.handle_base =
  2560. mem_descr->mem_array[0].virtual_address;
  2561. INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
  2562. pasync_header_h =
  2563. (struct hd_async_handle *)
  2564. pasync_ctx->async_header.handle_base;
  2565. pasync_data_h =
  2566. (struct hd_async_handle *)
  2567. pasync_ctx->async_data.handle_base;
  2568. /* setup data buffers */
  2569. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2570. mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2571. (ulp_num * MEM_DESCR_OFFSET);
  2572. if (mem_descr->mem_array[0].virtual_address) {
  2573. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2574. "BM_%d : hwi_init_async_pdu_ctx"
  2575. " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
  2576. ulp_num,
  2577. mem_descr->mem_array[0].
  2578. virtual_address);
  2579. } else
  2580. beiscsi_log(phba, KERN_WARNING,
  2581. BEISCSI_LOG_INIT,
  2582. "BM_%d : No Virtual address for ULP : %d\n",
  2583. ulp_num);
  2584. idx = 0;
  2585. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  2586. pasync_ctx->async_data.va_base =
  2587. mem_descr->mem_array[idx].virtual_address;
  2588. pasync_ctx->async_data.pa_base.u.a64.address =
  2589. mem_descr->mem_array[idx].
  2590. bus_address.u.a64.address;
  2591. num_async_data = ((mem_descr->mem_array[idx].size) /
  2592. phba->params.defpdu_data_sz);
  2593. num_per_mem = 0;
  2594. for (index = 0; index < BEISCSI_GET_CID_COUNT
  2595. (phba, ulp_num); index++) {
  2596. pasync_header_h->cri = -1;
  2597. pasync_header_h->is_header = 1;
  2598. pasync_header_h->index = index;
  2599. INIT_LIST_HEAD(&pasync_header_h->link);
  2600. pasync_header_h->pbuffer =
  2601. (void *)((unsigned long)
  2602. (pasync_ctx->
  2603. async_header.va_base) +
  2604. (p->defpdu_hdr_sz * index));
  2605. pasync_header_h->pa.u.a64.address =
  2606. pasync_ctx->async_header.pa_base.u.a64.
  2607. address + (p->defpdu_hdr_sz * index);
  2608. list_add_tail(&pasync_header_h->link,
  2609. &pasync_ctx->async_header.
  2610. free_list);
  2611. pasync_header_h++;
  2612. pasync_ctx->async_header.free_entries++;
  2613. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2614. wq.list);
  2615. pasync_ctx->async_entry[index].header = NULL;
  2616. pasync_data_h->cri = -1;
  2617. pasync_data_h->is_header = 0;
  2618. pasync_data_h->index = index;
  2619. INIT_LIST_HEAD(&pasync_data_h->link);
  2620. if (!num_async_data) {
  2621. num_per_mem = 0;
  2622. idx++;
  2623. pasync_ctx->async_data.va_base =
  2624. mem_descr->mem_array[idx].
  2625. virtual_address;
  2626. pasync_ctx->async_data.pa_base.u.
  2627. a64.address =
  2628. mem_descr->mem_array[idx].
  2629. bus_address.u.a64.address;
  2630. num_async_data =
  2631. ((mem_descr->mem_array[idx].
  2632. size) /
  2633. phba->params.defpdu_data_sz);
  2634. }
  2635. pasync_data_h->pbuffer =
  2636. (void *)((unsigned long)
  2637. (pasync_ctx->async_data.va_base) +
  2638. (p->defpdu_data_sz * num_per_mem));
  2639. pasync_data_h->pa.u.a64.address =
  2640. pasync_ctx->async_data.pa_base.u.a64.
  2641. address + (p->defpdu_data_sz *
  2642. num_per_mem);
  2643. num_per_mem++;
  2644. num_async_data--;
  2645. list_add_tail(&pasync_data_h->link,
  2646. &pasync_ctx->async_data.
  2647. free_list);
  2648. pasync_data_h++;
  2649. pasync_ctx->async_data.free_entries++;
  2650. pasync_ctx->async_entry[index].data = NULL;
  2651. }
  2652. }
  2653. }
  2654. return 0;
  2655. }
  2656. static int
  2657. be_sgl_create_contiguous(void *virtual_address,
  2658. u64 physical_address, u32 length,
  2659. struct be_dma_mem *sgl)
  2660. {
  2661. WARN_ON(!virtual_address);
  2662. WARN_ON(!physical_address);
  2663. WARN_ON(!length);
  2664. WARN_ON(!sgl);
  2665. sgl->va = virtual_address;
  2666. sgl->dma = (unsigned long)physical_address;
  2667. sgl->size = length;
  2668. return 0;
  2669. }
  2670. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2671. {
  2672. memset(sgl, 0, sizeof(*sgl));
  2673. }
  2674. static void
  2675. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2676. struct mem_array *pmem, struct be_dma_mem *sgl)
  2677. {
  2678. if (sgl->va)
  2679. be_sgl_destroy_contiguous(sgl);
  2680. be_sgl_create_contiguous(pmem->virtual_address,
  2681. pmem->bus_address.u.a64.address,
  2682. pmem->size, sgl);
  2683. }
  2684. static void
  2685. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2686. struct mem_array *pmem, struct be_dma_mem *sgl)
  2687. {
  2688. if (sgl->va)
  2689. be_sgl_destroy_contiguous(sgl);
  2690. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2691. pmem->bus_address.u.a64.address,
  2692. pmem->size, sgl);
  2693. }
  2694. static int be_fill_queue(struct be_queue_info *q,
  2695. u16 len, u16 entry_size, void *vaddress)
  2696. {
  2697. struct be_dma_mem *mem = &q->dma_mem;
  2698. memset(q, 0, sizeof(*q));
  2699. q->len = len;
  2700. q->entry_size = entry_size;
  2701. mem->size = len * entry_size;
  2702. mem->va = vaddress;
  2703. if (!mem->va)
  2704. return -ENOMEM;
  2705. memset(mem->va, 0, mem->size);
  2706. return 0;
  2707. }
  2708. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2709. struct hwi_context_memory *phwi_context)
  2710. {
  2711. int ret = -ENOMEM, eq_for_mcc;
  2712. unsigned int i, num_eq_pages;
  2713. struct be_queue_info *eq;
  2714. struct be_dma_mem *mem;
  2715. void *eq_vaddress;
  2716. dma_addr_t paddr;
  2717. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2718. sizeof(struct be_eq_entry));
  2719. if (phba->msix_enabled)
  2720. eq_for_mcc = 1;
  2721. else
  2722. eq_for_mcc = 0;
  2723. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2724. eq = &phwi_context->be_eq[i].q;
  2725. mem = &eq->dma_mem;
  2726. phwi_context->be_eq[i].phba = phba;
  2727. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2728. num_eq_pages * PAGE_SIZE,
  2729. &paddr);
  2730. if (!eq_vaddress)
  2731. goto create_eq_error;
  2732. mem->va = eq_vaddress;
  2733. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2734. sizeof(struct be_eq_entry), eq_vaddress);
  2735. if (ret) {
  2736. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2737. "BM_%d : be_fill_queue Failed for EQ\n");
  2738. goto create_eq_error;
  2739. }
  2740. mem->dma = paddr;
  2741. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2742. phwi_context->cur_eqd);
  2743. if (ret) {
  2744. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2745. "BM_%d : beiscsi_cmd_eq_create"
  2746. "Failed for EQ\n");
  2747. goto create_eq_error;
  2748. }
  2749. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2750. "BM_%d : eqid = %d\n",
  2751. phwi_context->be_eq[i].q.id);
  2752. }
  2753. return 0;
  2754. create_eq_error:
  2755. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2756. eq = &phwi_context->be_eq[i].q;
  2757. mem = &eq->dma_mem;
  2758. if (mem->va)
  2759. pci_free_consistent(phba->pcidev, num_eq_pages
  2760. * PAGE_SIZE,
  2761. mem->va, mem->dma);
  2762. }
  2763. return ret;
  2764. }
  2765. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2766. struct hwi_context_memory *phwi_context)
  2767. {
  2768. unsigned int i, num_cq_pages;
  2769. struct be_queue_info *cq, *eq;
  2770. struct be_dma_mem *mem;
  2771. struct be_eq_obj *pbe_eq;
  2772. void *cq_vaddress;
  2773. int ret = -ENOMEM;
  2774. dma_addr_t paddr;
  2775. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2776. sizeof(struct sol_cqe));
  2777. for (i = 0; i < phba->num_cpus; i++) {
  2778. cq = &phwi_context->be_cq[i];
  2779. eq = &phwi_context->be_eq[i].q;
  2780. pbe_eq = &phwi_context->be_eq[i];
  2781. pbe_eq->cq = cq;
  2782. pbe_eq->phba = phba;
  2783. mem = &cq->dma_mem;
  2784. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2785. num_cq_pages * PAGE_SIZE,
  2786. &paddr);
  2787. if (!cq_vaddress)
  2788. goto create_cq_error;
  2789. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2790. sizeof(struct sol_cqe), cq_vaddress);
  2791. if (ret) {
  2792. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2793. "BM_%d : be_fill_queue Failed "
  2794. "for ISCSI CQ\n");
  2795. goto create_cq_error;
  2796. }
  2797. mem->dma = paddr;
  2798. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2799. false, 0);
  2800. if (ret) {
  2801. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2802. "BM_%d : beiscsi_cmd_eq_create"
  2803. "Failed for ISCSI CQ\n");
  2804. goto create_cq_error;
  2805. }
  2806. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2807. "BM_%d : iscsi cq_id is %d for eq_id %d\n"
  2808. "iSCSI CQ CREATED\n", cq->id, eq->id);
  2809. }
  2810. return 0;
  2811. create_cq_error:
  2812. for (i = 0; i < phba->num_cpus; i++) {
  2813. cq = &phwi_context->be_cq[i];
  2814. mem = &cq->dma_mem;
  2815. if (mem->va)
  2816. pci_free_consistent(phba->pcidev, num_cq_pages
  2817. * PAGE_SIZE,
  2818. mem->va, mem->dma);
  2819. }
  2820. return ret;
  2821. }
  2822. static int
  2823. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2824. struct hwi_context_memory *phwi_context,
  2825. struct hwi_controller *phwi_ctrlr,
  2826. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2827. {
  2828. unsigned int idx;
  2829. int ret;
  2830. struct be_queue_info *dq, *cq;
  2831. struct be_dma_mem *mem;
  2832. struct be_mem_descriptor *mem_descr;
  2833. void *dq_vaddress;
  2834. idx = 0;
  2835. dq = &phwi_context->be_def_hdrq[ulp_num];
  2836. cq = &phwi_context->be_cq[0];
  2837. mem = &dq->dma_mem;
  2838. mem_descr = phba->init_mem;
  2839. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2840. (ulp_num * MEM_DESCR_OFFSET);
  2841. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2842. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2843. sizeof(struct phys_addr),
  2844. sizeof(struct phys_addr), dq_vaddress);
  2845. if (ret) {
  2846. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2847. "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
  2848. ulp_num);
  2849. return ret;
  2850. }
  2851. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2852. bus_address.u.a64.address;
  2853. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2854. def_pdu_ring_sz,
  2855. phba->params.defpdu_hdr_sz,
  2856. BEISCSI_DEFQ_HDR, ulp_num);
  2857. if (ret) {
  2858. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2859. "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
  2860. ulp_num);
  2861. return ret;
  2862. }
  2863. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2864. "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
  2865. ulp_num,
  2866. phwi_context->be_def_hdrq[ulp_num].id);
  2867. return 0;
  2868. }
  2869. static int
  2870. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2871. struct hwi_context_memory *phwi_context,
  2872. struct hwi_controller *phwi_ctrlr,
  2873. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2874. {
  2875. unsigned int idx;
  2876. int ret;
  2877. struct be_queue_info *dataq, *cq;
  2878. struct be_dma_mem *mem;
  2879. struct be_mem_descriptor *mem_descr;
  2880. void *dq_vaddress;
  2881. idx = 0;
  2882. dataq = &phwi_context->be_def_dataq[ulp_num];
  2883. cq = &phwi_context->be_cq[0];
  2884. mem = &dataq->dma_mem;
  2885. mem_descr = phba->init_mem;
  2886. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2887. (ulp_num * MEM_DESCR_OFFSET);
  2888. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2889. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2890. sizeof(struct phys_addr),
  2891. sizeof(struct phys_addr), dq_vaddress);
  2892. if (ret) {
  2893. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2894. "BM_%d : be_fill_queue Failed for DEF PDU "
  2895. "DATA on ULP : %d\n",
  2896. ulp_num);
  2897. return ret;
  2898. }
  2899. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2900. bus_address.u.a64.address;
  2901. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2902. def_pdu_ring_sz,
  2903. phba->params.defpdu_data_sz,
  2904. BEISCSI_DEFQ_DATA, ulp_num);
  2905. if (ret) {
  2906. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2907. "BM_%d be_cmd_create_default_pdu_queue"
  2908. " Failed for DEF PDU DATA on ULP : %d\n",
  2909. ulp_num);
  2910. return ret;
  2911. }
  2912. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2913. "BM_%d : iscsi def data id on ULP : %d is %d\n",
  2914. ulp_num,
  2915. phwi_context->be_def_dataq[ulp_num].id);
  2916. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2917. "BM_%d : DEFAULT PDU DATA RING CREATED"
  2918. "on ULP : %d\n", ulp_num);
  2919. return 0;
  2920. }
  2921. static int
  2922. beiscsi_post_template_hdr(struct beiscsi_hba *phba)
  2923. {
  2924. struct be_mem_descriptor *mem_descr;
  2925. struct mem_array *pm_arr;
  2926. struct be_dma_mem sgl;
  2927. int status, ulp_num;
  2928. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2929. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2930. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2931. mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
  2932. (ulp_num * MEM_DESCR_OFFSET);
  2933. pm_arr = mem_descr->mem_array;
  2934. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2935. status = be_cmd_iscsi_post_template_hdr(
  2936. &phba->ctrl, &sgl);
  2937. if (status != 0) {
  2938. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2939. "BM_%d : Post Template HDR Failed for"
  2940. "ULP_%d\n", ulp_num);
  2941. return status;
  2942. }
  2943. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2944. "BM_%d : Template HDR Pages Posted for"
  2945. "ULP_%d\n", ulp_num);
  2946. }
  2947. }
  2948. return 0;
  2949. }
  2950. static int
  2951. beiscsi_post_pages(struct beiscsi_hba *phba)
  2952. {
  2953. struct be_mem_descriptor *mem_descr;
  2954. struct mem_array *pm_arr;
  2955. unsigned int page_offset, i;
  2956. struct be_dma_mem sgl;
  2957. int status, ulp_num = 0;
  2958. mem_descr = phba->init_mem;
  2959. mem_descr += HWI_MEM_SGE;
  2960. pm_arr = mem_descr->mem_array;
  2961. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  2962. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  2963. break;
  2964. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2965. phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
  2966. for (i = 0; i < mem_descr->num_elements; i++) {
  2967. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2968. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2969. page_offset,
  2970. (pm_arr->size / PAGE_SIZE));
  2971. page_offset += pm_arr->size / PAGE_SIZE;
  2972. if (status != 0) {
  2973. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2974. "BM_%d : post sgl failed.\n");
  2975. return status;
  2976. }
  2977. pm_arr++;
  2978. }
  2979. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2980. "BM_%d : POSTED PAGES\n");
  2981. return 0;
  2982. }
  2983. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2984. {
  2985. struct be_dma_mem *mem = &q->dma_mem;
  2986. if (mem->va) {
  2987. pci_free_consistent(phba->pcidev, mem->size,
  2988. mem->va, mem->dma);
  2989. mem->va = NULL;
  2990. }
  2991. }
  2992. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2993. u16 len, u16 entry_size)
  2994. {
  2995. struct be_dma_mem *mem = &q->dma_mem;
  2996. memset(q, 0, sizeof(*q));
  2997. q->len = len;
  2998. q->entry_size = entry_size;
  2999. mem->size = len * entry_size;
  3000. mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
  3001. if (!mem->va)
  3002. return -ENOMEM;
  3003. return 0;
  3004. }
  3005. static int
  3006. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  3007. struct hwi_context_memory *phwi_context,
  3008. struct hwi_controller *phwi_ctrlr)
  3009. {
  3010. unsigned int wrb_mem_index, offset, size, num_wrb_rings;
  3011. u64 pa_addr_lo;
  3012. unsigned int idx, num, i, ulp_num;
  3013. struct mem_array *pwrb_arr;
  3014. void *wrb_vaddr;
  3015. struct be_dma_mem sgl;
  3016. struct be_mem_descriptor *mem_descr;
  3017. struct hwi_wrb_context *pwrb_context;
  3018. int status;
  3019. uint8_t ulp_count = 0, ulp_base_num = 0;
  3020. uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
  3021. idx = 0;
  3022. mem_descr = phba->init_mem;
  3023. mem_descr += HWI_MEM_WRB;
  3024. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  3025. GFP_KERNEL);
  3026. if (!pwrb_arr) {
  3027. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3028. "BM_%d : Memory alloc failed in create wrb ring.\n");
  3029. return -ENOMEM;
  3030. }
  3031. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3032. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  3033. num_wrb_rings = mem_descr->mem_array[idx].size /
  3034. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  3035. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  3036. if (num_wrb_rings) {
  3037. pwrb_arr[num].virtual_address = wrb_vaddr;
  3038. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  3039. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3040. sizeof(struct iscsi_wrb);
  3041. wrb_vaddr += pwrb_arr[num].size;
  3042. pa_addr_lo += pwrb_arr[num].size;
  3043. num_wrb_rings--;
  3044. } else {
  3045. idx++;
  3046. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3047. pa_addr_lo = mem_descr->mem_array[idx].\
  3048. bus_address.u.a64.address;
  3049. num_wrb_rings = mem_descr->mem_array[idx].size /
  3050. (phba->params.wrbs_per_cxn *
  3051. sizeof(struct iscsi_wrb));
  3052. pwrb_arr[num].virtual_address = wrb_vaddr;
  3053. pwrb_arr[num].bus_address.u.a64.address\
  3054. = pa_addr_lo;
  3055. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3056. sizeof(struct iscsi_wrb);
  3057. wrb_vaddr += pwrb_arr[num].size;
  3058. pa_addr_lo += pwrb_arr[num].size;
  3059. num_wrb_rings--;
  3060. }
  3061. }
  3062. /* Get the ULP Count */
  3063. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3064. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3065. ulp_count++;
  3066. ulp_base_num = ulp_num;
  3067. cid_count_ulp[ulp_num] =
  3068. BEISCSI_GET_CID_COUNT(phba, ulp_num);
  3069. }
  3070. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3071. wrb_mem_index = 0;
  3072. offset = 0;
  3073. size = 0;
  3074. if (ulp_count > 1) {
  3075. ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
  3076. if (!cid_count_ulp[ulp_base_num])
  3077. ulp_base_num = (ulp_base_num + 1) %
  3078. BEISCSI_ULP_COUNT;
  3079. cid_count_ulp[ulp_base_num]--;
  3080. }
  3081. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  3082. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  3083. &phwi_context->be_wrbq[i],
  3084. &phwi_ctrlr->wrb_context[i],
  3085. ulp_base_num);
  3086. if (status != 0) {
  3087. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3088. "BM_%d : wrbq create failed.");
  3089. kfree(pwrb_arr);
  3090. return status;
  3091. }
  3092. pwrb_context = &phwi_ctrlr->wrb_context[i];
  3093. BE_SET_CID_TO_CRI(i, pwrb_context->cid);
  3094. }
  3095. kfree(pwrb_arr);
  3096. return 0;
  3097. }
  3098. static void free_wrb_handles(struct beiscsi_hba *phba)
  3099. {
  3100. unsigned int index;
  3101. struct hwi_controller *phwi_ctrlr;
  3102. struct hwi_wrb_context *pwrb_context;
  3103. phwi_ctrlr = phba->phwi_ctrlr;
  3104. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  3105. pwrb_context = &phwi_ctrlr->wrb_context[index];
  3106. kfree(pwrb_context->pwrb_handle_base);
  3107. kfree(pwrb_context->pwrb_handle_basestd);
  3108. }
  3109. }
  3110. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  3111. {
  3112. struct be_ctrl_info *ctrl = &phba->ctrl;
  3113. struct be_dma_mem *ptag_mem;
  3114. struct be_queue_info *q;
  3115. int i, tag;
  3116. q = &phba->ctrl.mcc_obj.q;
  3117. for (i = 0; i < MAX_MCC_CMD; i++) {
  3118. tag = i + 1;
  3119. if (!test_bit(MCC_TAG_STATE_RUNNING,
  3120. &ctrl->ptag_state[tag].tag_state))
  3121. continue;
  3122. if (test_bit(MCC_TAG_STATE_TIMEOUT,
  3123. &ctrl->ptag_state[tag].tag_state)) {
  3124. ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
  3125. if (ptag_mem->size) {
  3126. pci_free_consistent(ctrl->pdev,
  3127. ptag_mem->size,
  3128. ptag_mem->va,
  3129. ptag_mem->dma);
  3130. ptag_mem->size = 0;
  3131. }
  3132. continue;
  3133. }
  3134. /**
  3135. * If MCC is still active and waiting then wake up the process.
  3136. * We are here only because port is going offline. The process
  3137. * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
  3138. * returned for the operation and allocated memory cleaned up.
  3139. */
  3140. if (waitqueue_active(&ctrl->mcc_wait[tag])) {
  3141. ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
  3142. ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
  3143. wake_up_interruptible(&ctrl->mcc_wait[tag]);
  3144. /*
  3145. * Control tag info gets reinitialized in enable
  3146. * so wait for the process to clear running state.
  3147. */
  3148. while (test_bit(MCC_TAG_STATE_RUNNING,
  3149. &ctrl->ptag_state[tag].tag_state))
  3150. schedule_timeout_uninterruptible(HZ);
  3151. }
  3152. /**
  3153. * For MCC with tag_states MCC_TAG_STATE_ASYNC and
  3154. * MCC_TAG_STATE_IGNORE nothing needs to done.
  3155. */
  3156. }
  3157. if (q->created) {
  3158. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  3159. be_queue_free(phba, q);
  3160. }
  3161. q = &phba->ctrl.mcc_obj.cq;
  3162. if (q->created) {
  3163. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3164. be_queue_free(phba, q);
  3165. }
  3166. }
  3167. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  3168. struct hwi_context_memory *phwi_context)
  3169. {
  3170. struct be_queue_info *q, *cq;
  3171. struct be_ctrl_info *ctrl = &phba->ctrl;
  3172. /* Alloc MCC compl queue */
  3173. cq = &phba->ctrl.mcc_obj.cq;
  3174. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  3175. sizeof(struct be_mcc_compl)))
  3176. goto err;
  3177. /* Ask BE to create MCC compl queue; */
  3178. if (phba->msix_enabled) {
  3179. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
  3180. [phba->num_cpus].q, false, true, 0))
  3181. goto mcc_cq_free;
  3182. } else {
  3183. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  3184. false, true, 0))
  3185. goto mcc_cq_free;
  3186. }
  3187. /* Alloc MCC queue */
  3188. q = &phba->ctrl.mcc_obj.q;
  3189. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  3190. goto mcc_cq_destroy;
  3191. /* Ask BE to create MCC queue */
  3192. if (beiscsi_cmd_mccq_create(phba, q, cq))
  3193. goto mcc_q_free;
  3194. return 0;
  3195. mcc_q_free:
  3196. be_queue_free(phba, q);
  3197. mcc_cq_destroy:
  3198. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  3199. mcc_cq_free:
  3200. be_queue_free(phba, cq);
  3201. err:
  3202. return -ENOMEM;
  3203. }
  3204. /**
  3205. * find_num_cpus()- Get the CPU online count
  3206. * @phba: ptr to priv structure
  3207. *
  3208. * CPU count is used for creating EQ.
  3209. **/
  3210. static void find_num_cpus(struct beiscsi_hba *phba)
  3211. {
  3212. int num_cpus = 0;
  3213. num_cpus = num_online_cpus();
  3214. switch (phba->generation) {
  3215. case BE_GEN2:
  3216. case BE_GEN3:
  3217. phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
  3218. BEISCSI_MAX_NUM_CPUS : num_cpus;
  3219. break;
  3220. case BE_GEN4:
  3221. /*
  3222. * If eqid_count == 1 fall back to
  3223. * INTX mechanism
  3224. **/
  3225. if (phba->fw_config.eqid_count == 1) {
  3226. enable_msix = 0;
  3227. phba->num_cpus = 1;
  3228. return;
  3229. }
  3230. phba->num_cpus =
  3231. (num_cpus > (phba->fw_config.eqid_count - 1)) ?
  3232. (phba->fw_config.eqid_count - 1) : num_cpus;
  3233. break;
  3234. default:
  3235. phba->num_cpus = 1;
  3236. }
  3237. }
  3238. static void hwi_purge_eq(struct beiscsi_hba *phba)
  3239. {
  3240. struct hwi_controller *phwi_ctrlr;
  3241. struct hwi_context_memory *phwi_context;
  3242. struct be_queue_info *eq;
  3243. struct be_eq_entry *eqe = NULL;
  3244. int i, eq_msix;
  3245. unsigned int num_processed;
  3246. if (beiscsi_hba_in_error(phba))
  3247. return;
  3248. phwi_ctrlr = phba->phwi_ctrlr;
  3249. phwi_context = phwi_ctrlr->phwi_ctxt;
  3250. if (phba->msix_enabled)
  3251. eq_msix = 1;
  3252. else
  3253. eq_msix = 0;
  3254. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  3255. eq = &phwi_context->be_eq[i].q;
  3256. eqe = queue_tail_node(eq);
  3257. num_processed = 0;
  3258. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  3259. & EQE_VALID_MASK) {
  3260. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  3261. queue_tail_inc(eq);
  3262. eqe = queue_tail_node(eq);
  3263. num_processed++;
  3264. }
  3265. if (num_processed)
  3266. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  3267. }
  3268. }
  3269. static void hwi_cleanup_port(struct beiscsi_hba *phba)
  3270. {
  3271. struct be_queue_info *q;
  3272. struct be_ctrl_info *ctrl = &phba->ctrl;
  3273. struct hwi_controller *phwi_ctrlr;
  3274. struct hwi_context_memory *phwi_context;
  3275. struct hd_async_context *pasync_ctx;
  3276. int i, eq_for_mcc, ulp_num;
  3277. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3278. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3279. beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
  3280. /**
  3281. * Purge all EQ entries that may have been left out. This is to
  3282. * workaround a problem we've seen occasionally where driver gets an
  3283. * interrupt with EQ entry bit set after stopping the controller.
  3284. */
  3285. hwi_purge_eq(phba);
  3286. phwi_ctrlr = phba->phwi_ctrlr;
  3287. phwi_context = phwi_ctrlr->phwi_ctxt;
  3288. be_cmd_iscsi_remove_template_hdr(ctrl);
  3289. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3290. q = &phwi_context->be_wrbq[i];
  3291. if (q->created)
  3292. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  3293. }
  3294. kfree(phwi_context->be_wrbq);
  3295. free_wrb_handles(phba);
  3296. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3297. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3298. q = &phwi_context->be_def_hdrq[ulp_num];
  3299. if (q->created)
  3300. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3301. q = &phwi_context->be_def_dataq[ulp_num];
  3302. if (q->created)
  3303. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3304. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
  3305. }
  3306. }
  3307. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  3308. for (i = 0; i < (phba->num_cpus); i++) {
  3309. q = &phwi_context->be_cq[i];
  3310. if (q->created) {
  3311. be_queue_free(phba, q);
  3312. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3313. }
  3314. }
  3315. be_mcc_queues_destroy(phba);
  3316. if (phba->msix_enabled)
  3317. eq_for_mcc = 1;
  3318. else
  3319. eq_for_mcc = 0;
  3320. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  3321. q = &phwi_context->be_eq[i].q;
  3322. if (q->created) {
  3323. be_queue_free(phba, q);
  3324. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  3325. }
  3326. }
  3327. /* this ensures complete FW cleanup */
  3328. beiscsi_cmd_function_reset(phba);
  3329. /* last communication, indicate driver is unloading */
  3330. beiscsi_cmd_special_wrb(&phba->ctrl, 0);
  3331. }
  3332. static int hwi_init_port(struct beiscsi_hba *phba)
  3333. {
  3334. struct hwi_controller *phwi_ctrlr;
  3335. struct hwi_context_memory *phwi_context;
  3336. unsigned int def_pdu_ring_sz;
  3337. struct be_ctrl_info *ctrl = &phba->ctrl;
  3338. int status, ulp_num;
  3339. phwi_ctrlr = phba->phwi_ctrlr;
  3340. phwi_context = phwi_ctrlr->phwi_ctxt;
  3341. phwi_context->max_eqd = 128;
  3342. phwi_context->min_eqd = 0;
  3343. phwi_context->cur_eqd = 32;
  3344. /* set port optic state to unknown */
  3345. phba->optic_state = 0xff;
  3346. status = beiscsi_create_eqs(phba, phwi_context);
  3347. if (status != 0) {
  3348. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3349. "BM_%d : EQ not created\n");
  3350. goto error;
  3351. }
  3352. status = be_mcc_queues_create(phba, phwi_context);
  3353. if (status != 0)
  3354. goto error;
  3355. status = beiscsi_check_supported_fw(ctrl, phba);
  3356. if (status != 0) {
  3357. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3358. "BM_%d : Unsupported fw version\n");
  3359. goto error;
  3360. }
  3361. status = beiscsi_create_cqs(phba, phwi_context);
  3362. if (status != 0) {
  3363. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3364. "BM_%d : CQ not created\n");
  3365. goto error;
  3366. }
  3367. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3368. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3369. def_pdu_ring_sz =
  3370. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  3371. sizeof(struct phys_addr);
  3372. status = beiscsi_create_def_hdr(phba, phwi_context,
  3373. phwi_ctrlr,
  3374. def_pdu_ring_sz,
  3375. ulp_num);
  3376. if (status != 0) {
  3377. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3378. "BM_%d : Default Header not created for ULP : %d\n",
  3379. ulp_num);
  3380. goto error;
  3381. }
  3382. status = beiscsi_create_def_data(phba, phwi_context,
  3383. phwi_ctrlr,
  3384. def_pdu_ring_sz,
  3385. ulp_num);
  3386. if (status != 0) {
  3387. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3388. "BM_%d : Default Data not created for ULP : %d\n",
  3389. ulp_num);
  3390. goto error;
  3391. }
  3392. /**
  3393. * Now that the default PDU rings have been created,
  3394. * let EP know about it.
  3395. * Call beiscsi_cmd_iscsi_cleanup before posting?
  3396. */
  3397. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
  3398. ulp_num);
  3399. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
  3400. ulp_num);
  3401. }
  3402. }
  3403. status = beiscsi_post_pages(phba);
  3404. if (status != 0) {
  3405. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3406. "BM_%d : Post SGL Pages Failed\n");
  3407. goto error;
  3408. }
  3409. status = beiscsi_post_template_hdr(phba);
  3410. if (status != 0) {
  3411. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3412. "BM_%d : Template HDR Posting for CXN Failed\n");
  3413. }
  3414. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  3415. if (status != 0) {
  3416. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3417. "BM_%d : WRB Rings not created\n");
  3418. goto error;
  3419. }
  3420. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3421. uint16_t async_arr_idx = 0;
  3422. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3423. uint16_t cri = 0;
  3424. struct hd_async_context *pasync_ctx;
  3425. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
  3426. phwi_ctrlr, ulp_num);
  3427. for (cri = 0; cri <
  3428. phba->params.cxns_per_ctrl; cri++) {
  3429. if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
  3430. (phwi_ctrlr, cri))
  3431. pasync_ctx->cid_to_async_cri_map[
  3432. phwi_ctrlr->wrb_context[cri].cid] =
  3433. async_arr_idx++;
  3434. }
  3435. /**
  3436. * Now that the default PDU rings have been created,
  3437. * let EP know about it.
  3438. */
  3439. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
  3440. ulp_num);
  3441. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
  3442. ulp_num);
  3443. }
  3444. }
  3445. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3446. "BM_%d : hwi_init_port success\n");
  3447. return 0;
  3448. error:
  3449. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3450. "BM_%d : hwi_init_port failed");
  3451. hwi_cleanup_port(phba);
  3452. return status;
  3453. }
  3454. static int hwi_init_controller(struct beiscsi_hba *phba)
  3455. {
  3456. struct hwi_controller *phwi_ctrlr;
  3457. phwi_ctrlr = phba->phwi_ctrlr;
  3458. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  3459. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  3460. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  3461. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3462. "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
  3463. phwi_ctrlr->phwi_ctxt);
  3464. } else {
  3465. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3466. "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
  3467. "than one element.Failing to load\n");
  3468. return -ENOMEM;
  3469. }
  3470. iscsi_init_global_templates(phba);
  3471. if (beiscsi_init_wrb_handle(phba))
  3472. return -ENOMEM;
  3473. if (hwi_init_async_pdu_ctx(phba)) {
  3474. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3475. "BM_%d : hwi_init_async_pdu_ctx failed\n");
  3476. return -ENOMEM;
  3477. }
  3478. if (hwi_init_port(phba) != 0) {
  3479. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3480. "BM_%d : hwi_init_controller failed\n");
  3481. return -ENOMEM;
  3482. }
  3483. return 0;
  3484. }
  3485. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  3486. {
  3487. struct be_mem_descriptor *mem_descr;
  3488. int i, j;
  3489. mem_descr = phba->init_mem;
  3490. i = 0;
  3491. j = 0;
  3492. for (i = 0; i < SE_MEM_MAX; i++) {
  3493. for (j = mem_descr->num_elements; j > 0; j--) {
  3494. pci_free_consistent(phba->pcidev,
  3495. mem_descr->mem_array[j - 1].size,
  3496. mem_descr->mem_array[j - 1].virtual_address,
  3497. (unsigned long)mem_descr->mem_array[j - 1].
  3498. bus_address.u.a64.address);
  3499. }
  3500. kfree(mem_descr->mem_array);
  3501. mem_descr++;
  3502. }
  3503. kfree(phba->init_mem);
  3504. kfree(phba->phwi_ctrlr->wrb_context);
  3505. kfree(phba->phwi_ctrlr);
  3506. }
  3507. static int beiscsi_init_controller(struct beiscsi_hba *phba)
  3508. {
  3509. int ret = -ENOMEM;
  3510. ret = beiscsi_get_memory(phba);
  3511. if (ret < 0) {
  3512. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3513. "BM_%d : beiscsi_dev_probe -"
  3514. "Failed in beiscsi_alloc_memory\n");
  3515. return ret;
  3516. }
  3517. ret = hwi_init_controller(phba);
  3518. if (ret)
  3519. goto free_init;
  3520. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3521. "BM_%d : Return success from beiscsi_init_controller");
  3522. return 0;
  3523. free_init:
  3524. beiscsi_free_mem(phba);
  3525. return ret;
  3526. }
  3527. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  3528. {
  3529. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  3530. struct sgl_handle *psgl_handle;
  3531. struct iscsi_sge *pfrag;
  3532. unsigned int arr_index, i, idx;
  3533. unsigned int ulp_icd_start, ulp_num = 0;
  3534. phba->io_sgl_hndl_avbl = 0;
  3535. phba->eh_sgl_hndl_avbl = 0;
  3536. mem_descr_sglh = phba->init_mem;
  3537. mem_descr_sglh += HWI_MEM_SGLH;
  3538. if (1 == mem_descr_sglh->num_elements) {
  3539. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3540. phba->params.ios_per_ctrl,
  3541. GFP_KERNEL);
  3542. if (!phba->io_sgl_hndl_base) {
  3543. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3544. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3545. return -ENOMEM;
  3546. }
  3547. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3548. (phba->params.icds_per_ctrl -
  3549. phba->params.ios_per_ctrl),
  3550. GFP_KERNEL);
  3551. if (!phba->eh_sgl_hndl_base) {
  3552. kfree(phba->io_sgl_hndl_base);
  3553. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3554. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3555. return -ENOMEM;
  3556. }
  3557. } else {
  3558. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3559. "BM_%d : HWI_MEM_SGLH is more than one element."
  3560. "Failing to load\n");
  3561. return -ENOMEM;
  3562. }
  3563. arr_index = 0;
  3564. idx = 0;
  3565. while (idx < mem_descr_sglh->num_elements) {
  3566. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  3567. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  3568. sizeof(struct sgl_handle)); i++) {
  3569. if (arr_index < phba->params.ios_per_ctrl) {
  3570. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  3571. phba->io_sgl_hndl_avbl++;
  3572. arr_index++;
  3573. } else {
  3574. phba->eh_sgl_hndl_base[arr_index -
  3575. phba->params.ios_per_ctrl] =
  3576. psgl_handle;
  3577. arr_index++;
  3578. phba->eh_sgl_hndl_avbl++;
  3579. }
  3580. psgl_handle++;
  3581. }
  3582. idx++;
  3583. }
  3584. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3585. "BM_%d : phba->io_sgl_hndl_avbl=%d"
  3586. "phba->eh_sgl_hndl_avbl=%d\n",
  3587. phba->io_sgl_hndl_avbl,
  3588. phba->eh_sgl_hndl_avbl);
  3589. mem_descr_sg = phba->init_mem;
  3590. mem_descr_sg += HWI_MEM_SGE;
  3591. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3592. "\n BM_%d : mem_descr_sg->num_elements=%d\n",
  3593. mem_descr_sg->num_elements);
  3594. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3595. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3596. break;
  3597. ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  3598. arr_index = 0;
  3599. idx = 0;
  3600. while (idx < mem_descr_sg->num_elements) {
  3601. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  3602. for (i = 0;
  3603. i < (mem_descr_sg->mem_array[idx].size) /
  3604. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  3605. i++) {
  3606. if (arr_index < phba->params.ios_per_ctrl)
  3607. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  3608. else
  3609. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  3610. phba->params.ios_per_ctrl];
  3611. psgl_handle->pfrag = pfrag;
  3612. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  3613. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  3614. pfrag += phba->params.num_sge_per_io;
  3615. psgl_handle->sgl_index = ulp_icd_start + arr_index++;
  3616. }
  3617. idx++;
  3618. }
  3619. phba->io_sgl_free_index = 0;
  3620. phba->io_sgl_alloc_index = 0;
  3621. phba->eh_sgl_free_index = 0;
  3622. phba->eh_sgl_alloc_index = 0;
  3623. return 0;
  3624. }
  3625. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  3626. {
  3627. int ret;
  3628. uint16_t i, ulp_num;
  3629. struct ulp_cid_info *ptr_cid_info = NULL;
  3630. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3631. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3632. ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
  3633. GFP_KERNEL);
  3634. if (!ptr_cid_info) {
  3635. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3636. "BM_%d : Failed to allocate memory"
  3637. "for ULP_CID_INFO for ULP : %d\n",
  3638. ulp_num);
  3639. ret = -ENOMEM;
  3640. goto free_memory;
  3641. }
  3642. /* Allocate memory for CID array */
  3643. ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
  3644. BEISCSI_GET_CID_COUNT(phba,
  3645. ulp_num), GFP_KERNEL);
  3646. if (!ptr_cid_info->cid_array) {
  3647. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3648. "BM_%d : Failed to allocate memory"
  3649. "for CID_ARRAY for ULP : %d\n",
  3650. ulp_num);
  3651. kfree(ptr_cid_info);
  3652. ptr_cid_info = NULL;
  3653. ret = -ENOMEM;
  3654. goto free_memory;
  3655. }
  3656. ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
  3657. phba, ulp_num);
  3658. /* Save the cid_info_array ptr */
  3659. phba->cid_array_info[ulp_num] = ptr_cid_info;
  3660. }
  3661. }
  3662. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  3663. phba->params.cxns_per_ctrl, GFP_KERNEL);
  3664. if (!phba->ep_array) {
  3665. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3666. "BM_%d : Failed to allocate memory in "
  3667. "hba_setup_cid_tbls\n");
  3668. ret = -ENOMEM;
  3669. goto free_memory;
  3670. }
  3671. phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
  3672. phba->params.cxns_per_ctrl, GFP_KERNEL);
  3673. if (!phba->conn_table) {
  3674. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3675. "BM_%d : Failed to allocate memory in"
  3676. "hba_setup_cid_tbls\n");
  3677. kfree(phba->ep_array);
  3678. phba->ep_array = NULL;
  3679. ret = -ENOMEM;
  3680. goto free_memory;
  3681. }
  3682. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3683. ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
  3684. ptr_cid_info = phba->cid_array_info[ulp_num];
  3685. ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
  3686. phba->phwi_ctrlr->wrb_context[i].cid;
  3687. }
  3688. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3689. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3690. ptr_cid_info = phba->cid_array_info[ulp_num];
  3691. ptr_cid_info->cid_alloc = 0;
  3692. ptr_cid_info->cid_free = 0;
  3693. }
  3694. }
  3695. return 0;
  3696. free_memory:
  3697. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3698. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3699. ptr_cid_info = phba->cid_array_info[ulp_num];
  3700. if (ptr_cid_info) {
  3701. kfree(ptr_cid_info->cid_array);
  3702. kfree(ptr_cid_info);
  3703. phba->cid_array_info[ulp_num] = NULL;
  3704. }
  3705. }
  3706. }
  3707. return ret;
  3708. }
  3709. static void hwi_enable_intr(struct beiscsi_hba *phba)
  3710. {
  3711. struct be_ctrl_info *ctrl = &phba->ctrl;
  3712. struct hwi_controller *phwi_ctrlr;
  3713. struct hwi_context_memory *phwi_context;
  3714. struct be_queue_info *eq;
  3715. u8 __iomem *addr;
  3716. u32 reg, i;
  3717. u32 enabled;
  3718. phwi_ctrlr = phba->phwi_ctrlr;
  3719. phwi_context = phwi_ctrlr->phwi_ctxt;
  3720. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  3721. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  3722. reg = ioread32(addr);
  3723. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3724. if (!enabled) {
  3725. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3726. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3727. "BM_%d : reg =x%08x addr=%p\n", reg, addr);
  3728. iowrite32(reg, addr);
  3729. }
  3730. if (!phba->msix_enabled) {
  3731. eq = &phwi_context->be_eq[0].q;
  3732. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3733. "BM_%d : eq->id=%d\n", eq->id);
  3734. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3735. } else {
  3736. for (i = 0; i <= phba->num_cpus; i++) {
  3737. eq = &phwi_context->be_eq[i].q;
  3738. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3739. "BM_%d : eq->id=%d\n", eq->id);
  3740. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3741. }
  3742. }
  3743. }
  3744. static void hwi_disable_intr(struct beiscsi_hba *phba)
  3745. {
  3746. struct be_ctrl_info *ctrl = &phba->ctrl;
  3747. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  3748. u32 reg = ioread32(addr);
  3749. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3750. if (enabled) {
  3751. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3752. iowrite32(reg, addr);
  3753. } else
  3754. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  3755. "BM_%d : In hwi_disable_intr, Already Disabled\n");
  3756. }
  3757. static int beiscsi_init_port(struct beiscsi_hba *phba)
  3758. {
  3759. int ret;
  3760. ret = beiscsi_init_controller(phba);
  3761. if (ret < 0) {
  3762. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3763. "BM_%d : beiscsi_dev_probe - Failed in"
  3764. "beiscsi_init_controller\n");
  3765. return ret;
  3766. }
  3767. ret = beiscsi_init_sgl_handle(phba);
  3768. if (ret < 0) {
  3769. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3770. "BM_%d : beiscsi_dev_probe - Failed in"
  3771. "beiscsi_init_sgl_handle\n");
  3772. goto do_cleanup_ctrlr;
  3773. }
  3774. ret = hba_setup_cid_tbls(phba);
  3775. if (ret < 0) {
  3776. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3777. "BM_%d : Failed in hba_setup_cid_tbls\n");
  3778. kfree(phba->io_sgl_hndl_base);
  3779. kfree(phba->eh_sgl_hndl_base);
  3780. goto do_cleanup_ctrlr;
  3781. }
  3782. return ret;
  3783. do_cleanup_ctrlr:
  3784. hwi_cleanup_port(phba);
  3785. return ret;
  3786. }
  3787. static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
  3788. {
  3789. struct ulp_cid_info *ptr_cid_info = NULL;
  3790. int ulp_num;
  3791. kfree(phba->io_sgl_hndl_base);
  3792. kfree(phba->eh_sgl_hndl_base);
  3793. kfree(phba->ep_array);
  3794. kfree(phba->conn_table);
  3795. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3796. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3797. ptr_cid_info = phba->cid_array_info[ulp_num];
  3798. if (ptr_cid_info) {
  3799. kfree(ptr_cid_info->cid_array);
  3800. kfree(ptr_cid_info);
  3801. phba->cid_array_info[ulp_num] = NULL;
  3802. }
  3803. }
  3804. }
  3805. }
  3806. /**
  3807. * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
  3808. * @beiscsi_conn: ptr to the conn to be cleaned up
  3809. * @task: ptr to iscsi_task resource to be freed.
  3810. *
  3811. * Free driver mgmt resources binded to CXN.
  3812. **/
  3813. void
  3814. beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
  3815. struct iscsi_task *task)
  3816. {
  3817. struct beiscsi_io_task *io_task;
  3818. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3819. struct hwi_wrb_context *pwrb_context;
  3820. struct hwi_controller *phwi_ctrlr;
  3821. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3822. beiscsi_conn->beiscsi_conn_cid);
  3823. phwi_ctrlr = phba->phwi_ctrlr;
  3824. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3825. io_task = task->dd_data;
  3826. if (io_task->pwrb_handle) {
  3827. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3828. io_task->pwrb_handle = NULL;
  3829. }
  3830. if (io_task->psgl_handle) {
  3831. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3832. io_task->psgl_handle = NULL;
  3833. }
  3834. if (io_task->mtask_addr) {
  3835. pci_unmap_single(phba->pcidev,
  3836. io_task->mtask_addr,
  3837. io_task->mtask_data_count,
  3838. PCI_DMA_TODEVICE);
  3839. io_task->mtask_addr = 0;
  3840. }
  3841. }
  3842. /**
  3843. * beiscsi_cleanup_task()- Free driver resources of the task
  3844. * @task: ptr to the iscsi task
  3845. *
  3846. **/
  3847. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3848. {
  3849. struct beiscsi_io_task *io_task = task->dd_data;
  3850. struct iscsi_conn *conn = task->conn;
  3851. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3852. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3853. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3854. struct hwi_wrb_context *pwrb_context;
  3855. struct hwi_controller *phwi_ctrlr;
  3856. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3857. beiscsi_conn->beiscsi_conn_cid);
  3858. phwi_ctrlr = phba->phwi_ctrlr;
  3859. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3860. if (io_task->cmd_bhs) {
  3861. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3862. io_task->bhs_pa.u.a64.address);
  3863. io_task->cmd_bhs = NULL;
  3864. task->hdr = NULL;
  3865. }
  3866. if (task->sc) {
  3867. if (io_task->pwrb_handle) {
  3868. free_wrb_handle(phba, pwrb_context,
  3869. io_task->pwrb_handle);
  3870. io_task->pwrb_handle = NULL;
  3871. }
  3872. if (io_task->psgl_handle) {
  3873. free_io_sgl_handle(phba, io_task->psgl_handle);
  3874. io_task->psgl_handle = NULL;
  3875. }
  3876. if (io_task->scsi_cmnd) {
  3877. if (io_task->num_sg)
  3878. scsi_dma_unmap(io_task->scsi_cmnd);
  3879. io_task->scsi_cmnd = NULL;
  3880. }
  3881. } else {
  3882. if (!beiscsi_conn->login_in_progress)
  3883. beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
  3884. }
  3885. }
  3886. void
  3887. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3888. struct beiscsi_offload_params *params)
  3889. {
  3890. struct wrb_handle *pwrb_handle;
  3891. struct hwi_wrb_context *pwrb_context = NULL;
  3892. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3893. struct iscsi_task *task = beiscsi_conn->task;
  3894. struct iscsi_session *session = task->conn->session;
  3895. u32 doorbell = 0;
  3896. /*
  3897. * We can always use 0 here because it is reserved by libiscsi for
  3898. * login/startup related tasks.
  3899. */
  3900. beiscsi_conn->login_in_progress = 0;
  3901. spin_lock_bh(&session->back_lock);
  3902. beiscsi_cleanup_task(task);
  3903. spin_unlock_bh(&session->back_lock);
  3904. pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
  3905. &pwrb_context);
  3906. /* Check for the adapter family */
  3907. if (is_chip_be2_be3r(phba))
  3908. beiscsi_offload_cxn_v0(params, pwrb_handle,
  3909. phba->init_mem,
  3910. pwrb_context);
  3911. else
  3912. beiscsi_offload_cxn_v2(params, pwrb_handle,
  3913. pwrb_context);
  3914. be_dws_le_to_cpu(pwrb_handle->pwrb,
  3915. sizeof(struct iscsi_target_context_update_wrb));
  3916. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3917. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3918. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3919. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3920. iowrite32(doorbell, phba->db_va +
  3921. beiscsi_conn->doorbell_offset);
  3922. /*
  3923. * There is no completion for CONTEXT_UPDATE. The completion of next
  3924. * WRB posted guarantees FW's processing and DMA'ing of it.
  3925. * Use beiscsi_put_wrb_handle to put it back in the pool which makes
  3926. * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
  3927. */
  3928. beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
  3929. phba->params.wrbs_per_cxn);
  3930. beiscsi_log(phba, KERN_INFO,
  3931. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3932. "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
  3933. pwrb_handle, pwrb_context->free_index,
  3934. pwrb_context->wrb_handles_available);
  3935. }
  3936. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3937. int *index, int *age)
  3938. {
  3939. *index = (int)itt;
  3940. if (age)
  3941. *age = conn->session->age;
  3942. }
  3943. /**
  3944. * beiscsi_alloc_pdu - allocates pdu and related resources
  3945. * @task: libiscsi task
  3946. * @opcode: opcode of pdu for task
  3947. *
  3948. * This is called with the session lock held. It will allocate
  3949. * the wrb and sgl if needed for the command. And it will prep
  3950. * the pdu's itt. beiscsi_parse_pdu will later translate
  3951. * the pdu itt to the libiscsi task itt.
  3952. */
  3953. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3954. {
  3955. struct beiscsi_io_task *io_task = task->dd_data;
  3956. struct iscsi_conn *conn = task->conn;
  3957. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3958. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3959. struct hwi_wrb_context *pwrb_context;
  3960. struct hwi_controller *phwi_ctrlr;
  3961. itt_t itt;
  3962. uint16_t cri_index = 0;
  3963. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3964. dma_addr_t paddr;
  3965. io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
  3966. GFP_ATOMIC, &paddr);
  3967. if (!io_task->cmd_bhs)
  3968. return -ENOMEM;
  3969. io_task->bhs_pa.u.a64.address = paddr;
  3970. io_task->libiscsi_itt = (itt_t)task->itt;
  3971. io_task->conn = beiscsi_conn;
  3972. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3973. task->hdr_max = sizeof(struct be_cmd_bhs);
  3974. io_task->psgl_handle = NULL;
  3975. io_task->pwrb_handle = NULL;
  3976. if (task->sc) {
  3977. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3978. if (!io_task->psgl_handle) {
  3979. beiscsi_log(phba, KERN_ERR,
  3980. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3981. "BM_%d : Alloc of IO_SGL_ICD Failed"
  3982. "for the CID : %d\n",
  3983. beiscsi_conn->beiscsi_conn_cid);
  3984. goto free_hndls;
  3985. }
  3986. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3987. beiscsi_conn->beiscsi_conn_cid,
  3988. &io_task->pwrb_context);
  3989. if (!io_task->pwrb_handle) {
  3990. beiscsi_log(phba, KERN_ERR,
  3991. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3992. "BM_%d : Alloc of WRB_HANDLE Failed"
  3993. "for the CID : %d\n",
  3994. beiscsi_conn->beiscsi_conn_cid);
  3995. goto free_io_hndls;
  3996. }
  3997. } else {
  3998. io_task->scsi_cmnd = NULL;
  3999. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  4000. beiscsi_conn->task = task;
  4001. if (!beiscsi_conn->login_in_progress) {
  4002. io_task->psgl_handle = (struct sgl_handle *)
  4003. alloc_mgmt_sgl_handle(phba);
  4004. if (!io_task->psgl_handle) {
  4005. beiscsi_log(phba, KERN_ERR,
  4006. BEISCSI_LOG_IO |
  4007. BEISCSI_LOG_CONFIG,
  4008. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  4009. "for the CID : %d\n",
  4010. beiscsi_conn->
  4011. beiscsi_conn_cid);
  4012. goto free_hndls;
  4013. }
  4014. beiscsi_conn->login_in_progress = 1;
  4015. beiscsi_conn->plogin_sgl_handle =
  4016. io_task->psgl_handle;
  4017. io_task->pwrb_handle =
  4018. alloc_wrb_handle(phba,
  4019. beiscsi_conn->beiscsi_conn_cid,
  4020. &io_task->pwrb_context);
  4021. if (!io_task->pwrb_handle) {
  4022. beiscsi_log(phba, KERN_ERR,
  4023. BEISCSI_LOG_IO |
  4024. BEISCSI_LOG_CONFIG,
  4025. "BM_%d : Alloc of WRB_HANDLE Failed"
  4026. "for the CID : %d\n",
  4027. beiscsi_conn->
  4028. beiscsi_conn_cid);
  4029. goto free_mgmt_hndls;
  4030. }
  4031. beiscsi_conn->plogin_wrb_handle =
  4032. io_task->pwrb_handle;
  4033. } else {
  4034. io_task->psgl_handle =
  4035. beiscsi_conn->plogin_sgl_handle;
  4036. io_task->pwrb_handle =
  4037. beiscsi_conn->plogin_wrb_handle;
  4038. }
  4039. } else {
  4040. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  4041. if (!io_task->psgl_handle) {
  4042. beiscsi_log(phba, KERN_ERR,
  4043. BEISCSI_LOG_IO |
  4044. BEISCSI_LOG_CONFIG,
  4045. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  4046. "for the CID : %d\n",
  4047. beiscsi_conn->
  4048. beiscsi_conn_cid);
  4049. goto free_hndls;
  4050. }
  4051. io_task->pwrb_handle =
  4052. alloc_wrb_handle(phba,
  4053. beiscsi_conn->beiscsi_conn_cid,
  4054. &io_task->pwrb_context);
  4055. if (!io_task->pwrb_handle) {
  4056. beiscsi_log(phba, KERN_ERR,
  4057. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  4058. "BM_%d : Alloc of WRB_HANDLE Failed"
  4059. "for the CID : %d\n",
  4060. beiscsi_conn->beiscsi_conn_cid);
  4061. goto free_mgmt_hndls;
  4062. }
  4063. }
  4064. }
  4065. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  4066. wrb_index << 16) | (unsigned int)
  4067. (io_task->psgl_handle->sgl_index));
  4068. io_task->pwrb_handle->pio_handle = task;
  4069. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  4070. return 0;
  4071. free_io_hndls:
  4072. free_io_sgl_handle(phba, io_task->psgl_handle);
  4073. goto free_hndls;
  4074. free_mgmt_hndls:
  4075. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  4076. io_task->psgl_handle = NULL;
  4077. free_hndls:
  4078. phwi_ctrlr = phba->phwi_ctrlr;
  4079. cri_index = BE_GET_CRI_FROM_CID(
  4080. beiscsi_conn->beiscsi_conn_cid);
  4081. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  4082. if (io_task->pwrb_handle)
  4083. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  4084. io_task->pwrb_handle = NULL;
  4085. pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  4086. io_task->bhs_pa.u.a64.address);
  4087. io_task->cmd_bhs = NULL;
  4088. return -ENOMEM;
  4089. }
  4090. int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
  4091. unsigned int num_sg, unsigned int xferlen,
  4092. unsigned int writedir)
  4093. {
  4094. struct beiscsi_io_task *io_task = task->dd_data;
  4095. struct iscsi_conn *conn = task->conn;
  4096. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4097. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4098. struct iscsi_wrb *pwrb = NULL;
  4099. unsigned int doorbell = 0;
  4100. pwrb = io_task->pwrb_handle->pwrb;
  4101. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4102. if (writedir) {
  4103. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4104. INI_WR_CMD);
  4105. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
  4106. } else {
  4107. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4108. INI_RD_CMD);
  4109. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
  4110. }
  4111. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
  4112. type, pwrb);
  4113. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
  4114. cpu_to_be16(*(unsigned short *)
  4115. &io_task->cmd_bhs->iscsi_hdr.lun));
  4116. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
  4117. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4118. io_task->pwrb_handle->wrb_index);
  4119. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4120. be32_to_cpu(task->cmdsn));
  4121. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4122. io_task->psgl_handle->sgl_index);
  4123. hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
  4124. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4125. io_task->pwrb_handle->wrb_index);
  4126. if (io_task->pwrb_context->plast_wrb)
  4127. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4128. io_task->pwrb_context->plast_wrb,
  4129. io_task->pwrb_handle->wrb_index);
  4130. io_task->pwrb_context->plast_wrb = pwrb;
  4131. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4132. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4133. doorbell |= (io_task->pwrb_handle->wrb_index &
  4134. DB_DEF_PDU_WRB_INDEX_MASK) <<
  4135. DB_DEF_PDU_WRB_INDEX_SHIFT;
  4136. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4137. iowrite32(doorbell, phba->db_va +
  4138. beiscsi_conn->doorbell_offset);
  4139. return 0;
  4140. }
  4141. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  4142. unsigned int num_sg, unsigned int xferlen,
  4143. unsigned int writedir)
  4144. {
  4145. struct beiscsi_io_task *io_task = task->dd_data;
  4146. struct iscsi_conn *conn = task->conn;
  4147. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4148. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4149. struct iscsi_wrb *pwrb = NULL;
  4150. unsigned int doorbell = 0;
  4151. pwrb = io_task->pwrb_handle->pwrb;
  4152. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4153. if (writedir) {
  4154. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4155. INI_WR_CMD);
  4156. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  4157. } else {
  4158. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4159. INI_RD_CMD);
  4160. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  4161. }
  4162. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
  4163. type, pwrb);
  4164. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  4165. cpu_to_be16(*(unsigned short *)
  4166. &io_task->cmd_bhs->iscsi_hdr.lun));
  4167. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  4168. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4169. io_task->pwrb_handle->wrb_index);
  4170. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4171. be32_to_cpu(task->cmdsn));
  4172. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4173. io_task->psgl_handle->sgl_index);
  4174. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  4175. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4176. io_task->pwrb_handle->wrb_index);
  4177. if (io_task->pwrb_context->plast_wrb)
  4178. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4179. io_task->pwrb_context->plast_wrb,
  4180. io_task->pwrb_handle->wrb_index);
  4181. io_task->pwrb_context->plast_wrb = pwrb;
  4182. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4183. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4184. doorbell |= (io_task->pwrb_handle->wrb_index &
  4185. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4186. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4187. iowrite32(doorbell, phba->db_va +
  4188. beiscsi_conn->doorbell_offset);
  4189. return 0;
  4190. }
  4191. static int beiscsi_mtask(struct iscsi_task *task)
  4192. {
  4193. struct beiscsi_io_task *io_task = task->dd_data;
  4194. struct iscsi_conn *conn = task->conn;
  4195. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4196. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4197. struct iscsi_wrb *pwrb = NULL;
  4198. unsigned int doorbell = 0;
  4199. unsigned int cid;
  4200. unsigned int pwrb_typeoffset = 0;
  4201. int ret = 0;
  4202. cid = beiscsi_conn->beiscsi_conn_cid;
  4203. pwrb = io_task->pwrb_handle->pwrb;
  4204. if (is_chip_be2_be3r(phba)) {
  4205. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4206. be32_to_cpu(task->cmdsn));
  4207. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4208. io_task->pwrb_handle->wrb_index);
  4209. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4210. io_task->psgl_handle->sgl_index);
  4211. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  4212. task->data_count);
  4213. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4214. io_task->pwrb_handle->wrb_index);
  4215. if (io_task->pwrb_context->plast_wrb)
  4216. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4217. io_task->pwrb_context->plast_wrb,
  4218. io_task->pwrb_handle->wrb_index);
  4219. io_task->pwrb_context->plast_wrb = pwrb;
  4220. pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
  4221. } else {
  4222. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4223. be32_to_cpu(task->cmdsn));
  4224. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4225. io_task->pwrb_handle->wrb_index);
  4226. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4227. io_task->psgl_handle->sgl_index);
  4228. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
  4229. task->data_count);
  4230. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4231. io_task->pwrb_handle->wrb_index);
  4232. if (io_task->pwrb_context->plast_wrb)
  4233. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4234. io_task->pwrb_context->plast_wrb,
  4235. io_task->pwrb_handle->wrb_index);
  4236. io_task->pwrb_context->plast_wrb = pwrb;
  4237. pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
  4238. }
  4239. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  4240. case ISCSI_OP_LOGIN:
  4241. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  4242. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4243. ret = hwi_write_buffer(pwrb, task);
  4244. break;
  4245. case ISCSI_OP_NOOP_OUT:
  4246. if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
  4247. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4248. if (is_chip_be2_be3r(phba))
  4249. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4250. dmsg, pwrb, 1);
  4251. else
  4252. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4253. dmsg, pwrb, 1);
  4254. } else {
  4255. ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
  4256. if (is_chip_be2_be3r(phba))
  4257. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4258. dmsg, pwrb, 0);
  4259. else
  4260. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4261. dmsg, pwrb, 0);
  4262. }
  4263. ret = hwi_write_buffer(pwrb, task);
  4264. break;
  4265. case ISCSI_OP_TEXT:
  4266. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4267. ret = hwi_write_buffer(pwrb, task);
  4268. break;
  4269. case ISCSI_OP_SCSI_TMFUNC:
  4270. ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
  4271. ret = hwi_write_buffer(pwrb, task);
  4272. break;
  4273. case ISCSI_OP_LOGOUT:
  4274. ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
  4275. ret = hwi_write_buffer(pwrb, task);
  4276. break;
  4277. default:
  4278. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4279. "BM_%d : opcode =%d Not supported\n",
  4280. task->hdr->opcode & ISCSI_OPCODE_MASK);
  4281. return -EINVAL;
  4282. }
  4283. if (ret)
  4284. return ret;
  4285. /* Set the task type */
  4286. io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
  4287. AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
  4288. AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
  4289. doorbell |= cid & DB_WRB_POST_CID_MASK;
  4290. doorbell |= (io_task->pwrb_handle->wrb_index &
  4291. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4292. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4293. iowrite32(doorbell, phba->db_va +
  4294. beiscsi_conn->doorbell_offset);
  4295. return 0;
  4296. }
  4297. static int beiscsi_task_xmit(struct iscsi_task *task)
  4298. {
  4299. struct beiscsi_io_task *io_task = task->dd_data;
  4300. struct scsi_cmnd *sc = task->sc;
  4301. struct beiscsi_hba *phba;
  4302. struct scatterlist *sg;
  4303. int num_sg;
  4304. unsigned int writedir = 0, xferlen = 0;
  4305. phba = io_task->conn->phba;
  4306. /**
  4307. * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
  4308. * operational if FW still gets heartbeat from EP FW. Is management
  4309. * path really needed to continue further?
  4310. */
  4311. if (!beiscsi_hba_is_online(phba))
  4312. return -EIO;
  4313. if (!io_task->conn->login_in_progress)
  4314. task->hdr->exp_statsn = 0;
  4315. if (!sc)
  4316. return beiscsi_mtask(task);
  4317. io_task->scsi_cmnd = sc;
  4318. io_task->num_sg = 0;
  4319. num_sg = scsi_dma_map(sc);
  4320. if (num_sg < 0) {
  4321. beiscsi_log(phba, KERN_ERR,
  4322. BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
  4323. "BM_%d : scsi_dma_map Failed "
  4324. "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
  4325. be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
  4326. io_task->libiscsi_itt, scsi_bufflen(sc));
  4327. return num_sg;
  4328. }
  4329. /**
  4330. * For scsi cmd task, check num_sg before unmapping in cleanup_task.
  4331. * For management task, cleanup_task checks mtask_addr before unmapping.
  4332. */
  4333. io_task->num_sg = num_sg;
  4334. xferlen = scsi_bufflen(sc);
  4335. sg = scsi_sglist(sc);
  4336. if (sc->sc_data_direction == DMA_TO_DEVICE)
  4337. writedir = 1;
  4338. else
  4339. writedir = 0;
  4340. return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
  4341. }
  4342. /**
  4343. * beiscsi_bsg_request - handle bsg request from ISCSI transport
  4344. * @job: job to handle
  4345. */
  4346. static int beiscsi_bsg_request(struct bsg_job *job)
  4347. {
  4348. struct Scsi_Host *shost;
  4349. struct beiscsi_hba *phba;
  4350. struct iscsi_bsg_request *bsg_req = job->request;
  4351. int rc = -EINVAL;
  4352. unsigned int tag;
  4353. struct be_dma_mem nonemb_cmd;
  4354. struct be_cmd_resp_hdr *resp;
  4355. struct iscsi_bsg_reply *bsg_reply = job->reply;
  4356. unsigned short status, extd_status;
  4357. shost = iscsi_job_to_shost(job);
  4358. phba = iscsi_host_priv(shost);
  4359. if (!beiscsi_hba_is_online(phba)) {
  4360. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  4361. "BM_%d : HBA in error 0x%lx\n", phba->state);
  4362. return -ENXIO;
  4363. }
  4364. switch (bsg_req->msgcode) {
  4365. case ISCSI_BSG_HST_VENDOR:
  4366. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  4367. job->request_payload.payload_len,
  4368. &nonemb_cmd.dma);
  4369. if (nonemb_cmd.va == NULL) {
  4370. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4371. "BM_%d : Failed to allocate memory for "
  4372. "beiscsi_bsg_request\n");
  4373. return -ENOMEM;
  4374. }
  4375. tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
  4376. &nonemb_cmd);
  4377. if (!tag) {
  4378. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4379. "BM_%d : MBX Tag Allocation Failed\n");
  4380. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4381. nonemb_cmd.va, nonemb_cmd.dma);
  4382. return -EAGAIN;
  4383. }
  4384. rc = wait_event_interruptible_timeout(
  4385. phba->ctrl.mcc_wait[tag],
  4386. phba->ctrl.mcc_tag_status[tag],
  4387. msecs_to_jiffies(
  4388. BEISCSI_HOST_MBX_TIMEOUT));
  4389. if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4390. clear_bit(MCC_TAG_STATE_RUNNING,
  4391. &phba->ctrl.ptag_state[tag].tag_state);
  4392. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4393. nonemb_cmd.va, nonemb_cmd.dma);
  4394. return -EIO;
  4395. }
  4396. extd_status = (phba->ctrl.mcc_tag_status[tag] &
  4397. CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
  4398. status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
  4399. free_mcc_wrb(&phba->ctrl, tag);
  4400. resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
  4401. sg_copy_from_buffer(job->reply_payload.sg_list,
  4402. job->reply_payload.sg_cnt,
  4403. nonemb_cmd.va, (resp->response_length
  4404. + sizeof(*resp)));
  4405. bsg_reply->reply_payload_rcv_len = resp->response_length;
  4406. bsg_reply->result = status;
  4407. bsg_job_done(job, bsg_reply->result,
  4408. bsg_reply->reply_payload_rcv_len);
  4409. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4410. nonemb_cmd.va, nonemb_cmd.dma);
  4411. if (status || extd_status) {
  4412. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4413. "BM_%d : MBX Cmd Failed"
  4414. " status = %d extd_status = %d\n",
  4415. status, extd_status);
  4416. return -EIO;
  4417. } else {
  4418. rc = 0;
  4419. }
  4420. break;
  4421. default:
  4422. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4423. "BM_%d : Unsupported bsg command: 0x%x\n",
  4424. bsg_req->msgcode);
  4425. break;
  4426. }
  4427. return rc;
  4428. }
  4429. void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
  4430. {
  4431. /* Set the logging parameter */
  4432. beiscsi_log_enable_init(phba, beiscsi_log_enable);
  4433. }
  4434. void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
  4435. {
  4436. if (phba->boot_struct.boot_kset)
  4437. return;
  4438. /* skip if boot work is already in progress */
  4439. if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
  4440. return;
  4441. phba->boot_struct.retry = 3;
  4442. phba->boot_struct.tag = 0;
  4443. phba->boot_struct.s_handle = s_handle;
  4444. phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
  4445. schedule_work(&phba->boot_work);
  4446. }
  4447. static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
  4448. {
  4449. struct beiscsi_hba *phba = data;
  4450. struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
  4451. struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
  4452. char *str = buf;
  4453. int rc = -EPERM;
  4454. switch (type) {
  4455. case ISCSI_BOOT_TGT_NAME:
  4456. rc = sprintf(buf, "%.*s\n",
  4457. (int)strlen(boot_sess->target_name),
  4458. (char *)&boot_sess->target_name);
  4459. break;
  4460. case ISCSI_BOOT_TGT_IP_ADDR:
  4461. if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
  4462. rc = sprintf(buf, "%pI4\n",
  4463. (char *)&boot_conn->dest_ipaddr.addr);
  4464. else
  4465. rc = sprintf(str, "%pI6\n",
  4466. (char *)&boot_conn->dest_ipaddr.addr);
  4467. break;
  4468. case ISCSI_BOOT_TGT_PORT:
  4469. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  4470. break;
  4471. case ISCSI_BOOT_TGT_CHAP_NAME:
  4472. rc = sprintf(str, "%.*s\n",
  4473. boot_conn->negotiated_login_options.auth_data.chap.
  4474. target_chap_name_length,
  4475. (char *)&boot_conn->negotiated_login_options.
  4476. auth_data.chap.target_chap_name);
  4477. break;
  4478. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4479. rc = sprintf(str, "%.*s\n",
  4480. boot_conn->negotiated_login_options.auth_data.chap.
  4481. target_secret_length,
  4482. (char *)&boot_conn->negotiated_login_options.
  4483. auth_data.chap.target_secret);
  4484. break;
  4485. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4486. rc = sprintf(str, "%.*s\n",
  4487. boot_conn->negotiated_login_options.auth_data.chap.
  4488. intr_chap_name_length,
  4489. (char *)&boot_conn->negotiated_login_options.
  4490. auth_data.chap.intr_chap_name);
  4491. break;
  4492. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4493. rc = sprintf(str, "%.*s\n",
  4494. boot_conn->negotiated_login_options.auth_data.chap.
  4495. intr_secret_length,
  4496. (char *)&boot_conn->negotiated_login_options.
  4497. auth_data.chap.intr_secret);
  4498. break;
  4499. case ISCSI_BOOT_TGT_FLAGS:
  4500. rc = sprintf(str, "2\n");
  4501. break;
  4502. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4503. rc = sprintf(str, "0\n");
  4504. break;
  4505. }
  4506. return rc;
  4507. }
  4508. static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
  4509. {
  4510. struct beiscsi_hba *phba = data;
  4511. char *str = buf;
  4512. int rc = -EPERM;
  4513. switch (type) {
  4514. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4515. rc = sprintf(str, "%s\n",
  4516. phba->boot_struct.boot_sess.initiator_iscsiname);
  4517. break;
  4518. }
  4519. return rc;
  4520. }
  4521. static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
  4522. {
  4523. struct beiscsi_hba *phba = data;
  4524. char *str = buf;
  4525. int rc = -EPERM;
  4526. switch (type) {
  4527. case ISCSI_BOOT_ETH_FLAGS:
  4528. rc = sprintf(str, "2\n");
  4529. break;
  4530. case ISCSI_BOOT_ETH_INDEX:
  4531. rc = sprintf(str, "0\n");
  4532. break;
  4533. case ISCSI_BOOT_ETH_MAC:
  4534. rc = beiscsi_get_macaddr(str, phba);
  4535. break;
  4536. }
  4537. return rc;
  4538. }
  4539. static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
  4540. {
  4541. umode_t rc = 0;
  4542. switch (type) {
  4543. case ISCSI_BOOT_TGT_NAME:
  4544. case ISCSI_BOOT_TGT_IP_ADDR:
  4545. case ISCSI_BOOT_TGT_PORT:
  4546. case ISCSI_BOOT_TGT_CHAP_NAME:
  4547. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4548. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4549. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4550. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4551. case ISCSI_BOOT_TGT_FLAGS:
  4552. rc = S_IRUGO;
  4553. break;
  4554. }
  4555. return rc;
  4556. }
  4557. static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
  4558. {
  4559. umode_t rc = 0;
  4560. switch (type) {
  4561. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4562. rc = S_IRUGO;
  4563. break;
  4564. }
  4565. return rc;
  4566. }
  4567. static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
  4568. {
  4569. umode_t rc = 0;
  4570. switch (type) {
  4571. case ISCSI_BOOT_ETH_FLAGS:
  4572. case ISCSI_BOOT_ETH_MAC:
  4573. case ISCSI_BOOT_ETH_INDEX:
  4574. rc = S_IRUGO;
  4575. break;
  4576. }
  4577. return rc;
  4578. }
  4579. static void beiscsi_boot_kobj_release(void *data)
  4580. {
  4581. struct beiscsi_hba *phba = data;
  4582. scsi_host_put(phba->shost);
  4583. }
  4584. static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
  4585. {
  4586. struct boot_struct *bs = &phba->boot_struct;
  4587. struct iscsi_boot_kobj *boot_kobj;
  4588. if (bs->boot_kset) {
  4589. __beiscsi_log(phba, KERN_ERR,
  4590. "BM_%d: boot_kset already created\n");
  4591. return 0;
  4592. }
  4593. bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
  4594. if (!bs->boot_kset) {
  4595. __beiscsi_log(phba, KERN_ERR,
  4596. "BM_%d: boot_kset alloc failed\n");
  4597. return -ENOMEM;
  4598. }
  4599. /* get shost ref because the show function will refer phba */
  4600. if (!scsi_host_get(phba->shost))
  4601. goto free_kset;
  4602. boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
  4603. beiscsi_show_boot_tgt_info,
  4604. beiscsi_tgt_get_attr_visibility,
  4605. beiscsi_boot_kobj_release);
  4606. if (!boot_kobj)
  4607. goto put_shost;
  4608. if (!scsi_host_get(phba->shost))
  4609. goto free_kset;
  4610. boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
  4611. beiscsi_show_boot_ini_info,
  4612. beiscsi_ini_get_attr_visibility,
  4613. beiscsi_boot_kobj_release);
  4614. if (!boot_kobj)
  4615. goto put_shost;
  4616. if (!scsi_host_get(phba->shost))
  4617. goto free_kset;
  4618. boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
  4619. beiscsi_show_boot_eth_info,
  4620. beiscsi_eth_get_attr_visibility,
  4621. beiscsi_boot_kobj_release);
  4622. if (!boot_kobj)
  4623. goto put_shost;
  4624. return 0;
  4625. put_shost:
  4626. scsi_host_put(phba->shost);
  4627. free_kset:
  4628. iscsi_boot_destroy_kset(bs->boot_kset);
  4629. bs->boot_kset = NULL;
  4630. return -ENOMEM;
  4631. }
  4632. static void beiscsi_boot_work(struct work_struct *work)
  4633. {
  4634. struct beiscsi_hba *phba =
  4635. container_of(work, struct beiscsi_hba, boot_work);
  4636. struct boot_struct *bs = &phba->boot_struct;
  4637. unsigned int tag = 0;
  4638. if (!beiscsi_hba_is_online(phba))
  4639. return;
  4640. beiscsi_log(phba, KERN_INFO,
  4641. BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
  4642. "BM_%d : %s action %d\n",
  4643. __func__, phba->boot_struct.action);
  4644. switch (phba->boot_struct.action) {
  4645. case BEISCSI_BOOT_REOPEN_SESS:
  4646. tag = beiscsi_boot_reopen_sess(phba);
  4647. break;
  4648. case BEISCSI_BOOT_GET_SHANDLE:
  4649. tag = __beiscsi_boot_get_shandle(phba, 1);
  4650. break;
  4651. case BEISCSI_BOOT_GET_SINFO:
  4652. tag = beiscsi_boot_get_sinfo(phba);
  4653. break;
  4654. case BEISCSI_BOOT_LOGOUT_SESS:
  4655. tag = beiscsi_boot_logout_sess(phba);
  4656. break;
  4657. case BEISCSI_BOOT_CREATE_KSET:
  4658. beiscsi_boot_create_kset(phba);
  4659. /**
  4660. * updated boot_kset is made visible to all before
  4661. * ending the boot work.
  4662. */
  4663. mb();
  4664. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4665. return;
  4666. }
  4667. if (!tag) {
  4668. if (bs->retry--)
  4669. schedule_work(&phba->boot_work);
  4670. else
  4671. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4672. }
  4673. }
  4674. static void beiscsi_eqd_update_work(struct work_struct *work)
  4675. {
  4676. struct hwi_context_memory *phwi_context;
  4677. struct be_set_eqd set_eqd[MAX_CPUS];
  4678. struct hwi_controller *phwi_ctrlr;
  4679. struct be_eq_obj *pbe_eq;
  4680. struct beiscsi_hba *phba;
  4681. unsigned int pps, delta;
  4682. struct be_aic_obj *aic;
  4683. int eqd, i, num = 0;
  4684. unsigned long now;
  4685. phba = container_of(work, struct beiscsi_hba, eqd_update.work);
  4686. if (!beiscsi_hba_is_online(phba))
  4687. return;
  4688. phwi_ctrlr = phba->phwi_ctrlr;
  4689. phwi_context = phwi_ctrlr->phwi_ctxt;
  4690. for (i = 0; i <= phba->num_cpus; i++) {
  4691. aic = &phba->aic_obj[i];
  4692. pbe_eq = &phwi_context->be_eq[i];
  4693. now = jiffies;
  4694. if (!aic->jiffies || time_before(now, aic->jiffies) ||
  4695. pbe_eq->cq_count < aic->eq_prev) {
  4696. aic->jiffies = now;
  4697. aic->eq_prev = pbe_eq->cq_count;
  4698. continue;
  4699. }
  4700. delta = jiffies_to_msecs(now - aic->jiffies);
  4701. pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
  4702. eqd = (pps / 1500) << 2;
  4703. if (eqd < 8)
  4704. eqd = 0;
  4705. eqd = min_t(u32, eqd, phwi_context->max_eqd);
  4706. eqd = max_t(u32, eqd, phwi_context->min_eqd);
  4707. aic->jiffies = now;
  4708. aic->eq_prev = pbe_eq->cq_count;
  4709. if (eqd != aic->prev_eqd) {
  4710. set_eqd[num].delay_multiplier = (eqd * 65)/100;
  4711. set_eqd[num].eq_id = pbe_eq->q.id;
  4712. aic->prev_eqd = eqd;
  4713. num++;
  4714. }
  4715. }
  4716. if (num)
  4717. /* completion of this is ignored */
  4718. beiscsi_modify_eq_delay(phba, set_eqd, num);
  4719. schedule_delayed_work(&phba->eqd_update,
  4720. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4721. }
  4722. static void beiscsi_msix_enable(struct beiscsi_hba *phba)
  4723. {
  4724. int i, status;
  4725. for (i = 0; i <= phba->num_cpus; i++)
  4726. phba->msix_entries[i].entry = i;
  4727. status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
  4728. phba->num_cpus + 1, phba->num_cpus + 1);
  4729. if (status > 0)
  4730. phba->msix_enabled = true;
  4731. }
  4732. static void beiscsi_hw_tpe_check(unsigned long ptr)
  4733. {
  4734. struct beiscsi_hba *phba;
  4735. u32 wait;
  4736. phba = (struct beiscsi_hba *)ptr;
  4737. /* if not TPE, do nothing */
  4738. if (!beiscsi_detect_tpe(phba))
  4739. return;
  4740. /* wait default 4000ms before recovering */
  4741. wait = 4000;
  4742. if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
  4743. wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
  4744. queue_delayed_work(phba->wq, &phba->recover_port,
  4745. msecs_to_jiffies(wait));
  4746. }
  4747. static void beiscsi_hw_health_check(unsigned long ptr)
  4748. {
  4749. struct beiscsi_hba *phba;
  4750. phba = (struct beiscsi_hba *)ptr;
  4751. beiscsi_detect_ue(phba);
  4752. if (beiscsi_detect_ue(phba)) {
  4753. __beiscsi_log(phba, KERN_ERR,
  4754. "BM_%d : port in error: %lx\n", phba->state);
  4755. /* sessions are no longer valid, so first fail the sessions */
  4756. queue_work(phba->wq, &phba->sess_work);
  4757. /* detect UER supported */
  4758. if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
  4759. return;
  4760. /* modify this timer to check TPE */
  4761. phba->hw_check.function = beiscsi_hw_tpe_check;
  4762. }
  4763. mod_timer(&phba->hw_check,
  4764. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4765. }
  4766. /*
  4767. * beiscsi_enable_port()- Enables the disabled port.
  4768. * Only port resources freed in disable function are reallocated.
  4769. * This is called in HBA error handling path.
  4770. *
  4771. * @phba: Instance of driver private structure
  4772. *
  4773. **/
  4774. static int beiscsi_enable_port(struct beiscsi_hba *phba)
  4775. {
  4776. struct hwi_context_memory *phwi_context;
  4777. struct hwi_controller *phwi_ctrlr;
  4778. struct be_eq_obj *pbe_eq;
  4779. int ret, i;
  4780. if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4781. __beiscsi_log(phba, KERN_ERR,
  4782. "BM_%d : %s : port is online %lx\n",
  4783. __func__, phba->state);
  4784. return 0;
  4785. }
  4786. ret = beiscsi_init_sliport(phba);
  4787. if (ret)
  4788. return ret;
  4789. if (enable_msix)
  4790. find_num_cpus(phba);
  4791. else
  4792. phba->num_cpus = 1;
  4793. if (enable_msix) {
  4794. beiscsi_msix_enable(phba);
  4795. if (!phba->msix_enabled)
  4796. phba->num_cpus = 1;
  4797. }
  4798. beiscsi_get_params(phba);
  4799. /* Re-enable UER. If different TPE occurs then it is recoverable. */
  4800. beiscsi_set_uer_feature(phba);
  4801. phba->shost->max_id = phba->params.cxns_per_ctrl;
  4802. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4803. ret = hwi_init_controller(phba);
  4804. if (ret) {
  4805. __beiscsi_log(phba, KERN_ERR,
  4806. "BM_%d : init controller failed %d\n", ret);
  4807. goto disable_msix;
  4808. }
  4809. for (i = 0; i < MAX_MCC_CMD; i++) {
  4810. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  4811. phba->ctrl.mcc_tag[i] = i + 1;
  4812. phba->ctrl.mcc_tag_status[i + 1] = 0;
  4813. phba->ctrl.mcc_tag_available++;
  4814. }
  4815. phwi_ctrlr = phba->phwi_ctrlr;
  4816. phwi_context = phwi_ctrlr->phwi_ctxt;
  4817. for (i = 0; i < phba->num_cpus; i++) {
  4818. pbe_eq = &phwi_context->be_eq[i];
  4819. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  4820. }
  4821. i = (phba->msix_enabled) ? i : 0;
  4822. /* Work item for MCC handling */
  4823. pbe_eq = &phwi_context->be_eq[i];
  4824. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  4825. ret = beiscsi_init_irqs(phba);
  4826. if (ret < 0) {
  4827. __beiscsi_log(phba, KERN_ERR,
  4828. "BM_%d : setup IRQs failed %d\n", ret);
  4829. goto cleanup_port;
  4830. }
  4831. hwi_enable_intr(phba);
  4832. /* port operational: clear all error bits */
  4833. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  4834. __beiscsi_log(phba, KERN_INFO,
  4835. "BM_%d : port online: 0x%lx\n", phba->state);
  4836. /* start hw_check timer and eqd_update work */
  4837. schedule_delayed_work(&phba->eqd_update,
  4838. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4839. /**
  4840. * Timer function gets modified for TPE detection.
  4841. * Always reinit to do health check first.
  4842. */
  4843. phba->hw_check.function = beiscsi_hw_health_check;
  4844. mod_timer(&phba->hw_check,
  4845. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4846. return 0;
  4847. cleanup_port:
  4848. for (i = 0; i < phba->num_cpus; i++) {
  4849. pbe_eq = &phwi_context->be_eq[i];
  4850. irq_poll_disable(&pbe_eq->iopoll);
  4851. }
  4852. hwi_cleanup_port(phba);
  4853. disable_msix:
  4854. if (phba->msix_enabled)
  4855. pci_disable_msix(phba->pcidev);
  4856. return ret;
  4857. }
  4858. /*
  4859. * beiscsi_disable_port()- Disable port and cleanup driver resources.
  4860. * This is called in HBA error handling and driver removal.
  4861. * @phba: Instance Priv structure
  4862. * @unload: indicate driver is unloading
  4863. *
  4864. * Free the OS and HW resources held by the driver
  4865. **/
  4866. static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
  4867. {
  4868. struct hwi_context_memory *phwi_context;
  4869. struct hwi_controller *phwi_ctrlr;
  4870. struct be_eq_obj *pbe_eq;
  4871. unsigned int i, msix_vec;
  4872. if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
  4873. return;
  4874. phwi_ctrlr = phba->phwi_ctrlr;
  4875. phwi_context = phwi_ctrlr->phwi_ctxt;
  4876. hwi_disable_intr(phba);
  4877. if (phba->msix_enabled) {
  4878. for (i = 0; i <= phba->num_cpus; i++) {
  4879. msix_vec = phba->msix_entries[i].vector;
  4880. free_irq(msix_vec, &phwi_context->be_eq[i]);
  4881. kfree(phba->msi_name[i]);
  4882. }
  4883. } else
  4884. if (phba->pcidev->irq)
  4885. free_irq(phba->pcidev->irq, phba);
  4886. pci_disable_msix(phba->pcidev);
  4887. for (i = 0; i < phba->num_cpus; i++) {
  4888. pbe_eq = &phwi_context->be_eq[i];
  4889. irq_poll_disable(&pbe_eq->iopoll);
  4890. }
  4891. cancel_delayed_work_sync(&phba->eqd_update);
  4892. cancel_work_sync(&phba->boot_work);
  4893. /* WQ might be running cancel queued mcc_work if we are not exiting */
  4894. if (!unload && beiscsi_hba_in_error(phba)) {
  4895. pbe_eq = &phwi_context->be_eq[i];
  4896. cancel_work_sync(&pbe_eq->mcc_work);
  4897. }
  4898. hwi_cleanup_port(phba);
  4899. }
  4900. static void beiscsi_sess_work(struct work_struct *work)
  4901. {
  4902. struct beiscsi_hba *phba;
  4903. phba = container_of(work, struct beiscsi_hba, sess_work);
  4904. /*
  4905. * This work gets scheduled only in case of HBA error.
  4906. * Old sessions are gone so need to be re-established.
  4907. * iscsi_session_failure needs process context hence this work.
  4908. */
  4909. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4910. }
  4911. static void beiscsi_recover_port(struct work_struct *work)
  4912. {
  4913. struct beiscsi_hba *phba;
  4914. phba = container_of(work, struct beiscsi_hba, recover_port.work);
  4915. beiscsi_disable_port(phba, 0);
  4916. beiscsi_enable_port(phba);
  4917. }
  4918. static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
  4919. pci_channel_state_t state)
  4920. {
  4921. struct beiscsi_hba *phba = NULL;
  4922. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4923. set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
  4924. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4925. "BM_%d : EEH error detected\n");
  4926. /* first stop UE detection when PCI error detected */
  4927. del_timer_sync(&phba->hw_check);
  4928. cancel_delayed_work_sync(&phba->recover_port);
  4929. /* sessions are no longer valid, so first fail the sessions */
  4930. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4931. beiscsi_disable_port(phba, 0);
  4932. if (state == pci_channel_io_perm_failure) {
  4933. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4934. "BM_%d : EEH : State PERM Failure");
  4935. return PCI_ERS_RESULT_DISCONNECT;
  4936. }
  4937. pci_disable_device(pdev);
  4938. /* The error could cause the FW to trigger a flash debug dump.
  4939. * Resetting the card while flash dump is in progress
  4940. * can cause it not to recover; wait for it to finish.
  4941. * Wait only for first function as it is needed only once per
  4942. * adapter.
  4943. **/
  4944. if (pdev->devfn == 0)
  4945. ssleep(30);
  4946. return PCI_ERS_RESULT_NEED_RESET;
  4947. }
  4948. static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
  4949. {
  4950. struct beiscsi_hba *phba = NULL;
  4951. int status = 0;
  4952. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4953. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4954. "BM_%d : EEH Reset\n");
  4955. status = pci_enable_device(pdev);
  4956. if (status)
  4957. return PCI_ERS_RESULT_DISCONNECT;
  4958. pci_set_master(pdev);
  4959. pci_set_power_state(pdev, PCI_D0);
  4960. pci_restore_state(pdev);
  4961. status = beiscsi_check_fw_rdy(phba);
  4962. if (status) {
  4963. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4964. "BM_%d : EEH Reset Completed\n");
  4965. } else {
  4966. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4967. "BM_%d : EEH Reset Completion Failure\n");
  4968. return PCI_ERS_RESULT_DISCONNECT;
  4969. }
  4970. pci_cleanup_aer_uncorrect_error_status(pdev);
  4971. return PCI_ERS_RESULT_RECOVERED;
  4972. }
  4973. static void beiscsi_eeh_resume(struct pci_dev *pdev)
  4974. {
  4975. struct beiscsi_hba *phba;
  4976. int ret;
  4977. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4978. pci_save_state(pdev);
  4979. ret = beiscsi_enable_port(phba);
  4980. if (ret)
  4981. __beiscsi_log(phba, KERN_ERR,
  4982. "BM_%d : AER EEH resume failed\n");
  4983. }
  4984. static int beiscsi_dev_probe(struct pci_dev *pcidev,
  4985. const struct pci_device_id *id)
  4986. {
  4987. struct beiscsi_hba *phba = NULL;
  4988. struct hwi_controller *phwi_ctrlr;
  4989. struct hwi_context_memory *phwi_context;
  4990. struct be_eq_obj *pbe_eq;
  4991. unsigned int s_handle;
  4992. int ret, i;
  4993. ret = beiscsi_enable_pci(pcidev);
  4994. if (ret < 0) {
  4995. dev_err(&pcidev->dev,
  4996. "beiscsi_dev_probe - Failed to enable pci device\n");
  4997. return ret;
  4998. }
  4999. phba = beiscsi_hba_alloc(pcidev);
  5000. if (!phba) {
  5001. dev_err(&pcidev->dev,
  5002. "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
  5003. ret = -ENOMEM;
  5004. goto disable_pci;
  5005. }
  5006. /* Enable EEH reporting */
  5007. ret = pci_enable_pcie_error_reporting(pcidev);
  5008. if (ret)
  5009. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  5010. "BM_%d : PCIe Error Reporting "
  5011. "Enabling Failed\n");
  5012. pci_save_state(pcidev);
  5013. /* Initialize Driver configuration Paramters */
  5014. beiscsi_hba_attrs_init(phba);
  5015. phba->mac_addr_set = false;
  5016. switch (pcidev->device) {
  5017. case BE_DEVICE_ID1:
  5018. case OC_DEVICE_ID1:
  5019. case OC_DEVICE_ID2:
  5020. phba->generation = BE_GEN2;
  5021. phba->iotask_fn = beiscsi_iotask;
  5022. break;
  5023. case BE_DEVICE_ID2:
  5024. case OC_DEVICE_ID3:
  5025. phba->generation = BE_GEN3;
  5026. phba->iotask_fn = beiscsi_iotask;
  5027. break;
  5028. case OC_SKH_ID1:
  5029. phba->generation = BE_GEN4;
  5030. phba->iotask_fn = beiscsi_iotask_v2;
  5031. break;
  5032. default:
  5033. phba->generation = 0;
  5034. }
  5035. ret = be_ctrl_init(phba, pcidev);
  5036. if (ret) {
  5037. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5038. "BM_%d : be_ctrl_init failed\n");
  5039. goto hba_free;
  5040. }
  5041. ret = beiscsi_init_sliport(phba);
  5042. if (ret)
  5043. goto hba_free;
  5044. spin_lock_init(&phba->io_sgl_lock);
  5045. spin_lock_init(&phba->mgmt_sgl_lock);
  5046. spin_lock_init(&phba->async_pdu_lock);
  5047. ret = beiscsi_get_fw_config(&phba->ctrl, phba);
  5048. if (ret != 0) {
  5049. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5050. "BM_%d : Error getting fw config\n");
  5051. goto free_port;
  5052. }
  5053. beiscsi_get_port_name(&phba->ctrl, phba);
  5054. beiscsi_get_params(phba);
  5055. beiscsi_set_uer_feature(phba);
  5056. if (enable_msix)
  5057. find_num_cpus(phba);
  5058. else
  5059. phba->num_cpus = 1;
  5060. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  5061. "BM_%d : num_cpus = %d\n",
  5062. phba->num_cpus);
  5063. if (enable_msix) {
  5064. beiscsi_msix_enable(phba);
  5065. if (!phba->msix_enabled)
  5066. phba->num_cpus = 1;
  5067. }
  5068. phba->shost->max_id = phba->params.cxns_per_ctrl;
  5069. phba->shost->can_queue = phba->params.ios_per_ctrl;
  5070. ret = beiscsi_init_port(phba);
  5071. if (ret < 0) {
  5072. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5073. "BM_%d : beiscsi_dev_probe-"
  5074. "Failed in beiscsi_init_port\n");
  5075. goto free_port;
  5076. }
  5077. for (i = 0; i < MAX_MCC_CMD; i++) {
  5078. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  5079. phba->ctrl.mcc_tag[i] = i + 1;
  5080. phba->ctrl.mcc_tag_status[i + 1] = 0;
  5081. phba->ctrl.mcc_tag_available++;
  5082. memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
  5083. sizeof(struct be_dma_mem));
  5084. }
  5085. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  5086. snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
  5087. phba->shost->host_no);
  5088. phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
  5089. if (!phba->wq) {
  5090. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5091. "BM_%d : beiscsi_dev_probe-"
  5092. "Failed to allocate work queue\n");
  5093. ret = -ENOMEM;
  5094. goto free_twq;
  5095. }
  5096. INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
  5097. phwi_ctrlr = phba->phwi_ctrlr;
  5098. phwi_context = phwi_ctrlr->phwi_ctxt;
  5099. for (i = 0; i < phba->num_cpus; i++) {
  5100. pbe_eq = &phwi_context->be_eq[i];
  5101. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  5102. }
  5103. i = (phba->msix_enabled) ? i : 0;
  5104. /* Work item for MCC handling */
  5105. pbe_eq = &phwi_context->be_eq[i];
  5106. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  5107. ret = beiscsi_init_irqs(phba);
  5108. if (ret < 0) {
  5109. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5110. "BM_%d : beiscsi_dev_probe-"
  5111. "Failed to beiscsi_init_irqs\n");
  5112. goto free_blkenbld;
  5113. }
  5114. hwi_enable_intr(phba);
  5115. ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
  5116. if (ret)
  5117. goto free_blkenbld;
  5118. /* set online bit after port is operational */
  5119. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  5120. __beiscsi_log(phba, KERN_INFO,
  5121. "BM_%d : port online: 0x%lx\n", phba->state);
  5122. INIT_WORK(&phba->boot_work, beiscsi_boot_work);
  5123. ret = beiscsi_boot_get_shandle(phba, &s_handle);
  5124. if (ret > 0) {
  5125. beiscsi_start_boot_work(phba, s_handle);
  5126. /**
  5127. * Set this bit after starting the work to let
  5128. * probe handle it first.
  5129. * ASYNC event can too schedule this work.
  5130. */
  5131. set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
  5132. }
  5133. beiscsi_iface_create_default(phba);
  5134. schedule_delayed_work(&phba->eqd_update,
  5135. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  5136. INIT_WORK(&phba->sess_work, beiscsi_sess_work);
  5137. INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
  5138. /**
  5139. * Start UE detection here. UE before this will cause stall in probe
  5140. * and eventually fail the probe.
  5141. */
  5142. init_timer(&phba->hw_check);
  5143. phba->hw_check.function = beiscsi_hw_health_check;
  5144. phba->hw_check.data = (unsigned long)phba;
  5145. mod_timer(&phba->hw_check,
  5146. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  5147. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  5148. "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
  5149. return 0;
  5150. free_blkenbld:
  5151. destroy_workqueue(phba->wq);
  5152. for (i = 0; i < phba->num_cpus; i++) {
  5153. pbe_eq = &phwi_context->be_eq[i];
  5154. irq_poll_disable(&pbe_eq->iopoll);
  5155. }
  5156. free_twq:
  5157. hwi_cleanup_port(phba);
  5158. beiscsi_cleanup_port(phba);
  5159. beiscsi_free_mem(phba);
  5160. free_port:
  5161. pci_free_consistent(phba->pcidev,
  5162. phba->ctrl.mbox_mem_alloced.size,
  5163. phba->ctrl.mbox_mem_alloced.va,
  5164. phba->ctrl.mbox_mem_alloced.dma);
  5165. beiscsi_unmap_pci_function(phba);
  5166. hba_free:
  5167. if (phba->msix_enabled)
  5168. pci_disable_msix(phba->pcidev);
  5169. pci_dev_put(phba->pcidev);
  5170. iscsi_host_free(phba->shost);
  5171. pci_set_drvdata(pcidev, NULL);
  5172. disable_pci:
  5173. pci_release_regions(pcidev);
  5174. pci_disable_device(pcidev);
  5175. return ret;
  5176. }
  5177. static void beiscsi_remove(struct pci_dev *pcidev)
  5178. {
  5179. struct beiscsi_hba *phba = NULL;
  5180. phba = pci_get_drvdata(pcidev);
  5181. if (!phba) {
  5182. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  5183. return;
  5184. }
  5185. /* first stop UE detection before unloading */
  5186. del_timer_sync(&phba->hw_check);
  5187. cancel_delayed_work_sync(&phba->recover_port);
  5188. cancel_work_sync(&phba->sess_work);
  5189. beiscsi_iface_destroy_default(phba);
  5190. iscsi_host_remove(phba->shost);
  5191. beiscsi_disable_port(phba, 1);
  5192. /* after cancelling boot_work */
  5193. iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
  5194. /* free all resources */
  5195. destroy_workqueue(phba->wq);
  5196. beiscsi_cleanup_port(phba);
  5197. beiscsi_free_mem(phba);
  5198. /* ctrl uninit */
  5199. beiscsi_unmap_pci_function(phba);
  5200. pci_free_consistent(phba->pcidev,
  5201. phba->ctrl.mbox_mem_alloced.size,
  5202. phba->ctrl.mbox_mem_alloced.va,
  5203. phba->ctrl.mbox_mem_alloced.dma);
  5204. pci_dev_put(phba->pcidev);
  5205. iscsi_host_free(phba->shost);
  5206. pci_disable_pcie_error_reporting(pcidev);
  5207. pci_set_drvdata(pcidev, NULL);
  5208. pci_release_regions(pcidev);
  5209. pci_disable_device(pcidev);
  5210. }
  5211. static struct pci_error_handlers beiscsi_eeh_handlers = {
  5212. .error_detected = beiscsi_eeh_err_detected,
  5213. .slot_reset = beiscsi_eeh_reset,
  5214. .resume = beiscsi_eeh_resume,
  5215. };
  5216. struct iscsi_transport beiscsi_iscsi_transport = {
  5217. .owner = THIS_MODULE,
  5218. .name = DRV_NAME,
  5219. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  5220. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  5221. .create_session = beiscsi_session_create,
  5222. .destroy_session = beiscsi_session_destroy,
  5223. .create_conn = beiscsi_conn_create,
  5224. .bind_conn = beiscsi_conn_bind,
  5225. .destroy_conn = iscsi_conn_teardown,
  5226. .attr_is_visible = beiscsi_attr_is_visible,
  5227. .set_iface_param = beiscsi_iface_set_param,
  5228. .get_iface_param = beiscsi_iface_get_param,
  5229. .set_param = beiscsi_set_param,
  5230. .get_conn_param = iscsi_conn_get_param,
  5231. .get_session_param = iscsi_session_get_param,
  5232. .get_host_param = beiscsi_get_host_param,
  5233. .start_conn = beiscsi_conn_start,
  5234. .stop_conn = iscsi_conn_stop,
  5235. .send_pdu = iscsi_conn_send_pdu,
  5236. .xmit_task = beiscsi_task_xmit,
  5237. .cleanup_task = beiscsi_cleanup_task,
  5238. .alloc_pdu = beiscsi_alloc_pdu,
  5239. .parse_pdu_itt = beiscsi_parse_pdu,
  5240. .get_stats = beiscsi_conn_get_stats,
  5241. .get_ep_param = beiscsi_ep_get_param,
  5242. .ep_connect = beiscsi_ep_connect,
  5243. .ep_poll = beiscsi_ep_poll,
  5244. .ep_disconnect = beiscsi_ep_disconnect,
  5245. .session_recovery_timedout = iscsi_session_recovery_timedout,
  5246. .bsg_request = beiscsi_bsg_request,
  5247. };
  5248. static struct pci_driver beiscsi_pci_driver = {
  5249. .name = DRV_NAME,
  5250. .probe = beiscsi_dev_probe,
  5251. .remove = beiscsi_remove,
  5252. .id_table = beiscsi_pci_id_table,
  5253. .err_handler = &beiscsi_eeh_handlers
  5254. };
  5255. static int __init beiscsi_module_init(void)
  5256. {
  5257. int ret;
  5258. beiscsi_scsi_transport =
  5259. iscsi_register_transport(&beiscsi_iscsi_transport);
  5260. if (!beiscsi_scsi_transport) {
  5261. printk(KERN_ERR
  5262. "beiscsi_module_init - Unable to register beiscsi transport.\n");
  5263. return -ENOMEM;
  5264. }
  5265. printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
  5266. &beiscsi_iscsi_transport);
  5267. ret = pci_register_driver(&beiscsi_pci_driver);
  5268. if (ret) {
  5269. printk(KERN_ERR
  5270. "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
  5271. goto unregister_iscsi_transport;
  5272. }
  5273. return 0;
  5274. unregister_iscsi_transport:
  5275. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5276. return ret;
  5277. }
  5278. static void __exit beiscsi_module_exit(void)
  5279. {
  5280. pci_unregister_driver(&beiscsi_pci_driver);
  5281. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5282. }
  5283. module_init(beiscsi_module_init);
  5284. module_exit(beiscsi_module_exit);