megaraid_sas_base.c 145 KB

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