megaraid_mm.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. rval = -ENOMEM;
  716. goto memalloc_error;
  717. }
  718. memset(adapter, 0, sizeof(mraid_mmadp_t));
  719. adapter->unique_id = lld_adp->unique_id;
  720. adapter->drvr_type = lld_adp->drvr_type;
  721. adapter->drvr_data = lld_adp->drvr_data;
  722. adapter->pdev = lld_adp->pdev;
  723. adapter->issue_uioc = lld_adp->issue_uioc;
  724. adapter->timeout = lld_adp->timeout;
  725. adapter->max_kioc = lld_adp->max_kioc;
  726. adapter->quiescent = 1;
  727. /*
  728. * Allocate single blocks of memory for all required kiocs,
  729. * mailboxes and passthru structures.
  730. */
  731. adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
  732. GFP_KERNEL);
  733. adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
  734. GFP_KERNEL);
  735. adapter->pthru_dma_pool = pci_pool_create("megaraid mm pthru pool",
  736. adapter->pdev,
  737. sizeof(mraid_passthru_t),
  738. 16, 0);
  739. if (!adapter->kioc_list || !adapter->mbox_list ||
  740. !adapter->pthru_dma_pool) {
  741. con_log(CL_ANN, (KERN_WARNING
  742. "megaraid cmm: out of memory, %s %d\n", __FUNCTION__,
  743. __LINE__));
  744. rval = (-ENOMEM);
  745. goto memalloc_error;
  746. }
  747. /*
  748. * Slice kioc_list and make a kioc_pool with the individiual kiocs
  749. */
  750. INIT_LIST_HEAD(&adapter->kioc_pool);
  751. spin_lock_init(&adapter->kioc_pool_lock);
  752. sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
  753. mbox_list = (mbox64_t *)adapter->mbox_list;
  754. for (i = 0; i < lld_adp->max_kioc; i++) {
  755. kioc = adapter->kioc_list + i;
  756. kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
  757. kioc->pthru32 = pci_pool_alloc(adapter->pthru_dma_pool,
  758. GFP_KERNEL, &kioc->pthru32_h);
  759. if (!kioc->pthru32) {
  760. con_log(CL_ANN, (KERN_WARNING
  761. "megaraid cmm: out of memory, %s %d\n",
  762. __FUNCTION__, __LINE__));
  763. rval = (-ENOMEM);
  764. goto pthru_dma_pool_error;
  765. }
  766. list_add_tail(&kioc->list, &adapter->kioc_pool);
  767. }
  768. // Setup the dma pools for data buffers
  769. if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
  770. goto dma_pool_error;
  771. }
  772. list_add_tail(&adapter->list, &adapters_list_g);
  773. adapters_count_g++;
  774. return 0;
  775. dma_pool_error:
  776. /* Do nothing */
  777. pthru_dma_pool_error:
  778. for (i = 0; i < lld_adp->max_kioc; i++) {
  779. kioc = adapter->kioc_list + i;
  780. if (kioc->pthru32) {
  781. pci_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
  782. kioc->pthru32_h);
  783. }
  784. }
  785. memalloc_error:
  786. kfree(adapter->kioc_list);
  787. kfree(adapter->mbox_list);
  788. if (adapter->pthru_dma_pool)
  789. pci_pool_destroy(adapter->pthru_dma_pool);
  790. kfree(adapter);
  791. return rval;
  792. }
  793. /**
  794. * mraid_mm_adapter_app_handle - return the application handle for this adapter
  795. *
  796. * For the given driver data, locate the adadpter in our global list and
  797. * return the corresponding handle, which is also used by applications to
  798. * uniquely identify an adapter.
  799. *
  800. * @param unique_id : adapter unique identifier
  801. *
  802. * @return adapter handle if found in the list
  803. * @return 0 if adapter could not be located, should never happen though
  804. */
  805. uint32_t
  806. mraid_mm_adapter_app_handle(uint32_t unique_id)
  807. {
  808. mraid_mmadp_t *adapter;
  809. mraid_mmadp_t *tmp;
  810. int index = 0;
  811. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  812. if (adapter->unique_id == unique_id) {
  813. return MKADAP(index);
  814. }
  815. index++;
  816. }
  817. return 0;
  818. }
  819. /**
  820. * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
  821. *
  822. * @adp : Adapter softstate
  823. *
  824. * We maintain a pool of dma buffers per each adapter. Each pool has one
  825. * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
  826. * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
  827. * dont' want to waste too much memory by allocating more buffers per each
  828. * pool.
  829. */
  830. static int
  831. mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
  832. {
  833. mm_dmapool_t *pool;
  834. int bufsize;
  835. int i;
  836. /*
  837. * Create MAX_DMA_POOLS number of pools
  838. */
  839. bufsize = MRAID_MM_INIT_BUFF_SIZE;
  840. for (i = 0; i < MAX_DMA_POOLS; i++){
  841. pool = &adp->dma_pool_list[i];
  842. pool->buf_size = bufsize;
  843. spin_lock_init(&pool->lock);
  844. pool->handle = pci_pool_create("megaraid mm data buffer",
  845. adp->pdev, bufsize, 16, 0);
  846. if (!pool->handle) {
  847. goto dma_pool_setup_error;
  848. }
  849. pool->vaddr = pci_pool_alloc(pool->handle, GFP_KERNEL,
  850. &pool->paddr);
  851. if (!pool->vaddr)
  852. goto dma_pool_setup_error;
  853. bufsize = bufsize * 2;
  854. }
  855. return 0;
  856. dma_pool_setup_error:
  857. mraid_mm_teardown_dma_pools(adp);
  858. return (-ENOMEM);
  859. }
  860. /*
  861. * mraid_mm_unregister_adp - Unregister routine for low level drivers
  862. * Assume no outstanding ioctls to llds.
  863. *
  864. * @unique_id : UID of the adpater
  865. */
  866. int
  867. mraid_mm_unregister_adp(uint32_t unique_id)
  868. {
  869. mraid_mmadp_t *adapter;
  870. mraid_mmadp_t *tmp;
  871. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  872. if (adapter->unique_id == unique_id) {
  873. adapters_count_g--;
  874. list_del_init(&adapter->list);
  875. mraid_mm_free_adp_resources(adapter);
  876. kfree(adapter);
  877. con_log(CL_ANN, (
  878. "megaraid cmm: Unregistered one adapter:%#x\n",
  879. unique_id));
  880. return 0;
  881. }
  882. }
  883. return (-ENODEV);
  884. }
  885. /**
  886. * mraid_mm_free_adp_resources - Free adapter softstate
  887. *
  888. * @adp : Adapter softstate
  889. */
  890. static void
  891. mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
  892. {
  893. uioc_t *kioc;
  894. int i;
  895. mraid_mm_teardown_dma_pools(adp);
  896. for (i = 0; i < adp->max_kioc; i++) {
  897. kioc = adp->kioc_list + i;
  898. pci_pool_free(adp->pthru_dma_pool, kioc->pthru32,
  899. kioc->pthru32_h);
  900. }
  901. kfree(adp->kioc_list);
  902. kfree(adp->mbox_list);
  903. pci_pool_destroy(adp->pthru_dma_pool);
  904. return;
  905. }
  906. /**
  907. * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
  908. *
  909. * @adp : Adapter softstate
  910. */
  911. static void
  912. mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
  913. {
  914. int i;
  915. mm_dmapool_t *pool;
  916. for (i = 0; i < MAX_DMA_POOLS; i++) {
  917. pool = &adp->dma_pool_list[i];
  918. if (pool->handle) {
  919. if (pool->vaddr)
  920. pci_pool_free(pool->handle, pool->vaddr,
  921. pool->paddr);
  922. pci_pool_destroy(pool->handle);
  923. pool->handle = NULL;
  924. }
  925. }
  926. return;
  927. }
  928. /**
  929. * mraid_mm_init : Module entry point
  930. */
  931. static int __init
  932. mraid_mm_init(void)
  933. {
  934. // Announce the driver version
  935. con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
  936. LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
  937. majorno = register_chrdev(0, "megadev", &lsi_fops);
  938. if (majorno < 0) {
  939. con_log(CL_ANN, ("megaraid cmm: cannot get major\n"));
  940. return majorno;
  941. }
  942. init_waitqueue_head(&wait_q);
  943. INIT_LIST_HEAD(&adapters_list_g);
  944. return 0;
  945. }
  946. /**
  947. * mraid_mm_compat_ioctl : 32bit to 64bit ioctl conversion routine
  948. */
  949. #ifdef CONFIG_COMPAT
  950. static long
  951. mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
  952. unsigned long arg)
  953. {
  954. int err;
  955. err = mraid_mm_ioctl(NULL, filep, cmd, arg);
  956. return err;
  957. }
  958. #endif
  959. /**
  960. * mraid_mm_exit : Module exit point
  961. */
  962. static void __exit
  963. mraid_mm_exit(void)
  964. {
  965. con_log(CL_DLEVEL1 , ("exiting common mod\n"));
  966. unregister_chrdev(majorno, "megadev");
  967. }
  968. module_init(mraid_mm_init);
  969. module_exit(mraid_mm_exit);
  970. /* vi: set ts=8 sw=8 tw=78: */