be_main.c 160 KB

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