megaraid_sas_base.c 131 KB

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