Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / ieee1394 / highlevel.c
blob997e1bf6297f8b1d1a6f797594bdeda16d1e7183
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 = kmalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
105 if (!hi)
106 return NULL;
108 memset(hi, 0, sizeof(*hi) + data_size);
110 if (data_size) {
111 data = hi->data = hi + 1;
112 hi->size = data_size;
113 } else
114 data = hi;
116 hi->host = host;
118 write_lock_irqsave(&hl->host_info_lock, flags);
119 list_add_tail(&hi->list, &hl->host_info_list);
120 write_unlock_irqrestore(&hl->host_info_lock, flags);
122 return data;
126 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
127 void *data)
129 struct hl_host_info *hi;
131 hi = hl_get_hostinfo(hl, host);
132 if (hi) {
133 if (!hi->size && !hi->data) {
134 hi->data = data;
135 return 0;
136 } else
137 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo already has data",
138 hl->name);
139 } else
140 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
141 hl->name);
143 return -EINVAL;
147 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
149 struct hl_host_info *hi;
151 hi = hl_get_hostinfo(hl, host);
152 if (hi) {
153 unsigned long flags;
154 write_lock_irqsave(&hl->host_info_lock, flags);
155 list_del(&hi->list);
156 write_unlock_irqrestore(&hl->host_info_lock, flags);
157 kfree(hi);
160 return;
164 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host, unsigned long key)
166 struct hl_host_info *hi;
168 hi = hl_get_hostinfo(hl, host);
169 if (hi)
170 hi->key = key;
172 return;
176 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
178 struct hl_host_info *hi;
179 void *data = NULL;
181 if (!hl)
182 return NULL;
184 read_lock(&hl->host_info_lock);
185 list_for_each_entry(hi, &hl->host_info_list, list) {
186 if (hi->key == key) {
187 data = hi->data;
188 break;
191 read_unlock(&hl->host_info_lock);
193 return data;
197 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
199 struct hpsb_highlevel *hl = __data;
201 hl->add_host(host);
203 if (host->update_config_rom) {
204 if (hpsb_update_config_rom_image(host) < 0) {
205 HPSB_ERR("Failed to generate Configuration ROM image for host "
206 "%s-%d", hl->name, host->id);
210 return 0;
213 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
215 INIT_LIST_HEAD(&hl->addr_list);
216 INIT_LIST_HEAD(&hl->host_info_list);
218 rwlock_init(&hl->host_info_lock);
220 down_write(&hl_drivers_sem);
221 list_add_tail(&hl->hl_list, &hl_drivers);
222 up_write(&hl_drivers_sem);
224 write_lock(&hl_irqs_lock);
225 list_add_tail(&hl->irq_list, &hl_irqs);
226 write_unlock(&hl_irqs_lock);
228 if (hl->add_host)
229 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
231 return;
234 static void __delete_addr(struct hpsb_address_serve *as)
236 list_del(&as->host_list);
237 list_del(&as->hl_list);
238 kfree(as);
241 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host, int update_cr)
243 unsigned long flags;
244 struct list_head *lh, *next;
245 struct hpsb_address_serve *as;
247 /* First, let the highlevel driver unreg */
248 if (hl->remove_host)
249 hl->remove_host(host);
251 /* Remove any addresses that are matched for this highlevel driver
252 * and this particular host. */
253 write_lock_irqsave(&addr_space_lock, flags);
254 list_for_each_safe (lh, next, &hl->addr_list) {
255 as = list_entry(lh, struct hpsb_address_serve, hl_list);
257 if (as->host == host)
258 __delete_addr(as);
260 write_unlock_irqrestore(&addr_space_lock, flags);
262 /* Now update the config-rom to reflect anything removed by the
263 * highlevel driver. */
264 if (update_cr && host->update_config_rom) {
265 if (hpsb_update_config_rom_image(host) < 0) {
266 HPSB_ERR("Failed to generate Configuration ROM image for host "
267 "%s-%d", hl->name, host->id);
271 /* And finally, remove all the host info associated between these
272 * two. */
273 hpsb_destroy_hostinfo(hl, host);
276 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
278 struct hpsb_highlevel *hl = __data;
280 __unregister_host(hl, host, 1);
282 return 0;
285 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
287 write_lock(&hl_irqs_lock);
288 list_del(&hl->irq_list);
289 write_unlock(&hl_irqs_lock);
291 down_write(&hl_drivers_sem);
292 list_del(&hl->hl_list);
293 up_write(&hl_drivers_sem);
295 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
298 u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
299 struct hpsb_host *host,
300 struct hpsb_address_ops *ops,
301 u64 size, u64 alignment,
302 u64 start, u64 end)
304 struct hpsb_address_serve *as, *a1, *a2;
305 struct list_head *entry;
306 u64 retval = ~0ULL;
307 unsigned long flags;
308 u64 align_mask = ~(alignment - 1);
310 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
311 ((hweight32(alignment >> 32) +
312 hweight32(alignment & 0xffffffff) != 1))) {
313 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
314 __FUNCTION__, (unsigned long long)alignment);
315 return retval;
318 if (start == ~0ULL && end == ~0ULL) {
319 start = CSR1212_ALL_SPACE_BASE + 0xffff00000000ULL; /* ohci1394.c limit */
320 end = CSR1212_ALL_SPACE_END;
323 if (((start|end) & ~align_mask) || (start >= end) || (end > 0x1000000000000ULL)) {
324 HPSB_ERR("%s called with invalid addresses (start = %012Lx end = %012Lx)",
325 __FUNCTION__, (unsigned long long)start, (unsigned long long)end);
326 return retval;
329 as = (struct hpsb_address_serve *)
330 kmalloc(sizeof(struct hpsb_address_serve), GFP_KERNEL);
331 if (as == NULL) {
332 return retval;
335 INIT_LIST_HEAD(&as->host_list);
336 INIT_LIST_HEAD(&as->hl_list);
337 as->op = ops;
338 as->host = host;
340 write_lock_irqsave(&addr_space_lock, flags);
342 list_for_each(entry, &host->addr_space) {
343 u64 a1sa, a1ea;
344 u64 a2sa, a2ea;
346 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
347 a2 = list_entry(entry->next, struct hpsb_address_serve, host_list);
349 a1sa = a1->start & align_mask;
350 a1ea = (a1->end + alignment -1) & align_mask;
351 a2sa = a2->start & align_mask;
352 a2ea = (a2->end + alignment -1) & align_mask;
354 if ((a2sa - a1ea >= size) && (a2sa - start >= size) && (a2sa > start)) {
355 as->start = max(start, a1ea);
356 as->end = as->start + size;
357 list_add(&as->host_list, entry);
358 list_add_tail(&as->hl_list, &hl->addr_list);
359 retval = as->start;
360 break;
364 write_unlock_irqrestore(&addr_space_lock, flags);
366 if (retval == ~0ULL) {
367 kfree(as);
370 return retval;
373 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
374 struct hpsb_address_ops *ops, u64 start, u64 end)
376 struct hpsb_address_serve *as;
377 struct list_head *lh;
378 int retval = 0;
379 unsigned long flags;
381 if (((start|end) & 3) || (start >= end) || (end > 0x1000000000000ULL)) {
382 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
383 return 0;
386 as = (struct hpsb_address_serve *)
387 kmalloc(sizeof(struct hpsb_address_serve), GFP_ATOMIC);
388 if (as == NULL) {
389 return 0;
392 INIT_LIST_HEAD(&as->host_list);
393 INIT_LIST_HEAD(&as->hl_list);
394 as->op = ops;
395 as->start = start;
396 as->end = end;
397 as->host = host;
399 write_lock_irqsave(&addr_space_lock, flags);
401 list_for_each(lh, &host->addr_space) {
402 struct hpsb_address_serve *as_this =
403 list_entry(lh, struct hpsb_address_serve, host_list);
404 struct hpsb_address_serve *as_next =
405 list_entry(lh->next, struct hpsb_address_serve, host_list);
407 if (as_this->end > as->start)
408 break;
410 if (as_next->start >= as->end) {
411 list_add(&as->host_list, lh);
412 list_add_tail(&as->hl_list, &hl->addr_list);
413 retval = 1;
414 break;
417 write_unlock_irqrestore(&addr_space_lock, flags);
419 if (retval == 0)
420 kfree(as);
422 return retval;
425 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
426 u64 start)
428 int retval = 0;
429 struct hpsb_address_serve *as;
430 struct list_head *lh, *next;
431 unsigned long flags;
433 write_lock_irqsave(&addr_space_lock, flags);
435 list_for_each_safe (lh, next, &hl->addr_list) {
436 as = list_entry(lh, struct hpsb_address_serve, hl_list);
437 if (as->start == start && as->host == host) {
438 __delete_addr(as);
439 retval = 1;
440 break;
444 write_unlock_irqrestore(&addr_space_lock, flags);
446 return retval;
449 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
450 unsigned int channel)
452 if (channel > 63) {
453 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
454 return -EINVAL;
457 if (host->iso_listen_count[channel]++ == 0) {
458 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
461 return 0;
464 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
465 unsigned int channel)
467 if (channel > 63) {
468 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
469 return;
472 if (--host->iso_listen_count[channel] == 0) {
473 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
477 static void init_hpsb_highlevel(struct hpsb_host *host)
479 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
480 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
481 INIT_LIST_HEAD(&dummy_max_addr.host_list);
482 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
484 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
486 dummy_zero_addr.start = dummy_zero_addr.end = 0;
487 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
489 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
490 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
493 void highlevel_add_host(struct hpsb_host *host)
495 struct hpsb_highlevel *hl;
497 init_hpsb_highlevel(host);
499 down_read(&hl_drivers_sem);
500 list_for_each_entry(hl, &hl_drivers, hl_list) {
501 if (hl->add_host)
502 hl->add_host(host);
504 up_read(&hl_drivers_sem);
505 if (host->update_config_rom) {
506 if (hpsb_update_config_rom_image(host) < 0)
507 HPSB_ERR("Failed to generate Configuration ROM image for "
508 "host %s-%d", hl->name, host->id);
512 void highlevel_remove_host(struct hpsb_host *host)
514 struct hpsb_highlevel *hl;
516 down_read(&hl_drivers_sem);
517 list_for_each_entry(hl, &hl_drivers, hl_list)
518 __unregister_host(hl, host, 0);
519 up_read(&hl_drivers_sem);
522 void highlevel_host_reset(struct hpsb_host *host)
524 struct hpsb_highlevel *hl;
526 read_lock(&hl_irqs_lock);
527 list_for_each_entry(hl, &hl_irqs, irq_list) {
528 if (hl->host_reset)
529 hl->host_reset(host);
531 read_unlock(&hl_irqs_lock);
534 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
536 struct hpsb_highlevel *hl;
537 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
539 read_lock(&hl_irqs_lock);
540 list_for_each_entry(hl, &hl_irqs, irq_list) {
541 if (hl->iso_receive)
542 hl->iso_receive(host, channel, data, length);
544 read_unlock(&hl_irqs_lock);
547 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
548 void *data, size_t length)
550 struct hpsb_highlevel *hl;
551 int cts = ((quadlet_t *)data)[0] >> 4;
553 read_lock(&hl_irqs_lock);
554 list_for_each_entry(hl, &hl_irqs, irq_list) {
555 if (hl->fcp_request)
556 hl->fcp_request(host, nodeid, direction, cts, data,
557 length);
559 read_unlock(&hl_irqs_lock);
562 int highlevel_read(struct hpsb_host *host, int nodeid, void *data,
563 u64 addr, unsigned int length, u16 flags)
565 struct hpsb_address_serve *as;
566 unsigned int partlength;
567 int rcode = RCODE_ADDRESS_ERROR;
569 read_lock(&addr_space_lock);
571 list_for_each_entry(as, &host->addr_space, host_list) {
572 if (as->start > addr)
573 break;
575 if (as->end > addr) {
576 partlength = min(as->end - addr, (u64) length);
578 if (as->op->read) {
579 rcode = as->op->read(host, nodeid, data,
580 addr, partlength, flags);
581 } else {
582 rcode = RCODE_TYPE_ERROR;
585 data += partlength;
586 length -= partlength;
587 addr += partlength;
589 if ((rcode != RCODE_COMPLETE) || !length) {
590 break;
595 read_unlock(&addr_space_lock);
597 if (length && (rcode == RCODE_COMPLETE)) {
598 rcode = RCODE_ADDRESS_ERROR;
601 return rcode;
604 int highlevel_write(struct hpsb_host *host, int nodeid, int destid,
605 void *data, u64 addr, unsigned int length, u16 flags)
607 struct hpsb_address_serve *as;
608 unsigned int partlength;
609 int rcode = RCODE_ADDRESS_ERROR;
611 read_lock(&addr_space_lock);
613 list_for_each_entry(as, &host->addr_space, host_list) {
614 if (as->start > addr)
615 break;
617 if (as->end > addr) {
618 partlength = min(as->end - addr, (u64) length);
620 if (as->op->write) {
621 rcode = as->op->write(host, nodeid, destid,
622 data, addr, partlength, flags);
623 } else {
624 rcode = RCODE_TYPE_ERROR;
627 data += partlength;
628 length -= partlength;
629 addr += partlength;
631 if ((rcode != RCODE_COMPLETE) || !length) {
632 break;
637 read_unlock(&addr_space_lock);
639 if (length && (rcode == RCODE_COMPLETE)) {
640 rcode = RCODE_ADDRESS_ERROR;
643 return rcode;
647 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
648 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags)
650 struct hpsb_address_serve *as;
651 int rcode = RCODE_ADDRESS_ERROR;
653 read_lock(&addr_space_lock);
655 list_for_each_entry(as, &host->addr_space, host_list) {
656 if (as->start > addr)
657 break;
659 if (as->end > addr) {
660 if (as->op->lock) {
661 rcode = as->op->lock(host, nodeid, store, addr,
662 data, arg, ext_tcode, flags);
663 } else {
664 rcode = RCODE_TYPE_ERROR;
667 break;
671 read_unlock(&addr_space_lock);
673 return rcode;
676 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
677 u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags)
679 struct hpsb_address_serve *as;
680 int rcode = RCODE_ADDRESS_ERROR;
682 read_lock(&addr_space_lock);
684 list_for_each_entry(as, &host->addr_space, host_list) {
685 if (as->start > addr)
686 break;
688 if (as->end > addr) {
689 if (as->op->lock64) {
690 rcode = as->op->lock64(host, nodeid, store,
691 addr, data, arg,
692 ext_tcode, flags);
693 } else {
694 rcode = RCODE_TYPE_ERROR;
697 break;
701 read_unlock(&addr_space_lock);
703 return rcode;