megaraid_sas.c 84 KB

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