megaraid_sas_base.c 132 KB

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