blob: 615ba62080131161ec1298ba529e06e3db1d216b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Jody McIntyre63bea352005-09-30 11:59:10 -070020#include <linux/jiffies.h>
Ben Collinsfa7614de2006-06-12 18:11:07 -040021#include <linux/mutex.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
David Howellsc4028952006-11-22 14:57:56 +000034static void delayed_reset_bus(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
David Howellsc4028952006-11-22 14:57:56 +000036 struct hpsb_host *host =
37 container_of(work, struct hpsb_host, delayed_reset.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 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) {
Stefan Richter1ed891c2006-10-10 21:12:39 +020047 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return;
52 }
53
54 host->csr.generation = generation;
55
56 host->update_config_rom = 0;
57 if (host->driver->set_hw_config_rom)
Stefan Richter1ed891c2006-10-10 21:12:39 +020058 host->driver->set_hw_config_rom(host,
59 host->csr.rom->bus_info_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 host->csr.gen_timestamp[host->csr.generation] = jiffies;
62 hpsb_reset_bus(host, SHORT_RESET);
63}
64
65static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p)
66{
Stefan Richter741854e2005-12-01 18:52:03 -050067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg)
71{
Stefan Richter741854e2005-12-01 18:52:03 -050072 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Stefan Richter1ed891c2006-10-10 21:12:39 +020075static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command,
76 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 return -1;
79}
80
81static struct hpsb_host_driver dummy_driver = {
Stefan Richter741854e2005-12-01 18:52:03 -050082 .transmit_packet = dummy_transmit_packet,
83 .devctl = dummy_devctl,
84 .isoctl = dummy_isoctl
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87static 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
Stefan Richter9b4f2e92006-09-17 18:17:19 +020097/*
98 * The pending_packet_queue is special in that it's processed
99 * from hardirq context too (such as hpsb_bus_reset()). Hence
100 * split the lock class from the usual networking skb-head
101 * lock class by using a separate key for it:
102 */
103static struct lock_class_key pending_packet_queue_key;
104
Stefan Richter3c6c65f2006-07-03 12:02:37 -0400105static DEFINE_MUTEX(host_num_alloc);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/**
108 * hpsb_alloc_host - allocate a new host controller.
109 * @drv: the driver that will manage the host controller
110 * @extra: number of extra bytes to allocate for the driver
111 *
112 * Allocate a &hpsb_host and initialize the general subsystem specific
113 * fields. If the driver needs to store per host data, as drivers
114 * usually do, the amount of memory required can be specified by the
115 * @extra parameter. Once allocated, the driver should initialize the
116 * driver specific parts, enable the controller and make it available
117 * to the general subsystem using hpsb_add_host().
118 *
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200119 * Return Value: a pointer to the &hpsb_host if successful, %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * no memory was available.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra,
123 struct device *dev)
124{
Stefan Richter741854e2005-12-01 18:52:03 -0500125 struct hpsb_host *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int i;
127 int hostnum = 0;
128
Christoph Lametere94b1762006-12-06 20:33:17 -0800129 h = kzalloc(sizeof(*h) + extra, GFP_KERNEL);
Stefan Richter741854e2005-12-01 18:52:03 -0500130 if (!h)
Stefan Richter85511582005-11-07 06:31:45 -0500131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h);
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200134 if (!h->csr.rom)
135 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 h->hostdata = h + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500138 h->driver = drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 skb_queue_head_init(&h->pending_packet_queue);
Ingo Molnard3788342006-07-03 00:25:14 -0700141 lockdep_set_class(&h->pending_packet_queue.lock,
142 &pending_packet_queue_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 INIT_LIST_HEAD(&h->addr_space);
144
145 for (i = 2; i < 16; i++)
146 h->csr.gen_timestamp[i] = jiffies - 60 * HZ;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 atomic_set(&h->generation, 0);
149
David Howellsc4028952006-11-22 14:57:56 +0000150 INIT_DELAYED_WORK(&h->delayed_reset, delayed_reset_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 init_timer(&h->timeout);
153 h->timeout.data = (unsigned long) h;
154 h->timeout.function = abort_timedouts;
Stefan Richter1ed891c2006-10-10 21:12:39 +0200155 h->timeout_interval = HZ / 20; /* 50ms, half of minimum SPLIT_TIMEOUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Stefan Richter741854e2005-12-01 18:52:03 -0500157 h->topology_map = h->csr.topology_map + 3;
158 h->speed_map = (u8 *)(h->csr.speed_map + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Ben Collinsfa7614de2006-06-12 18:11:07 -0400160 mutex_lock(&host_num_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb))
162 hostnum++;
Stefan Richter5c37dcb2006-10-10 21:11:43 +0200163 mutex_unlock(&host_num_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 h->id = hostnum;
165
166 memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device));
167 h->device.parent = dev;
168 snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id);
169
170 h->class_dev.dev = &h->device;
171 h->class_dev.class = &hpsb_host_class;
172 snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id);
173
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200174 if (device_register(&h->device))
175 goto fail;
176 if (class_device_register(&h->class_dev)) {
177 device_unregister(&h->device);
178 goto fail;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 get_device(&h->device);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return h;
Stefan Richterc1c9c7c2006-10-10 21:19:21 +0200183
184fail:
185 kfree(h);
186 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189int hpsb_add_host(struct hpsb_host *host)
190{
191 if (hpsb_default_host_entry(host))
192 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 hpsb_add_extra_config_roms(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 highlevel_add_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196}
197
Stefan Richter33601772007-01-07 21:49:27 +0100198void hpsb_resume_host(struct hpsb_host *host)
199{
200 if (host->driver->set_hw_config_rom)
201 host->driver->set_hw_config_rom(host,
202 host->csr.rom->bus_info_data);
203 host->driver->devctl(host, RESET_BUS, SHORT_RESET);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206void hpsb_remove_host(struct hpsb_host *host)
207{
Stefan Richter741854e2005-12-01 18:52:03 -0500208 host->is_shutdown = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 cancel_delayed_work(&host->delayed_reset);
211 flush_scheduled_work();
212
Stefan Richter741854e2005-12-01 18:52:03 -0500213 host->driver = &dummy_driver;
Stefan Richter741854e2005-12-01 18:52:03 -0500214 highlevel_remove_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 hpsb_remove_extra_config_roms(host);
216
217 class_device_unregister(&host->class_dev);
218 device_unregister(&host->device);
219}
220
Stefan Richterafd65462007-03-05 03:06:23 +0100221/**
222 * hpsb_update_config_rom_image - updates configuration ROM image of a host
223 *
224 * Updates the configuration ROM image of a host. rom_version must be the
225 * current version, otherwise it will fail with return value -1. If this
226 * host does not support config-rom-update, it will return -%EINVAL.
227 * Return value 0 indicates success.
228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229int hpsb_update_config_rom_image(struct hpsb_host *host)
230{
231 unsigned long reset_delay;
232 int next_gen = host->csr.generation + 1;
233
234 if (!host->update_config_rom)
235 return -EINVAL;
236
237 if (next_gen > 0xf)
238 next_gen = 2;
239
240 /* Stop the delayed interrupt, we're about to change the config rom and
241 * it would be a waste to do a bus reset twice. */
242 cancel_delayed_work(&host->delayed_reset);
243
244 /* IEEE 1394a-2000 prohibits using the same generation number
245 * twice in a 60 second period. */
Jody McIntyre63bea352005-09-30 11:59:10 -0700246 if (time_before(jiffies, host->csr.gen_timestamp[next_gen] + 60 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Wait 60 seconds from the last time this generation number was
248 * used. */
Stefan Richter1ed891c2006-10-10 21:12:39 +0200249 reset_delay =
250 (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 else
252 /* Wait 1 second in case some other code wants to change the
253 * Config ROM in the near future. */
254 reset_delay = HZ;
255
David Howellsc4028952006-11-22 14:57:56 +0000256 PREPARE_DELAYED_WORK(&host->delayed_reset, delayed_reset_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 schedule_delayed_work(&host->delayed_reset, reset_delay);
258
259 return 0;
260}