megaraid_mm.c 27 KB

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