blob: 694da82d820b5c7012d004dd14fa1c31bb934bce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1394 for Linux
3 *
4 * Copyright (C) 1999 Andreas E. Bombe
5 *
6 * This code is licensed under the GPL. See the file COPYING in the root
7 * directory of the kernel sources for details.
8 *
9 *
10 * Contributions:
11 *
12 * Christian Toegel <christian.toegel@gmx.at>
13 * unregister address space
14 *
15 * Manfred Weihs <weihs@ict.tuwien.ac.at>
16 * unregister address space
17 *
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/list.h>
22#include <linux/bitops.h>
23
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
31
32struct hl_host_info {
33 struct list_head list;
34 struct hpsb_host *host;
35 size_t size;
36 unsigned long key;
37 void *data;
38};
39
40
41static LIST_HEAD(hl_drivers);
42static DECLARE_RWSEM(hl_drivers_sem);
43
44static LIST_HEAD(hl_irqs);
45static DEFINE_RWLOCK(hl_irqs_lock);
46
47static DEFINE_RWLOCK(addr_space_lock);
48
49/* addr_space list will have zero and max already included as bounds */
50static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
51static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
52
53
54static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
Ben Collins2c4b69b2006-06-12 18:16:16 -040055 struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct hl_host_info *hi = NULL;
58
59 if (!hl || !host)
60 return NULL;
61
62 read_lock(&hl->host_info_lock);
63 list_for_each_entry(hi, &hl->host_info_list, list) {
64 if (hi->host == host) {
65 read_unlock(&hl->host_info_lock);
66 return hi;
67 }
68 }
69 read_unlock(&hl->host_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return NULL;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Returns a per host/driver data structure that was previously stored by
74 * hpsb_create_hostinfo. */
75void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
76{
77 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
78
Ben Collins2c4b69b2006-06-12 18:16:16 -040079 return hi ? hi->data : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* If size is zero, then the return here is only valid for error checking */
83void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
84 size_t data_size)
85{
86 struct hl_host_info *hi;
87 void *data;
88 unsigned long flags;
89
90 hi = hl_get_hostinfo(hl, host);
91 if (hi) {
Ben Collins2c4b69b2006-06-12 18:16:16 -040092 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
93 " exists", hl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return NULL;
95 }
96
Stefan Richter85511582005-11-07 06:31:45 -050097 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!hi)
99 return NULL;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (data_size) {
102 data = hi->data = hi + 1;
103 hi->size = data_size;
104 } else
105 data = hi;
106
107 hi->host = host;
108
109 write_lock_irqsave(&hl->host_info_lock, flags);
110 list_add_tail(&hi->list, &hl->host_info_list);
111 write_unlock_irqrestore(&hl->host_info_lock, flags);
112
113 return data;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
117 void *data)
118{
119 struct hl_host_info *hi;
120
121 hi = hl_get_hostinfo(hl, host);
122 if (hi) {
123 if (!hi->size && !hi->data) {
124 hi->data = data;
125 return 0;
126 } else
Ben Collins2c4b69b2006-06-12 18:16:16 -0400127 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
128 "already has data", hl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } else
130 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
131 hl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EINVAL;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
136{
137 struct hl_host_info *hi;
138
139 hi = hl_get_hostinfo(hl, host);
140 if (hi) {
141 unsigned long flags;
142 write_lock_irqsave(&hl->host_info_lock, flags);
143 list_del(&hi->list);
144 write_unlock_irqrestore(&hl->host_info_lock, flags);
145 kfree(hi);
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return;
148}
149
Ben Collins2c4b69b2006-06-12 18:16:16 -0400150void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
151 unsigned long key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct hl_host_info *hi;
154
155 hi = hl_get_hostinfo(hl, host);
156 if (hi)
157 hi->key = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
162{
163 struct hl_host_info *hi;
164 void *data = NULL;
165
166 if (!hl)
167 return NULL;
168
169 read_lock(&hl->host_info_lock);
170 list_for_each_entry(hi, &hl->host_info_list, list) {
171 if (hi->key == key) {
172 data = hi->data;
173 break;
174 }
175 }
176 read_unlock(&hl->host_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return data;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
181{
182 struct hpsb_highlevel *hl = __data;
183
184 hl->add_host(host);
185
Ben Collins2c4b69b2006-06-12 18:16:16 -0400186 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
187 HPSB_ERR("Failed to generate Configuration ROM image for host "
188 "%s-%d", hl->name, host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190}
191
192void hpsb_register_highlevel(struct hpsb_highlevel *hl)
193{
Ben Collins44515192006-06-12 18:16:01 -0400194 unsigned long flags;
195
Ben Collins2c4b69b2006-06-12 18:16:16 -0400196 INIT_LIST_HEAD(&hl->addr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 INIT_LIST_HEAD(&hl->host_info_list);
198
199 rwlock_init(&hl->host_info_lock);
200
201 down_write(&hl_drivers_sem);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400202 list_add_tail(&hl->hl_list, &hl_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 up_write(&hl_drivers_sem);
204
Ben Collins44515192006-06-12 18:16:01 -0400205 write_lock_irqsave(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&hl->irq_list, &hl_irqs);
Ben Collins44515192006-06-12 18:16:01 -0400207 write_unlock_irqrestore(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (hl->add_host)
210 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400211 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void __delete_addr(struct hpsb_address_serve *as)
215{
216 list_del(&as->host_list);
217 list_del(&as->hl_list);
218 kfree(as);
219}
220
Ben Collins2c4b69b2006-06-12 18:16:16 -0400221static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
222 int update_cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 unsigned long flags;
225 struct list_head *lh, *next;
226 struct hpsb_address_serve *as;
227
228 /* First, let the highlevel driver unreg */
229 if (hl->remove_host)
230 hl->remove_host(host);
231
232 /* Remove any addresses that are matched for this highlevel driver
233 * and this particular host. */
234 write_lock_irqsave(&addr_space_lock, flags);
235 list_for_each_safe (lh, next, &hl->addr_list) {
236 as = list_entry(lh, struct hpsb_address_serve, hl_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (as->host == host)
238 __delete_addr(as);
239 }
240 write_unlock_irqrestore(&addr_space_lock, flags);
241
242 /* Now update the config-rom to reflect anything removed by the
243 * highlevel driver. */
Ben Collins2c4b69b2006-06-12 18:16:16 -0400244 if (update_cr && host->update_config_rom &&
245 hpsb_update_config_rom_image(host) < 0)
246 HPSB_ERR("Failed to generate Configuration ROM image for host "
247 "%s-%d", hl->name, host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Ben Collins2c4b69b2006-06-12 18:16:16 -0400249 /* Finally remove all the host info associated between these two. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 hpsb_destroy_hostinfo(hl, host);
251}
252
253static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
254{
255 struct hpsb_highlevel *hl = __data;
256
257 __unregister_host(hl, host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
261void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
262{
Ben Collins44515192006-06-12 18:16:01 -0400263 unsigned long flags;
264
265 write_lock_irqsave(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 list_del(&hl->irq_list);
Ben Collins44515192006-06-12 18:16:01 -0400267 write_unlock_irqrestore(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 down_write(&hl_drivers_sem);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400270 list_del(&hl->hl_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 up_write(&hl_drivers_sem);
272
273 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
274}
275
276u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
277 struct hpsb_host *host,
278 struct hpsb_address_ops *ops,
279 u64 size, u64 alignment,
280 u64 start, u64 end)
281{
282 struct hpsb_address_serve *as, *a1, *a2;
283 struct list_head *entry;
Ben Collins67372312006-06-12 18:15:31 -0400284 u64 retval = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 unsigned long flags;
286 u64 align_mask = ~(alignment - 1);
287
288 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
Akinobu Mita37d54112006-03-26 01:39:56 -0800289 (hweight64(alignment) != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
291 __FUNCTION__, (unsigned long long)alignment);
292 return retval;
293 }
294
Ben Collins8aef63f2006-06-12 18:13:21 -0400295 /* default range,
296 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
Ben Collins67372312006-06-12 18:15:31 -0400297 if (start == CSR1212_INVALID_ADDR_SPACE &&
298 end == CSR1212_INVALID_ADDR_SPACE) {
Ben Collins8aef63f2006-06-12 18:13:21 -0400299 start = host->middle_addr_space;
Ben Collins67372312006-06-12 18:15:31 -0400300 end = CSR1212_ALL_SPACE_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Ben Collins2c4b69b2006-06-12 18:16:16 -0400303 if (((start|end) & ~align_mask) || (start >= end) ||
304 (end > CSR1212_ALL_SPACE_END)) {
305 HPSB_ERR("%s called with invalid addresses "
306 "(start = %012Lx end = %012Lx)", __FUNCTION__,
307 (unsigned long long)start,(unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return retval;
309 }
310
Stefan Richter85511582005-11-07 06:31:45 -0500311 as = kmalloc(sizeof(*as), GFP_KERNEL);
312 if (!as)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 INIT_LIST_HEAD(&as->host_list);
316 INIT_LIST_HEAD(&as->hl_list);
317 as->op = ops;
318 as->host = host;
319
320 write_lock_irqsave(&addr_space_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 list_for_each(entry, &host->addr_space) {
322 u64 a1sa, a1ea;
323 u64 a2sa, a2ea;
324
325 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400326 a2 = list_entry(entry->next, struct hpsb_address_serve,
327 host_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 a1sa = a1->start & align_mask;
330 a1ea = (a1->end + alignment -1) & align_mask;
331 a2sa = a2->start & align_mask;
332 a2ea = (a2->end + alignment -1) & align_mask;
333
Ben Collins2c4b69b2006-06-12 18:16:16 -0400334 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
335 (a2sa > start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 as->start = max(start, a1ea);
337 as->end = as->start + size;
338 list_add(&as->host_list, entry);
339 list_add_tail(&as->hl_list, &hl->addr_list);
340 retval = as->start;
341 break;
342 }
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 write_unlock_irqrestore(&addr_space_lock, flags);
345
Ben Collins2c4b69b2006-06-12 18:16:16 -0400346 if (retval == CSR1212_INVALID_ADDR_SPACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 kfree(as);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return retval;
349}
350
351int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400352 struct hpsb_address_ops *ops, u64 start, u64 end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400354 struct hpsb_address_serve *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct list_head *lh;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400356 int retval = 0;
357 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Ben Collins2c4b69b2006-06-12 18:16:16 -0400359 if (((start|end) & 3) || (start >= end) ||
360 (end > CSR1212_ALL_SPACE_END)) {
361 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
362 return 0;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Stefan Richter85511582005-11-07 06:31:45 -0500365 as = kmalloc(sizeof(*as), GFP_ATOMIC);
366 if (!as)
367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ben Collins2c4b69b2006-06-12 18:16:16 -0400369 INIT_LIST_HEAD(&as->host_list);
370 INIT_LIST_HEAD(&as->hl_list);
371 as->op = ops;
372 as->start = start;
373 as->end = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 as->host = host;
375
376 write_lock_irqsave(&addr_space_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 list_for_each(lh, &host->addr_space) {
378 struct hpsb_address_serve *as_this =
379 list_entry(lh, struct hpsb_address_serve, host_list);
380 struct hpsb_address_serve *as_next =
Ben Collins2c4b69b2006-06-12 18:16:16 -0400381 list_entry(lh->next, struct hpsb_address_serve,
382 host_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (as_this->end > as->start)
385 break;
386
387 if (as_next->start >= as->end) {
388 list_add(&as->host_list, lh);
389 list_add_tail(&as->hl_list, &hl->addr_list);
390 retval = 1;
391 break;
392 }
393 }
394 write_unlock_irqrestore(&addr_space_lock, flags);
395
396 if (retval == 0)
397 kfree(as);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400398 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400402 u64 start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400404 int retval = 0;
405 struct hpsb_address_serve *as;
406 struct list_head *lh, *next;
407 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ben Collins2c4b69b2006-06-12 18:16:16 -0400409 write_lock_irqsave(&addr_space_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 list_for_each_safe (lh, next, &hl->addr_list) {
Ben Collins2c4b69b2006-06-12 18:16:16 -0400411 as = list_entry(lh, struct hpsb_address_serve, hl_list);
412 if (as->start == start && as->host == host) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 __delete_addr(as);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400414 retval = 1;
415 break;
416 }
417 }
418 write_unlock_irqrestore(&addr_space_lock, flags);
419 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400423 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400425 if (channel > 63) {
426 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
427 return -EINVAL;
428 }
429 if (host->iso_listen_count[channel]++ == 0)
430 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400435 unsigned int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400437 if (channel > 63) {
438 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
439 return;
440 }
441 if (--host->iso_listen_count[channel] == 0)
442 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static void init_hpsb_highlevel(struct hpsb_host *host)
446{
447 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
448 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
449 INIT_LIST_HEAD(&dummy_max_addr.host_list);
450 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
451
452 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
453
454 dummy_zero_addr.start = dummy_zero_addr.end = 0;
455 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
456
457 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
458 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
459}
460
461void highlevel_add_host(struct hpsb_host *host)
462{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400463 struct hpsb_highlevel *hl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 init_hpsb_highlevel(host);
466
467 down_read(&hl_drivers_sem);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400468 list_for_each_entry(hl, &hl_drivers, hl_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (hl->add_host)
470 hl->add_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Ben Collins2c4b69b2006-06-12 18:16:16 -0400472 up_read(&hl_drivers_sem);
473 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
474 HPSB_ERR("Failed to generate Configuration ROM image for host "
475 "%s-%d", hl->name, host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478void highlevel_remove_host(struct hpsb_host *host)
479{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400480 struct hpsb_highlevel *hl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 down_read(&hl_drivers_sem);
483 list_for_each_entry(hl, &hl_drivers, hl_list)
484 __unregister_host(hl, host, 0);
485 up_read(&hl_drivers_sem);
486}
487
488void highlevel_host_reset(struct hpsb_host *host)
489{
Ben Collins44515192006-06-12 18:16:01 -0400490 unsigned long flags;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400491 struct hpsb_highlevel *hl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Ben Collins44515192006-06-12 18:16:01 -0400493 read_lock_irqsave(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 list_for_each_entry(hl, &hl_irqs, irq_list) {
Ben Collins2c4b69b2006-06-12 18:16:16 -0400495 if (hl->host_reset)
496 hl->host_reset(host);
497 }
Ben Collins44515192006-06-12 18:16:01 -0400498 read_unlock_irqrestore(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
502{
Ben Collins44515192006-06-12 18:16:01 -0400503 unsigned long flags;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400504 struct hpsb_highlevel *hl;
505 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Ben Collins2c4b69b2006-06-12 18:16:16 -0400507 read_lock_irqsave(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 list_for_each_entry(hl, &hl_irqs, irq_list) {
Ben Collins2c4b69b2006-06-12 18:16:16 -0400509 if (hl->iso_receive)
510 hl->iso_receive(host, channel, data, length);
511 }
512 read_unlock_irqrestore(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
516 void *data, size_t length)
517{
Ben Collins44515192006-06-12 18:16:01 -0400518 unsigned long flags;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400519 struct hpsb_highlevel *hl;
520 int cts = ((quadlet_t *)data)[0] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ben Collins2c4b69b2006-06-12 18:16:16 -0400522 read_lock_irqsave(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 list_for_each_entry(hl, &hl_irqs, irq_list) {
Ben Collins2c4b69b2006-06-12 18:16:16 -0400524 if (hl->fcp_request)
525 hl->fcp_request(host, nodeid, direction, cts, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 length);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400527 }
528 read_unlock_irqrestore(&hl_irqs_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Ben Collins2c4b69b2006-06-12 18:16:16 -0400531int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
532 unsigned int length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400534 struct hpsb_address_serve *as;
535 unsigned int partlength;
536 int rcode = RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Ben Collins2c4b69b2006-06-12 18:16:16 -0400538 read_lock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 list_for_each_entry(as, &host->addr_space, host_list) {
540 if (as->start > addr)
541 break;
542
Ben Collins2c4b69b2006-06-12 18:16:16 -0400543 if (as->end > addr) {
544 partlength = min(as->end - addr, (u64) length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Ben Collins2c4b69b2006-06-12 18:16:16 -0400546 if (as->op->read)
547 rcode = as->op->read(host, nodeid, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 addr, partlength, flags);
Ben Collins2c4b69b2006-06-12 18:16:16 -0400549 else
550 rcode = RCODE_TYPE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 data += partlength;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400553 length -= partlength;
554 addr += partlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ben Collins2c4b69b2006-06-12 18:16:16 -0400556 if ((rcode != RCODE_COMPLETE) || !length)
557 break;
558 }
559 }
560 read_unlock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Ben Collins2c4b69b2006-06-12 18:16:16 -0400562 if (length && (rcode == RCODE_COMPLETE))
563 rcode = RCODE_ADDRESS_ERROR;
564 return rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Ben Collins2c4b69b2006-06-12 18:16:16 -0400567int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
568 u64 addr, unsigned int length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400570 struct hpsb_address_serve *as;
571 unsigned int partlength;
572 int rcode = RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Ben Collins2c4b69b2006-06-12 18:16:16 -0400574 read_lock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 list_for_each_entry(as, &host->addr_space, host_list) {
576 if (as->start > addr)
577 break;
578
Ben Collins2c4b69b2006-06-12 18:16:16 -0400579 if (as->end > addr) {
580 partlength = min(as->end - addr, (u64) length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Ben Collins2c4b69b2006-06-12 18:16:16 -0400582 if (as->op->write)
583 rcode = as->op->write(host, nodeid, destid,
584 data, addr, partlength,
585 flags);
586 else
587 rcode = RCODE_TYPE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 data += partlength;
Ben Collins2c4b69b2006-06-12 18:16:16 -0400590 length -= partlength;
591 addr += partlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Ben Collins2c4b69b2006-06-12 18:16:16 -0400593 if ((rcode != RCODE_COMPLETE) || !length)
594 break;
595 }
596 }
597 read_unlock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Ben Collins2c4b69b2006-06-12 18:16:16 -0400599 if (length && (rcode == RCODE_COMPLETE))
600 rcode = RCODE_ADDRESS_ERROR;
601 return rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400605 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
606 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400608 struct hpsb_address_serve *as;
609 int rcode = RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Ben Collins2c4b69b2006-06-12 18:16:16 -0400611 read_lock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 list_for_each_entry(as, &host->addr_space, host_list) {
613 if (as->start > addr)
614 break;
615
Ben Collins2c4b69b2006-06-12 18:16:16 -0400616 if (as->end > addr) {
617 if (as->op->lock)
618 rcode = as->op->lock(host, nodeid, store, addr,
619 data, arg, ext_tcode,
620 flags);
621 else
622 rcode = RCODE_TYPE_ERROR;
623 break;
624 }
625 }
626 read_unlock(&addr_space_lock);
627 return rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
Ben Collins2c4b69b2006-06-12 18:16:16 -0400631 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
632 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Ben Collins2c4b69b2006-06-12 18:16:16 -0400634 struct hpsb_address_serve *as;
635 int rcode = RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Ben Collins2c4b69b2006-06-12 18:16:16 -0400637 read_lock(&addr_space_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 list_for_each_entry(as, &host->addr_space, host_list) {
640 if (as->start > addr)
641 break;
642
Ben Collins2c4b69b2006-06-12 18:16:16 -0400643 if (as->end > addr) {
644 if (as->op->lock64)
645 rcode = as->op->lock64(host, nodeid, store,
646 addr, data, arg,
647 ext_tcode, flags);
648 else
649 rcode = RCODE_TYPE_ERROR;
650 break;
651 }
652 }
653 read_unlock(&addr_space_lock);
654 return rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}