blob: 9da40d9e79daa8526e0e73c858eb486a4e2864c3 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * Connection Management Procedures (IEC 61883-1) helper functions
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
8#include <linux/device.h>
9#include <linux/firewire.h>
10#include <linux/firewire-constants.h>
11#include <linux/module.h>
12#include <linux/sched.h>
13#include "lib.h"
14#include "iso-resources.h"
15#include "cmp.h"
16
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +090017/* MPR common fields */
18#define MPR_SPEED_MASK 0xc0000000
19#define MPR_SPEED_SHIFT 30
20#define MPR_XSPEED_MASK 0x00000060
21#define MPR_XSPEED_SHIFT 5
22#define MPR_PLUGS_MASK 0x0000001f
Clemens Ladisch31ef9132011-03-15 07:53:21 +010023
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +090024/* PCR common fields */
25#define PCR_ONLINE 0x80000000
26#define PCR_BCAST_CONN 0x40000000
27#define PCR_P2P_CONN_MASK 0x3f000000
28#define PCR_P2P_CONN_SHIFT 24
29#define PCR_CHANNEL_MASK 0x003f0000
30#define PCR_CHANNEL_SHIFT 16
Clemens Ladisch31ef9132011-03-15 07:53:21 +010031
Takashi Sakamoto44aff692014-04-25 22:44:56 +090032/* oPCR specific fields */
33#define OPCR_XSPEED_MASK 0x00C00000
34#define OPCR_XSPEED_SHIFT 22
35#define OPCR_SPEED_MASK 0x0000C000
36#define OPCR_SPEED_SHIFT 14
37#define OPCR_OVERHEAD_ID_MASK 0x00003C00
38#define OPCR_OVERHEAD_ID_SHIFT 10
39
Clemens Ladisch31ef9132011-03-15 07:53:21 +010040enum bus_reset_handling {
41 ABORT_ON_BUS_RESET,
42 SUCCEED_ON_BUS_RESET,
43};
44
Joe Perchesb9075fa2011-10-31 17:11:33 -070045static __printf(2, 3)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010046void cmp_error(struct cmp_connection *c, const char *fmt, ...)
47{
48 va_list va;
49
50 va_start(va, fmt);
51 dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
Takashi Sakamoto44aff692014-04-25 22:44:56 +090052 (c->direction == CMP_INPUT) ? 'i' : 'o',
53 c->pcr_index, &(struct va_format){ fmt, &va });
Clemens Ladisch31ef9132011-03-15 07:53:21 +010054 va_end(va);
55}
56
Takashi Sakamoto44aff692014-04-25 22:44:56 +090057static u64 mpr_address(struct cmp_connection *c)
58{
59 if (c->direction == CMP_INPUT)
60 return CSR_REGISTER_BASE + CSR_IMPR;
61 else
62 return CSR_REGISTER_BASE + CSR_OMPR;
63}
64
65static u64 pcr_address(struct cmp_connection *c)
66{
67 if (c->direction == CMP_INPUT)
68 return CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index);
69 else
70 return CSR_REGISTER_BASE + CSR_OPCR(c->pcr_index);
71}
72
Clemens Ladisch31ef9132011-03-15 07:53:21 +010073static int pcr_modify(struct cmp_connection *c,
74 __be32 (*modify)(struct cmp_connection *c, __be32 old),
75 int (*check)(struct cmp_connection *c, __be32 pcr),
76 enum bus_reset_handling bus_reset_handling)
77{
Stefan Richterf30e6d32011-04-22 15:13:54 +020078 __be32 old_arg, buffer[2];
Clemens Ladisch31ef9132011-03-15 07:53:21 +010079 int err;
80
81 buffer[0] = c->last_pcr_value;
82 for (;;) {
83 old_arg = buffer[0];
84 buffer[1] = modify(c, buffer[0]);
85
Clemens Ladisch1b704852011-09-04 22:17:38 +020086 err = snd_fw_transaction(
87 c->resources.unit, TCODE_LOCK_COMPARE_SWAP,
Takashi Sakamoto44aff692014-04-25 22:44:56 +090088 pcr_address(c), buffer, 8,
Clemens Ladisch1b704852011-09-04 22:17:38 +020089 FW_FIXED_GENERATION | c->resources.generation);
Clemens Ladisch31ef9132011-03-15 07:53:21 +010090
Clemens Ladisch1b704852011-09-04 22:17:38 +020091 if (err < 0) {
92 if (err == -EAGAIN &&
93 bus_reset_handling == SUCCEED_ON_BUS_RESET)
94 err = 0;
95 return err;
96 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +010097
Clemens Ladisch1b704852011-09-04 22:17:38 +020098 if (buffer[0] == old_arg) /* success? */
99 break;
100
101 if (check) {
102 err = check(c, buffer[0]);
103 if (err < 0)
104 return err;
105 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100106 }
107 c->last_pcr_value = buffer[1];
108
109 return 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100110}
111
112
113/**
114 * cmp_connection_init - initializes a connection manager
115 * @c: the connection manager to initialize
116 * @unit: a unit of the target device
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900117 * @pcr_index: the index of the iPCR/oPCR on the target device
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100118 */
119int cmp_connection_init(struct cmp_connection *c,
120 struct fw_unit *unit,
Takashi Sakamotoc68a1c62014-04-25 22:44:55 +0900121 enum cmp_direction direction,
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900122 unsigned int pcr_index)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100123{
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900124 __be32 mpr_be;
125 u32 mpr;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100126 int err;
127
Takashi Sakamoto44aff692014-04-25 22:44:56 +0900128 c->direction = direction;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100129 err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
Takashi Sakamoto44aff692014-04-25 22:44:56 +0900130 mpr_address(c), &mpr_be, 4, 0);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100131 if (err < 0)
132 return err;
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900133 mpr = be32_to_cpu(mpr_be);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100134
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900135 if (pcr_index >= (mpr & MPR_PLUGS_MASK))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100136 return -EINVAL;
137
Clemens Ladisch5b2599a2011-03-15 07:55:50 +0100138 err = fw_iso_resources_init(&c->resources, unit);
139 if (err < 0)
140 return err;
141
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100142 c->connected = false;
143 mutex_init(&c->mutex);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100144 c->last_pcr_value = cpu_to_be32(0x80000000);
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900145 c->pcr_index = pcr_index;
146 c->max_speed = (mpr & MPR_SPEED_MASK) >> MPR_SPEED_SHIFT;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100147 if (c->max_speed == SCODE_BETA)
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900148 c->max_speed += (mpr & MPR_XSPEED_MASK) >> MPR_XSPEED_SHIFT;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100149
150 return 0;
151}
152EXPORT_SYMBOL(cmp_connection_init);
153
154/**
155 * cmp_connection_destroy - free connection manager resources
156 * @c: the connection manager
157 */
158void cmp_connection_destroy(struct cmp_connection *c)
159{
160 WARN_ON(c->connected);
161 mutex_destroy(&c->mutex);
162 fw_iso_resources_destroy(&c->resources);
163}
164EXPORT_SYMBOL(cmp_connection_destroy);
165
166
167static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
168{
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900169 ipcr &= ~cpu_to_be32(PCR_BCAST_CONN |
170 PCR_P2P_CONN_MASK |
171 PCR_CHANNEL_MASK);
172 ipcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
173 ipcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100174
175 return ipcr;
176}
177
Takashi Sakamoto44aff692014-04-25 22:44:56 +0900178static int get_overhead_id(struct cmp_connection *c)
179{
180 int id;
181
182 /*
183 * apply "oPCR overhead ID encoding"
184 * the encoding table can convert up to 512.
185 * here the value over 512 is converted as the same way as 512.
186 */
187 for (id = 1; id < 16; id++) {
188 if (c->resources.bandwidth_overhead < (id << 5))
189 break;
190 }
191 if (id == 16)
192 id = 0;
193
194 return id;
195}
196
197static __be32 opcr_set_modify(struct cmp_connection *c, __be32 opcr)
198{
199 unsigned int spd, xspd;
200
201 /* generate speed and extended speed field value */
202 if (c->speed > SCODE_400) {
203 spd = SCODE_800;
204 xspd = c->speed - SCODE_800;
205 } else {
206 spd = c->speed;
207 xspd = 0;
208 }
209
210 opcr &= ~cpu_to_be32(PCR_BCAST_CONN |
211 PCR_P2P_CONN_MASK |
212 OPCR_XSPEED_MASK |
213 PCR_CHANNEL_MASK |
214 OPCR_SPEED_MASK |
215 OPCR_OVERHEAD_ID_MASK);
216 opcr |= cpu_to_be32(1 << PCR_P2P_CONN_SHIFT);
217 opcr |= cpu_to_be32(xspd << OPCR_XSPEED_SHIFT);
218 opcr |= cpu_to_be32(c->resources.channel << PCR_CHANNEL_SHIFT);
219 opcr |= cpu_to_be32(spd << OPCR_SPEED_SHIFT);
220 opcr |= cpu_to_be32(get_overhead_id(c) << OPCR_OVERHEAD_ID_SHIFT);
221
222 return opcr;
223}
224
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900225static int pcr_set_check(struct cmp_connection *c, __be32 pcr)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100226{
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900227 if (pcr & cpu_to_be32(PCR_BCAST_CONN |
228 PCR_P2P_CONN_MASK)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100229 cmp_error(c, "plug is already in use\n");
230 return -EBUSY;
231 }
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900232 if (!(pcr & cpu_to_be32(PCR_ONLINE))) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100233 cmp_error(c, "plug is not on-line\n");
234 return -ECONNREFUSED;
235 }
236
237 return 0;
238}
239
240/**
241 * cmp_connection_establish - establish a connection to the target
242 * @c: the connection manager
243 * @max_payload_bytes: the amount of data (including CIP headers) per packet
244 *
245 * This function establishes a point-to-point connection from the local
246 * computer to the target by allocating isochronous resources (channel and
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900247 * bandwidth) and setting the target's input/output plug control register.
248 * When this function succeeds, the caller is responsible for starting
249 * transmitting packets.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100250 */
251int cmp_connection_establish(struct cmp_connection *c,
252 unsigned int max_payload_bytes)
253{
254 int err;
255
256 if (WARN_ON(c->connected))
257 return -EISCONN;
258
259 c->speed = min(c->max_speed,
260 fw_parent_device(c->resources.unit)->max_speed);
261
262 mutex_lock(&c->mutex);
263
264retry_after_bus_reset:
265 err = fw_iso_resources_allocate(&c->resources,
266 max_payload_bytes, c->speed);
267 if (err < 0)
268 goto err_mutex;
269
Takashi Sakamoto44aff692014-04-25 22:44:56 +0900270 if (c->direction == CMP_OUTPUT)
271 err = pcr_modify(c, opcr_set_modify, pcr_set_check,
272 ABORT_ON_BUS_RESET);
273 else
274 err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
275 ABORT_ON_BUS_RESET);
276
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100277 if (err == -EAGAIN) {
278 fw_iso_resources_free(&c->resources);
279 goto retry_after_bus_reset;
280 }
281 if (err < 0)
282 goto err_resources;
283
284 c->connected = true;
285
286 mutex_unlock(&c->mutex);
287
288 return 0;
289
290err_resources:
291 fw_iso_resources_free(&c->resources);
292err_mutex:
293 mutex_unlock(&c->mutex);
294
295 return err;
296}
297EXPORT_SYMBOL(cmp_connection_establish);
298
299/**
300 * cmp_connection_update - update the connection after a bus reset
301 * @c: the connection manager
302 *
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900303 * This function must be called from the driver's .update handler to
304 * reestablish any connection that might have been active.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100305 *
306 * Returns zero on success, or a negative error code. On an error, the
307 * connection is broken and the caller must stop transmitting iso packets.
308 */
309int cmp_connection_update(struct cmp_connection *c)
310{
311 int err;
312
313 mutex_lock(&c->mutex);
314
315 if (!c->connected) {
316 mutex_unlock(&c->mutex);
317 return 0;
318 }
319
320 err = fw_iso_resources_update(&c->resources);
321 if (err < 0)
322 goto err_unconnect;
323
Takashi Sakamoto44aff692014-04-25 22:44:56 +0900324 if (c->direction == CMP_OUTPUT)
325 err = pcr_modify(c, opcr_set_modify, pcr_set_check,
326 SUCCEED_ON_BUS_RESET);
327 else
328 err = pcr_modify(c, ipcr_set_modify, pcr_set_check,
329 SUCCEED_ON_BUS_RESET);
330
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100331 if (err < 0)
332 goto err_resources;
333
334 mutex_unlock(&c->mutex);
335
336 return 0;
337
338err_resources:
339 fw_iso_resources_free(&c->resources);
340err_unconnect:
341 c->connected = false;
342 mutex_unlock(&c->mutex);
343
344 return err;
345}
346EXPORT_SYMBOL(cmp_connection_update);
347
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900348static __be32 pcr_break_modify(struct cmp_connection *c, __be32 pcr)
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100349{
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900350 return pcr & ~cpu_to_be32(PCR_BCAST_CONN | PCR_P2P_CONN_MASK);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100351}
352
353/**
354 * cmp_connection_break - break the connection to the target
355 * @c: the connection manager
356 *
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900357 * This function deactives the connection in the target's input/output plug
358 * control register, and frees the isochronous resources of the connection.
359 * Before calling this function, the caller should cease transmitting packets.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100360 */
361void cmp_connection_break(struct cmp_connection *c)
362{
363 int err;
364
365 mutex_lock(&c->mutex);
366
367 if (!c->connected) {
368 mutex_unlock(&c->mutex);
369 return;
370 }
371
Takashi Sakamotoa7fa0d02014-04-25 22:44:54 +0900372 err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100373 if (err < 0)
374 cmp_error(c, "plug is still connected\n");
375
376 fw_iso_resources_free(&c->resources);
377
378 c->connected = false;
379
380 mutex_unlock(&c->mutex);
381}
382EXPORT_SYMBOL(cmp_connection_break);