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