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/slab.h>
21 #include <linux/list.h>
22 #include <linux/bitops.h>
25 #include "ieee1394_types.h"
27 #include "ieee1394_core.h"
28 #include "highlevel.h"
33 struct list_head list;
34 struct hpsb_host *host;
41 static LIST_HEAD(hl_drivers);
42 static DECLARE_RWSEM(hl_drivers_sem);
44 static LIST_HEAD(hl_irqs);
45 static DEFINE_RWLOCK(hl_irqs_lock);
47 static DEFINE_RWLOCK(addr_space_lock);
49 /* addr_space list will have zero and max already included as bounds */
50 static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
51 static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
54 static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
55 struct hpsb_host *host)
57 struct hl_host_info *hi = NULL;
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);
69 read_unlock(&hl->host_info_lock);
73 /* Returns a per host/driver data structure that was previously stored by
74 * hpsb_create_hostinfo. */
75 void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
77 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
79 return hi ? hi->data : NULL;
82 /* If size is zero, then the return here is only valid for error checking */
83 void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
86 struct hl_host_info *hi;
90 hi = hl_get_hostinfo(hl, host);
92 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
97 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
102 data = hi->data = hi + 1;
103 hi->size = data_size;
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);
116 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
119 struct hl_host_info *hi;
121 hi = hl_get_hostinfo(hl, host);
123 if (!hi->size && !hi->data) {
127 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
128 "already has data", hl->name);
130 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
135 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
137 struct hl_host_info *hi;
139 hi = hl_get_hostinfo(hl, host);
142 write_lock_irqsave(&hl->host_info_lock, flags);
144 write_unlock_irqrestore(&hl->host_info_lock, flags);
150 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
153 struct hl_host_info *hi;
155 hi = hl_get_hostinfo(hl, host);
161 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
163 struct hl_host_info *hi;
169 read_lock(&hl->host_info_lock);
170 list_for_each_entry(hi, &hl->host_info_list, list) {
171 if (hi->key == key) {
176 read_unlock(&hl->host_info_lock);
180 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
182 struct hpsb_highlevel *hl = __data;
186 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);
192 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
196 INIT_LIST_HEAD(&hl->addr_list);
197 INIT_LIST_HEAD(&hl->host_info_list);
199 rwlock_init(&hl->host_info_lock);
201 down_write(&hl_drivers_sem);
202 list_add_tail(&hl->hl_list, &hl_drivers);
203 up_write(&hl_drivers_sem);
205 write_lock_irqsave(&hl_irqs_lock, flags);
206 list_add_tail(&hl->irq_list, &hl_irqs);
207 write_unlock_irqrestore(&hl_irqs_lock, flags);
210 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
214 static void __delete_addr(struct hpsb_address_serve *as)
216 list_del(&as->host_list);
217 list_del(&as->hl_list);
221 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
225 struct list_head *lh, *next;
226 struct hpsb_address_serve *as;
228 /* First, let the highlevel driver unreg */
230 hl->remove_host(host);
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);
237 if (as->host == host)
240 write_unlock_irqrestore(&addr_space_lock, flags);
242 /* Now update the config-rom to reflect anything removed by the
243 * highlevel driver. */
244 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);
249 /* Finally remove all the host info associated between these two. */
250 hpsb_destroy_hostinfo(hl, host);
253 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
255 struct hpsb_highlevel *hl = __data;
257 __unregister_host(hl, host, 1);
261 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
265 write_lock_irqsave(&hl_irqs_lock, flags);
266 list_del(&hl->irq_list);
267 write_unlock_irqrestore(&hl_irqs_lock, flags);
269 down_write(&hl_drivers_sem);
270 list_del(&hl->hl_list);
271 up_write(&hl_drivers_sem);
273 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
276 u64 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,
282 struct hpsb_address_serve *as, *a1, *a2;
283 struct list_head *entry;
284 u64 retval = CSR1212_INVALID_ADDR_SPACE;
286 u64 align_mask = ~(alignment - 1);
288 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
289 (hweight64(alignment) != 1)) {
290 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
291 __FUNCTION__, (unsigned long long)alignment);
296 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
297 if (start == CSR1212_INVALID_ADDR_SPACE &&
298 end == CSR1212_INVALID_ADDR_SPACE) {
299 start = host->middle_addr_space;
300 end = CSR1212_ALL_SPACE_END;
303 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);
311 as = kmalloc(sizeof(*as), GFP_KERNEL);
315 INIT_LIST_HEAD(&as->host_list);
316 INIT_LIST_HEAD(&as->hl_list);
320 write_lock_irqsave(&addr_space_lock, flags);
321 list_for_each(entry, &host->addr_space) {
325 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
326 a2 = list_entry(entry->next, struct hpsb_address_serve,
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;
334 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
336 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);
344 write_unlock_irqrestore(&addr_space_lock, flags);
346 if (retval == CSR1212_INVALID_ADDR_SPACE)
351 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
352 struct hpsb_address_ops *ops, u64 start, u64 end)
354 struct hpsb_address_serve *as;
355 struct list_head *lh;
359 if (((start|end) & 3) || (start >= end) ||
360 (end > CSR1212_ALL_SPACE_END)) {
361 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
365 as = kmalloc(sizeof(*as), GFP_ATOMIC);
369 INIT_LIST_HEAD(&as->host_list);
370 INIT_LIST_HEAD(&as->hl_list);
376 write_lock_irqsave(&addr_space_lock, flags);
377 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 =
381 list_entry(lh->next, struct hpsb_address_serve,
384 if (as_this->end > as->start)
387 if (as_next->start >= as->end) {
388 list_add(&as->host_list, lh);
389 list_add_tail(&as->hl_list, &hl->addr_list);
394 write_unlock_irqrestore(&addr_space_lock, flags);
401 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
405 struct hpsb_address_serve *as;
406 struct list_head *lh, *next;
409 write_lock_irqsave(&addr_space_lock, flags);
410 list_for_each_safe (lh, next, &hl->addr_list) {
411 as = list_entry(lh, struct hpsb_address_serve, hl_list);
412 if (as->start == start && as->host == host) {
418 write_unlock_irqrestore(&addr_space_lock, flags);
422 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
423 unsigned int channel)
426 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
429 if (host->iso_listen_count[channel]++ == 0)
430 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
434 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
435 unsigned int channel)
438 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
441 if (--host->iso_listen_count[channel] == 0)
442 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
445 static void init_hpsb_highlevel(struct hpsb_host *host)
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);
452 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
454 dummy_zero_addr.start = dummy_zero_addr.end = 0;
455 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
457 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
458 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
461 void highlevel_add_host(struct hpsb_host *host)
463 struct hpsb_highlevel *hl;
465 init_hpsb_highlevel(host);
467 down_read(&hl_drivers_sem);
468 list_for_each_entry(hl, &hl_drivers, hl_list) {
472 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);
478 void highlevel_remove_host(struct hpsb_host *host)
480 struct hpsb_highlevel *hl;
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);
488 void highlevel_host_reset(struct hpsb_host *host)
491 struct hpsb_highlevel *hl;
493 read_lock_irqsave(&hl_irqs_lock, flags);
494 list_for_each_entry(hl, &hl_irqs, irq_list) {
496 hl->host_reset(host);
498 read_unlock_irqrestore(&hl_irqs_lock, flags);
501 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
504 struct hpsb_highlevel *hl;
505 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
507 read_lock_irqsave(&hl_irqs_lock, flags);
508 list_for_each_entry(hl, &hl_irqs, irq_list) {
510 hl->iso_receive(host, channel, data, length);
512 read_unlock_irqrestore(&hl_irqs_lock, flags);
515 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
516 void *data, size_t length)
519 struct hpsb_highlevel *hl;
520 int cts = ((quadlet_t *)data)[0] >> 4;
522 read_lock_irqsave(&hl_irqs_lock, flags);
523 list_for_each_entry(hl, &hl_irqs, irq_list) {
525 hl->fcp_request(host, nodeid, direction, cts, data,
528 read_unlock_irqrestore(&hl_irqs_lock, flags);
531 int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
532 unsigned int length, u16 flags)
534 struct hpsb_address_serve *as;
535 unsigned int partlength;
536 int rcode = RCODE_ADDRESS_ERROR;
538 read_lock(&addr_space_lock);
539 list_for_each_entry(as, &host->addr_space, host_list) {
540 if (as->start > addr)
543 if (as->end > addr) {
544 partlength = min(as->end - addr, (u64) length);
547 rcode = as->op->read(host, nodeid, data,
548 addr, partlength, flags);
550 rcode = RCODE_TYPE_ERROR;
553 length -= partlength;
556 if ((rcode != RCODE_COMPLETE) || !length)
560 read_unlock(&addr_space_lock);
562 if (length && (rcode == RCODE_COMPLETE))
563 rcode = RCODE_ADDRESS_ERROR;
567 int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
568 u64 addr, unsigned int length, u16 flags)
570 struct hpsb_address_serve *as;
571 unsigned int partlength;
572 int rcode = RCODE_ADDRESS_ERROR;
574 read_lock(&addr_space_lock);
575 list_for_each_entry(as, &host->addr_space, host_list) {
576 if (as->start > addr)
579 if (as->end > addr) {
580 partlength = min(as->end - addr, (u64) length);
583 rcode = as->op->write(host, nodeid, destid,
584 data, addr, partlength,
587 rcode = RCODE_TYPE_ERROR;
590 length -= partlength;
593 if ((rcode != RCODE_COMPLETE) || !length)
597 read_unlock(&addr_space_lock);
599 if (length && (rcode == RCODE_COMPLETE))
600 rcode = RCODE_ADDRESS_ERROR;
604 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
605 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
608 struct hpsb_address_serve *as;
609 int rcode = RCODE_ADDRESS_ERROR;
611 read_lock(&addr_space_lock);
612 list_for_each_entry(as, &host->addr_space, host_list) {
613 if (as->start > addr)
616 if (as->end > addr) {
618 rcode = as->op->lock(host, nodeid, store, addr,
619 data, arg, ext_tcode,
622 rcode = RCODE_TYPE_ERROR;
626 read_unlock(&addr_space_lock);
630 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
631 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
634 struct hpsb_address_serve *as;
635 int rcode = RCODE_ADDRESS_ERROR;
637 read_lock(&addr_space_lock);
639 list_for_each_entry(as, &host->addr_space, host_list) {
640 if (as->start > addr)
643 if (as->end > addr) {
645 rcode = as->op->lock64(host, nodeid, store,
649 rcode = RCODE_TYPE_ERROR;
653 read_unlock(&addr_space_lock);