i2o_config.c 26 KB

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