blob: 49354de9fb8a6cd31702977c10b896d2d11fd09e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ben Collinsf6542402006-06-12 18:15:50 -040036#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/byteorder.h>
39#include <asm/semaphore.h>
40
41#include "ieee1394_types.h"
42#include "ieee1394.h"
43#include "hosts.h"
44#include "ieee1394_core.h"
45#include "highlevel.h"
46#include "ieee1394_transactions.h"
47#include "csr.h"
48#include "nodemgr.h"
49#include "dma.h"
50#include "iso.h"
51#include "config_roms.h"
52
53/*
54 * Disable the nodemgr detection and config rom reading functionality.
55 */
Ben Collins1934b8b2005-07-09 20:01:23 -040056static int disable_nodemgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057module_param(disable_nodemgr, int, 0444);
58MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
59
60/* Disable Isochronous Resource Manager functionality */
61int hpsb_disable_irm = 0;
Stefan Richter23e93f12006-03-28 20:03:34 -050062module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_PARM_DESC(disable_irm,
64 "Disable Isochronous Resource Manager functionality.");
65
66/* We are GPL, so treat us special */
67MODULE_LICENSE("GPL");
68
69/* Some globals used */
70const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
gregkh@suse.de7e25ab92005-03-23 09:53:36 -080071struct class *hpsb_protocol_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#ifdef CONFIG_IEEE1394_VERBOSEDEBUG
Jody McIntyredb2fd662005-09-30 11:59:09 -070074static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i;
77
78 size /= 4;
79 size = (size > 4 ? 4 : size);
80
81 printk(KERN_DEBUG "ieee1394: %s", text);
Jody McIntyredb2fd662005-09-30 11:59:09 -070082 if (speed > -1 && speed < 6)
83 printk(" at %s", hpsb_speedto_str[speed]);
84 printk(":");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 for (i = 0; i < size; i++)
86 printk(" %08x", data[i]);
87 printk("\n");
88}
89#else
Jody McIntyredb2fd662005-09-30 11:59:09 -070090#define dump_packet(a,b,c,d)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
92
93static void abort_requests(struct hpsb_host *host);
94static void queue_packet_complete(struct hpsb_packet *packet);
95
96
97/**
98 * hpsb_set_packet_complete_task - set the task that runs when a packet
99 * completes. You cannot call this more than once on a single packet
100 * before it is sent.
101 *
102 * @packet: the packet whose completion we want the task added to
103 * @routine: function to call
104 * @data: data (if any) to pass to the above function
105 */
106void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
107 void (*routine)(void *), void *data)
108{
109 WARN_ON(packet->complete_routine != NULL);
110 packet->complete_routine = routine;
111 packet->complete_data = data;
112 return;
113}
114
115/**
116 * hpsb_alloc_packet - allocate new packet structure
117 * @data_size: size of the data block to be allocated
118 *
119 * This function allocates, initializes and returns a new &struct hpsb_packet.
120 * It can be used in interrupt context. A header block is always included, its
121 * size is big enough to contain all possible 1394 headers. The data block is
122 * only allocated when @data_size is not zero.
123 *
124 * For packets for which responses will be received the @data_size has to be big
125 * enough to contain the response's data block since no further allocation
126 * occurs at response matching time.
127 *
128 * The packet's generation value will be set to the current generation number
129 * for ease of use. Remember to overwrite it with your own recorded generation
130 * number if you can not be sure that your code will not race with a bus reset.
131 *
132 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
133 * failure.
134 */
135struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
136{
137 struct hpsb_packet *packet = NULL;
138 struct sk_buff *skb;
139
140 data_size = ((data_size + 3) & ~3);
141
142 skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
143 if (skb == NULL)
144 return NULL;
145
146 memset(skb->data, 0, data_size + sizeof(*packet));
147
148 packet = (struct hpsb_packet *)skb->data;
149 packet->skb = skb;
150
151 packet->header = packet->embedded_header;
152 packet->state = hpsb_unused;
153 packet->generation = -1;
154 INIT_LIST_HEAD(&packet->driver_list);
155 atomic_set(&packet->refcnt, 1);
156
157 if (data_size) {
158 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
159 packet->data_size = data_size;
160 }
161
162 return packet;
163}
164
165
166/**
167 * hpsb_free_packet - free packet and data associated with it
168 * @packet: packet to free (is NULL safe)
169 *
170 * This function will free packet->data and finally the packet itself.
171 */
172void hpsb_free_packet(struct hpsb_packet *packet)
173{
174 if (packet && atomic_dec_and_test(&packet->refcnt)) {
175 BUG_ON(!list_empty(&packet->driver_list));
176 kfree_skb(packet->skb);
177 }
178}
179
180
181int hpsb_reset_bus(struct hpsb_host *host, int type)
182{
Stefan Richter741854e2005-12-01 18:52:03 -0500183 if (!host->in_bus_reset) {
184 host->driver->devctl(host, RESET_BUS, type);
185 return 0;
186 } else {
187 return 1;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191
192int hpsb_bus_reset(struct hpsb_host *host)
193{
Stefan Richter741854e2005-12-01 18:52:03 -0500194 if (host->in_bus_reset) {
195 HPSB_NOTICE("%s called while bus reset already in progress",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 __FUNCTION__);
Stefan Richter741854e2005-12-01 18:52:03 -0500197 return 1;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Stefan Richter741854e2005-12-01 18:52:03 -0500200 abort_requests(host);
201 host->in_bus_reset = 1;
202 host->irm_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 host->is_irm = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500204 host->busmgr_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 host->is_busmgr = 0;
206 host->is_cycmst = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500207 host->node_count = 0;
208 host->selfid_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Stefan Richter741854e2005-12-01 18:52:03 -0500210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213
214/*
215 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
216 * case verification failed.
217 */
218static int check_selfids(struct hpsb_host *host)
219{
Stefan Richter741854e2005-12-01 18:52:03 -0500220 int nodeid = -1;
221 int rest_of_selfids = host->selfid_count;
222 struct selfid *sid = (struct selfid *)host->topology_map;
223 struct ext_selfid *esid;
224 int esid_seq = 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 host->nodes_active = 0;
227
Stefan Richter741854e2005-12-01 18:52:03 -0500228 while (rest_of_selfids--) {
229 if (!sid->extended) {
230 nodeid++;
231 esid_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Stefan Richter741854e2005-12-01 18:52:03 -0500233 if (sid->phy_id != nodeid) {
234 HPSB_INFO("SelfIDs failed monotony check with "
235 "%d", sid->phy_id);
236 return 0;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (sid->link_active) {
240 host->nodes_active++;
241 if (sid->contender)
242 host->irm_id = LOCAL_BUS | sid->phy_id;
243 }
Stefan Richter741854e2005-12-01 18:52:03 -0500244 } else {
245 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Stefan Richter741854e2005-12-01 18:52:03 -0500247 if ((esid->phy_id != nodeid)
248 || (esid->seq_nr != esid_seq)) {
249 HPSB_INFO("SelfIDs failed monotony check with "
250 "%d/%d", esid->phy_id, esid->seq_nr);
251 return 0;
252 }
253 esid_seq++;
254 }
255 sid++;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Stefan Richter741854e2005-12-01 18:52:03 -0500258 esid = (struct ext_selfid *)(sid - 1);
259 while (esid->extended) {
260 if ((esid->porta == SELFID_PORT_PARENT) ||
Stefan Richterd7758462005-12-01 18:51:56 -0500261 (esid->portb == SELFID_PORT_PARENT) ||
262 (esid->portc == SELFID_PORT_PARENT) ||
263 (esid->portd == SELFID_PORT_PARENT) ||
264 (esid->porte == SELFID_PORT_PARENT) ||
265 (esid->portf == SELFID_PORT_PARENT) ||
266 (esid->portg == SELFID_PORT_PARENT) ||
267 (esid->porth == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 HPSB_INFO("SelfIDs failed root check on "
269 "extended SelfID");
270 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500271 }
272 esid--;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Stefan Richter741854e2005-12-01 18:52:03 -0500275 sid = (struct selfid *)esid;
Stefan Richterd7758462005-12-01 18:51:56 -0500276 if ((sid->port0 == SELFID_PORT_PARENT) ||
277 (sid->port1 == SELFID_PORT_PARENT) ||
278 (sid->port2 == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 HPSB_INFO("SelfIDs failed root check");
280 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 host->node_count = nodeid + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500284 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static void build_speed_map(struct hpsb_host *host, int nodecount)
288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 u8 cldcnt[nodecount];
Stefan Richter741854e2005-12-01 18:52:03 -0500290 u8 *map = host->speed_map;
Ben Collins647dcb52006-06-12 18:12:37 -0400291 u8 *speedcap = host->speed;
Stefan Richter741854e2005-12-01 18:52:03 -0500292 struct selfid *sid;
293 struct ext_selfid *esid;
294 int i, j, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Stefan Richter741854e2005-12-01 18:52:03 -0500296 for (i = 0; i < (nodecount * 64); i += 64) {
297 for (j = 0; j < nodecount; j++) {
298 map[i+j] = IEEE1394_SPEED_MAX;
299 }
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Stefan Richter741854e2005-12-01 18:52:03 -0500302 for (i = 0; i < nodecount; i++) {
303 cldcnt[i] = 0;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Stefan Richter741854e2005-12-01 18:52:03 -0500306 /* find direct children count and speed */
307 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
308 n = nodecount - 1;
309 (void *)sid >= (void *)host->topology_map; sid--) {
310 if (sid->extended) {
311 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Stefan Richterd7758462005-12-01 18:51:56 -0500313 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
314 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
315 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
316 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
317 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
318 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
319 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
320 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 } else {
Stefan Richterd7758462005-12-01 18:51:56 -0500322 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
323 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
324 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Stefan Richter741854e2005-12-01 18:52:03 -0500326 speedcap[n] = sid->speed;
327 n--;
328 }
329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Stefan Richter741854e2005-12-01 18:52:03 -0500331 /* set self mapping */
332 for (i = 0; i < nodecount; i++) {
333 map[64*i + i] = speedcap[i];
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Stefan Richter741854e2005-12-01 18:52:03 -0500336 /* fix up direct children count to total children count;
337 * also fix up speedcaps for sibling and parent communication */
338 for (i = 1; i < nodecount; i++) {
339 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
340 cldcnt[i] += cldcnt[n];
341 speedcap[n] = min(speedcap[n], speedcap[i]);
342 n -= cldcnt[n] + 1;
343 }
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Stefan Richter741854e2005-12-01 18:52:03 -0500346 for (n = 0; n < nodecount; n++) {
347 for (i = n - cldcnt[n]; i <= n; i++) {
348 for (j = 0; j < (n - cldcnt[n]); j++) {
349 map[j*64 + i] = map[i*64 + j] =
350 min(map[i*64 + j], speedcap[n]);
351 }
352 for (j = n + 1; j < nodecount; j++) {
353 map[j*64 + i] = map[i*64 + j] =
354 min(map[i*64 + j], speedcap[n]);
355 }
356 }
357 }
Ben Collins647dcb52006-06-12 18:12:37 -0400358
359 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
360 for (n = 0; n < nodecount; n++)
361 if (speedcap[n] == 3)
362 speedcap[n] = IEEE1394_SPEED_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365
366void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
367{
Stefan Richter741854e2005-12-01 18:52:03 -0500368 if (host->in_bus_reset) {
369 HPSB_VERBOSE("Including SelfID 0x%x", sid);
370 host->topology_map[host->selfid_count++] = sid;
371 } else {
372 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 sid, NODEID_TO_BUS(host->node_id));
Stefan Richter741854e2005-12-01 18:52:03 -0500374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
378{
379 if (!host->in_bus_reset)
380 HPSB_NOTICE("SelfID completion called outside of bus reset!");
381
Stefan Richter741854e2005-12-01 18:52:03 -0500382 host->node_id = LOCAL_BUS | phyid;
383 host->is_root = isroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Stefan Richter741854e2005-12-01 18:52:03 -0500385 if (!check_selfids(host)) {
386 if (host->reset_retries++ < 20) {
387 /* selfid stage did not complete without error */
388 HPSB_NOTICE("Error in SelfID stage, resetting");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 host->in_bus_reset = 0;
390 /* this should work from ohci1394 now... */
Stefan Richter741854e2005-12-01 18:52:03 -0500391 hpsb_reset_bus(host, LONG_RESET);
392 return;
393 } else {
394 HPSB_NOTICE("Stopping out-of-control reset loop");
395 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500397 }
398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500400 build_speed_map(host, host->node_count);
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
404 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
405
Stefan Richter741854e2005-12-01 18:52:03 -0500406 /* irm_id is kept up to date by check_selfids() */
407 if (host->irm_id == host->node_id) {
408 host->is_irm = 1;
409 } else {
410 host->is_busmgr = 0;
411 host->is_irm = 0;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Stefan Richter741854e2005-12-01 18:52:03 -0500414 if (isroot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
416 host->is_cycmst = 1;
417 }
418 atomic_inc(&host->generation);
419 host->in_bus_reset = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500420 highlevel_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423
424void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
Stefan Richter741854e2005-12-01 18:52:03 -0500425 int ackcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 unsigned long flags;
428
429 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
430
431 packet->ack_code = ackcode;
432
433 if (packet->no_waiter || packet->state == hpsb_complete) {
434 /* if packet->no_waiter, must not have a tlabel allocated */
435 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
436 hpsb_free_packet(packet);
437 return;
438 }
439
440 atomic_dec(&packet->refcnt); /* drop HC's reference */
441 /* here the packet must be on the host->pending_packet_queue */
442
443 if (ackcode != ACK_PENDING || !packet->expect_response) {
444 packet->state = hpsb_complete;
445 __skb_unlink(packet->skb, &host->pending_packet_queue);
446 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
447 queue_packet_complete(packet);
448 return;
449 }
450
451 packet->state = hpsb_pending;
452 packet->sendtime = jiffies;
453
454 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
455
456 mod_timer(&host->timeout, jiffies + host->timeout_interval);
457}
458
459/**
460 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
461 * @host: host that PHY config packet gets sent through
462 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
463 * @gapcnt: gap count value to set (-1 = don't set gap count)
464 *
465 * This function sends a PHY config packet on the bus through the specified host.
466 *
467 * Return value: 0 for success or error number otherwise.
468 */
469int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
470{
471 struct hpsb_packet *packet;
Stefan Richter546513f2005-12-01 18:52:01 -0500472 quadlet_t d = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int retval = 0;
474
475 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
476 (rootid == -1 && gapcnt == -1)) {
477 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
478 rootid, gapcnt);
479 return -EINVAL;
480 }
481
Stefan Richter546513f2005-12-01 18:52:01 -0500482 if (rootid != -1)
483 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
484 if (gapcnt != -1)
485 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
486
487 packet = hpsb_make_phypacket(host, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!packet)
489 return -ENOMEM;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 packet->generation = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 retval = hpsb_send_packet_and_wait(packet);
493 hpsb_free_packet(packet);
494
495 return retval;
496}
497
498/**
499 * hpsb_send_packet - transmit a packet on the bus
500 * @packet: packet to send
501 *
502 * The packet is sent through the host specified in the packet->host field.
503 * Before sending, the packet's transmit speed is automatically determined
504 * using the local speed map when it is an async, non-broadcast packet.
505 *
506 * Possibilities for failure are that host is either not initialized, in bus
507 * reset, the packet's generation number doesn't match the current generation
508 * number or the host reports a transmit error.
509 *
510 * Return value: 0 on success, negative errno on failure.
511 */
512int hpsb_send_packet(struct hpsb_packet *packet)
513{
514 struct hpsb_host *host = packet->host;
515
Stefan Richter741854e2005-12-01 18:52:03 -0500516 if (host->is_shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EINVAL;
518 if (host->in_bus_reset ||
519 (packet->generation != get_hpsb_generation(host)))
Stefan Richter741854e2005-12-01 18:52:03 -0500520 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Stefan Richter741854e2005-12-01 18:52:03 -0500522 packet->state = hpsb_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* This just seems silly to me */
525 WARN_ON(packet->no_waiter && packet->expect_response);
526
527 if (!packet->no_waiter || packet->expect_response) {
528 atomic_inc(&packet->refcnt);
Ben Collins1934b8b2005-07-09 20:01:23 -0400529 /* Set the initial "sendtime" to 10 seconds from now, to
530 prevent premature expiry. If a packet takes more than
531 10 seconds to hit the wire, we have bigger problems :) */
Jody McIntyre6262d062005-05-16 21:54:05 -0700532 packet->sendtime = jiffies + 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 skb_queue_tail(&host->pending_packet_queue, packet->skb);
534 }
535
Stefan Richter741854e2005-12-01 18:52:03 -0500536 if (packet->node_id == host->node_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* it is a local request, so handle it locally */
538
Stefan Richter741854e2005-12-01 18:52:03 -0500539 quadlet_t *data;
540 size_t size = packet->data_size + packet->header_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Stefan Richter741854e2005-12-01 18:52:03 -0500542 data = kmalloc(size, GFP_ATOMIC);
543 if (!data) {
544 HPSB_ERR("unable to allocate memory for concatenating header and data");
545 return -ENOMEM;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Stefan Richter741854e2005-12-01 18:52:03 -0500548 memcpy(data, packet->header, packet->header_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Stefan Richter741854e2005-12-01 18:52:03 -0500550 if (packet->data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
552
Stefan Richter741854e2005-12-01 18:52:03 -0500553 dump_packet("send packet local", packet->header, packet->header_size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Stefan Richter741854e2005-12-01 18:52:03 -0500555 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
556 hpsb_packet_received(host, data, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Stefan Richter741854e2005-12-01 18:52:03 -0500558 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Stefan Richter741854e2005-12-01 18:52:03 -0500560 return 0;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Ben Collins647dcb52006-06-12 18:12:37 -0400563 if (packet->type == hpsb_async &&
564 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
Stefan Richter741854e2005-12-01 18:52:03 -0500565 packet->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -0400566 host->speed[NODEID_TO_NODE(packet->node_id)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Stefan Richter741854e2005-12-01 18:52:03 -0500568 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Stefan Richter741854e2005-12-01 18:52:03 -0500570 return host->driver->transmit_packet(host, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/* We could just use complete() directly as the packet complete
574 * callback, but this is more typesafe, in the sense that we get a
575 * compiler error if the prototype for complete() changes. */
576
577static void complete_packet(void *data)
578{
579 complete((struct completion *) data);
580}
581
582int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
583{
584 struct completion done;
585 int retval;
586
587 init_completion(&done);
588 hpsb_set_packet_complete_task(packet, complete_packet, &done);
589 retval = hpsb_send_packet(packet);
590 if (retval == 0)
591 wait_for_completion(&done);
592
593 return retval;
594}
595
596static void send_packet_nocare(struct hpsb_packet *packet)
597{
Stefan Richter741854e2005-12-01 18:52:03 -0500598 if (hpsb_send_packet(packet) < 0) {
599 hpsb_free_packet(packet);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603
604static void handle_packet_response(struct hpsb_host *host, int tcode,
605 quadlet_t *data, size_t size)
606{
Stefan Richter741854e2005-12-01 18:52:03 -0500607 struct hpsb_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct sk_buff *skb;
Stefan Richter741854e2005-12-01 18:52:03 -0500609 int tcode_match = 0;
610 int tlabel;
611 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Stefan Richter741854e2005-12-01 18:52:03 -0500613 tlabel = (data[0] >> 10) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
616
617 skb_queue_walk(&host->pending_packet_queue, skb) {
618 packet = (struct hpsb_packet *)skb->data;
Stefan Richter741854e2005-12-01 18:52:03 -0500619 if ((packet->tlabel == tlabel)
620 && (packet->node_id == (data[1] >> 16))){
621 break;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 packet = NULL;
Stefan Richter741854e2005-12-01 18:52:03 -0500625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (packet == NULL) {
Stefan Richter741854e2005-12-01 18:52:03 -0500628 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
629 dump_packet("contents", data, 16, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500631 return;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Stefan Richter741854e2005-12-01 18:52:03 -0500634 switch (packet->tcode) {
635 case TCODE_WRITEQ:
636 case TCODE_WRITEB:
637 if (tcode != TCODE_WRITE_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639 tcode_match = 1;
640 memcpy(packet->header, data, 12);
Stefan Richter741854e2005-12-01 18:52:03 -0500641 break;
642 case TCODE_READQ:
643 if (tcode != TCODE_READQ_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645 tcode_match = 1;
646 memcpy(packet->header, data, 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500647 break;
648 case TCODE_READB:
649 if (tcode != TCODE_READB_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651 tcode_match = 1;
652 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
653 memcpy(packet->header, data, 16);
654 memcpy(packet->data, data + 4, size - 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500655 break;
656 case TCODE_LOCK_REQUEST:
657 if (tcode != TCODE_LOCK_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 tcode_match = 1;
660 size = min((size - 16), (size_t)8);
661 BUG_ON(packet->skb->len - sizeof(*packet) < size);
662 memcpy(packet->header, data, 16);
663 memcpy(packet->data, data + 4, size);
Stefan Richter741854e2005-12-01 18:52:03 -0500664 break;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Stefan Richter741854e2005-12-01 18:52:03 -0500667 if (!tcode_match) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500669 HPSB_INFO("unsolicited response packet received - tcode mismatch");
670 dump_packet("contents", data, 16, -1);
671 return;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David S. Miller8728b832005-08-09 19:25:21 -0700674 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (packet->state == hpsb_queued) {
677 packet->sendtime = jiffies;
678 packet->ack_code = ACK_PENDING;
679 }
680
681 packet->state = hpsb_complete;
682 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
683
684 queue_packet_complete(packet);
685}
686
687
688static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
689 quadlet_t *data, size_t dsize)
690{
Stefan Richter741854e2005-12-01 18:52:03 -0500691 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Stefan Richter741854e2005-12-01 18:52:03 -0500693 p = hpsb_alloc_packet(dsize);
694 if (unlikely(p == NULL)) {
695 /* FIXME - send data_error response */
696 return NULL;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Stefan Richter741854e2005-12-01 18:52:03 -0500699 p->type = hpsb_async;
700 p->state = hpsb_unused;
701 p->host = host;
702 p->node_id = data[1] >> 16;
703 p->tlabel = (data[0] >> 10) & 0x3f;
704 p->no_waiter = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 p->generation = get_hpsb_generation(host);
707
708 if (dsize % 4)
709 p->data[dsize / 4] = 0;
710
Stefan Richter741854e2005-12-01 18:52:03 -0500711 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714#define PREP_ASYNC_HEAD_RCODE(tc) \
715 packet->tcode = tc; \
716 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
717 | (1 << 8) | (tc << 4); \
718 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
719 packet->header[2] = 0
720
721static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500722 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
725 packet->header[3] = data;
726 packet->header_size = 16;
727 packet->data_size = 0;
728}
729
730static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500731 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 if (rcode != RCODE_COMPLETE)
734 length = 0;
735
736 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
737 packet->header[3] = length << 16;
738 packet->header_size = 16;
739 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
740}
741
742static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
743{
744 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
745 packet->header[2] = 0;
746 packet->header_size = 12;
747 packet->data_size = 0;
748}
749
750static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500751 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 if (rcode != RCODE_COMPLETE)
754 length = 0;
755
756 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
757 packet->header[3] = (length << 16) | extcode;
758 packet->header_size = 16;
759 packet->data_size = length;
760}
761
762#define PREP_REPLY_PACKET(length) \
Stefan Richter741854e2005-12-01 18:52:03 -0500763 packet = create_reply_packet(host, data, length); \
764 if (packet == NULL) break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766static void handle_incoming_packet(struct hpsb_host *host, int tcode,
767 quadlet_t *data, size_t size, int write_acked)
768{
Stefan Richter741854e2005-12-01 18:52:03 -0500769 struct hpsb_packet *packet;
770 int length, rcode, extcode;
771 quadlet_t buffer;
772 nodeid_t source = data[1] >> 16;
773 nodeid_t dest = data[0] >> 16;
774 u16 flags = (u16) data[0];
775 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Stefan Richter741854e2005-12-01 18:52:03 -0500777 /* big FIXME - no error checking is done for an out of bounds length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Stefan Richter741854e2005-12-01 18:52:03 -0500779 switch (tcode) {
780 case TCODE_WRITEQ:
781 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
782 rcode = highlevel_write(host, source, dest, data+3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 addr, 4, flags);
784
Stefan Richter741854e2005-12-01 18:52:03 -0500785 if (!write_acked
786 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
787 && (rcode >= 0)) {
788 /* not a broadcast write, reply */
789 PREP_REPLY_PACKET(0);
790 fill_async_write_resp(packet, rcode);
791 send_packet_nocare(packet);
792 }
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Stefan Richter741854e2005-12-01 18:52:03 -0500795 case TCODE_WRITEB:
796 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
797 rcode = highlevel_write(host, source, dest, data+4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 addr, data[3]>>16, flags);
799
Stefan Richter741854e2005-12-01 18:52:03 -0500800 if (!write_acked
801 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
802 && (rcode >= 0)) {
803 /* not a broadcast write, reply */
804 PREP_REPLY_PACKET(0);
805 fill_async_write_resp(packet, rcode);
806 send_packet_nocare(packet);
807 }
808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Stefan Richter741854e2005-12-01 18:52:03 -0500810 case TCODE_READQ:
811 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
812 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Stefan Richter741854e2005-12-01 18:52:03 -0500814 if (rcode >= 0) {
815 PREP_REPLY_PACKET(0);
816 fill_async_readquad_resp(packet, rcode, buffer);
817 send_packet_nocare(packet);
818 }
819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Stefan Richter741854e2005-12-01 18:52:03 -0500821 case TCODE_READB:
822 length = data[3] >> 16;
823 PREP_REPLY_PACKET(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Stefan Richter741854e2005-12-01 18:52:03 -0500825 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
826 rcode = highlevel_read(host, source, packet->data, addr,
827 length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Stefan Richter741854e2005-12-01 18:52:03 -0500829 if (rcode >= 0) {
830 fill_async_readblock_resp(packet, rcode, length);
831 send_packet_nocare(packet);
832 } else {
833 hpsb_free_packet(packet);
834 }
835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Stefan Richter741854e2005-12-01 18:52:03 -0500837 case TCODE_LOCK_REQUEST:
838 length = data[3] >> 16;
839 extcode = data[3] & 0xffff;
840 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Stefan Richter741854e2005-12-01 18:52:03 -0500842 PREP_REPLY_PACKET(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Stefan Richter741854e2005-12-01 18:52:03 -0500844 if ((extcode == 0) || (extcode >= 7)) {
845 /* let switch default handle error */
846 length = 0;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Stefan Richter741854e2005-12-01 18:52:03 -0500849 switch (length) {
850 case 4:
851 rcode = highlevel_lock(host, source, packet->data, addr,
852 data[4], 0, extcode,flags);
853 fill_async_lock_resp(packet, rcode, extcode, 4);
854 break;
855 case 8:
856 if ((extcode != EXTCODE_FETCH_ADD)
857 && (extcode != EXTCODE_LITTLE_ADD)) {
858 rcode = highlevel_lock(host, source,
859 packet->data, addr,
860 data[5], data[4],
861 extcode, flags);
862 fill_async_lock_resp(packet, rcode, extcode, 4);
863 } else {
864 rcode = highlevel_lock64(host, source,
865 (octlet_t *)packet->data, addr,
866 *(octlet_t *)(data + 4), 0ULL,
867 extcode, flags);
868 fill_async_lock_resp(packet, rcode, extcode, 8);
869 }
870 break;
871 case 16:
872 rcode = highlevel_lock64(host, source,
873 (octlet_t *)packet->data, addr,
874 *(octlet_t *)(data + 6),
875 *(octlet_t *)(data + 4),
876 extcode, flags);
877 fill_async_lock_resp(packet, rcode, extcode, 8);
878 break;
879 default:
880 rcode = RCODE_TYPE_ERROR;
881 fill_async_lock_resp(packet, rcode,
882 extcode, 0);
883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Stefan Richter741854e2005-12-01 18:52:03 -0500885 if (rcode >= 0) {
886 send_packet_nocare(packet);
887 } else {
888 hpsb_free_packet(packet);
889 }
890 break;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893}
894#undef PREP_REPLY_PACKET
895
896
897void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
Stefan Richter741854e2005-12-01 18:52:03 -0500898 int write_acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Stefan Richter741854e2005-12-01 18:52:03 -0500900 int tcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Stefan Richter741854e2005-12-01 18:52:03 -0500902 if (host->in_bus_reset) {
903 HPSB_INFO("received packet during reset; ignoring");
904 return;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Stefan Richter741854e2005-12-01 18:52:03 -0500907 dump_packet("received packet", data, size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Stefan Richter741854e2005-12-01 18:52:03 -0500909 tcode = (data[0] >> 4) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stefan Richter741854e2005-12-01 18:52:03 -0500911 switch (tcode) {
912 case TCODE_WRITE_RESPONSE:
913 case TCODE_READQ_RESPONSE:
914 case TCODE_READB_RESPONSE:
915 case TCODE_LOCK_RESPONSE:
916 handle_packet_response(host, tcode, data, size);
917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Stefan Richter741854e2005-12-01 18:52:03 -0500919 case TCODE_WRITEQ:
920 case TCODE_WRITEB:
921 case TCODE_READQ:
922 case TCODE_READB:
923 case TCODE_LOCK_REQUEST:
924 handle_incoming_packet(host, tcode, data, size, write_acked);
925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927
Stefan Richter741854e2005-12-01 18:52:03 -0500928 case TCODE_ISO_DATA:
929 highlevel_iso_receive(host, data, size);
930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Stefan Richter741854e2005-12-01 18:52:03 -0500932 case TCODE_CYCLE_START:
933 /* simply ignore this packet if it is passed on */
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Stefan Richter741854e2005-12-01 18:52:03 -0500936 default:
937 HPSB_NOTICE("received packet with bogus transaction code %d",
938 tcode);
939 break;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943
944static void abort_requests(struct hpsb_host *host)
945{
946 struct hpsb_packet *packet;
947 struct sk_buff *skb;
948
949 host->driver->devctl(host, CANCEL_REQUESTS, 0);
950
951 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
952 packet = (struct hpsb_packet *)skb->data;
953
954 packet->state = hpsb_complete;
955 packet->ack_code = ACKX_ABORTED;
956 queue_packet_complete(packet);
957 }
958}
959
960void abort_timedouts(unsigned long __opaque)
961{
962 struct hpsb_host *host = (struct hpsb_host *)__opaque;
963 unsigned long flags;
964 struct hpsb_packet *packet;
965 struct sk_buff *skb;
966 unsigned long expire;
967
968 spin_lock_irqsave(&host->csr.lock, flags);
969 expire = host->csr.expire;
970 spin_unlock_irqrestore(&host->csr.lock, flags);
971
972 /* Hold the lock around this, since we aren't dequeuing all
973 * packets, just ones we need. */
974 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
975
976 while (!skb_queue_empty(&host->pending_packet_queue)) {
977 skb = skb_peek(&host->pending_packet_queue);
978
979 packet = (struct hpsb_packet *)skb->data;
980
981 if (time_before(packet->sendtime + expire, jiffies)) {
David S. Miller8728b832005-08-09 19:25:21 -0700982 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 packet->state = hpsb_complete;
984 packet->ack_code = ACKX_TIMEOUT;
985 queue_packet_complete(packet);
986 } else {
987 /* Since packets are added to the tail, the oldest
988 * ones are first, always. When we get to one that
989 * isn't timed out, the rest aren't either. */
990 break;
991 }
992 }
993
994 if (!skb_queue_empty(&host->pending_packet_queue))
995 mod_timer(&host->timeout, jiffies + host->timeout_interval);
996
997 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
998}
999
1000
1001/* Kernel thread and vars, which handles packets that are completed. Only
1002 * packets that have a "complete" function are sent here. This way, the
1003 * completion is run out of kernel context, and doesn't block the rest of
1004 * the stack. */
Ben Collinsf6542402006-06-12 18:15:50 -04001005static struct task_struct *khpsbpkt_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static struct sk_buff_head hpsbpkt_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008static void queue_packet_complete(struct hpsb_packet *packet)
1009{
1010 if (packet->no_waiter) {
1011 hpsb_free_packet(packet);
1012 return;
1013 }
1014 if (packet->complete_routine != NULL) {
1015 skb_queue_tail(&hpsbpkt_queue, packet->skb);
Ben Collinsf6542402006-06-12 18:15:50 -04001016 wake_up_process(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 return;
1019}
1020
1021static int hpsbpkt_thread(void *__hi)
1022{
1023 struct sk_buff *skb;
1024 struct hpsb_packet *packet;
1025 void (*complete_routine)(void*);
1026 void *complete_data;
1027
Dave Jones8c1d2862006-01-06 00:18:38 -08001028 current->flags |= PF_NOFREEZE;
1029
Ben Collinsf6542402006-06-12 18:15:50 -04001030 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1032 packet = (struct hpsb_packet *)skb->data;
1033
1034 complete_routine = packet->complete_routine;
1035 complete_data = packet->complete_data;
1036
1037 packet->complete_routine = packet->complete_data = NULL;
1038
1039 complete_routine(complete_data);
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ben Collinsf6542402006-06-12 18:15:50 -04001042 set_current_state(TASK_INTERRUPTIBLE);
1043 if (!skb_peek(&hpsbpkt_queue))
1044 schedule();
1045 __set_current_state(TASK_RUNNING);
1046 }
1047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int __init ieee1394_init(void)
1051{
1052 int i, ret;
1053
1054 skb_queue_head_init(&hpsbpkt_queue);
1055
1056 /* non-fatal error */
1057 if (hpsb_init_config_roms()) {
1058 HPSB_ERR("Failed to initialize some config rom entries.\n");
1059 HPSB_ERR("Some features may not be available\n");
1060 }
1061
Ben Collinsf6542402006-06-12 18:15:50 -04001062 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1063 if (IS_ERR(khpsbpkt_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 HPSB_ERR("Failed to start hpsbpkt thread!\n");
Ben Collinsf6542402006-06-12 18:15:50 -04001065 ret = PTR_ERR(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto exit_cleanup_config_roms;
1067 }
1068
1069 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1070 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1071 ret = -ENODEV;
1072 goto exit_release_kernel_thread;
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 ret = bus_register(&ieee1394_bus_type);
1076 if (ret < 0) {
1077 HPSB_INFO("bus register failed");
Stefan Richtera8748442006-03-28 19:55:41 -05001078 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
1081 for (i = 0; fw_bus_attrs[i]; i++) {
1082 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1083 if (ret < 0) {
1084 while (i >= 0) {
1085 bus_remove_file(&ieee1394_bus_type,
1086 fw_bus_attrs[i--]);
1087 }
1088 bus_unregister(&ieee1394_bus_type);
Stefan Richtera8748442006-03-28 19:55:41 -05001089 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 }
1092
1093 ret = class_register(&hpsb_host_class);
1094 if (ret < 0)
1095 goto release_all_bus;
1096
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001097 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (IS_ERR(hpsb_protocol_class)) {
1099 ret = PTR_ERR(hpsb_protocol_class);
1100 goto release_class_host;
1101 }
1102
1103 ret = init_csr();
1104 if (ret) {
1105 HPSB_INFO("init csr failed");
1106 ret = -ENOMEM;
1107 goto release_class_protocol;
1108 }
1109
1110 if (disable_nodemgr) {
1111 HPSB_INFO("nodemgr and IRM functionality disabled");
1112 /* We shouldn't contend for IRM with nodemgr disabled, since
1113 nodemgr implements functionality required of ieee1394a-2000
1114 IRMs */
1115 hpsb_disable_irm = 1;
Stefan Richter741854e2005-12-01 18:52:03 -05001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return 0;
1118 }
1119
1120 if (hpsb_disable_irm) {
1121 HPSB_INFO("IRM functionality disabled");
1122 }
1123
1124 ret = init_ieee1394_nodemgr();
1125 if (ret < 0) {
1126 HPSB_INFO("init nodemgr failed");
1127 goto cleanup_csr;
1128 }
1129
1130 return 0;
1131
1132cleanup_csr:
1133 cleanup_csr();
1134release_class_protocol:
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001135 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136release_class_host:
1137 class_unregister(&hpsb_host_class);
1138release_all_bus:
1139 for (i = 0; fw_bus_attrs[i]; i++)
1140 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1141 bus_unregister(&ieee1394_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142release_chrdev:
1143 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1144exit_release_kernel_thread:
Ben Collinsf6542402006-06-12 18:15:50 -04001145 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146exit_cleanup_config_roms:
1147 hpsb_cleanup_config_roms();
1148 return ret;
1149}
1150
1151static void __exit ieee1394_cleanup(void)
1152{
1153 int i;
1154
1155 if (!disable_nodemgr)
1156 cleanup_ieee1394_nodemgr();
1157
1158 cleanup_csr();
1159
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001160 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 class_unregister(&hpsb_host_class);
1162 for (i = 0; fw_bus_attrs[i]; i++)
1163 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1164 bus_unregister(&ieee1394_bus_type);
1165
Ben Collinsf6542402006-06-12 18:15:50 -04001166 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 hpsb_cleanup_config_roms();
1169
1170 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173module_init(ieee1394_init);
1174module_exit(ieee1394_cleanup);
1175
1176/* Exported symbols */
1177
1178/** hosts.c **/
1179EXPORT_SYMBOL(hpsb_alloc_host);
1180EXPORT_SYMBOL(hpsb_add_host);
1181EXPORT_SYMBOL(hpsb_remove_host);
1182EXPORT_SYMBOL(hpsb_update_config_rom_image);
1183
1184/** ieee1394_core.c **/
1185EXPORT_SYMBOL(hpsb_speedto_str);
1186EXPORT_SYMBOL(hpsb_protocol_class);
1187EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1188EXPORT_SYMBOL(hpsb_alloc_packet);
1189EXPORT_SYMBOL(hpsb_free_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190EXPORT_SYMBOL(hpsb_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL(hpsb_reset_bus);
1192EXPORT_SYMBOL(hpsb_bus_reset);
1193EXPORT_SYMBOL(hpsb_selfid_received);
1194EXPORT_SYMBOL(hpsb_selfid_complete);
1195EXPORT_SYMBOL(hpsb_packet_sent);
1196EXPORT_SYMBOL(hpsb_packet_received);
1197EXPORT_SYMBOL_GPL(hpsb_disable_irm);
Ben Collins1934b8b2005-07-09 20:01:23 -04001198#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
1199EXPORT_SYMBOL(hpsb_send_phy_config);
1200EXPORT_SYMBOL(hpsb_send_packet_and_wait);
1201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203/** ieee1394_transactions.c **/
1204EXPORT_SYMBOL(hpsb_get_tlabel);
1205EXPORT_SYMBOL(hpsb_free_tlabel);
1206EXPORT_SYMBOL(hpsb_make_readpacket);
1207EXPORT_SYMBOL(hpsb_make_writepacket);
1208EXPORT_SYMBOL(hpsb_make_streampacket);
1209EXPORT_SYMBOL(hpsb_make_lockpacket);
1210EXPORT_SYMBOL(hpsb_make_lock64packet);
1211EXPORT_SYMBOL(hpsb_make_phypacket);
1212EXPORT_SYMBOL(hpsb_make_isopacket);
1213EXPORT_SYMBOL(hpsb_read);
1214EXPORT_SYMBOL(hpsb_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215EXPORT_SYMBOL(hpsb_packet_success);
1216
1217/** highlevel.c **/
1218EXPORT_SYMBOL(hpsb_register_highlevel);
1219EXPORT_SYMBOL(hpsb_unregister_highlevel);
1220EXPORT_SYMBOL(hpsb_register_addrspace);
1221EXPORT_SYMBOL(hpsb_unregister_addrspace);
1222EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1223EXPORT_SYMBOL(hpsb_listen_channel);
1224EXPORT_SYMBOL(hpsb_unlisten_channel);
1225EXPORT_SYMBOL(hpsb_get_hostinfo);
1226EXPORT_SYMBOL(hpsb_create_hostinfo);
1227EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1228EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1229EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1230EXPORT_SYMBOL(hpsb_set_hostinfo);
Ben Collins1934b8b2005-07-09 20:01:23 -04001231EXPORT_SYMBOL(highlevel_host_reset);
1232#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233EXPORT_SYMBOL(highlevel_add_host);
1234EXPORT_SYMBOL(highlevel_remove_host);
Ben Collins1934b8b2005-07-09 20:01:23 -04001235#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237/** nodemgr.c **/
1238EXPORT_SYMBOL(hpsb_node_fill_packet);
1239EXPORT_SYMBOL(hpsb_node_write);
1240EXPORT_SYMBOL(hpsb_register_protocol);
1241EXPORT_SYMBOL(hpsb_unregister_protocol);
1242EXPORT_SYMBOL(ieee1394_bus_type);
Ben Collins1934b8b2005-07-09 20:01:23 -04001243#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244EXPORT_SYMBOL(nodemgr_for_each_host);
Ben Collins1934b8b2005-07-09 20:01:23 -04001245#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247/** csr.c **/
1248EXPORT_SYMBOL(hpsb_update_config_rom);
1249
1250/** dma.c **/
1251EXPORT_SYMBOL(dma_prog_region_init);
1252EXPORT_SYMBOL(dma_prog_region_alloc);
1253EXPORT_SYMBOL(dma_prog_region_free);
1254EXPORT_SYMBOL(dma_region_init);
1255EXPORT_SYMBOL(dma_region_alloc);
1256EXPORT_SYMBOL(dma_region_free);
1257EXPORT_SYMBOL(dma_region_sync_for_cpu);
1258EXPORT_SYMBOL(dma_region_sync_for_device);
1259EXPORT_SYMBOL(dma_region_mmap);
1260EXPORT_SYMBOL(dma_region_offset_to_bus);
1261
1262/** iso.c **/
1263EXPORT_SYMBOL(hpsb_iso_xmit_init);
1264EXPORT_SYMBOL(hpsb_iso_recv_init);
1265EXPORT_SYMBOL(hpsb_iso_xmit_start);
1266EXPORT_SYMBOL(hpsb_iso_recv_start);
1267EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1268EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1269EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1270EXPORT_SYMBOL(hpsb_iso_stop);
1271EXPORT_SYMBOL(hpsb_iso_shutdown);
1272EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1273EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1274EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1275EXPORT_SYMBOL(hpsb_iso_n_ready);
1276EXPORT_SYMBOL(hpsb_iso_packet_sent);
1277EXPORT_SYMBOL(hpsb_iso_packet_received);
1278EXPORT_SYMBOL(hpsb_iso_wake);
1279EXPORT_SYMBOL(hpsb_iso_recv_flush);
1280
1281/** csr1212.c **/
Ben Collins1934b8b2005-07-09 20:01:23 -04001282EXPORT_SYMBOL(csr1212_new_directory);
1283EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1284EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1285EXPORT_SYMBOL(csr1212_release_keyval);
1286EXPORT_SYMBOL(csr1212_read);
1287EXPORT_SYMBOL(csr1212_parse_keyval);
1288EXPORT_SYMBOL(_csr1212_read_keyval);
1289EXPORT_SYMBOL(_csr1212_destroy_keyval);
1290#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291EXPORT_SYMBOL(csr1212_create_csr);
1292EXPORT_SYMBOL(csr1212_init_local_csr);
1293EXPORT_SYMBOL(csr1212_new_immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294EXPORT_SYMBOL(csr1212_associate_keyval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295EXPORT_SYMBOL(csr1212_new_string_descriptor_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296EXPORT_SYMBOL(csr1212_destroy_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297EXPORT_SYMBOL(csr1212_generate_csr_image);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298EXPORT_SYMBOL(csr1212_parse_csr);
Ben Collins1934b8b2005-07-09 20:01:23 -04001299#endif