megaraid_sas.c 109 KB

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