megaraid_sas_base.c 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2003-2012 LSI Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE: megaraid_sas_base.c
  21. * Version : 06.700.06.00-rc1
  22. *
  23. * Authors: LSI Corporation
  24. * Sreenivas Bagalkote
  25. * Sumant Patro
  26. * Bo Yang
  27. * Adam Radford <linuxraid@lsi.com>
  28. *
  29. * Send feedback to: <megaraidlinux@lsi.com>
  30. *
  31. * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  32. * ATTN: Linuxraid
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/pci.h>
  37. #include <linux/list.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/delay.h>
  43. #include <linux/uio.h>
  44. #include <linux/slab.h>
  45. #include <asm/uaccess.h>
  46. #include <linux/fs.h>
  47. #include <linux/compat.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/mutex.h>
  50. #include <linux/poll.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_device.h>
  54. #include <scsi/scsi_host.h>
  55. #include <scsi/scsi_tcq.h>
  56. #include "megaraid_sas_fusion.h"
  57. #include "megaraid_sas.h"
  58. /*
  59. * Number of sectors per IO command
  60. * Will be set in megasas_init_mfi if user does not provide
  61. */
  62. static unsigned int max_sectors;
  63. module_param_named(max_sectors, max_sectors, int, 0);
  64. MODULE_PARM_DESC(max_sectors,
  65. "Maximum number of sectors per IO command");
  66. static int msix_disable;
  67. module_param(msix_disable, int, S_IRUGO);
  68. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  69. static unsigned int msix_vectors;
  70. module_param(msix_vectors, int, S_IRUGO);
  71. MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  72. static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  73. module_param(throttlequeuedepth, int, S_IRUGO);
  74. MODULE_PARM_DESC(throttlequeuedepth,
  75. "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  76. int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  77. module_param(resetwaittime, int, S_IRUGO);
  78. MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
  79. "before resetting adapter. Default: 180");
  80. MODULE_LICENSE("GPL");
  81. MODULE_VERSION(MEGASAS_VERSION);
  82. MODULE_AUTHOR("megaraidlinux@lsi.com");
  83. MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
  84. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  85. static int megasas_get_pd_list(struct megasas_instance *instance);
  86. static int megasas_ld_list_query(struct megasas_instance *instance,
  87. u8 query_type);
  88. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  89. static int megasas_register_aen(struct megasas_instance *instance,
  90. u32 seq_num, u32 class_locale_word);
  91. /*
  92. * PCI ID table for all supported controllers
  93. */
  94. static struct pci_device_id megasas_pci_table[] = {
  95. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  96. /* xscale IOP */
  97. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  98. /* ppc IOP */
  99. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  100. /* ppc IOP */
  101. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  102. /* gen2*/
  103. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  104. /* gen2*/
  105. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  106. /* skinny*/
  107. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  108. /* skinny*/
  109. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  110. /* xscale IOP, vega */
  111. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  112. /* xscale IOP */
  113. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  114. /* Fusion */
  115. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
  116. /* Invader */
  117. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
  118. /* Fury */
  119. {}
  120. };
  121. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  122. static int megasas_mgmt_majorno;
  123. static struct megasas_mgmt_info megasas_mgmt_info;
  124. static struct fasync_struct *megasas_async_queue;
  125. static DEFINE_MUTEX(megasas_async_queue_mutex);
  126. static int megasas_poll_wait_aen;
  127. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  128. static u32 support_poll_for_event;
  129. u32 megasas_dbg_lvl;
  130. static u32 support_device_change;
  131. /* define lock for aen poll */
  132. spinlock_t poll_aen_lock;
  133. void
  134. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  135. u8 alt_status);
  136. static u32
  137. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  138. static int
  139. megasas_adp_reset_gen2(struct megasas_instance *instance,
  140. struct megasas_register_set __iomem *reg_set);
  141. static irqreturn_t megasas_isr(int irq, void *devp);
  142. static u32
  143. megasas_init_adapter_mfi(struct megasas_instance *instance);
  144. u32
  145. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  146. struct scsi_cmnd *scmd);
  147. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  148. void
  149. megasas_release_fusion(struct megasas_instance *instance);
  150. int
  151. megasas_ioc_init_fusion(struct megasas_instance *instance);
  152. void
  153. megasas_free_cmds_fusion(struct megasas_instance *instance);
  154. u8
  155. megasas_get_map_info(struct megasas_instance *instance);
  156. int
  157. megasas_sync_map_info(struct megasas_instance *instance);
  158. int
  159. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
  160. void megasas_reset_reply_desc(struct megasas_instance *instance);
  161. int megasas_reset_fusion(struct Scsi_Host *shost);
  162. void megasas_fusion_ocr_wq(struct work_struct *work);
  163. void
  164. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  165. {
  166. instance->instancet->fire_cmd(instance,
  167. cmd->frame_phys_addr, 0, instance->reg_set);
  168. }
  169. /**
  170. * megasas_get_cmd - Get a command from the free pool
  171. * @instance: Adapter soft state
  172. *
  173. * Returns a free command from the pool
  174. */
  175. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  176. *instance)
  177. {
  178. unsigned long flags;
  179. struct megasas_cmd *cmd = NULL;
  180. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  181. if (!list_empty(&instance->cmd_pool)) {
  182. cmd = list_entry((&instance->cmd_pool)->next,
  183. struct megasas_cmd, list);
  184. list_del_init(&cmd->list);
  185. } else {
  186. printk(KERN_ERR "megasas: Command pool empty!\n");
  187. }
  188. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  189. return cmd;
  190. }
  191. /**
  192. * megasas_return_cmd - Return a cmd to free command pool
  193. * @instance: Adapter soft state
  194. * @cmd: Command packet to be returned to free command pool
  195. */
  196. inline void
  197. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  201. cmd->scmd = NULL;
  202. cmd->frame_count = 0;
  203. if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
  204. (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
  205. (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
  206. (reset_devices))
  207. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  208. list_add_tail(&cmd->list, &instance->cmd_pool);
  209. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  210. }
  211. /**
  212. * The following functions are defined for xscale
  213. * (deviceid : 1064R, PERC5) controllers
  214. */
  215. /**
  216. * megasas_enable_intr_xscale - Enables interrupts
  217. * @regs: MFI register set
  218. */
  219. static inline void
  220. megasas_enable_intr_xscale(struct megasas_instance *instance)
  221. {
  222. struct megasas_register_set __iomem *regs;
  223. regs = instance->reg_set;
  224. writel(0, &(regs)->outbound_intr_mask);
  225. /* Dummy readl to force pci flush */
  226. readl(&regs->outbound_intr_mask);
  227. }
  228. /**
  229. * megasas_disable_intr_xscale -Disables interrupt
  230. * @regs: MFI register set
  231. */
  232. static inline void
  233. megasas_disable_intr_xscale(struct megasas_instance *instance)
  234. {
  235. struct megasas_register_set __iomem *regs;
  236. u32 mask = 0x1f;
  237. regs = instance->reg_set;
  238. writel(mask, &regs->outbound_intr_mask);
  239. /* Dummy readl to force pci flush */
  240. readl(&regs->outbound_intr_mask);
  241. }
  242. /**
  243. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  244. * @regs: MFI register set
  245. */
  246. static u32
  247. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  248. {
  249. return readl(&(regs)->outbound_msg_0);
  250. }
  251. /**
  252. * megasas_clear_interrupt_xscale - Check & clear interrupt
  253. * @regs: MFI register set
  254. */
  255. static int
  256. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  257. {
  258. u32 status;
  259. u32 mfiStatus = 0;
  260. /*
  261. * Check if it is our interrupt
  262. */
  263. status = readl(&regs->outbound_intr_status);
  264. if (status & MFI_OB_INTR_STATUS_MASK)
  265. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  266. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  267. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  268. /*
  269. * Clear the interrupt by writing back the same value
  270. */
  271. if (mfiStatus)
  272. writel(status, &regs->outbound_intr_status);
  273. /* Dummy readl to force pci flush */
  274. readl(&regs->outbound_intr_status);
  275. return mfiStatus;
  276. }
  277. /**
  278. * megasas_fire_cmd_xscale - Sends command to the FW
  279. * @frame_phys_addr : Physical address of cmd
  280. * @frame_count : Number of frames for the command
  281. * @regs : MFI register set
  282. */
  283. static inline void
  284. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  285. dma_addr_t frame_phys_addr,
  286. u32 frame_count,
  287. struct megasas_register_set __iomem *regs)
  288. {
  289. unsigned long flags;
  290. spin_lock_irqsave(&instance->hba_lock, flags);
  291. writel((frame_phys_addr >> 3)|(frame_count),
  292. &(regs)->inbound_queue_port);
  293. spin_unlock_irqrestore(&instance->hba_lock, flags);
  294. }
  295. /**
  296. * megasas_adp_reset_xscale - For controller reset
  297. * @regs: MFI register set
  298. */
  299. static int
  300. megasas_adp_reset_xscale(struct megasas_instance *instance,
  301. struct megasas_register_set __iomem *regs)
  302. {
  303. u32 i;
  304. u32 pcidata;
  305. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  306. for (i = 0; i < 3; i++)
  307. msleep(1000); /* sleep for 3 secs */
  308. pcidata = 0;
  309. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  310. printk(KERN_NOTICE "pcidata = %x\n", pcidata);
  311. if (pcidata & 0x2) {
  312. printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
  313. pcidata &= ~0x2;
  314. pci_write_config_dword(instance->pdev,
  315. MFI_1068_PCSR_OFFSET, pcidata);
  316. for (i = 0; i < 2; i++)
  317. msleep(1000); /* need to wait 2 secs again */
  318. pcidata = 0;
  319. pci_read_config_dword(instance->pdev,
  320. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  321. printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
  322. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  323. printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
  324. pcidata = 0;
  325. pci_write_config_dword(instance->pdev,
  326. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  327. }
  328. }
  329. return 0;
  330. }
  331. /**
  332. * megasas_check_reset_xscale - For controller reset check
  333. * @regs: MFI register set
  334. */
  335. static int
  336. megasas_check_reset_xscale(struct megasas_instance *instance,
  337. struct megasas_register_set __iomem *regs)
  338. {
  339. if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
  340. (le32_to_cpu(*instance->consumer) ==
  341. MEGASAS_ADPRESET_INPROG_SIGN))
  342. return 1;
  343. return 0;
  344. }
  345. static struct megasas_instance_template megasas_instance_template_xscale = {
  346. .fire_cmd = megasas_fire_cmd_xscale,
  347. .enable_intr = megasas_enable_intr_xscale,
  348. .disable_intr = megasas_disable_intr_xscale,
  349. .clear_intr = megasas_clear_intr_xscale,
  350. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  351. .adp_reset = megasas_adp_reset_xscale,
  352. .check_reset = megasas_check_reset_xscale,
  353. .service_isr = megasas_isr,
  354. .tasklet = megasas_complete_cmd_dpc,
  355. .init_adapter = megasas_init_adapter_mfi,
  356. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  357. .issue_dcmd = megasas_issue_dcmd,
  358. };
  359. /**
  360. * This is the end of set of functions & definitions specific
  361. * to xscale (deviceid : 1064R, PERC5) controllers
  362. */
  363. /**
  364. * The following functions are defined for ppc (deviceid : 0x60)
  365. * controllers
  366. */
  367. /**
  368. * megasas_enable_intr_ppc - Enables interrupts
  369. * @regs: MFI register set
  370. */
  371. static inline void
  372. megasas_enable_intr_ppc(struct megasas_instance *instance)
  373. {
  374. struct megasas_register_set __iomem *regs;
  375. regs = instance->reg_set;
  376. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  377. writel(~0x80000000, &(regs)->outbound_intr_mask);
  378. /* Dummy readl to force pci flush */
  379. readl(&regs->outbound_intr_mask);
  380. }
  381. /**
  382. * megasas_disable_intr_ppc - Disable interrupt
  383. * @regs: MFI register set
  384. */
  385. static inline void
  386. megasas_disable_intr_ppc(struct megasas_instance *instance)
  387. {
  388. struct megasas_register_set __iomem *regs;
  389. u32 mask = 0xFFFFFFFF;
  390. regs = instance->reg_set;
  391. writel(mask, &regs->outbound_intr_mask);
  392. /* Dummy readl to force pci flush */
  393. readl(&regs->outbound_intr_mask);
  394. }
  395. /**
  396. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  397. * @regs: MFI register set
  398. */
  399. static u32
  400. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  401. {
  402. return readl(&(regs)->outbound_scratch_pad);
  403. }
  404. /**
  405. * megasas_clear_interrupt_ppc - Check & clear interrupt
  406. * @regs: MFI register set
  407. */
  408. static int
  409. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  410. {
  411. u32 status, mfiStatus = 0;
  412. /*
  413. * Check if it is our interrupt
  414. */
  415. status = readl(&regs->outbound_intr_status);
  416. if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
  417. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  418. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
  419. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  420. /*
  421. * Clear the interrupt by writing back the same value
  422. */
  423. writel(status, &regs->outbound_doorbell_clear);
  424. /* Dummy readl to force pci flush */
  425. readl(&regs->outbound_doorbell_clear);
  426. return mfiStatus;
  427. }
  428. /**
  429. * megasas_fire_cmd_ppc - Sends command to the FW
  430. * @frame_phys_addr : Physical address of cmd
  431. * @frame_count : Number of frames for the command
  432. * @regs : MFI register set
  433. */
  434. static inline void
  435. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  436. dma_addr_t frame_phys_addr,
  437. u32 frame_count,
  438. struct megasas_register_set __iomem *regs)
  439. {
  440. unsigned long flags;
  441. spin_lock_irqsave(&instance->hba_lock, flags);
  442. writel((frame_phys_addr | (frame_count<<1))|1,
  443. &(regs)->inbound_queue_port);
  444. spin_unlock_irqrestore(&instance->hba_lock, flags);
  445. }
  446. /**
  447. * megasas_check_reset_ppc - For controller reset check
  448. * @regs: MFI register set
  449. */
  450. static int
  451. megasas_check_reset_ppc(struct megasas_instance *instance,
  452. struct megasas_register_set __iomem *regs)
  453. {
  454. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
  455. return 1;
  456. return 0;
  457. }
  458. static struct megasas_instance_template megasas_instance_template_ppc = {
  459. .fire_cmd = megasas_fire_cmd_ppc,
  460. .enable_intr = megasas_enable_intr_ppc,
  461. .disable_intr = megasas_disable_intr_ppc,
  462. .clear_intr = megasas_clear_intr_ppc,
  463. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  464. .adp_reset = megasas_adp_reset_xscale,
  465. .check_reset = megasas_check_reset_ppc,
  466. .service_isr = megasas_isr,
  467. .tasklet = megasas_complete_cmd_dpc,
  468. .init_adapter = megasas_init_adapter_mfi,
  469. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  470. .issue_dcmd = megasas_issue_dcmd,
  471. };
  472. /**
  473. * megasas_enable_intr_skinny - Enables interrupts
  474. * @regs: MFI register set
  475. */
  476. static inline void
  477. megasas_enable_intr_skinny(struct megasas_instance *instance)
  478. {
  479. struct megasas_register_set __iomem *regs;
  480. regs = instance->reg_set;
  481. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  482. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  483. /* Dummy readl to force pci flush */
  484. readl(&regs->outbound_intr_mask);
  485. }
  486. /**
  487. * megasas_disable_intr_skinny - Disables interrupt
  488. * @regs: MFI register set
  489. */
  490. static inline void
  491. megasas_disable_intr_skinny(struct megasas_instance *instance)
  492. {
  493. struct megasas_register_set __iomem *regs;
  494. u32 mask = 0xFFFFFFFF;
  495. regs = instance->reg_set;
  496. writel(mask, &regs->outbound_intr_mask);
  497. /* Dummy readl to force pci flush */
  498. readl(&regs->outbound_intr_mask);
  499. }
  500. /**
  501. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  502. * @regs: MFI register set
  503. */
  504. static u32
  505. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  506. {
  507. return readl(&(regs)->outbound_scratch_pad);
  508. }
  509. /**
  510. * megasas_clear_interrupt_skinny - Check & clear interrupt
  511. * @regs: MFI register set
  512. */
  513. static int
  514. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  515. {
  516. u32 status;
  517. u32 mfiStatus = 0;
  518. /*
  519. * Check if it is our interrupt
  520. */
  521. status = readl(&regs->outbound_intr_status);
  522. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  523. return 0;
  524. }
  525. /*
  526. * Check if it is our interrupt
  527. */
  528. if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
  529. MFI_STATE_FAULT) {
  530. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  531. } else
  532. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  533. /*
  534. * Clear the interrupt by writing back the same value
  535. */
  536. writel(status, &regs->outbound_intr_status);
  537. /*
  538. * dummy read to flush PCI
  539. */
  540. readl(&regs->outbound_intr_status);
  541. return mfiStatus;
  542. }
  543. /**
  544. * megasas_fire_cmd_skinny - Sends command to the FW
  545. * @frame_phys_addr : Physical address of cmd
  546. * @frame_count : Number of frames for the command
  547. * @regs : MFI register set
  548. */
  549. static inline void
  550. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  551. dma_addr_t frame_phys_addr,
  552. u32 frame_count,
  553. struct megasas_register_set __iomem *regs)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&instance->hba_lock, flags);
  557. writel(upper_32_bits(frame_phys_addr),
  558. &(regs)->inbound_high_queue_port);
  559. writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
  560. &(regs)->inbound_low_queue_port);
  561. spin_unlock_irqrestore(&instance->hba_lock, flags);
  562. }
  563. /**
  564. * megasas_check_reset_skinny - For controller reset check
  565. * @regs: MFI register set
  566. */
  567. static int
  568. megasas_check_reset_skinny(struct megasas_instance *instance,
  569. struct megasas_register_set __iomem *regs)
  570. {
  571. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
  572. return 1;
  573. return 0;
  574. }
  575. static struct megasas_instance_template megasas_instance_template_skinny = {
  576. .fire_cmd = megasas_fire_cmd_skinny,
  577. .enable_intr = megasas_enable_intr_skinny,
  578. .disable_intr = megasas_disable_intr_skinny,
  579. .clear_intr = megasas_clear_intr_skinny,
  580. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  581. .adp_reset = megasas_adp_reset_gen2,
  582. .check_reset = megasas_check_reset_skinny,
  583. .service_isr = megasas_isr,
  584. .tasklet = megasas_complete_cmd_dpc,
  585. .init_adapter = megasas_init_adapter_mfi,
  586. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  587. .issue_dcmd = megasas_issue_dcmd,
  588. };
  589. /**
  590. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  591. * controllers
  592. */
  593. /**
  594. * megasas_enable_intr_gen2 - Enables interrupts
  595. * @regs: MFI register set
  596. */
  597. static inline void
  598. megasas_enable_intr_gen2(struct megasas_instance *instance)
  599. {
  600. struct megasas_register_set __iomem *regs;
  601. regs = instance->reg_set;
  602. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  603. /* write ~0x00000005 (4 & 1) to the intr mask*/
  604. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  605. /* Dummy readl to force pci flush */
  606. readl(&regs->outbound_intr_mask);
  607. }
  608. /**
  609. * megasas_disable_intr_gen2 - Disables interrupt
  610. * @regs: MFI register set
  611. */
  612. static inline void
  613. megasas_disable_intr_gen2(struct megasas_instance *instance)
  614. {
  615. struct megasas_register_set __iomem *regs;
  616. u32 mask = 0xFFFFFFFF;
  617. regs = instance->reg_set;
  618. writel(mask, &regs->outbound_intr_mask);
  619. /* Dummy readl to force pci flush */
  620. readl(&regs->outbound_intr_mask);
  621. }
  622. /**
  623. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  624. * @regs: MFI register set
  625. */
  626. static u32
  627. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  628. {
  629. return readl(&(regs)->outbound_scratch_pad);
  630. }
  631. /**
  632. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  633. * @regs: MFI register set
  634. */
  635. static int
  636. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  637. {
  638. u32 status;
  639. u32 mfiStatus = 0;
  640. /*
  641. * Check if it is our interrupt
  642. */
  643. status = readl(&regs->outbound_intr_status);
  644. if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
  645. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  646. }
  647. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  648. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  649. }
  650. /*
  651. * Clear the interrupt by writing back the same value
  652. */
  653. if (mfiStatus)
  654. writel(status, &regs->outbound_doorbell_clear);
  655. /* Dummy readl to force pci flush */
  656. readl(&regs->outbound_intr_status);
  657. return mfiStatus;
  658. }
  659. /**
  660. * megasas_fire_cmd_gen2 - Sends command to the FW
  661. * @frame_phys_addr : Physical address of cmd
  662. * @frame_count : Number of frames for the command
  663. * @regs : MFI register set
  664. */
  665. static inline void
  666. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  667. dma_addr_t frame_phys_addr,
  668. u32 frame_count,
  669. struct megasas_register_set __iomem *regs)
  670. {
  671. unsigned long flags;
  672. spin_lock_irqsave(&instance->hba_lock, flags);
  673. writel((frame_phys_addr | (frame_count<<1))|1,
  674. &(regs)->inbound_queue_port);
  675. spin_unlock_irqrestore(&instance->hba_lock, flags);
  676. }
  677. /**
  678. * megasas_adp_reset_gen2 - For controller reset
  679. * @regs: MFI register set
  680. */
  681. static int
  682. megasas_adp_reset_gen2(struct megasas_instance *instance,
  683. struct megasas_register_set __iomem *reg_set)
  684. {
  685. u32 retry = 0 ;
  686. u32 HostDiag;
  687. u32 *seq_offset = &reg_set->seq_offset;
  688. u32 *hostdiag_offset = &reg_set->host_diag;
  689. if (instance->instancet == &megasas_instance_template_skinny) {
  690. seq_offset = &reg_set->fusion_seq_offset;
  691. hostdiag_offset = &reg_set->fusion_host_diag;
  692. }
  693. writel(0, seq_offset);
  694. writel(4, seq_offset);
  695. writel(0xb, seq_offset);
  696. writel(2, seq_offset);
  697. writel(7, seq_offset);
  698. writel(0xd, seq_offset);
  699. msleep(1000);
  700. HostDiag = (u32)readl(hostdiag_offset);
  701. while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
  702. msleep(100);
  703. HostDiag = (u32)readl(hostdiag_offset);
  704. printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
  705. retry, HostDiag);
  706. if (retry++ >= 100)
  707. return 1;
  708. }
  709. printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  710. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  711. ssleep(10);
  712. HostDiag = (u32)readl(hostdiag_offset);
  713. while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
  714. msleep(100);
  715. HostDiag = (u32)readl(hostdiag_offset);
  716. printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
  717. retry, HostDiag);
  718. if (retry++ >= 1000)
  719. return 1;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * megasas_check_reset_gen2 - For controller reset check
  725. * @regs: MFI register set
  726. */
  727. static int
  728. megasas_check_reset_gen2(struct megasas_instance *instance,
  729. struct megasas_register_set __iomem *regs)
  730. {
  731. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  732. return 1;
  733. }
  734. return 0;
  735. }
  736. static struct megasas_instance_template megasas_instance_template_gen2 = {
  737. .fire_cmd = megasas_fire_cmd_gen2,
  738. .enable_intr = megasas_enable_intr_gen2,
  739. .disable_intr = megasas_disable_intr_gen2,
  740. .clear_intr = megasas_clear_intr_gen2,
  741. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  742. .adp_reset = megasas_adp_reset_gen2,
  743. .check_reset = megasas_check_reset_gen2,
  744. .service_isr = megasas_isr,
  745. .tasklet = megasas_complete_cmd_dpc,
  746. .init_adapter = megasas_init_adapter_mfi,
  747. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  748. .issue_dcmd = megasas_issue_dcmd,
  749. };
  750. /**
  751. * This is the end of set of functions & definitions
  752. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  753. */
  754. /*
  755. * Template added for TB (Fusion)
  756. */
  757. extern struct megasas_instance_template megasas_instance_template_fusion;
  758. /**
  759. * megasas_issue_polled - Issues a polling command
  760. * @instance: Adapter soft state
  761. * @cmd: Command packet to be issued
  762. *
  763. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  764. */
  765. int
  766. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  767. {
  768. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  769. frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
  770. frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  771. /*
  772. * Issue the frame using inbound queue port
  773. */
  774. instance->instancet->issue_dcmd(instance, cmd);
  775. /*
  776. * Wait for cmd_status to change
  777. */
  778. return wait_and_poll(instance, cmd);
  779. }
  780. /**
  781. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  782. * @instance: Adapter soft state
  783. * @cmd: Command to be issued
  784. *
  785. * This function waits on an event for the command to be returned from ISR.
  786. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  787. * Used to issue ioctl commands.
  788. */
  789. static int
  790. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  791. struct megasas_cmd *cmd)
  792. {
  793. cmd->cmd_status = ENODATA;
  794. instance->instancet->issue_dcmd(instance, cmd);
  795. wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
  796. return 0;
  797. }
  798. /**
  799. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  800. * @instance: Adapter soft state
  801. * @cmd_to_abort: Previously issued cmd to be aborted
  802. *
  803. * MFI firmware can abort previously issued AEN command (automatic event
  804. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  805. * cmd and waits for return status.
  806. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  807. */
  808. static int
  809. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  810. struct megasas_cmd *cmd_to_abort)
  811. {
  812. struct megasas_cmd *cmd;
  813. struct megasas_abort_frame *abort_fr;
  814. cmd = megasas_get_cmd(instance);
  815. if (!cmd)
  816. return -1;
  817. abort_fr = &cmd->frame->abort;
  818. /*
  819. * Prepare and issue the abort frame
  820. */
  821. abort_fr->cmd = MFI_CMD_ABORT;
  822. abort_fr->cmd_status = 0xFF;
  823. abort_fr->flags = cpu_to_le16(0);
  824. abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
  825. abort_fr->abort_mfi_phys_addr_lo =
  826. cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
  827. abort_fr->abort_mfi_phys_addr_hi =
  828. cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
  829. cmd->sync_cmd = 1;
  830. cmd->cmd_status = 0xFF;
  831. instance->instancet->issue_dcmd(instance, cmd);
  832. /*
  833. * Wait for this cmd to complete
  834. */
  835. wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
  836. cmd->sync_cmd = 0;
  837. megasas_return_cmd(instance, cmd);
  838. return 0;
  839. }
  840. /**
  841. * megasas_make_sgl32 - Prepares 32-bit SGL
  842. * @instance: Adapter soft state
  843. * @scp: SCSI command from the mid-layer
  844. * @mfi_sgl: SGL to be filled in
  845. *
  846. * If successful, this function returns the number of SG elements. Otherwise,
  847. * it returnes -1.
  848. */
  849. static int
  850. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  851. union megasas_sgl *mfi_sgl)
  852. {
  853. int i;
  854. int sge_count;
  855. struct scatterlist *os_sgl;
  856. sge_count = scsi_dma_map(scp);
  857. BUG_ON(sge_count < 0);
  858. if (sge_count) {
  859. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  860. mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  861. mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
  862. }
  863. }
  864. return sge_count;
  865. }
  866. /**
  867. * megasas_make_sgl64 - Prepares 64-bit SGL
  868. * @instance: Adapter soft state
  869. * @scp: SCSI command from the mid-layer
  870. * @mfi_sgl: SGL to be filled in
  871. *
  872. * If successful, this function returns the number of SG elements. Otherwise,
  873. * it returnes -1.
  874. */
  875. static int
  876. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  877. union megasas_sgl *mfi_sgl)
  878. {
  879. int i;
  880. int sge_count;
  881. struct scatterlist *os_sgl;
  882. sge_count = scsi_dma_map(scp);
  883. BUG_ON(sge_count < 0);
  884. if (sge_count) {
  885. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  886. mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  887. mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
  888. }
  889. }
  890. return sge_count;
  891. }
  892. /**
  893. * megasas_make_sgl_skinny - Prepares IEEE SGL
  894. * @instance: Adapter soft state
  895. * @scp: SCSI command from the mid-layer
  896. * @mfi_sgl: SGL to be filled in
  897. *
  898. * If successful, this function returns the number of SG elements. Otherwise,
  899. * it returnes -1.
  900. */
  901. static int
  902. megasas_make_sgl_skinny(struct megasas_instance *instance,
  903. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  904. {
  905. int i;
  906. int sge_count;
  907. struct scatterlist *os_sgl;
  908. sge_count = scsi_dma_map(scp);
  909. if (sge_count) {
  910. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  911. mfi_sgl->sge_skinny[i].length =
  912. cpu_to_le32(sg_dma_len(os_sgl));
  913. mfi_sgl->sge_skinny[i].phys_addr =
  914. cpu_to_le64(sg_dma_address(os_sgl));
  915. mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
  916. }
  917. }
  918. return sge_count;
  919. }
  920. /**
  921. * megasas_get_frame_count - Computes the number of frames
  922. * @frame_type : type of frame- io or pthru frame
  923. * @sge_count : number of sg elements
  924. *
  925. * Returns the number of frames required for numnber of sge's (sge_count)
  926. */
  927. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  928. u8 sge_count, u8 frame_type)
  929. {
  930. int num_cnt;
  931. int sge_bytes;
  932. u32 sge_sz;
  933. u32 frame_count=0;
  934. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  935. sizeof(struct megasas_sge32);
  936. if (instance->flag_ieee) {
  937. sge_sz = sizeof(struct megasas_sge_skinny);
  938. }
  939. /*
  940. * Main frame can contain 2 SGEs for 64-bit SGLs and
  941. * 3 SGEs for 32-bit SGLs for ldio &
  942. * 1 SGEs for 64-bit SGLs and
  943. * 2 SGEs for 32-bit SGLs for pthru frame
  944. */
  945. if (unlikely(frame_type == PTHRU_FRAME)) {
  946. if (instance->flag_ieee == 1) {
  947. num_cnt = sge_count - 1;
  948. } else if (IS_DMA64)
  949. num_cnt = sge_count - 1;
  950. else
  951. num_cnt = sge_count - 2;
  952. } else {
  953. if (instance->flag_ieee == 1) {
  954. num_cnt = sge_count - 1;
  955. } else if (IS_DMA64)
  956. num_cnt = sge_count - 2;
  957. else
  958. num_cnt = sge_count - 3;
  959. }
  960. if(num_cnt>0){
  961. sge_bytes = sge_sz * num_cnt;
  962. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  963. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  964. }
  965. /* Main frame */
  966. frame_count +=1;
  967. if (frame_count > 7)
  968. frame_count = 8;
  969. return frame_count;
  970. }
  971. /**
  972. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  973. * @instance: Adapter soft state
  974. * @scp: SCSI command
  975. * @cmd: Command to be prepared in
  976. *
  977. * This function prepares CDB commands. These are typcially pass-through
  978. * commands to the devices.
  979. */
  980. static int
  981. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  982. struct megasas_cmd *cmd)
  983. {
  984. u32 is_logical;
  985. u32 device_id;
  986. u16 flags = 0;
  987. struct megasas_pthru_frame *pthru;
  988. is_logical = MEGASAS_IS_LOGICAL(scp);
  989. device_id = MEGASAS_DEV_INDEX(instance, scp);
  990. pthru = (struct megasas_pthru_frame *)cmd->frame;
  991. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  992. flags = MFI_FRAME_DIR_WRITE;
  993. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  994. flags = MFI_FRAME_DIR_READ;
  995. else if (scp->sc_data_direction == PCI_DMA_NONE)
  996. flags = MFI_FRAME_DIR_NONE;
  997. if (instance->flag_ieee == 1) {
  998. flags |= MFI_FRAME_IEEE;
  999. }
  1000. /*
  1001. * Prepare the DCDB frame
  1002. */
  1003. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  1004. pthru->cmd_status = 0x0;
  1005. pthru->scsi_status = 0x0;
  1006. pthru->target_id = device_id;
  1007. pthru->lun = scp->device->lun;
  1008. pthru->cdb_len = scp->cmd_len;
  1009. pthru->timeout = 0;
  1010. pthru->pad_0 = 0;
  1011. pthru->flags = cpu_to_le16(flags);
  1012. pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
  1013. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1014. /*
  1015. * If the command is for the tape device, set the
  1016. * pthru timeout to the os layer timeout value.
  1017. */
  1018. if (scp->device->type == TYPE_TAPE) {
  1019. if ((scp->request->timeout / HZ) > 0xFFFF)
  1020. pthru->timeout = 0xFFFF;
  1021. else
  1022. pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
  1023. }
  1024. /*
  1025. * Construct SGL
  1026. */
  1027. if (instance->flag_ieee == 1) {
  1028. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1029. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  1030. &pthru->sgl);
  1031. } else if (IS_DMA64) {
  1032. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1033. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1034. &pthru->sgl);
  1035. } else
  1036. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1037. &pthru->sgl);
  1038. if (pthru->sge_count > instance->max_num_sge) {
  1039. printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
  1040. pthru->sge_count);
  1041. return 0;
  1042. }
  1043. /*
  1044. * Sense info specific
  1045. */
  1046. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1047. pthru->sense_buf_phys_addr_hi =
  1048. cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
  1049. pthru->sense_buf_phys_addr_lo =
  1050. cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
  1051. /*
  1052. * Compute the total number of frames this command consumes. FW uses
  1053. * this number to pull sufficient number of frames from host memory.
  1054. */
  1055. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1056. PTHRU_FRAME);
  1057. return cmd->frame_count;
  1058. }
  1059. /**
  1060. * megasas_build_ldio - Prepares IOs to logical devices
  1061. * @instance: Adapter soft state
  1062. * @scp: SCSI command
  1063. * @cmd: Command to be prepared
  1064. *
  1065. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1066. */
  1067. static int
  1068. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1069. struct megasas_cmd *cmd)
  1070. {
  1071. u32 device_id;
  1072. u8 sc = scp->cmnd[0];
  1073. u16 flags = 0;
  1074. struct megasas_io_frame *ldio;
  1075. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1076. ldio = (struct megasas_io_frame *)cmd->frame;
  1077. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1078. flags = MFI_FRAME_DIR_WRITE;
  1079. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1080. flags = MFI_FRAME_DIR_READ;
  1081. if (instance->flag_ieee == 1) {
  1082. flags |= MFI_FRAME_IEEE;
  1083. }
  1084. /*
  1085. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1086. */
  1087. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1088. ldio->cmd_status = 0x0;
  1089. ldio->scsi_status = 0x0;
  1090. ldio->target_id = device_id;
  1091. ldio->timeout = 0;
  1092. ldio->reserved_0 = 0;
  1093. ldio->pad_0 = 0;
  1094. ldio->flags = cpu_to_le16(flags);
  1095. ldio->start_lba_hi = 0;
  1096. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1097. /*
  1098. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1099. */
  1100. if (scp->cmd_len == 6) {
  1101. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
  1102. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
  1103. ((u32) scp->cmnd[2] << 8) |
  1104. (u32) scp->cmnd[3]);
  1105. ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
  1106. }
  1107. /*
  1108. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1109. */
  1110. else if (scp->cmd_len == 10) {
  1111. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
  1112. ((u32) scp->cmnd[7] << 8));
  1113. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1114. ((u32) scp->cmnd[3] << 16) |
  1115. ((u32) scp->cmnd[4] << 8) |
  1116. (u32) scp->cmnd[5]);
  1117. }
  1118. /*
  1119. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1120. */
  1121. else if (scp->cmd_len == 12) {
  1122. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1123. ((u32) scp->cmnd[7] << 16) |
  1124. ((u32) scp->cmnd[8] << 8) |
  1125. (u32) scp->cmnd[9]);
  1126. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1127. ((u32) scp->cmnd[3] << 16) |
  1128. ((u32) scp->cmnd[4] << 8) |
  1129. (u32) scp->cmnd[5]);
  1130. }
  1131. /*
  1132. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1133. */
  1134. else if (scp->cmd_len == 16) {
  1135. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
  1136. ((u32) scp->cmnd[11] << 16) |
  1137. ((u32) scp->cmnd[12] << 8) |
  1138. (u32) scp->cmnd[13]);
  1139. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1140. ((u32) scp->cmnd[7] << 16) |
  1141. ((u32) scp->cmnd[8] << 8) |
  1142. (u32) scp->cmnd[9]);
  1143. ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1144. ((u32) scp->cmnd[3] << 16) |
  1145. ((u32) scp->cmnd[4] << 8) |
  1146. (u32) scp->cmnd[5]);
  1147. }
  1148. /*
  1149. * Construct SGL
  1150. */
  1151. if (instance->flag_ieee) {
  1152. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1153. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1154. &ldio->sgl);
  1155. } else if (IS_DMA64) {
  1156. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1157. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1158. } else
  1159. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1160. if (ldio->sge_count > instance->max_num_sge) {
  1161. printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
  1162. ldio->sge_count);
  1163. return 0;
  1164. }
  1165. /*
  1166. * Sense info specific
  1167. */
  1168. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1169. ldio->sense_buf_phys_addr_hi = 0;
  1170. ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
  1171. /*
  1172. * Compute the total number of frames this command consumes. FW uses
  1173. * this number to pull sufficient number of frames from host memory.
  1174. */
  1175. cmd->frame_count = megasas_get_frame_count(instance,
  1176. ldio->sge_count, IO_FRAME);
  1177. return cmd->frame_count;
  1178. }
  1179. /**
  1180. * megasas_is_ldio - Checks if the cmd is for logical drive
  1181. * @scmd: SCSI command
  1182. *
  1183. * Called by megasas_queue_command to find out if the command to be queued
  1184. * is a logical drive command
  1185. */
  1186. inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  1187. {
  1188. if (!MEGASAS_IS_LOGICAL(cmd))
  1189. return 0;
  1190. switch (cmd->cmnd[0]) {
  1191. case READ_10:
  1192. case WRITE_10:
  1193. case READ_12:
  1194. case WRITE_12:
  1195. case READ_6:
  1196. case WRITE_6:
  1197. case READ_16:
  1198. case WRITE_16:
  1199. return 1;
  1200. default:
  1201. return 0;
  1202. }
  1203. }
  1204. /**
  1205. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1206. * in FW
  1207. * @instance: Adapter soft state
  1208. */
  1209. static inline void
  1210. megasas_dump_pending_frames(struct megasas_instance *instance)
  1211. {
  1212. struct megasas_cmd *cmd;
  1213. int i,n;
  1214. union megasas_sgl *mfi_sgl;
  1215. struct megasas_io_frame *ldio;
  1216. struct megasas_pthru_frame *pthru;
  1217. u32 sgcount;
  1218. u32 max_cmd = instance->max_fw_cmds;
  1219. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1220. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1221. if (IS_DMA64)
  1222. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1223. else
  1224. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1225. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1226. for (i = 0; i < max_cmd; i++) {
  1227. cmd = instance->cmd_list[i];
  1228. if(!cmd->scmd)
  1229. continue;
  1230. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1231. if (megasas_is_ldio(cmd->scmd)){
  1232. ldio = (struct megasas_io_frame *)cmd->frame;
  1233. mfi_sgl = &ldio->sgl;
  1234. sgcount = ldio->sge_count;
  1235. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
  1236. " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1237. instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
  1238. le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
  1239. le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
  1240. }
  1241. else {
  1242. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1243. mfi_sgl = &pthru->sgl;
  1244. sgcount = pthru->sge_count;
  1245. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
  1246. "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1247. instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
  1248. pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
  1249. le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
  1250. }
  1251. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  1252. for (n = 0; n < sgcount; n++){
  1253. if (IS_DMA64)
  1254. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
  1255. le32_to_cpu(mfi_sgl->sge64[n].length),
  1256. le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
  1257. else
  1258. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
  1259. le32_to_cpu(mfi_sgl->sge32[n].length),
  1260. le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
  1261. }
  1262. }
  1263. printk(KERN_ERR "\n");
  1264. } /*for max_cmd*/
  1265. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1266. for (i = 0; i < max_cmd; i++) {
  1267. cmd = instance->cmd_list[i];
  1268. if(cmd->sync_cmd == 1){
  1269. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1270. }
  1271. }
  1272. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  1273. }
  1274. u32
  1275. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1276. struct scsi_cmnd *scmd)
  1277. {
  1278. struct megasas_cmd *cmd;
  1279. u32 frame_count;
  1280. cmd = megasas_get_cmd(instance);
  1281. if (!cmd)
  1282. return SCSI_MLQUEUE_HOST_BUSY;
  1283. /*
  1284. * Logical drive command
  1285. */
  1286. if (megasas_is_ldio(scmd))
  1287. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1288. else
  1289. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1290. if (!frame_count)
  1291. goto out_return_cmd;
  1292. cmd->scmd = scmd;
  1293. scmd->SCp.ptr = (char *)cmd;
  1294. /*
  1295. * Issue the command to the FW
  1296. */
  1297. atomic_inc(&instance->fw_outstanding);
  1298. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1299. cmd->frame_count-1, instance->reg_set);
  1300. return 0;
  1301. out_return_cmd:
  1302. megasas_return_cmd(instance, cmd);
  1303. return 1;
  1304. }
  1305. /**
  1306. * megasas_queue_command - Queue entry point
  1307. * @scmd: SCSI command to be queued
  1308. * @done: Callback entry point
  1309. */
  1310. static int
  1311. megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  1312. {
  1313. struct megasas_instance *instance;
  1314. unsigned long flags;
  1315. instance = (struct megasas_instance *)
  1316. scmd->device->host->hostdata;
  1317. if (instance->issuepend_done == 0)
  1318. return SCSI_MLQUEUE_HOST_BUSY;
  1319. spin_lock_irqsave(&instance->hba_lock, flags);
  1320. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1321. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1322. scmd->result = DID_ERROR << 16;
  1323. done(scmd);
  1324. return 0;
  1325. }
  1326. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1327. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1328. return SCSI_MLQUEUE_HOST_BUSY;
  1329. }
  1330. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1331. scmd->scsi_done = done;
  1332. scmd->result = 0;
  1333. if (MEGASAS_IS_LOGICAL(scmd) &&
  1334. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  1335. scmd->result = DID_BAD_TARGET << 16;
  1336. goto out_done;
  1337. }
  1338. switch (scmd->cmnd[0]) {
  1339. case SYNCHRONIZE_CACHE:
  1340. /*
  1341. * FW takes care of flush cache on its own
  1342. * No need to send it down
  1343. */
  1344. scmd->result = DID_OK << 16;
  1345. goto out_done;
  1346. default:
  1347. break;
  1348. }
  1349. if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
  1350. printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
  1351. return SCSI_MLQUEUE_HOST_BUSY;
  1352. }
  1353. return 0;
  1354. out_done:
  1355. done(scmd);
  1356. return 0;
  1357. }
  1358. static DEF_SCSI_QCMD(megasas_queue_command)
  1359. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1360. {
  1361. int i;
  1362. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1363. if ((megasas_mgmt_info.instance[i]) &&
  1364. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1365. return megasas_mgmt_info.instance[i];
  1366. }
  1367. return NULL;
  1368. }
  1369. static int megasas_slave_configure(struct scsi_device *sdev)
  1370. {
  1371. u16 pd_index = 0;
  1372. struct megasas_instance *instance ;
  1373. instance = megasas_lookup_instance(sdev->host->host_no);
  1374. /*
  1375. * Don't export physical disk devices to the disk driver.
  1376. *
  1377. * FIXME: Currently we don't export them to the midlayer at all.
  1378. * That will be fixed once LSI engineers have audited the
  1379. * firmware for possible issues.
  1380. */
  1381. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1382. sdev->type == TYPE_DISK) {
  1383. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1384. sdev->id;
  1385. if (instance->pd_list[pd_index].driveState ==
  1386. MR_PD_STATE_SYSTEM) {
  1387. blk_queue_rq_timeout(sdev->request_queue,
  1388. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1389. return 0;
  1390. }
  1391. return -ENXIO;
  1392. }
  1393. /*
  1394. * The RAID firmware may require extended timeouts.
  1395. */
  1396. blk_queue_rq_timeout(sdev->request_queue,
  1397. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1398. return 0;
  1399. }
  1400. static int megasas_slave_alloc(struct scsi_device *sdev)
  1401. {
  1402. u16 pd_index = 0;
  1403. struct megasas_instance *instance ;
  1404. instance = megasas_lookup_instance(sdev->host->host_no);
  1405. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1406. (sdev->type == TYPE_DISK)) {
  1407. /*
  1408. * Open the OS scan to the SYSTEM PD
  1409. */
  1410. pd_index =
  1411. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1412. sdev->id;
  1413. if ((instance->pd_list[pd_index].driveState ==
  1414. MR_PD_STATE_SYSTEM) &&
  1415. (instance->pd_list[pd_index].driveType ==
  1416. TYPE_DISK)) {
  1417. return 0;
  1418. }
  1419. return -ENXIO;
  1420. }
  1421. return 0;
  1422. }
  1423. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1424. {
  1425. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1426. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1427. (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1428. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1429. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1430. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1431. } else {
  1432. writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
  1433. }
  1434. }
  1435. /**
  1436. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1437. * restored to max value
  1438. * @instance: Adapter soft state
  1439. *
  1440. */
  1441. void
  1442. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1443. {
  1444. unsigned long flags;
  1445. if (instance->flag & MEGASAS_FW_BUSY
  1446. && time_after(jiffies, instance->last_time + 5 * HZ)
  1447. && atomic_read(&instance->fw_outstanding) <
  1448. instance->throttlequeuedepth + 1) {
  1449. spin_lock_irqsave(instance->host->host_lock, flags);
  1450. instance->flag &= ~MEGASAS_FW_BUSY;
  1451. if (instance->is_imr) {
  1452. instance->host->can_queue =
  1453. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1454. } else
  1455. instance->host->can_queue =
  1456. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1457. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1458. }
  1459. }
  1460. /**
  1461. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1462. * @instance_addr: Address of adapter soft state
  1463. *
  1464. * Tasklet to complete cmds
  1465. */
  1466. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1467. {
  1468. u32 producer;
  1469. u32 consumer;
  1470. u32 context;
  1471. struct megasas_cmd *cmd;
  1472. struct megasas_instance *instance =
  1473. (struct megasas_instance *)instance_addr;
  1474. unsigned long flags;
  1475. /* If we have already declared adapter dead, donot complete cmds */
  1476. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
  1477. return;
  1478. spin_lock_irqsave(&instance->completion_lock, flags);
  1479. producer = le32_to_cpu(*instance->producer);
  1480. consumer = le32_to_cpu(*instance->consumer);
  1481. while (consumer != producer) {
  1482. context = le32_to_cpu(instance->reply_queue[consumer]);
  1483. if (context >= instance->max_fw_cmds) {
  1484. printk(KERN_ERR "Unexpected context value %x\n",
  1485. context);
  1486. BUG();
  1487. }
  1488. cmd = instance->cmd_list[context];
  1489. megasas_complete_cmd(instance, cmd, DID_OK);
  1490. consumer++;
  1491. if (consumer == (instance->max_fw_cmds + 1)) {
  1492. consumer = 0;
  1493. }
  1494. }
  1495. *instance->consumer = cpu_to_le32(producer);
  1496. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1497. /*
  1498. * Check if we can restore can_queue
  1499. */
  1500. megasas_check_and_restore_queue_depth(instance);
  1501. }
  1502. static void
  1503. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1504. static void
  1505. process_fw_state_change_wq(struct work_struct *work);
  1506. void megasas_do_ocr(struct megasas_instance *instance)
  1507. {
  1508. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1509. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1510. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1511. *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  1512. }
  1513. instance->instancet->disable_intr(instance);
  1514. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1515. instance->issuepend_done = 0;
  1516. atomic_set(&instance->fw_outstanding, 0);
  1517. megasas_internal_reset_defer_cmds(instance);
  1518. process_fw_state_change_wq(&instance->work_init);
  1519. }
  1520. /**
  1521. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1522. * @instance: Adapter soft state
  1523. *
  1524. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  1525. * complete all its outstanding commands. Returns error if one or more IOs
  1526. * are pending after this time period. It also marks the controller dead.
  1527. */
  1528. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1529. {
  1530. int i;
  1531. u32 reset_index;
  1532. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1533. u8 adprecovery;
  1534. unsigned long flags;
  1535. struct list_head clist_local;
  1536. struct megasas_cmd *reset_cmd;
  1537. u32 fw_state;
  1538. u8 kill_adapter_flag;
  1539. spin_lock_irqsave(&instance->hba_lock, flags);
  1540. adprecovery = instance->adprecovery;
  1541. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1542. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1543. INIT_LIST_HEAD(&clist_local);
  1544. spin_lock_irqsave(&instance->hba_lock, flags);
  1545. list_splice_init(&instance->internal_reset_pending_q,
  1546. &clist_local);
  1547. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1548. printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
  1549. for (i = 0; i < wait_time; i++) {
  1550. msleep(1000);
  1551. spin_lock_irqsave(&instance->hba_lock, flags);
  1552. adprecovery = instance->adprecovery;
  1553. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1554. if (adprecovery == MEGASAS_HBA_OPERATIONAL)
  1555. break;
  1556. }
  1557. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1558. printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
  1559. spin_lock_irqsave(&instance->hba_lock, flags);
  1560. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1561. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1562. return FAILED;
  1563. }
  1564. reset_index = 0;
  1565. while (!list_empty(&clist_local)) {
  1566. reset_cmd = list_entry((&clist_local)->next,
  1567. struct megasas_cmd, list);
  1568. list_del_init(&reset_cmd->list);
  1569. if (reset_cmd->scmd) {
  1570. reset_cmd->scmd->result = DID_RESET << 16;
  1571. printk(KERN_NOTICE "%d:%p reset [%02x]\n",
  1572. reset_index, reset_cmd,
  1573. reset_cmd->scmd->cmnd[0]);
  1574. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  1575. megasas_return_cmd(instance, reset_cmd);
  1576. } else if (reset_cmd->sync_cmd) {
  1577. printk(KERN_NOTICE "megasas:%p synch cmds"
  1578. "reset queue\n",
  1579. reset_cmd);
  1580. reset_cmd->cmd_status = ENODATA;
  1581. instance->instancet->fire_cmd(instance,
  1582. reset_cmd->frame_phys_addr,
  1583. 0, instance->reg_set);
  1584. } else {
  1585. printk(KERN_NOTICE "megasas: %p unexpected"
  1586. "cmds lst\n",
  1587. reset_cmd);
  1588. }
  1589. reset_index++;
  1590. }
  1591. return SUCCESS;
  1592. }
  1593. for (i = 0; i < resetwaittime; i++) {
  1594. int outstanding = atomic_read(&instance->fw_outstanding);
  1595. if (!outstanding)
  1596. break;
  1597. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1598. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1599. "commands to complete\n",i,outstanding);
  1600. /*
  1601. * Call cmd completion routine. Cmd to be
  1602. * be completed directly without depending on isr.
  1603. */
  1604. megasas_complete_cmd_dpc((unsigned long)instance);
  1605. }
  1606. msleep(1000);
  1607. }
  1608. i = 0;
  1609. kill_adapter_flag = 0;
  1610. do {
  1611. fw_state = instance->instancet->read_fw_status_reg(
  1612. instance->reg_set) & MFI_STATE_MASK;
  1613. if ((fw_state == MFI_STATE_FAULT) &&
  1614. (instance->disableOnlineCtrlReset == 0)) {
  1615. if (i == 3) {
  1616. kill_adapter_flag = 2;
  1617. break;
  1618. }
  1619. megasas_do_ocr(instance);
  1620. kill_adapter_flag = 1;
  1621. /* wait for 1 secs to let FW finish the pending cmds */
  1622. msleep(1000);
  1623. }
  1624. i++;
  1625. } while (i <= 3);
  1626. if (atomic_read(&instance->fw_outstanding) &&
  1627. !kill_adapter_flag) {
  1628. if (instance->disableOnlineCtrlReset == 0) {
  1629. megasas_do_ocr(instance);
  1630. /* wait for 5 secs to let FW finish the pending cmds */
  1631. for (i = 0; i < wait_time; i++) {
  1632. int outstanding =
  1633. atomic_read(&instance->fw_outstanding);
  1634. if (!outstanding)
  1635. return SUCCESS;
  1636. msleep(1000);
  1637. }
  1638. }
  1639. }
  1640. if (atomic_read(&instance->fw_outstanding) ||
  1641. (kill_adapter_flag == 2)) {
  1642. printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
  1643. /*
  1644. * Send signal to FW to stop processing any pending cmds.
  1645. * The controller will be taken offline by the OS now.
  1646. */
  1647. if ((instance->pdev->device ==
  1648. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1649. (instance->pdev->device ==
  1650. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1651. writel(MFI_STOP_ADP,
  1652. &instance->reg_set->doorbell);
  1653. } else {
  1654. writel(MFI_STOP_ADP,
  1655. &instance->reg_set->inbound_doorbell);
  1656. }
  1657. megasas_dump_pending_frames(instance);
  1658. spin_lock_irqsave(&instance->hba_lock, flags);
  1659. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1660. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1661. return FAILED;
  1662. }
  1663. printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
  1664. return SUCCESS;
  1665. }
  1666. /**
  1667. * megasas_generic_reset - Generic reset routine
  1668. * @scmd: Mid-layer SCSI command
  1669. *
  1670. * This routine implements a generic reset handler for device, bus and host
  1671. * reset requests. Device, bus and host specific reset handlers can use this
  1672. * function after they do their specific tasks.
  1673. */
  1674. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1675. {
  1676. int ret_val;
  1677. struct megasas_instance *instance;
  1678. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1679. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  1680. scmd->cmnd[0], scmd->retries);
  1681. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1682. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1683. "failures\n");
  1684. return FAILED;
  1685. }
  1686. ret_val = megasas_wait_for_outstanding(instance);
  1687. if (ret_val == SUCCESS)
  1688. printk(KERN_NOTICE "megasas: reset successful \n");
  1689. else
  1690. printk(KERN_ERR "megasas: failed to do reset\n");
  1691. return ret_val;
  1692. }
  1693. /**
  1694. * megasas_reset_timer - quiesce the adapter if required
  1695. * @scmd: scsi cmnd
  1696. *
  1697. * Sets the FW busy flag and reduces the host->can_queue if the
  1698. * cmd has not been completed within the timeout period.
  1699. */
  1700. static enum
  1701. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1702. {
  1703. struct megasas_instance *instance;
  1704. unsigned long flags;
  1705. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1706. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1707. return BLK_EH_NOT_HANDLED;
  1708. }
  1709. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1710. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1711. /* FW is busy, throttle IO */
  1712. spin_lock_irqsave(instance->host->host_lock, flags);
  1713. instance->host->can_queue = instance->throttlequeuedepth;
  1714. instance->last_time = jiffies;
  1715. instance->flag |= MEGASAS_FW_BUSY;
  1716. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1717. }
  1718. return BLK_EH_RESET_TIMER;
  1719. }
  1720. /**
  1721. * megasas_reset_device - Device reset handler entry point
  1722. */
  1723. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1724. {
  1725. int ret;
  1726. /*
  1727. * First wait for all commands to complete
  1728. */
  1729. ret = megasas_generic_reset(scmd);
  1730. return ret;
  1731. }
  1732. /**
  1733. * megasas_reset_bus_host - Bus & host reset handler entry point
  1734. */
  1735. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1736. {
  1737. int ret;
  1738. struct megasas_instance *instance;
  1739. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1740. /*
  1741. * First wait for all commands to complete
  1742. */
  1743. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1744. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1745. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  1746. ret = megasas_reset_fusion(scmd->device->host);
  1747. else
  1748. ret = megasas_generic_reset(scmd);
  1749. return ret;
  1750. }
  1751. /**
  1752. * megasas_bios_param - Returns disk geometry for a disk
  1753. * @sdev: device handle
  1754. * @bdev: block device
  1755. * @capacity: drive capacity
  1756. * @geom: geometry parameters
  1757. */
  1758. static int
  1759. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1760. sector_t capacity, int geom[])
  1761. {
  1762. int heads;
  1763. int sectors;
  1764. sector_t cylinders;
  1765. unsigned long tmp;
  1766. /* Default heads (64) & sectors (32) */
  1767. heads = 64;
  1768. sectors = 32;
  1769. tmp = heads * sectors;
  1770. cylinders = capacity;
  1771. sector_div(cylinders, tmp);
  1772. /*
  1773. * Handle extended translation size for logical drives > 1Gb
  1774. */
  1775. if (capacity >= 0x200000) {
  1776. heads = 255;
  1777. sectors = 63;
  1778. tmp = heads*sectors;
  1779. cylinders = capacity;
  1780. sector_div(cylinders, tmp);
  1781. }
  1782. geom[0] = heads;
  1783. geom[1] = sectors;
  1784. geom[2] = cylinders;
  1785. return 0;
  1786. }
  1787. static void megasas_aen_polling(struct work_struct *work);
  1788. /**
  1789. * megasas_service_aen - Processes an event notification
  1790. * @instance: Adapter soft state
  1791. * @cmd: AEN command completed by the ISR
  1792. *
  1793. * For AEN, driver sends a command down to FW that is held by the FW till an
  1794. * event occurs. When an event of interest occurs, FW completes the command
  1795. * that it was previously holding.
  1796. *
  1797. * This routines sends SIGIO signal to processes that have registered with the
  1798. * driver for AEN.
  1799. */
  1800. static void
  1801. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1802. {
  1803. unsigned long flags;
  1804. /*
  1805. * Don't signal app if it is just an aborted previously registered aen
  1806. */
  1807. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1808. spin_lock_irqsave(&poll_aen_lock, flags);
  1809. megasas_poll_wait_aen = 1;
  1810. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1811. wake_up(&megasas_poll_wait);
  1812. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1813. }
  1814. else
  1815. cmd->abort_aen = 0;
  1816. instance->aen_cmd = NULL;
  1817. megasas_return_cmd(instance, cmd);
  1818. if ((instance->unload == 0) &&
  1819. ((instance->issuepend_done == 1))) {
  1820. struct megasas_aen_event *ev;
  1821. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1822. if (!ev) {
  1823. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1824. } else {
  1825. ev->instance = instance;
  1826. instance->ev = ev;
  1827. INIT_DELAYED_WORK(&ev->hotplug_work,
  1828. megasas_aen_polling);
  1829. schedule_delayed_work(&ev->hotplug_work, 0);
  1830. }
  1831. }
  1832. }
  1833. static int megasas_change_queue_depth(struct scsi_device *sdev,
  1834. int queue_depth, int reason)
  1835. {
  1836. if (reason != SCSI_QDEPTH_DEFAULT)
  1837. return -EOPNOTSUPP;
  1838. if (queue_depth > sdev->host->can_queue)
  1839. queue_depth = sdev->host->can_queue;
  1840. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1841. queue_depth);
  1842. return queue_depth;
  1843. }
  1844. /*
  1845. * Scsi host template for megaraid_sas driver
  1846. */
  1847. static struct scsi_host_template megasas_template = {
  1848. .module = THIS_MODULE,
  1849. .name = "LSI SAS based MegaRAID driver",
  1850. .proc_name = "megaraid_sas",
  1851. .slave_configure = megasas_slave_configure,
  1852. .slave_alloc = megasas_slave_alloc,
  1853. .queuecommand = megasas_queue_command,
  1854. .eh_device_reset_handler = megasas_reset_device,
  1855. .eh_bus_reset_handler = megasas_reset_bus_host,
  1856. .eh_host_reset_handler = megasas_reset_bus_host,
  1857. .eh_timed_out = megasas_reset_timer,
  1858. .bios_param = megasas_bios_param,
  1859. .use_clustering = ENABLE_CLUSTERING,
  1860. .change_queue_depth = megasas_change_queue_depth,
  1861. };
  1862. /**
  1863. * megasas_complete_int_cmd - Completes an internal command
  1864. * @instance: Adapter soft state
  1865. * @cmd: Command to be completed
  1866. *
  1867. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1868. * after it issues a command. This function wakes up that waiting routine by
  1869. * calling wake_up() on the wait queue.
  1870. */
  1871. static void
  1872. megasas_complete_int_cmd(struct megasas_instance *instance,
  1873. struct megasas_cmd *cmd)
  1874. {
  1875. cmd->cmd_status = cmd->frame->io.cmd_status;
  1876. if (cmd->cmd_status == ENODATA) {
  1877. cmd->cmd_status = 0;
  1878. }
  1879. wake_up(&instance->int_cmd_wait_q);
  1880. }
  1881. /**
  1882. * megasas_complete_abort - Completes aborting a command
  1883. * @instance: Adapter soft state
  1884. * @cmd: Cmd that was issued to abort another cmd
  1885. *
  1886. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1887. * after it issues an abort on a previously issued command. This function
  1888. * wakes up all functions waiting on the same wait queue.
  1889. */
  1890. static void
  1891. megasas_complete_abort(struct megasas_instance *instance,
  1892. struct megasas_cmd *cmd)
  1893. {
  1894. if (cmd->sync_cmd) {
  1895. cmd->sync_cmd = 0;
  1896. cmd->cmd_status = 0;
  1897. wake_up(&instance->abort_cmd_wait_q);
  1898. }
  1899. return;
  1900. }
  1901. /**
  1902. * megasas_complete_cmd - Completes a command
  1903. * @instance: Adapter soft state
  1904. * @cmd: Command to be completed
  1905. * @alt_status: If non-zero, use this value as status to
  1906. * SCSI mid-layer instead of the value returned
  1907. * by the FW. This should be used if caller wants
  1908. * an alternate status (as in the case of aborted
  1909. * commands)
  1910. */
  1911. void
  1912. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1913. u8 alt_status)
  1914. {
  1915. int exception = 0;
  1916. struct megasas_header *hdr = &cmd->frame->hdr;
  1917. unsigned long flags;
  1918. struct fusion_context *fusion = instance->ctrl_context;
  1919. u32 opcode;
  1920. /* flag for the retry reset */
  1921. cmd->retry_for_fw_reset = 0;
  1922. if (cmd->scmd)
  1923. cmd->scmd->SCp.ptr = NULL;
  1924. switch (hdr->cmd) {
  1925. case MFI_CMD_INVALID:
  1926. /* Some older 1068 controller FW may keep a pended
  1927. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  1928. when booting the kdump kernel. Ignore this command to
  1929. prevent a kernel panic on shutdown of the kdump kernel. */
  1930. printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
  1931. "completed.\n");
  1932. printk(KERN_WARNING "megaraid_sas: If you have a controller "
  1933. "other than PERC5, please upgrade your firmware.\n");
  1934. break;
  1935. case MFI_CMD_PD_SCSI_IO:
  1936. case MFI_CMD_LD_SCSI_IO:
  1937. /*
  1938. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1939. * issued either through an IO path or an IOCTL path. If it
  1940. * was via IOCTL, we will send it to internal completion.
  1941. */
  1942. if (cmd->sync_cmd) {
  1943. cmd->sync_cmd = 0;
  1944. megasas_complete_int_cmd(instance, cmd);
  1945. break;
  1946. }
  1947. case MFI_CMD_LD_READ:
  1948. case MFI_CMD_LD_WRITE:
  1949. if (alt_status) {
  1950. cmd->scmd->result = alt_status << 16;
  1951. exception = 1;
  1952. }
  1953. if (exception) {
  1954. atomic_dec(&instance->fw_outstanding);
  1955. scsi_dma_unmap(cmd->scmd);
  1956. cmd->scmd->scsi_done(cmd->scmd);
  1957. megasas_return_cmd(instance, cmd);
  1958. break;
  1959. }
  1960. switch (hdr->cmd_status) {
  1961. case MFI_STAT_OK:
  1962. cmd->scmd->result = DID_OK << 16;
  1963. break;
  1964. case MFI_STAT_SCSI_IO_FAILED:
  1965. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1966. cmd->scmd->result =
  1967. (DID_ERROR << 16) | hdr->scsi_status;
  1968. break;
  1969. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1970. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1971. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1972. memset(cmd->scmd->sense_buffer, 0,
  1973. SCSI_SENSE_BUFFERSIZE);
  1974. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1975. hdr->sense_len);
  1976. cmd->scmd->result |= DRIVER_SENSE << 24;
  1977. }
  1978. break;
  1979. case MFI_STAT_LD_OFFLINE:
  1980. case MFI_STAT_DEVICE_NOT_FOUND:
  1981. cmd->scmd->result = DID_BAD_TARGET << 16;
  1982. break;
  1983. default:
  1984. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1985. hdr->cmd_status);
  1986. cmd->scmd->result = DID_ERROR << 16;
  1987. break;
  1988. }
  1989. atomic_dec(&instance->fw_outstanding);
  1990. scsi_dma_unmap(cmd->scmd);
  1991. cmd->scmd->scsi_done(cmd->scmd);
  1992. megasas_return_cmd(instance, cmd);
  1993. break;
  1994. case MFI_CMD_SMP:
  1995. case MFI_CMD_STP:
  1996. case MFI_CMD_DCMD:
  1997. opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
  1998. /* Check for LD map update */
  1999. if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
  2000. && (cmd->frame->dcmd.mbox.b[1] == 1)) {
  2001. fusion->fast_path_io = 0;
  2002. spin_lock_irqsave(instance->host->host_lock, flags);
  2003. if (cmd->frame->hdr.cmd_status != 0) {
  2004. if (cmd->frame->hdr.cmd_status !=
  2005. MFI_STAT_NOT_FOUND)
  2006. printk(KERN_WARNING "megasas: map sync"
  2007. "failed, status = 0x%x.\n",
  2008. cmd->frame->hdr.cmd_status);
  2009. else {
  2010. megasas_return_cmd(instance, cmd);
  2011. spin_unlock_irqrestore(
  2012. instance->host->host_lock,
  2013. flags);
  2014. break;
  2015. }
  2016. } else
  2017. instance->map_id++;
  2018. megasas_return_cmd(instance, cmd);
  2019. /*
  2020. * Set fast path IO to ZERO.
  2021. * Validate Map will set proper value.
  2022. * Meanwhile all IOs will go as LD IO.
  2023. */
  2024. if (MR_ValidateMapInfo(instance))
  2025. fusion->fast_path_io = 1;
  2026. else
  2027. fusion->fast_path_io = 0;
  2028. megasas_sync_map_info(instance);
  2029. spin_unlock_irqrestore(instance->host->host_lock,
  2030. flags);
  2031. break;
  2032. }
  2033. if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  2034. opcode == MR_DCMD_CTRL_EVENT_GET) {
  2035. spin_lock_irqsave(&poll_aen_lock, flags);
  2036. megasas_poll_wait_aen = 0;
  2037. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2038. }
  2039. /*
  2040. * See if got an event notification
  2041. */
  2042. if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
  2043. megasas_service_aen(instance, cmd);
  2044. else
  2045. megasas_complete_int_cmd(instance, cmd);
  2046. break;
  2047. case MFI_CMD_ABORT:
  2048. /*
  2049. * Cmd issued to abort another cmd returned
  2050. */
  2051. megasas_complete_abort(instance, cmd);
  2052. break;
  2053. default:
  2054. printk("megasas: Unknown command completed! [0x%X]\n",
  2055. hdr->cmd);
  2056. break;
  2057. }
  2058. }
  2059. /**
  2060. * megasas_issue_pending_cmds_again - issue all pending cmds
  2061. * in FW again because of the fw reset
  2062. * @instance: Adapter soft state
  2063. */
  2064. static inline void
  2065. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2066. {
  2067. struct megasas_cmd *cmd;
  2068. struct list_head clist_local;
  2069. union megasas_evt_class_locale class_locale;
  2070. unsigned long flags;
  2071. u32 seq_num;
  2072. INIT_LIST_HEAD(&clist_local);
  2073. spin_lock_irqsave(&instance->hba_lock, flags);
  2074. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2075. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2076. while (!list_empty(&clist_local)) {
  2077. cmd = list_entry((&clist_local)->next,
  2078. struct megasas_cmd, list);
  2079. list_del_init(&cmd->list);
  2080. if (cmd->sync_cmd || cmd->scmd) {
  2081. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  2082. "detected to be pending while HBA reset.\n",
  2083. cmd, cmd->scmd, cmd->sync_cmd);
  2084. cmd->retry_for_fw_reset++;
  2085. if (cmd->retry_for_fw_reset == 3) {
  2086. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2087. "was tried multiple times during reset."
  2088. "Shutting down the HBA\n",
  2089. cmd, cmd->scmd, cmd->sync_cmd);
  2090. megaraid_sas_kill_hba(instance);
  2091. instance->adprecovery =
  2092. MEGASAS_HW_CRITICAL_ERROR;
  2093. return;
  2094. }
  2095. }
  2096. if (cmd->sync_cmd == 1) {
  2097. if (cmd->scmd) {
  2098. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2099. "cmd attached to internal command!\n");
  2100. }
  2101. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2102. "on the internal reset queue,"
  2103. "issue it again.\n", cmd);
  2104. cmd->cmd_status = ENODATA;
  2105. instance->instancet->fire_cmd(instance,
  2106. cmd->frame_phys_addr ,
  2107. 0, instance->reg_set);
  2108. } else if (cmd->scmd) {
  2109. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
  2110. "detected on the internal queue, issue again.\n",
  2111. cmd, cmd->scmd->cmnd[0]);
  2112. atomic_inc(&instance->fw_outstanding);
  2113. instance->instancet->fire_cmd(instance,
  2114. cmd->frame_phys_addr,
  2115. cmd->frame_count-1, instance->reg_set);
  2116. } else {
  2117. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2118. "internal reset defer list while re-issue!!\n",
  2119. cmd);
  2120. }
  2121. }
  2122. if (instance->aen_cmd) {
  2123. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2124. megasas_return_cmd(instance, instance->aen_cmd);
  2125. instance->aen_cmd = NULL;
  2126. }
  2127. /*
  2128. * Initiate AEN (Asynchronous Event Notification)
  2129. */
  2130. seq_num = instance->last_seq_num;
  2131. class_locale.members.reserved = 0;
  2132. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2133. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2134. megasas_register_aen(instance, seq_num, class_locale.word);
  2135. }
  2136. /**
  2137. * Move the internal reset pending commands to a deferred queue.
  2138. *
  2139. * We move the commands pending at internal reset time to a
  2140. * pending queue. This queue would be flushed after successful
  2141. * completion of the internal reset sequence. if the internal reset
  2142. * did not complete in time, the kernel reset handler would flush
  2143. * these commands.
  2144. **/
  2145. static void
  2146. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2147. {
  2148. struct megasas_cmd *cmd;
  2149. int i;
  2150. u32 max_cmd = instance->max_fw_cmds;
  2151. u32 defer_index;
  2152. unsigned long flags;
  2153. defer_index = 0;
  2154. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2155. for (i = 0; i < max_cmd; i++) {
  2156. cmd = instance->cmd_list[i];
  2157. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2158. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2159. "on the defer queue as internal\n",
  2160. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2161. if (!list_empty(&cmd->list)) {
  2162. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2163. " moving this cmd:%p, %d %p, it was"
  2164. "discovered on some list?\n",
  2165. cmd, cmd->sync_cmd, cmd->scmd);
  2166. list_del_init(&cmd->list);
  2167. }
  2168. defer_index++;
  2169. list_add_tail(&cmd->list,
  2170. &instance->internal_reset_pending_q);
  2171. }
  2172. }
  2173. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2174. }
  2175. static void
  2176. process_fw_state_change_wq(struct work_struct *work)
  2177. {
  2178. struct megasas_instance *instance =
  2179. container_of(work, struct megasas_instance, work_init);
  2180. u32 wait;
  2181. unsigned long flags;
  2182. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2183. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2184. instance->adprecovery);
  2185. return ;
  2186. }
  2187. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2188. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2189. "state, restarting it...\n");
  2190. instance->instancet->disable_intr(instance);
  2191. atomic_set(&instance->fw_outstanding, 0);
  2192. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2193. instance->instancet->adp_reset(instance, instance->reg_set);
  2194. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2195. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2196. "initiating next stage...\n");
  2197. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2198. "state 2 starting...\n");
  2199. /*waitting for about 20 second before start the second init*/
  2200. for (wait = 0; wait < 30; wait++) {
  2201. msleep(1000);
  2202. }
  2203. if (megasas_transition_to_ready(instance, 1)) {
  2204. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2205. megaraid_sas_kill_hba(instance);
  2206. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2207. return ;
  2208. }
  2209. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2210. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2211. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2212. ) {
  2213. *instance->consumer = *instance->producer;
  2214. } else {
  2215. *instance->consumer = 0;
  2216. *instance->producer = 0;
  2217. }
  2218. megasas_issue_init_mfi(instance);
  2219. spin_lock_irqsave(&instance->hba_lock, flags);
  2220. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2221. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2222. instance->instancet->enable_intr(instance);
  2223. megasas_issue_pending_cmds_again(instance);
  2224. instance->issuepend_done = 1;
  2225. }
  2226. return ;
  2227. }
  2228. /**
  2229. * megasas_deplete_reply_queue - Processes all completed commands
  2230. * @instance: Adapter soft state
  2231. * @alt_status: Alternate status to be returned to
  2232. * SCSI mid-layer instead of the status
  2233. * returned by the FW
  2234. * Note: this must be called with hba lock held
  2235. */
  2236. static int
  2237. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2238. u8 alt_status)
  2239. {
  2240. u32 mfiStatus;
  2241. u32 fw_state;
  2242. if ((mfiStatus = instance->instancet->check_reset(instance,
  2243. instance->reg_set)) == 1) {
  2244. return IRQ_HANDLED;
  2245. }
  2246. if ((mfiStatus = instance->instancet->clear_intr(
  2247. instance->reg_set)
  2248. ) == 0) {
  2249. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2250. if (!instance->msix_vectors)
  2251. return IRQ_NONE;
  2252. }
  2253. instance->mfiStatus = mfiStatus;
  2254. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2255. fw_state = instance->instancet->read_fw_status_reg(
  2256. instance->reg_set) & MFI_STATE_MASK;
  2257. if (fw_state != MFI_STATE_FAULT) {
  2258. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2259. fw_state);
  2260. }
  2261. if ((fw_state == MFI_STATE_FAULT) &&
  2262. (instance->disableOnlineCtrlReset == 0)) {
  2263. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2264. if ((instance->pdev->device ==
  2265. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2266. (instance->pdev->device ==
  2267. PCI_DEVICE_ID_DELL_PERC5) ||
  2268. (instance->pdev->device ==
  2269. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2270. *instance->consumer =
  2271. cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  2272. }
  2273. instance->instancet->disable_intr(instance);
  2274. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2275. instance->issuepend_done = 0;
  2276. atomic_set(&instance->fw_outstanding, 0);
  2277. megasas_internal_reset_defer_cmds(instance);
  2278. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2279. fw_state, instance->adprecovery);
  2280. schedule_work(&instance->work_init);
  2281. return IRQ_HANDLED;
  2282. } else {
  2283. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2284. fw_state, instance->disableOnlineCtrlReset);
  2285. }
  2286. }
  2287. tasklet_schedule(&instance->isr_tasklet);
  2288. return IRQ_HANDLED;
  2289. }
  2290. /**
  2291. * megasas_isr - isr entry point
  2292. */
  2293. static irqreturn_t megasas_isr(int irq, void *devp)
  2294. {
  2295. struct megasas_irq_context *irq_context = devp;
  2296. struct megasas_instance *instance = irq_context->instance;
  2297. unsigned long flags;
  2298. irqreturn_t rc;
  2299. if (atomic_read(&instance->fw_reset_no_pci_access))
  2300. return IRQ_HANDLED;
  2301. spin_lock_irqsave(&instance->hba_lock, flags);
  2302. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2303. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2304. return rc;
  2305. }
  2306. /**
  2307. * megasas_transition_to_ready - Move the FW to READY state
  2308. * @instance: Adapter soft state
  2309. *
  2310. * During the initialization, FW passes can potentially be in any one of
  2311. * several possible states. If the FW in operational, waiting-for-handshake
  2312. * states, driver must take steps to bring it to ready state. Otherwise, it
  2313. * has to wait for the ready state.
  2314. */
  2315. int
  2316. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  2317. {
  2318. int i;
  2319. u8 max_wait;
  2320. u32 fw_state;
  2321. u32 cur_state;
  2322. u32 abs_state, curr_abs_state;
  2323. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2324. if (fw_state != MFI_STATE_READY)
  2325. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2326. " state\n");
  2327. while (fw_state != MFI_STATE_READY) {
  2328. abs_state =
  2329. instance->instancet->read_fw_status_reg(instance->reg_set);
  2330. switch (fw_state) {
  2331. case MFI_STATE_FAULT:
  2332. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2333. if (ocr) {
  2334. max_wait = MEGASAS_RESET_WAIT_TIME;
  2335. cur_state = MFI_STATE_FAULT;
  2336. break;
  2337. } else
  2338. return -ENODEV;
  2339. case MFI_STATE_WAIT_HANDSHAKE:
  2340. /*
  2341. * Set the CLR bit in inbound doorbell
  2342. */
  2343. if ((instance->pdev->device ==
  2344. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2345. (instance->pdev->device ==
  2346. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2347. (instance->pdev->device ==
  2348. PCI_DEVICE_ID_LSI_FUSION) ||
  2349. (instance->pdev->device ==
  2350. PCI_DEVICE_ID_LSI_INVADER) ||
  2351. (instance->pdev->device ==
  2352. PCI_DEVICE_ID_LSI_FURY)) {
  2353. writel(
  2354. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2355. &instance->reg_set->doorbell);
  2356. } else {
  2357. writel(
  2358. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2359. &instance->reg_set->inbound_doorbell);
  2360. }
  2361. max_wait = MEGASAS_RESET_WAIT_TIME;
  2362. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2363. break;
  2364. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2365. if ((instance->pdev->device ==
  2366. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2367. (instance->pdev->device ==
  2368. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2369. (instance->pdev->device ==
  2370. PCI_DEVICE_ID_LSI_FUSION) ||
  2371. (instance->pdev->device ==
  2372. PCI_DEVICE_ID_LSI_INVADER) ||
  2373. (instance->pdev->device ==
  2374. PCI_DEVICE_ID_LSI_FURY)) {
  2375. writel(MFI_INIT_HOTPLUG,
  2376. &instance->reg_set->doorbell);
  2377. } else
  2378. writel(MFI_INIT_HOTPLUG,
  2379. &instance->reg_set->inbound_doorbell);
  2380. max_wait = MEGASAS_RESET_WAIT_TIME;
  2381. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2382. break;
  2383. case MFI_STATE_OPERATIONAL:
  2384. /*
  2385. * Bring it to READY state; assuming max wait 10 secs
  2386. */
  2387. instance->instancet->disable_intr(instance);
  2388. if ((instance->pdev->device ==
  2389. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2390. (instance->pdev->device ==
  2391. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2392. (instance->pdev->device
  2393. == PCI_DEVICE_ID_LSI_FUSION) ||
  2394. (instance->pdev->device
  2395. == PCI_DEVICE_ID_LSI_INVADER) ||
  2396. (instance->pdev->device
  2397. == PCI_DEVICE_ID_LSI_FURY)) {
  2398. writel(MFI_RESET_FLAGS,
  2399. &instance->reg_set->doorbell);
  2400. if ((instance->pdev->device ==
  2401. PCI_DEVICE_ID_LSI_FUSION) ||
  2402. (instance->pdev->device ==
  2403. PCI_DEVICE_ID_LSI_INVADER) ||
  2404. (instance->pdev->device ==
  2405. PCI_DEVICE_ID_LSI_FURY)) {
  2406. for (i = 0; i < (10 * 1000); i += 20) {
  2407. if (readl(
  2408. &instance->
  2409. reg_set->
  2410. doorbell) & 1)
  2411. msleep(20);
  2412. else
  2413. break;
  2414. }
  2415. }
  2416. } else
  2417. writel(MFI_RESET_FLAGS,
  2418. &instance->reg_set->inbound_doorbell);
  2419. max_wait = MEGASAS_RESET_WAIT_TIME;
  2420. cur_state = MFI_STATE_OPERATIONAL;
  2421. break;
  2422. case MFI_STATE_UNDEFINED:
  2423. /*
  2424. * This state should not last for more than 2 seconds
  2425. */
  2426. max_wait = MEGASAS_RESET_WAIT_TIME;
  2427. cur_state = MFI_STATE_UNDEFINED;
  2428. break;
  2429. case MFI_STATE_BB_INIT:
  2430. max_wait = MEGASAS_RESET_WAIT_TIME;
  2431. cur_state = MFI_STATE_BB_INIT;
  2432. break;
  2433. case MFI_STATE_FW_INIT:
  2434. max_wait = MEGASAS_RESET_WAIT_TIME;
  2435. cur_state = MFI_STATE_FW_INIT;
  2436. break;
  2437. case MFI_STATE_FW_INIT_2:
  2438. max_wait = MEGASAS_RESET_WAIT_TIME;
  2439. cur_state = MFI_STATE_FW_INIT_2;
  2440. break;
  2441. case MFI_STATE_DEVICE_SCAN:
  2442. max_wait = MEGASAS_RESET_WAIT_TIME;
  2443. cur_state = MFI_STATE_DEVICE_SCAN;
  2444. break;
  2445. case MFI_STATE_FLUSH_CACHE:
  2446. max_wait = MEGASAS_RESET_WAIT_TIME;
  2447. cur_state = MFI_STATE_FLUSH_CACHE;
  2448. break;
  2449. default:
  2450. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2451. fw_state);
  2452. return -ENODEV;
  2453. }
  2454. /*
  2455. * The cur_state should not last for more than max_wait secs
  2456. */
  2457. for (i = 0; i < (max_wait * 1000); i++) {
  2458. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2459. MFI_STATE_MASK ;
  2460. curr_abs_state =
  2461. instance->instancet->read_fw_status_reg(instance->reg_set);
  2462. if (abs_state == curr_abs_state) {
  2463. msleep(1);
  2464. } else
  2465. break;
  2466. }
  2467. /*
  2468. * Return error if fw_state hasn't changed after max_wait
  2469. */
  2470. if (curr_abs_state == abs_state) {
  2471. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2472. "in %d secs\n", fw_state, max_wait);
  2473. return -ENODEV;
  2474. }
  2475. }
  2476. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2477. return 0;
  2478. }
  2479. /**
  2480. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2481. * @instance: Adapter soft state
  2482. */
  2483. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2484. {
  2485. int i;
  2486. u32 max_cmd = instance->max_mfi_cmds;
  2487. struct megasas_cmd *cmd;
  2488. if (!instance->frame_dma_pool)
  2489. return;
  2490. /*
  2491. * Return all frames to pool
  2492. */
  2493. for (i = 0; i < max_cmd; i++) {
  2494. cmd = instance->cmd_list[i];
  2495. if (cmd->frame)
  2496. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2497. cmd->frame_phys_addr);
  2498. if (cmd->sense)
  2499. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2500. cmd->sense_phys_addr);
  2501. }
  2502. /*
  2503. * Now destroy the pool itself
  2504. */
  2505. pci_pool_destroy(instance->frame_dma_pool);
  2506. pci_pool_destroy(instance->sense_dma_pool);
  2507. instance->frame_dma_pool = NULL;
  2508. instance->sense_dma_pool = NULL;
  2509. }
  2510. /**
  2511. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2512. * @instance: Adapter soft state
  2513. *
  2514. * Each command packet has an embedded DMA memory buffer that is used for
  2515. * filling MFI frame and the SG list that immediately follows the frame. This
  2516. * function creates those DMA memory buffers for each command packet by using
  2517. * PCI pool facility.
  2518. */
  2519. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2520. {
  2521. int i;
  2522. u32 max_cmd;
  2523. u32 sge_sz;
  2524. u32 sgl_sz;
  2525. u32 total_sz;
  2526. u32 frame_count;
  2527. struct megasas_cmd *cmd;
  2528. max_cmd = instance->max_mfi_cmds;
  2529. /*
  2530. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2531. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2532. */
  2533. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2534. sizeof(struct megasas_sge32);
  2535. if (instance->flag_ieee) {
  2536. sge_sz = sizeof(struct megasas_sge_skinny);
  2537. }
  2538. /*
  2539. * Calculated the number of 64byte frames required for SGL
  2540. */
  2541. sgl_sz = sge_sz * instance->max_num_sge;
  2542. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2543. frame_count = 15;
  2544. /*
  2545. * We need one extra frame for the MFI command
  2546. */
  2547. frame_count++;
  2548. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2549. /*
  2550. * Use DMA pool facility provided by PCI layer
  2551. */
  2552. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2553. instance->pdev, total_sz, 64,
  2554. 0);
  2555. if (!instance->frame_dma_pool) {
  2556. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2557. return -ENOMEM;
  2558. }
  2559. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2560. instance->pdev, 128, 4, 0);
  2561. if (!instance->sense_dma_pool) {
  2562. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2563. pci_pool_destroy(instance->frame_dma_pool);
  2564. instance->frame_dma_pool = NULL;
  2565. return -ENOMEM;
  2566. }
  2567. /*
  2568. * Allocate and attach a frame to each of the commands in cmd_list.
  2569. * By making cmd->index as the context instead of the &cmd, we can
  2570. * always use 32bit context regardless of the architecture
  2571. */
  2572. for (i = 0; i < max_cmd; i++) {
  2573. cmd = instance->cmd_list[i];
  2574. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2575. GFP_KERNEL, &cmd->frame_phys_addr);
  2576. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2577. GFP_KERNEL, &cmd->sense_phys_addr);
  2578. /*
  2579. * megasas_teardown_frame_pool() takes care of freeing
  2580. * whatever has been allocated
  2581. */
  2582. if (!cmd->frame || !cmd->sense) {
  2583. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2584. megasas_teardown_frame_pool(instance);
  2585. return -ENOMEM;
  2586. }
  2587. memset(cmd->frame, 0, total_sz);
  2588. cmd->frame->io.context = cpu_to_le32(cmd->index);
  2589. cmd->frame->io.pad_0 = 0;
  2590. if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
  2591. (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
  2592. (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
  2593. (reset_devices))
  2594. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  2595. }
  2596. return 0;
  2597. }
  2598. /**
  2599. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2600. * @instance: Adapter soft state
  2601. */
  2602. void megasas_free_cmds(struct megasas_instance *instance)
  2603. {
  2604. int i;
  2605. /* First free the MFI frame pool */
  2606. megasas_teardown_frame_pool(instance);
  2607. /* Free all the commands in the cmd_list */
  2608. for (i = 0; i < instance->max_mfi_cmds; i++)
  2609. kfree(instance->cmd_list[i]);
  2610. /* Free the cmd_list buffer itself */
  2611. kfree(instance->cmd_list);
  2612. instance->cmd_list = NULL;
  2613. INIT_LIST_HEAD(&instance->cmd_pool);
  2614. }
  2615. /**
  2616. * megasas_alloc_cmds - Allocates the command packets
  2617. * @instance: Adapter soft state
  2618. *
  2619. * Each command that is issued to the FW, whether IO commands from the OS or
  2620. * internal commands like IOCTLs, are wrapped in local data structure called
  2621. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2622. * the FW.
  2623. *
  2624. * Each frame has a 32-bit field called context (tag). This context is used
  2625. * to get back the megasas_cmd from the frame when a frame gets completed in
  2626. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2627. * the context. But we wanted to keep the differences between 32 and 64 bit
  2628. * systems to the mininum. We always use 32 bit integers for the context. In
  2629. * this driver, the 32 bit values are the indices into an array cmd_list.
  2630. * This array is used only to look up the megasas_cmd given the context. The
  2631. * free commands themselves are maintained in a linked list called cmd_pool.
  2632. */
  2633. int megasas_alloc_cmds(struct megasas_instance *instance)
  2634. {
  2635. int i;
  2636. int j;
  2637. u32 max_cmd;
  2638. struct megasas_cmd *cmd;
  2639. max_cmd = instance->max_mfi_cmds;
  2640. /*
  2641. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2642. * Allocate the dynamic array first and then allocate individual
  2643. * commands.
  2644. */
  2645. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2646. if (!instance->cmd_list) {
  2647. printk(KERN_DEBUG "megasas: out of memory\n");
  2648. return -ENOMEM;
  2649. }
  2650. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2651. for (i = 0; i < max_cmd; i++) {
  2652. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2653. GFP_KERNEL);
  2654. if (!instance->cmd_list[i]) {
  2655. for (j = 0; j < i; j++)
  2656. kfree(instance->cmd_list[j]);
  2657. kfree(instance->cmd_list);
  2658. instance->cmd_list = NULL;
  2659. return -ENOMEM;
  2660. }
  2661. }
  2662. /*
  2663. * Add all the commands to command pool (instance->cmd_pool)
  2664. */
  2665. for (i = 0; i < max_cmd; i++) {
  2666. cmd = instance->cmd_list[i];
  2667. memset(cmd, 0, sizeof(struct megasas_cmd));
  2668. cmd->index = i;
  2669. cmd->scmd = NULL;
  2670. cmd->instance = instance;
  2671. list_add_tail(&cmd->list, &instance->cmd_pool);
  2672. }
  2673. /*
  2674. * Create a frame pool and assign one frame to each cmd
  2675. */
  2676. if (megasas_create_frame_pool(instance)) {
  2677. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2678. megasas_free_cmds(instance);
  2679. }
  2680. return 0;
  2681. }
  2682. /*
  2683. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2684. * @instance: Adapter soft state
  2685. * @pd_list: pd_list structure
  2686. *
  2687. * Issues an internal command (DCMD) to get the FW's controller PD
  2688. * list structure. This information is mainly used to find out SYSTEM
  2689. * supported by the FW.
  2690. */
  2691. static int
  2692. megasas_get_pd_list(struct megasas_instance *instance)
  2693. {
  2694. int ret = 0, pd_index = 0;
  2695. struct megasas_cmd *cmd;
  2696. struct megasas_dcmd_frame *dcmd;
  2697. struct MR_PD_LIST *ci;
  2698. struct MR_PD_ADDRESS *pd_addr;
  2699. dma_addr_t ci_h = 0;
  2700. cmd = megasas_get_cmd(instance);
  2701. if (!cmd) {
  2702. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2703. return -ENOMEM;
  2704. }
  2705. dcmd = &cmd->frame->dcmd;
  2706. ci = pci_alloc_consistent(instance->pdev,
  2707. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2708. if (!ci) {
  2709. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2710. megasas_return_cmd(instance, cmd);
  2711. return -ENOMEM;
  2712. }
  2713. memset(ci, 0, sizeof(*ci));
  2714. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2715. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2716. dcmd->mbox.b[1] = 0;
  2717. dcmd->cmd = MFI_CMD_DCMD;
  2718. dcmd->cmd_status = 0xFF;
  2719. dcmd->sge_count = 1;
  2720. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  2721. dcmd->timeout = 0;
  2722. dcmd->pad_0 = 0;
  2723. dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  2724. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
  2725. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  2726. dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  2727. if (!megasas_issue_polled(instance, cmd)) {
  2728. ret = 0;
  2729. } else {
  2730. ret = -1;
  2731. }
  2732. /*
  2733. * the following function will get the instance PD LIST.
  2734. */
  2735. pd_addr = ci->addr;
  2736. if ( ret == 0 &&
  2737. (le32_to_cpu(ci->count) <
  2738. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2739. memset(instance->local_pd_list, 0,
  2740. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2741. for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
  2742. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
  2743. le16_to_cpu(pd_addr->deviceId);
  2744. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
  2745. pd_addr->scsiDevType;
  2746. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
  2747. MR_PD_STATE_SYSTEM;
  2748. pd_addr++;
  2749. }
  2750. memcpy(instance->pd_list, instance->local_pd_list,
  2751. sizeof(instance->pd_list));
  2752. }
  2753. pci_free_consistent(instance->pdev,
  2754. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2755. ci, ci_h);
  2756. megasas_return_cmd(instance, cmd);
  2757. return ret;
  2758. }
  2759. /*
  2760. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2761. * @instance: Adapter soft state
  2762. * @ld_list: ld_list structure
  2763. *
  2764. * Issues an internal command (DCMD) to get the FW's controller PD
  2765. * list structure. This information is mainly used to find out SYSTEM
  2766. * supported by the FW.
  2767. */
  2768. static int
  2769. megasas_get_ld_list(struct megasas_instance *instance)
  2770. {
  2771. int ret = 0, ld_index = 0, ids = 0;
  2772. struct megasas_cmd *cmd;
  2773. struct megasas_dcmd_frame *dcmd;
  2774. struct MR_LD_LIST *ci;
  2775. dma_addr_t ci_h = 0;
  2776. u32 ld_count;
  2777. cmd = megasas_get_cmd(instance);
  2778. if (!cmd) {
  2779. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2780. return -ENOMEM;
  2781. }
  2782. dcmd = &cmd->frame->dcmd;
  2783. ci = pci_alloc_consistent(instance->pdev,
  2784. sizeof(struct MR_LD_LIST),
  2785. &ci_h);
  2786. if (!ci) {
  2787. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2788. megasas_return_cmd(instance, cmd);
  2789. return -ENOMEM;
  2790. }
  2791. memset(ci, 0, sizeof(*ci));
  2792. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2793. dcmd->cmd = MFI_CMD_DCMD;
  2794. dcmd->cmd_status = 0xFF;
  2795. dcmd->sge_count = 1;
  2796. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  2797. dcmd->timeout = 0;
  2798. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
  2799. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
  2800. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  2801. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
  2802. dcmd->pad_0 = 0;
  2803. if (!megasas_issue_polled(instance, cmd)) {
  2804. ret = 0;
  2805. } else {
  2806. ret = -1;
  2807. }
  2808. ld_count = le32_to_cpu(ci->ldCount);
  2809. /* the following function will get the instance PD LIST */
  2810. if ((ret == 0) && (ld_count <= MAX_LOGICAL_DRIVES)) {
  2811. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2812. for (ld_index = 0; ld_index < ld_count; ld_index++) {
  2813. if (ci->ldList[ld_index].state != 0) {
  2814. ids = ci->ldList[ld_index].ref.targetId;
  2815. instance->ld_ids[ids] =
  2816. ci->ldList[ld_index].ref.targetId;
  2817. }
  2818. }
  2819. }
  2820. pci_free_consistent(instance->pdev,
  2821. sizeof(struct MR_LD_LIST),
  2822. ci,
  2823. ci_h);
  2824. megasas_return_cmd(instance, cmd);
  2825. return ret;
  2826. }
  2827. /**
  2828. * megasas_ld_list_query - Returns FW's ld_list structure
  2829. * @instance: Adapter soft state
  2830. * @ld_list: ld_list structure
  2831. *
  2832. * Issues an internal command (DCMD) to get the FW's controller PD
  2833. * list structure. This information is mainly used to find out SYSTEM
  2834. * supported by the FW.
  2835. */
  2836. static int
  2837. megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
  2838. {
  2839. int ret = 0, ld_index = 0, ids = 0;
  2840. struct megasas_cmd *cmd;
  2841. struct megasas_dcmd_frame *dcmd;
  2842. struct MR_LD_TARGETID_LIST *ci;
  2843. dma_addr_t ci_h = 0;
  2844. u32 tgtid_count;
  2845. cmd = megasas_get_cmd(instance);
  2846. if (!cmd) {
  2847. printk(KERN_WARNING
  2848. "megasas:(megasas_ld_list_query): Failed to get cmd\n");
  2849. return -ENOMEM;
  2850. }
  2851. dcmd = &cmd->frame->dcmd;
  2852. ci = pci_alloc_consistent(instance->pdev,
  2853. sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
  2854. if (!ci) {
  2855. printk(KERN_WARNING
  2856. "megasas: Failed to alloc mem for ld_list_query\n");
  2857. megasas_return_cmd(instance, cmd);
  2858. return -ENOMEM;
  2859. }
  2860. memset(ci, 0, sizeof(*ci));
  2861. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2862. dcmd->mbox.b[0] = query_type;
  2863. dcmd->cmd = MFI_CMD_DCMD;
  2864. dcmd->cmd_status = 0xFF;
  2865. dcmd->sge_count = 1;
  2866. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  2867. dcmd->timeout = 0;
  2868. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  2869. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
  2870. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  2871. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  2872. dcmd->pad_0 = 0;
  2873. if (!megasas_issue_polled(instance, cmd) && !dcmd->cmd_status) {
  2874. ret = 0;
  2875. } else {
  2876. /* On failure, call older LD list DCMD */
  2877. ret = 1;
  2878. }
  2879. tgtid_count = le32_to_cpu(ci->count);
  2880. if ((ret == 0) && (tgtid_count <= (MAX_LOGICAL_DRIVES))) {
  2881. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2882. for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
  2883. ids = ci->targetId[ld_index];
  2884. instance->ld_ids[ids] = ci->targetId[ld_index];
  2885. }
  2886. }
  2887. pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
  2888. ci, ci_h);
  2889. megasas_return_cmd(instance, cmd);
  2890. return ret;
  2891. }
  2892. /**
  2893. * megasas_get_controller_info - Returns FW's controller structure
  2894. * @instance: Adapter soft state
  2895. * @ctrl_info: Controller information structure
  2896. *
  2897. * Issues an internal command (DCMD) to get the FW's controller structure.
  2898. * This information is mainly used to find out the maximum IO transfer per
  2899. * command supported by the FW.
  2900. */
  2901. static int
  2902. megasas_get_ctrl_info(struct megasas_instance *instance,
  2903. struct megasas_ctrl_info *ctrl_info)
  2904. {
  2905. int ret = 0;
  2906. struct megasas_cmd *cmd;
  2907. struct megasas_dcmd_frame *dcmd;
  2908. struct megasas_ctrl_info *ci;
  2909. dma_addr_t ci_h = 0;
  2910. cmd = megasas_get_cmd(instance);
  2911. if (!cmd) {
  2912. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2913. return -ENOMEM;
  2914. }
  2915. dcmd = &cmd->frame->dcmd;
  2916. ci = pci_alloc_consistent(instance->pdev,
  2917. sizeof(struct megasas_ctrl_info), &ci_h);
  2918. if (!ci) {
  2919. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2920. megasas_return_cmd(instance, cmd);
  2921. return -ENOMEM;
  2922. }
  2923. memset(ci, 0, sizeof(*ci));
  2924. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2925. dcmd->cmd = MFI_CMD_DCMD;
  2926. dcmd->cmd_status = 0xFF;
  2927. dcmd->sge_count = 1;
  2928. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  2929. dcmd->timeout = 0;
  2930. dcmd->pad_0 = 0;
  2931. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  2932. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
  2933. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  2934. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  2935. if (!megasas_issue_polled(instance, cmd)) {
  2936. ret = 0;
  2937. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2938. } else {
  2939. ret = -1;
  2940. }
  2941. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2942. ci, ci_h);
  2943. megasas_return_cmd(instance, cmd);
  2944. return ret;
  2945. }
  2946. /**
  2947. * megasas_issue_init_mfi - Initializes the FW
  2948. * @instance: Adapter soft state
  2949. *
  2950. * Issues the INIT MFI cmd
  2951. */
  2952. static int
  2953. megasas_issue_init_mfi(struct megasas_instance *instance)
  2954. {
  2955. u32 context;
  2956. struct megasas_cmd *cmd;
  2957. struct megasas_init_frame *init_frame;
  2958. struct megasas_init_queue_info *initq_info;
  2959. dma_addr_t init_frame_h;
  2960. dma_addr_t initq_info_h;
  2961. /*
  2962. * Prepare a init frame. Note the init frame points to queue info
  2963. * structure. Each frame has SGL allocated after first 64 bytes. For
  2964. * this frame - since we don't need any SGL - we use SGL's space as
  2965. * queue info structure
  2966. *
  2967. * We will not get a NULL command below. We just created the pool.
  2968. */
  2969. cmd = megasas_get_cmd(instance);
  2970. init_frame = (struct megasas_init_frame *)cmd->frame;
  2971. initq_info = (struct megasas_init_queue_info *)
  2972. ((unsigned long)init_frame + 64);
  2973. init_frame_h = cmd->frame_phys_addr;
  2974. initq_info_h = init_frame_h + 64;
  2975. context = init_frame->context;
  2976. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2977. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2978. init_frame->context = context;
  2979. initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
  2980. initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
  2981. initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
  2982. initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
  2983. init_frame->cmd = MFI_CMD_INIT;
  2984. init_frame->cmd_status = 0xFF;
  2985. init_frame->queue_info_new_phys_addr_lo =
  2986. cpu_to_le32(lower_32_bits(initq_info_h));
  2987. init_frame->queue_info_new_phys_addr_hi =
  2988. cpu_to_le32(upper_32_bits(initq_info_h));
  2989. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
  2990. /*
  2991. * disable the intr before firing the init frame to FW
  2992. */
  2993. instance->instancet->disable_intr(instance);
  2994. /*
  2995. * Issue the init frame in polled mode
  2996. */
  2997. if (megasas_issue_polled(instance, cmd)) {
  2998. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2999. megasas_return_cmd(instance, cmd);
  3000. goto fail_fw_init;
  3001. }
  3002. megasas_return_cmd(instance, cmd);
  3003. return 0;
  3004. fail_fw_init:
  3005. return -EINVAL;
  3006. }
  3007. static u32
  3008. megasas_init_adapter_mfi(struct megasas_instance *instance)
  3009. {
  3010. struct megasas_register_set __iomem *reg_set;
  3011. u32 context_sz;
  3012. u32 reply_q_sz;
  3013. reg_set = instance->reg_set;
  3014. /*
  3015. * Get various operational parameters from status register
  3016. */
  3017. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  3018. /*
  3019. * Reduce the max supported cmds by 1. This is to ensure that the
  3020. * reply_q_sz (1 more than the max cmd that driver may send)
  3021. * does not exceed max cmds that the FW can support
  3022. */
  3023. instance->max_fw_cmds = instance->max_fw_cmds-1;
  3024. instance->max_mfi_cmds = instance->max_fw_cmds;
  3025. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  3026. 0x10;
  3027. /*
  3028. * Create a pool of commands
  3029. */
  3030. if (megasas_alloc_cmds(instance))
  3031. goto fail_alloc_cmds;
  3032. /*
  3033. * Allocate memory for reply queue. Length of reply queue should
  3034. * be _one_ more than the maximum commands handled by the firmware.
  3035. *
  3036. * Note: When FW completes commands, it places corresponding contex
  3037. * values in this circular reply queue. This circular queue is a fairly
  3038. * typical producer-consumer queue. FW is the producer (of completed
  3039. * commands) and the driver is the consumer.
  3040. */
  3041. context_sz = sizeof(u32);
  3042. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  3043. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  3044. reply_q_sz,
  3045. &instance->reply_queue_h);
  3046. if (!instance->reply_queue) {
  3047. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  3048. goto fail_reply_queue;
  3049. }
  3050. if (megasas_issue_init_mfi(instance))
  3051. goto fail_fw_init;
  3052. instance->fw_support_ieee = 0;
  3053. instance->fw_support_ieee =
  3054. (instance->instancet->read_fw_status_reg(reg_set) &
  3055. 0x04000000);
  3056. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  3057. instance->fw_support_ieee);
  3058. if (instance->fw_support_ieee)
  3059. instance->flag_ieee = 1;
  3060. return 0;
  3061. fail_fw_init:
  3062. pci_free_consistent(instance->pdev, reply_q_sz,
  3063. instance->reply_queue, instance->reply_queue_h);
  3064. fail_reply_queue:
  3065. megasas_free_cmds(instance);
  3066. fail_alloc_cmds:
  3067. return 1;
  3068. }
  3069. /**
  3070. * megasas_init_fw - Initializes the FW
  3071. * @instance: Adapter soft state
  3072. *
  3073. * This is the main function for initializing firmware
  3074. */
  3075. static int megasas_init_fw(struct megasas_instance *instance)
  3076. {
  3077. u32 max_sectors_1;
  3078. u32 max_sectors_2;
  3079. u32 tmp_sectors, msix_enable, scratch_pad_2;
  3080. struct megasas_register_set __iomem *reg_set;
  3081. struct megasas_ctrl_info *ctrl_info;
  3082. unsigned long bar_list;
  3083. int i, loop, fw_msix_count = 0;
  3084. /* Find first memory bar */
  3085. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  3086. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  3087. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  3088. if (pci_request_selected_regions(instance->pdev, instance->bar,
  3089. "megasas: LSI")) {
  3090. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  3091. return -EBUSY;
  3092. }
  3093. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  3094. if (!instance->reg_set) {
  3095. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  3096. goto fail_ioremap;
  3097. }
  3098. reg_set = instance->reg_set;
  3099. switch (instance->pdev->device) {
  3100. case PCI_DEVICE_ID_LSI_FUSION:
  3101. case PCI_DEVICE_ID_LSI_INVADER:
  3102. case PCI_DEVICE_ID_LSI_FURY:
  3103. instance->instancet = &megasas_instance_template_fusion;
  3104. break;
  3105. case PCI_DEVICE_ID_LSI_SAS1078R:
  3106. case PCI_DEVICE_ID_LSI_SAS1078DE:
  3107. instance->instancet = &megasas_instance_template_ppc;
  3108. break;
  3109. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  3110. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  3111. instance->instancet = &megasas_instance_template_gen2;
  3112. break;
  3113. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  3114. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  3115. instance->instancet = &megasas_instance_template_skinny;
  3116. break;
  3117. case PCI_DEVICE_ID_LSI_SAS1064R:
  3118. case PCI_DEVICE_ID_DELL_PERC5:
  3119. default:
  3120. instance->instancet = &megasas_instance_template_xscale;
  3121. break;
  3122. }
  3123. if (megasas_transition_to_ready(instance, 0)) {
  3124. atomic_set(&instance->fw_reset_no_pci_access, 1);
  3125. instance->instancet->adp_reset
  3126. (instance, instance->reg_set);
  3127. atomic_set(&instance->fw_reset_no_pci_access, 0);
  3128. dev_info(&instance->pdev->dev,
  3129. "megasas: FW restarted successfully from %s!\n",
  3130. __func__);
  3131. /*waitting for about 30 second before retry*/
  3132. ssleep(30);
  3133. if (megasas_transition_to_ready(instance, 0))
  3134. goto fail_ready_state;
  3135. }
  3136. /*
  3137. * MSI-X host index 0 is common for all adapter.
  3138. * It is used for all MPT based Adapters.
  3139. */
  3140. instance->reply_post_host_index_addr[0] =
  3141. (u32 *)((u8 *)instance->reg_set +
  3142. MPI2_REPLY_POST_HOST_INDEX_OFFSET);
  3143. /* Check if MSI-X is supported while in ready state */
  3144. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  3145. 0x4000000) >> 0x1a;
  3146. if (msix_enable && !msix_disable) {
  3147. scratch_pad_2 = readl
  3148. (&instance->reg_set->outbound_scratch_pad_2);
  3149. /* Check max MSI-X vectors */
  3150. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
  3151. instance->msix_vectors = (scratch_pad_2
  3152. & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
  3153. fw_msix_count = instance->msix_vectors;
  3154. if (msix_vectors)
  3155. instance->msix_vectors =
  3156. min(msix_vectors,
  3157. instance->msix_vectors);
  3158. } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
  3159. || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  3160. /* Invader/Fury supports more than 8 MSI-X */
  3161. instance->msix_vectors = ((scratch_pad_2
  3162. & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
  3163. >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
  3164. fw_msix_count = instance->msix_vectors;
  3165. /* Save 1-15 reply post index address to local memory
  3166. * Index 0 is already saved from reg offset
  3167. * MPI2_REPLY_POST_HOST_INDEX_OFFSET
  3168. */
  3169. for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
  3170. instance->reply_post_host_index_addr[loop] =
  3171. (u32 *)((u8 *)instance->reg_set +
  3172. MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
  3173. + (loop * 0x10));
  3174. }
  3175. if (msix_vectors)
  3176. instance->msix_vectors = min(msix_vectors,
  3177. instance->msix_vectors);
  3178. } else
  3179. instance->msix_vectors = 1;
  3180. /* Don't bother allocating more MSI-X vectors than cpus */
  3181. instance->msix_vectors = min(instance->msix_vectors,
  3182. (unsigned int)num_online_cpus());
  3183. for (i = 0; i < instance->msix_vectors; i++)
  3184. instance->msixentry[i].entry = i;
  3185. i = pci_enable_msix(instance->pdev, instance->msixentry,
  3186. instance->msix_vectors);
  3187. if (i >= 0) {
  3188. if (i) {
  3189. if (!pci_enable_msix(instance->pdev,
  3190. instance->msixentry, i))
  3191. instance->msix_vectors = i;
  3192. else
  3193. instance->msix_vectors = 0;
  3194. }
  3195. } else
  3196. instance->msix_vectors = 0;
  3197. dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
  3198. "<%d> MSIX vector,Online CPUs: <%d>,"
  3199. "Current MSIX <%d>\n", instance->host->host_no,
  3200. fw_msix_count, (unsigned int)num_online_cpus(),
  3201. instance->msix_vectors);
  3202. }
  3203. /* Get operational params, sge flags, send init cmd to controller */
  3204. if (instance->instancet->init_adapter(instance))
  3205. goto fail_init_adapter;
  3206. printk(KERN_ERR "megasas: INIT adapter done\n");
  3207. /** for passthrough
  3208. * the following function will get the PD LIST.
  3209. */
  3210. memset(instance->pd_list, 0 ,
  3211. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  3212. megasas_get_pd_list(instance);
  3213. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3214. if (megasas_ld_list_query(instance,
  3215. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  3216. megasas_get_ld_list(instance);
  3217. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3218. /*
  3219. * Compute the max allowed sectors per IO: The controller info has two
  3220. * limits on max sectors. Driver should use the minimum of these two.
  3221. *
  3222. * 1 << stripe_sz_ops.min = max sectors per strip
  3223. *
  3224. * Note that older firmwares ( < FW ver 30) didn't report information
  3225. * to calculate max_sectors_1. So the number ended up as zero always.
  3226. */
  3227. tmp_sectors = 0;
  3228. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3229. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3230. le16_to_cpu(ctrl_info->max_strips_per_io);
  3231. max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
  3232. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3233. /*Check whether controller is iMR or MR */
  3234. if (ctrl_info->memory_size) {
  3235. instance->is_imr = 0;
  3236. dev_info(&instance->pdev->dev, "Controller type: MR,"
  3237. "Memory size is: %dMB\n",
  3238. le16_to_cpu(ctrl_info->memory_size));
  3239. } else {
  3240. instance->is_imr = 1;
  3241. dev_info(&instance->pdev->dev,
  3242. "Controller type: iMR\n");
  3243. }
  3244. /* OnOffProperties are converted into CPU arch*/
  3245. le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
  3246. instance->disableOnlineCtrlReset =
  3247. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3248. /* adapterOperations2 are converted into CPU arch*/
  3249. le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
  3250. instance->UnevenSpanSupport =
  3251. ctrl_info->adapterOperations2.supportUnevenSpans;
  3252. if (instance->UnevenSpanSupport) {
  3253. struct fusion_context *fusion = instance->ctrl_context;
  3254. dev_info(&instance->pdev->dev, "FW supports: "
  3255. "UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
  3256. if (MR_ValidateMapInfo(instance))
  3257. fusion->fast_path_io = 1;
  3258. else
  3259. fusion->fast_path_io = 0;
  3260. }
  3261. }
  3262. instance->max_sectors_per_req = instance->max_num_sge *
  3263. PAGE_SIZE / 512;
  3264. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3265. instance->max_sectors_per_req = tmp_sectors;
  3266. kfree(ctrl_info);
  3267. /* Check for valid throttlequeuedepth module parameter */
  3268. if (instance->is_imr) {
  3269. if (throttlequeuedepth > (instance->max_fw_cmds -
  3270. MEGASAS_SKINNY_INT_CMDS))
  3271. instance->throttlequeuedepth =
  3272. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3273. else
  3274. instance->throttlequeuedepth = throttlequeuedepth;
  3275. } else {
  3276. if (throttlequeuedepth > (instance->max_fw_cmds -
  3277. MEGASAS_INT_CMDS))
  3278. instance->throttlequeuedepth =
  3279. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3280. else
  3281. instance->throttlequeuedepth = throttlequeuedepth;
  3282. }
  3283. /*
  3284. * Setup tasklet for cmd completion
  3285. */
  3286. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3287. (unsigned long)instance);
  3288. return 0;
  3289. fail_init_adapter:
  3290. fail_ready_state:
  3291. iounmap(instance->reg_set);
  3292. fail_ioremap:
  3293. pci_release_selected_regions(instance->pdev, instance->bar);
  3294. return -EINVAL;
  3295. }
  3296. /**
  3297. * megasas_release_mfi - Reverses the FW initialization
  3298. * @intance: Adapter soft state
  3299. */
  3300. static void megasas_release_mfi(struct megasas_instance *instance)
  3301. {
  3302. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3303. if (instance->reply_queue)
  3304. pci_free_consistent(instance->pdev, reply_q_sz,
  3305. instance->reply_queue, instance->reply_queue_h);
  3306. megasas_free_cmds(instance);
  3307. iounmap(instance->reg_set);
  3308. pci_release_selected_regions(instance->pdev, instance->bar);
  3309. }
  3310. /**
  3311. * megasas_get_seq_num - Gets latest event sequence numbers
  3312. * @instance: Adapter soft state
  3313. * @eli: FW event log sequence numbers information
  3314. *
  3315. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3316. * usually find out the latest sequence number of the events, the seq number at
  3317. * the boot etc. They would "read" all the events below the latest seq number
  3318. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3319. * number), they would subsribe to AEN (asynchronous event notification) and
  3320. * wait for the events to happen.
  3321. */
  3322. static int
  3323. megasas_get_seq_num(struct megasas_instance *instance,
  3324. struct megasas_evt_log_info *eli)
  3325. {
  3326. struct megasas_cmd *cmd;
  3327. struct megasas_dcmd_frame *dcmd;
  3328. struct megasas_evt_log_info *el_info;
  3329. dma_addr_t el_info_h = 0;
  3330. cmd = megasas_get_cmd(instance);
  3331. if (!cmd) {
  3332. return -ENOMEM;
  3333. }
  3334. dcmd = &cmd->frame->dcmd;
  3335. el_info = pci_alloc_consistent(instance->pdev,
  3336. sizeof(struct megasas_evt_log_info),
  3337. &el_info_h);
  3338. if (!el_info) {
  3339. megasas_return_cmd(instance, cmd);
  3340. return -ENOMEM;
  3341. }
  3342. memset(el_info, 0, sizeof(*el_info));
  3343. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3344. dcmd->cmd = MFI_CMD_DCMD;
  3345. dcmd->cmd_status = 0x0;
  3346. dcmd->sge_count = 1;
  3347. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3348. dcmd->timeout = 0;
  3349. dcmd->pad_0 = 0;
  3350. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  3351. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
  3352. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
  3353. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  3354. megasas_issue_blocked_cmd(instance, cmd);
  3355. /*
  3356. * Copy the data back into callers buffer
  3357. */
  3358. eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
  3359. eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
  3360. eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
  3361. eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
  3362. eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
  3363. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3364. el_info, el_info_h);
  3365. megasas_return_cmd(instance, cmd);
  3366. return 0;
  3367. }
  3368. /**
  3369. * megasas_register_aen - Registers for asynchronous event notification
  3370. * @instance: Adapter soft state
  3371. * @seq_num: The starting sequence number
  3372. * @class_locale: Class of the event
  3373. *
  3374. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3375. * to be notified if and only if the event is of type @class_locale
  3376. */
  3377. static int
  3378. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3379. u32 class_locale_word)
  3380. {
  3381. int ret_val;
  3382. struct megasas_cmd *cmd;
  3383. struct megasas_dcmd_frame *dcmd;
  3384. union megasas_evt_class_locale curr_aen;
  3385. union megasas_evt_class_locale prev_aen;
  3386. /*
  3387. * If there an AEN pending already (aen_cmd), check if the
  3388. * class_locale of that pending AEN is inclusive of the new
  3389. * AEN request we currently have. If it is, then we don't have
  3390. * to do anything. In other words, whichever events the current
  3391. * AEN request is subscribing to, have already been subscribed
  3392. * to.
  3393. *
  3394. * If the old_cmd is _not_ inclusive, then we have to abort
  3395. * that command, form a class_locale that is superset of both
  3396. * old and current and re-issue to the FW
  3397. */
  3398. curr_aen.word = class_locale_word;
  3399. if (instance->aen_cmd) {
  3400. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3401. prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
  3402. /*
  3403. * A class whose enum value is smaller is inclusive of all
  3404. * higher values. If a PROGRESS (= -1) was previously
  3405. * registered, then a new registration requests for higher
  3406. * classes need not be sent to FW. They are automatically
  3407. * included.
  3408. *
  3409. * Locale numbers don't have such hierarchy. They are bitmap
  3410. * values
  3411. */
  3412. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3413. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3414. curr_aen.members.locale)) {
  3415. /*
  3416. * Previously issued event registration includes
  3417. * current request. Nothing to do.
  3418. */
  3419. return 0;
  3420. } else {
  3421. curr_aen.members.locale |= prev_aen.members.locale;
  3422. if (prev_aen.members.class < curr_aen.members.class)
  3423. curr_aen.members.class = prev_aen.members.class;
  3424. instance->aen_cmd->abort_aen = 1;
  3425. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3426. instance->
  3427. aen_cmd);
  3428. if (ret_val) {
  3429. printk(KERN_DEBUG "megasas: Failed to abort "
  3430. "previous AEN command\n");
  3431. return ret_val;
  3432. }
  3433. }
  3434. }
  3435. cmd = megasas_get_cmd(instance);
  3436. if (!cmd)
  3437. return -ENOMEM;
  3438. dcmd = &cmd->frame->dcmd;
  3439. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3440. /*
  3441. * Prepare DCMD for aen registration
  3442. */
  3443. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3444. dcmd->cmd = MFI_CMD_DCMD;
  3445. dcmd->cmd_status = 0x0;
  3446. dcmd->sge_count = 1;
  3447. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3448. dcmd->timeout = 0;
  3449. dcmd->pad_0 = 0;
  3450. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
  3451. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
  3452. dcmd->mbox.w[0] = cpu_to_le32(seq_num);
  3453. instance->last_seq_num = seq_num;
  3454. dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
  3455. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
  3456. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
  3457. if (instance->aen_cmd != NULL) {
  3458. megasas_return_cmd(instance, cmd);
  3459. return 0;
  3460. }
  3461. /*
  3462. * Store reference to the cmd used to register for AEN. When an
  3463. * application wants us to register for AEN, we have to abort this
  3464. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3465. */
  3466. instance->aen_cmd = cmd;
  3467. /*
  3468. * Issue the aen registration frame
  3469. */
  3470. instance->instancet->issue_dcmd(instance, cmd);
  3471. return 0;
  3472. }
  3473. /**
  3474. * megasas_start_aen - Subscribes to AEN during driver load time
  3475. * @instance: Adapter soft state
  3476. */
  3477. static int megasas_start_aen(struct megasas_instance *instance)
  3478. {
  3479. struct megasas_evt_log_info eli;
  3480. union megasas_evt_class_locale class_locale;
  3481. /*
  3482. * Get the latest sequence number from FW
  3483. */
  3484. memset(&eli, 0, sizeof(eli));
  3485. if (megasas_get_seq_num(instance, &eli))
  3486. return -1;
  3487. /*
  3488. * Register AEN with FW for latest sequence number plus 1
  3489. */
  3490. class_locale.members.reserved = 0;
  3491. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3492. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3493. return megasas_register_aen(instance,
  3494. eli.newest_seq_num + 1,
  3495. class_locale.word);
  3496. }
  3497. /**
  3498. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3499. * @instance: Adapter soft state
  3500. */
  3501. static int megasas_io_attach(struct megasas_instance *instance)
  3502. {
  3503. struct Scsi_Host *host = instance->host;
  3504. /*
  3505. * Export parameters required by SCSI mid-layer
  3506. */
  3507. host->irq = instance->pdev->irq;
  3508. host->unique_id = instance->unique_id;
  3509. if (instance->is_imr) {
  3510. host->can_queue =
  3511. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3512. } else
  3513. host->can_queue =
  3514. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3515. host->this_id = instance->init_id;
  3516. host->sg_tablesize = instance->max_num_sge;
  3517. if (instance->fw_support_ieee)
  3518. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3519. /*
  3520. * Check if the module parameter value for max_sectors can be used
  3521. */
  3522. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3523. instance->max_sectors_per_req = max_sectors;
  3524. else {
  3525. if (max_sectors) {
  3526. if (((instance->pdev->device ==
  3527. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3528. (instance->pdev->device ==
  3529. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3530. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3531. instance->max_sectors_per_req = max_sectors;
  3532. } else {
  3533. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3534. "and <= %d (or < 1MB for GEN2 controller)\n",
  3535. instance->max_sectors_per_req);
  3536. }
  3537. }
  3538. }
  3539. host->max_sectors = instance->max_sectors_per_req;
  3540. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3541. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3542. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3543. host->max_lun = MEGASAS_MAX_LUN;
  3544. host->max_cmd_len = 16;
  3545. /* Fusion only supports host reset */
  3546. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3547. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  3548. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  3549. host->hostt->eh_device_reset_handler = NULL;
  3550. host->hostt->eh_bus_reset_handler = NULL;
  3551. }
  3552. /*
  3553. * Notify the mid-layer about the new controller
  3554. */
  3555. if (scsi_add_host(host, &instance->pdev->dev)) {
  3556. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3557. return -ENODEV;
  3558. }
  3559. /*
  3560. * Trigger SCSI to scan our drives
  3561. */
  3562. scsi_scan_host(host);
  3563. return 0;
  3564. }
  3565. static int
  3566. megasas_set_dma_mask(struct pci_dev *pdev)
  3567. {
  3568. /*
  3569. * All our contollers are capable of performing 64-bit DMA
  3570. */
  3571. if (IS_DMA64) {
  3572. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3573. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3574. goto fail_set_dma_mask;
  3575. }
  3576. } else {
  3577. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3578. goto fail_set_dma_mask;
  3579. }
  3580. return 0;
  3581. fail_set_dma_mask:
  3582. return 1;
  3583. }
  3584. /**
  3585. * megasas_probe_one - PCI hotplug entry point
  3586. * @pdev: PCI device structure
  3587. * @id: PCI ids of supported hotplugged adapter
  3588. */
  3589. static int megasas_probe_one(struct pci_dev *pdev,
  3590. const struct pci_device_id *id)
  3591. {
  3592. int rval, pos, i, j;
  3593. struct Scsi_Host *host;
  3594. struct megasas_instance *instance;
  3595. u16 control = 0;
  3596. /* Reset MSI-X in the kdump kernel */
  3597. if (reset_devices) {
  3598. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3599. if (pos) {
  3600. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
  3601. &control);
  3602. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3603. dev_info(&pdev->dev, "resetting MSI-X\n");
  3604. pci_write_config_word(pdev,
  3605. pos + PCI_MSIX_FLAGS,
  3606. control &
  3607. ~PCI_MSIX_FLAGS_ENABLE);
  3608. }
  3609. }
  3610. }
  3611. /*
  3612. * Announce PCI information
  3613. */
  3614. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3615. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3616. pdev->subsystem_device);
  3617. printk("bus %d:slot %d:func %d\n",
  3618. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3619. /*
  3620. * PCI prepping: enable device set bus mastering and dma mask
  3621. */
  3622. rval = pci_enable_device_mem(pdev);
  3623. if (rval) {
  3624. return rval;
  3625. }
  3626. pci_set_master(pdev);
  3627. if (megasas_set_dma_mask(pdev))
  3628. goto fail_set_dma_mask;
  3629. host = scsi_host_alloc(&megasas_template,
  3630. sizeof(struct megasas_instance));
  3631. if (!host) {
  3632. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3633. goto fail_alloc_instance;
  3634. }
  3635. instance = (struct megasas_instance *)host->hostdata;
  3636. memset(instance, 0, sizeof(*instance));
  3637. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3638. instance->pdev = pdev;
  3639. switch (instance->pdev->device) {
  3640. case PCI_DEVICE_ID_LSI_FUSION:
  3641. case PCI_DEVICE_ID_LSI_INVADER:
  3642. case PCI_DEVICE_ID_LSI_FURY:
  3643. {
  3644. struct fusion_context *fusion;
  3645. instance->ctrl_context =
  3646. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3647. if (!instance->ctrl_context) {
  3648. printk(KERN_DEBUG "megasas: Failed to allocate "
  3649. "memory for Fusion context info\n");
  3650. goto fail_alloc_dma_buf;
  3651. }
  3652. fusion = instance->ctrl_context;
  3653. INIT_LIST_HEAD(&fusion->cmd_pool);
  3654. spin_lock_init(&fusion->cmd_pool_lock);
  3655. }
  3656. break;
  3657. default: /* For all other supported controllers */
  3658. instance->producer =
  3659. pci_alloc_consistent(pdev, sizeof(u32),
  3660. &instance->producer_h);
  3661. instance->consumer =
  3662. pci_alloc_consistent(pdev, sizeof(u32),
  3663. &instance->consumer_h);
  3664. if (!instance->producer || !instance->consumer) {
  3665. printk(KERN_DEBUG "megasas: Failed to allocate"
  3666. "memory for producer, consumer\n");
  3667. goto fail_alloc_dma_buf;
  3668. }
  3669. *instance->producer = 0;
  3670. *instance->consumer = 0;
  3671. break;
  3672. }
  3673. megasas_poll_wait_aen = 0;
  3674. instance->flag_ieee = 0;
  3675. instance->ev = NULL;
  3676. instance->issuepend_done = 1;
  3677. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3678. instance->is_imr = 0;
  3679. megasas_poll_wait_aen = 0;
  3680. instance->evt_detail = pci_alloc_consistent(pdev,
  3681. sizeof(struct
  3682. megasas_evt_detail),
  3683. &instance->evt_detail_h);
  3684. if (!instance->evt_detail) {
  3685. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3686. "event detail structure\n");
  3687. goto fail_alloc_dma_buf;
  3688. }
  3689. /*
  3690. * Initialize locks and queues
  3691. */
  3692. INIT_LIST_HEAD(&instance->cmd_pool);
  3693. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3694. atomic_set(&instance->fw_outstanding,0);
  3695. init_waitqueue_head(&instance->int_cmd_wait_q);
  3696. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3697. spin_lock_init(&instance->cmd_pool_lock);
  3698. spin_lock_init(&instance->hba_lock);
  3699. spin_lock_init(&instance->completion_lock);
  3700. mutex_init(&instance->aen_mutex);
  3701. mutex_init(&instance->reset_mutex);
  3702. /*
  3703. * Initialize PCI related and misc parameters
  3704. */
  3705. instance->host = host;
  3706. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3707. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3708. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3709. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3710. instance->flag_ieee = 1;
  3711. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3712. } else
  3713. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3714. megasas_dbg_lvl = 0;
  3715. instance->flag = 0;
  3716. instance->unload = 1;
  3717. instance->last_time = 0;
  3718. instance->disableOnlineCtrlReset = 1;
  3719. instance->UnevenSpanSupport = 0;
  3720. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3721. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  3722. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  3723. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3724. else
  3725. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3726. /*
  3727. * Initialize MFI Firmware
  3728. */
  3729. if (megasas_init_fw(instance))
  3730. goto fail_init_mfi;
  3731. retry_irq_register:
  3732. /*
  3733. * Register IRQ
  3734. */
  3735. if (instance->msix_vectors) {
  3736. for (i = 0 ; i < instance->msix_vectors; i++) {
  3737. instance->irq_context[i].instance = instance;
  3738. instance->irq_context[i].MSIxIndex = i;
  3739. if (request_irq(instance->msixentry[i].vector,
  3740. instance->instancet->service_isr, 0,
  3741. "megasas",
  3742. &instance->irq_context[i])) {
  3743. printk(KERN_DEBUG "megasas: Failed to "
  3744. "register IRQ for vector %d.\n", i);
  3745. for (j = 0 ; j < i ; j++)
  3746. free_irq(
  3747. instance->msixentry[j].vector,
  3748. &instance->irq_context[j]);
  3749. /* Retry irq register for IO_APIC */
  3750. instance->msix_vectors = 0;
  3751. goto retry_irq_register;
  3752. }
  3753. }
  3754. } else {
  3755. instance->irq_context[0].instance = instance;
  3756. instance->irq_context[0].MSIxIndex = 0;
  3757. if (request_irq(pdev->irq, instance->instancet->service_isr,
  3758. IRQF_SHARED, "megasas",
  3759. &instance->irq_context[0])) {
  3760. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3761. goto fail_irq;
  3762. }
  3763. }
  3764. instance->instancet->enable_intr(instance);
  3765. /*
  3766. * Store instance in PCI softstate
  3767. */
  3768. pci_set_drvdata(pdev, instance);
  3769. /*
  3770. * Add this controller to megasas_mgmt_info structure so that it
  3771. * can be exported to management applications
  3772. */
  3773. megasas_mgmt_info.count++;
  3774. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3775. megasas_mgmt_info.max_index++;
  3776. /*
  3777. * Register with SCSI mid-layer
  3778. */
  3779. if (megasas_io_attach(instance))
  3780. goto fail_io_attach;
  3781. instance->unload = 0;
  3782. /*
  3783. * Initiate AEN (Asynchronous Event Notification)
  3784. */
  3785. if (megasas_start_aen(instance)) {
  3786. printk(KERN_DEBUG "megasas: start aen failed\n");
  3787. goto fail_start_aen;
  3788. }
  3789. return 0;
  3790. fail_start_aen:
  3791. fail_io_attach:
  3792. megasas_mgmt_info.count--;
  3793. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3794. megasas_mgmt_info.max_index--;
  3795. pci_set_drvdata(pdev, NULL);
  3796. instance->instancet->disable_intr(instance);
  3797. if (instance->msix_vectors)
  3798. for (i = 0 ; i < instance->msix_vectors; i++)
  3799. free_irq(instance->msixentry[i].vector,
  3800. &instance->irq_context[i]);
  3801. else
  3802. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3803. fail_irq:
  3804. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3805. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  3806. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  3807. megasas_release_fusion(instance);
  3808. else
  3809. megasas_release_mfi(instance);
  3810. fail_init_mfi:
  3811. if (instance->msix_vectors)
  3812. pci_disable_msix(instance->pdev);
  3813. fail_alloc_dma_buf:
  3814. if (instance->evt_detail)
  3815. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3816. instance->evt_detail,
  3817. instance->evt_detail_h);
  3818. if (instance->producer)
  3819. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3820. instance->producer_h);
  3821. if (instance->consumer)
  3822. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3823. instance->consumer_h);
  3824. scsi_host_put(host);
  3825. fail_alloc_instance:
  3826. fail_set_dma_mask:
  3827. pci_disable_device(pdev);
  3828. return -ENODEV;
  3829. }
  3830. /**
  3831. * megasas_flush_cache - Requests FW to flush all its caches
  3832. * @instance: Adapter soft state
  3833. */
  3834. static void megasas_flush_cache(struct megasas_instance *instance)
  3835. {
  3836. struct megasas_cmd *cmd;
  3837. struct megasas_dcmd_frame *dcmd;
  3838. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3839. return;
  3840. cmd = megasas_get_cmd(instance);
  3841. if (!cmd)
  3842. return;
  3843. dcmd = &cmd->frame->dcmd;
  3844. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3845. dcmd->cmd = MFI_CMD_DCMD;
  3846. dcmd->cmd_status = 0x0;
  3847. dcmd->sge_count = 0;
  3848. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  3849. dcmd->timeout = 0;
  3850. dcmd->pad_0 = 0;
  3851. dcmd->data_xfer_len = 0;
  3852. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
  3853. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3854. megasas_issue_blocked_cmd(instance, cmd);
  3855. megasas_return_cmd(instance, cmd);
  3856. return;
  3857. }
  3858. /**
  3859. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3860. * @instance: Adapter soft state
  3861. * @opcode: Shutdown/Hibernate
  3862. */
  3863. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3864. u32 opcode)
  3865. {
  3866. struct megasas_cmd *cmd;
  3867. struct megasas_dcmd_frame *dcmd;
  3868. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3869. return;
  3870. cmd = megasas_get_cmd(instance);
  3871. if (!cmd)
  3872. return;
  3873. if (instance->aen_cmd)
  3874. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3875. if (instance->map_update_cmd)
  3876. megasas_issue_blocked_abort_cmd(instance,
  3877. instance->map_update_cmd);
  3878. dcmd = &cmd->frame->dcmd;
  3879. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3880. dcmd->cmd = MFI_CMD_DCMD;
  3881. dcmd->cmd_status = 0x0;
  3882. dcmd->sge_count = 0;
  3883. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  3884. dcmd->timeout = 0;
  3885. dcmd->pad_0 = 0;
  3886. dcmd->data_xfer_len = 0;
  3887. dcmd->opcode = cpu_to_le32(opcode);
  3888. megasas_issue_blocked_cmd(instance, cmd);
  3889. megasas_return_cmd(instance, cmd);
  3890. return;
  3891. }
  3892. #ifdef CONFIG_PM
  3893. /**
  3894. * megasas_suspend - driver suspend entry point
  3895. * @pdev: PCI device structure
  3896. * @state: PCI power state to suspend routine
  3897. */
  3898. static int
  3899. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3900. {
  3901. struct Scsi_Host *host;
  3902. struct megasas_instance *instance;
  3903. int i;
  3904. instance = pci_get_drvdata(pdev);
  3905. host = instance->host;
  3906. instance->unload = 1;
  3907. megasas_flush_cache(instance);
  3908. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3909. /* cancel the delayed work if this work still in queue */
  3910. if (instance->ev != NULL) {
  3911. struct megasas_aen_event *ev = instance->ev;
  3912. cancel_delayed_work_sync(&ev->hotplug_work);
  3913. instance->ev = NULL;
  3914. }
  3915. tasklet_kill(&instance->isr_tasklet);
  3916. pci_set_drvdata(instance->pdev, instance);
  3917. instance->instancet->disable_intr(instance);
  3918. if (instance->msix_vectors)
  3919. for (i = 0 ; i < instance->msix_vectors; i++)
  3920. free_irq(instance->msixentry[i].vector,
  3921. &instance->irq_context[i]);
  3922. else
  3923. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3924. if (instance->msix_vectors)
  3925. pci_disable_msix(instance->pdev);
  3926. pci_save_state(pdev);
  3927. pci_disable_device(pdev);
  3928. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3929. return 0;
  3930. }
  3931. /**
  3932. * megasas_resume- driver resume entry point
  3933. * @pdev: PCI device structure
  3934. */
  3935. static int
  3936. megasas_resume(struct pci_dev *pdev)
  3937. {
  3938. int rval, i, j;
  3939. struct Scsi_Host *host;
  3940. struct megasas_instance *instance;
  3941. instance = pci_get_drvdata(pdev);
  3942. host = instance->host;
  3943. pci_set_power_state(pdev, PCI_D0);
  3944. pci_enable_wake(pdev, PCI_D0, 0);
  3945. pci_restore_state(pdev);
  3946. /*
  3947. * PCI prepping: enable device set bus mastering and dma mask
  3948. */
  3949. rval = pci_enable_device_mem(pdev);
  3950. if (rval) {
  3951. printk(KERN_ERR "megasas: Enable device failed\n");
  3952. return rval;
  3953. }
  3954. pci_set_master(pdev);
  3955. if (megasas_set_dma_mask(pdev))
  3956. goto fail_set_dma_mask;
  3957. /*
  3958. * Initialize MFI Firmware
  3959. */
  3960. atomic_set(&instance->fw_outstanding, 0);
  3961. /*
  3962. * We expect the FW state to be READY
  3963. */
  3964. if (megasas_transition_to_ready(instance, 0))
  3965. goto fail_ready_state;
  3966. /* Now re-enable MSI-X */
  3967. if (instance->msix_vectors)
  3968. pci_enable_msix(instance->pdev, instance->msixentry,
  3969. instance->msix_vectors);
  3970. switch (instance->pdev->device) {
  3971. case PCI_DEVICE_ID_LSI_FUSION:
  3972. case PCI_DEVICE_ID_LSI_INVADER:
  3973. case PCI_DEVICE_ID_LSI_FURY:
  3974. {
  3975. megasas_reset_reply_desc(instance);
  3976. if (megasas_ioc_init_fusion(instance)) {
  3977. megasas_free_cmds(instance);
  3978. megasas_free_cmds_fusion(instance);
  3979. goto fail_init_mfi;
  3980. }
  3981. if (!megasas_get_map_info(instance))
  3982. megasas_sync_map_info(instance);
  3983. }
  3984. break;
  3985. default:
  3986. *instance->producer = 0;
  3987. *instance->consumer = 0;
  3988. if (megasas_issue_init_mfi(instance))
  3989. goto fail_init_mfi;
  3990. break;
  3991. }
  3992. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3993. (unsigned long)instance);
  3994. /*
  3995. * Register IRQ
  3996. */
  3997. if (instance->msix_vectors) {
  3998. for (i = 0 ; i < instance->msix_vectors; i++) {
  3999. instance->irq_context[i].instance = instance;
  4000. instance->irq_context[i].MSIxIndex = i;
  4001. if (request_irq(instance->msixentry[i].vector,
  4002. instance->instancet->service_isr, 0,
  4003. "megasas",
  4004. &instance->irq_context[i])) {
  4005. printk(KERN_DEBUG "megasas: Failed to "
  4006. "register IRQ for vector %d.\n", i);
  4007. for (j = 0 ; j < i ; j++)
  4008. free_irq(
  4009. instance->msixentry[j].vector,
  4010. &instance->irq_context[j]);
  4011. goto fail_irq;
  4012. }
  4013. }
  4014. } else {
  4015. instance->irq_context[0].instance = instance;
  4016. instance->irq_context[0].MSIxIndex = 0;
  4017. if (request_irq(pdev->irq, instance->instancet->service_isr,
  4018. IRQF_SHARED, "megasas",
  4019. &instance->irq_context[0])) {
  4020. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  4021. goto fail_irq;
  4022. }
  4023. }
  4024. instance->instancet->enable_intr(instance);
  4025. instance->unload = 0;
  4026. /*
  4027. * Initiate AEN (Asynchronous Event Notification)
  4028. */
  4029. if (megasas_start_aen(instance))
  4030. printk(KERN_ERR "megasas: Start AEN failed\n");
  4031. return 0;
  4032. fail_irq:
  4033. fail_init_mfi:
  4034. if (instance->evt_detail)
  4035. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  4036. instance->evt_detail,
  4037. instance->evt_detail_h);
  4038. if (instance->producer)
  4039. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  4040. instance->producer_h);
  4041. if (instance->consumer)
  4042. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  4043. instance->consumer_h);
  4044. scsi_host_put(host);
  4045. fail_set_dma_mask:
  4046. fail_ready_state:
  4047. pci_disable_device(pdev);
  4048. return -ENODEV;
  4049. }
  4050. #else
  4051. #define megasas_suspend NULL
  4052. #define megasas_resume NULL
  4053. #endif
  4054. /**
  4055. * megasas_detach_one - PCI hot"un"plug entry point
  4056. * @pdev: PCI device structure
  4057. */
  4058. static void megasas_detach_one(struct pci_dev *pdev)
  4059. {
  4060. int i;
  4061. struct Scsi_Host *host;
  4062. struct megasas_instance *instance;
  4063. struct fusion_context *fusion;
  4064. instance = pci_get_drvdata(pdev);
  4065. instance->unload = 1;
  4066. host = instance->host;
  4067. fusion = instance->ctrl_context;
  4068. scsi_remove_host(instance->host);
  4069. megasas_flush_cache(instance);
  4070. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  4071. /* cancel the delayed work if this work still in queue*/
  4072. if (instance->ev != NULL) {
  4073. struct megasas_aen_event *ev = instance->ev;
  4074. cancel_delayed_work_sync(&ev->hotplug_work);
  4075. instance->ev = NULL;
  4076. }
  4077. tasklet_kill(&instance->isr_tasklet);
  4078. /*
  4079. * Take the instance off the instance array. Note that we will not
  4080. * decrement the max_index. We let this array be sparse array
  4081. */
  4082. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4083. if (megasas_mgmt_info.instance[i] == instance) {
  4084. megasas_mgmt_info.count--;
  4085. megasas_mgmt_info.instance[i] = NULL;
  4086. break;
  4087. }
  4088. }
  4089. pci_set_drvdata(instance->pdev, NULL);
  4090. instance->instancet->disable_intr(instance);
  4091. if (instance->msix_vectors)
  4092. for (i = 0 ; i < instance->msix_vectors; i++)
  4093. free_irq(instance->msixentry[i].vector,
  4094. &instance->irq_context[i]);
  4095. else
  4096. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  4097. if (instance->msix_vectors)
  4098. pci_disable_msix(instance->pdev);
  4099. switch (instance->pdev->device) {
  4100. case PCI_DEVICE_ID_LSI_FUSION:
  4101. case PCI_DEVICE_ID_LSI_INVADER:
  4102. case PCI_DEVICE_ID_LSI_FURY:
  4103. megasas_release_fusion(instance);
  4104. for (i = 0; i < 2 ; i++)
  4105. if (fusion->ld_map[i])
  4106. dma_free_coherent(&instance->pdev->dev,
  4107. fusion->map_sz,
  4108. fusion->ld_map[i],
  4109. fusion->
  4110. ld_map_phys[i]);
  4111. kfree(instance->ctrl_context);
  4112. break;
  4113. default:
  4114. megasas_release_mfi(instance);
  4115. pci_free_consistent(pdev, sizeof(u32),
  4116. instance->producer,
  4117. instance->producer_h);
  4118. pci_free_consistent(pdev, sizeof(u32),
  4119. instance->consumer,
  4120. instance->consumer_h);
  4121. break;
  4122. }
  4123. if (instance->evt_detail)
  4124. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  4125. instance->evt_detail, instance->evt_detail_h);
  4126. scsi_host_put(host);
  4127. pci_set_drvdata(pdev, NULL);
  4128. pci_disable_device(pdev);
  4129. return;
  4130. }
  4131. /**
  4132. * megasas_shutdown - Shutdown entry point
  4133. * @device: Generic device structure
  4134. */
  4135. static void megasas_shutdown(struct pci_dev *pdev)
  4136. {
  4137. int i;
  4138. struct megasas_instance *instance = pci_get_drvdata(pdev);
  4139. instance->unload = 1;
  4140. megasas_flush_cache(instance);
  4141. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  4142. instance->instancet->disable_intr(instance);
  4143. if (instance->msix_vectors)
  4144. for (i = 0 ; i < instance->msix_vectors; i++)
  4145. free_irq(instance->msixentry[i].vector,
  4146. &instance->irq_context[i]);
  4147. else
  4148. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  4149. if (instance->msix_vectors)
  4150. pci_disable_msix(instance->pdev);
  4151. }
  4152. /**
  4153. * megasas_mgmt_open - char node "open" entry point
  4154. */
  4155. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  4156. {
  4157. /*
  4158. * Allow only those users with admin rights
  4159. */
  4160. if (!capable(CAP_SYS_ADMIN))
  4161. return -EACCES;
  4162. return 0;
  4163. }
  4164. /**
  4165. * megasas_mgmt_fasync - Async notifier registration from applications
  4166. *
  4167. * This function adds the calling process to a driver global queue. When an
  4168. * event occurs, SIGIO will be sent to all processes in this queue.
  4169. */
  4170. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  4171. {
  4172. int rc;
  4173. mutex_lock(&megasas_async_queue_mutex);
  4174. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  4175. mutex_unlock(&megasas_async_queue_mutex);
  4176. if (rc >= 0) {
  4177. /* For sanity check when we get ioctl */
  4178. filep->private_data = filep;
  4179. return 0;
  4180. }
  4181. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  4182. return rc;
  4183. }
  4184. /**
  4185. * megasas_mgmt_poll - char node "poll" entry point
  4186. * */
  4187. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  4188. {
  4189. unsigned int mask;
  4190. unsigned long flags;
  4191. poll_wait(file, &megasas_poll_wait, wait);
  4192. spin_lock_irqsave(&poll_aen_lock, flags);
  4193. if (megasas_poll_wait_aen)
  4194. mask = (POLLIN | POLLRDNORM);
  4195. else
  4196. mask = 0;
  4197. spin_unlock_irqrestore(&poll_aen_lock, flags);
  4198. return mask;
  4199. }
  4200. /**
  4201. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  4202. * @instance: Adapter soft state
  4203. * @argp: User's ioctl packet
  4204. */
  4205. static int
  4206. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  4207. struct megasas_iocpacket __user * user_ioc,
  4208. struct megasas_iocpacket *ioc)
  4209. {
  4210. struct megasas_sge32 *kern_sge32;
  4211. struct megasas_cmd *cmd;
  4212. void *kbuff_arr[MAX_IOCTL_SGE];
  4213. dma_addr_t buf_handle = 0;
  4214. int error = 0, i;
  4215. void *sense = NULL;
  4216. dma_addr_t sense_handle;
  4217. unsigned long *sense_ptr;
  4218. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  4219. if (ioc->sge_count > MAX_IOCTL_SGE) {
  4220. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  4221. ioc->sge_count, MAX_IOCTL_SGE);
  4222. return -EINVAL;
  4223. }
  4224. cmd = megasas_get_cmd(instance);
  4225. if (!cmd) {
  4226. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  4227. return -ENOMEM;
  4228. }
  4229. /*
  4230. * User's IOCTL packet has 2 frames (maximum). Copy those two
  4231. * frames into our cmd's frames. cmd->frame's context will get
  4232. * overwritten when we copy from user's frames. So set that value
  4233. * alone separately
  4234. */
  4235. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  4236. cmd->frame->hdr.context = cpu_to_le32(cmd->index);
  4237. cmd->frame->hdr.pad_0 = 0;
  4238. cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
  4239. MFI_FRAME_SGL64 |
  4240. MFI_FRAME_SENSE64));
  4241. /*
  4242. * The management interface between applications and the fw uses
  4243. * MFI frames. E.g, RAID configuration changes, LD property changes
  4244. * etc are accomplishes through different kinds of MFI frames. The
  4245. * driver needs to care only about substituting user buffers with
  4246. * kernel buffers in SGLs. The location of SGL is embedded in the
  4247. * struct iocpacket itself.
  4248. */
  4249. kern_sge32 = (struct megasas_sge32 *)
  4250. ((unsigned long)cmd->frame + ioc->sgl_off);
  4251. /*
  4252. * For each user buffer, create a mirror buffer and copy in
  4253. */
  4254. for (i = 0; i < ioc->sge_count; i++) {
  4255. if (!ioc->sgl[i].iov_len)
  4256. continue;
  4257. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  4258. ioc->sgl[i].iov_len,
  4259. &buf_handle, GFP_KERNEL);
  4260. if (!kbuff_arr[i]) {
  4261. printk(KERN_DEBUG "megasas: Failed to alloc "
  4262. "kernel SGL buffer for IOCTL \n");
  4263. error = -ENOMEM;
  4264. goto out;
  4265. }
  4266. /*
  4267. * We don't change the dma_coherent_mask, so
  4268. * pci_alloc_consistent only returns 32bit addresses
  4269. */
  4270. kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
  4271. kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
  4272. /*
  4273. * We created a kernel buffer corresponding to the
  4274. * user buffer. Now copy in from the user buffer
  4275. */
  4276. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  4277. (u32) (ioc->sgl[i].iov_len))) {
  4278. error = -EFAULT;
  4279. goto out;
  4280. }
  4281. }
  4282. if (ioc->sense_len) {
  4283. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  4284. &sense_handle, GFP_KERNEL);
  4285. if (!sense) {
  4286. error = -ENOMEM;
  4287. goto out;
  4288. }
  4289. sense_ptr =
  4290. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  4291. *sense_ptr = cpu_to_le32(sense_handle);
  4292. }
  4293. /*
  4294. * Set the sync_cmd flag so that the ISR knows not to complete this
  4295. * cmd to the SCSI mid-layer
  4296. */
  4297. cmd->sync_cmd = 1;
  4298. megasas_issue_blocked_cmd(instance, cmd);
  4299. cmd->sync_cmd = 0;
  4300. /*
  4301. * copy out the kernel buffers to user buffers
  4302. */
  4303. for (i = 0; i < ioc->sge_count; i++) {
  4304. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  4305. ioc->sgl[i].iov_len)) {
  4306. error = -EFAULT;
  4307. goto out;
  4308. }
  4309. }
  4310. /*
  4311. * copy out the sense
  4312. */
  4313. if (ioc->sense_len) {
  4314. /*
  4315. * sense_ptr points to the location that has the user
  4316. * sense buffer address
  4317. */
  4318. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  4319. ioc->sense_off);
  4320. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  4321. sense, ioc->sense_len)) {
  4322. printk(KERN_ERR "megasas: Failed to copy out to user "
  4323. "sense data\n");
  4324. error = -EFAULT;
  4325. goto out;
  4326. }
  4327. }
  4328. /*
  4329. * copy the status codes returned by the fw
  4330. */
  4331. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4332. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4333. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4334. error = -EFAULT;
  4335. }
  4336. out:
  4337. if (sense) {
  4338. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4339. sense, sense_handle);
  4340. }
  4341. for (i = 0; i < ioc->sge_count; i++) {
  4342. if (kbuff_arr[i])
  4343. dma_free_coherent(&instance->pdev->dev,
  4344. le32_to_cpu(kern_sge32[i].length),
  4345. kbuff_arr[i],
  4346. le32_to_cpu(kern_sge32[i].phys_addr));
  4347. }
  4348. megasas_return_cmd(instance, cmd);
  4349. return error;
  4350. }
  4351. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4352. {
  4353. struct megasas_iocpacket __user *user_ioc =
  4354. (struct megasas_iocpacket __user *)arg;
  4355. struct megasas_iocpacket *ioc;
  4356. struct megasas_instance *instance;
  4357. int error;
  4358. int i;
  4359. unsigned long flags;
  4360. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4361. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4362. if (!ioc)
  4363. return -ENOMEM;
  4364. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4365. error = -EFAULT;
  4366. goto out_kfree_ioc;
  4367. }
  4368. instance = megasas_lookup_instance(ioc->host_no);
  4369. if (!instance) {
  4370. error = -ENODEV;
  4371. goto out_kfree_ioc;
  4372. }
  4373. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4374. printk(KERN_ERR "Controller in crit error\n");
  4375. error = -ENODEV;
  4376. goto out_kfree_ioc;
  4377. }
  4378. if (instance->unload == 1) {
  4379. error = -ENODEV;
  4380. goto out_kfree_ioc;
  4381. }
  4382. /*
  4383. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4384. */
  4385. if (down_interruptible(&instance->ioctl_sem)) {
  4386. error = -ERESTARTSYS;
  4387. goto out_kfree_ioc;
  4388. }
  4389. for (i = 0; i < wait_time; i++) {
  4390. spin_lock_irqsave(&instance->hba_lock, flags);
  4391. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4392. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4393. break;
  4394. }
  4395. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4396. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4397. printk(KERN_NOTICE "megasas: waiting"
  4398. "for controller reset to finish\n");
  4399. }
  4400. msleep(1000);
  4401. }
  4402. spin_lock_irqsave(&instance->hba_lock, flags);
  4403. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4404. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4405. printk(KERN_ERR "megaraid_sas: timed out while"
  4406. "waiting for HBA to recover\n");
  4407. error = -ENODEV;
  4408. goto out_up;
  4409. }
  4410. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4411. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4412. out_up:
  4413. up(&instance->ioctl_sem);
  4414. out_kfree_ioc:
  4415. kfree(ioc);
  4416. return error;
  4417. }
  4418. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4419. {
  4420. struct megasas_instance *instance;
  4421. struct megasas_aen aen;
  4422. int error;
  4423. int i;
  4424. unsigned long flags;
  4425. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4426. if (file->private_data != file) {
  4427. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4428. "called first\n");
  4429. return -EINVAL;
  4430. }
  4431. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4432. return -EFAULT;
  4433. instance = megasas_lookup_instance(aen.host_no);
  4434. if (!instance)
  4435. return -ENODEV;
  4436. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4437. return -ENODEV;
  4438. }
  4439. if (instance->unload == 1) {
  4440. return -ENODEV;
  4441. }
  4442. for (i = 0; i < wait_time; i++) {
  4443. spin_lock_irqsave(&instance->hba_lock, flags);
  4444. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4445. spin_unlock_irqrestore(&instance->hba_lock,
  4446. flags);
  4447. break;
  4448. }
  4449. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4450. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4451. printk(KERN_NOTICE "megasas: waiting for"
  4452. "controller reset to finish\n");
  4453. }
  4454. msleep(1000);
  4455. }
  4456. spin_lock_irqsave(&instance->hba_lock, flags);
  4457. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4458. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4459. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4460. "for HBA to recover.\n");
  4461. return -ENODEV;
  4462. }
  4463. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4464. mutex_lock(&instance->aen_mutex);
  4465. error = megasas_register_aen(instance, aen.seq_num,
  4466. aen.class_locale_word);
  4467. mutex_unlock(&instance->aen_mutex);
  4468. return error;
  4469. }
  4470. /**
  4471. * megasas_mgmt_ioctl - char node ioctl entry point
  4472. */
  4473. static long
  4474. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4475. {
  4476. switch (cmd) {
  4477. case MEGASAS_IOC_FIRMWARE:
  4478. return megasas_mgmt_ioctl_fw(file, arg);
  4479. case MEGASAS_IOC_GET_AEN:
  4480. return megasas_mgmt_ioctl_aen(file, arg);
  4481. }
  4482. return -ENOTTY;
  4483. }
  4484. #ifdef CONFIG_COMPAT
  4485. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4486. {
  4487. struct compat_megasas_iocpacket __user *cioc =
  4488. (struct compat_megasas_iocpacket __user *)arg;
  4489. struct megasas_iocpacket __user *ioc =
  4490. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4491. int i;
  4492. int error = 0;
  4493. compat_uptr_t ptr;
  4494. if (clear_user(ioc, sizeof(*ioc)))
  4495. return -EFAULT;
  4496. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4497. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4498. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4499. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4500. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4501. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4502. return -EFAULT;
  4503. /*
  4504. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4505. * sense_len is not null, so prepare the 64bit value under
  4506. * the same condition.
  4507. */
  4508. if (ioc->sense_len) {
  4509. void __user **sense_ioc_ptr =
  4510. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4511. compat_uptr_t *sense_cioc_ptr =
  4512. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4513. if (get_user(ptr, sense_cioc_ptr) ||
  4514. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4515. return -EFAULT;
  4516. }
  4517. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4518. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4519. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4520. copy_in_user(&ioc->sgl[i].iov_len,
  4521. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4522. return -EFAULT;
  4523. }
  4524. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4525. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4526. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4527. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4528. return -EFAULT;
  4529. }
  4530. return error;
  4531. }
  4532. static long
  4533. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4534. unsigned long arg)
  4535. {
  4536. switch (cmd) {
  4537. case MEGASAS_IOC_FIRMWARE32:
  4538. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4539. case MEGASAS_IOC_GET_AEN:
  4540. return megasas_mgmt_ioctl_aen(file, arg);
  4541. }
  4542. return -ENOTTY;
  4543. }
  4544. #endif
  4545. /*
  4546. * File operations structure for management interface
  4547. */
  4548. static const struct file_operations megasas_mgmt_fops = {
  4549. .owner = THIS_MODULE,
  4550. .open = megasas_mgmt_open,
  4551. .fasync = megasas_mgmt_fasync,
  4552. .unlocked_ioctl = megasas_mgmt_ioctl,
  4553. .poll = megasas_mgmt_poll,
  4554. #ifdef CONFIG_COMPAT
  4555. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4556. #endif
  4557. .llseek = noop_llseek,
  4558. };
  4559. /*
  4560. * PCI hotplug support registration structure
  4561. */
  4562. static struct pci_driver megasas_pci_driver = {
  4563. .name = "megaraid_sas",
  4564. .id_table = megasas_pci_table,
  4565. .probe = megasas_probe_one,
  4566. .remove = megasas_detach_one,
  4567. .suspend = megasas_suspend,
  4568. .resume = megasas_resume,
  4569. .shutdown = megasas_shutdown,
  4570. };
  4571. /*
  4572. * Sysfs driver attributes
  4573. */
  4574. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4575. {
  4576. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4577. MEGASAS_VERSION);
  4578. }
  4579. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4580. static ssize_t
  4581. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4582. {
  4583. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4584. MEGASAS_RELDATE);
  4585. }
  4586. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4587. NULL);
  4588. static ssize_t
  4589. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4590. {
  4591. return sprintf(buf, "%u\n", support_poll_for_event);
  4592. }
  4593. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4594. megasas_sysfs_show_support_poll_for_event, NULL);
  4595. static ssize_t
  4596. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4597. {
  4598. return sprintf(buf, "%u\n", support_device_change);
  4599. }
  4600. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4601. megasas_sysfs_show_support_device_change, NULL);
  4602. static ssize_t
  4603. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4604. {
  4605. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4606. }
  4607. static ssize_t
  4608. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4609. {
  4610. int retval = count;
  4611. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4612. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4613. retval = -EINVAL;
  4614. }
  4615. return retval;
  4616. }
  4617. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4618. megasas_sysfs_set_dbg_lvl);
  4619. static void
  4620. megasas_aen_polling(struct work_struct *work)
  4621. {
  4622. struct megasas_aen_event *ev =
  4623. container_of(work, struct megasas_aen_event, hotplug_work.work);
  4624. struct megasas_instance *instance = ev->instance;
  4625. union megasas_evt_class_locale class_locale;
  4626. struct Scsi_Host *host;
  4627. struct scsi_device *sdev1;
  4628. u16 pd_index = 0;
  4629. u16 ld_index = 0;
  4630. int i, j, doscan = 0;
  4631. u32 seq_num;
  4632. int error;
  4633. if (!instance) {
  4634. printk(KERN_ERR "invalid instance!\n");
  4635. kfree(ev);
  4636. return;
  4637. }
  4638. instance->ev = NULL;
  4639. host = instance->host;
  4640. if (instance->evt_detail) {
  4641. switch (le32_to_cpu(instance->evt_detail->code)) {
  4642. case MR_EVT_PD_INSERTED:
  4643. if (megasas_get_pd_list(instance) == 0) {
  4644. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4645. for (j = 0;
  4646. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4647. j++) {
  4648. pd_index =
  4649. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4650. sdev1 =
  4651. scsi_device_lookup(host, i, j, 0);
  4652. if (instance->pd_list[pd_index].driveState
  4653. == MR_PD_STATE_SYSTEM) {
  4654. if (!sdev1) {
  4655. scsi_add_device(host, i, j, 0);
  4656. }
  4657. if (sdev1)
  4658. scsi_device_put(sdev1);
  4659. }
  4660. }
  4661. }
  4662. }
  4663. doscan = 0;
  4664. break;
  4665. case MR_EVT_PD_REMOVED:
  4666. if (megasas_get_pd_list(instance) == 0) {
  4667. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4668. for (j = 0;
  4669. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4670. j++) {
  4671. pd_index =
  4672. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4673. sdev1 =
  4674. scsi_device_lookup(host, i, j, 0);
  4675. if (instance->pd_list[pd_index].driveState
  4676. == MR_PD_STATE_SYSTEM) {
  4677. if (sdev1) {
  4678. scsi_device_put(sdev1);
  4679. }
  4680. } else {
  4681. if (sdev1) {
  4682. scsi_remove_device(sdev1);
  4683. scsi_device_put(sdev1);
  4684. }
  4685. }
  4686. }
  4687. }
  4688. }
  4689. doscan = 0;
  4690. break;
  4691. case MR_EVT_LD_OFFLINE:
  4692. case MR_EVT_CFG_CLEARED:
  4693. case MR_EVT_LD_DELETED:
  4694. if (megasas_ld_list_query(instance,
  4695. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4696. megasas_get_ld_list(instance);
  4697. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4698. for (j = 0;
  4699. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4700. j++) {
  4701. ld_index =
  4702. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4703. sdev1 = scsi_device_lookup(host,
  4704. MEGASAS_MAX_PD_CHANNELS + i,
  4705. j,
  4706. 0);
  4707. if (instance->ld_ids[ld_index] != 0xff) {
  4708. if (sdev1) {
  4709. scsi_device_put(sdev1);
  4710. }
  4711. } else {
  4712. if (sdev1) {
  4713. scsi_remove_device(sdev1);
  4714. scsi_device_put(sdev1);
  4715. }
  4716. }
  4717. }
  4718. }
  4719. doscan = 0;
  4720. break;
  4721. case MR_EVT_LD_CREATED:
  4722. if (megasas_ld_list_query(instance,
  4723. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4724. megasas_get_ld_list(instance);
  4725. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4726. for (j = 0;
  4727. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4728. j++) {
  4729. ld_index =
  4730. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4731. sdev1 = scsi_device_lookup(host,
  4732. MEGASAS_MAX_PD_CHANNELS + i,
  4733. j, 0);
  4734. if (instance->ld_ids[ld_index] !=
  4735. 0xff) {
  4736. if (!sdev1) {
  4737. scsi_add_device(host,
  4738. MEGASAS_MAX_PD_CHANNELS + i,
  4739. j, 0);
  4740. }
  4741. }
  4742. if (sdev1) {
  4743. scsi_device_put(sdev1);
  4744. }
  4745. }
  4746. }
  4747. doscan = 0;
  4748. break;
  4749. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4750. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4751. case MR_EVT_LD_STATE_CHANGE:
  4752. doscan = 1;
  4753. break;
  4754. default:
  4755. doscan = 0;
  4756. break;
  4757. }
  4758. } else {
  4759. printk(KERN_ERR "invalid evt_detail!\n");
  4760. kfree(ev);
  4761. return;
  4762. }
  4763. if (doscan) {
  4764. printk(KERN_INFO "scanning ...\n");
  4765. megasas_get_pd_list(instance);
  4766. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4767. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4768. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4769. sdev1 = scsi_device_lookup(host, i, j, 0);
  4770. if (instance->pd_list[pd_index].driveState ==
  4771. MR_PD_STATE_SYSTEM) {
  4772. if (!sdev1) {
  4773. scsi_add_device(host, i, j, 0);
  4774. }
  4775. if (sdev1)
  4776. scsi_device_put(sdev1);
  4777. } else {
  4778. if (sdev1) {
  4779. scsi_remove_device(sdev1);
  4780. scsi_device_put(sdev1);
  4781. }
  4782. }
  4783. }
  4784. }
  4785. if (megasas_ld_list_query(instance,
  4786. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4787. megasas_get_ld_list(instance);
  4788. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4789. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4790. ld_index =
  4791. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4792. sdev1 = scsi_device_lookup(host,
  4793. MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  4794. if (instance->ld_ids[ld_index] != 0xff) {
  4795. if (!sdev1) {
  4796. scsi_add_device(host,
  4797. MEGASAS_MAX_PD_CHANNELS + i,
  4798. j, 0);
  4799. } else {
  4800. scsi_device_put(sdev1);
  4801. }
  4802. } else {
  4803. if (sdev1) {
  4804. scsi_remove_device(sdev1);
  4805. scsi_device_put(sdev1);
  4806. }
  4807. }
  4808. }
  4809. }
  4810. }
  4811. if ( instance->aen_cmd != NULL ) {
  4812. kfree(ev);
  4813. return ;
  4814. }
  4815. seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
  4816. /* Register AEN with FW for latest sequence number plus 1 */
  4817. class_locale.members.reserved = 0;
  4818. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4819. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4820. mutex_lock(&instance->aen_mutex);
  4821. error = megasas_register_aen(instance, seq_num,
  4822. class_locale.word);
  4823. mutex_unlock(&instance->aen_mutex);
  4824. if (error)
  4825. printk(KERN_ERR "register aen failed error %x\n", error);
  4826. kfree(ev);
  4827. }
  4828. /**
  4829. * megasas_init - Driver load entry point
  4830. */
  4831. static int __init megasas_init(void)
  4832. {
  4833. int rval;
  4834. /*
  4835. * Announce driver version and other information
  4836. */
  4837. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4838. MEGASAS_EXT_VERSION);
  4839. spin_lock_init(&poll_aen_lock);
  4840. support_poll_for_event = 2;
  4841. support_device_change = 1;
  4842. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4843. /*
  4844. * Register character device node
  4845. */
  4846. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4847. if (rval < 0) {
  4848. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4849. return rval;
  4850. }
  4851. megasas_mgmt_majorno = rval;
  4852. /*
  4853. * Register ourselves as PCI hotplug module
  4854. */
  4855. rval = pci_register_driver(&megasas_pci_driver);
  4856. if (rval) {
  4857. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4858. goto err_pcidrv;
  4859. }
  4860. rval = driver_create_file(&megasas_pci_driver.driver,
  4861. &driver_attr_version);
  4862. if (rval)
  4863. goto err_dcf_attr_ver;
  4864. rval = driver_create_file(&megasas_pci_driver.driver,
  4865. &driver_attr_release_date);
  4866. if (rval)
  4867. goto err_dcf_rel_date;
  4868. rval = driver_create_file(&megasas_pci_driver.driver,
  4869. &driver_attr_support_poll_for_event);
  4870. if (rval)
  4871. goto err_dcf_support_poll_for_event;
  4872. rval = driver_create_file(&megasas_pci_driver.driver,
  4873. &driver_attr_dbg_lvl);
  4874. if (rval)
  4875. goto err_dcf_dbg_lvl;
  4876. rval = driver_create_file(&megasas_pci_driver.driver,
  4877. &driver_attr_support_device_change);
  4878. if (rval)
  4879. goto err_dcf_support_device_change;
  4880. return rval;
  4881. err_dcf_support_device_change:
  4882. driver_remove_file(&megasas_pci_driver.driver,
  4883. &driver_attr_dbg_lvl);
  4884. err_dcf_dbg_lvl:
  4885. driver_remove_file(&megasas_pci_driver.driver,
  4886. &driver_attr_support_poll_for_event);
  4887. err_dcf_support_poll_for_event:
  4888. driver_remove_file(&megasas_pci_driver.driver,
  4889. &driver_attr_release_date);
  4890. err_dcf_rel_date:
  4891. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4892. err_dcf_attr_ver:
  4893. pci_unregister_driver(&megasas_pci_driver);
  4894. err_pcidrv:
  4895. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4896. return rval;
  4897. }
  4898. /**
  4899. * megasas_exit - Driver unload entry point
  4900. */
  4901. static void __exit megasas_exit(void)
  4902. {
  4903. driver_remove_file(&megasas_pci_driver.driver,
  4904. &driver_attr_dbg_lvl);
  4905. driver_remove_file(&megasas_pci_driver.driver,
  4906. &driver_attr_support_poll_for_event);
  4907. driver_remove_file(&megasas_pci_driver.driver,
  4908. &driver_attr_support_device_change);
  4909. driver_remove_file(&megasas_pci_driver.driver,
  4910. &driver_attr_release_date);
  4911. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4912. pci_unregister_driver(&megasas_pci_driver);
  4913. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4914. }
  4915. module_init(megasas_init);
  4916. module_exit(megasas_exit);