exec-osm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Executive OSM
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * A lot of the I2O message side code from this is taken from the Red
  14. * Creek RCPCI45 adapter driver by Red Creek Communications
  15. *
  16. * Fixes/additions:
  17. * Philipp Rumpf
  18. * Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
  19. * Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
  20. * Deepak Saxena <deepak@plexity.net>
  21. * Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
  22. * Alan Cox <alan@redhat.com>:
  23. * Ported to Linux 2.5.
  24. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  25. * Minor fixes for 2.6.
  26. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  27. * Support for sysfs included.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/i2o.h>
  31. #include <linux/delay.h>
  32. #define OSM_NAME "exec-osm"
  33. struct i2o_driver i2o_exec_driver;
  34. static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind);
  35. /* Module internal functions from other sources */
  36. extern int i2o_device_parse_lct(struct i2o_controller *);
  37. /* global wait list for POST WAIT */
  38. static LIST_HEAD(i2o_exec_wait_list);
  39. /* Wait struct needed for POST WAIT */
  40. struct i2o_exec_wait {
  41. wait_queue_head_t *wq; /* Pointer to Wait queue */
  42. struct i2o_dma dma; /* DMA buffers to free on failure */
  43. u32 tcntxt; /* transaction context from reply */
  44. int complete; /* 1 if reply received otherwise 0 */
  45. u32 m; /* message id */
  46. struct i2o_message __iomem *msg; /* pointer to the reply message */
  47. struct list_head list; /* node in global wait list */
  48. };
  49. /* Exec OSM class handling definition */
  50. static struct i2o_class_id i2o_exec_class_id[] = {
  51. {I2O_CLASS_EXECUTIVE},
  52. {I2O_CLASS_END}
  53. };
  54. /**
  55. * i2o_exec_wait_alloc - Allocate a i2o_exec_wait struct an initialize it
  56. *
  57. * Allocate the i2o_exec_wait struct and initialize the wait.
  58. *
  59. * Returns i2o_exec_wait pointer on success or negative error code on
  60. * failure.
  61. */
  62. static struct i2o_exec_wait *i2o_exec_wait_alloc(void)
  63. {
  64. struct i2o_exec_wait *wait;
  65. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  66. if (!wait)
  67. return ERR_PTR(-ENOMEM);
  68. memset(wait, 0, sizeof(*wait));
  69. INIT_LIST_HEAD(&wait->list);
  70. return wait;
  71. };
  72. /**
  73. * i2o_exec_wait_free - Free a i2o_exec_wait struct
  74. * @i2o_exec_wait: I2O wait data which should be cleaned up
  75. */
  76. static void i2o_exec_wait_free(struct i2o_exec_wait *wait)
  77. {
  78. kfree(wait);
  79. };
  80. /**
  81. * i2o_msg_post_wait_mem - Post and wait a message with DMA buffers
  82. * @c: controller
  83. * @m: message to post
  84. * @timeout: time in seconds to wait
  85. * @dma: i2o_dma struct of the DMA buffer to free on failure
  86. *
  87. * This API allows an OSM to post a message and then be told whether or
  88. * not the system received a successful reply. If the message times out
  89. * then the value '-ETIMEDOUT' is returned. This is a special case. In
  90. * this situation the message may (should) complete at an indefinite time
  91. * in the future. When it completes it will use the memory buffer
  92. * attached to the request. If -ETIMEDOUT is returned then the memory
  93. * buffer must not be freed. Instead the event completion will free them
  94. * for you. In all other cases the buffer are your problem.
  95. *
  96. * Returns 0 on success or negative error code on failure.
  97. */
  98. int i2o_msg_post_wait_mem(struct i2o_controller *c, u32 m, unsigned long
  99. timeout, struct i2o_dma *dma)
  100. {
  101. DECLARE_WAIT_QUEUE_HEAD(wq);
  102. struct i2o_exec_wait *wait;
  103. static u32 tcntxt = 0x80000000;
  104. struct i2o_message __iomem *msg = c->in_queue.virt + m;
  105. int rc = 0;
  106. wait = i2o_exec_wait_alloc();
  107. if (!wait)
  108. return -ENOMEM;
  109. if (tcntxt == 0xffffffff)
  110. tcntxt = 0x80000000;
  111. if (dma)
  112. wait->dma = *dma;
  113. /*
  114. * Fill in the message initiator context and transaction context.
  115. * We will only use transaction contexts >= 0x80000000 for POST WAIT,
  116. * so we could find a POST WAIT reply easier in the reply handler.
  117. */
  118. writel(i2o_exec_driver.context, &msg->u.s.icntxt);
  119. wait->tcntxt = tcntxt++;
  120. writel(wait->tcntxt, &msg->u.s.tcntxt);
  121. /*
  122. * Post the message to the controller. At some point later it will
  123. * return. If we time out before it returns then complete will be zero.
  124. */
  125. i2o_msg_post(c, m);
  126. if (!wait->complete) {
  127. wait->wq = &wq;
  128. /*
  129. * we add elements add the head, because if a entry in the list
  130. * will never be removed, we have to iterate over it every time
  131. */
  132. list_add(&wait->list, &i2o_exec_wait_list);
  133. wait_event_interruptible_timeout(wq, wait->complete,
  134. timeout * HZ);
  135. wait->wq = NULL;
  136. }
  137. barrier();
  138. if (wait->complete) {
  139. if (readl(&wait->msg->body[0]) >> 24)
  140. rc = readl(&wait->msg->body[0]) & 0xff;
  141. i2o_flush_reply(c, wait->m);
  142. i2o_exec_wait_free(wait);
  143. } else {
  144. /*
  145. * We cannot remove it now. This is important. When it does
  146. * terminate (which it must do if the controller has not
  147. * died...) then it will otherwise scribble on stuff.
  148. *
  149. * FIXME: try abort message
  150. */
  151. if (dma)
  152. dma->virt = NULL;
  153. rc = -ETIMEDOUT;
  154. }
  155. return rc;
  156. };
  157. /**
  158. * i2o_msg_post_wait_complete - Reply to a i2o_msg_post request from IOP
  159. * @c: I2O controller which answers
  160. * @m: message id
  161. * @msg: pointer to the I2O reply message
  162. *
  163. * This function is called in interrupt context only. If the reply reached
  164. * before the timeout, the i2o_exec_wait struct is filled with the message
  165. * and the task will be waked up. The task is now responsible for returning
  166. * the message m back to the controller! If the message reaches us after
  167. * the timeout clean up the i2o_exec_wait struct (including allocated
  168. * DMA buffer).
  169. *
  170. * Return 0 on success and if the message m should not be given back to the
  171. * I2O controller, or >0 on success and if the message should be given back
  172. * afterwords. Returns negative error code on failure. In this case the
  173. * message must also be given back to the controller.
  174. */
  175. static int i2o_msg_post_wait_complete(struct i2o_controller *c, u32 m,
  176. struct i2o_message __iomem *msg)
  177. {
  178. struct i2o_exec_wait *wait, *tmp;
  179. static spinlock_t lock;
  180. int rc = 1;
  181. u32 context;
  182. spin_lock_init(&lock);
  183. context = readl(&msg->u.s.tcntxt);
  184. /*
  185. * We need to search through the i2o_exec_wait_list to see if the given
  186. * message is still outstanding. If not, it means that the IOP took
  187. * longer to respond to the message than we had allowed and timer has
  188. * already expired. Not much we can do about that except log it for
  189. * debug purposes, increase timeout, and recompile.
  190. */
  191. spin_lock(&lock);
  192. list_for_each_entry_safe(wait, tmp, &i2o_exec_wait_list, list) {
  193. if (wait->tcntxt == context) {
  194. list_del(&wait->list);
  195. wait->m = m;
  196. wait->msg = msg;
  197. wait->complete = 1;
  198. barrier();
  199. if (wait->wq) {
  200. wake_up_interruptible(wait->wq);
  201. rc = 0;
  202. } else {
  203. struct device *dev;
  204. dev = &c->pdev->dev;
  205. pr_debug("%s: timedout reply received!\n",
  206. c->name);
  207. i2o_dma_free(dev, &wait->dma);
  208. i2o_exec_wait_free(wait);
  209. rc = -1;
  210. }
  211. spin_unlock(&lock);
  212. return rc;
  213. }
  214. }
  215. spin_unlock(&lock);
  216. pr_debug("%s: Bogus reply in POST WAIT (tr-context: %08x)!\n", c->name,
  217. context);
  218. return -1;
  219. };
  220. /**
  221. * i2o_exec_probe - Called if a new I2O device (executive class) appears
  222. * @dev: I2O device which should be probed
  223. *
  224. * Registers event notification for every event from Executive device. The
  225. * return is always 0, because we want all devices of class Executive.
  226. *
  227. * Returns 0 on success.
  228. */
  229. static int i2o_exec_probe(struct device *dev)
  230. {
  231. struct i2o_device *i2o_dev = to_i2o_device(dev);
  232. i2o_event_register(i2o_dev, &i2o_exec_driver, 0, 0xffffffff);
  233. i2o_dev->iop->exec = i2o_dev;
  234. return 0;
  235. };
  236. /**
  237. * i2o_exec_remove - Called on I2O device removal
  238. * @dev: I2O device which was removed
  239. *
  240. * Unregisters event notification from Executive I2O device.
  241. *
  242. * Returns 0 on success.
  243. */
  244. static int i2o_exec_remove(struct device *dev)
  245. {
  246. i2o_event_register(to_i2o_device(dev), &i2o_exec_driver, 0, 0);
  247. return 0;
  248. };
  249. /**
  250. * i2o_exec_lct_modified - Called on LCT NOTIFY reply
  251. * @c: I2O controller on which the LCT has modified
  252. *
  253. * This function handles asynchronus LCT NOTIFY replies. It parses the
  254. * new LCT and if the buffer for the LCT was to small sends a LCT NOTIFY
  255. * again.
  256. */
  257. static void i2o_exec_lct_modified(struct i2o_controller *c)
  258. {
  259. if (i2o_device_parse_lct(c) == -EAGAIN)
  260. i2o_exec_lct_notify(c, 0);
  261. };
  262. /**
  263. * i2o_exec_reply - I2O Executive reply handler
  264. * @c: I2O controller from which the reply comes
  265. * @m: message id
  266. * @msg: pointer to the I2O reply message
  267. *
  268. * This function is always called from interrupt context. If a POST WAIT
  269. * reply was received, pass it to the complete function. If a LCT NOTIFY
  270. * reply was received, a new event is created to handle the update.
  271. *
  272. * Returns 0 on success and if the reply should not be flushed or > 0
  273. * on success and if the reply should be flushed. Returns negative error
  274. * code on failure and if the reply should be flushed.
  275. */
  276. static int i2o_exec_reply(struct i2o_controller *c, u32 m,
  277. struct i2o_message *msg)
  278. {
  279. if (le32_to_cpu(msg->u.head[0]) & MSG_FAIL) { // Fail bit is set
  280. struct i2o_message __iomem *pmsg; /* preserved message */
  281. u32 pm;
  282. pm = le32_to_cpu(msg->body[3]);
  283. pmsg = i2o_msg_in_to_virt(c, pm);
  284. i2o_report_status(KERN_INFO, "i2o_core", msg);
  285. /* Release the preserved msg by resubmitting it as a NOP */
  286. i2o_msg_nop(c, pm);
  287. /* If reply to i2o_post_wait failed, return causes a timeout */
  288. return -1;
  289. }
  290. if (le32_to_cpu(msg->u.s.tcntxt) & 0x80000000)
  291. return i2o_msg_post_wait_complete(c, m, msg);
  292. if ((le32_to_cpu(msg->u.head[1]) >> 24) == I2O_CMD_LCT_NOTIFY) {
  293. struct work_struct *work;
  294. pr_debug("%s: LCT notify received\n", c->name);
  295. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  296. if (!work)
  297. return -ENOMEM;
  298. INIT_WORK(work, (void (*)(void *))i2o_exec_lct_modified, c);
  299. queue_work(i2o_exec_driver.event_queue, work);
  300. return 1;
  301. }
  302. /*
  303. * If this happens, we want to dump the message to the syslog so
  304. * it can be sent back to the card manufacturer by the end user
  305. * to aid in debugging.
  306. *
  307. */
  308. printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
  309. "Message dumped to syslog\n", c->name);
  310. i2o_dump_message(msg);
  311. return -EFAULT;
  312. }
  313. /**
  314. * i2o_exec_event - Event handling function
  315. * @evt: Event which occurs
  316. *
  317. * Handles events send by the Executive device. At the moment does not do
  318. * anything useful.
  319. */
  320. static void i2o_exec_event(struct i2o_event *evt)
  321. {
  322. osm_info("Event received from device: %d\n",
  323. evt->i2o_dev->lct_data.tid);
  324. kfree(evt);
  325. };
  326. /**
  327. * i2o_exec_lct_get - Get the IOP's Logical Configuration Table
  328. * @c: I2O controller from which the LCT should be fetched
  329. *
  330. * Send a LCT NOTIFY request to the controller, and wait
  331. * I2O_TIMEOUT_LCT_GET seconds until arrival of response. If the LCT is
  332. * to large, retry it.
  333. *
  334. * Returns 0 on success or negative error code on failure.
  335. */
  336. int i2o_exec_lct_get(struct i2o_controller *c)
  337. {
  338. struct i2o_message __iomem *msg;
  339. u32 m;
  340. int i = 0;
  341. int rc = -EAGAIN;
  342. for (i = 1; i <= I2O_LCT_GET_TRIES; i++) {
  343. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  344. if (m == I2O_QUEUE_EMPTY)
  345. return -ETIMEDOUT;
  346. writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
  347. writel(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 | ADAPTER_TID,
  348. &msg->u.head[1]);
  349. writel(0xffffffff, &msg->body[0]);
  350. writel(0x00000000, &msg->body[1]);
  351. writel(0xd0000000 | c->dlct.len, &msg->body[2]);
  352. writel(c->dlct.phys, &msg->body[3]);
  353. rc = i2o_msg_post_wait(c, m, I2O_TIMEOUT_LCT_GET);
  354. if (rc < 0)
  355. break;
  356. rc = i2o_device_parse_lct(c);
  357. if (rc != -EAGAIN)
  358. break;
  359. }
  360. return rc;
  361. }
  362. /**
  363. * i2o_exec_lct_notify - Send a asynchronus LCT NOTIFY request
  364. * @c: I2O controller to which the request should be send
  365. * @change_ind: change indicator
  366. *
  367. * This function sends a LCT NOTIFY request to the I2O controller with
  368. * the change indicator change_ind. If the change_ind == 0 the controller
  369. * replies immediately after the request. If change_ind > 0 the reply is
  370. * send after change indicator of the LCT is > change_ind.
  371. */
  372. static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind)
  373. {
  374. i2o_status_block *sb = c->status_block.virt;
  375. struct device *dev;
  376. struct i2o_message __iomem *msg;
  377. u32 m;
  378. dev = &c->pdev->dev;
  379. if (i2o_dma_realloc(dev, &c->dlct, sb->expected_lct_size, GFP_KERNEL))
  380. return -ENOMEM;
  381. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  382. if (m == I2O_QUEUE_EMPTY)
  383. return -ETIMEDOUT;
  384. writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
  385. writel(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 | ADAPTER_TID,
  386. &msg->u.head[1]);
  387. writel(i2o_exec_driver.context, &msg->u.s.icntxt);
  388. writel(0, &msg->u.s.tcntxt); /* FIXME */
  389. writel(0xffffffff, &msg->body[0]);
  390. writel(change_ind, &msg->body[1]);
  391. writel(0xd0000000 | c->dlct.len, &msg->body[2]);
  392. writel(c->dlct.phys, &msg->body[3]);
  393. i2o_msg_post(c, m);
  394. return 0;
  395. };
  396. /* Exec OSM driver struct */
  397. struct i2o_driver i2o_exec_driver = {
  398. .name = OSM_NAME,
  399. .reply = i2o_exec_reply,
  400. .event = i2o_exec_event,
  401. .classes = i2o_exec_class_id,
  402. .driver = {
  403. .probe = i2o_exec_probe,
  404. .remove = i2o_exec_remove,
  405. },
  406. };
  407. /**
  408. * i2o_exec_init - Registers the Exec OSM
  409. *
  410. * Registers the Exec OSM in the I2O core.
  411. *
  412. * Returns 0 on success or negative error code on failure.
  413. */
  414. int __init i2o_exec_init(void)
  415. {
  416. return i2o_driver_register(&i2o_exec_driver);
  417. };
  418. /**
  419. * i2o_exec_exit - Removes the Exec OSM
  420. *
  421. * Unregisters the Exec OSM from the I2O core.
  422. */
  423. void __exit i2o_exec_exit(void)
  424. {
  425. i2o_driver_unregister(&i2o_exec_driver);
  426. };
  427. EXPORT_SYMBOL(i2o_msg_post_wait_mem);
  428. EXPORT_SYMBOL(i2o_exec_lct_get);