blob: d791d08c743ce22d100ad34e440f3cdb932ca30f [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>
Pieter Palmers3dc5ea92007-02-03 17:44:39 +010036#include <linux/preempt.h>
37#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Pieter Palmers3dc5ea92007-02-03 17:44:39 +010039#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "ieee1394_types.h"
43#include "ieee1394.h"
44#include "hosts.h"
45#include "ieee1394_core.h"
46#include "highlevel.h"
47#include "ieee1394_transactions.h"
48#include "csr.h"
49#include "nodemgr.h"
50#include "dma.h"
51#include "iso.h"
52#include "config_roms.h"
53
54/*
55 * Disable the nodemgr detection and config rom reading functionality.
56 */
Ben Collins1934b8b2005-07-09 20:01:23 -040057static int disable_nodemgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058module_param(disable_nodemgr, int, 0444);
59MODULE_PARM_DESC(disable_nodemgr, "Disable nodemgr functionality.");
60
61/* Disable Isochronous Resource Manager functionality */
62int hpsb_disable_irm = 0;
Stefan Richter23e93f12006-03-28 20:03:34 -050063module_param_named(disable_irm, hpsb_disable_irm, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_PARM_DESC(disable_irm,
65 "Disable Isochronous Resource Manager functionality.");
66
67/* We are GPL, so treat us special */
68MODULE_LICENSE("GPL");
69
70/* Some globals used */
71const char *hpsb_speedto_str[] = { "S100", "S200", "S400", "S800", "S1600", "S3200" };
gregkh@suse.de7e25ab92005-03-23 09:53:36 -080072struct class *hpsb_protocol_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#ifdef CONFIG_IEEE1394_VERBOSEDEBUG
Jody McIntyredb2fd662005-09-30 11:59:09 -070075static void dump_packet(const char *text, quadlet_t *data, int size, int speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int i;
78
79 size /= 4;
80 size = (size > 4 ? 4 : size);
81
82 printk(KERN_DEBUG "ieee1394: %s", text);
Jody McIntyredb2fd662005-09-30 11:59:09 -070083 if (speed > -1 && speed < 6)
84 printk(" at %s", hpsb_speedto_str[speed]);
85 printk(":");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 for (i = 0; i < size; i++)
87 printk(" %08x", data[i]);
88 printk("\n");
89}
90#else
Stefan Richter611aa192006-08-02 18:44:00 +020091#define dump_packet(a,b,c,d) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif
93
94static void abort_requests(struct hpsb_host *host);
95static void queue_packet_complete(struct hpsb_packet *packet);
96
97
98/**
99 * hpsb_set_packet_complete_task - set the task that runs when a packet
100 * completes. You cannot call this more than once on a single packet
101 * before it is sent.
102 *
103 * @packet: the packet whose completion we want the task added to
104 * @routine: function to call
105 * @data: data (if any) to pass to the above function
106 */
107void hpsb_set_packet_complete_task(struct hpsb_packet *packet,
108 void (*routine)(void *), void *data)
109{
110 WARN_ON(packet->complete_routine != NULL);
111 packet->complete_routine = routine;
112 packet->complete_data = data;
113 return;
114}
115
116/**
117 * hpsb_alloc_packet - allocate new packet structure
118 * @data_size: size of the data block to be allocated
119 *
120 * This function allocates, initializes and returns a new &struct hpsb_packet.
121 * It can be used in interrupt context. A header block is always included, its
122 * size is big enough to contain all possible 1394 headers. The data block is
123 * only allocated when @data_size is not zero.
124 *
125 * For packets for which responses will be received the @data_size has to be big
126 * enough to contain the response's data block since no further allocation
127 * occurs at response matching time.
128 *
129 * The packet's generation value will be set to the current generation number
130 * for ease of use. Remember to overwrite it with your own recorded generation
131 * number if you can not be sure that your code will not race with a bus reset.
132 *
133 * Return value: A pointer to a &struct hpsb_packet or NULL on allocation
134 * failure.
135 */
136struct hpsb_packet *hpsb_alloc_packet(size_t data_size)
137{
138 struct hpsb_packet *packet = NULL;
139 struct sk_buff *skb;
140
141 data_size = ((data_size + 3) & ~3);
142
143 skb = alloc_skb(data_size + sizeof(*packet), GFP_ATOMIC);
144 if (skb == NULL)
145 return NULL;
146
147 memset(skb->data, 0, data_size + sizeof(*packet));
148
149 packet = (struct hpsb_packet *)skb->data;
150 packet->skb = skb;
151
152 packet->header = packet->embedded_header;
153 packet->state = hpsb_unused;
154 packet->generation = -1;
155 INIT_LIST_HEAD(&packet->driver_list);
156 atomic_set(&packet->refcnt, 1);
157
158 if (data_size) {
159 packet->data = (quadlet_t *)(skb->data + sizeof(*packet));
160 packet->data_size = data_size;
161 }
162
163 return packet;
164}
165
166
167/**
168 * hpsb_free_packet - free packet and data associated with it
169 * @packet: packet to free (is NULL safe)
170 *
171 * This function will free packet->data and finally the packet itself.
172 */
173void hpsb_free_packet(struct hpsb_packet *packet)
174{
175 if (packet && atomic_dec_and_test(&packet->refcnt)) {
176 BUG_ON(!list_empty(&packet->driver_list));
177 kfree_skb(packet->skb);
178 }
179}
180
181
182int hpsb_reset_bus(struct hpsb_host *host, int type)
183{
Stefan Richter741854e2005-12-01 18:52:03 -0500184 if (!host->in_bus_reset) {
185 host->driver->devctl(host, RESET_BUS, type);
186 return 0;
187 } else {
188 return 1;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Pieter Palmers3dc5ea92007-02-03 17:44:39 +0100192/**
193 * hpsb_read_cycle_timer - read cycle timer register and system time
194 * @host: host whose isochronous cycle timer register is read
195 * @cycle_timer: address of bitfield to return the register contents
196 * @local_time: address to return the system time
197 *
198 * The format of * @cycle_timer, is described in OHCI 1.1 clause 5.13. This
199 * format is also read from non-OHCI controllers. * @local_time contains the
200 * system time in microseconds since the Epoch, read at the moment when the
201 * cycle timer was read.
202 *
203 * Return value: 0 for success or error number otherwise.
204 */
205int hpsb_read_cycle_timer(struct hpsb_host *host, u32 *cycle_timer,
206 u64 *local_time)
207{
208 int ctr;
209 struct timeval tv;
210 unsigned long flags;
211
212 if (!host || !cycle_timer || !local_time)
213 return -EINVAL;
214
215 preempt_disable();
216 local_irq_save(flags);
217
218 ctr = host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
219 if (ctr)
220 do_gettimeofday(&tv);
221
222 local_irq_restore(flags);
223 preempt_enable();
224
225 if (!ctr)
226 return -EIO;
227 *cycle_timer = ctr;
228 *local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
229 return 0;
230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232int hpsb_bus_reset(struct hpsb_host *host)
233{
Stefan Richter741854e2005-12-01 18:52:03 -0500234 if (host->in_bus_reset) {
235 HPSB_NOTICE("%s called while bus reset already in progress",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 __FUNCTION__);
Stefan Richter741854e2005-12-01 18:52:03 -0500237 return 1;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Stefan Richter741854e2005-12-01 18:52:03 -0500240 abort_requests(host);
241 host->in_bus_reset = 1;
242 host->irm_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 host->is_irm = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500244 host->busmgr_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 host->is_busmgr = 0;
246 host->is_cycmst = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500247 host->node_count = 0;
248 host->selfid_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Stefan Richter741854e2005-12-01 18:52:03 -0500250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253
254/*
255 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
256 * case verification failed.
257 */
258static int check_selfids(struct hpsb_host *host)
259{
Stefan Richter741854e2005-12-01 18:52:03 -0500260 int nodeid = -1;
261 int rest_of_selfids = host->selfid_count;
262 struct selfid *sid = (struct selfid *)host->topology_map;
263 struct ext_selfid *esid;
264 int esid_seq = 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 host->nodes_active = 0;
267
Stefan Richter741854e2005-12-01 18:52:03 -0500268 while (rest_of_selfids--) {
269 if (!sid->extended) {
270 nodeid++;
271 esid_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Stefan Richter741854e2005-12-01 18:52:03 -0500273 if (sid->phy_id != nodeid) {
274 HPSB_INFO("SelfIDs failed monotony check with "
275 "%d", sid->phy_id);
276 return 0;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (sid->link_active) {
280 host->nodes_active++;
281 if (sid->contender)
282 host->irm_id = LOCAL_BUS | sid->phy_id;
283 }
Stefan Richter741854e2005-12-01 18:52:03 -0500284 } else {
285 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Stefan Richter741854e2005-12-01 18:52:03 -0500287 if ((esid->phy_id != nodeid)
288 || (esid->seq_nr != esid_seq)) {
289 HPSB_INFO("SelfIDs failed monotony check with "
290 "%d/%d", esid->phy_id, esid->seq_nr);
291 return 0;
292 }
293 esid_seq++;
294 }
295 sid++;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Stefan Richter741854e2005-12-01 18:52:03 -0500298 esid = (struct ext_selfid *)(sid - 1);
299 while (esid->extended) {
300 if ((esid->porta == SELFID_PORT_PARENT) ||
Stefan Richterd7758462005-12-01 18:51:56 -0500301 (esid->portb == SELFID_PORT_PARENT) ||
302 (esid->portc == SELFID_PORT_PARENT) ||
303 (esid->portd == SELFID_PORT_PARENT) ||
304 (esid->porte == SELFID_PORT_PARENT) ||
305 (esid->portf == SELFID_PORT_PARENT) ||
306 (esid->portg == SELFID_PORT_PARENT) ||
307 (esid->porth == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 HPSB_INFO("SelfIDs failed root check on "
309 "extended SelfID");
310 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500311 }
312 esid--;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Stefan Richter741854e2005-12-01 18:52:03 -0500315 sid = (struct selfid *)esid;
Stefan Richterd7758462005-12-01 18:51:56 -0500316 if ((sid->port0 == SELFID_PORT_PARENT) ||
317 (sid->port1 == SELFID_PORT_PARENT) ||
318 (sid->port2 == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 HPSB_INFO("SelfIDs failed root check");
320 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 host->node_count = nodeid + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500324 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static void build_speed_map(struct hpsb_host *host, int nodecount)
328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 u8 cldcnt[nodecount];
Stefan Richter741854e2005-12-01 18:52:03 -0500330 u8 *map = host->speed_map;
Ben Collins647dcb52006-06-12 18:12:37 -0400331 u8 *speedcap = host->speed;
Stefan Richter741854e2005-12-01 18:52:03 -0500332 struct selfid *sid;
333 struct ext_selfid *esid;
334 int i, j, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Stefan Richter741854e2005-12-01 18:52:03 -0500336 for (i = 0; i < (nodecount * 64); i += 64) {
337 for (j = 0; j < nodecount; j++) {
338 map[i+j] = IEEE1394_SPEED_MAX;
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Stefan Richter741854e2005-12-01 18:52:03 -0500342 for (i = 0; i < nodecount; i++) {
343 cldcnt[i] = 0;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Stefan Richter741854e2005-12-01 18:52:03 -0500346 /* find direct children count and speed */
347 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
348 n = nodecount - 1;
349 (void *)sid >= (void *)host->topology_map; sid--) {
350 if (sid->extended) {
351 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Stefan Richterd7758462005-12-01 18:51:56 -0500353 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
354 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
355 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
356 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
357 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
358 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
359 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
360 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 } else {
Stefan Richterd7758462005-12-01 18:51:56 -0500362 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
363 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
364 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Stefan Richter741854e2005-12-01 18:52:03 -0500366 speedcap[n] = sid->speed;
367 n--;
368 }
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Stefan Richter741854e2005-12-01 18:52:03 -0500371 /* set self mapping */
372 for (i = 0; i < nodecount; i++) {
373 map[64*i + i] = speedcap[i];
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Stefan Richter741854e2005-12-01 18:52:03 -0500376 /* fix up direct children count to total children count;
377 * also fix up speedcaps for sibling and parent communication */
378 for (i = 1; i < nodecount; i++) {
379 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
380 cldcnt[i] += cldcnt[n];
381 speedcap[n] = min(speedcap[n], speedcap[i]);
382 n -= cldcnt[n] + 1;
383 }
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Stefan Richter741854e2005-12-01 18:52:03 -0500386 for (n = 0; n < nodecount; n++) {
387 for (i = n - cldcnt[n]; i <= n; i++) {
388 for (j = 0; j < (n - cldcnt[n]); j++) {
389 map[j*64 + i] = map[i*64 + j] =
390 min(map[i*64 + j], speedcap[n]);
391 }
392 for (j = n + 1; j < nodecount; j++) {
393 map[j*64 + i] = map[i*64 + j] =
394 min(map[i*64 + j], speedcap[n]);
395 }
396 }
397 }
Ben Collins647dcb52006-06-12 18:12:37 -0400398
Stefan Richter433a87d2006-07-03 12:02:27 -0400399#if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
Ben Collins647dcb52006-06-12 18:12:37 -0400400 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
401 for (n = 0; n < nodecount; n++)
Stefan Richter433a87d2006-07-03 12:02:27 -0400402 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
Ben Collins647dcb52006-06-12 18:12:37 -0400403 speedcap[n] = IEEE1394_SPEED_MAX;
Stefan Richter433a87d2006-07-03 12:02:27 -0400404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407
408void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
409{
Stefan Richter741854e2005-12-01 18:52:03 -0500410 if (host->in_bus_reset) {
411 HPSB_VERBOSE("Including SelfID 0x%x", sid);
412 host->topology_map[host->selfid_count++] = sid;
413 } else {
414 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sid, NODEID_TO_BUS(host->node_id));
Stefan Richter741854e2005-12-01 18:52:03 -0500416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
420{
421 if (!host->in_bus_reset)
422 HPSB_NOTICE("SelfID completion called outside of bus reset!");
423
Stefan Richter741854e2005-12-01 18:52:03 -0500424 host->node_id = LOCAL_BUS | phyid;
425 host->is_root = isroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Stefan Richter741854e2005-12-01 18:52:03 -0500427 if (!check_selfids(host)) {
428 if (host->reset_retries++ < 20) {
429 /* selfid stage did not complete without error */
430 HPSB_NOTICE("Error in SelfID stage, resetting");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 host->in_bus_reset = 0;
432 /* this should work from ohci1394 now... */
Stefan Richter741854e2005-12-01 18:52:03 -0500433 hpsb_reset_bus(host, LONG_RESET);
434 return;
435 } else {
436 HPSB_NOTICE("Stopping out-of-control reset loop");
437 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500439 }
440 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500442 build_speed_map(host, host->node_count);
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
446 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
447
Stefan Richter741854e2005-12-01 18:52:03 -0500448 /* irm_id is kept up to date by check_selfids() */
449 if (host->irm_id == host->node_id) {
450 host->is_irm = 1;
451 } else {
452 host->is_busmgr = 0;
453 host->is_irm = 0;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Stefan Richter741854e2005-12-01 18:52:03 -0500456 if (isroot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
458 host->is_cycmst = 1;
459 }
460 atomic_inc(&host->generation);
461 host->in_bus_reset = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500462 highlevel_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465
466void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
Stefan Richter741854e2005-12-01 18:52:03 -0500467 int ackcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned long flags;
470
471 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
472
473 packet->ack_code = ackcode;
474
475 if (packet->no_waiter || packet->state == hpsb_complete) {
476 /* if packet->no_waiter, must not have a tlabel allocated */
477 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
478 hpsb_free_packet(packet);
479 return;
480 }
481
482 atomic_dec(&packet->refcnt); /* drop HC's reference */
483 /* here the packet must be on the host->pending_packet_queue */
484
485 if (ackcode != ACK_PENDING || !packet->expect_response) {
486 packet->state = hpsb_complete;
487 __skb_unlink(packet->skb, &host->pending_packet_queue);
488 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
489 queue_packet_complete(packet);
490 return;
491 }
492
493 packet->state = hpsb_pending;
494 packet->sendtime = jiffies;
495
496 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
497
498 mod_timer(&host->timeout, jiffies + host->timeout_interval);
499}
500
501/**
502 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
503 * @host: host that PHY config packet gets sent through
504 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
505 * @gapcnt: gap count value to set (-1 = don't set gap count)
506 *
507 * This function sends a PHY config packet on the bus through the specified host.
508 *
509 * Return value: 0 for success or error number otherwise.
510 */
511int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
512{
513 struct hpsb_packet *packet;
Stefan Richter546513f2005-12-01 18:52:01 -0500514 quadlet_t d = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int retval = 0;
516
517 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
518 (rootid == -1 && gapcnt == -1)) {
519 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
520 rootid, gapcnt);
521 return -EINVAL;
522 }
523
Stefan Richter546513f2005-12-01 18:52:01 -0500524 if (rootid != -1)
525 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
526 if (gapcnt != -1)
527 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
528
529 packet = hpsb_make_phypacket(host, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!packet)
531 return -ENOMEM;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 packet->generation = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 retval = hpsb_send_packet_and_wait(packet);
535 hpsb_free_packet(packet);
536
537 return retval;
538}
539
540/**
541 * hpsb_send_packet - transmit a packet on the bus
542 * @packet: packet to send
543 *
544 * The packet is sent through the host specified in the packet->host field.
545 * Before sending, the packet's transmit speed is automatically determined
546 * using the local speed map when it is an async, non-broadcast packet.
547 *
548 * Possibilities for failure are that host is either not initialized, in bus
549 * reset, the packet's generation number doesn't match the current generation
550 * number or the host reports a transmit error.
551 *
552 * Return value: 0 on success, negative errno on failure.
553 */
554int hpsb_send_packet(struct hpsb_packet *packet)
555{
556 struct hpsb_host *host = packet->host;
557
Stefan Richter741854e2005-12-01 18:52:03 -0500558 if (host->is_shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -EINVAL;
560 if (host->in_bus_reset ||
561 (packet->generation != get_hpsb_generation(host)))
Stefan Richter741854e2005-12-01 18:52:03 -0500562 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Stefan Richter741854e2005-12-01 18:52:03 -0500564 packet->state = hpsb_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* This just seems silly to me */
567 WARN_ON(packet->no_waiter && packet->expect_response);
568
569 if (!packet->no_waiter || packet->expect_response) {
570 atomic_inc(&packet->refcnt);
Ben Collins1934b8b2005-07-09 20:01:23 -0400571 /* Set the initial "sendtime" to 10 seconds from now, to
572 prevent premature expiry. If a packet takes more than
573 10 seconds to hit the wire, we have bigger problems :) */
Jody McIntyre6262d062005-05-16 21:54:05 -0700574 packet->sendtime = jiffies + 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 skb_queue_tail(&host->pending_packet_queue, packet->skb);
576 }
577
Stefan Richter741854e2005-12-01 18:52:03 -0500578 if (packet->node_id == host->node_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* it is a local request, so handle it locally */
580
Stefan Richter741854e2005-12-01 18:52:03 -0500581 quadlet_t *data;
582 size_t size = packet->data_size + packet->header_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Stefan Richter741854e2005-12-01 18:52:03 -0500584 data = kmalloc(size, GFP_ATOMIC);
585 if (!data) {
586 HPSB_ERR("unable to allocate memory for concatenating header and data");
587 return -ENOMEM;
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Stefan Richter741854e2005-12-01 18:52:03 -0500590 memcpy(data, packet->header, packet->header_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Stefan Richter741854e2005-12-01 18:52:03 -0500592 if (packet->data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
594
Stefan Richter741854e2005-12-01 18:52:03 -0500595 dump_packet("send packet local", packet->header, packet->header_size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Stefan Richter741854e2005-12-01 18:52:03 -0500597 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
598 hpsb_packet_received(host, data, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Stefan Richter741854e2005-12-01 18:52:03 -0500600 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Stefan Richter741854e2005-12-01 18:52:03 -0500602 return 0;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Ben Collins647dcb52006-06-12 18:12:37 -0400605 if (packet->type == hpsb_async &&
606 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
Stefan Richter741854e2005-12-01 18:52:03 -0500607 packet->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -0400608 host->speed[NODEID_TO_NODE(packet->node_id)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Stefan Richter741854e2005-12-01 18:52:03 -0500610 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Stefan Richter741854e2005-12-01 18:52:03 -0500612 return host->driver->transmit_packet(host, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/* We could just use complete() directly as the packet complete
616 * callback, but this is more typesafe, in the sense that we get a
617 * compiler error if the prototype for complete() changes. */
618
619static void complete_packet(void *data)
620{
621 complete((struct completion *) data);
622}
623
624int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
625{
626 struct completion done;
627 int retval;
628
629 init_completion(&done);
630 hpsb_set_packet_complete_task(packet, complete_packet, &done);
631 retval = hpsb_send_packet(packet);
632 if (retval == 0)
633 wait_for_completion(&done);
634
635 return retval;
636}
637
638static void send_packet_nocare(struct hpsb_packet *packet)
639{
Stefan Richter741854e2005-12-01 18:52:03 -0500640 if (hpsb_send_packet(packet) < 0) {
641 hpsb_free_packet(packet);
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645
646static void handle_packet_response(struct hpsb_host *host, int tcode,
647 quadlet_t *data, size_t size)
648{
Stefan Richter741854e2005-12-01 18:52:03 -0500649 struct hpsb_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct sk_buff *skb;
Stefan Richter741854e2005-12-01 18:52:03 -0500651 int tcode_match = 0;
652 int tlabel;
653 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Stefan Richter741854e2005-12-01 18:52:03 -0500655 tlabel = (data[0] >> 10) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
658
659 skb_queue_walk(&host->pending_packet_queue, skb) {
660 packet = (struct hpsb_packet *)skb->data;
Stefan Richter741854e2005-12-01 18:52:03 -0500661 if ((packet->tlabel == tlabel)
662 && (packet->node_id == (data[1] >> 16))){
663 break;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 packet = NULL;
Stefan Richter741854e2005-12-01 18:52:03 -0500667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (packet == NULL) {
Stefan Richter741854e2005-12-01 18:52:03 -0500670 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
671 dump_packet("contents", data, 16, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500673 return;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Stefan Richter741854e2005-12-01 18:52:03 -0500676 switch (packet->tcode) {
677 case TCODE_WRITEQ:
678 case TCODE_WRITEB:
679 if (tcode != TCODE_WRITE_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 tcode_match = 1;
682 memcpy(packet->header, data, 12);
Stefan Richter741854e2005-12-01 18:52:03 -0500683 break;
684 case TCODE_READQ:
685 if (tcode != TCODE_READQ_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687 tcode_match = 1;
688 memcpy(packet->header, data, 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500689 break;
690 case TCODE_READB:
691 if (tcode != TCODE_READB_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 tcode_match = 1;
694 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
695 memcpy(packet->header, data, 16);
696 memcpy(packet->data, data + 4, size - 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500697 break;
698 case TCODE_LOCK_REQUEST:
699 if (tcode != TCODE_LOCK_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701 tcode_match = 1;
702 size = min((size - 16), (size_t)8);
703 BUG_ON(packet->skb->len - sizeof(*packet) < size);
704 memcpy(packet->header, data, 16);
705 memcpy(packet->data, data + 4, size);
Stefan Richter741854e2005-12-01 18:52:03 -0500706 break;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Stefan Richter741854e2005-12-01 18:52:03 -0500709 if (!tcode_match) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500711 HPSB_INFO("unsolicited response packet received - tcode mismatch");
712 dump_packet("contents", data, 16, -1);
713 return;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
David S. Miller8728b832005-08-09 19:25:21 -0700716 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (packet->state == hpsb_queued) {
719 packet->sendtime = jiffies;
720 packet->ack_code = ACK_PENDING;
721 }
722
723 packet->state = hpsb_complete;
724 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
725
726 queue_packet_complete(packet);
727}
728
729
730static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
731 quadlet_t *data, size_t dsize)
732{
Stefan Richter741854e2005-12-01 18:52:03 -0500733 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Stefan Richter741854e2005-12-01 18:52:03 -0500735 p = hpsb_alloc_packet(dsize);
736 if (unlikely(p == NULL)) {
737 /* FIXME - send data_error response */
738 return NULL;
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Stefan Richter741854e2005-12-01 18:52:03 -0500741 p->type = hpsb_async;
742 p->state = hpsb_unused;
743 p->host = host;
744 p->node_id = data[1] >> 16;
745 p->tlabel = (data[0] >> 10) & 0x3f;
746 p->no_waiter = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 p->generation = get_hpsb_generation(host);
749
750 if (dsize % 4)
751 p->data[dsize / 4] = 0;
752
Stefan Richter741854e2005-12-01 18:52:03 -0500753 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756#define PREP_ASYNC_HEAD_RCODE(tc) \
757 packet->tcode = tc; \
758 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
759 | (1 << 8) | (tc << 4); \
760 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
761 packet->header[2] = 0
762
763static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500764 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
767 packet->header[3] = data;
768 packet->header_size = 16;
769 packet->data_size = 0;
770}
771
772static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500773 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 if (rcode != RCODE_COMPLETE)
776 length = 0;
777
778 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
779 packet->header[3] = length << 16;
780 packet->header_size = 16;
781 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
782}
783
784static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
785{
786 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
787 packet->header[2] = 0;
788 packet->header_size = 12;
789 packet->data_size = 0;
790}
791
792static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500793 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 if (rcode != RCODE_COMPLETE)
796 length = 0;
797
798 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
799 packet->header[3] = (length << 16) | extcode;
800 packet->header_size = 16;
801 packet->data_size = length;
802}
803
804#define PREP_REPLY_PACKET(length) \
Stefan Richter741854e2005-12-01 18:52:03 -0500805 packet = create_reply_packet(host, data, length); \
806 if (packet == NULL) break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808static void handle_incoming_packet(struct hpsb_host *host, int tcode,
809 quadlet_t *data, size_t size, int write_acked)
810{
Stefan Richter741854e2005-12-01 18:52:03 -0500811 struct hpsb_packet *packet;
812 int length, rcode, extcode;
813 quadlet_t buffer;
814 nodeid_t source = data[1] >> 16;
815 nodeid_t dest = data[0] >> 16;
816 u16 flags = (u16) data[0];
817 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Stefan Richter741854e2005-12-01 18:52:03 -0500819 /* big FIXME - no error checking is done for an out of bounds length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Stefan Richter741854e2005-12-01 18:52:03 -0500821 switch (tcode) {
822 case TCODE_WRITEQ:
823 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
824 rcode = highlevel_write(host, source, dest, data+3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 addr, 4, flags);
826
Stefan Richter741854e2005-12-01 18:52:03 -0500827 if (!write_acked
828 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
829 && (rcode >= 0)) {
830 /* not a broadcast write, reply */
831 PREP_REPLY_PACKET(0);
832 fill_async_write_resp(packet, rcode);
833 send_packet_nocare(packet);
834 }
835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Stefan Richter741854e2005-12-01 18:52:03 -0500837 case TCODE_WRITEB:
838 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
839 rcode = highlevel_write(host, source, dest, data+4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 addr, data[3]>>16, flags);
841
Stefan Richter741854e2005-12-01 18:52:03 -0500842 if (!write_acked
843 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
844 && (rcode >= 0)) {
845 /* not a broadcast write, reply */
846 PREP_REPLY_PACKET(0);
847 fill_async_write_resp(packet, rcode);
848 send_packet_nocare(packet);
849 }
850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Stefan Richter741854e2005-12-01 18:52:03 -0500852 case TCODE_READQ:
853 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
854 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Stefan Richter741854e2005-12-01 18:52:03 -0500856 if (rcode >= 0) {
857 PREP_REPLY_PACKET(0);
858 fill_async_readquad_resp(packet, rcode, buffer);
859 send_packet_nocare(packet);
860 }
861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Stefan Richter741854e2005-12-01 18:52:03 -0500863 case TCODE_READB:
864 length = data[3] >> 16;
865 PREP_REPLY_PACKET(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Stefan Richter741854e2005-12-01 18:52:03 -0500867 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
868 rcode = highlevel_read(host, source, packet->data, addr,
869 length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Stefan Richter741854e2005-12-01 18:52:03 -0500871 if (rcode >= 0) {
872 fill_async_readblock_resp(packet, rcode, length);
873 send_packet_nocare(packet);
874 } else {
875 hpsb_free_packet(packet);
876 }
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Stefan Richter741854e2005-12-01 18:52:03 -0500879 case TCODE_LOCK_REQUEST:
880 length = data[3] >> 16;
881 extcode = data[3] & 0xffff;
882 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Stefan Richter741854e2005-12-01 18:52:03 -0500884 PREP_REPLY_PACKET(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Stefan Richter741854e2005-12-01 18:52:03 -0500886 if ((extcode == 0) || (extcode >= 7)) {
887 /* let switch default handle error */
888 length = 0;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Stefan Richter741854e2005-12-01 18:52:03 -0500891 switch (length) {
892 case 4:
893 rcode = highlevel_lock(host, source, packet->data, addr,
894 data[4], 0, extcode,flags);
895 fill_async_lock_resp(packet, rcode, extcode, 4);
896 break;
897 case 8:
898 if ((extcode != EXTCODE_FETCH_ADD)
899 && (extcode != EXTCODE_LITTLE_ADD)) {
900 rcode = highlevel_lock(host, source,
901 packet->data, addr,
902 data[5], data[4],
903 extcode, flags);
904 fill_async_lock_resp(packet, rcode, extcode, 4);
905 } else {
906 rcode = highlevel_lock64(host, source,
907 (octlet_t *)packet->data, addr,
908 *(octlet_t *)(data + 4), 0ULL,
909 extcode, flags);
910 fill_async_lock_resp(packet, rcode, extcode, 8);
911 }
912 break;
913 case 16:
914 rcode = highlevel_lock64(host, source,
915 (octlet_t *)packet->data, addr,
916 *(octlet_t *)(data + 6),
917 *(octlet_t *)(data + 4),
918 extcode, flags);
919 fill_async_lock_resp(packet, rcode, extcode, 8);
920 break;
921 default:
922 rcode = RCODE_TYPE_ERROR;
923 fill_async_lock_resp(packet, rcode,
924 extcode, 0);
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stefan Richter741854e2005-12-01 18:52:03 -0500927 if (rcode >= 0) {
928 send_packet_nocare(packet);
929 } else {
930 hpsb_free_packet(packet);
931 }
932 break;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935}
936#undef PREP_REPLY_PACKET
937
938
939void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
Stefan Richter741854e2005-12-01 18:52:03 -0500940 int write_acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Stefan Richter741854e2005-12-01 18:52:03 -0500942 int tcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Stefan Richter741854e2005-12-01 18:52:03 -0500944 if (host->in_bus_reset) {
945 HPSB_INFO("received packet during reset; ignoring");
946 return;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Stefan Richter741854e2005-12-01 18:52:03 -0500949 dump_packet("received packet", data, size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Stefan Richter741854e2005-12-01 18:52:03 -0500951 tcode = (data[0] >> 4) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Stefan Richter741854e2005-12-01 18:52:03 -0500953 switch (tcode) {
954 case TCODE_WRITE_RESPONSE:
955 case TCODE_READQ_RESPONSE:
956 case TCODE_READB_RESPONSE:
957 case TCODE_LOCK_RESPONSE:
958 handle_packet_response(host, tcode, data, size);
959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Stefan Richter741854e2005-12-01 18:52:03 -0500961 case TCODE_WRITEQ:
962 case TCODE_WRITEB:
963 case TCODE_READQ:
964 case TCODE_READB:
965 case TCODE_LOCK_REQUEST:
966 handle_incoming_packet(host, tcode, data, size, write_acked);
967 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969
Stefan Richter741854e2005-12-01 18:52:03 -0500970 case TCODE_ISO_DATA:
971 highlevel_iso_receive(host, data, size);
972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Stefan Richter741854e2005-12-01 18:52:03 -0500974 case TCODE_CYCLE_START:
975 /* simply ignore this packet if it is passed on */
976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Stefan Richter741854e2005-12-01 18:52:03 -0500978 default:
979 HPSB_NOTICE("received packet with bogus transaction code %d",
980 tcode);
981 break;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985
986static void abort_requests(struct hpsb_host *host)
987{
988 struct hpsb_packet *packet;
989 struct sk_buff *skb;
990
991 host->driver->devctl(host, CANCEL_REQUESTS, 0);
992
993 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
994 packet = (struct hpsb_packet *)skb->data;
995
996 packet->state = hpsb_complete;
997 packet->ack_code = ACKX_ABORTED;
998 queue_packet_complete(packet);
999 }
1000}
1001
1002void abort_timedouts(unsigned long __opaque)
1003{
1004 struct hpsb_host *host = (struct hpsb_host *)__opaque;
1005 unsigned long flags;
1006 struct hpsb_packet *packet;
1007 struct sk_buff *skb;
1008 unsigned long expire;
1009
1010 spin_lock_irqsave(&host->csr.lock, flags);
1011 expire = host->csr.expire;
1012 spin_unlock_irqrestore(&host->csr.lock, flags);
1013
1014 /* Hold the lock around this, since we aren't dequeuing all
1015 * packets, just ones we need. */
1016 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
1017
1018 while (!skb_queue_empty(&host->pending_packet_queue)) {
1019 skb = skb_peek(&host->pending_packet_queue);
1020
1021 packet = (struct hpsb_packet *)skb->data;
1022
1023 if (time_before(packet->sendtime + expire, jiffies)) {
David S. Miller8728b832005-08-09 19:25:21 -07001024 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 packet->state = hpsb_complete;
1026 packet->ack_code = ACKX_TIMEOUT;
1027 queue_packet_complete(packet);
1028 } else {
1029 /* Since packets are added to the tail, the oldest
1030 * ones are first, always. When we get to one that
1031 * isn't timed out, the rest aren't either. */
1032 break;
1033 }
1034 }
1035
1036 if (!skb_queue_empty(&host->pending_packet_queue))
1037 mod_timer(&host->timeout, jiffies + host->timeout_interval);
1038
1039 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
1040}
1041
1042
1043/* Kernel thread and vars, which handles packets that are completed. Only
1044 * packets that have a "complete" function are sent here. This way, the
1045 * completion is run out of kernel context, and doesn't block the rest of
1046 * the stack. */
Ben Collinsf6542402006-06-12 18:15:50 -04001047static struct task_struct *khpsbpkt_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static struct sk_buff_head hpsbpkt_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050static void queue_packet_complete(struct hpsb_packet *packet)
1051{
1052 if (packet->no_waiter) {
1053 hpsb_free_packet(packet);
1054 return;
1055 }
1056 if (packet->complete_routine != NULL) {
1057 skb_queue_tail(&hpsbpkt_queue, packet->skb);
Ben Collinsf6542402006-06-12 18:15:50 -04001058 wake_up_process(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 return;
1061}
1062
1063static int hpsbpkt_thread(void *__hi)
1064{
1065 struct sk_buff *skb;
1066 struct hpsb_packet *packet;
1067 void (*complete_routine)(void*);
1068 void *complete_data;
1069
Dave Jones8c1d2862006-01-06 00:18:38 -08001070 current->flags |= PF_NOFREEZE;
1071
Ben Collinsf6542402006-06-12 18:15:50 -04001072 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1074 packet = (struct hpsb_packet *)skb->data;
1075
1076 complete_routine = packet->complete_routine;
1077 complete_data = packet->complete_data;
1078
1079 packet->complete_routine = packet->complete_data = NULL;
1080
1081 complete_routine(complete_data);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Ben Collinsf6542402006-06-12 18:15:50 -04001084 set_current_state(TASK_INTERRUPTIBLE);
1085 if (!skb_peek(&hpsbpkt_queue))
1086 schedule();
1087 __set_current_state(TASK_RUNNING);
1088 }
1089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092static int __init ieee1394_init(void)
1093{
1094 int i, ret;
1095
1096 skb_queue_head_init(&hpsbpkt_queue);
1097
1098 /* non-fatal error */
1099 if (hpsb_init_config_roms()) {
1100 HPSB_ERR("Failed to initialize some config rom entries.\n");
1101 HPSB_ERR("Some features may not be available\n");
1102 }
1103
Ben Collinsf6542402006-06-12 18:15:50 -04001104 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1105 if (IS_ERR(khpsbpkt_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 HPSB_ERR("Failed to start hpsbpkt thread!\n");
Ben Collinsf6542402006-06-12 18:15:50 -04001107 ret = PTR_ERR(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto exit_cleanup_config_roms;
1109 }
1110
1111 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1112 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1113 ret = -ENODEV;
1114 goto exit_release_kernel_thread;
1115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ret = bus_register(&ieee1394_bus_type);
1118 if (ret < 0) {
1119 HPSB_INFO("bus register failed");
Stefan Richtera8748442006-03-28 19:55:41 -05001120 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
1123 for (i = 0; fw_bus_attrs[i]; i++) {
1124 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1125 if (ret < 0) {
1126 while (i >= 0) {
1127 bus_remove_file(&ieee1394_bus_type,
1128 fw_bus_attrs[i--]);
1129 }
1130 bus_unregister(&ieee1394_bus_type);
Stefan Richtera8748442006-03-28 19:55:41 -05001131 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 }
1134
1135 ret = class_register(&hpsb_host_class);
1136 if (ret < 0)
1137 goto release_all_bus;
1138
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001139 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (IS_ERR(hpsb_protocol_class)) {
1141 ret = PTR_ERR(hpsb_protocol_class);
1142 goto release_class_host;
1143 }
1144
1145 ret = init_csr();
1146 if (ret) {
1147 HPSB_INFO("init csr failed");
1148 ret = -ENOMEM;
1149 goto release_class_protocol;
1150 }
1151
1152 if (disable_nodemgr) {
1153 HPSB_INFO("nodemgr and IRM functionality disabled");
1154 /* We shouldn't contend for IRM with nodemgr disabled, since
1155 nodemgr implements functionality required of ieee1394a-2000
1156 IRMs */
1157 hpsb_disable_irm = 1;
Stefan Richter741854e2005-12-01 18:52:03 -05001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160 }
1161
1162 if (hpsb_disable_irm) {
1163 HPSB_INFO("IRM functionality disabled");
1164 }
1165
1166 ret = init_ieee1394_nodemgr();
1167 if (ret < 0) {
1168 HPSB_INFO("init nodemgr failed");
1169 goto cleanup_csr;
1170 }
1171
1172 return 0;
1173
1174cleanup_csr:
1175 cleanup_csr();
1176release_class_protocol:
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001177 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178release_class_host:
1179 class_unregister(&hpsb_host_class);
1180release_all_bus:
1181 for (i = 0; fw_bus_attrs[i]; i++)
1182 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1183 bus_unregister(&ieee1394_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184release_chrdev:
1185 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1186exit_release_kernel_thread:
Ben Collinsf6542402006-06-12 18:15:50 -04001187 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188exit_cleanup_config_roms:
1189 hpsb_cleanup_config_roms();
1190 return ret;
1191}
1192
1193static void __exit ieee1394_cleanup(void)
1194{
1195 int i;
1196
1197 if (!disable_nodemgr)
1198 cleanup_ieee1394_nodemgr();
1199
1200 cleanup_csr();
1201
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001202 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 class_unregister(&hpsb_host_class);
1204 for (i = 0; fw_bus_attrs[i]; i++)
1205 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1206 bus_unregister(&ieee1394_bus_type);
1207
Ben Collinsf6542402006-06-12 18:15:50 -04001208 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 hpsb_cleanup_config_roms();
1211
1212 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Andi Kleen8df40832006-07-27 21:54:00 +02001215fs_initcall(ieee1394_init); /* same as ohci1394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216module_exit(ieee1394_cleanup);
1217
1218/* Exported symbols */
1219
1220/** hosts.c **/
1221EXPORT_SYMBOL(hpsb_alloc_host);
1222EXPORT_SYMBOL(hpsb_add_host);
Stefan Richter33601772007-01-07 21:49:27 +01001223EXPORT_SYMBOL(hpsb_resume_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224EXPORT_SYMBOL(hpsb_remove_host);
1225EXPORT_SYMBOL(hpsb_update_config_rom_image);
1226
1227/** ieee1394_core.c **/
1228EXPORT_SYMBOL(hpsb_speedto_str);
1229EXPORT_SYMBOL(hpsb_protocol_class);
1230EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1231EXPORT_SYMBOL(hpsb_alloc_packet);
1232EXPORT_SYMBOL(hpsb_free_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233EXPORT_SYMBOL(hpsb_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234EXPORT_SYMBOL(hpsb_reset_bus);
Pieter Palmers3dc5ea92007-02-03 17:44:39 +01001235EXPORT_SYMBOL(hpsb_read_cycle_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236EXPORT_SYMBOL(hpsb_bus_reset);
1237EXPORT_SYMBOL(hpsb_selfid_received);
1238EXPORT_SYMBOL(hpsb_selfid_complete);
1239EXPORT_SYMBOL(hpsb_packet_sent);
1240EXPORT_SYMBOL(hpsb_packet_received);
1241EXPORT_SYMBOL_GPL(hpsb_disable_irm);
1242
1243/** ieee1394_transactions.c **/
1244EXPORT_SYMBOL(hpsb_get_tlabel);
1245EXPORT_SYMBOL(hpsb_free_tlabel);
1246EXPORT_SYMBOL(hpsb_make_readpacket);
1247EXPORT_SYMBOL(hpsb_make_writepacket);
1248EXPORT_SYMBOL(hpsb_make_streampacket);
1249EXPORT_SYMBOL(hpsb_make_lockpacket);
1250EXPORT_SYMBOL(hpsb_make_lock64packet);
1251EXPORT_SYMBOL(hpsb_make_phypacket);
1252EXPORT_SYMBOL(hpsb_make_isopacket);
1253EXPORT_SYMBOL(hpsb_read);
1254EXPORT_SYMBOL(hpsb_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255EXPORT_SYMBOL(hpsb_packet_success);
1256
1257/** highlevel.c **/
1258EXPORT_SYMBOL(hpsb_register_highlevel);
1259EXPORT_SYMBOL(hpsb_unregister_highlevel);
1260EXPORT_SYMBOL(hpsb_register_addrspace);
1261EXPORT_SYMBOL(hpsb_unregister_addrspace);
1262EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1263EXPORT_SYMBOL(hpsb_listen_channel);
1264EXPORT_SYMBOL(hpsb_unlisten_channel);
1265EXPORT_SYMBOL(hpsb_get_hostinfo);
1266EXPORT_SYMBOL(hpsb_create_hostinfo);
1267EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1268EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1269EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1270EXPORT_SYMBOL(hpsb_set_hostinfo);
Ben Collins1934b8b2005-07-09 20:01:23 -04001271EXPORT_SYMBOL(highlevel_host_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273/** nodemgr.c **/
1274EXPORT_SYMBOL(hpsb_node_fill_packet);
1275EXPORT_SYMBOL(hpsb_node_write);
Ben Collinsed30c262006-11-23 13:59:48 -05001276EXPORT_SYMBOL(__hpsb_register_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277EXPORT_SYMBOL(hpsb_unregister_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279/** csr.c **/
1280EXPORT_SYMBOL(hpsb_update_config_rom);
1281
1282/** dma.c **/
1283EXPORT_SYMBOL(dma_prog_region_init);
1284EXPORT_SYMBOL(dma_prog_region_alloc);
1285EXPORT_SYMBOL(dma_prog_region_free);
1286EXPORT_SYMBOL(dma_region_init);
1287EXPORT_SYMBOL(dma_region_alloc);
1288EXPORT_SYMBOL(dma_region_free);
1289EXPORT_SYMBOL(dma_region_sync_for_cpu);
1290EXPORT_SYMBOL(dma_region_sync_for_device);
1291EXPORT_SYMBOL(dma_region_mmap);
1292EXPORT_SYMBOL(dma_region_offset_to_bus);
1293
1294/** iso.c **/
1295EXPORT_SYMBOL(hpsb_iso_xmit_init);
1296EXPORT_SYMBOL(hpsb_iso_recv_init);
1297EXPORT_SYMBOL(hpsb_iso_xmit_start);
1298EXPORT_SYMBOL(hpsb_iso_recv_start);
1299EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1300EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1301EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1302EXPORT_SYMBOL(hpsb_iso_stop);
1303EXPORT_SYMBOL(hpsb_iso_shutdown);
1304EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1305EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1306EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1307EXPORT_SYMBOL(hpsb_iso_n_ready);
1308EXPORT_SYMBOL(hpsb_iso_packet_sent);
1309EXPORT_SYMBOL(hpsb_iso_packet_received);
1310EXPORT_SYMBOL(hpsb_iso_wake);
1311EXPORT_SYMBOL(hpsb_iso_recv_flush);
1312
1313/** csr1212.c **/
Ben Collins1934b8b2005-07-09 20:01:23 -04001314EXPORT_SYMBOL(csr1212_new_directory);
1315EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1316EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
1317EXPORT_SYMBOL(csr1212_release_keyval);
1318EXPORT_SYMBOL(csr1212_read);
1319EXPORT_SYMBOL(csr1212_parse_keyval);
1320EXPORT_SYMBOL(_csr1212_read_keyval);
1321EXPORT_SYMBOL(_csr1212_destroy_keyval);