blob: 8b7511d525334e457dbb3fbb63a7e443dbeaaf2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1394 for Linux
3 *
4 * Transaction support.
5 *
6 * Copyright (C) 1999 Andreas E. Bombe
7 *
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/bitops.h>
Stefan Richter7fb9add2007-03-11 22:49:05 +010013#include <linux/hardirq.h>
Stefan Richter99519032006-07-02 14:17:00 +020014#include <linux/spinlock.h>
Torsten Kaiser3a23a812007-04-09 21:03:15 +020015#include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */
Stefan Richter99519032006-07-02 14:17:00 +020016#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Stefan Richterd83e7d82006-07-03 12:02:31 -040018#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/errno.h>
20
21#include "ieee1394.h"
22#include "ieee1394_types.h"
23#include "hosts.h"
24#include "ieee1394_core.h"
Adrian Bunke27d3012005-11-19 21:23:48 -050025#include "ieee1394_transactions.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define PREP_ASYNC_HEAD_ADDRESS(tc) \
28 packet->tcode = tc; \
29 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
30 | (1 << 8) | (tc << 4); \
31 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
32 packet->header[2] = addr & 0xffffffff
33
Stefan Richter99519032006-07-02 14:17:00 +020034#ifndef HPSB_DEBUG_TLABELS
35static
36#endif
37spinlock_t hpsb_tlabel_lock = SPIN_LOCK_UNLOCKED;
38
39static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
42{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050043 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
44 packet->header_size = 12;
45 packet->data_size = 0;
46 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050049static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
50 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050052 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
53 packet->header[3] = length << 16;
54 packet->header_size = 16;
55 packet->data_size = 0;
56 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050059static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
60 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050062 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
63 packet->header[3] = data;
64 packet->header_size = 16;
65 packet->data_size = 0;
66 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050069static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
70 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050072 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
73 packet->header[3] = length << 16;
74 packet->header_size = 16;
75 packet->expect_response = 1;
76 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050080 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050082 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
83 packet->header[3] = (length << 16) | extcode;
84 packet->header_size = 16;
85 packet->data_size = length;
86 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050090 int tag, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050092 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
93 | (TCODE_ISO_DATA << 4) | sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050095 packet->header_size = 4;
96 packet->data_size = length;
97 packet->type = hpsb_iso;
98 packet->tcode = TCODE_ISO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
102{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500103 packet->header[0] = data;
104 packet->header[1] = ~data;
105 packet->header_size = 8;
106 packet->data_size = 0;
107 packet->expect_response = 0;
108 packet->type = hpsb_raw; /* No CRC added */
109 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
113 int channel, int tag, int sync)
114{
115 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500116 | (TCODE_STREAM_DATA << 4) | sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 packet->header_size = 4;
119 packet->data_size = length;
120 packet->type = hpsb_async;
121 packet->tcode = TCODE_ISO_DATA;
122}
123
Stefan Richter99519032006-07-02 14:17:00 +0200124/* same as hpsb_get_tlabel, except that it returns immediately */
125static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
126{
127 unsigned long flags, *tp;
128 u8 *next;
129 int tlabel, n = NODEID_TO_NODE(packet->node_id);
130
131 /* Broadcast transactions are complete once the request has been sent.
132 * Use the same transaction label for all broadcast transactions. */
133 if (unlikely(n == ALL_NODES)) {
134 packet->tlabel = 0;
135 return 0;
136 }
137 tp = packet->host->tl_pool[n].map;
138 next = &packet->host->next_tl[n];
139
140 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
141 tlabel = find_next_zero_bit(tp, 64, *next);
142 if (tlabel > 63)
143 tlabel = find_first_zero_bit(tp, 64);
144 if (tlabel > 63) {
145 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
146 return -EAGAIN;
147 }
148 __set_bit(tlabel, tp);
149 *next = (tlabel + 1) & 63;
150 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
151
152 packet->tlabel = tlabel;
153 return 0;
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/**
157 * hpsb_get_tlabel - allocate a transaction label
Stefan Richter99519032006-07-02 14:17:00 +0200158 * @packet: the packet whose tlabel and tl_pool we set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Every asynchronous transaction on the 1394 bus needs a transaction
161 * label to match the response to the request. This label has to be
162 * different from any other transaction label in an outstanding request to
163 * the same node to make matching possible without ambiguity.
164 *
165 * There are 64 different tlabels, so an allocated tlabel has to be freed
166 * with hpsb_free_tlabel() after the transaction is complete (unless it's
167 * reused again for the same target node).
168 *
169 * Return value: Zero on success, otherwise non-zero. A non-zero return
170 * generally means there are no available tlabels. If this is called out
171 * of interrupt or atomic context, then it will sleep until can return a
Stefan Richter99519032006-07-02 14:17:00 +0200172 * tlabel or a signal is received.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174int hpsb_get_tlabel(struct hpsb_packet *packet)
175{
Stefan Richter99519032006-07-02 14:17:00 +0200176 if (irqs_disabled() || in_atomic())
177 return hpsb_get_tlabel_atomic(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Stefan Richter99519032006-07-02 14:17:00 +0200179 /* NB: The macro wait_event_interruptible() is called with a condition
180 * argument with side effect. This is only possible because the side
181 * effect does not occur until the condition became true, and
182 * wait_event_interruptible() won't evaluate the condition again after
183 * that. */
184 return wait_event_interruptible(tlabel_wq,
185 !hpsb_get_tlabel_atomic(packet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/**
189 * hpsb_free_tlabel - free an allocated transaction label
Stefan Richter99519032006-07-02 14:17:00 +0200190 * @packet: packet whose tlabel and tl_pool needs to be cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
192 * Frees the transaction label allocated with hpsb_get_tlabel(). The
193 * tlabel has to be freed after the transaction is complete (i.e. response
194 * was received for a split transaction or packet was sent for a unified
195 * transaction).
196 *
197 * A tlabel must not be freed twice.
198 */
199void hpsb_free_tlabel(struct hpsb_packet *packet)
200{
Stefan Richter99519032006-07-02 14:17:00 +0200201 unsigned long flags, *tp;
202 int tlabel, n = NODEID_TO_NODE(packet->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Ben Collinseaf88452006-06-12 18:12:49 -0400204 if (unlikely(n == ALL_NODES))
205 return;
Stefan Richter99519032006-07-02 14:17:00 +0200206 tp = packet->host->tl_pool[n].map;
207 tlabel = packet->tlabel;
208 BUG_ON(tlabel > 63 || tlabel < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Stefan Richter99519032006-07-02 14:17:00 +0200210 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
211 BUG_ON(!__test_and_clear_bit(tlabel, tp));
212 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Stefan Richter99519032006-07-02 14:17:00 +0200214 wake_up_interruptible(&tlabel_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Stefan Richterafd65462007-03-05 03:06:23 +0100217/**
218 * hpsb_packet_success - Make sense of the ack and reply codes
219 *
220 * Make sense of the ack and reply codes and return more convenient error codes:
221 * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can
222 * probably resolved by retry. -%EREMOTEIO = node suffers from an internal
223 * error. -%EACCES = this transaction is not allowed on requested address.
224 * -%EINVAL = invalid address at node.
225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226int hpsb_packet_success(struct hpsb_packet *packet)
227{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500228 switch (packet->ack_code) {
229 case ACK_PENDING:
230 switch ((packet->header[1] >> 12) & 0xf) {
231 case RCODE_COMPLETE:
232 return 0;
233 case RCODE_CONFLICT_ERROR:
234 return -EAGAIN;
235 case RCODE_DATA_ERROR:
236 return -EREMOTEIO;
237 case RCODE_TYPE_ERROR:
238 return -EACCES;
239 case RCODE_ADDRESS_ERROR:
240 return -EINVAL;
241 default:
242 HPSB_ERR("received reserved rcode %d from node %d",
243 (packet->header[1] >> 12) & 0xf,
244 packet->node_id);
245 return -EAGAIN;
246 }
Stefan Richterd83e7d82006-07-03 12:02:31 -0400247 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500249 case ACK_BUSY_X:
250 case ACK_BUSY_A:
251 case ACK_BUSY_B:
252 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500254 case ACK_TYPE_ERROR:
255 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500257 case ACK_COMPLETE:
258 if (packet->tcode == TCODE_WRITEQ
259 || packet->tcode == TCODE_WRITEB) {
260 return 0;
261 } else {
262 HPSB_ERR("impossible ack_complete from node %d "
263 "(tcode %d)", packet->node_id, packet->tcode);
264 return -EAGAIN;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500267 case ACK_DATA_ERROR:
268 if (packet->tcode == TCODE_WRITEB
269 || packet->tcode == TCODE_LOCK_REQUEST) {
270 return -EAGAIN;
271 } else {
272 HPSB_ERR("impossible ack_data_error from node %d "
273 "(tcode %d)", packet->node_id, packet->tcode);
274 return -EAGAIN;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500277 case ACK_ADDRESS_ERROR:
278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500280 case ACK_TARDY:
281 case ACK_CONFLICT_ERROR:
282 case ACKX_NONE:
283 case ACKX_SEND_ERROR:
284 case ACKX_ABORTED:
285 case ACKX_TIMEOUT:
286 /* error while sending */
287 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500289 default:
290 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
291 packet->ack_code, packet->node_id, packet->tcode);
292 return -EAGAIN;
293 }
Stefan Richterd83e7d82006-07-03 12:02:31 -0400294 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
298 u64 addr, size_t length)
299{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500300 struct hpsb_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (length == 0)
303 return NULL;
304
305 packet = hpsb_alloc_packet(length);
306 if (!packet)
307 return NULL;
308
309 packet->host = host;
310 packet->node_id = node;
311
312 if (hpsb_get_tlabel(packet)) {
313 hpsb_free_packet(packet);
314 return NULL;
315 }
316
317 if (length == 4)
318 fill_async_readquad(packet, addr);
319 else
320 fill_async_readblock(packet, addr, length);
321
322 return packet;
323}
324
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500325struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
326 u64 addr, quadlet_t * buffer,
327 size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct hpsb_packet *packet;
330
331 if (length == 0)
332 return NULL;
333
334 packet = hpsb_alloc_packet(length);
335 if (!packet)
336 return NULL;
337
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500338 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 packet->data[length >> 2] = 0;
340 }
341 packet->host = host;
342 packet->node_id = node;
343
344 if (hpsb_get_tlabel(packet)) {
345 hpsb_free_packet(packet);
346 return NULL;
347 }
348
349 if (length == 4) {
350 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
351 } else {
352 fill_async_writeblock(packet, addr, length);
353 if (buffer)
354 memcpy(packet->data, buffer, length);
355 }
356
357 return packet;
358}
359
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500360struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
361 int length, int channel, int tag,
362 int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct hpsb_packet *packet;
365
366 if (length == 0)
367 return NULL;
368
369 packet = hpsb_alloc_packet(length);
370 if (!packet)
371 return NULL;
372
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500373 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 packet->data[length >> 2] = 0;
375 }
376 packet->host = host;
377
378 if (hpsb_get_tlabel(packet)) {
379 hpsb_free_packet(packet);
380 return NULL;
381 }
382
383 fill_async_stream_packet(packet, length, channel, tag, sync);
384 if (buffer)
385 memcpy(packet->data, buffer, length);
386
387 return packet;
388}
389
390struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500391 u64 addr, int extcode,
392 quadlet_t * data, quadlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct hpsb_packet *p;
395 u32 length;
396
397 p = hpsb_alloc_packet(8);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500398 if (!p)
399 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 p->host = host;
402 p->node_id = node;
403 if (hpsb_get_tlabel(p)) {
404 hpsb_free_packet(p);
405 return NULL;
406 }
407
408 switch (extcode) {
409 case EXTCODE_FETCH_ADD:
410 case EXTCODE_LITTLE_ADD:
411 length = 4;
412 if (data)
413 p->data[0] = *data;
414 break;
415 default:
416 length = 8;
417 if (data) {
418 p->data[0] = arg;
419 p->data[1] = *data;
420 }
421 break;
422 }
423 fill_async_lock(p, addr, extcode, length);
424
425 return p;
426}
427
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500428struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
429 nodeid_t node, u64 addr, int extcode,
430 octlet_t * data, octlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct hpsb_packet *p;
433 u32 length;
434
435 p = hpsb_alloc_packet(16);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500436 if (!p)
437 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 p->host = host;
440 p->node_id = node;
441 if (hpsb_get_tlabel(p)) {
442 hpsb_free_packet(p);
443 return NULL;
444 }
445
446 switch (extcode) {
447 case EXTCODE_FETCH_ADD:
448 case EXTCODE_LITTLE_ADD:
449 length = 8;
450 if (data) {
451 p->data[0] = *data >> 32;
452 p->data[1] = *data & 0xffffffff;
453 }
454 break;
455 default:
456 length = 16;
457 if (data) {
458 p->data[0] = arg >> 32;
459 p->data[1] = arg & 0xffffffff;
460 p->data[2] = *data >> 32;
461 p->data[3] = *data & 0xffffffff;
462 }
463 break;
464 }
465 fill_async_lock(p, addr, extcode, length);
466
467 return p;
468}
469
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500470struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500472 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500474 p = hpsb_alloc_packet(0);
475 if (!p)
476 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500478 p->host = host;
479 fill_phy_packet(p, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500481 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
485 int length, int channel,
486 int tag, int sync)
487{
488 struct hpsb_packet *p;
489
490 p = hpsb_alloc_packet(length);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500491 if (!p)
492 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 p->host = host;
495 fill_iso_packet(p, length, channel, tag, sync);
496
497 p->generation = get_hpsb_generation(host);
498
499 return p;
500}
501
502/*
503 * FIXME - these functions should probably read from / write to user space to
504 * avoid in kernel buffers for user space callers
505 */
506
Stefan Richterafd65462007-03-05 03:06:23 +0100507/**
508 * hpsb_read - generic read function
509 *
510 * Recognizes the local node ID and act accordingly. Automatically uses a
511 * quadlet read request if @length == 4 and and a block read request otherwise.
512 * It does not yet support lengths that are not a multiple of 4.
513 *
514 * You must explicitly specifiy the @generation for which the node ID is valid,
515 * to avoid sending packets to the wrong nodes when we race with a bus reset.
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500518 u64 addr, quadlet_t * buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500520 struct hpsb_packet *packet;
521 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500523 if (length == 0)
524 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500526 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 packet = hpsb_make_readpacket(host, node, addr, length);
529
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500530 if (!packet) {
531 return -ENOMEM;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500535 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (retval < 0)
537 goto hpsb_read_fail;
538
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500539 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500541 if (retval == 0) {
542 if (length == 4) {
543 *buffer = packet->header[3];
544 } else {
545 memcpy(buffer, packet->data, length);
546 }
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500549 hpsb_read_fail:
550 hpsb_free_tlabel(packet);
551 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500553 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Stefan Richterafd65462007-03-05 03:06:23 +0100556/**
557 * hpsb_write - generic write function
558 *
559 * Recognizes the local node ID and act accordingly. Automatically uses a
560 * quadlet write request if @length == 4 and and a block write request
561 * otherwise. It does not yet support lengths that are not a multiple of 4.
562 *
563 * You must explicitly specifiy the @generation for which the node ID is valid,
564 * to avoid sending packets to the wrong nodes when we race with a bus reset.
565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500567 u64 addr, quadlet_t * buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct hpsb_packet *packet;
570 int retval;
571
572 if (length == 0)
573 return -EINVAL;
574
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500575 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500577 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (!packet)
580 return -ENOMEM;
581
582 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500583 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (retval < 0)
585 goto hpsb_write_fail;
586
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500587 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500589 hpsb_write_fail:
590 hpsb_free_tlabel(packet);
591 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500593 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Jody McIntyre9ac485d2005-05-16 21:54:00 -0700596#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500599 u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500601 struct hpsb_packet *packet;
602 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500604 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500607 if (!packet)
608 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500611 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (retval < 0)
613 goto hpsb_lock_fail;
614
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500615 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500617 if (retval == 0) {
618 *data = packet->data[0];
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500621 hpsb_lock_fail:
622 hpsb_free_tlabel(packet);
623 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500625 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500629 quadlet_t * buffer, size_t length, u32 specifier_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned int version)
631{
632 struct hpsb_packet *packet;
633 int retval = 0;
634 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
635 u8 specifier_id_lo = specifier_id & 0xff;
636
637 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
638
639 length += 8;
640
641 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
642 if (!packet)
643 return -ENOMEM;
644
645 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500646 packet->data[1] =
647 cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 memcpy(&(packet->data[2]), buffer, length - 8);
650
651 packet->generation = generation;
652
653 packet->no_waiter = 1;
654
655 retval = hpsb_send_packet(packet);
656 if (retval < 0)
657 hpsb_free_packet(packet);
658
659 return retval;
660}
Jody McIntyre9ac485d2005-05-16 21:54:00 -0700661
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500662#endif /* 0 */