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