~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux-2.6.17/drivers/ieee1394/ieee1394_core.c

Version: ~ [ 2.6.16 ] ~ [ 2.6.17 ] ~
Architecture: ~ [ ia64 ] ~ [ i386 ] ~ [ arm ] ~ [ ppc ] ~ [ sparc64 ] ~

  1 /*
  2  * IEEE 1394 for Linux
  3  *
  4  * Core support: hpsb_packet management, packet handling and forwarding to
  5  *               highlevel or lowlevel code
  6  *
  7  * Copyright (C) 1999, 2000 Andreas E. Bombe
  8  *                     2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
  9  *
 10  * This code is licensed under the GPL.  See the file COPYING in the root
 11  * directory of the kernel sources for details.
 12  *
 13  *
 14  * Contributions:
 15  *
 16  * Manfred Weihs <weihs@ict.tuwien.ac.at>
 17  *        loopback functionality in hpsb_send_packet
 18  *        allow highlevel drivers to disable automatic response generation
 19  *              and to generate responses themselves (deferred)
 20  *
 21  */
 22 
 23 #include <linux/config.h>
 24 #include <linux/kernel.h>
 25 #include <linux/list.h>
 26 #include <linux/string.h>
 27 #include <linux/init.h>
 28 #include <linux/slab.h>
 29 #include <linux/interrupt.h>
 30 #include <linux/module.h>
 31 #include <linux/moduleparam.h>
 32 #include <linux/bitops.h>
 33 #include <linux/kdev_t.h>
 34 #include <linux/skbuff.h>
 35 #include <linux/suspend.h>
 36 
 37 #include <asm/byteorder.h>
 38 #include <asm/semaphore.h>
 39 
 40 #include "ieee1394_types.h"
 41 #include "ieee1394.h"
 42 #include "hosts.h"
 43 #include "ieee1394_core.h"
 44 #include "highlevel.h"
 45 #include "ieee1394_transactions.h"
 46 #include "csr.h"
 47 #include "nodemgr.h"
 48 #include "dma.h"
 49 #include "iso.h"
 50 #include "config_roms.h"
 51 
 52 /*
 53  * Disable the nodemgr detection and config rom reading functionality.
 54  */
 55 static int disable_nodemgr;
 56 module_param(disable_nodemgr, int, 0444);
 57 MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
 58 
 59 /* Disable Isochronous Resource Manager functionality */
 60 int hpsb_disable_irm = 0;
 61 module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
 62 MODULE_PARM_DESC(disable_irm,
 63                  "Disable Isochronous Resource Manager functionality.");
 64 
 65 /* We are GPL, so treat us special */
 66 MODULE_LICENSE("GPL");
 67 
 68 /* Some globals used */
 69 const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
 70 struct class *hpsb_protocol_class;
 71 
 72 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
 73 static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
 74 {
 75         int i;
 76 
 77         size /= 4;
 78         size = (size > 4 ? 4 : size);
 79 
 80         printk(KERN_DEBUG "ieee1394: %s", text);
 81         if (speed > -1 && speed < 6)
 82                 printk(" at %s", hpsb_speedto_str[speed]);
 83         printk(":");
 84         for (i = 0; i < size; i++)
 85                 printk(" %08x", data[i]);
 86         printk("\n");
 87 }
 88 #else
 89 #define dump_packet(a,b,c,d)
 90 #endif
 91 
 92 static void abort_requests(struct hpsb_host *host);
 93 static void queue_packet_complete(struct hpsb_packet *packet);
 94 
 95 
 96 /**
 97  * hpsb_set_packet_complete_task - set the task that runs when a packet
 98  * completes. You cannot call this more than once on a single packet
 99  * before it is sent.
100  *
101  * @packet: the packet whose completion we want the task added to
102  * @routine: function to call
103  * @data: data (if any) to pass to the above function
104  */
105 void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
106                                    void (*routine)(void *), void *data)
107 {
108         WARN_ON(packet->complete_routine != NULL);
109         packet->complete_routine = routine;
110         packet->complete_data = data;
111         return;
112 }
113 
114 /**
115  * hpsb_alloc_packet - allocate new packet structure
116  * @data_size: size of the data block to be allocated
117  *
118  * This function allocates, initializes and returns a new &struct hpsb_packet.
119  * It can be used in interrupt context.  A header block is always included, its
120  * size is big enough to contain all possible 1394 headers.  The data block is
121  * only allocated when @data_size is not zero.
122  *
123  * For packets for which responses will be received the @data_size has to be big
124  * enough to contain the response's data block since no further allocation
125  * occurs at response matching time.
126  *
127  * The packet's generation value will be set to the current generation number
128  * for ease of use.  Remember to overwrite it with your own recorded generation
129  * number if you can not be sure that your code will not race with a bus reset.
130  *
131  * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
132  * failure.
133  */
134 struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
135 {
136         struct hpsb_packet *packet = NULL;
137         struct sk_buff *skb;
138 
139         data_size = ((data_size + 3) & ~3);
140 
141         skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
142         if (skb == NULL)
143                 return NULL;
144 
145         memset(skb->data, 0, data_size + sizeof(*packet));
146 
147         packet = (struct hpsb_packet *)skb->data;
148         packet->skb = skb;
149 
150         packet->header = packet->embedded_header;
151         packet->state = hpsb_unused;
152         packet->generation = -1;
153         INIT_LIST_HEAD(&packet->driver_list);
154         atomic_set(&packet->refcnt, 1);
155 
156         if (data_size) {
157                 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
158                 packet->data_size = data_size;
159         }
160 
161         return packet;
162 }
163 
164 
165 /**
166  * hpsb_free_packet - free packet and data associated with it
167  * @packet: packet to free (is NULL safe)
168  *
169  * This function will free packet->data and finally the packet itself.
170  */
171 void hpsb_free_packet(struct hpsb_packet *packet)
172 {
173         if (packet && atomic_dec_and_test(&packet->refcnt)) {
174                 BUG_ON(!list_empty(&packet->driver_list));
175                 kfree_skb(packet->skb);
176         }
177 }
178 
179 
180 int hpsb_reset_bus(struct hpsb_host *host, int type)
181 {
182         if (!host->in_bus_reset) {
183                 host->driver->devctl(host, RESET_BUS, type);
184                 return 0;
185         } else {
186                 return 1;
187         }
188 }
189 
190 
191 int hpsb_bus_reset(struct hpsb_host *host)
192 {
193         if (host->in_bus_reset) {
194                 HPSB_NOTICE("%s called while bus reset already in progress",
195                             __FUNCTION__);
196                 return 1;
197         }
198 
199         abort_requests(host);
200         host->in_bus_reset = 1;
201         host->irm_id = -1;
202         host->is_irm = 0;
203         host->busmgr_id = -1;
204         host->is_busmgr = 0;
205         host->is_cycmst = 0;
206         host->node_count = 0;
207         host->selfid_count = 0;
208 
209         return 0;
210 }
211 
212 
213 /*
214  * Verify num_of_selfids SelfIDs and return number of nodes.  Return zero in
215  * case verification failed.
216  */
217 static int check_selfids(struct hpsb_host *host)
218 {
219         int nodeid = -1;
220         int rest_of_selfids = host->selfid_count;
221         struct selfid *sid = (struct selfid *)host->topology_map;
222         struct ext_selfid *esid;
223         int esid_seq = 23;
224 
225         host->nodes_active = 0;
226 
227         while (rest_of_selfids--) {
228                 if (!sid->extended) {
229                         nodeid++;
230                         esid_seq = 0;
231 
232                         if (sid->phy_id != nodeid) {
233                                 HPSB_INFO("SelfIDs failed monotony check with "
234                                           "%d", sid->phy_id);
235                                 return 0;
236                         }
237 
238                         if (sid->link_active) {
239                                 host->nodes_active++;
240                                 if (sid->contender)
241                                         host->irm_id = LOCAL_BUS | sid->phy_id;
242                         }
243                 } else {
244                         esid = (struct ext_selfid *)sid;
245 
246                         if ((esid->phy_id != nodeid)
247                             || (esid->seq_nr != esid_seq)) {
248                                 HPSB_INFO("SelfIDs failed monotony check with "
249                                           "%d/%d", esid->phy_id, esid->seq_nr);
250                                 return 0;
251                         }
252                         esid_seq++;
253                 }
254                 sid++;
255         }
256 
257         esid = (struct ext_selfid *)(sid - 1);
258         while (esid->extended) {
259                 if ((esid->porta == SELFID_PORT_PARENT) ||
260                     (esid->portb == SELFID_PORT_PARENT) ||
261                     (esid->portc == SELFID_PORT_PARENT) ||
262                     (esid->portd == SELFID_PORT_PARENT) ||
263                     (esid->porte == SELFID_PORT_PARENT) ||
264                     (esid->portf == SELFID_PORT_PARENT) ||
265                     (esid->portg == SELFID_PORT_PARENT) ||
266                     (esid->porth == SELFID_PORT_PARENT)) {
267                         HPSB_INFO("SelfIDs failed root check on "
268                                   "extended SelfID");
269                         return 0;
270                 }
271                 esid--;
272         }
273 
274         sid = (struct selfid *)esid;
275         if ((sid->port0 == SELFID_PORT_PARENT) ||
276             (sid->port1 == SELFID_PORT_PARENT) ||
277             (sid->port2 == SELFID_PORT_PARENT)) {
278                 HPSB_INFO("SelfIDs failed root check");
279                 return 0;
280         }
281 
282         host->node_count = nodeid + 1;
283         return 1;
284 }
285 
286 static void build_speed_map(struct hpsb_host *host, int nodecount)
287 {
288         u8 speedcap[nodecount];
289         u8 cldcnt[nodecount];
290         u8 *map = host->speed_map;
291         struct selfid *sid;
292         struct ext_selfid *esid;
293         int i, j, n;
294 
295         for (i = 0; i < (nodecount * 64); i += 64) {
296                 for (j = 0; j < nodecount; j++) {
297                         map[i+j] = IEEE1394_SPEED_MAX;
298                 }
299         }
300 
301         for (i = 0; i < nodecount; i++) {
302                 cldcnt[i] = 0;
303         }
304 
305         /* find direct children count and speed */
306         for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
307                      n = nodecount - 1;
308              (void *)sid >= (void *)host->topology_map; sid--) {
309                 if (sid->extended) {
310                         esid = (struct ext_selfid *)sid;
311 
312                         if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
313                         if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
314                         if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
315                         if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
316                         if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
317                         if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
318                         if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
319                         if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
320                 } else {
321                         if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
322                         if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
323                         if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
324 
325                         speedcap[n] = sid->speed;
326                         n--;
327                 }
328         }
329 
330         /* set self mapping */
331         for (i = 0; i < nodecount; i++) {
332                 map[64*i + i] = speedcap[i];
333         }
334 
335         /* fix up direct children count to total children count;
336          * also fix up speedcaps for sibling and parent communication */
337         for (i = 1; i < nodecount; i++) {
338                 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
339                         cldcnt[i] += cldcnt[n];
340                         speedcap[n] = min(speedcap[n], speedcap[i]);
341                         n -= cldcnt[n] + 1;
342                 }
343         }
344 
345         for (n = 0; n < nodecount; n++) {
346                 for (i = n - cldcnt[n]; i <= n; i++) {
347                         for (j = 0; j < (n - cldcnt[n]); j++) {
348                                 map[j*64 + i] = map[i*64 + j] =
349                                         min(map[i*64 + j], speedcap[n]);
350                         }
351                         for (j = n + 1; j < nodecount; j++) {
352                                 map[j*64 + i] = map[i*64 + j] =
353                                         min(map[i*64 + j], speedcap[n]);
354                         }
355                 }
356         }
357 }
358 
359 
360 void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
361 {
362         if (host->in_bus_reset) {
363                 HPSB_VERBOSE("Including SelfID 0x%x", sid);
364                 host->topology_map[host->selfid_count++] = sid;
365         } else {
366                 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
367                             sid, NODEID_TO_BUS(host->node_id));
368         }
369 }
370 
371 void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
372 {
373         if (!host->in_bus_reset)
374                 HPSB_NOTICE("SelfID completion called outside of bus reset!");
375 
376         host->node_id = LOCAL_BUS | phyid;
377         host->is_root = isroot;
378 
379         if (!check_selfids(host)) {
380                 if (host->reset_retries++ < 20) {
381                         /* selfid stage did not complete without error */
382                         HPSB_NOTICE("Error in SelfID stage, resetting");
383                         host->in_bus_reset = 0;
384                         /* this should work from ohci1394 now... */
385                         hpsb_reset_bus(host, LONG_RESET);
386                         return;
387                 } else {
388                         HPSB_NOTICE("Stopping out-of-control reset loop");
389                         HPSB_NOTICE("Warning - topology map and speed map will not be valid");
390                         host->reset_retries = 0;
391                 }
392         } else {
393                 host->reset_retries = 0;
394                 build_speed_map(host, host->node_count);
395         }
396 
397         HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
398                      "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
399 
400         /* irm_id is kept up to date by check_selfids() */
401         if (host->irm_id == host->node_id) {
402                 host->is_irm = 1;
403         } else {
404                 host->is_busmgr = 0;
405                 host->is_irm = 0;
406         }
407 
408         if (isroot) {
409                 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
410                 host->is_cycmst = 1;
411         }
412         atomic_inc(&host->generation);
413         host->in_bus_reset = 0;
414         highlevel_host_reset(host);
415 }
416 
417 
418 void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
419                       int ackcode)
420 {
421         unsigned long flags;
422 
423         spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
424 
425         packet->ack_code = ackcode;
426 
427         if (packet->no_waiter || packet->state == hpsb_complete) {
428                 /* if packet->no_waiter, must not have a tlabel allocated */
429                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
430                 hpsb_free_packet(packet);
431                 return;
432         }
433 
434         atomic_dec(&packet->refcnt);    /* drop HC's reference */
435         /* here the packet must be on the host->pending_packet_queue */
436 
437         if (ackcode != ACK_PENDING || !packet->expect_response) {
438                 packet->state = hpsb_complete;
439                 __skb_unlink(packet->skb, &host->pending_packet_queue);
440                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
441                 queue_packet_complete(packet);
442                 return;
443         }
444 
445         packet->state = hpsb_pending;
446         packet->sendtime = jiffies;
447 
448         spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
449 
450         mod_timer(&host->timeout, jiffies + host->timeout_interval);
451 }
452 
453 /**
454  * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
455  * @host: host that PHY config packet gets sent through
456  * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
457  * @gapcnt: gap count value to set (-1 = don't set gap count)
458  *
459  * This function sends a PHY config packet on the bus through the specified host.
460  *
461  * Return value: 0 for success or error number otherwise.
462  */
463 int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
464 {
465         struct hpsb_packet *packet;
466         quadlet_t d = 0;
467         int retval = 0;
468 
469         if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
470            (rootid == -1 && gapcnt == -1)) {
471                 HPSB_DEBUG("Invalid Parameter: rootid = %d   gapcnt = %d",
472                            rootid, gapcnt);
473                 return -EINVAL;
474         }
475 
476         if (rootid != -1)
477                 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
478         if (gapcnt != -1)
479                 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
480 
481         packet = hpsb_make_phypacket(host, d);
482         if (!packet)
483                 return -ENOMEM;
484 
485         packet->generation = get_hpsb_generation(host);
486         retval = hpsb_send_packet_and_wait(packet);
487         hpsb_free_packet(packet);
488 
489         return retval;
490 }
491 
492 /**
493  * hpsb_send_packet - transmit a packet on the bus
494  * @packet: packet to send
495  *
496  * The packet is sent through the host specified in the packet->host field.
497  * Before sending, the packet's transmit speed is automatically determined
498  * using the local speed map when it is an async, non-broadcast packet.
499  *
500  * Possibilities for failure are that host is either not initialized, in bus
501  * reset, the packet's generation number doesn't match the current generation
502  * number or the host reports a transmit error.
503  *
504  * Return value: 0 on success, negative errno on failure.
505  */
506 int hpsb_send_packet(struct hpsb_packet *packet)
507 {
508         struct hpsb_host *host = packet->host;
509 
510         if (host->is_shutdown)
511                 return -EINVAL;
512         if (host->in_bus_reset ||
513             (packet->generation != get_hpsb_generation(host)))
514                 return -EAGAIN;
515 
516         packet->state = hpsb_queued;
517 
518         /* This just seems silly to me */
519         WARN_ON(packet->no_waiter && packet->expect_response);
520 
521         if (!packet->no_waiter || packet->expect_response) {
522                 atomic_inc(&packet->refcnt);
523                 /* Set the initial "sendtime" to 10 seconds from now, to
524                    prevent premature expiry.  If a packet takes more than
525                    10 seconds to hit the wire, we have bigger problems :) */
526                 packet->sendtime = jiffies + 10 * HZ;
527                 skb_queue_tail(&host->pending_packet_queue, packet->skb);
528         }
529 
530         if (packet->node_id == host->node_id) {
531                 /* it is a local request, so handle it locally */
532 
533                 quadlet_t *data;
534                 size_t size = packet->data_size + packet->header_size;
535 
536                 data = kmalloc(size, GFP_ATOMIC);
537                 if (!data) {
538                         HPSB_ERR("unable to allocate memory for concatenating header and data");
539                         return -ENOMEM;
540                 }
541 
542                 memcpy(data, packet->header, packet->header_size);
543 
544                 if (packet->data_size)
545                         memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
546 
547                 dump_packet("send packet local", packet->header, packet->header_size, -1);
548 
549                 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
550                 hpsb_packet_received(host, data, size, 0);
551 
552                 kfree(data);
553 
554                 return 0;
555         }
556 
557         if (packet->type == hpsb_async && packet->node_id != ALL_NODES) {
558                 packet->speed_code =
559                         host->speed_map[NODEID_TO_NODE(host->node_id) * 64
560                                        + NODEID_TO_NODE(packet->node_id)];
561         }
562 
563         dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
564 
565         return host->driver->transmit_packet(host, packet);
566 }
567 
568 /* We could just use complete() directly as the packet complete
569  * callback, but this is more typesafe, in the sense that we get a
570  * compiler error if the prototype for complete() changes. */
571 
572 static void complete_packet(void *data)
573 {
574         complete((struct completion *) data);
575 }
576 
577 int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
578 {
579         struct completion done;
580         int retval;
581 
582         init_completion(&done);
583         hpsb_set_packet_complete_task(packet, complete_packet, &done);
584         retval = hpsb_send_packet(packet);
585         if (retval == 0)
586                 wait_for_completion(&done);
587 
588         return retval;
589 }
590 
591 static void send_packet_nocare(struct hpsb_packet *packet)
592 {
593         if (hpsb_send_packet(packet) < 0) {
594                 hpsb_free_packet(packet);
595         }
596 }
597 
598 
599 static void handle_packet_response(struct hpsb_host *host, int tcode,
600                                    quadlet_t *data, size_t size)
601 {
602         struct hpsb_packet *packet = NULL;
603         struct sk_buff *skb;
604         int tcode_match = 0;
605         int tlabel;
606         unsigned long flags;
607 
608         tlabel = (data[0] >> 10) & 0x3f;
609 
610         spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
611 
612         skb_queue_walk(&host->pending_packet_queue, skb) {
613                 packet = (struct hpsb_packet *)skb->data;
614                 if ((packet->tlabel == tlabel)
615                     && (packet->node_id == (data[1] >> 16))){
616                         break;
617                 }
618 
619                 packet = NULL;
620         }
621 
622         if (packet == NULL) {
623                 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
624                 dump_packet("contents", data, 16, -1);
625                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
626                 return;
627         }
628 
629         switch (packet->tcode) {
630         case TCODE_WRITEQ:
631         case TCODE_WRITEB:
632                 if (tcode != TCODE_WRITE_RESPONSE)
633                         break;
634                 tcode_match = 1;
635                 memcpy(packet->header, data, 12);
636                 break;
637         case TCODE_READQ:
638                 if (tcode != TCODE_READQ_RESPONSE)
639                         break;
640                 tcode_match = 1;
641                 memcpy(packet->header, data, 16);
642                 break;
643         case TCODE_READB:
644                 if (tcode != TCODE_READB_RESPONSE)
645                         break;
646                 tcode_match = 1;
647                 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
648                 memcpy(packet->header, data, 16);
649                 memcpy(packet->data, data + 4, size - 16);
650                 break;
651         case TCODE_LOCK_REQUEST:
652                 if (tcode != TCODE_LOCK_RESPONSE)
653                         break;
654                 tcode_match = 1;
655                 size = min((size - 16), (size_t)8);
656                 BUG_ON(packet->skb->len - sizeof(*packet) < size);
657                 memcpy(packet->header, data, 16);
658                 memcpy(packet->data, data + 4, size);
659                 break;
660         }
661 
662         if (!tcode_match) {
663                 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
664                 HPSB_INFO("unsolicited response packet received - tcode mismatch");
665                 dump_packet("contents", data, 16, -1);
666                 return;
667         }
668 
669         __skb_unlink(skb, &host->pending_packet_queue);
670 
671         if (packet->state == hpsb_queued) {
672                 packet->sendtime = jiffies;
673                 packet->ack_code = ACK_PENDING;
674         }
675 
676         packet->state = hpsb_complete;
677         spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
678 
679         queue_packet_complete(packet);
680 }
681 
682 
683 static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
684                                                quadlet_t *data, size_t dsize)
685 {
686         struct hpsb_packet *p;
687 
688         p = hpsb_alloc_packet(dsize);
689         if (unlikely(p == NULL)) {
690                 /* FIXME - send data_error response */
691                 return NULL;
692         }
693 
694         p->type = hpsb_async;
695         p->state = hpsb_unused;
696         p->host = host;
697         p->node_id = data[1] >> 16;
698         p->tlabel = (data[0] >> 10) & 0x3f;
699         p->no_waiter = 1;
700 
701         p->generation = get_hpsb_generation(host);
702 
703         if (dsize % 4)
704                 p->data[dsize / 4] = 0;
705 
706         return p;
707 }
708 
709 #define PREP_ASYNC_HEAD_RCODE(tc) \
710         packet->tcode = tc; \
711         packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
712                 | (1 << 8) | (tc << 4); \
713         packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
714         packet->header[2] = 0
715 
716 static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
717                               quadlet_t data)
718 {
719         PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
720         packet->header[3] = data;
721         packet->header_size = 16;
722         packet->data_size = 0;
723 }
724 
725 static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
726                                int length)
727 {
728         if (rcode != RCODE_COMPLETE)
729                 length = 0;
730 
731         PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
732         packet->header[3] = length << 16;
733         packet->header_size = 16;
734         packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
735 }
736 
737 static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
738 {
739         PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
740         packet->header[2] = 0;
741         packet->header_size = 12;
742         packet->data_size = 0;
743 }
744 
745 static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
746                           int length)
747 {
748         if (rcode != RCODE_COMPLETE)
749                 length = 0;
750 
751         PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
752         packet->header[3] = (length << 16) | extcode;
753         packet->header_size = 16;
754         packet->data_size = length;
755 }
756 
757 #define PREP_REPLY_PACKET(length) \
758                 packet = create_reply_packet(host, data, length); \
759                 if (packet == NULL) break
760 
761 static void handle_incoming_packet(struct hpsb_host *host, int tcode,
762                                    quadlet_t *data, size_t size, int write_acked)
763 {
764         struct hpsb_packet *packet;
765         int length, rcode, extcode;
766         quadlet_t buffer;
767         nodeid_t source = data[1] >> 16;
768         nodeid_t dest = data[0] >> 16;
769         u16 flags = (u16) data[0];
770         u64 addr;
771 
772         /* big FIXME - no error checking is done for an out of bounds length */
773 
774         switch (tcode) {
775         case TCODE_WRITEQ:
776                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
777                 rcode = highlevel_write(host, source, dest, data+3,
778                                         addr, 4, flags);
779 
780                 if (!write_acked
781                     && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
782                     && (rcode >= 0)) {
783                         /* not a broadcast write, reply */
784                         PREP_REPLY_PACKET(0);
785                         fill_async_write_resp(packet, rcode);
786                         send_packet_nocare(packet);
787                 }
788                 break;
789 
790         case TCODE_WRITEB:
791                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
792                 rcode = highlevel_write(host, source, dest, data+4,
793                                         addr, data[3]>>16, flags);
794 
795                 if (!write_acked
796                     && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
797                     && (rcode >= 0)) {
798                         /* not a broadcast write, reply */
799                         PREP_REPLY_PACKET(0);
800                         fill_async_write_resp(packet, rcode);
801                         send_packet_nocare(packet);
802                 }
803                 break;
804 
805         case TCODE_READQ:
806                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
807                 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
808 
809                 if (rcode >= 0) {
810                         PREP_REPLY_PACKET(0);
811                         fill_async_readquad_resp(packet, rcode, buffer);
812                         send_packet_nocare(packet);
813                 }
814                 break;
815 
816         case TCODE_READB:
817                 length = data[3] >> 16;
818                 PREP_REPLY_PACKET(length);
819 
820                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
821                 rcode = highlevel_read(host, source, packet->data, addr,
822                                        length, flags);
823 
824                 if (rcode >= 0) {
825                         fill_async_readblock_resp(packet, rcode, length);
826                         send_packet_nocare(packet);
827                 } else {
828                         hpsb_free_packet(packet);
829                 }
830                 break;
831 
832         case TCODE_LOCK_REQUEST:
833                 length = data[3] >> 16;
834                 extcode = data[3] & 0xffff;
835                 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
836 
837                 PREP_REPLY_PACKET(8);
838 
839                 if ((extcode == 0) || (extcode >= 7)) {
840                         /* let switch default handle error */
841                         length = 0;
842                 }
843 
844                 switch (length) {
845                 case 4:
846                         rcode = highlevel_lock(host, source, packet->data, addr,
847                                                data[4], 0, extcode,flags);
848                         fill_async_lock_resp(packet, rcode, extcode, 4);
849                         break;
850                 case 8:
851                         if ((extcode != EXTCODE_FETCH_ADD)
852                             && (extcode != EXTCODE_LITTLE_ADD)) {
853                                 rcode = highlevel_lock(host, source,
854                                                        packet->data, addr,
855                                                        data[5], data[4],
856                                                        extcode, flags);
857                                 fill_async_lock_resp(packet, rcode, extcode, 4);
858                         } else {
859                                 rcode = highlevel_lock64(host, source,
860                                              (octlet_t *)packet->data, addr,
861                                              *(octlet_t *)(data + 4), 0ULL,
862                                              extcode, flags);
863                                 fill_async_lock_resp(packet, rcode, extcode, 8);
864                         }
865                         break;
866                 case 16:
867                         rcode = highlevel_lock64(host, source,
868                                                  (octlet_t *)packet->data, addr,
869                                                  *(octlet_t *)(data + 6),
870                                                  *(octlet_t *)(data + 4),
871                                                  extcode, flags);
872                         fill_async_lock_resp(packet, rcode, extcode, 8);
873                         break;
874                 default:
875                         rcode = RCODE_TYPE_ERROR;
876                         fill_async_lock_resp(packet, rcode,
877                                              extcode, 0);
878                 }
879 
880                 if (rcode >= 0) {
881                         send_packet_nocare(packet);
882                 } else {
883                         hpsb_free_packet(packet);
884                 }
885                 break;
886         }
887 
888 }
889 #undef PREP_REPLY_PACKET
890 
891 
892 void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
893                           int write_acked)
894 {
895         int tcode;
896 
897         if (host->in_bus_reset) {
898                 HPSB_INFO("received packet during reset; ignoring");
899                 return;
900         }
901 
902         dump_packet("received packet", data, size, -1);
903 
904         tcode = (data[0] >> 4) & 0xf;
905