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 | |
| 92 | 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] | 93 | int tag, int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 95 | packet->header[0] = (length << 16) | (tag << 14) | (channel << 8) |
| 96 | | (TCODE_ISO_DATA << 4) | sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 98 | packet->header_size = 4; |
| 99 | packet->data_size = length; |
| 100 | packet->type = hpsb_iso; |
| 101 | packet->tcode = TCODE_ISO_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) |
| 105 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 119 | | (TCODE_STREAM_DATA << 4) | sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | packet->header_size = 4; |
| 122 | packet->data_size = length; |
| 123 | packet->type = hpsb_async; |
| 124 | packet->tcode = TCODE_ISO_DATA; |
| 125 | } |
| 126 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /** |
| 160 | * hpsb_get_tlabel - allocate a transaction label |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 161 | * @packet: the packet whose tlabel and tl_pool we set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 175 | * tlabel or a signal is received. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
| 177 | int hpsb_get_tlabel(struct hpsb_packet *packet) |
| 178 | { |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 179 | if (irqs_disabled() || in_atomic()) |
| 180 | return hpsb_get_tlabel_atomic(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * hpsb_free_tlabel - free an allocated transaction label |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 193 | * @packet: packet whose tlabel and tl_pool needs to be cleared |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 204 | unsigned long flags, *tp; |
| 205 | int tlabel, n = NODEID_TO_NODE(packet->node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Ben Collins | eaf8845 | 2006-06-12 18:12:49 -0400 | [diff] [blame] | 207 | if (unlikely(n == ALL_NODES)) |
| 208 | return; |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 209 | tp = packet->host->tl_pool[n].map; |
| 210 | tlabel = packet->tlabel; |
| 211 | BUG_ON(tlabel > 63 || tlabel < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Stefan Richter | 9951903 | 2006-07-02 14:17:00 +0200 | [diff] [blame] | 217 | wake_up_interruptible(&tlabel_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int hpsb_packet_success(struct hpsb_packet *packet) |
| 230 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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 | } |
Stefan Richter | d83e7d8 | 2006-07-03 12:02:31 -0400 | [diff] [blame] | 250 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 252 | case ACK_BUSY_X: |
| 253 | case ACK_BUSY_A: |
| 254 | case ACK_BUSY_B: |
| 255 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 257 | case ACK_TYPE_ERROR: |
| 258 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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 | } |
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 | case ACK_ADDRESS_ERROR: |
| 281 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 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 | } |
Stefan Richter | d83e7d8 | 2006-07-03 12:02:31 -0400 | [diff] [blame] | 297 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node, |
| 301 | u64 addr, size_t length) |
| 302 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 303 | struct hpsb_packet *packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 328 | struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node, |
| 329 | u64 addr, quadlet_t * buffer, |
| 330 | size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 341 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 363 | struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer, |
| 364 | int length, int channel, int tag, |
| 365 | int sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 376 | if (length % 4) { /* zero padding bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | packet->data[length >> 2] = 0; |
| 378 | } |
| 379 | packet->host = host; |
| 380 | |
Stefan Richter | ea9057a | 2007-04-23 21:27:13 +0200 | [diff] [blame] | 381 | /* Because it is too difficult to determine all PHY speeds and link |
| 382 | * speeds here, we use S100... */ |
| 383 | packet->speed_code = IEEE1394_SPEED_100; |
| 384 | |
| 385 | /* ...and prevent hpsb_send_packet() from overriding it. */ |
| 386 | packet->node_id = LOCAL_BUS | ALL_NODES; |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | if (hpsb_get_tlabel(packet)) { |
| 389 | hpsb_free_packet(packet); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | fill_async_stream_packet(packet, length, channel, tag, sync); |
| 394 | if (buffer) |
| 395 | memcpy(packet->data, buffer, length); |
| 396 | |
| 397 | return packet; |
| 398 | } |
| 399 | |
| 400 | 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] | 401 | u64 addr, int extcode, |
| 402 | quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | struct hpsb_packet *p; |
| 405 | u32 length; |
| 406 | |
| 407 | p = hpsb_alloc_packet(8); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 408 | if (!p) |
| 409 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | p->host = host; |
| 412 | p->node_id = node; |
| 413 | if (hpsb_get_tlabel(p)) { |
| 414 | hpsb_free_packet(p); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | switch (extcode) { |
| 419 | case EXTCODE_FETCH_ADD: |
| 420 | case EXTCODE_LITTLE_ADD: |
| 421 | length = 4; |
| 422 | if (data) |
| 423 | p->data[0] = *data; |
| 424 | break; |
| 425 | default: |
| 426 | length = 8; |
| 427 | if (data) { |
| 428 | p->data[0] = arg; |
| 429 | p->data[1] = *data; |
| 430 | } |
| 431 | break; |
| 432 | } |
| 433 | fill_async_lock(p, addr, extcode, length); |
| 434 | |
| 435 | return p; |
| 436 | } |
| 437 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 438 | struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host, |
| 439 | nodeid_t node, u64 addr, int extcode, |
| 440 | octlet_t * data, octlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
| 442 | struct hpsb_packet *p; |
| 443 | u32 length; |
| 444 | |
| 445 | p = hpsb_alloc_packet(16); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 446 | if (!p) |
| 447 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | p->host = host; |
| 450 | p->node_id = node; |
| 451 | if (hpsb_get_tlabel(p)) { |
| 452 | hpsb_free_packet(p); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | switch (extcode) { |
| 457 | case EXTCODE_FETCH_ADD: |
| 458 | case EXTCODE_LITTLE_ADD: |
| 459 | length = 8; |
| 460 | if (data) { |
| 461 | p->data[0] = *data >> 32; |
| 462 | p->data[1] = *data & 0xffffffff; |
| 463 | } |
| 464 | break; |
| 465 | default: |
| 466 | length = 16; |
| 467 | if (data) { |
| 468 | p->data[0] = arg >> 32; |
| 469 | p->data[1] = arg & 0xffffffff; |
| 470 | p->data[2] = *data >> 32; |
| 471 | p->data[3] = *data & 0xffffffff; |
| 472 | } |
| 473 | break; |
| 474 | } |
| 475 | fill_async_lock(p, addr, extcode, length); |
| 476 | |
| 477 | return p; |
| 478 | } |
| 479 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 480 | 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] | 481 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 482 | struct hpsb_packet *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 484 | p = hpsb_alloc_packet(0); |
| 485 | if (!p) |
| 486 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 488 | p->host = host; |
| 489 | fill_phy_packet(p, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 491 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host, |
| 495 | int length, int channel, |
| 496 | int tag, int sync) |
| 497 | { |
| 498 | struct hpsb_packet *p; |
| 499 | |
| 500 | p = hpsb_alloc_packet(length); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 501 | if (!p) |
| 502 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | p->host = host; |
| 505 | fill_iso_packet(p, length, channel, tag, sync); |
| 506 | |
| 507 | p->generation = get_hpsb_generation(host); |
| 508 | |
| 509 | return p; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * FIXME - these functions should probably read from / write to user space to |
| 514 | * avoid in kernel buffers for user space callers |
| 515 | */ |
| 516 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 517 | /** |
| 518 | * hpsb_read - generic read function |
| 519 | * |
| 520 | * Recognizes the local node ID and act accordingly. Automatically uses a |
| 521 | * quadlet read request if @length == 4 and and a block read request otherwise. |
| 522 | * It does not yet support lengths that are not a multiple of 4. |
| 523 | * |
| 524 | * You must explicitly specifiy the @generation for which the node ID is valid, |
| 525 | * to avoid sending packets to the wrong nodes when we race with a bus reset. |
| 526 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | 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] | 528 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 530 | struct hpsb_packet *packet; |
| 531 | int retval = 0; |
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 | if (length == 0) |
| 534 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 536 | 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] | 537 | |
| 538 | packet = hpsb_make_readpacket(host, node, addr, length); |
| 539 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 540 | if (!packet) { |
| 541 | return -ENOMEM; |
| 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 545 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (retval < 0) |
| 547 | goto hpsb_read_fail; |
| 548 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 549 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 551 | if (retval == 0) { |
| 552 | if (length == 4) { |
| 553 | *buffer = packet->header[3]; |
| 554 | } else { |
| 555 | memcpy(buffer, packet->data, length); |
| 556 | } |
| 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 559 | hpsb_read_fail: |
| 560 | hpsb_free_tlabel(packet); |
| 561 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 563 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 566 | /** |
| 567 | * hpsb_write - generic write function |
| 568 | * |
| 569 | * Recognizes the local node ID and act accordingly. Automatically uses a |
| 570 | * quadlet write request if @length == 4 and and a block write request |
| 571 | * otherwise. It does not yet support lengths that are not a multiple of 4. |
| 572 | * |
| 573 | * You must explicitly specifiy the @generation for which the node ID is valid, |
| 574 | * to avoid sending packets to the wrong nodes when we race with a bus reset. |
| 575 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | 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] | 577 | u64 addr, quadlet_t * buffer, size_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | struct hpsb_packet *packet; |
| 580 | int retval; |
| 581 | |
| 582 | if (length == 0) |
| 583 | return -EINVAL; |
| 584 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 585 | 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] | 586 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 587 | packet = hpsb_make_writepacket(host, node, addr, buffer, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | if (!packet) |
| 590 | return -ENOMEM; |
| 591 | |
| 592 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 593 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (retval < 0) |
| 595 | goto hpsb_write_fail; |
| 596 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 597 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 599 | hpsb_write_fail: |
| 600 | hpsb_free_tlabel(packet); |
| 601 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 603 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 606 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | 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] | 609 | u64 addr, int extcode, quadlet_t * data, quadlet_t arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 611 | struct hpsb_packet *packet; |
| 612 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 614 | 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] | 615 | |
| 616 | packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg); |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 617 | if (!packet) |
| 618 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | packet->generation = generation; |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 621 | retval = hpsb_send_packet_and_wait(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (retval < 0) |
| 623 | goto hpsb_lock_fail; |
| 624 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 625 | retval = hpsb_packet_success(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 627 | if (retval == 0) { |
| 628 | *data = packet->data[0]; |
| 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 631 | hpsb_lock_fail: |
| 632 | hpsb_free_tlabel(packet); |
| 633 | hpsb_free_packet(packet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 635 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | 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] | 639 | quadlet_t * buffer, size_t length, u32 specifier_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | unsigned int version) |
| 641 | { |
| 642 | struct hpsb_packet *packet; |
| 643 | int retval = 0; |
| 644 | u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8; |
| 645 | u8 specifier_id_lo = specifier_id & 0xff; |
| 646 | |
| 647 | HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length); |
| 648 | |
| 649 | length += 8; |
| 650 | |
| 651 | packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0); |
| 652 | if (!packet) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | 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] | 656 | packet->data[1] = |
| 657 | cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | memcpy(&(packet->data[2]), buffer, length - 8); |
| 660 | |
| 661 | packet->generation = generation; |
| 662 | |
| 663 | packet->no_waiter = 1; |
| 664 | |
| 665 | retval = hpsb_send_packet(packet); |
| 666 | if (retval < 0) |
| 667 | hpsb_free_packet(packet); |
| 668 | |
| 669 | return retval; |
| 670 | } |
Jody McIntyre | 9ac485d | 2005-05-16 21:54:00 -0700 | [diff] [blame] | 671 | |
Jens-Michael Hoffmann | 16c333a | 2005-11-22 12:34:16 -0500 | [diff] [blame] | 672 | #endif /* 0 */ |