i2o_config.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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, GFP_KERNEL)) {
  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, GFP_KERNEL)) {
  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
  511. (&c->pdev->dev, p, sg_size,
  512. PCI_DMA_BIDIRECTIONAL)) {
  513. printk(KERN_DEBUG
  514. "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  515. c->name, sg_size, i, sg_count);
  516. rcode = -ENOMEM;
  517. goto sg_list_cleanup;
  518. }
  519. sg_index++;
  520. /* Copy in the user's SG buffer if necessary */
  521. if (sg[i].
  522. flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
  523. // TODO 64bit fix
  524. if (copy_from_user
  525. (p->virt,
  526. (void __user *)(unsigned long)sg[i].
  527. addr_bus, sg_size)) {
  528. printk(KERN_DEBUG
  529. "%s: Could not copy SG buf %d FROM user\n",
  530. c->name, i);
  531. rcode = -EFAULT;
  532. goto sg_list_cleanup;
  533. }
  534. }
  535. //TODO 64bit fix
  536. sg[i].addr_bus = (u32) p->phys;
  537. }
  538. }
  539. rcode = i2o_msg_post_wait(c, msg, 60);
  540. msg = NULL;
  541. if (rcode) {
  542. reply[4] = ((u32) rcode) << 24;
  543. goto sg_list_cleanup;
  544. }
  545. if (sg_offset) {
  546. u32 rmsg[I2O_OUTBOUND_MSG_FRAME_SIZE];
  547. /* Copy back the Scatter Gather buffers back to user space */
  548. u32 j;
  549. // TODO 64bit fix
  550. struct sg_simple_element *sg;
  551. int sg_size;
  552. // re-acquire the original message to handle correctly the sg copy operation
  553. memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
  554. // get user msg size in u32s
  555. if (get_user(size, &user_msg[0])) {
  556. rcode = -EFAULT;
  557. goto sg_list_cleanup;
  558. }
  559. size = size >> 16;
  560. size *= 4;
  561. /* Copy in the user's I2O command */
  562. if (copy_from_user(rmsg, user_msg, size)) {
  563. rcode = -EFAULT;
  564. goto sg_list_cleanup;
  565. }
  566. sg_count =
  567. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  568. // TODO 64bit fix
  569. sg = (struct sg_simple_element *)(rmsg + sg_offset);
  570. for (j = 0; j < sg_count; j++) {
  571. /* Copy out the SG list to user's buffer if necessary */
  572. if (!
  573. (sg[j].
  574. flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
  575. sg_size = sg[j].flag_count & 0xffffff;
  576. // TODO 64bit fix
  577. if (copy_to_user
  578. ((void __user *)(u64) sg[j].addr_bus,
  579. sg_list[j].virt, sg_size)) {
  580. printk(KERN_WARNING
  581. "%s: Could not copy %p TO user %x\n",
  582. c->name, sg_list[j].virt,
  583. sg[j].addr_bus);
  584. rcode = -EFAULT;
  585. goto sg_list_cleanup;
  586. }
  587. }
  588. }
  589. }
  590. sg_list_cleanup:
  591. /* Copy back the reply to user space */
  592. if (reply_size) {
  593. // we wrote our own values for context - now restore the user supplied ones
  594. if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
  595. printk(KERN_WARNING
  596. "%s: Could not copy message context FROM user\n",
  597. c->name);
  598. rcode = -EFAULT;
  599. }
  600. if (copy_to_user(user_reply, reply, reply_size)) {
  601. printk(KERN_WARNING
  602. "%s: Could not copy reply TO user\n", c->name);
  603. rcode = -EFAULT;
  604. }
  605. }
  606. for (i = 0; i < sg_index; i++)
  607. i2o_dma_free(&c->pdev->dev, &sg_list[i]);
  608. cleanup:
  609. kfree(reply);
  610. out:
  611. if (msg)
  612. i2o_msg_nop(c, msg);
  613. return rcode;
  614. }
  615. static long i2o_cfg_compat_ioctl(struct file *file, unsigned cmd,
  616. unsigned long arg)
  617. {
  618. int ret;
  619. lock_kernel();
  620. switch (cmd) {
  621. case I2OGETIOPS:
  622. ret = i2o_cfg_ioctl(NULL, file, cmd, arg);
  623. break;
  624. case I2OPASSTHRU32:
  625. ret = i2o_cfg_passthru32(file, cmd, arg);
  626. break;
  627. default:
  628. ret = -ENOIOCTLCMD;
  629. break;
  630. }
  631. unlock_kernel();
  632. return ret;
  633. }
  634. #endif
  635. #ifdef CONFIG_I2O_EXT_ADAPTEC
  636. static int i2o_cfg_passthru(unsigned long arg)
  637. {
  638. struct i2o_cmd_passthru __user *cmd =
  639. (struct i2o_cmd_passthru __user *)arg;
  640. struct i2o_controller *c;
  641. u32 __user *user_msg;
  642. u32 *reply = NULL;
  643. u32 __user *user_reply = NULL;
  644. u32 size = 0;
  645. u32 reply_size = 0;
  646. u32 rcode = 0;
  647. void *sg_list[SG_TABLESIZE];
  648. u32 sg_offset = 0;
  649. u32 sg_count = 0;
  650. int sg_index = 0;
  651. u32 i = 0;
  652. void *p = NULL;
  653. i2o_status_block *sb;
  654. struct i2o_message *msg;
  655. unsigned int iop;
  656. if (get_user(iop, &cmd->iop) || get_user(user_msg, &cmd->msg))
  657. return -EFAULT;
  658. c = i2o_find_iop(iop);
  659. if (!c) {
  660. osm_warn("controller %d not found\n", iop);
  661. return -ENXIO;
  662. }
  663. sb = c->status_block.virt;
  664. if (get_user(size, &user_msg[0]))
  665. return -EFAULT;
  666. size = size >> 16;
  667. if (size > sb->inbound_frame_size) {
  668. osm_warn("size of message > inbound_frame_size");
  669. return -EFAULT;
  670. }
  671. user_reply = &user_msg[size];
  672. size <<= 2; // Convert to bytes
  673. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  674. if (IS_ERR(msg))
  675. return PTR_ERR(msg);
  676. rcode = -EFAULT;
  677. /* Copy in the user's I2O command */
  678. if (copy_from_user(msg, user_msg, size))
  679. goto out;
  680. if (get_user(reply_size, &user_reply[0]) < 0)
  681. goto out;
  682. reply_size >>= 16;
  683. reply_size <<= 2;
  684. reply = kzalloc(reply_size, GFP_KERNEL);
  685. if (!reply) {
  686. printk(KERN_WARNING "%s: Could not allocate reply buffer\n",
  687. c->name);
  688. rcode = -ENOMEM;
  689. goto out;
  690. }
  691. sg_offset = (msg->u.head[0] >> 4) & 0x0f;
  692. memset(sg_list, 0, sizeof(sg_list[0]) * SG_TABLESIZE);
  693. if (sg_offset) {
  694. struct sg_simple_element *sg;
  695. if (sg_offset * 4 >= size) {
  696. rcode = -EFAULT;
  697. goto cleanup;
  698. }
  699. // TODO 64bit fix
  700. sg = (struct sg_simple_element *)((&msg->u.head[0]) +
  701. sg_offset);
  702. sg_count =
  703. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  704. if (sg_count > SG_TABLESIZE) {
  705. printk(KERN_DEBUG "%s:IOCTL SG List too large (%u)\n",
  706. c->name, sg_count);
  707. rcode = -EINVAL;
  708. goto cleanup;
  709. }
  710. for (i = 0; i < sg_count; i++) {
  711. int sg_size;
  712. if (!(sg[i].flag_count & 0x10000000
  713. /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT */ )) {
  714. printk(KERN_DEBUG
  715. "%s:Bad SG element %d - not simple (%x)\n",
  716. c->name, i, sg[i].flag_count);
  717. rcode = -EINVAL;
  718. goto sg_list_cleanup;
  719. }
  720. sg_size = sg[i].flag_count & 0xffffff;
  721. /* Allocate memory for the transfer */
  722. p = kmalloc(sg_size, GFP_KERNEL);
  723. if (!p) {
  724. printk(KERN_DEBUG
  725. "%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
  726. c->name, sg_size, i, sg_count);
  727. rcode = -ENOMEM;
  728. goto sg_list_cleanup;
  729. }
  730. sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
  731. /* Copy in the user's SG buffer if necessary */
  732. if (sg[i].
  733. flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR */ ) {
  734. // TODO 64bit fix
  735. if (copy_from_user
  736. (p, (void __user *)sg[i].addr_bus,
  737. sg_size)) {
  738. printk(KERN_DEBUG
  739. "%s: Could not copy SG buf %d FROM user\n",
  740. c->name, i);
  741. rcode = -EFAULT;
  742. goto sg_list_cleanup;
  743. }
  744. }
  745. //TODO 64bit fix
  746. sg[i].addr_bus = virt_to_bus(p);
  747. }
  748. }
  749. rcode = i2o_msg_post_wait(c, msg, 60);
  750. msg = NULL;
  751. if (rcode) {
  752. reply[4] = ((u32) rcode) << 24;
  753. goto sg_list_cleanup;
  754. }
  755. if (sg_offset) {
  756. u32 rmsg[128];
  757. /* Copy back the Scatter Gather buffers back to user space */
  758. u32 j;
  759. // TODO 64bit fix
  760. struct sg_simple_element *sg;
  761. int sg_size;
  762. // re-acquire the original message to handle correctly the sg copy operation
  763. memset(&rmsg, 0, I2O_OUTBOUND_MSG_FRAME_SIZE * 4);
  764. // get user msg size in u32s
  765. if (get_user(size, &user_msg[0])) {
  766. rcode = -EFAULT;
  767. goto sg_list_cleanup;
  768. }
  769. size = size >> 16;
  770. size *= 4;
  771. /* Copy in the user's I2O command */
  772. if (copy_from_user(rmsg, user_msg, size)) {
  773. rcode = -EFAULT;
  774. goto sg_list_cleanup;
  775. }
  776. sg_count =
  777. (size - sg_offset * 4) / sizeof(struct sg_simple_element);
  778. // TODO 64bit fix
  779. sg = (struct sg_simple_element *)(rmsg + sg_offset);
  780. for (j = 0; j < sg_count; j++) {
  781. /* Copy out the SG list to user's buffer if necessary */
  782. if (!
  783. (sg[j].
  784. flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR */ )) {
  785. sg_size = sg[j].flag_count & 0xffffff;
  786. // TODO 64bit fix
  787. if (copy_to_user
  788. ((void __user *)sg[j].addr_bus, sg_list[j],
  789. sg_size)) {
  790. printk(KERN_WARNING
  791. "%s: Could not copy %p TO user %x\n",
  792. c->name, sg_list[j],
  793. sg[j].addr_bus);
  794. rcode = -EFAULT;
  795. goto sg_list_cleanup;
  796. }
  797. }
  798. }
  799. }
  800. sg_list_cleanup:
  801. /* Copy back the reply to user space */
  802. if (reply_size) {
  803. // we wrote our own values for context - now restore the user supplied ones
  804. if (copy_from_user(reply + 2, user_msg + 2, sizeof(u32) * 2)) {
  805. printk(KERN_WARNING
  806. "%s: Could not copy message context FROM user\n",
  807. c->name);
  808. rcode = -EFAULT;
  809. }
  810. if (copy_to_user(user_reply, reply, reply_size)) {
  811. printk(KERN_WARNING
  812. "%s: Could not copy reply TO user\n", c->name);
  813. rcode = -EFAULT;
  814. }
  815. }
  816. for (i = 0; i < sg_index; i++)
  817. kfree(sg_list[i]);
  818. cleanup:
  819. kfree(reply);
  820. out:
  821. if (msg)
  822. i2o_msg_nop(c, msg);
  823. return rcode;
  824. }
  825. #endif
  826. /*
  827. * IOCTL Handler
  828. */
  829. static int i2o_cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
  830. unsigned long arg)
  831. {
  832. int ret;
  833. switch (cmd) {
  834. case I2OGETIOPS:
  835. ret = i2o_cfg_getiops(arg);
  836. break;
  837. case I2OHRTGET:
  838. ret = i2o_cfg_gethrt(arg);
  839. break;
  840. case I2OLCTGET:
  841. ret = i2o_cfg_getlct(arg);
  842. break;
  843. case I2OPARMSET:
  844. ret = i2o_cfg_parms(arg, I2OPARMSET);
  845. break;
  846. case I2OPARMGET:
  847. ret = i2o_cfg_parms(arg, I2OPARMGET);
  848. break;
  849. case I2OSWDL:
  850. ret = i2o_cfg_swdl(arg);
  851. break;
  852. case I2OSWUL:
  853. ret = i2o_cfg_swul(arg);
  854. break;
  855. case I2OSWDEL:
  856. ret = i2o_cfg_swdel(arg);
  857. break;
  858. case I2OVALIDATE:
  859. ret = i2o_cfg_validate(arg);
  860. break;
  861. case I2OEVTREG:
  862. ret = i2o_cfg_evt_reg(arg, fp);
  863. break;
  864. case I2OEVTGET:
  865. ret = i2o_cfg_evt_get(arg, fp);
  866. break;
  867. #ifdef CONFIG_I2O_EXT_ADAPTEC
  868. case I2OPASSTHRU:
  869. ret = i2o_cfg_passthru(arg);
  870. break;
  871. #endif
  872. default:
  873. osm_debug("unknown ioctl called!\n");
  874. ret = -EINVAL;
  875. }
  876. return ret;
  877. }
  878. static int cfg_open(struct inode *inode, struct file *file)
  879. {
  880. struct i2o_cfg_info *tmp =
  881. (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info),
  882. GFP_KERNEL);
  883. unsigned long flags;
  884. if (!tmp)
  885. return -ENOMEM;
  886. lock_kernel();
  887. file->private_data = (void *)(i2o_cfg_info_id++);
  888. tmp->fp = file;
  889. tmp->fasync = NULL;
  890. tmp->q_id = (ulong) file->private_data;
  891. tmp->q_len = 0;
  892. tmp->q_in = 0;
  893. tmp->q_out = 0;
  894. tmp->q_lost = 0;
  895. tmp->next = open_files;
  896. spin_lock_irqsave(&i2o_config_lock, flags);
  897. open_files = tmp;
  898. spin_unlock_irqrestore(&i2o_config_lock, flags);
  899. unlock_kernel();
  900. return 0;
  901. }
  902. static int cfg_fasync(int fd, struct file *fp, int on)
  903. {
  904. ulong id = (ulong) fp->private_data;
  905. struct i2o_cfg_info *p;
  906. int ret = -EBADF;
  907. lock_kernel();
  908. for (p = open_files; p; p = p->next)
  909. if (p->q_id == id)
  910. break;
  911. if (p)
  912. ret = fasync_helper(fd, fp, on, &p->fasync);
  913. unlock_kernel();
  914. return ret;
  915. }
  916. static int cfg_release(struct inode *inode, struct file *file)
  917. {
  918. ulong id = (ulong) file->private_data;
  919. struct i2o_cfg_info *p1, *p2;
  920. unsigned long flags;
  921. lock_kernel();
  922. p1 = p2 = NULL;
  923. spin_lock_irqsave(&i2o_config_lock, flags);
  924. for (p1 = open_files; p1;) {
  925. if (p1->q_id == id) {
  926. if (p1->fasync)
  927. cfg_fasync(-1, file, 0);
  928. if (p2)
  929. p2->next = p1->next;
  930. else
  931. open_files = p1->next;
  932. kfree(p1);
  933. break;
  934. }
  935. p2 = p1;
  936. p1 = p1->next;
  937. }
  938. spin_unlock_irqrestore(&i2o_config_lock, flags);
  939. unlock_kernel();
  940. return 0;
  941. }
  942. static const struct file_operations config_fops = {
  943. .owner = THIS_MODULE,
  944. .llseek = no_llseek,
  945. .ioctl = i2o_cfg_ioctl,
  946. #ifdef CONFIG_COMPAT
  947. .compat_ioctl = i2o_cfg_compat_ioctl,
  948. #endif
  949. .open = cfg_open,
  950. .release = cfg_release,
  951. .fasync = cfg_fasync,
  952. };
  953. static struct miscdevice i2o_miscdev = {
  954. I2O_MINOR,
  955. "i2octl",
  956. &config_fops
  957. };
  958. static int __init i2o_config_old_init(void)
  959. {
  960. spin_lock_init(&i2o_config_lock);
  961. if (misc_register(&i2o_miscdev) < 0) {
  962. osm_err("can't register device.\n");
  963. return -EBUSY;
  964. }
  965. return 0;
  966. }
  967. static void i2o_config_old_exit(void)
  968. {
  969. misc_deregister(&i2o_miscdev);
  970. }
  971. MODULE_AUTHOR("Red Hat Software");