blob: f86abb42bf89dcb4c895b94389885fc0f9964462 [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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <linux/sched.h>
Markus Lidel9e875452005-06-23 22:02:21 -070032#include "core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Markus Lidel9e875452005-06-23 22:02:21 -070034#define OSM_NAME "i2o"
35#define OSM_VERSION "1.288"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define OSM_DESCRIPTION "I2O subsystem"
37
38/* global I2O controller list */
39LIST_HEAD(i2o_controllers);
40
41/*
42 * global I2O System Table. Contains information about all the IOPs in the
43 * system. Used to inform IOPs about each others existence.
44 */
45static struct i2o_dma i2o_systab;
46
47static int i2o_hrt_get(struct i2o_controller *c);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * i2o_msg_get_wait - obtain an I2O message from the IOP
51 * @c: I2O controller
52 * @msg: pointer to a I2O message pointer
53 * @wait: how long to wait until timeout
54 *
55 * This function waits up to wait seconds for a message slot to be
56 * available.
57 *
58 * On a success the message is returned and the pointer to the message is
59 * set in msg. The returned message is the physical page frame offset
60 * address from the read port (see the i2o spec). If no message is
61 * available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
62 */
Markus Lidela1a5ea72006-01-06 00:19:29 -080063struct i2o_message *i2o_msg_get_wait(struct i2o_controller *c, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 unsigned long timeout = jiffies + wait * HZ;
Markus Lidela1a5ea72006-01-06 00:19:29 -080066 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Markus Lidela1a5ea72006-01-06 00:19:29 -080068 while (IS_ERR(msg = i2o_msg_get(c))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (time_after(jiffies, timeout)) {
Markus Lidel9e875452005-06-23 22:02:21 -070070 osm_debug("%s: Timeout waiting for message frame.\n",
71 c->name);
Markus Lidela1a5ea72006-01-06 00:19:29 -080072 return ERR_PTR(-ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Nishanth Aravamudan65210182005-11-07 01:01:19 -080074 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
Markus Lidela1a5ea72006-01-06 00:19:29 -080077 return msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80#if BITS_PER_LONG == 64
81/**
82 * i2o_cntxt_list_add - Append a pointer to context list and return a id
83 * @c: controller to which the context list belong
84 * @ptr: pointer to add to the context list
85 *
86 * Because the context field in I2O is only 32-bit large, on 64-bit the
87 * pointer is to large to fit in the context field. The i2o_cntxt_list
88 * functions therefore map pointers to context fields.
89 *
90 * Returns context id > 0 on success or 0 on failure.
91 */
92u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
93{
94 struct i2o_context_list_element *entry;
95 unsigned long flags;
96
97 if (!ptr)
Markus Lidelf33213e2005-06-23 22:02:23 -070098 osm_err("%s: couldn't add NULL pointer to context list!\n",
99 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
102 if (!entry) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700103 osm_err("%s: Could not allocate memory for context list element"
104 "\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106 }
107
108 entry->ptr = ptr;
109 entry->timestamp = jiffies;
110 INIT_LIST_HEAD(&entry->list);
111
112 spin_lock_irqsave(&c->context_list_lock, flags);
113
114 if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
115 atomic_inc(&c->context_list_counter);
116
117 entry->context = atomic_read(&c->context_list_counter);
118
119 list_add(&entry->list, &c->context_list);
120
121 spin_unlock_irqrestore(&c->context_list_lock, flags);
122
Markus Lidelf33213e2005-06-23 22:02:23 -0700123 osm_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return entry->context;
126};
127
128/**
129 * i2o_cntxt_list_remove - Remove a pointer from the context list
130 * @c: controller to which the context list belong
131 * @ptr: pointer which should be removed from the context list
132 *
133 * Removes a previously added pointer from the context list and returns
134 * the matching context id.
135 *
136 * Returns context id on succes or 0 on failure.
137 */
138u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
139{
140 struct i2o_context_list_element *entry;
141 u32 context = 0;
142 unsigned long flags;
143
144 spin_lock_irqsave(&c->context_list_lock, flags);
145 list_for_each_entry(entry, &c->context_list, list)
146 if (entry->ptr == ptr) {
147 list_del(&entry->list);
148 context = entry->context;
149 kfree(entry);
150 break;
151 }
152 spin_unlock_irqrestore(&c->context_list_lock, flags);
153
154 if (!context)
Markus Lidelf33213e2005-06-23 22:02:23 -0700155 osm_warn("%s: Could not remove nonexistent ptr %p\n", c->name,
156 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Markus Lidelf33213e2005-06-23 22:02:23 -0700158 osm_debug("%s: remove ptr from context list %d -> %p\n", c->name,
159 context, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return context;
162};
163
164/**
165 * i2o_cntxt_list_get - Get a pointer from the context list and remove it
166 * @c: controller to which the context list belong
167 * @context: context id to which the pointer belong
168 *
169 * Returns pointer to the matching context id on success or NULL on
170 * failure.
171 */
172void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
173{
174 struct i2o_context_list_element *entry;
175 unsigned long flags;
176 void *ptr = NULL;
177
178 spin_lock_irqsave(&c->context_list_lock, flags);
179 list_for_each_entry(entry, &c->context_list, list)
180 if (entry->context == context) {
181 list_del(&entry->list);
182 ptr = entry->ptr;
183 kfree(entry);
184 break;
185 }
186 spin_unlock_irqrestore(&c->context_list_lock, flags);
187
188 if (!ptr)
Markus Lidelf33213e2005-06-23 22:02:23 -0700189 osm_warn("%s: context id %d not found\n", c->name, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Markus Lidelf33213e2005-06-23 22:02:23 -0700191 osm_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
192 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 return ptr;
195};
196
197/**
198 * i2o_cntxt_list_get_ptr - Get a context id from the context list
199 * @c: controller to which the context list belong
200 * @ptr: pointer to which the context id should be fetched
201 *
202 * Returns context id which matches to the pointer on succes or 0 on
203 * failure.
204 */
205u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
206{
207 struct i2o_context_list_element *entry;
208 u32 context = 0;
209 unsigned long flags;
210
211 spin_lock_irqsave(&c->context_list_lock, flags);
212 list_for_each_entry(entry, &c->context_list, list)
213 if (entry->ptr == ptr) {
214 context = entry->context;
215 break;
216 }
217 spin_unlock_irqrestore(&c->context_list_lock, flags);
218
219 if (!context)
Markus Lidelf33213e2005-06-23 22:02:23 -0700220 osm_warn("%s: Could not find nonexistent ptr %p\n", c->name,
221 ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Markus Lidelf33213e2005-06-23 22:02:23 -0700223 osm_debug("%s: get context id from context list %p -> %d\n", c->name,
224 ptr, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return context;
227};
228#endif
229
230/**
231 * i2o_iop_find - Find an I2O controller by id
232 * @unit: unit number of the I2O controller to search for
233 *
234 * Lookup the I2O controller on the controller list.
235 *
236 * Returns pointer to the I2O controller on success or NULL if not found.
237 */
238struct i2o_controller *i2o_find_iop(int unit)
239{
240 struct i2o_controller *c;
241
242 list_for_each_entry(c, &i2o_controllers, list) {
243 if (c->unit == unit)
244 return c;
245 }
246
247 return NULL;
248};
249
250/**
251 * i2o_iop_find_device - Find a I2O device on an I2O controller
252 * @c: I2O controller where the I2O device hangs on
253 * @tid: TID of the I2O device to search for
254 *
255 * Searches the devices of the I2O controller for a device with TID tid and
256 * returns it.
257 *
258 * Returns a pointer to the I2O device if found, otherwise NULL.
259 */
260struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
261{
262 struct i2o_device *dev;
263
264 list_for_each_entry(dev, &c->devices, list)
265 if (dev->lct_data.tid == tid)
266 return dev;
267
268 return NULL;
269};
270
271/**
272 * i2o_quiesce_controller - quiesce controller
273 * @c: controller
274 *
275 * Quiesce an IOP. Causes IOP to make external operation quiescent
276 * (i2o 'READY' state). Internal operation of the IOP continues normally.
277 *
278 * Returns 0 on success or negative error code on failure.
279 */
280static int i2o_iop_quiesce(struct i2o_controller *c)
281{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800282 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 i2o_status_block *sb = c->status_block.virt;
284 int rc;
285
286 i2o_status_get(c);
287
288 /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
289 if ((sb->iop_state != ADAPTER_STATE_READY) &&
290 (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
291 return 0;
292
Markus Lidela1a5ea72006-01-06 00:19:29 -0800293 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
294 if (IS_ERR(msg))
295 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Markus Lidela1a5ea72006-01-06 00:19:29 -0800297 msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
298 msg->u.head[1] =
299 cpu_to_le32(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 |
300 ADAPTER_TID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* Long timeout needed for quiesce if lots of devices */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800303 if ((rc = i2o_msg_post_wait(c, msg, 240)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700304 osm_info("%s: Unable to quiesce (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700306 osm_debug("%s: Quiesced.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 i2o_status_get(c); // Entered READY state
309
310 return rc;
311};
312
313/**
314 * i2o_iop_enable - move controller from ready to OPERATIONAL
315 * @c: I2O controller
316 *
317 * Enable IOP. This allows the IOP to resume external operations and
318 * reverses the effect of a quiesce. Returns zero or an error code if
319 * an error occurs.
320 */
321static int i2o_iop_enable(struct i2o_controller *c)
322{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800323 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 i2o_status_block *sb = c->status_block.virt;
325 int rc;
326
327 i2o_status_get(c);
328
329 /* Enable only allowed on READY state */
330 if (sb->iop_state != ADAPTER_STATE_READY)
331 return -EINVAL;
332
Markus Lidela1a5ea72006-01-06 00:19:29 -0800333 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
334 if (IS_ERR(msg))
335 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Markus Lidela1a5ea72006-01-06 00:19:29 -0800337 msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
338 msg->u.head[1] =
339 cpu_to_le32(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 |
340 ADAPTER_TID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* How long of a timeout do we need? */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800343 if ((rc = i2o_msg_post_wait(c, msg, 240)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700344 osm_err("%s: Could not enable (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700346 osm_debug("%s: Enabled.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 i2o_status_get(c); // entered OPERATIONAL state
349
350 return rc;
351};
352
353/**
354 * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
355 *
356 * Quiesce all I2O controllers which are connected to the system.
357 */
358static inline void i2o_iop_quiesce_all(void)
359{
360 struct i2o_controller *c, *tmp;
361
362 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
363 if (!c->no_quiesce)
364 i2o_iop_quiesce(c);
365 }
366};
367
368/**
369 * i2o_iop_enable_all - Enables all controllers on the system
370 *
371 * Enables all I2O controllers which are connected to the system.
372 */
373static inline void i2o_iop_enable_all(void)
374{
375 struct i2o_controller *c, *tmp;
376
377 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
378 i2o_iop_enable(c);
379};
380
381/**
382 * i2o_clear_controller - Bring I2O controller into HOLD state
383 * @c: controller
384 *
385 * Clear an IOP to HOLD state, ie. terminate external operations, clear all
386 * input queues and prepare for a system restart. IOP's internal operation
387 * continues normally and the outbound queue is alive. The IOP is not
388 * expected to rebuild its LCT.
389 *
390 * Returns 0 on success or negative error code on failure.
391 */
392static int i2o_iop_clear(struct i2o_controller *c)
393{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800394 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int rc;
396
Markus Lidela1a5ea72006-01-06 00:19:29 -0800397 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
398 if (IS_ERR(msg))
399 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Quiesce all IOPs first */
402 i2o_iop_quiesce_all();
403
Markus Lidela1a5ea72006-01-06 00:19:29 -0800404 msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
405 msg->u.head[1] =
406 cpu_to_le32(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 |
407 ADAPTER_TID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Markus Lidela1a5ea72006-01-06 00:19:29 -0800409 if ((rc = i2o_msg_post_wait(c, msg, 30)))
Markus Lidelf33213e2005-06-23 22:02:23 -0700410 osm_info("%s: Unable to clear (status=%#x).\n", c->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700412 osm_debug("%s: Cleared.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Enable all IOPs */
415 i2o_iop_enable_all();
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return rc;
418}
419
420/**
Markus Lidelf10378f2005-06-23 22:02:16 -0700421 * i2o_iop_init_outbound_queue - setup the outbound message queue
422 * @c: I2O controller
423 *
424 * Clear and (re)initialize IOP's outbound queue and post the message
425 * frames to the IOP.
426 *
Markus Lidela1a5ea72006-01-06 00:19:29 -0800427 * Returns 0 on success or negative error code on failure.
Markus Lidelf10378f2005-06-23 22:02:16 -0700428 */
429static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
430{
Markus Lidelf10378f2005-06-23 22:02:16 -0700431 u32 m;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800432 volatile u8 *status = c->status.virt;
433 struct i2o_message *msg;
Markus Lidelf10378f2005-06-23 22:02:16 -0700434 ulong timeout;
435 int i;
436
437 osm_debug("%s: Initializing Outbound Queue...\n", c->name);
438
Markus Lidel9e875452005-06-23 22:02:21 -0700439 memset(c->status.virt, 0, 4);
Markus Lidelf10378f2005-06-23 22:02:16 -0700440
Markus Lidela1a5ea72006-01-06 00:19:29 -0800441 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
442 if (IS_ERR(msg))
443 return PTR_ERR(msg);
Markus Lidelf10378f2005-06-23 22:02:16 -0700444
Markus Lidela1a5ea72006-01-06 00:19:29 -0800445 msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);
446 msg->u.head[1] =
447 cpu_to_le32(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 |
448 ADAPTER_TID);
449 msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
450 msg->u.s.tcntxt = cpu_to_le32(0x00000000);
451 msg->body[0] = cpu_to_le32(PAGE_SIZE);
Markus Lidelf10378f2005-06-23 22:02:16 -0700452 /* Outbound msg frame size in words and Initcode */
Markus Lidela1a5ea72006-01-06 00:19:29 -0800453 msg->body[1] = cpu_to_le32(I2O_OUTBOUND_MSG_FRAME_SIZE << 16 | 0x80);
454 msg->body[2] = cpu_to_le32(0xd0000004);
455 msg->body[3] = cpu_to_le32(i2o_dma_low(c->status.phys));
456 msg->body[4] = cpu_to_le32(i2o_dma_high(c->status.phys));
Markus Lidelf10378f2005-06-23 22:02:16 -0700457
Markus Lidela1a5ea72006-01-06 00:19:29 -0800458 i2o_msg_post(c, msg);
Markus Lidelf10378f2005-06-23 22:02:16 -0700459
460 timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
461 while (*status <= I2O_CMD_IN_PROGRESS) {
462 if (time_after(jiffies, timeout)) {
463 osm_warn("%s: Timeout Initializing\n", c->name);
464 return -ETIMEDOUT;
465 }
Nishanth Aravamudan65210182005-11-07 01:01:19 -0800466 schedule_timeout_uninterruptible(1);
Markus Lidelf10378f2005-06-23 22:02:16 -0700467 }
468
469 m = c->out_queue.phys;
470
471 /* Post frames */
Markus Lidel9e875452005-06-23 22:02:21 -0700472 for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700473 i2o_flush_reply(c, m);
474 udelay(1); /* Promise */
Markus Lidel9e875452005-06-23 22:02:21 -0700475 m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
Markus Lidelf10378f2005-06-23 22:02:16 -0700476 }
477
478 return 0;
479}
480
481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * i2o_iop_reset - reset an I2O controller
483 * @c: controller to reset
484 *
485 * Reset the IOP into INIT state and wait until IOP gets into RESET state.
486 * Terminate all external operations, clear IOP's inbound and outbound
487 * queues, terminate all DDMs, and reload the IOP's operating environment
488 * and all local DDMs. The IOP rebuilds its LCT.
489 */
490static int i2o_iop_reset(struct i2o_controller *c)
491{
Markus Lidel9e875452005-06-23 22:02:21 -0700492 volatile u8 *status = c->status.virt;
Markus Lidela1a5ea72006-01-06 00:19:29 -0800493 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned long timeout;
495 i2o_status_block *sb = c->status_block.virt;
496 int rc = 0;
497
Markus Lidel9e875452005-06-23 22:02:21 -0700498 osm_debug("%s: Resetting controller\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Markus Lidela1a5ea72006-01-06 00:19:29 -0800500 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
501 if (IS_ERR(msg))
502 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Markus Lidel9e875452005-06-23 22:02:21 -0700504 memset(c->status_block.virt, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* Quiesce all IOPs first */
507 i2o_iop_quiesce_all();
508
Markus Lidela1a5ea72006-01-06 00:19:29 -0800509 msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0);
510 msg->u.head[1] =
511 cpu_to_le32(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 |
512 ADAPTER_TID);
513 msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
514 msg->u.s.tcntxt = cpu_to_le32(0x00000000);
515 msg->body[0] = cpu_to_le32(0x00000000);
516 msg->body[1] = cpu_to_le32(0x00000000);
517 msg->body[2] = cpu_to_le32(i2o_dma_low(c->status.phys));
518 msg->body[3] = cpu_to_le32(i2o_dma_high(c->status.phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Markus Lidela1a5ea72006-01-06 00:19:29 -0800520 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Wait for a reply */
523 timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
524 while (!*status) {
Markus Lidelf10378f2005-06-23 22:02:16 -0700525 if (time_after(jiffies, timeout))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Nishanth Aravamudan65210182005-11-07 01:01:19 -0800528 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Markus Lidelf10378f2005-06-23 22:02:16 -0700531 switch (*status) {
532 case I2O_CMD_REJECTED:
533 osm_warn("%s: IOP reset rejected\n", c->name);
534 rc = -EPERM;
535 break;
536
537 case I2O_CMD_IN_PROGRESS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * Once the reset is sent, the IOP goes into the INIT state
Markus Lidelf10378f2005-06-23 22:02:16 -0700540 * which is indeterminate. We need to wait until the IOP has
541 * rebooted before we can let the system talk to it. We read
542 * the inbound Free_List until a message is available. If we
543 * can't read one in the given ammount of time, we assume the
544 * IOP could not reboot properly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Markus Lidelf33213e2005-06-23 22:02:23 -0700546 osm_debug("%s: Reset in progress, waiting for reboot...\n",
547 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Markus Lidela1a5ea72006-01-06 00:19:29 -0800549 while (IS_ERR(msg = i2o_msg_get_wait(c, I2O_TIMEOUT_RESET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (time_after(jiffies, timeout)) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700551 osm_err("%s: IOP reset timeout.\n", c->name);
Markus Lidela1a5ea72006-01-06 00:19:29 -0800552 rc = PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto exit;
554 }
Nishanth Aravamudan65210182005-11-07 01:01:19 -0800555 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Markus Lidela1a5ea72006-01-06 00:19:29 -0800557 i2o_msg_nop(c, msg);
Markus Lidelf10378f2005-06-23 22:02:16 -0700558
559 /* from here all quiesce commands are safe */
560 c->no_quiesce = 0;
561
562 /* verify if controller is in state RESET */
563 i2o_status_get(c);
564
565 if (!c->promise && (sb->iop_state != ADAPTER_STATE_RESET))
566 osm_warn("%s: reset completed, but adapter not in RESET"
567 " state.\n", c->name);
568 else
569 osm_debug("%s: reset completed.\n", c->name);
570
571 break;
572
573 default:
574 osm_err("%s: IOP reset timeout.\n", c->name);
575 rc = -ETIMEDOUT;
576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 exit:
580 /* Enable all IOPs */
581 i2o_iop_enable_all();
582
583 return rc;
584};
585
586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * i2o_iop_activate - Bring controller up to HOLD
588 * @c: controller
589 *
590 * This function brings an I2O controller into HOLD state. The adapter
591 * is reset if necessary and then the queues and resource table are read.
592 *
593 * Returns 0 on success or negative error code on failure.
594 */
595static int i2o_iop_activate(struct i2o_controller *c)
596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 i2o_status_block *sb = c->status_block.virt;
598 int rc;
Markus Lidelf10378f2005-06-23 22:02:16 -0700599 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
602 /* In READY state, Get status */
603
604 rc = i2o_status_get(c);
605 if (rc) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700606 osm_info("%s: Unable to obtain status, attempting a reset.\n",
607 c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700608 rc = i2o_iop_reset(c);
609 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return rc;
611 }
612
613 if (sb->i2o_version > I2OVER15) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700614 osm_err("%s: Not running version 1.5 of the I2O Specification."
615 "\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -ENODEV;
617 }
618
619 switch (sb->iop_state) {
620 case ADAPTER_STATE_FAULTED:
Markus Lidelf33213e2005-06-23 22:02:23 -0700621 osm_err("%s: hardware fault\n", c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700622 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 case ADAPTER_STATE_READY:
625 case ADAPTER_STATE_OPERATIONAL:
626 case ADAPTER_STATE_HOLD:
627 case ADAPTER_STATE_FAILED:
Markus Lidelf33213e2005-06-23 22:02:23 -0700628 osm_debug("%s: already running, trying to reset...\n", c->name);
Markus Lidelf10378f2005-06-23 22:02:16 -0700629 rc = i2o_iop_reset(c);
630 if (rc)
631 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Markus Lidelf10378f2005-06-23 22:02:16 -0700634 /* preserve state */
635 state = sb->iop_state;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 rc = i2o_iop_init_outbound_queue(c);
638 if (rc)
639 return rc;
640
Markus Lidelf10378f2005-06-23 22:02:16 -0700641 /* if adapter was not in RESET state clear now */
642 if (state != ADAPTER_STATE_RESET)
643 i2o_iop_clear(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Markus Lidelf10378f2005-06-23 22:02:16 -0700645 i2o_status_get(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Markus Lidelf10378f2005-06-23 22:02:16 -0700647 if (sb->iop_state != ADAPTER_STATE_HOLD) {
648 osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
649 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Markus Lidelf10378f2005-06-23 22:02:16 -0700652 return i2o_hrt_get(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653};
654
655/**
656 * i2o_iop_systab_set - Set the I2O System Table of the specified IOP
657 * @c: I2O controller to which the system table should be send
658 *
659 * Before the systab could be set i2o_systab_build() must be called.
660 *
661 * Returns 0 on success or negative error code on failure.
662 */
663static int i2o_iop_systab_set(struct i2o_controller *c)
664{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800665 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 i2o_status_block *sb = c->status_block.virt;
667 struct device *dev = &c->pdev->dev;
668 struct resource *root;
669 int rc;
670
671 if (sb->current_mem_size < sb->desired_mem_size) {
672 struct resource *res = &c->mem_resource;
673 res->name = c->pdev->bus->name;
674 res->flags = IORESOURCE_MEM;
675 res->start = 0;
676 res->end = 0;
Markus Lidelf33213e2005-06-23 22:02:23 -0700677 osm_info("%s: requires private memory resources.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 root = pci_find_parent_resource(c->pdev, res);
679 if (root == NULL)
Markus Lidelf33213e2005-06-23 22:02:23 -0700680 osm_warn("%s: Can't find parent resource!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 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 */
682 NULL, NULL) >= 0) {
683 c->mem_alloc = 1;
684 sb->current_mem_size = 1 + res->end - res->start;
685 sb->current_mem_base = res->start;
Markus Lidelf33213e2005-06-23 22:02:23 -0700686 osm_info("%s: allocated %ld bytes of PCI memory at "
687 "0x%08lX.\n", c->name,
688 1 + res->end - res->start, res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 }
691
692 if (sb->current_io_size < sb->desired_io_size) {
693 struct resource *res = &c->io_resource;
694 res->name = c->pdev->bus->name;
695 res->flags = IORESOURCE_IO;
696 res->start = 0;
697 res->end = 0;
Markus Lidelf33213e2005-06-23 22:02:23 -0700698 osm_info("%s: requires private memory resources.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 root = pci_find_parent_resource(c->pdev, res);
700 if (root == NULL)
Markus Lidelf33213e2005-06-23 22:02:23 -0700701 osm_warn("%s: Can't find parent resource!\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 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 */
703 NULL, NULL) >= 0) {
704 c->io_alloc = 1;
705 sb->current_io_size = 1 + res->end - res->start;
706 sb->current_mem_base = res->start;
Markus Lidelf33213e2005-06-23 22:02:23 -0700707 osm_info("%s: allocated %ld bytes of PCI I/O at 0x%08lX"
708 ".\n", c->name, 1 + res->end - res->start,
709 res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 }
712
Markus Lidela1a5ea72006-01-06 00:19:29 -0800713 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
714 if (IS_ERR(msg))
715 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
718 PCI_DMA_TODEVICE);
719 if (!i2o_systab.phys) {
Markus Lidela1a5ea72006-01-06 00:19:29 -0800720 i2o_msg_nop(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENOMEM;
722 }
723
Markus Lidela1a5ea72006-01-06 00:19:29 -0800724 msg->u.head[0] = cpu_to_le32(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6);
725 msg->u.head[1] =
726 cpu_to_le32(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 |
727 ADAPTER_TID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /*
730 * Provide three SGL-elements:
731 * System table (SysTab), Private memory space declaration and
732 * Private i/o space declaration
733 *
734 * FIXME: is this still true?
735 * Nasty one here. We can't use dma_alloc_coherent to send the
736 * same table to everyone. We have to go remap it for them all
737 */
738
Markus Lidela1a5ea72006-01-06 00:19:29 -0800739 msg->body[0] = cpu_to_le32(c->unit + 2);
740 msg->body[1] = cpu_to_le32(0x00000000);
741 msg->body[2] = cpu_to_le32(0x54000000 | i2o_systab.len);
742 msg->body[3] = cpu_to_le32(i2o_systab.phys);
743 msg->body[4] = cpu_to_le32(0x54000000 | sb->current_mem_size);
744 msg->body[5] = cpu_to_le32(sb->current_mem_base);
745 msg->body[6] = cpu_to_le32(0xd4000000 | sb->current_io_size);
746 msg->body[6] = cpu_to_le32(sb->current_io_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Markus Lidela1a5ea72006-01-06 00:19:29 -0800748 rc = i2o_msg_post_wait(c, msg, 120);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
751 PCI_DMA_TODEVICE);
752
753 if (rc < 0)
Markus Lidelf33213e2005-06-23 22:02:23 -0700754 osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
755 -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 else
Markus Lidelf33213e2005-06-23 22:02:23 -0700757 osm_debug("%s: SysTab set.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 i2o_status_get(c); // Entered READY state
760
761 return rc;
762}
763
764/**
765 * i2o_iop_online - Bring a controller online into OPERATIONAL state.
766 * @c: I2O controller
767 *
768 * Send the system table and enable the I2O controller.
769 *
770 * Returns 0 on success or negativer error code on failure.
771 */
772static int i2o_iop_online(struct i2o_controller *c)
773{
774 int rc;
775
776 rc = i2o_iop_systab_set(c);
777 if (rc)
778 return rc;
779
780 /* In READY state */
Markus Lidelf33213e2005-06-23 22:02:23 -0700781 osm_debug("%s: Attempting to enable...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 rc = i2o_iop_enable(c);
783 if (rc)
784 return rc;
785
786 return 0;
787};
788
789/**
790 * i2o_iop_remove - Remove the I2O controller from the I2O core
791 * @c: I2O controller
792 *
793 * Remove the I2O controller from the I2O core. If devices are attached to
794 * the controller remove these also and finally reset the controller.
795 */
796void i2o_iop_remove(struct i2o_controller *c)
797{
798 struct i2o_device *dev, *tmp;
799
Markus Lidelf33213e2005-06-23 22:02:23 -0700800 osm_debug("%s: deleting controller\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 i2o_driver_notify_controller_remove_all(c);
803
804 list_del(&c->list);
805
806 list_for_each_entry_safe(dev, tmp, &c->devices, list)
807 i2o_device_remove(dev);
808
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -0700809 class_device_unregister(c->classdev);
Markus Lidelf88e1192005-06-23 22:02:14 -0700810 device_del(&c->device);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* Ask the IOP to switch to RESET state */
813 i2o_iop_reset(c);
Markus Lidelf88e1192005-06-23 22:02:14 -0700814
815 put_device(&c->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/**
819 * i2o_systab_build - Build system table
820 *
821 * The system table contains information about all the IOPs in the system
822 * (duh) and is used by the Executives on the IOPs to establish peer2peer
823 * connections. We're not supporting peer2peer at the moment, but this
824 * will be needed down the road for things like lan2lan forwarding.
825 *
826 * Returns 0 on success or negative error code on failure.
827 */
828static int i2o_systab_build(void)
829{
830 struct i2o_controller *c, *tmp;
831 int num_controllers = 0;
832 u32 change_ind = 0;
833 int count = 0;
834 struct i2o_sys_tbl *systab = i2o_systab.virt;
835
836 list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
837 num_controllers++;
838
839 if (systab) {
840 change_ind = systab->change_ind;
841 kfree(i2o_systab.virt);
842 }
843
844 /* Header + IOPs */
845 i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
846 sizeof(struct i2o_sys_tbl_entry);
847
848 systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
849 if (!systab) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700850 osm_err("unable to allocate memory for System Table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -ENOMEM;
852 }
853 memset(systab, 0, i2o_systab.len);
854
855 systab->version = I2OVERSION;
856 systab->change_ind = change_ind + 1;
857
858 list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
859 i2o_status_block *sb;
860
861 if (count >= num_controllers) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700862 osm_err("controller added while building system table"
863 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865 }
866
867 sb = c->status_block.virt;
868
869 /*
870 * Get updated IOP state so we have the latest information
871 *
872 * We should delete the controller at this point if it
873 * doesn't respond since if it's not on the system table
874 * it is techninically not part of the I2O subsystem...
875 */
876 if (unlikely(i2o_status_get(c))) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700877 osm_err("%s: Deleting b/c could not get status while "
878 "attempting to build system table\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 i2o_iop_remove(c);
880 continue; // try the next one
881 }
882
883 systab->iops[count].org_id = sb->org_id;
884 systab->iops[count].iop_id = c->unit + 2;
885 systab->iops[count].seg_num = 0;
886 systab->iops[count].i2o_version = sb->i2o_version;
887 systab->iops[count].iop_state = sb->iop_state;
888 systab->iops[count].msg_type = sb->msg_type;
889 systab->iops[count].frame_size = sb->inbound_frame_size;
890 systab->iops[count].last_changed = change_ind;
891 systab->iops[count].iop_capabilities = sb->iop_capabilities;
Markus Lidelf88e1192005-06-23 22:02:14 -0700892 systab->iops[count].inbound_low =
893 i2o_dma_low(c->base.phys + I2O_IN_PORT);
894 systab->iops[count].inbound_high =
895 i2o_dma_high(c->base.phys + I2O_IN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 count++;
898 }
899
900 systab->num_entries = count;
901
902 return 0;
903};
904
905/**
906 * i2o_parse_hrt - Parse the hardware resource table.
907 * @c: I2O controller
908 *
909 * We don't do anything with it except dumping it (in debug mode).
910 *
911 * Returns 0.
912 */
913static int i2o_parse_hrt(struct i2o_controller *c)
914{
915 i2o_dump_hrt(c);
916 return 0;
917};
918
919/**
920 * i2o_status_get - Get the status block from the I2O controller
921 * @c: I2O controller
922 *
923 * Issue a status query on the controller. This updates the attached
924 * status block. The status block could then be accessed through
925 * c->status_block.
926 *
927 * Returns 0 on sucess or negative error code on failure.
928 */
929int i2o_status_get(struct i2o_controller *c)
930{
Markus Lidela1a5ea72006-01-06 00:19:29 -0800931 struct i2o_message *msg;
Markus Lidel9e875452005-06-23 22:02:21 -0700932 volatile u8 *status_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned long timeout;
934
935 status_block = (u8 *) c->status_block.virt;
Markus Lidel9e875452005-06-23 22:02:21 -0700936 memset(c->status_block.virt, 0, sizeof(i2o_status_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Markus Lidela1a5ea72006-01-06 00:19:29 -0800938 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
939 if (IS_ERR(msg))
940 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Markus Lidela1a5ea72006-01-06 00:19:29 -0800942 msg->u.head[0] = cpu_to_le32(NINE_WORD_MSG_SIZE | SGL_OFFSET_0);
943 msg->u.head[1] =
944 cpu_to_le32(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 |
945 ADAPTER_TID);
946 msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
947 msg->u.s.tcntxt = cpu_to_le32(0x00000000);
948 msg->body[0] = cpu_to_le32(0x00000000);
949 msg->body[1] = cpu_to_le32(0x00000000);
950 msg->body[2] = cpu_to_le32(i2o_dma_low(c->status_block.phys));
951 msg->body[3] = cpu_to_le32(i2o_dma_high(c->status_block.phys));
952 msg->body[4] = cpu_to_le32(sizeof(i2o_status_block)); /* always 88 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Markus Lidela1a5ea72006-01-06 00:19:29 -0800954 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* Wait for a reply */
957 timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
958 while (status_block[87] != 0xFF) {
959 if (time_after(jiffies, timeout)) {
Markus Lidelf33213e2005-06-23 22:02:23 -0700960 osm_err("%s: Get status timeout.\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ETIMEDOUT;
962 }
963
Nishanth Aravamudan65210182005-11-07 01:01:19 -0800964 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
967#ifdef DEBUG
968 i2o_debug_state(c);
969#endif
970
971 return 0;
972}
973
974/*
975 * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
976 * @c: I2O controller from which the HRT should be fetched
977 *
978 * The HRT contains information about possible hidden devices but is
979 * mostly useless to us.
980 *
981 * Returns 0 on success or negativer error code on failure.
982 */
983static int i2o_hrt_get(struct i2o_controller *c)
984{
985 int rc;
986 int i;
987 i2o_hrt *hrt = c->hrt.virt;
988 u32 size = sizeof(i2o_hrt);
989 struct device *dev = &c->pdev->dev;
990
991 for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
Markus Lidela1a5ea72006-01-06 00:19:29 -0800992 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Markus Lidela1a5ea72006-01-06 00:19:29 -0800994 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
995 if (IS_ERR(msg))
996 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Markus Lidela1a5ea72006-01-06 00:19:29 -0800998 msg->u.head[0] = cpu_to_le32(SIX_WORD_MSG_SIZE | SGL_OFFSET_4);
999 msg->u.head[1] =
1000 cpu_to_le32(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 |
1001 ADAPTER_TID);
1002 msg->body[0] = cpu_to_le32(0xd0000000 | c->hrt.len);
1003 msg->body[1] = cpu_to_le32(c->hrt.phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Markus Lidela1a5ea72006-01-06 00:19:29 -08001005 rc = i2o_msg_post_wait_mem(c, msg, 20, &c->hrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (rc < 0) {
Markus Lidelf33213e2005-06-23 22:02:23 -07001008 osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
1009 -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return rc;
1011 }
1012
1013 size = hrt->num_entries * hrt->entry_len << 2;
1014 if (size > c->hrt.len) {
1015 if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1016 return -ENOMEM;
1017 else
1018 hrt = c->hrt.virt;
1019 } else
1020 return i2o_parse_hrt(c);
1021 }
1022
Markus Lidelf33213e2005-06-23 22:02:23 -07001023 osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
1024 I2O_HRT_GET_TRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 return -EBUSY;
1027}
1028
1029/**
Markus Lidelf88e1192005-06-23 22:02:14 -07001030 * i2o_iop_free - Free the i2o_controller struct
1031 * @c: I2O controller to free
1032 */
1033void i2o_iop_free(struct i2o_controller *c)
1034{
Markus Lidela1a5ea72006-01-06 00:19:29 -08001035 i2o_pool_free(&c->in_msg);
Markus Lidelf88e1192005-06-23 22:02:14 -07001036 kfree(c);
1037};
1038
Markus Lidelf88e1192005-06-23 22:02:14 -07001039/**
1040 * i2o_iop_release - release the memory for a I2O controller
1041 * @dev: I2O controller which should be released
1042 *
1043 * Release the allocated memory. This function is called if refcount of
1044 * device reaches 0 automatically.
1045 */
1046static void i2o_iop_release(struct device *dev)
1047{
1048 struct i2o_controller *c = to_i2o_controller(dev);
1049
1050 i2o_iop_free(c);
1051};
1052
Markus Lidel9e875452005-06-23 22:02:21 -07001053/* I2O controller class */
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001054static struct class *i2o_controller_class;
Markus Lidel9e875452005-06-23 22:02:21 -07001055
Markus Lidelf88e1192005-06-23 22:02:14 -07001056/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1058 *
1059 * Allocate the necessary memory for a i2o_controller struct and
Markus Lidela1a5ea72006-01-06 00:19:29 -08001060 * initialize the lists and message mempool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
1062 * Returns a pointer to the I2O controller or a negative error code on
1063 * failure.
1064 */
1065struct i2o_controller *i2o_iop_alloc(void)
1066{
1067 static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1068 struct i2o_controller *c;
Markus Lidela1a5ea72006-01-06 00:19:29 -08001069 char poolname[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 c = kmalloc(sizeof(*c), GFP_KERNEL);
1072 if (!c) {
Markus Lidelf33213e2005-06-23 22:02:23 -07001073 osm_err("i2o: Insufficient memory to allocate a I2O controller."
1074 "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return ERR_PTR(-ENOMEM);
1076 }
1077 memset(c, 0, sizeof(*c));
1078
Markus Lidela1a5ea72006-01-06 00:19:29 -08001079 c->unit = unit++;
1080 sprintf(c->name, "iop%d", c->unit);
1081
1082 snprintf(poolname, sizeof(poolname), "i2o_%s_msg_inpool", c->name);
1083 if (i2o_pool_alloc
1084 (&c->in_msg, poolname, I2O_INBOUND_MSG_FRAME_SIZE * 4,
1085 I2O_MSG_INPOOL_MIN)) {
1086 kfree(c);
1087 return ERR_PTR(-ENOMEM);
1088 };
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 INIT_LIST_HEAD(&c->devices);
1091 spin_lock_init(&c->lock);
1092 init_MUTEX(&c->lct_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Markus Lidelf88e1192005-06-23 22:02:14 -07001094 device_initialize(&c->device);
Markus Lidel9e875452005-06-23 22:02:21 -07001095
Markus Lidelf88e1192005-06-23 22:02:14 -07001096 c->device.release = &i2o_iop_release;
Markus Lidel9e875452005-06-23 22:02:21 -07001097
Markus Lidelf88e1192005-06-23 22:02:14 -07001098 snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#if BITS_PER_LONG == 64
1101 spin_lock_init(&c->context_list_lock);
1102 atomic_set(&c->context_list_counter, 0);
1103 INIT_LIST_HEAD(&c->context_list);
1104#endif
1105
1106 return c;
1107};
1108
1109/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1111 * @c: controller
1112 *
1113 * Initialize the I2O controller and if no error occurs add him to the I2O
1114 * core.
1115 *
1116 * Returns 0 on success or negative error code on failure.
1117 */
1118int i2o_iop_add(struct i2o_controller *c)
1119{
1120 int rc;
1121
Markus Lidel9e875452005-06-23 22:02:21 -07001122 if ((rc = device_add(&c->device))) {
1123 osm_err("%s: could not add controller\n", c->name);
Markus Lidelf88e1192005-06-23 22:02:14 -07001124 goto iop_reset;
1125 }
1126
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -07001127 c->classdev = class_device_create(i2o_controller_class, NULL, MKDEV(0,0),
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001128 &c->device, "iop%d", c->unit);
1129 if (IS_ERR(c->classdev)) {
Markus Lidel9e875452005-06-23 22:02:21 -07001130 osm_err("%s: could not add controller class\n", c->name);
1131 goto device_del;
1132 }
1133
1134 osm_info("%s: Activating I2O controller...\n", c->name);
1135 osm_info("%s: This may take a few minutes if there are many devices\n",
1136 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if ((rc = i2o_iop_activate(c))) {
Markus Lidel9e875452005-06-23 22:02:21 -07001139 osm_err("%s: could not activate controller\n", c->name);
1140 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
Markus Lidel9e875452005-06-23 22:02:21 -07001143 osm_debug("%s: building sys table...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Markus Lidelf88e1192005-06-23 22:02:14 -07001145 if ((rc = i2o_systab_build()))
Markus Lidel9e875452005-06-23 22:02:21 -07001146 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Markus Lidel9e875452005-06-23 22:02:21 -07001148 osm_debug("%s: online controller...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Markus Lidelf88e1192005-06-23 22:02:14 -07001150 if ((rc = i2o_iop_online(c)))
Markus Lidel9e875452005-06-23 22:02:21 -07001151 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Markus Lidel9e875452005-06-23 22:02:21 -07001153 osm_debug("%s: getting LCT...\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Markus Lidelf88e1192005-06-23 22:02:14 -07001155 if ((rc = i2o_exec_lct_get(c)))
Markus Lidel9e875452005-06-23 22:02:21 -07001156 goto class_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 list_add(&c->list, &i2o_controllers);
1159
1160 i2o_driver_notify_controller_add_all(c);
1161
Markus Lidel9e875452005-06-23 22:02:21 -07001162 osm_info("%s: Controller added\n", c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 return 0;
Markus Lidelf88e1192005-06-23 22:02:14 -07001165
Markus Lidel9e875452005-06-23 22:02:21 -07001166 class_del:
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001167 class_device_unregister(c->classdev);
Markus Lidel9e875452005-06-23 22:02:21 -07001168
1169 device_del:
1170 device_del(&c->device);
1171
1172 iop_reset:
Markus Lidelf88e1192005-06-23 22:02:14 -07001173 i2o_iop_reset(c);
1174
1175 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176};
1177
1178/**
1179 * i2o_event_register - Turn on/off event notification for a I2O device
1180 * @dev: I2O device which should receive the event registration request
1181 * @drv: driver which want to get notified
1182 * @tcntxt: transaction context to use with this notifier
1183 * @evt_mask: mask of events
1184 *
1185 * Create and posts an event registration message to the task. No reply
1186 * is waited for, or expected. If you do not want further notifications,
1187 * call the i2o_event_register again with a evt_mask of 0.
1188 *
Markus Lidela1a5ea72006-01-06 00:19:29 -08001189 * Returns 0 on success or negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 */
1191int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1192 int tcntxt, u32 evt_mask)
1193{
1194 struct i2o_controller *c = dev->iop;
Markus Lidela1a5ea72006-01-06 00:19:29 -08001195 struct i2o_message *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Markus Lidela1a5ea72006-01-06 00:19:29 -08001197 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
1198 if (IS_ERR(msg))
1199 return PTR_ERR(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Markus Lidela1a5ea72006-01-06 00:19:29 -08001201 msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
1202 msg->u.head[1] =
1203 cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->
1204 lct_data.tid);
1205 msg->u.s.icntxt = cpu_to_le32(drv->context);
1206 msg->u.s.tcntxt = cpu_to_le32(tcntxt);
1207 msg->body[0] = cpu_to_le32(evt_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Markus Lidela1a5ea72006-01-06 00:19:29 -08001209 i2o_msg_post(c, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 return 0;
1212};
1213
1214/**
1215 * i2o_iop_init - I2O main initialization function
1216 *
1217 * Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1218 * initialize the I2O PCI part and finally initialize I2O device stuff.
1219 *
1220 * Returns 0 on success or negative error code on failure.
1221 */
1222static int __init i2o_iop_init(void)
1223{
1224 int rc = 0;
1225
1226 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1227
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001228 i2o_controller_class = class_create(THIS_MODULE, "i2o_controller");
1229 if (IS_ERR(i2o_controller_class)) {
Markus Lidel9e875452005-06-23 22:02:21 -07001230 osm_err("can't register class i2o_controller\n");
Dmitry Torokhov7bd7b092005-09-29 00:40:07 -05001231 goto exit;
Markus Lidel9e875452005-06-23 22:02:21 -07001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Markus Lidel9e875452005-06-23 22:02:21 -07001234 if ((rc = i2o_driver_init()))
1235 goto class_exit;
1236
1237 if ((rc = i2o_exec_init()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 goto driver_exit;
1239
Markus Lidel9e875452005-06-23 22:02:21 -07001240 if ((rc = i2o_pci_init()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto exec_exit;
1242
1243 return 0;
1244
1245 exec_exit:
1246 i2o_exec_exit();
1247
1248 driver_exit:
1249 i2o_driver_exit();
1250
Markus Lidel9e875452005-06-23 22:02:21 -07001251 class_exit:
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001252 class_destroy(i2o_controller_class);
Markus Lidel9e875452005-06-23 22:02:21 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 exit:
1255 return rc;
1256}
1257
1258/**
1259 * i2o_iop_exit - I2O main exit function
1260 *
1261 * Removes I2O controllers from PCI subsystem and shut down OSMs.
1262 */
1263static void __exit i2o_iop_exit(void)
1264{
1265 i2o_pci_exit();
1266 i2o_exec_exit();
1267 i2o_driver_exit();
Greg Kroah-Hartman607cf4d2005-10-27 22:25:43 -07001268 class_destroy(i2o_controller_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269};
1270
1271module_init(i2o_iop_init);
1272module_exit(i2o_iop_exit);
1273
1274MODULE_AUTHOR("Red Hat Software");
1275MODULE_LICENSE("GPL");
1276MODULE_DESCRIPTION(OSM_DESCRIPTION);
1277MODULE_VERSION(OSM_VERSION);
1278
1279#if BITS_PER_LONG == 64
1280EXPORT_SYMBOL(i2o_cntxt_list_add);
1281EXPORT_SYMBOL(i2o_cntxt_list_get);
1282EXPORT_SYMBOL(i2o_cntxt_list_remove);
1283EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1284#endif
1285EXPORT_SYMBOL(i2o_msg_get_wait);
1286EXPORT_SYMBOL(i2o_msg_nop);
1287EXPORT_SYMBOL(i2o_find_iop);
1288EXPORT_SYMBOL(i2o_iop_find_device);
1289EXPORT_SYMBOL(i2o_event_register);
1290EXPORT_SYMBOL(i2o_status_get);
1291EXPORT_SYMBOL(i2o_controllers);