megaraid_sas_base.c 139 KB

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