blob: 559c477092f85a3397091d7808db3769d1b40557 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/string.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/interrupt.h>
29#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/bitops.h>
32#include <linux/kdev_t.h>
33#include <linux/skbuff.h>
34#include <linux/suspend.h>
Ben Collinsf6542402006-06-12 18:15:50 -040035#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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 */
Ben Collins1934b8b2005-07-09 20:01:23 -040055static int disable_nodemgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056module_param(disable_nodemgr, int, 0444);
57MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
58
59/* Disable Isochronous Resource Manager functionality */
60int hpsb_disable_irm = 0;
Stefan Richter23e93f12006-03-28 20:03:34 -050061module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_PARM_DESC(disable_irm,
63 "Disable Isochronous Resource Manager functionality.");
64
65/* We are GPL, so treat us special */
66MODULE_LICENSE("GPL");
67
68/* Some globals used */
69const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
gregkh@suse.de7e25ab92005-03-23 09:53:36 -080070struct class *hpsb_protocol_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifdef CONFIG_IEEE1394_VERBOSEDEBUG
Jody McIntyredb2fd662005-09-30 11:59:09 -070073static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 int i;
76
77 size /= 4;
78 size = (size > 4 ? 4 : size);
79
80 printk(KERN_DEBUG "ieee1394: %s", text);
Jody McIntyredb2fd662005-09-30 11:59:09 -070081 if (speed > -1 && speed < 6)
82 printk(" at %s", hpsb_speedto_str[speed]);
83 printk(":");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 for (i = 0; i < size; i++)
85 printk(" %08x", data[i]);
86 printk("\n");
87}
88#else
Jody McIntyredb2fd662005-09-30 11:59:09 -070089#define dump_packet(a,b,c,d)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
91
92static void abort_requests(struct hpsb_host *host);
93static 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 */
105void 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 */
134struct 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 */
171void 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
180int hpsb_reset_bus(struct hpsb_host *host, int type)
181{
Stefan Richter741854e2005-12-01 18:52:03 -0500182 if (!host->in_bus_reset) {
183 host->driver->devctl(host, RESET_BUS, type);
184 return 0;
185 } else {
186 return 1;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190
191int hpsb_bus_reset(struct hpsb_host *host)
192{
Stefan Richter741854e2005-12-01 18:52:03 -0500193 if (host->in_bus_reset) {
194 HPSB_NOTICE("%s called while bus reset already in progress",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 __FUNCTION__);
Stefan Richter741854e2005-12-01 18:52:03 -0500196 return 1;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Stefan Richter741854e2005-12-01 18:52:03 -0500199 abort_requests(host);
200 host->in_bus_reset = 1;
201 host->irm_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 host->is_irm = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500203 host->busmgr_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 host->is_busmgr = 0;
205 host->is_cycmst = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500206 host->node_count = 0;
207 host->selfid_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Stefan Richter741854e2005-12-01 18:52:03 -0500209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212
213/*
214 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
215 * case verification failed.
216 */
217static int check_selfids(struct hpsb_host *host)
218{
Stefan Richter741854e2005-12-01 18:52:03 -0500219 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 host->nodes_active = 0;
226
Stefan Richter741854e2005-12-01 18:52:03 -0500227 while (rest_of_selfids--) {
228 if (!sid->extended) {
229 nodeid++;
230 esid_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Stefan Richter741854e2005-12-01 18:52:03 -0500232 if (sid->phy_id != nodeid) {
233 HPSB_INFO("SelfIDs failed monotony check with "
234 "%d", sid->phy_id);
235 return 0;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (sid->link_active) {
239 host->nodes_active++;
240 if (sid->contender)
241 host->irm_id = LOCAL_BUS | sid->phy_id;
242 }
Stefan Richter741854e2005-12-01 18:52:03 -0500243 } else {
244 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Stefan Richter741854e2005-12-01 18:52:03 -0500246 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Stefan Richter741854e2005-12-01 18:52:03 -0500257 esid = (struct ext_selfid *)(sid - 1);
258 while (esid->extended) {
259 if ((esid->porta == SELFID_PORT_PARENT) ||
Stefan Richterd7758462005-12-01 18:51:56 -0500260 (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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 HPSB_INFO("SelfIDs failed root check on "
268 "extended SelfID");
269 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500270 }
271 esid--;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Stefan Richter741854e2005-12-01 18:52:03 -0500274 sid = (struct selfid *)esid;
Stefan Richterd7758462005-12-01 18:51:56 -0500275 if ((sid->port0 == SELFID_PORT_PARENT) ||
276 (sid->port1 == SELFID_PORT_PARENT) ||
277 (sid->port2 == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 HPSB_INFO("SelfIDs failed root check");
279 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 host->node_count = nodeid + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286static void build_speed_map(struct hpsb_host *host, int nodecount)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 u8 cldcnt[nodecount];
Stefan Richter741854e2005-12-01 18:52:03 -0500289 u8 *map = host->speed_map;
Ben Collins647dcb52006-06-12 18:12:37 -0400290 u8 *speedcap = host->speed;
Stefan Richter741854e2005-12-01 18:52:03 -0500291 struct selfid *sid;
292 struct ext_selfid *esid;
293 int i, j, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Stefan Richter741854e2005-12-01 18:52:03 -0500295 for (i = 0; i < (nodecount * 64); i += 64) {
296 for (j = 0; j < nodecount; j++) {
297 map[i+j] = IEEE1394_SPEED_MAX;
298 }
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Stefan Richter741854e2005-12-01 18:52:03 -0500301 for (i = 0; i < nodecount; i++) {
302 cldcnt[i] = 0;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Stefan Richter741854e2005-12-01 18:52:03 -0500305 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Stefan Richterd7758462005-12-01 18:51:56 -0500312 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]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else {
Stefan Richterd7758462005-12-01 18:51:56 -0500321 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]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Stefan Richter741854e2005-12-01 18:52:03 -0500325 speedcap[n] = sid->speed;
326 n--;
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Stefan Richter741854e2005-12-01 18:52:03 -0500330 /* set self mapping */
331 for (i = 0; i < nodecount; i++) {
332 map[64*i + i] = speedcap[i];
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Stefan Richter741854e2005-12-01 18:52:03 -0500335 /* 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Stefan Richter741854e2005-12-01 18:52:03 -0500345 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 }
Ben Collins647dcb52006-06-12 18:12:37 -0400357
Stefan Richter433a87d2006-07-03 12:02:27 -0400358#if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
Ben Collins647dcb52006-06-12 18:12:37 -0400359 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
360 for (n = 0; n < nodecount; n++)
Stefan Richter433a87d2006-07-03 12:02:27 -0400361 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
Ben Collins647dcb52006-06-12 18:12:37 -0400362 speedcap[n] = IEEE1394_SPEED_MAX;
Stefan Richter433a87d2006-07-03 12:02:27 -0400363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366
367void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
368{
Stefan Richter741854e2005-12-01 18:52:03 -0500369 if (host->in_bus_reset) {
370 HPSB_VERBOSE("Including SelfID 0x%x", sid);
371 host->topology_map[host->selfid_count++] = sid;
372 } else {
373 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 sid, NODEID_TO_BUS(host->node_id));
Stefan Richter741854e2005-12-01 18:52:03 -0500375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
379{
380 if (!host->in_bus_reset)
381 HPSB_NOTICE("SelfID completion called outside of bus reset!");
382
Stefan Richter741854e2005-12-01 18:52:03 -0500383 host->node_id = LOCAL_BUS | phyid;
384 host->is_root = isroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Stefan Richter741854e2005-12-01 18:52:03 -0500386 if (!check_selfids(host)) {
387 if (host->reset_retries++ < 20) {
388 /* selfid stage did not complete without error */
389 HPSB_NOTICE("Error in SelfID stage, resetting");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 host->in_bus_reset = 0;
391 /* this should work from ohci1394 now... */
Stefan Richter741854e2005-12-01 18:52:03 -0500392 hpsb_reset_bus(host, LONG_RESET);
393 return;
394 } else {
395 HPSB_NOTICE("Stopping out-of-control reset loop");
396 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500398 }
399 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500401 build_speed_map(host, host->node_count);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
405 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
406
Stefan Richter741854e2005-12-01 18:52:03 -0500407 /* irm_id is kept up to date by check_selfids() */
408 if (host->irm_id == host->node_id) {
409 host->is_irm = 1;
410 } else {
411 host->is_busmgr = 0;
412 host->is_irm = 0;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Stefan Richter741854e2005-12-01 18:52:03 -0500415 if (isroot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
417 host->is_cycmst = 1;
418 }
419 atomic_inc(&host->generation);
420 host->in_bus_reset = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500421 highlevel_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424
425void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
Stefan Richter741854e2005-12-01 18:52:03 -0500426 int ackcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 unsigned long flags;
429
430 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
431
432 packet->ack_code = ackcode;
433
434 if (packet->no_waiter || packet->state == hpsb_complete) {
435 /* if packet->no_waiter, must not have a tlabel allocated */
436 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
437 hpsb_free_packet(packet);
438 return;
439 }
440
441 atomic_dec(&packet->refcnt); /* drop HC's reference */
442 /* here the packet must be on the host->pending_packet_queue */
443
444 if (ackcode != ACK_PENDING || !packet->expect_response) {
445 packet->state = hpsb_complete;
446 __skb_unlink(packet->skb, &host->pending_packet_queue);
447 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
448 queue_packet_complete(packet);
449 return;
450 }
451
452 packet->state = hpsb_pending;
453 packet->sendtime = jiffies;
454
455 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
456
457 mod_timer(&host->timeout, jiffies + host->timeout_interval);
458}
459
460/**
461 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
462 * @host: host that PHY config packet gets sent through
463 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
464 * @gapcnt: gap count value to set (-1 = don't set gap count)
465 *
466 * This function sends a PHY config packet on the bus through the specified host.
467 *
468 * Return value: 0 for success or error number otherwise.
469 */
470int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
471{
472 struct hpsb_packet *packet;
Stefan Richter546513f2005-12-01 18:52:01 -0500473 quadlet_t d = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int retval = 0;
475
476 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
477 (rootid == -1 && gapcnt == -1)) {
478 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
479 rootid, gapcnt);
480 return -EINVAL;
481 }
482
Stefan Richter546513f2005-12-01 18:52:01 -0500483 if (rootid != -1)
484 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
485 if (gapcnt != -1)
486 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
487
488 packet = hpsb_make_phypacket(host, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (!packet)
490 return -ENOMEM;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 packet->generation = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 retval = hpsb_send_packet_and_wait(packet);
494 hpsb_free_packet(packet);
495
496 return retval;
497}
498
499/**
500 * hpsb_send_packet - transmit a packet on the bus
501 * @packet: packet to send
502 *
503 * The packet is sent through the host specified in the packet->host field.
504 * Before sending, the packet's transmit speed is automatically determined
505 * using the local speed map when it is an async, non-broadcast packet.
506 *
507 * Possibilities for failure are that host is either not initialized, in bus
508 * reset, the packet's generation number doesn't match the current generation
509 * number or the host reports a transmit error.
510 *
511 * Return value: 0 on success, negative errno on failure.
512 */
513int hpsb_send_packet(struct hpsb_packet *packet)
514{
515 struct hpsb_host *host = packet->host;
516
Stefan Richter741854e2005-12-01 18:52:03 -0500517 if (host->is_shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EINVAL;
519 if (host->in_bus_reset ||
520 (packet->generation != get_hpsb_generation(host)))
Stefan Richter741854e2005-12-01 18:52:03 -0500521 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Stefan Richter741854e2005-12-01 18:52:03 -0500523 packet->state = hpsb_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* This just seems silly to me */
526 WARN_ON(packet->no_waiter && packet->expect_response);
527
528 if (!packet->no_waiter || packet->expect_response) {
529 atomic_inc(&packet->refcnt);
Ben Collins1934b8b2005-07-09 20:01:23 -0400530 /* Set the initial "sendtime" to 10 seconds from now, to
531 prevent premature expiry. If a packet takes more than
532 10 seconds to hit the wire, we have bigger problems :) */
Jody McIntyre6262d062005-05-16 21:54:05 -0700533 packet->sendtime = jiffies + 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 skb_queue_tail(&host->pending_packet_queue, packet->skb);
535 }
536
Stefan Richter741854e2005-12-01 18:52:03 -0500537 if (packet->node_id == host->node_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* it is a local request, so handle it locally */
539
Stefan Richter741854e2005-12-01 18:52:03 -0500540 quadlet_t *data;
541 size_t size = packet->data_size + packet->header_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Stefan Richter741854e2005-12-01 18:52:03 -0500543 data = kmalloc(size, GFP_ATOMIC);
544 if (!data) {
545 HPSB_ERR("unable to allocate memory for concatenating header and data");
546 return -ENOMEM;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Stefan Richter741854e2005-12-01 18:52:03 -0500549 memcpy(data, packet->header, packet->header_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Stefan Richter741854e2005-12-01 18:52:03 -0500551 if (packet->data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
553
Stefan Richter741854e2005-12-01 18:52:03 -0500554 dump_packet("send packet local", packet->header, packet->header_size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Stefan Richter741854e2005-12-01 18:52:03 -0500556 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
557 hpsb_packet_received(host, data, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Stefan Richter741854e2005-12-01 18:52:03 -0500559 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Stefan Richter741854e2005-12-01 18:52:03 -0500561 return 0;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Ben Collins647dcb52006-06-12 18:12:37 -0400564 if (packet->type == hpsb_async &&
565 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
Stefan Richter741854e2005-12-01 18:52:03 -0500566 packet->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -0400567 host->speed[NODEID_TO_NODE(packet->node_id)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stefan Richter741854e2005-12-01 18:52:03 -0500569 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Stefan Richter741854e2005-12-01 18:52:03 -0500571 return host->driver->transmit_packet(host, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574/* We could just use complete() directly as the packet complete
575 * callback, but this is more typesafe, in the sense that we get a
576 * compiler error if the prototype for complete() changes. */
577
578static void complete_packet(void *data)
579{
580 complete((struct completion *) data);
581}
582
583int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
584{
585 struct completion done;
586 int retval;
587
588 init_completion(&done);
589 hpsb_set_packet_complete_task(packet, complete_packet, &done);
590 retval = hpsb_send_packet(packet);
591 if (retval == 0)
592 wait_for_completion(&done);
593
594 return retval;
595}
596
597static void send_packet_nocare(struct hpsb_packet *packet)
598{
Stefan Richter741854e2005-12-01 18:52:03 -0500599 if (hpsb_send_packet(packet) < 0) {
600 hpsb_free_packet(packet);
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604
605static void handle_packet_response(struct hpsb_host *host, int tcode,
606 quadlet_t *data, size_t size)
607{
Stefan Richter741854e2005-12-01 18:52:03 -0500608 struct hpsb_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct sk_buff *skb;
Stefan Richter741854e2005-12-01 18:52:03 -0500610 int tcode_match = 0;
611 int tlabel;
612 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Stefan Richter741854e2005-12-01 18:52:03 -0500614 tlabel = (data[0] >> 10) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
617
618 skb_queue_walk(&host->pending_packet_queue, skb) {
619 packet = (struct hpsb_packet *)skb->data;
Stefan Richter741854e2005-12-01 18:52:03 -0500620 if ((packet->tlabel == tlabel)
621 && (packet->node_id == (data[1] >> 16))){
622 break;
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 packet = NULL;
Stefan Richter741854e2005-12-01 18:52:03 -0500626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (packet == NULL) {
Stefan Richter741854e2005-12-01 18:52:03 -0500629 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
630 dump_packet("contents", data, 16, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500632 return;
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Stefan Richter741854e2005-12-01 18:52:03 -0500635 switch (packet->tcode) {
636 case TCODE_WRITEQ:
637 case TCODE_WRITEB:
638 if (tcode != TCODE_WRITE_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 tcode_match = 1;
641 memcpy(packet->header, data, 12);
Stefan Richter741854e2005-12-01 18:52:03 -0500642 break;
643 case TCODE_READQ:
644 if (tcode != TCODE_READQ_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 tcode_match = 1;
647 memcpy(packet->header, data, 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500648 break;
649 case TCODE_READB:
650 if (tcode != TCODE_READB_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 tcode_match = 1;
653 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
654 memcpy(packet->header, data, 16);
655 memcpy(packet->data, data + 4, size - 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500656 break;
657 case TCODE_LOCK_REQUEST:
658 if (tcode != TCODE_LOCK_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 break;
660 tcode_match = 1;
661 size = min((size - 16), (size_t)8);
662 BUG_ON(packet->skb->len - sizeof(*packet) < size);
663 memcpy(packet->header, data, 16);
664 memcpy(packet->data, data + 4, size);
Stefan Richter741854e2005-12-01 18:52:03 -0500665 break;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Stefan Richter741854e2005-12-01 18:52:03 -0500668 if (!tcode_match) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500670 HPSB_INFO("unsolicited response packet received - tcode mismatch");
671 dump_packet("contents", data, 16, -1);
672 return;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
David S. Miller8728b832005-08-09 19:25:21 -0700675 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (packet->state == hpsb_queued) {
678 packet->sendtime = jiffies;
679 packet->ack_code = ACK_PENDING;
680 }
681
682 packet->state = hpsb_complete;
683 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
684
685 queue_packet_complete(packet);
686}
687
688
689static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
690 quadlet_t *data, size_t dsize)
691{
Stefan Richter741854e2005-12-01 18:52:03 -0500692 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stefan Richter741854e2005-12-01 18:52:03 -0500694 p = hpsb_alloc_packet(dsize);
695 if (unlikely(p == NULL)) {
696 /* FIXME - send data_error response */
697 return NULL;
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Stefan Richter741854e2005-12-01 18:52:03 -0500700 p->type = hpsb_async;
701 p->state = hpsb_unused;
702 p->host = host;
703 p->node_id = data[1] >> 16;
704 p->tlabel = (data[0] >> 10) & 0x3f;
705 p->no_waiter = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 p->generation = get_hpsb_generation(host);
708
709 if (dsize % 4)
710 p->data[dsize / 4] = 0;
711
Stefan Richter741854e2005-12-01 18:52:03 -0500712 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715#define PREP_ASYNC_HEAD_RCODE(tc) \
716 packet->tcode = tc; \
717 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
718 | (1 << 8) | (tc << 4); \
719 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
720 packet->header[2] = 0
721
722static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500723 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
726 packet->header[3] = data;
727 packet->header_size = 16;
728 packet->data_size = 0;
729}
730
731static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500732 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 if (rcode != RCODE_COMPLETE)
735 length = 0;
736
737 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
738 packet->header[3] = length << 16;
739 packet->header_size = 16;
740 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
741}
742
743static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
744{
745 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
746 packet->header[2] = 0;
747 packet->header_size = 12;
748 packet->data_size = 0;
749}
750
751static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500752 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 if (rcode != RCODE_COMPLETE)
755 length = 0;
756
757 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
758 packet->header[3] = (length << 16) | extcode;
759 packet->header_size = 16;
760 packet->data_size = length;
761}
762
763#define PREP_REPLY_PACKET(length) \
Stefan Richter741854e2005-12-01 18:52:03 -0500764 packet = create_reply_packet(host, data, length); \
765 if (packet == NULL) break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767static void handle_incoming_packet(struct hpsb_host *host, int tcode,
768 quadlet_t *data, size_t size, int write_acked)
769{
Stefan Richter741854e2005-12-01 18:52:03 -0500770 struct hpsb_packet *packet;
771 int length, rcode, extcode;
772 quadlet_t buffer;
773 nodeid_t source = data[1] >> 16;
774 nodeid_t dest = data[0] >> 16;
775 u16 flags = (u16) data[0];
776 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Stefan Richter741854e2005-12-01 18:52:03 -0500778 /* big FIXME - no error checking is done for an out of bounds length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Stefan Richter741854e2005-12-01 18:52:03 -0500780 switch (tcode) {
781 case TCODE_WRITEQ:
782 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
783 rcode = highlevel_write(host, source, dest, data+3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 addr, 4, flags);
785
Stefan Richter741854e2005-12-01 18:52:03 -0500786 if (!write_acked
787 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
788 && (rcode >= 0)) {
789 /* not a broadcast write, reply */
790 PREP_REPLY_PACKET(0);
791 fill_async_write_resp(packet, rcode);
792 send_packet_nocare(packet);
793 }
794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Stefan Richter741854e2005-12-01 18:52:03 -0500796 case TCODE_WRITEB:
797 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
798 rcode = highlevel_write(host, source, dest, data+4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 addr, data[3]>>16, flags);
800
Stefan Richter741854e2005-12-01 18:52:03 -0500801 if (!write_acked
802 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
803 && (rcode >= 0)) {
804 /* not a broadcast write, reply */
805 PREP_REPLY_PACKET(0);
806 fill_async_write_resp(packet, rcode);
807 send_packet_nocare(packet);
808 }
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Stefan Richter741854e2005-12-01 18:52:03 -0500811 case TCODE_READQ:
812 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
813 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Stefan Richter741854e2005-12-01 18:52:03 -0500815 if (rcode >= 0) {
816 PREP_REPLY_PACKET(0);
817 fill_async_readquad_resp(packet, rcode, buffer);
818 send_packet_nocare(packet);
819 }
820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Stefan Richter741854e2005-12-01 18:52:03 -0500822 case TCODE_READB:
823 length = data[3] >> 16;
824 PREP_REPLY_PACKET(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Stefan Richter741854e2005-12-01 18:52:03 -0500826 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
827 rcode = highlevel_read(host, source, packet->data, addr,
828 length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Stefan Richter741854e2005-12-01 18:52:03 -0500830 if (rcode >= 0) {
831 fill_async_readblock_resp(packet, rcode, length);
832 send_packet_nocare(packet);
833 } else {
834 hpsb_free_packet(packet);
835 }
836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Stefan Richter741854e2005-12-01 18:52:03 -0500838 case TCODE_LOCK_REQUEST:
839 length = data[3] >> 16;
840 extcode = data[3] & 0xffff;
841 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Stefan Richter741854e2005-12-01 18:52:03 -0500843 PREP_REPLY_PACKET(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Stefan Richter741854e2005-12-01 18:52:03 -0500845 if ((extcode == 0) || (extcode >= 7)) {
846 /* let switch default handle error */
847 length = 0;
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Stefan Richter741854e2005-12-01 18:52:03 -0500850 switch (length) {
851 case 4:
852 rcode = highlevel_lock(host, source, packet->data, addr,
853 data[4], 0, extcode,flags);
854 fill_async_lock_resp(packet, rcode, extcode, 4);
855 break;
856 case 8:
857 if ((extcode != EXTCODE_FETCH_ADD)
858 && (extcode != EXTCODE_LITTLE_ADD)) {
859 rcode = highlevel_lock(host, source,
860 packet->data, addr,
861 data[5], data[4],
862 extcode, flags);
863 fill_async_lock_resp(packet, rcode, extcode, 4);
864 } else {
865 rcode = highlevel_lock64(host, source,
866 (octlet_t *)packet->data, addr,
867 *(octlet_t *)(data + 4), 0ULL,
868 extcode, flags);
869 fill_async_lock_resp(packet, rcode, extcode, 8);
870 }
871 break;
872 case 16:
873 rcode = highlevel_lock64(host, source,
874 (octlet_t *)packet->data, addr,
875 *(octlet_t *)(data + 6),
876 *(octlet_t *)(data + 4),
877 extcode, flags);
878 fill_async_lock_resp(packet, rcode, extcode, 8);
879 break;
880 default:
881 rcode = RCODE_TYPE_ERROR;
882 fill_async_lock_resp(packet, rcode,
883 extcode, 0);
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Stefan Richter741854e2005-12-01 18:52:03 -0500886 if (rcode >= 0) {
887 send_packet_nocare(packet);
888 } else {
889 hpsb_free_packet(packet);
890 }
891 break;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894}
895#undef PREP_REPLY_PACKET
896
897
898void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
Stefan Richter741854e2005-12-01 18:52:03 -0500899 int write_acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Stefan Richter741854e2005-12-01 18:52:03 -0500901 int tcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Stefan Richter741854e2005-12-01 18:52:03 -0500903 if (host->in_bus_reset) {
904 HPSB_INFO("received packet during reset; ignoring");
905 return;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Stefan Richter741854e2005-12-01 18:52:03 -0500908 dump_packet("received packet", data, size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Stefan Richter741854e2005-12-01 18:52:03 -0500910 tcode = (data[0] >> 4) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Stefan Richter741854e2005-12-01 18:52:03 -0500912 switch (tcode) {
913 case TCODE_WRITE_RESPONSE:
914 case TCODE_READQ_RESPONSE:
915 case TCODE_READB_RESPONSE:
916 case TCODE_LOCK_RESPONSE:
917 handle_packet_response(host, tcode, data, size);
918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Stefan Richter741854e2005-12-01 18:52:03 -0500920 case TCODE_WRITEQ:
921 case TCODE_WRITEB:
922 case TCODE_READQ:
923 case TCODE_READB:
924 case TCODE_LOCK_REQUEST:
925 handle_incoming_packet(host, tcode, data, size, write_acked);
926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928
Stefan Richter741854e2005-12-01 18:52:03 -0500929 case TCODE_ISO_DATA:
930 highlevel_iso_receive(host, data, size);
931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Stefan Richter741854e2005-12-01 18:52:03 -0500933 case TCODE_CYCLE_START:
934 /* simply ignore this packet if it is passed on */
935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Stefan Richter741854e2005-12-01 18:52:03 -0500937 default:
938 HPSB_NOTICE("received packet with bogus transaction code %d",
939 tcode);
940 break;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944
945static void abort_requests(struct hpsb_host *host)
946{
947 struct hpsb_packet *packet;
948 struct sk_buff *skb;
949
950 host->driver->devctl(host, CANCEL_REQUESTS, 0);
951
952 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
953 packet = (struct hpsb_packet *)skb->data;
954
955 packet->state = hpsb_complete;
956 packet->ack_code = ACKX_ABORTED;
957 queue_packet_complete(packet);
958 }
959}
960
961void abort_timedouts(unsigned long __opaque)
962{
963 struct hpsb_host *host = (struct hpsb_host *)__opaque;
964 unsigned long flags;
965 struct hpsb_packet *packet;
966 struct sk_buff *skb;
967 unsigned long expire;
968
969 spin_lock_irqsave(&host->csr.lock, flags);
970 expire = host->csr.expire;
971 spin_unlock_irqrestore(&host->csr.lock, flags);
972
973 /* Hold the lock around this, since we aren't dequeuing all
974 * packets, just ones we need. */
975 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
976
977 while (!skb_queue_empty(&host->pending_packet_queue)) {
978 skb = skb_peek(&host->pending_packet_queue);
979
980 packet = (struct hpsb_packet *)skb->data;
981
982 if (time_before(packet->sendtime + expire, jiffies)) {
David S. Miller8728b832005-08-09 19:25:21 -0700983 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 packet->state = hpsb_complete;
985 packet->ack_code = ACKX_TIMEOUT;
986 queue_packet_complete(packet);
987 } else {
988 /* Since packets are added to the tail, the oldest
989 * ones are first, always. When we get to one that
990 * isn't timed out, the rest aren't either. */
991 break;
992 }
993 }
994
995 if (!skb_queue_empty(&host->pending_packet_queue))
996 mod_timer(&host->timeout, jiffies + host->timeout_interval);
997
998 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
999}
1000
1001
1002/* Kernel thread and vars, which handles packets that are completed. Only
1003 * packets that have a "complete" function are sent here. This way, the
1004 * completion is run out of kernel context, and doesn't block the rest of
1005 * the stack. */
Ben Collinsf6542402006-06-12 18:15:50 -04001006static struct task_struct *khpsbpkt_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007static struct sk_buff_head hpsbpkt_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009static void queue_packet_complete(struct hpsb_packet *packet)
1010{
1011 if (packet->no_waiter) {
1012 hpsb_free_packet(packet);
1013 return;
1014 }
1015 if (packet->complete_routine != NULL) {
1016 skb_queue_tail(&hpsbpkt_queue, packet->skb);
Ben Collinsf6542402006-06-12 18:15:50 -04001017 wake_up_process(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 return;
1020}
1021
1022static int hpsbpkt_thread(void *__hi)
1023{
1024 struct sk_buff *skb;
1025 struct hpsb_packet *packet;
1026 void (*complete_routine)(void*);
1027 void *complete_data;
1028
Dave Jones8c1d2862006-01-06 00:18:38 -08001029 current->flags |= PF_NOFREEZE;
1030
Ben Collinsf6542402006-06-12 18:15:50 -04001031 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1033 packet = (struct hpsb_packet *)skb->data;
1034
1035 complete_routine = packet->complete_routine;
1036 complete_data = packet->complete_data;
1037
1038 packet->complete_routine = packet->complete_data = NULL;
1039
1040 complete_routine(complete_data);
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Ben Collinsf6542402006-06-12 18:15:50 -04001043 set_current_state(TASK_INTERRUPTIBLE);
1044 if (!skb_peek(&hpsbpkt_queue))
1045 schedule();
1046 __set_current_state(TASK_RUNNING);
1047 }
1048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static int __init ieee1394_init(void)
1052{
1053 int i, ret;
1054
1055 skb_queue_head_init(&hpsbpkt_queue);
1056
1057 /* non-fatal error */
1058 if (hpsb_init_config_roms()) {
1059 HPSB_ERR("Failed to initialize some config rom entries.\n");
1060 HPSB_ERR("Some features may not be available\n");
1061 }
1062
Ben Collinsf6542402006-06-12 18:15:50 -04001063 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1064 if (IS_ERR(khpsbpkt_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 HPSB_ERR("Failed to start hpsbpkt thread!\n");
Ben Collinsf6542402006-06-12 18:15:50 -04001066 ret = PTR_ERR(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto exit_cleanup_config_roms;
1068 }
1069
1070 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1071 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1072 ret = -ENODEV;
1073 goto exit_release_kernel_thread;
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ret = bus_register(&ieee1394_bus_type);
1077 if (ret < 0) {
1078 HPSB_INFO("bus register failed");
Stefan Richtera8748442006-03-28 19:55:41 -05001079 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 for (i = 0; fw_bus_attrs[i]; i++) {
1083 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1084 if (ret < 0) {
1085 while (i >= 0) {
1086 bus_remove_file(&ieee1394_bus_type,
1087 fw_bus_attrs[i--]);
1088 }
1089 bus_unregister(&ieee1394_bus_type);
Stefan Richtera8748442006-03-28 19:55:41 -05001090 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 }
1093
1094 ret = class_register(&hpsb_host_class);
1095 if (ret < 0)
1096 goto release_all_bus;
1097
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001098 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (IS_ERR(hpsb_protocol_class)) {
1100 ret = PTR_ERR(hpsb_protocol_class);
1101 goto release_class_host;
1102 }
1103
1104 ret = init_csr();
1105 if (ret) {
1106 HPSB_INFO("init csr failed");
1107 ret = -ENOMEM;
1108 goto release_class_protocol;
1109 }
1110
1111 if (disable_nodemgr) {
1112 HPSB_INFO("nodemgr and IRM functionality disabled");
1113 /* We shouldn't contend for IRM with nodemgr disabled, since
1114 nodemgr implements functionality required of ieee1394a-2000
1115 IRMs */
1116 hpsb_disable_irm = 1;
Stefan Richter741854e2005-12-01 18:52:03 -05001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119 }
1120
1121 if (hpsb_disable_irm) {
1122 HPSB_INFO("IRM functionality disabled");
1123 }
1124
1125 ret = init_ieee1394_nodemgr();
1126 if (ret < 0) {
1127 HPSB_INFO("init nodemgr failed");
1128 goto cleanup_csr;
1129 }
1130
1131 return 0;
1132
1133cleanup_csr:
1134 cleanup_csr();
1135release_class_protocol:
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001136 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137release_class_host:
1138 class_unregister(&hpsb_host_class);
1139release_all_bus:
1140 for (i = 0; fw_bus_attrs[i]; i++)
1141 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1142 bus_unregister(&ieee1394_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143release_chrdev:
1144 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1145exit_release_kernel_thread:
Ben Collinsf6542402006-06-12 18:15:50 -04001146 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147exit_cleanup_config_roms:
1148 hpsb_cleanup_config_roms();
1149 return ret;
1150}
1151
1152static void __exit ieee1394_cleanup(void)
1153{
1154 int i;
1155
1156 if (!disable_nodemgr)
1157 cleanup_ieee1394_nodemgr();
1158
1159 cleanup_csr();
1160
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001161 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 class_unregister(&hpsb_host_class);
1163 for (i = 0; fw_bus_attrs[i]; i++)
1164 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1165 bus_unregister(&ieee1394_bus_type);
1166
Ben Collinsf6542402006-06-12 18:15:50 -04001167 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 hpsb_cleanup_config_roms();
1170
1171 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174module_init(ieee1394_init);
1175module_exit(ieee1394_cleanup);
1176
1177/* Exported symbols */
1178
1179/** hosts.c **/
1180EXPORT_SYMBOL(hpsb_alloc_host);
1181EXPORT_SYMBOL(hpsb_add_host);
1182EXPORT_SYMBOL(hpsb_remove_host);
1183EXPORT_SYMBOL(hpsb_update_config_rom_image);
1184
1185/** ieee1394_core.c **/
1186EXPORT_SYMBOL(hpsb_speedto_str);
1187EXPORT_SYMBOL(hpsb_protocol_class);
1188EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1189EXPORT_SYMBOL(hpsb_alloc_packet);
1190EXPORT_SYMBOL(hpsb_free_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL(hpsb_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192EXPORT_SYMBOL(hpsb_reset_bus);
1193EXPORT_SYMBOL(hpsb_bus_reset);
1194EXPORT_SYMBOL(hpsb_selfid_received);
1195EXPORT_SYMBOL(hpsb_selfid_complete);
1196EXPORT_SYMBOL(hpsb_packet_sent);
1197EXPORT_SYMBOL(hpsb_packet_received);
1198EXPORT_SYMBOL_GPL(hpsb_disable_irm);
Ben Collins1934b8b2005-07-09 20:01:23 -04001199#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
1200EXPORT_SYMBOL(hpsb_send_phy_config);
1201EXPORT_SYMBOL(hpsb_send_packet_and_wait);
1202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204/** ieee1394_transactions.c **/
1205EXPORT_SYMBOL(hpsb_get_tlabel);
1206EXPORT_SYMBOL(hpsb_free_tlabel);
1207EXPORT_SYMBOL(hpsb_make_readpacket);
1208EXPORT_SYMBOL(hpsb_make_writepacket);
1209EXPORT_SYMBOL(hpsb_make_streampacket);
1210EXPORT_SYMBOL(hpsb_make_lockpacket);
1211EXPORT_SYMBOL(hpsb_make_lock64packet);
1212EXPORT_SYMBOL(hpsb_make_phypacket);
1213EXPORT_SYMBOL(hpsb_make_isopacket);
1214EXPORT_SYMBOL(hpsb_read);
1215EXPORT_SYMBOL(hpsb_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216EXPORT_SYMBOL(hpsb_packet_success);
1217
1218/** highlevel.c **/
1219EXPORT_SYMBOL(hpsb_register_highlevel);
1220EXPORT_SYMBOL(hpsb_unregister_highlevel);
1221EXPORT_SYMBOL(hpsb_register_addrspace);
1222EXPORT_SYMBOL(hpsb_unregister_addrspace);
1223EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1224EXPORT_SYMBOL(hpsb_listen_channel);
1225EXPORT_SYMBOL(hpsb_unlisten_channel);
1226EXPORT_SYMBOL(hpsb_get_hostinfo);
1227EXPORT_SYMBOL(hpsb_create_hostinfo);
1228EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1229EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1230EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1231EXPORT_SYMBOL(hpsb_set_hostinfo);
Ben Collins1934b8b2005-07-09 20:01:23 -04001232EXPORT_SYMBOL(highlevel_host_reset);
1233#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234EXPORT_SYMBOL(highlevel_add_host);
1235EXPORT_SYMBOL(highlevel_remove_host);
Ben Collins1934b8b2005-07-09 20:01:23 -04001236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238/** nodemgr.c **/
1239EXPORT_SYMBOL(hpsb_node_fill_packet);
1240EXPORT_SYMBOL(hpsb_node_write);
1241EXPORT_SYMBOL(hpsb_register_protocol);
1242EXPORT_SYMBOL(hpsb_unregister_protocol);
1243EXPORT_SYMBOL(ieee1394_bus_type);
Ben Collins1934b8b2005-07-09 20:01:23 -04001244#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245EXPORT_SYMBOL(nodemgr_for_each_host);
Ben Collins1934b8b2005-07-09 20:01:23 -04001246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248/** csr.c **/
1249EXPORT_SYMBOL(hpsb_update_config_rom);
1250
1251/** dma.c **/
1252EXPORT_SYMBOL(dma_prog_region_init);
1253EXPORT_SYMBOL(dma_prog_region_alloc);
1254EXPORT_SYMBOL(dma_prog_region_free);
1255EXPORT_SYMBOL(dma_region_init);
1256EXPORT_SYMBOL(dma_region_alloc);
1257EXPORT_SYMBOL(dma_region_free);
1258EXPORT_SYMBOL(dma_region_sync_for_cpu);
1259EXPORT_SYMBOL(dma_region_sync_for_device);
1260EXPORT_SYMBOL(dma_region_mmap);
1261EXPORT_SYMBOL(dma_region_offset_to_bus);
1262
1263/** iso.c **/
1264EXPORT_SYMBOL(hpsb_iso_xmit_init);
1265EXPORT_SYMBOL(hpsb_iso_recv_init);
1266EXPORT_SYMBOL(hpsb_iso_xmit_start);
1267EXPORT_SYMBOL(hpsb_iso_recv_start);
1268EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1269EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1270EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1271EXPORT_SYMBOL(hpsb_iso_stop);
1272EXPORT_SYMBOL(hpsb_iso_shutdown);
1273EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1274EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1275EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1276EXPORT_SYMBOL(hpsb_iso_n_ready);
1277EXPORT_SYMBOL(hpsb_iso_packet_sent);
1278EXPORT_SYMBOL(hpsb_iso_packet_received);
1279EXPORT_SYMBOL(hpsb_iso_wake);
1280EXPORT_SYMBOL(hpsb_iso_recv_flush);
1281
1282/** csr1212.c **/
Ben Collins1934b8b2005-07-09 20:01:23 -04001283EXPORT_SYMBOL(csr1212_new_directory);
1284EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1285EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1286EXPORT_SYMBOL(csr1212_release_keyval);
1287EXPORT_SYMBOL(csr1212_read);
1288EXPORT_SYMBOL(csr1212_parse_keyval);
1289EXPORT_SYMBOL(_csr1212_read_keyval);
1290EXPORT_SYMBOL(_csr1212_destroy_keyval);
1291#ifdef CONFIG_IEEE1394_EXPORT_FULL_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292EXPORT_SYMBOL(csr1212_create_csr);
1293EXPORT_SYMBOL(csr1212_init_local_csr);
1294EXPORT_SYMBOL(csr1212_new_immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295EXPORT_SYMBOL(csr1212_associate_keyval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296EXPORT_SYMBOL(csr1212_new_string_descriptor_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297EXPORT_SYMBOL(csr1212_destroy_csr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298EXPORT_SYMBOL(csr1212_generate_csr_image);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299EXPORT_SYMBOL(csr1212_parse_csr);
Ben Collins1934b8b2005-07-09 20:01:23 -04001300#endif