summaryrefslogtreecommitdiffstats
path: root/drivers/ieee1394/hosts.c
blob: a7b4e0360cd34e60d6cb8650713f6b5a266482d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * IEEE 1394 for Linux
 *
 * Low level (host adapter) management.
 *
 * Copyright (C) 1999 Andreas E. Bombe
 * Copyright (C) 1999 Emanuel Pirker
 *
 * This code is licensed under the GPL.  See the file COPYING in the root
 * directory of the kernel sources for details.
 */

#include <linux/config.h>

#include <linux/types.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>

#include "ieee1394_types.h"
#include "hosts.h"
#include "ieee1394_core.h"
#include "highlevel.h"


static struct hpsb_host_template *templates = NULL;
spinlock_t templates_lock = SPIN_LOCK_UNLOCKED;

/*
 * This function calls the add_host/remove_host hooks for every host currently
 * registered.  Init == TRUE means add_host.
 */
void hl_all_hosts(struct hpsb_highlevel *hl, int init)
{
        struct hpsb_host_template *tmpl;
        struct hpsb_host *host;

        spin_lock(&templates_lock);

        for (tmpl = templates; tmpl != NULL; tmpl = tmpl->next) {
                for (host = tmpl->hosts; host != NULL; host = host->next) {
                        if (host->initialized) {
                                if (init) {
                                        if (hl->op->add_host) {
                                                hl->op->add_host(host);
                                        }
                                } else {
                                        if (hl->op->remove_host) {
                                                hl->op->remove_host(host);
                                        }
                                }
                        }
                }
        }

        spin_unlock(&templates_lock);
}

int hpsb_inc_host_usage(struct hpsb_host *host)
{
        struct hpsb_host_template *tmpl;
        struct hpsb_host *h;
        int retval = 0;
	unsigned long flags;

        spin_lock_irqsave(&templates_lock, flags);

        for (tmpl = templates; (tmpl != NULL) && !retval; tmpl = tmpl->next) {
                for (h = tmpl->hosts; h != NULL; h = h->next) {
                        if (h == host) {
                                tmpl->devctl(h, MODIFY_USAGE, 1);
                                retval = 1;
                                break;
                        }
                }
        }

        spin_unlock_irqrestore(&templates_lock, flags);

        return retval;
}

void hpsb_dec_host_usage(struct hpsb_host *host)
{
        host->template->devctl(host, MODIFY_USAGE, 0);
}

/*
 * The following function is exported for module usage.  It will be called from
 * the detect function of a adapter driver.
 */
struct hpsb_host *hpsb_get_host(struct hpsb_host_template *tmpl, 
                                size_t hd_size)
{
        struct hpsb_host *h;

        h = vmalloc(sizeof(struct hpsb_host) + hd_size);
        if (h == NULL) {
                return NULL;
        }

        memset(h, 0, sizeof(struct hpsb_host) + hd_size);
        INIT_LIST_HEAD(&h->pending_packets);
        spin_lock_init(&h->pending_pkt_lock);

        sema_init(&h->tlabel_count, 64);
        spin_lock_init(&h->tlabel_lock);

        h->timeout_tq.routine = (void (*)(void*))abort_timedouts;
        h->timeout_tq.data = h;

        h->topology_map = h->csr.topology_map + 3;
        h->speed_map = (u8 *)(h->csr.speed_map + 2);

        h->template = tmpl;
        if (hd_size) {
                h->hostdata = &h->embedded_hostdata[0];
        }

        if (tmpl->hosts == NULL) {
                tmpl->hosts = h;
        } else {
                struct hpsb_host *last = tmpl->hosts;

                while (last->next != NULL) {
                        last = last->next;
                }
                last->next = h;
        }

        return h;
}

static void free_all_hosts(struct hpsb_host_template *tmpl)
{
        struct hpsb_host *next, *host = tmpl->hosts;

        while (host) {
                next = host->next;
                vfree(host);
                host = next;
        }
}


static void init_hosts(struct hpsb_host_template *tmpl)
{
        int count;
        struct hpsb_host *host;

        count = tmpl->detect_hosts(tmpl);

        for (host = tmpl->hosts; host != NULL; host = host->next) {
                if (tmpl->initialize_host(host)) {
                        host->initialized = 1;

                        highlevel_add_host(host);
                        hpsb_reset_bus(host);
                }
        }

        tmpl->number_of_hosts = count;
        HPSB_INFO("detected %d %s adapter%c", count, tmpl->name,
                  (count != 1 ? 's' : ' '));
}

static void shutdown_hosts(struct hpsb_host_template *tmpl)
{
        struct hpsb_host *host;

        for (host = tmpl->hosts; host != NULL; host = host->next) {
                if (host->initialized) {
                        host->initialized = 0;
                        abort_requests(host);

                        highlevel_remove_host(host);

                        tmpl->release_host(host);
                        while (test_bit(0, &host->timeout_tq.sync)) {
                                schedule();
                        }
                }
        }
        free_all_hosts(tmpl);
        tmpl->release_host(NULL);

        tmpl->number_of_hosts = 0;
}


static int add_template(struct hpsb_host_template *new)
{
        new->next = NULL;
        new->hosts = NULL;
        new->number_of_hosts = 0;

        spin_lock(&templates_lock);
        if (templates == NULL) {
                templates = new;
        } else {
                struct hpsb_host_template *last = templates;
                while (last->next != NULL) {
                        last = last->next;
                }
                last->next = new;
        }
        spin_unlock(&templates_lock);

        return 0;
}

static int remove_template(struct hpsb_host_template *tmpl)
{
        int retval = 0;

        if (tmpl->number_of_hosts) {
                HPSB_ERR("attempted to remove busy host template "
                         "of %s at address 0x%p", tmpl->name, tmpl);
                return 1;
        }

        spin_lock(&templates_lock);
        if (templates == tmpl) {
                templates = tmpl->next;
        } else {
                struct hpsb_host_template *t;

                t = templates;
                while (t->next != tmpl && t->next != NULL) {
                        t = t->next;
                }

                if (t->next == NULL) {
                        HPSB_ERR("attempted to remove unregistered host template "
                                 "of %s at address 0x%p", tmpl->name, tmpl);
                        retval = -1;
                } else {
                        t->next = tmpl->next;
                }
        }
        spin_unlock(&templates_lock);

        inc_hpsb_generation();
        return retval;
}


/*
 * The following two functions are exported symbols for module usage.
 */
int hpsb_register_lowlevel(struct hpsb_host_template *tmpl)
{
        add_template(tmpl);
        HPSB_INFO("registered %s driver, initializing now", tmpl->name);
        init_hosts(tmpl);

        return 0;
}

void hpsb_unregister_lowlevel(struct hpsb_host_template *tmpl)
{
        shutdown_hosts(tmpl);

        if (remove_template(tmpl)) {
                HPSB_PANIC("remove_template failed on %s", tmpl->name);
        }
}



#ifndef MODULE

/*
 * This is the init function for builtin lowlevel drivers.  To add new drivers
 * put their setup code (get and register template) here.  Module only
 * drivers don't need to touch this.
 */

#define SETUP_TEMPLATE(name, visname) \
do {                                                                       \
        extern struct hpsb_host_template *get_ ## name ## _template(void); \
        t = get_ ## name ## _template();                                   \
                                                                           \
        if (t != NULL) {                                                   \
                if(!hpsb_register_lowlevel(t)) {                           \
                        count++;                                           \
                }                                                          \
        } else {                                                           \
                HPSB_WARN(visname " driver returned no host template");    \
        }                                                                  \
} while (0)

void __init register_builtin_lowlevels()
{
        struct hpsb_host_template *t;
        int count = 0;

        /* Touch t to avoid warning if no drivers are configured to
         * be built directly into the kernel. */
        t = NULL;

#ifdef CONFIG_IEEE1394_PCILYNX
        SETUP_TEMPLATE(lynx, "Lynx");
#endif

#ifdef CONFIG_IEEE1394_AIC5800
        SETUP_TEMPLATE(aic, "AIC-5800");
#endif
 
#ifdef CONFIG_IEEE1394_OHCI1394
        SETUP_TEMPLATE(ohci, "OHCI-1394");
#endif

        HPSB_INFO("%d host adapter%s initialized", count,
                  (count != 1 ? "s" : ""));
}

#undef SETUP_TEMPLATE

#endif /* !MODULE */