blob: 751960037e27f3a943bacd144aa0969a38e3b06e [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
12#include <linux/sched.h>
13#include <linux/bitops.h>
14#include <linux/smp_lock.h>
15#include <linux/interrupt.h>
16
Stefan Richterd83e7d82006-07-03 12:02:31 -040017#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/errno.h>
19
20#include "ieee1394.h"
21#include "ieee1394_types.h"
22#include "hosts.h"
23#include "ieee1394_core.h"
24#include "highlevel.h"
25#include "nodemgr.h"
Adrian Bunke27d3012005-11-19 21:23:48 -050026#include "ieee1394_transactions.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define PREP_ASYNC_HEAD_ADDRESS(tc) \
29 packet->tcode = tc; \
30 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
31 | (1 << 8) | (tc << 4); \
32 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
33 packet->header[2] = addr & 0xffffffff
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
36{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050037 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
38 packet->header_size = 12;
39 packet->data_size = 0;
40 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050043static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
44 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050046 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
47 packet->header[3] = length << 16;
48 packet->header_size = 16;
49 packet->data_size = 0;
50 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050053static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
54 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050056 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
57 packet->header[3] = data;
58 packet->header_size = 16;
59 packet->data_size = 0;
60 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050063static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
64 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050066 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
67 packet->header[3] = length << 16;
68 packet->header_size = 16;
69 packet->expect_response = 1;
70 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050074 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050076 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
77 packet->header[3] = (length << 16) | extcode;
78 packet->header_size = 16;
79 packet->data_size = length;
80 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050084 int tag, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050086 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
87 | (TCODE_ISO_DATA << 4) | sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050089 packet->header_size = 4;
90 packet->data_size = length;
91 packet->type = hpsb_iso;
92 packet->tcode = TCODE_ISO_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
96{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050097 packet->header[0] = data;
98 packet->header[1] = ~data;
99 packet->header_size = 8;
100 packet->data_size = 0;
101 packet->expect_response = 0;
102 packet->type = hpsb_raw; /* No CRC added */
103 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
107 int channel, int tag, int sync)
108{
109 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500110 | (TCODE_STREAM_DATA << 4) | sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 packet->header_size = 4;
113 packet->data_size = length;
114 packet->type = hpsb_async;
115 packet->tcode = TCODE_ISO_DATA;
116}
117
118/**
119 * hpsb_get_tlabel - allocate a transaction label
120 * @packet: the packet who's tlabel/tpool we set
121 *
122 * Every asynchronous transaction on the 1394 bus needs a transaction
123 * label to match the response to the request. This label has to be
124 * different from any other transaction label in an outstanding request to
125 * the same node to make matching possible without ambiguity.
126 *
127 * There are 64 different tlabels, so an allocated tlabel has to be freed
128 * with hpsb_free_tlabel() after the transaction is complete (unless it's
129 * reused again for the same target node).
130 *
131 * Return value: Zero on success, otherwise non-zero. A non-zero return
132 * generally means there are no available tlabels. If this is called out
133 * of interrupt or atomic context, then it will sleep until can return a
134 * tlabel.
135 */
136int hpsb_get_tlabel(struct hpsb_packet *packet)
137{
138 unsigned long flags;
139 struct hpsb_tlabel_pool *tp;
Ben Collinseaf88452006-06-12 18:12:49 -0400140 int n = NODEID_TO_NODE(packet->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Ben Collinseaf88452006-06-12 18:12:49 -0400142 if (unlikely(n == ALL_NODES))
143 return 0;
144 tp = &packet->host->tpool[n];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (irqs_disabled() || in_atomic()) {
147 if (down_trylock(&tp->count))
148 return 1;
149 } else {
150 down(&tp->count);
151 }
152
153 spin_lock_irqsave(&tp->lock, flags);
154
155 packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);
156 if (packet->tlabel > 63)
157 packet->tlabel = find_first_zero_bit(tp->pool, 64);
158 tp->next = (packet->tlabel + 1) % 64;
159 /* Should _never_ happen */
160 BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));
161 tp->allocations++;
162 spin_unlock_irqrestore(&tp->lock, flags);
163
164 return 0;
165}
166
167/**
168 * hpsb_free_tlabel - free an allocated transaction label
169 * @packet: packet whos tlabel/tpool needs to be cleared
170 *
171 * Frees the transaction label allocated with hpsb_get_tlabel(). The
172 * tlabel has to be freed after the transaction is complete (i.e. response
173 * was received for a split transaction or packet was sent for a unified
174 * transaction).
175 *
176 * A tlabel must not be freed twice.
177 */
178void hpsb_free_tlabel(struct hpsb_packet *packet)
179{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500180 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct hpsb_tlabel_pool *tp;
Ben Collinseaf88452006-06-12 18:12:49 -0400182 int n = NODEID_TO_NODE(packet->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ben Collinseaf88452006-06-12 18:12:49 -0400184 if (unlikely(n == ALL_NODES))
185 return;
186 tp = &packet->host->tpool[n];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);
189
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500190 spin_lock_irqsave(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500192 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 up(&tp->count);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197int hpsb_packet_success(struct hpsb_packet *packet)
198{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500199 switch (packet->ack_code) {
200 case ACK_PENDING:
201 switch ((packet->header[1] >> 12) & 0xf) {
202 case RCODE_COMPLETE:
203 return 0;
204 case RCODE_CONFLICT_ERROR:
205 return -EAGAIN;
206 case RCODE_DATA_ERROR:
207 return -EREMOTEIO;
208 case RCODE_TYPE_ERROR:
209 return -EACCES;
210 case RCODE_ADDRESS_ERROR:
211 return -EINVAL;
212 default:
213 HPSB_ERR("received reserved rcode %d from node %d",
214 (packet->header[1] >> 12) & 0xf,
215 packet->node_id);
216 return -EAGAIN;
217 }
Stefan Richterd83e7d82006-07-03 12:02:31 -0400218 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500220 case ACK_BUSY_X:
221 case ACK_BUSY_A:
222 case ACK_BUSY_B:
223 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500225 case ACK_TYPE_ERROR:
226 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500228 case ACK_COMPLETE:
229 if (packet->tcode == TCODE_WRITEQ
230 || packet->tcode == TCODE_WRITEB) {
231 return 0;
232 } else {
233 HPSB_ERR("impossible ack_complete from node %d "
234 "(tcode %d)", packet->node_id, packet->tcode);
235 return -EAGAIN;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500238 case ACK_DATA_ERROR:
239 if (packet->tcode == TCODE_WRITEB
240 || packet->tcode == TCODE_LOCK_REQUEST) {
241 return -EAGAIN;
242 } else {
243 HPSB_ERR("impossible ack_data_error from node %d "
244 "(tcode %d)", packet->node_id, packet->tcode);
245 return -EAGAIN;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500248 case ACK_ADDRESS_ERROR:
249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500251 case ACK_TARDY:
252 case ACK_CONFLICT_ERROR:
253 case ACKX_NONE:
254 case ACKX_SEND_ERROR:
255 case ACKX_ABORTED:
256 case ACKX_TIMEOUT:
257 /* error while sending */
258 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500260 default:
261 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
262 packet->ack_code, packet->node_id, packet->tcode);
263 return -EAGAIN;
264 }
Stefan Richterd83e7d82006-07-03 12:02:31 -0400265 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
269 u64 addr, size_t length)
270{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500271 struct hpsb_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (length == 0)
274 return NULL;
275
276 packet = hpsb_alloc_packet(length);
277 if (!packet)
278 return NULL;
279
280 packet->host = host;
281 packet->node_id = node;
282
283 if (hpsb_get_tlabel(packet)) {
284 hpsb_free_packet(packet);
285 return NULL;
286 }
287
288 if (length == 4)
289 fill_async_readquad(packet, addr);
290 else
291 fill_async_readblock(packet, addr, length);
292
293 return packet;
294}
295
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500296struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
297 u64 addr, quadlet_t * buffer,
298 size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct hpsb_packet *packet;
301
302 if (length == 0)
303 return NULL;
304
305 packet = hpsb_alloc_packet(length);
306 if (!packet)
307 return NULL;
308
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500309 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 packet->data[length >> 2] = 0;
311 }
312 packet->host = host;
313 packet->node_id = node;
314
315 if (hpsb_get_tlabel(packet)) {
316 hpsb_free_packet(packet);
317 return NULL;
318 }
319
320 if (length == 4) {
321 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
322 } else {
323 fill_async_writeblock(packet, addr, length);
324 if (buffer)
325 memcpy(packet->data, buffer, length);
326 }
327
328 return packet;
329}
330
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500331struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
332 int length, int channel, int tag,
333 int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct hpsb_packet *packet;
336
337 if (length == 0)
338 return NULL;
339
340 packet = hpsb_alloc_packet(length);
341 if (!packet)
342 return NULL;
343
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500344 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 packet->data[length >> 2] = 0;
346 }
347 packet->host = host;
348
349 if (hpsb_get_tlabel(packet)) {
350 hpsb_free_packet(packet);
351 return NULL;
352 }
353
354 fill_async_stream_packet(packet, length, channel, tag, sync);
355 if (buffer)
356 memcpy(packet->data, buffer, length);
357
358 return packet;
359}
360
361struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500362 u64 addr, int extcode,
363 quadlet_t * data, quadlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct hpsb_packet *p;
366 u32 length;
367
368 p = hpsb_alloc_packet(8);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500369 if (!p)
370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 p->host = host;
373 p->node_id = node;
374 if (hpsb_get_tlabel(p)) {
375 hpsb_free_packet(p);
376 return NULL;
377 }
378
379 switch (extcode) {
380 case EXTCODE_FETCH_ADD:
381 case EXTCODE_LITTLE_ADD:
382 length = 4;
383 if (data)
384 p->data[0] = *data;
385 break;
386 default:
387 length = 8;
388 if (data) {
389 p->data[0] = arg;
390 p->data[1] = *data;
391 }
392 break;
393 }
394 fill_async_lock(p, addr, extcode, length);
395
396 return p;
397}
398
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500399struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
400 nodeid_t node, u64 addr, int extcode,
401 octlet_t * data, octlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct hpsb_packet *p;
404 u32 length;
405
406 p = hpsb_alloc_packet(16);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500407 if (!p)
408 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 p->host = host;
411 p->node_id = node;
412 if (hpsb_get_tlabel(p)) {
413 hpsb_free_packet(p);
414 return NULL;
415 }
416
417 switch (extcode) {
418 case EXTCODE_FETCH_ADD:
419 case EXTCODE_LITTLE_ADD:
420 length = 8;
421 if (data) {
422 p->data[0] = *data >> 32;
423 p->data[1] = *data & 0xffffffff;
424 }
425 break;
426 default:
427 length = 16;
428 if (data) {
429 p->data[0] = arg >> 32;
430 p->data[1] = arg & 0xffffffff;
431 p->data[2] = *data >> 32;
432 p->data[3] = *data & 0xffffffff;
433 }
434 break;
435 }
436 fill_async_lock(p, addr, extcode, length);
437
438 return p;
439}
440
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500441struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500443 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500445 p = hpsb_alloc_packet(0);
446 if (!p)
447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500449 p->host = host;
450 fill_phy_packet(p, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500452 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
456 int length, int channel,
457 int tag, int sync)
458{
459 struct hpsb_packet *p;
460
461 p = hpsb_alloc_packet(length);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500462 if (!p)
463 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 p->host = host;
466 fill_iso_packet(p, length, channel, tag, sync);
467
468 p->generation = get_hpsb_generation(host);
469
470 return p;
471}
472
473/*
474 * FIXME - these functions should probably read from / write to user space to
475 * avoid in kernel buffers for user space callers
476 */
477
478int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500479 u64 addr, quadlet_t * buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500481 struct hpsb_packet *packet;
482 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500484 if (length == 0)
485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500487 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 packet = hpsb_make_readpacket(host, node, addr, length);
490
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500491 if (!packet) {
492 return -ENOMEM;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500496 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (retval < 0)
498 goto hpsb_read_fail;
499
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500500 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500502 if (retval == 0) {
503 if (length == 4) {
504 *buffer = packet->header[3];
505 } else {
506 memcpy(buffer, packet->data, length);
507 }
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500510 hpsb_read_fail:
511 hpsb_free_tlabel(packet);
512 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500514 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517int hpsb_write(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{
520 struct hpsb_packet *packet;
521 int retval;
522
523 if (length == 0)
524 return -EINVAL;
525
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
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500528 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!packet)
531 return -ENOMEM;
532
533 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500534 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (retval < 0)
536 goto hpsb_write_fail;
537
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500538 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500540 hpsb_write_fail:
541 hpsb_free_tlabel(packet);
542 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500544 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Jody McIntyre9ac485d2005-05-16 21:54:00 -0700547#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500550 u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500552 struct hpsb_packet *packet;
553 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500555 BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500558 if (!packet)
559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500562 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (retval < 0)
564 goto hpsb_lock_fail;
565
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500566 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500568 if (retval == 0) {
569 *data = packet->data[0];
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500572 hpsb_lock_fail:
573 hpsb_free_tlabel(packet);
574 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500576 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500580 quadlet_t * buffer, size_t length, u32 specifier_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 unsigned int version)
582{
583 struct hpsb_packet *packet;
584 int retval = 0;
585 u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
586 u8 specifier_id_lo = specifier_id & 0xff;
587
588 HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);
589
590 length += 8;
591
592 packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
593 if (!packet)
594 return -ENOMEM;
595
596 packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500597 packet->data[1] =
598 cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 memcpy(&(packet->data[2]), buffer, length - 8);
601
602 packet->generation = generation;
603
604 packet->no_waiter = 1;
605
606 retval = hpsb_send_packet(packet);
607 if (retval < 0)
608 hpsb_free_packet(packet);
609
610 return retval;
611}
Jody McIntyre9ac485d2005-05-16 21:54:00 -0700612
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500613#endif /* 0 */