megaraid_mm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 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_mm.c
  13. * Version : v2.20.2.6 (Mar 7 2005)
  14. *
  15. * Common management module
  16. */
  17. #include "megaraid_mm.h"
  18. // Entry points for char node driver
  19. static int mraid_mm_open(struct inode *, struct file *);
  20. static int mraid_mm_ioctl(struct inode *, struct file *, uint, unsigned long);
  21. // routines to convert to and from the old the format
  22. static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
  23. static int kioc_to_mimd(uioc_t *, mimd_t __user *);
  24. // Helper functions
  25. static int handle_drvrcmd(void __user *, uint8_t, int *);
  26. static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
  27. static void ioctl_done(uioc_t *);
  28. static void lld_timedout(unsigned long);
  29. static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
  30. static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
  31. static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
  32. static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
  33. static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
  34. static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
  35. static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
  36. static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
  37. #ifdef CONFIG_COMPAT
  38. static long mraid_mm_compat_ioctl(struct file *, unsigned int, unsigned long);
  39. #endif
  40. MODULE_AUTHOR("LSI Logic Corporation");
  41. MODULE_DESCRIPTION("LSI Logic Management Module");
  42. MODULE_LICENSE("GPL");
  43. MODULE_VERSION(LSI_COMMON_MOD_VERSION);
  44. static int dbglevel = CL_ANN;
  45. module_param_named(dlevel, dbglevel, int, 0);
  46. MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
  47. EXPORT_SYMBOL(mraid_mm_register_adp);
  48. EXPORT_SYMBOL(mraid_mm_unregister_adp);
  49. EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
  50. static int majorno;
  51. static uint32_t drvr_ver = 0x02200206;
  52. static int adapters_count_g;
  53. static struct list_head adapters_list_g;
  54. static wait_queue_head_t wait_q;
  55. static struct file_operations lsi_fops = {
  56. .open = mraid_mm_open,
  57. .ioctl = mraid_mm_ioctl,
  58. #ifdef CONFIG_COMPAT
  59. .compat_ioctl = mraid_mm_compat_ioctl,
  60. #endif
  61. .owner = THIS_MODULE,
  62. };
  63. /**
  64. * mraid_mm_open - open routine for char node interface
  65. * @inod : unused
  66. * @filep : unused
  67. *
  68. * allow ioctl operations by apps only if they superuser privilege
  69. */
  70. static int
  71. mraid_mm_open(struct inode *inode, struct file *filep)
  72. {
  73. /*
  74. * Only allow superuser to access private ioctl interface
  75. */
  76. if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
  77. return 0;
  78. }
  79. /**
  80. * mraid_mm_ioctl - module entry-point for ioctls
  81. * @inode : inode (ignored)
  82. * @filep : file operations pointer (ignored)
  83. * @cmd : ioctl command
  84. * @arg : user ioctl packet
  85. */
  86. static int
  87. mraid_mm_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  88. unsigned long arg)
  89. {
  90. uioc_t *kioc;
  91. char signature[EXT_IOCTL_SIGN_SZ] = {0};
  92. int rval;
  93. mraid_mmadp_t *adp;
  94. uint8_t old_ioctl;
  95. int drvrcmd_rval;
  96. void __user *argp = (void __user *)arg;
  97. /*
  98. * Make sure only USCSICMD are issued through this interface.
  99. * MIMD application would still fire different command.
  100. */
  101. if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
  102. return (-EINVAL);
  103. }
  104. /*
  105. * Look for signature to see if this is the new or old ioctl format.
  106. */
  107. if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
  108. con_log(CL_ANN, (KERN_WARNING
  109. "megaraid cmm: copy from usr addr failed\n"));
  110. return (-EFAULT);
  111. }
  112. if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
  113. old_ioctl = 0;
  114. else
  115. old_ioctl = 1;
  116. /*
  117. * At present, we don't support the new ioctl packet
  118. */
  119. if (!old_ioctl )
  120. return (-EINVAL);
  121. /*
  122. * If it is a driver ioctl (as opposed to fw ioctls), then we can
  123. * handle the command locally. rval > 0 means it is not a drvr cmd
  124. */
  125. rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
  126. if (rval < 0)
  127. return rval;
  128. else if (rval == 0)
  129. return drvrcmd_rval;
  130. rval = 0;
  131. if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
  132. return rval;
  133. }
  134. /*
  135. * Check if adapter can accept ioctl. We may have marked it offline
  136. * if any previous kioc had timedout on this controller.
  137. */
  138. if (!adp->quiescent) {
  139. con_log(CL_ANN, (KERN_WARNING
  140. "megaraid cmm: controller cannot accept cmds due to "
  141. "earlier errors\n" ));
  142. return -EFAULT;
  143. }
  144. /*
  145. * The following call will block till a kioc is available
  146. */
  147. kioc = mraid_mm_alloc_kioc(adp);
  148. /*
  149. * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
  150. */
  151. if ((rval = mimd_to_kioc(argp, adp, kioc))) {
  152. mraid_mm_dealloc_kioc(adp, kioc);
  153. return rval;
  154. }
  155. kioc->done = ioctl_done;
  156. /*
  157. * Issue the IOCTL to the low level driver. After the IOCTL completes
  158. * release the kioc if and only if it was _not_ timedout. If it was
  159. * timedout, that means that resources are still with low level driver.
  160. */
  161. if ((rval = lld_ioctl(adp, kioc))) {
  162. if (!kioc->timedout)
  163. mraid_mm_dealloc_kioc(adp, kioc);
  164. return rval;
  165. }
  166. /*
  167. * Convert the kioc back to user space
  168. */
  169. rval = kioc_to_mimd(kioc, argp);
  170. /*
  171. * Return the kioc to free pool
  172. */
  173. mraid_mm_dealloc_kioc(adp, kioc);
  174. return rval;
  175. }
  176. /**
  177. * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
  178. * @umimd : User space mimd_t ioctl packet
  179. * @adapter : pointer to the adapter (OUT)
  180. */
  181. static mraid_mmadp_t *
  182. mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
  183. {
  184. mraid_mmadp_t *adapter;
  185. mimd_t mimd;
  186. uint32_t adapno;
  187. int iterator;
  188. if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
  189. *rval = -EFAULT;
  190. return NULL;
  191. }
  192. adapno = GETADAP(mimd.ui.fcs.adapno);
  193. if (adapno >= adapters_count_g) {
  194. *rval = -ENODEV;
  195. return NULL;
  196. }
  197. adapter = NULL;
  198. iterator = 0;
  199. list_for_each_entry(adapter, &adapters_list_g, list) {
  200. if (iterator++ == adapno) break;
  201. }
  202. if (!adapter) {
  203. *rval = -ENODEV;
  204. return NULL;
  205. }
  206. return adapter;
  207. }
  208. /*
  209. * handle_drvrcmd - This routine checks if the opcode is a driver
  210. * cmd and if it is, handles it.
  211. * @arg : packet sent by the user app
  212. * @old_ioctl : mimd if 1; uioc otherwise
  213. */
  214. static int
  215. handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
  216. {
  217. mimd_t __user *umimd;
  218. mimd_t kmimd;
  219. uint8_t opcode;
  220. uint8_t subopcode;
  221. if (old_ioctl)
  222. goto old_packet;
  223. else
  224. goto new_packet;
  225. new_packet:
  226. return (-ENOTSUPP);
  227. old_packet:
  228. *rval = 0;
  229. umimd = arg;
  230. if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
  231. return (-EFAULT);
  232. opcode = kmimd.ui.fcs.opcode;
  233. subopcode = kmimd.ui.fcs.subopcode;
  234. /*
  235. * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
  236. * GET_NUMADP, then we can handle. Otherwise we should return 1 to
  237. * indicate that we cannot handle this.
  238. */
  239. if (opcode != 0x82)
  240. return 1;
  241. switch (subopcode) {
  242. case MEGAIOC_QDRVRVER:
  243. if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
  244. return (-EFAULT);
  245. return 0;
  246. case MEGAIOC_QNADAP:
  247. *rval = adapters_count_g;
  248. if (copy_to_user(kmimd.data, &adapters_count_g,
  249. sizeof(uint32_t)))
  250. return (-EFAULT);
  251. return 0;
  252. default:
  253. /* cannot handle */
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * mimd_to_kioc - Converter from old to new ioctl format
  260. *
  261. * @umimd : user space old MIMD IOCTL
  262. * @kioc : kernel space new format IOCTL
  263. *
  264. * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
  265. * new packet is in kernel space so that driver can perform operations on it
  266. * freely.
  267. */
  268. static int
  269. mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
  270. {
  271. mbox64_t *mbox64;
  272. mbox_t *mbox;
  273. mraid_passthru_t *pthru32;
  274. uint32_t adapno;
  275. uint8_t opcode;
  276. uint8_t subopcode;
  277. mimd_t mimd;
  278. if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
  279. return (-EFAULT);
  280. /*
  281. * Applications are not allowed to send extd pthru
  282. */
  283. if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
  284. (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
  285. return (-EINVAL);
  286. opcode = mimd.ui.fcs.opcode;
  287. subopcode = mimd.ui.fcs.subopcode;
  288. adapno = GETADAP(mimd.ui.fcs.adapno);
  289. if (adapno >= adapters_count_g)
  290. return (-ENODEV);
  291. kioc->adapno = adapno;
  292. kioc->mb_type = MBOX_LEGACY;
  293. kioc->app_type = APPTYPE_MIMD;
  294. switch (opcode) {
  295. case 0x82:
  296. if (subopcode == MEGAIOC_QADAPINFO) {
  297. kioc->opcode = GET_ADAP_INFO;
  298. kioc->data_dir = UIOC_RD;
  299. kioc->xferlen = sizeof(mraid_hba_info_t);
  300. if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
  301. return (-ENOMEM);
  302. }
  303. else {
  304. con_log(CL_ANN, (KERN_WARNING
  305. "megaraid cmm: Invalid subop\n"));
  306. return (-EINVAL);
  307. }
  308. break;
  309. case 0x81:
  310. kioc->opcode = MBOX_CMD;
  311. kioc->xferlen = mimd.ui.fcs.length;
  312. kioc->user_data_len = kioc->xferlen;
  313. kioc->user_data = mimd.ui.fcs.buffer;
  314. if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
  315. return (-ENOMEM);
  316. if (mimd.outlen) kioc->data_dir = UIOC_RD;
  317. if (mimd.inlen) kioc->data_dir |= UIOC_WR;
  318. break;
  319. case 0x80:
  320. kioc->opcode = MBOX_CMD;
  321. kioc->xferlen = (mimd.outlen > mimd.inlen) ?
  322. mimd.outlen : mimd.inlen;
  323. kioc->user_data_len = kioc->xferlen;
  324. kioc->user_data = mimd.data;
  325. if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
  326. return (-ENOMEM);
  327. if (mimd.outlen) kioc->data_dir = UIOC_RD;
  328. if (mimd.inlen) kioc->data_dir |= UIOC_WR;
  329. break;
  330. default:
  331. return (-EINVAL);
  332. }
  333. /*
  334. * If driver command, nothing else to do
  335. */
  336. if (opcode == 0x82)
  337. return 0;
  338. /*
  339. * This is a mailbox cmd; copy the mailbox from mimd
  340. */
  341. mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
  342. mbox = &mbox64->mbox32;
  343. memcpy(mbox, mimd.mbox, 14);
  344. if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
  345. mbox->xferaddr = (uint32_t)kioc->buf_paddr;
  346. if (kioc->data_dir & UIOC_WR) {
  347. if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
  348. kioc->xferlen)) {
  349. return (-EFAULT);
  350. }
  351. }
  352. return 0;
  353. }
  354. /*
  355. * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
  356. * Just like in above case, the beginning for memblk is treated as
  357. * a mailbox. The passthru will begin at next 1K boundary. And the
  358. * data will start 1K after that.
  359. */
  360. pthru32 = kioc->pthru32;
  361. kioc->user_pthru = &umimd->pthru;
  362. mbox->xferaddr = (uint32_t)kioc->pthru32_h;
  363. if (copy_from_user(pthru32, kioc->user_pthru,
  364. sizeof(mraid_passthru_t))) {
  365. return (-EFAULT);
  366. }
  367. pthru32->dataxferaddr = kioc->buf_paddr;
  368. if (kioc->data_dir & UIOC_WR) {
  369. if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
  370. pthru32->dataxferlen)) {
  371. return (-EFAULT);
  372. }
  373. }
  374. return 0;
  375. }
  376. /**
  377. * mraid_mm_attch_buf - Attach a free dma buffer for required size
  378. *
  379. * @adp : Adapter softstate
  380. * @kioc : kioc that the buffer needs to be attached to
  381. * @xferlen : required length for buffer
  382. *
  383. * First we search for a pool with smallest buffer that is >= @xferlen. If
  384. * that pool has no free buffer, we will try for the next bigger size. If none
  385. * is available, we will try to allocate the smallest buffer that is >=
  386. * @xferlen and attach it the pool.
  387. */
  388. static int
  389. mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
  390. {
  391. mm_dmapool_t *pool;
  392. int right_pool = -1;
  393. unsigned long flags;
  394. int i;
  395. kioc->pool_index = -1;
  396. kioc->buf_vaddr = NULL;
  397. kioc->buf_paddr = 0;
  398. kioc->free_buf = 0;
  399. /*
  400. * We need xferlen amount of memory. See if we can get it from our
  401. * dma pools. If we don't get exact size, we will try bigger buffer
  402. */
  403. for (i = 0; i < MAX_DMA_POOLS; i++) {
  404. pool = &adp->dma_pool_list[i];
  405. if (xferlen > pool->buf_size)
  406. continue;
  407. if (right_pool == -1)
  408. right_pool = i;
  409. spin_lock_irqsave(&pool->lock, flags);
  410. if (!pool->in_use) {
  411. pool->in_use = 1;
  412. kioc->pool_index = i;
  413. kioc->buf_vaddr = pool->vaddr;
  414. kioc->buf_paddr = pool->paddr;
  415. spin_unlock_irqrestore(&pool->lock, flags);
  416. return 0;
  417. }
  418. else {
  419. spin_unlock_irqrestore(&pool->lock, flags);
  420. continue;
  421. }
  422. }
  423. /*
  424. * If xferlen doesn't match any of our pools, return error
  425. */
  426. if (right_pool == -1)
  427. return -EINVAL;
  428. /*
  429. * We did not get any buffer from the preallocated pool. Let us try
  430. * to allocate one new buffer. NOTE: This is a blocking call.
  431. */
  432. pool = &adp->dma_pool_list[right_pool];
  433. spin_lock_irqsave(&pool->lock, flags);
  434. kioc->pool_index = right_pool;
  435. kioc->free_buf = 1;
  436. kioc->buf_vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
  437. &kioc->buf_paddr);
  438. spin_unlock_irqrestore(&pool->lock, flags);
  439. if (!kioc->buf_vaddr)
  440. return -ENOMEM;
  441. return 0;
  442. }
  443. /**
  444. * mraid_mm_alloc_kioc - Returns a uioc_t from free list
  445. * @adp : Adapter softstate for this module
  446. *
  447. * The kioc_semaphore is initialized with number of kioc nodes in the
  448. * free kioc pool. If the kioc pool is empty, this function blocks till
  449. * a kioc becomes free.
  450. */
  451. static uioc_t *
  452. mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
  453. {
  454. uioc_t *kioc;
  455. struct list_head* head;
  456. unsigned long flags;
  457. down(&adp->kioc_semaphore);
  458. spin_lock_irqsave(&adp->kioc_pool_lock, flags);
  459. head = &adp->kioc_pool;
  460. if (list_empty(head)) {
  461. up(&adp->kioc_semaphore);
  462. spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
  463. con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
  464. return NULL;
  465. }
  466. kioc = list_entry(head->next, uioc_t, list);
  467. list_del_init(&kioc->list);
  468. spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
  469. memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
  470. memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
  471. kioc->buf_vaddr = NULL;
  472. kioc->buf_paddr = 0;
  473. kioc->pool_index =-1;
  474. kioc->free_buf = 0;
  475. kioc->user_data = NULL;
  476. kioc->user_data_len = 0;
  477. kioc->user_pthru = NULL;
  478. kioc->timedout = 0;
  479. return kioc;
  480. }
  481. /**
  482. * mraid_mm_dealloc_kioc - Return kioc to free pool
  483. *
  484. * @adp : Adapter softstate
  485. * @kioc : uioc_t node to be returned to free pool
  486. */
  487. static void
  488. mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
  489. {
  490. mm_dmapool_t *pool;
  491. unsigned long flags;
  492. if (kioc->pool_index != -1) {
  493. pool = &adp->dma_pool_list[kioc->pool_index];
  494. /* This routine may be called in non-isr context also */
  495. spin_lock_irqsave(&pool->lock, flags);
  496. /*
  497. * While attaching the dma buffer, if we didn't get the
  498. * required buffer from the pool, we would have allocated
  499. * it at the run time and set the free_buf flag. We must
  500. * free that buffer. Otherwise, just mark that the buffer is
  501. * not in use
  502. */
  503. if (kioc->free_buf == 1)
  504. pci_pool_free(pool->handle, kioc->buf_vaddr,
  505. kioc->buf_paddr);
  506. else
  507. pool->in_use = 0;
  508. spin_unlock_irqrestore(&pool->lock, flags);
  509. }
  510. /* Return the kioc to the free pool */
  511. spin_lock_irqsave(&adp->kioc_pool_lock, flags);
  512. list_add(&kioc->list, &adp->kioc_pool);
  513. spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
  514. /* increment the free kioc count */
  515. up(&adp->kioc_semaphore);
  516. return;
  517. }
  518. /**
  519. * lld_ioctl - Routine to issue ioctl to low level drvr
  520. *
  521. * @adp : The adapter handle
  522. * @kioc : The ioctl packet with kernel addresses
  523. */
  524. static int
  525. lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
  526. {
  527. int rval;
  528. struct timer_list timer;
  529. struct timer_list *tp = NULL;
  530. kioc->status = -ENODATA;
  531. rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
  532. if (rval) return rval;
  533. /*
  534. * Start the timer
  535. */
  536. if (adp->timeout > 0) {
  537. tp = &timer;
  538. init_timer(tp);
  539. tp->function = lld_timedout;
  540. tp->data = (unsigned long)kioc;
  541. tp->expires = jiffies + adp->timeout * HZ;
  542. add_timer(tp);
  543. }
  544. /*
  545. * Wait till the low level driver completes the ioctl. After this
  546. * call, the ioctl either completed successfully or timedout.
  547. */
  548. wait_event(wait_q, (kioc->status != -ENODATA));
  549. if (tp) {
  550. del_timer_sync(tp);
  551. }
  552. /*
  553. * If the command had timedout, we mark the controller offline
  554. * before returning
  555. */
  556. if (kioc->timedout) {
  557. adp->quiescent = 0;
  558. }
  559. return kioc->status;
  560. }
  561. /**
  562. * ioctl_done - callback from the low level driver
  563. *
  564. * @kioc : completed ioctl packet
  565. */
  566. static void
  567. ioctl_done(uioc_t *kioc)
  568. {
  569. uint32_t adapno;
  570. int iterator;
  571. mraid_mmadp_t* adapter;
  572. /*
  573. * When the kioc returns from driver, make sure it still doesn't
  574. * have ENODATA in status. Otherwise, driver will hang on wait_event
  575. * forever
  576. */
  577. if (kioc->status == -ENODATA) {
  578. con_log(CL_ANN, (KERN_WARNING
  579. "megaraid cmm: lld didn't change status!\n"));
  580. kioc->status = -EINVAL;
  581. }
  582. /*
  583. * Check if this kioc was timedout before. If so, nobody is waiting
  584. * on this kioc. We don't have to wake up anybody. Instead, we just
  585. * have to free the kioc
  586. */
  587. if (kioc->timedout) {
  588. iterator = 0;
  589. adapter = NULL;
  590. adapno = kioc->adapno;
  591. con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
  592. "ioctl that was timedout before\n"));
  593. list_for_each_entry(adapter, &adapters_list_g, list) {
  594. if (iterator++ == adapno) break;
  595. }
  596. kioc->timedout = 0;
  597. if (adapter) {
  598. mraid_mm_dealloc_kioc( adapter, kioc );
  599. }
  600. }
  601. else {
  602. wake_up(&wait_q);
  603. }
  604. }
  605. /*
  606. * lld_timedout : callback from the expired timer
  607. *
  608. * @ptr : ioctl packet that timed out
  609. */
  610. static void
  611. lld_timedout(unsigned long ptr)
  612. {
  613. uioc_t *kioc = (uioc_t *)ptr;
  614. kioc->status = -ETIME;
  615. kioc->timedout = 1;
  616. con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
  617. wake_up(&wait_q);
  618. }
  619. /**
  620. * kioc_to_mimd : Converter from new back to old format
  621. *
  622. * @kioc : Kernel space IOCTL packet (successfully issued)
  623. * @mimd : User space MIMD packet
  624. */
  625. static int
  626. kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
  627. {
  628. mimd_t kmimd;
  629. uint8_t opcode;
  630. uint8_t subopcode;
  631. mbox64_t *mbox64;
  632. mraid_passthru_t __user *upthru32;
  633. mraid_passthru_t *kpthru32;
  634. mcontroller_t cinfo;
  635. mraid_hba_info_t *hinfo;
  636. if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
  637. return (-EFAULT);
  638. opcode = kmimd.ui.fcs.opcode;
  639. subopcode = kmimd.ui.fcs.subopcode;
  640. if (opcode == 0x82) {
  641. switch (subopcode) {
  642. case MEGAIOC_QADAPINFO:
  643. hinfo = (mraid_hba_info_t *)(unsigned long)
  644. kioc->buf_vaddr;
  645. hinfo_to_cinfo(hinfo, &cinfo);
  646. if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
  647. return (-EFAULT);
  648. return 0;
  649. default:
  650. return (-EINVAL);
  651. }
  652. return 0;
  653. }
  654. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  655. if (kioc->user_pthru) {
  656. upthru32 = kioc->user_pthru;
  657. kpthru32 = kioc->pthru32;
  658. if (copy_to_user(&upthru32->scsistatus,
  659. &kpthru32->scsistatus,
  660. sizeof(uint8_t))) {
  661. return (-EFAULT);
  662. }
  663. }
  664. if (kioc->user_data) {
  665. if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
  666. kioc->user_data_len)) {
  667. return (-EFAULT);
  668. }
  669. }
  670. if (copy_to_user(&mimd->mbox[17],
  671. &mbox64->mbox32.status, sizeof(uint8_t))) {
  672. return (-EFAULT);
  673. }
  674. return 0;
  675. }
  676. /**
  677. * hinfo_to_cinfo - Convert new format hba info into old format
  678. *
  679. * @hinfo : New format, more comprehensive adapter info
  680. * @cinfo : Old format adapter info to support mimd_t apps
  681. */
  682. static void
  683. hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
  684. {
  685. if (!hinfo || !cinfo)
  686. return;
  687. cinfo->base = hinfo->baseport;
  688. cinfo->irq = hinfo->irq;
  689. cinfo->numldrv = hinfo->num_ldrv;
  690. cinfo->pcibus = hinfo->pci_bus;
  691. cinfo->pcidev = hinfo->pci_slot;
  692. cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
  693. cinfo->pciid = hinfo->pci_device_id;
  694. cinfo->pcivendor = hinfo->pci_vendor_id;
  695. cinfo->pcislot = hinfo->pci_slot;
  696. cinfo->uid = hinfo->unique_id;
  697. }
  698. /*
  699. * mraid_mm_register_adp - Registration routine for low level drvrs
  700. *
  701. * @adp : Adapter objejct
  702. */
  703. int
  704. mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
  705. {
  706. mraid_mmadp_t *adapter;
  707. mbox64_t *mbox_list;
  708. uioc_t *kioc;
  709. uint32_t rval;
  710. int i;
  711. if (lld_adp->drvr_type != DRVRTYPE_MBOX)
  712. return (-EINVAL);
  713. adapter = kmalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
  714. if (!adapter)
  715. return -ENOMEM;
  716. memset(adapter, 0, sizeof(mraid_mmadp_t));
  717. adapter->unique_id = lld_adp->unique_id;
  718. adapter->drvr_type = lld_adp->drvr_type;
  719. adapter->drvr_data = lld_adp->drvr_data;
  720. adapter->pdev = lld_adp->pdev;
  721. adapter->issue_uioc = lld_adp->issue_uioc;
  722. adapter->timeout = lld_adp->timeout;
  723. adapter->max_kioc = lld_adp->max_kioc;
  724. adapter->quiescent = 1;
  725. /*
  726. * Allocate single blocks of memory for all required kiocs,
  727. * mailboxes and passthru structures.
  728. */
  729. adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
  730. GFP_KERNEL);
  731. adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
  732. GFP_KERNEL);
  733. adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
  734. adapter->pdev,
  735. sizeof(mraid_passthru_t),
  736. 16, 0);
  737. if (!adapter->kioc_list || !adapter->mbox_list ||
  738. !adapter->pthru_dma_pool) {
  739. con_log(CL_ANN, (KERN_WARNING
  740. "megaraid cmm: out of memory, %s %d\n", __FUNCTION__,
  741. __LINE__));
  742. rval = (-ENOMEM);
  743. goto memalloc_error;
  744. }
  745. /*
  746. * Slice kioc_list and make a kioc_pool with the individiual kiocs
  747. */
  748. INIT_LIST_HEAD(&adapter->kioc_pool);
  749. spin_lock_init(&adapter->kioc_pool_lock);
  750. sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
  751. mbox_list = (mbox64_t *)adapter->mbox_list;
  752. for (i = 0; i < lld_adp->max_kioc; i++) {
  753. kioc = adapter->kioc_list + i;
  754. kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
  755. kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
  756. GFP_KERNEL, &kioc->pthru32_h);
  757. if (!kioc->pthru32) {
  758. con_log(CL_ANN, (KERN_WARNING
  759. "megaraid cmm: out of memory, %s %d\n",
  760. __FUNCTION__, __LINE__));
  761. rval = (-ENOMEM);
  762. goto pthru_dma_pool_error;
  763. }
  764. list_add_tail(&kioc->list, &adapter->kioc_pool);
  765. }
  766. // Setup the dma pools for data buffers
  767. if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
  768. goto dma_pool_error;
  769. }
  770. list_add_tail(&adapter->list, &adapters_list_g);
  771. adapters_count_g++;
  772. return 0;
  773. dma_pool_error:
  774. /* Do nothing */
  775. pthru_dma_pool_error:
  776. for (i = 0; i < lld_adp->max_kioc; i++) {
  777. kioc = adapter->kioc_list + i;
  778. if (kioc->pthru32) {
  779. pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
  780. kioc->pthru32_h);
  781. }
  782. }
  783. memalloc_error:
  784. kfree(adapter->kioc_list);
  785. kfree(adapter->mbox_list);
  786. if (adapter->pthru_dma_pool)
  787. pci_pool_destroy(adapter->pthru_dma_pool);
  788. kfree(adapter);
  789. return rval;
  790. }
  791. /**
  792. * mraid_mm_adapter_app_handle - return the application handle for this adapter
  793. *
  794. * For the given driver data, locate the adadpter in our global list and
  795. * return the corresponding handle, which is also used by applications to
  796. * uniquely identify an adapter.
  797. *
  798. * @param unique_id : adapter unique identifier
  799. *
  800. * @return adapter handle if found in the list
  801. * @return 0 if adapter could not be located, should never happen though
  802. */
  803. uint32_t
  804. mraid_mm_adapter_app_handle(uint32_t unique_id)
  805. {
  806. mraid_mmadp_t *adapter;
  807. mraid_mmadp_t *tmp;
  808. int index = 0;
  809. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  810. if (adapter->unique_id == unique_id) {
  811. return MKADAP(index);
  812. }
  813. index++;
  814. }
  815. return 0;
  816. }
  817. /**
  818. * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
  819. *
  820. * @adp : Adapter softstate
  821. *
  822. * We maintain a pool of dma buffers per each adapter. Each pool has one
  823. * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
  824. * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
  825. * dont' want to waste too much memory by allocating more buffers per each
  826. * pool.
  827. */
  828. static int
  829. mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
  830. {
  831. mm_dmapool_t *pool;
  832. int bufsize;
  833. int i;
  834. /*
  835. * Create MAX_DMA_POOLS number of pools
  836. */
  837. bufsize = MRAID_MM_INIT_BUFF_SIZE;
  838. for (i = 0; i < MAX_DMA_POOLS; i++){
  839. pool = &adp->dma_pool_list[i];
  840. pool->buf_size = bufsize;
  841. spin_lock_init(&pool->lock);
  842. pool->handle = pci_pool_create("megaraid mm data buffer",
  843. adp->pdev, bufsize, 16, 0);
  844. if (!pool->handle) {
  845. goto dma_pool_setup_error;
  846. }
  847. pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
  848. &pool->paddr);
  849. if (!pool->vaddr)
  850. goto dma_pool_setup_error;
  851. bufsize = bufsize * 2;
  852. }
  853. return 0;
  854. dma_pool_setup_error:
  855. mraid_mm_teardown_dma_pools(adp);
  856. return (-ENOMEM);
  857. }
  858. /*
  859. * mraid_mm_unregister_adp - Unregister routine for low level drivers
  860. * Assume no outstanding ioctls to llds.
  861. *
  862. * @unique_id : UID of the adpater
  863. */
  864. int
  865. mraid_mm_unregister_adp(uint32_t unique_id)
  866. {
  867. mraid_mmadp_t *adapter;
  868. mraid_mmadp_t *tmp;
  869. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  870. if (adapter->unique_id == unique_id) {
  871. adapters_count_g--;
  872. list_del_init(&adapter->list);
  873. mraid_mm_free_adp_resources(adapter);
  874. kfree(adapter);
  875. con_log(CL_ANN, (
  876. "megaraid cmm: Unregistered one adapter:%#x\n",
  877. unique_id));
  878. return 0;
  879. }
  880. }
  881. return (-ENODEV);
  882. }
  883. /**
  884. * mraid_mm_free_adp_resources - Free adapter softstate
  885. *
  886. * @adp : Adapter softstate
  887. */
  888. static void
  889. mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
  890. {
  891. uioc_t *kioc;
  892. int i;
  893. mraid_mm_teardown_dma_pools(adp);
  894. for (i = 0; i < adp->max_kioc; i++) {
  895. kioc = adp->kioc_list + i;
  896. pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
  897. kioc->pthru32_h);
  898. }
  899. kfree(adp->kioc_list);
  900. kfree(adp->mbox_list);
  901. pci_pool_destroy(adp->pthru_dma_pool);
  902. return;
  903. }
  904. /**
  905. * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
  906. *
  907. * @adp : Adapter softstate
  908. */
  909. static void
  910. mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
  911. {
  912. int i;
  913. mm_dmapool_t *pool;
  914. for (i = 0; i < MAX_DMA_POOLS; i++) {
  915. pool = &adp->dma_pool_list[i];
  916. if (pool->handle) {
  917. if (pool->vaddr)
  918. pci_pool_free(pool->handle, pool->vaddr,
  919. pool->paddr);
  920. pci_pool_destroy(pool->handle);
  921. pool->handle = NULL;
  922. }
  923. }
  924. return;
  925. }
  926. /**
  927. * mraid_mm_init : Module entry point
  928. */
  929. static int __init
  930. mraid_mm_init(void)
  931. {
  932. // Announce the driver version
  933. con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
  934. LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
  935. majorno = register_chrdev(0, "megadev", &lsi_fops);
  936. if (majorno < 0) {
  937. con_log(CL_ANN, ("megaraid cmm: cannot get major\n"));
  938. return majorno;
  939. }
  940. init_waitqueue_head(&wait_q);
  941. INIT_LIST_HEAD(&adapters_list_g);
  942. return 0;
  943. }
  944. /**
  945. * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine
  946. */
  947. #ifdef CONFIG_COMPAT
  948. static long
  949. mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
  950. unsigned long arg)
  951. {
  952. int err;
  953. err = mraid_mm_ioctl(NULL, filep, cmd, arg);
  954. return err;
  955. }
  956. #endif
  957. /**
  958. * mraid_mm_exit : Module exit point
  959. */
  960. static void __exit
  961. mraid_mm_exit(void)
  962. {
  963. con_log(CL_DLEVEL1 , ("exiting common mod\n"));
  964. unregister_chrdev(majorno, "megadev");
  965. }
  966. module_init(mraid_mm_init);
  967. module_exit(mraid_mm_exit);
  968. /* vi: set ts=8 sw=8 tw=78: */