megaraid_sas.c 79 KB

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