i2o_config.c 26 KB

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