megaraid_sas_base.c 142 KB

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