blob: ba09741fc82607fc09269ac4930f09055c84aa9e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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
34static void delayed_reset_bus(void * __reset_info)
35{
36 struct hpsb_host *host = (struct hpsb_host*)__reset_info;
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) {
46 /* CSR image creation failed, reset generation field and do not
47 * issue a bus reset. */
48 CSR_SET_BUS_INFO_GENERATION(host->csr.rom, host->csr.generation);
49 return;
50 }
51
52 host->csr.generation = generation;
53
54 host->update_config_rom = 0;
55 if (host->driver->set_hw_config_rom)
56 host->driver->set_hw_config_rom(host, host->csr.rom->bus_info_data);
57
58 host->csr.gen_timestamp[host->csr.generation] = jiffies;
59 hpsb_reset_bus(host, SHORT_RESET);
60}
61
62static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
63{
Stefan Richter741854e2005-12-01 18:52:03 -050064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
68{
Stefan Richter741854e2005-12-01 18:52:03 -050069 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg)
73{
74 return -1;
75}
76
77static struct hpsb_host_driver dummy_driver = {
Stefan Richter741854e2005-12-01 18:52:03 -050078 .transmit_packet = dummy_transmit_packet,
79 .devctl = dummy_devctl,
80 .isoctl = dummy_isoctl
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static int alloc_hostnum_cb(struct hpsb_host *host, void *__data)
84{
85 int *hostnum = __data;
86
87 if (host->id == *hostnum)
88 return 1;
89
90 return 0;
91}
92
93/**
94 * hpsb_alloc_host - allocate a new host controller.
95 * @drv: the driver that will manage the host controller
96 * @extra: number of extra bytes to allocate for the driver
97 *
98 * Allocate a &hpsb_host and initialize the general subsystem specific
99 * fields. If the driver needs to store per host data, as drivers
100 * usually do, the amount of memory required can be specified by the
101 * @extra parameter. Once allocated, the driver should initialize the
102 * driver specific parts, enable the controller and make it available
103 * to the general subsystem using hpsb_add_host().
104 *
105 * Return Value: a pointer to the &hpsb_host if succesful, %NULL if
106 * no memory was available.
107 */
108static DECLARE_MUTEX(host_num_alloc);
109
110struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
111 struct device *dev)
112{
Stefan Richter741854e2005-12-01 18:52:03 -0500113 struct hpsb_host *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int i;
115 int hostnum = 0;
116
Stefan Richter741854e2005-12-01 18:52:03 -0500117 h = kzalloc(sizeof(*h) + extra, SLAB_KERNEL);
118 if (!h)
Stefan Richter85511582005-11-07 06:31:45 -0500119 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
122 if (!h->csr.rom) {
123 kfree(h);
124 return NULL;
125 }
126
127 h->hostdata = h + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500128 h->driver = drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 skb_queue_head_init(&h->pending_packet_queue);
131 INIT_LIST_HEAD(&h->addr_space);
132
133 for (i = 2; i < 16; i++)
134 h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
135
136 for (i = 0; i < ARRAY_SIZE(h->tpool); i++)
137 HPSB_TPOOL_INIT(&h->tpool[i]);
138
139 atomic_set(&h->generation, 0);
140
141 INIT_WORK(&h->delayed_reset, delayed_reset_bus, h);
142
143 init_timer(&h->timeout);
144 h->timeout.data = (unsigned long) h;
145 h->timeout.function = abort_timedouts;
146 h->timeout_interval = HZ / 20; // 50ms by default
147
Stefan Richter741854e2005-12-01 18:52:03 -0500148 h->topology_map = h->csr.topology_map + 3;
149 h->speed_map = (u8 *)(h->csr.speed_map + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 down(&host_num_alloc);
152
153 while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
154 hostnum++;
155
156 h->id = hostnum;
157
158 memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
159 h->device.parent = dev;
160 snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
161
162 h->class_dev.dev = &h->device;
163 h->class_dev.class = &hpsb_host_class;
164 snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
165
166 device_register(&h->device);
167 class_device_register(&h->class_dev);
168 get_device(&h->device);
169
170 up(&host_num_alloc);
171
172 return h;
173}
174
175int hpsb_add_host(struct hpsb_host *host)
176{
177 if (hpsb_default_host_entry(host))
178 return -ENOMEM;
179
180 hpsb_add_extra_config_roms(host);
181
182 highlevel_add_host(host);
183
184 return 0;
185}
186
187void hpsb_remove_host(struct hpsb_host *host)
188{
Stefan Richter741854e2005-12-01 18:52:03 -0500189 host->is_shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 cancel_delayed_work(&host->delayed_reset);
192 flush_scheduled_work();
193
Stefan Richter741854e2005-12-01 18:52:03 -0500194 host->driver = &dummy_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Stefan Richter741854e2005-12-01 18:52:03 -0500196 highlevel_remove_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 hpsb_remove_extra_config_roms(host);
199
200 class_device_unregister(&host->class_dev);
201 device_unregister(&host->device);
202}
203
204int hpsb_update_config_rom_image(struct hpsb_host *host)
205{
206 unsigned long reset_delay;
207 int next_gen = host->csr.generation + 1;
208
209 if (!host->update_config_rom)
210 return -EINVAL;
211
212 if (next_gen > 0xf)
213 next_gen = 2;
214
215 /* Stop the delayed interrupt, we're about to change the config rom and
216 * it would be a waste to do a bus reset twice. */
217 cancel_delayed_work(&host->delayed_reset);
218
219 /* IEEE 1394a-2000 prohibits using the same generation number
220 * twice in a 60 second period. */
Jody McIntyre63bea352005-09-30 11:59:10 -0700221 if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Wait 60 seconds from the last time this generation number was
223 * used. */
224 reset_delay = (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
225 else
226 /* Wait 1 second in case some other code wants to change the
227 * Config ROM in the near future. */
228 reset_delay = HZ;
229
230 PREPARE_WORK(&host->delayed_reset, delayed_reset_bus, host);
231 schedule_delayed_work(&host->delayed_reset, reset_delay);
232
233 return 0;
234}