blob: 270885679df1716b79426f0498f6b557ee40ef42 [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/**
Stefan Richterafd65462007-03-05 03:06:23 +010099 * hpsb_set_packet_complete_task - set task that runs when a packet completes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * @packet: the packet whose completion we want the task added to
101 * @routine: function to call
102 * @data: data (if any) to pass to the above function
Stefan Richterafd65462007-03-05 03:06:23 +0100103 *
104 * Set the task that runs when a packet completes. You cannot call this more
105 * than once on a single packet before it is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
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
Stefan Richterafd65462007-03-05 03:06:23 +0100182/**
183 * hpsb_reset_bus - initiate bus reset on the given host
184 * @host: host controller whose bus to reset
185 * @type: one of enum reset_types
186 *
187 * Returns 1 if bus reset already in progress, 0 otherwise.
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189int hpsb_reset_bus(struct hpsb_host *host, int type)
190{
Stefan Richter741854e2005-12-01 18:52:03 -0500191 if (!host->in_bus_reset) {
192 host->driver->devctl(host, RESET_BUS, type);
193 return 0;
194 } else {
195 return 1;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Pieter Palmers3dc5ea92007-02-03 17:44:39 +0100199/**
200 * hpsb_read_cycle_timer - read cycle timer register and system time
201 * @host: host whose isochronous cycle timer register is read
202 * @cycle_timer: address of bitfield to return the register contents
203 * @local_time: address to return the system time
204 *
205 * The format of * @cycle_timer, is described in OHCI 1.1 clause 5.13. This
206 * format is also read from non-OHCI controllers. * @local_time contains the
207 * system time in microseconds since the Epoch, read at the moment when the
208 * cycle timer was read.
209 *
210 * Return value: 0 for success or error number otherwise.
211 */
212int hpsb_read_cycle_timer(struct hpsb_host *host, u32 *cycle_timer,
213 u64 *local_time)
214{
215 int ctr;
216 struct timeval tv;
217 unsigned long flags;
218
219 if (!host || !cycle_timer || !local_time)
220 return -EINVAL;
221
222 preempt_disable();
223 local_irq_save(flags);
224
225 ctr = host->driver->devctl(host, GET_CYCLE_COUNTER, 0);
226 if (ctr)
227 do_gettimeofday(&tv);
228
229 local_irq_restore(flags);
230 preempt_enable();
231
232 if (!ctr)
233 return -EIO;
234 *cycle_timer = ctr;
235 *local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
236 return 0;
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Stefan Richterafd65462007-03-05 03:06:23 +0100239/**
240 * hpsb_bus_reset - notify a bus reset to the core
241 *
242 * For host driver module usage. Safe to use in interrupt context, although
243 * quite complex; so you may want to run it in the bottom rather than top half.
244 *
245 * Returns 1 if bus reset already in progress, 0 otherwise.
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247int hpsb_bus_reset(struct hpsb_host *host)
248{
Stefan Richter741854e2005-12-01 18:52:03 -0500249 if (host->in_bus_reset) {
250 HPSB_NOTICE("%s called while bus reset already in progress",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 __FUNCTION__);
Stefan Richter741854e2005-12-01 18:52:03 -0500252 return 1;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Stefan Richter741854e2005-12-01 18:52:03 -0500255 abort_requests(host);
256 host->in_bus_reset = 1;
257 host->irm_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 host->is_irm = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500259 host->busmgr_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 host->is_busmgr = 0;
261 host->is_cycmst = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500262 host->node_count = 0;
263 host->selfid_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Stefan Richter741854e2005-12-01 18:52:03 -0500265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268
269/*
270 * Verify num_of_selfids SelfIDs and return number of nodes. Return zero in
271 * case verification failed.
272 */
273static int check_selfids(struct hpsb_host *host)
274{
Stefan Richter741854e2005-12-01 18:52:03 -0500275 int nodeid = -1;
276 int rest_of_selfids = host->selfid_count;
277 struct selfid *sid = (struct selfid *)host->topology_map;
278 struct ext_selfid *esid;
279 int esid_seq = 23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 host->nodes_active = 0;
282
Stefan Richter741854e2005-12-01 18:52:03 -0500283 while (rest_of_selfids--) {
284 if (!sid->extended) {
285 nodeid++;
286 esid_seq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Stefan Richter741854e2005-12-01 18:52:03 -0500288 if (sid->phy_id != nodeid) {
289 HPSB_INFO("SelfIDs failed monotony check with "
290 "%d", sid->phy_id);
291 return 0;
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (sid->link_active) {
295 host->nodes_active++;
296 if (sid->contender)
297 host->irm_id = LOCAL_BUS | sid->phy_id;
298 }
Stefan Richter741854e2005-12-01 18:52:03 -0500299 } else {
300 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Stefan Richter741854e2005-12-01 18:52:03 -0500302 if ((esid->phy_id != nodeid)
303 || (esid->seq_nr != esid_seq)) {
304 HPSB_INFO("SelfIDs failed monotony check with "
305 "%d/%d", esid->phy_id, esid->seq_nr);
306 return 0;
307 }
308 esid_seq++;
309 }
310 sid++;
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Stefan Richter741854e2005-12-01 18:52:03 -0500313 esid = (struct ext_selfid *)(sid - 1);
314 while (esid->extended) {
315 if ((esid->porta == SELFID_PORT_PARENT) ||
Stefan Richterd7758462005-12-01 18:51:56 -0500316 (esid->portb == SELFID_PORT_PARENT) ||
317 (esid->portc == SELFID_PORT_PARENT) ||
318 (esid->portd == SELFID_PORT_PARENT) ||
319 (esid->porte == SELFID_PORT_PARENT) ||
320 (esid->portf == SELFID_PORT_PARENT) ||
321 (esid->portg == SELFID_PORT_PARENT) ||
322 (esid->porth == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 HPSB_INFO("SelfIDs failed root check on "
324 "extended SelfID");
325 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500326 }
327 esid--;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Stefan Richter741854e2005-12-01 18:52:03 -0500330 sid = (struct selfid *)esid;
Stefan Richterd7758462005-12-01 18:51:56 -0500331 if ((sid->port0 == SELFID_PORT_PARENT) ||
332 (sid->port1 == SELFID_PORT_PARENT) ||
333 (sid->port2 == SELFID_PORT_PARENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 HPSB_INFO("SelfIDs failed root check");
335 return 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 host->node_count = nodeid + 1;
Stefan Richter741854e2005-12-01 18:52:03 -0500339 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static void build_speed_map(struct hpsb_host *host, int nodecount)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 u8 cldcnt[nodecount];
Stefan Richter741854e2005-12-01 18:52:03 -0500345 u8 *map = host->speed_map;
Ben Collins647dcb52006-06-12 18:12:37 -0400346 u8 *speedcap = host->speed;
Stefan Richter741854e2005-12-01 18:52:03 -0500347 struct selfid *sid;
348 struct ext_selfid *esid;
349 int i, j, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Stefan Richter741854e2005-12-01 18:52:03 -0500351 for (i = 0; i < (nodecount * 64); i += 64) {
352 for (j = 0; j < nodecount; j++) {
353 map[i+j] = IEEE1394_SPEED_MAX;
354 }
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Stefan Richter741854e2005-12-01 18:52:03 -0500357 for (i = 0; i < nodecount; i++) {
358 cldcnt[i] = 0;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Stefan Richter741854e2005-12-01 18:52:03 -0500361 /* find direct children count and speed */
362 for (sid = (struct selfid *)&host->topology_map[host->selfid_count-1],
363 n = nodecount - 1;
364 (void *)sid >= (void *)host->topology_map; sid--) {
365 if (sid->extended) {
366 esid = (struct ext_selfid *)sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Stefan Richterd7758462005-12-01 18:51:56 -0500368 if (esid->porta == SELFID_PORT_CHILD) cldcnt[n]++;
369 if (esid->portb == SELFID_PORT_CHILD) cldcnt[n]++;
370 if (esid->portc == SELFID_PORT_CHILD) cldcnt[n]++;
371 if (esid->portd == SELFID_PORT_CHILD) cldcnt[n]++;
372 if (esid->porte == SELFID_PORT_CHILD) cldcnt[n]++;
373 if (esid->portf == SELFID_PORT_CHILD) cldcnt[n]++;
374 if (esid->portg == SELFID_PORT_CHILD) cldcnt[n]++;
375 if (esid->porth == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Stefan Richterd7758462005-12-01 18:51:56 -0500377 if (sid->port0 == SELFID_PORT_CHILD) cldcnt[n]++;
378 if (sid->port1 == SELFID_PORT_CHILD) cldcnt[n]++;
379 if (sid->port2 == SELFID_PORT_CHILD) cldcnt[n]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Stefan Richter741854e2005-12-01 18:52:03 -0500381 speedcap[n] = sid->speed;
382 n--;
383 }
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Stefan Richter741854e2005-12-01 18:52:03 -0500386 /* set self mapping */
387 for (i = 0; i < nodecount; i++) {
388 map[64*i + i] = speedcap[i];
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Stefan Richter741854e2005-12-01 18:52:03 -0500391 /* fix up direct children count to total children count;
392 * also fix up speedcaps for sibling and parent communication */
393 for (i = 1; i < nodecount; i++) {
394 for (j = cldcnt[i], n = i - 1; j > 0; j--) {
395 cldcnt[i] += cldcnt[n];
396 speedcap[n] = min(speedcap[n], speedcap[i]);
397 n -= cldcnt[n] + 1;
398 }
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Stefan Richter741854e2005-12-01 18:52:03 -0500401 for (n = 0; n < nodecount; n++) {
402 for (i = n - cldcnt[n]; i <= n; i++) {
403 for (j = 0; j < (n - cldcnt[n]); j++) {
404 map[j*64 + i] = map[i*64 + j] =
405 min(map[i*64 + j], speedcap[n]);
406 }
407 for (j = n + 1; j < nodecount; j++) {
408 map[j*64 + i] = map[i*64 + j] =
409 min(map[i*64 + j], speedcap[n]);
410 }
411 }
412 }
Ben Collins647dcb52006-06-12 18:12:37 -0400413
Stefan Richter433a87d2006-07-03 12:02:27 -0400414#if SELFID_SPEED_UNKNOWN != IEEE1394_SPEED_MAX
Ben Collins647dcb52006-06-12 18:12:37 -0400415 /* assume maximum speed for 1394b PHYs, nodemgr will correct it */
416 for (n = 0; n < nodecount; n++)
Stefan Richter433a87d2006-07-03 12:02:27 -0400417 if (speedcap[n] == SELFID_SPEED_UNKNOWN)
Ben Collins647dcb52006-06-12 18:12:37 -0400418 speedcap[n] = IEEE1394_SPEED_MAX;
Stefan Richter433a87d2006-07-03 12:02:27 -0400419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422
Stefan Richterafd65462007-03-05 03:06:23 +0100423/**
424 * hpsb_selfid_received - hand over received selfid packet to the core
425 *
426 * For host driver module usage. Safe to use in interrupt context.
427 *
428 * The host driver should have done a successful complement check (second
429 * quadlet is complement of first) beforehand.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431void hpsb_selfid_received(struct hpsb_host *host, quadlet_t sid)
432{
Stefan Richter741854e2005-12-01 18:52:03 -0500433 if (host->in_bus_reset) {
434 HPSB_VERBOSE("Including SelfID 0x%x", sid);
435 host->topology_map[host->selfid_count++] = sid;
436 } else {
437 HPSB_NOTICE("Spurious SelfID packet (0x%08x) received from bus %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sid, NODEID_TO_BUS(host->node_id));
Stefan Richter741854e2005-12-01 18:52:03 -0500439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Stefan Richterafd65462007-03-05 03:06:23 +0100442/**
443 * hpsb_selfid_complete - notify completion of SelfID stage to the core
444 *
445 * For host driver module usage. Safe to use in interrupt context, although
446 * quite complex; so you may want to run it in the bottom rather than top half.
447 *
448 * Notify completion of SelfID stage to the core and report new physical ID
449 * and whether host is root now.
450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451void hpsb_selfid_complete(struct hpsb_host *host, int phyid, int isroot)
452{
453 if (!host->in_bus_reset)
454 HPSB_NOTICE("SelfID completion called outside of bus reset!");
455
Stefan Richter741854e2005-12-01 18:52:03 -0500456 host->node_id = LOCAL_BUS | phyid;
457 host->is_root = isroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Stefan Richter741854e2005-12-01 18:52:03 -0500459 if (!check_selfids(host)) {
460 if (host->reset_retries++ < 20) {
461 /* selfid stage did not complete without error */
462 HPSB_NOTICE("Error in SelfID stage, resetting");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 host->in_bus_reset = 0;
464 /* this should work from ohci1394 now... */
Stefan Richter741854e2005-12-01 18:52:03 -0500465 hpsb_reset_bus(host, LONG_RESET);
466 return;
467 } else {
468 HPSB_NOTICE("Stopping out-of-control reset loop");
469 HPSB_NOTICE("Warning - topology map and speed map will not be valid");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500471 }
472 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 host->reset_retries = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500474 build_speed_map(host, host->node_count);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 HPSB_VERBOSE("selfid_complete called with successful SelfID stage "
478 "... irm_id: 0x%X node_id: 0x%X",host->irm_id,host->node_id);
479
Stefan Richter741854e2005-12-01 18:52:03 -0500480 /* irm_id is kept up to date by check_selfids() */
481 if (host->irm_id == host->node_id) {
482 host->is_irm = 1;
483 } else {
484 host->is_busmgr = 0;
485 host->is_irm = 0;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Stefan Richter741854e2005-12-01 18:52:03 -0500488 if (isroot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 host->driver->devctl(host, ACT_CYCLE_MASTER, 1);
490 host->is_cycmst = 1;
491 }
492 atomic_inc(&host->generation);
493 host->in_bus_reset = 0;
Stefan Richter741854e2005-12-01 18:52:03 -0500494 highlevel_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Stefan Richterafd65462007-03-05 03:06:23 +0100497/**
498 * hpsb_packet_sent - notify core of sending a packet
499 *
500 * For host driver module usage. Safe to call from within a transmit packet
501 * routine.
502 *
503 * Notify core of sending a packet. Ackcode is the ack code returned for async
504 * transmits or ACKX_SEND_ERROR if the transmission failed completely; ACKX_NONE
505 * for other cases (internal errors that don't justify a panic).
506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507void hpsb_packet_sent(struct hpsb_host *host, struct hpsb_packet *packet,
Stefan Richter741854e2005-12-01 18:52:03 -0500508 int ackcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned long flags;
511
512 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
513
514 packet->ack_code = ackcode;
515
516 if (packet->no_waiter || packet->state == hpsb_complete) {
517 /* if packet->no_waiter, must not have a tlabel allocated */
518 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
519 hpsb_free_packet(packet);
520 return;
521 }
522
523 atomic_dec(&packet->refcnt); /* drop HC's reference */
524 /* here the packet must be on the host->pending_packet_queue */
525
526 if (ackcode != ACK_PENDING || !packet->expect_response) {
527 packet->state = hpsb_complete;
528 __skb_unlink(packet->skb, &host->pending_packet_queue);
529 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
530 queue_packet_complete(packet);
531 return;
532 }
533
534 packet->state = hpsb_pending;
535 packet->sendtime = jiffies;
536
537 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
538
539 mod_timer(&host->timeout, jiffies + host->timeout_interval);
540}
541
542/**
543 * hpsb_send_phy_config - transmit a PHY configuration packet on the bus
544 * @host: host that PHY config packet gets sent through
545 * @rootid: root whose force_root bit should get set (-1 = don't set force_root)
546 * @gapcnt: gap count value to set (-1 = don't set gap count)
547 *
Stefan Richterafd65462007-03-05 03:06:23 +0100548 * This function sends a PHY config packet on the bus through the specified
549 * host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
Stefan Richterafd65462007-03-05 03:06:23 +0100551 * Return value: 0 for success or negative error number otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
553int hpsb_send_phy_config(struct hpsb_host *host, int rootid, int gapcnt)
554{
555 struct hpsb_packet *packet;
Stefan Richter546513f2005-12-01 18:52:01 -0500556 quadlet_t d = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int retval = 0;
558
559 if (rootid >= ALL_NODES || rootid < -1 || gapcnt > 0x3f || gapcnt < -1 ||
560 (rootid == -1 && gapcnt == -1)) {
561 HPSB_DEBUG("Invalid Parameter: rootid = %d gapcnt = %d",
562 rootid, gapcnt);
563 return -EINVAL;
564 }
565
Stefan Richter546513f2005-12-01 18:52:01 -0500566 if (rootid != -1)
567 d |= PHYPACKET_PHYCONFIG_R | rootid << PHYPACKET_PORT_SHIFT;
568 if (gapcnt != -1)
569 d |= PHYPACKET_PHYCONFIG_T | gapcnt << PHYPACKET_GAPCOUNT_SHIFT;
570
571 packet = hpsb_make_phypacket(host, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!packet)
573 return -ENOMEM;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 packet->generation = get_hpsb_generation(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 retval = hpsb_send_packet_and_wait(packet);
577 hpsb_free_packet(packet);
578
579 return retval;
580}
581
582/**
583 * hpsb_send_packet - transmit a packet on the bus
584 * @packet: packet to send
585 *
586 * The packet is sent through the host specified in the packet->host field.
587 * Before sending, the packet's transmit speed is automatically determined
588 * using the local speed map when it is an async, non-broadcast packet.
589 *
590 * Possibilities for failure are that host is either not initialized, in bus
591 * reset, the packet's generation number doesn't match the current generation
592 * number or the host reports a transmit error.
593 *
594 * Return value: 0 on success, negative errno on failure.
595 */
596int hpsb_send_packet(struct hpsb_packet *packet)
597{
598 struct hpsb_host *host = packet->host;
599
Stefan Richter741854e2005-12-01 18:52:03 -0500600 if (host->is_shutdown)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -EINVAL;
602 if (host->in_bus_reset ||
603 (packet->generation != get_hpsb_generation(host)))
Stefan Richter741854e2005-12-01 18:52:03 -0500604 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Stefan Richter741854e2005-12-01 18:52:03 -0500606 packet->state = hpsb_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* This just seems silly to me */
609 WARN_ON(packet->no_waiter && packet->expect_response);
610
611 if (!packet->no_waiter || packet->expect_response) {
612 atomic_inc(&packet->refcnt);
Ben Collins1934b8b2005-07-09 20:01:23 -0400613 /* Set the initial "sendtime" to 10 seconds from now, to
614 prevent premature expiry. If a packet takes more than
615 10 seconds to hit the wire, we have bigger problems :) */
Jody McIntyre6262d062005-05-16 21:54:05 -0700616 packet->sendtime = jiffies + 10 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 skb_queue_tail(&host->pending_packet_queue, packet->skb);
618 }
619
Stefan Richter741854e2005-12-01 18:52:03 -0500620 if (packet->node_id == host->node_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* it is a local request, so handle it locally */
622
Stefan Richter741854e2005-12-01 18:52:03 -0500623 quadlet_t *data;
624 size_t size = packet->data_size + packet->header_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Stefan Richter741854e2005-12-01 18:52:03 -0500626 data = kmalloc(size, GFP_ATOMIC);
627 if (!data) {
628 HPSB_ERR("unable to allocate memory for concatenating header and data");
629 return -ENOMEM;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Stefan Richter741854e2005-12-01 18:52:03 -0500632 memcpy(data, packet->header, packet->header_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Stefan Richter741854e2005-12-01 18:52:03 -0500634 if (packet->data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 memcpy(((u8*)data) + packet->header_size, packet->data, packet->data_size);
636
Stefan Richter741854e2005-12-01 18:52:03 -0500637 dump_packet("send packet local", packet->header, packet->header_size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Stefan Richter741854e2005-12-01 18:52:03 -0500639 hpsb_packet_sent(host, packet, packet->expect_response ? ACK_PENDING : ACK_COMPLETE);
640 hpsb_packet_received(host, data, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Stefan Richter741854e2005-12-01 18:52:03 -0500642 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Stefan Richter741854e2005-12-01 18:52:03 -0500644 return 0;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Ben Collins647dcb52006-06-12 18:12:37 -0400647 if (packet->type == hpsb_async &&
648 NODEID_TO_NODE(packet->node_id) != ALL_NODES)
Stefan Richter741854e2005-12-01 18:52:03 -0500649 packet->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -0400650 host->speed[NODEID_TO_NODE(packet->node_id)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Stefan Richter741854e2005-12-01 18:52:03 -0500652 dump_packet("send packet", packet->header, packet->header_size, packet->speed_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Stefan Richter741854e2005-12-01 18:52:03 -0500654 return host->driver->transmit_packet(host, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/* We could just use complete() directly as the packet complete
658 * callback, but this is more typesafe, in the sense that we get a
659 * compiler error if the prototype for complete() changes. */
660
661static void complete_packet(void *data)
662{
663 complete((struct completion *) data);
664}
665
Stefan Richterafd65462007-03-05 03:06:23 +0100666/**
667 * hpsb_send_packet_and_wait - enqueue packet, block until transaction completes
668 * @packet: packet to send
669 *
670 * Return value: 0 on success, negative errno on failure.
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672int hpsb_send_packet_and_wait(struct hpsb_packet *packet)
673{
674 struct completion done;
675 int retval;
676
677 init_completion(&done);
678 hpsb_set_packet_complete_task(packet, complete_packet, &done);
679 retval = hpsb_send_packet(packet);
680 if (retval == 0)
681 wait_for_completion(&done);
682
683 return retval;
684}
685
686static void send_packet_nocare(struct hpsb_packet *packet)
687{
Stefan Richter741854e2005-12-01 18:52:03 -0500688 if (hpsb_send_packet(packet) < 0) {
689 hpsb_free_packet(packet);
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693
694static void handle_packet_response(struct hpsb_host *host, int tcode,
695 quadlet_t *data, size_t size)
696{
Stefan Richter741854e2005-12-01 18:52:03 -0500697 struct hpsb_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct sk_buff *skb;
Stefan Richter741854e2005-12-01 18:52:03 -0500699 int tcode_match = 0;
700 int tlabel;
701 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Stefan Richter741854e2005-12-01 18:52:03 -0500703 tlabel = (data[0] >> 10) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
706
707 skb_queue_walk(&host->pending_packet_queue, skb) {
708 packet = (struct hpsb_packet *)skb->data;
Stefan Richter741854e2005-12-01 18:52:03 -0500709 if ((packet->tlabel == tlabel)
710 && (packet->node_id == (data[1] >> 16))){
711 break;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 packet = NULL;
Stefan Richter741854e2005-12-01 18:52:03 -0500715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (packet == NULL) {
Stefan Richter741854e2005-12-01 18:52:03 -0500718 HPSB_DEBUG("unsolicited response packet received - no tlabel match");
719 dump_packet("contents", data, 16, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500721 return;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Stefan Richter741854e2005-12-01 18:52:03 -0500724 switch (packet->tcode) {
725 case TCODE_WRITEQ:
726 case TCODE_WRITEB:
727 if (tcode != TCODE_WRITE_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 tcode_match = 1;
730 memcpy(packet->header, data, 12);
Stefan Richter741854e2005-12-01 18:52:03 -0500731 break;
732 case TCODE_READQ:
733 if (tcode != TCODE_READQ_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
735 tcode_match = 1;
736 memcpy(packet->header, data, 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500737 break;
738 case TCODE_READB:
739 if (tcode != TCODE_READB_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 tcode_match = 1;
742 BUG_ON(packet->skb->len - sizeof(*packet) < size - 16);
743 memcpy(packet->header, data, 16);
744 memcpy(packet->data, data + 4, size - 16);
Stefan Richter741854e2005-12-01 18:52:03 -0500745 break;
746 case TCODE_LOCK_REQUEST:
747 if (tcode != TCODE_LOCK_RESPONSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 tcode_match = 1;
750 size = min((size - 16), (size_t)8);
751 BUG_ON(packet->skb->len - sizeof(*packet) < size);
752 memcpy(packet->header, data, 16);
753 memcpy(packet->data, data + 4, size);
Stefan Richter741854e2005-12-01 18:52:03 -0500754 break;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Stefan Richter741854e2005-12-01 18:52:03 -0500757 if (!tcode_match) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
Stefan Richter741854e2005-12-01 18:52:03 -0500759 HPSB_INFO("unsolicited response packet received - tcode mismatch");
760 dump_packet("contents", data, 16, -1);
761 return;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
David S. Miller8728b832005-08-09 19:25:21 -0700764 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (packet->state == hpsb_queued) {
767 packet->sendtime = jiffies;
768 packet->ack_code = ACK_PENDING;
769 }
770
771 packet->state = hpsb_complete;
772 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
773
774 queue_packet_complete(packet);
775}
776
777
778static struct hpsb_packet *create_reply_packet(struct hpsb_host *host,
779 quadlet_t *data, size_t dsize)
780{
Stefan Richter741854e2005-12-01 18:52:03 -0500781 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Stefan Richter741854e2005-12-01 18:52:03 -0500783 p = hpsb_alloc_packet(dsize);
784 if (unlikely(p == NULL)) {
785 /* FIXME - send data_error response */
786 return NULL;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Stefan Richter741854e2005-12-01 18:52:03 -0500789 p->type = hpsb_async;
790 p->state = hpsb_unused;
791 p->host = host;
792 p->node_id = data[1] >> 16;
793 p->tlabel = (data[0] >> 10) & 0x3f;
794 p->no_waiter = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 p->generation = get_hpsb_generation(host);
797
798 if (dsize % 4)
799 p->data[dsize / 4] = 0;
800
Stefan Richter741854e2005-12-01 18:52:03 -0500801 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804#define PREP_ASYNC_HEAD_RCODE(tc) \
805 packet->tcode = tc; \
806 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
807 | (1 << 8) | (tc << 4); \
808 packet->header[1] = (packet->host->node_id << 16) | (rcode << 12); \
809 packet->header[2] = 0
810
811static void fill_async_readquad_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500812 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 PREP_ASYNC_HEAD_RCODE(TCODE_READQ_RESPONSE);
815 packet->header[3] = data;
816 packet->header_size = 16;
817 packet->data_size = 0;
818}
819
820static void fill_async_readblock_resp(struct hpsb_packet *packet, int rcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500821 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 if (rcode != RCODE_COMPLETE)
824 length = 0;
825
826 PREP_ASYNC_HEAD_RCODE(TCODE_READB_RESPONSE);
827 packet->header[3] = length << 16;
828 packet->header_size = 16;
829 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
830}
831
832static void fill_async_write_resp(struct hpsb_packet *packet, int rcode)
833{
834 PREP_ASYNC_HEAD_RCODE(TCODE_WRITE_RESPONSE);
835 packet->header[2] = 0;
836 packet->header_size = 12;
837 packet->data_size = 0;
838}
839
840static void fill_async_lock_resp(struct hpsb_packet *packet, int rcode, int extcode,
Stefan Richter741854e2005-12-01 18:52:03 -0500841 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 if (rcode != RCODE_COMPLETE)
844 length = 0;
845
846 PREP_ASYNC_HEAD_RCODE(TCODE_LOCK_RESPONSE);
847 packet->header[3] = (length << 16) | extcode;
848 packet->header_size = 16;
849 packet->data_size = length;
850}
851
852#define PREP_REPLY_PACKET(length) \
Stefan Richter741854e2005-12-01 18:52:03 -0500853 packet = create_reply_packet(host, data, length); \
854 if (packet == NULL) break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856static void handle_incoming_packet(struct hpsb_host *host, int tcode,
857 quadlet_t *data, size_t size, int write_acked)
858{
Stefan Richter741854e2005-12-01 18:52:03 -0500859 struct hpsb_packet *packet;
860 int length, rcode, extcode;
861 quadlet_t buffer;
862 nodeid_t source = data[1] >> 16;
863 nodeid_t dest = data[0] >> 16;
864 u16 flags = (u16) data[0];
865 u64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Stefan Richter741854e2005-12-01 18:52:03 -0500867 /* big FIXME - no error checking is done for an out of bounds length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Stefan Richter741854e2005-12-01 18:52:03 -0500869 switch (tcode) {
870 case TCODE_WRITEQ:
871 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
872 rcode = highlevel_write(host, source, dest, data+3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 addr, 4, flags);
874
Stefan Richter741854e2005-12-01 18:52:03 -0500875 if (!write_acked
876 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
877 && (rcode >= 0)) {
878 /* not a broadcast write, reply */
879 PREP_REPLY_PACKET(0);
880 fill_async_write_resp(packet, rcode);
881 send_packet_nocare(packet);
882 }
883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Stefan Richter741854e2005-12-01 18:52:03 -0500885 case TCODE_WRITEB:
886 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
887 rcode = highlevel_write(host, source, dest, data+4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 addr, data[3]>>16, flags);
889
Stefan Richter741854e2005-12-01 18:52:03 -0500890 if (!write_acked
891 && (NODEID_TO_NODE(data[0] >> 16) != NODE_MASK)
892 && (rcode >= 0)) {
893 /* not a broadcast write, reply */
894 PREP_REPLY_PACKET(0);
895 fill_async_write_resp(packet, rcode);
896 send_packet_nocare(packet);
897 }
898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Stefan Richter741854e2005-12-01 18:52:03 -0500900 case TCODE_READQ:
901 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
902 rcode = highlevel_read(host, source, &buffer, addr, 4, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Stefan Richter741854e2005-12-01 18:52:03 -0500904 if (rcode >= 0) {
905 PREP_REPLY_PACKET(0);
906 fill_async_readquad_resp(packet, rcode, buffer);
907 send_packet_nocare(packet);
908 }
909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Stefan Richter741854e2005-12-01 18:52:03 -0500911 case TCODE_READB:
912 length = data[3] >> 16;
913 PREP_REPLY_PACKET(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Stefan Richter741854e2005-12-01 18:52:03 -0500915 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
916 rcode = highlevel_read(host, source, packet->data, addr,
917 length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Stefan Richter741854e2005-12-01 18:52:03 -0500919 if (rcode >= 0) {
920 fill_async_readblock_resp(packet, rcode, length);
921 send_packet_nocare(packet);
922 } else {
923 hpsb_free_packet(packet);
924 }
925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stefan Richter741854e2005-12-01 18:52:03 -0500927 case TCODE_LOCK_REQUEST:
928 length = data[3] >> 16;
929 extcode = data[3] & 0xffff;
930 addr = (((u64)(data[1] & 0xffff)) << 32) | data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Stefan Richter741854e2005-12-01 18:52:03 -0500932 PREP_REPLY_PACKET(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stefan Richter741854e2005-12-01 18:52:03 -0500934 if ((extcode == 0) || (extcode >= 7)) {
935 /* let switch default handle error */
936 length = 0;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Stefan Richter741854e2005-12-01 18:52:03 -0500939 switch (length) {
940 case 4:
941 rcode = highlevel_lock(host, source, packet->data, addr,
942 data[4], 0, extcode,flags);
943 fill_async_lock_resp(packet, rcode, extcode, 4);
944 break;
945 case 8:
946 if ((extcode != EXTCODE_FETCH_ADD)
947 && (extcode != EXTCODE_LITTLE_ADD)) {
948 rcode = highlevel_lock(host, source,
949 packet->data, addr,
950 data[5], data[4],
951 extcode, flags);
952 fill_async_lock_resp(packet, rcode, extcode, 4);
953 } else {
954 rcode = highlevel_lock64(host, source,
955 (octlet_t *)packet->data, addr,
956 *(octlet_t *)(data + 4), 0ULL,
957 extcode, flags);
958 fill_async_lock_resp(packet, rcode, extcode, 8);
959 }
960 break;
961 case 16:
962 rcode = highlevel_lock64(host, source,
963 (octlet_t *)packet->data, addr,
964 *(octlet_t *)(data + 6),
965 *(octlet_t *)(data + 4),
966 extcode, flags);
967 fill_async_lock_resp(packet, rcode, extcode, 8);
968 break;
969 default:
970 rcode = RCODE_TYPE_ERROR;
971 fill_async_lock_resp(packet, rcode,
972 extcode, 0);
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Stefan Richter741854e2005-12-01 18:52:03 -0500975 if (rcode >= 0) {
976 send_packet_nocare(packet);
977 } else {
978 hpsb_free_packet(packet);
979 }
980 break;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983}
984#undef PREP_REPLY_PACKET
985
Stefan Richterafd65462007-03-05 03:06:23 +0100986/**
987 * hpsb_packet_received - hand over received packet to the core
988 *
989 * For host driver module usage.
990 *
991 * The contents of data are expected to be the full packet but with the CRCs
992 * left out (data block follows header immediately), with the header (i.e. the
993 * first four quadlets) in machine byte order and the data block in big endian.
994 * *@data can be safely overwritten after this call.
995 *
996 * If the packet is a write request, @write_acked is to be set to true if it was
997 * ack_complete'd already, false otherwise. This argument is ignored for any
998 * other packet type.
999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000void hpsb_packet_received(struct hpsb_host *host, quadlet_t *data, size_t size,
Stefan Richter741854e2005-12-01 18:52:03 -05001001 int write_acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Stefan Richter741854e2005-12-01 18:52:03 -05001003 int tcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Stefan Richter741854e2005-12-01 18:52:03 -05001005 if (host->in_bus_reset) {
1006 HPSB_INFO("received packet during reset; ignoring");
1007 return;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Stefan Richter741854e2005-12-01 18:52:03 -05001010 dump_packet("received packet", data, size, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Stefan Richter741854e2005-12-01 18:52:03 -05001012 tcode = (data[0] >> 4) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Stefan Richter741854e2005-12-01 18:52:03 -05001014 switch (tcode) {
1015 case TCODE_WRITE_RESPONSE:
1016 case TCODE_READQ_RESPONSE:
1017 case TCODE_READB_RESPONSE:
1018 case TCODE_LOCK_RESPONSE:
1019 handle_packet_response(host, tcode, data, size);
1020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Stefan Richter741854e2005-12-01 18:52:03 -05001022 case TCODE_WRITEQ:
1023 case TCODE_WRITEB:
1024 case TCODE_READQ:
1025 case TCODE_READB:
1026 case TCODE_LOCK_REQUEST:
1027 handle_incoming_packet(host, tcode, data, size, write_acked);
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030
Stefan Richter741854e2005-12-01 18:52:03 -05001031 case TCODE_ISO_DATA:
1032 highlevel_iso_receive(host, data, size);
1033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Stefan Richter741854e2005-12-01 18:52:03 -05001035 case TCODE_CYCLE_START:
1036 /* simply ignore this packet if it is passed on */
1037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Stefan Richter741854e2005-12-01 18:52:03 -05001039 default:
1040 HPSB_NOTICE("received packet with bogus transaction code %d",
1041 tcode);
1042 break;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046
1047static void abort_requests(struct hpsb_host *host)
1048{
1049 struct hpsb_packet *packet;
1050 struct sk_buff *skb;
1051
1052 host->driver->devctl(host, CANCEL_REQUESTS, 0);
1053
1054 while ((skb = skb_dequeue(&host->pending_packet_queue)) != NULL) {
1055 packet = (struct hpsb_packet *)skb->data;
1056
1057 packet->state = hpsb_complete;
1058 packet->ack_code = ACKX_ABORTED;
1059 queue_packet_complete(packet);
1060 }
1061}
1062
1063void abort_timedouts(unsigned long __opaque)
1064{
1065 struct hpsb_host *host = (struct hpsb_host *)__opaque;
1066 unsigned long flags;
1067 struct hpsb_packet *packet;
1068 struct sk_buff *skb;
1069 unsigned long expire;
1070
1071 spin_lock_irqsave(&host->csr.lock, flags);
1072 expire = host->csr.expire;
1073 spin_unlock_irqrestore(&host->csr.lock, flags);
1074
1075 /* Hold the lock around this, since we aren't dequeuing all
1076 * packets, just ones we need. */
1077 spin_lock_irqsave(&host->pending_packet_queue.lock, flags);
1078
1079 while (!skb_queue_empty(&host->pending_packet_queue)) {
1080 skb = skb_peek(&host->pending_packet_queue);
1081
1082 packet = (struct hpsb_packet *)skb->data;
1083
1084 if (time_before(packet->sendtime + expire, jiffies)) {
David S. Miller8728b832005-08-09 19:25:21 -07001085 __skb_unlink(skb, &host->pending_packet_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 packet->state = hpsb_complete;
1087 packet->ack_code = ACKX_TIMEOUT;
1088 queue_packet_complete(packet);
1089 } else {
1090 /* Since packets are added to the tail, the oldest
1091 * ones are first, always. When we get to one that
1092 * isn't timed out, the rest aren't either. */
1093 break;
1094 }
1095 }
1096
1097 if (!skb_queue_empty(&host->pending_packet_queue))
1098 mod_timer(&host->timeout, jiffies + host->timeout_interval);
1099
1100 spin_unlock_irqrestore(&host->pending_packet_queue.lock, flags);
1101}
1102
1103
1104/* Kernel thread and vars, which handles packets that are completed. Only
1105 * packets that have a "complete" function are sent here. This way, the
1106 * completion is run out of kernel context, and doesn't block the rest of
1107 * the stack. */
Ben Collinsf6542402006-06-12 18:15:50 -04001108static struct task_struct *khpsbpkt_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109static struct sk_buff_head hpsbpkt_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111static void queue_packet_complete(struct hpsb_packet *packet)
1112{
1113 if (packet->no_waiter) {
1114 hpsb_free_packet(packet);
1115 return;
1116 }
1117 if (packet->complete_routine != NULL) {
1118 skb_queue_tail(&hpsbpkt_queue, packet->skb);
Ben Collinsf6542402006-06-12 18:15:50 -04001119 wake_up_process(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 return;
1122}
1123
1124static int hpsbpkt_thread(void *__hi)
1125{
1126 struct sk_buff *skb;
1127 struct hpsb_packet *packet;
1128 void (*complete_routine)(void*);
1129 void *complete_data;
1130
Dave Jones8c1d2862006-01-06 00:18:38 -08001131 current->flags |= PF_NOFREEZE;
1132
Ben Collinsf6542402006-06-12 18:15:50 -04001133 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 while ((skb = skb_dequeue(&hpsbpkt_queue)) != NULL) {
1135 packet = (struct hpsb_packet *)skb->data;
1136
1137 complete_routine = packet->complete_routine;
1138 complete_data = packet->complete_data;
1139
1140 packet->complete_routine = packet->complete_data = NULL;
1141
1142 complete_routine(complete_data);
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Ben Collinsf6542402006-06-12 18:15:50 -04001145 set_current_state(TASK_INTERRUPTIBLE);
1146 if (!skb_peek(&hpsbpkt_queue))
1147 schedule();
1148 __set_current_state(TASK_RUNNING);
1149 }
1150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153static int __init ieee1394_init(void)
1154{
1155 int i, ret;
1156
1157 skb_queue_head_init(&hpsbpkt_queue);
1158
1159 /* non-fatal error */
1160 if (hpsb_init_config_roms()) {
1161 HPSB_ERR("Failed to initialize some config rom entries.\n");
1162 HPSB_ERR("Some features may not be available\n");
1163 }
1164
Ben Collinsf6542402006-06-12 18:15:50 -04001165 khpsbpkt_thread = kthread_run(hpsbpkt_thread, NULL, "khpsbpkt");
1166 if (IS_ERR(khpsbpkt_thread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 HPSB_ERR("Failed to start hpsbpkt thread!\n");
Ben Collinsf6542402006-06-12 18:15:50 -04001168 ret = PTR_ERR(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto exit_cleanup_config_roms;
1170 }
1171
1172 if (register_chrdev_region(IEEE1394_CORE_DEV, 256, "ieee1394")) {
1173 HPSB_ERR("unable to register character device major %d!\n", IEEE1394_MAJOR);
1174 ret = -ENODEV;
1175 goto exit_release_kernel_thread;
1176 }
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 ret = bus_register(&ieee1394_bus_type);
1179 if (ret < 0) {
1180 HPSB_INFO("bus register failed");
Stefan Richtera8748442006-03-28 19:55:41 -05001181 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 for (i = 0; fw_bus_attrs[i]; i++) {
1185 ret = bus_create_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1186 if (ret < 0) {
1187 while (i >= 0) {
1188 bus_remove_file(&ieee1394_bus_type,
1189 fw_bus_attrs[i--]);
1190 }
1191 bus_unregister(&ieee1394_bus_type);
Stefan Richtera8748442006-03-28 19:55:41 -05001192 goto release_chrdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 }
1195
1196 ret = class_register(&hpsb_host_class);
1197 if (ret < 0)
1198 goto release_all_bus;
1199
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001200 hpsb_protocol_class = class_create(THIS_MODULE, "ieee1394_protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (IS_ERR(hpsb_protocol_class)) {
1202 ret = PTR_ERR(hpsb_protocol_class);
1203 goto release_class_host;
1204 }
1205
1206 ret = init_csr();
1207 if (ret) {
1208 HPSB_INFO("init csr failed");
1209 ret = -ENOMEM;
1210 goto release_class_protocol;
1211 }
1212
1213 if (disable_nodemgr) {
1214 HPSB_INFO("nodemgr and IRM functionality disabled");
1215 /* We shouldn't contend for IRM with nodemgr disabled, since
1216 nodemgr implements functionality required of ieee1394a-2000
1217 IRMs */
1218 hpsb_disable_irm = 1;
Stefan Richter741854e2005-12-01 18:52:03 -05001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return 0;
1221 }
1222
1223 if (hpsb_disable_irm) {
1224 HPSB_INFO("IRM functionality disabled");
1225 }
1226
1227 ret = init_ieee1394_nodemgr();
1228 if (ret < 0) {
1229 HPSB_INFO("init nodemgr failed");
1230 goto cleanup_csr;
1231 }
1232
1233 return 0;
1234
1235cleanup_csr:
1236 cleanup_csr();
1237release_class_protocol:
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001238 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239release_class_host:
1240 class_unregister(&hpsb_host_class);
1241release_all_bus:
1242 for (i = 0; fw_bus_attrs[i]; i++)
1243 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1244 bus_unregister(&ieee1394_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245release_chrdev:
1246 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
1247exit_release_kernel_thread:
Ben Collinsf6542402006-06-12 18:15:50 -04001248 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249exit_cleanup_config_roms:
1250 hpsb_cleanup_config_roms();
1251 return ret;
1252}
1253
1254static void __exit ieee1394_cleanup(void)
1255{
1256 int i;
1257
1258 if (!disable_nodemgr)
1259 cleanup_ieee1394_nodemgr();
1260
1261 cleanup_csr();
1262
gregkh@suse.de7e25ab92005-03-23 09:53:36 -08001263 class_destroy(hpsb_protocol_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 class_unregister(&hpsb_host_class);
1265 for (i = 0; fw_bus_attrs[i]; i++)
1266 bus_remove_file(&ieee1394_bus_type, fw_bus_attrs[i]);
1267 bus_unregister(&ieee1394_bus_type);
1268
Ben Collinsf6542402006-06-12 18:15:50 -04001269 kthread_stop(khpsbpkt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 hpsb_cleanup_config_roms();
1272
1273 unregister_chrdev_region(IEEE1394_CORE_DEV, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Andi Kleen8df40832006-07-27 21:54:00 +02001276fs_initcall(ieee1394_init); /* same as ohci1394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277module_exit(ieee1394_cleanup);
1278
1279/* Exported symbols */
1280
1281/** hosts.c **/
1282EXPORT_SYMBOL(hpsb_alloc_host);
1283EXPORT_SYMBOL(hpsb_add_host);
Stefan Richter33601772007-01-07 21:49:27 +01001284EXPORT_SYMBOL(hpsb_resume_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285EXPORT_SYMBOL(hpsb_remove_host);
1286EXPORT_SYMBOL(hpsb_update_config_rom_image);
1287
1288/** ieee1394_core.c **/
1289EXPORT_SYMBOL(hpsb_speedto_str);
1290EXPORT_SYMBOL(hpsb_protocol_class);
1291EXPORT_SYMBOL(hpsb_set_packet_complete_task);
1292EXPORT_SYMBOL(hpsb_alloc_packet);
1293EXPORT_SYMBOL(hpsb_free_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294EXPORT_SYMBOL(hpsb_send_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295EXPORT_SYMBOL(hpsb_reset_bus);
Pieter Palmers3dc5ea92007-02-03 17:44:39 +01001296EXPORT_SYMBOL(hpsb_read_cycle_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297EXPORT_SYMBOL(hpsb_bus_reset);
1298EXPORT_SYMBOL(hpsb_selfid_received);
1299EXPORT_SYMBOL(hpsb_selfid_complete);
1300EXPORT_SYMBOL(hpsb_packet_sent);
1301EXPORT_SYMBOL(hpsb_packet_received);
1302EXPORT_SYMBOL_GPL(hpsb_disable_irm);
1303
1304/** ieee1394_transactions.c **/
1305EXPORT_SYMBOL(hpsb_get_tlabel);
1306EXPORT_SYMBOL(hpsb_free_tlabel);
1307EXPORT_SYMBOL(hpsb_make_readpacket);
1308EXPORT_SYMBOL(hpsb_make_writepacket);
1309EXPORT_SYMBOL(hpsb_make_streampacket);
1310EXPORT_SYMBOL(hpsb_make_lockpacket);
1311EXPORT_SYMBOL(hpsb_make_lock64packet);
1312EXPORT_SYMBOL(hpsb_make_phypacket);
1313EXPORT_SYMBOL(hpsb_make_isopacket);
1314EXPORT_SYMBOL(hpsb_read);
1315EXPORT_SYMBOL(hpsb_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316EXPORT_SYMBOL(hpsb_packet_success);
1317
1318/** highlevel.c **/
1319EXPORT_SYMBOL(hpsb_register_highlevel);
1320EXPORT_SYMBOL(hpsb_unregister_highlevel);
1321EXPORT_SYMBOL(hpsb_register_addrspace);
1322EXPORT_SYMBOL(hpsb_unregister_addrspace);
1323EXPORT_SYMBOL(hpsb_allocate_and_register_addrspace);
1324EXPORT_SYMBOL(hpsb_listen_channel);
1325EXPORT_SYMBOL(hpsb_unlisten_channel);
1326EXPORT_SYMBOL(hpsb_get_hostinfo);
1327EXPORT_SYMBOL(hpsb_create_hostinfo);
1328EXPORT_SYMBOL(hpsb_destroy_hostinfo);
1329EXPORT_SYMBOL(hpsb_set_hostinfo_key);
1330EXPORT_SYMBOL(hpsb_get_hostinfo_bykey);
1331EXPORT_SYMBOL(hpsb_set_hostinfo);
Ben Collins1934b8b2005-07-09 20:01:23 -04001332EXPORT_SYMBOL(highlevel_host_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/** nodemgr.c **/
1335EXPORT_SYMBOL(hpsb_node_fill_packet);
1336EXPORT_SYMBOL(hpsb_node_write);
Ben Collinsed30c262006-11-23 13:59:48 -05001337EXPORT_SYMBOL(__hpsb_register_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338EXPORT_SYMBOL(hpsb_unregister_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340/** csr.c **/
1341EXPORT_SYMBOL(hpsb_update_config_rom);
1342
1343/** dma.c **/
1344EXPORT_SYMBOL(dma_prog_region_init);
1345EXPORT_SYMBOL(dma_prog_region_alloc);
1346EXPORT_SYMBOL(dma_prog_region_free);
1347EXPORT_SYMBOL(dma_region_init);
1348EXPORT_SYMBOL(dma_region_alloc);
1349EXPORT_SYMBOL(dma_region_free);
1350EXPORT_SYMBOL(dma_region_sync_for_cpu);
1351EXPORT_SYMBOL(dma_region_sync_for_device);
1352EXPORT_SYMBOL(dma_region_mmap);
1353EXPORT_SYMBOL(dma_region_offset_to_bus);
1354
1355/** iso.c **/
1356EXPORT_SYMBOL(hpsb_iso_xmit_init);
1357EXPORT_SYMBOL(hpsb_iso_recv_init);
1358EXPORT_SYMBOL(hpsb_iso_xmit_start);
1359EXPORT_SYMBOL(hpsb_iso_recv_start);
1360EXPORT_SYMBOL(hpsb_iso_recv_listen_channel);
1361EXPORT_SYMBOL(hpsb_iso_recv_unlisten_channel);
1362EXPORT_SYMBOL(hpsb_iso_recv_set_channel_mask);
1363EXPORT_SYMBOL(hpsb_iso_stop);
1364EXPORT_SYMBOL(hpsb_iso_shutdown);
1365EXPORT_SYMBOL(hpsb_iso_xmit_queue_packet);
1366EXPORT_SYMBOL(hpsb_iso_xmit_sync);
1367EXPORT_SYMBOL(hpsb_iso_recv_release_packets);
1368EXPORT_SYMBOL(hpsb_iso_n_ready);
1369EXPORT_SYMBOL(hpsb_iso_packet_sent);
1370EXPORT_SYMBOL(hpsb_iso_packet_received);
1371EXPORT_SYMBOL(hpsb_iso_wake);
1372EXPORT_SYMBOL(hpsb_iso_recv_flush);
1373
1374/** csr1212.c **/
Ben Collins1934b8b2005-07-09 20:01:23 -04001375EXPORT_SYMBOL(csr1212_attach_keyval_to_directory);
1376EXPORT_SYMBOL(csr1212_detach_keyval_from_directory);
Stefan Richterc1a37f22007-03-14 00:20:53 +01001377EXPORT_SYMBOL(csr1212_get_keyval);
1378EXPORT_SYMBOL(csr1212_new_directory);
Ben Collins1934b8b2005-07-09 20:01:23 -04001379EXPORT_SYMBOL(csr1212_parse_keyval);
Stefan Richterc1a37f22007-03-14 00:20:53 +01001380EXPORT_SYMBOL(csr1212_read);
1381EXPORT_SYMBOL(csr1212_release_keyval);