megaraid_mm.c 27 KB

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