Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 | 12 | #include <linux/bitops.h> |
9543a931 | 13 | #include <linux/compiler.h> |
7fb9addb | 14 | #include <linux/hardirq.h> |
9951903e | 15 | #include <linux/spinlock.h> |
9543a931 | 16 | #include <linux/string.h> |
3a23a81e | 17 | #include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */ |
9951903e | 18 | #include <linux/wait.h> |
1da177e4 | 19 | |
d83e7d8e | 20 | #include <asm/bug.h> |
1da177e4 | 21 | #include <asm/errno.h> |
9543a931 | 22 | #include <asm/system.h> |
1da177e4 LT |
23 | |
24 | #include "ieee1394.h" | |
25 | #include "ieee1394_types.h" | |
26 | #include "hosts.h" | |
27 | #include "ieee1394_core.h" | |
e27d3014 | 28 | #include "ieee1394_transactions.h" |
1da177e4 | 29 | |
1da177e4 LT |
30 | #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 | ||
9951903e SR |
37 | #ifndef HPSB_DEBUG_TLABELS |
38 | static | |
39 | #endif | |
40 | spinlock_t hpsb_tlabel_lock = SPIN_LOCK_UNLOCKED; | |
41 | ||
42 | static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq); | |
43 | ||
1da177e4 LT |
44 | static void fill_async_readquad(struct hpsb_packet *packet, u64 addr) |
45 | { | |
16c333a3 JMH |
46 | PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ); |
47 | packet->header_size = 12; | |
48 | packet->data_size = 0; | |
49 | packet->expect_response = 1; | |
1da177e4 LT |
50 | } |
51 | ||
16c333a3 JMH |
52 | static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, |
53 | int length) | |
1da177e4 | 54 | { |
16c333a3 JMH |
55 | 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; | |
1da177e4 LT |
60 | } |
61 | ||
16c333a3 JMH |
62 | static void fill_async_writequad(struct hpsb_packet *packet, u64 addr, |
63 | quadlet_t data) | |
1da177e4 | 64 | { |
16c333a3 JMH |
65 | 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; | |
1da177e4 LT |
70 | } |
71 | ||
16c333a3 JMH |
72 | static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, |
73 | int length) | |
1da177e4 | 74 | { |
16c333a3 JMH |
75 | 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); | |
1da177e4 LT |
80 | } |
81 | ||
82 | static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode, | |
16c333a3 | 83 | int length) |
1da177e4 | 84 | { |
16c333a3 JMH |
85 | 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; | |
1da177e4 LT |
90 | } |
91 | ||
92 | static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel, | |
16c333a3 | 93 | int tag, int sync) |
1da177e4 | 94 | { |
16c333a3 JMH |
95 | packet->header[0] = (length << 16) | (tag << 14) | (channel << 8) |
96 | | (TCODE_ISO_DATA << 4) | sync; | |
1da177e4 | 97 | |
16c333a3 JMH |
98 | packet->header_size = 4; |
99 | packet->data_size = length; | |
100 | packet->type = hpsb_iso; | |
101 | packet->tcode = TCODE_ISO_DATA; | |
1da177e4 LT |
102 | } |
103 | ||
104 | static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) | |
105 | { | |
16c333a3 JMH |
106 | packet->header[0] = data; |
107 | packet->header[1] = ~data; | |
108 | packet->header_size = 8; | |
109 | packet->data_size = 0; | |
110 | packet->expect_response = 0; | |
111 | packet->type = hpsb_raw; /* No CRC added */ | |
112 | packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */ | |
1da177e4 LT |
113 | } |
114 | ||
115 | static void fill_async_stream_packet(struct hpsb_packet *packet, int length, | |
116 | int channel, int tag, int sync) | |
117 | { | |
118 | packet->header[0] = (length << 16) | (tag << 14) | (channel << 8) | |
16c333a3 | 119 | | (TCODE_STREAM_DATA << 4) | sync; |
1da177e4 LT |
120 | |
121 | packet->header_size = 4; | |
122 | packet->data_size = length; | |
123 | packet->type = hpsb_async; | |
124 | packet->tcode = TCODE_ISO_DATA; | |
125 | } | |
126 | ||
9951903e SR |
127 | /* same as hpsb_get_tlabel, except that it returns immediately */ |
128 | static int hpsb_get_tlabel_atomic(struct hpsb_packet *packet) | |
129 | { | |
130 | unsigned long flags, *tp; | |
131 | u8 *next; | |
132 | int tlabel, n = NODEID_TO_NODE(packet->node_id); | |
133 | ||
134 | /* Broadcast transactions are complete once the request has been sent. | |
135 | * Use the same transaction label for all broadcast transactions. */ | |
136 | if (unlikely(n == ALL_NODES)) { | |
137 | packet->tlabel = 0; | |
138 | return 0; | |
139 | } | |
140 | tp = packet->host->tl_pool[n].map; | |
141 | next = &packet->host->next_tl[n]; | |
142 | ||
143 | spin_lock_irqsave(&hpsb_tlabel_lock, flags); | |
144 | tlabel = find_next_zero_bit(tp, 64, *next); | |
145 | if (tlabel > 63) | |
146 | tlabel = find_first_zero_bit(tp, 64); | |
147 | if (tlabel > 63) { | |
148 | spin_unlock_irqrestore(&hpsb_tlabel_lock, flags); | |
149 | return -EAGAIN; | |
150 | } | |
151 | __set_bit(tlabel, tp); | |
152 | *next = (tlabel + 1) & 63; | |
153 | spin_unlock_irqrestore(&hpsb_tlabel_lock, flags); | |
154 | ||
155 | packet->tlabel = tlabel; | |
156 | return 0; | |
157 | } | |
158 | ||
1da177e4 LT |
159 | /** |
160 | * hpsb_get_tlabel - allocate a transaction label | |
9951903e | 161 | * @packet: the packet whose tlabel and tl_pool we set |
1da177e4 LT |
162 | * |
163 | * Every asynchronous transaction on the 1394 bus needs a transaction | |
164 | * label to match the response to the request. This label has to be | |
165 | * different from any other transaction label in an outstanding request to | |
166 | * the same node to make matching possible without ambiguity. | |
167 | * | |
168 | * There are 64 different tlabels, so an allocated tlabel has to be freed | |
169 | * with hpsb_free_tlabel() after the transaction is complete (unless it's | |
170 | * reused again for the same target node). | |
171 | * | |
172 | * Return value: Zero on success, otherwise non-zero. A non-zero return | |
173 | * generally means there are no available tlabels. If this is called out | |
174 | * of interrupt or atomic context, then it will sleep until can return a | |
9951903e | 175 | * tlabel or a signal is received. |
1da177e4 LT |
176 | */ |
177 | int hpsb_get_tlabel(struct hpsb_packet *packet) | |
178 | { | |
9951903e SR |
179 | if (irqs_disabled() || in_atomic()) |
180 | return hpsb_get_tlabel_atomic(packet); | |
181 | ||
182 | /* NB: The macro wait_event_interruptible() is called with a condition | |
183 | * argument with side effect. This is only possible because the side | |
184 | * effect does not occur until the condition became true, and | |
185 | * wait_event_interruptible() won't evaluate the condition again after | |
186 | * that. */ | |
187 | return wait_event_interruptible(tlabel_wq, | |
188 | !hpsb_get_tlabel_atomic(packet)); | |
1da177e4 LT |
189 | } |
190 | ||
191 | /** | |
192 | * hpsb_free_tlabel - free an allocated transaction label | |
9951903e | 193 | * @packet: packet whose tlabel and tl_pool needs to be cleared |
1da177e4 LT |
194 | * |
195 | * Frees the transaction label allocated with hpsb_get_tlabel(). The | |
196 | * tlabel has to be freed after the transaction is complete (i.e. response | |
197 | * was received for a split transaction or packet was sent for a unified | |
198 | * transaction). | |
199 | * | |
200 | * A tlabel must not be freed twice. | |
201 | */ | |
202 | void hpsb_free_tlabel(struct hpsb_packet *packet) | |
203 | { | |
9951903e SR |
204 | unsigned long flags, *tp; |
205 | int tlabel, n = NODEID_TO_NODE(packet->node_id); | |
1da177e4 | 206 | |
eaf88450 BC |
207 | if (unlikely(n == ALL_NODES)) |
208 | return; | |
9951903e SR |
209 | tp = packet->host->tl_pool[n].map; |
210 | tlabel = packet->tlabel; | |
211 | BUG_ON(tlabel > 63 || tlabel < 0); | |
1da177e4 | 212 | |
9951903e SR |
213 | spin_lock_irqsave(&hpsb_tlabel_lock, flags); |
214 | BUG_ON(!__test_and_clear_bit(tlabel, tp)); | |
215 | spin_unlock_irqrestore(&hpsb_tlabel_lock, flags); | |
1da177e4 | 216 | |
9951903e | 217 | wake_up_interruptible(&tlabel_wq); |
1da177e4 LT |
218 | } |
219 | ||
afd6546d SR |
220 | /** |
221 | * hpsb_packet_success - Make sense of the ack and reply codes | |
222 | * | |
223 | * Make sense of the ack and reply codes and return more convenient error codes: | |
224 | * 0 = success. -%EBUSY = node is busy, try again. -%EAGAIN = error which can | |
225 | * probably resolved by retry. -%EREMOTEIO = node suffers from an internal | |
226 | * error. -%EACCES = this transaction is not allowed on requested address. | |
227 | * -%EINVAL = invalid address at node. | |
228 | */ | |
1da177e4 LT |
229 | int hpsb_packet_success(struct hpsb_packet *packet) |
230 | { | |
16c333a3 JMH |
231 | switch (packet->ack_code) { |
232 | case ACK_PENDING: | |
233 | switch ((packet->header[1] >> 12) & 0xf) { | |
234 | case RCODE_COMPLETE: | |
235 | return 0; | |
236 | case RCODE_CONFLICT_ERROR: | |
237 | return -EAGAIN; | |
238 | case RCODE_DATA_ERROR: | |
239 | return -EREMOTEIO; | |
240 | case RCODE_TYPE_ERROR: | |
241 | return -EACCES; | |
242 | case RCODE_ADDRESS_ERROR: | |
243 | return -EINVAL; | |
244 | default: | |
245 | HPSB_ERR("received reserved rcode %d from node %d", | |
246 | (packet->header[1] >> 12) & 0xf, | |
247 | packet->node_id); | |
248 | return -EAGAIN; | |
249 | } | |
d83e7d8e | 250 | BUG(); |
16c333a3 JMH |
251 | |
252 | case ACK_BUSY_X: | |
253 | case ACK_BUSY_A: | |
254 | case ACK_BUSY_B: | |
255 | return -EBUSY; | |
256 | ||
257 | case ACK_TYPE_ERROR: | |
258 | return -EACCES; | |
259 | ||
260 | case ACK_COMPLETE: | |
261 | if (packet->tcode == TCODE_WRITEQ | |
262 | || packet->tcode == TCODE_WRITEB) { | |
263 | return 0; | |
264 | } else { | |
265 | HPSB_ERR("impossible ack_complete from node %d " | |
266 | "(tcode %d)", packet->node_id, packet->tcode); | |
267 | return -EAGAIN; | |
268 | } | |
269 | ||
270 | case ACK_DATA_ERROR: | |
271 | if (packet->tcode == TCODE_WRITEB | |
272 | || packet->tcode == TCODE_LOCK_REQUEST) { | |
273 | return -EAGAIN; | |
274 | } else { | |
275 | HPSB_ERR("impossible ack_data_error from node %d " | |
276 | "(tcode %d)", packet->node_id, packet->tcode); | |
277 | return -EAGAIN; | |
278 | } | |
279 | ||
280 | case ACK_ADDRESS_ERROR: | |
281 | return -EINVAL; | |
282 | ||
283 | case ACK_TARDY: | |
284 | case ACK_CONFLICT_ERROR: | |
285 | case ACKX_NONE: | |
286 | case ACKX_SEND_ERROR: | |
287 | case ACKX_ABORTED: | |
288 | case ACKX_TIMEOUT: | |
289 | /* error while sending */ | |
290 | return -EAGAIN; | |
291 | ||
292 | default: | |
293 | HPSB_ERR("got invalid ack %d from node %d (tcode %d)", | |
294 | packet->ack_code, packet->node_id, packet->tcode); | |
295 | return -EAGAIN; | |
296 | } | |
d83e7d8e | 297 | BUG(); |
1da177e4 LT |
298 | } |
299 | ||
300 | struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node, | |
301 | u64 addr, size_t length) | |
302 | { | |
16c333a3 | 303 | struct hpsb_packet *packet; |
1da177e4 LT |
304 | |
305 | if (length == 0) | |
306 | return NULL; | |
307 | ||
308 | packet = hpsb_alloc_packet(length); | |
309 | if (!packet) | |
310 | return NULL; | |
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_readquad(packet, addr); | |
322 | else | |
323 | fill_async_readblock(packet, addr, length); | |
324 | ||
325 | return packet; | |
326 | } | |
327 | ||
16c333a3 JMH |
328 | struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node, |
329 | u64 addr, quadlet_t * buffer, | |
330 | size_t length) | |
1da177e4 LT |
331 | { |
332 | struct hpsb_packet *packet; | |
333 | ||
334 | if (length == 0) | |
335 | return NULL; | |
336 | ||
337 | packet = hpsb_alloc_packet(length); | |
338 | if (!packet) | |
339 | return NULL; | |
340 | ||
16c333a3 | 341 | if (length % 4) { /* zero padding bytes */ |
1da177e4 LT |
342 | packet->data[length >> 2] = 0; |
343 | } | |
344 | packet->host = host; | |
345 | packet->node_id = node; | |
346 | ||
347 | if (hpsb_get_tlabel(packet)) { | |
348 | hpsb_free_packet(packet); | |
349 | return NULL; | |
350 | } | |
351 | ||
352 | if (length == 4) { | |
353 | fill_async_writequad(packet, addr, buffer ? *buffer : 0); | |
354 | } else { | |
355 | fill_async_writeblock(packet, addr, length); | |
356 | if (buffer) | |
357 | memcpy(packet->data, buffer, length); | |
358 | } | |
359 | ||
360 | return packet; | |
361 | } | |
362 | ||
16c333a3 JMH |
363 | struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer, |
364 | int length, int channel, int tag, | |
365 | int sync) | |
1da177e4 LT |
366 | { |
367 | struct hpsb_packet *packet; | |
368 | ||
369 | if (length == 0) | |
370 | return NULL; | |
371 | ||
372 | packet = hpsb_alloc_packet(length); | |
373 | if (!packet) | |
374 | return NULL; | |
375 | ||
16c333a3 | 376 | if (length % 4) { /* zero padding bytes */ |
1da177e4 LT |
377 | packet->data[length >> 2] = 0; |
378 | } | |
379 | packet->host = host; | |
380 | ||
381 | if (hpsb_get_tlabel(packet)) { | |
382 | hpsb_free_packet(packet); | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | fill_async_stream_packet(packet, length, channel, tag, sync); | |
387 | if (buffer) | |
388 | memcpy(packet->data, buffer, length); | |
389 | ||
390 | return packet; | |
391 | } | |
392 | ||
393 | struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node, | |
16c333a3 JMH |
394 | u64 addr, int extcode, |
395 | quadlet_t * data, quadlet_t arg) | |
1da177e4 LT |
396 | { |
397 | struct hpsb_packet *p; | |
398 | u32 length; | |
399 | ||
400 | p = hpsb_alloc_packet(8); | |
16c333a3 JMH |
401 | if (!p) |
402 | return NULL; | |
1da177e4 LT |
403 | |
404 | p->host = host; | |
405 | p->node_id = node; | |
406 | if (hpsb_get_tlabel(p)) { | |
407 | hpsb_free_packet(p); | |
408 | return NULL; | |
409 | } | |
410 | ||
411 | switch (extcode) { | |
412 | case EXTCODE_FETCH_ADD: | |
413 | case EXTCODE_LITTLE_ADD: | |
414 | length = 4; | |
415 | if (data) | |
416 | p->data[0] = *data; | |
417 | break; | |
418 | default: | |
419 | length = 8; | |
420 | if (data) { | |
421 | p->data[0] = arg; | |
422 | p->data[1] = *data; | |
423 | } | |
424 | break; | |
425 | } | |
426 | fill_async_lock(p, addr, extcode, length); | |
427 | ||
428 | return p; | |
429 | } | |
430 | ||
16c333a3 JMH |
431 | struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, |
432 | nodeid_t node, u64 addr, int extcode, | |
433 | octlet_t * data, octlet_t arg) | |
1da177e4 LT |
434 | { |
435 | struct hpsb_packet *p; | |
436 | u32 length; | |
437 | ||
438 | p = hpsb_alloc_packet(16); | |
16c333a3 JMH |
439 | if (!p) |
440 | return NULL; | |
1da177e4 LT |
441 | |
442 | p->host = host; | |
443 | p->node_id = node; | |
444 | if (hpsb_get_tlabel(p)) { | |
445 | hpsb_free_packet(p); | |
446 | return NULL; | |
447 | } | |
448 | ||
449 | switch (extcode) { | |
450 | case EXTCODE_FETCH_ADD: | |
451 | case EXTCODE_LITTLE_ADD: | |
452 | length = 8; | |
453 | if (data) { | |
454 | p->data[0] = *data >> 32; | |
455 | p->data[1] = *data & 0xffffffff; | |
456 | } | |
457 | break; | |
458 | default: | |
459 | length = 16; | |
460 | if (data) { | |
461 | p->data[0] = arg >> 32; | |
462 | p->data[1] = arg & 0xffffffff; | |
463 | p->data[2] = *data >> 32; | |
464 | p->data[3] = *data & 0xffffffff; | |
465 | } | |
466 | break; | |
467 | } | |
468 | fill_async_lock(p, addr, extcode, length); | |
469 | ||
470 | return p; | |
471 | } | |
472 | ||
16c333a3 | 473 | struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data) |
1da177e4 | 474 | { |
16c333a3 | 475 | struct hpsb_packet *p; |
1da177e4 | 476 | |
16c333a3 JMH |
477 | p = hpsb_alloc_packet(0); |
478 | if (!p) | |
479 | return NULL; | |
1da177e4 | 480 | |
16c333a3 JMH |
481 | p->host = host; |
482 | fill_phy_packet(p, data); | |
1da177e4 | 483 | |
16c333a3 | 484 | return p; |
1da177e4 LT |
485 | } |
486 | ||
487 | struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host, | |
488 | int length, int channel, | |
489 | int tag, int sync) | |
490 | { | |
491 | struct hpsb_packet *p; | |
492 | ||
493 | p = hpsb_alloc_packet(length); | |
16c333a3 JMH |
494 | if (!p) |
495 | return NULL; | |
1da177e4 LT |
496 | |
497 | p->host = host; | |
498 | fill_iso_packet(p, length, channel, tag, sync); | |
499 | ||
500 | p->generation = get_hpsb_generation(host); | |
501 | ||
502 | return p; | |
503 | } | |
504 | ||
505 | /* | |
506 | * FIXME - these functions should probably read from / write to user space to | |
507 | * avoid in kernel buffers for user space callers | |
508 | */ | |
509 | ||
afd6546d SR |
510 | /** |
511 | * hpsb_read - generic read function | |
512 | * | |
513 | * Recognizes the local node ID and act accordingly. Automatically uses a | |
514 | * quadlet read request if @length == 4 and and a block read request otherwise. | |
515 | * It does not yet support lengths that are not a multiple of 4. | |
516 | * | |
517 | * You must explicitly specifiy the @generation for which the node ID is valid, | |
518 | * to avoid sending packets to the wrong nodes when we race with a bus reset. | |
519 | */ | |
1da177e4 | 520 | int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation, |
16c333a3 | 521 | u64 addr, quadlet_t * buffer, size_t length) |
1da177e4 | 522 | { |
16c333a3 JMH |
523 | struct hpsb_packet *packet; |
524 | int retval = 0; | |
1da177e4 | 525 | |
16c333a3 JMH |
526 | if (length == 0) |
527 | return -EINVAL; | |
1da177e4 | 528 | |
16c333a3 | 529 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
1da177e4 LT |
530 | |
531 | packet = hpsb_make_readpacket(host, node, addr, length); | |
532 | ||
16c333a3 JMH |
533 | if (!packet) { |
534 | return -ENOMEM; | |
535 | } | |
1da177e4 LT |
536 | |
537 | packet->generation = generation; | |
16c333a3 | 538 | retval = hpsb_send_packet_and_wait(packet); |
1da177e4 LT |
539 | if (retval < 0) |
540 | goto hpsb_read_fail; | |
541 | ||
16c333a3 | 542 | retval = hpsb_packet_success(packet); |
1da177e4 | 543 | |
16c333a3 JMH |
544 | if (retval == 0) { |
545 | if (length == 4) { | |
546 | *buffer = packet->header[3]; | |
547 | } else { | |
548 | memcpy(buffer, packet->data, length); | |
549 | } | |
550 | } | |
1da177e4 | 551 | |
16c333a3 JMH |
552 | hpsb_read_fail: |
553 | hpsb_free_tlabel(packet); | |
554 | hpsb_free_packet(packet); | |
1da177e4 | 555 | |
16c333a3 | 556 | return retval; |
1da177e4 LT |
557 | } |
558 | ||
afd6546d SR |
559 | /** |
560 | * hpsb_write - generic write function | |
561 | * | |
562 | * Recognizes the local node ID and act accordingly. Automatically uses a | |
563 | * quadlet write request if @length == 4 and and a block write request | |
564 | * otherwise. It does not yet support lengths that are not a multiple of 4. | |
565 | * | |
566 | * You must explicitly specifiy the @generation for which the node ID is valid, | |
567 | * to avoid sending packets to the wrong nodes when we race with a bus reset. | |
568 | */ | |
1da177e4 | 569 | int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation, |
16c333a3 | 570 | u64 addr, quadlet_t * buffer, size_t length) |
1da177e4 LT |
571 | { |
572 | struct hpsb_packet *packet; | |
573 | int retval; | |
574 | ||
575 | if (length == 0) | |
576 | return -EINVAL; | |
577 | ||
16c333a3 | 578 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
1da177e4 | 579 | |
16c333a3 | 580 | packet = hpsb_make_writepacket(host, node, addr, buffer, length); |
1da177e4 LT |
581 | |
582 | if (!packet) | |
583 | return -ENOMEM; | |
584 | ||
585 | packet->generation = generation; | |
16c333a3 | 586 | retval = hpsb_send_packet_and_wait(packet); |
1da177e4 LT |
587 | if (retval < 0) |
588 | goto hpsb_write_fail; | |
589 | ||
16c333a3 | 590 | retval = hpsb_packet_success(packet); |
1da177e4 | 591 | |
16c333a3 JMH |
592 | hpsb_write_fail: |
593 | hpsb_free_tlabel(packet); | |
594 | hpsb_free_packet(packet); | |
1da177e4 | 595 | |
16c333a3 | 596 | return retval; |
1da177e4 LT |
597 | } |
598 | ||
9ac485dc | 599 | #if 0 |
1da177e4 LT |
600 | |
601 | int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation, | |
16c333a3 | 602 | u64 addr, int extcode, quadlet_t * data, quadlet_t arg) |
1da177e4 | 603 | { |
16c333a3 JMH |
604 | struct hpsb_packet *packet; |
605 | int retval = 0; | |
1da177e4 | 606 | |
16c333a3 | 607 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
1da177e4 LT |
608 | |
609 | packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg); | |
16c333a3 JMH |
610 | if (!packet) |
611 | return -ENOMEM; | |
1da177e4 LT |
612 | |
613 | packet->generation = generation; | |
16c333a3 | 614 | retval = hpsb_send_packet_and_wait(packet); |
1da177e4 LT |
615 | if (retval < 0) |
616 | goto hpsb_lock_fail; | |
617 | ||
16c333a3 | 618 | retval = hpsb_packet_success(packet); |
1da177e4 | 619 | |
16c333a3 JMH |
620 | if (retval == 0) { |
621 | *data = packet->data[0]; | |
622 | } | |
1da177e4 | 623 | |
16c333a3 JMH |
624 | hpsb_lock_fail: |
625 | hpsb_free_tlabel(packet); | |
626 | hpsb_free_packet(packet); | |
1da177e4 | 627 | |
16c333a3 | 628 | return retval; |
1da177e4 LT |
629 | } |
630 | ||
1da177e4 | 631 | int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation, |
16c333a3 | 632 | quadlet_t * buffer, size_t length, u32 specifier_id, |
1da177e4 LT |
633 | unsigned int version) |
634 | { | |
635 | struct hpsb_packet *packet; | |
636 | int retval = 0; | |
637 | u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8; | |
638 | u8 specifier_id_lo = specifier_id & 0xff; | |
639 | ||
640 | HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length); | |
641 | ||
642 | length += 8; | |
643 | ||
644 | packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0); | |
645 | if (!packet) | |
646 | return -ENOMEM; | |
647 | ||
648 | packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi); | |
16c333a3 JMH |
649 | packet->data[1] = |
650 | cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff)); | |
1da177e4 LT |
651 | |
652 | memcpy(&(packet->data[2]), buffer, length - 8); | |
653 | ||
654 | packet->generation = generation; | |
655 | ||
656 | packet->no_waiter = 1; | |
657 | ||
658 | retval = hpsb_send_packet(packet); | |
659 | if (retval < 0) | |
660 | hpsb_free_packet(packet); | |
661 | ||
662 | return retval; | |
663 | } | |
9ac485dc | 664 | |
16c333a3 | 665 | #endif /* 0 */ |