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);
74 /* Returns a per host/driver data structure that was previously stored by
75 * hpsb_create_hostinfo. */
76 void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
78 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
80 return hi ? hi->data : NULL;
83 /* If size is zero, then the return here is only valid for error checking */
84 void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
87 struct hl_host_info *hi;
91 hi = hl_get_hostinfo(hl, host);
93 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
98 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
103 data = hi->data = hi + 1;
104 hi->size = data_size;
110 write_lock_irqsave(&hl->host_info_lock, flags);
111 list_add_tail(&hi->list, &hl->host_info_list);
112 write_unlock_irqrestore(&hl->host_info_lock, flags);
117 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
120 struct hl_host_info *hi;
122 hi = hl_get_hostinfo(hl, host);
124 if (!hi->size && !hi->data) {
128 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
129 "already has data", hl->name);
131 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
136 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
138 struct hl_host_info *hi;
140 hi = hl_get_hostinfo(hl, host);
143 write_lock_irqsave(&hl->host_info_lock, flags);
145 write_unlock_irqrestore(&hl->host_info_lock, flags);
151 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
154 struct hl_host_info *hi;
156 hi = hl_get_hostinfo(hl, host);
162 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
164 struct hl_host_info *hi;
170 read_lock(&hl->host_info_lock);
171 list_for_each_entry(hi, &hl->host_info_list, list) {
172 if (hi->key == key) {
177 read_unlock(&hl->host_info_lock);
181 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
183 struct hpsb_highlevel *hl = __data;
187 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
188 HPSB_ERR("Failed to generate Configuration ROM image for host "
189 "%s-%d", hl->name, host->id);
193 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
197 INIT_LIST_HEAD(&hl->addr_list);
198 INIT_LIST_HEAD(&hl->host_info_list);
200 rwlock_init(&hl->host_info_lock);
202 down_write(&hl_drivers_sem);
203 list_add_tail(&hl->hl_list, &hl_drivers);
204 up_write(&hl_drivers_sem);
206 write_lock_irqsave(&hl_irqs_lock, flags);
207 list_add_tail(&hl->irq_list, &hl_irqs);
208 write_unlock_irqrestore(&hl_irqs_lock, flags);
211 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
215 static void __delete_addr(struct hpsb_address_serve *as)
217 list_del(&as->host_list);
218 list_del(&as->hl_list);
222 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
226 struct list_head *lh, *next;
227 struct hpsb_address_serve *as;
229 /* First, let the highlevel driver unreg */
231 hl->remove_host(host);
233 /* Remove any addresses that are matched for this highlevel driver
234 * and this particular host. */
235 write_lock_irqsave(&addr_space_lock, flags);
236 list_for_each_safe (lh, next, &hl->addr_list) {
237 as = list_entry(lh, struct hpsb_address_serve, hl_list);
238 if (as->host == host)
241 write_unlock_irqrestore(&addr_space_lock, flags);
243 /* Now update the config-rom to reflect anything removed by the
244 * highlevel driver. */
245 if (update_cr && host->update_config_rom &&
246 hpsb_update_config_rom_image(host) < 0)
247 HPSB_ERR("Failed to generate Configuration ROM image for host "
248 "%s-%d", hl->name, host->id);
250 /* Finally remove all the host info associated between these two. */
251 hpsb_destroy_hostinfo(hl, host);
254 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
256 struct hpsb_highlevel *hl = __data;
258 __unregister_host(hl, host, 1);
262 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
266 write_lock_irqsave(&hl_irqs_lock, flags);
267 list_del(&hl->irq_list);
268 write_unlock_irqrestore(&hl_irqs_lock, flags);
270 down_write(&hl_drivers_sem);
271 list_del(&hl->hl_list);
272 up_write(&hl_drivers_sem);
274 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
277 u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
278 struct hpsb_host *host,
279 struct hpsb_address_ops *ops,
280 u64 size, u64 alignment,
283 struct hpsb_address_serve *as, *a1, *a2;
284 struct list_head *entry;
285 u64 retval = CSR1212_INVALID_ADDR_SPACE;
287 u64 align_mask = ~(alignment - 1);
289 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
290 (hweight64(alignment) != 1)) {
291 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
292 __FUNCTION__, (unsigned long long)alignment);
297 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
298 if (start == CSR1212_INVALID_ADDR_SPACE &&
299 end == CSR1212_INVALID_ADDR_SPACE) {
300 start = host->middle_addr_space;
301 end = CSR1212_ALL_SPACE_END;
304 if (((start|end) & ~align_mask) || (start >= end) ||
305 (end > CSR1212_ALL_SPACE_END)) {
306 HPSB_ERR("%s called with invalid addresses "
307 "(start = %012Lx end = %012Lx)", __FUNCTION__,
308 (unsigned long long)start,(unsigned long long)end);
312 as = kmalloc(sizeof(*as), GFP_KERNEL);
316 INIT_LIST_HEAD(&as->host_list);
317 INIT_LIST_HEAD(&as->hl_list);
321 write_lock_irqsave(&addr_space_lock, flags);
322 list_for_each(entry, &host->addr_space) {
326 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
327 a2 = list_entry(entry->next, struct hpsb_address_serve,
330 a1sa = a1->start & align_mask;
331 a1ea = (a1->end + alignment -1) & align_mask;
332 a2sa = a2->start & align_mask;
333 a2ea = (a2->end + alignment -1) & align_mask;
335 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
337 as->start = max(start, a1ea);
338 as->end = as->start + size;
339 list_add(&as->host_list, entry);
340 list_add_tail(&as->hl_list, &hl->addr_list);
345 write_unlock_irqrestore(&addr_space_lock, flags);
347 if (retval == CSR1212_INVALID_ADDR_SPACE)
352 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
353 struct hpsb_address_ops *ops, u64 start, u64 end)
355 struct hpsb_address_serve *as;
356 struct list_head *lh;
360 if (((start|end) & 3) || (start >= end) ||
361 (end > CSR1212_ALL_SPACE_END)) {
362 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
366 as = kmalloc(sizeof(*as), GFP_ATOMIC);
370 INIT_LIST_HEAD(&as->host_list);
371 INIT_LIST_HEAD(&as->hl_list);
377 write_lock_irqsave(&addr_space_lock, flags);
378 list_for_each(lh, &host->addr_space) {
379 struct hpsb_address_serve *as_this =
380 list_entry(lh, struct hpsb_address_serve, host_list);
381 struct hpsb_address_serve *as_next =
382 list_entry(lh->next, struct hpsb_address_serve,
385 if (as_this->end > as->start)
388 if (as_next->start >= as->end) {
389 list_add(&as->host_list, lh);
390 list_add_tail(&as->hl_list, &hl->addr_list);
395 write_unlock_irqrestore(&addr_space_lock, flags);
402 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
406 struct hpsb_address_serve *as;
407 struct list_head *lh, *next;
410 write_lock_irqsave(&addr_space_lock, flags);
411 list_for_each_safe (lh, next, &hl->addr_list) {
412 as = list_entry(lh, struct hpsb_address_serve, hl_list);
413 if (as->start == start && as->host == host) {
419 write_unlock_irqrestore(&addr_space_lock, flags);
423 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
424 unsigned int channel)
427 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
430 if (host->iso_listen_count[channel]++ == 0)
431 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
435 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
436 unsigned int channel)
439 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
442 if (--host->iso_listen_count[channel] == 0)
443 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
446 static void init_hpsb_highlevel(struct hpsb_host *host)
448 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
449 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
450 INIT_LIST_HEAD(&dummy_max_addr.host_list);
451 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
453 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
455 dummy_zero_addr.start = dummy_zero_addr.end = 0;
456 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
458 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
459 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
462 void highlevel_add_host(struct hpsb_host *host)
464 struct hpsb_highlevel *hl;
466 init_hpsb_highlevel(host);
468 down_read(&hl_drivers_sem);
469 list_for_each_entry(hl, &hl_drivers, hl_list) {
473 up_read(&hl_drivers_sem);
474 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
475 HPSB_ERR("Failed to generate Configuration ROM image for host "
476 "%s-%d", hl->name, host->id);
479 void highlevel_remove_host(struct hpsb_host *host)
481 struct hpsb_highlevel *hl;
483 down_read(&hl_drivers_sem);
484 list_for_each_entry(hl, &hl_drivers, hl_list)
485 __unregister_host(hl, host, 0);
486 up_read(&hl_drivers_sem);
489 void highlevel_host_reset(struct hpsb_host *host)
492 struct hpsb_highlevel *hl;
494 read_lock_irqsave(&hl_irqs_lock, flags);
495 list_for_each_entry(hl, &hl_irqs, irq_list) {
497 hl->host_reset(host);
499 read_unlock_irqrestore(&hl_irqs_lock, flags);
502 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
505 struct hpsb_highlevel *hl;
506 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
508 read_lock_irqsave(&hl_irqs_lock, flags);
509 list_for_each_entry(hl, &hl_irqs, irq_list) {
511 hl->iso_receive(host, channel, data, length);
513 read_unlock_irqrestore(&hl_irqs_lock, flags);
516 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
517 void *data, size_t length)
520 struct hpsb_highlevel *hl;
521 int cts = ((quadlet_t *)data)[0] >> 4;
523 read_lock_irqsave(&hl_irqs_lock, flags);
524 list_for_each_entry(hl, &hl_irqs, irq_list) {
526 hl->fcp_request(host, nodeid, direction, cts, data,
529 read_unlock_irqrestore(&hl_irqs_lock, flags);
532 int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
533 unsigned int length, u16 flags)
535 struct hpsb_address_serve *as;
536 unsigned int partlength;
537 int rcode = RCODE_ADDRESS_ERROR;
539 read_lock(&addr_space_lock);
540 list_for_each_entry(as, &host->addr_space, host_list) {
541 if (as->start > addr)
544 if (as->end > addr) {
545 partlength = min(as->end - addr, (u64) length);
548 rcode = as->op->read(host, nodeid, data,
549 addr, partlength, flags);
551 rcode = RCODE_TYPE_ERROR;
554 length -= partlength;
557 if ((rcode != RCODE_COMPLETE) || !length)
561 read_unlock(&addr_space_lock);
563 if (length && (rcode == RCODE_COMPLETE))
564 rcode = RCODE_ADDRESS_ERROR;
568 int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
569 u64 addr, unsigned int length, u16 flags)
571 struct hpsb_address_serve *as;
572 unsigned int partlength;
573 int rcode = RCODE_ADDRESS_ERROR;
575 read_lock(&addr_space_lock);
576 list_for_each_entry(as, &host->addr_space, host_list) {
577 if (as->start > addr)
580 if (as->end > addr) {
581 partlength = min(as->end - addr, (u64) length);
584 rcode = as->op->write(host, nodeid, destid,
585 data, addr, partlength,
588 rcode = RCODE_TYPE_ERROR;
591 length -= partlength;
594 if ((rcode != RCODE_COMPLETE) || !length)
598 read_unlock(&addr_space_lock);
600 if (length && (rcode == RCODE_COMPLETE))
601 rcode = RCODE_ADDRESS_ERROR;
605 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
606 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
609 struct hpsb_address_serve *as;
610 int rcode = RCODE_ADDRESS_ERROR;
612 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) {
619 rcode = as->op->lock(host, nodeid, store, addr,
620 data, arg, ext_tcode,
623 rcode = RCODE_TYPE_ERROR;
627 read_unlock(&addr_space_lock);
631 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
632 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
635 struct hpsb_address_serve *as;
636 int rcode = RCODE_ADDRESS_ERROR;
638 read_lock(&addr_space_lock);
640 list_for_each_entry(as, &host->addr_space, host_list) {
641 if (as->start > addr)
644 if (as->end > addr) {
646 rcode = as->op->lock64(host, nodeid, store,
650 rcode = RCODE_TYPE_ERROR;
654 read_unlock(&addr_space_lock);