megaraid_sas_base.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2011 LSI Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE: megaraid_sas_base.c
  21. * Version : v00.00.06.18-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_GEN2_ENABLE_INTERRUPT_MASK) {
  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_HBA_OPERATIONAL) {
  1279. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1280. return SCSI_MLQUEUE_HOST_BUSY;
  1281. }
  1282. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1283. scmd->scsi_done = done;
  1284. scmd->result = 0;
  1285. if (MEGASAS_IS_LOGICAL(scmd) &&
  1286. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  1287. scmd->result = DID_BAD_TARGET << 16;
  1288. goto out_done;
  1289. }
  1290. switch (scmd->cmnd[0]) {
  1291. case SYNCHRONIZE_CACHE:
  1292. /*
  1293. * FW takes care of flush cache on its own
  1294. * No need to send it down
  1295. */
  1296. scmd->result = DID_OK << 16;
  1297. goto out_done;
  1298. default:
  1299. break;
  1300. }
  1301. if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
  1302. printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
  1303. return SCSI_MLQUEUE_HOST_BUSY;
  1304. }
  1305. return 0;
  1306. out_done:
  1307. done(scmd);
  1308. return 0;
  1309. }
  1310. static DEF_SCSI_QCMD(megasas_queue_command)
  1311. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1312. {
  1313. int i;
  1314. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1315. if ((megasas_mgmt_info.instance[i]) &&
  1316. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1317. return megasas_mgmt_info.instance[i];
  1318. }
  1319. return NULL;
  1320. }
  1321. static int megasas_slave_configure(struct scsi_device *sdev)
  1322. {
  1323. u16 pd_index = 0;
  1324. struct megasas_instance *instance ;
  1325. instance = megasas_lookup_instance(sdev->host->host_no);
  1326. /*
  1327. * Don't export physical disk devices to the disk driver.
  1328. *
  1329. * FIXME: Currently we don't export them to the midlayer at all.
  1330. * That will be fixed once LSI engineers have audited the
  1331. * firmware for possible issues.
  1332. */
  1333. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1334. sdev->type == TYPE_DISK) {
  1335. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1336. sdev->id;
  1337. if (instance->pd_list[pd_index].driveState ==
  1338. MR_PD_STATE_SYSTEM) {
  1339. blk_queue_rq_timeout(sdev->request_queue,
  1340. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1341. return 0;
  1342. }
  1343. return -ENXIO;
  1344. }
  1345. /*
  1346. * The RAID firmware may require extended timeouts.
  1347. */
  1348. blk_queue_rq_timeout(sdev->request_queue,
  1349. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1350. return 0;
  1351. }
  1352. static int megasas_slave_alloc(struct scsi_device *sdev)
  1353. {
  1354. u16 pd_index = 0;
  1355. struct megasas_instance *instance ;
  1356. instance = megasas_lookup_instance(sdev->host->host_no);
  1357. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1358. (sdev->type == TYPE_DISK)) {
  1359. /*
  1360. * Open the OS scan to the SYSTEM PD
  1361. */
  1362. pd_index =
  1363. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1364. sdev->id;
  1365. if ((instance->pd_list[pd_index].driveState ==
  1366. MR_PD_STATE_SYSTEM) &&
  1367. (instance->pd_list[pd_index].driveType ==
  1368. TYPE_DISK)) {
  1369. return 0;
  1370. }
  1371. return -ENXIO;
  1372. }
  1373. return 0;
  1374. }
  1375. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1376. {
  1377. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1378. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1379. (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1380. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  1381. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1382. } else {
  1383. writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
  1384. }
  1385. }
  1386. /**
  1387. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1388. * restored to max value
  1389. * @instance: Adapter soft state
  1390. *
  1391. */
  1392. void
  1393. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1394. {
  1395. unsigned long flags;
  1396. if (instance->flag & MEGASAS_FW_BUSY
  1397. && time_after(jiffies, instance->last_time + 5 * HZ)
  1398. && atomic_read(&instance->fw_outstanding) <
  1399. instance->throttlequeuedepth + 1) {
  1400. spin_lock_irqsave(instance->host->host_lock, flags);
  1401. instance->flag &= ~MEGASAS_FW_BUSY;
  1402. if ((instance->pdev->device ==
  1403. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1404. (instance->pdev->device ==
  1405. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1406. instance->host->can_queue =
  1407. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1408. } else
  1409. instance->host->can_queue =
  1410. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1411. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1412. }
  1413. }
  1414. /**
  1415. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1416. * @instance_addr: Address of adapter soft state
  1417. *
  1418. * Tasklet to complete cmds
  1419. */
  1420. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1421. {
  1422. u32 producer;
  1423. u32 consumer;
  1424. u32 context;
  1425. struct megasas_cmd *cmd;
  1426. struct megasas_instance *instance =
  1427. (struct megasas_instance *)instance_addr;
  1428. unsigned long flags;
  1429. /* If we have already declared adapter dead, donot complete cmds */
  1430. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
  1431. return;
  1432. spin_lock_irqsave(&instance->completion_lock, flags);
  1433. producer = *instance->producer;
  1434. consumer = *instance->consumer;
  1435. while (consumer != producer) {
  1436. context = instance->reply_queue[consumer];
  1437. if (context >= instance->max_fw_cmds) {
  1438. printk(KERN_ERR "Unexpected context value %x\n",
  1439. context);
  1440. BUG();
  1441. }
  1442. cmd = instance->cmd_list[context];
  1443. megasas_complete_cmd(instance, cmd, DID_OK);
  1444. consumer++;
  1445. if (consumer == (instance->max_fw_cmds + 1)) {
  1446. consumer = 0;
  1447. }
  1448. }
  1449. *instance->consumer = producer;
  1450. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1451. /*
  1452. * Check if we can restore can_queue
  1453. */
  1454. megasas_check_and_restore_queue_depth(instance);
  1455. }
  1456. static void
  1457. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1458. static void
  1459. process_fw_state_change_wq(struct work_struct *work);
  1460. void megasas_do_ocr(struct megasas_instance *instance)
  1461. {
  1462. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1463. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1464. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1465. *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
  1466. }
  1467. instance->instancet->disable_intr(instance->reg_set);
  1468. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1469. instance->issuepend_done = 0;
  1470. atomic_set(&instance->fw_outstanding, 0);
  1471. megasas_internal_reset_defer_cmds(instance);
  1472. process_fw_state_change_wq(&instance->work_init);
  1473. }
  1474. /**
  1475. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1476. * @instance: Adapter soft state
  1477. *
  1478. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  1479. * complete all its outstanding commands. Returns error if one or more IOs
  1480. * are pending after this time period. It also marks the controller dead.
  1481. */
  1482. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1483. {
  1484. int i;
  1485. u32 reset_index;
  1486. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1487. u8 adprecovery;
  1488. unsigned long flags;
  1489. struct list_head clist_local;
  1490. struct megasas_cmd *reset_cmd;
  1491. u32 fw_state;
  1492. u8 kill_adapter_flag;
  1493. spin_lock_irqsave(&instance->hba_lock, flags);
  1494. adprecovery = instance->adprecovery;
  1495. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1496. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1497. INIT_LIST_HEAD(&clist_local);
  1498. spin_lock_irqsave(&instance->hba_lock, flags);
  1499. list_splice_init(&instance->internal_reset_pending_q,
  1500. &clist_local);
  1501. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1502. printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
  1503. for (i = 0; i < wait_time; i++) {
  1504. msleep(1000);
  1505. spin_lock_irqsave(&instance->hba_lock, flags);
  1506. adprecovery = instance->adprecovery;
  1507. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1508. if (adprecovery == MEGASAS_HBA_OPERATIONAL)
  1509. break;
  1510. }
  1511. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1512. printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
  1513. spin_lock_irqsave(&instance->hba_lock, flags);
  1514. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1515. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1516. return FAILED;
  1517. }
  1518. reset_index = 0;
  1519. while (!list_empty(&clist_local)) {
  1520. reset_cmd = list_entry((&clist_local)->next,
  1521. struct megasas_cmd, list);
  1522. list_del_init(&reset_cmd->list);
  1523. if (reset_cmd->scmd) {
  1524. reset_cmd->scmd->result = DID_RESET << 16;
  1525. printk(KERN_NOTICE "%d:%p reset [%02x]\n",
  1526. reset_index, reset_cmd,
  1527. reset_cmd->scmd->cmnd[0]);
  1528. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  1529. megasas_return_cmd(instance, reset_cmd);
  1530. } else if (reset_cmd->sync_cmd) {
  1531. printk(KERN_NOTICE "megasas:%p synch cmds"
  1532. "reset queue\n",
  1533. reset_cmd);
  1534. reset_cmd->cmd_status = ENODATA;
  1535. instance->instancet->fire_cmd(instance,
  1536. reset_cmd->frame_phys_addr,
  1537. 0, instance->reg_set);
  1538. } else {
  1539. printk(KERN_NOTICE "megasas: %p unexpected"
  1540. "cmds lst\n",
  1541. reset_cmd);
  1542. }
  1543. reset_index++;
  1544. }
  1545. return SUCCESS;
  1546. }
  1547. for (i = 0; i < resetwaittime; i++) {
  1548. int outstanding = atomic_read(&instance->fw_outstanding);
  1549. if (!outstanding)
  1550. break;
  1551. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1552. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1553. "commands to complete\n",i,outstanding);
  1554. /*
  1555. * Call cmd completion routine. Cmd to be
  1556. * be completed directly without depending on isr.
  1557. */
  1558. megasas_complete_cmd_dpc((unsigned long)instance);
  1559. }
  1560. msleep(1000);
  1561. }
  1562. i = 0;
  1563. kill_adapter_flag = 0;
  1564. do {
  1565. fw_state = instance->instancet->read_fw_status_reg(
  1566. instance->reg_set) & MFI_STATE_MASK;
  1567. if ((fw_state == MFI_STATE_FAULT) &&
  1568. (instance->disableOnlineCtrlReset == 0)) {
  1569. if (i == 3) {
  1570. kill_adapter_flag = 2;
  1571. break;
  1572. }
  1573. megasas_do_ocr(instance);
  1574. kill_adapter_flag = 1;
  1575. /* wait for 1 secs to let FW finish the pending cmds */
  1576. msleep(1000);
  1577. }
  1578. i++;
  1579. } while (i <= 3);
  1580. if (atomic_read(&instance->fw_outstanding) &&
  1581. !kill_adapter_flag) {
  1582. if (instance->disableOnlineCtrlReset == 0) {
  1583. megasas_do_ocr(instance);
  1584. /* wait for 5 secs to let FW finish the pending cmds */
  1585. for (i = 0; i < wait_time; i++) {
  1586. int outstanding =
  1587. atomic_read(&instance->fw_outstanding);
  1588. if (!outstanding)
  1589. return SUCCESS;
  1590. msleep(1000);
  1591. }
  1592. }
  1593. }
  1594. if (atomic_read(&instance->fw_outstanding) ||
  1595. (kill_adapter_flag == 2)) {
  1596. printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
  1597. /*
  1598. * Send signal to FW to stop processing any pending cmds.
  1599. * The controller will be taken offline by the OS now.
  1600. */
  1601. if ((instance->pdev->device ==
  1602. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1603. (instance->pdev->device ==
  1604. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1605. writel(MFI_STOP_ADP,
  1606. &instance->reg_set->doorbell);
  1607. } else {
  1608. writel(MFI_STOP_ADP,
  1609. &instance->reg_set->inbound_doorbell);
  1610. }
  1611. megasas_dump_pending_frames(instance);
  1612. spin_lock_irqsave(&instance->hba_lock, flags);
  1613. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1614. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1615. return FAILED;
  1616. }
  1617. printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
  1618. return SUCCESS;
  1619. }
  1620. /**
  1621. * megasas_generic_reset - Generic reset routine
  1622. * @scmd: Mid-layer SCSI command
  1623. *
  1624. * This routine implements a generic reset handler for device, bus and host
  1625. * reset requests. Device, bus and host specific reset handlers can use this
  1626. * function after they do their specific tasks.
  1627. */
  1628. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1629. {
  1630. int ret_val;
  1631. struct megasas_instance *instance;
  1632. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1633. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  1634. scmd->cmnd[0], scmd->retries);
  1635. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1636. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1637. "failures\n");
  1638. return FAILED;
  1639. }
  1640. ret_val = megasas_wait_for_outstanding(instance);
  1641. if (ret_val == SUCCESS)
  1642. printk(KERN_NOTICE "megasas: reset successful \n");
  1643. else
  1644. printk(KERN_ERR "megasas: failed to do reset\n");
  1645. return ret_val;
  1646. }
  1647. /**
  1648. * megasas_reset_timer - quiesce the adapter if required
  1649. * @scmd: scsi cmnd
  1650. *
  1651. * Sets the FW busy flag and reduces the host->can_queue if the
  1652. * cmd has not been completed within the timeout period.
  1653. */
  1654. static enum
  1655. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1656. {
  1657. struct megasas_instance *instance;
  1658. unsigned long flags;
  1659. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1660. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1661. return BLK_EH_NOT_HANDLED;
  1662. }
  1663. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1664. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1665. /* FW is busy, throttle IO */
  1666. spin_lock_irqsave(instance->host->host_lock, flags);
  1667. instance->host->can_queue = instance->throttlequeuedepth;
  1668. instance->last_time = jiffies;
  1669. instance->flag |= MEGASAS_FW_BUSY;
  1670. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1671. }
  1672. return BLK_EH_RESET_TIMER;
  1673. }
  1674. /**
  1675. * megasas_reset_device - Device reset handler entry point
  1676. */
  1677. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1678. {
  1679. int ret;
  1680. /*
  1681. * First wait for all commands to complete
  1682. */
  1683. ret = megasas_generic_reset(scmd);
  1684. return ret;
  1685. }
  1686. /**
  1687. * megasas_reset_bus_host - Bus & host reset handler entry point
  1688. */
  1689. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1690. {
  1691. int ret;
  1692. struct megasas_instance *instance;
  1693. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1694. /*
  1695. * First wait for all commands to complete
  1696. */
  1697. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  1698. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  1699. ret = megasas_reset_fusion(scmd->device->host);
  1700. else
  1701. ret = megasas_generic_reset(scmd);
  1702. return ret;
  1703. }
  1704. /**
  1705. * megasas_bios_param - Returns disk geometry for a disk
  1706. * @sdev: device handle
  1707. * @bdev: block device
  1708. * @capacity: drive capacity
  1709. * @geom: geometry parameters
  1710. */
  1711. static int
  1712. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1713. sector_t capacity, int geom[])
  1714. {
  1715. int heads;
  1716. int sectors;
  1717. sector_t cylinders;
  1718. unsigned long tmp;
  1719. /* Default heads (64) & sectors (32) */
  1720. heads = 64;
  1721. sectors = 32;
  1722. tmp = heads * sectors;
  1723. cylinders = capacity;
  1724. sector_div(cylinders, tmp);
  1725. /*
  1726. * Handle extended translation size for logical drives > 1Gb
  1727. */
  1728. if (capacity >= 0x200000) {
  1729. heads = 255;
  1730. sectors = 63;
  1731. tmp = heads*sectors;
  1732. cylinders = capacity;
  1733. sector_div(cylinders, tmp);
  1734. }
  1735. geom[0] = heads;
  1736. geom[1] = sectors;
  1737. geom[2] = cylinders;
  1738. return 0;
  1739. }
  1740. static void megasas_aen_polling(struct work_struct *work);
  1741. /**
  1742. * megasas_service_aen - Processes an event notification
  1743. * @instance: Adapter soft state
  1744. * @cmd: AEN command completed by the ISR
  1745. *
  1746. * For AEN, driver sends a command down to FW that is held by the FW till an
  1747. * event occurs. When an event of interest occurs, FW completes the command
  1748. * that it was previously holding.
  1749. *
  1750. * This routines sends SIGIO signal to processes that have registered with the
  1751. * driver for AEN.
  1752. */
  1753. static void
  1754. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1755. {
  1756. unsigned long flags;
  1757. /*
  1758. * Don't signal app if it is just an aborted previously registered aen
  1759. */
  1760. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1761. spin_lock_irqsave(&poll_aen_lock, flags);
  1762. megasas_poll_wait_aen = 1;
  1763. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1764. wake_up(&megasas_poll_wait);
  1765. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1766. }
  1767. else
  1768. cmd->abort_aen = 0;
  1769. instance->aen_cmd = NULL;
  1770. megasas_return_cmd(instance, cmd);
  1771. if ((instance->unload == 0) &&
  1772. ((instance->issuepend_done == 1))) {
  1773. struct megasas_aen_event *ev;
  1774. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1775. if (!ev) {
  1776. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1777. } else {
  1778. ev->instance = instance;
  1779. instance->ev = ev;
  1780. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1781. schedule_delayed_work(
  1782. (struct delayed_work *)&ev->hotplug_work, 0);
  1783. }
  1784. }
  1785. }
  1786. static int megasas_change_queue_depth(struct scsi_device *sdev,
  1787. int queue_depth, int reason)
  1788. {
  1789. if (reason != SCSI_QDEPTH_DEFAULT)
  1790. return -EOPNOTSUPP;
  1791. if (queue_depth > sdev->host->can_queue)
  1792. queue_depth = sdev->host->can_queue;
  1793. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1794. queue_depth);
  1795. return queue_depth;
  1796. }
  1797. /*
  1798. * Scsi host template for megaraid_sas driver
  1799. */
  1800. static struct scsi_host_template megasas_template = {
  1801. .module = THIS_MODULE,
  1802. .name = "LSI SAS based MegaRAID driver",
  1803. .proc_name = "megaraid_sas",
  1804. .slave_configure = megasas_slave_configure,
  1805. .slave_alloc = megasas_slave_alloc,
  1806. .queuecommand = megasas_queue_command,
  1807. .eh_device_reset_handler = megasas_reset_device,
  1808. .eh_bus_reset_handler = megasas_reset_bus_host,
  1809. .eh_host_reset_handler = megasas_reset_bus_host,
  1810. .eh_timed_out = megasas_reset_timer,
  1811. .bios_param = megasas_bios_param,
  1812. .use_clustering = ENABLE_CLUSTERING,
  1813. .change_queue_depth = megasas_change_queue_depth,
  1814. };
  1815. /**
  1816. * megasas_complete_int_cmd - Completes an internal command
  1817. * @instance: Adapter soft state
  1818. * @cmd: Command to be completed
  1819. *
  1820. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1821. * after it issues a command. This function wakes up that waiting routine by
  1822. * calling wake_up() on the wait queue.
  1823. */
  1824. static void
  1825. megasas_complete_int_cmd(struct megasas_instance *instance,
  1826. struct megasas_cmd *cmd)
  1827. {
  1828. cmd->cmd_status = cmd->frame->io.cmd_status;
  1829. if (cmd->cmd_status == ENODATA) {
  1830. cmd->cmd_status = 0;
  1831. }
  1832. wake_up(&instance->int_cmd_wait_q);
  1833. }
  1834. /**
  1835. * megasas_complete_abort - Completes aborting a command
  1836. * @instance: Adapter soft state
  1837. * @cmd: Cmd that was issued to abort another cmd
  1838. *
  1839. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1840. * after it issues an abort on a previously issued command. This function
  1841. * wakes up all functions waiting on the same wait queue.
  1842. */
  1843. static void
  1844. megasas_complete_abort(struct megasas_instance *instance,
  1845. struct megasas_cmd *cmd)
  1846. {
  1847. if (cmd->sync_cmd) {
  1848. cmd->sync_cmd = 0;
  1849. cmd->cmd_status = 0;
  1850. wake_up(&instance->abort_cmd_wait_q);
  1851. }
  1852. return;
  1853. }
  1854. /**
  1855. * megasas_complete_cmd - Completes a command
  1856. * @instance: Adapter soft state
  1857. * @cmd: Command to be completed
  1858. * @alt_status: If non-zero, use this value as status to
  1859. * SCSI mid-layer instead of the value returned
  1860. * by the FW. This should be used if caller wants
  1861. * an alternate status (as in the case of aborted
  1862. * commands)
  1863. */
  1864. void
  1865. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1866. u8 alt_status)
  1867. {
  1868. int exception = 0;
  1869. struct megasas_header *hdr = &cmd->frame->hdr;
  1870. unsigned long flags;
  1871. struct fusion_context *fusion = instance->ctrl_context;
  1872. /* flag for the retry reset */
  1873. cmd->retry_for_fw_reset = 0;
  1874. if (cmd->scmd)
  1875. cmd->scmd->SCp.ptr = NULL;
  1876. switch (hdr->cmd) {
  1877. case MFI_CMD_INVALID:
  1878. /* Some older 1068 controller FW may keep a pended
  1879. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  1880. when booting the kdump kernel. Ignore this command to
  1881. prevent a kernel panic on shutdown of the kdump kernel. */
  1882. printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
  1883. "completed.\n");
  1884. printk(KERN_WARNING "megaraid_sas: If you have a controller "
  1885. "other than PERC5, please upgrade your firmware.\n");
  1886. break;
  1887. case MFI_CMD_PD_SCSI_IO:
  1888. case MFI_CMD_LD_SCSI_IO:
  1889. /*
  1890. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1891. * issued either through an IO path or an IOCTL path. If it
  1892. * was via IOCTL, we will send it to internal completion.
  1893. */
  1894. if (cmd->sync_cmd) {
  1895. cmd->sync_cmd = 0;
  1896. megasas_complete_int_cmd(instance, cmd);
  1897. break;
  1898. }
  1899. case MFI_CMD_LD_READ:
  1900. case MFI_CMD_LD_WRITE:
  1901. if (alt_status) {
  1902. cmd->scmd->result = alt_status << 16;
  1903. exception = 1;
  1904. }
  1905. if (exception) {
  1906. atomic_dec(&instance->fw_outstanding);
  1907. scsi_dma_unmap(cmd->scmd);
  1908. cmd->scmd->scsi_done(cmd->scmd);
  1909. megasas_return_cmd(instance, cmd);
  1910. break;
  1911. }
  1912. switch (hdr->cmd_status) {
  1913. case MFI_STAT_OK:
  1914. cmd->scmd->result = DID_OK << 16;
  1915. break;
  1916. case MFI_STAT_SCSI_IO_FAILED:
  1917. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1918. cmd->scmd->result =
  1919. (DID_ERROR << 16) | hdr->scsi_status;
  1920. break;
  1921. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1922. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1923. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1924. memset(cmd->scmd->sense_buffer, 0,
  1925. SCSI_SENSE_BUFFERSIZE);
  1926. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1927. hdr->sense_len);
  1928. cmd->scmd->result |= DRIVER_SENSE << 24;
  1929. }
  1930. break;
  1931. case MFI_STAT_LD_OFFLINE:
  1932. case MFI_STAT_DEVICE_NOT_FOUND:
  1933. cmd->scmd->result = DID_BAD_TARGET << 16;
  1934. break;
  1935. default:
  1936. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1937. hdr->cmd_status);
  1938. cmd->scmd->result = DID_ERROR << 16;
  1939. break;
  1940. }
  1941. atomic_dec(&instance->fw_outstanding);
  1942. scsi_dma_unmap(cmd->scmd);
  1943. cmd->scmd->scsi_done(cmd->scmd);
  1944. megasas_return_cmd(instance, cmd);
  1945. break;
  1946. case MFI_CMD_SMP:
  1947. case MFI_CMD_STP:
  1948. case MFI_CMD_DCMD:
  1949. /* Check for LD map update */
  1950. if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
  1951. (cmd->frame->dcmd.mbox.b[1] == 1)) {
  1952. spin_lock_irqsave(instance->host->host_lock, flags);
  1953. if (cmd->frame->hdr.cmd_status != 0) {
  1954. if (cmd->frame->hdr.cmd_status !=
  1955. MFI_STAT_NOT_FOUND)
  1956. printk(KERN_WARNING "megasas: map sync"
  1957. "failed, status = 0x%x.\n",
  1958. cmd->frame->hdr.cmd_status);
  1959. else {
  1960. megasas_return_cmd(instance, cmd);
  1961. spin_unlock_irqrestore(
  1962. instance->host->host_lock,
  1963. flags);
  1964. break;
  1965. }
  1966. } else
  1967. instance->map_id++;
  1968. megasas_return_cmd(instance, cmd);
  1969. if (MR_ValidateMapInfo(
  1970. fusion->ld_map[(instance->map_id & 1)],
  1971. fusion->load_balance_info))
  1972. fusion->fast_path_io = 1;
  1973. else
  1974. fusion->fast_path_io = 0;
  1975. megasas_sync_map_info(instance);
  1976. spin_unlock_irqrestore(instance->host->host_lock,
  1977. flags);
  1978. break;
  1979. }
  1980. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1981. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1982. spin_lock_irqsave(&poll_aen_lock, flags);
  1983. megasas_poll_wait_aen = 0;
  1984. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1985. }
  1986. /*
  1987. * See if got an event notification
  1988. */
  1989. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1990. megasas_service_aen(instance, cmd);
  1991. else
  1992. megasas_complete_int_cmd(instance, cmd);
  1993. break;
  1994. case MFI_CMD_ABORT:
  1995. /*
  1996. * Cmd issued to abort another cmd returned
  1997. */
  1998. megasas_complete_abort(instance, cmd);
  1999. break;
  2000. default:
  2001. printk("megasas: Unknown command completed! [0x%X]\n",
  2002. hdr->cmd);
  2003. break;
  2004. }
  2005. }
  2006. /**
  2007. * megasas_issue_pending_cmds_again - issue all pending cmds
  2008. * in FW again because of the fw reset
  2009. * @instance: Adapter soft state
  2010. */
  2011. static inline void
  2012. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2013. {
  2014. struct megasas_cmd *cmd;
  2015. struct list_head clist_local;
  2016. union megasas_evt_class_locale class_locale;
  2017. unsigned long flags;
  2018. u32 seq_num;
  2019. INIT_LIST_HEAD(&clist_local);
  2020. spin_lock_irqsave(&instance->hba_lock, flags);
  2021. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2022. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2023. while (!list_empty(&clist_local)) {
  2024. cmd = list_entry((&clist_local)->next,
  2025. struct megasas_cmd, list);
  2026. list_del_init(&cmd->list);
  2027. if (cmd->sync_cmd || cmd->scmd) {
  2028. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  2029. "detected to be pending while HBA reset.\n",
  2030. cmd, cmd->scmd, cmd->sync_cmd);
  2031. cmd->retry_for_fw_reset++;
  2032. if (cmd->retry_for_fw_reset == 3) {
  2033. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2034. "was tried multiple times during reset."
  2035. "Shutting down the HBA\n",
  2036. cmd, cmd->scmd, cmd->sync_cmd);
  2037. megaraid_sas_kill_hba(instance);
  2038. instance->adprecovery =
  2039. MEGASAS_HW_CRITICAL_ERROR;
  2040. return;
  2041. }
  2042. }
  2043. if (cmd->sync_cmd == 1) {
  2044. if (cmd->scmd) {
  2045. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2046. "cmd attached to internal command!\n");
  2047. }
  2048. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2049. "on the internal reset queue,"
  2050. "issue it again.\n", cmd);
  2051. cmd->cmd_status = ENODATA;
  2052. instance->instancet->fire_cmd(instance,
  2053. cmd->frame_phys_addr ,
  2054. 0, instance->reg_set);
  2055. } else if (cmd->scmd) {
  2056. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
  2057. "detected on the internal queue, issue again.\n",
  2058. cmd, cmd->scmd->cmnd[0]);
  2059. atomic_inc(&instance->fw_outstanding);
  2060. instance->instancet->fire_cmd(instance,
  2061. cmd->frame_phys_addr,
  2062. cmd->frame_count-1, instance->reg_set);
  2063. } else {
  2064. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2065. "internal reset defer list while re-issue!!\n",
  2066. cmd);
  2067. }
  2068. }
  2069. if (instance->aen_cmd) {
  2070. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2071. megasas_return_cmd(instance, instance->aen_cmd);
  2072. instance->aen_cmd = NULL;
  2073. }
  2074. /*
  2075. * Initiate AEN (Asynchronous Event Notification)
  2076. */
  2077. seq_num = instance->last_seq_num;
  2078. class_locale.members.reserved = 0;
  2079. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2080. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2081. megasas_register_aen(instance, seq_num, class_locale.word);
  2082. }
  2083. /**
  2084. * Move the internal reset pending commands to a deferred queue.
  2085. *
  2086. * We move the commands pending at internal reset time to a
  2087. * pending queue. This queue would be flushed after successful
  2088. * completion of the internal reset sequence. if the internal reset
  2089. * did not complete in time, the kernel reset handler would flush
  2090. * these commands.
  2091. **/
  2092. static void
  2093. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2094. {
  2095. struct megasas_cmd *cmd;
  2096. int i;
  2097. u32 max_cmd = instance->max_fw_cmds;
  2098. u32 defer_index;
  2099. unsigned long flags;
  2100. defer_index = 0;
  2101. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2102. for (i = 0; i < max_cmd; i++) {
  2103. cmd = instance->cmd_list[i];
  2104. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2105. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2106. "on the defer queue as internal\n",
  2107. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2108. if (!list_empty(&cmd->list)) {
  2109. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2110. " moving this cmd:%p, %d %p, it was"
  2111. "discovered on some list?\n",
  2112. cmd, cmd->sync_cmd, cmd->scmd);
  2113. list_del_init(&cmd->list);
  2114. }
  2115. defer_index++;
  2116. list_add_tail(&cmd->list,
  2117. &instance->internal_reset_pending_q);
  2118. }
  2119. }
  2120. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2121. }
  2122. static void
  2123. process_fw_state_change_wq(struct work_struct *work)
  2124. {
  2125. struct megasas_instance *instance =
  2126. container_of(work, struct megasas_instance, work_init);
  2127. u32 wait;
  2128. unsigned long flags;
  2129. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2130. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2131. instance->adprecovery);
  2132. return ;
  2133. }
  2134. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2135. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2136. "state, restarting it...\n");
  2137. instance->instancet->disable_intr(instance->reg_set);
  2138. atomic_set(&instance->fw_outstanding, 0);
  2139. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2140. instance->instancet->adp_reset(instance, instance->reg_set);
  2141. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2142. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2143. "initiating next stage...\n");
  2144. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2145. "state 2 starting...\n");
  2146. /*waitting for about 20 second before start the second init*/
  2147. for (wait = 0; wait < 30; wait++) {
  2148. msleep(1000);
  2149. }
  2150. if (megasas_transition_to_ready(instance, 1)) {
  2151. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2152. megaraid_sas_kill_hba(instance);
  2153. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2154. return ;
  2155. }
  2156. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2157. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2158. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2159. ) {
  2160. *instance->consumer = *instance->producer;
  2161. } else {
  2162. *instance->consumer = 0;
  2163. *instance->producer = 0;
  2164. }
  2165. megasas_issue_init_mfi(instance);
  2166. spin_lock_irqsave(&instance->hba_lock, flags);
  2167. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2168. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2169. instance->instancet->enable_intr(instance->reg_set);
  2170. megasas_issue_pending_cmds_again(instance);
  2171. instance->issuepend_done = 1;
  2172. }
  2173. return ;
  2174. }
  2175. /**
  2176. * megasas_deplete_reply_queue - Processes all completed commands
  2177. * @instance: Adapter soft state
  2178. * @alt_status: Alternate status to be returned to
  2179. * SCSI mid-layer instead of the status
  2180. * returned by the FW
  2181. * Note: this must be called with hba lock held
  2182. */
  2183. static int
  2184. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2185. u8 alt_status)
  2186. {
  2187. u32 mfiStatus;
  2188. u32 fw_state;
  2189. if ((mfiStatus = instance->instancet->check_reset(instance,
  2190. instance->reg_set)) == 1) {
  2191. return IRQ_HANDLED;
  2192. }
  2193. if ((mfiStatus = instance->instancet->clear_intr(
  2194. instance->reg_set)
  2195. ) == 0) {
  2196. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2197. if (!instance->msix_vectors)
  2198. return IRQ_NONE;
  2199. }
  2200. instance->mfiStatus = mfiStatus;
  2201. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2202. fw_state = instance->instancet->read_fw_status_reg(
  2203. instance->reg_set) & MFI_STATE_MASK;
  2204. if (fw_state != MFI_STATE_FAULT) {
  2205. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2206. fw_state);
  2207. }
  2208. if ((fw_state == MFI_STATE_FAULT) &&
  2209. (instance->disableOnlineCtrlReset == 0)) {
  2210. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2211. if ((instance->pdev->device ==
  2212. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2213. (instance->pdev->device ==
  2214. PCI_DEVICE_ID_DELL_PERC5) ||
  2215. (instance->pdev->device ==
  2216. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2217. *instance->consumer =
  2218. MEGASAS_ADPRESET_INPROG_SIGN;
  2219. }
  2220. instance->instancet->disable_intr(instance->reg_set);
  2221. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2222. instance->issuepend_done = 0;
  2223. atomic_set(&instance->fw_outstanding, 0);
  2224. megasas_internal_reset_defer_cmds(instance);
  2225. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2226. fw_state, instance->adprecovery);
  2227. schedule_work(&instance->work_init);
  2228. return IRQ_HANDLED;
  2229. } else {
  2230. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2231. fw_state, instance->disableOnlineCtrlReset);
  2232. }
  2233. }
  2234. tasklet_schedule(&instance->isr_tasklet);
  2235. return IRQ_HANDLED;
  2236. }
  2237. /**
  2238. * megasas_isr - isr entry point
  2239. */
  2240. static irqreturn_t megasas_isr(int irq, void *devp)
  2241. {
  2242. struct megasas_irq_context *irq_context = devp;
  2243. struct megasas_instance *instance = irq_context->instance;
  2244. unsigned long flags;
  2245. irqreturn_t rc;
  2246. if (atomic_read(&instance->fw_reset_no_pci_access))
  2247. return IRQ_HANDLED;
  2248. spin_lock_irqsave(&instance->hba_lock, flags);
  2249. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2250. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2251. return rc;
  2252. }
  2253. /**
  2254. * megasas_transition_to_ready - Move the FW to READY state
  2255. * @instance: Adapter soft state
  2256. *
  2257. * During the initialization, FW passes can potentially be in any one of
  2258. * several possible states. If the FW in operational, waiting-for-handshake
  2259. * states, driver must take steps to bring it to ready state. Otherwise, it
  2260. * has to wait for the ready state.
  2261. */
  2262. int
  2263. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  2264. {
  2265. int i;
  2266. u8 max_wait;
  2267. u32 fw_state;
  2268. u32 cur_state;
  2269. u32 abs_state, curr_abs_state;
  2270. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2271. if (fw_state != MFI_STATE_READY)
  2272. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2273. " state\n");
  2274. while (fw_state != MFI_STATE_READY) {
  2275. abs_state =
  2276. instance->instancet->read_fw_status_reg(instance->reg_set);
  2277. switch (fw_state) {
  2278. case MFI_STATE_FAULT:
  2279. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2280. if (ocr) {
  2281. max_wait = MEGASAS_RESET_WAIT_TIME;
  2282. cur_state = MFI_STATE_FAULT;
  2283. break;
  2284. } else
  2285. return -ENODEV;
  2286. case MFI_STATE_WAIT_HANDSHAKE:
  2287. /*
  2288. * Set the CLR bit in inbound doorbell
  2289. */
  2290. if ((instance->pdev->device ==
  2291. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2292. (instance->pdev->device ==
  2293. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2294. (instance->pdev->device ==
  2295. PCI_DEVICE_ID_LSI_FUSION) ||
  2296. (instance->pdev->device ==
  2297. PCI_DEVICE_ID_LSI_INVADER)) {
  2298. writel(
  2299. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2300. &instance->reg_set->doorbell);
  2301. } else {
  2302. writel(
  2303. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2304. &instance->reg_set->inbound_doorbell);
  2305. }
  2306. max_wait = MEGASAS_RESET_WAIT_TIME;
  2307. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2308. break;
  2309. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2310. if ((instance->pdev->device ==
  2311. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2312. (instance->pdev->device ==
  2313. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2314. (instance->pdev->device ==
  2315. PCI_DEVICE_ID_LSI_FUSION) ||
  2316. (instance->pdev->device ==
  2317. PCI_DEVICE_ID_LSI_INVADER)) {
  2318. writel(MFI_INIT_HOTPLUG,
  2319. &instance->reg_set->doorbell);
  2320. } else
  2321. writel(MFI_INIT_HOTPLUG,
  2322. &instance->reg_set->inbound_doorbell);
  2323. max_wait = MEGASAS_RESET_WAIT_TIME;
  2324. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2325. break;
  2326. case MFI_STATE_OPERATIONAL:
  2327. /*
  2328. * Bring it to READY state; assuming max wait 10 secs
  2329. */
  2330. instance->instancet->disable_intr(instance->reg_set);
  2331. if ((instance->pdev->device ==
  2332. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2333. (instance->pdev->device ==
  2334. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2335. (instance->pdev->device
  2336. == PCI_DEVICE_ID_LSI_FUSION) ||
  2337. (instance->pdev->device
  2338. == PCI_DEVICE_ID_LSI_INVADER)) {
  2339. writel(MFI_RESET_FLAGS,
  2340. &instance->reg_set->doorbell);
  2341. if ((instance->pdev->device ==
  2342. PCI_DEVICE_ID_LSI_FUSION) ||
  2343. (instance->pdev->device ==
  2344. PCI_DEVICE_ID_LSI_INVADER)) {
  2345. for (i = 0; i < (10 * 1000); i += 20) {
  2346. if (readl(
  2347. &instance->
  2348. reg_set->
  2349. doorbell) & 1)
  2350. msleep(20);
  2351. else
  2352. break;
  2353. }
  2354. }
  2355. } else
  2356. writel(MFI_RESET_FLAGS,
  2357. &instance->reg_set->inbound_doorbell);
  2358. max_wait = MEGASAS_RESET_WAIT_TIME;
  2359. cur_state = MFI_STATE_OPERATIONAL;
  2360. break;
  2361. case MFI_STATE_UNDEFINED:
  2362. /*
  2363. * This state should not last for more than 2 seconds
  2364. */
  2365. max_wait = MEGASAS_RESET_WAIT_TIME;
  2366. cur_state = MFI_STATE_UNDEFINED;
  2367. break;
  2368. case MFI_STATE_BB_INIT:
  2369. max_wait = MEGASAS_RESET_WAIT_TIME;
  2370. cur_state = MFI_STATE_BB_INIT;
  2371. break;
  2372. case MFI_STATE_FW_INIT:
  2373. max_wait = MEGASAS_RESET_WAIT_TIME;
  2374. cur_state = MFI_STATE_FW_INIT;
  2375. break;
  2376. case MFI_STATE_FW_INIT_2:
  2377. max_wait = MEGASAS_RESET_WAIT_TIME;
  2378. cur_state = MFI_STATE_FW_INIT_2;
  2379. break;
  2380. case MFI_STATE_DEVICE_SCAN:
  2381. max_wait = MEGASAS_RESET_WAIT_TIME;
  2382. cur_state = MFI_STATE_DEVICE_SCAN;
  2383. break;
  2384. case MFI_STATE_FLUSH_CACHE:
  2385. max_wait = MEGASAS_RESET_WAIT_TIME;
  2386. cur_state = MFI_STATE_FLUSH_CACHE;
  2387. break;
  2388. default:
  2389. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2390. fw_state);
  2391. return -ENODEV;
  2392. }
  2393. /*
  2394. * The cur_state should not last for more than max_wait secs
  2395. */
  2396. for (i = 0; i < (max_wait * 1000); i++) {
  2397. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2398. MFI_STATE_MASK ;
  2399. curr_abs_state =
  2400. instance->instancet->read_fw_status_reg(instance->reg_set);
  2401. if (abs_state == curr_abs_state) {
  2402. msleep(1);
  2403. } else
  2404. break;
  2405. }
  2406. /*
  2407. * Return error if fw_state hasn't changed after max_wait
  2408. */
  2409. if (curr_abs_state == abs_state) {
  2410. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2411. "in %d secs\n", fw_state, max_wait);
  2412. return -ENODEV;
  2413. }
  2414. }
  2415. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2416. return 0;
  2417. }
  2418. /**
  2419. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2420. * @instance: Adapter soft state
  2421. */
  2422. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2423. {
  2424. int i;
  2425. u32 max_cmd = instance->max_mfi_cmds;
  2426. struct megasas_cmd *cmd;
  2427. if (!instance->frame_dma_pool)
  2428. return;
  2429. /*
  2430. * Return all frames to pool
  2431. */
  2432. for (i = 0; i < max_cmd; i++) {
  2433. cmd = instance->cmd_list[i];
  2434. if (cmd->frame)
  2435. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2436. cmd->frame_phys_addr);
  2437. if (cmd->sense)
  2438. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2439. cmd->sense_phys_addr);
  2440. }
  2441. /*
  2442. * Now destroy the pool itself
  2443. */
  2444. pci_pool_destroy(instance->frame_dma_pool);
  2445. pci_pool_destroy(instance->sense_dma_pool);
  2446. instance->frame_dma_pool = NULL;
  2447. instance->sense_dma_pool = NULL;
  2448. }
  2449. /**
  2450. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2451. * @instance: Adapter soft state
  2452. *
  2453. * Each command packet has an embedded DMA memory buffer that is used for
  2454. * filling MFI frame and the SG list that immediately follows the frame. This
  2455. * function creates those DMA memory buffers for each command packet by using
  2456. * PCI pool facility.
  2457. */
  2458. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2459. {
  2460. int i;
  2461. u32 max_cmd;
  2462. u32 sge_sz;
  2463. u32 sgl_sz;
  2464. u32 total_sz;
  2465. u32 frame_count;
  2466. struct megasas_cmd *cmd;
  2467. max_cmd = instance->max_mfi_cmds;
  2468. /*
  2469. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2470. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2471. */
  2472. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2473. sizeof(struct megasas_sge32);
  2474. if (instance->flag_ieee) {
  2475. sge_sz = sizeof(struct megasas_sge_skinny);
  2476. }
  2477. /*
  2478. * Calculated the number of 64byte frames required for SGL
  2479. */
  2480. sgl_sz = sge_sz * instance->max_num_sge;
  2481. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2482. frame_count = 15;
  2483. /*
  2484. * We need one extra frame for the MFI command
  2485. */
  2486. frame_count++;
  2487. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2488. /*
  2489. * Use DMA pool facility provided by PCI layer
  2490. */
  2491. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2492. instance->pdev, total_sz, 64,
  2493. 0);
  2494. if (!instance->frame_dma_pool) {
  2495. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2496. return -ENOMEM;
  2497. }
  2498. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2499. instance->pdev, 128, 4, 0);
  2500. if (!instance->sense_dma_pool) {
  2501. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2502. pci_pool_destroy(instance->frame_dma_pool);
  2503. instance->frame_dma_pool = NULL;
  2504. return -ENOMEM;
  2505. }
  2506. /*
  2507. * Allocate and attach a frame to each of the commands in cmd_list.
  2508. * By making cmd->index as the context instead of the &cmd, we can
  2509. * always use 32bit context regardless of the architecture
  2510. */
  2511. for (i = 0; i < max_cmd; i++) {
  2512. cmd = instance->cmd_list[i];
  2513. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2514. GFP_KERNEL, &cmd->frame_phys_addr);
  2515. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2516. GFP_KERNEL, &cmd->sense_phys_addr);
  2517. /*
  2518. * megasas_teardown_frame_pool() takes care of freeing
  2519. * whatever has been allocated
  2520. */
  2521. if (!cmd->frame || !cmd->sense) {
  2522. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2523. megasas_teardown_frame_pool(instance);
  2524. return -ENOMEM;
  2525. }
  2526. memset(cmd->frame, 0, total_sz);
  2527. cmd->frame->io.context = cmd->index;
  2528. cmd->frame->io.pad_0 = 0;
  2529. if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
  2530. (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
  2531. (reset_devices))
  2532. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  2533. }
  2534. return 0;
  2535. }
  2536. /**
  2537. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2538. * @instance: Adapter soft state
  2539. */
  2540. void megasas_free_cmds(struct megasas_instance *instance)
  2541. {
  2542. int i;
  2543. /* First free the MFI frame pool */
  2544. megasas_teardown_frame_pool(instance);
  2545. /* Free all the commands in the cmd_list */
  2546. for (i = 0; i < instance->max_mfi_cmds; i++)
  2547. kfree(instance->cmd_list[i]);
  2548. /* Free the cmd_list buffer itself */
  2549. kfree(instance->cmd_list);
  2550. instance->cmd_list = NULL;
  2551. INIT_LIST_HEAD(&instance->cmd_pool);
  2552. }
  2553. /**
  2554. * megasas_alloc_cmds - Allocates the command packets
  2555. * @instance: Adapter soft state
  2556. *
  2557. * Each command that is issued to the FW, whether IO commands from the OS or
  2558. * internal commands like IOCTLs, are wrapped in local data structure called
  2559. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2560. * the FW.
  2561. *
  2562. * Each frame has a 32-bit field called context (tag). This context is used
  2563. * to get back the megasas_cmd from the frame when a frame gets completed in
  2564. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2565. * the context. But we wanted to keep the differences between 32 and 64 bit
  2566. * systems to the mininum. We always use 32 bit integers for the context. In
  2567. * this driver, the 32 bit values are the indices into an array cmd_list.
  2568. * This array is used only to look up the megasas_cmd given the context. The
  2569. * free commands themselves are maintained in a linked list called cmd_pool.
  2570. */
  2571. int megasas_alloc_cmds(struct megasas_instance *instance)
  2572. {
  2573. int i;
  2574. int j;
  2575. u32 max_cmd;
  2576. struct megasas_cmd *cmd;
  2577. max_cmd = instance->max_mfi_cmds;
  2578. /*
  2579. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2580. * Allocate the dynamic array first and then allocate individual
  2581. * commands.
  2582. */
  2583. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2584. if (!instance->cmd_list) {
  2585. printk(KERN_DEBUG "megasas: out of memory\n");
  2586. return -ENOMEM;
  2587. }
  2588. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2589. for (i = 0; i < max_cmd; i++) {
  2590. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2591. GFP_KERNEL);
  2592. if (!instance->cmd_list[i]) {
  2593. for (j = 0; j < i; j++)
  2594. kfree(instance->cmd_list[j]);
  2595. kfree(instance->cmd_list);
  2596. instance->cmd_list = NULL;
  2597. return -ENOMEM;
  2598. }
  2599. }
  2600. /*
  2601. * Add all the commands to command pool (instance->cmd_pool)
  2602. */
  2603. for (i = 0; i < max_cmd; i++) {
  2604. cmd = instance->cmd_list[i];
  2605. memset(cmd, 0, sizeof(struct megasas_cmd));
  2606. cmd->index = i;
  2607. cmd->scmd = NULL;
  2608. cmd->instance = instance;
  2609. list_add_tail(&cmd->list, &instance->cmd_pool);
  2610. }
  2611. /*
  2612. * Create a frame pool and assign one frame to each cmd
  2613. */
  2614. if (megasas_create_frame_pool(instance)) {
  2615. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2616. megasas_free_cmds(instance);
  2617. }
  2618. return 0;
  2619. }
  2620. /*
  2621. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2622. * @instance: Adapter soft state
  2623. * @pd_list: pd_list structure
  2624. *
  2625. * Issues an internal command (DCMD) to get the FW's controller PD
  2626. * list structure. This information is mainly used to find out SYSTEM
  2627. * supported by the FW.
  2628. */
  2629. static int
  2630. megasas_get_pd_list(struct megasas_instance *instance)
  2631. {
  2632. int ret = 0, pd_index = 0;
  2633. struct megasas_cmd *cmd;
  2634. struct megasas_dcmd_frame *dcmd;
  2635. struct MR_PD_LIST *ci;
  2636. struct MR_PD_ADDRESS *pd_addr;
  2637. dma_addr_t ci_h = 0;
  2638. cmd = megasas_get_cmd(instance);
  2639. if (!cmd) {
  2640. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2641. return -ENOMEM;
  2642. }
  2643. dcmd = &cmd->frame->dcmd;
  2644. ci = pci_alloc_consistent(instance->pdev,
  2645. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2646. if (!ci) {
  2647. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2648. megasas_return_cmd(instance, cmd);
  2649. return -ENOMEM;
  2650. }
  2651. memset(ci, 0, sizeof(*ci));
  2652. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2653. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2654. dcmd->mbox.b[1] = 0;
  2655. dcmd->cmd = MFI_CMD_DCMD;
  2656. dcmd->cmd_status = 0xFF;
  2657. dcmd->sge_count = 1;
  2658. dcmd->flags = MFI_FRAME_DIR_READ;
  2659. dcmd->timeout = 0;
  2660. dcmd->pad_0 = 0;
  2661. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2662. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  2663. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2664. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2665. if (!megasas_issue_polled(instance, cmd)) {
  2666. ret = 0;
  2667. } else {
  2668. ret = -1;
  2669. }
  2670. /*
  2671. * the following function will get the instance PD LIST.
  2672. */
  2673. pd_addr = ci->addr;
  2674. if ( ret == 0 &&
  2675. (ci->count <
  2676. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2677. memset(instance->pd_list, 0,
  2678. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2679. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  2680. instance->pd_list[pd_addr->deviceId].tid =
  2681. pd_addr->deviceId;
  2682. instance->pd_list[pd_addr->deviceId].driveType =
  2683. pd_addr->scsiDevType;
  2684. instance->pd_list[pd_addr->deviceId].driveState =
  2685. MR_PD_STATE_SYSTEM;
  2686. pd_addr++;
  2687. }
  2688. }
  2689. pci_free_consistent(instance->pdev,
  2690. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2691. ci, ci_h);
  2692. megasas_return_cmd(instance, cmd);
  2693. return ret;
  2694. }
  2695. /*
  2696. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2697. * @instance: Adapter soft state
  2698. * @ld_list: ld_list structure
  2699. *
  2700. * Issues an internal command (DCMD) to get the FW's controller PD
  2701. * list structure. This information is mainly used to find out SYSTEM
  2702. * supported by the FW.
  2703. */
  2704. static int
  2705. megasas_get_ld_list(struct megasas_instance *instance)
  2706. {
  2707. int ret = 0, ld_index = 0, ids = 0;
  2708. struct megasas_cmd *cmd;
  2709. struct megasas_dcmd_frame *dcmd;
  2710. struct MR_LD_LIST *ci;
  2711. dma_addr_t ci_h = 0;
  2712. cmd = megasas_get_cmd(instance);
  2713. if (!cmd) {
  2714. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2715. return -ENOMEM;
  2716. }
  2717. dcmd = &cmd->frame->dcmd;
  2718. ci = pci_alloc_consistent(instance->pdev,
  2719. sizeof(struct MR_LD_LIST),
  2720. &ci_h);
  2721. if (!ci) {
  2722. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2723. megasas_return_cmd(instance, cmd);
  2724. return -ENOMEM;
  2725. }
  2726. memset(ci, 0, sizeof(*ci));
  2727. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2728. dcmd->cmd = MFI_CMD_DCMD;
  2729. dcmd->cmd_status = 0xFF;
  2730. dcmd->sge_count = 1;
  2731. dcmd->flags = MFI_FRAME_DIR_READ;
  2732. dcmd->timeout = 0;
  2733. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2734. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2735. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2736. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2737. dcmd->pad_0 = 0;
  2738. if (!megasas_issue_polled(instance, cmd)) {
  2739. ret = 0;
  2740. } else {
  2741. ret = -1;
  2742. }
  2743. /* the following function will get the instance PD LIST */
  2744. if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
  2745. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2746. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2747. if (ci->ldList[ld_index].state != 0) {
  2748. ids = ci->ldList[ld_index].ref.targetId;
  2749. instance->ld_ids[ids] =
  2750. ci->ldList[ld_index].ref.targetId;
  2751. }
  2752. }
  2753. }
  2754. pci_free_consistent(instance->pdev,
  2755. sizeof(struct MR_LD_LIST),
  2756. ci,
  2757. ci_h);
  2758. megasas_return_cmd(instance, cmd);
  2759. return ret;
  2760. }
  2761. /**
  2762. * megasas_get_controller_info - Returns FW's controller structure
  2763. * @instance: Adapter soft state
  2764. * @ctrl_info: Controller information structure
  2765. *
  2766. * Issues an internal command (DCMD) to get the FW's controller structure.
  2767. * This information is mainly used to find out the maximum IO transfer per
  2768. * command supported by the FW.
  2769. */
  2770. static int
  2771. megasas_get_ctrl_info(struct megasas_instance *instance,
  2772. struct megasas_ctrl_info *ctrl_info)
  2773. {
  2774. int ret = 0;
  2775. struct megasas_cmd *cmd;
  2776. struct megasas_dcmd_frame *dcmd;
  2777. struct megasas_ctrl_info *ci;
  2778. dma_addr_t ci_h = 0;
  2779. cmd = megasas_get_cmd(instance);
  2780. if (!cmd) {
  2781. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2782. return -ENOMEM;
  2783. }
  2784. dcmd = &cmd->frame->dcmd;
  2785. ci = pci_alloc_consistent(instance->pdev,
  2786. sizeof(struct megasas_ctrl_info), &ci_h);
  2787. if (!ci) {
  2788. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2789. megasas_return_cmd(instance, cmd);
  2790. return -ENOMEM;
  2791. }
  2792. memset(ci, 0, sizeof(*ci));
  2793. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2794. dcmd->cmd = MFI_CMD_DCMD;
  2795. dcmd->cmd_status = 0xFF;
  2796. dcmd->sge_count = 1;
  2797. dcmd->flags = MFI_FRAME_DIR_READ;
  2798. dcmd->timeout = 0;
  2799. dcmd->pad_0 = 0;
  2800. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2801. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2802. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2803. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2804. if (!megasas_issue_polled(instance, cmd)) {
  2805. ret = 0;
  2806. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2807. } else {
  2808. ret = -1;
  2809. }
  2810. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2811. ci, ci_h);
  2812. megasas_return_cmd(instance, cmd);
  2813. return ret;
  2814. }
  2815. /**
  2816. * megasas_issue_init_mfi - Initializes the FW
  2817. * @instance: Adapter soft state
  2818. *
  2819. * Issues the INIT MFI cmd
  2820. */
  2821. static int
  2822. megasas_issue_init_mfi(struct megasas_instance *instance)
  2823. {
  2824. u32 context;
  2825. struct megasas_cmd *cmd;
  2826. struct megasas_init_frame *init_frame;
  2827. struct megasas_init_queue_info *initq_info;
  2828. dma_addr_t init_frame_h;
  2829. dma_addr_t initq_info_h;
  2830. /*
  2831. * Prepare a init frame. Note the init frame points to queue info
  2832. * structure. Each frame has SGL allocated after first 64 bytes. For
  2833. * this frame - since we don't need any SGL - we use SGL's space as
  2834. * queue info structure
  2835. *
  2836. * We will not get a NULL command below. We just created the pool.
  2837. */
  2838. cmd = megasas_get_cmd(instance);
  2839. init_frame = (struct megasas_init_frame *)cmd->frame;
  2840. initq_info = (struct megasas_init_queue_info *)
  2841. ((unsigned long)init_frame + 64);
  2842. init_frame_h = cmd->frame_phys_addr;
  2843. initq_info_h = init_frame_h + 64;
  2844. context = init_frame->context;
  2845. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2846. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2847. init_frame->context = context;
  2848. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2849. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2850. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2851. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2852. init_frame->cmd = MFI_CMD_INIT;
  2853. init_frame->cmd_status = 0xFF;
  2854. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2855. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2856. /*
  2857. * disable the intr before firing the init frame to FW
  2858. */
  2859. instance->instancet->disable_intr(instance->reg_set);
  2860. /*
  2861. * Issue the init frame in polled mode
  2862. */
  2863. if (megasas_issue_polled(instance, cmd)) {
  2864. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2865. megasas_return_cmd(instance, cmd);
  2866. goto fail_fw_init;
  2867. }
  2868. megasas_return_cmd(instance, cmd);
  2869. return 0;
  2870. fail_fw_init:
  2871. return -EINVAL;
  2872. }
  2873. static u32
  2874. megasas_init_adapter_mfi(struct megasas_instance *instance)
  2875. {
  2876. struct megasas_register_set __iomem *reg_set;
  2877. u32 context_sz;
  2878. u32 reply_q_sz;
  2879. reg_set = instance->reg_set;
  2880. /*
  2881. * Get various operational parameters from status register
  2882. */
  2883. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2884. /*
  2885. * Reduce the max supported cmds by 1. This is to ensure that the
  2886. * reply_q_sz (1 more than the max cmd that driver may send)
  2887. * does not exceed max cmds that the FW can support
  2888. */
  2889. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2890. instance->max_mfi_cmds = instance->max_fw_cmds;
  2891. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2892. 0x10;
  2893. /*
  2894. * Create a pool of commands
  2895. */
  2896. if (megasas_alloc_cmds(instance))
  2897. goto fail_alloc_cmds;
  2898. /*
  2899. * Allocate memory for reply queue. Length of reply queue should
  2900. * be _one_ more than the maximum commands handled by the firmware.
  2901. *
  2902. * Note: When FW completes commands, it places corresponding contex
  2903. * values in this circular reply queue. This circular queue is a fairly
  2904. * typical producer-consumer queue. FW is the producer (of completed
  2905. * commands) and the driver is the consumer.
  2906. */
  2907. context_sz = sizeof(u32);
  2908. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2909. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2910. reply_q_sz,
  2911. &instance->reply_queue_h);
  2912. if (!instance->reply_queue) {
  2913. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2914. goto fail_reply_queue;
  2915. }
  2916. if (megasas_issue_init_mfi(instance))
  2917. goto fail_fw_init;
  2918. instance->fw_support_ieee = 0;
  2919. instance->fw_support_ieee =
  2920. (instance->instancet->read_fw_status_reg(reg_set) &
  2921. 0x04000000);
  2922. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  2923. instance->fw_support_ieee);
  2924. if (instance->fw_support_ieee)
  2925. instance->flag_ieee = 1;
  2926. return 0;
  2927. fail_fw_init:
  2928. pci_free_consistent(instance->pdev, reply_q_sz,
  2929. instance->reply_queue, instance->reply_queue_h);
  2930. fail_reply_queue:
  2931. megasas_free_cmds(instance);
  2932. fail_alloc_cmds:
  2933. return 1;
  2934. }
  2935. /**
  2936. * megasas_init_fw - Initializes the FW
  2937. * @instance: Adapter soft state
  2938. *
  2939. * This is the main function for initializing firmware
  2940. */
  2941. static int megasas_init_fw(struct megasas_instance *instance)
  2942. {
  2943. u32 max_sectors_1;
  2944. u32 max_sectors_2;
  2945. u32 tmp_sectors, msix_enable;
  2946. struct megasas_register_set __iomem *reg_set;
  2947. struct megasas_ctrl_info *ctrl_info;
  2948. unsigned long bar_list;
  2949. int i;
  2950. /* Find first memory bar */
  2951. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  2952. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  2953. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  2954. if (pci_request_selected_regions(instance->pdev, instance->bar,
  2955. "megasas: LSI")) {
  2956. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2957. return -EBUSY;
  2958. }
  2959. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2960. if (!instance->reg_set) {
  2961. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2962. goto fail_ioremap;
  2963. }
  2964. reg_set = instance->reg_set;
  2965. switch (instance->pdev->device) {
  2966. case PCI_DEVICE_ID_LSI_FUSION:
  2967. case PCI_DEVICE_ID_LSI_INVADER:
  2968. instance->instancet = &megasas_instance_template_fusion;
  2969. break;
  2970. case PCI_DEVICE_ID_LSI_SAS1078R:
  2971. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2972. instance->instancet = &megasas_instance_template_ppc;
  2973. break;
  2974. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2975. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2976. instance->instancet = &megasas_instance_template_gen2;
  2977. break;
  2978. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2979. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2980. instance->instancet = &megasas_instance_template_skinny;
  2981. break;
  2982. case PCI_DEVICE_ID_LSI_SAS1064R:
  2983. case PCI_DEVICE_ID_DELL_PERC5:
  2984. default:
  2985. instance->instancet = &megasas_instance_template_xscale;
  2986. break;
  2987. }
  2988. /*
  2989. * We expect the FW state to be READY
  2990. */
  2991. if (megasas_transition_to_ready(instance, 0))
  2992. goto fail_ready_state;
  2993. /* Check if MSI-X is supported while in ready state */
  2994. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  2995. 0x4000000) >> 0x1a;
  2996. if (msix_enable && !msix_disable) {
  2997. /* Check max MSI-X vectors */
  2998. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  2999. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  3000. instance->msix_vectors = (readl(&instance->reg_set->
  3001. outbound_scratch_pad_2
  3002. ) & 0x1F) + 1;
  3003. if (msix_vectors)
  3004. instance->msix_vectors =
  3005. min(msix_vectors,
  3006. instance->msix_vectors);
  3007. } else
  3008. instance->msix_vectors = 1;
  3009. /* Don't bother allocating more MSI-X vectors than cpus */
  3010. instance->msix_vectors = min(instance->msix_vectors,
  3011. (unsigned int)num_online_cpus());
  3012. for (i = 0; i < instance->msix_vectors; i++)
  3013. instance->msixentry[i].entry = i;
  3014. i = pci_enable_msix(instance->pdev, instance->msixentry,
  3015. instance->msix_vectors);
  3016. if (i >= 0) {
  3017. if (i) {
  3018. if (!pci_enable_msix(instance->pdev,
  3019. instance->msixentry, i))
  3020. instance->msix_vectors = i;
  3021. else
  3022. instance->msix_vectors = 0;
  3023. }
  3024. } else
  3025. instance->msix_vectors = 0;
  3026. }
  3027. /* Get operational params, sge flags, send init cmd to controller */
  3028. if (instance->instancet->init_adapter(instance))
  3029. goto fail_init_adapter;
  3030. printk(KERN_ERR "megasas: INIT adapter done\n");
  3031. /** for passthrough
  3032. * the following function will get the PD LIST.
  3033. */
  3034. memset(instance->pd_list, 0 ,
  3035. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  3036. megasas_get_pd_list(instance);
  3037. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3038. megasas_get_ld_list(instance);
  3039. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3040. /*
  3041. * Compute the max allowed sectors per IO: The controller info has two
  3042. * limits on max sectors. Driver should use the minimum of these two.
  3043. *
  3044. * 1 << stripe_sz_ops.min = max sectors per strip
  3045. *
  3046. * Note that older firmwares ( < FW ver 30) didn't report information
  3047. * to calculate max_sectors_1. So the number ended up as zero always.
  3048. */
  3049. tmp_sectors = 0;
  3050. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3051. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3052. ctrl_info->max_strips_per_io;
  3053. max_sectors_2 = ctrl_info->max_request_size;
  3054. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3055. instance->disableOnlineCtrlReset =
  3056. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3057. }
  3058. instance->max_sectors_per_req = instance->max_num_sge *
  3059. PAGE_SIZE / 512;
  3060. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3061. instance->max_sectors_per_req = tmp_sectors;
  3062. kfree(ctrl_info);
  3063. /* Check for valid throttlequeuedepth module parameter */
  3064. if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
  3065. instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
  3066. if (throttlequeuedepth > (instance->max_fw_cmds -
  3067. MEGASAS_SKINNY_INT_CMDS))
  3068. instance->throttlequeuedepth =
  3069. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3070. else
  3071. instance->throttlequeuedepth = throttlequeuedepth;
  3072. } else {
  3073. if (throttlequeuedepth > (instance->max_fw_cmds -
  3074. MEGASAS_INT_CMDS))
  3075. instance->throttlequeuedepth =
  3076. MEGASAS_THROTTLE_QUEUE_DEPTH;
  3077. else
  3078. instance->throttlequeuedepth = throttlequeuedepth;
  3079. }
  3080. /*
  3081. * Setup tasklet for cmd completion
  3082. */
  3083. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3084. (unsigned long)instance);
  3085. return 0;
  3086. fail_init_adapter:
  3087. fail_ready_state:
  3088. iounmap(instance->reg_set);
  3089. fail_ioremap:
  3090. pci_release_selected_regions(instance->pdev, instance->bar);
  3091. return -EINVAL;
  3092. }
  3093. /**
  3094. * megasas_release_mfi - Reverses the FW initialization
  3095. * @intance: Adapter soft state
  3096. */
  3097. static void megasas_release_mfi(struct megasas_instance *instance)
  3098. {
  3099. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3100. if (instance->reply_queue)
  3101. pci_free_consistent(instance->pdev, reply_q_sz,
  3102. instance->reply_queue, instance->reply_queue_h);
  3103. megasas_free_cmds(instance);
  3104. iounmap(instance->reg_set);
  3105. pci_release_selected_regions(instance->pdev, instance->bar);
  3106. }
  3107. /**
  3108. * megasas_get_seq_num - Gets latest event sequence numbers
  3109. * @instance: Adapter soft state
  3110. * @eli: FW event log sequence numbers information
  3111. *
  3112. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3113. * usually find out the latest sequence number of the events, the seq number at
  3114. * the boot etc. They would "read" all the events below the latest seq number
  3115. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3116. * number), they would subsribe to AEN (asynchronous event notification) and
  3117. * wait for the events to happen.
  3118. */
  3119. static int
  3120. megasas_get_seq_num(struct megasas_instance *instance,
  3121. struct megasas_evt_log_info *eli)
  3122. {
  3123. struct megasas_cmd *cmd;
  3124. struct megasas_dcmd_frame *dcmd;
  3125. struct megasas_evt_log_info *el_info;
  3126. dma_addr_t el_info_h = 0;
  3127. cmd = megasas_get_cmd(instance);
  3128. if (!cmd) {
  3129. return -ENOMEM;
  3130. }
  3131. dcmd = &cmd->frame->dcmd;
  3132. el_info = pci_alloc_consistent(instance->pdev,
  3133. sizeof(struct megasas_evt_log_info),
  3134. &el_info_h);
  3135. if (!el_info) {
  3136. megasas_return_cmd(instance, cmd);
  3137. return -ENOMEM;
  3138. }
  3139. memset(el_info, 0, sizeof(*el_info));
  3140. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3141. dcmd->cmd = MFI_CMD_DCMD;
  3142. dcmd->cmd_status = 0x0;
  3143. dcmd->sge_count = 1;
  3144. dcmd->flags = MFI_FRAME_DIR_READ;
  3145. dcmd->timeout = 0;
  3146. dcmd->pad_0 = 0;
  3147. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  3148. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  3149. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  3150. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  3151. megasas_issue_blocked_cmd(instance, cmd);
  3152. /*
  3153. * Copy the data back into callers buffer
  3154. */
  3155. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  3156. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3157. el_info, el_info_h);
  3158. megasas_return_cmd(instance, cmd);
  3159. return 0;
  3160. }
  3161. /**
  3162. * megasas_register_aen - Registers for asynchronous event notification
  3163. * @instance: Adapter soft state
  3164. * @seq_num: The starting sequence number
  3165. * @class_locale: Class of the event
  3166. *
  3167. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3168. * to be notified if and only if the event is of type @class_locale
  3169. */
  3170. static int
  3171. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3172. u32 class_locale_word)
  3173. {
  3174. int ret_val;
  3175. struct megasas_cmd *cmd;
  3176. struct megasas_dcmd_frame *dcmd;
  3177. union megasas_evt_class_locale curr_aen;
  3178. union megasas_evt_class_locale prev_aen;
  3179. /*
  3180. * If there an AEN pending already (aen_cmd), check if the
  3181. * class_locale of that pending AEN is inclusive of the new
  3182. * AEN request we currently have. If it is, then we don't have
  3183. * to do anything. In other words, whichever events the current
  3184. * AEN request is subscribing to, have already been subscribed
  3185. * to.
  3186. *
  3187. * If the old_cmd is _not_ inclusive, then we have to abort
  3188. * that command, form a class_locale that is superset of both
  3189. * old and current and re-issue to the FW
  3190. */
  3191. curr_aen.word = class_locale_word;
  3192. if (instance->aen_cmd) {
  3193. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3194. /*
  3195. * A class whose enum value is smaller is inclusive of all
  3196. * higher values. If a PROGRESS (= -1) was previously
  3197. * registered, then a new registration requests for higher
  3198. * classes need not be sent to FW. They are automatically
  3199. * included.
  3200. *
  3201. * Locale numbers don't have such hierarchy. They are bitmap
  3202. * values
  3203. */
  3204. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3205. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3206. curr_aen.members.locale)) {
  3207. /*
  3208. * Previously issued event registration includes
  3209. * current request. Nothing to do.
  3210. */
  3211. return 0;
  3212. } else {
  3213. curr_aen.members.locale |= prev_aen.members.locale;
  3214. if (prev_aen.members.class < curr_aen.members.class)
  3215. curr_aen.members.class = prev_aen.members.class;
  3216. instance->aen_cmd->abort_aen = 1;
  3217. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3218. instance->
  3219. aen_cmd);
  3220. if (ret_val) {
  3221. printk(KERN_DEBUG "megasas: Failed to abort "
  3222. "previous AEN command\n");
  3223. return ret_val;
  3224. }
  3225. }
  3226. }
  3227. cmd = megasas_get_cmd(instance);
  3228. if (!cmd)
  3229. return -ENOMEM;
  3230. dcmd = &cmd->frame->dcmd;
  3231. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3232. /*
  3233. * Prepare DCMD for aen registration
  3234. */
  3235. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3236. dcmd->cmd = MFI_CMD_DCMD;
  3237. dcmd->cmd_status = 0x0;
  3238. dcmd->sge_count = 1;
  3239. dcmd->flags = MFI_FRAME_DIR_READ;
  3240. dcmd->timeout = 0;
  3241. dcmd->pad_0 = 0;
  3242. instance->last_seq_num = seq_num;
  3243. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  3244. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  3245. dcmd->mbox.w[0] = seq_num;
  3246. dcmd->mbox.w[1] = curr_aen.word;
  3247. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  3248. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  3249. if (instance->aen_cmd != NULL) {
  3250. megasas_return_cmd(instance, cmd);
  3251. return 0;
  3252. }
  3253. /*
  3254. * Store reference to the cmd used to register for AEN. When an
  3255. * application wants us to register for AEN, we have to abort this
  3256. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3257. */
  3258. instance->aen_cmd = cmd;
  3259. /*
  3260. * Issue the aen registration frame
  3261. */
  3262. instance->instancet->issue_dcmd(instance, cmd);
  3263. return 0;
  3264. }
  3265. /**
  3266. * megasas_start_aen - Subscribes to AEN during driver load time
  3267. * @instance: Adapter soft state
  3268. */
  3269. static int megasas_start_aen(struct megasas_instance *instance)
  3270. {
  3271. struct megasas_evt_log_info eli;
  3272. union megasas_evt_class_locale class_locale;
  3273. /*
  3274. * Get the latest sequence number from FW
  3275. */
  3276. memset(&eli, 0, sizeof(eli));
  3277. if (megasas_get_seq_num(instance, &eli))
  3278. return -1;
  3279. /*
  3280. * Register AEN with FW for latest sequence number plus 1
  3281. */
  3282. class_locale.members.reserved = 0;
  3283. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3284. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3285. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  3286. class_locale.word);
  3287. }
  3288. /**
  3289. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3290. * @instance: Adapter soft state
  3291. */
  3292. static int megasas_io_attach(struct megasas_instance *instance)
  3293. {
  3294. struct Scsi_Host *host = instance->host;
  3295. /*
  3296. * Export parameters required by SCSI mid-layer
  3297. */
  3298. host->irq = instance->pdev->irq;
  3299. host->unique_id = instance->unique_id;
  3300. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3301. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3302. host->can_queue =
  3303. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3304. } else
  3305. host->can_queue =
  3306. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3307. host->this_id = instance->init_id;
  3308. host->sg_tablesize = instance->max_num_sge;
  3309. if (instance->fw_support_ieee)
  3310. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3311. /*
  3312. * Check if the module parameter value for max_sectors can be used
  3313. */
  3314. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3315. instance->max_sectors_per_req = max_sectors;
  3316. else {
  3317. if (max_sectors) {
  3318. if (((instance->pdev->device ==
  3319. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3320. (instance->pdev->device ==
  3321. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3322. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3323. instance->max_sectors_per_req = max_sectors;
  3324. } else {
  3325. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3326. "and <= %d (or < 1MB for GEN2 controller)\n",
  3327. instance->max_sectors_per_req);
  3328. }
  3329. }
  3330. }
  3331. host->max_sectors = instance->max_sectors_per_req;
  3332. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3333. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3334. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3335. host->max_lun = MEGASAS_MAX_LUN;
  3336. host->max_cmd_len = 16;
  3337. /* Fusion only supports host reset */
  3338. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3339. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
  3340. host->hostt->eh_device_reset_handler = NULL;
  3341. host->hostt->eh_bus_reset_handler = NULL;
  3342. }
  3343. /*
  3344. * Notify the mid-layer about the new controller
  3345. */
  3346. if (scsi_add_host(host, &instance->pdev->dev)) {
  3347. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3348. return -ENODEV;
  3349. }
  3350. /*
  3351. * Trigger SCSI to scan our drives
  3352. */
  3353. scsi_scan_host(host);
  3354. return 0;
  3355. }
  3356. static int
  3357. megasas_set_dma_mask(struct pci_dev *pdev)
  3358. {
  3359. /*
  3360. * All our contollers are capable of performing 64-bit DMA
  3361. */
  3362. if (IS_DMA64) {
  3363. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3364. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3365. goto fail_set_dma_mask;
  3366. }
  3367. } else {
  3368. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3369. goto fail_set_dma_mask;
  3370. }
  3371. return 0;
  3372. fail_set_dma_mask:
  3373. return 1;
  3374. }
  3375. /**
  3376. * megasas_probe_one - PCI hotplug entry point
  3377. * @pdev: PCI device structure
  3378. * @id: PCI ids of supported hotplugged adapter
  3379. */
  3380. static int __devinit
  3381. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3382. {
  3383. int rval, pos, i, j;
  3384. struct Scsi_Host *host;
  3385. struct megasas_instance *instance;
  3386. u16 control = 0;
  3387. /* Reset MSI-X in the kdump kernel */
  3388. if (reset_devices) {
  3389. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3390. if (pos) {
  3391. pci_read_config_word(pdev, msi_control_reg(pos),
  3392. &control);
  3393. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3394. dev_info(&pdev->dev, "resetting MSI-X\n");
  3395. pci_write_config_word(pdev,
  3396. msi_control_reg(pos),
  3397. control &
  3398. ~PCI_MSIX_FLAGS_ENABLE);
  3399. }
  3400. }
  3401. }
  3402. /*
  3403. * Announce PCI information
  3404. */
  3405. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3406. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3407. pdev->subsystem_device);
  3408. printk("bus %d:slot %d:func %d\n",
  3409. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3410. /*
  3411. * PCI prepping: enable device set bus mastering and dma mask
  3412. */
  3413. rval = pci_enable_device_mem(pdev);
  3414. if (rval) {
  3415. return rval;
  3416. }
  3417. pci_set_master(pdev);
  3418. if (megasas_set_dma_mask(pdev))
  3419. goto fail_set_dma_mask;
  3420. host = scsi_host_alloc(&megasas_template,
  3421. sizeof(struct megasas_instance));
  3422. if (!host) {
  3423. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3424. goto fail_alloc_instance;
  3425. }
  3426. instance = (struct megasas_instance *)host->hostdata;
  3427. memset(instance, 0, sizeof(*instance));
  3428. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3429. instance->pdev = pdev;
  3430. switch (instance->pdev->device) {
  3431. case PCI_DEVICE_ID_LSI_FUSION:
  3432. case PCI_DEVICE_ID_LSI_INVADER:
  3433. {
  3434. struct fusion_context *fusion;
  3435. instance->ctrl_context =
  3436. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3437. if (!instance->ctrl_context) {
  3438. printk(KERN_DEBUG "megasas: Failed to allocate "
  3439. "memory for Fusion context info\n");
  3440. goto fail_alloc_dma_buf;
  3441. }
  3442. fusion = instance->ctrl_context;
  3443. INIT_LIST_HEAD(&fusion->cmd_pool);
  3444. spin_lock_init(&fusion->cmd_pool_lock);
  3445. }
  3446. break;
  3447. default: /* For all other supported controllers */
  3448. instance->producer =
  3449. pci_alloc_consistent(pdev, sizeof(u32),
  3450. &instance->producer_h);
  3451. instance->consumer =
  3452. pci_alloc_consistent(pdev, sizeof(u32),
  3453. &instance->consumer_h);
  3454. if (!instance->producer || !instance->consumer) {
  3455. printk(KERN_DEBUG "megasas: Failed to allocate"
  3456. "memory for producer, consumer\n");
  3457. goto fail_alloc_dma_buf;
  3458. }
  3459. *instance->producer = 0;
  3460. *instance->consumer = 0;
  3461. break;
  3462. }
  3463. megasas_poll_wait_aen = 0;
  3464. instance->flag_ieee = 0;
  3465. instance->ev = NULL;
  3466. instance->issuepend_done = 1;
  3467. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3468. megasas_poll_wait_aen = 0;
  3469. instance->evt_detail = pci_alloc_consistent(pdev,
  3470. sizeof(struct
  3471. megasas_evt_detail),
  3472. &instance->evt_detail_h);
  3473. if (!instance->evt_detail) {
  3474. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3475. "event detail structure\n");
  3476. goto fail_alloc_dma_buf;
  3477. }
  3478. /*
  3479. * Initialize locks and queues
  3480. */
  3481. INIT_LIST_HEAD(&instance->cmd_pool);
  3482. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3483. atomic_set(&instance->fw_outstanding,0);
  3484. init_waitqueue_head(&instance->int_cmd_wait_q);
  3485. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3486. spin_lock_init(&instance->cmd_pool_lock);
  3487. spin_lock_init(&instance->hba_lock);
  3488. spin_lock_init(&instance->completion_lock);
  3489. mutex_init(&instance->aen_mutex);
  3490. mutex_init(&instance->reset_mutex);
  3491. /*
  3492. * Initialize PCI related and misc parameters
  3493. */
  3494. instance->host = host;
  3495. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3496. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3497. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3498. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3499. instance->flag_ieee = 1;
  3500. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3501. } else
  3502. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3503. megasas_dbg_lvl = 0;
  3504. instance->flag = 0;
  3505. instance->unload = 1;
  3506. instance->last_time = 0;
  3507. instance->disableOnlineCtrlReset = 1;
  3508. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3509. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  3510. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3511. else
  3512. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3513. /*
  3514. * Initialize MFI Firmware
  3515. */
  3516. if (megasas_init_fw(instance))
  3517. goto fail_init_mfi;
  3518. /*
  3519. * Register IRQ
  3520. */
  3521. if (instance->msix_vectors) {
  3522. for (i = 0 ; i < instance->msix_vectors; i++) {
  3523. instance->irq_context[i].instance = instance;
  3524. instance->irq_context[i].MSIxIndex = i;
  3525. if (request_irq(instance->msixentry[i].vector,
  3526. instance->instancet->service_isr, 0,
  3527. "megasas",
  3528. &instance->irq_context[i])) {
  3529. printk(KERN_DEBUG "megasas: Failed to "
  3530. "register IRQ for vector %d.\n", i);
  3531. for (j = 0 ; j < i ; j++)
  3532. free_irq(
  3533. instance->msixentry[j].vector,
  3534. &instance->irq_context[j]);
  3535. goto fail_irq;
  3536. }
  3537. }
  3538. } else {
  3539. instance->irq_context[0].instance = instance;
  3540. instance->irq_context[0].MSIxIndex = 0;
  3541. if (request_irq(pdev->irq, instance->instancet->service_isr,
  3542. IRQF_SHARED, "megasas",
  3543. &instance->irq_context[0])) {
  3544. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3545. goto fail_irq;
  3546. }
  3547. }
  3548. instance->instancet->enable_intr(instance->reg_set);
  3549. /*
  3550. * Store instance in PCI softstate
  3551. */
  3552. pci_set_drvdata(pdev, instance);
  3553. /*
  3554. * Add this controller to megasas_mgmt_info structure so that it
  3555. * can be exported to management applications
  3556. */
  3557. megasas_mgmt_info.count++;
  3558. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3559. megasas_mgmt_info.max_index++;
  3560. /*
  3561. * Register with SCSI mid-layer
  3562. */
  3563. if (megasas_io_attach(instance))
  3564. goto fail_io_attach;
  3565. instance->unload = 0;
  3566. /*
  3567. * Initiate AEN (Asynchronous Event Notification)
  3568. */
  3569. if (megasas_start_aen(instance)) {
  3570. printk(KERN_DEBUG "megasas: start aen failed\n");
  3571. goto fail_start_aen;
  3572. }
  3573. return 0;
  3574. fail_start_aen:
  3575. fail_io_attach:
  3576. megasas_mgmt_info.count--;
  3577. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3578. megasas_mgmt_info.max_index--;
  3579. pci_set_drvdata(pdev, NULL);
  3580. instance->instancet->disable_intr(instance->reg_set);
  3581. if (instance->msix_vectors)
  3582. for (i = 0 ; i < instance->msix_vectors; i++)
  3583. free_irq(instance->msixentry[i].vector,
  3584. &instance->irq_context[i]);
  3585. else
  3586. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3587. fail_irq:
  3588. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  3589. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
  3590. megasas_release_fusion(instance);
  3591. else
  3592. megasas_release_mfi(instance);
  3593. fail_init_mfi:
  3594. if (instance->msix_vectors)
  3595. pci_disable_msix(instance->pdev);
  3596. fail_alloc_dma_buf:
  3597. if (instance->evt_detail)
  3598. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3599. instance->evt_detail,
  3600. instance->evt_detail_h);
  3601. if (instance->producer)
  3602. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3603. instance->producer_h);
  3604. if (instance->consumer)
  3605. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3606. instance->consumer_h);
  3607. scsi_host_put(host);
  3608. fail_alloc_instance:
  3609. fail_set_dma_mask:
  3610. pci_disable_device(pdev);
  3611. return -ENODEV;
  3612. }
  3613. /**
  3614. * megasas_flush_cache - Requests FW to flush all its caches
  3615. * @instance: Adapter soft state
  3616. */
  3617. static void megasas_flush_cache(struct megasas_instance *instance)
  3618. {
  3619. struct megasas_cmd *cmd;
  3620. struct megasas_dcmd_frame *dcmd;
  3621. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3622. return;
  3623. cmd = megasas_get_cmd(instance);
  3624. if (!cmd)
  3625. return;
  3626. dcmd = &cmd->frame->dcmd;
  3627. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3628. dcmd->cmd = MFI_CMD_DCMD;
  3629. dcmd->cmd_status = 0x0;
  3630. dcmd->sge_count = 0;
  3631. dcmd->flags = MFI_FRAME_DIR_NONE;
  3632. dcmd->timeout = 0;
  3633. dcmd->pad_0 = 0;
  3634. dcmd->data_xfer_len = 0;
  3635. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  3636. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3637. megasas_issue_blocked_cmd(instance, cmd);
  3638. megasas_return_cmd(instance, cmd);
  3639. return;
  3640. }
  3641. /**
  3642. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3643. * @instance: Adapter soft state
  3644. * @opcode: Shutdown/Hibernate
  3645. */
  3646. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3647. u32 opcode)
  3648. {
  3649. struct megasas_cmd *cmd;
  3650. struct megasas_dcmd_frame *dcmd;
  3651. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3652. return;
  3653. cmd = megasas_get_cmd(instance);
  3654. if (!cmd)
  3655. return;
  3656. if (instance->aen_cmd)
  3657. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3658. if (instance->map_update_cmd)
  3659. megasas_issue_blocked_abort_cmd(instance,
  3660. instance->map_update_cmd);
  3661. dcmd = &cmd->frame->dcmd;
  3662. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3663. dcmd->cmd = MFI_CMD_DCMD;
  3664. dcmd->cmd_status = 0x0;
  3665. dcmd->sge_count = 0;
  3666. dcmd->flags = MFI_FRAME_DIR_NONE;
  3667. dcmd->timeout = 0;
  3668. dcmd->pad_0 = 0;
  3669. dcmd->data_xfer_len = 0;
  3670. dcmd->opcode = opcode;
  3671. megasas_issue_blocked_cmd(instance, cmd);
  3672. megasas_return_cmd(instance, cmd);
  3673. return;
  3674. }
  3675. #ifdef CONFIG_PM
  3676. /**
  3677. * megasas_suspend - driver suspend entry point
  3678. * @pdev: PCI device structure
  3679. * @state: PCI power state to suspend routine
  3680. */
  3681. static int
  3682. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3683. {
  3684. struct Scsi_Host *host;
  3685. struct megasas_instance *instance;
  3686. int i;
  3687. instance = pci_get_drvdata(pdev);
  3688. host = instance->host;
  3689. instance->unload = 1;
  3690. megasas_flush_cache(instance);
  3691. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3692. /* cancel the delayed work if this work still in queue */
  3693. if (instance->ev != NULL) {
  3694. struct megasas_aen_event *ev = instance->ev;
  3695. cancel_delayed_work_sync(
  3696. (struct delayed_work *)&ev->hotplug_work);
  3697. instance->ev = NULL;
  3698. }
  3699. tasklet_kill(&instance->isr_tasklet);
  3700. pci_set_drvdata(instance->pdev, instance);
  3701. instance->instancet->disable_intr(instance->reg_set);
  3702. if (instance->msix_vectors)
  3703. for (i = 0 ; i < instance->msix_vectors; i++)
  3704. free_irq(instance->msixentry[i].vector,
  3705. &instance->irq_context[i]);
  3706. else
  3707. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3708. if (instance->msix_vectors)
  3709. pci_disable_msix(instance->pdev);
  3710. pci_save_state(pdev);
  3711. pci_disable_device(pdev);
  3712. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3713. return 0;
  3714. }
  3715. /**
  3716. * megasas_resume- driver resume entry point
  3717. * @pdev: PCI device structure
  3718. */
  3719. static int
  3720. megasas_resume(struct pci_dev *pdev)
  3721. {
  3722. int rval, i, j;
  3723. struct Scsi_Host *host;
  3724. struct megasas_instance *instance;
  3725. instance = pci_get_drvdata(pdev);
  3726. host = instance->host;
  3727. pci_set_power_state(pdev, PCI_D0);
  3728. pci_enable_wake(pdev, PCI_D0, 0);
  3729. pci_restore_state(pdev);
  3730. /*
  3731. * PCI prepping: enable device set bus mastering and dma mask
  3732. */
  3733. rval = pci_enable_device_mem(pdev);
  3734. if (rval) {
  3735. printk(KERN_ERR "megasas: Enable device failed\n");
  3736. return rval;
  3737. }
  3738. pci_set_master(pdev);
  3739. if (megasas_set_dma_mask(pdev))
  3740. goto fail_set_dma_mask;
  3741. /*
  3742. * Initialize MFI Firmware
  3743. */
  3744. atomic_set(&instance->fw_outstanding, 0);
  3745. /*
  3746. * We expect the FW state to be READY
  3747. */
  3748. if (megasas_transition_to_ready(instance, 0))
  3749. goto fail_ready_state;
  3750. /* Now re-enable MSI-X */
  3751. if (instance->msix_vectors)
  3752. pci_enable_msix(instance->pdev, instance->msixentry,
  3753. instance->msix_vectors);
  3754. switch (instance->pdev->device) {
  3755. case PCI_DEVICE_ID_LSI_FUSION:
  3756. case PCI_DEVICE_ID_LSI_INVADER:
  3757. {
  3758. megasas_reset_reply_desc(instance);
  3759. if (megasas_ioc_init_fusion(instance)) {
  3760. megasas_free_cmds(instance);
  3761. megasas_free_cmds_fusion(instance);
  3762. goto fail_init_mfi;
  3763. }
  3764. if (!megasas_get_map_info(instance))
  3765. megasas_sync_map_info(instance);
  3766. }
  3767. break;
  3768. default:
  3769. *instance->producer = 0;
  3770. *instance->consumer = 0;
  3771. if (megasas_issue_init_mfi(instance))
  3772. goto fail_init_mfi;
  3773. break;
  3774. }
  3775. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3776. (unsigned long)instance);
  3777. /*
  3778. * Register IRQ
  3779. */
  3780. if (instance->msix_vectors) {
  3781. for (i = 0 ; i < instance->msix_vectors; i++) {
  3782. instance->irq_context[i].instance = instance;
  3783. instance->irq_context[i].MSIxIndex = i;
  3784. if (request_irq(instance->msixentry[i].vector,
  3785. instance->instancet->service_isr, 0,
  3786. "megasas",
  3787. &instance->irq_context[i])) {
  3788. printk(KERN_DEBUG "megasas: Failed to "
  3789. "register IRQ for vector %d.\n", i);
  3790. for (j = 0 ; j < i ; j++)
  3791. free_irq(
  3792. instance->msixentry[j].vector,
  3793. &instance->irq_context[j]);
  3794. goto fail_irq;
  3795. }
  3796. }
  3797. } else {
  3798. instance->irq_context[0].instance = instance;
  3799. instance->irq_context[0].MSIxIndex = 0;
  3800. if (request_irq(pdev->irq, instance->instancet->service_isr,
  3801. IRQF_SHARED, "megasas",
  3802. &instance->irq_context[0])) {
  3803. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3804. goto fail_irq;
  3805. }
  3806. }
  3807. instance->instancet->enable_intr(instance->reg_set);
  3808. instance->unload = 0;
  3809. /*
  3810. * Initiate AEN (Asynchronous Event Notification)
  3811. */
  3812. if (megasas_start_aen(instance))
  3813. printk(KERN_ERR "megasas: Start AEN failed\n");
  3814. return 0;
  3815. fail_irq:
  3816. fail_init_mfi:
  3817. if (instance->evt_detail)
  3818. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3819. instance->evt_detail,
  3820. instance->evt_detail_h);
  3821. if (instance->producer)
  3822. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3823. instance->producer_h);
  3824. if (instance->consumer)
  3825. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3826. instance->consumer_h);
  3827. scsi_host_put(host);
  3828. fail_set_dma_mask:
  3829. fail_ready_state:
  3830. pci_disable_device(pdev);
  3831. return -ENODEV;
  3832. }
  3833. #else
  3834. #define megasas_suspend NULL
  3835. #define megasas_resume NULL
  3836. #endif
  3837. /**
  3838. * megasas_detach_one - PCI hot"un"plug entry point
  3839. * @pdev: PCI device structure
  3840. */
  3841. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  3842. {
  3843. int i;
  3844. struct Scsi_Host *host;
  3845. struct megasas_instance *instance;
  3846. struct fusion_context *fusion;
  3847. instance = pci_get_drvdata(pdev);
  3848. instance->unload = 1;
  3849. host = instance->host;
  3850. fusion = instance->ctrl_context;
  3851. scsi_remove_host(instance->host);
  3852. megasas_flush_cache(instance);
  3853. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3854. /* cancel the delayed work if this work still in queue*/
  3855. if (instance->ev != NULL) {
  3856. struct megasas_aen_event *ev = instance->ev;
  3857. cancel_delayed_work_sync(
  3858. (struct delayed_work *)&ev->hotplug_work);
  3859. instance->ev = NULL;
  3860. }
  3861. tasklet_kill(&instance->isr_tasklet);
  3862. /*
  3863. * Take the instance off the instance array. Note that we will not
  3864. * decrement the max_index. We let this array be sparse array
  3865. */
  3866. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3867. if (megasas_mgmt_info.instance[i] == instance) {
  3868. megasas_mgmt_info.count--;
  3869. megasas_mgmt_info.instance[i] = NULL;
  3870. break;
  3871. }
  3872. }
  3873. pci_set_drvdata(instance->pdev, NULL);
  3874. instance->instancet->disable_intr(instance->reg_set);
  3875. if (instance->msix_vectors)
  3876. for (i = 0 ; i < instance->msix_vectors; i++)
  3877. free_irq(instance->msixentry[i].vector,
  3878. &instance->irq_context[i]);
  3879. else
  3880. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3881. if (instance->msix_vectors)
  3882. pci_disable_msix(instance->pdev);
  3883. switch (instance->pdev->device) {
  3884. case PCI_DEVICE_ID_LSI_FUSION:
  3885. case PCI_DEVICE_ID_LSI_INVADER:
  3886. megasas_release_fusion(instance);
  3887. for (i = 0; i < 2 ; i++)
  3888. if (fusion->ld_map[i])
  3889. dma_free_coherent(&instance->pdev->dev,
  3890. fusion->map_sz,
  3891. fusion->ld_map[i],
  3892. fusion->
  3893. ld_map_phys[i]);
  3894. kfree(instance->ctrl_context);
  3895. break;
  3896. default:
  3897. megasas_release_mfi(instance);
  3898. pci_free_consistent(pdev,
  3899. sizeof(struct megasas_evt_detail),
  3900. instance->evt_detail,
  3901. instance->evt_detail_h);
  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. scsi_host_put(host);
  3911. pci_set_drvdata(pdev, NULL);
  3912. pci_disable_device(pdev);
  3913. return;
  3914. }
  3915. /**
  3916. * megasas_shutdown - Shutdown entry point
  3917. * @device: Generic device structure
  3918. */
  3919. static void megasas_shutdown(struct pci_dev *pdev)
  3920. {
  3921. int i;
  3922. struct megasas_instance *instance = pci_get_drvdata(pdev);
  3923. instance->unload = 1;
  3924. megasas_flush_cache(instance);
  3925. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3926. instance->instancet->disable_intr(instance->reg_set);
  3927. if (instance->msix_vectors)
  3928. for (i = 0 ; i < instance->msix_vectors; i++)
  3929. free_irq(instance->msixentry[i].vector,
  3930. &instance->irq_context[i]);
  3931. else
  3932. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  3933. if (instance->msix_vectors)
  3934. pci_disable_msix(instance->pdev);
  3935. }
  3936. /**
  3937. * megasas_mgmt_open - char node "open" entry point
  3938. */
  3939. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  3940. {
  3941. /*
  3942. * Allow only those users with admin rights
  3943. */
  3944. if (!capable(CAP_SYS_ADMIN))
  3945. return -EACCES;
  3946. return 0;
  3947. }
  3948. /**
  3949. * megasas_mgmt_fasync - Async notifier registration from applications
  3950. *
  3951. * This function adds the calling process to a driver global queue. When an
  3952. * event occurs, SIGIO will be sent to all processes in this queue.
  3953. */
  3954. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  3955. {
  3956. int rc;
  3957. mutex_lock(&megasas_async_queue_mutex);
  3958. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  3959. mutex_unlock(&megasas_async_queue_mutex);
  3960. if (rc >= 0) {
  3961. /* For sanity check when we get ioctl */
  3962. filep->private_data = filep;
  3963. return 0;
  3964. }
  3965. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3966. return rc;
  3967. }
  3968. /**
  3969. * megasas_mgmt_poll - char node "poll" entry point
  3970. * */
  3971. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3972. {
  3973. unsigned int mask;
  3974. unsigned long flags;
  3975. poll_wait(file, &megasas_poll_wait, wait);
  3976. spin_lock_irqsave(&poll_aen_lock, flags);
  3977. if (megasas_poll_wait_aen)
  3978. mask = (POLLIN | POLLRDNORM);
  3979. else
  3980. mask = 0;
  3981. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3982. return mask;
  3983. }
  3984. /**
  3985. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3986. * @instance: Adapter soft state
  3987. * @argp: User's ioctl packet
  3988. */
  3989. static int
  3990. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3991. struct megasas_iocpacket __user * user_ioc,
  3992. struct megasas_iocpacket *ioc)
  3993. {
  3994. struct megasas_sge32 *kern_sge32;
  3995. struct megasas_cmd *cmd;
  3996. void *kbuff_arr[MAX_IOCTL_SGE];
  3997. dma_addr_t buf_handle = 0;
  3998. int error = 0, i;
  3999. void *sense = NULL;
  4000. dma_addr_t sense_handle;
  4001. unsigned long *sense_ptr;
  4002. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  4003. if (ioc->sge_count > MAX_IOCTL_SGE) {
  4004. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  4005. ioc->sge_count, MAX_IOCTL_SGE);
  4006. return -EINVAL;
  4007. }
  4008. cmd = megasas_get_cmd(instance);
  4009. if (!cmd) {
  4010. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  4011. return -ENOMEM;
  4012. }
  4013. /*
  4014. * User's IOCTL packet has 2 frames (maximum). Copy those two
  4015. * frames into our cmd's frames. cmd->frame's context will get
  4016. * overwritten when we copy from user's frames. So set that value
  4017. * alone separately
  4018. */
  4019. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  4020. cmd->frame->hdr.context = cmd->index;
  4021. cmd->frame->hdr.pad_0 = 0;
  4022. cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
  4023. MFI_FRAME_SENSE64);
  4024. /*
  4025. * The management interface between applications and the fw uses
  4026. * MFI frames. E.g, RAID configuration changes, LD property changes
  4027. * etc are accomplishes through different kinds of MFI frames. The
  4028. * driver needs to care only about substituting user buffers with
  4029. * kernel buffers in SGLs. The location of SGL is embedded in the
  4030. * struct iocpacket itself.
  4031. */
  4032. kern_sge32 = (struct megasas_sge32 *)
  4033. ((unsigned long)cmd->frame + ioc->sgl_off);
  4034. /*
  4035. * For each user buffer, create a mirror buffer and copy in
  4036. */
  4037. for (i = 0; i < ioc->sge_count; i++) {
  4038. if (!ioc->sgl[i].iov_len)
  4039. continue;
  4040. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  4041. ioc->sgl[i].iov_len,
  4042. &buf_handle, GFP_KERNEL);
  4043. if (!kbuff_arr[i]) {
  4044. printk(KERN_DEBUG "megasas: Failed to alloc "
  4045. "kernel SGL buffer for IOCTL \n");
  4046. error = -ENOMEM;
  4047. goto out;
  4048. }
  4049. /*
  4050. * We don't change the dma_coherent_mask, so
  4051. * pci_alloc_consistent only returns 32bit addresses
  4052. */
  4053. kern_sge32[i].phys_addr = (u32) buf_handle;
  4054. kern_sge32[i].length = ioc->sgl[i].iov_len;
  4055. /*
  4056. * We created a kernel buffer corresponding to the
  4057. * user buffer. Now copy in from the user buffer
  4058. */
  4059. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  4060. (u32) (ioc->sgl[i].iov_len))) {
  4061. error = -EFAULT;
  4062. goto out;
  4063. }
  4064. }
  4065. if (ioc->sense_len) {
  4066. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  4067. &sense_handle, GFP_KERNEL);
  4068. if (!sense) {
  4069. error = -ENOMEM;
  4070. goto out;
  4071. }
  4072. sense_ptr =
  4073. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  4074. *sense_ptr = sense_handle;
  4075. }
  4076. /*
  4077. * Set the sync_cmd flag so that the ISR knows not to complete this
  4078. * cmd to the SCSI mid-layer
  4079. */
  4080. cmd->sync_cmd = 1;
  4081. megasas_issue_blocked_cmd(instance, cmd);
  4082. cmd->sync_cmd = 0;
  4083. /*
  4084. * copy out the kernel buffers to user buffers
  4085. */
  4086. for (i = 0; i < ioc->sge_count; i++) {
  4087. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  4088. ioc->sgl[i].iov_len)) {
  4089. error = -EFAULT;
  4090. goto out;
  4091. }
  4092. }
  4093. /*
  4094. * copy out the sense
  4095. */
  4096. if (ioc->sense_len) {
  4097. /*
  4098. * sense_ptr points to the location that has the user
  4099. * sense buffer address
  4100. */
  4101. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  4102. ioc->sense_off);
  4103. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  4104. sense, ioc->sense_len)) {
  4105. printk(KERN_ERR "megasas: Failed to copy out to user "
  4106. "sense data\n");
  4107. error = -EFAULT;
  4108. goto out;
  4109. }
  4110. }
  4111. /*
  4112. * copy the status codes returned by the fw
  4113. */
  4114. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4115. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4116. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4117. error = -EFAULT;
  4118. }
  4119. out:
  4120. if (sense) {
  4121. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4122. sense, sense_handle);
  4123. }
  4124. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  4125. dma_free_coherent(&instance->pdev->dev,
  4126. kern_sge32[i].length,
  4127. kbuff_arr[i], kern_sge32[i].phys_addr);
  4128. }
  4129. megasas_return_cmd(instance, cmd);
  4130. return error;
  4131. }
  4132. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4133. {
  4134. struct megasas_iocpacket __user *user_ioc =
  4135. (struct megasas_iocpacket __user *)arg;
  4136. struct megasas_iocpacket *ioc;
  4137. struct megasas_instance *instance;
  4138. int error;
  4139. int i;
  4140. unsigned long flags;
  4141. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4142. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4143. if (!ioc)
  4144. return -ENOMEM;
  4145. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4146. error = -EFAULT;
  4147. goto out_kfree_ioc;
  4148. }
  4149. instance = megasas_lookup_instance(ioc->host_no);
  4150. if (!instance) {
  4151. error = -ENODEV;
  4152. goto out_kfree_ioc;
  4153. }
  4154. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4155. printk(KERN_ERR "Controller in crit error\n");
  4156. error = -ENODEV;
  4157. goto out_kfree_ioc;
  4158. }
  4159. if (instance->unload == 1) {
  4160. error = -ENODEV;
  4161. goto out_kfree_ioc;
  4162. }
  4163. /*
  4164. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4165. */
  4166. if (down_interruptible(&instance->ioctl_sem)) {
  4167. error = -ERESTARTSYS;
  4168. goto out_kfree_ioc;
  4169. }
  4170. for (i = 0; i < wait_time; i++) {
  4171. spin_lock_irqsave(&instance->hba_lock, flags);
  4172. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4173. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4174. break;
  4175. }
  4176. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4177. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4178. printk(KERN_NOTICE "megasas: waiting"
  4179. "for controller reset to finish\n");
  4180. }
  4181. msleep(1000);
  4182. }
  4183. spin_lock_irqsave(&instance->hba_lock, flags);
  4184. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4185. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4186. printk(KERN_ERR "megaraid_sas: timed out while"
  4187. "waiting for HBA to recover\n");
  4188. error = -ENODEV;
  4189. goto out_kfree_ioc;
  4190. }
  4191. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4192. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4193. up(&instance->ioctl_sem);
  4194. out_kfree_ioc:
  4195. kfree(ioc);
  4196. return error;
  4197. }
  4198. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4199. {
  4200. struct megasas_instance *instance;
  4201. struct megasas_aen aen;
  4202. int error;
  4203. int i;
  4204. unsigned long flags;
  4205. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4206. if (file->private_data != file) {
  4207. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4208. "called first\n");
  4209. return -EINVAL;
  4210. }
  4211. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4212. return -EFAULT;
  4213. instance = megasas_lookup_instance(aen.host_no);
  4214. if (!instance)
  4215. return -ENODEV;
  4216. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4217. return -ENODEV;
  4218. }
  4219. if (instance->unload == 1) {
  4220. return -ENODEV;
  4221. }
  4222. for (i = 0; i < wait_time; i++) {
  4223. spin_lock_irqsave(&instance->hba_lock, flags);
  4224. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4225. spin_unlock_irqrestore(&instance->hba_lock,
  4226. flags);
  4227. break;
  4228. }
  4229. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4230. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4231. printk(KERN_NOTICE "megasas: waiting for"
  4232. "controller reset to finish\n");
  4233. }
  4234. msleep(1000);
  4235. }
  4236. spin_lock_irqsave(&instance->hba_lock, flags);
  4237. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4238. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4239. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4240. "for HBA to recover.\n");
  4241. return -ENODEV;
  4242. }
  4243. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4244. mutex_lock(&instance->aen_mutex);
  4245. error = megasas_register_aen(instance, aen.seq_num,
  4246. aen.class_locale_word);
  4247. mutex_unlock(&instance->aen_mutex);
  4248. return error;
  4249. }
  4250. /**
  4251. * megasas_mgmt_ioctl - char node ioctl entry point
  4252. */
  4253. static long
  4254. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4255. {
  4256. switch (cmd) {
  4257. case MEGASAS_IOC_FIRMWARE:
  4258. return megasas_mgmt_ioctl_fw(file, arg);
  4259. case MEGASAS_IOC_GET_AEN:
  4260. return megasas_mgmt_ioctl_aen(file, arg);
  4261. }
  4262. return -ENOTTY;
  4263. }
  4264. #ifdef CONFIG_COMPAT
  4265. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4266. {
  4267. struct compat_megasas_iocpacket __user *cioc =
  4268. (struct compat_megasas_iocpacket __user *)arg;
  4269. struct megasas_iocpacket __user *ioc =
  4270. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4271. int i;
  4272. int error = 0;
  4273. compat_uptr_t ptr;
  4274. if (clear_user(ioc, sizeof(*ioc)))
  4275. return -EFAULT;
  4276. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4277. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4278. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4279. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4280. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4281. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4282. return -EFAULT;
  4283. /*
  4284. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4285. * sense_len is not null, so prepare the 64bit value under
  4286. * the same condition.
  4287. */
  4288. if (ioc->sense_len) {
  4289. void __user **sense_ioc_ptr =
  4290. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4291. compat_uptr_t *sense_cioc_ptr =
  4292. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4293. if (get_user(ptr, sense_cioc_ptr) ||
  4294. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4295. return -EFAULT;
  4296. }
  4297. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4298. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4299. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4300. copy_in_user(&ioc->sgl[i].iov_len,
  4301. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4302. return -EFAULT;
  4303. }
  4304. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4305. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4306. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4307. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4308. return -EFAULT;
  4309. }
  4310. return error;
  4311. }
  4312. static long
  4313. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4314. unsigned long arg)
  4315. {
  4316. switch (cmd) {
  4317. case MEGASAS_IOC_FIRMWARE32:
  4318. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4319. case MEGASAS_IOC_GET_AEN:
  4320. return megasas_mgmt_ioctl_aen(file, arg);
  4321. }
  4322. return -ENOTTY;
  4323. }
  4324. #endif
  4325. /*
  4326. * File operations structure for management interface
  4327. */
  4328. static const struct file_operations megasas_mgmt_fops = {
  4329. .owner = THIS_MODULE,
  4330. .open = megasas_mgmt_open,
  4331. .fasync = megasas_mgmt_fasync,
  4332. .unlocked_ioctl = megasas_mgmt_ioctl,
  4333. .poll = megasas_mgmt_poll,
  4334. #ifdef CONFIG_COMPAT
  4335. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4336. #endif
  4337. .llseek = noop_llseek,
  4338. };
  4339. /*
  4340. * PCI hotplug support registration structure
  4341. */
  4342. static struct pci_driver megasas_pci_driver = {
  4343. .name = "megaraid_sas",
  4344. .id_table = megasas_pci_table,
  4345. .probe = megasas_probe_one,
  4346. .remove = __devexit_p(megasas_detach_one),
  4347. .suspend = megasas_suspend,
  4348. .resume = megasas_resume,
  4349. .shutdown = megasas_shutdown,
  4350. };
  4351. /*
  4352. * Sysfs driver attributes
  4353. */
  4354. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4355. {
  4356. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4357. MEGASAS_VERSION);
  4358. }
  4359. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4360. static ssize_t
  4361. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4362. {
  4363. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4364. MEGASAS_RELDATE);
  4365. }
  4366. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4367. NULL);
  4368. static ssize_t
  4369. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4370. {
  4371. return sprintf(buf, "%u\n", support_poll_for_event);
  4372. }
  4373. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4374. megasas_sysfs_show_support_poll_for_event, NULL);
  4375. static ssize_t
  4376. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4377. {
  4378. return sprintf(buf, "%u\n", support_device_change);
  4379. }
  4380. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4381. megasas_sysfs_show_support_device_change, NULL);
  4382. static ssize_t
  4383. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4384. {
  4385. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4386. }
  4387. static ssize_t
  4388. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4389. {
  4390. int retval = count;
  4391. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4392. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4393. retval = -EINVAL;
  4394. }
  4395. return retval;
  4396. }
  4397. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4398. megasas_sysfs_set_dbg_lvl);
  4399. static void
  4400. megasas_aen_polling(struct work_struct *work)
  4401. {
  4402. struct megasas_aen_event *ev =
  4403. container_of(work, struct megasas_aen_event, hotplug_work);
  4404. struct megasas_instance *instance = ev->instance;
  4405. union megasas_evt_class_locale class_locale;
  4406. struct Scsi_Host *host;
  4407. struct scsi_device *sdev1;
  4408. u16 pd_index = 0;
  4409. u16 ld_index = 0;
  4410. int i, j, doscan = 0;
  4411. u32 seq_num;
  4412. int error;
  4413. if (!instance) {
  4414. printk(KERN_ERR "invalid instance!\n");
  4415. kfree(ev);
  4416. return;
  4417. }
  4418. instance->ev = NULL;
  4419. host = instance->host;
  4420. if (instance->evt_detail) {
  4421. switch (instance->evt_detail->code) {
  4422. case MR_EVT_PD_INSERTED:
  4423. if (megasas_get_pd_list(instance) == 0) {
  4424. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4425. for (j = 0;
  4426. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4427. j++) {
  4428. pd_index =
  4429. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4430. sdev1 =
  4431. scsi_device_lookup(host, i, j, 0);
  4432. if (instance->pd_list[pd_index].driveState
  4433. == MR_PD_STATE_SYSTEM) {
  4434. if (!sdev1) {
  4435. scsi_add_device(host, i, j, 0);
  4436. }
  4437. if (sdev1)
  4438. scsi_device_put(sdev1);
  4439. }
  4440. }
  4441. }
  4442. }
  4443. doscan = 0;
  4444. break;
  4445. case MR_EVT_PD_REMOVED:
  4446. if (megasas_get_pd_list(instance) == 0) {
  4447. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4448. for (j = 0;
  4449. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4450. j++) {
  4451. pd_index =
  4452. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4453. sdev1 =
  4454. scsi_device_lookup(host, i, j, 0);
  4455. if (instance->pd_list[pd_index].driveState
  4456. == MR_PD_STATE_SYSTEM) {
  4457. if (sdev1) {
  4458. scsi_device_put(sdev1);
  4459. }
  4460. } else {
  4461. if (sdev1) {
  4462. scsi_remove_device(sdev1);
  4463. scsi_device_put(sdev1);
  4464. }
  4465. }
  4466. }
  4467. }
  4468. }
  4469. doscan = 0;
  4470. break;
  4471. case MR_EVT_LD_OFFLINE:
  4472. case MR_EVT_CFG_CLEARED:
  4473. case MR_EVT_LD_DELETED:
  4474. megasas_get_ld_list(instance);
  4475. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4476. for (j = 0;
  4477. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4478. j++) {
  4479. ld_index =
  4480. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4481. sdev1 = scsi_device_lookup(host,
  4482. i + MEGASAS_MAX_LD_CHANNELS,
  4483. j,
  4484. 0);
  4485. if (instance->ld_ids[ld_index] != 0xff) {
  4486. if (sdev1) {
  4487. scsi_device_put(sdev1);
  4488. }
  4489. } else {
  4490. if (sdev1) {
  4491. scsi_remove_device(sdev1);
  4492. scsi_device_put(sdev1);
  4493. }
  4494. }
  4495. }
  4496. }
  4497. doscan = 0;
  4498. break;
  4499. case MR_EVT_LD_CREATED:
  4500. megasas_get_ld_list(instance);
  4501. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4502. for (j = 0;
  4503. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4504. j++) {
  4505. ld_index =
  4506. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4507. sdev1 = scsi_device_lookup(host,
  4508. i+MEGASAS_MAX_LD_CHANNELS,
  4509. j, 0);
  4510. if (instance->ld_ids[ld_index] !=
  4511. 0xff) {
  4512. if (!sdev1) {
  4513. scsi_add_device(host,
  4514. i + 2,
  4515. j, 0);
  4516. }
  4517. }
  4518. if (sdev1) {
  4519. scsi_device_put(sdev1);
  4520. }
  4521. }
  4522. }
  4523. doscan = 0;
  4524. break;
  4525. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4526. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4527. case MR_EVT_LD_STATE_CHANGE:
  4528. doscan = 1;
  4529. break;
  4530. default:
  4531. doscan = 0;
  4532. break;
  4533. }
  4534. } else {
  4535. printk(KERN_ERR "invalid evt_detail!\n");
  4536. kfree(ev);
  4537. return;
  4538. }
  4539. if (doscan) {
  4540. printk(KERN_INFO "scanning ...\n");
  4541. megasas_get_pd_list(instance);
  4542. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4543. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4544. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4545. sdev1 = scsi_device_lookup(host, i, j, 0);
  4546. if (instance->pd_list[pd_index].driveState ==
  4547. MR_PD_STATE_SYSTEM) {
  4548. if (!sdev1) {
  4549. scsi_add_device(host, i, j, 0);
  4550. }
  4551. if (sdev1)
  4552. scsi_device_put(sdev1);
  4553. } else {
  4554. if (sdev1) {
  4555. scsi_remove_device(sdev1);
  4556. scsi_device_put(sdev1);
  4557. }
  4558. }
  4559. }
  4560. }
  4561. megasas_get_ld_list(instance);
  4562. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4563. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4564. ld_index =
  4565. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4566. sdev1 = scsi_device_lookup(host,
  4567. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  4568. if (instance->ld_ids[ld_index] != 0xff) {
  4569. if (!sdev1) {
  4570. scsi_add_device(host,
  4571. i+2,
  4572. j, 0);
  4573. } else {
  4574. scsi_device_put(sdev1);
  4575. }
  4576. } else {
  4577. if (sdev1) {
  4578. scsi_remove_device(sdev1);
  4579. scsi_device_put(sdev1);
  4580. }
  4581. }
  4582. }
  4583. }
  4584. }
  4585. if ( instance->aen_cmd != NULL ) {
  4586. kfree(ev);
  4587. return ;
  4588. }
  4589. seq_num = instance->evt_detail->seq_num + 1;
  4590. /* Register AEN with FW for latest sequence number plus 1 */
  4591. class_locale.members.reserved = 0;
  4592. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4593. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4594. mutex_lock(&instance->aen_mutex);
  4595. error = megasas_register_aen(instance, seq_num,
  4596. class_locale.word);
  4597. mutex_unlock(&instance->aen_mutex);
  4598. if (error)
  4599. printk(KERN_ERR "register aen failed error %x\n", error);
  4600. kfree(ev);
  4601. }
  4602. /**
  4603. * megasas_init - Driver load entry point
  4604. */
  4605. static int __init megasas_init(void)
  4606. {
  4607. int rval;
  4608. /*
  4609. * Announce driver version and other information
  4610. */
  4611. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4612. MEGASAS_EXT_VERSION);
  4613. spin_lock_init(&poll_aen_lock);
  4614. support_poll_for_event = 2;
  4615. support_device_change = 1;
  4616. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4617. /*
  4618. * Register character device node
  4619. */
  4620. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4621. if (rval < 0) {
  4622. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4623. return rval;
  4624. }
  4625. megasas_mgmt_majorno = rval;
  4626. /*
  4627. * Register ourselves as PCI hotplug module
  4628. */
  4629. rval = pci_register_driver(&megasas_pci_driver);
  4630. if (rval) {
  4631. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4632. goto err_pcidrv;
  4633. }
  4634. rval = driver_create_file(&megasas_pci_driver.driver,
  4635. &driver_attr_version);
  4636. if (rval)
  4637. goto err_dcf_attr_ver;
  4638. rval = driver_create_file(&megasas_pci_driver.driver,
  4639. &driver_attr_release_date);
  4640. if (rval)
  4641. goto err_dcf_rel_date;
  4642. rval = driver_create_file(&megasas_pci_driver.driver,
  4643. &driver_attr_support_poll_for_event);
  4644. if (rval)
  4645. goto err_dcf_support_poll_for_event;
  4646. rval = driver_create_file(&megasas_pci_driver.driver,
  4647. &driver_attr_dbg_lvl);
  4648. if (rval)
  4649. goto err_dcf_dbg_lvl;
  4650. rval = driver_create_file(&megasas_pci_driver.driver,
  4651. &driver_attr_support_device_change);
  4652. if (rval)
  4653. goto err_dcf_support_device_change;
  4654. return rval;
  4655. err_dcf_support_device_change:
  4656. driver_remove_file(&megasas_pci_driver.driver,
  4657. &driver_attr_dbg_lvl);
  4658. err_dcf_dbg_lvl:
  4659. driver_remove_file(&megasas_pci_driver.driver,
  4660. &driver_attr_support_poll_for_event);
  4661. err_dcf_support_poll_for_event:
  4662. driver_remove_file(&megasas_pci_driver.driver,
  4663. &driver_attr_release_date);
  4664. err_dcf_rel_date:
  4665. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4666. err_dcf_attr_ver:
  4667. pci_unregister_driver(&megasas_pci_driver);
  4668. err_pcidrv:
  4669. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4670. return rval;
  4671. }
  4672. /**
  4673. * megasas_exit - Driver unload entry point
  4674. */
  4675. static void __exit megasas_exit(void)
  4676. {
  4677. driver_remove_file(&megasas_pci_driver.driver,
  4678. &driver_attr_dbg_lvl);
  4679. driver_remove_file(&megasas_pci_driver.driver,
  4680. &driver_attr_support_poll_for_event);
  4681. driver_remove_file(&megasas_pci_driver.driver,
  4682. &driver_attr_support_device_change);
  4683. driver_remove_file(&megasas_pci_driver.driver,
  4684. &driver_attr_release_date);
  4685. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4686. pci_unregister_driver(&megasas_pci_driver);
  4687. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4688. }
  4689. module_init(megasas_init);
  4690. module_exit(megasas_exit);