megaraid_mm.c 26 KB

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