Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / ieee1394 / highlevel.c
blob491e6032bdec911f3887b4cc34feb3eb99c9ee26
1 /*
2 * IEEE 1394 for Linux
4 * Copyright (C) 1999 Andreas E. Bombe
6 * This code is licensed under the GPL. See the file COPYING in the root
7 * directory of the kernel sources for details.
10 * Contributions:
12 * Christian Toegel <christian.toegel@gmx.at>
13 * unregister address space
15 * Manfred Weihs <weihs@ict.tuwien.ac.at>
16 * unregister address space
20 #include <linux/config.h>
21 #include <linux/slab.h>
22 #include <linux/list.h>
23 #include <linux/bitops.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"
33 struct hl_host_info {
34 struct list_head list;
35 struct hpsb_host *host;
36 size_t size;
37 unsigned long key;
38 void *data;
42 static LIST_HEAD(hl_drivers);
43 static DECLARE_RWSEM(hl_drivers_sem);
45 static LIST_HEAD(hl_irqs);
46 static DEFINE_RWLOCK(hl_irqs_lock);
48 static DEFINE_RWLOCK(addr_space_lock);
50 /* addr_space list will have zero and max already included as bounds */
51 static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
52 static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
55 static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
56 struct hpsb_host *host)
58 struct hl_host_info *hi = NULL;
60 if (!hl || !host)
61 return NULL;
63 read_lock(&hl->host_info_lock);
64 list_for_each_entry(hi, &hl->host_info_list, list) {
65 if (hi->host == host) {
66 read_unlock(&hl->host_info_lock);
67 return hi;
70 read_unlock(&hl->host_info_lock);
72 return NULL;
76 /* Returns a per host/driver data structure that was previously stored by
77 * hpsb_create_hostinfo. */
78 void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
80 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
82 if (hi)
83 return hi->data;
85 return NULL;
89 /* If size is zero, then the return here is only valid for error checking */
90 void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
91 size_t data_size)
93 struct hl_host_info *hi;
94 void *data;
95 unsigned long flags;
97 hi = hl_get_hostinfo(hl, host);
98 if (hi) {
99 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already exists",
100 hl->name);
101 return NULL;
104 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
105 if (!hi)
106 return NULL;
108 if (data_size) {
109 data = hi->data = hi + 1;
110 hi->size = data_size;
111 } else
112 data = hi;
114 hi->host = host;
116 write_lock_irqsave(&hl->host_info_lock, flags);
117 list_add_tail(&hi->list, &hl->host_info_list);
118 write_unlock_irqrestore(&hl->host_info_lock, flags);
120 return data;
124 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
125 void *data)
127 struct hl_host_info *hi;
129 hi = hl_get_hostinfo(hl, host);
130 if (hi) {
131 if (!hi->size && !hi->data) {
132 hi->data = data;
133 return 0;
134 } else
135 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo already has data",
136 hl->name);
137 } else
138 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
139 hl->name);
141 return -EINVAL;
145 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
147 struct hl_host_info *hi;
149 hi = hl_get_hostinfo(hl, host);
150 if (hi) {
151 unsigned long flags;
152 write_lock_irqsave(&hl->host_info_lock, flags);
153 list_del(&hi->list);
154 write_unlock_irqrestore(&hl->host_info_lock, flags);
155 kfree(hi);
158 return;
162 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host, unsigned long key)
164 struct hl_host_info *hi;
166 hi = hl_get_hostinfo(hl, host);
167 if (hi)
168 hi->key = key;
170 return;
174 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
176 struct hl_host_info *hi;
177 void *data = NULL;
179 if (!hl)
180 return NULL;
182 read_lock(&hl->host_info_lock);
183 list_for_each_entry(hi, &hl->host_info_list, list) {
184 if (hi->key == key) {
185 data = hi->data;
186 break;
189 read_unlock(&hl->host_info_lock);
191 return data;
195 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
197 struct hpsb_highlevel *hl = __data;
199 hl->add_host(host);
201 if (host->update_config_rom) {
202 if (hpsb_update_config_rom_image(host) < 0) {
203 HPSB_ERR("Failed to generate Configuration ROM image for host "
204 "%s-%d", hl->name, host->id);
208 return 0;
211 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
213 INIT_LIST_HEAD(&hl->addr_list);
214 INIT_LIST_HEAD(&hl->host_info_list);
216 rwlock_init(&hl->host_info_lock);
218 down_write(&hl_drivers_sem);
219 list_add_tail(&hl->hl_list, &hl_drivers);
220 up_write(&hl_drivers_sem);
222 write_lock(&hl_irqs_lock);
223 list_add_tail(&hl->irq_list, &hl_irqs);
224 write_unlock(&hl_irqs_lock);
226 if (hl->add_host)
227 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
229 return;
232 static void __delete_addr(struct hpsb_address_serve *as)
234 list_del(&as->host_list);
235 list_del(&as->hl_list);
236 kfree(as);
239 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host, int update_cr)
241 unsigned long flags;
242 struct list_head *lh, *next;
243 struct hpsb_address_serve *as;
245 /* First, let the highlevel driver unreg */
246 if (hl->remove_host)
247 hl->remove_host(host);
249 /* Remove any addresses that are matched for this highlevel driver
250 * and this particular host. */
251 write_lock_irqsave(&addr_space_lock, flags);
252 list_for_each_safe (lh, next, &hl->addr_list) {
253 as = list_entry(lh, struct hpsb_address_serve, hl_list);
255 if (as->host == host)
256 __delete_addr(as);
258 write_unlock_irqrestore(&addr_space_lock, flags);
260 /* Now update the config-rom to reflect anything removed by the
261 * highlevel driver. */
262 if (update_cr && host->update_config_rom) {
263 if (hpsb_update_config_rom_image(host) < 0) {
264 HPSB_ERR("Failed to generate Configuration ROM image for host "
265 "%s-%d", hl->name, host->id);
269 /* And finally, remove all the host info associated between these
270 * two. */
271 hpsb_destroy_hostinfo(hl, host);
274 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
276 struct hpsb_highlevel *hl = __data;
278 __unregister_host(hl, host, 1);
280 return 0;
283 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
285 write_lock(&hl_irqs_lock);
286 list_del(&hl->irq_list);
287 write_unlock(&hl_irqs_lock);
289 down_write(&hl_drivers_sem);
290 list_del(&hl->hl_list);
291 up_write(&hl_drivers_sem);
293 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
296 u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
297 struct hpsb_host *host,
298 struct hpsb_address_ops *ops,
299 u64 size, u64 alignment,
300 u64 start, u64 end)
302 struct hpsb_address_serve *as, *a1, *a2;
303 struct list_head *entry;
304 u64 retval = ~0ULL;
305 unsigned long flags;
306 u64 align_mask = ~(alignment - 1);
308 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
309 (hweight64(alignment) != 1)) {
310 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
311 __FUNCTION__, (unsigned long long)alignment);
312 return retval;
315 if (start == ~0ULL && end == ~0ULL) {
316 start = CSR1212_ALL_SPACE_BASE + 0xffff00000000ULL; /* ohci1394.c limit */
317 end = CSR1212_ALL_SPACE_END;
320 if (((start|end) & ~align_mask) || (start >= end) || (end > 0x1000000000000ULL)) {
321 HPSB_ERR("%s called with invalid addresses (start = %012Lx end = %012Lx)",
322 __FUNCTION__, (unsigned long long)start, (unsigned long long)end);
323 return retval;
326 as = kmalloc(sizeof(*as), GFP_KERNEL);
327 if (!as)
328 return retval;
330 INIT_LIST_HEAD(&as->host_list);
331 INIT_LIST_HEAD(&as->hl_list);
332 as->op = ops;
333 as->host = host;
335 write_lock_irqsave(&addr_space_lock, flags);
337 list_for_each(entry, &host->addr_space) {
338 u64 a1sa, a1ea;
339 u64 a2sa, a2ea;
341 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
342 a2 = list_entry(entry->next, struct hpsb_address_serve, host_list);
344 a1sa = a1->start & align_mask;
345 a1ea = (a1->end + alignment -1) & align_mask;
346 a2sa = a2->start & align_mask;
347 a2ea = (a2->end + alignment -1) & align_mask;
349 if ((a2sa - a1ea >= size) && (a2sa - start >= size) && (a2sa > start)) {
350 as->start = max(start, a1ea);
351 as->end = as->start + size;
352 list_add(&as->host_list, entry);
353 list_add_tail(&as->hl_list, &hl->addr_list);
354 retval = as->start;
355 break;
359 write_unlock_irqrestore(&addr_space_lock, flags);
361 if (retval == ~0ULL) {
362 kfree(as);
365 return retval;
368 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
369 struct hpsb_address_ops *ops, u64 start, u64 end)
371 struct hpsb_address_serve *as;
372 struct list_head *lh;
373 int retval = 0;
374 unsigned long flags;
376 if (((start|end) & 3) || (start >= end) || (end > 0x1000000000000ULL)) {
377 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
378 return 0;
381 as = kmalloc(sizeof(*as), GFP_ATOMIC);
382 if (!as)
383 return 0;
385 INIT_LIST_HEAD(&as->host_list);
386 INIT_LIST_HEAD(&as->hl_list);
387 as->op = ops;
388 as->start = start;
389 as->end = end;
390 as->host = host;
392 write_lock_irqsave(&addr_space_lock, flags);
394 list_for_each(lh, &host->addr_space) {
395 struct hpsb_address_serve *as_this =
396 list_entry(lh, struct hpsb_address_serve, host_list);
397 struct hpsb_address_serve *as_next =
398 list_entry(lh->next, struct hpsb_address_serve, host_list);
400 if (as_this->end > as->start)
401 break;
403 if (as_next->start >= as->end) {
404 list_add(&as->host_list, lh);
405 list_add_tail(&as->hl_list, &hl->addr_list);
406 retval = 1;
407 break;
410 write_unlock_irqrestore(&addr_space_lock, flags);
412 if (retval == 0)
413 kfree(as);
415 return retval;
418 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
419 u64 start)
421 int retval = 0;
422 struct hpsb_address_serve *as;
423 struct list_head *lh, *next;
424 unsigned long flags;
426 write_lock_irqsave(&addr_space_lock, flags);
428 list_for_each_safe (lh, next, &hl->addr_list) {
429 as = list_entry(lh, struct hpsb_address_serve, hl_list);
430 if (as->start == start && as->host == host) {
431 __delete_addr(as);
432 retval = 1;
433 break;
437 write_unlock_irqrestore(&addr_space_lock, flags);
439 return retval;
442 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
443 unsigned int channel)
445 if (channel > 63) {
446 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
447 return -EINVAL;
450 if (host->iso_listen_count[channel]++ == 0) {
451 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
454 return 0;
457 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
458 unsigned int channel)
460 if (channel > 63) {
461 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
462 return;
465 if (--host->iso_listen_count[channel] == 0) {
466 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
470 static void init_hpsb_highlevel(struct hpsb_host *host)
472 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
473 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
474 INIT_LIST_HEAD(&dummy_max_addr.host_list);
475 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
477 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
479 dummy_zero_addr.start = dummy_zero_addr.end = 0;
480 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
482 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
483 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
486 void highlevel_add_host(struct hpsb_host *host)
488 struct hpsb_highlevel *hl;
490 init_hpsb_highlevel(host);
492 down_read(&hl_drivers_sem);
493 list_for_each_entry(hl, &hl_drivers, hl_list) {
494 if (hl->add_host)
495 hl->add_host(host);
497 up_read(&hl_drivers_sem);
498 if (host->update_config_rom) {
499 if (hpsb_update_config_rom_image(host) < 0)
500 HPSB_ERR("Failed to generate Configuration ROM image for "
501 "host %s-%d", hl->name, host->id);
505 void highlevel_remove_host(struct hpsb_host *host)
507 struct hpsb_highlevel *hl;
509 down_read(&hl_drivers_sem);
510 list_for_each_entry(hl, &hl_drivers, hl_list)
511 __unregister_host(hl, host, 0);
512 up_read(&hl_drivers_sem);
515 void highlevel_host_reset(struct hpsb_host *host)
517 struct hpsb_highlevel *hl;
519 read_lock(&hl_irqs_lock);
520 list_for_each_entry(hl, &hl_irqs, irq_list) {
521 if (hl->host_reset)
522 hl->host_reset(host);
524 read_unlock(&hl_irqs_lock);
527 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
529 struct hpsb_highlevel *hl;
530 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
532 read_lock(&hl_irqs_lock);
533 list_for_each_entry(hl, &hl_irqs, irq_list) {
534 if (hl->iso_receive)
535 hl->iso_receive(host, channel, data, length);
537 read_unlock(&hl_irqs_lock);
540 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
541 void *data, size_t length)
543 struct hpsb_highlevel *hl;
544 int cts = ((quadlet_t *)data)[0] >> 4;
546 read_lock(&hl_irqs_lock);
547 list_for_each_entry(hl, &hl_irqs, irq_list) {
548 if (hl->fcp_request)
549 hl->fcp_request(host, nodeid, direction, cts, data,
550 length);
552 read_unlock(&hl_irqs_lock);
555 int highlevel_read(struct hpsb_host *host, int nodeid, void *data,
556 u64 addr, unsigned int length, u16 flags)
558 struct hpsb_address_serve *as;
559 unsigned int partlength;
560 int rcode = RCODE_ADDRESS_ERROR;
562 read_lock(&addr_space_lock);
564 list_for_each_entry(as, &host->addr_space, host_list) {
565 if (as->start > addr)
566 break;
568 if (as->end > addr) {
569 partlength = min(as->end - addr, (u64) length);
571 if (as->op->read) {
572 rcode = as->op->read(host, nodeid, data,
573 addr, partlength, flags);
574 } else {
575 rcode = RCODE_TYPE_ERROR;
578 data += partlength;
579 length -= partlength;
580 addr += partlength;
582 if ((rcode != RCODE_COMPLETE) || !length) {
583 break;
588 read_unlock(&addr_space_lock);
590 if (length && (rcode == RCODE_COMPLETE)) {
591 rcode = RCODE_ADDRESS_ERROR;
594 return rcode;
597 int highlevel_write(struct hpsb_host *host, int nodeid, int destid,
598 void *data, u64 addr, unsigned int length, u16 flags)
600 struct hpsb_address_serve *as;
601 unsigned int partlength;
602 int rcode = RCODE_ADDRESS_ERROR;
604 read_lock(&addr_space_lock);
606 list_for_each_entry(as, &host->addr_space, host_list) {
607 if (as->start > addr)
608 break;
610 if (as->end > addr) {
611 partlength = min(as->end - addr, (u64) length);
613 if (as->op->write) {
614 rcode = as->op->write(host, nodeid, destid,
615 data, addr, partlength, flags);
616 } else {
617 rcode = RCODE_TYPE_ERROR;
620 data += partlength;
621 length -= partlength;
622 addr += partlength;
624 if ((rcode != RCODE_COMPLETE) || !length) {
625 break;
630 read_unlock(&addr_space_lock);
632 if (length && (rcode == RCODE_COMPLETE)) {
633 rcode = RCODE_ADDRESS_ERROR;
636 return rcode;
640 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
641 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags)
643 struct hpsb_address_serve *as;
644 int rcode = RCODE_ADDRESS_ERROR;
646 read_lock(&addr_space_lock);
648 list_for_each_entry(as, &host->addr_space, host_list) {
649 if (as->start > addr)
650 break;
652 if (as->end > addr) {
653 if (as->op->lock) {
654 rcode = as->op->lock(host, nodeid, store, addr,
655 data, arg, ext_tcode, flags);
656 } else {
657 rcode = RCODE_TYPE_ERROR;
660 break;
664 read_unlock(&addr_space_lock);
666 return rcode;
669 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
670 u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags)
672 struct hpsb_address_serve *as;
673 int rcode = RCODE_ADDRESS_ERROR;
675 read_lock(&addr_space_lock);
677 list_for_each_entry(as, &host->addr_space, host_list) {
678 if (as->start > addr)
679 break;
681 if (as->end > addr) {
682 if (as->op->lock64) {
683 rcode = as->op->lock64(host, nodeid, store,
684 addr, data, arg,
685 ext_tcode, flags);
686 } else {
687 rcode = RCODE_TYPE_ERROR;
690 break;
694 read_unlock(&addr_space_lock);
696 return rcode;