megaraid_mm.c 26 KB

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