be_main.c 163 KB

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