hosts.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/timer.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/mutex.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(struct work_struct *work)
  30. {
  31. struct hpsb_host *host =
  32. container_of(work, struct hpsb_host, delayed_reset.work);
  33. u8 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.
  41. * Reset generation field and do not issue a bus reset. */
  42. csr_set_bus_info_generation(host->csr.rom,
  43. 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,
  50. host->csr.rom->bus_info_data);
  51. host->csr.gen_timestamp[host->csr.generation] = jiffies;
  52. hpsb_reset_bus(host, SHORT_RESET);
  53. }
  54. static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
  55. {
  56. return 0;
  57. }
  58. static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
  59. {
  60. return -1;
  61. }
  62. static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command,
  63. unsigned long arg)
  64. {
  65. return -1;
  66. }
  67. static struct hpsb_host_driver dummy_driver = {
  68. .transmit_packet = dummy_transmit_packet,
  69. .devctl = dummy_devctl,
  70. .isoctl = dummy_isoctl
  71. };
  72. static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
  73. {
  74. int *hostnum = __data;
  75. if (host->id == *hostnum)
  76. return 1;
  77. return 0;
  78. }
  79. static DEFINE_MUTEX(host_num_alloc);
  80. /**
  81. * hpsb_alloc_host - allocate a new host controller.
  82. * @drv: the driver that will manage the host controller
  83. * @extra: number of extra bytes to allocate for the driver
  84. *
  85. * Allocate a &hpsb_host and initialize the general subsystem specific
  86. * fields. If the driver needs to store per host data, as drivers
  87. * usually do, the amount of memory required can be specified by the
  88. * @extra parameter. Once allocated, the driver should initialize the
  89. * driver specific parts, enable the controller and make it available
  90. * to the general subsystem using hpsb_add_host().
  91. *
  92. * Return Value: a pointer to the &hpsb_host if successful, %NULL if
  93. * no memory was available.
  94. */
  95. struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
  96. struct device *dev)
  97. {
  98. struct hpsb_host *h;
  99. int i;
  100. int hostnum = 0;
  101. h = kzalloc(sizeof(*h) + extra, GFP_KERNEL);
  102. if (!h)
  103. return NULL;
  104. h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
  105. if (!h->csr.rom)
  106. goto fail;
  107. h->hostdata = h + 1;
  108. h->driver = drv;
  109. INIT_LIST_HEAD(&h->pending_packets);
  110. INIT_LIST_HEAD(&h->addr_space);
  111. for (i = 2; i < 16; i++)
  112. h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
  113. atomic_set(&h->generation, 0);
  114. INIT_DELAYED_WORK(&h->delayed_reset, delayed_reset_bus);
  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, half of minimum SPLIT_TIMEOUT */
  119. h->topology_map = h->csr.topology_map + 3;
  120. h->speed_map = (u8 *)(h->csr.speed_map + 2);
  121. mutex_lock(&host_num_alloc);
  122. while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
  123. hostnum++;
  124. mutex_unlock(&host_num_alloc);
  125. h->id = hostnum;
  126. memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
  127. h->device.parent = dev;
  128. set_dev_node(&h->device, dev_to_node(dev));
  129. dev_set_name(&h->device, "fw-host%d", h->id);
  130. h->host_dev.parent = &h->device;
  131. h->host_dev.class = &hpsb_host_class;
  132. dev_set_name(&h->host_dev, "fw-host%d", h->id);
  133. if (device_register(&h->device))
  134. goto fail;
  135. if (device_register(&h->host_dev)) {
  136. device_unregister(&h->device);
  137. goto fail;
  138. }
  139. get_device(&h->device);
  140. return h;
  141. fail:
  142. kfree(h);
  143. return NULL;
  144. }
  145. int hpsb_add_host(struct hpsb_host *host)
  146. {
  147. if (hpsb_default_host_entry(host))
  148. return -ENOMEM;
  149. highlevel_add_host(host);
  150. return 0;
  151. }
  152. void hpsb_resume_host(struct hpsb_host *host)
  153. {
  154. if (host->driver->set_hw_config_rom)
  155. host->driver->set_hw_config_rom(host,
  156. host->csr.rom->bus_info_data);
  157. host->driver->devctl(host, RESET_BUS, SHORT_RESET);
  158. }
  159. void hpsb_remove_host(struct hpsb_host *host)
  160. {
  161. host->is_shutdown = 1;
  162. cancel_delayed_work(&host->delayed_reset);
  163. flush_scheduled_work();
  164. host->driver = &dummy_driver;
  165. highlevel_remove_host(host);
  166. device_unregister(&host->host_dev);
  167. device_unregister(&host->device);
  168. }
  169. /**
  170. * hpsb_update_config_rom_image - updates configuration ROM image of a host
  171. *
  172. * Updates the configuration ROM image of a host. rom_version must be the
  173. * current version, otherwise it will fail with return value -1. If this
  174. * host does not support config-rom-update, it will return -%EINVAL.
  175. * Return value 0 indicates success.
  176. */
  177. int hpsb_update_config_rom_image(struct hpsb_host *host)
  178. {
  179. unsigned long reset_delay;
  180. int next_gen = host->csr.generation + 1;
  181. if (!host->update_config_rom)
  182. return -EINVAL;
  183. if (next_gen > 0xf)
  184. next_gen = 2;
  185. /* Stop the delayed interrupt, we're about to change the config rom and
  186. * it would be a waste to do a bus reset twice. */
  187. cancel_delayed_work(&host->delayed_reset);
  188. /* IEEE 1394a-2000 prohibits using the same generation number
  189. * twice in a 60 second period. */
  190. if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
  191. /* Wait 60 seconds from the last time this generation number was
  192. * used. */
  193. reset_delay =
  194. (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
  195. else
  196. /* Wait 1 second in case some other code wants to change the
  197. * Config ROM in the near future. */
  198. reset_delay = HZ;
  199. PREPARE_DELAYED_WORK(&host->delayed_reset, delayed_reset_bus);
  200. schedule_delayed_work(&host->delayed_reset, reset_delay);
  201. return 0;
  202. }