Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Stefan Richter | 9543a93 | 2007-04-10 02:39:07 +0200 | [diff] [blame] | 13 | #include <linux/compiler.h> |
Stefan Richter | 7fb9add | 2007-03-11 22:49:05 +0100 | [diff] [blame] | 14 | #include <linux/hardirq.h> |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
Stefan Richter | 9543a93 | 2007-04-10 02:39:07 +0200 | [diff] [blame] | 16 | #include <linux/string.h> |
Torsten Kaiser | 3a23a81 | 2007-04-09 21:03:15 +0200 | [diff] [blame] | 17 | #include <linux/sched.h> /* because linux/wait.h is broken if CONFIG_SMP=n */ |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 18 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Stefan Richter | d83e7d8 | 2006-07-03 12:02:31 -0400 | [diff] [blame] | 20 | #include <asm/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/errno.h> |
Stefan Richter | 9543a93 | 2007-04-10 02:39:07 +0200 | [diff] [blame] | 22 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "ieee1394.h" |
| 25 | #include "ieee1394_types.h" |
| 26 | #include "hosts.h" |
| 27 | #include "ieee1394_core.h" |
Adrian Bunk | e27d301 | 2005-11-19 21:23:48 -0500 | [diff] [blame] | 28 | #include "ieee1394_transactions.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 37 | #ifndef HPSB_DEBUG_TLABELS |
| 38 | static |
| 39 | #endif |
Milind Arun Choudhary | df18ce8 | 2007-04-11 23:24:34 +0530 | [diff] [blame] | 40 | DEFINE_SPINLOCK(hpsb_tlabel_lock); |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 41 | |
| 42 | static DECLARE_WAIT_QUEUE_HEAD(tlabel_wq); |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static void fill_async_readquad(struct hpsb_packet *packet, u64 addr) |
| 45 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 46 | PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ); |
| 47 | packet->header_size = 12; |
| 48 | packet->data_size = 0; |
| 49 | packet->expect_response = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 52 | static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, |
| 53 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 62 | static void fill_async_writequad(struct hpsb_packet *packet, u64 addr, |
| 63 | quadlet_t data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 72 | static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, |
| 73 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 83 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) |
| 93 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 94 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static 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 Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 107 | | (TCODE_STREAM_DATA << 4) | sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | packet->header_size = 4; |
| 110 | packet->data_size = length; |
| 111 | packet->type = hpsb_async; |
| 112 | packet->tcode = TCODE_ISO_DATA; |
| 113 | } |
| 114 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 115 | /* same as hpsb_get_tlabel, except that it returns immediately */ |
| 116 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /** |
| 148 | * hpsb_get_tlabel - allocate a transaction label |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 149 | * @packet: the packet whose tlabel and tl_pool we set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * |
| 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 Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 163 | * tlabel or a signal is received. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | */ |
| 165 | int hpsb_get_tlabel(struct hpsb_packet *packet) |
| 166 | { |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 167 | if (irqs_disabled() || in_atomic()) |
| 168 | return hpsb_get_tlabel_atomic(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 170 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * hpsb_free_tlabel - free an allocated transaction label |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 181 | * @packet: packet whose tlabel and tl_pool needs to be cleared |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * |
| 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 | */ |
| 190 | void hpsb_free_tlabel(struct hpsb_packet *packet) |
| 191 | { |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 192 | unsigned long flags, *tp; |
| 193 | int tlabel, n = NODEID_TO_NODE(packet->node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Ben Collins | eaf8845 | 2006-06-12 18:12:49 -0400 | [diff] [blame] | 195 | if (unlikely(n == ALL_NODES)) |
| 196 | return; |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 197 | tp = packet->host->tl_pool[n].map; |
| 198 | tlabel = packet->tlabel; |
| 199 | BUG_ON(tlabel > 63 || tlabel < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 201 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 205 | wake_up_interruptible(&tlabel_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 208 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | int hpsb_packet_success(struct hpsb_packet *packet) |
| 218 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 219 | 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 | } |
Stefan Richter | d83e7d8 | 2006-07-03 12:02:31 -0400 | [diff] [blame] | 238 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 240 | case ACK_BUSY_X: |
| 241 | case ACK_BUSY_A: |
| 242 | case ACK_BUSY_B: |
| 243 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 245 | case ACK_TYPE_ERROR: |
| 246 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 248 | case ACK_COMPLETE: |
| 249 | if (packet->tcode == TCODE_WRITEQ |
| 250 | || packet->tcode == TCODE_WRITEB) { |
| 251 | return 0; |
| 252 | } else { |
| 253 | HPSB_ERR("impossible ack_complete from node %d " |
| 254 | "(tcode %d)", packet->node_id, packet->tcode); |
| 255 | return -EAGAIN; |
| 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 258 | case ACK_DATA_ERROR: |
| 259 | if (packet->tcode == TCODE_WRITEB |
| 260 | || packet->tcode == TCODE_LOCK_REQUEST) { |
| 261 | return -EAGAIN; |
| 262 | } else { |
| 263 | HPSB_ERR("impossible ack_data_error from node %d " |
| 264 | "(tcode %d)", packet->node_id, packet->tcode); |
| 265 | return -EAGAIN; |
| 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 268 | case ACK_ADDRESS_ERROR: |
| 269 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 271 | case ACK_TARDY: |
| 272 | case ACK_CONFLICT_ERROR: |
| 273 | case ACKX_NONE: |
| 274 | case ACKX_SEND_ERROR: |
| 275 | case ACKX_ABORTED: |
| 276 | case ACKX_TIMEOUT: |
| 277 | /* error while sending */ |
| 278 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 280 | default: |
| 281 | HPSB_ERR("got invalid ack %d from node %d (tcode %d)", |
| 282 | packet->ack_code, packet->node_id, packet->tcode); |
| 283 | return -EAGAIN; |
| 284 | } |
Stefan Richter | d83e7d8 | 2006-07-03 12:02:31 -0400 | [diff] [blame] | 285 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node, |
| 289 | u64 addr, size_t length) |
| 290 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 291 | struct hpsb_packet *packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | if (length == 0) |
| 294 | return NULL; |
| 295 | |
| 296 | packet = hpsb_alloc_packet(length); |
| 297 | if (!packet) |
| 298 | return NULL; |
| 299 | |
| 300 | packet->host = host; |
| 301 | packet->node_id = node; |
| 302 | |
| 303 | if (hpsb_get_tlabel(packet)) { |
| 304 | hpsb_free_packet(packet); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | if (length == 4) |
| 309 | fill_async_readquad(packet, addr); |
| 310 | else |
| 311 | fill_async_readblock(packet, addr, length); |
| 312 | |
| 313 | return packet; |
| 314 | } |
| 315 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 316 | struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node, |
| 317 | u64 addr, quadlet_t * buffer, |
| 318 | size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
| 320 | struct hpsb_packet *packet; |
| 321 | |
| 322 | if (length == 0) |
| 323 | return NULL; |
| 324 | |
| 325 | packet = hpsb_alloc_packet(length); |
| 326 | if (!packet) |
| 327 | return NULL; |
| 328 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 329 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | packet->data[length >> 2] = 0; |
| 331 | } |
| 332 | packet->host = host; |
| 333 | packet->node_id = node; |
| 334 | |
| 335 | if (hpsb_get_tlabel(packet)) { |
| 336 | hpsb_free_packet(packet); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | if (length == 4) { |
| 341 | fill_async_writequad(packet, addr, buffer ? *buffer : 0); |
| 342 | } else { |
| 343 | fill_async_writeblock(packet, addr, length); |
| 344 | if (buffer) |
| 345 | memcpy(packet->data, buffer, length); |
| 346 | } |
| 347 | |
| 348 | return packet; |
| 349 | } |
| 350 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 351 | struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer, |
| 352 | int length, int channel, int tag, |
| 353 | int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | struct hpsb_packet *packet; |
| 356 | |
| 357 | if (length == 0) |
| 358 | return NULL; |
| 359 | |
| 360 | packet = hpsb_alloc_packet(length); |
| 361 | if (!packet) |
| 362 | return NULL; |
| 363 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 364 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | packet->data[length >> 2] = 0; |
| 366 | } |
| 367 | packet->host = host; |
| 368 | |
Stefan Richter | ea9057a | 2007-04-23 21:27:13 +0200 | [diff] [blame] | 369 | /* Because it is too difficult to determine all PHY speeds and link |
| 370 | * speeds here, we use S100... */ |
| 371 | packet->speed_code = IEEE1394_SPEED_100; |
| 372 | |
| 373 | /* ...and prevent hpsb_send_packet() from overriding it. */ |
| 374 | packet->node_id = LOCAL_BUS | ALL_NODES; |
| 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (hpsb_get_tlabel(packet)) { |
| 377 | hpsb_free_packet(packet); |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | fill_async_stream_packet(packet, length, channel, tag, sync); |
| 382 | if (buffer) |
| 383 | memcpy(packet->data, buffer, length); |
| 384 | |
| 385 | return packet; |
| 386 | } |
| 387 | |
| 388 | struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 389 | u64 addr, int extcode, |
| 390 | quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
| 392 | struct hpsb_packet *p; |
| 393 | u32 length; |
| 394 | |
| 395 | p = hpsb_alloc_packet(8); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 396 | if (!p) |
| 397 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | p->host = host; |
| 400 | p->node_id = node; |
| 401 | if (hpsb_get_tlabel(p)) { |
| 402 | hpsb_free_packet(p); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | switch (extcode) { |
| 407 | case EXTCODE_FETCH_ADD: |
| 408 | case EXTCODE_LITTLE_ADD: |
| 409 | length = 4; |
| 410 | if (data) |
| 411 | p->data[0] = *data; |
| 412 | break; |
| 413 | default: |
| 414 | length = 8; |
| 415 | if (data) { |
| 416 | p->data[0] = arg; |
| 417 | p->data[1] = *data; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | fill_async_lock(p, addr, extcode, length); |
| 422 | |
| 423 | return p; |
| 424 | } |
| 425 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 426 | struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, |
| 427 | nodeid_t node, u64 addr, int extcode, |
| 428 | octlet_t * data, octlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | struct hpsb_packet *p; |
| 431 | u32 length; |
| 432 | |
| 433 | p = hpsb_alloc_packet(16); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 434 | if (!p) |
| 435 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | p->host = host; |
| 438 | p->node_id = node; |
| 439 | if (hpsb_get_tlabel(p)) { |
| 440 | hpsb_free_packet(p); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | switch (extcode) { |
| 445 | case EXTCODE_FETCH_ADD: |
| 446 | case EXTCODE_LITTLE_ADD: |
| 447 | length = 8; |
| 448 | if (data) { |
| 449 | p->data[0] = *data >> 32; |
| 450 | p->data[1] = *data & 0xffffffff; |
| 451 | } |
| 452 | break; |
| 453 | default: |
| 454 | length = 16; |
| 455 | if (data) { |
| 456 | p->data[0] = arg >> 32; |
| 457 | p->data[1] = arg & 0xffffffff; |
| 458 | p->data[2] = *data >> 32; |
| 459 | p->data[3] = *data & 0xffffffff; |
| 460 | } |
| 461 | break; |
| 462 | } |
| 463 | fill_async_lock(p, addr, extcode, length); |
| 464 | |
| 465 | return p; |
| 466 | } |
| 467 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 468 | struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 470 | struct hpsb_packet *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 472 | p = hpsb_alloc_packet(0); |
| 473 | if (!p) |
| 474 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 476 | p->host = host; |
| 477 | fill_phy_packet(p, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 479 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* |
| 483 | * FIXME - these functions should probably read from / write to user space to |
| 484 | * avoid in kernel buffers for user space callers |
| 485 | */ |
| 486 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 487 | /** |
| 488 | * hpsb_read - generic read function |
| 489 | * |
| 490 | * Recognizes the local node ID and act accordingly. Automatically uses a |
| 491 | * quadlet read request if @length == 4 and and a block read request otherwise. |
| 492 | * It does not yet support lengths that are not a multiple of 4. |
| 493 | * |
| 494 | * You must explicitly specifiy the @generation for which the node ID is valid, |
| 495 | * to avoid sending packets to the wrong nodes when we race with a bus reset. |
| 496 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 498 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 500 | struct hpsb_packet *packet; |
| 501 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 503 | if (length == 0) |
| 504 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 506 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | packet = hpsb_make_readpacket(host, node, addr, length); |
| 509 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 510 | if (!packet) { |
| 511 | return -ENOMEM; |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 515 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (retval < 0) |
| 517 | goto hpsb_read_fail; |
| 518 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 519 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 521 | if (retval == 0) { |
| 522 | if (length == 4) { |
| 523 | *buffer = packet->header[3]; |
| 524 | } else { |
| 525 | memcpy(buffer, packet->data, length); |
| 526 | } |
| 527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 529 | hpsb_read_fail: |
| 530 | hpsb_free_tlabel(packet); |
| 531 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 533 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 536 | /** |
| 537 | * hpsb_write - generic write function |
| 538 | * |
| 539 | * Recognizes the local node ID and act accordingly. Automatically uses a |
| 540 | * quadlet write request if @length == 4 and and a block write request |
| 541 | * otherwise. It does not yet support lengths that are not a multiple of 4. |
| 542 | * |
| 543 | * You must explicitly specifiy the @generation for which the node ID is valid, |
| 544 | * to avoid sending packets to the wrong nodes when we race with a bus reset. |
| 545 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 547 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | { |
| 549 | struct hpsb_packet *packet; |
| 550 | int retval; |
| 551 | |
| 552 | if (length == 0) |
| 553 | return -EINVAL; |
| 554 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 555 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 557 | packet = hpsb_make_writepacket(host, node, addr, buffer, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | if (!packet) |
| 560 | return -ENOMEM; |
| 561 | |
| 562 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 563 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (retval < 0) |
| 565 | goto hpsb_write_fail; |
| 566 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 567 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 569 | hpsb_write_fail: |
| 570 | hpsb_free_tlabel(packet); |
| 571 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 573 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 576 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 579 | u64 addr, int extcode, quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 581 | struct hpsb_packet *packet; |
| 582 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 584 | BUG_ON(in_interrupt()); // We can't be called in an interrupt, yet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 587 | if (!packet) |
| 588 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 591 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if (retval < 0) |
| 593 | goto hpsb_lock_fail; |
| 594 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 595 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 597 | if (retval == 0) { |
| 598 | *data = packet->data[0]; |
| 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 601 | hpsb_lock_fail: |
| 602 | hpsb_free_tlabel(packet); |
| 603 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 605 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 609 | quadlet_t * buffer, size_t length, u32 specifier_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | unsigned int version) |
| 611 | { |
| 612 | struct hpsb_packet *packet; |
| 613 | int retval = 0; |
| 614 | u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8; |
| 615 | u8 specifier_id_lo = specifier_id & 0xff; |
| 616 | |
| 617 | HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length); |
| 618 | |
| 619 | length += 8; |
| 620 | |
| 621 | packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0); |
| 622 | if (!packet) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 626 | packet->data[1] = |
| 627 | cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | memcpy(&(packet->data[2]), buffer, length - 8); |
| 630 | |
| 631 | packet->generation = generation; |
| 632 | |
| 633 | packet->no_waiter = 1; |
| 634 | |
| 635 | retval = hpsb_send_packet(packet); |
| 636 | if (retval < 0) |
| 637 | hpsb_free_packet(packet); |
| 638 | |
| 639 | return retval; |
| 640 | } |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 641 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 642 | #endif /* 0 */ |