GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / firewire / core-transaction.c
1 /*
2  * Core IEEE1394 transaction logic
3  *
4  * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <linux/bug.h>
22 #include <linux/completion.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/firewire.h>
26 #include <linux/firewire-constants.h>
27 #include <linux/fs.h>
28 #include <linux/init.h>
29 #include <linux/idr.h>
30 #include <linux/jiffies.h>
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/rculist.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/types.h>
40 #include <linux/workqueue.h>
41
42 #include <asm/byteorder.h>
43
44 #include "core.h"
45
46 #define HEADER_PRI(pri)                 ((pri) << 0)
47 #define HEADER_TCODE(tcode)             ((tcode) << 4)
48 #define HEADER_RETRY(retry)             ((retry) << 8)
49 #define HEADER_TLABEL(tlabel)           ((tlabel) << 10)
50 #define HEADER_DESTINATION(destination) ((destination) << 16)
51 #define HEADER_SOURCE(source)           ((source) << 16)
52 #define HEADER_RCODE(rcode)             ((rcode) << 12)
53 #define HEADER_OFFSET_HIGH(offset_high) ((offset_high) << 0)
54 #define HEADER_DATA_LENGTH(length)      ((length) << 16)
55 #define HEADER_EXTENDED_TCODE(tcode)    ((tcode) << 0)
56
57 #define HEADER_GET_TCODE(q)             (((q) >> 4) & 0x0f)
58 #define HEADER_GET_TLABEL(q)            (((q) >> 10) & 0x3f)
59 #define HEADER_GET_RCODE(q)             (((q) >> 12) & 0x0f)
60 #define HEADER_GET_DESTINATION(q)       (((q) >> 16) & 0xffff)
61 #define HEADER_GET_SOURCE(q)            (((q) >> 16) & 0xffff)
62 #define HEADER_GET_OFFSET_HIGH(q)       (((q) >> 0) & 0xffff)
63 #define HEADER_GET_DATA_LENGTH(q)       (((q) >> 16) & 0xffff)
64 #define HEADER_GET_EXTENDED_TCODE(q)    (((q) >> 0) & 0xffff)
65
66 #define HEADER_DESTINATION_IS_BROADCAST(q) \
67         (((q) & HEADER_DESTINATION(0x3f)) == HEADER_DESTINATION(0x3f))
68
69 #define PHY_PACKET_CONFIG       0x0
70 #define PHY_PACKET_LINK_ON      0x1
71 #define PHY_PACKET_SELF_ID      0x2
72
73 #define PHY_CONFIG_GAP_COUNT(gap_count) (((gap_count) << 16) | (1 << 22))
74 #define PHY_CONFIG_ROOT_ID(node_id)     ((((node_id) & 0x3f) << 24) | (1 << 23))
75 #define PHY_IDENTIFIER(id)              ((id) << 30)
76
77 /* returns 0 if the split timeout handler is already running */
78 static int try_cancel_split_timeout(struct fw_transaction *t)
79 {
80         if (t->is_split_transaction)
81                 return del_timer(&t->split_timeout_timer);
82         else
83                 return 1;
84 }
85
86 static int close_transaction(struct fw_transaction *transaction,
87                              struct fw_card *card, int rcode)
88 {
89         struct fw_transaction *t = NULL, *iter;
90         unsigned long flags;
91
92         spin_lock_irqsave(&card->lock, flags);
93         list_for_each_entry(iter, &card->transaction_list, link) {
94                 if (iter == transaction) {
95                         if (!try_cancel_split_timeout(iter)) {
96                                 spin_unlock_irqrestore(&card->lock, flags);
97                                 goto timed_out;
98                         }
99                         list_del_init(&iter->link);
100                         card->tlabel_mask &= ~(1ULL << iter->tlabel);
101                         t = iter;
102                         break;
103                 }
104         }
105         spin_unlock_irqrestore(&card->lock, flags);
106
107         if (t) {
108                 t->callback(card, rcode, NULL, 0, t->callback_data);
109                 return 0;
110         }
111
112  timed_out:
113         return -ENOENT;
114 }
115
116 /*
117  * Only valid for transactions that are potentially pending (ie have
118  * been sent).
119  */
120 int fw_cancel_transaction(struct fw_card *card,
121                           struct fw_transaction *transaction)
122 {
123         /*
124          * Cancel the packet transmission if it's still queued.  That
125          * will call the packet transmission callback which cancels
126          * the transaction.
127          */
128
129         if (card->driver->cancel_packet(card, &transaction->packet) == 0)
130                 return 0;
131
132         /*
133          * If the request packet has already been sent, we need to see
134          * if the transaction is still pending and remove it in that case.
135          */
136
137         return close_transaction(transaction, card, RCODE_CANCELLED);
138 }
139 EXPORT_SYMBOL(fw_cancel_transaction);
140
141 static void split_transaction_timeout_callback(struct timer_list *timer)
142 {
143         struct fw_transaction *t = from_timer(t, timer, split_timeout_timer);
144         struct fw_card *card = t->card;
145         unsigned long flags;
146
147         spin_lock_irqsave(&card->lock, flags);
148         if (list_empty(&t->link)) {
149                 spin_unlock_irqrestore(&card->lock, flags);
150                 return;
151         }
152         list_del(&t->link);
153         card->tlabel_mask &= ~(1ULL << t->tlabel);
154         spin_unlock_irqrestore(&card->lock, flags);
155
156         t->callback(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
157 }
158
159 static void start_split_transaction_timeout(struct fw_transaction *t,
160                                             struct fw_card *card)
161 {
162         unsigned long flags;
163
164         spin_lock_irqsave(&card->lock, flags);
165
166         if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) {
167                 spin_unlock_irqrestore(&card->lock, flags);
168                 return;
169         }
170
171         t->is_split_transaction = true;
172         mod_timer(&t->split_timeout_timer,
173                   jiffies + card->split_timeout_jiffies);
174
175         spin_unlock_irqrestore(&card->lock, flags);
176 }
177
178 static void transmit_complete_callback(struct fw_packet *packet,
179                                        struct fw_card *card, int status)
180 {
181         struct fw_transaction *t =
182             container_of(packet, struct fw_transaction, packet);
183
184         switch (status) {
185         case ACK_COMPLETE:
186                 close_transaction(t, card, RCODE_COMPLETE);
187                 break;
188         case ACK_PENDING:
189                 start_split_transaction_timeout(t, card);
190                 break;
191         case ACK_BUSY_X:
192         case ACK_BUSY_A:
193         case ACK_BUSY_B:
194                 close_transaction(t, card, RCODE_BUSY);
195                 break;
196         case ACK_DATA_ERROR:
197                 close_transaction(t, card, RCODE_DATA_ERROR);
198                 break;
199         case ACK_TYPE_ERROR:
200                 close_transaction(t, card, RCODE_TYPE_ERROR);
201                 break;
202         default:
203                 /*
204                  * In this case the ack is really a juju specific
205                  * rcode, so just forward that to the callback.
206                  */
207                 close_transaction(t, card, status);
208                 break;
209         }
210 }
211
212 static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
213                 int destination_id, int source_id, int generation, int speed,
214                 unsigned long long offset, void *payload, size_t length)
215 {
216         int ext_tcode;
217
218         if (tcode == TCODE_STREAM_DATA) {
219                 packet->header[0] =
220                         HEADER_DATA_LENGTH(length) |
221                         destination_id |
222                         HEADER_TCODE(TCODE_STREAM_DATA);
223                 packet->header_length = 4;
224                 packet->payload = payload;
225                 packet->payload_length = length;
226
227                 goto common;
228         }
229
230         if (tcode > 0x10) {
231                 ext_tcode = tcode & ~0x10;
232                 tcode = TCODE_LOCK_REQUEST;
233         } else
234                 ext_tcode = 0;
235
236         packet->header[0] =
237                 HEADER_RETRY(RETRY_X) |
238                 HEADER_TLABEL(tlabel) |
239                 HEADER_TCODE(tcode) |
240                 HEADER_DESTINATION(destination_id);
241         packet->header[1] =
242                 HEADER_OFFSET_HIGH(offset >> 32) | HEADER_SOURCE(source_id);
243         packet->header[2] =
244                 offset;
245
246         switch (tcode) {
247         case TCODE_WRITE_QUADLET_REQUEST:
248                 packet->header[3] = *(u32 *)payload;
249                 packet->header_length = 16;
250                 packet->payload_length = 0;
251                 break;
252
253         case TCODE_LOCK_REQUEST:
254         case TCODE_WRITE_BLOCK_REQUEST:
255                 packet->header[3] =
256                         HEADER_DATA_LENGTH(length) |
257                         HEADER_EXTENDED_TCODE(ext_tcode);
258                 packet->header_length = 16;
259                 packet->payload = payload;
260                 packet->payload_length = length;
261                 break;
262
263         case TCODE_READ_QUADLET_REQUEST:
264                 packet->header_length = 12;
265                 packet->payload_length = 0;
266                 break;
267
268         case TCODE_READ_BLOCK_REQUEST:
269                 packet->header[3] =
270                         HEADER_DATA_LENGTH(length) |
271                         HEADER_EXTENDED_TCODE(ext_tcode);
272                 packet->header_length = 16;
273                 packet->payload_length = 0;
274                 break;
275
276         default:
277                 WARN(1, "wrong tcode %d\n", tcode);
278         }
279  common:
280         packet->speed = speed;
281         packet->generation = generation;
282         packet->ack = 0;
283         packet->payload_mapped = false;
284 }
285
286 static int allocate_tlabel(struct fw_card *card)
287 {
288         int tlabel;
289
290         tlabel = card->current_tlabel;
291         while (card->tlabel_mask & (1ULL << tlabel)) {
292                 tlabel = (tlabel + 1) & 0x3f;
293                 if (tlabel == card->current_tlabel)
294                         return -EBUSY;
295         }
296
297         card->current_tlabel = (tlabel + 1) & 0x3f;
298         card->tlabel_mask |= 1ULL << tlabel;
299
300         return tlabel;
301 }
302
303 /**
304  * fw_send_request() - submit a request packet for transmission
305  * @card:               interface to send the request at
306  * @t:                  transaction instance to which the request belongs
307  * @tcode:              transaction code
308  * @destination_id:     destination node ID, consisting of bus_ID and phy_ID
309  * @generation:         bus generation in which request and response are valid
310  * @speed:              transmission speed
311  * @offset:             48bit wide offset into destination's address space
312  * @payload:            data payload for the request subaction
313  * @length:             length of the payload, in bytes
314  * @callback:           function to be called when the transaction is completed
315  * @callback_data:      data to be passed to the transaction completion callback
316  *
317  * Submit a request packet into the asynchronous request transmission queue.
318  * Can be called from atomic context.  If you prefer a blocking API, use
319  * fw_run_transaction() in a context that can sleep.
320  *
321  * In case of lock requests, specify one of the firewire-core specific %TCODE_
322  * constants instead of %TCODE_LOCK_REQUEST in @tcode.
323  *
324  * Make sure that the value in @destination_id is not older than the one in
325  * @generation.  Otherwise the request is in danger to be sent to a wrong node.
326  *
327  * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
328  * needs to synthesize @destination_id with fw_stream_packet_destination_id().
329  * It will contain tag, channel, and sy data instead of a node ID then.
330  *
331  * The payload buffer at @data is going to be DMA-mapped except in case of
332  * @length <= 8 or of local (loopback) requests.  Hence make sure that the
333  * buffer complies with the restrictions of the streaming DMA mapping API.
334  * @payload must not be freed before the @callback is called.
335  *
336  * In case of request types without payload, @data is NULL and @length is 0.
337  *
338  * After the transaction is completed successfully or unsuccessfully, the
339  * @callback will be called.  Among its parameters is the response code which
340  * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
341  * the firewire-core specific %RCODE_SEND_ERROR.  The other firewire-core
342  * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION,
343  * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request
344  * generation, or missing ACK respectively.
345  *
346  * Note some timing corner cases:  fw_send_request() may complete much earlier
347  * than when the request packet actually hits the wire.  On the other hand,
348  * transaction completion and hence execution of @callback may happen even
349  * before fw_send_request() returns.
350  */
351 void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
352                      int destination_id, int generation, int speed,
353                      unsigned long long offset, void *payload, size_t length,
354                      fw_transaction_callback_t callback, void *callback_data)
355 {
356         unsigned long flags;
357         int tlabel;
358
359         /*
360          * Allocate tlabel from the bitmap and put the transaction on
361          * the list while holding the card spinlock.
362          */
363
364         spin_lock_irqsave(&card->lock, flags);
365
366         tlabel = allocate_tlabel(card);
367         if (tlabel < 0) {
368                 spin_unlock_irqrestore(&card->lock, flags);
369                 callback(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
370                 return;
371         }
372
373         t->node_id = destination_id;
374         t->tlabel = tlabel;
375         t->card = card;
376         t->is_split_transaction = false;
377         timer_setup(&t->split_timeout_timer,
378                     split_transaction_timeout_callback, 0);
379         t->callback = callback;
380         t->callback_data = callback_data;
381
382         fw_fill_request(&t->packet, tcode, t->tlabel,
383                         destination_id, card->node_id, generation,
384                         speed, offset, payload, length);
385         t->packet.callback = transmit_complete_callback;
386
387         list_add_tail(&t->link, &card->transaction_list);
388
389         spin_unlock_irqrestore(&card->lock, flags);
390
391         card->driver->send_request(card, &t->packet);
392 }
393 EXPORT_SYMBOL(fw_send_request);
394
395 struct transaction_callback_data {
396         struct completion done;
397         void *payload;
398         int rcode;
399 };
400
401 static void transaction_callback(struct fw_card *card, int rcode,
402                                  void *payload, size_t length, void *data)
403 {
404         struct transaction_callback_data *d = data;
405
406         if (rcode == RCODE_COMPLETE)
407                 memcpy(d->payload, payload, length);
408         d->rcode = rcode;
409         complete(&d->done);
410 }
411
412 /**
413  * fw_run_transaction() - send request and sleep until transaction is completed
414  *
415  * Returns the RCODE.  See fw_send_request() for parameter documentation.
416  * Unlike fw_send_request(), @data points to the payload of the request or/and
417  * to the payload of the response.  DMA mapping restrictions apply to outbound
418  * request payloads of >= 8 bytes but not to inbound response payloads.
419  */
420 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
421                        int generation, int speed, unsigned long long offset,
422                        void *payload, size_t length)
423 {
424         struct transaction_callback_data d;
425         struct fw_transaction t;
426
427         timer_setup_on_stack(&t.split_timeout_timer, NULL, 0);
428         init_completion(&d.done);
429         d.payload = payload;
430         fw_send_request(card, &t, tcode, destination_id, generation, speed,
431                         offset, payload, length, transaction_callback, &d);
432         wait_for_completion(&d.done);
433         destroy_timer_on_stack(&t.split_timeout_timer);
434
435         return d.rcode;
436 }
437 EXPORT_SYMBOL(fw_run_transaction);
438
439 static DEFINE_MUTEX(phy_config_mutex);
440 static DECLARE_COMPLETION(phy_config_done);
441
442 static void transmit_phy_packet_callback(struct fw_packet *packet,
443                                          struct fw_card *card, int status)
444 {
445         complete(&phy_config_done);
446 }
447
448 static struct fw_packet phy_config_packet = {
449         .header_length  = 12,
450         .header[0]      = TCODE_LINK_INTERNAL << 4,
451         .payload_length = 0,
452         .speed          = SCODE_100,
453         .callback       = transmit_phy_packet_callback,
454 };
455
456 void fw_send_phy_config(struct fw_card *card,
457                         int node_id, int generation, int gap_count)
458 {
459         long timeout = DIV_ROUND_UP(HZ, 10);
460         u32 data = PHY_IDENTIFIER(PHY_PACKET_CONFIG);
461
462         if (node_id != FW_PHY_CONFIG_NO_NODE_ID)
463                 data |= PHY_CONFIG_ROOT_ID(node_id);
464
465         if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
466                 gap_count = card->driver->read_phy_reg(card, 1);
467                 if (gap_count < 0)
468                         return;
469
470                 gap_count &= 63;
471                 if (gap_count == 63)
472                         return;
473         }
474         data |= PHY_CONFIG_GAP_COUNT(gap_count);
475
476         mutex_lock(&phy_config_mutex);
477
478         phy_config_packet.header[1] = data;
479         phy_config_packet.header[2] = ~data;
480         phy_config_packet.generation = generation;
481         reinit_completion(&phy_config_done);
482
483         card->driver->send_request(card, &phy_config_packet);
484         wait_for_completion_timeout(&phy_config_done, timeout);
485
486         mutex_unlock(&phy_config_mutex);
487 }
488
489 static struct fw_address_handler *lookup_overlapping_address_handler(
490         struct list_head *list, unsigned long long offset, size_t length)
491 {
492         struct fw_address_handler *handler;
493
494         list_for_each_entry_rcu(handler, list, link) {
495                 if (handler->offset < offset + length &&
496                     offset < handler->offset + handler->length)
497                         return handler;
498         }
499
500         return NULL;
501 }
502
503 static bool is_enclosing_handler(struct fw_address_handler *handler,
504                                  unsigned long long offset, size_t length)
505 {
506         return handler->offset <= offset &&
507                 offset + length <= handler->offset + handler->length;
508 }
509
510 static struct fw_address_handler *lookup_enclosing_address_handler(
511         struct list_head *list, unsigned long long offset, size_t length)
512 {
513         struct fw_address_handler *handler;
514
515         list_for_each_entry_rcu(handler, list, link) {
516                 if (is_enclosing_handler(handler, offset, length))
517                         return handler;
518         }
519
520         return NULL;
521 }
522
523 static DEFINE_SPINLOCK(address_handler_list_lock);
524 static LIST_HEAD(address_handler_list);
525
526 const struct fw_address_region fw_high_memory_region =
527         { .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, };
528 EXPORT_SYMBOL(fw_high_memory_region);
529
530 static const struct fw_address_region low_memory_region =
531         { .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, };
532
533 #if 0
534 const struct fw_address_region fw_private_region =
535         { .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL,  };
536 const struct fw_address_region fw_csr_region =
537         { .start = CSR_REGISTER_BASE,
538           .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END,  };
539 const struct fw_address_region fw_unit_space_region =
540         { .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
541 #endif  /*  0  */
542
543 static bool is_in_fcp_region(u64 offset, size_t length)
544 {
545         return offset >= (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
546                 offset + length <= (CSR_REGISTER_BASE | CSR_FCP_END);
547 }
548
549 /**
550  * fw_core_add_address_handler() - register for incoming requests
551  * @handler:    callback
552  * @region:     region in the IEEE 1212 node space address range
553  *
554  * region->start, ->end, and handler->length have to be quadlet-aligned.
555  *
556  * When a request is received that falls within the specified address range,
557  * the specified callback is invoked.  The parameters passed to the callback
558  * give the details of the particular request.
559  *
560  * To be called in process context.
561  * Return value:  0 on success, non-zero otherwise.
562  *
563  * The start offset of the handler's address region is determined by
564  * fw_core_add_address_handler() and is returned in handler->offset.
565  *
566  * Address allocations are exclusive, except for the FCP registers.
567  */
568 int fw_core_add_address_handler(struct fw_address_handler *handler,
569                                 const struct fw_address_region *region)
570 {
571         struct fw_address_handler *other;
572         int ret = -EBUSY;
573
574         if (region->start & 0xffff000000000003ULL ||
575             region->start >= region->end ||
576             region->end   > 0x0001000000000000ULL ||
577             handler->length & 3 ||
578             handler->length == 0)
579                 return -EINVAL;
580
581         spin_lock(&address_handler_list_lock);
582
583         handler->offset = region->start;
584         while (handler->offset + handler->length <= region->end) {
585                 if (is_in_fcp_region(handler->offset, handler->length))
586                         other = NULL;
587                 else
588                         other = lookup_overlapping_address_handler
589                                         (&address_handler_list,
590                                          handler->offset, handler->length);
591                 if (other != NULL) {
592                         handler->offset += other->length;
593                 } else {
594                         list_add_tail_rcu(&handler->link, &address_handler_list);
595                         ret = 0;
596                         break;
597                 }
598         }
599
600         spin_unlock(&address_handler_list_lock);
601
602         return ret;
603 }
604 EXPORT_SYMBOL(fw_core_add_address_handler);
605
606 /**
607  * fw_core_remove_address_handler() - unregister an address handler
608  *
609  * To be called in process context.
610  *
611  * When fw_core_remove_address_handler() returns, @handler->callback() is
612  * guaranteed to not run on any CPU anymore.
613  */
614 void fw_core_remove_address_handler(struct fw_address_handler *handler)
615 {
616         spin_lock(&address_handler_list_lock);
617         list_del_rcu(&handler->link);
618         spin_unlock(&address_handler_list_lock);
619         synchronize_rcu();
620 }
621 EXPORT_SYMBOL(fw_core_remove_address_handler);
622
623 struct fw_request {
624         struct fw_packet response;
625         u32 request_header[4];
626         int ack;
627         u32 length;
628         u32 data[0];
629 };
630
631 static void free_response_callback(struct fw_packet *packet,
632                                    struct fw_card *card, int status)
633 {
634         struct fw_request *request;
635
636         request = container_of(packet, struct fw_request, response);
637         kfree(request);
638 }
639
640 int fw_get_response_length(struct fw_request *r)
641 {
642         int tcode, ext_tcode, data_length;
643
644         tcode = HEADER_GET_TCODE(r->request_header[0]);
645
646         switch (tcode) {
647         case TCODE_WRITE_QUADLET_REQUEST:
648         case TCODE_WRITE_BLOCK_REQUEST:
649                 return 0;
650
651         case TCODE_READ_QUADLET_REQUEST:
652                 return 4;
653
654         case TCODE_READ_BLOCK_REQUEST:
655                 data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
656                 return data_length;
657
658         case TCODE_LOCK_REQUEST:
659                 ext_tcode = HEADER_GET_EXTENDED_TCODE(r->request_header[3]);
660                 data_length = HEADER_GET_DATA_LENGTH(r->request_header[3]);
661                 switch (ext_tcode) {
662                 case EXTCODE_FETCH_ADD:
663                 case EXTCODE_LITTLE_ADD:
664                         return data_length;
665                 default:
666                         return data_length / 2;
667                 }
668
669         default:
670                 WARN(1, "wrong tcode %d\n", tcode);
671                 return 0;
672         }
673 }
674
675 void fw_fill_response(struct fw_packet *response, u32 *request_header,
676                       int rcode, void *payload, size_t length)
677 {
678         int tcode, tlabel, extended_tcode, source, destination;
679
680         tcode          = HEADER_GET_TCODE(request_header[0]);
681         tlabel         = HEADER_GET_TLABEL(request_header[0]);
682         source         = HEADER_GET_DESTINATION(request_header[0]);
683         destination    = HEADER_GET_SOURCE(request_header[1]);
684         extended_tcode = HEADER_GET_EXTENDED_TCODE(request_header[3]);
685
686         response->header[0] =
687                 HEADER_RETRY(RETRY_1) |
688                 HEADER_TLABEL(tlabel) |
689                 HEADER_DESTINATION(destination);
690         response->header[1] =
691                 HEADER_SOURCE(source) |
692                 HEADER_RCODE(rcode);
693         response->header[2] = 0;
694
695         switch (tcode) {
696         case TCODE_WRITE_QUADLET_REQUEST:
697         case TCODE_WRITE_BLOCK_REQUEST:
698                 response->header[0] |= HEADER_TCODE(TCODE_WRITE_RESPONSE);
699                 response->header_length = 12;
700                 response->payload_length = 0;
701                 break;
702
703         case TCODE_READ_QUADLET_REQUEST:
704                 response->header[0] |=
705                         HEADER_TCODE(TCODE_READ_QUADLET_RESPONSE);
706                 if (payload != NULL)
707                         response->header[3] = *(u32 *)payload;
708                 else
709                         response->header[3] = 0;
710                 response->header_length = 16;
711                 response->payload_length = 0;
712                 break;
713
714         case TCODE_READ_BLOCK_REQUEST:
715         case TCODE_LOCK_REQUEST:
716                 response->header[0] |= HEADER_TCODE(tcode + 2);
717                 response->header[3] =
718                         HEADER_DATA_LENGTH(length) |
719                         HEADER_EXTENDED_TCODE(extended_tcode);
720                 response->header_length = 16;
721                 response->payload = payload;
722                 response->payload_length = length;
723                 break;
724
725         default:
726                 WARN(1, "wrong tcode %d\n", tcode);
727         }
728
729         response->payload_mapped = false;
730 }
731 EXPORT_SYMBOL(fw_fill_response);
732
733 static u32 compute_split_timeout_timestamp(struct fw_card *card,
734                                            u32 request_timestamp)
735 {
736         unsigned int cycles;
737         u32 timestamp;
738
739         cycles = card->split_timeout_cycles;
740         cycles += request_timestamp & 0x1fff;
741
742         timestamp = request_timestamp & ~0x1fff;
743         timestamp += (cycles / 8000) << 13;
744         timestamp |= cycles % 8000;
745
746         return timestamp;
747 }
748
749 static struct fw_request *allocate_request(struct fw_card *card,
750                                            struct fw_packet *p)
751 {
752         struct fw_request *request;
753         u32 *data, length;
754         int request_tcode;
755
756         request_tcode = HEADER_GET_TCODE(p->header[0]);
757         switch (request_tcode) {
758         case TCODE_WRITE_QUADLET_REQUEST:
759                 data = &p->header[3];
760                 length = 4;
761                 break;
762
763         case TCODE_WRITE_BLOCK_REQUEST:
764         case TCODE_LOCK_REQUEST:
765                 data = p->payload;
766                 length = HEADER_GET_DATA_LENGTH(p->header[3]);
767                 break;
768
769         case TCODE_READ_QUADLET_REQUEST:
770                 data = NULL;
771                 length = 4;
772                 break;
773
774         case TCODE_READ_BLOCK_REQUEST:
775                 data = NULL;
776                 length = HEADER_GET_DATA_LENGTH(p->header[3]);
777                 break;
778
779         default:
780                 fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
781                          p->header[0], p->header[1], p->header[2]);
782                 return NULL;
783         }
784
785         request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
786         if (request == NULL)
787                 return NULL;
788
789         request->response.speed = p->speed;
790         request->response.timestamp =
791                         compute_split_timeout_timestamp(card, p->timestamp);
792         request->response.generation = p->generation;
793         request->response.ack = 0;
794         request->response.callback = free_response_callback;
795         request->ack = p->ack;
796         request->length = length;
797         if (data)
798                 memcpy(request->data, data, length);
799
800         memcpy(request->request_header, p->header, sizeof(p->header));
801
802         return request;
803 }
804
805 void fw_send_response(struct fw_card *card,
806                       struct fw_request *request, int rcode)
807 {
808         if (WARN_ONCE(!request, "invalid for FCP address handlers"))
809                 return;
810
811         /* unified transaction or broadcast transaction: don't respond */
812         if (request->ack != ACK_PENDING ||
813             HEADER_DESTINATION_IS_BROADCAST(request->request_header[0])) {
814                 kfree(request);
815                 return;
816         }
817
818         if (rcode == RCODE_COMPLETE)
819                 fw_fill_response(&request->response, request->request_header,
820                                  rcode, request->data,
821                                  fw_get_response_length(request));
822         else
823                 fw_fill_response(&request->response, request->request_header,
824                                  rcode, NULL, 0);
825
826         card->driver->send_response(card, &request->response);
827 }
828 EXPORT_SYMBOL(fw_send_response);
829
830 /**
831  * fw_get_request_speed() - returns speed at which the @request was received
832  */
833 int fw_get_request_speed(struct fw_request *request)
834 {
835         return request->response.speed;
836 }
837 EXPORT_SYMBOL(fw_get_request_speed);
838
839 static void handle_exclusive_region_request(struct fw_card *card,
840                                             struct fw_packet *p,
841                                             struct fw_request *request,
842                                             unsigned long long offset)
843 {
844         struct fw_address_handler *handler;
845         int tcode, destination, source;
846
847         destination = HEADER_GET_DESTINATION(p->header[0]);
848         source      = HEADER_GET_SOURCE(p->header[1]);
849         tcode       = HEADER_GET_TCODE(p->header[0]);
850         if (tcode == TCODE_LOCK_REQUEST)
851                 tcode = 0x10 + HEADER_GET_EXTENDED_TCODE(p->header[3]);
852
853         rcu_read_lock();
854         handler = lookup_enclosing_address_handler(&address_handler_list,
855                                                    offset, request->length);
856         if (handler)
857                 handler->address_callback(card, request,
858                                           tcode, destination, source,
859                                           p->generation, offset,
860                                           request->data, request->length,
861                                           handler->callback_data);
862         rcu_read_unlock();
863
864         if (!handler)
865                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
866 }
867
868 static void handle_fcp_region_request(struct fw_card *card,
869                                       struct fw_packet *p,
870                                       struct fw_request *request,
871                                       unsigned long long offset)
872 {
873         struct fw_address_handler *handler;
874         int tcode, destination, source;
875
876         if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
877              offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
878             request->length > 0x200) {
879                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
880
881                 return;
882         }
883
884         tcode       = HEADER_GET_TCODE(p->header[0]);
885         destination = HEADER_GET_DESTINATION(p->header[0]);
886         source      = HEADER_GET_SOURCE(p->header[1]);
887
888         if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
889             tcode != TCODE_WRITE_BLOCK_REQUEST) {
890                 fw_send_response(card, request, RCODE_TYPE_ERROR);
891
892                 return;
893         }
894
895         rcu_read_lock();
896         list_for_each_entry_rcu(handler, &address_handler_list, link) {
897                 if (is_enclosing_handler(handler, offset, request->length))
898                         handler->address_callback(card, NULL, tcode,
899                                                   destination, source,
900                                                   p->generation, offset,
901                                                   request->data,
902                                                   request->length,
903                                                   handler->callback_data);
904         }
905         rcu_read_unlock();
906
907         fw_send_response(card, request, RCODE_COMPLETE);
908 }
909
910 void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
911 {
912         struct fw_request *request;
913         unsigned long long offset;
914
915         if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
916                 return;
917
918         if (TCODE_IS_LINK_INTERNAL(HEADER_GET_TCODE(p->header[0]))) {
919                 fw_cdev_handle_phy_packet(card, p);
920                 return;
921         }
922
923         request = allocate_request(card, p);
924         if (request == NULL) {
925                 /* FIXME: send statically allocated busy packet. */
926                 return;
927         }
928
929         offset = ((u64)HEADER_GET_OFFSET_HIGH(p->header[1]) << 32) |
930                 p->header[2];
931
932         if (!is_in_fcp_region(offset, request->length))
933                 handle_exclusive_region_request(card, p, request, offset);
934         else
935                 handle_fcp_region_request(card, p, request, offset);
936
937 }
938 EXPORT_SYMBOL(fw_core_handle_request);
939
940 void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
941 {
942         struct fw_transaction *t = NULL, *iter;
943         unsigned long flags;
944         u32 *data;
945         size_t data_length;
946         int tcode, tlabel, source, rcode;
947
948         tcode   = HEADER_GET_TCODE(p->header[0]);
949         tlabel  = HEADER_GET_TLABEL(p->header[0]);
950         source  = HEADER_GET_SOURCE(p->header[1]);
951         rcode   = HEADER_GET_RCODE(p->header[1]);
952
953         spin_lock_irqsave(&card->lock, flags);
954         list_for_each_entry(iter, &card->transaction_list, link) {
955                 if (iter->node_id == source && iter->tlabel == tlabel) {
956                         if (!try_cancel_split_timeout(iter)) {
957                                 spin_unlock_irqrestore(&card->lock, flags);
958                                 goto timed_out;
959                         }
960                         list_del_init(&iter->link);
961                         card->tlabel_mask &= ~(1ULL << iter->tlabel);
962                         t = iter;
963                         break;
964                 }
965         }
966         spin_unlock_irqrestore(&card->lock, flags);
967
968         if (!t) {
969  timed_out:
970                 fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
971                           source, tlabel);
972                 return;
973         }
974
975         /*
976          * FIXME: sanity check packet, is length correct, does tcodes
977          * and addresses match.
978          */
979
980         switch (tcode) {
981         case TCODE_READ_QUADLET_RESPONSE:
982                 data = (u32 *) &p->header[3];
983                 data_length = 4;
984                 break;
985
986         case TCODE_WRITE_RESPONSE:
987                 data = NULL;
988                 data_length = 0;
989                 break;
990
991         case TCODE_READ_BLOCK_RESPONSE:
992         case TCODE_LOCK_RESPONSE:
993                 data = p->payload;
994                 data_length = HEADER_GET_DATA_LENGTH(p->header[3]);
995                 break;
996
997         default:
998                 /* Should never happen, this is just to shut up gcc. */
999                 data = NULL;
1000                 data_length = 0;
1001                 break;
1002         }
1003
1004         /*
1005          * The response handler may be executed while the request handler
1006          * is still pending.  Cancel the request handler.
1007          */
1008         card->driver->cancel_packet(card, &t->packet);
1009
1010         t->callback(card, rcode, data, data_length, t->callback_data);
1011 }
1012 EXPORT_SYMBOL(fw_core_handle_response);
1013
1014 /**
1015  * fw_rcode_string - convert a firewire result code to an error description
1016  * @rcode: the result code
1017  */
1018 const char *fw_rcode_string(int rcode)
1019 {
1020         static const char *const names[] = {
1021                 [RCODE_COMPLETE]       = "no error",
1022                 [RCODE_CONFLICT_ERROR] = "conflict error",
1023                 [RCODE_DATA_ERROR]     = "data error",
1024                 [RCODE_TYPE_ERROR]     = "type error",
1025                 [RCODE_ADDRESS_ERROR]  = "address error",
1026                 [RCODE_SEND_ERROR]     = "send error",
1027                 [RCODE_CANCELLED]      = "timeout",
1028                 [RCODE_BUSY]           = "busy",
1029                 [RCODE_GENERATION]     = "bus reset",
1030                 [RCODE_NO_ACK]         = "no ack",
1031         };
1032
1033         if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode])
1034                 return names[rcode];
1035         else
1036                 return "unknown";
1037 }
1038 EXPORT_SYMBOL(fw_rcode_string);
1039
1040 static const struct fw_address_region topology_map_region =
1041         { .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
1042           .end   = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
1043
1044 static void handle_topology_map(struct fw_card *card, struct fw_request *request,
1045                 int tcode, int destination, int source, int generation,
1046                 unsigned long long offset, void *payload, size_t length,
1047                 void *callback_data)
1048 {
1049         int start;
1050
1051         if (!TCODE_IS_READ_REQUEST(tcode)) {
1052                 fw_send_response(card, request, RCODE_TYPE_ERROR);
1053                 return;
1054         }
1055
1056         if ((offset & 3) > 0 || (length & 3) > 0) {
1057                 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
1058                 return;
1059         }
1060
1061         start = (offset - topology_map_region.start) / 4;
1062         memcpy(payload, &card->topology_map[start], length);
1063
1064         fw_send_response(card, request, RCODE_COMPLETE);
1065 }
1066
1067 static struct fw_address_handler topology_map = {
1068         .length                 = 0x400,
1069         .address_callback       = handle_topology_map,
1070 };
1071
1072 static const struct fw_address_region registers_region =
1073         { .start = CSR_REGISTER_BASE,
1074           .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
1075
1076 static void update_split_timeout(struct fw_card *card)
1077 {
1078         unsigned int cycles;
1079
1080         cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
1081
1082         /* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
1083         cycles = clamp(cycles, 800u, 3u * 8000u);
1084
1085         card->split_timeout_cycles = cycles;
1086         card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
1087 }
1088
1089 static void handle_registers(struct fw_card *card, struct fw_request *request,
1090                 int tcode, int destination, int source, int generation,
1091                 unsigned long long offset, void *payload, size_t length,
1092                 void *callback_data)
1093 {
1094         int reg = offset & ~CSR_REGISTER_BASE;
1095         __be32 *data = payload;
1096         int rcode = RCODE_COMPLETE;
1097         unsigned long flags;
1098
1099         switch (reg) {
1100         case CSR_PRIORITY_BUDGET:
1101                 if (!card->priority_budget_implemented) {
1102                         rcode = RCODE_ADDRESS_ERROR;
1103                         break;
1104                 }
1105                 /* else fall through */
1106
1107         case CSR_NODE_IDS:
1108                 /*
1109                  * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
1110                  * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
1111                  */
1112                 /* fall through */
1113
1114         case CSR_STATE_CLEAR:
1115         case CSR_STATE_SET:
1116         case CSR_CYCLE_TIME:
1117         case CSR_BUS_TIME:
1118         case CSR_BUSY_TIMEOUT:
1119                 if (tcode == TCODE_READ_QUADLET_REQUEST)
1120                         *data = cpu_to_be32(card->driver->read_csr(card, reg));
1121                 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1122                         card->driver->write_csr(card, reg, be32_to_cpu(*data));
1123                 else
1124                         rcode = RCODE_TYPE_ERROR;
1125                 break;
1126
1127         case CSR_RESET_START:
1128                 if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1129                         card->driver->write_csr(card, CSR_STATE_CLEAR,
1130                                                 CSR_STATE_BIT_ABDICATE);
1131                 else
1132                         rcode = RCODE_TYPE_ERROR;
1133                 break;
1134
1135         case CSR_SPLIT_TIMEOUT_HI:
1136                 if (tcode == TCODE_READ_QUADLET_REQUEST) {
1137                         *data = cpu_to_be32(card->split_timeout_hi);
1138                 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1139                         spin_lock_irqsave(&card->lock, flags);
1140                         card->split_timeout_hi = be32_to_cpu(*data) & 7;
1141                         update_split_timeout(card);
1142                         spin_unlock_irqrestore(&card->lock, flags);
1143                 } else {
1144                         rcode = RCODE_TYPE_ERROR;
1145                 }
1146                 break;
1147
1148         case CSR_SPLIT_TIMEOUT_LO:
1149                 if (tcode == TCODE_READ_QUADLET_REQUEST) {
1150                         *data = cpu_to_be32(card->split_timeout_lo);
1151                 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1152                         spin_lock_irqsave(&card->lock, flags);
1153                         card->split_timeout_lo =
1154                                         be32_to_cpu(*data) & 0xfff80000;
1155                         update_split_timeout(card);
1156                         spin_unlock_irqrestore(&card->lock, flags);
1157                 } else {
1158                         rcode = RCODE_TYPE_ERROR;
1159                 }
1160                 break;
1161
1162         case CSR_MAINT_UTILITY:
1163                 if (tcode == TCODE_READ_QUADLET_REQUEST)
1164                         *data = card->maint_utility_register;
1165                 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1166                         card->maint_utility_register = *data;
1167                 else
1168                         rcode = RCODE_TYPE_ERROR;
1169                 break;
1170
1171         case CSR_BROADCAST_CHANNEL:
1172                 if (tcode == TCODE_READ_QUADLET_REQUEST)
1173                         *data = cpu_to_be32(card->broadcast_channel);
1174                 else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1175                         card->broadcast_channel =
1176                             (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
1177                             BROADCAST_CHANNEL_INITIAL;
1178                 else
1179                         rcode = RCODE_TYPE_ERROR;
1180                 break;
1181
1182         case CSR_BUS_MANAGER_ID:
1183         case CSR_BANDWIDTH_AVAILABLE:
1184         case CSR_CHANNELS_AVAILABLE_HI:
1185         case CSR_CHANNELS_AVAILABLE_LO:
1186                 /*
1187                  * FIXME: these are handled by the OHCI hardware and
1188                  * the stack never sees these request. If we add
1189                  * support for a new type of controller that doesn't
1190                  * handle this in hardware we need to deal with these
1191                  * transactions.
1192                  */
1193                 BUG();
1194                 break;
1195
1196         default:
1197                 rcode = RCODE_ADDRESS_ERROR;
1198                 break;
1199         }
1200
1201         fw_send_response(card, request, rcode);
1202 }
1203
1204 static struct fw_address_handler registers = {
1205         .length                 = 0x400,
1206         .address_callback       = handle_registers,
1207 };
1208
1209 static void handle_low_memory(struct fw_card *card, struct fw_request *request,
1210                 int tcode, int destination, int source, int generation,
1211                 unsigned long long offset, void *payload, size_t length,
1212                 void *callback_data)
1213 {
1214         /*
1215          * This catches requests not handled by the physical DMA unit,
1216          * i.e., wrong transaction types or unauthorized source nodes.
1217          */
1218         fw_send_response(card, request, RCODE_TYPE_ERROR);
1219 }
1220
1221 static struct fw_address_handler low_memory = {
1222         .length                 = FW_MAX_PHYSICAL_RANGE,
1223         .address_callback       = handle_low_memory,
1224 };
1225
1226 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1227 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
1228 MODULE_LICENSE("GPL");
1229
1230 static const u32 vendor_textual_descriptor[] = {
1231         /* textual descriptor leaf () */
1232         0x00060000,
1233         0x00000000,
1234         0x00000000,
1235         0x4c696e75,             /* L i n u */
1236         0x78204669,             /* x   F i */
1237         0x72657769,             /* r e w i */
1238         0x72650000,             /* r e     */
1239 };
1240
1241 static const u32 model_textual_descriptor[] = {
1242         /* model descriptor leaf () */
1243         0x00030000,
1244         0x00000000,
1245         0x00000000,
1246         0x4a756a75,             /* J u j u */
1247 };
1248
1249 static struct fw_descriptor vendor_id_descriptor = {
1250         .length = ARRAY_SIZE(vendor_textual_descriptor),
1251         .immediate = 0x03001f11,
1252         .key = 0x81000000,
1253         .data = vendor_textual_descriptor,
1254 };
1255
1256 static struct fw_descriptor model_id_descriptor = {
1257         .length = ARRAY_SIZE(model_textual_descriptor),
1258         .immediate = 0x17023901,
1259         .key = 0x81000000,
1260         .data = model_textual_descriptor,
1261 };
1262
1263 static int __init fw_core_init(void)
1264 {
1265         int ret;
1266
1267         fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM, 0);
1268         if (!fw_workqueue)
1269                 return -ENOMEM;
1270
1271         ret = bus_register(&fw_bus_type);
1272         if (ret < 0) {
1273                 destroy_workqueue(fw_workqueue);
1274                 return ret;
1275         }
1276
1277         fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
1278         if (fw_cdev_major < 0) {
1279                 bus_unregister(&fw_bus_type);
1280                 destroy_workqueue(fw_workqueue);
1281                 return fw_cdev_major;
1282         }
1283
1284         fw_core_add_address_handler(&topology_map, &topology_map_region);
1285         fw_core_add_address_handler(&registers, &registers_region);
1286         fw_core_add_address_handler(&low_memory, &low_memory_region);
1287         fw_core_add_descriptor(&vendor_id_descriptor);
1288         fw_core_add_descriptor(&model_id_descriptor);
1289
1290         return 0;
1291 }
1292
1293 static void __exit fw_core_cleanup(void)
1294 {
1295         unregister_chrdev(fw_cdev_major, "firewire");
1296         bus_unregister(&fw_bus_type);
1297         destroy_workqueue(fw_workqueue);
1298         idr_destroy(&fw_device_idr);
1299 }
1300
1301 module_init(fw_core_init);
1302 module_exit(fw_core_cleanup);