megaraid_sas.c 103 KB

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