megaraid_sas_base.c 144 KB

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