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