megaraid_sas.c 67 KB

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