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