i2o_config.c 26 KB

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