Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[linux-2.6] / drivers / ieee1394 / highlevel.c
1 /*
2  * IEEE 1394 for Linux
3  *
4  * Copyright (C) 1999 Andreas E. Bombe
5  *
6  * This code is licensed under the GPL.  See the file COPYING in the root
7  * directory of the kernel sources for details.
8  *
9  *
10  * Contributions:
11  *
12  * Christian Toegel <christian.toegel@gmx.at>
13  *        unregister address space
14  *
15  * Manfred Weihs <weihs@ict.tuwien.ac.at>
16  *        unregister address space
17  *
18  */
19
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/bitops.h>
23
24 #include "ieee1394.h"
25 #include "ieee1394_types.h"
26 #include "hosts.h"
27 #include "ieee1394_core.h"
28 #include "highlevel.h"
29 #include "nodemgr.h"
30
31
32 struct hl_host_info {
33         struct list_head list;
34         struct hpsb_host *host;
35         size_t size;
36         unsigned long key;
37         void *data;
38 };
39
40
41 static LIST_HEAD(hl_drivers);
42 static DECLARE_RWSEM(hl_drivers_sem);
43
44 static LIST_HEAD(hl_irqs);
45 static DEFINE_RWLOCK(hl_irqs_lock);
46
47 static DEFINE_RWLOCK(addr_space_lock);
48
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;
52
53
54 static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
55                                             struct hpsb_host *host)
56 {
57         struct hl_host_info *hi = NULL;
58
59         if (!hl || !host)
60                 return NULL;
61
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);
66                         return hi;
67                 }
68         }
69         read_unlock(&hl->host_info_lock);
70         return NULL;
71 }
72
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)
76 {
77         struct hl_host_info *hi = hl_get_hostinfo(hl, host);
78
79         return hi ? hi->data : NULL;
80 }
81
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,
84                            size_t data_size)
85 {
86         struct hl_host_info *hi;
87         void *data;
88         unsigned long flags;
89
90         hi = hl_get_hostinfo(hl, host);
91         if (hi) {
92                 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
93                          " exists", hl->name);
94                 return NULL;
95         }
96
97         hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
98         if (!hi)
99                 return NULL;
100
101         if (data_size) {
102                 data = hi->data = hi + 1;
103                 hi->size = data_size;
104         } else
105                 data = hi;
106
107         hi->host = host;
108
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);
112
113         return data;
114 }
115
116 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
117                       void *data)
118 {
119         struct hl_host_info *hi;
120
121         hi = hl_get_hostinfo(hl, host);
122         if (hi) {
123                 if (!hi->size && !hi->data) {
124                         hi->data = data;
125                         return 0;
126                 } else
127                         HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
128                                  "already has data", hl->name);
129         } else
130                 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
131                          hl->name);
132         return -EINVAL;
133 }
134
135 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
136 {
137         struct hl_host_info *hi;
138
139         hi = hl_get_hostinfo(hl, host);
140         if (hi) {
141                 unsigned long flags;
142                 write_lock_irqsave(&hl->host_info_lock, flags);
143                 list_del(&hi->list);
144                 write_unlock_irqrestore(&hl->host_info_lock, flags);
145                 kfree(hi);
146         }
147         return;
148 }
149
150 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
151                            unsigned long key)
152 {
153         struct hl_host_info *hi;
154
155         hi = hl_get_hostinfo(hl, host);
156         if (hi)
157                 hi->key = key;
158         return;
159 }
160
161 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
162 {
163         struct hl_host_info *hi;
164         void *data = NULL;
165
166         if (!hl)
167                 return NULL;
168
169         read_lock(&hl->host_info_lock);
170         list_for_each_entry(hi, &hl->host_info_list, list) {
171                 if (hi->key == key) {
172                         data = hi->data;
173                         break;
174                 }
175         }
176         read_unlock(&hl->host_info_lock);
177         return data;
178 }
179
180 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
181 {
182         struct hpsb_highlevel *hl = __data;
183
184         hl->add_host(host);
185
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);
189         return 0;
190 }
191
192 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
193 {
194         unsigned long flags;
195
196         INIT_LIST_HEAD(&hl->addr_list);
197         INIT_LIST_HEAD(&hl->host_info_list);
198
199         rwlock_init(&hl->host_info_lock);
200
201         down_write(&hl_drivers_sem);
202         list_add_tail(&hl->hl_list, &hl_drivers);
203         up_write(&hl_drivers_sem);
204
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);
208
209         if (hl->add_host)
210                 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
211         return;
212 }
213
214 static void __delete_addr(struct hpsb_address_serve *as)
215 {
216         list_del(&as->host_list);
217         list_del(&as->hl_list);
218         kfree(as);
219 }
220
221 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
222                               int update_cr)
223 {
224         unsigned long flags;
225         struct list_head *lh, *next;
226         struct hpsb_address_serve *as;
227
228         /* First, let the highlevel driver unreg */
229         if (hl->remove_host)
230                 hl->remove_host(host);
231
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)
238                         __delete_addr(as);
239         }
240         write_unlock_irqrestore(&addr_space_lock, flags);
241
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);
248
249         /* Finally remove all the host info associated between these two. */
250         hpsb_destroy_hostinfo(hl, host);
251 }
252
253 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
254 {
255         struct hpsb_highlevel *hl = __data;
256
257         __unregister_host(hl, host, 1);
258         return 0;
259 }
260
261 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
262 {
263         unsigned long flags;
264
265         write_lock_irqsave(&hl_irqs_lock, flags);
266         list_del(&hl->irq_list);
267         write_unlock_irqrestore(&hl_irqs_lock, flags);
268
269         down_write(&hl_drivers_sem);
270         list_del(&hl->hl_list);
271         up_write(&hl_drivers_sem);
272
273         nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
274 }
275
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,
280                                          u64 start, u64 end)
281 {
282         struct hpsb_address_serve *as, *a1, *a2;
283         struct list_head *entry;
284         u64 retval = CSR1212_INVALID_ADDR_SPACE;
285         unsigned long flags;
286         u64 align_mask = ~(alignment - 1);
287
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);
292                 return retval;
293         }
294
295         /* default range,
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;
301         }
302
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);
308                 return retval;
309         }
310
311         as = kmalloc(sizeof(*as), GFP_KERNEL);
312         if (!as)
313                 return retval;
314
315         INIT_LIST_HEAD(&as->host_list);
316         INIT_LIST_HEAD(&as->hl_list);
317         as->op = ops;
318         as->host = host;
319
320         write_lock_irqsave(&addr_space_lock, flags);
321         list_for_each(entry, &host->addr_space) {
322                 u64 a1sa, a1ea;
323                 u64 a2sa, a2ea;
324
325                 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
326                 a2 = list_entry(entry->next, struct hpsb_address_serve,
327                                 host_list);
328
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;
333
334                 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
335                     (a2sa > start)) {
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);
340                         retval = as->start;
341                         break;
342                 }
343         }
344         write_unlock_irqrestore(&addr_space_lock, flags);
345
346         if (retval == CSR1212_INVALID_ADDR_SPACE)
347                 kfree(as);
348         return retval;
349 }
350
351 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
352                             struct hpsb_address_ops *ops, u64 start, u64 end)
353 {
354         struct hpsb_address_serve *as;
355         struct list_head *lh;
356         int retval = 0;
357         unsigned long flags;
358
359         if (((start|end) & 3) || (start >= end) ||
360             (end > CSR1212_ALL_SPACE_END)) {
361                 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
362                 return 0;
363         }
364
365         as = kmalloc(sizeof(*as), GFP_ATOMIC);
366         if (!as)
367                 return 0;
368
369         INIT_LIST_HEAD(&as->host_list);
370         INIT_LIST_HEAD(&as->hl_list);
371         as->op = ops;
372         as->start = start;
373         as->end = end;
374         as->host = host;
375
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,
382                                    host_list);
383
384                 if (as_this->end > as->start)
385                         break;
386
387                 if (as_next->start >= as->end) {
388                         list_add(&as->host_list, lh);
389                         list_add_tail(&as->hl_list, &hl->addr_list);
390                         retval = 1;
391                         break;
392                 }
393         }
394         write_unlock_irqrestore(&addr_space_lock, flags);
395
396         if (retval == 0)
397                 kfree(as);
398         return retval;
399 }
400
401 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
402                               u64 start)
403 {
404         int retval = 0;
405         struct hpsb_address_serve *as;
406         struct list_head *lh, *next;
407         unsigned long flags;
408
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) {
413                         __delete_addr(as);
414                         retval = 1;
415                         break;
416                 }
417         }
418         write_unlock_irqrestore(&addr_space_lock, flags);
419         return retval;
420 }
421
422 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
423                         unsigned int channel)
424 {
425         if (channel > 63) {
426                 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
427                 return -EINVAL;
428         }
429         if (host->iso_listen_count[channel]++ == 0)
430                 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
431         return 0;
432 }
433
434 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
435                            unsigned int channel)
436 {
437         if (channel > 63) {
438                 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
439                 return;
440         }
441         if (--host->iso_listen_count[channel] == 0)
442                 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
443 }
444
445 static void init_hpsb_highlevel(struct hpsb_host *host)
446 {
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);
451
452         dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
453
454         dummy_zero_addr.start = dummy_zero_addr.end = 0;
455         dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
456
457         list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
458         list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
459 }
460
461 void highlevel_add_host(struct hpsb_host *host)
462 {
463         struct hpsb_highlevel *hl;
464
465         init_hpsb_highlevel(host);
466
467         down_read(&hl_drivers_sem);
468         list_for_each_entry(hl, &hl_drivers, hl_list) {
469                 if (hl->add_host)
470                         hl->add_host(host);
471         }
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);
476 }
477
478 void highlevel_remove_host(struct hpsb_host *host)
479 {
480         struct hpsb_highlevel *hl;
481
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);
486 }
487
488 void highlevel_host_reset(struct hpsb_host *host)
489 {
490         unsigned long flags;
491         struct hpsb_highlevel *hl;
492
493         read_lock_irqsave(&hl_irqs_lock, flags);
494         list_for_each_entry(hl, &hl_irqs, irq_list) {
495                 if (hl->host_reset)
496                         hl->host_reset(host);
497         }
498         read_unlock_irqrestore(&hl_irqs_lock, flags);
499 }
500
501 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
502 {
503         unsigned long flags;
504         struct hpsb_highlevel *hl;
505         int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
506
507         read_lock_irqsave(&hl_irqs_lock, flags);
508         list_for_each_entry(hl, &hl_irqs, irq_list) {
509                 if (hl->iso_receive)
510                         hl->iso_receive(host, channel, data, length);
511         }
512         read_unlock_irqrestore(&hl_irqs_lock, flags);
513 }
514
515 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
516                            void *data, size_t length)
517 {
518         unsigned long flags;
519         struct hpsb_highlevel *hl;
520         int cts = ((quadlet_t *)data)[0] >> 4;
521
522         read_lock_irqsave(&hl_irqs_lock, flags);
523         list_for_each_entry(hl, &hl_irqs, irq_list) {
524                 if (hl->fcp_request)
525                         hl->fcp_request(host, nodeid, direction, cts, data,
526                                         length);
527         }
528         read_unlock_irqrestore(&hl_irqs_lock, flags);
529 }
530
531 int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
532                    unsigned int length, u16 flags)
533 {
534         struct hpsb_address_serve *as;
535         unsigned int partlength;
536         int rcode = RCODE_ADDRESS_ERROR;
537
538         read_lock(&addr_space_lock);
539         list_for_each_entry(as, &host->addr_space, host_list) {
540                 if (as->start > addr)
541                         break;
542
543                 if (as->end > addr) {
544                         partlength = min(as->end - addr, (u64) length);
545
546                         if (as->op->read)
547                                 rcode = as->op->read(host, nodeid, data,
548                                                      addr, partlength, flags);
549                         else
550                                 rcode = RCODE_TYPE_ERROR;
551
552                         data += partlength;
553                         length -= partlength;
554                         addr += partlength;
555
556                         if ((rcode != RCODE_COMPLETE) || !length)
557                                 break;
558                 }
559         }
560         read_unlock(&addr_space_lock);
561
562         if (length && (rcode == RCODE_COMPLETE))
563                 rcode = RCODE_ADDRESS_ERROR;
564         return rcode;
565 }
566
567 int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
568                     u64 addr, unsigned int length, u16 flags)
569 {
570         struct hpsb_address_serve *as;
571         unsigned int partlength;
572         int rcode = RCODE_ADDRESS_ERROR;
573
574         read_lock(&addr_space_lock);
575         list_for_each_entry(as, &host->addr_space, host_list) {
576                 if (as->start > addr)
577                         break;
578
579                 if (as->end > addr) {
580                         partlength = min(as->end - addr, (u64) length);
581
582                         if (as->op->write)
583                                 rcode = as->op->write(host, nodeid, destid,
584                                                       data, addr, partlength,
585                                                       flags);
586                         else
587                                 rcode = RCODE_TYPE_ERROR;
588
589                         data += partlength;
590                         length -= partlength;
591                         addr += partlength;
592
593                         if ((rcode != RCODE_COMPLETE) || !length)
594                                 break;
595                 }
596         }
597         read_unlock(&addr_space_lock);
598
599         if (length && (rcode == RCODE_COMPLETE))
600                 rcode = RCODE_ADDRESS_ERROR;
601         return rcode;
602 }
603
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,
606                    u16 flags)
607 {
608         struct hpsb_address_serve *as;
609         int rcode = RCODE_ADDRESS_ERROR;
610
611         read_lock(&addr_space_lock);
612         list_for_each_entry(as, &host->addr_space, host_list) {
613                 if (as->start > addr)
614                         break;
615
616                 if (as->end > addr) {
617                         if (as->op->lock)
618                                 rcode = as->op->lock(host, nodeid, store, addr,
619                                                      data, arg, ext_tcode,
620                                                      flags);
621                         else
622                                 rcode = RCODE_TYPE_ERROR;
623                         break;
624                 }
625         }
626         read_unlock(&addr_space_lock);
627         return rcode;
628 }
629
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,
632                      u16 flags)
633 {
634         struct hpsb_address_serve *as;
635         int rcode = RCODE_ADDRESS_ERROR;
636
637         read_lock(&addr_space_lock);
638
639         list_for_each_entry(as, &host->addr_space, host_list) {
640                 if (as->start > addr)
641                         break;
642
643                 if (as->end > addr) {
644                         if (as->op->lock64)
645                                 rcode = as->op->lock64(host, nodeid, store,
646                                                        addr, data, arg,
647                                                        ext_tcode, flags);
648                         else
649                                 rcode = RCODE_TYPE_ERROR;
650                         break;
651                 }
652         }
653         read_unlock(&addr_space_lock);
654         return rcode;
655 }