i2o_config.c 26 KB

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