xref: /linux/include/linux/firewire.h (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
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 	u64 reset_jiffies;
92 
93 	struct {
94 		int current_tlabel;
95 		u64 tlabel_mask;
96 		struct list_head list;
97 		spinlock_t lock;
98 	} transactions;
99 
100 	struct {
101 		u32 hi;
102 		u32 lo;
103 		unsigned int cycles;
104 		unsigned int jiffies;
105 		spinlock_t lock;
106 	} split_timeout;
107 
108 	unsigned long long guid;
109 	unsigned max_receive;
110 	int link_speed;
111 	int config_rom_generation;
112 
113 	spinlock_t lock;
114 
115 	struct fw_node *local_node;
116 	struct fw_node *root_node;
117 	struct fw_node *irm_node;
118 	u8 color; /* must be u8 to match the definition in struct fw_node */
119 	int gap_count;
120 	bool beta_repeaters_present;
121 
122 	int index;
123 	struct list_head link;
124 
125 	struct delayed_work br_work; /* bus reset job */
126 	bool br_short;
127 
128 	struct delayed_work bm_work; /* bus manager job */
129 	int bm_retries;
130 	int bm_generation;
131 	int bm_node_id;
132 	bool bm_abdicate;
133 
134 	bool priority_budget_implemented;	/* controller feature */
135 	bool broadcast_channel_auto_allocated;	/* controller feature */
136 
137 	bool broadcast_channel_allocated;
138 	u32 broadcast_channel;
139 
140 	struct {
141 		__be32 buffer[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4];
142 		spinlock_t lock;
143 	} topology_map;
144 
145 	__be32 maint_utility_register;
146 
147 	struct workqueue_struct *isoc_wq;
148 	struct workqueue_struct *async_wq;
149 };
150 
151 static inline struct fw_card *fw_card_get(struct fw_card *card)
152 {
153 	kref_get(&card->kref);
154 
155 	return card;
156 }
157 
158 void fw_card_release(struct kref *kref);
159 
160 static inline void fw_card_put(struct fw_card *card)
161 {
162 	kref_put(&card->kref, fw_card_release);
163 }
164 
165 int fw_card_read_cycle_time(struct fw_card *card, u32 *cycle_time);
166 
167 struct fw_attribute_group {
168 	struct attribute_group *groups[2];
169 	struct attribute_group group;
170 	struct attribute *attrs[13];
171 };
172 
173 enum fw_device_quirk {
174 	// See afa1282a35d3 ("firewire: core: check for 1394a compliant IRM, fix inaccessibility of Sony camcorder").
175 	FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY = BIT(0),
176 
177 	// See a509e43ff338 ("firewire: core: fix unstable I/O with Canon camcorder").
178 	FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER = BIT(1),
179 
180 	// MOTU Audio Express transfers acknowledge packet with 0x10 for pending state.
181 	FW_DEVICE_QUIRK_ACK_PACKET_WITH_INVALID_PENDING_CODE = BIT(2),
182 
183 	// TASCAM FW-1082/FW-1804/FW-1884 often freezes when receiving S400 packets.
184 	FW_DEVICE_QUIRK_UNSTABLE_AT_S400 = BIT(3),
185 };
186 
187 enum fw_device_state {
188 	FW_DEVICE_INITIALIZING,
189 	FW_DEVICE_RUNNING,
190 	FW_DEVICE_GONE,
191 	FW_DEVICE_SHUTDOWN,
192 };
193 
194 /*
195  * Note, fw_device.generation always has to be read before fw_device.node_id.
196  * Use SMP memory barriers to ensure this.  Otherwise requests will be sent
197  * to an outdated node_id if the generation was updated in the meantime due
198  * to a bus reset.
199  *
200  * Likewise, fw-core will take care to update .node_id before .generation so
201  * that whenever fw_device.generation is current WRT the actual bus generation,
202  * fw_device.node_id is guaranteed to be current too.
203  *
204  * The same applies to fw_device.card->node_id vs. fw_device.generation.
205  *
206  * fw_device.config_rom and fw_device.config_rom_length may be accessed during
207  * the lifetime of any fw_unit belonging to the fw_device, before device_del()
208  * was called on the last fw_unit.  Alternatively, they may be accessed while
209  * holding fw_device_rwsem.
210  */
211 struct fw_device {
212 	atomic_t state;
213 	struct fw_node *node;
214 	int node_id;
215 	int generation;
216 	unsigned max_speed;
217 	struct fw_card *card;
218 	struct device device;
219 
220 	// A set of enum fw_device_quirk.
221 	int quirks;
222 
223 	struct mutex client_list_mutex;
224 	struct list_head client_list;
225 
226 	const u32 *config_rom;
227 	size_t config_rom_length;
228 	int config_rom_retries;
229 	unsigned is_local:1;
230 	unsigned max_rec:4;
231 	unsigned cmc:1;
232 	unsigned irmc:1;
233 	unsigned bc_implemented:2;
234 
235 	work_func_t workfn;
236 	struct delayed_work work;
237 	struct fw_attribute_group attribute_group;
238 };
239 
240 #define fw_device(dev)	container_of_const(dev, struct fw_device, device)
241 
242 static inline int fw_device_is_shutdown(struct fw_device *device)
243 {
244 	return atomic_read(&device->state) == FW_DEVICE_SHUTDOWN;
245 }
246 
247 int fw_device_enable_phys_dma(struct fw_device *device);
248 
249 /*
250  * fw_unit.directory must not be accessed after device_del(&fw_unit.device).
251  */
252 struct fw_unit {
253 	struct device device;
254 	const u32 *directory;
255 	struct fw_attribute_group attribute_group;
256 };
257 
258 #define fw_unit(dev)	container_of_const(dev, struct fw_unit, device)
259 
260 static inline struct fw_unit *fw_unit_get(struct fw_unit *unit)
261 {
262 	get_device(&unit->device);
263 
264 	return unit;
265 }
266 
267 static inline void fw_unit_put(struct fw_unit *unit)
268 {
269 	put_device(&unit->device);
270 }
271 
272 #define fw_parent_device(unit)	fw_device(unit->device.parent)
273 
274 struct ieee1394_device_id;
275 
276 struct fw_driver {
277 	struct device_driver driver;
278 	int (*probe)(struct fw_unit *unit, const struct ieee1394_device_id *id);
279 	/* Called when the parent device sits through a bus reset. */
280 	void (*update)(struct fw_unit *unit);
281 	void (*remove)(struct fw_unit *unit);
282 	const struct ieee1394_device_id *id_table;
283 };
284 
285 struct fw_packet;
286 struct fw_request;
287 
288 typedef void (*fw_packet_callback_t)(struct fw_packet *packet,
289 				     struct fw_card *card, int status);
290 typedef void (*fw_transaction_callback_t)(struct fw_card *card, int rcode,
291 					  void *data, size_t length,
292 					  void *callback_data);
293 typedef void (*fw_transaction_callback_with_tstamp_t)(struct fw_card *card, int rcode,
294 					u32 request_tstamp, u32 response_tstamp, void *data,
295 					size_t length, void *callback_data);
296 
297 union fw_transaction_callback {
298 	fw_transaction_callback_t without_tstamp;
299 	fw_transaction_callback_with_tstamp_t with_tstamp;
300 };
301 
302 /*
303  * This callback handles an inbound request subaction.  It is called in
304  * RCU read-side context, therefore must not sleep.
305  *
306  * The callback should not initiate outbound request subactions directly.
307  * Otherwise there is a danger of recursion of inbound and outbound
308  * transactions from and to the local node.
309  *
310  * The callback is responsible that fw_send_response() is called on the @request, except for FCP
311  * registers for which the core takes care of that.
312  */
313 typedef void (*fw_address_callback_t)(struct fw_card *card,
314 				      struct fw_request *request,
315 				      int tcode, int destination, int source,
316 				      int generation,
317 				      unsigned long long offset,
318 				      void *data, size_t length,
319 				      void *callback_data);
320 
321 struct fw_packet {
322 	int speed;
323 	int generation;
324 	u32 header[4];
325 	size_t header_length;
326 	void *payload;
327 	size_t payload_length;
328 	dma_addr_t payload_bus;
329 	bool payload_mapped;
330 	u32 timestamp;
331 
332 	/*
333 	 * This callback is called when the packet transmission has completed.
334 	 * For successful transmission, the status code is the ack received
335 	 * from the destination.  Otherwise it is one of the juju-specific
336 	 * rcodes:  RCODE_SEND_ERROR, _CANCELLED, _BUSY, _GENERATION, _NO_ACK.
337 	 * The callback can be called from workqueue and thus must never block.
338 	 */
339 	fw_packet_callback_t callback;
340 	int ack;
341 	struct list_head link;
342 	void *driver_data;
343 };
344 
345 struct fw_transaction {
346 	int node_id; /* The generation is implied; it is always the current. */
347 	int tlabel;
348 	struct list_head link;
349 	struct fw_card *card;
350 	bool is_split_transaction;
351 	struct timer_list split_timeout_timer;
352 	u32 split_timeout_cycle;
353 
354 	struct fw_packet packet;
355 
356 	/*
357 	 * The data passed to the callback is valid only during the
358 	 * callback.
359 	 */
360 	union fw_transaction_callback callback;
361 	bool with_tstamp;
362 	void *callback_data;
363 };
364 
365 struct fw_address_handler {
366 	u64 offset;
367 	u64 length;
368 	fw_address_callback_t address_callback;
369 	void *callback_data;
370 
371 	// Only for core functions.
372 	struct list_head link;
373 	struct kref kref;
374 	struct completion done;
375 };
376 
377 struct fw_address_region {
378 	u64 start;
379 	u64 end;
380 };
381 
382 extern const struct fw_address_region fw_high_memory_region;
383 
384 int fw_core_add_address_handler(struct fw_address_handler *handler,
385 				const struct fw_address_region *region);
386 void fw_core_remove_address_handler(struct fw_address_handler *handler);
387 void fw_send_response(struct fw_card *card,
388 		      struct fw_request *request, int rcode);
389 int fw_get_request_speed(struct fw_request *request);
390 u32 fw_request_get_timestamp(const struct fw_request *request);
391 
392 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
393 		int destination_id, int generation, int speed, unsigned long long offset,
394 		void *payload, size_t length, union fw_transaction_callback callback,
395 		bool with_tstamp, void *callback_data);
396 
397 /**
398  * fw_send_request() - submit a request packet for transmission to generate callback for response
399  *		       subaction without 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 without time
413  * stamp.
414  *
415  * The callback is invoked in the workqueue context in most cases. However, if an error is detected
416  * before queueing or the destination address refers to the local node, it is invoked in the
417  * current context instead.
418  */
419 static inline void fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
420 				   int destination_id, int generation, int speed,
421 				   unsigned long long offset, void *payload, size_t length,
422 				   fw_transaction_callback_t callback, void *callback_data)
423 {
424 	union fw_transaction_callback cb = {
425 		.without_tstamp = callback,
426 	};
427 	__fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
428 			  length, cb, false, callback_data);
429 }
430 
431 /**
432  * fw_send_request_with_tstamp() - submit a request packet for transmission to generate callback for
433  *				   response with time stamp.
434  * @card:		interface to send the request at
435  * @t:			transaction instance to which the request belongs
436  * @tcode:		transaction code
437  * @destination_id:	destination node ID, consisting of bus_ID and phy_ID
438  * @generation:		bus generation in which request and response are valid
439  * @speed:		transmission speed
440  * @offset:		48bit wide offset into destination's address space
441  * @payload:		data payload for the request subaction
442  * @length:		length of the payload, in bytes
443  * @callback:		function to be called when the transaction is completed
444  * @callback_data:	data to be passed to the transaction completion callback
445  *
446  * A variation of __fw_send_request() to generate callback for response subaction with time stamp.
447  *
448  * The callback is invoked in the workqueue context in most cases. However, if an error is detected
449  * before queueing or the destination address refers to the local node, it is invoked in the current
450  * context instead.
451  */
452 static inline void fw_send_request_with_tstamp(struct fw_card *card, struct fw_transaction *t,
453 	int tcode, int destination_id, int generation, int speed, unsigned long long offset,
454 	void *payload, size_t length, fw_transaction_callback_with_tstamp_t callback,
455 	void *callback_data)
456 {
457 	union fw_transaction_callback cb = {
458 		.with_tstamp = callback,
459 	};
460 	__fw_send_request(card, t, tcode, destination_id, generation, speed, offset, payload,
461 			  length, cb, true, callback_data);
462 }
463 
464 int fw_cancel_transaction(struct fw_card *card,
465 			  struct fw_transaction *transaction);
466 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
467 		       int generation, int speed, unsigned long long offset,
468 		       void *payload, size_t length);
469 const char *fw_rcode_string(int rcode);
470 
471 static inline int fw_stream_packet_destination_id(int tag, int channel, int sy)
472 {
473 	return tag << 14 | channel << 8 | sy;
474 }
475 
476 void fw_schedule_bus_reset(struct fw_card *card, bool delayed,
477 			   bool short_reset);
478 
479 struct fw_descriptor {
480 	struct list_head link;
481 	size_t length;
482 	u32 immediate;
483 	u32 key;
484 	const u32 *data;
485 };
486 
487 int fw_core_add_descriptor(struct fw_descriptor *desc);
488 void fw_core_remove_descriptor(struct fw_descriptor *desc);
489 
490 /*
491  * The iso packet format allows for an immediate header/payload part
492  * stored in 'header' immediately after the packet info plus an
493  * indirect payload part that is pointer to by the 'payload' field.
494  * Applications can use one or the other or both to implement simple
495  * low-bandwidth streaming (e.g. audio) or more advanced
496  * scatter-gather streaming (e.g. assembling video frame automatically).
497  */
498 struct fw_iso_packet {
499 	u16 payload_length;	/* Length of indirect payload		*/
500 	u32 interrupt:1;	/* Generate interrupt on this packet	*/
501 	u32 skip:1;		/* tx: Set to not send packet at all	*/
502 				/* rx: Sync bit, wait for matching sy	*/
503 	u32 tag:2;		/* tx: Tag in packet header		*/
504 	u32 sy:4;		/* tx: Sy in packet header		*/
505 	u32 header_length:8;	/* Size of immediate header		*/
506 	u32 header[];		/* tx: Top of 1394 isoch. data_block	*/
507 };
508 
509 #define FW_ISO_CONTEXT_TRANSMIT			0
510 #define FW_ISO_CONTEXT_RECEIVE			1
511 #define FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL	2
512 
513 #define FW_ISO_CONTEXT_MATCH_TAG0	 1
514 #define FW_ISO_CONTEXT_MATCH_TAG1	 2
515 #define FW_ISO_CONTEXT_MATCH_TAG2	 4
516 #define FW_ISO_CONTEXT_MATCH_TAG3	 8
517 #define FW_ISO_CONTEXT_MATCH_ALL_TAGS	15
518 
519 /*
520  * An iso buffer is just a set of pages mapped for DMA in the
521  * specified direction.  Since the pages are to be used for DMA, they
522  * are not mapped into the kernel virtual address space.  We store the
523  * DMA address in the page private. The helper function
524  * fw_iso_buffer_map() will map the pages into a given vma.
525  */
526 struct fw_iso_buffer {
527 	enum dma_data_direction direction;
528 	struct page **pages;
529 	int page_count;
530 	int page_count_mapped;
531 };
532 
533 int fw_iso_buffer_init(struct fw_iso_buffer *buffer, struct fw_card *card,
534 		       int page_count, enum dma_data_direction direction);
535 void fw_iso_buffer_destroy(struct fw_iso_buffer *buffer, struct fw_card *card);
536 size_t fw_iso_buffer_lookup(struct fw_iso_buffer *buffer, dma_addr_t completed);
537 
538 struct fw_iso_context;
539 typedef void (*fw_iso_callback_t)(struct fw_iso_context *context,
540 				  u32 cycle, size_t header_length,
541 				  void *header, void *data);
542 typedef void (*fw_iso_mc_callback_t)(struct fw_iso_context *context,
543 				     dma_addr_t completed, void *data);
544 
545 union fw_iso_callback {
546 	fw_iso_callback_t sc;
547 	fw_iso_mc_callback_t mc;
548 };
549 
550 struct fw_iso_context {
551 	struct fw_card *card;
552 	struct work_struct work;
553 	int type;
554 	int channel;
555 	int speed;
556 	bool drop_overflow_headers;
557 	size_t header_size;
558 	union fw_iso_callback callback;
559 	void *callback_data;
560 };
561 
562 struct fw_iso_context *fw_iso_context_create(struct fw_card *card,
563 		int type, int channel, int speed, size_t header_size,
564 		fw_iso_callback_t callback, void *callback_data);
565 int fw_iso_context_set_channels(struct fw_iso_context *ctx, u64 *channels);
566 int fw_iso_context_queue(struct fw_iso_context *ctx,
567 			 struct fw_iso_packet *packet,
568 			 struct fw_iso_buffer *buffer,
569 			 unsigned long payload);
570 void fw_iso_context_queue_flush(struct fw_iso_context *ctx);
571 int fw_iso_context_flush_completions(struct fw_iso_context *ctx);
572 
573 /**
574  * fw_iso_context_schedule_flush_completions() - schedule work item to process isochronous context.
575  * @ctx: the isochronous context
576  *
577  * Schedule a work item on workqueue to process the isochronous context. The registered callback
578  * function is called by the worker when a queued packet buffer with the interrupt flag is
579  * completed, either after transmission in the IT context or after being filled in the IR context.
580  * The callback function is also called when the header buffer in the context becomes full, If it
581  * is required to process the context in the current context, fw_iso_context_flush_completions() is
582  * available instead.
583  *
584  * Context: Any context.
585  */
586 static inline void fw_iso_context_schedule_flush_completions(struct fw_iso_context *ctx)
587 {
588 	queue_work(ctx->card->isoc_wq, &ctx->work);
589 }
590 
591 int fw_iso_context_start(struct fw_iso_context *ctx,
592 			 int cycle, int sync, int tags);
593 int fw_iso_context_stop(struct fw_iso_context *ctx);
594 void fw_iso_context_destroy(struct fw_iso_context *ctx);
595 void fw_iso_resource_manage(struct fw_card *card, int generation,
596 			    u64 channels_mask, int *channel, int *bandwidth,
597 			    bool allocate);
598 
599 extern struct workqueue_struct *fw_workqueue;
600 
601 #endif /* _LINUX_FIREWIRE_H */
602