hosts.c 6.0 KB

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