blob: b0d671db178a85b6ab98de29f05b994db99a8697 [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;
Gustavo A. R. Silvac38e7e22020-05-28 09:35:11 -050055 char data[];
Stefan Richter28646822010-07-27 10:26:33 +020056};
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;
Zheyu Ma829933e2021-04-03 06:58:36 +0000349 int ret;
Stefan Richter28646822010-07-27 10:26:33 +0200350
351 switch (cmd) {
Stefan Richterb5e47722010-07-27 10:28:30 +0200352 case NOSY_IOC_GET_STATS:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200353 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200354 stats.total_packet_count = client->buffer.total_packet_count;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200355 stats.lost_packet_count = client->buffer.lost_packet_count;
356 spin_unlock_irq(client_list_lock);
357
Stefan Richterc89db7b82010-07-22 11:56:38 +0200358 if (copy_to_user((void __user *) arg, &stats, sizeof stats))
Stefan Richter28646822010-07-27 10:26:33 +0200359 return -EFAULT;
360 else
361 return 0;
Stefan Richterb5e47722010-07-27 10:28:30 +0200362
Stefan Richter28646822010-07-27 10:26:33 +0200363 case NOSY_IOC_START:
Zheyu Ma829933e2021-04-03 06:58:36 +0000364 ret = -EBUSY;
Stefan Richter55e77c02010-07-22 11:56:38 +0200365 spin_lock_irq(client_list_lock);
Zheyu Ma829933e2021-04-03 06:58:36 +0000366 if (list_empty(&client->link)) {
367 list_add_tail(&client->link, &client->lynx->client_list);
368 ret = 0;
369 }
Stefan Richter55e77c02010-07-22 11:56:38 +0200370 spin_unlock_irq(client_list_lock);
371
Zheyu Ma829933e2021-04-03 06:58:36 +0000372 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200373
374 case NOSY_IOC_STOP:
Stefan Richter55e77c02010-07-22 11:56:38 +0200375 spin_lock_irq(client_list_lock);
376 list_del_init(&client->link);
377 spin_unlock_irq(client_list_lock);
378
Stefan Richter28646822010-07-27 10:26:33 +0200379 return 0;
380
381 case NOSY_IOC_FILTER:
Stefan Richterc7b2a992010-07-22 11:56:38 +0200382 spin_lock_irq(client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200383 client->tcode_mask = arg;
Stefan Richterc7b2a992010-07-22 11:56:38 +0200384 spin_unlock_irq(client_list_lock);
Stefan Richter55e77c02010-07-22 11:56:38 +0200385
Stefan Richter28646822010-07-27 10:26:33 +0200386 return 0;
387
388 default:
389 return -EINVAL;
390 /* Flush buffer, configure filter. */
391 }
392}
393
Stefan Richterb5e47722010-07-27 10:28:30 +0200394static const struct file_operations nosy_ops = {
Stefan Richterc7b2a992010-07-22 11:56:38 +0200395 .owner = THIS_MODULE,
396 .read = nosy_read,
397 .unlocked_ioctl = nosy_ioctl,
398 .poll = nosy_poll,
399 .open = nosy_open,
400 .release = nosy_release,
Stefan Richter28646822010-07-27 10:26:33 +0200401};
402
403#define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */
404
Stefan Richter28646822010-07-27 10:26:33 +0200405static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200406packet_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200407{
Stefan Richter28646822010-07-27 10:26:33 +0200408 struct client *client;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530409 u32 tcode_mask, tcode, timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200410 size_t length;
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530411 struct timespec64 ts64;
Stefan Richter28646822010-07-27 10:26:33 +0200412
413 /* FIXME: Also report rcv_speed. */
414
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200415 length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff;
416 tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf;
Stefan Richter28646822010-07-27 10:26:33 +0200417
Amitoj Kaur Chawla2ae4b6b2015-10-22 04:05:00 +0530418 ktime_get_real_ts64(&ts64);
419 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
420 lynx->rcv_buffer[0] = (__force __le32)timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200421
422 if (length == PHY_PACKET_SIZE)
423 tcode_mask = 1 << TCODE_PHY_PACKET;
424 else
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200425 tcode_mask = 1 << tcode;
Stefan Richter28646822010-07-27 10:26:33 +0200426
Stefan Richter685c3f82010-07-22 11:56:38 +0200427 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200428
Stefan Richterb5e47722010-07-27 10:28:30 +0200429 list_for_each_entry(client, &lynx->client_list, link)
Stefan Richter28646822010-07-27 10:26:33 +0200430 if (client->tcode_mask & tcode_mask)
Stefan Richterb5e47722010-07-27 10:28:30 +0200431 packet_buffer_put(&client->buffer,
Stefan Richter28646822010-07-27 10:26:33 +0200432 lynx->rcv_buffer, length + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200433
Stefan Richter685c3f82010-07-22 11:56:38 +0200434 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200435}
436
437static void
Stefan Richter685c3f82010-07-22 11:56:38 +0200438bus_reset_irq_handler(struct pcilynx *lynx)
Stefan Richter28646822010-07-27 10:26:33 +0200439{
Stefan Richter28646822010-07-27 10:26:33 +0200440 struct client *client;
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700441 struct timespec64 ts64;
442 u32 timestamp;
Stefan Richter28646822010-07-27 10:26:33 +0200443
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700444 ktime_get_real_ts64(&ts64);
445 timestamp = ts64.tv_nsec / NSEC_PER_USEC;
Stefan Richter28646822010-07-27 10:26:33 +0200446
Stefan Richter685c3f82010-07-22 11:56:38 +0200447 spin_lock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200448
Stefan Richterb5e47722010-07-27 10:28:30 +0200449 list_for_each_entry(client, &lynx->client_list, link)
Tina Ruchandani384fbb92016-03-20 22:59:11 -0700450 packet_buffer_put(&client->buffer, &timestamp, 4);
Stefan Richter28646822010-07-27 10:26:33 +0200451
Stefan Richter685c3f82010-07-22 11:56:38 +0200452 spin_unlock(&lynx->client_list_lock);
Stefan Richter28646822010-07-27 10:26:33 +0200453}
454
Stefan Richter28646822010-07-27 10:26:33 +0200455static irqreturn_t
456irq_handler(int irq, void *device)
457{
Stefan Richterb5e47722010-07-27 10:28:30 +0200458 struct pcilynx *lynx = device;
Stefan Richter28646822010-07-27 10:26:33 +0200459 u32 pci_int_status;
Stefan Richterb5e47722010-07-27 10:28:30 +0200460
461 pci_int_status = reg_read(lynx, PCI_INT_STATUS);
Stefan Richter28646822010-07-27 10:26:33 +0200462
Stefan Richter16547662010-07-22 11:56:38 +0200463 if (pci_int_status == ~0)
464 /* Card was ejected. */
465 return IRQ_NONE;
466
Stefan Richter28646822010-07-27 10:26:33 +0200467 if ((pci_int_status & PCI_INT_INT_PEND) == 0)
468 /* Not our interrupt, bail out quickly. */
469 return IRQ_NONE;
470
471 if ((pci_int_status & PCI_INT_P1394_INT) != 0) {
472 u32 link_int_status;
473
474 link_int_status = reg_read(lynx, LINK_INT_STATUS);
475 reg_write(lynx, LINK_INT_STATUS, link_int_status);
476
477 if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0)
Stefan Richter685c3f82010-07-22 11:56:38 +0200478 bus_reset_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200479 }
480
481 /* Clear the PCI_INT_STATUS register only after clearing the
482 * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will
483 * be set again immediately. */
484
485 reg_write(lynx, PCI_INT_STATUS, pci_int_status);
486
487 if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) {
Stefan Richter685c3f82010-07-22 11:56:38 +0200488 packet_irq_handler(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200489 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
490 }
491
492 return IRQ_HANDLED;
493}
494
495static void
496remove_card(struct pci_dev *dev)
497{
Stefan Richter424d66c2010-07-22 11:56:38 +0200498 struct pcilynx *lynx = pci_get_drvdata(dev);
499 struct client *client;
Stefan Richter28646822010-07-27 10:26:33 +0200500
Stefan Richter424d66c2010-07-22 11:56:38 +0200501 mutex_lock(&card_mutex);
502 list_del_init(&lynx->link);
503 misc_deregister(&lynx->misc);
504 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200505
506 reg_write(lynx, PCI_INT_ENABLE, 0);
507 free_irq(lynx->pci_device->irq, lynx);
508
Stefan Richter424d66c2010-07-22 11:56:38 +0200509 spin_lock_irq(&lynx->client_list_lock);
510 list_for_each_entry(client, &lynx->client_list, link)
511 wake_up_interruptible(&client->buffer.wait);
512 spin_unlock_irq(&lynx->client_list_lock);
513
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200514 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
515 lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus);
516 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
517 lynx->rcv_pcl, lynx->rcv_pcl_bus);
518 dma_free_coherent(&lynx->pci_device->dev, PAGE_SIZE, lynx->rcv_buffer,
519 lynx->rcv_buffer_bus);
Stefan Richter28646822010-07-27 10:26:33 +0200520
521 iounmap(lynx->registers);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200522 pci_disable_device(dev);
Stefan Richter424d66c2010-07-22 11:56:38 +0200523 lynx_put(lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200524}
525
526#define RCV_BUFFER_SIZE (16 * 1024)
527
Bill Pemberton03f94c02012-11-19 13:22:57 -0500528static int
Stefan Richter28646822010-07-27 10:26:33 +0200529add_card(struct pci_dev *dev, const struct pci_device_id *unused)
530{
Stefan Richterb5e47722010-07-27 10:28:30 +0200531 struct pcilynx *lynx;
Stefan Richter28646822010-07-27 10:26:33 +0200532 u32 p, end;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200533 int ret, i;
Stefan Richter28646822010-07-27 10:26:33 +0200534
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200535 if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
Stefan Richter7429b172010-07-22 11:56:38 +0200536 dev_err(&dev->dev,
537 "DMA address limits not supported for PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200538 return -ENXIO;
539 }
540 if (pci_enable_device(dev)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200541 dev_err(&dev->dev, "Failed to enable PCILynx hardware\n");
Stefan Richterb5e47722010-07-27 10:28:30 +0200542 return -ENXIO;
543 }
544 pci_set_master(dev);
Stefan Richter28646822010-07-27 10:26:33 +0200545
546 lynx = kzalloc(sizeof *lynx, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200547 if (lynx == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200548 dev_err(&dev->dev, "Failed to allocate control structure\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200549 ret = -ENOMEM;
550 goto fail_disable;
Stefan Richterb5e47722010-07-27 10:28:30 +0200551 }
552 lynx->pci_device = dev;
553 pci_set_drvdata(dev, lynx);
Stefan Richter28646822010-07-27 10:26:33 +0200554
555 spin_lock_init(&lynx->client_list_lock);
556 INIT_LIST_HEAD(&lynx->client_list);
Stefan Richter424d66c2010-07-22 11:56:38 +0200557 kref_init(&lynx->kref);
Stefan Richter28646822010-07-27 10:26:33 +0200558
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100559 lynx->registers = ioremap(pci_resource_start(dev, 0),
Stefan Richterb5e47722010-07-27 10:28:30 +0200560 PCILYNX_MAX_REGISTER);
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300561 if (lynx->registers == NULL) {
562 dev_err(&dev->dev, "Failed to map registers\n");
563 ret = -ENOMEM;
564 goto fail_deallocate_lynx;
565 }
Stefan Richter28646822010-07-27 10:26:33 +0200566
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200567 lynx->rcv_start_pcl = dma_alloc_coherent(&lynx->pci_device->dev,
568 sizeof(struct pcl),
569 &lynx->rcv_start_pcl_bus,
570 GFP_KERNEL);
571 lynx->rcv_pcl = dma_alloc_coherent(&lynx->pci_device->dev,
572 sizeof(struct pcl),
573 &lynx->rcv_pcl_bus, GFP_KERNEL);
574 lynx->rcv_buffer = dma_alloc_coherent(&lynx->pci_device->dev,
575 RCV_BUFFER_SIZE,
576 &lynx->rcv_buffer_bus, GFP_KERNEL);
Stefan Richterb5e47722010-07-27 10:28:30 +0200577 if (lynx->rcv_start_pcl == NULL ||
Stefan Richter28646822010-07-27 10:26:33 +0200578 lynx->rcv_pcl == NULL ||
Stefan Richterb5e47722010-07-27 10:28:30 +0200579 lynx->rcv_buffer == NULL) {
Stefan Richter7429b172010-07-22 11:56:38 +0200580 dev_err(&dev->dev, "Failed to allocate receive buffer\n");
Stefan Richterb6d9c122010-07-22 11:56:38 +0200581 ret = -ENOMEM;
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300582 goto fail_deallocate_buffers;
Stefan Richterb5e47722010-07-27 10:28:30 +0200583 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200584 lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus);
585 lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID);
586 lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID);
Stefan Richter28646822010-07-27 10:26:33 +0200587
Stefan Richterb5e47722010-07-27 10:28:30 +0200588 lynx->rcv_pcl->buffer[0].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200589 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044);
590 lynx->rcv_pcl->buffer[0].pointer =
591 cpu_to_le32(lynx->rcv_buffer_bus + 4);
Stefan Richter28646822010-07-27 10:26:33 +0200592 p = lynx->rcv_buffer_bus + 2048;
593 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE;
594 for (i = 1; p < end; i++, p += 2048) {
595 lynx->rcv_pcl->buffer[i].control =
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200596 cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048);
597 lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p);
Stefan Richter28646822010-07-27 10:26:33 +0200598 }
Stefan Richterfd8c8d42010-07-22 11:56:38 +0200599 lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF);
Stefan Richter28646822010-07-27 10:26:33 +0200600
Stefan Richterb5e47722010-07-27 10:28:30 +0200601 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
602 /* Fix buggy cards with autoboot pin not tied low: */
603 reg_write(lynx, DMA0_CHAN_CTRL, 0);
604 reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24);
Stefan Richter28646822010-07-27 10:26:33 +0200605
606#if 0
Stefan Richterb5e47722010-07-27 10:28:30 +0200607 /* now, looking for PHY register set */
608 if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
609 lynx->phyic.reg_1394a = 1;
610 PRINT(KERN_INFO, lynx->id,
611 "found 1394a conform PHY (using extended register set)");
612 lynx->phyic.vendor = get_phy_vendorid(lynx);
613 lynx->phyic.product = get_phy_productid(lynx);
614 } else {
615 lynx->phyic.reg_1394a = 0;
616 PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
617 }
Stefan Richter28646822010-07-27 10:26:33 +0200618#endif
619
620 /* Setup the general receive FIFO max size. */
621 reg_write(lynx, FIFO_SIZES, 255);
622
Stefan Richterb5e47722010-07-27 10:28:30 +0200623 reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
Stefan Richter28646822010-07-27 10:26:33 +0200624
Stefan Richterb5e47722010-07-27 10:28:30 +0200625 reg_write(lynx, LINK_INT_ENABLE,
Stefan Richter28646822010-07-27 10:26:33 +0200626 LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD |
627 LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK |
628 LINK_INT_AT_STUCK | LINK_INT_SNTRJ |
629 LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW |
630 LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW);
631
632 /* Disable the L flag in self ID packets. */
633 set_phy_reg(lynx, 4, 0);
634
635 /* Put this baby into snoop mode */
636 reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE);
637
638 run_pcl(lynx, lynx->rcv_start_pcl_bus, 0);
639
Stefan Richterb5e47722010-07-27 10:28:30 +0200640 if (request_irq(dev->irq, irq_handler, IRQF_SHARED,
641 driver_name, lynx)) {
Stefan Richter7429b172010-07-22 11:56:38 +0200642 dev_err(&dev->dev,
643 "Failed to allocate shared interrupt %d\n", dev->irq);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200644 ret = -EIO;
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300645 goto fail_deallocate_buffers;
Stefan Richterb5e47722010-07-27 10:28:30 +0200646 }
Stefan Richter28646822010-07-27 10:26:33 +0200647
648 lynx->misc.parent = &dev->dev;
649 lynx->misc.minor = MISC_DYNAMIC_MINOR;
650 lynx->misc.name = "nosy";
651 lynx->misc.fops = &nosy_ops;
Stefan Richter424d66c2010-07-22 11:56:38 +0200652
653 mutex_lock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200654 ret = misc_register(&lynx->misc);
655 if (ret) {
Stefan Richter7429b172010-07-22 11:56:38 +0200656 dev_err(&dev->dev, "Failed to register misc char device\n");
Stefan Richter424d66c2010-07-22 11:56:38 +0200657 mutex_unlock(&card_mutex);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200658 goto fail_free_irq;
Stefan Richterb5e47722010-07-27 10:28:30 +0200659 }
Stefan Richter424d66c2010-07-22 11:56:38 +0200660 list_add_tail(&lynx->link, &card_list);
661 mutex_unlock(&card_mutex);
Stefan Richter28646822010-07-27 10:26:33 +0200662
Stefan Richter7429b172010-07-22 11:56:38 +0200663 dev_info(&dev->dev,
664 "Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq);
Stefan Richter28646822010-07-27 10:26:33 +0200665
Stefan Richterb5e47722010-07-27 10:28:30 +0200666 return 0;
Stefan Richterb6d9c122010-07-22 11:56:38 +0200667
668fail_free_irq:
669 reg_write(lynx, PCI_INT_ENABLE, 0);
670 free_irq(lynx->pci_device->irq, lynx);
671
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300672fail_deallocate_buffers:
Stefan Richterb6d9c122010-07-22 11:56:38 +0200673 if (lynx->rcv_start_pcl)
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200674 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
675 lynx->rcv_start_pcl,
676 lynx->rcv_start_pcl_bus);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200677 if (lynx->rcv_pcl)
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200678 dma_free_coherent(&lynx->pci_device->dev, sizeof(struct pcl),
679 lynx->rcv_pcl, lynx->rcv_pcl_bus);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200680 if (lynx->rcv_buffer)
Christophe JAILLET01d12a62021-06-13 15:27:43 +0200681 dma_free_coherent(&lynx->pci_device->dev, PAGE_SIZE,
682 lynx->rcv_buffer, lynx->rcv_buffer_bus);
Stefan Richterb6d9c122010-07-22 11:56:38 +0200683 iounmap(lynx->registers);
Alexey Khoroshilov6449e312016-09-25 00:19:05 +0300684
685fail_deallocate_lynx:
Stefan Richterb6d9c122010-07-22 11:56:38 +0200686 kfree(lynx);
687
688fail_disable:
689 pci_disable_device(dev);
690
691 return ret;
Stefan Richter28646822010-07-27 10:26:33 +0200692}
693
Bill Pemberton7eeb7412012-11-19 13:24:13 -0500694static struct pci_device_id pci_table[] = {
Stefan Richter28646822010-07-27 10:26:33 +0200695 {
Stefan Richterb5e47722010-07-27 10:28:30 +0200696 .vendor = PCI_VENDOR_ID_TI,
697 .device = PCI_DEVICE_ID_TI_PCILYNX,
698 .subvendor = PCI_ANY_ID,
699 .subdevice = PCI_ANY_ID,
Stefan Richter28646822010-07-27 10:26:33 +0200700 },
701 { } /* Terminating entry */
702};
703
Axel Linfe2af112012-04-03 10:07:01 +0800704MODULE_DEVICE_TABLE(pci, pci_table);
705
Stefan Richter28646822010-07-27 10:26:33 +0200706static struct pci_driver lynx_pci_driver = {
Stefan Richterb5e47722010-07-27 10:28:30 +0200707 .name = driver_name,
Stefan Richter28646822010-07-27 10:26:33 +0200708 .id_table = pci_table,
709 .probe = add_card,
Stefan Richterb5e47722010-07-27 10:28:30 +0200710 .remove = remove_card,
Stefan Richter28646822010-07-27 10:26:33 +0200711};
712
Axel Linfe2af112012-04-03 10:07:01 +0800713module_pci_driver(lynx_pci_driver);
714
Stefan Richterb5e47722010-07-27 10:28:30 +0200715MODULE_AUTHOR("Kristian Hoegsberg");
Stefan Richter28646822010-07-27 10:26:33 +0200716MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers");
717MODULE_LICENSE("GPL");