exec-osm.c 16 KB

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