exec-osm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #include <linux/workqueue.h>
  33. #include <linux/string.h>
  34. #include <linux/slab.h>
  35. #include <linux/sched.h> /* wait_event_interruptible_timeout() needs this */
  36. #include <asm/param.h> /* HZ */
  37. #include "core.h"
  38. #define OSM_NAME "exec-osm"
  39. struct i2o_driver i2o_exec_driver;
  40. static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind);
  41. /* global wait list for POST WAIT */
  42. static LIST_HEAD(i2o_exec_wait_list);
  43. /* Wait struct needed for POST WAIT */
  44. struct i2o_exec_wait {
  45. wait_queue_head_t *wq; /* Pointer to Wait queue */
  46. struct i2o_dma dma; /* DMA buffers to free on failure */
  47. u32 tcntxt; /* transaction context from reply */
  48. int complete; /* 1 if reply received otherwise 0 */
  49. u32 m; /* message id */
  50. struct i2o_message *msg; /* pointer to the reply message */
  51. struct list_head list; /* node in global wait list */
  52. };
  53. /* Exec OSM class handling definition */
  54. static struct i2o_class_id i2o_exec_class_id[] = {
  55. {I2O_CLASS_EXECUTIVE},
  56. {I2O_CLASS_END}
  57. };
  58. /**
  59. * i2o_exec_wait_alloc - Allocate a i2o_exec_wait struct an initialize it
  60. *
  61. * Allocate the i2o_exec_wait struct and initialize the wait.
  62. *
  63. * Returns i2o_exec_wait pointer on success or negative error code on
  64. * failure.
  65. */
  66. static struct i2o_exec_wait *i2o_exec_wait_alloc(void)
  67. {
  68. struct i2o_exec_wait *wait;
  69. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  70. if (!wait)
  71. return ERR_PTR(-ENOMEM);
  72. memset(wait, 0, sizeof(*wait));
  73. INIT_LIST_HEAD(&wait->list);
  74. return wait;
  75. };
  76. /**
  77. * i2o_exec_wait_free - Free a i2o_exec_wait struct
  78. * @i2o_exec_wait: I2O wait data which should be cleaned up
  79. */
  80. static void i2o_exec_wait_free(struct i2o_exec_wait *wait)
  81. {
  82. kfree(wait);
  83. };
  84. /**
  85. * i2o_msg_post_wait_mem - Post and wait a message with DMA buffers
  86. * @c: controller
  87. * @m: message to post
  88. * @timeout: time in seconds to wait
  89. * @dma: i2o_dma struct of the DMA buffer to free on failure
  90. *
  91. * This API allows an OSM to post a message and then be told whether or
  92. * not the system received a successful reply. If the message times out
  93. * then the value '-ETIMEDOUT' is returned. This is a special case. In
  94. * this situation the message may (should) complete at an indefinite time
  95. * in the future. When it completes it will use the memory buffer
  96. * attached to the request. If -ETIMEDOUT is returned then the memory
  97. * buffer must not be freed. Instead the event completion will free them
  98. * for you. In all other cases the buffer are your problem.
  99. *
  100. * Returns 0 on success, negative error code on timeout or positive error
  101. * code from reply.
  102. */
  103. int i2o_msg_post_wait_mem(struct i2o_controller *c, u32 m, unsigned long
  104. timeout, struct i2o_dma *dma)
  105. {
  106. DECLARE_WAIT_QUEUE_HEAD(wq);
  107. struct i2o_exec_wait *wait;
  108. static u32 tcntxt = 0x80000000;
  109. struct i2o_message __iomem *msg = i2o_msg_in_to_virt(c, m);
  110. int rc = 0;
  111. wait = i2o_exec_wait_alloc();
  112. if (!wait)
  113. return -ENOMEM;
  114. if (tcntxt == 0xffffffff)
  115. tcntxt = 0x80000000;
  116. if (dma)
  117. wait->dma = *dma;
  118. /*
  119. * Fill in the message initiator context and transaction context.
  120. * We will only use transaction contexts >= 0x80000000 for POST WAIT,
  121. * so we could find a POST WAIT reply easier in the reply handler.
  122. */
  123. writel(i2o_exec_driver.context, &msg->u.s.icntxt);
  124. wait->tcntxt = tcntxt++;
  125. writel(wait->tcntxt, &msg->u.s.tcntxt);
  126. /*
  127. * Post the message to the controller. At some point later it will
  128. * return. If we time out before it returns then complete will be zero.
  129. */
  130. i2o_msg_post(c, m);
  131. if (!wait->complete) {
  132. wait->wq = &wq;
  133. /*
  134. * we add elements add the head, because if a entry in the list
  135. * will never be removed, we have to iterate over it every time
  136. */
  137. list_add(&wait->list, &i2o_exec_wait_list);
  138. wait_event_interruptible_timeout(wq, wait->complete,
  139. timeout * HZ);
  140. wait->wq = NULL;
  141. }
  142. barrier();
  143. if (wait->complete) {
  144. rc = le32_to_cpu(wait->msg->body[0]) >> 24;
  145. i2o_flush_reply(c, wait->m);
  146. i2o_exec_wait_free(wait);
  147. } else {
  148. /*
  149. * We cannot remove it now. This is important. When it does
  150. * terminate (which it must do if the controller has not
  151. * died...) then it will otherwise scribble on stuff.
  152. *
  153. * FIXME: try abort message
  154. */
  155. if (dma)
  156. dma->virt = NULL;
  157. rc = -ETIMEDOUT;
  158. }
  159. return rc;
  160. };
  161. /**
  162. * i2o_msg_post_wait_complete - Reply to a i2o_msg_post request from IOP
  163. * @c: I2O controller which answers
  164. * @m: message id
  165. * @msg: pointer to the I2O reply message
  166. * @context: transaction context of request
  167. *
  168. * This function is called in interrupt context only. If the reply reached
  169. * before the timeout, the i2o_exec_wait struct is filled with the message
  170. * and the task will be waked up. The task is now responsible for returning
  171. * the message m back to the controller! If the message reaches us after
  172. * the timeout clean up the i2o_exec_wait struct (including allocated
  173. * DMA buffer).
  174. *
  175. * Return 0 on success and if the message m should not be given back to the
  176. * I2O controller, or >0 on success and if the message should be given back
  177. * afterwords. Returns negative error code on failure. In this case the
  178. * message must also be given back to the controller.
  179. */
  180. static int i2o_msg_post_wait_complete(struct i2o_controller *c, u32 m,
  181. struct i2o_message *msg, u32 context)
  182. {
  183. struct i2o_exec_wait *wait, *tmp;
  184. unsigned long flags;
  185. static spinlock_t lock = SPIN_LOCK_UNLOCKED;
  186. int rc = 1;
  187. /*
  188. * We need to search through the i2o_exec_wait_list to see if the given
  189. * message is still outstanding. If not, it means that the IOP took
  190. * longer to respond to the message than we had allowed and timer has
  191. * already expired. Not much we can do about that except log it for
  192. * debug purposes, increase timeout, and recompile.
  193. */
  194. spin_lock_irqsave(&lock, flags);
  195. list_for_each_entry_safe(wait, tmp, &i2o_exec_wait_list, list) {
  196. if (wait->tcntxt == context) {
  197. list_del(&wait->list);
  198. spin_unlock_irqrestore(&lock, flags);
  199. wait->m = m;
  200. wait->msg = msg;
  201. wait->complete = 1;
  202. barrier();
  203. if (wait->wq) {
  204. wake_up_interruptible(wait->wq);
  205. rc = 0;
  206. } else {
  207. struct device *dev;
  208. dev = &c->pdev->dev;
  209. pr_debug("%s: timedout reply received!\n",
  210. c->name);
  211. i2o_dma_free(dev, &wait->dma);
  212. i2o_exec_wait_free(wait);
  213. rc = -1;
  214. }
  215. return rc;
  216. }
  217. }
  218. spin_unlock_irqrestore(&lock, flags);
  219. osm_warn("%s: Bogus reply in POST WAIT (tr-context: %08x)!\n", c->name,
  220. context);
  221. return -1;
  222. };
  223. /**
  224. * i2o_exec_show_vendor_id - Displays Vendor ID of controller
  225. * @d: device of which the Vendor ID should be displayed
  226. * @buf: buffer into which the Vendor ID should be printed
  227. *
  228. * Returns number of bytes printed into buffer.
  229. */
  230. static ssize_t i2o_exec_show_vendor_id(struct device *d, struct device_attribute *attr, char *buf)
  231. {
  232. struct i2o_device *dev = to_i2o_device(d);
  233. u16 id;
  234. if (i2o_parm_field_get(dev, 0x0000, 0, &id, 2)) {
  235. sprintf(buf, "0x%04x", id);
  236. return strlen(buf) + 1;
  237. }
  238. return 0;
  239. };
  240. /**
  241. * i2o_exec_show_product_id - Displays Product ID of controller
  242. * @d: device of which the Product ID should be displayed
  243. * @buf: buffer into which the Product ID should be printed
  244. *
  245. * Returns number of bytes printed into buffer.
  246. */
  247. static ssize_t i2o_exec_show_product_id(struct device *d, struct device_attribute *attr, char *buf)
  248. {
  249. struct i2o_device *dev = to_i2o_device(d);
  250. u16 id;
  251. if (i2o_parm_field_get(dev, 0x0000, 1, &id, 2)) {
  252. sprintf(buf, "0x%04x", id);
  253. return strlen(buf) + 1;
  254. }
  255. return 0;
  256. };
  257. /* Exec-OSM device attributes */
  258. static DEVICE_ATTR(vendor_id, S_IRUGO, i2o_exec_show_vendor_id, NULL);
  259. static DEVICE_ATTR(product_id, S_IRUGO, i2o_exec_show_product_id, NULL);
  260. /**
  261. * i2o_exec_probe - Called if a new I2O device (executive class) appears
  262. * @dev: I2O device which should be probed
  263. *
  264. * Registers event notification for every event from Executive device. The
  265. * return is always 0, because we want all devices of class Executive.
  266. *
  267. * Returns 0 on success.
  268. */
  269. static int i2o_exec_probe(struct device *dev)
  270. {
  271. struct i2o_device *i2o_dev = to_i2o_device(dev);
  272. struct i2o_controller *c = i2o_dev->iop;
  273. i2o_event_register(i2o_dev, &i2o_exec_driver, 0, 0xffffffff);
  274. c->exec = i2o_dev;
  275. i2o_exec_lct_notify(c, c->lct->change_ind + 1);
  276. device_create_file(dev, &dev_attr_vendor_id);
  277. device_create_file(dev, &dev_attr_product_id);
  278. return 0;
  279. };
  280. /**
  281. * i2o_exec_remove - Called on I2O device removal
  282. * @dev: I2O device which was removed
  283. *
  284. * Unregisters event notification from Executive I2O device.
  285. *
  286. * Returns 0 on success.
  287. */
  288. static int i2o_exec_remove(struct device *dev)
  289. {
  290. device_remove_file(dev, &dev_attr_product_id);
  291. device_remove_file(dev, &dev_attr_vendor_id);
  292. i2o_event_register(to_i2o_device(dev), &i2o_exec_driver, 0, 0);
  293. return 0;
  294. };
  295. /**
  296. * i2o_exec_lct_modified - Called on LCT NOTIFY reply
  297. * @c: I2O controller on which the LCT has modified
  298. *
  299. * This function handles asynchronus LCT NOTIFY replies. It parses the
  300. * new LCT and if the buffer for the LCT was to small sends a LCT NOTIFY
  301. * again, otherwise send LCT NOTIFY to get informed on next LCT change.
  302. */
  303. static void i2o_exec_lct_modified(struct i2o_controller *c)
  304. {
  305. u32 change_ind = 0;
  306. if (i2o_device_parse_lct(c) != -EAGAIN)
  307. change_ind = c->lct->change_ind + 1;
  308. i2o_exec_lct_notify(c, change_ind);
  309. };
  310. /**
  311. * i2o_exec_reply - I2O Executive reply handler
  312. * @c: I2O controller from which the reply comes
  313. * @m: message id
  314. * @msg: pointer to the I2O reply message
  315. *
  316. * This function is always called from interrupt context. If a POST WAIT
  317. * reply was received, pass it to the complete function. If a LCT NOTIFY
  318. * reply was received, a new event is created to handle the update.
  319. *
  320. * Returns 0 on success and if the reply should not be flushed or > 0
  321. * on success and if the reply should be flushed. Returns negative error
  322. * code on failure and if the reply should be flushed.
  323. */
  324. static int i2o_exec_reply(struct i2o_controller *c, u32 m,
  325. struct i2o_message *msg)
  326. {
  327. u32 context;
  328. if (le32_to_cpu(msg->u.head[0]) & MSG_FAIL) {
  329. /*
  330. * If Fail bit is set we must take the transaction context of
  331. * the preserved message to find the right request again.
  332. */
  333. struct i2o_message __iomem *pmsg;
  334. u32 pm;
  335. pm = le32_to_cpu(msg->body[3]);
  336. pmsg = i2o_msg_in_to_virt(c, pm);
  337. i2o_report_status(KERN_INFO, "i2o_core", msg);
  338. context = readl(&pmsg->u.s.tcntxt);
  339. /* Release the preserved msg */
  340. i2o_msg_nop(c, pm);
  341. } else
  342. context = le32_to_cpu(msg->u.s.tcntxt);
  343. if (context & 0x80000000)
  344. return i2o_msg_post_wait_complete(c, m, msg, context);
  345. if ((le32_to_cpu(msg->u.head[1]) >> 24) == I2O_CMD_LCT_NOTIFY) {
  346. struct work_struct *work;
  347. pr_debug("%s: LCT notify received\n", c->name);
  348. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  349. if (!work)
  350. return -ENOMEM;
  351. INIT_WORK(work, (void (*)(void *))i2o_exec_lct_modified, c);
  352. queue_work(i2o_exec_driver.event_queue, work);
  353. return 1;
  354. }
  355. /*
  356. * If this happens, we want to dump the message to the syslog so
  357. * it can be sent back to the card manufacturer by the end user
  358. * to aid in debugging.
  359. *
  360. */
  361. printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
  362. "Message dumped to syslog\n", c->name);
  363. i2o_dump_message(msg);
  364. return -EFAULT;
  365. }
  366. /**
  367. * i2o_exec_event - Event handling function
  368. * @evt: Event which occurs
  369. *
  370. * Handles events send by the Executive device. At the moment does not do
  371. * anything useful.
  372. */
  373. static void i2o_exec_event(struct i2o_event *evt)
  374. {
  375. if (likely(evt->i2o_dev))
  376. osm_debug("Event received from device: %d\n",
  377. evt->i2o_dev->lct_data.tid);
  378. kfree(evt);
  379. };
  380. /**
  381. * i2o_exec_lct_get - Get the IOP's Logical Configuration Table
  382. * @c: I2O controller from which the LCT should be fetched
  383. *
  384. * Send a LCT NOTIFY request to the controller, and wait
  385. * I2O_TIMEOUT_LCT_GET seconds until arrival of response. If the LCT is
  386. * to large, retry it.
  387. *
  388. * Returns 0 on success or negative error code on failure.
  389. */
  390. int i2o_exec_lct_get(struct i2o_controller *c)
  391. {
  392. struct i2o_message __iomem *msg;
  393. u32 m;
  394. int i = 0;
  395. int rc = -EAGAIN;
  396. for (i = 1; i <= I2O_LCT_GET_TRIES; i++) {
  397. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  398. if (m == I2O_QUEUE_EMPTY)
  399. return -ETIMEDOUT;
  400. writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
  401. writel(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 | ADAPTER_TID,
  402. &msg->u.head[1]);
  403. writel(0xffffffff, &msg->body[0]);
  404. writel(0x00000000, &msg->body[1]);
  405. writel(0xd0000000 | c->dlct.len, &msg->body[2]);
  406. writel(c->dlct.phys, &msg->body[3]);
  407. rc = i2o_msg_post_wait(c, m, I2O_TIMEOUT_LCT_GET);
  408. if (rc < 0)
  409. break;
  410. rc = i2o_device_parse_lct(c);
  411. if (rc != -EAGAIN)
  412. break;
  413. }
  414. return rc;
  415. }
  416. /**
  417. * i2o_exec_lct_notify - Send a asynchronus LCT NOTIFY request
  418. * @c: I2O controller to which the request should be send
  419. * @change_ind: change indicator
  420. *
  421. * This function sends a LCT NOTIFY request to the I2O controller with
  422. * the change indicator change_ind. If the change_ind == 0 the controller
  423. * replies immediately after the request. If change_ind > 0 the reply is
  424. * send after change indicator of the LCT is > change_ind.
  425. */
  426. static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind)
  427. {
  428. i2o_status_block *sb = c->status_block.virt;
  429. struct device *dev;
  430. struct i2o_message __iomem *msg;
  431. u32 m;
  432. dev = &c->pdev->dev;
  433. if (i2o_dma_realloc(dev, &c->dlct, sb->expected_lct_size, GFP_KERNEL))
  434. return -ENOMEM;
  435. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  436. if (m == I2O_QUEUE_EMPTY)
  437. return -ETIMEDOUT;
  438. writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
  439. writel(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 | ADAPTER_TID,
  440. &msg->u.head[1]);
  441. writel(i2o_exec_driver.context, &msg->u.s.icntxt);
  442. writel(0, &msg->u.s.tcntxt); /* FIXME */
  443. writel(0xffffffff, &msg->body[0]);
  444. writel(change_ind, &msg->body[1]);
  445. writel(0xd0000000 | c->dlct.len, &msg->body[2]);
  446. writel(c->dlct.phys, &msg->body[3]);
  447. i2o_msg_post(c, m);
  448. return 0;
  449. };
  450. /* Exec OSM driver struct */
  451. struct i2o_driver i2o_exec_driver = {
  452. .name = OSM_NAME,
  453. .reply = i2o_exec_reply,
  454. .event = i2o_exec_event,
  455. .classes = i2o_exec_class_id,
  456. .driver = {
  457. .probe = i2o_exec_probe,
  458. .remove = i2o_exec_remove,
  459. },
  460. };
  461. /**
  462. * i2o_exec_init - Registers the Exec OSM
  463. *
  464. * Registers the Exec OSM in the I2O core.
  465. *
  466. * Returns 0 on success or negative error code on failure.
  467. */
  468. int __init i2o_exec_init(void)
  469. {
  470. return i2o_driver_register(&i2o_exec_driver);
  471. };
  472. /**
  473. * i2o_exec_exit - Removes the Exec OSM
  474. *
  475. * Unregisters the Exec OSM from the I2O core.
  476. */
  477. void __exit i2o_exec_exit(void)
  478. {
  479. i2o_driver_unregister(&i2o_exec_driver);
  480. };
  481. EXPORT_SYMBOL(i2o_msg_post_wait_mem);
  482. EXPORT_SYMBOL(i2o_exec_lct_get);