megaraid_sas_base.c 141 KB

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