ieee1394: remove usage of skb_queue as packet queue
[safe/jmp/linux-2.6] / drivers / ieee1394 / hosts.c
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
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 <linux/jiffies.h>
21 #include <linux/mutex.h>
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
34 static void delayed_reset_bus(struct work_struct *work)
35 {
36         struct hpsb_host *host =
37                 container_of(work, struct hpsb_host, delayed_reset.work);
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.
48                  * Reset generation field and do not issue a bus reset. */
49                 CSR_SET_BUS_INFO_GENERATION(host->csr.rom,
50                                             host->csr.generation);
51                 return;
52         }
53
54         host->csr.generation = generation;
55
56         host->update_config_rom = 0;
57         if (host->driver->set_hw_config_rom)
58                 host->driver->set_hw_config_rom(host,
59                                                 host->csr.rom->bus_info_data);
60
61         host->csr.gen_timestamp[host->csr.generation] = jiffies;
62         hpsb_reset_bus(host, SHORT_RESET);
63 }
64
65 static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
66 {
67         return 0;
68 }
69
70 static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
71 {
72         return -1;
73 }
74
75 static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command,
76                         unsigned long arg)
77 {
78         return -1;
79 }
80
81 static struct hpsb_host_driver dummy_driver = {
82         .transmit_packet = dummy_transmit_packet,
83         .devctl =          dummy_devctl,
84         .isoctl =          dummy_isoctl
85 };
86
87 static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
88 {
89         int *hostnum = __data;
90
91         if (host->id == *hostnum)
92                 return 1;
93
94         return 0;
95 }
96
97 static DEFINE_MUTEX(host_num_alloc);
98
99 /**
100  * hpsb_alloc_host - allocate a new host controller.
101  * @drv: the driver that will manage the host controller
102  * @extra: number of extra bytes to allocate for the driver
103  *
104  * Allocate a &hpsb_host and initialize the general subsystem specific
105  * fields.  If the driver needs to store per host data, as drivers
106  * usually do, the amount of memory required can be specified by the
107  * @extra parameter.  Once allocated, the driver should initialize the
108  * driver specific parts, enable the controller and make it available
109  * to the general subsystem using hpsb_add_host().
110  *
111  * Return Value: a pointer to the &hpsb_host if successful, %NULL if
112  * no memory was available.
113  */
114 struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
115                                   struct device *dev)
116 {
117         struct hpsb_host *h;
118         int i;
119         int hostnum = 0;
120
121         h = kzalloc(sizeof(*h) + extra, GFP_KERNEL);
122         if (!h)
123                 return NULL;
124
125         h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
126         if (!h->csr.rom)
127                 goto fail;
128
129         h->hostdata = h + 1;
130         h->driver = drv;
131
132         INIT_LIST_HEAD(&h->pending_packets);
133         INIT_LIST_HEAD(&h->addr_space);
134
135         for (i = 2; i < 16; i++)
136                 h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
137
138         atomic_set(&h->generation, 0);
139
140         INIT_DELAYED_WORK(&h->delayed_reset, delayed_reset_bus);
141         
142         init_timer(&h->timeout);
143         h->timeout.data = (unsigned long) h;
144         h->timeout.function = abort_timedouts;
145         h->timeout_interval = HZ / 20; /* 50ms, half of minimum SPLIT_TIMEOUT */
146
147         h->topology_map = h->csr.topology_map + 3;
148         h->speed_map = (u8 *)(h->csr.speed_map + 2);
149
150         mutex_lock(&host_num_alloc);
151         while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
152                 hostnum++;
153         mutex_unlock(&host_num_alloc);
154         h->id = hostnum;
155
156         memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
157         h->device.parent = dev;
158         snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
159
160         h->class_dev.dev = &h->device;
161         h->class_dev.class = &hpsb_host_class;
162         snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
163
164         if (device_register(&h->device))
165                 goto fail;
166         if (class_device_register(&h->class_dev)) {
167                 device_unregister(&h->device);
168                 goto fail;
169         }
170         get_device(&h->device);
171
172         return h;
173
174 fail:
175         kfree(h);
176         return NULL;
177 }
178
179 int hpsb_add_host(struct hpsb_host *host)
180 {
181         if (hpsb_default_host_entry(host))
182                 return -ENOMEM;
183         hpsb_add_extra_config_roms(host);
184         highlevel_add_host(host);
185         return 0;
186 }
187
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
196 void hpsb_remove_host(struct hpsb_host *host)
197 {
198         host->is_shutdown = 1;
199
200         cancel_delayed_work(&host->delayed_reset);
201         flush_scheduled_work();
202
203         host->driver = &dummy_driver;
204         highlevel_remove_host(host);
205         hpsb_remove_extra_config_roms(host);
206
207         class_device_unregister(&host->class_dev);
208         device_unregister(&host->device);
209 }
210
211 /**
212  * hpsb_update_config_rom_image - updates configuration ROM image of a host
213  *
214  * Updates the configuration ROM image of a host.  rom_version must be the
215  * current version, otherwise it will fail with return value -1. If this
216  * host does not support config-rom-update, it will return -%EINVAL.
217  * Return value 0 indicates success.
218  */
219 int hpsb_update_config_rom_image(struct hpsb_host *host)
220 {
221         unsigned long reset_delay;
222         int next_gen = host->csr.generation + 1;
223
224         if (!host->update_config_rom)
225                 return -EINVAL;
226
227         if (next_gen > 0xf)
228                 next_gen = 2;
229
230         /* Stop the delayed interrupt, we're about to change the config rom and
231          * it would be a waste to do a bus reset twice. */
232         cancel_delayed_work(&host->delayed_reset);
233
234         /* IEEE 1394a-2000 prohibits using the same generation number
235          * twice in a 60 second period. */
236         if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
237                 /* Wait 60 seconds from the last time this generation number was
238                  * used. */
239                 reset_delay =
240                         (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
241         else
242                 /* Wait 1 second in case some other code wants to change the
243                  * Config ROM in the near future. */
244                 reset_delay = HZ;
245
246         PREPARE_DELAYED_WORK(&host->delayed_reset, delayed_reset_bus);
247         schedule_delayed_work(&host->delayed_reset, reset_delay);
248
249         return 0;
250 }