megaraid_mm.c 26 KB

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