megaraid_mm.c 26 KB

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