1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FIREWIRE_H
3 #define _LINUX_FIREWIRE_H
4
5 #include <linux/completion.h>
6 #include <linux/device.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/kernel.h>
9 #include <linux/kref.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/sysfs.h>
14 #include <linux/timer.h>
15 #include <linux/types.h>
16 #include <linux/workqueue.h>
17
18 #include <linux/atomic.h>
19 #include <asm/byteorder.h>
20
21 #define CSR_REGISTER_BASE 0xfffff0000000ULL
22
23 /* register offsets are relative to CSR_REGISTER_BASE */
24 #define CSR_STATE_CLEAR 0x0
25 #define CSR_STATE_SET 0x4
26 #define CSR_NODE_IDS 0x8
27 #define CSR_RESET_START 0xc
28 #define CSR_SPLIT_TIMEOUT_HI 0x18
29 #define CSR_SPLIT_TIMEOUT_LO 0x1c
30 #define CSR_CYCLE_TIME 0x200
31 #define CSR_BUS_TIME 0x204
32 #define CSR_BUSY_TIMEOUT 0x210
33 #define CSR_PRIORITY_BUDGET 0x218
34 #define CSR_BUS_MANAGER_ID 0x21c
35 #define CSR_BANDWIDTH_AVAILABLE 0x220
36 #define CSR_CHANNELS_AVAILABLE 0x224
37 #define CSR_CHANNELS_AVAILABLE_HI 0x224
38 #define CSR_CHANNELS_AVAILABLE_LO 0x228
39 #define CSR_MAINT_UTILITY 0x230
40 #define CSR_BROADCAST_CHANNEL 0x234
41 #define CSR_CONFIG_ROM 0x400
42 #define CSR_CONFIG_ROM_END 0x800
43 #define CSR_OMPR 0x900
44 #define CSR_OPCR(i) (0x904 + (i) * 4)
45 #define CSR_IMPR 0x980
46 #define CSR_IPCR(i) (0x984 + (i) * 4)
47 #define CSR_FCP_COMMAND 0xB00
48 #define CSR_FCP_RESPONSE 0xD00
49 #define CSR_FCP_END 0xF00
50 #define CSR_TOPOLOGY_MAP 0x1000
51 #define CSR_TOPOLOGY_MAP_END 0x1400
52 #define CSR_SPEED_MAP 0x2000
53 #define CSR_SPEED_MAP_END 0x3000
54
55 #define CSR_OFFSET 0x40
56 #define CSR_LEAF 0x80
57 #define CSR_DIRECTORY 0xc0
58
59 #define CSR_DESCRIPTOR 0x01
60 #define CSR_VENDOR 0x03
61 #define CSR_HARDWARE_VERSION 0x04
62 #define CSR_UNIT 0x11
63 #define CSR_SPECIFIER_ID 0x12
64 #define CSR_VERSION 0x13
65 #define CSR_DEPENDENT_INFO 0x14
66 #define CSR_MODEL 0x17
67 #define CSR_DIRECTORY_ID 0x20
68
69 struct fw_csr_iterator {
70 const u32 *p;
71 const u32 *end;
72 };
73
74 void fw_csr_iterator_init(struct fw_csr_iterator *ci, const u32 *p);
75 int fw_csr_iterator_next(struct fw_csr_iterator *ci, int *key, int *value);
76 int fw_csr_string(const u32 *directory, int key, char *buf, size_t size);
77
78 extern const struct bus_type fw_bus_type;
79
80 struct fw_card_driver;
81 struct fw_node;
82
83 struct fw_card {
84 const struct fw_card_driver *driver;
85 struct device *device;
86 struct kref kref;
87 struct completion done;
88
89 int node_id;
90 int generation;
91 int current_tlabel;
92 u64 tlabel_mask;
93 struct list_head transaction_list;
94 u64 reset_jiffies;
95
96 u32 split_timeout_hi;
97 u32 split_timeout_lo;
98 unsigned int split_timeout_cycles;
99 unsigned int split_timeout_jiffies;
100
101 unsigned long long guid;
102 unsigned max_receive;
103 int link_speed;
104 int config_rom_generation;
105
106 spinlock_t lock; /* Take this lock when handling the lists in
107 * this struct. */
108 struct fw_node *local_node;
109 struct fw_node *root_node;
110 struct fw_node *irm_node;
111 u8 color; /* must be u8 to match the definition in struct fw_node */
112 int gap_count;
113 bool beta_repeaters_present;
114
115 int index;
116 struct list_head link;
117
118 struct list_head phy_receiver_list;
119
120 struct delayed_work br_work; /* bus reset job */
121 bool br_short;
122
123 struct delayed_work bm_work; /* bus manager job */
124 int bm_retries;
125 int bm_generation;
126 int bm_node_id;
127 bool bm_abdicate;
128
129 bool priority_budget_implemented; /* controller feature */
130 bool broadcast_channel_auto_allocated; /* controller feature */
131
132 bool broadcast_channel_allocated;
133 u32 broadcast_channel;
134 __be32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
135
136 __be32 maint_utility_register;
137
138 struct workqueue_struct *isoc_wq;
139 };
140
fw_card_get(struct fw_card * card)141 static inline struct fw_card *fw_card_get(struct fw_card *card)
142 {
143 kref_get(&card->kref);
144
145 return card;
146 }
147
148 void fw_card_release(struct kref *kref);
149
fw_card_put(struct fw_card * card)150 static inline void fw_card_put(struct fw_card *card)
151 {
152 kref_put(&card->kref, fw_card_release);
153 }
154
155 int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time);
156
157 struct fw_attribute_group {
158 struct attribute_group *groups[2];
159 struct attribute_group group;
160 struct attribute *attrs[13];
161 };
162
163 enum fw_device_state {
164 FW_DEVICE_INITIALIZING,
165 FW_DEVICE_RUNNING,
166 FW_DEVICE_GONE,
167 FW_DEVICE_SHUTDOWN,
168 };
169
170 /*
171 * Note, fw_device.generation always has to be read before fw_device.node_id.
172 * Use SMP memory barriers to ensure this. Otherwise requests will be sent
173 * to an outdated node_id if the generation was updated in the meantime due
174 * to a bus reset.
175 *
176 * Likewise, fw-core will take care to update .node_id before .generation so
177 * that whenever fw_device.generation is current WRT the actual bus generation,
178 * fw_device.node_id is guaranteed to be current too.
179 *
180 * The same applies to fw_device.card->node_id vs. fw_device.generation.
181 *
182 * fw_device.config_rom and fw_device.config_rom_length may be accessed during
183 * the lifetime of any fw_unit belonging to the fw_device, before device_del()
184 * was called on the last fw_unit. Alternatively, they may be accessed while
185 * holding fw_device_rwsem.
186 */
187 struct fw_device {
188 atomic_t state;
189 struct fw_node *node;
190 int node_id;
191 int generation;
192 unsigned max_speed;
193 struct fw_card *card;
194 struct device device;
195
196 struct mutex client_list_mutex;
197 struct list_head client_list;
198
199 const u32 *config_rom;
200 size_t config_rom_length;
201 int config_rom_retries;
202 unsigned is_local:1;
203 unsigned max_rec:4;
204 unsigned cmc:1;
205 unsigned irmc:1;
206 unsigned bc_implemented:2;
207
208 work_func_t workfn;
209 struct delayed_work work;
210 struct fw_attribute_group attribute_group;
211 };
212
213 #define fw_device(dev) container_of_const(dev, struct fw_device, device)
214
fw_device_is_shutdown(struct fw_device * device)215 static inline int fw_device_is_shutdown(struct fw_device *device)
216 {
217 return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
218 }
219
220 int fw_device_enable_phys_dma(struct fw_device *device);
221
222 /*
223 * fw_unit.directory must not be accessed after device_del(&fw_unit.device).
224 */
225 struct fw_unit {
226 struct device device;
227 const u32 *directory;
228 struct fw_attribute_group attribute_group;
229 };
230
231 #define fw_unit(dev) container_of_const(dev, struct fw_unit, device)
232
fw_unit_get(struct fw_unit * unit)233 static inline struct fw_unit *fw_unit_get(struct fw_unit *unit)
234 {
235 get_device(&unit->device);
236
237 return unit;
238 }
239
fw_unit_put(struct fw_unit * unit)240 static inline void fw_unit_put(struct fw_unit *unit)
241 {
242 put_device(&unit->device);
243 }
244
245 #define fw_parent_device(unit) fw_device(unit->device.parent)
246
247 struct ieee1394_device_id;
248
249 struct fw_driver {
250 struct device_driver driver;
251 int (*probe)(struct fw_unit *unit, const struct ieee1394_device_id *id);
252 /* Called when the parent device sits through a bus reset. */
253 void (*update)(struct fw_unit *unit);
254 void (*remove)(struct fw_unit *unit);
255 const struct ieee1394_device_id *id_table;
256 };
257
258 struct fw_packet;
259 struct fw_request;
260
261 typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
262 struct fw_card *card, int status);
263 typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
264 void *data, size_t length,
265 void *callback_data);
266 typedef void (*fw_transaction_callback_with_tstamp_t)(struct fw_card *card, int rcode,
267 u32 request_tstamp, u32 response_tstamp, void *data,
268 size_t length, void *callback_data);
269
270 union fw_transaction_callback {
271 fw_transaction_callback_t without_tstamp;
272 fw_transaction_callback_with_tstamp_t with_tstamp;
273 };
274
275 /*
276 * This callback handles an inbound request subaction. It is called in
277 * RCU read-side context, therefore must not sleep.
278 *
279 * The callback should not initiate outbound request subactions directly.
280 * Otherwise there is a danger of recursion of inbound and outbound
281 * transactions from and to the local node.
282 *
283 * The callback is responsible that fw_send_response() is called on the @request, except for FCP
284 * registers for which the core takes care of that.
285 */
286 typedef void (*fw_address_callback_t)(struct fw_card *card,
287 struct fw_request *request,
288 int tcode, int destination, int source,
289 int generation,
290 unsigned long long offset,
291 void *data, size_t length,
292 void *callback_data);
293
294 struct fw_packet {
295 int speed;
296 int generation;
297 u32 header[4];
298 size_t header_length;
299 void *payload;
300 size_t payload_length;
301 dma_addr_t payload_bus;
302 bool payload_mapped;
303 u32 timestamp;
304
305 /*
306 * This callback is called when the packet transmission has completed.
307 * For successful transmission, the status code is the ack received
308 * from the destination. Otherwise it is one of the juju-specific
309 * rcodes: RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK.
310 * The callback can be called from tasklet context and thus
311 * must never block.
312 */
313 fw_packet_callback_t callback;
314 int ack;
315 struct list_head link;
316 void *driver_data;
317 };
318
319 struct fw_transaction {
320 int node_id; /* The generation is implied; it is always the current. */
321 int tlabel;
322 struct list_head link;
323 struct fw_card *card;
324 bool is_split_transaction;
325 struct timer_list split_timeout_timer;
326 u32 split_timeout_cycle;
327
328 struct fw_packet packet;
329
330 /*
331 * The data passed to the callback is valid only during the
332 * callback.
333 */
334 union fw_transaction_callback callback;
335 bool with_tstamp;
336 void *callback_data;
337 };
338
339 struct fw_address_handler {
340 u64 offset;
341 u64 length;
342 fw_address_callback_t address_callback;
343 void *callback_data;
344 struct list_head link;
345 };
346
347 struct fw_address_region {
348 u64 start;
349 u64 end;
350 };
351
352 extern const struct fw_address_region fw_high_memory_region;
353
354 int fw_core_add_address_handler(struct fw_address_handler *handler,
355 const struct fw_address_region *region);
356 void fw_core_remove_address_handler(struct fw_address_handler *handler);
357 void fw_send_response(struct fw_card *card,
358 struct fw_request *request, int rcode);
359 int fw_get_request_speed(struct fw_request *request);
360 u32 fw_request_get_timestamp(const struct fw_request *request);
361
362 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
363 int destination_id, int generation, int speed, unsigned long long offset,
364 void *payload, size_t length, union fw_transaction_callback callback,
365 bool with_tstamp, void *callback_data);
366
367 /**
368 * fw_send_request() - submit a request packet for transmission to generate callback for response
369 * subaction without time stamp.
370 * @card: interface to send the request at
371 * @t: transaction instance to which the request belongs
372 * @tcode: transaction code
373 * @destination_id: destination node ID, consisting of bus_ID and phy_ID
374 * @generation: bus generation in which request and response are valid
375 * @speed: transmission speed
376 * @offset: 48bit wide offset into destination's address space
377 * @payload: data payload for the request subaction
378 * @length: length of the payload, in bytes
379 * @callback: function to be called when the transaction is completed
380 * @callback_data: data to be passed to the transaction completion callback
381 *
382 * A variation of __fw_send_request() to generate callback for response subaction without time
383 * stamp.
384 */
fw_send_request(struct fw_card * card,struct fw_transaction * t,int tcode,int destination_id,int generation,int speed,unsigned long long offset,void * payload,size_t length,fw_transaction_callback_t callback,void * callback_data)385 static inline void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
386 int destination_id, int generation, int speed,
387 unsigned long long offset, void *payload, size_t length,
388 fw_transaction_callback_t callback, void *callback_data)
389 {
390 union fw_transaction_callback cb = {
391 .without_tstamp = callback,
392 };
393 __fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
394 length, cb, false, callback_data);
395 }
396
397 /**
398 * fw_send_request_with_tstamp() - submit a request packet for transmission to generate callback for
399 * response with time stamp.
400 * @card: interface to send the request at
401 * @t: transaction instance to which the request belongs
402 * @tcode: transaction code
403 * @destination_id: destination node ID, consisting of bus_ID and phy_ID
404 * @generation: bus generation in which request and response are valid
405 * @speed: transmission speed
406 * @offset: 48bit wide offset into destination's address space
407 * @payload: data payload for the request subaction
408 * @length: length of the payload, in bytes
409 * @callback: function to be called when the transaction is completed
410 * @callback_data: data to be passed to the transaction completion callback
411 *
412 * A variation of __fw_send_request() to generate callback for response subaction with time stamp.
413 */
fw_send_request_with_tstamp(struct fw_card * card,struct fw_transaction * t,int tcode,int destination_id,int generation,int speed,unsigned long long offset,void * payload,size_t length,fw_transaction_callback_with_tstamp_t callback,void * callback_data)414 static inline void fw_send_request_with_tstamp(struct fw_card *card, struct fw_transaction *t,
415 int tcode, int destination_id, int generation, int speed, unsigned long long offset,
416 void *payload, size_t length, fw_transaction_callback_with_tstamp_t callback,
417 void *callback_data)
418 {
419 union fw_transaction_callback cb = {
420 .with_tstamp = callback,
421 };
422 __fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
423 length, cb, true, callback_data);
424 }
425
426 int fw_cancel_transaction(struct fw_card *card,
427 struct fw_transaction *transaction);
428 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
429 int generation, int speed, unsigned long long offset,
430 void *payload, size_t length);
431 const char *fw_rcode_string(int rcode);
432
fw_stream_packet_destination_id(int tag,int channel,int sy)433 static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
434 {
435 return tag << 14 | channel << 8 | sy;
436 }
437
438 void fw_schedule_bus_reset(struct fw_card *card, bool delayed,
439 bool short_reset);
440
441 struct fw_descriptor {
442 struct list_head link;
443 size_t length;
444 u32 immediate;
445 u32 key;
446 const u32 *data;
447 };
448
449 int fw_core_add_descriptor(struct fw_descriptor *desc);
450 void fw_core_remove_descriptor(struct fw_descriptor *desc);
451
452 /*
453 * The iso packet format allows for an immediate header/payload part
454 * stored in 'header' immediately after the packet info plus an
455 * indirect payload part that is pointer to by the 'payload' field.
456 * Applications can use one or the other or both to implement simple
457 * low-bandwidth streaming (e.g. audio) or more advanced
458 * scatter-gather streaming (e.g. assembling video frame automatically).
459 */
460 struct fw_iso_packet {
461 u16 payload_length; /* Length of indirect payload */
462 u32 interrupt:1; /* Generate interrupt on this packet */
463 u32 skip:1; /* tx: Set to not send packet at all */
464 /* rx: Sync bit, wait for matching sy */
465 u32 tag:2; /* tx: Tag in packet header */
466 u32 sy:4; /* tx: Sy in packet header */
467 u32 header_length:8; /* Size of immediate header */
468 u32 header[]; /* tx: Top of 1394 isoch. data_block */
469 };
470
471 #define FW_ISO_CONTEXT_TRANSMIT 0
472 #define FW_ISO_CONTEXT_RECEIVE 1
473 #define FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL 2
474
475 #define FW_ISO_CONTEXT_MATCH_TAG0 1
476 #define FW_ISO_CONTEXT_MATCH_TAG1 2
477 #define FW_ISO_CONTEXT_MATCH_TAG2 4
478 #define FW_ISO_CONTEXT_MATCH_TAG3 8
479 #define FW_ISO_CONTEXT_MATCH_ALL_TAGS 15
480
481 /*
482 * An iso buffer is just a set of pages mapped for DMA in the
483 * specified direction. Since the pages are to be used for DMA, they
484 * are not mapped into the kernel virtual address space. We store the
485 * DMA address in the page private. The helper function
486 * fw_iso_buffer_map() will map the pages into a given vma.
487 */
488 struct fw_iso_buffer {
489 enum dma_data_direction direction;
490 struct page **pages;
491 int page_count;
492 int page_count_mapped;
493 };
494
495 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
496 int page_count, enum dma_data_direction direction);
497 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
498 size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed);
499
500 struct fw_iso_context;
501 typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
502 u32 cycle, size_t header_length,
503 void *header, void *data);
504 typedef void (*fw_iso_mc_callback_t)(struct fw_iso_context *context,
505 dma_addr_t completed, void *data);
506
507 union fw_iso_callback {
508 fw_iso_callback_t sc;
509 fw_iso_mc_callback_t mc;
510 };
511
512 struct fw_iso_context {
513 struct fw_card *card;
514 struct work_struct work;
515 int type;
516 int channel;
517 int speed;
518 bool drop_overflow_headers;
519 size_t header_size;
520 union fw_iso_callback callback;
521 void *callback_data;
522 };
523
524 struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
525 int type, int channel, int speed, size_t header_size,
526 fw_iso_callback_t callback, void *callback_data);
527 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels);
528 int fw_iso_context_queue(struct fw_iso_context *ctx,
529 struct fw_iso_packet *packet,
530 struct fw_iso_buffer *buffer,
531 unsigned long payload);
532 void fw_iso_context_queue_flush(struct fw_iso_context *ctx);
533 int fw_iso_context_flush_completions(struct fw_iso_context *ctx);
534
535 /**
536 * fw_iso_context_schedule_flush_completions() - schedule work item to process isochronous context.
537 * @ctx: the isochronous context
538 *
539 * Schedule a work item on workqueue to process the isochronous context. The registered callback
540 * function is called by the worker when a queued packet buffer with the interrupt flag is
541 * completed, either after transmission in the IT context or after being filled in the IR context.
542 * The callback function is also called when the header buffer in the context becomes full, If it
543 * is required to process the context in the current context, fw_iso_context_flush_completions() is
544 * available instead.
545 *
546 * Context: Any context.
547 */
fw_iso_context_schedule_flush_completions(struct fw_iso_context * ctx)548 static inline void fw_iso_context_schedule_flush_completions(struct fw_iso_context *ctx)
549 {
550 queue_work(ctx->card->isoc_wq, &ctx->work);
551 }
552
553 int fw_iso_context_start(struct fw_iso_context *ctx,
554 int cycle, int sync, int tags);
555 int fw_iso_context_stop(struct fw_iso_context *ctx);
556 void fw_iso_context_destroy(struct fw_iso_context *ctx);
557 void fw_iso_resource_manage(struct fw_card *card, int generation,
558 u64 channels_mask, int *channel, int *bandwidth,
559 bool allocate);
560
561 extern struct workqueue_struct *fw_workqueue;
562
563 #endif /* _LINUX_FIREWIRE_H */
564