blob: 675b3135d5f19a78816976b0835deb54468c94af [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 Richter9543a932007-04-10 02:39:07 +020013#include <linux/compiler.h>
Stefan Richter7fb9add2007-03-11 22:49:05 +010014#include <linux/hardirq.h>
Stefan Richter99519032006-07-02 14:17:00 +020015#include <linux/spinlock.h>
Stefan Richter9543a932007-04-10 02:39:07 +020016#include <linux/string.h>
Torsten Kaiser3a23a812007-04-09 21:03:15 +020017#include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */
Stefan Richter99519032006-07-02 14:17:00 +020018#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Stefan Richterd83e7d82006-07-03 12:02:31 -040020#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/errno.h>
Stefan Richter9543a932007-04-10 02:39:07 +020022#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "ieee1394.h"
25#include "ieee1394_types.h"
26#include "hosts.h"
27#include "ieee1394_core.h"
Adrian Bunke27d3012005-11-19 21:23:48 -050028#include "ieee1394_transactions.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define PREP_ASYNC_HEAD_ADDRESS(tc) \
31 packet->tcode = tc; \
32 packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
33 | (1 << 8) | (tc << 4); \
34 packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
35 packet->header[2] = addr & 0xffffffff
36
Stefan Richter99519032006-07-02 14:17:00 +020037#ifndef HPSB_DEBUG_TLABELS
38static
39#endif
Milind Arun Choudharydf18ce82007-04-11 23:24:34 +053040DEFINE_SPINLOCK(hpsb_tlabel_lock);
Stefan Richter99519032006-07-02 14:17:00 +020041
42static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
45{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050046 PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
47 packet->header_size = 12;
48 packet->data_size = 0;
49 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050052static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
53 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050055 PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
56 packet->header[3] = length << 16;
57 packet->header_size = 16;
58 packet->data_size = 0;
59 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050062static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
63 quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050065 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
66 packet->header[3] = data;
67 packet->header_size = 16;
68 packet->data_size = 0;
69 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050072static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
73 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050075 PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
76 packet->header[3] = length << 16;
77 packet->header_size = 16;
78 packet->expect_response = 1;
79 packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050083 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050085 PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
86 packet->header[3] = (length << 16) | extcode;
87 packet->header_size = 16;
88 packet->data_size = length;
89 packet->expect_response = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
93{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -050094 packet->header[0] = data;
95 packet->header[1] = ~data;
96 packet->header_size = 8;
97 packet->data_size = 0;
98 packet->expect_response = 0;
99 packet->type = hpsb_raw; /* No CRC added */
100 packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
104 int channel, int tag, int sync)
105{
106 packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500107 | (TCODE_STREAM_DATA << 4) | sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 packet->header_size = 4;
110 packet->data_size = length;
111 packet->type = hpsb_async;
112 packet->tcode = TCODE_ISO_DATA;
113}
114
Stefan Richter99519032006-07-02 14:17:00 +0200115/* same as hpsb_get_tlabel, except that it returns immediately */
116static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet)
117{
118 unsigned long flags, *tp;
119 u8 *next;
120 int tlabel, n = NODEID_TO_NODE(packet->node_id);
121
122 /* Broadcast transactions are complete once the request has been sent.
123 * Use the same transaction label for all broadcast transactions. */
124 if (unlikely(n == ALL_NODES)) {
125 packet->tlabel = 0;
126 return 0;
127 }
128 tp = packet->host->tl_pool[n].map;
129 next = &packet->host->next_tl[n];
130
131 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
132 tlabel = find_next_zero_bit(tp, 64, *next);
133 if (tlabel > 63)
134 tlabel = find_first_zero_bit(tp, 64);
135 if (tlabel > 63) {
136 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
137 return -EAGAIN;
138 }
139 __set_bit(tlabel, tp);
140 *next = (tlabel + 1) & 63;
141 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
142
143 packet->tlabel = tlabel;
144 return 0;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/**
148 * hpsb_get_tlabel - allocate a transaction label
Stefan Richter99519032006-07-02 14:17:00 +0200149 * @packet: the packet whose tlabel and tl_pool we set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Every asynchronous transaction on the 1394 bus needs a transaction
152 * label to match the response to the request. This label has to be
153 * different from any other transaction label in an outstanding request to
154 * the same node to make matching possible without ambiguity.
155 *
156 * There are 64 different tlabels, so an allocated tlabel has to be freed
157 * with hpsb_free_tlabel() after the transaction is complete (unless it's
158 * reused again for the same target node).
159 *
160 * Return value: Zero on success, otherwise non-zero. A non-zero return
161 * generally means there are no available tlabels. If this is called out
162 * of interrupt or atomic context, then it will sleep until can return a
Stefan Richter99519032006-07-02 14:17:00 +0200163 * tlabel or a signal is received.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
165int hpsb_get_tlabel(struct hpsb_packet *packet)
166{
Stefan Richter99519032006-07-02 14:17:00 +0200167 if (irqs_disabled() || in_atomic())
168 return hpsb_get_tlabel_atomic(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Stefan Richter99519032006-07-02 14:17:00 +0200170 /* NB: The macro wait_event_interruptible() is called with a condition
171 * argument with side effect. This is only possible because the side
172 * effect does not occur until the condition became true, and
173 * wait_event_interruptible() won't evaluate the condition again after
174 * that. */
175 return wait_event_interruptible(tlabel_wq,
176 !hpsb_get_tlabel_atomic(packet));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/**
180 * hpsb_free_tlabel - free an allocated transaction label
Stefan Richter99519032006-07-02 14:17:00 +0200181 * @packet: packet whose tlabel and tl_pool needs to be cleared
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 * Frees the transaction label allocated with hpsb_get_tlabel(). The
184 * tlabel has to be freed after the transaction is complete (i.e. response
185 * was received for a split transaction or packet was sent for a unified
186 * transaction).
187 *
188 * A tlabel must not be freed twice.
189 */
190void hpsb_free_tlabel(struct hpsb_packet *packet)
191{
Stefan Richter99519032006-07-02 14:17:00 +0200192 unsigned long flags, *tp;
193 int tlabel, n = NODEID_TO_NODE(packet->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Ben Collinseaf88452006-06-12 18:12:49 -0400195 if (unlikely(n == ALL_NODES))
196 return;
Stefan Richter99519032006-07-02 14:17:00 +0200197 tp = packet->host->tl_pool[n].map;
198 tlabel = packet->tlabel;
199 BUG_ON(tlabel > 63 || tlabel < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Stefan Richter99519032006-07-02 14:17:00 +0200201 spin_lock_irqsave(&hpsb_tlabel_lock, flags);
202 BUG_ON(!__test_and_clear_bit(tlabel, tp));
203 spin_unlock_irqrestore(&hpsb_tlabel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Stefan Richter99519032006-07-02 14:17:00 +0200205 wake_up_interruptible(&tlabel_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Stefan Richterafd65462007-03-05 03:06:23 +0100208/**
209 * hpsb_packet_success - Make sense of the ack and reply codes
210 *
211 * Make sense of the ack and reply codes and return more convenient error codes:
212 * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can
213 * probably resolved by retry. -%EREMOTEIO = node suffers from an internal
214 * error. -%EACCES = this transaction is not allowed on requested address.
215 * -%EINVAL = invalid address at node.
216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217int hpsb_packet_success(struct hpsb_packet *packet)
218{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500219 switch (packet->ack_code) {
220 case ACK_PENDING:
221 switch ((packet->header[1] >> 12) & 0xf) {
222 case RCODE_COMPLETE:
223 return 0;
224 case RCODE_CONFLICT_ERROR:
225 return -EAGAIN;
226 case RCODE_DATA_ERROR:
227 return -EREMOTEIO;
228 case RCODE_TYPE_ERROR:
229 return -EACCES;
230 case RCODE_ADDRESS_ERROR:
231 return -EINVAL;
232 default:
233 HPSB_ERR("received reserved rcode %d from node %d",
234 (packet->header[1] >> 12) & 0xf,
235 packet->node_id);
236 return -EAGAIN;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500239 case ACK_BUSY_X:
240 case ACK_BUSY_A:
241 case ACK_BUSY_B:
242 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500244 case ACK_TYPE_ERROR:
245 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500247 case ACK_COMPLETE:
248 if (packet->tcode == TCODE_WRITEQ
249 || packet->tcode == TCODE_WRITEB) {
250 return 0;
251 } else {
252 HPSB_ERR("impossible ack_complete from node %d "
253 "(tcode %d)", packet->node_id, packet->tcode);
254 return -EAGAIN;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500257 case ACK_DATA_ERROR:
258 if (packet->tcode == TCODE_WRITEB
259 || packet->tcode == TCODE_LOCK_REQUEST) {
260 return -EAGAIN;
261 } else {
262 HPSB_ERR("impossible ack_data_error 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_ADDRESS_ERROR:
268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500270 case ACK_TARDY:
271 case ACK_CONFLICT_ERROR:
272 case ACKX_NONE:
273 case ACKX_SEND_ERROR:
274 case ACKX_ABORTED:
275 case ACKX_TIMEOUT:
276 /* error while sending */
277 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500279 default:
280 HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
281 packet->ack_code, packet->node_id, packet->tcode);
282 return -EAGAIN;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
287 u64 addr, size_t length)
288{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500289 struct hpsb_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (length == 0)
292 return NULL;
293
294 packet = hpsb_alloc_packet(length);
295 if (!packet)
296 return NULL;
297
298 packet->host = host;
299 packet->node_id = node;
300
301 if (hpsb_get_tlabel(packet)) {
302 hpsb_free_packet(packet);
303 return NULL;
304 }
305
306 if (length == 4)
307 fill_async_readquad(packet, addr);
308 else
309 fill_async_readblock(packet, addr, length);
310
311 return packet;
312}
313
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500314struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
315 u64 addr, quadlet_t * buffer,
316 size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct hpsb_packet *packet;
319
320 if (length == 0)
321 return NULL;
322
323 packet = hpsb_alloc_packet(length);
324 if (!packet)
325 return NULL;
326
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500327 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 packet->data[length >> 2] = 0;
329 }
330 packet->host = host;
331 packet->node_id = node;
332
333 if (hpsb_get_tlabel(packet)) {
334 hpsb_free_packet(packet);
335 return NULL;
336 }
337
338 if (length == 4) {
339 fill_async_writequad(packet, addr, buffer ? *buffer : 0);
340 } else {
341 fill_async_writeblock(packet, addr, length);
342 if (buffer)
343 memcpy(packet->data, buffer, length);
344 }
345
346 return packet;
347}
348
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500349struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
350 int length, int channel, int tag,
351 int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct hpsb_packet *packet;
354
355 if (length == 0)
356 return NULL;
357
358 packet = hpsb_alloc_packet(length);
359 if (!packet)
360 return NULL;
361
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500362 if (length % 4) { /* zero padding bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 packet->data[length >> 2] = 0;
364 }
365 packet->host = host;
366
Stefan Richterea9057a2007-04-23 21:27:13 +0200367 /* Because it is too difficult to determine all PHY speeds and link
368 * speeds here, we use S100... */
369 packet->speed_code = IEEE1394_SPEED_100;
370
371 /* ...and prevent hpsb_send_packet() from overriding it. */
372 packet->node_id = LOCAL_BUS | ALL_NODES;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (hpsb_get_tlabel(packet)) {
375 hpsb_free_packet(packet);
376 return NULL;
377 }
378
379 fill_async_stream_packet(packet, length, channel, tag, sync);
380 if (buffer)
381 memcpy(packet->data, buffer, length);
382
383 return packet;
384}
385
386struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500387 u64 addr, int extcode,
388 quadlet_t * data, quadlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct hpsb_packet *p;
391 u32 length;
392
393 p = hpsb_alloc_packet(8);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500394 if (!p)
395 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 p->host = host;
398 p->node_id = node;
399 if (hpsb_get_tlabel(p)) {
400 hpsb_free_packet(p);
401 return NULL;
402 }
403
404 switch (extcode) {
405 case EXTCODE_FETCH_ADD:
406 case EXTCODE_LITTLE_ADD:
407 length = 4;
408 if (data)
409 p->data[0] = *data;
410 break;
411 default:
412 length = 8;
413 if (data) {
414 p->data[0] = arg;
415 p->data[1] = *data;
416 }
417 break;
418 }
419 fill_async_lock(p, addr, extcode, length);
420
421 return p;
422}
423
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500424struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
425 nodeid_t node, u64 addr, int extcode,
426 octlet_t * data, octlet_t arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct hpsb_packet *p;
429 u32 length;
430
431 p = hpsb_alloc_packet(16);
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500432 if (!p)
433 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 p->host = host;
436 p->node_id = node;
437 if (hpsb_get_tlabel(p)) {
438 hpsb_free_packet(p);
439 return NULL;
440 }
441
442 switch (extcode) {
443 case EXTCODE_FETCH_ADD:
444 case EXTCODE_LITTLE_ADD:
445 length = 8;
446 if (data) {
447 p->data[0] = *data >> 32;
448 p->data[1] = *data & 0xffffffff;
449 }
450 break;
451 default:
452 length = 16;
453 if (data) {
454 p->data[0] = arg >> 32;
455 p->data[1] = arg & 0xffffffff;
456 p->data[2] = *data >> 32;
457 p->data[3] = *data & 0xffffffff;
458 }
459 break;
460 }
461 fill_async_lock(p, addr, extcode, length);
462
463 return p;
464}
465
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500466struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500468 struct hpsb_packet *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500470 p = hpsb_alloc_packet(0);
471 if (!p)
472 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500474 p->host = host;
475 fill_phy_packet(p, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500477 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * FIXME - these functions should probably read from / write to user space to
482 * avoid in kernel buffers for user space callers
483 */
484
Stefan Richterafd65462007-03-05 03:06:23 +0100485/**
486 * hpsb_read - generic read function
487 *
488 * Recognizes the local node ID and act accordingly. Automatically uses a
489 * quadlet read request if @length == 4 and and a block read request otherwise.
490 * It does not yet support lengths that are not a multiple of 4.
491 *
492 * You must explicitly specifiy the @generation for which the node ID is valid,
493 * to avoid sending packets to the wrong nodes when we race with a bus reset.
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500496 u64 addr, quadlet_t * buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500498 struct hpsb_packet *packet;
499 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500501 if (length == 0)
502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 packet = hpsb_make_readpacket(host, node, addr, length);
505
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500506 if (!packet) {
507 return -ENOMEM;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500511 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (retval < 0)
513 goto hpsb_read_fail;
514
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500515 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500517 if (retval == 0) {
518 if (length == 4) {
519 *buffer = packet->header[3];
520 } else {
521 memcpy(buffer, packet->data, length);
522 }
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500525 hpsb_read_fail:
526 hpsb_free_tlabel(packet);
527 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Stefan Richterafd65462007-03-05 03:06:23 +0100532/**
533 * hpsb_write - generic write function
534 *
535 * Recognizes the local node ID and act accordingly. Automatically uses a
536 * quadlet write request if @length == 4 and and a block write request
537 * otherwise. It does not yet support lengths that are not a multiple of 4.
538 *
539 * You must explicitly specifiy the @generation for which the node ID is valid,
540 * to avoid sending packets to the wrong nodes when we race with a bus reset.
541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500543 u64 addr, quadlet_t * buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct hpsb_packet *packet;
546 int retval;
547
548 if (length == 0)
549 return -EINVAL;
550
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500551 packet = hpsb_make_writepacket(host, node, addr, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (!packet)
554 return -ENOMEM;
555
556 packet->generation = generation;
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500557 retval = hpsb_send_packet_and_wait(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (retval < 0)
559 goto hpsb_write_fail;
560
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500561 retval = hpsb_packet_success(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500563 hpsb_write_fail:
564 hpsb_free_tlabel(packet);
565 hpsb_free_packet(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jens-Michael Hoffmann16c333a2005-11-22 12:34:16 -0500567 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
Stefan Richter612262a2008-08-26 00:17:30 +0200569
570int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
571 u64 addr, int extcode, quadlet_t *data, quadlet_t arg)
572{
573 struct hpsb_packet *packet;
574 int retval = 0;
575
Stefan Richter612262a2008-08-26 00:17:30 +0200576 packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
577 if (!packet)
578 return -ENOMEM;
579
580 packet->generation = generation;
581 retval = hpsb_send_packet_and_wait(packet);
582 if (retval < 0)
583 goto hpsb_lock_fail;
584
585 retval = hpsb_packet_success(packet);
586
587 if (retval == 0)
588 *data = packet->data[0];
589
590hpsb_lock_fail:
591 hpsb_free_tlabel(packet);
592 hpsb_free_packet(packet);
593
594 return retval;
595}