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);
50 static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
51 struct hpsb_host *host)
53 struct hl_host_info *hi = NULL;
58 read_lock(&hl->host_info_lock);
59 list_for_each_entry(hi, &hl->host_info_list, list) {
60 if (hi->host == host) {
61 read_unlock(&hl->host_info_lock);
65 read_unlock(&hl->host_info_lock);
70 * hpsb_get_hostinfo - retrieve a hostinfo pointer bound to this driver/host
72 * Returns a per @host and @hl driver data structure that was previously stored
73 * by 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;
83 * hpsb_create_hostinfo - allocate a hostinfo pointer bound to this driver/host
85 * Allocate a hostinfo pointer backed by memory with @data_size and bind it to
86 * to this @hl driver and @host. If @data_size is zero, then the return here is
87 * only valid for error checking.
89 void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
92 struct hl_host_info *hi;
96 hi = hl_get_hostinfo(hl, host);
98 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
103 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
108 data = hi->data = hi + 1;
109 hi->size = data_size;
115 write_lock_irqsave(&hl->host_info_lock, flags);
116 list_add_tail(&hi->list, &hl->host_info_list);
117 write_unlock_irqrestore(&hl->host_info_lock, flags);
123 * hpsb_set_hostinfo - set the hostinfo pointer to something useful
125 * Usually follows a call to hpsb_create_hostinfo, where the size is 0.
127 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
130 struct hl_host_info *hi;
132 hi = hl_get_hostinfo(hl, host);
134 if (!hi->size && !hi->data) {
138 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
139 "already has data", hl->name);
141 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
147 * hpsb_destroy_hostinfo - free and remove a hostinfo pointer
149 * Free and remove the hostinfo pointer bound to this @hl driver and @host.
151 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
153 struct hl_host_info *hi;
155 hi = hl_get_hostinfo(hl, host);
158 write_lock_irqsave(&hl->host_info_lock, flags);
160 write_unlock_irqrestore(&hl->host_info_lock, flags);
167 * hpsb_set_hostinfo_key - set an alternate lookup key for an hostinfo
169 * Sets an alternate lookup key for the hostinfo bound to this @hl driver and
172 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
175 struct hl_host_info *hi;
177 hi = hl_get_hostinfo(hl, host);
184 * hpsb_get_hostinfo_bykey - retrieve a hostinfo pointer by its alternate key
186 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
188 struct hl_host_info *hi;
194 read_lock(&hl->host_info_lock);
195 list_for_each_entry(hi, &hl->host_info_list, list) {
196 if (hi->key == key) {
201 read_unlock(&hl->host_info_lock);
205 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
207 struct hpsb_highlevel *hl = __data;
211 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
212 HPSB_ERR("Failed to generate Configuration ROM image for host "
213 "%s-%d", hl->name, host->id);
218 * hpsb_register_highlevel - register highlevel driver
220 * The name pointer in @hl has to stay valid at all times because the string is
223 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
227 hpsb_init_highlevel(hl);
228 INIT_LIST_HEAD(&hl->addr_list);
230 down_write(&hl_drivers_sem);
231 list_add_tail(&hl->hl_list, &hl_drivers);
232 up_write(&hl_drivers_sem);
234 write_lock_irqsave(&hl_irqs_lock, flags);
235 list_add_tail(&hl->irq_list, &hl_irqs);
236 write_unlock_irqrestore(&hl_irqs_lock, flags);
239 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
243 static void __delete_addr(struct hpsb_address_serve *as)
245 list_del(&as->host_list);
246 list_del(&as->hl_list);
250 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
254 struct list_head *lh, *next;
255 struct hpsb_address_serve *as;
257 /* First, let the highlevel driver unreg */
259 hl->remove_host(host);
261 /* Remove any addresses that are matched for this highlevel driver
262 * and this particular host. */
263 write_lock_irqsave(&addr_space_lock, flags);
264 list_for_each_safe (lh, next, &hl->addr_list) {
265 as = list_entry(lh, struct hpsb_address_serve, hl_list);
266 if (as->host == host)
269 write_unlock_irqrestore(&addr_space_lock, flags);
271 /* Now update the config-rom to reflect anything removed by the
272 * highlevel driver. */
273 if (update_cr && host->update_config_rom &&
274 hpsb_update_config_rom_image(host) < 0)
275 HPSB_ERR("Failed to generate Configuration ROM image for host "
276 "%s-%d", hl->name, host->id);
278 /* Finally remove all the host info associated between these two. */
279 hpsb_destroy_hostinfo(hl, host);
282 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
284 struct hpsb_highlevel *hl = __data;
286 __unregister_host(hl, host, 1);
291 * hpsb_unregister_highlevel - unregister highlevel driver
293 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
297 write_lock_irqsave(&hl_irqs_lock, flags);
298 list_del(&hl->irq_list);
299 write_unlock_irqrestore(&hl_irqs_lock, flags);
301 down_write(&hl_drivers_sem);
302 list_del(&hl->hl_list);
303 up_write(&hl_drivers_sem);
305 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
309 * hpsb_allocate_and_register_addrspace - alloc' and reg' a host address space
311 * @start and @end are 48 bit pointers and have to be quadlet aligned.
312 * @end points to the first address behind the handled addresses. This
313 * function can be called multiple times for a single hpsb_highlevel @hl to
314 * implement sparse register sets. The requested region must not overlap any
315 * previously allocated region, otherwise registering will fail.
317 * It returns true for successful allocation. Address spaces can be
318 * unregistered with hpsb_unregister_addrspace. All remaining address spaces
319 * are automatically deallocated together with the hpsb_highlevel @hl.
321 u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
322 struct hpsb_host *host,
323 const struct hpsb_address_ops *ops,
324 u64 size, u64 alignment,
327 struct hpsb_address_serve *as, *a1, *a2;
328 struct list_head *entry;
329 u64 retval = CSR1212_INVALID_ADDR_SPACE;
331 u64 align_mask = ~(alignment - 1);
333 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
334 (hweight64(alignment) != 1)) {
335 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
336 __func__, (unsigned long long)alignment);
341 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
342 if (start == CSR1212_INVALID_ADDR_SPACE &&
343 end == CSR1212_INVALID_ADDR_SPACE) {
344 start = host->middle_addr_space;
345 end = CSR1212_ALL_SPACE_END;
348 if (((start|end) & ~align_mask) || (start >= end) ||
349 (end > CSR1212_ALL_SPACE_END)) {
350 HPSB_ERR("%s called with invalid addresses "
351 "(start = %012Lx end = %012Lx)", __func__,
352 (unsigned long long)start,(unsigned long long)end);
356 as = kmalloc(sizeof(*as), GFP_KERNEL);
360 INIT_LIST_HEAD(&as->host_list);
361 INIT_LIST_HEAD(&as->hl_list);
365 write_lock_irqsave(&addr_space_lock, flags);
366 list_for_each(entry, &host->addr_space) {
370 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
371 a2 = list_entry(entry->next, struct hpsb_address_serve,
374 a1sa = a1->start & align_mask;
375 a1ea = (a1->end + alignment -1) & align_mask;
376 a2sa = a2->start & align_mask;
377 a2ea = (a2->end + alignment -1) & align_mask;
379 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
381 as->start = max(start, a1ea);
382 as->end = as->start + size;
383 list_add(&as->host_list, entry);
384 list_add_tail(&as->hl_list, &hl->addr_list);
389 write_unlock_irqrestore(&addr_space_lock, flags);
391 if (retval == CSR1212_INVALID_ADDR_SPACE)
397 * hpsb_register_addrspace - register a host address space
399 * @start and @end are 48 bit pointers and have to be quadlet aligned.
400 * @end points to the first address behind the handled addresses. This
401 * function can be called multiple times for a single hpsb_highlevel @hl to
402 * implement sparse register sets. The requested region must not overlap any
403 * previously allocated region, otherwise registering will fail.
405 * It returns true for successful allocation. Address spaces can be
406 * unregistered with hpsb_unregister_addrspace. All remaining address spaces
407 * are automatically deallocated together with the hpsb_highlevel @hl.
409 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
410 const struct hpsb_address_ops *ops,
413 struct hpsb_address_serve *as;
414 struct list_head *lh;
418 if (((start|end) & 3) || (start >= end) ||
419 (end > CSR1212_ALL_SPACE_END)) {
420 HPSB_ERR("%s called with invalid addresses", __func__);
424 as = kmalloc(sizeof(*as), GFP_KERNEL);
428 INIT_LIST_HEAD(&as->host_list);
429 INIT_LIST_HEAD(&as->hl_list);
435 write_lock_irqsave(&addr_space_lock, flags);
436 list_for_each(lh, &host->addr_space) {
437 struct hpsb_address_serve *as_this =
438 list_entry(lh, struct hpsb_address_serve, host_list);
439 struct hpsb_address_serve *as_next =
440 list_entry(lh->next, struct hpsb_address_serve,
443 if (as_this->end > as->start)
446 if (as_next->start >= as->end) {
447 list_add(&as->host_list, lh);
448 list_add_tail(&as->hl_list, &hl->addr_list);
453 write_unlock_irqrestore(&addr_space_lock, flags);
460 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
464 struct hpsb_address_serve *as;
465 struct list_head *lh, *next;
468 write_lock_irqsave(&addr_space_lock, flags);
469 list_for_each_safe (lh, next, &hl->addr_list) {
470 as = list_entry(lh, struct hpsb_address_serve, hl_list);
471 if (as->start == start && as->host == host) {
477 write_unlock_irqrestore(&addr_space_lock, flags);
481 const static struct hpsb_address_ops dummy_ops;
483 /* dummy address spaces as lower and upper bounds of the host's a.s. list */
484 static void init_hpsb_highlevel(struct hpsb_host *host)
486 INIT_LIST_HEAD(&host->dummy_zero_addr.host_list);
487 INIT_LIST_HEAD(&host->dummy_zero_addr.hl_list);
488 INIT_LIST_HEAD(&host->dummy_max_addr.host_list);
489 INIT_LIST_HEAD(&host->dummy_max_addr.hl_list);
491 host->dummy_zero_addr.op = host->dummy_max_addr.op = &dummy_ops;
493 host->dummy_zero_addr.start = host->dummy_zero_addr.end = 0;
494 host->dummy_max_addr.start = host->dummy_max_addr.end = ((u64) 1) << 48;
496 list_add_tail(&host->dummy_zero_addr.host_list, &host->addr_space);
497 list_add_tail(&host->dummy_max_addr.host_list, &host->addr_space);
500 void highlevel_add_host(struct hpsb_host *host)
502 struct hpsb_highlevel *hl;
504 init_hpsb_highlevel(host);
506 down_read(&hl_drivers_sem);
507 list_for_each_entry(hl, &hl_drivers, hl_list) {
511 up_read(&hl_drivers_sem);
512 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
513 HPSB_ERR("Failed to generate Configuration ROM image for host "
514 "%s-%d", hl->name, host->id);
517 void highlevel_remove_host(struct hpsb_host *host)
519 struct hpsb_highlevel *hl;
521 down_read(&hl_drivers_sem);
522 list_for_each_entry(hl, &hl_drivers, hl_list)
523 __unregister_host(hl, host, 0);
524 up_read(&hl_drivers_sem);
527 void highlevel_host_reset(struct hpsb_host *host)
530 struct hpsb_highlevel *hl;
532 read_lock_irqsave(&hl_irqs_lock, flags);
533 list_for_each_entry(hl, &hl_irqs, irq_list) {
535 hl->host_reset(host);
537 read_unlock_irqrestore(&hl_irqs_lock, flags);
540 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
541 void *data, size_t length)
544 struct hpsb_highlevel *hl;
545 int cts = ((quadlet_t *)data)[0] >> 4;
547 read_lock_irqsave(&hl_irqs_lock, flags);
548 list_for_each_entry(hl, &hl_irqs, irq_list) {
550 hl->fcp_request(host, nodeid, direction, cts, data,
553 read_unlock_irqrestore(&hl_irqs_lock, flags);
557 * highlevel_read, highlevel_write, highlevel_lock, highlevel_lock64:
559 * These functions are called to handle transactions. They are called when a
560 * packet arrives. The flags argument contains the second word of the first
561 * header quadlet of the incoming packet (containing transaction label, retry
562 * code, transaction code and priority). These functions either return a
563 * response code or a negative number. In the first case a response will be
564 * generated. In the latter case, no response will be sent and the driver which
565 * handled the request will send the response itself.
567 int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
568 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->read(host, nodeid, data,
584 addr, partlength, flags);
586 rcode = RCODE_TYPE_ERROR;
589 length -= partlength;
592 if ((rcode != RCODE_COMPLETE) || !length)
596 read_unlock(&addr_space_lock);
598 if (length && (rcode == RCODE_COMPLETE))
599 rcode = RCODE_ADDRESS_ERROR;
603 int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
604 u64 addr, unsigned int length, u16 flags)
606 struct hpsb_address_serve *as;
607 unsigned int partlength;
608 int rcode = RCODE_ADDRESS_ERROR;
610 read_lock(&addr_space_lock);
611 list_for_each_entry(as, &host->addr_space, host_list) {
612 if (as->start > addr)
615 if (as->end > addr) {
616 partlength = min(as->end - addr, (u64) length);
619 rcode = as->op->write(host, nodeid, destid,
620 data, addr, partlength,
623 rcode = RCODE_TYPE_ERROR;
626 length -= partlength;
629 if ((rcode != RCODE_COMPLETE) || !length)
633 read_unlock(&addr_space_lock);
635 if (length && (rcode == RCODE_COMPLETE))
636 rcode = RCODE_ADDRESS_ERROR;
640 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
641 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
644 struct hpsb_address_serve *as;
645 int rcode = RCODE_ADDRESS_ERROR;
647 read_lock(&addr_space_lock);
648 list_for_each_entry(as, &host->addr_space, host_list) {
649 if (as->start > addr)
652 if (as->end > addr) {
654 rcode = as->op->lock(host, nodeid, store, addr,
655 data, arg, ext_tcode,
658 rcode = RCODE_TYPE_ERROR;
662 read_unlock(&addr_space_lock);
666 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
667 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
670 struct hpsb_address_serve *as;
671 int rcode = RCODE_ADDRESS_ERROR;
673 read_lock(&addr_space_lock);
675 list_for_each_entry(as, &host->addr_space, host_list) {
676 if (as->start > addr)
679 if (as->end > addr) {
681 rcode = as->op->lock64(host, nodeid, store,
685 rcode = RCODE_TYPE_ERROR;
689 read_unlock(&addr_space_lock);