blob: ad49c040b6749017b877bd5607816725820ee7f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/config.h>
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/list.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/pci.h>
20#include <linux/timer.h>
Jody McIntyre63bea352005-09-30 11:59:10 -070021#include <linux/jiffies.h>
Ben Collinsfa7614d2006-06-12 18:11:07 -040022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "csr1212.h"
25#include "ieee1394.h"
26#include "ieee1394_types.h"
27#include "hosts.h"
28#include "ieee1394_core.h"
29#include "highlevel.h"
30#include "nodemgr.h"
31#include "csr.h"
32#include "config_roms.h"
33
34
35static void delayed_reset_bus(void * __reset_info)
36{
37 struct hpsb_host *host = (struct hpsb_host*)__reset_info;
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
63static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
64{
Stefan Richter741854e2005-12-01 18:52:03 -050065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
69{
Stefan Richter741854e2005-12-01 18:52:03 -050070 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
74{
75 return -1;
76}
77
78static struct hpsb_host_driver dummy_driver = {
Stefan Richter741854e2005-12-01 18:52:03 -050079 .transmit_packet = dummy_transmit_packet,
80 .devctl = dummy_devctl,
81 .isoctl = dummy_isoctl
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
84static 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
94/**
95 * hpsb_alloc_host - allocate a new host controller.
96 * @drv: the driver that will manage the host controller
97 * @extra: number of extra bytes to allocate for the driver
98 *
99 * Allocate a &hpsb_host and initialize the general subsystem specific
100 * fields. If the driver needs to store per host data, as drivers
101 * usually do, the amount of memory required can be specified by the
102 * @extra parameter. Once allocated, the driver should initialize the
103 * driver specific parts, enable the controller and make it available
104 * to the general subsystem using hpsb_add_host().
105 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200106 * Return Value: a pointer to the &hpsb_host if successful, %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * no memory was available.
108 */
Ben Collinsfa7614d2006-06-12 18:11:07 -0400109static DEFINE_MUTEX(host_num_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
112 struct device *dev)
113{
Stefan Richter741854e2005-12-01 18:52:03 -0500114 struct hpsb_host *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int i;
116 int hostnum = 0;
117
Stefan Richter741854e2005-12-01 18:52:03 -0500118 h = kzalloc(sizeof(*h) + extra, SLAB_KERNEL);
119 if (!h)
Stefan Richter85511582005-11-07 06:31:45 -0500120 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
123 if (!h->csr.rom) {
124 kfree(h);
125 return NULL;
126 }
127
128 h->hostdata = h + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500129 h->driver = drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 skb_queue_head_init(&h->pending_packet_queue);
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
137 for (i = 0; i < ARRAY_SIZE(h->tpool); i++)
138 HPSB_TPOOL_INIT(&h->tpool[i]);
139
140 atomic_set(&h->generation, 0);
141
142 INIT_WORK(&h->delayed_reset, delayed_reset_bus, h);
143
144 init_timer(&h->timeout);
145 h->timeout.data = (unsigned long) h;
146 h->timeout.function = abort_timedouts;
147 h->timeout_interval = HZ / 20; // 50ms by default
148
Stefan Richter741854e2005-12-01 18:52:03 -0500149 h->topology_map = h->csr.topology_map + 3;
150 h->speed_map = (u8 *)(h->csr.speed_map + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Ben Collinsfa7614d2006-06-12 18:11:07 -0400152 mutex_lock(&host_num_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
155 hostnum++;
156
157 h->id = hostnum;
158
159 memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
160 h->device.parent = dev;
161 snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
162
163 h->class_dev.dev = &h->device;
164 h->class_dev.class = &hpsb_host_class;
165 snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
166
167 device_register(&h->device);
168 class_device_register(&h->class_dev);
169 get_device(&h->device);
170
Ben Collinsfa7614d2006-06-12 18:11:07 -0400171 mutex_unlock(&host_num_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return h;
174}
175
176int hpsb_add_host(struct hpsb_host *host)
177{
178 if (hpsb_default_host_entry(host))
179 return -ENOMEM;
180
181 hpsb_add_extra_config_roms(host);
182
183 highlevel_add_host(host);
184
185 return 0;
186}
187
188void hpsb_remove_host(struct hpsb_host *host)
189{
Stefan Richter741854e2005-12-01 18:52:03 -0500190 host->is_shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 cancel_delayed_work(&host->delayed_reset);
193 flush_scheduled_work();
194
Stefan Richter741854e2005-12-01 18:52:03 -0500195 host->driver = &dummy_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Stefan Richter741854e2005-12-01 18:52:03 -0500197 highlevel_remove_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 hpsb_remove_extra_config_roms(host);
200
201 class_device_unregister(&host->class_dev);
202 device_unregister(&host->device);
203}
204
205int hpsb_update_config_rom_image(struct hpsb_host *host)
206{
207 unsigned long reset_delay;
208 int next_gen = host->csr.generation + 1;
209
210 if (!host->update_config_rom)
211 return -EINVAL;
212
213 if (next_gen > 0xf)
214 next_gen = 2;
215
216 /* Stop the delayed interrupt, we're about to change the config rom and
217 * it would be a waste to do a bus reset twice. */
218 cancel_delayed_work(&host->delayed_reset);
219
220 /* IEEE 1394a-2000 prohibits using the same generation number
221 * twice in a 60 second period. */
Jody McIntyre63bea352005-09-30 11:59:10 -0700222 if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Wait 60 seconds from the last time this generation number was
224 * used. */
225 reset_delay = (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
226 else
227 /* Wait 1 second in case some other code wants to change the
228 * Config ROM in the near future. */
229 reset_delay = HZ;
230
231 PREPARE_WORK(&host->delayed_reset, delayed_reset_bus, host);
232 schedule_delayed_work(&host->delayed_reset, reset_delay);
233
234 return 0;
235}