megaraid_mm.c 26 KB

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