i2o_config.c 26 KB

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