hosts.c 5.9 KB

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