Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 31 | |
| 32 | #define OSM_VERSION "$Rev$" |
| 33 | #define OSM_DESCRIPTION "I2O subsystem" |
| 34 | |
| 35 | /* global I2O controller list */ |
| 36 | LIST_HEAD(i2o_controllers); |
| 37 | |
| 38 | /* |
| 39 | * global I2O System Table. Contains information about all the IOPs in the |
| 40 | * system. Used to inform IOPs about each others existence. |
| 41 | */ |
| 42 | static struct i2o_dma i2o_systab; |
| 43 | |
| 44 | static int i2o_hrt_get(struct i2o_controller *c); |
| 45 | |
| 46 | /* Module internal functions from other sources */ |
| 47 | extern struct i2o_driver i2o_exec_driver; |
| 48 | extern int i2o_exec_lct_get(struct i2o_controller *); |
| 49 | extern void i2o_device_remove(struct i2o_device *); |
| 50 | |
| 51 | extern int __init i2o_driver_init(void); |
| 52 | extern void __exit i2o_driver_exit(void); |
| 53 | extern int __init i2o_exec_init(void); |
| 54 | extern void __exit i2o_exec_exit(void); |
| 55 | extern int __init i2o_pci_init(void); |
| 56 | extern void __exit i2o_pci_exit(void); |
| 57 | extern int i2o_device_init(void); |
| 58 | extern void i2o_device_exit(void); |
| 59 | |
| 60 | /** |
| 61 | * i2o_msg_nop - Returns a message which is not used |
| 62 | * @c: I2O controller from which the message was created |
| 63 | * @m: message which should be returned |
| 64 | * |
| 65 | * If you fetch a message via i2o_msg_get, and can't use it, you must |
| 66 | * return the message with this function. Otherwise the message frame |
| 67 | * is lost. |
| 68 | */ |
| 69 | void i2o_msg_nop(struct i2o_controller *c, u32 m) |
| 70 | { |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 71 | struct i2o_message __iomem *msg = i2o_msg_in_to_virt(c, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 74 | writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 75 | &msg->u.head[1]); |
| 76 | writel(0, &msg->u.head[2]); |
| 77 | writel(0, &msg->u.head[3]); |
| 78 | i2o_msg_post(c, m); |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * i2o_msg_get_wait - obtain an I2O message from the IOP |
| 83 | * @c: I2O controller |
| 84 | * @msg: pointer to a I2O message pointer |
| 85 | * @wait: how long to wait until timeout |
| 86 | * |
| 87 | * This function waits up to wait seconds for a message slot to be |
| 88 | * available. |
| 89 | * |
| 90 | * On a success the message is returned and the pointer to the message is |
| 91 | * set in msg. The returned message is the physical page frame offset |
| 92 | * address from the read port (see the i2o spec). If no message is |
| 93 | * available returns I2O_QUEUE_EMPTY and msg is leaved untouched. |
| 94 | */ |
| 95 | u32 i2o_msg_get_wait(struct i2o_controller *c, struct i2o_message __iomem **msg, |
| 96 | int wait) |
| 97 | { |
| 98 | unsigned long timeout = jiffies + wait * HZ; |
| 99 | u32 m; |
| 100 | |
| 101 | while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) { |
| 102 | if (time_after(jiffies, timeout)) { |
| 103 | pr_debug("%s: Timeout waiting for message frame.\n", |
| 104 | c->name); |
| 105 | return I2O_QUEUE_EMPTY; |
| 106 | } |
| 107 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 108 | schedule_timeout(1); |
| 109 | } |
| 110 | |
| 111 | return m; |
| 112 | }; |
| 113 | |
| 114 | #if BITS_PER_LONG == 64 |
| 115 | /** |
| 116 | * i2o_cntxt_list_add - Append a pointer to context list and return a id |
| 117 | * @c: controller to which the context list belong |
| 118 | * @ptr: pointer to add to the context list |
| 119 | * |
| 120 | * Because the context field in I2O is only 32-bit large, on 64-bit the |
| 121 | * pointer is to large to fit in the context field. The i2o_cntxt_list |
| 122 | * functions therefore map pointers to context fields. |
| 123 | * |
| 124 | * Returns context id > 0 on success or 0 on failure. |
| 125 | */ |
| 126 | u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr) |
| 127 | { |
| 128 | struct i2o_context_list_element *entry; |
| 129 | unsigned long flags; |
| 130 | |
| 131 | if (!ptr) |
| 132 | printk(KERN_ERR "%s: couldn't add NULL pointer to context list!" |
| 133 | "\n", c->name); |
| 134 | |
| 135 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 136 | if (!entry) { |
| 137 | printk(KERN_ERR "%s: Could not allocate memory for context " |
| 138 | "list element\n", c->name); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | entry->ptr = ptr; |
| 143 | entry->timestamp = jiffies; |
| 144 | INIT_LIST_HEAD(&entry->list); |
| 145 | |
| 146 | spin_lock_irqsave(&c->context_list_lock, flags); |
| 147 | |
| 148 | if (unlikely(atomic_inc_and_test(&c->context_list_counter))) |
| 149 | atomic_inc(&c->context_list_counter); |
| 150 | |
| 151 | entry->context = atomic_read(&c->context_list_counter); |
| 152 | |
| 153 | list_add(&entry->list, &c->context_list); |
| 154 | |
| 155 | spin_unlock_irqrestore(&c->context_list_lock, flags); |
| 156 | |
| 157 | pr_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context); |
| 158 | |
| 159 | return entry->context; |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * i2o_cntxt_list_remove - Remove a pointer from the context list |
| 164 | * @c: controller to which the context list belong |
| 165 | * @ptr: pointer which should be removed from the context list |
| 166 | * |
| 167 | * Removes a previously added pointer from the context list and returns |
| 168 | * the matching context id. |
| 169 | * |
| 170 | * Returns context id on succes or 0 on failure. |
| 171 | */ |
| 172 | u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr) |
| 173 | { |
| 174 | struct i2o_context_list_element *entry; |
| 175 | u32 context = 0; |
| 176 | unsigned long flags; |
| 177 | |
| 178 | spin_lock_irqsave(&c->context_list_lock, flags); |
| 179 | list_for_each_entry(entry, &c->context_list, list) |
| 180 | if (entry->ptr == ptr) { |
| 181 | list_del(&entry->list); |
| 182 | context = entry->context; |
| 183 | kfree(entry); |
| 184 | break; |
| 185 | } |
| 186 | spin_unlock_irqrestore(&c->context_list_lock, flags); |
| 187 | |
| 188 | if (!context) |
| 189 | printk(KERN_WARNING "%s: Could not remove nonexistent ptr " |
| 190 | "%p\n", c->name, ptr); |
| 191 | |
| 192 | pr_debug("%s: remove ptr from context list %d -> %p\n", c->name, |
| 193 | context, ptr); |
| 194 | |
| 195 | return context; |
| 196 | }; |
| 197 | |
| 198 | /** |
| 199 | * i2o_cntxt_list_get - Get a pointer from the context list and remove it |
| 200 | * @c: controller to which the context list belong |
| 201 | * @context: context id to which the pointer belong |
| 202 | * |
| 203 | * Returns pointer to the matching context id on success or NULL on |
| 204 | * failure. |
| 205 | */ |
| 206 | void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context) |
| 207 | { |
| 208 | struct i2o_context_list_element *entry; |
| 209 | unsigned long flags; |
| 210 | void *ptr = NULL; |
| 211 | |
| 212 | spin_lock_irqsave(&c->context_list_lock, flags); |
| 213 | list_for_each_entry(entry, &c->context_list, list) |
| 214 | if (entry->context == context) { |
| 215 | list_del(&entry->list); |
| 216 | ptr = entry->ptr; |
| 217 | kfree(entry); |
| 218 | break; |
| 219 | } |
| 220 | spin_unlock_irqrestore(&c->context_list_lock, flags); |
| 221 | |
| 222 | if (!ptr) |
| 223 | printk(KERN_WARNING "%s: context id %d not found\n", c->name, |
| 224 | context); |
| 225 | |
| 226 | pr_debug("%s: get ptr from context list %d -> %p\n", c->name, context, |
| 227 | ptr); |
| 228 | |
| 229 | return ptr; |
| 230 | }; |
| 231 | |
| 232 | /** |
| 233 | * i2o_cntxt_list_get_ptr - Get a context id from the context list |
| 234 | * @c: controller to which the context list belong |
| 235 | * @ptr: pointer to which the context id should be fetched |
| 236 | * |
| 237 | * Returns context id which matches to the pointer on succes or 0 on |
| 238 | * failure. |
| 239 | */ |
| 240 | u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr) |
| 241 | { |
| 242 | struct i2o_context_list_element *entry; |
| 243 | u32 context = 0; |
| 244 | unsigned long flags; |
| 245 | |
| 246 | spin_lock_irqsave(&c->context_list_lock, flags); |
| 247 | list_for_each_entry(entry, &c->context_list, list) |
| 248 | if (entry->ptr == ptr) { |
| 249 | context = entry->context; |
| 250 | break; |
| 251 | } |
| 252 | spin_unlock_irqrestore(&c->context_list_lock, flags); |
| 253 | |
| 254 | if (!context) |
| 255 | printk(KERN_WARNING "%s: Could not find nonexistent ptr " |
| 256 | "%p\n", c->name, ptr); |
| 257 | |
| 258 | pr_debug("%s: get context id from context list %p -> %d\n", c->name, |
| 259 | ptr, context); |
| 260 | |
| 261 | return context; |
| 262 | }; |
| 263 | #endif |
| 264 | |
| 265 | /** |
| 266 | * i2o_iop_find - Find an I2O controller by id |
| 267 | * @unit: unit number of the I2O controller to search for |
| 268 | * |
| 269 | * Lookup the I2O controller on the controller list. |
| 270 | * |
| 271 | * Returns pointer to the I2O controller on success or NULL if not found. |
| 272 | */ |
| 273 | struct i2o_controller *i2o_find_iop(int unit) |
| 274 | { |
| 275 | struct i2o_controller *c; |
| 276 | |
| 277 | list_for_each_entry(c, &i2o_controllers, list) { |
| 278 | if (c->unit == unit) |
| 279 | return c; |
| 280 | } |
| 281 | |
| 282 | return NULL; |
| 283 | }; |
| 284 | |
| 285 | /** |
| 286 | * i2o_iop_find_device - Find a I2O device on an I2O controller |
| 287 | * @c: I2O controller where the I2O device hangs on |
| 288 | * @tid: TID of the I2O device to search for |
| 289 | * |
| 290 | * Searches the devices of the I2O controller for a device with TID tid and |
| 291 | * returns it. |
| 292 | * |
| 293 | * Returns a pointer to the I2O device if found, otherwise NULL. |
| 294 | */ |
| 295 | struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid) |
| 296 | { |
| 297 | struct i2o_device *dev; |
| 298 | |
| 299 | list_for_each_entry(dev, &c->devices, list) |
| 300 | if (dev->lct_data.tid == tid) |
| 301 | return dev; |
| 302 | |
| 303 | return NULL; |
| 304 | }; |
| 305 | |
| 306 | /** |
| 307 | * i2o_quiesce_controller - quiesce controller |
| 308 | * @c: controller |
| 309 | * |
| 310 | * Quiesce an IOP. Causes IOP to make external operation quiescent |
| 311 | * (i2o 'READY' state). Internal operation of the IOP continues normally. |
| 312 | * |
| 313 | * Returns 0 on success or negative error code on failure. |
| 314 | */ |
| 315 | static int i2o_iop_quiesce(struct i2o_controller *c) |
| 316 | { |
| 317 | struct i2o_message __iomem *msg; |
| 318 | u32 m; |
| 319 | i2o_status_block *sb = c->status_block.virt; |
| 320 | int rc; |
| 321 | |
| 322 | i2o_status_get(c); |
| 323 | |
| 324 | /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */ |
| 325 | if ((sb->iop_state != ADAPTER_STATE_READY) && |
| 326 | (sb->iop_state != ADAPTER_STATE_OPERATIONAL)) |
| 327 | return 0; |
| 328 | |
| 329 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 330 | if (m == I2O_QUEUE_EMPTY) |
| 331 | return -ETIMEDOUT; |
| 332 | |
| 333 | writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 334 | writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 335 | &msg->u.head[1]); |
| 336 | |
| 337 | /* Long timeout needed for quiesce if lots of devices */ |
| 338 | if ((rc = i2o_msg_post_wait(c, m, 240))) |
| 339 | printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n", |
| 340 | c->name, -rc); |
| 341 | else |
| 342 | pr_debug("%s: Quiesced.\n", c->name); |
| 343 | |
| 344 | i2o_status_get(c); // Entered READY state |
| 345 | |
| 346 | return rc; |
| 347 | }; |
| 348 | |
| 349 | /** |
| 350 | * i2o_iop_enable - move controller from ready to OPERATIONAL |
| 351 | * @c: I2O controller |
| 352 | * |
| 353 | * Enable IOP. This allows the IOP to resume external operations and |
| 354 | * reverses the effect of a quiesce. Returns zero or an error code if |
| 355 | * an error occurs. |
| 356 | */ |
| 357 | static int i2o_iop_enable(struct i2o_controller *c) |
| 358 | { |
| 359 | struct i2o_message __iomem *msg; |
| 360 | u32 m; |
| 361 | i2o_status_block *sb = c->status_block.virt; |
| 362 | int rc; |
| 363 | |
| 364 | i2o_status_get(c); |
| 365 | |
| 366 | /* Enable only allowed on READY state */ |
| 367 | if (sb->iop_state != ADAPTER_STATE_READY) |
| 368 | return -EINVAL; |
| 369 | |
| 370 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 371 | if (m == I2O_QUEUE_EMPTY) |
| 372 | return -ETIMEDOUT; |
| 373 | |
| 374 | writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 375 | writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 376 | &msg->u.head[1]); |
| 377 | |
| 378 | /* How long of a timeout do we need? */ |
| 379 | if ((rc = i2o_msg_post_wait(c, m, 240))) |
| 380 | printk(KERN_ERR "%s: Could not enable (status=%#x).\n", |
| 381 | c->name, -rc); |
| 382 | else |
| 383 | pr_debug("%s: Enabled.\n", c->name); |
| 384 | |
| 385 | i2o_status_get(c); // entered OPERATIONAL state |
| 386 | |
| 387 | return rc; |
| 388 | }; |
| 389 | |
| 390 | /** |
| 391 | * i2o_iop_quiesce_all - Quiesce all I2O controllers on the system |
| 392 | * |
| 393 | * Quiesce all I2O controllers which are connected to the system. |
| 394 | */ |
| 395 | static inline void i2o_iop_quiesce_all(void) |
| 396 | { |
| 397 | struct i2o_controller *c, *tmp; |
| 398 | |
| 399 | list_for_each_entry_safe(c, tmp, &i2o_controllers, list) { |
| 400 | if (!c->no_quiesce) |
| 401 | i2o_iop_quiesce(c); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | /** |
| 406 | * i2o_iop_enable_all - Enables all controllers on the system |
| 407 | * |
| 408 | * Enables all I2O controllers which are connected to the system. |
| 409 | */ |
| 410 | static inline void i2o_iop_enable_all(void) |
| 411 | { |
| 412 | struct i2o_controller *c, *tmp; |
| 413 | |
| 414 | list_for_each_entry_safe(c, tmp, &i2o_controllers, list) |
| 415 | i2o_iop_enable(c); |
| 416 | }; |
| 417 | |
| 418 | /** |
| 419 | * i2o_clear_controller - Bring I2O controller into HOLD state |
| 420 | * @c: controller |
| 421 | * |
| 422 | * Clear an IOP to HOLD state, ie. terminate external operations, clear all |
| 423 | * input queues and prepare for a system restart. IOP's internal operation |
| 424 | * continues normally and the outbound queue is alive. The IOP is not |
| 425 | * expected to rebuild its LCT. |
| 426 | * |
| 427 | * Returns 0 on success or negative error code on failure. |
| 428 | */ |
| 429 | static int i2o_iop_clear(struct i2o_controller *c) |
| 430 | { |
| 431 | struct i2o_message __iomem *msg; |
| 432 | u32 m; |
| 433 | int rc; |
| 434 | |
| 435 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 436 | if (m == I2O_QUEUE_EMPTY) |
| 437 | return -ETIMEDOUT; |
| 438 | |
| 439 | /* Quiesce all IOPs first */ |
| 440 | i2o_iop_quiesce_all(); |
| 441 | |
| 442 | writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 443 | writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 444 | &msg->u.head[1]); |
| 445 | |
| 446 | if ((rc = i2o_msg_post_wait(c, m, 30))) |
| 447 | printk(KERN_INFO "%s: Unable to clear (status=%#x).\n", |
| 448 | c->name, -rc); |
| 449 | else |
| 450 | pr_debug("%s: Cleared.\n", c->name); |
| 451 | |
| 452 | /* Enable all IOPs */ |
| 453 | i2o_iop_enable_all(); |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | return rc; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * i2o_iop_reset - reset an I2O controller |
| 460 | * @c: controller to reset |
| 461 | * |
| 462 | * Reset the IOP into INIT state and wait until IOP gets into RESET state. |
| 463 | * Terminate all external operations, clear IOP's inbound and outbound |
| 464 | * queues, terminate all DDMs, and reload the IOP's operating environment |
| 465 | * and all local DDMs. The IOP rebuilds its LCT. |
| 466 | */ |
| 467 | static int i2o_iop_reset(struct i2o_controller *c) |
| 468 | { |
| 469 | u8 *status = c->status.virt; |
| 470 | struct i2o_message __iomem *msg; |
| 471 | u32 m; |
| 472 | unsigned long timeout; |
| 473 | i2o_status_block *sb = c->status_block.virt; |
| 474 | int rc = 0; |
| 475 | |
| 476 | pr_debug("%s: Resetting controller\n", c->name); |
| 477 | |
| 478 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 479 | if (m == I2O_QUEUE_EMPTY) |
| 480 | return -ETIMEDOUT; |
| 481 | |
| 482 | memset(status, 0, 8); |
| 483 | |
| 484 | /* Quiesce all IOPs first */ |
| 485 | i2o_iop_quiesce_all(); |
| 486 | |
| 487 | writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 488 | writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 489 | &msg->u.head[1]); |
| 490 | writel(i2o_exec_driver.context, &msg->u.s.icntxt); |
| 491 | writel(0, &msg->u.s.tcntxt); //FIXME: use reasonable transaction context |
| 492 | writel(0, &msg->body[0]); |
| 493 | writel(0, &msg->body[1]); |
| 494 | writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]); |
| 495 | writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]); |
| 496 | |
| 497 | i2o_msg_post(c, m); |
| 498 | |
| 499 | /* Wait for a reply */ |
| 500 | timeout = jiffies + I2O_TIMEOUT_RESET * HZ; |
| 501 | while (!*status) { |
| 502 | if (time_after(jiffies, timeout)) { |
| 503 | printk(KERN_ERR "%s: IOP reset timeout.\n", c->name); |
| 504 | rc = -ETIMEDOUT; |
| 505 | goto exit; |
| 506 | } |
| 507 | |
| 508 | /* Promise bug */ |
| 509 | if (status[1] || status[4]) { |
| 510 | *status = 0; |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 515 | schedule_timeout(1); |
| 516 | |
| 517 | rmb(); |
| 518 | } |
| 519 | |
| 520 | if (*status == I2O_CMD_IN_PROGRESS) { |
| 521 | /* |
| 522 | * Once the reset is sent, the IOP goes into the INIT state |
| 523 | * which is indeterminate. We need to wait until the IOP |
| 524 | * has rebooted before we can let the system talk to |
| 525 | * it. We read the inbound Free_List until a message is |
| 526 | * available. If we can't read one in the given ammount of |
| 527 | * time, we assume the IOP could not reboot properly. |
| 528 | */ |
| 529 | pr_debug("%s: Reset in progress, waiting for reboot...\n", |
| 530 | c->name); |
| 531 | |
| 532 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET); |
| 533 | while (m == I2O_QUEUE_EMPTY) { |
| 534 | if (time_after(jiffies, timeout)) { |
| 535 | printk(KERN_ERR "%s: IOP reset timeout.\n", |
| 536 | c->name); |
| 537 | rc = -ETIMEDOUT; |
| 538 | goto exit; |
| 539 | } |
| 540 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 541 | schedule_timeout(1); |
| 542 | |
| 543 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET); |
| 544 | } |
| 545 | i2o_msg_nop(c, m); |
| 546 | } |
| 547 | |
| 548 | /* from here all quiesce commands are safe */ |
| 549 | c->no_quiesce = 0; |
| 550 | |
| 551 | /* If IopReset was rejected or didn't perform reset, try IopClear */ |
| 552 | i2o_status_get(c); |
| 553 | if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) { |
| 554 | printk(KERN_WARNING "%s: Reset rejected, trying to clear\n", |
| 555 | c->name); |
| 556 | i2o_iop_clear(c); |
| 557 | } else |
| 558 | pr_debug("%s: Reset completed.\n", c->name); |
| 559 | |
| 560 | exit: |
| 561 | /* Enable all IOPs */ |
| 562 | i2o_iop_enable_all(); |
| 563 | |
| 564 | return rc; |
| 565 | }; |
| 566 | |
| 567 | /** |
| 568 | * i2o_iop_init_outbound_queue - setup the outbound message queue |
| 569 | * @c: I2O controller |
| 570 | * |
| 571 | * Clear and (re)initialize IOP's outbound queue and post the message |
| 572 | * frames to the IOP. |
| 573 | * |
| 574 | * Returns 0 on success or a negative errno code on failure. |
| 575 | */ |
| 576 | static int i2o_iop_init_outbound_queue(struct i2o_controller *c) |
| 577 | { |
| 578 | u8 *status = c->status.virt; |
| 579 | u32 m; |
| 580 | struct i2o_message __iomem *msg; |
| 581 | ulong timeout; |
| 582 | int i; |
| 583 | |
| 584 | pr_debug("%s: Initializing Outbound Queue...\n", c->name); |
| 585 | |
| 586 | memset(status, 0, 4); |
| 587 | |
| 588 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 589 | if (m == I2O_QUEUE_EMPTY) |
| 590 | return -ETIMEDOUT; |
| 591 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 592 | writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6, &msg->u.head[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 594 | &msg->u.head[1]); |
| 595 | writel(i2o_exec_driver.context, &msg->u.s.icntxt); |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 596 | writel(0x00000000, &msg->u.s.tcntxt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | writel(PAGE_SIZE, &msg->body[0]); |
| 598 | writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]); /* Outbound msg frame |
| 599 | size in words and Initcode */ |
| 600 | writel(0xd0000004, &msg->body[2]); |
| 601 | writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]); |
| 602 | writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]); |
| 603 | |
| 604 | i2o_msg_post(c, m); |
| 605 | |
| 606 | timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ; |
| 607 | while (*status <= I2O_CMD_IN_PROGRESS) { |
| 608 | if (time_after(jiffies, timeout)) { |
| 609 | printk(KERN_WARNING "%s: Timeout Initializing\n", |
| 610 | c->name); |
| 611 | return -ETIMEDOUT; |
| 612 | } |
| 613 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 614 | schedule_timeout(1); |
| 615 | |
| 616 | rmb(); |
| 617 | } |
| 618 | |
| 619 | m = c->out_queue.phys; |
| 620 | |
| 621 | /* Post frames */ |
| 622 | for (i = 0; i < NMBR_MSG_FRAMES; i++) { |
| 623 | i2o_flush_reply(c, m); |
| 624 | udelay(1); /* Promise */ |
| 625 | m += MSG_FRAME_SIZE * 4; |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * i2o_iop_send_nop - send a core NOP message |
| 633 | * @c: controller |
| 634 | * |
| 635 | * Send a no-operation message with a reply set to cause no |
| 636 | * action either. Needed for bringing up promise controllers. |
| 637 | */ |
| 638 | static int i2o_iop_send_nop(struct i2o_controller *c) |
| 639 | { |
| 640 | struct i2o_message __iomem *msg; |
| 641 | u32 m = i2o_msg_get_wait(c, &msg, HZ); |
| 642 | if (m == I2O_QUEUE_EMPTY) |
| 643 | return -ETIMEDOUT; |
| 644 | i2o_msg_nop(c, m); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * i2o_iop_activate - Bring controller up to HOLD |
| 650 | * @c: controller |
| 651 | * |
| 652 | * This function brings an I2O controller into HOLD state. The adapter |
| 653 | * is reset if necessary and then the queues and resource table are read. |
| 654 | * |
| 655 | * Returns 0 on success or negative error code on failure. |
| 656 | */ |
| 657 | static int i2o_iop_activate(struct i2o_controller *c) |
| 658 | { |
| 659 | struct pci_dev *i960 = NULL; |
| 660 | i2o_status_block *sb = c->status_block.virt; |
| 661 | int rc; |
| 662 | |
| 663 | if (c->promise) { |
| 664 | /* Beat up the hardware first of all */ |
| 665 | i960 = |
| 666 | pci_find_slot(c->pdev->bus->number, |
| 667 | PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0)); |
| 668 | if (i960) |
| 669 | pci_write_config_word(i960, 0x42, 0); |
| 670 | |
| 671 | /* Follow this sequence precisely or the controller |
| 672 | ceases to perform useful functions until reboot */ |
| 673 | if ((rc = i2o_iop_send_nop(c))) |
| 674 | return rc; |
| 675 | |
| 676 | if ((rc = i2o_iop_reset(c))) |
| 677 | return rc; |
| 678 | } |
| 679 | |
| 680 | /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */ |
| 681 | /* In READY state, Get status */ |
| 682 | |
| 683 | rc = i2o_status_get(c); |
| 684 | if (rc) { |
| 685 | printk(KERN_INFO "%s: Unable to obtain status, " |
| 686 | "attempting a reset.\n", c->name); |
| 687 | if (i2o_iop_reset(c)) |
| 688 | return rc; |
| 689 | } |
| 690 | |
| 691 | if (sb->i2o_version > I2OVER15) { |
| 692 | printk(KERN_ERR "%s: Not running version 1.5 of the I2O " |
| 693 | "Specification.\n", c->name); |
| 694 | return -ENODEV; |
| 695 | } |
| 696 | |
| 697 | switch (sb->iop_state) { |
| 698 | case ADAPTER_STATE_FAULTED: |
| 699 | printk(KERN_CRIT "%s: hardware fault\n", c->name); |
| 700 | return -ENODEV; |
| 701 | |
| 702 | case ADAPTER_STATE_READY: |
| 703 | case ADAPTER_STATE_OPERATIONAL: |
| 704 | case ADAPTER_STATE_HOLD: |
| 705 | case ADAPTER_STATE_FAILED: |
| 706 | pr_debug("%s: already running, trying to reset...\n", c->name); |
| 707 | if (i2o_iop_reset(c)) |
| 708 | return -ENODEV; |
| 709 | } |
| 710 | |
| 711 | rc = i2o_iop_init_outbound_queue(c); |
| 712 | if (rc) |
| 713 | return rc; |
| 714 | |
| 715 | if (c->promise) { |
| 716 | if ((rc = i2o_iop_send_nop(c))) |
| 717 | return rc; |
| 718 | |
| 719 | if ((rc = i2o_status_get(c))) |
| 720 | return rc; |
| 721 | |
| 722 | if (i960) |
| 723 | pci_write_config_word(i960, 0x42, 0x3FF); |
| 724 | } |
| 725 | |
| 726 | /* In HOLD state */ |
| 727 | |
| 728 | rc = i2o_hrt_get(c); |
| 729 | |
| 730 | return rc; |
| 731 | }; |
| 732 | |
| 733 | /** |
| 734 | * i2o_iop_systab_set - Set the I2O System Table of the specified IOP |
| 735 | * @c: I2O controller to which the system table should be send |
| 736 | * |
| 737 | * Before the systab could be set i2o_systab_build() must be called. |
| 738 | * |
| 739 | * Returns 0 on success or negative error code on failure. |
| 740 | */ |
| 741 | static int i2o_iop_systab_set(struct i2o_controller *c) |
| 742 | { |
| 743 | struct i2o_message __iomem *msg; |
| 744 | u32 m; |
| 745 | i2o_status_block *sb = c->status_block.virt; |
| 746 | struct device *dev = &c->pdev->dev; |
| 747 | struct resource *root; |
| 748 | int rc; |
| 749 | |
| 750 | if (sb->current_mem_size < sb->desired_mem_size) { |
| 751 | struct resource *res = &c->mem_resource; |
| 752 | res->name = c->pdev->bus->name; |
| 753 | res->flags = IORESOURCE_MEM; |
| 754 | res->start = 0; |
| 755 | res->end = 0; |
| 756 | printk(KERN_INFO "%s: requires private memory resources.\n", |
| 757 | c->name); |
| 758 | root = pci_find_parent_resource(c->pdev, res); |
| 759 | if (root == NULL) |
| 760 | printk(KERN_WARNING "%s: Can't find parent resource!\n", |
| 761 | c->name); |
| 762 | 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 */ |
| 763 | NULL, NULL) >= 0) { |
| 764 | c->mem_alloc = 1; |
| 765 | sb->current_mem_size = 1 + res->end - res->start; |
| 766 | sb->current_mem_base = res->start; |
| 767 | printk(KERN_INFO "%s: allocated %ld bytes of PCI memory" |
| 768 | " at 0x%08lX.\n", c->name, |
| 769 | 1 + res->end - res->start, res->start); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (sb->current_io_size < sb->desired_io_size) { |
| 774 | struct resource *res = &c->io_resource; |
| 775 | res->name = c->pdev->bus->name; |
| 776 | res->flags = IORESOURCE_IO; |
| 777 | res->start = 0; |
| 778 | res->end = 0; |
| 779 | printk(KERN_INFO "%s: requires private memory resources.\n", |
| 780 | c->name); |
| 781 | root = pci_find_parent_resource(c->pdev, res); |
| 782 | if (root == NULL) |
| 783 | printk(KERN_WARNING "%s: Can't find parent resource!\n", |
| 784 | c->name); |
| 785 | 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 */ |
| 786 | NULL, NULL) >= 0) { |
| 787 | c->io_alloc = 1; |
| 788 | sb->current_io_size = 1 + res->end - res->start; |
| 789 | sb->current_mem_base = res->start; |
| 790 | printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at" |
| 791 | " 0x%08lX.\n", c->name, |
| 792 | 1 + res->end - res->start, res->start); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 797 | if (m == I2O_QUEUE_EMPTY) |
| 798 | return -ETIMEDOUT; |
| 799 | |
| 800 | i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len, |
| 801 | PCI_DMA_TODEVICE); |
| 802 | if (!i2o_systab.phys) { |
| 803 | i2o_msg_nop(c, m); |
| 804 | return -ENOMEM; |
| 805 | } |
| 806 | |
| 807 | writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]); |
| 808 | writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 809 | &msg->u.head[1]); |
| 810 | |
| 811 | /* |
| 812 | * Provide three SGL-elements: |
| 813 | * System table (SysTab), Private memory space declaration and |
| 814 | * Private i/o space declaration |
| 815 | * |
| 816 | * FIXME: is this still true? |
| 817 | * Nasty one here. We can't use dma_alloc_coherent to send the |
| 818 | * same table to everyone. We have to go remap it for them all |
| 819 | */ |
| 820 | |
| 821 | writel(c->unit + 2, &msg->body[0]); |
| 822 | writel(0, &msg->body[1]); |
| 823 | writel(0x54000000 | i2o_systab.len, &msg->body[2]); |
| 824 | writel(i2o_systab.phys, &msg->body[3]); |
| 825 | writel(0x54000000 | sb->current_mem_size, &msg->body[4]); |
| 826 | writel(sb->current_mem_base, &msg->body[5]); |
| 827 | writel(0xd4000000 | sb->current_io_size, &msg->body[6]); |
| 828 | writel(sb->current_io_base, &msg->body[6]); |
| 829 | |
| 830 | rc = i2o_msg_post_wait(c, m, 120); |
| 831 | |
| 832 | dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len, |
| 833 | PCI_DMA_TODEVICE); |
| 834 | |
| 835 | if (rc < 0) |
| 836 | printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n", |
| 837 | c->name, -rc); |
| 838 | else |
| 839 | pr_debug("%s: SysTab set.\n", c->name); |
| 840 | |
| 841 | i2o_status_get(c); // Entered READY state |
| 842 | |
| 843 | return rc; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * i2o_iop_online - Bring a controller online into OPERATIONAL state. |
| 848 | * @c: I2O controller |
| 849 | * |
| 850 | * Send the system table and enable the I2O controller. |
| 851 | * |
| 852 | * Returns 0 on success or negativer error code on failure. |
| 853 | */ |
| 854 | static int i2o_iop_online(struct i2o_controller *c) |
| 855 | { |
| 856 | int rc; |
| 857 | |
| 858 | rc = i2o_iop_systab_set(c); |
| 859 | if (rc) |
| 860 | return rc; |
| 861 | |
| 862 | /* In READY state */ |
| 863 | pr_debug("%s: Attempting to enable...\n", c->name); |
| 864 | rc = i2o_iop_enable(c); |
| 865 | if (rc) |
| 866 | return rc; |
| 867 | |
| 868 | return 0; |
| 869 | }; |
| 870 | |
| 871 | /** |
| 872 | * i2o_iop_remove - Remove the I2O controller from the I2O core |
| 873 | * @c: I2O controller |
| 874 | * |
| 875 | * Remove the I2O controller from the I2O core. If devices are attached to |
| 876 | * the controller remove these also and finally reset the controller. |
| 877 | */ |
| 878 | void i2o_iop_remove(struct i2o_controller *c) |
| 879 | { |
| 880 | struct i2o_device *dev, *tmp; |
| 881 | |
| 882 | pr_debug("%s: deleting controller\n", c->name); |
| 883 | |
| 884 | i2o_driver_notify_controller_remove_all(c); |
| 885 | |
| 886 | list_del(&c->list); |
| 887 | |
| 888 | list_for_each_entry_safe(dev, tmp, &c->devices, list) |
| 889 | i2o_device_remove(dev); |
| 890 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 891 | device_del(&c->device); |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /* Ask the IOP to switch to RESET state */ |
| 894 | i2o_iop_reset(c); |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 895 | |
| 896 | put_device(&c->device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /** |
| 900 | * i2o_systab_build - Build system table |
| 901 | * |
| 902 | * The system table contains information about all the IOPs in the system |
| 903 | * (duh) and is used by the Executives on the IOPs to establish peer2peer |
| 904 | * connections. We're not supporting peer2peer at the moment, but this |
| 905 | * will be needed down the road for things like lan2lan forwarding. |
| 906 | * |
| 907 | * Returns 0 on success or negative error code on failure. |
| 908 | */ |
| 909 | static int i2o_systab_build(void) |
| 910 | { |
| 911 | struct i2o_controller *c, *tmp; |
| 912 | int num_controllers = 0; |
| 913 | u32 change_ind = 0; |
| 914 | int count = 0; |
| 915 | struct i2o_sys_tbl *systab = i2o_systab.virt; |
| 916 | |
| 917 | list_for_each_entry_safe(c, tmp, &i2o_controllers, list) |
| 918 | num_controllers++; |
| 919 | |
| 920 | if (systab) { |
| 921 | change_ind = systab->change_ind; |
| 922 | kfree(i2o_systab.virt); |
| 923 | } |
| 924 | |
| 925 | /* Header + IOPs */ |
| 926 | i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers * |
| 927 | sizeof(struct i2o_sys_tbl_entry); |
| 928 | |
| 929 | systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL); |
| 930 | if (!systab) { |
| 931 | printk(KERN_ERR "i2o: unable to allocate memory for System " |
| 932 | "Table\n"); |
| 933 | return -ENOMEM; |
| 934 | } |
| 935 | memset(systab, 0, i2o_systab.len); |
| 936 | |
| 937 | systab->version = I2OVERSION; |
| 938 | systab->change_ind = change_ind + 1; |
| 939 | |
| 940 | list_for_each_entry_safe(c, tmp, &i2o_controllers, list) { |
| 941 | i2o_status_block *sb; |
| 942 | |
| 943 | if (count >= num_controllers) { |
| 944 | printk(KERN_ERR "i2o: controller added while building " |
| 945 | "system table\n"); |
| 946 | break; |
| 947 | } |
| 948 | |
| 949 | sb = c->status_block.virt; |
| 950 | |
| 951 | /* |
| 952 | * Get updated IOP state so we have the latest information |
| 953 | * |
| 954 | * We should delete the controller at this point if it |
| 955 | * doesn't respond since if it's not on the system table |
| 956 | * it is techninically not part of the I2O subsystem... |
| 957 | */ |
| 958 | if (unlikely(i2o_status_get(c))) { |
| 959 | printk(KERN_ERR "%s: Deleting b/c could not get status" |
| 960 | " while attempting to build system table\n", |
| 961 | c->name); |
| 962 | i2o_iop_remove(c); |
| 963 | continue; // try the next one |
| 964 | } |
| 965 | |
| 966 | systab->iops[count].org_id = sb->org_id; |
| 967 | systab->iops[count].iop_id = c->unit + 2; |
| 968 | systab->iops[count].seg_num = 0; |
| 969 | systab->iops[count].i2o_version = sb->i2o_version; |
| 970 | systab->iops[count].iop_state = sb->iop_state; |
| 971 | systab->iops[count].msg_type = sb->msg_type; |
| 972 | systab->iops[count].frame_size = sb->inbound_frame_size; |
| 973 | systab->iops[count].last_changed = change_ind; |
| 974 | systab->iops[count].iop_capabilities = sb->iop_capabilities; |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 975 | systab->iops[count].inbound_low = |
| 976 | i2o_dma_low(c->base.phys + I2O_IN_PORT); |
| 977 | systab->iops[count].inbound_high = |
| 978 | i2o_dma_high(c->base.phys + I2O_IN_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | count++; |
| 981 | } |
| 982 | |
| 983 | systab->num_entries = count; |
| 984 | |
| 985 | return 0; |
| 986 | }; |
| 987 | |
| 988 | /** |
| 989 | * i2o_parse_hrt - Parse the hardware resource table. |
| 990 | * @c: I2O controller |
| 991 | * |
| 992 | * We don't do anything with it except dumping it (in debug mode). |
| 993 | * |
| 994 | * Returns 0. |
| 995 | */ |
| 996 | static int i2o_parse_hrt(struct i2o_controller *c) |
| 997 | { |
| 998 | i2o_dump_hrt(c); |
| 999 | return 0; |
| 1000 | }; |
| 1001 | |
| 1002 | /** |
| 1003 | * i2o_status_get - Get the status block from the I2O controller |
| 1004 | * @c: I2O controller |
| 1005 | * |
| 1006 | * Issue a status query on the controller. This updates the attached |
| 1007 | * status block. The status block could then be accessed through |
| 1008 | * c->status_block. |
| 1009 | * |
| 1010 | * Returns 0 on sucess or negative error code on failure. |
| 1011 | */ |
| 1012 | int i2o_status_get(struct i2o_controller *c) |
| 1013 | { |
| 1014 | struct i2o_message __iomem *msg; |
| 1015 | u32 m; |
| 1016 | u8 *status_block; |
| 1017 | unsigned long timeout; |
| 1018 | |
| 1019 | status_block = (u8 *) c->status_block.virt; |
| 1020 | memset(status_block, 0, sizeof(i2o_status_block)); |
| 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(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 1027 | writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 1028 | &msg->u.head[1]); |
| 1029 | writel(i2o_exec_driver.context, &msg->u.s.icntxt); |
| 1030 | writel(0, &msg->u.s.tcntxt); // FIXME: use resonable transaction context |
| 1031 | writel(0, &msg->body[0]); |
| 1032 | writel(0, &msg->body[1]); |
| 1033 | writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]); |
| 1034 | writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]); |
| 1035 | writel(sizeof(i2o_status_block), &msg->body[4]); /* always 88 bytes */ |
| 1036 | |
| 1037 | i2o_msg_post(c, m); |
| 1038 | |
| 1039 | /* Wait for a reply */ |
| 1040 | timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ; |
| 1041 | while (status_block[87] != 0xFF) { |
| 1042 | if (time_after(jiffies, timeout)) { |
| 1043 | printk(KERN_ERR "%s: Get status timeout.\n", c->name); |
| 1044 | return -ETIMEDOUT; |
| 1045 | } |
| 1046 | |
| 1047 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1048 | schedule_timeout(1); |
| 1049 | |
| 1050 | rmb(); |
| 1051 | } |
| 1052 | |
| 1053 | #ifdef DEBUG |
| 1054 | i2o_debug_state(c); |
| 1055 | #endif |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller |
| 1062 | * @c: I2O controller from which the HRT should be fetched |
| 1063 | * |
| 1064 | * The HRT contains information about possible hidden devices but is |
| 1065 | * mostly useless to us. |
| 1066 | * |
| 1067 | * Returns 0 on success or negativer error code on failure. |
| 1068 | */ |
| 1069 | static int i2o_hrt_get(struct i2o_controller *c) |
| 1070 | { |
| 1071 | int rc; |
| 1072 | int i; |
| 1073 | i2o_hrt *hrt = c->hrt.virt; |
| 1074 | u32 size = sizeof(i2o_hrt); |
| 1075 | struct device *dev = &c->pdev->dev; |
| 1076 | |
| 1077 | for (i = 0; i < I2O_HRT_GET_TRIES; i++) { |
| 1078 | struct i2o_message __iomem *msg; |
| 1079 | u32 m; |
| 1080 | |
| 1081 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 1082 | if (m == I2O_QUEUE_EMPTY) |
| 1083 | return -ETIMEDOUT; |
| 1084 | |
| 1085 | writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]); |
| 1086 | writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID, |
| 1087 | &msg->u.head[1]); |
| 1088 | writel(0xd0000000 | c->hrt.len, &msg->body[0]); |
| 1089 | writel(c->hrt.phys, &msg->body[1]); |
| 1090 | |
| 1091 | rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt); |
| 1092 | |
| 1093 | if (rc < 0) { |
| 1094 | printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n", |
| 1095 | c->name, -rc); |
| 1096 | return rc; |
| 1097 | } |
| 1098 | |
| 1099 | size = hrt->num_entries * hrt->entry_len << 2; |
| 1100 | if (size > c->hrt.len) { |
| 1101 | if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL)) |
| 1102 | return -ENOMEM; |
| 1103 | else |
| 1104 | hrt = c->hrt.virt; |
| 1105 | } else |
| 1106 | return i2o_parse_hrt(c); |
| 1107 | } |
| 1108 | |
| 1109 | printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n", |
| 1110 | c->name, I2O_HRT_GET_TRIES); |
| 1111 | |
| 1112 | return -EBUSY; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1116 | * i2o_iop_free - Free the i2o_controller struct |
| 1117 | * @c: I2O controller to free |
| 1118 | */ |
| 1119 | void i2o_iop_free(struct i2o_controller *c) |
| 1120 | { |
| 1121 | kfree(c); |
| 1122 | }; |
| 1123 | |
| 1124 | |
| 1125 | /** |
| 1126 | * i2o_iop_release - release the memory for a I2O controller |
| 1127 | * @dev: I2O controller which should be released |
| 1128 | * |
| 1129 | * Release the allocated memory. This function is called if refcount of |
| 1130 | * device reaches 0 automatically. |
| 1131 | */ |
| 1132 | static void i2o_iop_release(struct device *dev) |
| 1133 | { |
| 1134 | struct i2o_controller *c = to_i2o_controller(dev); |
| 1135 | |
| 1136 | i2o_iop_free(c); |
| 1137 | }; |
| 1138 | |
| 1139 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | * i2o_iop_alloc - Allocate and initialize a i2o_controller struct |
| 1141 | * |
| 1142 | * Allocate the necessary memory for a i2o_controller struct and |
| 1143 | * initialize the lists. |
| 1144 | * |
| 1145 | * Returns a pointer to the I2O controller or a negative error code on |
| 1146 | * failure. |
| 1147 | */ |
| 1148 | struct i2o_controller *i2o_iop_alloc(void) |
| 1149 | { |
| 1150 | static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */ |
| 1151 | struct i2o_controller *c; |
| 1152 | |
| 1153 | c = kmalloc(sizeof(*c), GFP_KERNEL); |
| 1154 | if (!c) { |
| 1155 | printk(KERN_ERR "i2o: Insufficient memory to allocate a I2O " |
| 1156 | "controller.\n"); |
| 1157 | return ERR_PTR(-ENOMEM); |
| 1158 | } |
| 1159 | memset(c, 0, sizeof(*c)); |
| 1160 | |
| 1161 | INIT_LIST_HEAD(&c->devices); |
| 1162 | spin_lock_init(&c->lock); |
| 1163 | init_MUTEX(&c->lct_lock); |
| 1164 | c->unit = unit++; |
| 1165 | sprintf(c->name, "iop%d", c->unit); |
| 1166 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1167 | device_initialize(&c->device); |
| 1168 | c->device.release = &i2o_iop_release; |
| 1169 | snprintf(c->device.bus_id, BUS_ID_SIZE, "iop%d", c->unit); |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | #if BITS_PER_LONG == 64 |
| 1172 | spin_lock_init(&c->context_list_lock); |
| 1173 | atomic_set(&c->context_list_counter, 0); |
| 1174 | INIT_LIST_HEAD(&c->context_list); |
| 1175 | #endif |
| 1176 | |
| 1177 | return c; |
| 1178 | }; |
| 1179 | |
| 1180 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | * i2o_iop_add - Initialize the I2O controller and add him to the I2O core |
| 1182 | * @c: controller |
| 1183 | * |
| 1184 | * Initialize the I2O controller and if no error occurs add him to the I2O |
| 1185 | * core. |
| 1186 | * |
| 1187 | * Returns 0 on success or negative error code on failure. |
| 1188 | */ |
| 1189 | int i2o_iop_add(struct i2o_controller *c) |
| 1190 | { |
| 1191 | int rc; |
| 1192 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1193 | if((rc = device_add(&c->device))) { |
| 1194 | printk(KERN_ERR "%s: could not register controller\n", c->name); |
| 1195 | goto iop_reset; |
| 1196 | } |
| 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | printk(KERN_INFO "%s: Activating I2O controller...\n", c->name); |
| 1199 | printk(KERN_INFO "%s: This may take a few minutes if there are many " |
| 1200 | "devices\n", c->name); |
| 1201 | |
| 1202 | if ((rc = i2o_iop_activate(c))) { |
| 1203 | printk(KERN_ERR "%s: could not activate controller\n", |
| 1204 | c->name); |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1205 | goto iop_reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | pr_debug("%s: building sys table...\n", c->name); |
| 1209 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1210 | if ((rc = i2o_systab_build())) |
| 1211 | goto iop_reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | pr_debug("%s: online controller...\n", c->name); |
| 1214 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1215 | if ((rc = i2o_iop_online(c))) |
| 1216 | goto iop_reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | pr_debug("%s: getting LCT...\n", c->name); |
| 1219 | |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1220 | if ((rc = i2o_exec_lct_get(c))) |
| 1221 | goto iop_reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
| 1223 | list_add(&c->list, &i2o_controllers); |
| 1224 | |
| 1225 | i2o_driver_notify_controller_add_all(c); |
| 1226 | |
| 1227 | printk(KERN_INFO "%s: Controller added\n", c->name); |
| 1228 | |
| 1229 | return 0; |
Markus Lidel | f88e119 | 2005-06-23 22:02:14 -0700 | [diff] [blame^] | 1230 | |
| 1231 | iop_reset: |
| 1232 | i2o_iop_reset(c); |
| 1233 | |
| 1234 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | }; |
| 1236 | |
| 1237 | /** |
| 1238 | * i2o_event_register - Turn on/off event notification for a I2O device |
| 1239 | * @dev: I2O device which should receive the event registration request |
| 1240 | * @drv: driver which want to get notified |
| 1241 | * @tcntxt: transaction context to use with this notifier |
| 1242 | * @evt_mask: mask of events |
| 1243 | * |
| 1244 | * Create and posts an event registration message to the task. No reply |
| 1245 | * is waited for, or expected. If you do not want further notifications, |
| 1246 | * call the i2o_event_register again with a evt_mask of 0. |
| 1247 | * |
| 1248 | * Returns 0 on success or -ETIMEDOUT if no message could be fetched for |
| 1249 | * sending the request. |
| 1250 | */ |
| 1251 | int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv, |
| 1252 | int tcntxt, u32 evt_mask) |
| 1253 | { |
| 1254 | struct i2o_controller *c = dev->iop; |
| 1255 | struct i2o_message __iomem *msg; |
| 1256 | u32 m; |
| 1257 | |
| 1258 | m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET); |
| 1259 | if (m == I2O_QUEUE_EMPTY) |
| 1260 | return -ETIMEDOUT; |
| 1261 | |
| 1262 | writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]); |
| 1263 | writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data. |
| 1264 | tid, &msg->u.head[1]); |
| 1265 | writel(drv->context, &msg->u.s.icntxt); |
| 1266 | writel(tcntxt, &msg->u.s.tcntxt); |
| 1267 | writel(evt_mask, &msg->body[0]); |
| 1268 | |
| 1269 | i2o_msg_post(c, m); |
| 1270 | |
| 1271 | return 0; |
| 1272 | }; |
| 1273 | |
| 1274 | /** |
| 1275 | * i2o_iop_init - I2O main initialization function |
| 1276 | * |
| 1277 | * Initialize the I2O drivers (OSM) functions, register the Executive OSM, |
| 1278 | * initialize the I2O PCI part and finally initialize I2O device stuff. |
| 1279 | * |
| 1280 | * Returns 0 on success or negative error code on failure. |
| 1281 | */ |
| 1282 | static int __init i2o_iop_init(void) |
| 1283 | { |
| 1284 | int rc = 0; |
| 1285 | |
| 1286 | printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n"); |
| 1287 | |
| 1288 | rc = i2o_device_init(); |
| 1289 | if (rc) |
| 1290 | goto exit; |
| 1291 | |
| 1292 | rc = i2o_driver_init(); |
| 1293 | if (rc) |
| 1294 | goto device_exit; |
| 1295 | |
| 1296 | rc = i2o_exec_init(); |
| 1297 | if (rc) |
| 1298 | goto driver_exit; |
| 1299 | |
| 1300 | rc = i2o_pci_init(); |
| 1301 | if (rc < 0) |
| 1302 | goto exec_exit; |
| 1303 | |
| 1304 | return 0; |
| 1305 | |
| 1306 | exec_exit: |
| 1307 | i2o_exec_exit(); |
| 1308 | |
| 1309 | driver_exit: |
| 1310 | i2o_driver_exit(); |
| 1311 | |
| 1312 | device_exit: |
| 1313 | i2o_device_exit(); |
| 1314 | |
| 1315 | exit: |
| 1316 | return rc; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * i2o_iop_exit - I2O main exit function |
| 1321 | * |
| 1322 | * Removes I2O controllers from PCI subsystem and shut down OSMs. |
| 1323 | */ |
| 1324 | static void __exit i2o_iop_exit(void) |
| 1325 | { |
| 1326 | i2o_pci_exit(); |
| 1327 | i2o_exec_exit(); |
| 1328 | i2o_driver_exit(); |
| 1329 | i2o_device_exit(); |
| 1330 | }; |
| 1331 | |
| 1332 | module_init(i2o_iop_init); |
| 1333 | module_exit(i2o_iop_exit); |
| 1334 | |
| 1335 | MODULE_AUTHOR("Red Hat Software"); |
| 1336 | MODULE_LICENSE("GPL"); |
| 1337 | MODULE_DESCRIPTION(OSM_DESCRIPTION); |
| 1338 | MODULE_VERSION(OSM_VERSION); |
| 1339 | |
| 1340 | #if BITS_PER_LONG == 64 |
| 1341 | EXPORT_SYMBOL(i2o_cntxt_list_add); |
| 1342 | EXPORT_SYMBOL(i2o_cntxt_list_get); |
| 1343 | EXPORT_SYMBOL(i2o_cntxt_list_remove); |
| 1344 | EXPORT_SYMBOL(i2o_cntxt_list_get_ptr); |
| 1345 | #endif |
| 1346 | EXPORT_SYMBOL(i2o_msg_get_wait); |
| 1347 | EXPORT_SYMBOL(i2o_msg_nop); |
| 1348 | EXPORT_SYMBOL(i2o_find_iop); |
| 1349 | EXPORT_SYMBOL(i2o_iop_find_device); |
| 1350 | EXPORT_SYMBOL(i2o_event_register); |
| 1351 | EXPORT_SYMBOL(i2o_status_get); |
| 1352 | EXPORT_SYMBOL(i2o_controllers); |