be_main.c 164 KB

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