megaraid_mm.c 27 KB

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