i2o_config.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * I2O Configuration Interface Driver
  3. *
  4. * (C) Copyright 1999-2002 Red Hat
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * Fixes/additions:
  9. * Deepak Saxena (04/20/1999):
  10. * Added basic ioctl() support
  11. * Deepak Saxena (06/07/1999):
  12. * Added software download ioctl (still testing)
  13. * Auvo Häkkinen (09/10/1999):
  14. * Changes to i2o_cfg_reply(), ioctl_parms()
  15. * Added ioct_validate()
  16. * Taneli Vähäkangas (09/30/1999):
  17. * Fixed ioctl_swdl()
  18. * Taneli Vähäkangas (10/04/1999):
  19. * Changed ioctl_swdl(), implemented ioctl_swul() and ioctl_swdel()
  20. * Deepak Saxena (11/18/1999):
  21. * Added event managmenet support
  22. * Alan Cox <alan@lxorguk.ukuu.org.uk>:
  23. * 2.4 rewrite ported to 2.5
  24. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  25. * Added pass-thru support for Adaptec's raidutils
  26. *
  27. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License
  29. * as published by the Free Software Foundation; either version
  30. * 2 of the License, or (at your option) any later version.
  31. */
  32. #include <linux/miscdevice.h>
  33. #include <linux/mutex.h>
  34. #include <linux/compat.h>
  35. #include <linux/slab.h>
  36. #include <asm/uaccess.h>
  37. #include "core.h"
  38. #define SG_TABLESIZE 30
  39. static DEFINE_MUTEX(i2o_cfg_mutex);
  40. static long i2o_cfg_ioctl(struct file *, unsigned int, unsigned long);
  41. static spinlock_t i2o_config_lock;
  42. #define MODINC(x,y) ((x) = ((x) + 1) % (y))
  43. struct sg_simple_element {
  44. u32 flag_count;
  45. u32 addr_bus;
  46. };
  47. struct i2o_cfg_info {
  48. struct file *fp;
  49. struct fasync_struct *fasync;
  50. struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
  51. u16 q_in; // Queue head index
  52. u16 q_out; // Queue tail index
  53. u16 q_len; // Queue length
  54. u16 q_lost; // Number of lost events
  55. ulong q_id; // Event queue ID...used as tx_context
  56. struct i2o_cfg_info *next;
  57. };
  58. static struct i2o_cfg_info *open_files = NULL;
  59. static ulong i2o_cfg_info_id = 0;
  60. static int i2o_cfg_getiops(unsigned long arg)
  61. {
  62. struct i2o_controller *c;
  63. u8 __user *user_iop_table = (void __user *)arg;
  64. u8 tmp[MAX_I2O_CONTROLLERS];
  65. int ret = 0;
  66. memset(tmp, 0, MAX_I2O_CONTROLLERS);
  67. list_for_each_entry(c, &i2o_controllers, list)
  68. tmp[c->unit] = 1;
  69. if (copy_to_user(user_iop_table, tmp, MAX_I2O_CONTROLLERS))
  70. ret = -EFAULT;
  71. return ret;
  72. };
  73. static int i2o_cfg_gethrt(unsigned long arg)
  74. {
  75. struct i2o_controller *c;
  76. struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
  77. struct i2o_cmd_hrtlct kcmd;
  78. i2o_hrt *hrt;
  79. int len;
  80. u32 reslen;
  81. int ret = 0;
  82. if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
  83. return -EFAULT;
  84. if (get_user(reslen, kcmd.reslen) < 0)
  85. return -EFAULT;
  86. if (kcmd.resbuf == NULL)
  87. return -EFAULT;
  88. c = i2o_find_iop(kcmd.iop);
  89. if (!c)
  90. return -ENXIO;
  91. hrt = (i2o_hrt *) c->hrt.virt;
  92. len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
  93. if (put_user(len, kcmd.reslen))
  94. ret = -EFAULT;
  95. else if (len > reslen)
  96. ret = -ENOBUFS;
  97. else if (copy_to_user(kcmd.resbuf, (void *)hrt, len))
  98. ret = -EFAULT;
  99. return ret;
  100. };
  101. static int i2o_cfg_getlct(unsigned long arg)
  102. {
  103. struct i2o_controller *c;
  104. struct i2o_cmd_hrtlct __user *cmd = (struct i2o_cmd_hrtlct __user *)arg;
  105. struct i2o_cmd_hrtlct kcmd;
  106. i2o_lct *lct;
  107. int len;
  108. int ret = 0;
  109. u32 reslen;
  110. if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
  111. return -EFAULT;
  112. if (get_user(reslen, kcmd.reslen) < 0)
  113. return -EFAULT;
  114. if (kcmd.resbuf == NULL)
  115. return -EFAULT;
  116. c = i2o_find_iop(kcmd.iop);
  117. if (!c)
  118. return -ENXIO;
  119. lct = (i2o_lct *) c->lct;
  120. len = (unsigned int)lct->table_size << 2;
  121. if (put_user(len, kcmd.reslen))
  122. ret = -EFAULT;
  123. else if (len > reslen)
  124. ret = -ENOBUFS;
  125. else if (copy_to_user(kcmd.resbuf, lct, len))
  126. ret = -EFAULT;
  127. return ret;
  128. };
  129. static int i2o_cfg_parms(unsigned long arg, unsigned int type)
  130. {
  131. int ret = 0;
  132. struct i2o_controller *c;
  133. struct i2o_device *dev;
  134. struct i2o_cmd_psetget __user *cmd =
  135. (struct i2o_cmd_psetget __user *)arg;
  136. struct i2o_cmd_psetget kcmd;
  137. u32 reslen;
  138. u8 *ops;
  139. u8 *res;
  140. int len = 0;
  141. u32 i2o_cmd = (type == I2OPARMGET ?
  142. I2O_CMD_UTIL_PARAMS_GET : I2O_CMD_UTIL_PARAMS_SET);
  143. if (copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
  144. return -EFAULT;
  145. if (get_user(reslen, kcmd.reslen))
  146. return -EFAULT;
  147. c = i2o_find_iop(kcmd.iop);
  148. if (!c)
  149. return -ENXIO;
  150. dev = i2o_iop_find_device(c, kcmd.tid);
  151. if (!dev)
  152. return -ENXIO;
  153. /*
  154. * Stop users being able to try and allocate arbitrary amounts
  155. * of DMA space. 64K is way more than sufficient for this.
  156. */
  157. if (kcmd.oplen > 65536)
  158. return -EMSGSIZE;
  159. ops = memdup_user(kcmd.opbuf, kcmd.oplen);
  160. if (IS_ERR(ops))
  161. return PTR_ERR(ops);
  162. /*
  163. * It's possible to have a _very_ large table
  164. * and that the user asks for all of it at once...
  165. */
  166. res = kmalloc(65536, GFP_KERNEL);
  167. if (!res) {
  168. kfree(ops);
  169. return -ENOMEM;
  170. }
  171. len = i2o_parm_issue(dev, i2o_cmd, ops, kcmd.oplen, res, 65536);
  172. kfree(ops);
  173. if (len < 0) {
  174. kfree(res);
  175. return -EAGAIN;
  176. }
  177. if (put_user(len, kcmd.reslen))
  178. ret = -EFAULT;
  179. else if (len > reslen)
  180. ret = -ENOBUFS;
  181. else if (copy_to_user(kcmd.resbuf, res, len))
  182. ret = -EFAULT;
  183. kfree(res);
  184. return ret;
  185. };
  186. static int i2o_cfg_swdl(unsigned long arg)
  187. {
  188. struct i2o_sw_xfer kxfer;
  189. struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
  190. unsigned char maxfrag = 0, curfrag = 1;
  191. struct i2o_dma buffer;
  192. struct i2o_message *msg;
  193. unsigned int status = 0, swlen = 0, fragsize = 8192;
  194. struct i2o_controller *c;
  195. if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
  196. return -EFAULT;
  197. if (get_user(swlen, kxfer.swlen) < 0)
  198. return -EFAULT;
  199. if (get_user(maxfrag, kxfer.maxfrag) < 0)
  200. return -EFAULT;
  201. if (get_user(curfrag, kxfer.curfrag) < 0)
  202. return -EFAULT;
  203. if (curfrag == maxfrag)
  204. fragsize = swlen - (maxfrag - 1) * 8192;
  205. if (!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
  206. return -EFAULT;
  207. c = i2o_find_iop(kxfer.iop);
  208. if (!c)
  209. return -ENXIO;
  210. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  211. if (IS_ERR(msg))
  212. return PTR_ERR(msg);
  213. if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
  214. i2o_msg_nop(c, msg);
  215. return -ENOMEM;
  216. }
  217. if (__copy_from_user(buffer.virt, kxfer.buf, fragsize)) {
  218. i2o_msg_nop(c, msg);
  219. i2o_dma_free(&c->pdev->dev, &buffer);
  220. return -EFAULT;
  221. }
  222. msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
  223. msg->u.head[1] =
  224. cpu_to_le32(I2O_CMD_SW_DOWNLOAD << 24 | HOST_TID << 12 |
  225. ADAPTER_TID);
  226. msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
  227. msg->u.head[3] = cpu_to_le32(0);
  228. msg->body[0] =
  229. cpu_to_le32((((u32) kxfer.flags) << 24) | (((u32) kxfer.
  230. sw_type) << 16) |
  231. (((u32) maxfrag) << 8) | (((u32) curfrag)));
  232. msg->body[1] = cpu_to_le32(swlen);
  233. msg->body[2] = cpu_to_le32(kxfer.sw_id);
  234. msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
  235. msg->body[4] = cpu_to_le32(buffer.phys);
  236. osm_debug("swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
  237. status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
  238. if (status != -ETIMEDOUT)
  239. i2o_dma_free(&c->pdev->dev, &buffer);
  240. if (status != I2O_POST_WAIT_OK) {
  241. // it fails if you try and send frags out of order
  242. // and for some yet unknown reasons too
  243. osm_info("swdl failed, DetailedStatus = %d\n", status);
  244. return status;
  245. }
  246. return 0;
  247. };
  248. static int i2o_cfg_swul(unsigned long arg)
  249. {
  250. struct i2o_sw_xfer kxfer;
  251. struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
  252. unsigned char maxfrag = 0, curfrag = 1;
  253. struct i2o_dma buffer;
  254. struct i2o_message *msg;
  255. unsigned int status = 0, swlen = 0, fragsize = 8192;
  256. struct i2o_controller *c;
  257. int ret = 0;
  258. if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
  259. return -EFAULT;
  260. if (get_user(swlen, kxfer.swlen) < 0)
  261. return -EFAULT;
  262. if (get_user(maxfrag, kxfer.maxfrag) < 0)
  263. return -EFAULT;
  264. if (get_user(curfrag, kxfer.curfrag) < 0)
  265. return -EFAULT;
  266. if (curfrag == maxfrag)
  267. fragsize = swlen - (maxfrag - 1) * 8192;
  268. if (!kxfer.buf)
  269. return -EFAULT;
  270. c = i2o_find_iop(kxfer.iop);
  271. if (!c)
  272. return -ENXIO;
  273. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  274. if (IS_ERR(msg))
  275. return PTR_ERR(msg);
  276. if (i2o_dma_alloc(&c->pdev->dev, &buffer, fragsize)) {
  277. i2o_msg_nop(c, msg);
  278. return -ENOMEM;
  279. }
  280. msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_7);
  281. msg->u.head[1] =
  282. cpu_to_le32(I2O_CMD_SW_UPLOAD << 24 | HOST_TID << 12 | ADAPTER_TID);
  283. msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
  284. msg->u.head[3] = cpu_to_le32(0);
  285. msg->body[0] =
  286. cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.
  287. sw_type << 16 | (u32) maxfrag << 8 | (u32) curfrag);
  288. msg->body[1] = cpu_to_le32(swlen);
  289. msg->body[2] = cpu_to_le32(kxfer.sw_id);
  290. msg->body[3] = cpu_to_le32(0xD0000000 | fragsize);
  291. msg->body[4] = cpu_to_le32(buffer.phys);
  292. osm_debug("swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
  293. status = i2o_msg_post_wait_mem(c, msg, 60, &buffer);
  294. if (status != I2O_POST_WAIT_OK) {
  295. if (status != -ETIMEDOUT)
  296. i2o_dma_free(&c->pdev->dev, &buffer);
  297. osm_info("swul failed, DetailedStatus = %d\n", status);
  298. return status;
  299. }
  300. if (copy_to_user(kxfer.buf, buffer.virt, fragsize))
  301. ret = -EFAULT;
  302. i2o_dma_free(&c->pdev->dev, &buffer);
  303. return ret;
  304. }
  305. static int i2o_cfg_swdel(unsigned long arg)
  306. {
  307. struct i2o_controller *c;
  308. struct i2o_sw_xfer kxfer;
  309. struct i2o_sw_xfer __user *pxfer = (struct i2o_sw_xfer __user *)arg;
  310. struct i2o_message *msg;
  311. unsigned int swlen;
  312. int token;
  313. if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
  314. return -EFAULT;
  315. if (get_user(swlen, kxfer.swlen) < 0)
  316. return -EFAULT;
  317. c = i2o_find_iop(kxfer.iop);
  318. if (!c)
  319. return -ENXIO;
  320. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  321. if (IS_ERR(msg))
  322. return PTR_ERR(msg);
  323. msg->u.head[0] = cpu_to_le32(SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0);
  324. msg->u.head[1] =
  325. cpu_to_le32(I2O_CMD_SW_REMOVE << 24 | HOST_TID << 12 | ADAPTER_TID);
  326. msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
  327. msg->u.head[3] = cpu_to_le32(0);
  328. msg->body[0] =
  329. cpu_to_le32((u32) kxfer.flags << 24 | (u32) kxfer.sw_type << 16);
  330. msg->body[1] = cpu_to_le32(swlen);
  331. msg->body[2] = cpu_to_le32(kxfer.sw_id);
  332. token = i2o_msg_post_wait(c, msg, 10);
  333. if (token != I2O_POST_WAIT_OK) {
  334. osm_info("swdel failed, DetailedStatus = %d\n", token);
  335. return -ETIMEDOUT;
  336. }
  337. return 0;
  338. };
  339. static int i2o_cfg_validate(unsigned long arg)
  340. {
  341. int token;
  342. int iop = (int)arg;
  343. struct i2o_message *msg;
  344. struct i2o_controller *c;
  345. c = i2o_find_iop(iop);
  346. if (!c)
  347. return -ENXIO;
  348. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  349. if (IS_ERR(msg))
  350. return PTR_ERR(msg);
  351. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  352. msg->u.head[1] =
  353. cpu_to_le32(I2O_CMD_CONFIG_VALIDATE << 24 | HOST_TID << 12 | iop);
  354. msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
  355. msg->u.head[3] = cpu_to_le32(0);
  356. token = i2o_msg_post_wait(c, msg, 10);
  357. if (token != I2O_POST_WAIT_OK) {
  358. osm_info("Can't validate configuration, ErrorStatus = %d\n",
  359. token);
  360. return -ETIMEDOUT;
  361. }
  362. return 0;
  363. };
  364. static int i2o_cfg_evt_reg(unsigned long arg, struct file *fp)
  365. {
  366. struct i2o_message *msg;
  367. struct i2o_evt_id __user *pdesc = (struct i2o_evt_id __user *)arg;
  368. struct i2o_evt_id kdesc;
  369. struct i2o_controller *c;
  370. struct i2o_device *d;
  371. if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
  372. return -EFAULT;
  373. /* IOP exists? */
  374. c = i2o_find_iop(kdesc.iop);
  375. if (!c)
  376. return -ENXIO;
  377. /* Device exists? */
  378. d = i2o_iop_find_device(c, kdesc.tid);
  379. if (!d)
  380. return -ENODEV;
  381. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  382. if (IS_ERR(msg))
  383. return PTR_ERR(msg);
  384. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  385. msg->u.head[1] =
  386. cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 |
  387. kdesc.tid);
  388. msg->u.head[2] = cpu_to_le32(i2o_config_driver.context);
  389. msg->u.head[3] = cpu_to_le32(i2o_cntxt_list_add(c, fp->private_data));
  390. msg->body[0] = cpu_to_le32(kdesc.evt_mask);
  391. i2o_msg_post(c, msg);
  392. return 0;
  393. }
  394. static int i2o_cfg_evt_get(unsigned long arg, struct file *fp)
  395. {
  396. struct i2o_cfg_info *p = NULL;
  397. struct i2o_evt_get __user *uget = (struct i2o_evt_get __user *)arg;
  398. struct i2o_evt_get kget;
  399. unsigned long flags;
  400. for (p = open_files; p; p = p->next)
  401. if (p->q_id == (ulong) fp->private_data)
  402. break;
  403. if (!p->q_len)
  404. return -ENOENT;
  405. memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
  406. MODINC(p->q_out, I2O_EVT_Q_LEN);
  407. spin_lock_irqsave(&i2o_config_lock, flags);
  408. p->q_len--;
  409. kget.pending = p->q_len;
  410. kget.lost = p->q_lost;
  411. spin_unlock_irqrestore(&i2o_config_lock, flags);
  412. if (copy_to_user(uget, &kget, sizeof(struct i2o_evt_get)))
  413. return -EFAULT;
  414. return 0;
  415. }
  416. #ifdef CONFIG_COMPAT
  417. static int i2o_cfg_passthru32(struct file *file, unsigned cmnd,
  418. unsigned long arg)
  419. {
  420. struct i2o_cmd_passthru32 __user *cmd;
  421. struct i2o_controller *c;
  422. u32 __user *user_msg;
  423. u32 *reply = NULL;
  424. u32 __user *user_reply = NULL;
  425. u32 size = 0;
  426. u32 reply_size = 0;
  427. u32 rcode = 0;
  428. struct i2o_dma sg_list[SG_TABLESIZE];
  429. u32 sg_offset = 0;
  430. u32 sg_count = 0;
  431. u32 i = 0;
  432. u32 sg_index = 0;
  433. i2o_status_block *sb;
  434. struct i2o_message *msg;
  435. unsigned int iop;
  436. cmd = (struct i2o_cmd_passthru32 __user *)arg;
  437. if (get_user(iop, &cmd->iop) || get_user(i, &cmd->msg))
  438. return -EFAULT;
  439. user_msg = compat_ptr(i);
  440. c = i2o_find_iop(iop);
  441. if (!c) {
  442. osm_debug("controller %d not found\n", iop);
  443. return -ENXIO;
  444. }
  445. sb = c->status_block.virt;
  446. if (get_user(size, &user_msg[0])) {
  447. osm_warn("unable to get size!\n");
  448. return -EFAULT;
  449. }
  450. size = size >> 16;
  451. if (size > sb->inbound_frame_size) {
  452. osm_warn("size of message > inbound_frame_size");
  453. return -EFAULT;
  454. }
  455. user_reply = &user_msg[size];
  456. size <<= 2; // Convert to bytes
  457. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  458. if (IS_ERR(msg))
  459. return PTR_ERR(msg);
  460. rcode = -EFAULT;
  461. /* Copy in the user's I2O command */
  462. if (copy_from_user(msg, user_msg, size)) {
  463. osm_warn("unable to copy user message\n");
  464. goto out;
  465. }
  466. i2o_dump_message(msg);
  467. if (get_user(reply_size, &user_reply[0]) < 0)
  468. goto out;
  469. reply_size >>= 16;
  470. reply_size <<= 2;
  471. rcode = -ENOMEM;
  472. reply = kzalloc(reply_size, GFP_KERNEL);
  473. if (!reply) {
  474. printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
  475. c->name);
  476. goto out;
  477. }
  478. sg_offset = (msg->u.head[0] >> 4) & 0x0f;
  479. memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
  480. if (sg_offset) {
  481. struct sg_simple_element *sg;
  482. if (sg_offset * 4 >= size) {
  483. rcode = -EFAULT;
  484. goto cleanup;
  485. }
  486. // TODO 64bit fix
  487. sg = (struct sg_simple_element *)((&msg->u.head[0]) +
  488. sg_offset);
  489. sg_count =
  490. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  491. if (sg_count > SG_TABLESIZE) {
  492. printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
  493. c->name, sg_count);
  494. rcode = -EINVAL;
  495. goto cleanup;
  496. }
  497. for (i = 0; i < sg_count; i++) {
  498. int sg_size;
  499. struct i2o_dma *p;
  500. if (!(sg[i].flag_count & 0x10000000
  501. /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
  502. printk(KERN_DEBUG
  503. "%s:Bad SG element %d - not simple (%x)\n",
  504. c->name, i, sg[i].flag_count);
  505. rcode = -EINVAL;
  506. goto cleanup;
  507. }
  508. sg_size = sg[i].flag_count & 0xffffff;
  509. p = &(sg_list[sg_index]);
  510. /* Allocate memory for the transfer */
  511. if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
  512. printk(KERN_DEBUG
  513. "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  514. c->name, sg_size, i, sg_count);
  515. rcode = -ENOMEM;
  516. goto sg_list_cleanup;
  517. }
  518. sg_index++;
  519. /* Copy in the user's SG buffer if necessary */
  520. if (sg[i].
  521. flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
  522. // TODO 64bit fix
  523. if (copy_from_user
  524. (p->virt,
  525. (void __user *)(unsigned long)sg[i].
  526. addr_bus, sg_size)) {
  527. printk(KERN_DEBUG
  528. "%s: Could not copy SG buf %d FROM user\n",
  529. c->name, i);
  530. rcode = -EFAULT;
  531. goto sg_list_cleanup;
  532. }
  533. }
  534. //TODO 64bit fix
  535. sg[i].addr_bus = (u32) p->phys;
  536. }
  537. }
  538. rcode = i2o_msg_post_wait(c, msg, 60);
  539. msg = NULL;
  540. if (rcode) {
  541. reply[4] = ((u32) rcode) << 24;
  542. goto sg_list_cleanup;
  543. }
  544. if (sg_offset) {
  545. u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
  546. /* Copy back the Scatter Gather buffers back to user space */
  547. u32 j;
  548. // TODO 64bit fix
  549. struct sg_simple_element *sg;
  550. int sg_size;
  551. // re-acquire the original message to handle correctly the sg copy operation
  552. memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
  553. // get user msg size in u32s
  554. if (get_user(size, &user_msg[0])) {
  555. rcode = -EFAULT;
  556. goto sg_list_cleanup;
  557. }
  558. size = size >> 16;
  559. size *= 4;
  560. /* Copy in the user's I2O command */
  561. if (copy_from_user(rmsg, user_msg, size)) {
  562. rcode = -EFAULT;
  563. goto sg_list_cleanup;
  564. }
  565. sg_count =
  566. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  567. // TODO 64bit fix
  568. sg = (struct sg_simple_element *)(rmsg + sg_offset);
  569. for (j = 0; j < sg_count; j++) {
  570. /* Copy out the SG list to user's buffer if necessary */
  571. if (!
  572. (sg[j].
  573. flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
  574. sg_size = sg[j].flag_count & 0xffffff;
  575. // TODO 64bit fix
  576. if (copy_to_user
  577. ((void __user *)(u64) sg[j].addr_bus,
  578. sg_list[j].virt, sg_size)) {
  579. printk(KERN_WARNING
  580. "%s: Could not copy %p TO user %x\n",
  581. c->name, sg_list[j].virt,
  582. sg[j].addr_bus);
  583. rcode = -EFAULT;
  584. goto sg_list_cleanup;
  585. }
  586. }
  587. }
  588. }
  589. sg_list_cleanup:
  590. /* Copy back the reply to user space */
  591. if (reply_size) {
  592. // we wrote our own values for context - now restore the user supplied ones
  593. if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
  594. printk(KERN_WARNING
  595. "%s: Could not copy message context FROM user\n",
  596. c->name);
  597. rcode = -EFAULT;
  598. }
  599. if (copy_to_user(user_reply, reply, reply_size)) {
  600. printk(KERN_WARNING
  601. "%s: Could not copy reply TO user\n", c->name);
  602. rcode = -EFAULT;
  603. }
  604. }
  605. for (i = 0; i < sg_index; i++)
  606. i2o_dma_free(&c->pdev->dev, &sg_list[i]);
  607. cleanup:
  608. kfree(reply);
  609. out:
  610. if (msg)
  611. i2o_msg_nop(c, msg);
  612. return rcode;
  613. }
  614. static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
  615. unsigned long arg)
  616. {
  617. int ret;
  618. mutex_lock(&i2o_cfg_mutex);
  619. switch (cmd) {
  620. case I2OGETIOPS:
  621. ret = i2o_cfg_ioctl(file, cmd, arg);
  622. break;
  623. case I2OPASSTHRU32:
  624. ret = i2o_cfg_passthru32(file, cmd, arg);
  625. break;
  626. default:
  627. ret = -ENOIOCTLCMD;
  628. break;
  629. }
  630. mutex_unlock(&i2o_cfg_mutex);
  631. return ret;
  632. }
  633. #endif
  634. #ifdef CONFIG_I2O_EXT_ADAPTEC
  635. static int i2o_cfg_passthru(unsigned long arg)
  636. {
  637. struct i2o_cmd_passthru __user *cmd =
  638. (struct i2o_cmd_passthru __user *)arg;
  639. struct i2o_controller *c;
  640. u32 __user *user_msg;
  641. u32 *reply = NULL;
  642. u32 __user *user_reply = NULL;
  643. u32 size = 0;
  644. u32 reply_size = 0;
  645. u32 rcode = 0;
  646. struct i2o_dma sg_list[SG_TABLESIZE];
  647. u32 sg_offset = 0;
  648. u32 sg_count = 0;
  649. int sg_index = 0;
  650. u32 i = 0;
  651. i2o_status_block *sb;
  652. struct i2o_message *msg;
  653. unsigned int iop;
  654. if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
  655. return -EFAULT;
  656. c = i2o_find_iop(iop);
  657. if (!c) {
  658. osm_warn("controller %d not found\n", iop);
  659. return -ENXIO;
  660. }
  661. sb = c->status_block.virt;
  662. if (get_user(size, &user_msg[0]))
  663. return -EFAULT;
  664. size = size >> 16;
  665. if (size > sb->inbound_frame_size) {
  666. osm_warn("size of message > inbound_frame_size");
  667. return -EFAULT;
  668. }
  669. user_reply = &user_msg[size];
  670. size <<= 2; // Convert to bytes
  671. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  672. if (IS_ERR(msg))
  673. return PTR_ERR(msg);
  674. rcode = -EFAULT;
  675. /* Copy in the user's I2O command */
  676. if (copy_from_user(msg, user_msg, size))
  677. goto out;
  678. if (get_user(reply_size, &user_reply[0]) < 0)
  679. goto out;
  680. reply_size >>= 16;
  681. reply_size <<= 2;
  682. reply = kzalloc(reply_size, GFP_KERNEL);
  683. if (!reply) {
  684. printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
  685. c->name);
  686. rcode = -ENOMEM;
  687. goto out;
  688. }
  689. sg_offset = (msg->u.head[0] >> 4) & 0x0f;
  690. memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
  691. if (sg_offset) {
  692. struct sg_simple_element *sg;
  693. struct i2o_dma *p;
  694. if (sg_offset * 4 >= size) {
  695. rcode = -EFAULT;
  696. goto cleanup;
  697. }
  698. // TODO 64bit fix
  699. sg = (struct sg_simple_element *)((&msg->u.head[0]) +
  700. sg_offset);
  701. sg_count =
  702. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  703. if (sg_count > SG_TABLESIZE) {
  704. printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
  705. c->name, sg_count);
  706. rcode = -EINVAL;
  707. goto cleanup;
  708. }
  709. for (i = 0; i < sg_count; i++) {
  710. int sg_size;
  711. if (!(sg[i].flag_count & 0x10000000
  712. /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
  713. printk(KERN_DEBUG
  714. "%s:Bad SG element %d - not simple (%x)\n",
  715. c->name, i, sg[i].flag_count);
  716. rcode = -EINVAL;
  717. goto sg_list_cleanup;
  718. }
  719. sg_size = sg[i].flag_count & 0xffffff;
  720. p = &(sg_list[sg_index]);
  721. if (i2o_dma_alloc(&c->pdev->dev, p, sg_size)) {
  722. /* Allocate memory for the transfer */
  723. printk(KERN_DEBUG
  724. "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  725. c->name, sg_size, i, sg_count);
  726. rcode = -ENOMEM;
  727. goto sg_list_cleanup;
  728. }
  729. sg_index++;
  730. /* Copy in the user's SG buffer if necessary */
  731. if (sg[i].
  732. flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
  733. // TODO 64bit fix
  734. if (copy_from_user
  735. (p->virt, (void __user *)sg[i].addr_bus,
  736. sg_size)) {
  737. printk(KERN_DEBUG
  738. "%s: Could not copy SG buf %d FROM user\n",
  739. c->name, i);
  740. rcode = -EFAULT;
  741. goto sg_list_cleanup;
  742. }
  743. }
  744. sg[i].addr_bus = p->phys;
  745. }
  746. }
  747. rcode = i2o_msg_post_wait(c, msg, 60);
  748. msg = NULL;
  749. if (rcode) {
  750. reply[4] = ((u32) rcode) << 24;
  751. goto sg_list_cleanup;
  752. }
  753. if (sg_offset) {
  754. u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
  755. /* Copy back the Scatter Gather buffers back to user space */
  756. u32 j;
  757. // TODO 64bit fix
  758. struct sg_simple_element *sg;
  759. int sg_size;
  760. // re-acquire the original message to handle correctly the sg copy operation
  761. memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
  762. // get user msg size in u32s
  763. if (get_user(size, &user_msg[0])) {
  764. rcode = -EFAULT;
  765. goto sg_list_cleanup;
  766. }
  767. size = size >> 16;
  768. size *= 4;
  769. /* Copy in the user's I2O command */
  770. if (copy_from_user(rmsg, user_msg, size)) {
  771. rcode = -EFAULT;
  772. goto sg_list_cleanup;
  773. }
  774. sg_count =
  775. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  776. // TODO 64bit fix
  777. sg = (struct sg_simple_element *)(rmsg + sg_offset);
  778. for (j = 0; j < sg_count; j++) {
  779. /* Copy out the SG list to user's buffer if necessary */
  780. if (!
  781. (sg[j].
  782. flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
  783. sg_size = sg[j].flag_count & 0xffffff;
  784. // TODO 64bit fix
  785. if (copy_to_user
  786. ((void __user *)sg[j].addr_bus, sg_list[j].virt,
  787. sg_size)) {
  788. printk(KERN_WARNING
  789. "%s: Could not copy %p TO user %x\n",
  790. c->name, sg_list[j].virt,
  791. sg[j].addr_bus);
  792. rcode = -EFAULT;
  793. goto sg_list_cleanup;
  794. }
  795. }
  796. }
  797. }
  798. sg_list_cleanup:
  799. /* Copy back the reply to user space */
  800. if (reply_size) {
  801. // we wrote our own values for context - now restore the user supplied ones
  802. if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
  803. printk(KERN_WARNING
  804. "%s: Could not copy message context FROM user\n",
  805. c->name);
  806. rcode = -EFAULT;
  807. }
  808. if (copy_to_user(user_reply, reply, reply_size)) {
  809. printk(KERN_WARNING
  810. "%s: Could not copy reply TO user\n", c->name);
  811. rcode = -EFAULT;
  812. }
  813. }
  814. for (i = 0; i < sg_index; i++)
  815. i2o_dma_free(&c->pdev->dev, &sg_list[i]);
  816. cleanup:
  817. kfree(reply);
  818. out:
  819. if (msg)
  820. i2o_msg_nop(c, msg);
  821. return rcode;
  822. }
  823. #endif
  824. /*
  825. * IOCTL Handler
  826. */
  827. static long i2o_cfg_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
  828. {
  829. int ret;
  830. mutex_lock(&i2o_cfg_mutex);
  831. switch (cmd) {
  832. case I2OGETIOPS:
  833. ret = i2o_cfg_getiops(arg);
  834. break;
  835. case I2OHRTGET:
  836. ret = i2o_cfg_gethrt(arg);
  837. break;
  838. case I2OLCTGET:
  839. ret = i2o_cfg_getlct(arg);
  840. break;
  841. case I2OPARMSET:
  842. ret = i2o_cfg_parms(arg, I2OPARMSET);
  843. break;
  844. case I2OPARMGET:
  845. ret = i2o_cfg_parms(arg, I2OPARMGET);
  846. break;
  847. case I2OSWDL:
  848. ret = i2o_cfg_swdl(arg);
  849. break;
  850. case I2OSWUL:
  851. ret = i2o_cfg_swul(arg);
  852. break;
  853. case I2OSWDEL:
  854. ret = i2o_cfg_swdel(arg);
  855. break;
  856. case I2OVALIDATE:
  857. ret = i2o_cfg_validate(arg);
  858. break;
  859. case I2OEVTREG:
  860. ret = i2o_cfg_evt_reg(arg, fp);
  861. break;
  862. case I2OEVTGET:
  863. ret = i2o_cfg_evt_get(arg, fp);
  864. break;
  865. #ifdef CONFIG_I2O_EXT_ADAPTEC
  866. case I2OPASSTHRU:
  867. ret = i2o_cfg_passthru(arg);
  868. break;
  869. #endif
  870. default:
  871. osm_debug("unknown ioctl called!\n");
  872. ret = -EINVAL;
  873. }
  874. mutex_unlock(&i2o_cfg_mutex);
  875. return ret;
  876. }
  877. static int cfg_open(struct inode *inode, struct file *file)
  878. {
  879. struct i2o_cfg_info *tmp = kmalloc(sizeof(struct i2o_cfg_info),
  880. GFP_KERNEL);
  881. unsigned long flags;
  882. if (!tmp)
  883. return -ENOMEM;
  884. mutex_lock(&i2o_cfg_mutex);
  885. file->private_data = (void *)(i2o_cfg_info_id++);
  886. tmp->fp = file;
  887. tmp->fasync = NULL;
  888. tmp->q_id = (ulong) file->private_data;
  889. tmp->q_len = 0;
  890. tmp->q_in = 0;
  891. tmp->q_out = 0;
  892. tmp->q_lost = 0;
  893. tmp->next = open_files;
  894. spin_lock_irqsave(&i2o_config_lock, flags);
  895. open_files = tmp;
  896. spin_unlock_irqrestore(&i2o_config_lock, flags);
  897. mutex_unlock(&i2o_cfg_mutex);
  898. return 0;
  899. }
  900. static int cfg_fasync(int fd, struct file *fp, int on)
  901. {
  902. ulong id = (ulong) fp->private_data;
  903. struct i2o_cfg_info *p;
  904. int ret = -EBADF;
  905. mutex_lock(&i2o_cfg_mutex);
  906. for (p = open_files; p; p = p->next)
  907. if (p->q_id == id)
  908. break;
  909. if (p)
  910. ret = fasync_helper(fd, fp, on, &p->fasync);
  911. mutex_unlock(&i2o_cfg_mutex);
  912. return ret;
  913. }
  914. static int cfg_release(struct inode *inode, struct file *file)
  915. {
  916. ulong id = (ulong) file->private_data;
  917. struct i2o_cfg_info *p, **q;
  918. unsigned long flags;
  919. mutex_lock(&i2o_cfg_mutex);
  920. spin_lock_irqsave(&i2o_config_lock, flags);
  921. for (q = &open_files; (p = *q) != NULL; q = &p->next) {
  922. if (p->q_id == id) {
  923. *q = p->next;
  924. kfree(p);
  925. break;
  926. }
  927. }
  928. spin_unlock_irqrestore(&i2o_config_lock, flags);
  929. mutex_unlock(&i2o_cfg_mutex);
  930. return 0;
  931. }
  932. static const struct file_operations config_fops = {
  933. .owner = THIS_MODULE,
  934. .llseek = no_llseek,
  935. .unlocked_ioctl = i2o_cfg_ioctl,
  936. #ifdef CONFIG_COMPAT
  937. .compat_ioctl = i2o_cfg_compat_ioctl,
  938. #endif
  939. .open = cfg_open,
  940. .release = cfg_release,
  941. .fasync = cfg_fasync,
  942. };
  943. static struct miscdevice i2o_miscdev = {
  944. I2O_MINOR,
  945. "i2octl",
  946. &config_fops
  947. };
  948. static int __init i2o_config_old_init(void)
  949. {
  950. spin_lock_init(&i2o_config_lock);
  951. if (misc_register(&i2o_miscdev) < 0) {
  952. osm_err("can't register device.\n");
  953. return -EBUSY;
  954. }
  955. return 0;
  956. }
  957. static void i2o_config_old_exit(void)
  958. {
  959. misc_deregister(&i2o_miscdev);
  960. }
  961. MODULE_AUTHOR("Red Hat Software");