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 = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
109 data = hi->data = hi + 1;
110 hi->size = data_size;
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);
124 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
127 struct hl_host_info *hi;
129 hi = hl_get_hostinfo(hl, host);
131 if (!hi->size && !hi->data) {
135 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo already has data",
138 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
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);
152 write_lock_irqsave(&hl->host_info_lock, flags);
154 write_unlock_irqrestore(&hl->host_info_lock, flags);
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);
174 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
176 struct hl_host_info *hi;
182 read_lock(&hl->host_info_lock);
183 list_for_each_entry(hi, &hl->host_info_list, list) {
184 if (hi->key == key) {
189 read_unlock(&hl->host_info_lock);
195 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
197 struct hpsb_highlevel *hl = __data;
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);
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);
227 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
232 static void __delete_addr(struct hpsb_address_serve *as)
234 list_del(&as->host_list);
235 list_del(&as->hl_list);
239 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host, int update_cr)
242 struct list_head *lh, *next;
243 struct hpsb_address_serve *as;
245 /* First, let the highlevel driver unreg */
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)
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
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);
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,
302 struct hpsb_address_serve *as, *a1, *a2;
303 struct list_head *entry;
304 u64 retval = CSR1212_INVALID_ADDR_SPACE;
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);
316 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
317 if (start == CSR1212_INVALID_ADDR_SPACE &&
318 end == CSR1212_INVALID_ADDR_SPACE) {
319 start = host->middle_addr_space;
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 = kmalloc(sizeof(*as), GFP_KERNEL);
333 INIT_LIST_HEAD(&as->host_list);
334 INIT_LIST_HEAD(&as->hl_list);
338 write_lock_irqsave(&addr_space_lock, flags);
340 list_for_each(entry, &host->addr_space) {
344 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
345 a2 = list_entry(entry->next, struct hpsb_address_serve, host_list);
347 a1sa = a1->start & align_mask;
348 a1ea = (a1->end + alignment -1) & align_mask;
349 a2sa = a2->start & align_mask;
350 a2ea = (a2->end + alignment -1) & align_mask;
352 if ((a2sa - a1ea >= size) && (a2sa - start >= size) && (a2sa > start)) {
353 as->start = max(start, a1ea);
354 as->end = as->start + size;
355 list_add(&as->host_list, entry);
356 list_add_tail(&as->hl_list, &hl->addr_list);
362 write_unlock_irqrestore(&addr_space_lock, flags);
364 if (retval == CSR1212_INVALID_ADDR_SPACE) {
371 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
372 struct hpsb_address_ops *ops, u64 start, u64 end)
374 struct hpsb_address_serve *as;
375 struct list_head *lh;
379 if (((start|end) & 3) || (start >= end) || (end > 0x1000000000000ULL)) {
380 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
384 as = kmalloc(sizeof(*as), GFP_ATOMIC);
388 INIT_LIST_HEAD(&as->host_list);
389 INIT_LIST_HEAD(&as->hl_list);
395 write_lock_irqsave(&addr_space_lock, flags);
397 list_for_each(lh, &host->addr_space) {
398 struct hpsb_address_serve *as_this =
399 list_entry(lh, struct hpsb_address_serve, host_list);
400 struct hpsb_address_serve *as_next =
401 list_entry(lh->next, struct hpsb_address_serve, host_list);
403 if (as_this->end > as->start)
406 if (as_next->start >= as->end) {
407 list_add(&as->host_list, lh);
408 list_add_tail(&as->hl_list, &hl->addr_list);
413 write_unlock_irqrestore(&addr_space_lock, flags);
421 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
425 struct hpsb_address_serve *as;
426 struct list_head *lh, *next;
429 write_lock_irqsave(&addr_space_lock, flags);
431 list_for_each_safe (lh, next, &hl->addr_list) {
432 as = list_entry(lh, struct hpsb_address_serve, hl_list);
433 if (as->start == start && as->host == host) {
440 write_unlock_irqrestore(&addr_space_lock, flags);
445 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
446 unsigned int channel)
449 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
453 if (host->iso_listen_count[channel]++ == 0) {
454 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
460 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
461 unsigned int channel)
464 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
468 if (--host->iso_listen_count[channel] == 0) {
469 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
473 static void init_hpsb_highlevel(struct hpsb_host *host)
475 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
476 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
477 INIT_LIST_HEAD(&dummy_max_addr.host_list);
478 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
480 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
482 dummy_zero_addr.start = dummy_zero_addr.end = 0;
483 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
485 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
486 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
489 void highlevel_add_host(struct hpsb_host *host)
491 struct hpsb_highlevel *hl;
493 init_hpsb_highlevel(host);
495 down_read(&hl_drivers_sem);
496 list_for_each_entry(hl, &hl_drivers, hl_list) {
500 up_read(&hl_drivers_sem);
501 if (host->update_config_rom) {
502 if (hpsb_update_config_rom_image(host) < 0)
503 HPSB_ERR("Failed to generate Configuration ROM image for "
504 "host %s-%d", hl->name, host->id);
508 void highlevel_remove_host(struct hpsb_host *host)
510 struct hpsb_highlevel *hl;
512 down_read(&hl_drivers_sem);
513 list_for_each_entry(hl, &hl_drivers, hl_list)
514 __unregister_host(hl, host, 0);
515 up_read(&hl_drivers_sem);
518 void highlevel_host_reset(struct hpsb_host *host)
520 struct hpsb_highlevel *hl;
522 read_lock(&hl_irqs_lock);
523 list_for_each_entry(hl, &hl_irqs, irq_list) {
525 hl->host_reset(host);
527 read_unlock(&hl_irqs_lock);
530 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
532 struct hpsb_highlevel *hl;
533 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
535 read_lock(&hl_irqs_lock);
536 list_for_each_entry(hl, &hl_irqs, irq_list) {
538 hl->iso_receive(host, channel, data, length);
540 read_unlock(&hl_irqs_lock);
543 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
544 void *data, size_t length)
546 struct hpsb_highlevel *hl;
547 int cts = ((quadlet_t *)data)[0] >> 4;
549 read_lock(&hl_irqs_lock);
550 list_for_each_entry(hl, &hl_irqs, irq_list) {
552 hl->fcp_request(host, nodeid, direction, cts, data,
555 read_unlock(&hl_irqs_lock);
558 int highlevel_read(struct hpsb_host *host, int nodeid, void *data,
559 u64 addr, unsigned int length, u16 flags)
561 struct hpsb_address_serve *as;
562 unsigned int partlength;
563 int rcode = RCODE_ADDRESS_ERROR;
565 read_lock(&addr_space_lock);
567 list_for_each_entry(as, &host->addr_space, host_list) {
568 if (as->start > addr)
571 if (as->end > addr) {
572 partlength = min(as->end - addr, (u64) length);
575 rcode = as->op->read(host, nodeid, data,
576 addr, partlength, flags);
578 rcode = RCODE_TYPE_ERROR;
582 length -= partlength;
585 if ((rcode != RCODE_COMPLETE) || !length) {
591 read_unlock(&addr_space_lock);
593 if (length && (rcode == RCODE_COMPLETE)) {
594 rcode = RCODE_ADDRESS_ERROR;
600 int highlevel_write(struct hpsb_host *host, int nodeid, int destid,
601 void *data, u64 addr, unsigned int length, u16 flags)
603 struct hpsb_address_serve *as;
604 unsigned int partlength;
605 int rcode = RCODE_ADDRESS_ERROR;
607 read_lock(&addr_space_lock);
609 list_for_each_entry(as, &host->addr_space, host_list) {
610 if (as->start > addr)
613 if (as->end > addr) {
614 partlength = min(as->end - addr, (u64) length);
617 rcode = as->op->write(host, nodeid, destid,
618 data, addr, partlength, flags);
620 rcode = RCODE_TYPE_ERROR;
624 length -= partlength;
627 if ((rcode != RCODE_COMPLETE) || !length) {
633 read_unlock(&addr_space_lock);
635 if (length && (rcode == RCODE_COMPLETE)) {
636 rcode = RCODE_ADDRESS_ERROR;
643 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
644 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags)
646 struct hpsb_address_serve *as;
647 int rcode = RCODE_ADDRESS_ERROR;
649 read_lock(&addr_space_lock);
651 list_for_each_entry(as, &host->addr_space, host_list) {
652 if (as->start > addr)
655 if (as->end > addr) {
657 rcode = as->op->lock(host, nodeid, store, addr,
658 data, arg, ext_tcode, flags);
660 rcode = RCODE_TYPE_ERROR;
667 read_unlock(&addr_space_lock);
672 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
673 u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags)
675 struct hpsb_address_serve *as;
676 int rcode = RCODE_ADDRESS_ERROR;
678 read_lock(&addr_space_lock);
680 list_for_each_entry(as, &host->addr_space, host_list) {
681 if (as->start > addr)
684 if (as->end > addr) {
685 if (as->op->lock64) {
686 rcode = as->op->lock64(host, nodeid, store,
690 rcode = RCODE_TYPE_ERROR;
697 read_unlock(&addr_space_lock);