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(unsigned long);
  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 timer_list timer;
  553. struct timer_list *tp = NULL;
  554. kioc->status = -ENODATA;
  555. rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
  556. if (rval) return rval;
  557. /*
  558. * Start the timer
  559. */
  560. if (adp->timeout > 0) {
  561. tp = &timer;
  562. init_timer(tp);
  563. tp->function = lld_timedout;
  564. tp->data = (unsigned long)kioc;
  565. tp->expires = jiffies + adp->timeout * HZ;
  566. add_timer(tp);
  567. }
  568. /*
  569. * Wait till the low level driver completes the ioctl. After this
  570. * call, the ioctl either completed successfully or timedout.
  571. */
  572. wait_event(wait_q, (kioc->status != -ENODATA));
  573. if (tp) {
  574. del_timer_sync(tp);
  575. }
  576. /*
  577. * If the command had timedout, we mark the controller offline
  578. * before returning
  579. */
  580. if (kioc->timedout) {
  581. adp->quiescent = 0;
  582. }
  583. return kioc->status;
  584. }
  585. /**
  586. * ioctl_done - callback from the low level driver
  587. * @kioc : completed ioctl packet
  588. */
  589. static void
  590. ioctl_done(uioc_t *kioc)
  591. {
  592. uint32_t adapno;
  593. int iterator;
  594. mraid_mmadp_t* adapter;
  595. /*
  596. * When the kioc returns from driver, make sure it still doesn't
  597. * have ENODATA in status. Otherwise, driver will hang on wait_event
  598. * forever
  599. */
  600. if (kioc->status == -ENODATA) {
  601. con_log(CL_ANN, (KERN_WARNING
  602. "megaraid cmm: lld didn't change status!\n"));
  603. kioc->status = -EINVAL;
  604. }
  605. /*
  606. * Check if this kioc was timedout before. If so, nobody is waiting
  607. * on this kioc. We don't have to wake up anybody. Instead, we just
  608. * have to free the kioc
  609. */
  610. if (kioc->timedout) {
  611. iterator = 0;
  612. adapter = NULL;
  613. adapno = kioc->adapno;
  614. con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
  615. "ioctl that was timedout before\n"));
  616. list_for_each_entry(adapter, &adapters_list_g, list) {
  617. if (iterator++ == adapno) break;
  618. }
  619. kioc->timedout = 0;
  620. if (adapter) {
  621. mraid_mm_dealloc_kioc( adapter, kioc );
  622. }
  623. }
  624. else {
  625. wake_up(&wait_q);
  626. }
  627. }
  628. /**
  629. * lld_timedout - callback from the expired timer
  630. * @ptr : ioctl packet that timed out
  631. */
  632. static void
  633. lld_timedout(unsigned long ptr)
  634. {
  635. uioc_t *kioc = (uioc_t *)ptr;
  636. kioc->status = -ETIME;
  637. kioc->timedout = 1;
  638. con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
  639. wake_up(&wait_q);
  640. }
  641. /**
  642. * kioc_to_mimd - Converter from new back to old format
  643. * @kioc : Kernel space IOCTL packet (successfully issued)
  644. * @mimd : User space MIMD packet
  645. */
  646. static int
  647. kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
  648. {
  649. mimd_t kmimd;
  650. uint8_t opcode;
  651. uint8_t subopcode;
  652. mbox64_t *mbox64;
  653. mraid_passthru_t __user *upthru32;
  654. mraid_passthru_t *kpthru32;
  655. mcontroller_t cinfo;
  656. mraid_hba_info_t *hinfo;
  657. if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
  658. return (-EFAULT);
  659. opcode = kmimd.ui.fcs.opcode;
  660. subopcode = kmimd.ui.fcs.subopcode;
  661. if (opcode == 0x82) {
  662. switch (subopcode) {
  663. case MEGAIOC_QADAPINFO:
  664. hinfo = (mraid_hba_info_t *)(unsigned long)
  665. kioc->buf_vaddr;
  666. hinfo_to_cinfo(hinfo, &cinfo);
  667. if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
  668. return (-EFAULT);
  669. return 0;
  670. default:
  671. return (-EINVAL);
  672. }
  673. return 0;
  674. }
  675. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  676. if (kioc->user_pthru) {
  677. upthru32 = kioc->user_pthru;
  678. kpthru32 = kioc->pthru32;
  679. if (copy_to_user(&upthru32->scsistatus,
  680. &kpthru32->scsistatus,
  681. sizeof(uint8_t))) {
  682. return (-EFAULT);
  683. }
  684. }
  685. if (kioc->user_data) {
  686. if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
  687. kioc->user_data_len)) {
  688. return (-EFAULT);
  689. }
  690. }
  691. if (copy_to_user(&mimd->mbox[17],
  692. &mbox64->mbox32.status, sizeof(uint8_t))) {
  693. return (-EFAULT);
  694. }
  695. return 0;
  696. }
  697. /**
  698. * hinfo_to_cinfo - Convert new format hba info into old format
  699. * @hinfo : New format, more comprehensive adapter info
  700. * @cinfo : Old format adapter info to support mimd_t apps
  701. */
  702. static void
  703. hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
  704. {
  705. if (!hinfo || !cinfo)
  706. return;
  707. cinfo->base = hinfo->baseport;
  708. cinfo->irq = hinfo->irq;
  709. cinfo->numldrv = hinfo->num_ldrv;
  710. cinfo->pcibus = hinfo->pci_bus;
  711. cinfo->pcidev = hinfo->pci_slot;
  712. cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
  713. cinfo->pciid = hinfo->pci_device_id;
  714. cinfo->pcivendor = hinfo->pci_vendor_id;
  715. cinfo->pcislot = hinfo->pci_slot;
  716. cinfo->uid = hinfo->unique_id;
  717. }
  718. /**
  719. * mraid_mm_register_adp - Registration routine for low level drivers
  720. * @lld_adp : Adapter object
  721. */
  722. int
  723. mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
  724. {
  725. mraid_mmadp_t *adapter;
  726. mbox64_t *mbox_list;
  727. uioc_t *kioc;
  728. uint32_t rval;
  729. int i;
  730. if (lld_adp->drvr_type != DRVRTYPE_MBOX)
  731. return (-EINVAL);
  732. adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
  733. if (!adapter)
  734. return -ENOMEM;
  735. adapter->unique_id = lld_adp->unique_id;
  736. adapter->drvr_type = lld_adp->drvr_type;
  737. adapter->drvr_data = lld_adp->drvr_data;
  738. adapter->pdev = lld_adp->pdev;
  739. adapter->issue_uioc = lld_adp->issue_uioc;
  740. adapter->timeout = lld_adp->timeout;
  741. adapter->max_kioc = lld_adp->max_kioc;
  742. adapter->quiescent = 1;
  743. /*
  744. * Allocate single blocks of memory for all required kiocs,
  745. * mailboxes and passthru structures.
  746. */
  747. adapter->kioc_list = kmalloc(sizeof(uioc_t) * lld_adp->max_kioc,
  748. GFP_KERNEL);
  749. adapter->mbox_list = kmalloc(sizeof(mbox64_t) * lld_adp->max_kioc,
  750. GFP_KERNEL);
  751. adapter->pthru_dma_pool = dma_pool_create("megaraid mm pthru pool",
  752. &adapter->pdev->dev,
  753. sizeof(mraid_passthru_t),
  754. 16, 0);
  755. if (!adapter->kioc_list || !adapter->mbox_list ||
  756. !adapter->pthru_dma_pool) {
  757. con_log(CL_ANN, (KERN_WARNING
  758. "megaraid cmm: out of memory, %s %d\n", __func__,
  759. __LINE__));
  760. rval = (-ENOMEM);
  761. goto memalloc_error;
  762. }
  763. /*
  764. * Slice kioc_list and make a kioc_pool with the individiual kiocs
  765. */
  766. INIT_LIST_HEAD(&adapter->kioc_pool);
  767. spin_lock_init(&adapter->kioc_pool_lock);
  768. sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
  769. mbox_list = (mbox64_t *)adapter->mbox_list;
  770. for (i = 0; i < lld_adp->max_kioc; i++) {
  771. kioc = adapter->kioc_list + i;
  772. kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
  773. kioc->pthru32 = dma_pool_alloc(adapter->pthru_dma_pool,
  774. GFP_KERNEL, &kioc->pthru32_h);
  775. if (!kioc->pthru32) {
  776. con_log(CL_ANN, (KERN_WARNING
  777. "megaraid cmm: out of memory, %s %d\n",
  778. __func__, __LINE__));
  779. rval = (-ENOMEM);
  780. goto pthru_dma_pool_error;
  781. }
  782. list_add_tail(&kioc->list, &adapter->kioc_pool);
  783. }
  784. // Setup the dma pools for data buffers
  785. if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
  786. goto dma_pool_error;
  787. }
  788. list_add_tail(&adapter->list, &adapters_list_g);
  789. adapters_count_g++;
  790. return 0;
  791. dma_pool_error:
  792. /* Do nothing */
  793. pthru_dma_pool_error:
  794. for (i = 0; i < lld_adp->max_kioc; i++) {
  795. kioc = adapter->kioc_list + i;
  796. if (kioc->pthru32) {
  797. dma_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
  798. kioc->pthru32_h);
  799. }
  800. }
  801. memalloc_error:
  802. kfree(adapter->kioc_list);
  803. kfree(adapter->mbox_list);
  804. if (adapter->pthru_dma_pool)
  805. dma_pool_destroy(adapter->pthru_dma_pool);
  806. kfree(adapter);
  807. return rval;
  808. }
  809. /**
  810. * mraid_mm_adapter_app_handle - return the application handle for this adapter
  811. * @unique_id : adapter unique identifier
  812. *
  813. * For the given driver data, locate the adapter in our global list and
  814. * return the corresponding handle, which is also used by applications to
  815. * uniquely identify an adapter.
  816. *
  817. * Return adapter handle if found in the list.
  818. * Return 0 if adapter could not be located, should never happen though.
  819. */
  820. uint32_t
  821. mraid_mm_adapter_app_handle(uint32_t unique_id)
  822. {
  823. mraid_mmadp_t *adapter;
  824. mraid_mmadp_t *tmp;
  825. int index = 0;
  826. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  827. if (adapter->unique_id == unique_id) {
  828. return MKADAP(index);
  829. }
  830. index++;
  831. }
  832. return 0;
  833. }
  834. /**
  835. * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
  836. * @adp : Adapter softstate
  837. *
  838. * We maintain a pool of dma buffers per each adapter. Each pool has one
  839. * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
  840. * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
  841. * dont' want to waste too much memory by allocating more buffers per each
  842. * pool.
  843. */
  844. static int
  845. mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
  846. {
  847. mm_dmapool_t *pool;
  848. int bufsize;
  849. int i;
  850. /*
  851. * Create MAX_DMA_POOLS number of pools
  852. */
  853. bufsize = MRAID_MM_INIT_BUFF_SIZE;
  854. for (i = 0; i < MAX_DMA_POOLS; i++){
  855. pool = &adp->dma_pool_list[i];
  856. pool->buf_size = bufsize;
  857. spin_lock_init(&pool->lock);
  858. pool->handle = dma_pool_create("megaraid mm data buffer",
  859. &adp->pdev->dev, bufsize,
  860. 16, 0);
  861. if (!pool->handle) {
  862. goto dma_pool_setup_error;
  863. }
  864. pool->vaddr = dma_pool_alloc(pool->handle, GFP_KERNEL,
  865. &pool->paddr);
  866. if (!pool->vaddr)
  867. goto dma_pool_setup_error;
  868. bufsize = bufsize * 2;
  869. }
  870. return 0;
  871. dma_pool_setup_error:
  872. mraid_mm_teardown_dma_pools(adp);
  873. return (-ENOMEM);
  874. }
  875. /**
  876. * mraid_mm_unregister_adp - Unregister routine for low level drivers
  877. * @unique_id : UID of the adpater
  878. *
  879. * Assumes no outstanding ioctls to llds.
  880. */
  881. int
  882. mraid_mm_unregister_adp(uint32_t unique_id)
  883. {
  884. mraid_mmadp_t *adapter;
  885. mraid_mmadp_t *tmp;
  886. list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
  887. if (adapter->unique_id == unique_id) {
  888. adapters_count_g--;
  889. list_del_init(&adapter->list);
  890. mraid_mm_free_adp_resources(adapter);
  891. kfree(adapter);
  892. con_log(CL_ANN, (
  893. "megaraid cmm: Unregistered one adapter:%#x\n",
  894. unique_id));
  895. return 0;
  896. }
  897. }
  898. return (-ENODEV);
  899. }
  900. /**
  901. * mraid_mm_free_adp_resources - Free adapter softstate
  902. * @adp : Adapter softstate
  903. */
  904. static void
  905. mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
  906. {
  907. uioc_t *kioc;
  908. int i;
  909. mraid_mm_teardown_dma_pools(adp);
  910. for (i = 0; i < adp->max_kioc; i++) {
  911. kioc = adp->kioc_list + i;
  912. dma_pool_free(adp->pthru_dma_pool, kioc->pthru32,
  913. kioc->pthru32_h);
  914. }
  915. kfree(adp->kioc_list);
  916. kfree(adp->mbox_list);
  917. dma_pool_destroy(adp->pthru_dma_pool);
  918. return;
  919. }
  920. /**
  921. * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
  922. * @adp : Adapter softstate
  923. */
  924. static void
  925. mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
  926. {
  927. int i;
  928. mm_dmapool_t *pool;
  929. for (i = 0; i < MAX_DMA_POOLS; i++) {
  930. pool = &adp->dma_pool_list[i];
  931. if (pool->handle) {
  932. if (pool->vaddr)
  933. dma_pool_free(pool->handle, pool->vaddr,
  934. pool->paddr);
  935. dma_pool_destroy(pool->handle);
  936. pool->handle = NULL;
  937. }
  938. }
  939. return;
  940. }
  941. /**
  942. * mraid_mm_init - Module entry point
  943. */
  944. static int __init
  945. mraid_mm_init(void)
  946. {
  947. int err;
  948. // Announce the driver version
  949. con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
  950. LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
  951. err = misc_register(&megaraid_mm_dev);
  952. if (err < 0) {
  953. con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
  954. return err;
  955. }
  956. init_waitqueue_head(&wait_q);
  957. INIT_LIST_HEAD(&adapters_list_g);
  958. return 0;
  959. }
  960. #ifdef CONFIG_COMPAT
  961. /**
  962. * mraid_mm_compat_ioctl - 32bit to 64bit ioctl conversion routine
  963. * @filep : file operations pointer (ignored)
  964. * @cmd : ioctl command
  965. * @arg : user ioctl packet
  966. */
  967. static long
  968. mraid_mm_compat_ioctl(struct file *filep, unsigned int cmd,
  969. unsigned long arg)
  970. {
  971. int err;
  972. err = mraid_mm_ioctl(filep, cmd, arg);
  973. return err;
  974. }
  975. #endif
  976. /**
  977. * mraid_mm_exit - Module exit point
  978. */
  979. static void __exit
  980. mraid_mm_exit(void)
  981. {
  982. con_log(CL_DLEVEL1 , ("exiting common mod\n"));
  983. misc_deregister(&megaraid_mm_dev);
  984. }
  985. module_init(mraid_mm_init);
  986. module_exit(mraid_mm_exit);
  987. /* vi: set ts=8 sw=8 tw=78: */