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