megaraid_mm.c 27 KB

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