megaraid_sas.c 80 KB

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