i2o_config.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. file->private_data = (void *)(i2o_cfg_info_id++);
  887. tmp->fp = file;
  888. tmp->fasync = NULL;
  889. tmp->q_id = (ulong) file->private_data;
  890. tmp->q_len = 0;
  891. tmp->q_in = 0;
  892. tmp->q_out = 0;
  893. tmp->q_lost = 0;
  894. tmp->next = open_files;
  895. spin_lock_irqsave(&i2o_config_lock, flags);
  896. open_files = tmp;
  897. spin_unlock_irqrestore(&i2o_config_lock, flags);
  898. return 0;
  899. }
  900. static int cfg_fasync(int fd, struct file *fp, int on)
  901. {
  902. ulong id = (ulong) fp->private_data;
  903. struct i2o_cfg_info *p;
  904. for (p = open_files; p; p = p->next)
  905. if (p->q_id == id)
  906. break;
  907. if (!p)
  908. return -EBADF;
  909. return fasync_helper(fd, fp, on, &p->fasync);
  910. }
  911. static int cfg_release(struct inode *inode, struct file *file)
  912. {
  913. ulong id = (ulong) file->private_data;
  914. struct i2o_cfg_info *p1, *p2;
  915. unsigned long flags;
  916. lock_kernel();
  917. p1 = p2 = NULL;
  918. spin_lock_irqsave(&i2o_config_lock, flags);
  919. for (p1 = open_files; p1;) {
  920. if (p1->q_id == id) {
  921. if (p1->fasync)
  922. cfg_fasync(-1, file, 0);
  923. if (p2)
  924. p2->next = p1->next;
  925. else
  926. open_files = p1->next;
  927. kfree(p1);
  928. break;
  929. }
  930. p2 = p1;
  931. p1 = p1->next;
  932. }
  933. spin_unlock_irqrestore(&i2o_config_lock, flags);
  934. unlock_kernel();
  935. return 0;
  936. }
  937. static const struct file_operations config_fops = {
  938. .owner = THIS_MODULE,
  939. .llseek = no_llseek,
  940. .ioctl = i2o_cfg_ioctl,
  941. #ifdef CONFIG_COMPAT
  942. .compat_ioctl = i2o_cfg_compat_ioctl,
  943. #endif
  944. .open = cfg_open,
  945. .release = cfg_release,
  946. .fasync = cfg_fasync,
  947. };
  948. static struct miscdevice i2o_miscdev = {
  949. I2O_MINOR,
  950. "i2octl",
  951. &config_fops
  952. };
  953. static int __init i2o_config_old_init(void)
  954. {
  955. spin_lock_init(&i2o_config_lock);
  956. if (misc_register(&i2o_miscdev) < 0) {
  957. osm_err("can't register device.\n");
  958. return -EBUSY;
  959. }
  960. return 0;
  961. }
  962. static void i2o_config_old_exit(void)
  963. {
  964. misc_deregister(&i2o_miscdev);
  965. }
  966. MODULE_AUTHOR("Red Hat Software");