VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
1a59d1b8e05ea (Thomas Gleixner     2019-05-27 08:55:05 +0200   1) // SPDX-License-Identifier: GPL-2.0-or-later
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200   2) /*
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200   3)  * nosy - Snoop mode driver for TI PCILynx 1394 controllers
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200   4)  * Copyright (C) 2002-2007 Kristian Høgsberg
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200   5)  */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200   6) 
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200   7) #include <linux/device.h>
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200   8) #include <linux/errno.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200   9) #include <linux/fs.h>
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  10) #include <linux/init.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  11) #include <linux/interrupt.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  12) #include <linux/io.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  13) #include <linux/kernel.h>
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  14) #include <linux/kref.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  15) #include <linux/miscdevice.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  16) #include <linux/module.h>
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  17) #include <linux/mutex.h>
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  18) #include <linux/pci.h>
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  19) #include <linux/poll.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  20) #include <linux/sched.h> /* required for linux/wait.h */
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  21) #include <linux/slab.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  22) #include <linux/spinlock.h>
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530  23) #include <linux/time64.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  24) #include <linux/timex.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  25) #include <linux/uaccess.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  26) #include <linux/wait.h>
e894d1d7fd8cf (santosh nayak       2012-02-21 15:38:13 +0530  27) #include <linux/dma-mapping.h>
60063497a95e7 (Arun Sharma         2011-07-26 16:09:06 -0700  28) #include <linux/atomic.h>
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  29) #include <asm/byteorder.h>
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  30) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  31) #include "nosy.h"
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  32) #include "nosy-user.h"
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  33) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  34) #define TCODE_PHY_PACKET		0x10
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  35) #define PCI_DEVICE_ID_TI_PCILYNX	0x8000
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  36) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  37) static char driver_name[] = KBUILD_MODNAME;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  38) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  39) /* this is the physical layout of a PCL, its size is 128 bytes */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  40) struct pcl {
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  41) 	__le32 next;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  42) 	__le32 async_error_next;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  43) 	u32 user_data;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  44) 	__le32 pcl_status;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  45) 	__le32 remaining_transfer_count;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  46) 	__le32 next_data_buffer;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  47) 	struct {
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  48) 		__le32 control;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  49) 		__le32 pointer;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  50) 	} buffer[13];
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  51) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  52) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  53) struct packet {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  54) 	unsigned int length;
c38e7e212acdb (Gustavo A. R. Silva 2020-05-28 09:35:11 -0500  55) 	char data[];
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  56) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  57) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  58) struct packet_buffer {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  59) 	char *data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  60) 	size_t capacity;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  61) 	long total_packet_count, lost_packet_count;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  62) 	atomic_t size;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  63) 	struct packet *head, *tail;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200  64) 	wait_queue_head_t wait;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  65) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  66) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  67) struct pcilynx {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  68) 	struct pci_dev *pci_device;
c89db7b8bc88d (Stefan Richter      2010-07-22 11:56:38 +0200  69) 	__iomem char *registers;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  70) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  71) 	struct pcl *rcv_start_pcl, *rcv_pcl;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200  72) 	__le32 *rcv_buffer;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  73) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  74) 	dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  75) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  76) 	spinlock_t client_list_lock;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  77) 	struct list_head client_list;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  78) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  79) 	struct miscdevice misc;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  80) 	struct list_head link;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  81) 	struct kref kref;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  82) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200  83) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  84) static inline struct pcilynx *
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  85) lynx_get(struct pcilynx *lynx)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  86) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  87) 	kref_get(&lynx->kref);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  88) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  89) 	return lynx;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  90) }
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  91) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  92) static void
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  93) lynx_release(struct kref *kref)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  94) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  95) 	kfree(container_of(kref, struct pcilynx, kref));
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  96) }
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  97) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  98) static inline void
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200  99) lynx_put(struct pcilynx *lynx)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 100) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 101) 	kref_put(&lynx->kref, lynx_release);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 102) }
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 103) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 104) struct client {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 105) 	struct pcilynx *lynx;
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 106) 	u32 tcode_mask;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 107) 	struct packet_buffer buffer;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 108) 	struct list_head link;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 109) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 110) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 111) static DEFINE_MUTEX(card_mutex);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 112) static LIST_HEAD(card_list);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 113) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 114) static int
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 115) packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 116) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 117) 	buffer->data = kmalloc(capacity, GFP_KERNEL);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 118) 	if (buffer->data == NULL)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 119) 		return -ENOMEM;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 120) 	buffer->head = (struct packet *) buffer->data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 121) 	buffer->tail = (struct packet *) buffer->data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 122) 	buffer->capacity = capacity;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 123) 	buffer->lost_packet_count = 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 124) 	atomic_set(&buffer->size, 0);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 125) 	init_waitqueue_head(&buffer->wait);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 126) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 127) 	return 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 128) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 129) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 130) static void
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 131) packet_buffer_destroy(struct packet_buffer *buffer)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 132) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 133) 	kfree(buffer->data);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 134) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 135) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 136) static int
c89db7b8bc88d (Stefan Richter      2010-07-22 11:56:38 +0200 137) packet_buffer_get(struct client *client, char __user *data, size_t user_length)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 138) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 139) 	struct packet_buffer *buffer = &client->buffer;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 140) 	size_t length;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 141) 	char *end;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 142) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 143) 	if (wait_event_interruptible(buffer->wait,
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 144) 				     atomic_read(&buffer->size) > 0) ||
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 145) 				     list_empty(&client->lynx->link))
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 146) 		return -ERESTARTSYS;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 147) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 148) 	if (atomic_read(&buffer->size) == 0)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 149) 		return -ENODEV;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 150) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 151) 	/* FIXME: Check length <= user_length. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 152) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 153) 	end = buffer->data + buffer->capacity;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 154) 	length = buffer->head->length;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 155) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 156) 	if (&buffer->head->data[length] < end) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 157) 		if (copy_to_user(data, buffer->head->data, length))
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 158) 			return -EFAULT;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 159) 		buffer->head = (struct packet *) &buffer->head->data[length];
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 160) 	} else {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 161) 		size_t split = end - buffer->head->data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 162) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 163) 		if (copy_to_user(data, buffer->head->data, split))
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 164) 			return -EFAULT;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 165) 		if (copy_to_user(data + split, buffer->data, length - split))
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 166) 			return -EFAULT;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 167) 		buffer->head = (struct packet *) &buffer->data[length - split];
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 168) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 169) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 170) 	/*
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 171) 	 * Decrease buffer->size as the last thing, since this is what
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 172) 	 * keeps the interrupt from overwriting the packet we are
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 173) 	 * retrieving from the buffer.
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 174) 	 */
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 175) 	atomic_sub(sizeof(struct packet) + length, &buffer->size);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 176) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 177) 	return length;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 178) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 179) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 180) static void
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 181) packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 182) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 183) 	char *end;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 184) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 185) 	buffer->total_packet_count++;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 186) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 187) 	if (buffer->capacity <
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 188) 	    atomic_read(&buffer->size) + sizeof(struct packet) + length) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 189) 		buffer->lost_packet_count++;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 190) 		return;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 191) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 192) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 193) 	end = buffer->data + buffer->capacity;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 194) 	buffer->tail->length = length;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 195) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 196) 	if (&buffer->tail->data[length] < end) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 197) 		memcpy(buffer->tail->data, data, length);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 198) 		buffer->tail = (struct packet *) &buffer->tail->data[length];
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 199) 	} else {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 200) 		size_t split = end - buffer->tail->data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 201) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 202) 		memcpy(buffer->tail->data, data, split);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 203) 		memcpy(buffer->data, data + split, length - split);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 204) 		buffer->tail = (struct packet *) &buffer->data[length - split];
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 205) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 206) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 207) 	/* Finally, adjust buffer size and wake up userspace reader. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 208) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 209) 	atomic_add(sizeof(struct packet) + length, &buffer->size);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 210) 	wake_up_interruptible(&buffer->wait);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 211) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 212) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 213) static inline void
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 214) reg_write(struct pcilynx *lynx, int offset, u32 data)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 215) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 216) 	writel(data, lynx->registers + offset);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 217) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 218) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 219) static inline u32
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 220) reg_read(struct pcilynx *lynx, int offset)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 221) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 222) 	return readl(lynx->registers + offset);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 223) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 224) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 225) static inline void
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 226) reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 227) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 228) 	reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 229) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 230) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 231) /*
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 232)  * Maybe the pcl programs could be set up to just append data instead
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 233)  * of using a whole packet.
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 234)  */
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 235) static inline void
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 236) run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 237) 			   int dmachan)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 238) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 239) 	reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 240) 	reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 241) 		  DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 242) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 243) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 244) static int
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 245) set_phy_reg(struct pcilynx *lynx, int addr, int val)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 246) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 247) 	if (addr > 15) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 248) 		dev_err(&lynx->pci_device->dev,
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 249) 			"PHY register address %d out of range\n", addr);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 250) 		return -1;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 251) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 252) 	if (val > 0xff) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 253) 		dev_err(&lynx->pci_device->dev,
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 254) 			"PHY register value %d out of range\n", val);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 255) 		return -1;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 256) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 257) 	reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 258) 		  LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 259) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 260) 	return 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 261) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 262) 
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 263) static int
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 264) nosy_open(struct inode *inode, struct file *file)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 265) {
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 266) 	int minor = iminor(inode);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 267) 	struct client *client;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 268) 	struct pcilynx *tmp, *lynx = NULL;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 269) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 270) 	mutex_lock(&card_mutex);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 271) 	list_for_each_entry(tmp, &card_list, link)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 272) 		if (tmp->misc.minor == minor) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 273) 			lynx = lynx_get(tmp);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 274) 			break;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 275) 		}
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 276) 	mutex_unlock(&card_mutex);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 277) 	if (lynx == NULL)
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 278) 		return -ENODEV;
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 279) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 280) 	client = kmalloc(sizeof *client, GFP_KERNEL);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 281) 	if (client == NULL)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 282) 		goto fail;
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 283) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 284) 	client->tcode_mask = ~0;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 285) 	client->lynx = lynx;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 286) 	INIT_LIST_HEAD(&client->link);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 287) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 288) 	if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 289) 		goto fail;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 290) 
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 291) 	file->private_data = client;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 292) 
c5bf68fe0c86a (Kirill Smelkov      2019-03-26 23:51:19 +0300 293) 	return stream_open(inode, file);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 294) fail:
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 295) 	kfree(client);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 296) 	lynx_put(lynx);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 297) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 298) 	return -ENOMEM;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 299) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 300) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 301) static int
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 302) nosy_release(struct inode *inode, struct file *file)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 303) {
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 304) 	struct client *client = file->private_data;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 305) 	struct pcilynx *lynx = client->lynx;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 306) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 307) 	spin_lock_irq(&lynx->client_list_lock);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 308) 	list_del_init(&client->link);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 309) 	spin_unlock_irq(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 310) 
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 311) 	packet_buffer_destroy(&client->buffer);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 312) 	kfree(client);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 313) 	lynx_put(lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 314) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 315) 	return 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 316) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 317) 
afc9a42b7464f (Al Viro             2017-07-03 06:39:46 -0400 318) static __poll_t
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 319) nosy_poll(struct file *file, poll_table *pt)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 320) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 321) 	struct client *client = file->private_data;
afc9a42b7464f (Al Viro             2017-07-03 06:39:46 -0400 322) 	__poll_t ret = 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 323) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 324) 	poll_wait(file, &client->buffer.wait, pt);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 325) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 326) 	if (atomic_read(&client->buffer.size) > 0)
a9a08845e9acb (Linus Torvalds      2018-02-11 14:34:03 -0800 327) 		ret = EPOLLIN | EPOLLRDNORM;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 328) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 329) 	if (list_empty(&client->lynx->link))
a9a08845e9acb (Linus Torvalds      2018-02-11 14:34:03 -0800 330) 		ret |= EPOLLHUP;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 331) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 332) 	return ret;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 333) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 334) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 335) static ssize_t
c89db7b8bc88d (Stefan Richter      2010-07-22 11:56:38 +0200 336) nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 337) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 338) 	struct client *client = file->private_data;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 339) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 340) 	return packet_buffer_get(client, buffer, count);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 341) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 342) 
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 343) static long
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 344) nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 345) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 346) 	struct client *client = file->private_data;
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 347) 	spinlock_t *client_list_lock = &client->lynx->client_list_lock;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 348) 	struct nosy_stats stats;
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 349) 	int ret;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 350) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 351) 	switch (cmd) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 352) 	case NOSY_IOC_GET_STATS:
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 353) 		spin_lock_irq(client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 354) 		stats.total_packet_count = client->buffer.total_packet_count;
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 355) 		stats.lost_packet_count  = client->buffer.lost_packet_count;
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 356) 		spin_unlock_irq(client_list_lock);
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 357) 
c89db7b8bc88d (Stefan Richter      2010-07-22 11:56:38 +0200 358) 		if (copy_to_user((void __user *) arg, &stats, sizeof stats))
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 359) 			return -EFAULT;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 360) 		else
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 361) 			return 0;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 362) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 363) 	case NOSY_IOC_START:
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 364) 		ret = -EBUSY;
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 365) 		spin_lock_irq(client_list_lock);
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 366) 		if (list_empty(&client->link)) {
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 367) 			list_add_tail(&client->link, &client->lynx->client_list);
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 368) 			ret = 0;
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 369) 		}
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 370) 		spin_unlock_irq(client_list_lock);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 371) 
829933ef05a95 (Zheyu Ma            2021-04-03 06:58:36 +0000 372) 		return ret;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 373) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 374) 	case NOSY_IOC_STOP:
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 375) 		spin_lock_irq(client_list_lock);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 376) 		list_del_init(&client->link);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 377) 		spin_unlock_irq(client_list_lock);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 378) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 379) 		return 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 380) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 381) 	case NOSY_IOC_FILTER:
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 382) 		spin_lock_irq(client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 383) 		client->tcode_mask = arg;
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 384) 		spin_unlock_irq(client_list_lock);
55e77c06c6017 (Stefan Richter      2010-07-22 11:56:38 +0200 385) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 386) 		return 0;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 387) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 388) 	default:
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 389) 		return -EINVAL;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 390) 		/* Flush buffer, configure filter. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 391) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 392) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 393) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 394) static const struct file_operations nosy_ops = {
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 395) 	.owner =		THIS_MODULE,
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 396) 	.read =			nosy_read,
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 397) 	.unlocked_ioctl =	nosy_ioctl,
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 398) 	.poll =			nosy_poll,
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 399) 	.open =			nosy_open,
c7b2a99c66e7b (Stefan Richter      2010-07-22 11:56:38 +0200 400) 	.release =		nosy_release,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 401) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 402) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 403) #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 404) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 405) static void
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 406) packet_irq_handler(struct pcilynx *lynx)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 407) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 408) 	struct client *client;
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530 409) 	u32 tcode_mask, tcode, timestamp;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 410) 	size_t length;
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530 411) 	struct timespec64 ts64;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 412) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 413) 	/* FIXME: Also report rcv_speed. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 414) 
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 415) 	length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 416) 	tcode  = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 417) 
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530 418) 	ktime_get_real_ts64(&ts64);
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530 419) 	timestamp = ts64.tv_nsec / NSEC_PER_USEC;
2ae4b6b20e200 (Amitoj Kaur Chawla  2015-10-22 04:05:00 +0530 420) 	lynx->rcv_buffer[0] = (__force __le32)timestamp;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 421) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 422) 	if (length == PHY_PACKET_SIZE)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 423) 		tcode_mask = 1 << TCODE_PHY_PACKET;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 424) 	else
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 425) 		tcode_mask = 1 << tcode;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 426) 
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 427) 	spin_lock(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 428) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 429) 	list_for_each_entry(client, &lynx->client_list, link)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 430) 		if (client->tcode_mask & tcode_mask)
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 431) 			packet_buffer_put(&client->buffer,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 432) 					  lynx->rcv_buffer, length + 4);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 433) 
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 434) 	spin_unlock(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 435) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 436) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 437) static void
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 438) bus_reset_irq_handler(struct pcilynx *lynx)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 439) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 440) 	struct client *client;
384fbb96f9268 (Tina Ruchandani     2016-03-20 22:59:11 -0700 441) 	struct timespec64 ts64;
384fbb96f9268 (Tina Ruchandani     2016-03-20 22:59:11 -0700 442) 	u32    timestamp;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 443) 
384fbb96f9268 (Tina Ruchandani     2016-03-20 22:59:11 -0700 444) 	ktime_get_real_ts64(&ts64);
384fbb96f9268 (Tina Ruchandani     2016-03-20 22:59:11 -0700 445) 	timestamp = ts64.tv_nsec / NSEC_PER_USEC;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 446) 
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 447) 	spin_lock(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 448) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 449) 	list_for_each_entry(client, &lynx->client_list, link)
384fbb96f9268 (Tina Ruchandani     2016-03-20 22:59:11 -0700 450) 		packet_buffer_put(&client->buffer, &timestamp, 4);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 451) 
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 452) 	spin_unlock(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 453) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 454) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 455) static irqreturn_t
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 456) irq_handler(int irq, void *device)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 457) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 458) 	struct pcilynx *lynx = device;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 459) 	u32 pci_int_status;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 460) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 461) 	pci_int_status = reg_read(lynx, PCI_INT_STATUS);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 462) 
165476671f731 (Stefan Richter      2010-07-22 11:56:38 +0200 463) 	if (pci_int_status == ~0)
165476671f731 (Stefan Richter      2010-07-22 11:56:38 +0200 464) 		/* Card was ejected. */
165476671f731 (Stefan Richter      2010-07-22 11:56:38 +0200 465) 		return IRQ_NONE;
165476671f731 (Stefan Richter      2010-07-22 11:56:38 +0200 466) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 467) 	if ((pci_int_status & PCI_INT_INT_PEND) == 0)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 468) 		/* Not our interrupt, bail out quickly. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 469) 		return IRQ_NONE;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 470) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 471) 	if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 472) 		u32 link_int_status;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 473) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 474) 		link_int_status = reg_read(lynx, LINK_INT_STATUS);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 475) 		reg_write(lynx, LINK_INT_STATUS, link_int_status);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 476) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 477) 		if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 478) 			bus_reset_irq_handler(lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 479) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 480) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 481) 	/* Clear the PCI_INT_STATUS register only after clearing the
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 482) 	 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 483) 	 * be set again immediately. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 484) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 485) 	reg_write(lynx, PCI_INT_STATUS, pci_int_status);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 486) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 487) 	if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
685c3f80b6d88 (Stefan Richter      2010-07-22 11:56:38 +0200 488) 		packet_irq_handler(lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 489) 		run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 490) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 491) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 492) 	return IRQ_HANDLED;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 493) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 494) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 495) static void
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 496) remove_card(struct pci_dev *dev)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 497) {
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 498) 	struct pcilynx *lynx = pci_get_drvdata(dev);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 499) 	struct client *client;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 500) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 501) 	mutex_lock(&card_mutex);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 502) 	list_del_init(&lynx->link);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 503) 	misc_deregister(&lynx->misc);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 504) 	mutex_unlock(&card_mutex);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 505) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 506) 	reg_write(lynx, PCI_INT_ENABLE, 0);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 507) 	free_irq(lynx->pci_device->irq, lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 508) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 509) 	spin_lock_irq(&lynx->client_list_lock);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 510) 	list_for_each_entry(client, &lynx->client_list, link)
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 511) 		wake_up_interruptible(&client->buffer.wait);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 512) 	spin_unlock_irq(&lynx->client_list_lock);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 513) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 514) 	pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 515) 			    lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 516) 	pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 517) 			    lynx->rcv_pcl, lynx->rcv_pcl_bus);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 518) 	pci_free_consistent(lynx->pci_device, PAGE_SIZE,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 519) 			    lynx->rcv_buffer, lynx->rcv_buffer_bus);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 520) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 521) 	iounmap(lynx->registers);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 522) 	pci_disable_device(dev);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 523) 	lynx_put(lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 524) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 525) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 526) #define RCV_BUFFER_SIZE (16 * 1024)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 527) 
03f94c0f6215f (Bill Pemberton      2012-11-19 13:22:57 -0500 528) static int
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 529) add_card(struct pci_dev *dev, const struct pci_device_id *unused)
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 530) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 531) 	struct pcilynx *lynx;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 532) 	u32 p, end;
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 533) 	int ret, i;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 534) 
e894d1d7fd8cf (santosh nayak       2012-02-21 15:38:13 +0530 535) 	if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 536) 		dev_err(&dev->dev,
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 537) 		    "DMA address limits not supported for PCILynx hardware\n");
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 538) 		return -ENXIO;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 539) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 540) 	if (pci_enable_device(dev)) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 541) 		dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 542) 		return -ENXIO;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 543) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 544) 	pci_set_master(dev);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 545) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 546) 	lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 547) 	if (lynx == NULL) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 548) 		dev_err(&dev->dev, "Failed to allocate control structure\n");
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 549) 		ret = -ENOMEM;
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 550) 		goto fail_disable;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 551) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 552) 	lynx->pci_device = dev;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 553) 	pci_set_drvdata(dev, lynx);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 554) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 555) 	spin_lock_init(&lynx->client_list_lock);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 556) 	INIT_LIST_HEAD(&lynx->client_list);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 557) 	kref_init(&lynx->kref);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 558) 
4bdc0d676a643 (Christoph Hellwig   2020-01-06 09:43:50 +0100 559) 	lynx->registers = ioremap(pci_resource_start(dev, 0),
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 560) 					  PCILYNX_MAX_REGISTER);
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 561) 	if (lynx->registers == NULL) {
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 562) 		dev_err(&dev->dev, "Failed to map registers\n");
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 563) 		ret = -ENOMEM;
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 564) 		goto fail_deallocate_lynx;
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 565) 	}
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 566) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 567) 	lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 568) 				sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 569) 	lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 570) 				sizeof(struct pcl), &lynx->rcv_pcl_bus);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 571) 	lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 572) 				RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 573) 	if (lynx->rcv_start_pcl == NULL ||
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 574) 	    lynx->rcv_pcl == NULL ||
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 575) 	    lynx->rcv_buffer == NULL) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 576) 		dev_err(&dev->dev, "Failed to allocate receive buffer\n");
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 577) 		ret = -ENOMEM;
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 578) 		goto fail_deallocate_buffers;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 579) 	}
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 580) 	lynx->rcv_start_pcl->next	= cpu_to_le32(lynx->rcv_pcl_bus);
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 581) 	lynx->rcv_pcl->next		= cpu_to_le32(PCL_NEXT_INVALID);
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 582) 	lynx->rcv_pcl->async_error_next	= cpu_to_le32(PCL_NEXT_INVALID);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 583) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 584) 	lynx->rcv_pcl->buffer[0].control =
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 585) 			cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 586) 	lynx->rcv_pcl->buffer[0].pointer =
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 587) 			cpu_to_le32(lynx->rcv_buffer_bus + 4);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 588) 	p = lynx->rcv_buffer_bus + 2048;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 589) 	end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 590) 	for (i = 1; p < end; i++, p += 2048) {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 591) 		lynx->rcv_pcl->buffer[i].control =
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 592) 			cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 593) 		lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 594) 	}
fd8c8d46ca940 (Stefan Richter      2010-07-22 11:56:38 +0200 595) 	lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 596) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 597) 	reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 598) 	/* Fix buggy cards with autoboot pin not tied low: */
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 599) 	reg_write(lynx, DMA0_CHAN_CTRL, 0);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 600) 	reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 601) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 602) #if 0
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 603) 	/* now, looking for PHY register set */
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 604) 	if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 605) 		lynx->phyic.reg_1394a = 1;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 606) 		PRINT(KERN_INFO, lynx->id,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 607) 		      "found 1394a conform PHY (using extended register set)");
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 608) 		lynx->phyic.vendor = get_phy_vendorid(lynx);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 609) 		lynx->phyic.product = get_phy_productid(lynx);
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 610) 	} else {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 611) 		lynx->phyic.reg_1394a = 0;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 612) 		PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 613) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 614) #endif
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 615) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 616) 	/* Setup the general receive FIFO max size. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 617) 	reg_write(lynx, FIFO_SIZES, 255);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 618) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 619) 	reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 620) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 621) 	reg_write(lynx, LINK_INT_ENABLE,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 622) 		  LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 623) 		  LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 624) 		  LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 625) 		  LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 626) 		  LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 627) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 628) 	/* Disable the L flag in self ID packets. */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 629) 	set_phy_reg(lynx, 4, 0);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 630) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 631) 	/* Put this baby into snoop mode */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 632) 	reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 633) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 634) 	run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 635) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 636) 	if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 637) 			driver_name, lynx)) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 638) 		dev_err(&dev->dev,
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 639) 			"Failed to allocate shared interrupt %d\n", dev->irq);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 640) 		ret = -EIO;
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 641) 		goto fail_deallocate_buffers;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 642) 	}
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 643) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 644) 	lynx->misc.parent = &dev->dev;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 645) 	lynx->misc.minor = MISC_DYNAMIC_MINOR;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 646) 	lynx->misc.name = "nosy";
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 647) 	lynx->misc.fops = &nosy_ops;
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 648) 
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 649) 	mutex_lock(&card_mutex);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 650) 	ret = misc_register(&lynx->misc);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 651) 	if (ret) {
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 652) 		dev_err(&dev->dev, "Failed to register misc char device\n");
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 653) 		mutex_unlock(&card_mutex);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 654) 		goto fail_free_irq;
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 655) 	}
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 656) 	list_add_tail(&lynx->link, &card_list);
424d66cedae8b (Stefan Richter      2010-07-22 11:56:38 +0200 657) 	mutex_unlock(&card_mutex);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 658) 
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 659) 	dev_info(&dev->dev,
7429b17d30a19 (Stefan Richter      2010-07-22 11:56:38 +0200 660) 		 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 661) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 662) 	return 0;
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 663) 
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 664) fail_free_irq:
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 665) 	reg_write(lynx, PCI_INT_ENABLE, 0);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 666) 	free_irq(lynx->pci_device->irq, lynx);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 667) 
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 668) fail_deallocate_buffers:
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 669) 	if (lynx->rcv_start_pcl)
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 670) 		pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 671) 				lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 672) 	if (lynx->rcv_pcl)
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 673) 		pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 674) 				lynx->rcv_pcl, lynx->rcv_pcl_bus);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 675) 	if (lynx->rcv_buffer)
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 676) 		pci_free_consistent(lynx->pci_device, PAGE_SIZE,
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 677) 				lynx->rcv_buffer, lynx->rcv_buffer_bus);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 678) 	iounmap(lynx->registers);
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 679) 
6449e31ddebdc (Alexey Khoroshilov  2016-09-25 00:19:05 +0300 680) fail_deallocate_lynx:
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 681) 	kfree(lynx);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 682) 
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 683) fail_disable:
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 684) 	pci_disable_device(dev);
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 685) 
b6d9c125e6610 (Stefan Richter      2010-07-22 11:56:38 +0200 686) 	return ret;
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 687) }
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 688) 
7eeb741895ea8 (Bill Pemberton      2012-11-19 13:24:13 -0500 689) static struct pci_device_id pci_table[] = {
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 690) 	{
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 691) 		.vendor =    PCI_VENDOR_ID_TI,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 692) 		.device =    PCI_DEVICE_ID_TI_PCILYNX,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 693) 		.subvendor = PCI_ANY_ID,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 694) 		.subdevice = PCI_ANY_ID,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 695) 	},
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 696) 	{ }	/* Terminating entry */
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 697) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 698) 
fe2af11c220c7 (Axel Lin            2012-04-03 10:07:01 +0800 699) MODULE_DEVICE_TABLE(pci, pci_table);
fe2af11c220c7 (Axel Lin            2012-04-03 10:07:01 +0800 700) 
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 701) static struct pci_driver lynx_pci_driver = {
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 702) 	.name =		driver_name,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 703) 	.id_table =	pci_table,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 704) 	.probe =	add_card,
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 705) 	.remove =	remove_card,
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 706) };
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 707) 
fe2af11c220c7 (Axel Lin            2012-04-03 10:07:01 +0800 708) module_pci_driver(lynx_pci_driver);
fe2af11c220c7 (Axel Lin            2012-04-03 10:07:01 +0800 709) 
b5e47729043c9 (Stefan Richter      2010-07-27 10:28:30 +0200 710) MODULE_AUTHOR("Kristian Hoegsberg");
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 711) MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
286468210d83c (Stefan Richter      2010-07-27 10:26:33 +0200 712) MODULE_LICENSE("GPL");