megaraid_sas.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  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-rc1
  14. *
  15. * Authors:
  16. * (email-id : megaraidlinux@lsi.com)
  17. * Sreenivas Bagalkote
  18. * Sumant Patro
  19. * Bo Yang
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/types.h>
  28. #include <linux/pci.h>
  29. #include <linux/list.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/module.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/uio.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/fs.h>
  38. #include <linux/compat.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/mutex.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_host.h>
  45. #include "megaraid_sas.h"
  46. 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. /*
  764. * Issue the command to the FW
  765. */
  766. atomic_inc(&instance->fw_outstanding);
  767. instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
  768. return 0;
  769. out_return_cmd:
  770. megasas_return_cmd(instance, cmd);
  771. out_done:
  772. done(scmd);
  773. return 0;
  774. }
  775. static int megasas_slave_configure(struct scsi_device *sdev)
  776. {
  777. /*
  778. * Don't export physical disk devices to the disk driver.
  779. *
  780. * FIXME: Currently we don't export them to the midlayer at all.
  781. * That will be fixed once LSI engineers have audited the
  782. * firmware for possible issues.
  783. */
  784. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
  785. return -ENXIO;
  786. /*
  787. * The RAID firmware may require extended timeouts.
  788. */
  789. if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
  790. sdev->timeout = 90 * HZ;
  791. return 0;
  792. }
  793. /**
  794. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  795. * @instance: Adapter soft state
  796. *
  797. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  798. * complete all its outstanding commands. Returns error if one or more IOs
  799. * are pending after this time period. It also marks the controller dead.
  800. */
  801. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  802. {
  803. int i;
  804. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  805. for (i = 0; i < wait_time; i++) {
  806. int outstanding = atomic_read(&instance->fw_outstanding);
  807. if (!outstanding)
  808. break;
  809. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  810. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  811. "commands to complete\n",i,outstanding);
  812. }
  813. msleep(1000);
  814. }
  815. if (atomic_read(&instance->fw_outstanding)) {
  816. /*
  817. * Send signal to FW to stop processing any pending cmds.
  818. * The controller will be taken offline by the OS now.
  819. */
  820. writel(MFI_STOP_ADP,
  821. &instance->reg_set->inbound_doorbell);
  822. megasas_dump_pending_frames(instance);
  823. instance->hw_crit_error = 1;
  824. return FAILED;
  825. }
  826. return SUCCESS;
  827. }
  828. /**
  829. * megasas_generic_reset - Generic reset routine
  830. * @scmd: Mid-layer SCSI command
  831. *
  832. * This routine implements a generic reset handler for device, bus and host
  833. * reset requests. Device, bus and host specific reset handlers can use this
  834. * function after they do their specific tasks.
  835. */
  836. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  837. {
  838. int ret_val;
  839. struct megasas_instance *instance;
  840. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  841. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
  842. scmd->serial_number, scmd->cmnd[0]);
  843. if (instance->hw_crit_error) {
  844. printk(KERN_ERR "megasas: cannot recover from previous reset "
  845. "failures\n");
  846. return FAILED;
  847. }
  848. ret_val = megasas_wait_for_outstanding(instance);
  849. if (ret_val == SUCCESS)
  850. printk(KERN_NOTICE "megasas: reset successful \n");
  851. else
  852. printk(KERN_ERR "megasas: failed to do reset\n");
  853. return ret_val;
  854. }
  855. /**
  856. * megasas_reset_device - Device reset handler entry point
  857. */
  858. static int megasas_reset_device(struct scsi_cmnd *scmd)
  859. {
  860. int ret;
  861. /*
  862. * First wait for all commands to complete
  863. */
  864. ret = megasas_generic_reset(scmd);
  865. return ret;
  866. }
  867. /**
  868. * megasas_reset_bus_host - Bus & host reset handler entry point
  869. */
  870. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  871. {
  872. int ret;
  873. /*
  874. * First wait for all commands to complete
  875. */
  876. ret = megasas_generic_reset(scmd);
  877. return ret;
  878. }
  879. /**
  880. * megasas_bios_param - Returns disk geometry for a disk
  881. * @sdev: device handle
  882. * @bdev: block device
  883. * @capacity: drive capacity
  884. * @geom: geometry parameters
  885. */
  886. static int
  887. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  888. sector_t capacity, int geom[])
  889. {
  890. int heads;
  891. int sectors;
  892. sector_t cylinders;
  893. unsigned long tmp;
  894. /* Default heads (64) & sectors (32) */
  895. heads = 64;
  896. sectors = 32;
  897. tmp = heads * sectors;
  898. cylinders = capacity;
  899. sector_div(cylinders, tmp);
  900. /*
  901. * Handle extended translation size for logical drives > 1Gb
  902. */
  903. if (capacity >= 0x200000) {
  904. heads = 255;
  905. sectors = 63;
  906. tmp = heads*sectors;
  907. cylinders = capacity;
  908. sector_div(cylinders, tmp);
  909. }
  910. geom[0] = heads;
  911. geom[1] = sectors;
  912. geom[2] = cylinders;
  913. return 0;
  914. }
  915. /**
  916. * megasas_service_aen - Processes an event notification
  917. * @instance: Adapter soft state
  918. * @cmd: AEN command completed by the ISR
  919. *
  920. * For AEN, driver sends a command down to FW that is held by the FW till an
  921. * event occurs. When an event of interest occurs, FW completes the command
  922. * that it was previously holding.
  923. *
  924. * This routines sends SIGIO signal to processes that have registered with the
  925. * driver for AEN.
  926. */
  927. static void
  928. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  929. {
  930. /*
  931. * Don't signal app if it is just an aborted previously registered aen
  932. */
  933. if (!cmd->abort_aen)
  934. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  935. else
  936. cmd->abort_aen = 0;
  937. instance->aen_cmd = NULL;
  938. megasas_return_cmd(instance, cmd);
  939. }
  940. /*
  941. * Scsi host template for megaraid_sas driver
  942. */
  943. static struct scsi_host_template megasas_template = {
  944. .module = THIS_MODULE,
  945. .name = "LSI Logic SAS based MegaRAID driver",
  946. .proc_name = "megaraid_sas",
  947. .slave_configure = megasas_slave_configure,
  948. .queuecommand = megasas_queue_command,
  949. .eh_device_reset_handler = megasas_reset_device,
  950. .eh_bus_reset_handler = megasas_reset_bus_host,
  951. .eh_host_reset_handler = megasas_reset_bus_host,
  952. .bios_param = megasas_bios_param,
  953. .use_clustering = ENABLE_CLUSTERING,
  954. };
  955. /**
  956. * megasas_complete_int_cmd - Completes an internal command
  957. * @instance: Adapter soft state
  958. * @cmd: Command to be completed
  959. *
  960. * The megasas_issue_blocked_cmd() function waits for a command to complete
  961. * after it issues a command. This function wakes up that waiting routine by
  962. * calling wake_up() on the wait queue.
  963. */
  964. static void
  965. megasas_complete_int_cmd(struct megasas_instance *instance,
  966. struct megasas_cmd *cmd)
  967. {
  968. cmd->cmd_status = cmd->frame->io.cmd_status;
  969. if (cmd->cmd_status == ENODATA) {
  970. cmd->cmd_status = 0;
  971. }
  972. wake_up(&instance->int_cmd_wait_q);
  973. }
  974. /**
  975. * megasas_complete_abort - Completes aborting a command
  976. * @instance: Adapter soft state
  977. * @cmd: Cmd that was issued to abort another cmd
  978. *
  979. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  980. * after it issues an abort on a previously issued command. This function
  981. * wakes up all functions waiting on the same wait queue.
  982. */
  983. static void
  984. megasas_complete_abort(struct megasas_instance *instance,
  985. struct megasas_cmd *cmd)
  986. {
  987. if (cmd->sync_cmd) {
  988. cmd->sync_cmd = 0;
  989. cmd->cmd_status = 0;
  990. wake_up(&instance->abort_cmd_wait_q);
  991. }
  992. return;
  993. }
  994. /**
  995. * megasas_unmap_sgbuf - Unmap SG buffers
  996. * @instance: Adapter soft state
  997. * @cmd: Completed command
  998. */
  999. static void
  1000. megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1001. {
  1002. dma_addr_t buf_h;
  1003. u8 opcode;
  1004. if (cmd->scmd->use_sg) {
  1005. pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
  1006. cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
  1007. return;
  1008. }
  1009. if (!cmd->scmd->request_bufflen)
  1010. return;
  1011. opcode = cmd->frame->hdr.cmd;
  1012. if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
  1013. if (IS_DMA64)
  1014. buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
  1015. else
  1016. buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
  1017. } else {
  1018. if (IS_DMA64)
  1019. buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
  1020. else
  1021. buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
  1022. }
  1023. pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
  1024. cmd->scmd->sc_data_direction);
  1025. return;
  1026. }
  1027. /**
  1028. * megasas_complete_cmd - Completes a command
  1029. * @instance: Adapter soft state
  1030. * @cmd: Command to be completed
  1031. * @alt_status: If non-zero, use this value as status to
  1032. * SCSI mid-layer instead of the value returned
  1033. * by the FW. This should be used if caller wants
  1034. * an alternate status (as in the case of aborted
  1035. * commands)
  1036. */
  1037. static void
  1038. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1039. u8 alt_status)
  1040. {
  1041. int exception = 0;
  1042. struct megasas_header *hdr = &cmd->frame->hdr;
  1043. if (cmd->scmd) {
  1044. cmd->scmd->SCp.ptr = (char *)0;
  1045. }
  1046. switch (hdr->cmd) {
  1047. case MFI_CMD_PD_SCSI_IO:
  1048. case MFI_CMD_LD_SCSI_IO:
  1049. /*
  1050. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1051. * issued either through an IO path or an IOCTL path. If it
  1052. * was via IOCTL, we will send it to internal completion.
  1053. */
  1054. if (cmd->sync_cmd) {
  1055. cmd->sync_cmd = 0;
  1056. megasas_complete_int_cmd(instance, cmd);
  1057. break;
  1058. }
  1059. case MFI_CMD_LD_READ:
  1060. case MFI_CMD_LD_WRITE:
  1061. if (alt_status) {
  1062. cmd->scmd->result = alt_status << 16;
  1063. exception = 1;
  1064. }
  1065. if (exception) {
  1066. atomic_dec(&instance->fw_outstanding);
  1067. megasas_unmap_sgbuf(instance, cmd);
  1068. cmd->scmd->scsi_done(cmd->scmd);
  1069. megasas_return_cmd(instance, cmd);
  1070. break;
  1071. }
  1072. switch (hdr->cmd_status) {
  1073. case MFI_STAT_OK:
  1074. cmd->scmd->result = DID_OK << 16;
  1075. break;
  1076. case MFI_STAT_SCSI_IO_FAILED:
  1077. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1078. cmd->scmd->result =
  1079. (DID_ERROR << 16) | hdr->scsi_status;
  1080. break;
  1081. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1082. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1083. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1084. memset(cmd->scmd->sense_buffer, 0,
  1085. SCSI_SENSE_BUFFERSIZE);
  1086. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1087. hdr->sense_len);
  1088. cmd->scmd->result |= DRIVER_SENSE << 24;
  1089. }
  1090. break;
  1091. case MFI_STAT_LD_OFFLINE:
  1092. case MFI_STAT_DEVICE_NOT_FOUND:
  1093. cmd->scmd->result = DID_BAD_TARGET << 16;
  1094. break;
  1095. default:
  1096. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1097. hdr->cmd_status);
  1098. cmd->scmd->result = DID_ERROR << 16;
  1099. break;
  1100. }
  1101. atomic_dec(&instance->fw_outstanding);
  1102. megasas_unmap_sgbuf(instance, cmd);
  1103. cmd->scmd->scsi_done(cmd->scmd);
  1104. megasas_return_cmd(instance, cmd);
  1105. break;
  1106. case MFI_CMD_SMP:
  1107. case MFI_CMD_STP:
  1108. case MFI_CMD_DCMD:
  1109. /*
  1110. * See if got an event notification
  1111. */
  1112. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1113. megasas_service_aen(instance, cmd);
  1114. else
  1115. megasas_complete_int_cmd(instance, cmd);
  1116. break;
  1117. case MFI_CMD_ABORT:
  1118. /*
  1119. * Cmd issued to abort another cmd returned
  1120. */
  1121. megasas_complete_abort(instance, cmd);
  1122. break;
  1123. default:
  1124. printk("megasas: Unknown command completed! [0x%X]\n",
  1125. hdr->cmd);
  1126. break;
  1127. }
  1128. }
  1129. /**
  1130. * megasas_deplete_reply_queue - Processes all completed commands
  1131. * @instance: Adapter soft state
  1132. * @alt_status: Alternate status to be returned to
  1133. * SCSI mid-layer instead of the status
  1134. * returned by the FW
  1135. */
  1136. static int
  1137. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  1138. {
  1139. /*
  1140. * Check if it is our interrupt
  1141. * Clear the interrupt
  1142. */
  1143. if(instance->instancet->clear_intr(instance->reg_set))
  1144. return IRQ_NONE;
  1145. if (instance->hw_crit_error)
  1146. goto out_done;
  1147. /*
  1148. * Schedule the tasklet for cmd completion
  1149. */
  1150. tasklet_schedule(&instance->isr_tasklet);
  1151. out_done:
  1152. return IRQ_HANDLED;
  1153. }
  1154. /**
  1155. * megasas_isr - isr entry point
  1156. */
  1157. static irqreturn_t megasas_isr(int irq, void *devp)
  1158. {
  1159. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  1160. DID_OK);
  1161. }
  1162. /**
  1163. * megasas_transition_to_ready - Move the FW to READY state
  1164. * @instance: Adapter soft state
  1165. *
  1166. * During the initialization, FW passes can potentially be in any one of
  1167. * several possible states. If the FW in operational, waiting-for-handshake
  1168. * states, driver must take steps to bring it to ready state. Otherwise, it
  1169. * has to wait for the ready state.
  1170. */
  1171. static int
  1172. megasas_transition_to_ready(struct megasas_instance* instance)
  1173. {
  1174. int i;
  1175. u8 max_wait;
  1176. u32 fw_state;
  1177. u32 cur_state;
  1178. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  1179. if (fw_state != MFI_STATE_READY)
  1180. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  1181. " state\n");
  1182. while (fw_state != MFI_STATE_READY) {
  1183. switch (fw_state) {
  1184. case MFI_STATE_FAULT:
  1185. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  1186. return -ENODEV;
  1187. case MFI_STATE_WAIT_HANDSHAKE:
  1188. /*
  1189. * Set the CLR bit in inbound doorbell
  1190. */
  1191. writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1192. &instance->reg_set->inbound_doorbell);
  1193. max_wait = 2;
  1194. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  1195. break;
  1196. case MFI_STATE_BOOT_MESSAGE_PENDING:
  1197. writel(MFI_INIT_HOTPLUG,
  1198. &instance->reg_set->inbound_doorbell);
  1199. max_wait = 10;
  1200. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  1201. break;
  1202. case MFI_STATE_OPERATIONAL:
  1203. /*
  1204. * Bring it to READY state; assuming max wait 10 secs
  1205. */
  1206. instance->instancet->disable_intr(instance->reg_set);
  1207. writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
  1208. max_wait = 10;
  1209. cur_state = MFI_STATE_OPERATIONAL;
  1210. break;
  1211. case MFI_STATE_UNDEFINED:
  1212. /*
  1213. * This state should not last for more than 2 seconds
  1214. */
  1215. max_wait = 2;
  1216. cur_state = MFI_STATE_UNDEFINED;
  1217. break;
  1218. case MFI_STATE_BB_INIT:
  1219. max_wait = 2;
  1220. cur_state = MFI_STATE_BB_INIT;
  1221. break;
  1222. case MFI_STATE_FW_INIT:
  1223. max_wait = 20;
  1224. cur_state = MFI_STATE_FW_INIT;
  1225. break;
  1226. case MFI_STATE_FW_INIT_2:
  1227. max_wait = 20;
  1228. cur_state = MFI_STATE_FW_INIT_2;
  1229. break;
  1230. case MFI_STATE_DEVICE_SCAN:
  1231. max_wait = 20;
  1232. cur_state = MFI_STATE_DEVICE_SCAN;
  1233. break;
  1234. case MFI_STATE_FLUSH_CACHE:
  1235. max_wait = 20;
  1236. cur_state = MFI_STATE_FLUSH_CACHE;
  1237. break;
  1238. default:
  1239. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1240. fw_state);
  1241. return -ENODEV;
  1242. }
  1243. /*
  1244. * The cur_state should not last for more than max_wait secs
  1245. */
  1246. for (i = 0; i < (max_wait * 1000); i++) {
  1247. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1248. MFI_STATE_MASK ;
  1249. if (fw_state == cur_state) {
  1250. msleep(1);
  1251. } else
  1252. break;
  1253. }
  1254. /*
  1255. * Return error if fw_state hasn't changed after max_wait
  1256. */
  1257. if (fw_state == cur_state) {
  1258. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1259. "in %d secs\n", fw_state, max_wait);
  1260. return -ENODEV;
  1261. }
  1262. };
  1263. printk(KERN_INFO "megasas: FW now in Ready state\n");
  1264. return 0;
  1265. }
  1266. /**
  1267. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1268. * @instance: Adapter soft state
  1269. */
  1270. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1271. {
  1272. int i;
  1273. u32 max_cmd = instance->max_fw_cmds;
  1274. struct megasas_cmd *cmd;
  1275. if (!instance->frame_dma_pool)
  1276. return;
  1277. /*
  1278. * Return all frames to pool
  1279. */
  1280. for (i = 0; i < max_cmd; i++) {
  1281. cmd = instance->cmd_list[i];
  1282. if (cmd->frame)
  1283. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1284. cmd->frame_phys_addr);
  1285. if (cmd->sense)
  1286. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  1287. cmd->sense_phys_addr);
  1288. }
  1289. /*
  1290. * Now destroy the pool itself
  1291. */
  1292. pci_pool_destroy(instance->frame_dma_pool);
  1293. pci_pool_destroy(instance->sense_dma_pool);
  1294. instance->frame_dma_pool = NULL;
  1295. instance->sense_dma_pool = NULL;
  1296. }
  1297. /**
  1298. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1299. * @instance: Adapter soft state
  1300. *
  1301. * Each command packet has an embedded DMA memory buffer that is used for
  1302. * filling MFI frame and the SG list that immediately follows the frame. This
  1303. * function creates those DMA memory buffers for each command packet by using
  1304. * PCI pool facility.
  1305. */
  1306. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1307. {
  1308. int i;
  1309. u32 max_cmd;
  1310. u32 sge_sz;
  1311. u32 sgl_sz;
  1312. u32 total_sz;
  1313. u32 frame_count;
  1314. struct megasas_cmd *cmd;
  1315. max_cmd = instance->max_fw_cmds;
  1316. /*
  1317. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1318. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1319. */
  1320. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1321. sizeof(struct megasas_sge32);
  1322. /*
  1323. * Calculated the number of 64byte frames required for SGL
  1324. */
  1325. sgl_sz = sge_sz * instance->max_num_sge;
  1326. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1327. /*
  1328. * We need one extra frame for the MFI command
  1329. */
  1330. frame_count++;
  1331. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1332. /*
  1333. * Use DMA pool facility provided by PCI layer
  1334. */
  1335. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1336. instance->pdev, total_sz, 64,
  1337. 0);
  1338. if (!instance->frame_dma_pool) {
  1339. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1340. return -ENOMEM;
  1341. }
  1342. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1343. instance->pdev, 128, 4, 0);
  1344. if (!instance->sense_dma_pool) {
  1345. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1346. pci_pool_destroy(instance->frame_dma_pool);
  1347. instance->frame_dma_pool = NULL;
  1348. return -ENOMEM;
  1349. }
  1350. /*
  1351. * Allocate and attach a frame to each of the commands in cmd_list.
  1352. * By making cmd->index as the context instead of the &cmd, we can
  1353. * always use 32bit context regardless of the architecture
  1354. */
  1355. for (i = 0; i < max_cmd; i++) {
  1356. cmd = instance->cmd_list[i];
  1357. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1358. GFP_KERNEL, &cmd->frame_phys_addr);
  1359. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1360. GFP_KERNEL, &cmd->sense_phys_addr);
  1361. /*
  1362. * megasas_teardown_frame_pool() takes care of freeing
  1363. * whatever has been allocated
  1364. */
  1365. if (!cmd->frame || !cmd->sense) {
  1366. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1367. megasas_teardown_frame_pool(instance);
  1368. return -ENOMEM;
  1369. }
  1370. cmd->frame->io.context = cmd->index;
  1371. }
  1372. return 0;
  1373. }
  1374. /**
  1375. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1376. * @instance: Adapter soft state
  1377. */
  1378. static void megasas_free_cmds(struct megasas_instance *instance)
  1379. {
  1380. int i;
  1381. /* First free the MFI frame pool */
  1382. megasas_teardown_frame_pool(instance);
  1383. /* Free all the commands in the cmd_list */
  1384. for (i = 0; i < instance->max_fw_cmds; i++)
  1385. kfree(instance->cmd_list[i]);
  1386. /* Free the cmd_list buffer itself */
  1387. kfree(instance->cmd_list);
  1388. instance->cmd_list = NULL;
  1389. INIT_LIST_HEAD(&instance->cmd_pool);
  1390. }
  1391. /**
  1392. * megasas_alloc_cmds - Allocates the command packets
  1393. * @instance: Adapter soft state
  1394. *
  1395. * Each command that is issued to the FW, whether IO commands from the OS or
  1396. * internal commands like IOCTLs, are wrapped in local data structure called
  1397. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1398. * the FW.
  1399. *
  1400. * Each frame has a 32-bit field called context (tag). This context is used
  1401. * to get back the megasas_cmd from the frame when a frame gets completed in
  1402. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1403. * the context. But we wanted to keep the differences between 32 and 64 bit
  1404. * systems to the mininum. We always use 32 bit integers for the context. In
  1405. * this driver, the 32 bit values are the indices into an array cmd_list.
  1406. * This array is used only to look up the megasas_cmd given the context. The
  1407. * free commands themselves are maintained in a linked list called cmd_pool.
  1408. */
  1409. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1410. {
  1411. int i;
  1412. int j;
  1413. u32 max_cmd;
  1414. struct megasas_cmd *cmd;
  1415. max_cmd = instance->max_fw_cmds;
  1416. /*
  1417. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1418. * Allocate the dynamic array first and then allocate individual
  1419. * commands.
  1420. */
  1421. instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
  1422. GFP_KERNEL);
  1423. if (!instance->cmd_list) {
  1424. printk(KERN_DEBUG "megasas: out of memory\n");
  1425. return -ENOMEM;
  1426. }
  1427. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
  1428. for (i = 0; i < max_cmd; i++) {
  1429. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1430. GFP_KERNEL);
  1431. if (!instance->cmd_list[i]) {
  1432. for (j = 0; j < i; j++)
  1433. kfree(instance->cmd_list[j]);
  1434. kfree(instance->cmd_list);
  1435. instance->cmd_list = NULL;
  1436. return -ENOMEM;
  1437. }
  1438. }
  1439. /*
  1440. * Add all the commands to command pool (instance->cmd_pool)
  1441. */
  1442. for (i = 0; i < max_cmd; i++) {
  1443. cmd = instance->cmd_list[i];
  1444. memset(cmd, 0, sizeof(struct megasas_cmd));
  1445. cmd->index = i;
  1446. cmd->instance = instance;
  1447. list_add_tail(&cmd->list, &instance->cmd_pool);
  1448. }
  1449. /*
  1450. * Create a frame pool and assign one frame to each cmd
  1451. */
  1452. if (megasas_create_frame_pool(instance)) {
  1453. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1454. megasas_free_cmds(instance);
  1455. }
  1456. return 0;
  1457. }
  1458. /**
  1459. * megasas_get_controller_info - Returns FW's controller structure
  1460. * @instance: Adapter soft state
  1461. * @ctrl_info: Controller information structure
  1462. *
  1463. * Issues an internal command (DCMD) to get the FW's controller structure.
  1464. * This information is mainly used to find out the maximum IO transfer per
  1465. * command supported by the FW.
  1466. */
  1467. static int
  1468. megasas_get_ctrl_info(struct megasas_instance *instance,
  1469. struct megasas_ctrl_info *ctrl_info)
  1470. {
  1471. int ret = 0;
  1472. struct megasas_cmd *cmd;
  1473. struct megasas_dcmd_frame *dcmd;
  1474. struct megasas_ctrl_info *ci;
  1475. dma_addr_t ci_h = 0;
  1476. cmd = megasas_get_cmd(instance);
  1477. if (!cmd) {
  1478. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  1479. return -ENOMEM;
  1480. }
  1481. dcmd = &cmd->frame->dcmd;
  1482. ci = pci_alloc_consistent(instance->pdev,
  1483. sizeof(struct megasas_ctrl_info), &ci_h);
  1484. if (!ci) {
  1485. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  1486. megasas_return_cmd(instance, cmd);
  1487. return -ENOMEM;
  1488. }
  1489. memset(ci, 0, sizeof(*ci));
  1490. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1491. dcmd->cmd = MFI_CMD_DCMD;
  1492. dcmd->cmd_status = 0xFF;
  1493. dcmd->sge_count = 1;
  1494. dcmd->flags = MFI_FRAME_DIR_READ;
  1495. dcmd->timeout = 0;
  1496. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  1497. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  1498. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1499. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  1500. if (!megasas_issue_polled(instance, cmd)) {
  1501. ret = 0;
  1502. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  1503. } else {
  1504. ret = -1;
  1505. }
  1506. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  1507. ci, ci_h);
  1508. megasas_return_cmd(instance, cmd);
  1509. return ret;
  1510. }
  1511. /**
  1512. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1513. * @instance_addr: Address of adapter soft state
  1514. *
  1515. * Tasklet to complete cmds
  1516. */
  1517. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1518. {
  1519. u32 producer;
  1520. u32 consumer;
  1521. u32 context;
  1522. struct megasas_cmd *cmd;
  1523. struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
  1524. /* If we have already declared adapter dead, donot complete cmds */
  1525. if (instance->hw_crit_error)
  1526. return;
  1527. producer = *instance->producer;
  1528. consumer = *instance->consumer;
  1529. while (consumer != producer) {
  1530. context = instance->reply_queue[consumer];
  1531. cmd = instance->cmd_list[context];
  1532. megasas_complete_cmd(instance, cmd, DID_OK);
  1533. consumer++;
  1534. if (consumer == (instance->max_fw_cmds + 1)) {
  1535. consumer = 0;
  1536. }
  1537. }
  1538. *instance->consumer = producer;
  1539. }
  1540. /**
  1541. * megasas_init_mfi - Initializes the FW
  1542. * @instance: Adapter soft state
  1543. *
  1544. * This is the main function for initializing MFI firmware.
  1545. */
  1546. static int megasas_init_mfi(struct megasas_instance *instance)
  1547. {
  1548. u32 context_sz;
  1549. u32 reply_q_sz;
  1550. u32 max_sectors_1;
  1551. u32 max_sectors_2;
  1552. struct megasas_register_set __iomem *reg_set;
  1553. struct megasas_cmd *cmd;
  1554. struct megasas_ctrl_info *ctrl_info;
  1555. struct megasas_init_frame *init_frame;
  1556. struct megasas_init_queue_info *initq_info;
  1557. dma_addr_t init_frame_h;
  1558. dma_addr_t initq_info_h;
  1559. /*
  1560. * Map the message registers
  1561. */
  1562. instance->base_addr = pci_resource_start(instance->pdev, 0);
  1563. if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
  1564. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  1565. return -EBUSY;
  1566. }
  1567. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  1568. if (!instance->reg_set) {
  1569. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  1570. goto fail_ioremap;
  1571. }
  1572. reg_set = instance->reg_set;
  1573. switch(instance->pdev->device)
  1574. {
  1575. case PCI_DEVICE_ID_LSI_SAS1078R:
  1576. instance->instancet = &megasas_instance_template_ppc;
  1577. break;
  1578. case PCI_DEVICE_ID_LSI_SAS1064R:
  1579. case PCI_DEVICE_ID_DELL_PERC5:
  1580. default:
  1581. instance->instancet = &megasas_instance_template_xscale;
  1582. break;
  1583. }
  1584. /*
  1585. * We expect the FW state to be READY
  1586. */
  1587. if (megasas_transition_to_ready(instance))
  1588. goto fail_ready_state;
  1589. /*
  1590. * Get various operational parameters from status register
  1591. */
  1592. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  1593. /*
  1594. * Reduce the max supported cmds by 1. This is to ensure that the
  1595. * reply_q_sz (1 more than the max cmd that driver may send)
  1596. * does not exceed max cmds that the FW can support
  1597. */
  1598. instance->max_fw_cmds = instance->max_fw_cmds-1;
  1599. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  1600. 0x10;
  1601. /*
  1602. * Create a pool of commands
  1603. */
  1604. if (megasas_alloc_cmds(instance))
  1605. goto fail_alloc_cmds;
  1606. /*
  1607. * Allocate memory for reply queue. Length of reply queue should
  1608. * be _one_ more than the maximum commands handled by the firmware.
  1609. *
  1610. * Note: When FW completes commands, it places corresponding contex
  1611. * values in this circular reply queue. This circular queue is a fairly
  1612. * typical producer-consumer queue. FW is the producer (of completed
  1613. * commands) and the driver is the consumer.
  1614. */
  1615. context_sz = sizeof(u32);
  1616. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  1617. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  1618. reply_q_sz,
  1619. &instance->reply_queue_h);
  1620. if (!instance->reply_queue) {
  1621. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  1622. goto fail_reply_queue;
  1623. }
  1624. /*
  1625. * Prepare a init frame. Note the init frame points to queue info
  1626. * structure. Each frame has SGL allocated after first 64 bytes. For
  1627. * this frame - since we don't need any SGL - we use SGL's space as
  1628. * queue info structure
  1629. *
  1630. * We will not get a NULL command below. We just created the pool.
  1631. */
  1632. cmd = megasas_get_cmd(instance);
  1633. init_frame = (struct megasas_init_frame *)cmd->frame;
  1634. initq_info = (struct megasas_init_queue_info *)
  1635. ((unsigned long)init_frame + 64);
  1636. init_frame_h = cmd->frame_phys_addr;
  1637. initq_info_h = init_frame_h + 64;
  1638. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  1639. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  1640. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  1641. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  1642. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  1643. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  1644. init_frame->cmd = MFI_CMD_INIT;
  1645. init_frame->cmd_status = 0xFF;
  1646. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  1647. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  1648. /*
  1649. * disable the intr before firing the init frame to FW
  1650. */
  1651. instance->instancet->disable_intr(instance->reg_set);
  1652. /*
  1653. * Issue the init frame in polled mode
  1654. */
  1655. if (megasas_issue_polled(instance, cmd)) {
  1656. printk(KERN_DEBUG "megasas: Failed to init firmware\n");
  1657. goto fail_fw_init;
  1658. }
  1659. megasas_return_cmd(instance, cmd);
  1660. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  1661. /*
  1662. * Compute the max allowed sectors per IO: The controller info has two
  1663. * limits on max sectors. Driver should use the minimum of these two.
  1664. *
  1665. * 1 << stripe_sz_ops.min = max sectors per strip
  1666. *
  1667. * Note that older firmwares ( < FW ver 30) didn't report information
  1668. * to calculate max_sectors_1. So the number ended up as zero always.
  1669. */
  1670. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  1671. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  1672. ctrl_info->max_strips_per_io;
  1673. max_sectors_2 = ctrl_info->max_request_size;
  1674. instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
  1675. ? max_sectors_1 : max_sectors_2;
  1676. } else
  1677. instance->max_sectors_per_req = instance->max_num_sge *
  1678. PAGE_SIZE / 512;
  1679. kfree(ctrl_info);
  1680. /*
  1681. * Setup tasklet for cmd completion
  1682. */
  1683. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  1684. (unsigned long)instance);
  1685. return 0;
  1686. fail_fw_init:
  1687. megasas_return_cmd(instance, cmd);
  1688. pci_free_consistent(instance->pdev, reply_q_sz,
  1689. instance->reply_queue, instance->reply_queue_h);
  1690. fail_reply_queue:
  1691. megasas_free_cmds(instance);
  1692. fail_alloc_cmds:
  1693. fail_ready_state:
  1694. iounmap(instance->reg_set);
  1695. fail_ioremap:
  1696. pci_release_regions(instance->pdev);
  1697. return -EINVAL;
  1698. }
  1699. /**
  1700. * megasas_release_mfi - Reverses the FW initialization
  1701. * @intance: Adapter soft state
  1702. */
  1703. static void megasas_release_mfi(struct megasas_instance *instance)
  1704. {
  1705. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  1706. pci_free_consistent(instance->pdev, reply_q_sz,
  1707. instance->reply_queue, instance->reply_queue_h);
  1708. megasas_free_cmds(instance);
  1709. iounmap(instance->reg_set);
  1710. pci_release_regions(instance->pdev);
  1711. }
  1712. /**
  1713. * megasas_get_seq_num - Gets latest event sequence numbers
  1714. * @instance: Adapter soft state
  1715. * @eli: FW event log sequence numbers information
  1716. *
  1717. * FW maintains a log of all events in a non-volatile area. Upper layers would
  1718. * usually find out the latest sequence number of the events, the seq number at
  1719. * the boot etc. They would "read" all the events below the latest seq number
  1720. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  1721. * number), they would subsribe to AEN (asynchronous event notification) and
  1722. * wait for the events to happen.
  1723. */
  1724. static int
  1725. megasas_get_seq_num(struct megasas_instance *instance,
  1726. struct megasas_evt_log_info *eli)
  1727. {
  1728. struct megasas_cmd *cmd;
  1729. struct megasas_dcmd_frame *dcmd;
  1730. struct megasas_evt_log_info *el_info;
  1731. dma_addr_t el_info_h = 0;
  1732. cmd = megasas_get_cmd(instance);
  1733. if (!cmd) {
  1734. return -ENOMEM;
  1735. }
  1736. dcmd = &cmd->frame->dcmd;
  1737. el_info = pci_alloc_consistent(instance->pdev,
  1738. sizeof(struct megasas_evt_log_info),
  1739. &el_info_h);
  1740. if (!el_info) {
  1741. megasas_return_cmd(instance, cmd);
  1742. return -ENOMEM;
  1743. }
  1744. memset(el_info, 0, sizeof(*el_info));
  1745. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1746. dcmd->cmd = MFI_CMD_DCMD;
  1747. dcmd->cmd_status = 0x0;
  1748. dcmd->sge_count = 1;
  1749. dcmd->flags = MFI_FRAME_DIR_READ;
  1750. dcmd->timeout = 0;
  1751. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  1752. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  1753. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  1754. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  1755. megasas_issue_blocked_cmd(instance, cmd);
  1756. /*
  1757. * Copy the data back into callers buffer
  1758. */
  1759. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  1760. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  1761. el_info, el_info_h);
  1762. megasas_return_cmd(instance, cmd);
  1763. return 0;
  1764. }
  1765. /**
  1766. * megasas_register_aen - Registers for asynchronous event notification
  1767. * @instance: Adapter soft state
  1768. * @seq_num: The starting sequence number
  1769. * @class_locale: Class of the event
  1770. *
  1771. * This function subscribes for AEN for events beyond the @seq_num. It requests
  1772. * to be notified if and only if the event is of type @class_locale
  1773. */
  1774. static int
  1775. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  1776. u32 class_locale_word)
  1777. {
  1778. int ret_val;
  1779. struct megasas_cmd *cmd;
  1780. struct megasas_dcmd_frame *dcmd;
  1781. union megasas_evt_class_locale curr_aen;
  1782. union megasas_evt_class_locale prev_aen;
  1783. /*
  1784. * If there an AEN pending already (aen_cmd), check if the
  1785. * class_locale of that pending AEN is inclusive of the new
  1786. * AEN request we currently have. If it is, then we don't have
  1787. * to do anything. In other words, whichever events the current
  1788. * AEN request is subscribing to, have already been subscribed
  1789. * to.
  1790. *
  1791. * If the old_cmd is _not_ inclusive, then we have to abort
  1792. * that command, form a class_locale that is superset of both
  1793. * old and current and re-issue to the FW
  1794. */
  1795. curr_aen.word = class_locale_word;
  1796. if (instance->aen_cmd) {
  1797. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  1798. /*
  1799. * A class whose enum value is smaller is inclusive of all
  1800. * higher values. If a PROGRESS (= -1) was previously
  1801. * registered, then a new registration requests for higher
  1802. * classes need not be sent to FW. They are automatically
  1803. * included.
  1804. *
  1805. * Locale numbers don't have such hierarchy. They are bitmap
  1806. * values
  1807. */
  1808. if ((prev_aen.members.class <= curr_aen.members.class) &&
  1809. !((prev_aen.members.locale & curr_aen.members.locale) ^
  1810. curr_aen.members.locale)) {
  1811. /*
  1812. * Previously issued event registration includes
  1813. * current request. Nothing to do.
  1814. */
  1815. return 0;
  1816. } else {
  1817. curr_aen.members.locale |= prev_aen.members.locale;
  1818. if (prev_aen.members.class < curr_aen.members.class)
  1819. curr_aen.members.class = prev_aen.members.class;
  1820. instance->aen_cmd->abort_aen = 1;
  1821. ret_val = megasas_issue_blocked_abort_cmd(instance,
  1822. instance->
  1823. aen_cmd);
  1824. if (ret_val) {
  1825. printk(KERN_DEBUG "megasas: Failed to abort "
  1826. "previous AEN command\n");
  1827. return ret_val;
  1828. }
  1829. }
  1830. }
  1831. cmd = megasas_get_cmd(instance);
  1832. if (!cmd)
  1833. return -ENOMEM;
  1834. dcmd = &cmd->frame->dcmd;
  1835. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  1836. /*
  1837. * Prepare DCMD for aen registration
  1838. */
  1839. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1840. dcmd->cmd = MFI_CMD_DCMD;
  1841. dcmd->cmd_status = 0x0;
  1842. dcmd->sge_count = 1;
  1843. dcmd->flags = MFI_FRAME_DIR_READ;
  1844. dcmd->timeout = 0;
  1845. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  1846. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  1847. dcmd->mbox.w[0] = seq_num;
  1848. dcmd->mbox.w[1] = curr_aen.word;
  1849. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  1850. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  1851. /*
  1852. * Store reference to the cmd used to register for AEN. When an
  1853. * application wants us to register for AEN, we have to abort this
  1854. * cmd and re-register with a new EVENT LOCALE supplied by that app
  1855. */
  1856. instance->aen_cmd = cmd;
  1857. /*
  1858. * Issue the aen registration frame
  1859. */
  1860. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  1861. return 0;
  1862. }
  1863. /**
  1864. * megasas_start_aen - Subscribes to AEN during driver load time
  1865. * @instance: Adapter soft state
  1866. */
  1867. static int megasas_start_aen(struct megasas_instance *instance)
  1868. {
  1869. struct megasas_evt_log_info eli;
  1870. union megasas_evt_class_locale class_locale;
  1871. /*
  1872. * Get the latest sequence number from FW
  1873. */
  1874. memset(&eli, 0, sizeof(eli));
  1875. if (megasas_get_seq_num(instance, &eli))
  1876. return -1;
  1877. /*
  1878. * Register AEN with FW for latest sequence number plus 1
  1879. */
  1880. class_locale.members.reserved = 0;
  1881. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  1882. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  1883. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  1884. class_locale.word);
  1885. }
  1886. /**
  1887. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  1888. * @instance: Adapter soft state
  1889. */
  1890. static int megasas_io_attach(struct megasas_instance *instance)
  1891. {
  1892. struct Scsi_Host *host = instance->host;
  1893. /*
  1894. * Export parameters required by SCSI mid-layer
  1895. */
  1896. host->irq = instance->pdev->irq;
  1897. host->unique_id = instance->unique_id;
  1898. host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1899. host->this_id = instance->init_id;
  1900. host->sg_tablesize = instance->max_num_sge;
  1901. host->max_sectors = instance->max_sectors_per_req;
  1902. host->cmd_per_lun = 128;
  1903. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  1904. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  1905. host->max_lun = MEGASAS_MAX_LUN;
  1906. host->max_cmd_len = 16;
  1907. /*
  1908. * Notify the mid-layer about the new controller
  1909. */
  1910. if (scsi_add_host(host, &instance->pdev->dev)) {
  1911. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  1912. return -ENODEV;
  1913. }
  1914. /*
  1915. * Trigger SCSI to scan our drives
  1916. */
  1917. scsi_scan_host(host);
  1918. return 0;
  1919. }
  1920. /**
  1921. * megasas_probe_one - PCI hotplug entry point
  1922. * @pdev: PCI device structure
  1923. * @id: PCI ids of supported hotplugged adapter
  1924. */
  1925. static int __devinit
  1926. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1927. {
  1928. int rval;
  1929. struct Scsi_Host *host;
  1930. struct megasas_instance *instance;
  1931. /*
  1932. * Announce PCI information
  1933. */
  1934. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  1935. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1936. pdev->subsystem_device);
  1937. printk("bus %d:slot %d:func %d\n",
  1938. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1939. /*
  1940. * PCI prepping: enable device set bus mastering and dma mask
  1941. */
  1942. rval = pci_enable_device(pdev);
  1943. if (rval) {
  1944. return rval;
  1945. }
  1946. pci_set_master(pdev);
  1947. /*
  1948. * All our contollers are capable of performing 64-bit DMA
  1949. */
  1950. if (IS_DMA64) {
  1951. if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
  1952. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1953. goto fail_set_dma_mask;
  1954. }
  1955. } else {
  1956. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1957. goto fail_set_dma_mask;
  1958. }
  1959. host = scsi_host_alloc(&megasas_template,
  1960. sizeof(struct megasas_instance));
  1961. if (!host) {
  1962. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  1963. goto fail_alloc_instance;
  1964. }
  1965. instance = (struct megasas_instance *)host->hostdata;
  1966. memset(instance, 0, sizeof(*instance));
  1967. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  1968. &instance->producer_h);
  1969. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  1970. &instance->consumer_h);
  1971. if (!instance->producer || !instance->consumer) {
  1972. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1973. "producer, consumer\n");
  1974. goto fail_alloc_dma_buf;
  1975. }
  1976. *instance->producer = 0;
  1977. *instance->consumer = 0;
  1978. instance->evt_detail = pci_alloc_consistent(pdev,
  1979. sizeof(struct
  1980. megasas_evt_detail),
  1981. &instance->evt_detail_h);
  1982. if (!instance->evt_detail) {
  1983. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1984. "event detail structure\n");
  1985. goto fail_alloc_dma_buf;
  1986. }
  1987. /*
  1988. * Initialize locks and queues
  1989. */
  1990. INIT_LIST_HEAD(&instance->cmd_pool);
  1991. atomic_set(&instance->fw_outstanding,0);
  1992. init_waitqueue_head(&instance->int_cmd_wait_q);
  1993. init_waitqueue_head(&instance->abort_cmd_wait_q);
  1994. spin_lock_init(&instance->cmd_pool_lock);
  1995. sema_init(&instance->aen_mutex, 1);
  1996. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  1997. /*
  1998. * Initialize PCI related and misc parameters
  1999. */
  2000. instance->pdev = pdev;
  2001. instance->host = host;
  2002. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  2003. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  2004. megasas_dbg_lvl = 0;
  2005. /*
  2006. * Initialize MFI Firmware
  2007. */
  2008. if (megasas_init_mfi(instance))
  2009. goto fail_init_mfi;
  2010. /*
  2011. * Register IRQ
  2012. */
  2013. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
  2014. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  2015. goto fail_irq;
  2016. }
  2017. instance->instancet->enable_intr(instance->reg_set);
  2018. /*
  2019. * Store instance in PCI softstate
  2020. */
  2021. pci_set_drvdata(pdev, instance);
  2022. /*
  2023. * Add this controller to megasas_mgmt_info structure so that it
  2024. * can be exported to management applications
  2025. */
  2026. megasas_mgmt_info.count++;
  2027. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  2028. megasas_mgmt_info.max_index++;
  2029. /*
  2030. * Initiate AEN (Asynchronous Event Notification)
  2031. */
  2032. if (megasas_start_aen(instance)) {
  2033. printk(KERN_DEBUG "megasas: start aen failed\n");
  2034. goto fail_start_aen;
  2035. }
  2036. /*
  2037. * Register with SCSI mid-layer
  2038. */
  2039. if (megasas_io_attach(instance))
  2040. goto fail_io_attach;
  2041. return 0;
  2042. fail_start_aen:
  2043. fail_io_attach:
  2044. megasas_mgmt_info.count--;
  2045. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  2046. megasas_mgmt_info.max_index--;
  2047. pci_set_drvdata(pdev, NULL);
  2048. instance->instancet->disable_intr(instance->reg_set);
  2049. free_irq(instance->pdev->irq, instance);
  2050. megasas_release_mfi(instance);
  2051. fail_irq:
  2052. fail_init_mfi:
  2053. fail_alloc_dma_buf:
  2054. if (instance->evt_detail)
  2055. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2056. instance->evt_detail,
  2057. instance->evt_detail_h);
  2058. if (instance->producer)
  2059. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2060. instance->producer_h);
  2061. if (instance->consumer)
  2062. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2063. instance->consumer_h);
  2064. scsi_host_put(host);
  2065. fail_alloc_instance:
  2066. fail_set_dma_mask:
  2067. pci_disable_device(pdev);
  2068. return -ENODEV;
  2069. }
  2070. /**
  2071. * megasas_flush_cache - Requests FW to flush all its caches
  2072. * @instance: Adapter soft state
  2073. */
  2074. static void megasas_flush_cache(struct megasas_instance *instance)
  2075. {
  2076. struct megasas_cmd *cmd;
  2077. struct megasas_dcmd_frame *dcmd;
  2078. cmd = megasas_get_cmd(instance);
  2079. if (!cmd)
  2080. return;
  2081. dcmd = &cmd->frame->dcmd;
  2082. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2083. dcmd->cmd = MFI_CMD_DCMD;
  2084. dcmd->cmd_status = 0x0;
  2085. dcmd->sge_count = 0;
  2086. dcmd->flags = MFI_FRAME_DIR_NONE;
  2087. dcmd->timeout = 0;
  2088. dcmd->data_xfer_len = 0;
  2089. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  2090. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  2091. megasas_issue_blocked_cmd(instance, cmd);
  2092. megasas_return_cmd(instance, cmd);
  2093. return;
  2094. }
  2095. /**
  2096. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  2097. * @instance: Adapter soft state
  2098. */
  2099. static void megasas_shutdown_controller(struct megasas_instance *instance)
  2100. {
  2101. struct megasas_cmd *cmd;
  2102. struct megasas_dcmd_frame *dcmd;
  2103. cmd = megasas_get_cmd(instance);
  2104. if (!cmd)
  2105. return;
  2106. if (instance->aen_cmd)
  2107. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  2108. dcmd = &cmd->frame->dcmd;
  2109. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2110. dcmd->cmd = MFI_CMD_DCMD;
  2111. dcmd->cmd_status = 0x0;
  2112. dcmd->sge_count = 0;
  2113. dcmd->flags = MFI_FRAME_DIR_NONE;
  2114. dcmd->timeout = 0;
  2115. dcmd->data_xfer_len = 0;
  2116. dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
  2117. megasas_issue_blocked_cmd(instance, cmd);
  2118. megasas_return_cmd(instance, cmd);
  2119. return;
  2120. }
  2121. /**
  2122. * megasas_detach_one - PCI hot"un"plug entry point
  2123. * @pdev: PCI device structure
  2124. */
  2125. static void megasas_detach_one(struct pci_dev *pdev)
  2126. {
  2127. int i;
  2128. struct Scsi_Host *host;
  2129. struct megasas_instance *instance;
  2130. instance = pci_get_drvdata(pdev);
  2131. host = instance->host;
  2132. scsi_remove_host(instance->host);
  2133. megasas_flush_cache(instance);
  2134. megasas_shutdown_controller(instance);
  2135. tasklet_kill(&instance->isr_tasklet);
  2136. /*
  2137. * Take the instance off the instance array. Note that we will not
  2138. * decrement the max_index. We let this array be sparse array
  2139. */
  2140. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2141. if (megasas_mgmt_info.instance[i] == instance) {
  2142. megasas_mgmt_info.count--;
  2143. megasas_mgmt_info.instance[i] = NULL;
  2144. break;
  2145. }
  2146. }
  2147. pci_set_drvdata(instance->pdev, NULL);
  2148. instance->instancet->disable_intr(instance->reg_set);
  2149. free_irq(instance->pdev->irq, instance);
  2150. megasas_release_mfi(instance);
  2151. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2152. instance->evt_detail, instance->evt_detail_h);
  2153. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2154. instance->producer_h);
  2155. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2156. instance->consumer_h);
  2157. scsi_host_put(host);
  2158. pci_set_drvdata(pdev, NULL);
  2159. pci_disable_device(pdev);
  2160. return;
  2161. }
  2162. /**
  2163. * megasas_shutdown - Shutdown entry point
  2164. * @device: Generic device structure
  2165. */
  2166. static void megasas_shutdown(struct pci_dev *pdev)
  2167. {
  2168. struct megasas_instance *instance = pci_get_drvdata(pdev);
  2169. megasas_flush_cache(instance);
  2170. }
  2171. /**
  2172. * megasas_mgmt_open - char node "open" entry point
  2173. */
  2174. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  2175. {
  2176. /*
  2177. * Allow only those users with admin rights
  2178. */
  2179. if (!capable(CAP_SYS_ADMIN))
  2180. return -EACCES;
  2181. return 0;
  2182. }
  2183. /**
  2184. * megasas_mgmt_release - char node "release" entry point
  2185. */
  2186. static int megasas_mgmt_release(struct inode *inode, struct file *filep)
  2187. {
  2188. filep->private_data = NULL;
  2189. fasync_helper(-1, filep, 0, &megasas_async_queue);
  2190. return 0;
  2191. }
  2192. /**
  2193. * megasas_mgmt_fasync - Async notifier registration from applications
  2194. *
  2195. * This function adds the calling process to a driver global queue. When an
  2196. * event occurs, SIGIO will be sent to all processes in this queue.
  2197. */
  2198. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  2199. {
  2200. int rc;
  2201. mutex_lock(&megasas_async_queue_mutex);
  2202. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  2203. mutex_unlock(&megasas_async_queue_mutex);
  2204. if (rc >= 0) {
  2205. /* For sanity check when we get ioctl */
  2206. filep->private_data = filep;
  2207. return 0;
  2208. }
  2209. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  2210. return rc;
  2211. }
  2212. /**
  2213. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  2214. * @instance: Adapter soft state
  2215. * @argp: User's ioctl packet
  2216. */
  2217. static int
  2218. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  2219. struct megasas_iocpacket __user * user_ioc,
  2220. struct megasas_iocpacket *ioc)
  2221. {
  2222. struct megasas_sge32 *kern_sge32;
  2223. struct megasas_cmd *cmd;
  2224. void *kbuff_arr[MAX_IOCTL_SGE];
  2225. dma_addr_t buf_handle = 0;
  2226. int error = 0, i;
  2227. void *sense = NULL;
  2228. dma_addr_t sense_handle;
  2229. u32 *sense_ptr;
  2230. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  2231. if (ioc->sge_count > MAX_IOCTL_SGE) {
  2232. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  2233. ioc->sge_count, MAX_IOCTL_SGE);
  2234. return -EINVAL;
  2235. }
  2236. cmd = megasas_get_cmd(instance);
  2237. if (!cmd) {
  2238. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  2239. return -ENOMEM;
  2240. }
  2241. /*
  2242. * User's IOCTL packet has 2 frames (maximum). Copy those two
  2243. * frames into our cmd's frames. cmd->frame's context will get
  2244. * overwritten when we copy from user's frames. So set that value
  2245. * alone separately
  2246. */
  2247. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  2248. cmd->frame->hdr.context = cmd->index;
  2249. /*
  2250. * The management interface between applications and the fw uses
  2251. * MFI frames. E.g, RAID configuration changes, LD property changes
  2252. * etc are accomplishes through different kinds of MFI frames. The
  2253. * driver needs to care only about substituting user buffers with
  2254. * kernel buffers in SGLs. The location of SGL is embedded in the
  2255. * struct iocpacket itself.
  2256. */
  2257. kern_sge32 = (struct megasas_sge32 *)
  2258. ((unsigned long)cmd->frame + ioc->sgl_off);
  2259. /*
  2260. * For each user buffer, create a mirror buffer and copy in
  2261. */
  2262. for (i = 0; i < ioc->sge_count; i++) {
  2263. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  2264. ioc->sgl[i].iov_len,
  2265. &buf_handle, GFP_KERNEL);
  2266. if (!kbuff_arr[i]) {
  2267. printk(KERN_DEBUG "megasas: Failed to alloc "
  2268. "kernel SGL buffer for IOCTL \n");
  2269. error = -ENOMEM;
  2270. goto out;
  2271. }
  2272. /*
  2273. * We don't change the dma_coherent_mask, so
  2274. * pci_alloc_consistent only returns 32bit addresses
  2275. */
  2276. kern_sge32[i].phys_addr = (u32) buf_handle;
  2277. kern_sge32[i].length = ioc->sgl[i].iov_len;
  2278. /*
  2279. * We created a kernel buffer corresponding to the
  2280. * user buffer. Now copy in from the user buffer
  2281. */
  2282. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  2283. (u32) (ioc->sgl[i].iov_len))) {
  2284. error = -EFAULT;
  2285. goto out;
  2286. }
  2287. }
  2288. if (ioc->sense_len) {
  2289. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  2290. &sense_handle, GFP_KERNEL);
  2291. if (!sense) {
  2292. error = -ENOMEM;
  2293. goto out;
  2294. }
  2295. sense_ptr =
  2296. (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
  2297. *sense_ptr = sense_handle;
  2298. }
  2299. /*
  2300. * Set the sync_cmd flag so that the ISR knows not to complete this
  2301. * cmd to the SCSI mid-layer
  2302. */
  2303. cmd->sync_cmd = 1;
  2304. megasas_issue_blocked_cmd(instance, cmd);
  2305. cmd->sync_cmd = 0;
  2306. /*
  2307. * copy out the kernel buffers to user buffers
  2308. */
  2309. for (i = 0; i < ioc->sge_count; i++) {
  2310. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  2311. ioc->sgl[i].iov_len)) {
  2312. error = -EFAULT;
  2313. goto out;
  2314. }
  2315. }
  2316. /*
  2317. * copy out the sense
  2318. */
  2319. if (ioc->sense_len) {
  2320. /*
  2321. * sense_ptr points to the location that has the user
  2322. * sense buffer address
  2323. */
  2324. sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
  2325. ioc->sense_off);
  2326. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  2327. sense, ioc->sense_len)) {
  2328. error = -EFAULT;
  2329. goto out;
  2330. }
  2331. }
  2332. /*
  2333. * copy the status codes returned by the fw
  2334. */
  2335. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  2336. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  2337. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  2338. error = -EFAULT;
  2339. }
  2340. out:
  2341. if (sense) {
  2342. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  2343. sense, sense_handle);
  2344. }
  2345. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  2346. dma_free_coherent(&instance->pdev->dev,
  2347. kern_sge32[i].length,
  2348. kbuff_arr[i], kern_sge32[i].phys_addr);
  2349. }
  2350. megasas_return_cmd(instance, cmd);
  2351. return error;
  2352. }
  2353. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  2354. {
  2355. int i;
  2356. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2357. if ((megasas_mgmt_info.instance[i]) &&
  2358. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  2359. return megasas_mgmt_info.instance[i];
  2360. }
  2361. return NULL;
  2362. }
  2363. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  2364. {
  2365. struct megasas_iocpacket __user *user_ioc =
  2366. (struct megasas_iocpacket __user *)arg;
  2367. struct megasas_iocpacket *ioc;
  2368. struct megasas_instance *instance;
  2369. int error;
  2370. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  2371. if (!ioc)
  2372. return -ENOMEM;
  2373. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  2374. error = -EFAULT;
  2375. goto out_kfree_ioc;
  2376. }
  2377. instance = megasas_lookup_instance(ioc->host_no);
  2378. if (!instance) {
  2379. error = -ENODEV;
  2380. goto out_kfree_ioc;
  2381. }
  2382. /*
  2383. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  2384. */
  2385. if (down_interruptible(&instance->ioctl_sem)) {
  2386. error = -ERESTARTSYS;
  2387. goto out_kfree_ioc;
  2388. }
  2389. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  2390. up(&instance->ioctl_sem);
  2391. out_kfree_ioc:
  2392. kfree(ioc);
  2393. return error;
  2394. }
  2395. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  2396. {
  2397. struct megasas_instance *instance;
  2398. struct megasas_aen aen;
  2399. int error;
  2400. if (file->private_data != file) {
  2401. printk(KERN_DEBUG "megasas: fasync_helper was not "
  2402. "called first\n");
  2403. return -EINVAL;
  2404. }
  2405. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  2406. return -EFAULT;
  2407. instance = megasas_lookup_instance(aen.host_no);
  2408. if (!instance)
  2409. return -ENODEV;
  2410. down(&instance->aen_mutex);
  2411. error = megasas_register_aen(instance, aen.seq_num,
  2412. aen.class_locale_word);
  2413. up(&instance->aen_mutex);
  2414. return error;
  2415. }
  2416. /**
  2417. * megasas_mgmt_ioctl - char node ioctl entry point
  2418. */
  2419. static long
  2420. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2421. {
  2422. switch (cmd) {
  2423. case MEGASAS_IOC_FIRMWARE:
  2424. return megasas_mgmt_ioctl_fw(file, arg);
  2425. case MEGASAS_IOC_GET_AEN:
  2426. return megasas_mgmt_ioctl_aen(file, arg);
  2427. }
  2428. return -ENOTTY;
  2429. }
  2430. #ifdef CONFIG_COMPAT
  2431. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  2432. {
  2433. struct compat_megasas_iocpacket __user *cioc =
  2434. (struct compat_megasas_iocpacket __user *)arg;
  2435. struct megasas_iocpacket __user *ioc =
  2436. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  2437. int i;
  2438. int error = 0;
  2439. if (clear_user(ioc, sizeof(*ioc)))
  2440. return -EFAULT;
  2441. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  2442. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  2443. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  2444. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  2445. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  2446. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  2447. return -EFAULT;
  2448. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  2449. compat_uptr_t ptr;
  2450. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  2451. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  2452. copy_in_user(&ioc->sgl[i].iov_len,
  2453. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  2454. return -EFAULT;
  2455. }
  2456. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  2457. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  2458. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  2459. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  2460. return -EFAULT;
  2461. }
  2462. return error;
  2463. }
  2464. static long
  2465. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  2466. unsigned long arg)
  2467. {
  2468. switch (cmd) {
  2469. case MEGASAS_IOC_FIRMWARE32:
  2470. return megasas_mgmt_compat_ioctl_fw(file, arg);
  2471. case MEGASAS_IOC_GET_AEN:
  2472. return megasas_mgmt_ioctl_aen(file, arg);
  2473. }
  2474. return -ENOTTY;
  2475. }
  2476. #endif
  2477. /*
  2478. * File operations structure for management interface
  2479. */
  2480. static const struct file_operations megasas_mgmt_fops = {
  2481. .owner = THIS_MODULE,
  2482. .open = megasas_mgmt_open,
  2483. .release = megasas_mgmt_release,
  2484. .fasync = megasas_mgmt_fasync,
  2485. .unlocked_ioctl = megasas_mgmt_ioctl,
  2486. #ifdef CONFIG_COMPAT
  2487. .compat_ioctl = megasas_mgmt_compat_ioctl,
  2488. #endif
  2489. };
  2490. /*
  2491. * PCI hotplug support registration structure
  2492. */
  2493. static struct pci_driver megasas_pci_driver = {
  2494. .name = "megaraid_sas",
  2495. .id_table = megasas_pci_table,
  2496. .probe = megasas_probe_one,
  2497. .remove = __devexit_p(megasas_detach_one),
  2498. .shutdown = megasas_shutdown,
  2499. };
  2500. /*
  2501. * Sysfs driver attributes
  2502. */
  2503. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  2504. {
  2505. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  2506. MEGASAS_VERSION);
  2507. }
  2508. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  2509. static ssize_t
  2510. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  2511. {
  2512. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  2513. MEGASAS_RELDATE);
  2514. }
  2515. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  2516. NULL);
  2517. static ssize_t
  2518. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  2519. {
  2520. return sprintf(buf,"%u",megasas_dbg_lvl);
  2521. }
  2522. static ssize_t
  2523. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  2524. {
  2525. int retval = count;
  2526. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  2527. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  2528. retval = -EINVAL;
  2529. }
  2530. return retval;
  2531. }
  2532. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
  2533. megasas_sysfs_set_dbg_lvl);
  2534. /**
  2535. * megasas_init - Driver load entry point
  2536. */
  2537. static int __init megasas_init(void)
  2538. {
  2539. int rval;
  2540. /*
  2541. * Announce driver version and other information
  2542. */
  2543. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  2544. MEGASAS_EXT_VERSION);
  2545. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  2546. /*
  2547. * Register character device node
  2548. */
  2549. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  2550. if (rval < 0) {
  2551. printk(KERN_DEBUG "megasas: failed to open device node\n");
  2552. return rval;
  2553. }
  2554. megasas_mgmt_majorno = rval;
  2555. /*
  2556. * Register ourselves as PCI hotplug module
  2557. */
  2558. rval = pci_register_driver(&megasas_pci_driver);
  2559. if (rval) {
  2560. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  2561. goto err_pcidrv;
  2562. }
  2563. rval = driver_create_file(&megasas_pci_driver.driver,
  2564. &driver_attr_version);
  2565. if (rval)
  2566. goto err_dcf_attr_ver;
  2567. rval = driver_create_file(&megasas_pci_driver.driver,
  2568. &driver_attr_release_date);
  2569. if (rval)
  2570. goto err_dcf_rel_date;
  2571. rval = driver_create_file(&megasas_pci_driver.driver,
  2572. &driver_attr_dbg_lvl);
  2573. if (rval)
  2574. goto err_dcf_dbg_lvl;
  2575. return rval;
  2576. err_dcf_dbg_lvl:
  2577. driver_remove_file(&megasas_pci_driver.driver,
  2578. &driver_attr_release_date);
  2579. err_dcf_rel_date:
  2580. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  2581. err_dcf_attr_ver:
  2582. pci_unregister_driver(&megasas_pci_driver);
  2583. err_pcidrv:
  2584. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2585. return rval;
  2586. }
  2587. /**
  2588. * megasas_exit - Driver unload entry point
  2589. */
  2590. static void __exit megasas_exit(void)
  2591. {
  2592. driver_remove_file(&megasas_pci_driver.driver,
  2593. &driver_attr_dbg_lvl);
  2594. driver_remove_file(&megasas_pci_driver.driver,
  2595. &driver_attr_release_date);
  2596. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  2597. pci_unregister_driver(&megasas_pci_driver);
  2598. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2599. }
  2600. module_init(megasas_init);
  2601. module_exit(megasas_exit);