megaraid_sas.c 75 KB

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