Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/timer.h> |
Jody McIntyre | 63bea35 | 2005-09-30 11:59:10 -0700 | [diff] [blame] | 19 | #include <linux/jiffies.h> |
Ben Collins | fa7614d | 2006-06-12 18:11:07 -0400 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 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 | |
| 32 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 33 | static void delayed_reset_bus(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 35 | struct hpsb_host *host = |
| 36 | container_of(work, struct hpsb_host, delayed_reset.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | int generation = host->csr.generation + 1; |
| 38 | |
| 39 | /* The generation field rolls over to 2 rather than 0 per IEEE |
| 40 | * 1394a-2000. */ |
| 41 | if (generation > 0xf || generation < 2) |
| 42 | generation = 2; |
| 43 | |
| 44 | CSR_SET_BUS_INFO_GENERATION(host->csr.rom, generation); |
| 45 | if (csr1212_generate_csr_image(host->csr.rom) != CSR1212_SUCCESS) { |
Stefan Richter | 1ed891c | 2006-10-10 21:12:39 +0200 | [diff] [blame] | 46 | /* CSR image creation failed. |
| 47 | * Reset generation field and do not issue a bus reset. */ |
| 48 | CSR_SET_BUS_INFO_GENERATION(host->csr.rom, |
| 49 | host->csr.generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | host->csr.generation = generation; |
| 54 | |
| 55 | host->update_config_rom = 0; |
| 56 | if (host->driver->set_hw_config_rom) |
Stefan Richter | 1ed891c | 2006-10-10 21:12:39 +0200 | [diff] [blame] | 57 | host->driver->set_hw_config_rom(host, |
| 58 | host->csr.rom->bus_info_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | host->csr.gen_timestamp[host->csr.generation] = jiffies; |
| 61 | hpsb_reset_bus(host, SHORT_RESET); |
| 62 | } |
| 63 | |
| 64 | static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p) |
| 65 | { |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 66 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg) |
| 70 | { |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 71 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Stefan Richter | 1ed891c | 2006-10-10 21:12:39 +0200 | [diff] [blame] | 74 | static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, |
| 75 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | static struct hpsb_host_driver dummy_driver = { |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 81 | .transmit_packet = dummy_transmit_packet, |
| 82 | .devctl = dummy_devctl, |
| 83 | .isoctl = dummy_isoctl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int alloc_hostnum_cb(struct hpsb_host *host, void *__data) |
| 87 | { |
| 88 | int *hostnum = __data; |
| 89 | |
| 90 | if (host->id == *hostnum) |
| 91 | return 1; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Stefan Richter | 3c6c65f | 2006-07-03 12:02:37 -0400 | [diff] [blame] | 96 | static DEFINE_MUTEX(host_num_alloc); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /** |
| 99 | * hpsb_alloc_host - allocate a new host controller. |
| 100 | * @drv: the driver that will manage the host controller |
| 101 | * @extra: number of extra bytes to allocate for the driver |
| 102 | * |
| 103 | * Allocate a &hpsb_host and initialize the general subsystem specific |
| 104 | * fields. If the driver needs to store per host data, as drivers |
| 105 | * usually do, the amount of memory required can be specified by the |
| 106 | * @extra parameter. Once allocated, the driver should initialize the |
| 107 | * driver specific parts, enable the controller and make it available |
| 108 | * to the general subsystem using hpsb_add_host(). |
| 109 | * |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 110 | * Return Value: a pointer to the &hpsb_host if successful, %NULL if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * no memory was available. |
| 112 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra, |
| 114 | struct device *dev) |
| 115 | { |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 116 | struct hpsb_host *h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int i; |
| 118 | int hostnum = 0; |
| 119 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 120 | h = kzalloc(sizeof(*h) + extra, GFP_KERNEL); |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 121 | if (!h) |
Stefan Richter | 8551158 | 2005-11-07 06:31:45 -0500 | [diff] [blame] | 122 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h); |
Stefan Richter | c1c9c7c | 2006-10-10 21:19:21 +0200 | [diff] [blame] | 125 | if (!h->csr.rom) |
| 126 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | h->hostdata = h + 1; |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 129 | h->driver = drv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Stefan Richter | 7542e0e | 2007-03-25 22:22:40 +0200 | [diff] [blame] | 131 | INIT_LIST_HEAD(&h->pending_packets); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | INIT_LIST_HEAD(&h->addr_space); |
| 133 | |
| 134 | for (i = 2; i < 16; i++) |
| 135 | h->csr.gen_timestamp[i] = jiffies - 60 * HZ; |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | atomic_set(&h->generation, 0); |
| 138 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 139 | INIT_DELAYED_WORK(&h->delayed_reset, delayed_reset_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | init_timer(&h->timeout); |
| 142 | h->timeout.data = (unsigned long) h; |
| 143 | h->timeout.function = abort_timedouts; |
Stefan Richter | 1ed891c | 2006-10-10 21:12:39 +0200 | [diff] [blame] | 144 | h->timeout_interval = HZ / 20; /* 50ms, half of minimum SPLIT_TIMEOUT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 146 | h->topology_map = h->csr.topology_map + 3; |
| 147 | h->speed_map = (u8 *)(h->csr.speed_map + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Ben Collins | fa7614d | 2006-06-12 18:11:07 -0400 | [diff] [blame] | 149 | mutex_lock(&host_num_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb)) |
| 151 | hostnum++; |
Stefan Richter | 5c37dcb | 2006-10-10 21:11:43 +0200 | [diff] [blame] | 152 | mutex_unlock(&host_num_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | h->id = hostnum; |
| 154 | |
| 155 | memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device)); |
| 156 | h->device.parent = dev; |
Stefan Richter | 93f2e02 | 2007-07-08 13:34:21 +0200 | [diff] [blame] | 157 | set_dev_node(&h->device, dev_to_node(dev)); |
Kay Sievers | 233976e | 2008-10-30 01:49:20 +0100 | [diff] [blame] | 158 | dev_set_name(&h->device, "fw-host%d", h->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Kay Sievers | dd7f292 | 2007-05-25 11:50:53 +0200 | [diff] [blame] | 160 | h->host_dev.parent = &h->device; |
| 161 | h->host_dev.class = &hpsb_host_class; |
Kay Sievers | 233976e | 2008-10-30 01:49:20 +0100 | [diff] [blame] | 162 | dev_set_name(&h->host_dev, "fw-host%d", h->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Stefan Richter | c1c9c7c | 2006-10-10 21:19:21 +0200 | [diff] [blame] | 164 | if (device_register(&h->device)) |
| 165 | goto fail; |
Kay Sievers | dd7f292 | 2007-05-25 11:50:53 +0200 | [diff] [blame] | 166 | if (device_register(&h->host_dev)) { |
Stefan Richter | c1c9c7c | 2006-10-10 21:19:21 +0200 | [diff] [blame] | 167 | device_unregister(&h->device); |
| 168 | goto fail; |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | get_device(&h->device); |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return h; |
Stefan Richter | c1c9c7c | 2006-10-10 21:19:21 +0200 | [diff] [blame] | 173 | |
| 174 | fail: |
| 175 | kfree(h); |
| 176 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int hpsb_add_host(struct hpsb_host *host) |
| 180 | { |
| 181 | if (hpsb_default_host_entry(host)) |
| 182 | return -ENOMEM; |
Stefan Richter | 70093cf | 2007-03-27 01:36:50 +0200 | [diff] [blame] | 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | highlevel_add_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Stefan Richter | 3360177 | 2007-01-07 21:49:27 +0100 | [diff] [blame] | 188 | void hpsb_resume_host(struct hpsb_host *host) |
| 189 | { |
| 190 | if (host->driver->set_hw_config_rom) |
| 191 | host->driver->set_hw_config_rom(host, |
| 192 | host->csr.rom->bus_info_data); |
| 193 | host->driver->devctl(host, RESET_BUS, SHORT_RESET); |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | void hpsb_remove_host(struct hpsb_host *host) |
| 197 | { |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 198 | host->is_shutdown = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | cancel_delayed_work(&host->delayed_reset); |
| 201 | flush_scheduled_work(); |
| 202 | |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 203 | host->driver = &dummy_driver; |
Stefan Richter | 741854e | 2005-12-01 18:52:03 -0500 | [diff] [blame] | 204 | highlevel_remove_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Kay Sievers | dd7f292 | 2007-05-25 11:50:53 +0200 | [diff] [blame] | 206 | device_unregister(&host->host_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | device_unregister(&host->device); |
| 208 | } |
| 209 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 210 | /** |
| 211 | * hpsb_update_config_rom_image - updates configuration ROM image of a host |
| 212 | * |
| 213 | * Updates the configuration ROM image of a host. rom_version must be the |
| 214 | * current version, otherwise it will fail with return value -1. If this |
| 215 | * host does not support config-rom-update, it will return -%EINVAL. |
| 216 | * Return value 0 indicates success. |
| 217 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int hpsb_update_config_rom_image(struct hpsb_host *host) |
| 219 | { |
| 220 | unsigned long reset_delay; |
| 221 | int next_gen = host->csr.generation + 1; |
| 222 | |
| 223 | if (!host->update_config_rom) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if (next_gen > 0xf) |
| 227 | next_gen = 2; |
| 228 | |
| 229 | /* Stop the delayed interrupt, we're about to change the config rom and |
| 230 | * it would be a waste to do a bus reset twice. */ |
| 231 | cancel_delayed_work(&host->delayed_reset); |
| 232 | |
| 233 | /* IEEE 1394a-2000 prohibits using the same generation number |
| 234 | * twice in a 60 second period. */ |
Jody McIntyre | 63bea35 | 2005-09-30 11:59:10 -0700 | [diff] [blame] | 235 | if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* Wait 60 seconds from the last time this generation number was |
| 237 | * used. */ |
Stefan Richter | 1ed891c | 2006-10-10 21:12:39 +0200 | [diff] [blame] | 238 | reset_delay = |
| 239 | (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | else |
| 241 | /* Wait 1 second in case some other code wants to change the |
| 242 | * Config ROM in the near future. */ |
| 243 | reset_delay = HZ; |
| 244 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 245 | PREPARE_DELAYED_WORK(&host->delayed_reset, delayed_reset_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | schedule_delayed_work(&host->delayed_reset, reset_delay); |
| 247 | |
| 248 | return 0; |
| 249 | } |