i2o_config.c 26 KB

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