blob: 6ca2f5ab6c57e8849dda47ad66b7d635341e50bf [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stefan Richterb5e47722010-07-27 10:28:30 +02002/*
3 * nosy - Snoop mode driver for TI PCILynx 1394 controllers
4 * Copyright (C) 2002-2007 Kristian Høgsberg
Stefan Richter28646822010-07-27 10:26:33 +02005 */
6
Stefan Richter7429b172010-07-22 11:56:38 +02007#include <linux/device.h>
Stefan Richter28646822010-07-27 10:26:33 +02008#include <linux/errno.h>
Stefan Richter28646822010-07-27 10:26:33 +02009#include <linux/fs.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020010#include <linux/init.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/kernel.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020014#include <linux/kref.h>
Stefan Richter28646822010-07-27 10:26:33 +020015#include <linux/miscdevice.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020016#include <linux/module.h>
Stefan Richter424d66c2010-07-22 11:56:38 +020017#include <linux/mutex.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020018#include <linux/pci.h>
19#include <linux/poll.h>
20#include <linux/sched.h> /* required for linux/wait.h */
21#include <linux/slab.h>
22#include <linux/spinlock.h>
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +053023#include <linux/time64.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020024#include <linux/timex.h>
25#include <linux/uaccess.h>
26#include <linux/wait.h>
santosh nayake894d1d2012-02-21 15:38:13 +053027#include <linux/dma-mapping.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Stefan Richterb5e47722010-07-27 10:28:30 +020029#include <asm/byteorder.h>
Stefan Richter28646822010-07-27 10:26:33 +020030
31#include "nosy.h"
32#include "nosy-user.h"
33
34#define TCODE_PHY_PACKET 0x10
35#define PCI_DEVICE_ID_TI_PCILYNX 0x8000
36
Stefan Richterb5e47722010-07-27 10:28:30 +020037static char driver_name[] = KBUILD_MODNAME;
Stefan Richter28646822010-07-27 10:26:33 +020038
Stefan Richter28646822010-07-27 10:26:33 +020039/* this is the physical layout of a PCL, its size is 128 bytes */
40struct pcl {
Stefan Richterfd8c8d42010-07-22 11:56:38 +020041 __le32 next;
42 __le32 async_error_next;
43 u32 user_data;
44 __le32 pcl_status;
45 __le32 remaining_transfer_count;
46 __le32 next_data_buffer;
47 struct {
48 __le32 control;
49 __le32 pointer;
50 } buffer[13];
51};
Stefan Richter28646822010-07-27 10:26:33 +020052
53struct packet {
Stefan Richterb5e47722010-07-27 10:28:30 +020054 unsigned int length;
Stefan Richter28646822010-07-27 10:26:33 +020055 char data[0];
56};
57
58struct packet_buffer {
59 char *data;
60 size_t capacity;
61 long total_packet_count, lost_packet_count;
62 atomic_t size;
Stefan Richterb5e47722010-07-27 10:28:30 +020063 struct packet *head, *tail;
64 wait_queue_head_t wait;
Stefan Richter28646822010-07-27 10:26:33 +020065};
66
67struct pcilynx {
68 struct pci_dev *pci_device;
Stefan Richterc89db7b82010-07-22 11:56:38 +020069 __iomem char *registers;
Stefan Richter28646822010-07-27 10:26:33 +020070
71 struct pcl *rcv_start_pcl, *rcv_pcl;
Stefan Richterfd8c8d42010-07-22 11:56:38 +020072 __le32 *rcv_buffer;
Stefan Richter28646822010-07-27 10:26:33 +020073
74 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus;
75
76 spinlock_t client_list_lock;
77 struct list_head client_list;
78
79 struct miscdevice misc;
Stefan Richter424d66c2010-07-22 11:56:38 +020080 struct list_head link;
81 struct kref kref;
Stefan Richter28646822010-07-27 10:26:33 +020082};
83
Stefan Richter424d66c2010-07-22 11:56:38 +020084static inline struct pcilynx *
85lynx_get(struct pcilynx *lynx)
86{
87 kref_get(&lynx->kref);
88
89 return lynx;
90}
91
92static void
93lynx_release(struct kref *kref)
94{
95 kfree(container_of(kref, struct pcilynx, kref));
96}
97
98static inline void
99lynx_put(struct pcilynx *lynx)
100{
101 kref_put(&lynx->kref, lynx_release);
102}
103
Stefan Richter28646822010-07-27 10:26:33 +0200104struct client {
105 struct pcilynx *lynx;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200106 u32 tcode_mask;
Stefan Richter28646822010-07-27 10:26:33 +0200107 struct packet_buffer buffer;
108 struct list_head link;
109};
110
Stefan Richter424d66c2010-07-22 11:56:38 +0200111static DEFINE_MUTEX(card_mutex);
112static LIST_HEAD(card_list);
Stefan Richter28646822010-07-27 10:26:33 +0200113
114static int
115packet_buffer_init(struct packet_buffer *buffer, size_t capacity)
116{
117 buffer->data = kmalloc(capacity, GFP_KERNEL);
118 if (buffer->data == NULL)
119 return -ENOMEM;
120 buffer->head = (struct packet *) buffer->data;
121 buffer->tail = (struct packet *) buffer->data;
122 buffer->capacity = capacity;
123 buffer->lost_packet_count = 0;
124 atomic_set(&buffer->size, 0);
Stefan Richterb5e47722010-07-27 10:28:30 +0200125 init_waitqueue_head(&buffer->wait);
Stefan Richter28646822010-07-27 10:26:33 +0200126
127 return 0;
128}
129
130static void
131packet_buffer_destroy(struct packet_buffer *buffer)
132{
133 kfree(buffer->data);
134}
135
136static int
Stefan Richterc89db7b82010-07-22 11:56:38 +0200137packet_buffer_get(struct client *client, char __user *data, size_t user_length)
Stefan Richter28646822010-07-27 10:26:33 +0200138{
Stefan Richter424d66c2010-07-22 11:56:38 +0200139 struct packet_buffer *buffer = &client->buffer;
Stefan Richter28646822010-07-27 10:26:33 +0200140 size_t length;
141 char *end;
142
143 if (wait_event_interruptible(buffer->wait,
Stefan Richter424d66c2010-07-22 11:56:38 +0200144 atomic_read(&buffer->size) > 0) ||
145 list_empty(&client->lynx->link))
Stefan Richter28646822010-07-27 10:26:33 +0200146 return -ERESTARTSYS;
147
Stefan Richter424d66c2010-07-22 11:56:38 +0200148 if (atomic_read(&buffer->size) == 0)
149 return -ENODEV;
150
Stefan Richter28646822010-07-27 10:26:33 +0200151 /* FIXME: Check length <= user_length. */
152
153 end = buffer->data + buffer->capacity;
154 length = buffer->head->length;
155
156 if (&buffer->head->data[length] < end) {
157 if (copy_to_user(data, buffer->head->data, length))
158 return -EFAULT;
159 buffer->head = (struct packet *) &buffer->head->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200160 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200161 size_t split = end - buffer->head->data;
162
163 if (copy_to_user(data, buffer->head->data, split))
164 return -EFAULT;
165 if (copy_to_user(data + split, buffer->data, length - split))
166 return -EFAULT;
167 buffer->head = (struct packet *) &buffer->data[length - split];
168 }
169
Stefan Richterb5e47722010-07-27 10:28:30 +0200170 /*
171 * Decrease buffer->size as the last thing, since this is what
Stefan Richter28646822010-07-27 10:26:33 +0200172 * keeps the interrupt from overwriting the packet we are
Stefan Richterb5e47722010-07-27 10:28:30 +0200173 * retrieving from the buffer.
174 */
175 atomic_sub(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200176
177 return length;
178}
179
180static void
181packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length)
182{
183 char *end;
184
185 buffer->total_packet_count++;
186
Stefan Richterb5e47722010-07-27 10:28:30 +0200187 if (buffer->capacity <
188 atomic_read(&buffer->size) + sizeof(struct packet) + length) {
Stefan Richter28646822010-07-27 10:26:33 +0200189 buffer->lost_packet_count++;
190 return;
191 }
192
193 end = buffer->data + buffer->capacity;
194 buffer->tail->length = length;
195
196 if (&buffer->tail->data[length] < end) {
197 memcpy(buffer->tail->data, data, length);
198 buffer->tail = (struct packet *) &buffer->tail->data[length];
Stefan Richterb5e47722010-07-27 10:28:30 +0200199 } else {
Stefan Richter28646822010-07-27 10:26:33 +0200200 size_t split = end - buffer->tail->data;
201
202 memcpy(buffer->tail->data, data, split);
203 memcpy(buffer->data, data + split, length - split);
204 buffer->tail = (struct packet *) &buffer->data[length - split];
205 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200206
Stefan Richter28646822010-07-27 10:26:33 +0200207 /* Finally, adjust buffer size and wake up userspace reader. */
208
Stefan Richterb5e47722010-07-27 10:28:30 +0200209 atomic_add(sizeof(struct packet) + length, &buffer->size);
Stefan Richter28646822010-07-27 10:26:33 +0200210 wake_up_interruptible(&buffer->wait);
211}
212
213static inline void
214reg_write(struct pcilynx *lynx, int offset, u32 data)
215{
Stefan Richterb5e47722010-07-27 10:28:30 +0200216 writel(data, lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200217}
218
219static inline u32
220reg_read(struct pcilynx *lynx, int offset)
221{
Stefan Richterb5e47722010-07-27 10:28:30 +0200222 return readl(lynx->registers + offset);
Stefan Richter28646822010-07-27 10:26:33 +0200223}
224
225static inline void
226reg_set_bits(struct pcilynx *lynx, int offset, u32 mask)
227{
Stefan Richterb5e47722010-07-27 10:28:30 +0200228 reg_write(lynx, offset, (reg_read(lynx, offset) | mask));
Stefan Richter28646822010-07-27 10:26:33 +0200229}
230
Stefan Richterb5e47722010-07-27 10:28:30 +0200231/*
232 * Maybe the pcl programs could be set up to just append data instead
233 * of using a whole packet.
234 */
235static inline void
236run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus,
237 int dmachan)
Stefan Richter28646822010-07-27 10:26:33 +0200238{
Stefan Richterb5e47722010-07-27 10:28:30 +0200239 reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus);
240 reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20,
241 DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK);
Stefan Richter28646822010-07-27 10:26:33 +0200242}
243
244static int
245set_phy_reg(struct pcilynx *lynx, int addr, int val)
246{
Stefan Richterb5e47722010-07-27 10:28:30 +0200247 if (addr > 15) {
Stefan Richter7429b172010-07-22 11:56:38 +0200248 dev_err(&lynx->pci_device->dev,
249 "PHY register address %d out of range\n", addr);
Stefan Richterb5e47722010-07-27 10:28:30 +0200250 return -1;
251 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200252 if (val > 0xff) {
Stefan Richter7429b172010-07-22 11:56:38 +0200253 dev_err(&lynx->pci_device->dev,
254 "PHY register value %d out of range\n", val);
Stefan Richterb5e47722010-07-27 10:28:30 +0200255 return -1;
256 }
Stefan Richterb5e47722010-07-27 10:28:30 +0200257 reg_write(lynx, LINK_PHY, LINK_PHY_WRITE |
Stefan Richter28646822010-07-27 10:26:33 +0200258 LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val));
259
Stefan Richterb5e47722010-07-27 10:28:30 +0200260 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200261}
262
Stefan Richter28646822010-07-27 10:26:33 +0200263static int
264nosy_open(struct inode *inode, struct file *file)
265{
266 int minor = iminor(inode);
Stefan Richter55e77c02010-07-22 11:56:38 +0200267 struct client *client;
Stefan Richter424d66c2010-07-22 11:56:38 +0200268 struct pcilynx *tmp, *lynx = NULL;
Stefan Richter28646822010-07-27 10:26:33 +0200269
Stefan Richter424d66c2010-07-22 11:56:38 +0200270 mutex_lock(&card_mutex);
271 list_for_each_entry(tmp, &card_list, link)
272 if (tmp->misc.minor == minor) {
273 lynx = lynx_get(tmp);
274 break;
275 }
276 mutex_unlock(&card_mutex);
277 if (lynx == NULL)
Stefan Richter28646822010-07-27 10:26:33 +0200278 return -ENODEV;
279
Stefan Richter55e77c02010-07-22 11:56:38 +0200280 client = kmalloc(sizeof *client, GFP_KERNEL);
281 if (client == NULL)
Stefan Richter424d66c2010-07-22 11:56:38 +0200282 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200283
284 client->tcode_mask = ~0;
Stefan Richter424d66c2010-07-22 11:56:38 +0200285 client->lynx = lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200286 INIT_LIST_HEAD(&client->link);
287
Stefan Richter424d66c2010-07-22 11:56:38 +0200288 if (packet_buffer_init(&client->buffer, 128 * 1024) < 0)
289 goto fail;
Stefan Richter55e77c02010-07-22 11:56:38 +0200290
291 file->private_data = client;
292
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300293 return stream_open(inode, file);
Stefan Richter424d66c2010-07-22 11:56:38 +0200294fail:
295 kfree(client);
296 lynx_put(lynx);
297
298 return -ENOMEM;
Stefan Richter28646822010-07-27 10:26:33 +0200299}
300
301static int
302nosy_release(struct inode *inode, struct file *file)
303{
Stefan Richter55e77c02010-07-22 11:56:38 +0200304 struct client *client = file->private_data;
Stefan Richter424d66c2010-07-22 11:56:38 +0200305 struct pcilynx *lynx = client->lynx;
Stefan Richter55e77c02010-07-22 11:56:38 +0200306
Stefan Richter424d66c2010-07-22 11:56:38 +0200307 spin_lock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200308 list_del_init(&client->link);
Stefan Richter424d66c2010-07-22 11:56:38 +0200309 spin_unlock_irq(&lynx->client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200310
311 packet_buffer_destroy(&client->buffer);
312 kfree(client);
Stefan Richter424d66c2010-07-22 11:56:38 +0200313 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200314
Stefan Richterb5e47722010-07-27 10:28:30 +0200315 return 0;
Stefan Richter28646822010-07-27 10:26:33 +0200316}
317
Al Viroafc9a422017-07-03 06:39:46 -0400318static __poll_t
Stefan Richter28646822010-07-27 10:26:33 +0200319nosy_poll(struct file *file, poll_table *pt)
320{
321 struct client *client = file->private_data;
Al Viroafc9a422017-07-03 06:39:46 -0400322 __poll_t ret = 0;
Stefan Richter28646822010-07-27 10:26:33 +0200323
324 poll_wait(file, &client->buffer.wait, pt);
325
326 if (atomic_read(&client->buffer.size) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800327 ret = EPOLLIN | EPOLLRDNORM;
Stefan Richter424d66c2010-07-22 11:56:38 +0200328
329 if (list_empty(&client->lynx->link))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800330 ret |= EPOLLHUP;
Stefan Richter424d66c2010-07-22 11:56:38 +0200331
332 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200333}
334
335static ssize_t
Stefan Richterc89db7b82010-07-22 11:56:38 +0200336nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset)
Stefan Richter28646822010-07-27 10:26:33 +0200337{
338 struct client *client = file->private_data;
339
Stefan Richter424d66c2010-07-22 11:56:38 +0200340 return packet_buffer_get(client, buffer, count);
Stefan Richter28646822010-07-27 10:26:33 +0200341}
342
Stefan Richterc7b2a992010-07-22 11:56:38 +0200343static long
344nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Stefan Richter28646822010-07-27 10:26:33 +0200345{
346 struct client *client = file->private_data;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200347 spinlock_t *client_list_lock = &client->lynx->client_list_lock;
Stefan Richterb5e47722010-07-27 10:28:30 +0200348 struct nosy_stats stats;
Stefan Richter28646822010-07-27 10:26:33 +0200349
350 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200351 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200352 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200353 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200354 stats.lost_packet_count = client->buffer.lost_packet_count;
355 spin_unlock_irq(client_list_lock);
356
Stefan Richterc89db7b82010-07-22 11:56:38 +0200357 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200358 return -EFAULT;
359 else
360 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200361
Stefan Richter28646822010-07-27 10:26:33 +0200362 case NOSY_IOC_START:
Stefan Richter55e77c02010-07-22 11:56:38 +0200363 spin_lock_irq(client_list_lock);
364 list_add_tail(&client->link, &client->lynx->client_list);
365 spin_unlock_irq(client_list_lock);
366
Stefan Richter28646822010-07-27 10:26:33 +0200367 return 0;
368
369 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200370 spin_lock_irq(client_list_lock);
371 list_del_init(&client->link);
372 spin_unlock_irq(client_list_lock);
373
Stefan Richter28646822010-07-27 10:26:33 +0200374 return 0;
375
376 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200377 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200378 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200379 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200380
Stefan Richter28646822010-07-27 10:26:33 +0200381 return 0;
382
383 default:
384 return -EINVAL;
385 /* Flush buffer, configure filter. */
386 }
387}
388
Stefan Richterb5e47722010-07-27 10:28:30 +0200389static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200390 .owner = THIS_MODULE,
391 .read = nosy_read,
392 .unlocked_ioctl = nosy_ioctl,
393 .poll = nosy_poll,
394 .open = nosy_open,
395 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200396};
397
398#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
399
Stefan Richter28646822010-07-27 10:26:33 +0200400static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200401packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200402{
Stefan Richter28646822010-07-27 10:26:33 +0200403 struct client *client;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530404 u32 tcode_mask, tcode, timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200405 size_t length;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530406 struct timespec64 ts64;
Stefan Richter28646822010-07-27 10:26:33 +0200407
408 /* FIXME: Also report rcv_speed. */
409
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200410 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
411 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
Stefan Richter28646822010-07-27 10:26:33 +0200412
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530413 ktime_get_real_ts64(&ts64);
414 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
415 lynx->rcv_buffer[0] = (__force __le32)timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200416
417 if (length == PHY_PACKET_SIZE)
418 tcode_mask = 1 << TCODE_PHY_PACKET;
419 else
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200420 tcode_mask = 1 << tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200421
Stefan Richter685c3f82010-07-22 11:56:38 +0200422 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200423
Stefan Richterb5e47722010-07-27 10:28:30 +0200424 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200425 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200426 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200427 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200428
Stefan Richter685c3f82010-07-22 11:56:38 +0200429 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200430}
431
432static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200433bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200434{
Stefan Richter28646822010-07-27 10:26:33 +0200435 struct client *client;
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700436 struct timespec64 ts64;
437 u32 timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200438
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700439 ktime_get_real_ts64(&ts64);
440 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
Stefan Richter28646822010-07-27 10:26:33 +0200441
Stefan Richter685c3f82010-07-22 11:56:38 +0200442 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200443
Stefan Richterb5e47722010-07-27 10:28:30 +0200444 list_for_each_entry(client, &lynx->client_list, link)
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700445 packet_buffer_put(&client->buffer, &timestamp, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200446
Stefan Richter685c3f82010-07-22 11:56:38 +0200447 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200448}
449
Stefan Richter28646822010-07-27 10:26:33 +0200450static irqreturn_t
451irq_handler(int irq, void *device)
452{
Stefan Richterb5e47722010-07-27 10:28:30 +0200453 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200454 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200455
456 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200457
Stefan Richter16547662010-07-22 11:56:38 +0200458 if (pci_int_status == ~0)
459 /* Card was ejected. */
460 return IRQ_NONE;
461
Stefan Richter28646822010-07-27 10:26:33 +0200462 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
463 /* Not our interrupt, bail out quickly. */
464 return IRQ_NONE;
465
466 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
467 u32 link_int_status;
468
469 link_int_status = reg_read(lynx, LINK_INT_STATUS);
470 reg_write(lynx, LINK_INT_STATUS, link_int_status);
471
472 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200473 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200474 }
475
476 /* Clear the PCI_INT_STATUS register only after clearing the
477 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
478 * be set again immediately. */
479
480 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
481
482 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200483 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200484 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
485 }
486
487 return IRQ_HANDLED;
488}
489
490static void
491remove_card(struct pci_dev *dev)
492{
Stefan Richter424d66c2010-07-22 11:56:38 +0200493 struct pcilynx *lynx = pci_get_drvdata(dev);
494 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200495
Stefan Richter424d66c2010-07-22 11:56:38 +0200496 mutex_lock(&card_mutex);
497 list_del_init(&lynx->link);
498 misc_deregister(&lynx->misc);
499 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200500
501 reg_write(lynx, PCI_INT_ENABLE, 0);
502 free_irq(lynx->pci_device->irq, lynx);
503
Stefan Richter424d66c2010-07-22 11:56:38 +0200504 spin_lock_irq(&lynx->client_list_lock);
505 list_for_each_entry(client, &lynx->client_list, link)
506 wake_up_interruptible(&client->buffer.wait);
507 spin_unlock_irq(&lynx->client_list_lock);
508
Stefan Richterb5e47722010-07-27 10:28:30 +0200509 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200510 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
Stefan Richterb5e47722010-07-27 10:28:30 +0200511 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
Stefan Richter28646822010-07-27 10:26:33 +0200512 lynx->rcv_pcl, lynx->rcv_pcl_bus);
513 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
514 lynx->rcv_buffer, lynx->rcv_buffer_bus);
515
516 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200517 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200518 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200519}
520
521#define RCV_BUFFER_SIZE (16 * 1024)
522
Bill Pemberton03f94c02012-11-19 13:22:57 -0500523static int
Stefan Richter28646822010-07-27 10:26:33 +0200524add_card(struct pci_dev *dev, const struct pci_device_id *unused)
525{
Stefan Richterb5e47722010-07-27 10:28:30 +0200526 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200527 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200528 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200529
santosh nayake894d1d2012-02-21 15:38:13 +0530530 if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
Stefan Richter7429b172010-07-22 11:56:38 +0200531 dev_err(&dev->dev,
532 "DMA address limits not supported for PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200533 return -ENXIO;
534 }
535 if (pci_enable_device(dev)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200536 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200537 return -ENXIO;
538 }
539 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200540
541 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200542 if (lynx == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200543 dev_err(&dev->dev, "Failed to allocate control structure\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200544 ret = -ENOMEM;
545 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200546 }
547 lynx->pci_device = dev;
548 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200549
550 spin_lock_init(&lynx->client_list_lock);
551 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200552 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200553
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100554 lynx->registers = ioremap(pci_resource_start(dev, 0),
Stefan Richterb5e47722010-07-27 10:28:30 +0200555 PCILYNX_MAX_REGISTER);
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300556 if (lynx->registers == NULL) {
557 dev_err(&dev->dev, "Failed to map registers\n");
558 ret = -ENOMEM;
559 goto fail_deallocate_lynx;
560 }
Stefan Richter28646822010-07-27 10:26:33 +0200561
Stefan Richterb5e47722010-07-27 10:28:30 +0200562 lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device,
563 sizeof(struct pcl), &lynx->rcv_start_pcl_bus);
564 lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device,
565 sizeof(struct pcl), &lynx->rcv_pcl_bus);
566 lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device,
567 RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus);
568 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200569 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200570 lynx->rcv_buffer == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200571 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200572 ret = -ENOMEM;
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300573 goto fail_deallocate_buffers;
Stefan Richterb5e47722010-07-27 10:28:30 +0200574 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200575 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
576 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
577 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
Stefan Richter28646822010-07-27 10:26:33 +0200578
Stefan Richterb5e47722010-07-27 10:28:30 +0200579 lynx->rcv_pcl->buffer[0].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200580 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
581 lynx->rcv_pcl->buffer[0].pointer =
582 cpu_to_le32(lynx->rcv_buffer_bus + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200583 p = lynx->rcv_buffer_bus + 2048;
584 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
585 for (i = 1; p < end; i++, p += 2048) {
586 lynx->rcv_pcl->buffer[i].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200587 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
588 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
Stefan Richter28646822010-07-27 10:26:33 +0200589 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200590 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
Stefan Richter28646822010-07-27 10:26:33 +0200591
Stefan Richterb5e47722010-07-27 10:28:30 +0200592 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
593 /* Fix buggy cards with autoboot pin not tied low: */
594 reg_write(lynx, DMA0_CHAN_CTRL, 0);
595 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200596
597#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200598 /* now, looking for PHY register set */
599 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
600 lynx->phyic.reg_1394a = 1;
601 PRINT(KERN_INFO, lynx->id,
602 "found 1394a conform PHY (using extended register set)");
603 lynx->phyic.vendor = get_phy_vendorid(lynx);
604 lynx->phyic.product = get_phy_productid(lynx);
605 } else {
606 lynx->phyic.reg_1394a = 0;
607 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
608 }
Stefan Richter28646822010-07-27 10:26:33 +0200609#endif
610
611 /* Setup the general receive FIFO max size. */
612 reg_write(lynx, FIFO_SIZES, 255);
613
Stefan Richterb5e47722010-07-27 10:28:30 +0200614 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200615
Stefan Richterb5e47722010-07-27 10:28:30 +0200616 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200617 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
618 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
619 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
620 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
621 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
622
623 /* Disable the L flag in self ID packets. */
624 set_phy_reg(lynx, 4, 0);
625
626 /* Put this baby into snoop mode */
627 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
628
629 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
630
Stefan Richterb5e47722010-07-27 10:28:30 +0200631 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
632 driver_name, lynx)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200633 dev_err(&dev->dev,
634 "Failed to allocate shared interrupt %d\n", dev->irq);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200635 ret = -EIO;
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300636 goto fail_deallocate_buffers;
Stefan Richterb5e47722010-07-27 10:28:30 +0200637 }
Stefan Richter28646822010-07-27 10:26:33 +0200638
639 lynx->misc.parent = &dev->dev;
640 lynx->misc.minor = MISC_DYNAMIC_MINOR;
641 lynx->misc.name = "nosy";
642 lynx->misc.fops = &nosy_ops;
Stefan Richter424d66c2010-07-22 11:56:38 +0200643
644 mutex_lock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200645 ret = misc_register(&lynx->misc);
646 if (ret) {
Stefan Richter7429b172010-07-22 11:56:38 +0200647 dev_err(&dev->dev, "Failed to register misc char device\n");
Stefan Richter424d66c2010-07-22 11:56:38 +0200648 mutex_unlock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200649 goto fail_free_irq;
Stefan Richterb5e47722010-07-27 10:28:30 +0200650 }
Stefan Richter424d66c2010-07-22 11:56:38 +0200651 list_add_tail(&lynx->link, &card_list);
652 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200653
Stefan Richter7429b172010-07-22 11:56:38 +0200654 dev_info(&dev->dev,
655 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
Stefan Richter28646822010-07-27 10:26:33 +0200656
Stefan Richterb5e47722010-07-27 10:28:30 +0200657 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200658
659fail_free_irq:
660 reg_write(lynx, PCI_INT_ENABLE, 0);
661 free_irq(lynx->pci_device->irq, lynx);
662
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300663fail_deallocate_buffers:
Stefan Richterb6d9c122010-07-22 11:56:38 +0200664 if (lynx->rcv_start_pcl)
665 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
666 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
667 if (lynx->rcv_pcl)
668 pci_free_consistent(lynx->pci_device, sizeof(struct pcl),
669 lynx->rcv_pcl, lynx->rcv_pcl_bus);
670 if (lynx->rcv_buffer)
671 pci_free_consistent(lynx->pci_device, PAGE_SIZE,
672 lynx->rcv_buffer, lynx->rcv_buffer_bus);
673 iounmap(lynx->registers);
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300674
675fail_deallocate_lynx:
Stefan Richterb6d9c122010-07-22 11:56:38 +0200676 kfree(lynx);
677
678fail_disable:
679 pci_disable_device(dev);
680
681 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200682}
683
Bill Pemberton7eeb7412012-11-19 13:24:13 -0500684static struct pci_device_id pci_table[] = {
Stefan Richter28646822010-07-27 10:26:33 +0200685 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200686 .vendor = PCI_VENDOR_ID_TI,
687 .device = PCI_DEVICE_ID_TI_PCILYNX,
688 .subvendor = PCI_ANY_ID,
689 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200690 },
691 { } /* Terminating entry */
692};
693
Axel Linfe2af112012-04-03 10:07:01 +0800694MODULE_DEVICE_TABLE(pci, pci_table);
695
Stefan Richter28646822010-07-27 10:26:33 +0200696static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200697 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200698 .id_table = pci_table,
699 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200700 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200701};
702
Axel Linfe2af112012-04-03 10:07:01 +0800703module_pci_driver(lynx_pci_driver);
704
Stefan Richterb5e47722010-07-27 10:28:30 +0200705MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200706MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
707MODULE_LICENSE("GPL");