blob: 42f8b810d6e57f8545a61ccfc2e0019a68cd3ddb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Functions to handle I2O controllers and I2O message handling
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
14 * Red 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 */
27
28#include <linux/module.h>
29#include <linux/i2o.h>
30#include <linux/delay.h>
Markus Lidel9e875452005-06-23 22:02:21 -070031#include "core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Markus Lidel9e875452005-06-23 22:02:21 -070033#define OSM_NAME "i2o"
34#define OSM_VERSION "1.288"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define OSM_DESCRIPTION "I2O subsystem"
36
37/* global I2O controller list */
38LIST_HEAD(i2o_controllers);
39
40/*
41 * global I2O System Table. Contains information about all the IOPs in the
42 * system. Used to inform IOPs about each others existence.
43 */
44static struct i2o_dma i2o_systab;
45
46static int i2o_hrt_get(struct i2o_controller *c);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * i2o_msg_nop - Returns a message which is not used
50 * @c: I2O controller from which the message was created
51 * @m: message which should be returned
52 *
53 * If you fetch a message via i2o_msg_get, and can't use it, you must
54 * return the message with this function. Otherwise the message frame
55 * is lost.
56 */
57void i2o_msg_nop(struct i2o_controller *c, u32 m)
58{
Markus Lidelf88e1192005-06-23 22:02:14 -070059 struct i2o_message __iomem *msg = i2o_msg_in_to_virt(c, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
62 writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID,
63 &msg->u.head[1]);
64 writel(0, &msg->u.head[2]);
65 writel(0, &msg->u.head[3]);
66 i2o_msg_post(c, m);
67};
68
69/**
70 * i2o_msg_get_wait - obtain an I2O message from the IOP
71 * @c: I2O controller
72 * @msg: pointer to a I2O message pointer
73 * @wait: how long to wait until timeout
74 *
75 * This function waits up to wait seconds for a message slot to be
76 * available.
77 *
78 * On a success the message is returned and the pointer to the message is
79 * set in msg. The returned message is the physical page frame offset
80 * address from the read port (see the i2o spec). If no message is
81 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
82 */
Markus Lidel9e875452005-06-23 22:02:21 -070083u32 i2o_msg_get_wait(struct i2o_controller *c,
84 struct i2o_message __iomem ** msg, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 unsigned long timeout = jiffies + wait * HZ;
87 u32 m;
88
89 while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) {
90 if (time_after(jiffies, timeout)) {
Markus Lidel9e875452005-06-23 22:02:21 -070091 osm_debug("%s: Timeout waiting for message frame.\n",
92 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return I2O_QUEUE_EMPTY;
94 }
95 set_current_state(TASK_UNINTERRUPTIBLE);
96 schedule_timeout(1);
97 }
98
99 return m;
100};
101
102#if BITS_PER_LONG == 64
103/**
104 * i2o_cntxt_list_add - Append a pointer to context list and return a id
105 * @c: controller to which the context list belong
106 * @ptr: pointer to add to the context list
107 *
108 * Because the context field in I2O is only 32-bit large, on 64-bit the
109 * pointer is to large to fit in the context field. The i2o_cntxt_list
110 * functions therefore map pointers to context fields.
111 *
112 * Returns context id > 0 on success or 0 on failure.
113 */
114u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
115{
116 struct i2o_context_list_element *entry;
117 unsigned long flags;
118
119 if (!ptr)
Markus Lidelf33213e2005-06-23 22:02:23 -0700120 osm_err("%s: couldn't add NULL pointer to context list!\n",
121 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
124 if (!entry) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700125 osm_err("%s: Could not allocate memory for context list element"
126 "\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return 0;
128 }
129
130 entry->ptr = ptr;
131 entry->timestamp = jiffies;
132 INIT_LIST_HEAD(&entry->list);
133
134 spin_lock_irqsave(&c->context_list_lock, flags);
135
136 if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
137 atomic_inc(&c->context_list_counter);
138
139 entry->context = atomic_read(&c->context_list_counter);
140
141 list_add(&entry->list, &c->context_list);
142
143 spin_unlock_irqrestore(&c->context_list_lock, flags);
144
Markus Lidelf33213e2005-06-23 22:02:23 -0700145 osm_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return entry->context;
148};
149
150/**
151 * i2o_cntxt_list_remove - Remove a pointer from the context list
152 * @c: controller to which the context list belong
153 * @ptr: pointer which should be removed from the context list
154 *
155 * Removes a previously added pointer from the context list and returns
156 * the matching context id.
157 *
158 * Returns context id on succes or 0 on failure.
159 */
160u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
161{
162 struct i2o_context_list_element *entry;
163 u32 context = 0;
164 unsigned long flags;
165
166 spin_lock_irqsave(&c->context_list_lock, flags);
167 list_for_each_entry(entry, &c->context_list, list)
168 if (entry->ptr == ptr) {
169 list_del(&entry->list);
170 context = entry->context;
171 kfree(entry);
172 break;
173 }
174 spin_unlock_irqrestore(&c->context_list_lock, flags);
175
176 if (!context)
Markus Lidelf33213e2005-06-23 22:02:23 -0700177 osm_warn("%s: Could not remove nonexistent ptr %p\n", c->name,
178 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Markus Lidelf33213e2005-06-23 22:02:23 -0700180 osm_debug("%s: remove ptr from context list %d -> %p\n", c->name,
181 context, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return context;
184};
185
186/**
187 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
188 * @c: controller to which the context list belong
189 * @context: context id to which the pointer belong
190 *
191 * Returns pointer to the matching context id on success or NULL on
192 * failure.
193 */
194void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
195{
196 struct i2o_context_list_element *entry;
197 unsigned long flags;
198 void *ptr = NULL;
199
200 spin_lock_irqsave(&c->context_list_lock, flags);
201 list_for_each_entry(entry, &c->context_list, list)
202 if (entry->context == context) {
203 list_del(&entry->list);
204 ptr = entry->ptr;
205 kfree(entry);
206 break;
207 }
208 spin_unlock_irqrestore(&c->context_list_lock, flags);
209
210 if (!ptr)
Markus Lidelf33213e2005-06-23 22:02:23 -0700211 osm_warn("%s: context id %d not found\n", c->name, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Markus Lidelf33213e2005-06-23 22:02:23 -0700213 osm_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
214 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 return ptr;
217};
218
219/**
220 * i2o_cntxt_list_get_ptr - Get a context id from the context list
221 * @c: controller to which the context list belong
222 * @ptr: pointer to which the context id should be fetched
223 *
224 * Returns context id which matches to the pointer on succes or 0 on
225 * failure.
226 */
227u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
228{
229 struct i2o_context_list_element *entry;
230 u32 context = 0;
231 unsigned long flags;
232
233 spin_lock_irqsave(&c->context_list_lock, flags);
234 list_for_each_entry(entry, &c->context_list, list)
235 if (entry->ptr == ptr) {
236 context = entry->context;
237 break;
238 }
239 spin_unlock_irqrestore(&c->context_list_lock, flags);
240
241 if (!context)
Markus Lidelf33213e2005-06-23 22:02:23 -0700242 osm_warn("%s: Could not find nonexistent ptr %p\n", c->name,
243 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Markus Lidelf33213e2005-06-23 22:02:23 -0700245 osm_debug("%s: get context id from context list %p -> %d\n", c->name,
246 ptr, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return context;
249};
250#endif
251
252/**
253 * i2o_iop_find - Find an I2O controller by id
254 * @unit: unit number of the I2O controller to search for
255 *
256 * Lookup the I2O controller on the controller list.
257 *
258 * Returns pointer to the I2O controller on success or NULL if not found.
259 */
260struct i2o_controller *i2o_find_iop(int unit)
261{
262 struct i2o_controller *c;
263
264 list_for_each_entry(c, &i2o_controllers, list) {
265 if (c->unit == unit)
266 return c;
267 }
268
269 return NULL;
270};
271
272/**
273 * i2o_iop_find_device - Find a I2O device on an I2O controller
274 * @c: I2O controller where the I2O device hangs on
275 * @tid: TID of the I2O device to search for
276 *
277 * Searches the devices of the I2O controller for a device with TID tid and
278 * returns it.
279 *
280 * Returns a pointer to the I2O device if found, otherwise NULL.
281 */
282struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
283{
284 struct i2o_device *dev;
285
286 list_for_each_entry(dev, &c->devices, list)
287 if (dev->lct_data.tid == tid)
288 return dev;
289
290 return NULL;
291};
292
293/**
294 * i2o_quiesce_controller - quiesce controller
295 * @c: controller
296 *
297 * Quiesce an IOP. Causes IOP to make external operation quiescent
298 * (i2o 'READY' state). Internal operation of the IOP continues normally.
299 *
300 * Returns 0 on success or negative error code on failure.
301 */
302static int i2o_iop_quiesce(struct i2o_controller *c)
303{
304 struct i2o_message __iomem *msg;
305 u32 m;
306 i2o_status_block *sb = c->status_block.virt;
307 int rc;
308
309 i2o_status_get(c);
310
311 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
312 if ((sb->iop_state != ADAPTER_STATE_READY) &&
313 (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
314 return 0;
315
316 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
317 if (m == I2O_QUEUE_EMPTY)
318 return -ETIMEDOUT;
319
320 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
321 writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID,
322 &msg->u.head[1]);
323
324 /* Long timeout needed for quiesce if lots of devices */
325 if ((rc = i2o_msg_post_wait(c, m, 240)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700326 osm_info("%s: Unable to quiesce (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700328 osm_debug("%s: Quiesced.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 i2o_status_get(c); // Entered READY state
331
332 return rc;
333};
334
335/**
336 * i2o_iop_enable - move controller from ready to OPERATIONAL
337 * @c: I2O controller
338 *
339 * Enable IOP. This allows the IOP to resume external operations and
340 * reverses the effect of a quiesce. Returns zero or an error code if
341 * an error occurs.
342 */
343static int i2o_iop_enable(struct i2o_controller *c)
344{
345 struct i2o_message __iomem *msg;
346 u32 m;
347 i2o_status_block *sb = c->status_block.virt;
348 int rc;
349
350 i2o_status_get(c);
351
352 /* Enable only allowed on READY state */
353 if (sb->iop_state != ADAPTER_STATE_READY)
354 return -EINVAL;
355
356 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
357 if (m == I2O_QUEUE_EMPTY)
358 return -ETIMEDOUT;
359
360 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
361 writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID,
362 &msg->u.head[1]);
363
364 /* How long of a timeout do we need? */
365 if ((rc = i2o_msg_post_wait(c, m, 240)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700366 osm_err("%s: Could not enable (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700368 osm_debug("%s: Enabled.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 i2o_status_get(c); // entered OPERATIONAL state
371
372 return rc;
373};
374
375/**
376 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
377 *
378 * Quiesce all I2O controllers which are connected to the system.
379 */
380static inline void i2o_iop_quiesce_all(void)
381{
382 struct i2o_controller *c, *tmp;
383
384 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
385 if (!c->no_quiesce)
386 i2o_iop_quiesce(c);
387 }
388};
389
390/**
391 * i2o_iop_enable_all - Enables all controllers on the system
392 *
393 * Enables all I2O controllers which are connected to the system.
394 */
395static inline void i2o_iop_enable_all(void)
396{
397 struct i2o_controller *c, *tmp;
398
399 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
400 i2o_iop_enable(c);
401};
402
403/**
404 * i2o_clear_controller - Bring I2O controller into HOLD state
405 * @c: controller
406 *
407 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
408 * input queues and prepare for a system restart. IOP's internal operation
409 * continues normally and the outbound queue is alive. The IOP is not
410 * expected to rebuild its LCT.
411 *
412 * Returns 0 on success or negative error code on failure.
413 */
414static int i2o_iop_clear(struct i2o_controller *c)
415{
416 struct i2o_message __iomem *msg;
417 u32 m;
418 int rc;
419
420 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
421 if (m == I2O_QUEUE_EMPTY)
422 return -ETIMEDOUT;
423
424 /* Quiesce all IOPs first */
425 i2o_iop_quiesce_all();
426
427 writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
428 writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID,
429 &msg->u.head[1]);
430
431 if ((rc = i2o_msg_post_wait(c, m, 30)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700432 osm_info("%s: Unable to clear (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700434 osm_debug("%s: Cleared.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Enable all IOPs */
437 i2o_iop_enable_all();
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return rc;
440}
441
442/**
Markus Lidelf10378f2005-06-23 22:02:16 -0700443 * i2o_iop_init_outbound_queue - setup the outbound message queue
444 * @c: I2O controller
445 *
446 * Clear and (re)initialize IOP's outbound queue and post the message
447 * frames to the IOP.
448 *
449 * Returns 0 on success or a negative errno code on failure.
450 */
451static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
452{
Markus Lidel9e875452005-06-23 22:02:21 -0700453 volatile u8 *status = c->status.virt;
Markus Lidelf10378f2005-06-23 22:02:16 -0700454 u32 m;
455 struct i2o_message __iomem *msg;
456 ulong timeout;
457 int i;
458
459 osm_debug("%s: Initializing Outbound Queue...\n", c->name);
460
Markus Lidel9e875452005-06-23 22:02:21 -0700461 memset(c->status.virt, 0, 4);
Markus Lidelf10378f2005-06-23 22:02:16 -0700462
463 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
464 if (m == I2O_QUEUE_EMPTY)
465 return -ETIMEDOUT;
466
Markus Lidel9e875452005-06-23 22:02:21 -0700467 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]);
Markus Lidelf10378f2005-06-23 22:02:16 -0700468 writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
469 &msg->u.head[1]);
470 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
Markus Lidel9e875452005-06-23 22:02:21 -0700471 writel(0x00000000, &msg->u.s.tcntxt);
Markus Lidelf10378f2005-06-23 22:02:16 -0700472 writel(PAGE_SIZE, &msg->body[0]);
473 /* Outbound msg frame size in words and Initcode */
Markus Lidel9e875452005-06-23 22:02:21 -0700474 writel(I2O_OUTBOUND_MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]);
Markus Lidelf10378f2005-06-23 22:02:16 -0700475 writel(0xd0000004, &msg->body[2]);
476 writel(i2o_dma_low(c->status.phys), &msg->body[3]);
477 writel(i2o_dma_high(c->status.phys), &msg->body[4]);
478
479 i2o_msg_post(c, m);
480
481 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
482 while (*status <= I2O_CMD_IN_PROGRESS) {
483 if (time_after(jiffies, timeout)) {
484 osm_warn("%s: Timeout Initializing\n", c->name);
485 return -ETIMEDOUT;
486 }
487 set_current_state(TASK_UNINTERRUPTIBLE);
488 schedule_timeout(1);
Markus Lidelf10378f2005-06-23 22:02:16 -0700489 }
490
491 m = c->out_queue.phys;
492
493 /* Post frames */
Markus Lidel9e875452005-06-23 22:02:21 -0700494 for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700495 i2o_flush_reply(c, m);
496 udelay(1); /* Promise */
Markus Lidel9e875452005-06-23 22:02:21 -0700497 m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
Markus Lidelf10378f2005-06-23 22:02:16 -0700498 }
499
500 return 0;
501}
502
503/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * i2o_iop_reset - reset an I2O controller
505 * @c: controller to reset
506 *
507 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
508 * Terminate all external operations, clear IOP's inbound and outbound
509 * queues, terminate all DDMs, and reload the IOP's operating environment
510 * and all local DDMs. The IOP rebuilds its LCT.
511 */
512static int i2o_iop_reset(struct i2o_controller *c)
513{
Markus Lidel9e875452005-06-23 22:02:21 -0700514 volatile u8 *status = c->status.virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct i2o_message __iomem *msg;
516 u32 m;
517 unsigned long timeout;
518 i2o_status_block *sb = c->status_block.virt;
519 int rc = 0;
520
Markus Lidel9e875452005-06-23 22:02:21 -0700521 osm_debug("%s: Resetting controller\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
524 if (m == I2O_QUEUE_EMPTY)
525 return -ETIMEDOUT;
526
Markus Lidel9e875452005-06-23 22:02:21 -0700527 memset(c->status_block.virt, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Quiesce all IOPs first */
530 i2o_iop_quiesce_all();
531
532 writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
533 writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
534 &msg->u.head[1]);
535 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
536 writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context
537 writel(0, &msg->body[0]);
538 writel(0, &msg->body[1]);
Markus Lidelf10378f2005-06-23 22:02:16 -0700539 writel(i2o_dma_low(c->status.phys), &msg->body[2]);
540 writel(i2o_dma_high(c->status.phys), &msg->body[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 i2o_msg_post(c, m);
543
544 /* Wait for a reply */
545 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
546 while (!*status) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700547 if (time_after(jiffies, timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 set_current_state(TASK_UNINTERRUPTIBLE);
551 schedule_timeout(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
Markus Lidelf10378f2005-06-23 22:02:16 -0700554 switch (*status) {
555 case I2O_CMD_REJECTED:
556 osm_warn("%s: IOP reset rejected\n", c->name);
557 rc = -EPERM;
558 break;
559
560 case I2O_CMD_IN_PROGRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * Once the reset is sent, the IOP goes into the INIT state
Markus Lidelf10378f2005-06-23 22:02:16 -0700563 * which is indeterminate. We need to wait until the IOP has
564 * rebooted before we can let the system talk to it. We read
565 * the inbound Free_List until a message is available. If we
566 * can't read one in the given ammount of time, we assume the
567 * IOP could not reboot properly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Markus Lidelf33213e2005-06-23 22:02:23 -0700569 osm_debug("%s: Reset in progress, waiting for reboot...\n",
570 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
573 while (m == I2O_QUEUE_EMPTY) {
574 if (time_after(jiffies, timeout)) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700575 osm_err("%s: IOP reset timeout.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rc = -ETIMEDOUT;
577 goto exit;
578 }
579 set_current_state(TASK_UNINTERRUPTIBLE);
580 schedule_timeout(1);
581
582 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
583 }
584 i2o_msg_nop(c, m);
Markus Lidelf10378f2005-06-23 22:02:16 -0700585
586 /* from here all quiesce commands are safe */
587 c->no_quiesce = 0;
588
589 /* verify if controller is in state RESET */
590 i2o_status_get(c);
591
592 if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
593 osm_warn("%s: reset completed, but adapter not in RESET"
594 " state.\n", c->name);
595 else
596 osm_debug("%s: reset completed.\n", c->name);
597
598 break;
599
600 default:
601 osm_err("%s: IOP reset timeout.\n", c->name);
602 rc = -ETIMEDOUT;
603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 exit:
607 /* Enable all IOPs */
608 i2o_iop_enable_all();
609
610 return rc;
611};
612
613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * i2o_iop_activate - Bring controller up to HOLD
615 * @c: controller
616 *
617 * This function brings an I2O controller into HOLD state. The adapter
618 * is reset if necessary and then the queues and resource table are read.
619 *
620 * Returns 0 on success or negative error code on failure.
621 */
622static int i2o_iop_activate(struct i2o_controller *c)
623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 i2o_status_block *sb = c->status_block.virt;
625 int rc;
Markus Lidelf10378f2005-06-23 22:02:16 -0700626 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
629 /* In READY state, Get status */
630
631 rc = i2o_status_get(c);
632 if (rc) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700633 osm_info("%s: Unable to obtain status, attempting a reset.\n",
634 c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700635 rc = i2o_iop_reset(c);
636 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return rc;
638 }
639
640 if (sb->i2o_version > I2OVER15) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700641 osm_err("%s: Not running version 1.5 of the I2O Specification."
642 "\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -ENODEV;
644 }
645
646 switch (sb->iop_state) {
647 case ADAPTER_STATE_FAULTED:
Markus Lidelf33213e2005-06-23 22:02:23 -0700648 osm_err("%s: hardware fault\n", c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700649 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 case ADAPTER_STATE_READY:
652 case ADAPTER_STATE_OPERATIONAL:
653 case ADAPTER_STATE_HOLD:
654 case ADAPTER_STATE_FAILED:
Markus Lidelf33213e2005-06-23 22:02:23 -0700655 osm_debug("%s: already running, trying to reset...\n", c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700656 rc = i2o_iop_reset(c);
657 if (rc)
658 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Markus Lidelf10378f2005-06-23 22:02:16 -0700661 /* preserve state */
662 state = sb->iop_state;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 rc = i2o_iop_init_outbound_queue(c);
665 if (rc)
666 return rc;
667
Markus Lidelf10378f2005-06-23 22:02:16 -0700668 /* if adapter was not in RESET state clear now */
669 if (state != ADAPTER_STATE_RESET)
670 i2o_iop_clear(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Markus Lidelf10378f2005-06-23 22:02:16 -0700672 i2o_status_get(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Markus Lidelf10378f2005-06-23 22:02:16 -0700674 if (sb->iop_state != ADAPTER_STATE_HOLD) {
675 osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
676 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Markus Lidelf10378f2005-06-23 22:02:16 -0700679 return i2o_hrt_get(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680};
681
682/**
683 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
684 * @c: I2O controller to which the system table should be send
685 *
686 * Before the systab could be set i2o_systab_build() must be called.
687 *
688 * Returns 0 on success or negative error code on failure.
689 */
690static int i2o_iop_systab_set(struct i2o_controller *c)
691{
692 struct i2o_message __iomem *msg;
693 u32 m;
694 i2o_status_block *sb = c->status_block.virt;
695 struct device *dev = &c->pdev->dev;
696 struct resource *root;
697 int rc;
698
699 if (sb->current_mem_size < sb->desired_mem_size) {
700 struct resource *res = &c->mem_resource;
701 res->name = c->pdev->bus->name;
702 res->flags = IORESOURCE_MEM;
703 res->start = 0;
704 res->end = 0;
Markus Lidelf33213e2005-06-23 22:02:23 -0700705 osm_info("%s: requires private memory resources.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 root = pci_find_parent_resource(c->pdev, res);
707 if (root == NULL)
Markus Lidelf33213e2005-06-23 22:02:23 -0700708 osm_warn("%s: Can't find parent resource!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (root && allocate_resource(root, res, sb->desired_mem_size, sb->desired_mem_size, sb->desired_mem_size, 1 << 20, /* Unspecified, so use 1Mb and play safe */
710 NULL, NULL) >= 0) {
711 c->mem_alloc = 1;
712 sb->current_mem_size = 1 + res->end - res->start;
713 sb->current_mem_base = res->start;
Markus Lidelf33213e2005-06-23 22:02:23 -0700714 osm_info("%s: allocated %ld bytes of PCI memory at "
715 "0x%08lX.\n", c->name,
716 1 + res->end - res->start, res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 }
719
720 if (sb->current_io_size < sb->desired_io_size) {
721 struct resource *res = &c->io_resource;
722 res->name = c->pdev->bus->name;
723 res->flags = IORESOURCE_IO;
724 res->start = 0;
725 res->end = 0;
Markus Lidelf33213e2005-06-23 22:02:23 -0700726 osm_info("%s: requires private memory resources.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 root = pci_find_parent_resource(c->pdev, res);
728 if (root == NULL)
Markus Lidelf33213e2005-06-23 22:02:23 -0700729 osm_warn("%s: Can't find parent resource!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (root && allocate_resource(root, res, sb->desired_io_size, sb->desired_io_size, sb->desired_io_size, 1 << 20, /* Unspecified, so use 1Mb and play safe */
731 NULL, NULL) >= 0) {
732 c->io_alloc = 1;
733 sb->current_io_size = 1 + res->end - res->start;
734 sb->current_mem_base = res->start;
Markus Lidelf33213e2005-06-23 22:02:23 -0700735 osm_info("%s: allocated %ld bytes of PCI I/O at 0x%08lX"
736 ".\n", c->name, 1 + res->end - res->start,
737 res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 }
740
741 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
742 if (m == I2O_QUEUE_EMPTY)
743 return -ETIMEDOUT;
744
745 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
746 PCI_DMA_TODEVICE);
747 if (!i2o_systab.phys) {
748 i2o_msg_nop(c, m);
749 return -ENOMEM;
750 }
751
752 writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
753 writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
754 &msg->u.head[1]);
755
756 /*
757 * Provide three SGL-elements:
758 * System table (SysTab), Private memory space declaration and
759 * Private i/o space declaration
760 *
761 * FIXME: is this still true?
762 * Nasty one here. We can't use dma_alloc_coherent to send the
763 * same table to everyone. We have to go remap it for them all
764 */
765
766 writel(c->unit + 2, &msg->body[0]);
767 writel(0, &msg->body[1]);
768 writel(0x54000000 | i2o_systab.len, &msg->body[2]);
769 writel(i2o_systab.phys, &msg->body[3]);
770 writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
771 writel(sb->current_mem_base, &msg->body[5]);
772 writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
773 writel(sb->current_io_base, &msg->body[6]);
774
775 rc = i2o_msg_post_wait(c, m, 120);
776
777 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
778 PCI_DMA_TODEVICE);
779
780 if (rc < 0)
Markus Lidelf33213e2005-06-23 22:02:23 -0700781 osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
782 -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700784 osm_debug("%s: SysTab set.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 i2o_status_get(c); // Entered READY state
787
788 return rc;
789}
790
791/**
792 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
793 * @c: I2O controller
794 *
795 * Send the system table and enable the I2O controller.
796 *
797 * Returns 0 on success or negativer error code on failure.
798 */
799static int i2o_iop_online(struct i2o_controller *c)
800{
801 int rc;
802
803 rc = i2o_iop_systab_set(c);
804 if (rc)
805 return rc;
806
807 /* In READY state */
Markus Lidelf33213e2005-06-23 22:02:23 -0700808 osm_debug("%s: Attempting to enable...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rc = i2o_iop_enable(c);
810 if (rc)
811 return rc;
812
813 return 0;
814};
815
816/**
817 * i2o_iop_remove - Remove the I2O controller from the I2O core
818 * @c: I2O controller
819 *
820 * Remove the I2O controller from the I2O core. If devices are attached to
821 * the controller remove these also and finally reset the controller.
822 */
823void i2o_iop_remove(struct i2o_controller *c)
824{
825 struct i2o_device *dev, *tmp;
826
Markus Lidelf33213e2005-06-23 22:02:23 -0700827 osm_debug("%s: deleting controller\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 i2o_driver_notify_controller_remove_all(c);
830
831 list_del(&c->list);
832
833 list_for_each_entry_safe(dev, tmp, &c->devices, list)
834 i2o_device_remove(dev);
835
Markus Lidelf88e1192005-06-23 22:02:14 -0700836 device_del(&c->device);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Ask the IOP to switch to RESET state */
839 i2o_iop_reset(c);
Markus Lidelf88e1192005-06-23 22:02:14 -0700840
841 put_device(&c->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/**
845 * i2o_systab_build - Build system table
846 *
847 * The system table contains information about all the IOPs in the system
848 * (duh) and is used by the Executives on the IOPs to establish peer2peer
849 * connections. We're not supporting peer2peer at the moment, but this
850 * will be needed down the road for things like lan2lan forwarding.
851 *
852 * Returns 0 on success or negative error code on failure.
853 */
854static int i2o_systab_build(void)
855{
856 struct i2o_controller *c, *tmp;
857 int num_controllers = 0;
858 u32 change_ind = 0;
859 int count = 0;
860 struct i2o_sys_tbl *systab = i2o_systab.virt;
861
862 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
863 num_controllers++;
864
865 if (systab) {
866 change_ind = systab->change_ind;
867 kfree(i2o_systab.virt);
868 }
869
870 /* Header + IOPs */
871 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
872 sizeof(struct i2o_sys_tbl_entry);
873
874 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
875 if (!systab) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700876 osm_err("unable to allocate memory for System Table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -ENOMEM;
878 }
879 memset(systab, 0, i2o_systab.len);
880
881 systab->version = I2OVERSION;
882 systab->change_ind = change_ind + 1;
883
884 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
885 i2o_status_block *sb;
886
887 if (count >= num_controllers) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700888 osm_err("controller added while building system table"
889 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
891 }
892
893 sb = c->status_block.virt;
894
895 /*
896 * Get updated IOP state so we have the latest information
897 *
898 * We should delete the controller at this point if it
899 * doesn't respond since if it's not on the system table
900 * it is techninically not part of the I2O subsystem...
901 */
902 if (unlikely(i2o_status_get(c))) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700903 osm_err("%s: Deleting b/c could not get status while "
904 "attempting to build system table\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 i2o_iop_remove(c);
906 continue; // try the next one
907 }
908
909 systab->iops[count].org_id = sb->org_id;
910 systab->iops[count].iop_id = c->unit + 2;
911 systab->iops[count].seg_num = 0;
912 systab->iops[count].i2o_version = sb->i2o_version;
913 systab->iops[count].iop_state = sb->iop_state;
914 systab->iops[count].msg_type = sb->msg_type;
915 systab->iops[count].frame_size = sb->inbound_frame_size;
916 systab->iops[count].last_changed = change_ind;
917 systab->iops[count].iop_capabilities = sb->iop_capabilities;
Markus Lidelf88e1192005-06-23 22:02:14 -0700918 systab->iops[count].inbound_low =
919 i2o_dma_low(c->base.phys + I2O_IN_PORT);
920 systab->iops[count].inbound_high =
921 i2o_dma_high(c->base.phys + I2O_IN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 count++;
924 }
925
926 systab->num_entries = count;
927
928 return 0;
929};
930
931/**
932 * i2o_parse_hrt - Parse the hardware resource table.
933 * @c: I2O controller
934 *
935 * We don't do anything with it except dumping it (in debug mode).
936 *
937 * Returns 0.
938 */
939static int i2o_parse_hrt(struct i2o_controller *c)
940{
941 i2o_dump_hrt(c);
942 return 0;
943};
944
945/**
946 * i2o_status_get - Get the status block from the I2O controller
947 * @c: I2O controller
948 *
949 * Issue a status query on the controller. This updates the attached
950 * status block. The status block could then be accessed through
951 * c->status_block.
952 *
953 * Returns 0 on sucess or negative error code on failure.
954 */
955int i2o_status_get(struct i2o_controller *c)
956{
957 struct i2o_message __iomem *msg;
958 u32 m;
Markus Lidel9e875452005-06-23 22:02:21 -0700959 volatile u8 *status_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 unsigned long timeout;
961
962 status_block = (u8 *) c->status_block.virt;
Markus Lidel9e875452005-06-23 22:02:21 -0700963 memset(c->status_block.virt, 0, sizeof(i2o_status_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
966 if (m == I2O_QUEUE_EMPTY)
967 return -ETIMEDOUT;
968
969 writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
970 writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
971 &msg->u.head[1]);
972 writel(i2o_exec_driver.context, &msg->u.s.icntxt);
973 writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context
974 writel(0, &msg->body[0]);
975 writel(0, &msg->body[1]);
Markus Lidelf10378f2005-06-23 22:02:16 -0700976 writel(i2o_dma_low(c->status_block.phys), &msg->body[2]);
977 writel(i2o_dma_high(c->status_block.phys), &msg->body[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */
979
980 i2o_msg_post(c, m);
981
982 /* Wait for a reply */
983 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
984 while (status_block[87] != 0xFF) {
985 if (time_after(jiffies, timeout)) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700986 osm_err("%s: Get status timeout.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -ETIMEDOUT;
988 }
989
990 set_current_state(TASK_UNINTERRUPTIBLE);
991 schedule_timeout(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
994#ifdef DEBUG
995 i2o_debug_state(c);
996#endif
997
998 return 0;
999}
1000
1001/*
1002 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
1003 * @c: I2O controller from which the HRT should be fetched
1004 *
1005 * The HRT contains information about possible hidden devices but is
1006 * mostly useless to us.
1007 *
1008 * Returns 0 on success or negativer error code on failure.
1009 */
1010static int i2o_hrt_get(struct i2o_controller *c)
1011{
1012 int rc;
1013 int i;
1014 i2o_hrt *hrt = c->hrt.virt;
1015 u32 size = sizeof(i2o_hrt);
1016 struct device *dev = &c->pdev->dev;
1017
1018 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1019 struct i2o_message __iomem *msg;
1020 u32 m;
1021
1022 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1023 if (m == I2O_QUEUE_EMPTY)
1024 return -ETIMEDOUT;
1025
1026 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1027 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1028 &msg->u.head[1]);
1029 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1030 writel(c->hrt.phys, &msg->body[1]);
1031
1032 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1033
1034 if (rc < 0) {
Markus Lidelf33213e2005-06-23 22:02:23 -07001035 osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
1036 -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return rc;
1038 }
1039
1040 size = hrt->num_entries * hrt->entry_len << 2;
1041 if (size > c->hrt.len) {
1042 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1043 return -ENOMEM;
1044 else
1045 hrt = c->hrt.virt;
1046 } else
1047 return i2o_parse_hrt(c);
1048 }
1049
Markus Lidelf33213e2005-06-23 22:02:23 -07001050 osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
1051 I2O_HRT_GET_TRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return -EBUSY;
1054}
1055
1056/**
Markus Lidelf88e1192005-06-23 22:02:14 -07001057 * i2o_iop_free - Free the i2o_controller struct
1058 * @c: I2O controller to free
1059 */
1060void i2o_iop_free(struct i2o_controller *c)
1061{
1062 kfree(c);
1063};
1064
Markus Lidelf88e1192005-06-23 22:02:14 -07001065/**
1066 * i2o_iop_release - release the memory for a I2O controller
1067 * @dev: I2O controller which should be released
1068 *
1069 * Release the allocated memory. This function is called if refcount of
1070 * device reaches 0 automatically.
1071 */
1072static void i2o_iop_release(struct device *dev)
1073{
1074 struct i2o_controller *c = to_i2o_controller(dev);
1075
1076 i2o_iop_free(c);
1077};
1078
Markus Lidel9e875452005-06-23 22:02:21 -07001079/* I2O controller class */
1080static struct class i2o_controller_class = {
1081 .name = "i2o_controller",
1082};
1083
Markus Lidelf88e1192005-06-23 22:02:14 -07001084/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1086 *
1087 * Allocate the necessary memory for a i2o_controller struct and
1088 * initialize the lists.
1089 *
1090 * Returns a pointer to the I2O controller or a negative error code on
1091 * failure.
1092 */
1093struct i2o_controller *i2o_iop_alloc(void)
1094{
1095 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1096 struct i2o_controller *c;
1097
1098 c = kmalloc(sizeof(*c), GFP_KERNEL);
1099 if (!c) {
Markus Lidelf33213e2005-06-23 22:02:23 -07001100 osm_err("i2o: Insufficient memory to allocate a I2O controller."
1101 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return ERR_PTR(-ENOMEM);
1103 }
1104 memset(c, 0, sizeof(*c));
1105
1106 INIT_LIST_HEAD(&c->devices);
1107 spin_lock_init(&c->lock);
1108 init_MUTEX(&c->lct_lock);
1109 c->unit = unit++;
1110 sprintf(c->name, "iop%d", c->unit);
1111
Markus Lidelf88e1192005-06-23 22:02:14 -07001112 device_initialize(&c->device);
Markus Lidel9e875452005-06-23 22:02:21 -07001113 class_device_initialize(&c->classdev);
1114
Markus Lidelf88e1192005-06-23 22:02:14 -07001115 c->device.release = &i2o_iop_release;
Markus Lidel9e875452005-06-23 22:02:21 -07001116 c->classdev.class = &i2o_controller_class;
1117 c->classdev.dev = &c->device;
1118
Markus Lidelf88e1192005-06-23 22:02:14 -07001119 snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
Markus Lidel9e875452005-06-23 22:02:21 -07001120 snprintf(c->classdev.class_id, BUS_ID_SIZE, "iop%d", c->unit);
Markus Lidelf88e1192005-06-23 22:02:14 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#if BITS_PER_LONG == 64
1123 spin_lock_init(&c->context_list_lock);
1124 atomic_set(&c->context_list_counter, 0);
1125 INIT_LIST_HEAD(&c->context_list);
1126#endif
1127
1128 return c;
1129};
1130
1131/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1133 * @c: controller
1134 *
1135 * Initialize the I2O controller and if no error occurs add him to the I2O
1136 * core.
1137 *
1138 * Returns 0 on success or negative error code on failure.
1139 */
1140int i2o_iop_add(struct i2o_controller *c)
1141{
1142 int rc;
1143
Markus Lidel9e875452005-06-23 22:02:21 -07001144 if ((rc = device_add(&c->device))) {
1145 osm_err("%s: could not add controller\n", c->name);
Markus Lidelf88e1192005-06-23 22:02:14 -07001146 goto iop_reset;
1147 }
1148
Markus Lidel9e875452005-06-23 22:02:21 -07001149 if ((rc = class_device_add(&c->classdev))) {
1150 osm_err("%s: could not add controller class\n", c->name);
1151 goto device_del;
1152 }
1153
1154 osm_info("%s: Activating I2O controller...\n", c->name);
1155 osm_info("%s: This may take a few minutes if there are many devices\n",
1156 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if ((rc = i2o_iop_activate(c))) {
Markus Lidel9e875452005-06-23 22:02:21 -07001159 osm_err("%s: could not activate controller\n", c->name);
1160 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
Markus Lidel9e875452005-06-23 22:02:21 -07001163 osm_debug("%s: building sys table...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Markus Lidelf88e1192005-06-23 22:02:14 -07001165 if ((rc = i2o_systab_build()))
Markus Lidel9e875452005-06-23 22:02:21 -07001166 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Markus Lidel9e875452005-06-23 22:02:21 -07001168 osm_debug("%s: online controller...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Markus Lidelf88e1192005-06-23 22:02:14 -07001170 if ((rc = i2o_iop_online(c)))
Markus Lidel9e875452005-06-23 22:02:21 -07001171 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Markus Lidel9e875452005-06-23 22:02:21 -07001173 osm_debug("%s: getting LCT...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Markus Lidelf88e1192005-06-23 22:02:14 -07001175 if ((rc = i2o_exec_lct_get(c)))
Markus Lidel9e875452005-06-23 22:02:21 -07001176 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 list_add(&c->list, &i2o_controllers);
1179
1180 i2o_driver_notify_controller_add_all(c);
1181
Markus Lidel9e875452005-06-23 22:02:21 -07001182 osm_info("%s: Controller added\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 return 0;
Markus Lidelf88e1192005-06-23 22:02:14 -07001185
Markus Lidel9e875452005-06-23 22:02:21 -07001186 class_del:
1187 class_device_del(&c->classdev);
1188
1189 device_del:
1190 device_del(&c->device);
1191
1192 iop_reset:
Markus Lidelf88e1192005-06-23 22:02:14 -07001193 i2o_iop_reset(c);
1194
1195 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196};
1197
1198/**
1199 * i2o_event_register - Turn on/off event notification for a I2O device
1200 * @dev: I2O device which should receive the event registration request
1201 * @drv: driver which want to get notified
1202 * @tcntxt: transaction context to use with this notifier
1203 * @evt_mask: mask of events
1204 *
1205 * Create and posts an event registration message to the task. No reply
1206 * is waited for, or expected. If you do not want further notifications,
1207 * call the i2o_event_register again with a evt_mask of 0.
1208 *
1209 * Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1210 * sending the request.
1211 */
1212int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1213 int tcntxt, u32 evt_mask)
1214{
1215 struct i2o_controller *c = dev->iop;
1216 struct i2o_message __iomem *msg;
1217 u32 m;
1218
1219 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1220 if (m == I2O_QUEUE_EMPTY)
1221 return -ETIMEDOUT;
1222
1223 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1224 writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1225 tid, &msg->u.head[1]);
1226 writel(drv->context, &msg->u.s.icntxt);
1227 writel(tcntxt, &msg->u.s.tcntxt);
1228 writel(evt_mask, &msg->body[0]);
1229
1230 i2o_msg_post(c, m);
1231
1232 return 0;
1233};
1234
1235/**
1236 * i2o_iop_init - I2O main initialization function
1237 *
1238 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1239 * initialize the I2O PCI part and finally initialize I2O device stuff.
1240 *
1241 * Returns 0 on success or negative error code on failure.
1242 */
1243static int __init i2o_iop_init(void)
1244{
1245 int rc = 0;
1246
1247 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1248
1249 rc = i2o_device_init();
1250 if (rc)
1251 goto exit;
1252
Markus Lidel9e875452005-06-23 22:02:21 -07001253 if ((rc = class_register(&i2o_controller_class))) {
1254 osm_err("can't register class i2o_controller\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto device_exit;
Markus Lidel9e875452005-06-23 22:02:21 -07001256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Markus Lidel9e875452005-06-23 22:02:21 -07001258 if ((rc = i2o_driver_init()))
1259 goto class_exit;
1260
1261 if ((rc = i2o_exec_init()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto driver_exit;
1263
Markus Lidel9e875452005-06-23 22:02:21 -07001264 if ((rc = i2o_pci_init()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto exec_exit;
1266
1267 return 0;
1268
1269 exec_exit:
1270 i2o_exec_exit();
1271
1272 driver_exit:
1273 i2o_driver_exit();
1274
Markus Lidel9e875452005-06-23 22:02:21 -07001275 class_exit:
1276 class_unregister(&i2o_controller_class);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 device_exit:
1279 i2o_device_exit();
1280
1281 exit:
1282 return rc;
1283}
1284
1285/**
1286 * i2o_iop_exit - I2O main exit function
1287 *
1288 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1289 */
1290static void __exit i2o_iop_exit(void)
1291{
1292 i2o_pci_exit();
1293 i2o_exec_exit();
1294 i2o_driver_exit();
Markus Lidel9e875452005-06-23 22:02:21 -07001295 class_unregister(&i2o_controller_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 i2o_device_exit();
1297};
1298
1299module_init(i2o_iop_init);
1300module_exit(i2o_iop_exit);
1301
1302MODULE_AUTHOR("Red Hat Software");
1303MODULE_LICENSE("GPL");
1304MODULE_DESCRIPTION(OSM_DESCRIPTION);
1305MODULE_VERSION(OSM_VERSION);
1306
1307#if BITS_PER_LONG == 64
1308EXPORT_SYMBOL(i2o_cntxt_list_add);
1309EXPORT_SYMBOL(i2o_cntxt_list_get);
1310EXPORT_SYMBOL(i2o_cntxt_list_remove);
1311EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1312#endif
1313EXPORT_SYMBOL(i2o_msg_get_wait);
1314EXPORT_SYMBOL(i2o_msg_nop);
1315EXPORT_SYMBOL(i2o_find_iop);
1316EXPORT_SYMBOL(i2o_iop_find_device);
1317EXPORT_SYMBOL(i2o_event_register);
1318EXPORT_SYMBOL(i2o_status_get);
1319EXPORT_SYMBOL(i2o_controllers);