18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _FIREWIRE_CORE_H
38c2ecf20Sopenharmony_ci#define _FIREWIRE_CORE_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/compiler.h>
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
88c2ecf20Sopenharmony_ci#include <linux/fs.h>
98c2ecf20Sopenharmony_ci#include <linux/list.h>
108c2ecf20Sopenharmony_ci#include <linux/idr.h>
118c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
128c2ecf20Sopenharmony_ci#include <linux/rwsem.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/refcount.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct device;
198c2ecf20Sopenharmony_cistruct fw_card;
208c2ecf20Sopenharmony_cistruct fw_device;
218c2ecf20Sopenharmony_cistruct fw_iso_buffer;
228c2ecf20Sopenharmony_cistruct fw_iso_context;
238c2ecf20Sopenharmony_cistruct fw_iso_packet;
248c2ecf20Sopenharmony_cistruct fw_node;
258c2ecf20Sopenharmony_cistruct fw_packet;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* -card */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciextern __printf(2, 3)
318c2ecf20Sopenharmony_civoid fw_err(const struct fw_card *card, const char *fmt, ...);
328c2ecf20Sopenharmony_ciextern __printf(2, 3)
338c2ecf20Sopenharmony_civoid fw_notice(const struct fw_card *card, const char *fmt, ...);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* bitfields within the PHY registers */
368c2ecf20Sopenharmony_ci#define PHY_LINK_ACTIVE		0x80
378c2ecf20Sopenharmony_ci#define PHY_CONTENDER		0x40
388c2ecf20Sopenharmony_ci#define PHY_BUS_RESET		0x40
398c2ecf20Sopenharmony_ci#define PHY_EXTENDED_REGISTERS	0xe0
408c2ecf20Sopenharmony_ci#define PHY_BUS_SHORT_RESET	0x40
418c2ecf20Sopenharmony_ci#define PHY_INT_STATUS_BITS	0x3c
428c2ecf20Sopenharmony_ci#define PHY_ENABLE_ACCEL	0x02
438c2ecf20Sopenharmony_ci#define PHY_ENABLE_MULTI	0x01
448c2ecf20Sopenharmony_ci#define PHY_PAGE_SELECT		0xe0
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define BANDWIDTH_AVAILABLE_INITIAL	4915
478c2ecf20Sopenharmony_ci#define BROADCAST_CHANNEL_INITIAL	(1 << 31 | 31)
488c2ecf20Sopenharmony_ci#define BROADCAST_CHANNEL_VALID		(1 << 30)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define CSR_STATE_BIT_CMSTR	(1 << 8)
518c2ecf20Sopenharmony_ci#define CSR_STATE_BIT_ABDICATE	(1 << 10)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct fw_card_driver {
548c2ecf20Sopenharmony_ci	/*
558c2ecf20Sopenharmony_ci	 * Enable the given card with the given initial config rom.
568c2ecf20Sopenharmony_ci	 * This function is expected to activate the card, and either
578c2ecf20Sopenharmony_ci	 * enable the PHY or set the link_on bit and initiate a bus
588c2ecf20Sopenharmony_ci	 * reset.
598c2ecf20Sopenharmony_ci	 */
608c2ecf20Sopenharmony_ci	int (*enable)(struct fw_card *card,
618c2ecf20Sopenharmony_ci		      const __be32 *config_rom, size_t length);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	int (*read_phy_reg)(struct fw_card *card, int address);
648c2ecf20Sopenharmony_ci	int (*update_phy_reg)(struct fw_card *card, int address,
658c2ecf20Sopenharmony_ci			      int clear_bits, int set_bits);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/*
688c2ecf20Sopenharmony_ci	 * Update the config rom for an enabled card.  This function
698c2ecf20Sopenharmony_ci	 * should change the config rom that is presented on the bus
708c2ecf20Sopenharmony_ci	 * and initiate a bus reset.
718c2ecf20Sopenharmony_ci	 */
728c2ecf20Sopenharmony_ci	int (*set_config_rom)(struct fw_card *card,
738c2ecf20Sopenharmony_ci			      const __be32 *config_rom, size_t length);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	void (*send_request)(struct fw_card *card, struct fw_packet *packet);
768c2ecf20Sopenharmony_ci	void (*send_response)(struct fw_card *card, struct fw_packet *packet);
778c2ecf20Sopenharmony_ci	/* Calling cancel is valid once a packet has been submitted. */
788c2ecf20Sopenharmony_ci	int (*cancel_packet)(struct fw_card *card, struct fw_packet *packet);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/*
818c2ecf20Sopenharmony_ci	 * Allow the specified node ID to do direct DMA out and in of
828c2ecf20Sopenharmony_ci	 * host memory.  The card will disable this for all node when
838c2ecf20Sopenharmony_ci	 * a bus reset happens, so driver need to reenable this after
848c2ecf20Sopenharmony_ci	 * bus reset.  Returns 0 on success, -ENODEV if the card
858c2ecf20Sopenharmony_ci	 * doesn't support this, -ESTALE if the generation doesn't
868c2ecf20Sopenharmony_ci	 * match.
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	int (*enable_phys_dma)(struct fw_card *card,
898c2ecf20Sopenharmony_ci			       int node_id, int generation);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	u32 (*read_csr)(struct fw_card *card, int csr_offset);
928c2ecf20Sopenharmony_ci	void (*write_csr)(struct fw_card *card, int csr_offset, u32 value);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	struct fw_iso_context *
958c2ecf20Sopenharmony_ci	(*allocate_iso_context)(struct fw_card *card,
968c2ecf20Sopenharmony_ci				int type, int channel, size_t header_size);
978c2ecf20Sopenharmony_ci	void (*free_iso_context)(struct fw_iso_context *ctx);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	int (*start_iso)(struct fw_iso_context *ctx,
1008c2ecf20Sopenharmony_ci			 s32 cycle, u32 sync, u32 tags);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	int (*set_iso_channels)(struct fw_iso_context *ctx, u64 *channels);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	int (*queue_iso)(struct fw_iso_context *ctx,
1058c2ecf20Sopenharmony_ci			 struct fw_iso_packet *packet,
1068c2ecf20Sopenharmony_ci			 struct fw_iso_buffer *buffer,
1078c2ecf20Sopenharmony_ci			 unsigned long payload);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	void (*flush_queue_iso)(struct fw_iso_context *ctx);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	int (*flush_iso_completions)(struct fw_iso_context *ctx);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	int (*stop_iso)(struct fw_iso_context *ctx);
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_civoid fw_card_initialize(struct fw_card *card,
1178c2ecf20Sopenharmony_ci		const struct fw_card_driver *driver, struct device *device);
1188c2ecf20Sopenharmony_ciint fw_card_add(struct fw_card *card,
1198c2ecf20Sopenharmony_ci		u32 max_receive, u32 link_speed, u64 guid);
1208c2ecf20Sopenharmony_civoid fw_core_remove_card(struct fw_card *card);
1218c2ecf20Sopenharmony_ciint fw_compute_block_crc(__be32 *block);
1228c2ecf20Sopenharmony_civoid fw_schedule_bm_work(struct fw_card *card, unsigned long delay);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/* -cdev */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciextern const struct file_operations fw_device_ops;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_civoid fw_device_cdev_update(struct fw_device *device);
1298c2ecf20Sopenharmony_civoid fw_device_cdev_remove(struct fw_device *device);
1308c2ecf20Sopenharmony_civoid fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* -device */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciextern struct rw_semaphore fw_device_rwsem;
1368c2ecf20Sopenharmony_ciextern struct idr fw_device_idr;
1378c2ecf20Sopenharmony_ciextern int fw_cdev_major;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline struct fw_device *fw_device_get(struct fw_device *device)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	get_device(&device->device);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	return device;
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic inline void fw_device_put(struct fw_device *device)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	put_device(&device->device);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistruct fw_device *fw_device_get_by_devt(dev_t devt);
1528c2ecf20Sopenharmony_ciint fw_device_set_broadcast_channel(struct device *dev, void *gen);
1538c2ecf20Sopenharmony_civoid fw_node_event(struct fw_card *card, struct fw_node *node, int event);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/* -iso */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciint fw_iso_buffer_alloc(struct fw_iso_buffer *buffer, int page_count);
1598c2ecf20Sopenharmony_ciint fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card,
1608c2ecf20Sopenharmony_ci			  enum dma_data_direction direction);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* -topology */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cienum {
1668c2ecf20Sopenharmony_ci	FW_NODE_CREATED,
1678c2ecf20Sopenharmony_ci	FW_NODE_UPDATED,
1688c2ecf20Sopenharmony_ci	FW_NODE_DESTROYED,
1698c2ecf20Sopenharmony_ci	FW_NODE_LINK_ON,
1708c2ecf20Sopenharmony_ci	FW_NODE_LINK_OFF,
1718c2ecf20Sopenharmony_ci	FW_NODE_INITIATED_RESET,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistruct fw_node {
1758c2ecf20Sopenharmony_ci	u16 node_id;
1768c2ecf20Sopenharmony_ci	u8 color;
1778c2ecf20Sopenharmony_ci	u8 port_count;
1788c2ecf20Sopenharmony_ci	u8 link_on:1;
1798c2ecf20Sopenharmony_ci	u8 initiated_reset:1;
1808c2ecf20Sopenharmony_ci	u8 b_path:1;
1818c2ecf20Sopenharmony_ci	u8 phy_speed:2;	/* As in the self ID packet. */
1828c2ecf20Sopenharmony_ci	u8 max_speed:2;	/* Minimum of all phy-speeds on the path from the
1838c2ecf20Sopenharmony_ci			 * local node to this node. */
1848c2ecf20Sopenharmony_ci	u8 max_depth:4;	/* Maximum depth to any leaf node */
1858c2ecf20Sopenharmony_ci	u8 max_hops:4;	/* Max hops in this sub tree */
1868c2ecf20Sopenharmony_ci	refcount_t ref_count;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* For serializing node topology into a list. */
1898c2ecf20Sopenharmony_ci	struct list_head link;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* Upper layer specific data. */
1928c2ecf20Sopenharmony_ci	void *data;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	struct fw_node *ports[];
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic inline struct fw_node *fw_node_get(struct fw_node *node)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	refcount_inc(&node->ref_count);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return node;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic inline void fw_node_put(struct fw_node *node)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&node->ref_count))
2078c2ecf20Sopenharmony_ci		kfree(node);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_civoid fw_core_handle_bus_reset(struct fw_card *card, int node_id,
2118c2ecf20Sopenharmony_ci	int generation, int self_id_count, u32 *self_ids, bool bm_abdicate);
2128c2ecf20Sopenharmony_civoid fw_destroy_nodes(struct fw_card *card);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/*
2158c2ecf20Sopenharmony_ci * Check whether new_generation is the immediate successor of old_generation.
2168c2ecf20Sopenharmony_ci * Take counter roll-over at 255 (as per OHCI) into account.
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_cistatic inline bool is_next_generation(int new_generation, int old_generation)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	return (new_generation & 0xff) == ((old_generation + 1) & 0xff);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/* -transaction */
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci#define TCODE_LINK_INTERNAL		0xe
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci#define TCODE_IS_READ_REQUEST(tcode)	(((tcode) & ~1) == 4)
2298c2ecf20Sopenharmony_ci#define TCODE_IS_BLOCK_PACKET(tcode)	(((tcode) &  1) != 0)
2308c2ecf20Sopenharmony_ci#define TCODE_IS_LINK_INTERNAL(tcode)	((tcode) == TCODE_LINK_INTERNAL)
2318c2ecf20Sopenharmony_ci#define TCODE_IS_REQUEST(tcode)		(((tcode) &  2) == 0)
2328c2ecf20Sopenharmony_ci#define TCODE_IS_RESPONSE(tcode)	(((tcode) &  2) != 0)
2338c2ecf20Sopenharmony_ci#define TCODE_HAS_REQUEST_DATA(tcode)	(((tcode) & 12) != 4)
2348c2ecf20Sopenharmony_ci#define TCODE_HAS_RESPONSE_DATA(tcode)	(((tcode) & 12) != 0)
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define LOCAL_BUS 0xffc0
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/* OHCI-1394's default upper bound for physical DMA: 4 GB */
2398c2ecf20Sopenharmony_ci#define FW_MAX_PHYSICAL_RANGE		(1ULL << 32)
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_civoid fw_core_handle_request(struct fw_card *card, struct fw_packet *request);
2428c2ecf20Sopenharmony_civoid fw_core_handle_response(struct fw_card *card, struct fw_packet *packet);
2438c2ecf20Sopenharmony_ciint fw_get_response_length(struct fw_request *request);
2448c2ecf20Sopenharmony_civoid fw_fill_response(struct fw_packet *response, u32 *request_header,
2458c2ecf20Sopenharmony_ci		      int rcode, void *payload, size_t length);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define FW_PHY_CONFIG_NO_NODE_ID	-1
2488c2ecf20Sopenharmony_ci#define FW_PHY_CONFIG_CURRENT_GAP_COUNT	-1
2498c2ecf20Sopenharmony_civoid fw_send_phy_config(struct fw_card *card,
2508c2ecf20Sopenharmony_ci			int node_id, int generation, int gap_count);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic inline bool is_ping_packet(u32 *data)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	return (data[0] & 0xc0ffffff) == 0 && ~data[0] == data[1];
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#endif /* _FIREWIRE_CORE_H */
258