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