be_main.c 164 KB

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