megaraid_sas_base.c 140 KB

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