megaraid_sas.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  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.04
  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. {
  52. PCI_VENDOR_ID_LSI_LOGIC,
  53. PCI_DEVICE_ID_LSI_SAS1064R, // xscale IOP
  54. PCI_ANY_ID,
  55. PCI_ANY_ID,
  56. },
  57. {
  58. PCI_VENDOR_ID_DELL,
  59. PCI_DEVICE_ID_DELL_PERC5, // xscale IOP
  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 DEFINE_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 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  77. *instance)
  78. {
  79. unsigned long flags;
  80. struct megasas_cmd *cmd = NULL;
  81. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  82. if (!list_empty(&instance->cmd_pool)) {
  83. cmd = list_entry((&instance->cmd_pool)->next,
  84. struct megasas_cmd, list);
  85. list_del_init(&cmd->list);
  86. } else {
  87. printk(KERN_ERR "megasas: Command pool empty!\n");
  88. }
  89. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  90. return cmd;
  91. }
  92. /**
  93. * megasas_return_cmd - Return a cmd to free command pool
  94. * @instance: Adapter soft state
  95. * @cmd: Command packet to be returned to free command pool
  96. */
  97. static inline void
  98. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  99. {
  100. unsigned long flags;
  101. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  102. cmd->scmd = NULL;
  103. list_add_tail(&cmd->list, &instance->cmd_pool);
  104. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  105. }
  106. /**
  107. * The following functions are defined for xscale
  108. * (deviceid : 1064R, PERC5) controllers
  109. */
  110. /**
  111. * megasas_enable_intr_xscale - Enables interrupts
  112. * @regs: MFI register set
  113. */
  114. static inline void
  115. megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
  116. {
  117. writel(1, &(regs)->outbound_intr_mask);
  118. /* Dummy readl to force pci flush */
  119. readl(&regs->outbound_intr_mask);
  120. }
  121. /**
  122. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  123. * @regs: MFI register set
  124. */
  125. static u32
  126. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  127. {
  128. return readl(&(regs)->outbound_msg_0);
  129. }
  130. /**
  131. * megasas_clear_interrupt_xscale - Check & clear interrupt
  132. * @regs: MFI register set
  133. */
  134. static int
  135. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  136. {
  137. u32 status;
  138. /*
  139. * Check if it is our interrupt
  140. */
  141. status = readl(&regs->outbound_intr_status);
  142. if (!(status & MFI_OB_INTR_STATUS_MASK)) {
  143. return 1;
  144. }
  145. /*
  146. * Clear the interrupt by writing back the same value
  147. */
  148. writel(status, &regs->outbound_intr_status);
  149. return 0;
  150. }
  151. /**
  152. * megasas_fire_cmd_xscale - Sends command to the FW
  153. * @frame_phys_addr : Physical address of cmd
  154. * @frame_count : Number of frames for the command
  155. * @regs : MFI register set
  156. */
  157. static inline void
  158. megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
  159. {
  160. writel((frame_phys_addr >> 3)|(frame_count),
  161. &(regs)->inbound_queue_port);
  162. }
  163. static struct megasas_instance_template megasas_instance_template_xscale = {
  164. .fire_cmd = megasas_fire_cmd_xscale,
  165. .enable_intr = megasas_enable_intr_xscale,
  166. .clear_intr = megasas_clear_intr_xscale,
  167. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  168. };
  169. /**
  170. * This is the end of set of functions & definitions specific
  171. * to xscale (deviceid : 1064R, PERC5) controllers
  172. */
  173. /**
  174. * megasas_disable_intr - Disables interrupts
  175. * @regs: MFI register set
  176. */
  177. static inline void
  178. megasas_disable_intr(struct megasas_register_set __iomem * regs)
  179. {
  180. u32 mask = 0x1f;
  181. writel(mask, &regs->outbound_intr_mask);
  182. /* Dummy readl to force pci flush */
  183. readl(&regs->outbound_intr_mask);
  184. }
  185. /**
  186. * megasas_issue_polled - Issues a polling command
  187. * @instance: Adapter soft state
  188. * @cmd: Command packet to be issued
  189. *
  190. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  191. */
  192. static int
  193. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  194. {
  195. int i;
  196. u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
  197. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  198. frame_hdr->cmd_status = 0xFF;
  199. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  200. /*
  201. * Issue the frame using inbound queue port
  202. */
  203. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  204. /*
  205. * Wait for cmd_status to change
  206. */
  207. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
  208. rmb();
  209. msleep(1);
  210. }
  211. if (frame_hdr->cmd_status == 0xff)
  212. return -ETIME;
  213. return 0;
  214. }
  215. /**
  216. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  217. * @instance: Adapter soft state
  218. * @cmd: Command to be issued
  219. *
  220. * This function waits on an event for the command to be returned from ISR.
  221. * Used to issue ioctl commands.
  222. */
  223. static int
  224. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  225. struct megasas_cmd *cmd)
  226. {
  227. cmd->cmd_status = ENODATA;
  228. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  229. wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
  230. return 0;
  231. }
  232. /**
  233. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  234. * @instance: Adapter soft state
  235. * @cmd_to_abort: Previously issued cmd to be aborted
  236. *
  237. * MFI firmware can abort previously issued AEN comamnd (automatic event
  238. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  239. * cmd and blocks till it is completed.
  240. */
  241. static int
  242. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  243. struct megasas_cmd *cmd_to_abort)
  244. {
  245. struct megasas_cmd *cmd;
  246. struct megasas_abort_frame *abort_fr;
  247. cmd = megasas_get_cmd(instance);
  248. if (!cmd)
  249. return -1;
  250. abort_fr = &cmd->frame->abort;
  251. /*
  252. * Prepare and issue the abort frame
  253. */
  254. abort_fr->cmd = MFI_CMD_ABORT;
  255. abort_fr->cmd_status = 0xFF;
  256. abort_fr->flags = 0;
  257. abort_fr->abort_context = cmd_to_abort->index;
  258. abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
  259. abort_fr->abort_mfi_phys_addr_hi = 0;
  260. cmd->sync_cmd = 1;
  261. cmd->cmd_status = 0xFF;
  262. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  263. /*
  264. * Wait for this cmd to complete
  265. */
  266. wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
  267. megasas_return_cmd(instance, cmd);
  268. return 0;
  269. }
  270. /**
  271. * megasas_make_sgl32 - Prepares 32-bit SGL
  272. * @instance: Adapter soft state
  273. * @scp: SCSI command from the mid-layer
  274. * @mfi_sgl: SGL to be filled in
  275. *
  276. * If successful, this function returns the number of SG elements. Otherwise,
  277. * it returnes -1.
  278. */
  279. static int
  280. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  281. union megasas_sgl *mfi_sgl)
  282. {
  283. int i;
  284. int sge_count;
  285. struct scatterlist *os_sgl;
  286. /*
  287. * Return 0 if there is no data transfer
  288. */
  289. if (!scp->request_buffer || !scp->request_bufflen)
  290. return 0;
  291. if (!scp->use_sg) {
  292. mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
  293. scp->
  294. request_buffer,
  295. scp->
  296. request_bufflen,
  297. scp->
  298. sc_data_direction);
  299. mfi_sgl->sge32[0].length = scp->request_bufflen;
  300. return 1;
  301. }
  302. os_sgl = (struct scatterlist *)scp->request_buffer;
  303. sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
  304. scp->sc_data_direction);
  305. for (i = 0; i < sge_count; i++, os_sgl++) {
  306. mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
  307. mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
  308. }
  309. return sge_count;
  310. }
  311. /**
  312. * megasas_make_sgl64 - Prepares 64-bit SGL
  313. * @instance: Adapter soft state
  314. * @scp: SCSI command from the mid-layer
  315. * @mfi_sgl: SGL to be filled in
  316. *
  317. * If successful, this function returns the number of SG elements. Otherwise,
  318. * it returnes -1.
  319. */
  320. static int
  321. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  322. union megasas_sgl *mfi_sgl)
  323. {
  324. int i;
  325. int sge_count;
  326. struct scatterlist *os_sgl;
  327. /*
  328. * Return 0 if there is no data transfer
  329. */
  330. if (!scp->request_buffer || !scp->request_bufflen)
  331. return 0;
  332. if (!scp->use_sg) {
  333. mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
  334. scp->
  335. request_buffer,
  336. scp->
  337. request_bufflen,
  338. scp->
  339. sc_data_direction);
  340. mfi_sgl->sge64[0].length = scp->request_bufflen;
  341. return 1;
  342. }
  343. os_sgl = (struct scatterlist *)scp->request_buffer;
  344. sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
  345. scp->sc_data_direction);
  346. for (i = 0; i < sge_count; i++, os_sgl++) {
  347. mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
  348. mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
  349. }
  350. return sge_count;
  351. }
  352. /**
  353. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  354. * @instance: Adapter soft state
  355. * @scp: SCSI command
  356. * @cmd: Command to be prepared in
  357. *
  358. * This function prepares CDB commands. These are typcially pass-through
  359. * commands to the devices.
  360. */
  361. static int
  362. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  363. struct megasas_cmd *cmd)
  364. {
  365. u32 sge_sz;
  366. int sge_bytes;
  367. u32 is_logical;
  368. u32 device_id;
  369. u16 flags = 0;
  370. struct megasas_pthru_frame *pthru;
  371. is_logical = MEGASAS_IS_LOGICAL(scp);
  372. device_id = MEGASAS_DEV_INDEX(instance, scp);
  373. pthru = (struct megasas_pthru_frame *)cmd->frame;
  374. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  375. flags = MFI_FRAME_DIR_WRITE;
  376. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  377. flags = MFI_FRAME_DIR_READ;
  378. else if (scp->sc_data_direction == PCI_DMA_NONE)
  379. flags = MFI_FRAME_DIR_NONE;
  380. /*
  381. * Prepare the DCDB frame
  382. */
  383. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  384. pthru->cmd_status = 0x0;
  385. pthru->scsi_status = 0x0;
  386. pthru->target_id = device_id;
  387. pthru->lun = scp->device->lun;
  388. pthru->cdb_len = scp->cmd_len;
  389. pthru->timeout = 0;
  390. pthru->flags = flags;
  391. pthru->data_xfer_len = scp->request_bufflen;
  392. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  393. /*
  394. * Construct SGL
  395. */
  396. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  397. sizeof(struct megasas_sge32);
  398. if (IS_DMA64) {
  399. pthru->flags |= MFI_FRAME_SGL64;
  400. pthru->sge_count = megasas_make_sgl64(instance, scp,
  401. &pthru->sgl);
  402. } else
  403. pthru->sge_count = megasas_make_sgl32(instance, scp,
  404. &pthru->sgl);
  405. /*
  406. * Sense info specific
  407. */
  408. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  409. pthru->sense_buf_phys_addr_hi = 0;
  410. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  411. sge_bytes = sge_sz * pthru->sge_count;
  412. /*
  413. * Compute the total number of frames this command consumes. FW uses
  414. * this number to pull sufficient number of frames from host memory.
  415. */
  416. cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  417. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
  418. if (cmd->frame_count > 7)
  419. cmd->frame_count = 8;
  420. return cmd->frame_count;
  421. }
  422. /**
  423. * megasas_build_ldio - Prepares IOs to logical devices
  424. * @instance: Adapter soft state
  425. * @scp: SCSI command
  426. * @cmd: Command to to be prepared
  427. *
  428. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  429. */
  430. static int
  431. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  432. struct megasas_cmd *cmd)
  433. {
  434. u32 sge_sz;
  435. int sge_bytes;
  436. u32 device_id;
  437. u8 sc = scp->cmnd[0];
  438. u16 flags = 0;
  439. struct megasas_io_frame *ldio;
  440. device_id = MEGASAS_DEV_INDEX(instance, scp);
  441. ldio = (struct megasas_io_frame *)cmd->frame;
  442. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  443. flags = MFI_FRAME_DIR_WRITE;
  444. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  445. flags = MFI_FRAME_DIR_READ;
  446. /*
  447. * Preare the Logical IO frame: 2nd bit is zero for all read cmds
  448. */
  449. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  450. ldio->cmd_status = 0x0;
  451. ldio->scsi_status = 0x0;
  452. ldio->target_id = device_id;
  453. ldio->timeout = 0;
  454. ldio->reserved_0 = 0;
  455. ldio->pad_0 = 0;
  456. ldio->flags = flags;
  457. ldio->start_lba_hi = 0;
  458. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  459. /*
  460. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  461. */
  462. if (scp->cmd_len == 6) {
  463. ldio->lba_count = (u32) scp->cmnd[4];
  464. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  465. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  466. ldio->start_lba_lo &= 0x1FFFFF;
  467. }
  468. /*
  469. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  470. */
  471. else if (scp->cmd_len == 10) {
  472. ldio->lba_count = (u32) scp->cmnd[8] |
  473. ((u32) scp->cmnd[7] << 8);
  474. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  475. ((u32) scp->cmnd[3] << 16) |
  476. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  477. }
  478. /*
  479. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  480. */
  481. else if (scp->cmd_len == 12) {
  482. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  483. ((u32) scp->cmnd[7] << 16) |
  484. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  485. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  486. ((u32) scp->cmnd[3] << 16) |
  487. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  488. }
  489. /*
  490. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  491. */
  492. else if (scp->cmd_len == 16) {
  493. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  494. ((u32) scp->cmnd[11] << 16) |
  495. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  496. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  497. ((u32) scp->cmnd[7] << 16) |
  498. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  499. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  500. ((u32) scp->cmnd[3] << 16) |
  501. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  502. }
  503. /*
  504. * Construct SGL
  505. */
  506. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  507. sizeof(struct megasas_sge32);
  508. if (IS_DMA64) {
  509. ldio->flags |= MFI_FRAME_SGL64;
  510. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  511. } else
  512. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  513. /*
  514. * Sense info specific
  515. */
  516. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  517. ldio->sense_buf_phys_addr_hi = 0;
  518. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  519. sge_bytes = sge_sz * ldio->sge_count;
  520. cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  521. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
  522. if (cmd->frame_count > 7)
  523. cmd->frame_count = 8;
  524. return cmd->frame_count;
  525. }
  526. /**
  527. * megasas_is_ldio - Checks if the cmd is for logical drive
  528. * @scmd: SCSI command
  529. *
  530. * Called by megasas_queue_command to find out if the command to be queued
  531. * is a logical drive command
  532. */
  533. static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  534. {
  535. if (!MEGASAS_IS_LOGICAL(cmd))
  536. return 0;
  537. switch (cmd->cmnd[0]) {
  538. case READ_10:
  539. case WRITE_10:
  540. case READ_12:
  541. case WRITE_12:
  542. case READ_6:
  543. case WRITE_6:
  544. case READ_16:
  545. case WRITE_16:
  546. return 1;
  547. default:
  548. return 0;
  549. }
  550. }
  551. /**
  552. * megasas_queue_command - Queue entry point
  553. * @scmd: SCSI command to be queued
  554. * @done: Callback entry point
  555. */
  556. static int
  557. megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  558. {
  559. u32 frame_count;
  560. unsigned long flags;
  561. struct megasas_cmd *cmd;
  562. struct megasas_instance *instance;
  563. instance = (struct megasas_instance *)
  564. scmd->device->host->hostdata;
  565. scmd->scsi_done = done;
  566. scmd->result = 0;
  567. if (MEGASAS_IS_LOGICAL(scmd) &&
  568. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  569. scmd->result = DID_BAD_TARGET << 16;
  570. goto out_done;
  571. }
  572. cmd = megasas_get_cmd(instance);
  573. if (!cmd)
  574. return SCSI_MLQUEUE_HOST_BUSY;
  575. /*
  576. * Logical drive command
  577. */
  578. if (megasas_is_ldio(scmd))
  579. frame_count = megasas_build_ldio(instance, scmd, cmd);
  580. else
  581. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  582. if (!frame_count)
  583. goto out_return_cmd;
  584. cmd->scmd = scmd;
  585. scmd->SCp.ptr = (char *)cmd;
  586. scmd->SCp.sent_command = jiffies;
  587. /*
  588. * Issue the command to the FW
  589. */
  590. spin_lock_irqsave(&instance->instance_lock, flags);
  591. instance->fw_outstanding++;
  592. spin_unlock_irqrestore(&instance->instance_lock, flags);
  593. instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
  594. return 0;
  595. out_return_cmd:
  596. megasas_return_cmd(instance, cmd);
  597. out_done:
  598. done(scmd);
  599. return 0;
  600. }
  601. /**
  602. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  603. * @instance: Adapter soft state
  604. *
  605. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  606. * complete all its outstanding commands. Returns error if one or more IOs
  607. * are pending after this time period. It also marks the controller dead.
  608. */
  609. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  610. {
  611. int i;
  612. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  613. for (i = 0; i < wait_time; i++) {
  614. if (!instance->fw_outstanding)
  615. break;
  616. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  617. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  618. "commands to complete\n", i,
  619. instance->fw_outstanding);
  620. }
  621. msleep(1000);
  622. }
  623. if (instance->fw_outstanding) {
  624. instance->hw_crit_error = 1;
  625. return FAILED;
  626. }
  627. return SUCCESS;
  628. }
  629. /**
  630. * megasas_generic_reset - Generic reset routine
  631. * @scmd: Mid-layer SCSI command
  632. *
  633. * This routine implements a generic reset handler for device, bus and host
  634. * reset requests. Device, bus and host specific reset handlers can use this
  635. * function after they do their specific tasks.
  636. */
  637. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  638. {
  639. int ret_val;
  640. struct megasas_instance *instance;
  641. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  642. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
  643. scmd->serial_number, scmd->cmnd[0]);
  644. if (instance->hw_crit_error) {
  645. printk(KERN_ERR "megasas: cannot recover from previous reset "
  646. "failures\n");
  647. return FAILED;
  648. }
  649. ret_val = megasas_wait_for_outstanding(instance);
  650. if (ret_val == SUCCESS)
  651. printk(KERN_NOTICE "megasas: reset successful \n");
  652. else
  653. printk(KERN_ERR "megasas: failed to do reset\n");
  654. return ret_val;
  655. }
  656. static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  657. {
  658. unsigned long seconds;
  659. if (scmd->SCp.ptr) {
  660. seconds = (jiffies - scmd->SCp.sent_command) / HZ;
  661. if (seconds < 90) {
  662. return EH_RESET_TIMER;
  663. } else {
  664. return EH_NOT_HANDLED;
  665. }
  666. }
  667. return EH_HANDLED;
  668. }
  669. /**
  670. * megasas_reset_device - Device reset handler entry point
  671. */
  672. static int megasas_reset_device(struct scsi_cmnd *scmd)
  673. {
  674. int ret;
  675. /*
  676. * First wait for all commands to complete
  677. */
  678. ret = megasas_generic_reset(scmd);
  679. return ret;
  680. }
  681. /**
  682. * megasas_reset_bus_host - Bus & host reset handler entry point
  683. */
  684. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  685. {
  686. int ret;
  687. /*
  688. * Frist wait for all commands to complete
  689. */
  690. ret = megasas_generic_reset(scmd);
  691. return ret;
  692. }
  693. /**
  694. * megasas_service_aen - Processes an event notification
  695. * @instance: Adapter soft state
  696. * @cmd: AEN command completed by the ISR
  697. *
  698. * For AEN, driver sends a command down to FW that is held by the FW till an
  699. * event occurs. When an event of interest occurs, FW completes the command
  700. * that it was previously holding.
  701. *
  702. * This routines sends SIGIO signal to processes that have registered with the
  703. * driver for AEN.
  704. */
  705. static void
  706. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  707. {
  708. /*
  709. * Don't signal app if it is just an aborted previously registered aen
  710. */
  711. if (!cmd->abort_aen)
  712. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  713. else
  714. cmd->abort_aen = 0;
  715. instance->aen_cmd = NULL;
  716. megasas_return_cmd(instance, cmd);
  717. }
  718. /*
  719. * Scsi host template for megaraid_sas driver
  720. */
  721. static struct scsi_host_template megasas_template = {
  722. .module = THIS_MODULE,
  723. .name = "LSI Logic SAS based MegaRAID driver",
  724. .proc_name = "megaraid_sas",
  725. .queuecommand = megasas_queue_command,
  726. .eh_device_reset_handler = megasas_reset_device,
  727. .eh_bus_reset_handler = megasas_reset_bus_host,
  728. .eh_host_reset_handler = megasas_reset_bus_host,
  729. .eh_timed_out = megasas_reset_timer,
  730. .use_clustering = ENABLE_CLUSTERING,
  731. };
  732. /**
  733. * megasas_complete_int_cmd - Completes an internal command
  734. * @instance: Adapter soft state
  735. * @cmd: Command to be completed
  736. *
  737. * The megasas_issue_blocked_cmd() function waits for a command to complete
  738. * after it issues a command. This function wakes up that waiting routine by
  739. * calling wake_up() on the wait queue.
  740. */
  741. static void
  742. megasas_complete_int_cmd(struct megasas_instance *instance,
  743. struct megasas_cmd *cmd)
  744. {
  745. cmd->cmd_status = cmd->frame->io.cmd_status;
  746. if (cmd->cmd_status == ENODATA) {
  747. cmd->cmd_status = 0;
  748. }
  749. wake_up(&instance->int_cmd_wait_q);
  750. }
  751. /**
  752. * megasas_complete_abort - Completes aborting a command
  753. * @instance: Adapter soft state
  754. * @cmd: Cmd that was issued to abort another cmd
  755. *
  756. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  757. * after it issues an abort on a previously issued command. This function
  758. * wakes up all functions waiting on the same wait queue.
  759. */
  760. static void
  761. megasas_complete_abort(struct megasas_instance *instance,
  762. struct megasas_cmd *cmd)
  763. {
  764. if (cmd->sync_cmd) {
  765. cmd->sync_cmd = 0;
  766. cmd->cmd_status = 0;
  767. wake_up(&instance->abort_cmd_wait_q);
  768. }
  769. return;
  770. }
  771. /**
  772. * megasas_unmap_sgbuf - Unmap SG buffers
  773. * @instance: Adapter soft state
  774. * @cmd: Completed command
  775. */
  776. static void
  777. megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
  778. {
  779. dma_addr_t buf_h;
  780. u8 opcode;
  781. if (cmd->scmd->use_sg) {
  782. pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
  783. cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
  784. return;
  785. }
  786. if (!cmd->scmd->request_bufflen)
  787. return;
  788. opcode = cmd->frame->hdr.cmd;
  789. if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
  790. if (IS_DMA64)
  791. buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
  792. else
  793. buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
  794. } else {
  795. if (IS_DMA64)
  796. buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
  797. else
  798. buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
  799. }
  800. pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
  801. cmd->scmd->sc_data_direction);
  802. return;
  803. }
  804. /**
  805. * megasas_complete_cmd - Completes a command
  806. * @instance: Adapter soft state
  807. * @cmd: Command to be completed
  808. * @alt_status: If non-zero, use this value as status to
  809. * SCSI mid-layer instead of the value returned
  810. * by the FW. This should be used if caller wants
  811. * an alternate status (as in the case of aborted
  812. * commands)
  813. */
  814. static void
  815. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  816. u8 alt_status)
  817. {
  818. int exception = 0;
  819. struct megasas_header *hdr = &cmd->frame->hdr;
  820. unsigned long flags;
  821. if (cmd->scmd) {
  822. cmd->scmd->SCp.ptr = (char *)0;
  823. }
  824. switch (hdr->cmd) {
  825. case MFI_CMD_PD_SCSI_IO:
  826. case MFI_CMD_LD_SCSI_IO:
  827. /*
  828. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  829. * issued either through an IO path or an IOCTL path. If it
  830. * was via IOCTL, we will send it to internal completion.
  831. */
  832. if (cmd->sync_cmd) {
  833. cmd->sync_cmd = 0;
  834. megasas_complete_int_cmd(instance, cmd);
  835. break;
  836. }
  837. /*
  838. * Don't export physical disk devices to mid-layer.
  839. */
  840. if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
  841. (hdr->cmd_status == MFI_STAT_OK) &&
  842. (cmd->scmd->cmnd[0] == INQUIRY)) {
  843. if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
  844. TYPE_DISK) {
  845. cmd->scmd->result = DID_BAD_TARGET << 16;
  846. exception = 1;
  847. }
  848. }
  849. case MFI_CMD_LD_READ:
  850. case MFI_CMD_LD_WRITE:
  851. if (alt_status) {
  852. cmd->scmd->result = alt_status << 16;
  853. exception = 1;
  854. }
  855. if (exception) {
  856. spin_lock_irqsave(&instance->instance_lock, flags);
  857. instance->fw_outstanding--;
  858. spin_unlock_irqrestore(&instance->instance_lock, flags);
  859. megasas_unmap_sgbuf(instance, cmd);
  860. cmd->scmd->scsi_done(cmd->scmd);
  861. megasas_return_cmd(instance, cmd);
  862. break;
  863. }
  864. switch (hdr->cmd_status) {
  865. case MFI_STAT_OK:
  866. cmd->scmd->result = DID_OK << 16;
  867. break;
  868. case MFI_STAT_SCSI_IO_FAILED:
  869. case MFI_STAT_LD_INIT_IN_PROGRESS:
  870. cmd->scmd->result =
  871. (DID_ERROR << 16) | hdr->scsi_status;
  872. break;
  873. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  874. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  875. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  876. memset(cmd->scmd->sense_buffer, 0,
  877. SCSI_SENSE_BUFFERSIZE);
  878. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  879. hdr->sense_len);
  880. cmd->scmd->result |= DRIVER_SENSE << 24;
  881. }
  882. break;
  883. case MFI_STAT_LD_OFFLINE:
  884. case MFI_STAT_DEVICE_NOT_FOUND:
  885. cmd->scmd->result = DID_BAD_TARGET << 16;
  886. break;
  887. default:
  888. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  889. hdr->cmd_status);
  890. cmd->scmd->result = DID_ERROR << 16;
  891. break;
  892. }
  893. spin_lock_irqsave(&instance->instance_lock, flags);
  894. instance->fw_outstanding--;
  895. spin_unlock_irqrestore(&instance->instance_lock, flags);
  896. megasas_unmap_sgbuf(instance, cmd);
  897. cmd->scmd->scsi_done(cmd->scmd);
  898. megasas_return_cmd(instance, cmd);
  899. break;
  900. case MFI_CMD_SMP:
  901. case MFI_CMD_STP:
  902. case MFI_CMD_DCMD:
  903. /*
  904. * See if got an event notification
  905. */
  906. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  907. megasas_service_aen(instance, cmd);
  908. else
  909. megasas_complete_int_cmd(instance, cmd);
  910. break;
  911. case MFI_CMD_ABORT:
  912. /*
  913. * Cmd issued to abort another cmd returned
  914. */
  915. megasas_complete_abort(instance, cmd);
  916. break;
  917. default:
  918. printk("megasas: Unknown command completed! [0x%X]\n",
  919. hdr->cmd);
  920. break;
  921. }
  922. }
  923. /**
  924. * megasas_deplete_reply_queue - Processes all completed commands
  925. * @instance: Adapter soft state
  926. * @alt_status: Alternate status to be returned to
  927. * SCSI mid-layer instead of the status
  928. * returned by the FW
  929. */
  930. static int
  931. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  932. {
  933. u32 producer;
  934. u32 consumer;
  935. u32 context;
  936. struct megasas_cmd *cmd;
  937. /*
  938. * Check if it is our interrupt
  939. * Clear the interrupt
  940. */
  941. if(instance->instancet->clear_intr(instance->reg_set))
  942. return IRQ_NONE;
  943. producer = *instance->producer;
  944. consumer = *instance->consumer;
  945. while (consumer != producer) {
  946. context = instance->reply_queue[consumer];
  947. cmd = instance->cmd_list[context];
  948. megasas_complete_cmd(instance, cmd, alt_status);
  949. consumer++;
  950. if (consumer == (instance->max_fw_cmds + 1)) {
  951. consumer = 0;
  952. }
  953. }
  954. *instance->consumer = producer;
  955. return IRQ_HANDLED;
  956. }
  957. /**
  958. * megasas_isr - isr entry point
  959. */
  960. static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
  961. {
  962. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  963. DID_OK);
  964. }
  965. /**
  966. * megasas_transition_to_ready - Move the FW to READY state
  967. * @instance: Adapter soft state
  968. *
  969. * During the initialization, FW passes can potentially be in any one of
  970. * several possible states. If the FW in operational, waiting-for-handshake
  971. * states, driver must take steps to bring it to ready state. Otherwise, it
  972. * has to wait for the ready state.
  973. */
  974. static int
  975. megasas_transition_to_ready(struct megasas_instance* instance)
  976. {
  977. int i;
  978. u8 max_wait;
  979. u32 fw_state;
  980. u32 cur_state;
  981. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  982. while (fw_state != MFI_STATE_READY) {
  983. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  984. " state\n");
  985. switch (fw_state) {
  986. case MFI_STATE_FAULT:
  987. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  988. return -ENODEV;
  989. case MFI_STATE_WAIT_HANDSHAKE:
  990. /*
  991. * Set the CLR bit in inbound doorbell
  992. */
  993. writel(MFI_INIT_CLEAR_HANDSHAKE,
  994. &instance->reg_set->inbound_doorbell);
  995. max_wait = 2;
  996. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  997. break;
  998. case MFI_STATE_OPERATIONAL:
  999. /*
  1000. * Bring it to READY state; assuming max wait 2 secs
  1001. */
  1002. megasas_disable_intr(instance->reg_set);
  1003. writel(MFI_INIT_READY, &instance->reg_set->inbound_doorbell);
  1004. max_wait = 10;
  1005. cur_state = MFI_STATE_OPERATIONAL;
  1006. break;
  1007. case MFI_STATE_UNDEFINED:
  1008. /*
  1009. * This state should not last for more than 2 seconds
  1010. */
  1011. max_wait = 2;
  1012. cur_state = MFI_STATE_UNDEFINED;
  1013. break;
  1014. case MFI_STATE_BB_INIT:
  1015. max_wait = 2;
  1016. cur_state = MFI_STATE_BB_INIT;
  1017. break;
  1018. case MFI_STATE_FW_INIT:
  1019. max_wait = 20;
  1020. cur_state = MFI_STATE_FW_INIT;
  1021. break;
  1022. case MFI_STATE_FW_INIT_2:
  1023. max_wait = 20;
  1024. cur_state = MFI_STATE_FW_INIT_2;
  1025. break;
  1026. case MFI_STATE_DEVICE_SCAN:
  1027. max_wait = 20;
  1028. cur_state = MFI_STATE_DEVICE_SCAN;
  1029. break;
  1030. case MFI_STATE_FLUSH_CACHE:
  1031. max_wait = 20;
  1032. cur_state = MFI_STATE_FLUSH_CACHE;
  1033. break;
  1034. default:
  1035. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1036. fw_state);
  1037. return -ENODEV;
  1038. }
  1039. /*
  1040. * The cur_state should not last for more than max_wait secs
  1041. */
  1042. for (i = 0; i < (max_wait * 1000); i++) {
  1043. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1044. MFI_STATE_MASK ;
  1045. if (fw_state == cur_state) {
  1046. msleep(1);
  1047. } else
  1048. break;
  1049. }
  1050. /*
  1051. * Return error if fw_state hasn't changed after max_wait
  1052. */
  1053. if (fw_state == cur_state) {
  1054. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1055. "in %d secs\n", fw_state, max_wait);
  1056. return -ENODEV;
  1057. }
  1058. };
  1059. return 0;
  1060. }
  1061. /**
  1062. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1063. * @instance: Adapter soft state
  1064. */
  1065. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1066. {
  1067. int i;
  1068. u32 max_cmd = instance->max_fw_cmds;
  1069. struct megasas_cmd *cmd;
  1070. if (!instance->frame_dma_pool)
  1071. return;
  1072. /*
  1073. * Return all frames to pool
  1074. */
  1075. for (i = 0; i < max_cmd; i++) {
  1076. cmd = instance->cmd_list[i];
  1077. if (cmd->frame)
  1078. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1079. cmd->frame_phys_addr);
  1080. if (cmd->sense)
  1081. pci_pool_free(instance->sense_dma_pool, cmd->frame,
  1082. cmd->sense_phys_addr);
  1083. }
  1084. /*
  1085. * Now destroy the pool itself
  1086. */
  1087. pci_pool_destroy(instance->frame_dma_pool);
  1088. pci_pool_destroy(instance->sense_dma_pool);
  1089. instance->frame_dma_pool = NULL;
  1090. instance->sense_dma_pool = NULL;
  1091. }
  1092. /**
  1093. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1094. * @instance: Adapter soft state
  1095. *
  1096. * Each command packet has an embedded DMA memory buffer that is used for
  1097. * filling MFI frame and the SG list that immediately follows the frame. This
  1098. * function creates those DMA memory buffers for each command packet by using
  1099. * PCI pool facility.
  1100. */
  1101. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1102. {
  1103. int i;
  1104. u32 max_cmd;
  1105. u32 sge_sz;
  1106. u32 sgl_sz;
  1107. u32 total_sz;
  1108. u32 frame_count;
  1109. struct megasas_cmd *cmd;
  1110. max_cmd = instance->max_fw_cmds;
  1111. /*
  1112. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1113. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1114. */
  1115. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1116. sizeof(struct megasas_sge32);
  1117. /*
  1118. * Calculated the number of 64byte frames required for SGL
  1119. */
  1120. sgl_sz = sge_sz * instance->max_num_sge;
  1121. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1122. /*
  1123. * We need one extra frame for the MFI command
  1124. */
  1125. frame_count++;
  1126. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1127. /*
  1128. * Use DMA pool facility provided by PCI layer
  1129. */
  1130. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1131. instance->pdev, total_sz, 64,
  1132. 0);
  1133. if (!instance->frame_dma_pool) {
  1134. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1135. return -ENOMEM;
  1136. }
  1137. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1138. instance->pdev, 128, 4, 0);
  1139. if (!instance->sense_dma_pool) {
  1140. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1141. pci_pool_destroy(instance->frame_dma_pool);
  1142. instance->frame_dma_pool = NULL;
  1143. return -ENOMEM;
  1144. }
  1145. /*
  1146. * Allocate and attach a frame to each of the commands in cmd_list.
  1147. * By making cmd->index as the context instead of the &cmd, we can
  1148. * always use 32bit context regardless of the architecture
  1149. */
  1150. for (i = 0; i < max_cmd; i++) {
  1151. cmd = instance->cmd_list[i];
  1152. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1153. GFP_KERNEL, &cmd->frame_phys_addr);
  1154. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1155. GFP_KERNEL, &cmd->sense_phys_addr);
  1156. /*
  1157. * megasas_teardown_frame_pool() takes care of freeing
  1158. * whatever has been allocated
  1159. */
  1160. if (!cmd->frame || !cmd->sense) {
  1161. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1162. megasas_teardown_frame_pool(instance);
  1163. return -ENOMEM;
  1164. }
  1165. cmd->frame->io.context = cmd->index;
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1171. * @instance: Adapter soft state
  1172. */
  1173. static void megasas_free_cmds(struct megasas_instance *instance)
  1174. {
  1175. int i;
  1176. /* First free the MFI frame pool */
  1177. megasas_teardown_frame_pool(instance);
  1178. /* Free all the commands in the cmd_list */
  1179. for (i = 0; i < instance->max_fw_cmds; i++)
  1180. kfree(instance->cmd_list[i]);
  1181. /* Free the cmd_list buffer itself */
  1182. kfree(instance->cmd_list);
  1183. instance->cmd_list = NULL;
  1184. INIT_LIST_HEAD(&instance->cmd_pool);
  1185. }
  1186. /**
  1187. * megasas_alloc_cmds - Allocates the command packets
  1188. * @instance: Adapter soft state
  1189. *
  1190. * Each command that is issued to the FW, whether IO commands from the OS or
  1191. * internal commands like IOCTLs, are wrapped in local data structure called
  1192. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1193. * the FW.
  1194. *
  1195. * Each frame has a 32-bit field called context (tag). This context is used
  1196. * to get back the megasas_cmd from the frame when a frame gets completed in
  1197. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1198. * the context. But we wanted to keep the differences between 32 and 64 bit
  1199. * systems to the mininum. We always use 32 bit integers for the context. In
  1200. * this driver, the 32 bit values are the indices into an array cmd_list.
  1201. * This array is used only to look up the megasas_cmd given the context. The
  1202. * free commands themselves are maintained in a linked list called cmd_pool.
  1203. */
  1204. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1205. {
  1206. int i;
  1207. int j;
  1208. u32 max_cmd;
  1209. struct megasas_cmd *cmd;
  1210. max_cmd = instance->max_fw_cmds;
  1211. /*
  1212. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1213. * Allocate the dynamic array first and then allocate individual
  1214. * commands.
  1215. */
  1216. instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
  1217. GFP_KERNEL);
  1218. if (!instance->cmd_list) {
  1219. printk(KERN_DEBUG "megasas: out of memory\n");
  1220. return -ENOMEM;
  1221. }
  1222. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
  1223. for (i = 0; i < max_cmd; i++) {
  1224. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1225. GFP_KERNEL);
  1226. if (!instance->cmd_list[i]) {
  1227. for (j = 0; j < i; j++)
  1228. kfree(instance->cmd_list[j]);
  1229. kfree(instance->cmd_list);
  1230. instance->cmd_list = NULL;
  1231. return -ENOMEM;
  1232. }
  1233. }
  1234. /*
  1235. * Add all the commands to command pool (instance->cmd_pool)
  1236. */
  1237. for (i = 0; i < max_cmd; i++) {
  1238. cmd = instance->cmd_list[i];
  1239. memset(cmd, 0, sizeof(struct megasas_cmd));
  1240. cmd->index = i;
  1241. cmd->instance = instance;
  1242. list_add_tail(&cmd->list, &instance->cmd_pool);
  1243. }
  1244. /*
  1245. * Create a frame pool and assign one frame to each cmd
  1246. */
  1247. if (megasas_create_frame_pool(instance)) {
  1248. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1249. megasas_free_cmds(instance);
  1250. }
  1251. return 0;
  1252. }
  1253. /**
  1254. * megasas_get_controller_info - Returns FW's controller structure
  1255. * @instance: Adapter soft state
  1256. * @ctrl_info: Controller information structure
  1257. *
  1258. * Issues an internal command (DCMD) to get the FW's controller structure.
  1259. * This information is mainly used to find out the maximum IO transfer per
  1260. * command supported by the FW.
  1261. */
  1262. static int
  1263. megasas_get_ctrl_info(struct megasas_instance *instance,
  1264. struct megasas_ctrl_info *ctrl_info)
  1265. {
  1266. int ret = 0;
  1267. struct megasas_cmd *cmd;
  1268. struct megasas_dcmd_frame *dcmd;
  1269. struct megasas_ctrl_info *ci;
  1270. dma_addr_t ci_h = 0;
  1271. cmd = megasas_get_cmd(instance);
  1272. if (!cmd) {
  1273. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  1274. return -ENOMEM;
  1275. }
  1276. dcmd = &cmd->frame->dcmd;
  1277. ci = pci_alloc_consistent(instance->pdev,
  1278. sizeof(struct megasas_ctrl_info), &ci_h);
  1279. if (!ci) {
  1280. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  1281. megasas_return_cmd(instance, cmd);
  1282. return -ENOMEM;
  1283. }
  1284. memset(ci, 0, sizeof(*ci));
  1285. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1286. dcmd->cmd = MFI_CMD_DCMD;
  1287. dcmd->cmd_status = 0xFF;
  1288. dcmd->sge_count = 1;
  1289. dcmd->flags = MFI_FRAME_DIR_READ;
  1290. dcmd->timeout = 0;
  1291. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  1292. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  1293. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1294. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  1295. if (!megasas_issue_polled(instance, cmd)) {
  1296. ret = 0;
  1297. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  1298. } else {
  1299. ret = -1;
  1300. }
  1301. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  1302. ci, ci_h);
  1303. megasas_return_cmd(instance, cmd);
  1304. return ret;
  1305. }
  1306. /**
  1307. * megasas_init_mfi - Initializes the FW
  1308. * @instance: Adapter soft state
  1309. *
  1310. * This is the main function for initializing MFI firmware.
  1311. */
  1312. static int megasas_init_mfi(struct megasas_instance *instance)
  1313. {
  1314. u32 context_sz;
  1315. u32 reply_q_sz;
  1316. u32 max_sectors_1;
  1317. u32 max_sectors_2;
  1318. struct megasas_register_set __iomem *reg_set;
  1319. struct megasas_cmd *cmd;
  1320. struct megasas_ctrl_info *ctrl_info;
  1321. struct megasas_init_frame *init_frame;
  1322. struct megasas_init_queue_info *initq_info;
  1323. dma_addr_t init_frame_h;
  1324. dma_addr_t initq_info_h;
  1325. /*
  1326. * Map the message registers
  1327. */
  1328. instance->base_addr = pci_resource_start(instance->pdev, 0);
  1329. if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
  1330. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  1331. return -EBUSY;
  1332. }
  1333. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  1334. if (!instance->reg_set) {
  1335. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  1336. goto fail_ioremap;
  1337. }
  1338. reg_set = instance->reg_set;
  1339. instance->instancet = &megasas_instance_template_xscale;
  1340. /*
  1341. * We expect the FW state to be READY
  1342. */
  1343. if (megasas_transition_to_ready(instance))
  1344. goto fail_ready_state;
  1345. /*
  1346. * Get various operational parameters from status register
  1347. */
  1348. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  1349. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 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. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  1602. return 0;
  1603. }
  1604. /**
  1605. * megasas_start_aen - Subscribes to AEN during driver load time
  1606. * @instance: Adapter soft state
  1607. */
  1608. static int megasas_start_aen(struct megasas_instance *instance)
  1609. {
  1610. struct megasas_evt_log_info eli;
  1611. union megasas_evt_class_locale class_locale;
  1612. /*
  1613. * Get the latest sequence number from FW
  1614. */
  1615. memset(&eli, 0, sizeof(eli));
  1616. if (megasas_get_seq_num(instance, &eli))
  1617. return -1;
  1618. /*
  1619. * Register AEN with FW for latest sequence number plus 1
  1620. */
  1621. class_locale.members.reserved = 0;
  1622. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  1623. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  1624. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  1625. class_locale.word);
  1626. }
  1627. /**
  1628. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  1629. * @instance: Adapter soft state
  1630. */
  1631. static int megasas_io_attach(struct megasas_instance *instance)
  1632. {
  1633. struct Scsi_Host *host = instance->host;
  1634. /*
  1635. * Export parameters required by SCSI mid-layer
  1636. */
  1637. host->irq = instance->pdev->irq;
  1638. host->unique_id = instance->unique_id;
  1639. host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1640. host->this_id = instance->init_id;
  1641. host->sg_tablesize = instance->max_num_sge;
  1642. host->max_sectors = instance->max_sectors_per_req;
  1643. host->cmd_per_lun = 128;
  1644. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  1645. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  1646. host->max_lun = MEGASAS_MAX_LUN;
  1647. host->max_cmd_len = 16;
  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. instance->instancet->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. mutex_lock(&megasas_async_queue_mutex);
  1941. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  1942. mutex_unlock(&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_FIRMWARE32:
  2208. return megasas_mgmt_compat_ioctl_fw(file, arg);
  2209. case MEGASAS_IOC_GET_AEN:
  2210. return megasas_mgmt_ioctl_aen(file, arg);
  2211. }
  2212. return -ENOTTY;
  2213. }
  2214. #endif
  2215. /*
  2216. * File operations structure for management interface
  2217. */
  2218. static struct file_operations megasas_mgmt_fops = {
  2219. .owner = THIS_MODULE,
  2220. .open = megasas_mgmt_open,
  2221. .release = megasas_mgmt_release,
  2222. .fasync = megasas_mgmt_fasync,
  2223. .unlocked_ioctl = megasas_mgmt_ioctl,
  2224. #ifdef CONFIG_COMPAT
  2225. .compat_ioctl = megasas_mgmt_compat_ioctl,
  2226. #endif
  2227. };
  2228. /*
  2229. * PCI hotplug support registration structure
  2230. */
  2231. static struct pci_driver megasas_pci_driver = {
  2232. .name = "megaraid_sas",
  2233. .id_table = megasas_pci_table,
  2234. .probe = megasas_probe_one,
  2235. .remove = __devexit_p(megasas_detach_one),
  2236. .shutdown = megasas_shutdown,
  2237. };
  2238. /*
  2239. * Sysfs driver attributes
  2240. */
  2241. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  2242. {
  2243. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  2244. MEGASAS_VERSION);
  2245. }
  2246. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  2247. static ssize_t
  2248. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  2249. {
  2250. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  2251. MEGASAS_RELDATE);
  2252. }
  2253. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  2254. NULL);
  2255. /**
  2256. * megasas_init - Driver load entry point
  2257. */
  2258. static int __init megasas_init(void)
  2259. {
  2260. int rval;
  2261. /*
  2262. * Announce driver version and other information
  2263. */
  2264. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  2265. MEGASAS_EXT_VERSION);
  2266. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  2267. /*
  2268. * Register character device node
  2269. */
  2270. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  2271. if (rval < 0) {
  2272. printk(KERN_DEBUG "megasas: failed to open device node\n");
  2273. return rval;
  2274. }
  2275. megasas_mgmt_majorno = rval;
  2276. /*
  2277. * Register ourselves as PCI hotplug module
  2278. */
  2279. rval = pci_module_init(&megasas_pci_driver);
  2280. if (rval) {
  2281. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  2282. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2283. }
  2284. driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
  2285. driver_create_file(&megasas_pci_driver.driver,
  2286. &driver_attr_release_date);
  2287. return rval;
  2288. }
  2289. /**
  2290. * megasas_exit - Driver unload entry point
  2291. */
  2292. static void __exit megasas_exit(void)
  2293. {
  2294. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  2295. driver_remove_file(&megasas_pci_driver.driver,
  2296. &driver_attr_release_date);
  2297. pci_unregister_driver(&megasas_pci_driver);
  2298. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2299. }
  2300. module_init(megasas_init);
  2301. module_exit(megasas_exit);