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 | |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/smp_lock.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | |
| 17 | #include <asm/errno.h> |
| 18 | |
| 19 | #include "ieee1394.h" |
| 20 | #include "ieee1394_types.h" |
| 21 | #include "hosts.h" |
| 22 | #include "ieee1394_core.h" |
| 23 | #include "highlevel.h" |
| 24 | #include "nodemgr.h" |
Adrian Bunk | e27d301 | 2005-11-19 21:23:48 -0500 | [diff] [blame] | 25 | #include "ieee1394_transactions.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #define PREP_ASYNC_HEAD_ADDRESS(tc) \ |
| 28 | packet->tcode = tc; \ |
| 29 | packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \ |
| 30 | | (1 << 8) | (tc << 4); \ |
| 31 | packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \ |
| 32 | packet->header[2] = addr & 0xffffffff |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | static void fill_async_readquad(struct hpsb_packet *packet, u64 addr) |
| 35 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 36 | PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ); |
| 37 | packet->header_size = 12; |
| 38 | packet->data_size = 0; |
| 39 | packet->expect_response = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 42 | static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, |
| 43 | int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 45 | PREP_ASYNC_HEAD_ADDRESS(TCODE_READB); |
| 46 | packet->header[3] = length << 16; |
| 47 | packet->header_size = 16; |
| 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_writequad(struct hpsb_packet *packet, u64 addr, |
| 53 | quadlet_t data) |
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_WRITEQ); |
| 56 | packet->header[3] = data; |
| 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_writeblock(struct hpsb_packet *packet, u64 addr, |
| 63 | int length) |
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_WRITEB); |
| 66 | packet->header[3] = length << 16; |
| 67 | packet->header_size = 16; |
| 68 | packet->expect_response = 1; |
| 69 | packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | 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] | 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_LOCK_REQUEST); |
| 76 | packet->header[3] = (length << 16) | extcode; |
| 77 | packet->header_size = 16; |
| 78 | packet->data_size = length; |
| 79 | packet->expect_response = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel, |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 83 | int tag, int sync) |
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 | packet->header[0] = (length << 16) | (tag << 14) | (channel << 8) |
| 86 | | (TCODE_ISO_DATA << 4) | sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 88 | packet->header_size = 4; |
| 89 | packet->data_size = length; |
| 90 | packet->type = hpsb_iso; |
| 91 | packet->tcode = TCODE_ISO_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) |
| 95 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 96 | packet->header[0] = data; |
| 97 | packet->header[1] = ~data; |
| 98 | packet->header_size = 8; |
| 99 | packet->data_size = 0; |
| 100 | packet->expect_response = 0; |
| 101 | packet->type = hpsb_raw; /* No CRC added */ |
| 102 | packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void fill_async_stream_packet(struct hpsb_packet *packet, int length, |
| 106 | int channel, int tag, int sync) |
| 107 | { |
| 108 | packet->header[0] = (length << 16) | (tag << 14) | (channel << 8) |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 109 | | (TCODE_STREAM_DATA << 4) | sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | packet->header_size = 4; |
| 112 | packet->data_size = length; |
| 113 | packet->type = hpsb_async; |
| 114 | packet->tcode = TCODE_ISO_DATA; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * hpsb_get_tlabel - allocate a transaction label |
| 119 | * @packet: the packet who's tlabel/tpool we set |
| 120 | * |
| 121 | * Every asynchronous transaction on the 1394 bus needs a transaction |
| 122 | * label to match the response to the request. This label has to be |
| 123 | * different from any other transaction label in an outstanding request to |
| 124 | * the same node to make matching possible without ambiguity. |
| 125 | * |
| 126 | * There are 64 different tlabels, so an allocated tlabel has to be freed |
| 127 | * with hpsb_free_tlabel() after the transaction is complete (unless it's |
| 128 | * reused again for the same target node). |
| 129 | * |
| 130 | * Return value: Zero on success, otherwise non-zero. A non-zero return |
| 131 | * generally means there are no available tlabels. If this is called out |
| 132 | * of interrupt or atomic context, then it will sleep until can return a |
| 133 | * tlabel. |
| 134 | */ |
| 135 | int hpsb_get_tlabel(struct hpsb_packet *packet) |
| 136 | { |
| 137 | unsigned long flags; |
| 138 | struct hpsb_tlabel_pool *tp; |
| 139 | |
| 140 | tp = &packet->host->tpool[packet->node_id & NODE_MASK]; |
| 141 | |
| 142 | if (irqs_disabled() || in_atomic()) { |
| 143 | if (down_trylock(&tp->count)) |
| 144 | return 1; |
| 145 | } else { |
| 146 | down(&tp->count); |
| 147 | } |
| 148 | |
| 149 | spin_lock_irqsave(&tp->lock, flags); |
| 150 | |
| 151 | packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next); |
| 152 | if (packet->tlabel > 63) |
| 153 | packet->tlabel = find_first_zero_bit(tp->pool, 64); |
| 154 | tp->next = (packet->tlabel + 1) % 64; |
| 155 | /* Should _never_ happen */ |
| 156 | BUG_ON(test_and_set_bit(packet->tlabel, tp->pool)); |
| 157 | tp->allocations++; |
| 158 | spin_unlock_irqrestore(&tp->lock, flags); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * hpsb_free_tlabel - free an allocated transaction label |
| 165 | * @packet: packet whos tlabel/tpool needs to be cleared |
| 166 | * |
| 167 | * Frees the transaction label allocated with hpsb_get_tlabel(). The |
| 168 | * tlabel has to be freed after the transaction is complete (i.e. response |
| 169 | * was received for a split transaction or packet was sent for a unified |
| 170 | * transaction). |
| 171 | * |
| 172 | * A tlabel must not be freed twice. |
| 173 | */ |
| 174 | void hpsb_free_tlabel(struct hpsb_packet *packet) |
| 175 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 176 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | struct hpsb_tlabel_pool *tp; |
| 178 | |
| 179 | tp = &packet->host->tpool[packet->node_id & NODE_MASK]; |
| 180 | |
| 181 | BUG_ON(packet->tlabel > 63 || packet->tlabel < 0); |
| 182 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 183 | spin_lock_irqsave(&tp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool)); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 185 | spin_unlock_irqrestore(&tp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | up(&tp->count); |
| 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int hpsb_packet_success(struct hpsb_packet *packet) |
| 191 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 192 | switch (packet->ack_code) { |
| 193 | case ACK_PENDING: |
| 194 | switch ((packet->header[1] >> 12) & 0xf) { |
| 195 | case RCODE_COMPLETE: |
| 196 | return 0; |
| 197 | case RCODE_CONFLICT_ERROR: |
| 198 | return -EAGAIN; |
| 199 | case RCODE_DATA_ERROR: |
| 200 | return -EREMOTEIO; |
| 201 | case RCODE_TYPE_ERROR: |
| 202 | return -EACCES; |
| 203 | case RCODE_ADDRESS_ERROR: |
| 204 | return -EINVAL; |
| 205 | default: |
| 206 | HPSB_ERR("received reserved rcode %d from node %d", |
| 207 | (packet->header[1] >> 12) & 0xf, |
| 208 | packet->node_id); |
| 209 | return -EAGAIN; |
| 210 | } |
| 211 | HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 213 | case ACK_BUSY_X: |
| 214 | case ACK_BUSY_A: |
| 215 | case ACK_BUSY_B: |
| 216 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 218 | case ACK_TYPE_ERROR: |
| 219 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 221 | case ACK_COMPLETE: |
| 222 | if (packet->tcode == TCODE_WRITEQ |
| 223 | || packet->tcode == TCODE_WRITEB) { |
| 224 | return 0; |
| 225 | } else { |
| 226 | HPSB_ERR("impossible ack_complete from node %d " |
| 227 | "(tcode %d)", packet->node_id, packet->tcode); |
| 228 | return -EAGAIN; |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 231 | case ACK_DATA_ERROR: |
| 232 | if (packet->tcode == TCODE_WRITEB |
| 233 | || packet->tcode == TCODE_LOCK_REQUEST) { |
| 234 | return -EAGAIN; |
| 235 | } else { |
| 236 | HPSB_ERR("impossible ack_data_error from node %d " |
| 237 | "(tcode %d)", packet->node_id, packet->tcode); |
| 238 | return -EAGAIN; |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 241 | case ACK_ADDRESS_ERROR: |
| 242 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 244 | case ACK_TARDY: |
| 245 | case ACK_CONFLICT_ERROR: |
| 246 | case ACKX_NONE: |
| 247 | case ACKX_SEND_ERROR: |
| 248 | case ACKX_ABORTED: |
| 249 | case ACKX_TIMEOUT: |
| 250 | /* error while sending */ |
| 251 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 253 | default: |
| 254 | HPSB_ERR("got invalid ack %d from node %d (tcode %d)", |
| 255 | packet->ack_code, packet->node_id, packet->tcode); |
| 256 | return -EAGAIN; |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 259 | HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node, |
| 263 | u64 addr, size_t length) |
| 264 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 265 | struct hpsb_packet *packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | if (length == 0) |
| 268 | return NULL; |
| 269 | |
| 270 | packet = hpsb_alloc_packet(length); |
| 271 | if (!packet) |
| 272 | return NULL; |
| 273 | |
| 274 | packet->host = host; |
| 275 | packet->node_id = node; |
| 276 | |
| 277 | if (hpsb_get_tlabel(packet)) { |
| 278 | hpsb_free_packet(packet); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | if (length == 4) |
| 283 | fill_async_readquad(packet, addr); |
| 284 | else |
| 285 | fill_async_readblock(packet, addr, length); |
| 286 | |
| 287 | return packet; |
| 288 | } |
| 289 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 290 | struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node, |
| 291 | u64 addr, quadlet_t * buffer, |
| 292 | size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | struct hpsb_packet *packet; |
| 295 | |
| 296 | if (length == 0) |
| 297 | return NULL; |
| 298 | |
| 299 | packet = hpsb_alloc_packet(length); |
| 300 | if (!packet) |
| 301 | return NULL; |
| 302 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 303 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | packet->data[length >> 2] = 0; |
| 305 | } |
| 306 | packet->host = host; |
| 307 | packet->node_id = node; |
| 308 | |
| 309 | if (hpsb_get_tlabel(packet)) { |
| 310 | hpsb_free_packet(packet); |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | if (length == 4) { |
| 315 | fill_async_writequad(packet, addr, buffer ? *buffer : 0); |
| 316 | } else { |
| 317 | fill_async_writeblock(packet, addr, length); |
| 318 | if (buffer) |
| 319 | memcpy(packet->data, buffer, length); |
| 320 | } |
| 321 | |
| 322 | return packet; |
| 323 | } |
| 324 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 325 | struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer, |
| 326 | int length, int channel, int tag, |
| 327 | int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | struct hpsb_packet *packet; |
| 330 | |
| 331 | if (length == 0) |
| 332 | return NULL; |
| 333 | |
| 334 | packet = hpsb_alloc_packet(length); |
| 335 | if (!packet) |
| 336 | return NULL; |
| 337 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 338 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | packet->data[length >> 2] = 0; |
| 340 | } |
| 341 | packet->host = host; |
| 342 | |
| 343 | if (hpsb_get_tlabel(packet)) { |
| 344 | hpsb_free_packet(packet); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | fill_async_stream_packet(packet, length, channel, tag, sync); |
| 349 | if (buffer) |
| 350 | memcpy(packet->data, buffer, length); |
| 351 | |
| 352 | return packet; |
| 353 | } |
| 354 | |
| 355 | 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] | 356 | u64 addr, int extcode, |
| 357 | quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | struct hpsb_packet *p; |
| 360 | u32 length; |
| 361 | |
| 362 | p = hpsb_alloc_packet(8); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 363 | if (!p) |
| 364 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | p->host = host; |
| 367 | p->node_id = node; |
| 368 | if (hpsb_get_tlabel(p)) { |
| 369 | hpsb_free_packet(p); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | switch (extcode) { |
| 374 | case EXTCODE_FETCH_ADD: |
| 375 | case EXTCODE_LITTLE_ADD: |
| 376 | length = 4; |
| 377 | if (data) |
| 378 | p->data[0] = *data; |
| 379 | break; |
| 380 | default: |
| 381 | length = 8; |
| 382 | if (data) { |
| 383 | p->data[0] = arg; |
| 384 | p->data[1] = *data; |
| 385 | } |
| 386 | break; |
| 387 | } |
| 388 | fill_async_lock(p, addr, extcode, length); |
| 389 | |
| 390 | return p; |
| 391 | } |
| 392 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 393 | struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, |
| 394 | nodeid_t node, u64 addr, int extcode, |
| 395 | octlet_t * data, octlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | struct hpsb_packet *p; |
| 398 | u32 length; |
| 399 | |
| 400 | p = hpsb_alloc_packet(16); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 401 | if (!p) |
| 402 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 = 8; |
| 415 | if (data) { |
| 416 | p->data[0] = *data >> 32; |
| 417 | p->data[1] = *data & 0xffffffff; |
| 418 | } |
| 419 | break; |
| 420 | default: |
| 421 | length = 16; |
| 422 | if (data) { |
| 423 | p->data[0] = arg >> 32; |
| 424 | p->data[1] = arg & 0xffffffff; |
| 425 | p->data[2] = *data >> 32; |
| 426 | p->data[3] = *data & 0xffffffff; |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | fill_async_lock(p, addr, extcode, length); |
| 431 | |
| 432 | return p; |
| 433 | } |
| 434 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 435 | 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] | 436 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 437 | struct hpsb_packet *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 439 | p = hpsb_alloc_packet(0); |
| 440 | if (!p) |
| 441 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 443 | p->host = host; |
| 444 | fill_phy_packet(p, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 446 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host, |
| 450 | int length, int channel, |
| 451 | int tag, int sync) |
| 452 | { |
| 453 | struct hpsb_packet *p; |
| 454 | |
| 455 | p = hpsb_alloc_packet(length); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 456 | if (!p) |
| 457 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | p->host = host; |
| 460 | fill_iso_packet(p, length, channel, tag, sync); |
| 461 | |
| 462 | p->generation = get_hpsb_generation(host); |
| 463 | |
| 464 | return p; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * FIXME - these functions should probably read from / write to user space to |
| 469 | * avoid in kernel buffers for user space callers |
| 470 | */ |
| 471 | |
| 472 | 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] | 473 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 475 | struct hpsb_packet *packet; |
| 476 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 478 | if (length == 0) |
| 479 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 481 | 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] | 482 | |
| 483 | packet = hpsb_make_readpacket(host, node, addr, length); |
| 484 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 485 | if (!packet) { |
| 486 | return -ENOMEM; |
| 487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 490 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (retval < 0) |
| 492 | goto hpsb_read_fail; |
| 493 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 494 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 496 | if (retval == 0) { |
| 497 | if (length == 4) { |
| 498 | *buffer = packet->header[3]; |
| 499 | } else { |
| 500 | memcpy(buffer, packet->data, length); |
| 501 | } |
| 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 504 | hpsb_read_fail: |
| 505 | hpsb_free_tlabel(packet); |
| 506 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 508 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | 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] | 512 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | struct hpsb_packet *packet; |
| 515 | int retval; |
| 516 | |
| 517 | if (length == 0) |
| 518 | return -EINVAL; |
| 519 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 520 | 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] | 521 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 522 | packet = hpsb_make_writepacket(host, node, addr, buffer, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | if (!packet) |
| 525 | return -ENOMEM; |
| 526 | |
| 527 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 528 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | if (retval < 0) |
| 530 | goto hpsb_write_fail; |
| 531 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 532 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 534 | hpsb_write_fail: |
| 535 | hpsb_free_tlabel(packet); |
| 536 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 538 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 541 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | 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] | 544 | u64 addr, int extcode, quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 546 | struct hpsb_packet *packet; |
| 547 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 549 | 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] | 550 | |
| 551 | packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 552 | if (!packet) |
| 553 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 556 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (retval < 0) |
| 558 | goto hpsb_lock_fail; |
| 559 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 560 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 562 | if (retval == 0) { |
| 563 | *data = packet->data[0]; |
| 564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 566 | hpsb_lock_fail: |
| 567 | hpsb_free_tlabel(packet); |
| 568 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 570 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 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] | 574 | quadlet_t * buffer, size_t length, u32 specifier_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | unsigned int version) |
| 576 | { |
| 577 | struct hpsb_packet *packet; |
| 578 | int retval = 0; |
| 579 | u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8; |
| 580 | u8 specifier_id_lo = specifier_id & 0xff; |
| 581 | |
| 582 | HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length); |
| 583 | |
| 584 | length += 8; |
| 585 | |
| 586 | packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0); |
| 587 | if (!packet) |
| 588 | return -ENOMEM; |
| 589 | |
| 590 | 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] | 591 | packet->data[1] = |
| 592 | cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | memcpy(&(packet->data[2]), buffer, length - 8); |
| 595 | |
| 596 | packet->generation = generation; |
| 597 | |
| 598 | packet->no_waiter = 1; |
| 599 | |
| 600 | retval = hpsb_send_packet(packet); |
| 601 | if (retval < 0) |
| 602 | hpsb_free_packet(packet); |
| 603 | |
| 604 | return retval; |
| 605 | } |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 606 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 607 | #endif /* 0 */ |