Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
iop.c
Go to the documentation of this file.
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 <[email protected]>
19  * Auvo Häkkinen <[email protected]>
20  * Deepak Saxena <[email protected]>
21  * Boji T Kannanthanam <[email protected]>
22  * Alan Cox <[email protected]>:
23  * Ported to Linux 2.5.
24  * Markus Lidel <[email protected]>:
25  * Minor fixes for 2.6.
26  */
27 
28 #include <linux/module.h>
29 #include <linux/i2o.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include "core.h"
34 
35 #define OSM_NAME "i2o"
36 #define OSM_VERSION "1.325"
37 #define OSM_DESCRIPTION "I2O subsystem"
38 
39 /* global I2O controller list */
40 LIST_HEAD(i2o_controllers);
41 
42 /*
43  * global I2O System Table. Contains information about all the IOPs in the
44  * system. Used to inform IOPs about each others existence.
45  */
46 static struct i2o_dma i2o_systab;
47 
48 static int i2o_hrt_get(struct i2o_controller *c);
49 
64 {
65  unsigned long timeout = jiffies + wait * HZ;
66  struct i2o_message *msg;
67 
68  while (IS_ERR(msg = i2o_msg_get(c))) {
69  if (time_after(jiffies, timeout)) {
70  osm_debug("%s: Timeout waiting for message frame.\n",
71  c->name);
72  return ERR_PTR(-ETIMEDOUT);
73  }
75  }
76 
77  return msg;
78 };
79 
80 #if BITS_PER_LONG == 64
81 
92 u32 i2o_cntxt_list_add(struct i2o_controller * c, void *ptr)
93 {
95  unsigned long flags;
96 
97  if (!ptr)
98  osm_err("%s: couldn't add NULL pointer to context list!\n",
99  c->name);
100 
101  entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
102  if (!entry) {
103  osm_err("%s: Could not allocate memory for context list element"
104  "\n", c->name);
105  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 
123  osm_debug("%s: Add context to list %p -> %d\n", c->name, ptr, context);
124 
125  return entry->context;
126 };
127 
138 u32 i2o_cntxt_list_remove(struct i2o_controller * c, void *ptr)
139 {
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)
155  osm_warn("%s: Could not remove nonexistent ptr %p\n", c->name,
156  ptr);
157 
158  osm_debug("%s: remove ptr from context list %d -> %p\n", c->name,
159  context, ptr);
160 
161  return context;
162 };
163 
172 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
173 {
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)
189  osm_warn("%s: context id %d not found\n", c->name, context);
190 
191  osm_debug("%s: get ptr from context list %d -> %p\n", c->name, context,
192  ptr);
193 
194  return ptr;
195 };
196 
205 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
206 {
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)
220  osm_warn("%s: Could not find nonexistent ptr %p\n", c->name,
221  ptr);
222 
223  osm_debug("%s: get context id from context list %p -> %d\n", c->name,
224  ptr, context);
225 
226  return context;
227 };
228 #endif
229 
239 {
240  struct i2o_controller *c;
241 
243  if (c->unit == unit)
244  return c;
245  }
246 
247  return NULL;
248 };
249 
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 
280 static int i2o_iop_quiesce(struct i2o_controller *c)
281 {
282  struct i2o_message *msg;
283  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) &&
291  return 0;
292 
294  if (IS_ERR(msg))
295  return PTR_ERR(msg);
296 
298  msg->u.head[1] =
300  ADAPTER_TID);
301 
302  /* Long timeout needed for quiesce if lots of devices */
303  if ((rc = i2o_msg_post_wait(c, msg, 240)))
304  osm_info("%s: Unable to quiesce (status=%#x).\n", c->name, -rc);
305  else
306  osm_debug("%s: Quiesced.\n", c->name);
307 
308  i2o_status_get(c); // Entered READY state
309 
310  return rc;
311 };
312 
321 static int i2o_iop_enable(struct i2o_controller *c)
322 {
323  struct i2o_message *msg;
324  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 
334  if (IS_ERR(msg))
335  return PTR_ERR(msg);
336 
338  msg->u.head[1] =
339  cpu_to_le32(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 |
340  ADAPTER_TID);
341 
342  /* How long of a timeout do we need? */
343  if ((rc = i2o_msg_post_wait(c, msg, 240)))
344  osm_err("%s: Could not enable (status=%#x).\n", c->name, -rc);
345  else
346  osm_debug("%s: Enabled.\n", c->name);
347 
348  i2o_status_get(c); // entered OPERATIONAL state
349 
350  return rc;
351 };
352 
358 static 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 
373 static 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 
392 static int i2o_iop_clear(struct i2o_controller *c)
393 {
394  struct i2o_message *msg;
395  int rc;
396 
398  if (IS_ERR(msg))
399  return PTR_ERR(msg);
400 
401  /* Quiesce all IOPs first */
402  i2o_iop_quiesce_all();
403 
405  msg->u.head[1] =
407  ADAPTER_TID);
408 
409  if ((rc = i2o_msg_post_wait(c, msg, 30)))
410  osm_info("%s: Unable to clear (status=%#x).\n", c->name, -rc);
411  else
412  osm_debug("%s: Cleared.\n", c->name);
413 
414  /* Enable all IOPs */
415  i2o_iop_enable_all();
416 
417  return rc;
418 }
419 
429 static int i2o_iop_init_outbound_queue(struct i2o_controller *c)
430 {
431  u32 m;
432  volatile u8 *status = c->status.virt;
433  struct i2o_message *msg;
434  ulong timeout;
435  int i;
436 
437  osm_debug("%s: Initializing Outbound Queue...\n", c->name);
438 
439  memset(c->status.virt, 0, 4);
440 
442  if (IS_ERR(msg))
443  return PTR_ERR(msg);
444 
446  msg->u.head[1] =
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);
452  /* Outbound msg frame size in words and Initcode */
453  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));
457 
458  i2o_msg_post(c, msg);
459 
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  }
467  }
468 
469  m = c->out_queue.phys;
470 
471  /* Post frames */
472  for (i = 0; i < I2O_MAX_OUTBOUND_MSG_FRAMES; i++) {
473  i2o_flush_reply(c, m);
474  udelay(1); /* Promise */
475  m += I2O_OUTBOUND_MSG_FRAME_SIZE * sizeof(u32);
476  }
477 
478  return 0;
479 }
480 
490 static int i2o_iop_reset(struct i2o_controller *c)
491 {
492  volatile u8 *status = c->status.virt;
493  struct i2o_message *msg;
494  unsigned long timeout;
495  i2o_status_block *sb = c->status_block.virt;
496  int rc = 0;
497 
498  osm_debug("%s: Resetting controller\n", c->name);
499 
501  if (IS_ERR(msg))
502  return PTR_ERR(msg);
503 
504  memset(c->status_block.virt, 0, 8);
505 
506  /* Quiesce all IOPs first */
507  i2o_iop_quiesce_all();
508 
510  msg->u.head[1] =
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));
519 
520  i2o_msg_post(c, msg);
521 
522  /* Wait for a reply */
523  timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
524  while (!*status) {
525  if (time_after(jiffies, timeout))
526  break;
527 
529  }
530 
531  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:
538  /*
539  * Once the reset is sent, the IOP goes into the INIT state
540  * 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 amount of time, we assume the
544  * IOP could not reboot properly.
545  */
546  osm_debug("%s: Reset in progress, waiting for reboot...\n",
547  c->name);
548 
549  while (IS_ERR(msg = i2o_msg_get_wait(c, I2O_TIMEOUT_RESET))) {
550  if (time_after(jiffies, timeout)) {
551  osm_err("%s: IOP reset timeout.\n", c->name);
552  rc = PTR_ERR(msg);
553  goto exit;
554  }
556  }
557  i2o_msg_nop(c, msg);
558 
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;
577  }
578 
579  exit:
580  /* Enable all IOPs */
581  i2o_iop_enable_all();
582 
583  return rc;
584 };
585 
595 static int i2o_iop_activate(struct i2o_controller *c)
596 {
597  i2o_status_block *sb = c->status_block.virt;
598  int rc;
599  int state;
600 
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) {
606  osm_info("%s: Unable to obtain status, attempting a reset.\n",
607  c->name);
608  rc = i2o_iop_reset(c);
609  if (rc)
610  return rc;
611  }
612 
613  if (sb->i2o_version > I2OVER15) {
614  osm_err("%s: Not running version 1.5 of the I2O Specification."
615  "\n", c->name);
616  return -ENODEV;
617  }
618 
619  switch (sb->iop_state) {
621  osm_err("%s: hardware fault\n", c->name);
622  return -EFAULT;
623 
624  case ADAPTER_STATE_READY:
626  case ADAPTER_STATE_HOLD:
628  osm_debug("%s: already running, trying to reset...\n", c->name);
629  rc = i2o_iop_reset(c);
630  if (rc)
631  return rc;
632  }
633 
634  /* preserve state */
635  state = sb->iop_state;
636 
637  rc = i2o_iop_init_outbound_queue(c);
638  if (rc)
639  return rc;
640 
641  /* if adapter was not in RESET state clear now */
642  if (state != ADAPTER_STATE_RESET)
643  i2o_iop_clear(c);
644 
645  i2o_status_get(c);
646 
647  if (sb->iop_state != ADAPTER_STATE_HOLD) {
648  osm_err("%s: failed to bring IOP into HOLD state\n", c->name);
649  return -EIO;
650  }
651 
652  return i2o_hrt_get(c);
653 };
654 
663 static int i2o_iop_systab_set(struct i2o_controller *c)
664 {
665  struct i2o_message *msg;
666  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;
677  osm_info("%s: requires private memory resources.\n", c->name);
678  root = pci_find_parent_resource(c->pdev, res);
679  if (root == NULL)
680  osm_warn("%s: Can't find parent resource!\n", c->name);
681  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 = resource_size(res);
685  sb->current_mem_base = res->start;
686  osm_info("%s: allocated %llu bytes of PCI memory at "
687  "0x%016llX.\n", c->name,
688  (unsigned long long)resource_size(res),
689  (unsigned long long)res->start);
690  }
691  }
692 
693  if (sb->current_io_size < sb->desired_io_size) {
694  struct resource *res = &c->io_resource;
695  res->name = c->pdev->bus->name;
696  res->flags = IORESOURCE_IO;
697  res->start = 0;
698  res->end = 0;
699  osm_info("%s: requires private memory resources.\n", c->name);
700  root = pci_find_parent_resource(c->pdev, res);
701  if (root == NULL)
702  osm_warn("%s: Can't find parent resource!\n", c->name);
703  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 */
704  NULL, NULL) >= 0) {
705  c->io_alloc = 1;
706  sb->current_io_size = resource_size(res);
707  sb->current_mem_base = res->start;
708  osm_info("%s: allocated %llu bytes of PCI I/O at "
709  "0x%016llX.\n", c->name,
710  (unsigned long long)resource_size(res),
711  (unsigned long long)res->start);
712  }
713  }
714 
716  if (IS_ERR(msg))
717  return PTR_ERR(msg);
718 
719  i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
721  if (!i2o_systab.phys) {
722  i2o_msg_nop(c, msg);
723  return -ENOMEM;
724  }
725 
727  msg->u.head[1] =
729  ADAPTER_TID);
730 
731  /*
732  * Provide three SGL-elements:
733  * System table (SysTab), Private memory space declaration and
734  * Private i/o space declaration
735  */
736 
737  msg->body[0] = cpu_to_le32(c->unit + 2);
738  msg->body[1] = cpu_to_le32(0x00000000);
739  msg->body[2] = cpu_to_le32(0x54000000 | i2o_systab.len);
740  msg->body[3] = cpu_to_le32(i2o_systab.phys);
741  msg->body[4] = cpu_to_le32(0x54000000 | sb->current_mem_size);
742  msg->body[5] = cpu_to_le32(sb->current_mem_base);
743  msg->body[6] = cpu_to_le32(0xd4000000 | sb->current_io_size);
744  msg->body[6] = cpu_to_le32(sb->current_io_base);
745 
746  rc = i2o_msg_post_wait(c, msg, 120);
747 
748  dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
750 
751  if (rc < 0)
752  osm_err("%s: Unable to set SysTab (status=%#x).\n", c->name,
753  -rc);
754  else
755  osm_debug("%s: SysTab set.\n", c->name);
756 
757  return rc;
758 }
759 
768 static int i2o_iop_online(struct i2o_controller *c)
769 {
770  int rc;
771 
772  rc = i2o_iop_systab_set(c);
773  if (rc)
774  return rc;
775 
776  /* In READY state */
777  osm_debug("%s: Attempting to enable...\n", c->name);
778  rc = i2o_iop_enable(c);
779  if (rc)
780  return rc;
781 
782  return 0;
783 };
784 
793 {
794  struct i2o_device *dev, *tmp;
795 
796  osm_debug("%s: deleting controller\n", c->name);
797 
799 
800  list_del(&c->list);
801 
802  list_for_each_entry_safe(dev, tmp, &c->devices, list)
803  i2o_device_remove(dev);
804 
805  device_del(&c->device);
806 
807  /* Ask the IOP to switch to RESET state */
808  i2o_iop_reset(c);
809 }
810 
821 static int i2o_systab_build(void)
822 {
823  struct i2o_controller *c, *tmp;
824  int num_controllers = 0;
825  u32 change_ind = 0;
826  int count = 0;
827  struct i2o_sys_tbl *systab = i2o_systab.virt;
828 
829  list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
830  num_controllers++;
831 
832  if (systab) {
833  change_ind = systab->change_ind;
834  kfree(i2o_systab.virt);
835  }
836 
837  /* Header + IOPs */
838  i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
839  sizeof(struct i2o_sys_tbl_entry);
840 
841  systab = i2o_systab.virt = kzalloc(i2o_systab.len, GFP_KERNEL);
842  if (!systab) {
843  osm_err("unable to allocate memory for System Table\n");
844  return -ENOMEM;
845  }
846 
847  systab->version = I2OVERSION;
848  systab->change_ind = change_ind + 1;
849 
850  list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
852 
853  if (count >= num_controllers) {
854  osm_err("controller added while building system table"
855  "\n");
856  break;
857  }
858 
859  sb = c->status_block.virt;
860 
861  /*
862  * Get updated IOP state so we have the latest information
863  *
864  * We should delete the controller at this point if it
865  * doesn't respond since if it's not on the system table
866  * it is techninically not part of the I2O subsystem...
867  */
868  if (unlikely(i2o_status_get(c))) {
869  osm_err("%s: Deleting b/c could not get status while "
870  "attempting to build system table\n", c->name);
871  i2o_iop_remove(c);
872  continue; // try the next one
873  }
874 
875  systab->iops[count].org_id = sb->org_id;
876  systab->iops[count].iop_id = c->unit + 2;
877  systab->iops[count].seg_num = 0;
878  systab->iops[count].i2o_version = sb->i2o_version;
879  systab->iops[count].iop_state = sb->iop_state;
880  systab->iops[count].msg_type = sb->msg_type;
881  systab->iops[count].frame_size = sb->inbound_frame_size;
882  systab->iops[count].last_changed = change_ind;
883  systab->iops[count].iop_capabilities = sb->iop_capabilities;
884  systab->iops[count].inbound_low =
885  i2o_dma_low(c->base.phys + I2O_IN_PORT);
886  systab->iops[count].inbound_high =
887  i2o_dma_high(c->base.phys + I2O_IN_PORT);
888 
889  count++;
890  }
891 
892  systab->num_entries = count;
893 
894  return 0;
895 };
896 
905 static int i2o_parse_hrt(struct i2o_controller *c)
906 {
907  i2o_dump_hrt(c);
908  return 0;
909 };
910 
922 {
923  struct i2o_message *msg;
924  volatile u8 *status_block;
925  unsigned long timeout;
926 
927  status_block = (u8 *) c->status_block.virt;
928  memset(c->status_block.virt, 0, sizeof(i2o_status_block));
929 
931  if (IS_ERR(msg))
932  return PTR_ERR(msg);
933 
935  msg->u.head[1] =
936  cpu_to_le32(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 |
937  ADAPTER_TID);
938  msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
939  msg->u.s.tcntxt = cpu_to_le32(0x00000000);
940  msg->body[0] = cpu_to_le32(0x00000000);
941  msg->body[1] = cpu_to_le32(0x00000000);
942  msg->body[2] = cpu_to_le32(i2o_dma_low(c->status_block.phys));
943  msg->body[3] = cpu_to_le32(i2o_dma_high(c->status_block.phys));
944  msg->body[4] = cpu_to_le32(sizeof(i2o_status_block)); /* always 88 bytes */
945 
946  i2o_msg_post(c, msg);
947 
948  /* Wait for a reply */
949  timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
950  while (status_block[87] != 0xFF) {
951  if (time_after(jiffies, timeout)) {
952  osm_err("%s: Get status timeout.\n", c->name);
953  return -ETIMEDOUT;
954  }
955 
957  }
958 
959 #ifdef DEBUG
960  i2o_debug_state(c);
961 #endif
962 
963  return 0;
964 }
965 
966 /*
967  * i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
968  * @c: I2O controller from which the HRT should be fetched
969  *
970  * The HRT contains information about possible hidden devices but is
971  * mostly useless to us.
972  *
973  * Returns 0 on success or negative error code on failure.
974  */
975 static int i2o_hrt_get(struct i2o_controller *c)
976 {
977  int rc;
978  int i;
979  i2o_hrt *hrt = c->hrt.virt;
980  u32 size = sizeof(i2o_hrt);
981  struct device *dev = &c->pdev->dev;
982 
983  for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
984  struct i2o_message *msg;
985 
987  if (IS_ERR(msg))
988  return PTR_ERR(msg);
989 
991  msg->u.head[1] =
992  cpu_to_le32(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 |
993  ADAPTER_TID);
994  msg->body[0] = cpu_to_le32(0xd0000000 | c->hrt.len);
995  msg->body[1] = cpu_to_le32(c->hrt.phys);
996 
997  rc = i2o_msg_post_wait_mem(c, msg, 20, &c->hrt);
998 
999  if (rc < 0) {
1000  osm_err("%s: Unable to get HRT (status=%#x)\n", c->name,
1001  -rc);
1002  return rc;
1003  }
1004 
1005  size = hrt->num_entries * hrt->entry_len << 2;
1006  if (size > c->hrt.len) {
1007  if (i2o_dma_realloc(dev, &c->hrt, size))
1008  return -ENOMEM;
1009  else
1010  hrt = c->hrt.virt;
1011  } else
1012  return i2o_parse_hrt(c);
1013  }
1014 
1015  osm_err("%s: Unable to get HRT after %d tries, giving up\n", c->name,
1016  I2O_HRT_GET_TRIES);
1017 
1018  return -EBUSY;
1019 }
1020 
1028 static void i2o_iop_release(struct device *dev)
1029 {
1030  struct i2o_controller *c = to_i2o_controller(dev);
1031 
1032  i2o_iop_free(c);
1033 };
1034 
1045 {
1046  static int unit = 0; /* 0 and 1 are NULL IOP and Local Host */
1047  struct i2o_controller *c;
1048  char poolname[32];
1049 
1050  c = kzalloc(sizeof(*c), GFP_KERNEL);
1051  if (!c) {
1052  osm_err("i2o: Insufficient memory to allocate a I2O controller."
1053  "\n");
1054  return ERR_PTR(-ENOMEM);
1055  }
1056 
1057  c->unit = unit++;
1058  sprintf(c->name, "iop%d", c->unit);
1059 
1060  snprintf(poolname, sizeof(poolname), "i2o_%s_msg_inpool", c->name);
1061  if (i2o_pool_alloc
1062  (&c->in_msg, poolname, I2O_INBOUND_MSG_FRAME_SIZE * 4 + sizeof(u32),
1063  I2O_MSG_INPOOL_MIN)) {
1064  kfree(c);
1065  return ERR_PTR(-ENOMEM);
1066  };
1067 
1068  INIT_LIST_HEAD(&c->devices);
1069  spin_lock_init(&c->lock);
1070  mutex_init(&c->lct_lock);
1071 
1073 
1074  c->device.release = &i2o_iop_release;
1075 
1076  dev_set_name(&c->device, "iop%d", c->unit);
1077 
1078 #if BITS_PER_LONG == 64
1079  spin_lock_init(&c->context_list_lock);
1080  atomic_set(&c->context_list_counter, 0);
1081  INIT_LIST_HEAD(&c->context_list);
1082 #endif
1083 
1084  return c;
1085 };
1086 
1097 {
1098  int rc;
1099 
1100  if ((rc = device_add(&c->device))) {
1101  osm_err("%s: could not add controller\n", c->name);
1102  goto iop_reset;
1103  }
1104 
1105  osm_info("%s: Activating I2O controller...\n", c->name);
1106  osm_info("%s: This may take a few minutes if there are many devices\n",
1107  c->name);
1108 
1109  if ((rc = i2o_iop_activate(c))) {
1110  osm_err("%s: could not activate controller\n", c->name);
1111  goto device_del;
1112  }
1113 
1114  osm_debug("%s: building sys table...\n", c->name);
1115 
1116  if ((rc = i2o_systab_build()))
1117  goto device_del;
1118 
1119  osm_debug("%s: online controller...\n", c->name);
1120 
1121  if ((rc = i2o_iop_online(c)))
1122  goto device_del;
1123 
1124  osm_debug("%s: getting LCT...\n", c->name);
1125 
1126  if ((rc = i2o_exec_lct_get(c)))
1127  goto device_del;
1128 
1129  list_add(&c->list, &i2o_controllers);
1130 
1132 
1133  osm_info("%s: Controller added\n", c->name);
1134 
1135  return 0;
1136 
1137  device_del:
1138  device_del(&c->device);
1139 
1140  iop_reset:
1141  i2o_iop_reset(c);
1142 
1143  return rc;
1144 };
1145 
1159 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1160  int tcntxt, u32 evt_mask)
1161 {
1162  struct i2o_controller *c = dev->iop;
1163  struct i2o_message *msg;
1164 
1166  if (IS_ERR(msg))
1167  return PTR_ERR(msg);
1168 
1170  msg->u.head[1] =
1171  cpu_to_le32(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->
1172  lct_data.tid);
1173  msg->u.s.icntxt = cpu_to_le32(drv->context);
1174  msg->u.s.tcntxt = cpu_to_le32(tcntxt);
1175  msg->body[0] = cpu_to_le32(evt_mask);
1176 
1177  i2o_msg_post(c, msg);
1178 
1179  return 0;
1180 };
1181 
1190 static int __init i2o_iop_init(void)
1191 {
1192  int rc = 0;
1193 
1195 
1196  if ((rc = i2o_driver_init()))
1197  goto exit;
1198 
1199  if ((rc = i2o_exec_init()))
1200  goto driver_exit;
1201 
1202  if ((rc = i2o_pci_init()))
1203  goto exec_exit;
1204 
1205  return 0;
1206 
1207  exec_exit:
1208  i2o_exec_exit();
1209 
1210  driver_exit:
1211  i2o_driver_exit();
1212 
1213  exit:
1214  return rc;
1215 }
1216 
1222 static void __exit i2o_iop_exit(void)
1223 {
1224  i2o_pci_exit();
1225  i2o_exec_exit();
1226  i2o_driver_exit();
1227 };
1228 
1229 module_init(i2o_iop_init);
1230 module_exit(i2o_iop_exit);
1231 
1232 MODULE_AUTHOR("Red Hat Software");
1233 MODULE_LICENSE("GPL");
1236 
1237 #if BITS_PER_LONG == 64
1238 EXPORT_SYMBOL(i2o_cntxt_list_add);
1239 EXPORT_SYMBOL(i2o_cntxt_list_get);
1240 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1241 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1242 #endif
1248 EXPORT_SYMBOL(i2o_controllers);