megaraid_sas.c 81 KB

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