hosts.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * IEEE 1394 for Linux
  3. *
  4. * Low level (host adapter) management.
  5. *
  6. * Copyright (C) 1999 Andreas E. Bombe
  7. * Copyright (C) 1999 Emanuel Pirker
  8. *
  9. * This code is licensed under the GPL. See the file COPYING in the root
  10. * directory of the kernel sources for details.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/list.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/pci.h>
  19. #include <linux/timer.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/mutex.h>
  22. #include "csr1212.h"
  23. #include "ieee1394.h"
  24. #include "ieee1394_types.h"
  25. #include "hosts.h"
  26. #include "ieee1394_core.h"
  27. #include "highlevel.h"
  28. #include "nodemgr.h"
  29. #include "csr.h"
  30. #include "config_roms.h"
  31. static void delayed_reset_bus(void * __reset_info)
  32. {
  33. struct hpsb_host *host = (struct hpsb_host*)__reset_info;
  34. int generation = host->csr.generation + 1;
  35. /* The generation field rolls over to 2 rather than 0 per IEEE
  36. * 1394a-2000. */
  37. if (generation > 0xf || generation < 2)
  38. generation = 2;
  39. CSR_SET_BUS_INFO_GENERATION(host->csr.rom, generation);
  40. if (csr1212_generate_csr_image(host->csr.rom) != CSR1212_SUCCESS) {
  41. /* CSR image creation failed, reset generation field and do not
  42. * issue a bus reset. */
  43. CSR_SET_BUS_INFO_GENERATION(host->csr.rom, host->csr.generation);
  44. return;
  45. }
  46. host->csr.generation = generation;
  47. host->update_config_rom = 0;
  48. if (host->driver->set_hw_config_rom)
  49. host->driver->set_hw_config_rom(host, host->csr.rom->bus_info_data);
  50. host->csr.gen_timestamp[host->csr.generation] = jiffies;
  51. hpsb_reset_bus(host, SHORT_RESET);
  52. }
  53. static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
  54. {
  55. return 0;
  56. }
  57. static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
  58. {
  59. return -1;
  60. }
  61. static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
  62. {
  63. return -1;
  64. }
  65. static struct hpsb_host_driver dummy_driver = {
  66. .transmit_packet = dummy_transmit_packet,
  67. .devctl = dummy_devctl,
  68. .isoctl = dummy_isoctl
  69. };
  70. static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
  71. {
  72. int *hostnum = __data;
  73. if (host->id == *hostnum)
  74. return 1;
  75. return 0;
  76. }
  77. /**
  78. * hpsb_alloc_host - allocate a new host controller.
  79. * @drv: the driver that will manage the host controller
  80. * @extra: number of extra bytes to allocate for the driver
  81. *
  82. * Allocate a &hpsb_host and initialize the general subsystem specific
  83. * fields. If the driver needs to store per host data, as drivers
  84. * usually do, the amount of memory required can be specified by the
  85. * @extra parameter. Once allocated, the driver should initialize the
  86. * driver specific parts, enable the controller and make it available
  87. * to the general subsystem using hpsb_add_host().
  88. *
  89. * Return Value: a pointer to the &hpsb_host if succesful, %NULL if
  90. * no memory was available.
  91. */
  92. static DEFINE_MUTEX(host_num_alloc);
  93. struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
  94. struct device *dev)
  95. {
  96. struct hpsb_host *h;
  97. int i;
  98. int hostnum = 0;
  99. h = kzalloc(sizeof(*h) + extra, SLAB_KERNEL);
  100. if (!h)
  101. return NULL;
  102. h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
  103. if (!h->csr.rom) {
  104. kfree(h);
  105. return NULL;
  106. }
  107. h->hostdata = h + 1;
  108. h->driver = drv;
  109. skb_queue_head_init(&h->pending_packet_queue);
  110. INIT_LIST_HEAD(&h->addr_space);
  111. for (i = 2; i < 16; i++)
  112. h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
  113. for (i = 0; i < ARRAY_SIZE(h->tpool); i++)
  114. HPSB_TPOOL_INIT(&h->tpool[i]);
  115. atomic_set(&h->generation, 0);
  116. INIT_WORK(&h->delayed_reset, delayed_reset_bus, h);
  117. init_timer(&h->timeout);
  118. h->timeout.data = (unsigned long) h;
  119. h->timeout.function = abort_timedouts;
  120. h->timeout_interval = HZ / 20; // 50ms by default
  121. h->topology_map = h->csr.topology_map + 3;
  122. h->speed_map = (u8 *)(h->csr.speed_map + 2);
  123. mutex_lock(&host_num_alloc);
  124. while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
  125. hostnum++;
  126. h->id = hostnum;
  127. memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
  128. h->device.parent = dev;
  129. snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
  130. h->class_dev.dev = &h->device;
  131. h->class_dev.class = &hpsb_host_class;
  132. snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
  133. device_register(&h->device);
  134. class_device_register(&h->class_dev);
  135. get_device(&h->device);
  136. mutex_unlock(&host_num_alloc);
  137. return h;
  138. }
  139. int hpsb_add_host(struct hpsb_host *host)
  140. {
  141. if (hpsb_default_host_entry(host))
  142. return -ENOMEM;
  143. hpsb_add_extra_config_roms(host);
  144. highlevel_add_host(host);
  145. return 0;
  146. }
  147. void hpsb_remove_host(struct hpsb_host *host)
  148. {
  149. host->is_shutdown = 1;
  150. cancel_delayed_work(&host->delayed_reset);
  151. flush_scheduled_work();
  152. host->driver = &dummy_driver;
  153. highlevel_remove_host(host);
  154. hpsb_remove_extra_config_roms(host);
  155. class_device_unregister(&host->class_dev);
  156. device_unregister(&host->device);
  157. }
  158. int hpsb_update_config_rom_image(struct hpsb_host *host)
  159. {
  160. unsigned long reset_delay;
  161. int next_gen = host->csr.generation + 1;
  162. if (!host->update_config_rom)
  163. return -EINVAL;
  164. if (next_gen > 0xf)
  165. next_gen = 2;
  166. /* Stop the delayed interrupt, we're about to change the config rom and
  167. * it would be a waste to do a bus reset twice. */
  168. cancel_delayed_work(&host->delayed_reset);
  169. /* IEEE 1394a-2000 prohibits using the same generation number
  170. * twice in a 60 second period. */
  171. if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
  172. /* Wait 60 seconds from the last time this generation number was
  173. * used. */
  174. reset_delay = (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
  175. else
  176. /* Wait 1 second in case some other code wants to change the
  177. * Config ROM in the near future. */
  178. reset_delay = HZ;
  179. PREPARE_WORK(&host->delayed_reset, delayed_reset_bus, host);
  180. schedule_delayed_work(&host->delayed_reset, reset_delay);
  181. return 0;
  182. }