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.
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>
26 #include "ieee1394_types.h"
28 #include "ieee1394_core.h"
29 #include "highlevel.h"
34 struct list_head list;
35 struct hpsb_host *host;
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;
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);
70 read_unlock(&hl->host_info_lock);
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);
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,
93 struct hl_host_info *hi;
97 hi = hl_get_hostinfo(hl, host);
99 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already exists",
104 hi = kmalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
108 memset(hi, 0, sizeof(*hi) + data_size);
111 data = hi->data = hi + 1;
112 hi->size = data_size;
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);
126 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
129 struct hl_host_info *hi;
131 hi = hl_get_hostinfo(hl, host);
133 if (!hi->size && !hi->data) {
137 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo already has data",
140 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
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);
154 write_lock_irqsave(&hl->host_info_lock, flags);
156 write_unlock_irqrestore(&hl->host_info_lock, flags);
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);
176 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
178 struct hl_host_info *hi;
184 read_lock(&hl->host_info_lock);
185 list_for_each_entry(hi, &hl->host_info_list, list) {
186 if (hi->key == key) {
191 read_unlock(&hl->host_info_lock);
197 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
199 struct hpsb_highlevel *hl = __data;
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);
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);
229 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
234 static void __delete_addr(struct hpsb_address_serve *as)
236 list_del(&as->host_list);
237 list_del(&as->hl_list);
241 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host, int update_cr)
244 struct list_head *lh, *next;
245 struct hpsb_address_serve *as;
247 /* First, let the highlevel driver unreg */
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)
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
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);
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,
304 struct hpsb_address_serve *as, *a1, *a2;
305 struct list_head *entry;
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);
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);
329 as = (struct hpsb_address_serve *)
330 kmalloc(sizeof(struct hpsb_address_serve), GFP_KERNEL);
335 INIT_LIST_HEAD(&as->host_list);
336 INIT_LIST_HEAD(&as->hl_list);
340 write_lock_irqsave(&addr_space_lock, flags);
342 list_for_each(entry, &host->addr_space) {
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);
364 write_unlock_irqrestore(&addr_space_lock, flags);
366 if (retval == ~0ULL) {
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;
381 if (((start|end) & 3) || (start >= end) || (end > 0x1000000000000ULL)) {
382 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
386 as = (struct hpsb_address_serve *)
387 kmalloc(sizeof(struct hpsb_address_serve), GFP_ATOMIC);
392 INIT_LIST_HEAD(&as->host_list);
393 INIT_LIST_HEAD(&as->hl_list);
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)
410 if (as_next->start >= as->end) {
411 list_add(&as->host_list, lh);
412 list_add_tail(&as->hl_list, &hl->addr_list);
417 write_unlock_irqrestore(&addr_space_lock, flags);
425 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
429 struct hpsb_address_serve *as;
430 struct list_head *lh, *next;
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) {
444 write_unlock_irqrestore(&addr_space_lock, flags);
449 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
450 unsigned int channel)
453 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
457 if (host->iso_listen_count[channel]++ == 0) {
458 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
464 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
465 unsigned int channel)
468 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
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) {
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) {
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) {
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) {
556 hl->fcp_request(host, nodeid, direction, cts, data,
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)
575 if (as->end > addr) {
576 partlength = min(as->end - addr, (u64) length);
579 rcode = as->op->read(host, nodeid, data,
580 addr, partlength, flags);
582 rcode = RCODE_TYPE_ERROR;
586 length -= partlength;
589 if ((rcode != RCODE_COMPLETE) || !length) {
595 read_unlock(&addr_space_lock);
597 if (length && (rcode == RCODE_COMPLETE)) {
598 rcode = RCODE_ADDRESS_ERROR;
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)
617 if (as->end > addr) {
618 partlength = min(as->end - addr, (u64) length);
621 rcode = as->op->write(host, nodeid, destid,
622 data, addr, partlength, flags);
624 rcode = RCODE_TYPE_ERROR;
628 length -= partlength;
631 if ((rcode != RCODE_COMPLETE) || !length) {
637 read_unlock(&addr_space_lock);
639 if (length && (rcode == RCODE_COMPLETE)) {
640 rcode = RCODE_ADDRESS_ERROR;
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)
659 if (as->end > addr) {
661 rcode = as->op->lock(host, nodeid, store, addr,
662 data, arg, ext_tcode, flags);
664 rcode = RCODE_TYPE_ERROR;
671 read_unlock(&addr_space_lock);
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)
688 if (as->end > addr) {
689 if (as->op->lock64) {
690 rcode = as->op->lock64(host, nodeid, store,
694 rcode = RCODE_TYPE_ERROR;
701 read_unlock(&addr_space_lock);