]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rdma/hfi1/diag.c
3e08165104ba147bf1cc2f6f0a07b96c22bf5e50
[karo-tx-linux.git] / drivers / staging / rdma / hfi1 / diag.c
1 /*
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Corporation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Copyright(c) 2015 Intel Corporation.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 /*
52  * This file contains support for diagnostic functions.  It is accessed by
53  * opening the hfi1_diag device, normally minor number 129.  Diagnostic use
54  * of the chip may render the chip or board unusable until the driver
55  * is unloaded, or in some cases, until the system is rebooted.
56  *
57  * Accesses to the chip through this interface are not similar to going
58  * through the /sys/bus/pci resource mmap interface.
59  */
60
61 #include <linux/io.h>
62 #include <linux/pci.h>
63 #include <linux/poll.h>
64 #include <linux/vmalloc.h>
65 #include <linux/export.h>
66 #include <linux/fs.h>
67 #include <linux/uaccess.h>
68 #include <linux/module.h>
69 #include <rdma/ib_smi.h>
70 #include "hfi.h"
71 #include "device.h"
72 #include "common.h"
73 #include "trace.h"
74
75 #undef pr_fmt
76 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
77 #define snoop_dbg(fmt, ...) \
78         hfi1_cdbg(SNOOP, fmt, ##__VA_ARGS__)
79
80 /* Snoop option mask */
81 #define SNOOP_DROP_SEND         BIT(0)
82 #define SNOOP_USE_METADATA      BIT(1)
83
84 static u8 snoop_flags;
85
86 /*
87  * Extract packet length from LRH header.
88  * Why & 0x7FF? Because len is only 11 bits in case it wasn't 0'd we throw the
89  * bogus bits away. This is in Dwords so multiply by 4 to get size in bytes
90  */
91 #define HFI1_GET_PKT_LEN(x)      (((be16_to_cpu((x)->lrh[2]) & 0x7FF)) << 2)
92
93 enum hfi1_filter_status {
94         HFI1_FILTER_HIT,
95         HFI1_FILTER_ERR,
96         HFI1_FILTER_MISS
97 };
98
99 /* snoop processing functions */
100 rhf_rcv_function_ptr snoop_rhf_rcv_functions[8] = {
101         [RHF_RCV_TYPE_EXPECTED] = snoop_recv_handler,
102         [RHF_RCV_TYPE_EAGER]    = snoop_recv_handler,
103         [RHF_RCV_TYPE_IB]       = snoop_recv_handler,
104         [RHF_RCV_TYPE_ERROR]    = snoop_recv_handler,
105         [RHF_RCV_TYPE_BYPASS]   = snoop_recv_handler,
106         [RHF_RCV_TYPE_INVALID5] = process_receive_invalid,
107         [RHF_RCV_TYPE_INVALID6] = process_receive_invalid,
108         [RHF_RCV_TYPE_INVALID7] = process_receive_invalid
109 };
110
111 /* Snoop packet structure */
112 struct snoop_packet {
113         struct list_head list;
114         u32 total_len;
115         u8 data[];
116 };
117
118 /* Do not make these an enum or it will blow up the capture_md */
119 #define PKT_DIR_EGRESS 0x0
120 #define PKT_DIR_INGRESS 0x1
121
122 /* Packet capture metadata returned to the user with the packet. */
123 struct capture_md {
124         u8 port;
125         u8 dir;
126         u8 reserved[6];
127         union {
128                 u64 pbc;
129                 u64 rhf;
130         } u;
131 };
132
133 static atomic_t diagpkt_count = ATOMIC_INIT(0);
134 static struct cdev diagpkt_cdev;
135 static struct device *diagpkt_device;
136
137 static ssize_t diagpkt_write(struct file *fp, const char __user *data,
138                                  size_t count, loff_t *off);
139
140 static const struct file_operations diagpkt_file_ops = {
141         .owner = THIS_MODULE,
142         .write = diagpkt_write,
143         .llseek = noop_llseek,
144 };
145
146 /*
147  * This is used for communication with user space for snoop extended IOCTLs
148  */
149 struct hfi1_link_info {
150         __be64 node_guid;
151         u8 port_mode;
152         u8 port_state;
153         u16 link_speed_active;
154         u16 link_width_active;
155         u16 vl15_init;
156         u8 port_number;
157         /*
158          * Add padding to make this a full IB SMP payload. Note: changing the
159          * size of this structure will make the IOCTLs created with _IOWR
160          * change.
161          * Be sure to run tests on all IOCTLs when making changes to this
162          * structure.
163          */
164         u8 res[47];
165 };
166
167 /*
168  * This starts our ioctl sequence numbers *way* off from the ones
169  * defined in ib_core.
170  */
171 #define SNOOP_CAPTURE_VERSION 0x1
172
173 #define IB_IOCTL_MAGIC          0x1b /* See Documentation/ioctl-number.txt */
174 #define HFI1_SNOOP_IOC_MAGIC IB_IOCTL_MAGIC
175 #define HFI1_SNOOP_IOC_BASE_SEQ 0x80
176
177 #define HFI1_SNOOP_IOCGETLINKSTATE \
178         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ)
179 #define HFI1_SNOOP_IOCSETLINKSTATE \
180         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+1)
181 #define HFI1_SNOOP_IOCCLEARQUEUE \
182         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+2)
183 #define HFI1_SNOOP_IOCCLEARFILTER \
184         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+3)
185 #define HFI1_SNOOP_IOCSETFILTER \
186         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+4)
187 #define HFI1_SNOOP_IOCGETVERSION \
188         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+5)
189 #define HFI1_SNOOP_IOCSET_OPTS \
190         _IO(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+6)
191
192 /*
193  * These offsets +6/+7 could change, but these are already known and used
194  * IOCTL numbers so don't change them without a good reason.
195  */
196 #define HFI1_SNOOP_IOCGETLINKSTATE_EXTRA \
197         _IOWR(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+6, \
198                 struct hfi1_link_info)
199 #define HFI1_SNOOP_IOCSETLINKSTATE_EXTRA \
200         _IOWR(HFI1_SNOOP_IOC_MAGIC, HFI1_SNOOP_IOC_BASE_SEQ+7, \
201                 struct hfi1_link_info)
202
203 static int hfi1_snoop_open(struct inode *in, struct file *fp);
204 static ssize_t hfi1_snoop_read(struct file *fp, char __user *data,
205                                 size_t pkt_len, loff_t *off);
206 static ssize_t hfi1_snoop_write(struct file *fp, const char __user *data,
207                                  size_t count, loff_t *off);
208 static long hfi1_ioctl(struct file *fp, unsigned int cmd, unsigned long arg);
209 static unsigned int hfi1_snoop_poll(struct file *fp,
210                                         struct poll_table_struct *wait);
211 static int hfi1_snoop_release(struct inode *in, struct file *fp);
212
213 struct hfi1_packet_filter_command {
214         int opcode;
215         int length;
216         void *value_ptr;
217 };
218
219 /* Can't re-use PKT_DIR_*GRESS here because 0 means no packets for this */
220 #define HFI1_SNOOP_INGRESS 0x1
221 #define HFI1_SNOOP_EGRESS  0x2
222
223 enum hfi1_packet_filter_opcodes {
224         FILTER_BY_LID,
225         FILTER_BY_DLID,
226         FILTER_BY_MAD_MGMT_CLASS,
227         FILTER_BY_QP_NUMBER,
228         FILTER_BY_PKT_TYPE,
229         FILTER_BY_SERVICE_LEVEL,
230         FILTER_BY_PKEY,
231         FILTER_BY_DIRECTION,
232 };
233
234 static const struct file_operations snoop_file_ops = {
235         .owner = THIS_MODULE,
236         .open = hfi1_snoop_open,
237         .read = hfi1_snoop_read,
238         .unlocked_ioctl = hfi1_ioctl,
239         .poll = hfi1_snoop_poll,
240         .write = hfi1_snoop_write,
241         .release = hfi1_snoop_release
242 };
243
244 struct hfi1_filter_array {
245         int (*filter)(void *, void *, void *);
246 };
247
248 static int hfi1_filter_lid(void *ibhdr, void *packet_data, void *value);
249 static int hfi1_filter_dlid(void *ibhdr, void *packet_data, void *value);
250 static int hfi1_filter_mad_mgmt_class(void *ibhdr, void *packet_data,
251                                       void *value);
252 static int hfi1_filter_qp_number(void *ibhdr, void *packet_data, void *value);
253 static int hfi1_filter_ibpacket_type(void *ibhdr, void *packet_data,
254                                      void *value);
255 static int hfi1_filter_ib_service_level(void *ibhdr, void *packet_data,
256                                         void *value);
257 static int hfi1_filter_ib_pkey(void *ibhdr, void *packet_data, void *value);
258 static int hfi1_filter_direction(void *ibhdr, void *packet_data, void *value);
259
260 static struct hfi1_filter_array hfi1_filters[] = {
261         { hfi1_filter_lid },
262         { hfi1_filter_dlid },
263         { hfi1_filter_mad_mgmt_class },
264         { hfi1_filter_qp_number },
265         { hfi1_filter_ibpacket_type },
266         { hfi1_filter_ib_service_level },
267         { hfi1_filter_ib_pkey },
268         { hfi1_filter_direction },
269 };
270
271 #define HFI1_MAX_FILTERS        ARRAY_SIZE(hfi1_filters)
272 #define HFI1_DIAG_MINOR_BASE    129
273
274 static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name);
275
276 int hfi1_diag_add(struct hfi1_devdata *dd)
277 {
278         char name[16];
279         int ret = 0;
280
281         snprintf(name, sizeof(name), "%s_diagpkt%d", class_name(),
282                  dd->unit);
283         /*
284          * Do this for each device as opposed to the normal diagpkt
285          * interface which is one per host
286          */
287         ret = hfi1_snoop_add(dd, name);
288         if (ret)
289                 dd_dev_err(dd, "Unable to init snoop/capture device");
290
291         snprintf(name, sizeof(name), "%s_diagpkt", class_name());
292         if (atomic_inc_return(&diagpkt_count) == 1) {
293                 ret = hfi1_cdev_init(HFI1_DIAGPKT_MINOR, name,
294                                      &diagpkt_file_ops, &diagpkt_cdev,
295                                      &diagpkt_device, false);
296         }
297
298         return ret;
299 }
300
301 /* this must be called w/ dd->snoop_in_lock held */
302 static void drain_snoop_list(struct list_head *queue)
303 {
304         struct list_head *pos, *q;
305         struct snoop_packet *packet;
306
307         list_for_each_safe(pos, q, queue) {
308                 packet = list_entry(pos, struct snoop_packet, list);
309                 list_del(pos);
310                 kfree(packet);
311         }
312 }
313
314 static void hfi1_snoop_remove(struct hfi1_devdata *dd)
315 {
316         unsigned long flags = 0;
317
318         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
319         drain_snoop_list(&dd->hfi1_snoop.queue);
320         hfi1_cdev_cleanup(&dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev);
321         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
322 }
323
324 void hfi1_diag_remove(struct hfi1_devdata *dd)
325 {
326
327         hfi1_snoop_remove(dd);
328         if (atomic_dec_and_test(&diagpkt_count))
329                 hfi1_cdev_cleanup(&diagpkt_cdev, &diagpkt_device);
330         hfi1_cdev_cleanup(&dd->diag_cdev, &dd->diag_device);
331 }
332
333
334 /*
335  * Allocated structure shared between the credit return mechanism and
336  * diagpkt_send().
337  */
338 struct diagpkt_wait {
339         struct completion credits_returned;
340         int code;
341         atomic_t count;
342 };
343
344 /*
345  * When each side is finished with the structure, they call this.
346  * The last user frees the structure.
347  */
348 static void put_diagpkt_wait(struct diagpkt_wait *wait)
349 {
350         if (atomic_dec_and_test(&wait->count))
351                 kfree(wait);
352 }
353
354 /*
355  * Callback from the credit return code.  Set the complete, which
356  * will let diapkt_send() continue.
357  */
358 static void diagpkt_complete(void *arg, int code)
359 {
360         struct diagpkt_wait *wait = (struct diagpkt_wait *)arg;
361
362         wait->code = code;
363         complete(&wait->credits_returned);
364         put_diagpkt_wait(wait); /* finished with the structure */
365 }
366
367 /**
368  * diagpkt_send - send a packet
369  * @dp: diag packet descriptor
370  */
371 static ssize_t diagpkt_send(struct diag_pkt *dp)
372 {
373         struct hfi1_devdata *dd;
374         struct send_context *sc;
375         struct pio_buf *pbuf;
376         u32 *tmpbuf = NULL;
377         ssize_t ret = 0;
378         u32 pkt_len, total_len;
379         pio_release_cb credit_cb = NULL;
380         void *credit_arg = NULL;
381         struct diagpkt_wait *wait = NULL;
382         int trycount = 0;
383
384         dd = hfi1_lookup(dp->unit);
385         if (!dd || !(dd->flags & HFI1_PRESENT) || !dd->kregbase) {
386                 ret = -ENODEV;
387                 goto bail;
388         }
389         if (!(dd->flags & HFI1_INITTED)) {
390                 /* no hardware, freeze, etc. */
391                 ret = -ENODEV;
392                 goto bail;
393         }
394
395         if (dp->version != _DIAG_PKT_VERS) {
396                 dd_dev_err(dd, "Invalid version %u for diagpkt_write\n",
397                             dp->version);
398                 ret = -EINVAL;
399                 goto bail;
400         }
401
402         /* send count must be an exact number of dwords */
403         if (dp->len & 3) {
404                 ret = -EINVAL;
405                 goto bail;
406         }
407
408         /* there is only port 1 */
409         if (dp->port != 1) {
410                 ret = -EINVAL;
411                 goto bail;
412         }
413
414         /* need a valid context */
415         if (dp->sw_index >= dd->num_send_contexts) {
416                 ret = -EINVAL;
417                 goto bail;
418         }
419         /* can only use kernel contexts */
420         if (dd->send_contexts[dp->sw_index].type != SC_KERNEL) {
421                 ret = -EINVAL;
422                 goto bail;
423         }
424         /* must be allocated */
425         sc = dd->send_contexts[dp->sw_index].sc;
426         if (!sc) {
427                 ret = -EINVAL;
428                 goto bail;
429         }
430         /* must be enabled */
431         if (!(sc->flags & SCF_ENABLED)) {
432                 ret = -EINVAL;
433                 goto bail;
434         }
435
436         /* allocate a buffer and copy the data in */
437         tmpbuf = vmalloc(dp->len);
438         if (!tmpbuf) {
439                 ret = -ENOMEM;
440                 goto bail;
441         }
442
443         if (copy_from_user(tmpbuf,
444                            (const void __user *) (unsigned long) dp->data,
445                            dp->len)) {
446                 ret = -EFAULT;
447                 goto bail;
448         }
449
450         /*
451          * pkt_len is how much data we have to write, includes header and data.
452          * total_len is length of the packet in Dwords plus the PBC should not
453          * include the CRC.
454          */
455         pkt_len = dp->len >> 2;
456         total_len = pkt_len + 2; /* PBC + packet */
457
458         /* if 0, fill in a default */
459         if (dp->pbc == 0) {
460                 struct hfi1_pportdata *ppd = dd->pport;
461
462                 hfi1_cdbg(PKT, "Generating PBC");
463                 dp->pbc = create_pbc(ppd, 0, 0, 0, total_len);
464         } else {
465                 hfi1_cdbg(PKT, "Using passed in PBC");
466         }
467
468         hfi1_cdbg(PKT, "Egress PBC content is 0x%llx", dp->pbc);
469
470         /*
471          * The caller wants to wait until the packet is sent and to
472          * check for errors.  The best we can do is wait until
473          * the buffer credits are returned and check if any packet
474          * error has occurred.  If there are any late errors, this
475          * could miss it.  If there are other senders who generate
476          * an error, this may find it.  However, in general, it
477          * should catch most.
478          */
479         if (dp->flags & F_DIAGPKT_WAIT) {
480                 /* always force a credit return */
481                 dp->pbc |= PBC_CREDIT_RETURN;
482                 /* turn on credit return interrupts */
483                 sc_add_credit_return_intr(sc);
484                 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
485                 if (!wait) {
486                         ret = -ENOMEM;
487                         goto bail;
488                 }
489                 init_completion(&wait->credits_returned);
490                 atomic_set(&wait->count, 2);
491                 wait->code = PRC_OK;
492
493                 credit_cb = diagpkt_complete;
494                 credit_arg = wait;
495         }
496
497 retry:
498         pbuf = sc_buffer_alloc(sc, total_len, credit_cb, credit_arg);
499         if (!pbuf) {
500                 if (trycount == 0) {
501                         /* force a credit return and try again */
502                         sc_return_credits(sc);
503                         trycount = 1;
504                         goto retry;
505                 }
506                 /*
507                  * No send buffer means no credit callback.  Undo
508                  * the wait set-up that was done above.  We free wait
509                  * because the callback will never be called.
510                  */
511                 if (dp->flags & F_DIAGPKT_WAIT) {
512                         sc_del_credit_return_intr(sc);
513                         kfree(wait);
514                         wait = NULL;
515                 }
516                 ret = -ENOSPC;
517                 goto bail;
518         }
519
520         pio_copy(dd, pbuf, dp->pbc, tmpbuf, pkt_len);
521         /* no flush needed as the HW knows the packet size */
522
523         ret = sizeof(*dp);
524
525         if (dp->flags & F_DIAGPKT_WAIT) {
526                 /* wait for credit return */
527                 ret = wait_for_completion_interruptible(
528                                                 &wait->credits_returned);
529                 /*
530                  * If the wait returns an error, the wait was interrupted,
531                  * e.g. with a ^C in the user program.  The callback is
532                  * still pending.  This is OK as the wait structure is
533                  * kmalloc'ed and the structure will free itself when
534                  * all users are done with it.
535                  *
536                  * A context disable occurs on a send context restart, so
537                  * include that in the list of errors below to check for.
538                  * NOTE: PRC_FILL_ERR is at best informational and cannot
539                  * be depended on.
540                  */
541                 if (!ret && (((wait->code & PRC_STATUS_ERR)
542                                 || (wait->code & PRC_FILL_ERR)
543                                 || (wait->code & PRC_SC_DISABLE))))
544                         ret = -EIO;
545
546                 put_diagpkt_wait(wait); /* finished with the structure */
547                 sc_del_credit_return_intr(sc);
548         }
549
550 bail:
551         vfree(tmpbuf);
552         return ret;
553 }
554
555 static ssize_t diagpkt_write(struct file *fp, const char __user *data,
556                                  size_t count, loff_t *off)
557 {
558         struct hfi1_devdata *dd;
559         struct send_context *sc;
560         u8 vl;
561
562         struct diag_pkt dp;
563
564         if (count != sizeof(dp))
565                 return -EINVAL;
566
567         if (copy_from_user(&dp, data, sizeof(dp)))
568                 return -EFAULT;
569
570         /*
571         * The Send Context is derived from the PbcVL value
572         * if PBC is populated
573         */
574         if (dp.pbc) {
575                 dd = hfi1_lookup(dp.unit);
576                 if (dd == NULL)
577                         return -ENODEV;
578                 vl = (dp.pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
579                 sc = dd->vld[vl].sc;
580                 if (sc) {
581                         dp.sw_index = sc->sw_index;
582                         hfi1_cdbg(
583                                PKT,
584                                "Packet sent over VL %d via Send Context %u(%u)",
585                                vl, sc->sw_index, sc->hw_context);
586                 }
587         }
588
589         return diagpkt_send(&dp);
590 }
591
592 static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name)
593 {
594         int ret = 0;
595
596         dd->hfi1_snoop.mode_flag = 0;
597         spin_lock_init(&dd->hfi1_snoop.snoop_lock);
598         INIT_LIST_HEAD(&dd->hfi1_snoop.queue);
599         init_waitqueue_head(&dd->hfi1_snoop.waitq);
600
601         ret = hfi1_cdev_init(HFI1_SNOOP_CAPTURE_BASE + dd->unit, name,
602                              &snoop_file_ops,
603                              &dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev,
604                              false);
605
606         if (ret) {
607                 dd_dev_err(dd, "Couldn't create %s device: %d", name, ret);
608                 hfi1_cdev_cleanup(&dd->hfi1_snoop.cdev,
609                                  &dd->hfi1_snoop.class_dev);
610         }
611
612         return ret;
613 }
614
615 static struct hfi1_devdata *hfi1_dd_from_sc_inode(struct inode *in)
616 {
617         int unit = iminor(in) - HFI1_SNOOP_CAPTURE_BASE;
618         struct hfi1_devdata *dd;
619
620         dd = hfi1_lookup(unit);
621         return dd;
622
623 }
624
625 /* clear or restore send context integrity checks */
626 static void adjust_integrity_checks(struct hfi1_devdata *dd)
627 {
628         struct send_context *sc;
629         unsigned long sc_flags;
630         int i;
631
632         spin_lock_irqsave(&dd->sc_lock, sc_flags);
633         for (i = 0; i < dd->num_send_contexts; i++) {
634                 int enable;
635
636                 sc = dd->send_contexts[i].sc;
637
638                 if (!sc)
639                         continue;       /* not allocated */
640
641                 enable = likely(!HFI1_CAP_IS_KSET(NO_INTEGRITY)) &&
642                          dd->hfi1_snoop.mode_flag != HFI1_PORT_SNOOP_MODE;
643
644                 set_pio_integrity(sc);
645
646                 if (enable) /* take HFI_CAP_* flags into account */
647                         hfi1_init_ctxt(sc);
648         }
649         spin_unlock_irqrestore(&dd->sc_lock, sc_flags);
650 }
651
652 static int hfi1_snoop_open(struct inode *in, struct file *fp)
653 {
654         int ret;
655         int mode_flag = 0;
656         unsigned long flags = 0;
657         struct hfi1_devdata *dd;
658         struct list_head *queue;
659
660         mutex_lock(&hfi1_mutex);
661
662         dd = hfi1_dd_from_sc_inode(in);
663         if (dd == NULL) {
664                 ret = -ENODEV;
665                 goto bail;
666         }
667
668         /*
669          * File mode determines snoop or capture. Some existing user
670          * applications expect the capture device to be able to be opened RDWR
671          * because they expect a dedicated capture device. For this reason we
672          * support a module param to force capture mode even if the file open
673          * mode matches snoop.
674          */
675         if ((fp->f_flags & O_ACCMODE) == O_RDONLY) {
676                 snoop_dbg("Capture Enabled");
677                 mode_flag = HFI1_PORT_CAPTURE_MODE;
678         } else if ((fp->f_flags & O_ACCMODE) == O_RDWR) {
679                 snoop_dbg("Snoop Enabled");
680                 mode_flag = HFI1_PORT_SNOOP_MODE;
681         } else {
682                 snoop_dbg("Invalid");
683                 ret =  -EINVAL;
684                 goto bail;
685         }
686         queue = &dd->hfi1_snoop.queue;
687
688         /*
689          * We are not supporting snoop and capture at the same time.
690          */
691         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
692         if (dd->hfi1_snoop.mode_flag) {
693                 ret = -EBUSY;
694                 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
695                 goto bail;
696         }
697
698         dd->hfi1_snoop.mode_flag = mode_flag;
699         drain_snoop_list(queue);
700
701         dd->hfi1_snoop.filter_callback = NULL;
702         dd->hfi1_snoop.filter_value = NULL;
703
704         /*
705          * Send side packet integrity checks are not helpful when snooping so
706          * disable and re-enable when we stop snooping.
707          */
708         if (mode_flag == HFI1_PORT_SNOOP_MODE) {
709                 /* clear after snoop mode is on */
710                 adjust_integrity_checks(dd); /* clear */
711
712                 /*
713                  * We also do not want to be doing the DLID LMC check for
714                  * ingressed packets.
715                  */
716                 dd->hfi1_snoop.dcc_cfg = read_csr(dd, DCC_CFG_PORT_CONFIG1);
717                 write_csr(dd, DCC_CFG_PORT_CONFIG1,
718                           (dd->hfi1_snoop.dcc_cfg >> 32) << 32);
719         }
720
721         /*
722          * As soon as we set these function pointers the recv and send handlers
723          * are active. This is a race condition so we must make sure to drain
724          * the queue and init filter values above. Technically we should add
725          * locking here but all that will happen is on recv a packet will get
726          * allocated and get stuck on the snoop_lock before getting added to the
727          * queue. Same goes for send.
728          */
729         dd->rhf_rcv_function_map = snoop_rhf_rcv_functions;
730         dd->process_pio_send = snoop_send_pio_handler;
731         dd->process_dma_send = snoop_send_pio_handler;
732         dd->pio_inline_send = snoop_inline_pio_send;
733
734         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
735         ret = 0;
736
737 bail:
738         mutex_unlock(&hfi1_mutex);
739
740         return ret;
741 }
742
743 static int hfi1_snoop_release(struct inode *in, struct file *fp)
744 {
745         unsigned long flags = 0;
746         struct hfi1_devdata *dd;
747         int mode_flag;
748
749         dd = hfi1_dd_from_sc_inode(in);
750         if (dd == NULL)
751                 return -ENODEV;
752
753         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
754
755         /* clear the snoop mode before re-adjusting send context CSRs */
756         mode_flag = dd->hfi1_snoop.mode_flag;
757         dd->hfi1_snoop.mode_flag = 0;
758
759         /*
760          * Drain the queue and clear the filters we are done with it. Don't
761          * forget to restore the packet integrity checks
762          */
763         drain_snoop_list(&dd->hfi1_snoop.queue);
764         if (mode_flag == HFI1_PORT_SNOOP_MODE) {
765                 /* restore after snoop mode is clear */
766                 adjust_integrity_checks(dd); /* restore */
767
768                 /*
769                  * Also should probably reset the DCC_CONFIG1 register for DLID
770                  * checking on incoming packets again. Use the value saved when
771                  * opening the snoop device.
772                  */
773                 write_csr(dd, DCC_CFG_PORT_CONFIG1, dd->hfi1_snoop.dcc_cfg);
774         }
775
776         dd->hfi1_snoop.filter_callback = NULL;
777         kfree(dd->hfi1_snoop.filter_value);
778         dd->hfi1_snoop.filter_value = NULL;
779
780         /*
781          * User is done snooping and capturing, return control to the normal
782          * handler. Re-enable SDMA handling.
783          */
784         dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
785         dd->process_pio_send = hfi1_verbs_send_pio;
786         dd->process_dma_send = hfi1_verbs_send_dma;
787         dd->pio_inline_send = pio_copy;
788
789         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
790
791         snoop_dbg("snoop/capture device released");
792
793         return 0;
794 }
795
796 static unsigned int hfi1_snoop_poll(struct file *fp,
797                                     struct poll_table_struct *wait)
798 {
799         int ret = 0;
800         unsigned long flags = 0;
801
802         struct hfi1_devdata *dd;
803
804         dd = hfi1_dd_from_sc_inode(fp->f_inode);
805         if (dd == NULL)
806                 return -ENODEV;
807
808         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
809
810         poll_wait(fp, &dd->hfi1_snoop.waitq, wait);
811         if (!list_empty(&dd->hfi1_snoop.queue))
812                 ret |= POLLIN | POLLRDNORM;
813
814         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
815         return ret;
816
817 }
818
819 static ssize_t hfi1_snoop_write(struct file *fp, const char __user *data,
820                                 size_t count, loff_t *off)
821 {
822         struct diag_pkt dpkt;
823         struct hfi1_devdata *dd;
824         size_t ret;
825         u8 byte_two, sl, sc5, sc4, vl, byte_one;
826         struct send_context *sc;
827         u32 len;
828         u64 pbc;
829         struct hfi1_ibport *ibp;
830         struct hfi1_pportdata *ppd;
831
832         dd = hfi1_dd_from_sc_inode(fp->f_inode);
833         if (dd == NULL)
834                 return -ENODEV;
835
836         ppd = dd->pport;
837         snoop_dbg("received %lu bytes from user", count);
838
839         memset(&dpkt, 0, sizeof(struct diag_pkt));
840         dpkt.version = _DIAG_PKT_VERS;
841         dpkt.unit = dd->unit;
842         dpkt.port = 1;
843
844         if (likely(!(snoop_flags & SNOOP_USE_METADATA))) {
845                 /*
846                 * We need to generate the PBC and not let diagpkt_send do it,
847                 * to do this we need the VL and the length in dwords.
848                 * The VL can be determined by using the SL and looking up the
849                 * SC. Then the SC can be converted into VL. The exception to
850                 * this is those packets which are from an SMI queue pair.
851                 * Since we can't detect anything about the QP here we have to
852                 * rely on the SC. If its 0xF then we assume its SMI and
853                 * do not look at the SL.
854                 */
855                 if (copy_from_user(&byte_one, data, 1))
856                         return -EINVAL;
857
858                 if (copy_from_user(&byte_two, data+1, 1))
859                         return -EINVAL;
860
861                 sc4 = (byte_one >> 4) & 0xf;
862                 if (sc4 == 0xF) {
863                         snoop_dbg("Detected VL15 packet ignoring SL in packet");
864                         vl = sc4;
865                 } else {
866                         sl = (byte_two >> 4) & 0xf;
867                         ibp = to_iport(&dd->verbs_dev.ibdev, 1);
868                         sc5 = ibp->sl_to_sc[sl];
869                         vl = sc_to_vlt(dd, sc5);
870                         if (vl != sc4) {
871                                 snoop_dbg("VL %d does not match SC %d of packet",
872                                           vl, sc4);
873                                 return -EINVAL;
874                         }
875                 }
876
877                 sc = dd->vld[vl].sc; /* Look up the context based on VL */
878                 if (sc) {
879                         dpkt.sw_index = sc->sw_index;
880                         snoop_dbg("Sending on context %u(%u)", sc->sw_index,
881                                   sc->hw_context);
882                 } else {
883                         snoop_dbg("Could not find context for vl %d", vl);
884                         return -EINVAL;
885                 }
886
887                 len = (count >> 2) + 2; /* Add in PBC */
888                 pbc = create_pbc(ppd, 0, 0, vl, len);
889         } else {
890                 if (copy_from_user(&pbc, data, sizeof(pbc)))
891                         return -EINVAL;
892                 vl = (pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
893                 sc = dd->vld[vl].sc; /* Look up the context based on VL */
894                 if (sc) {
895                         dpkt.sw_index = sc->sw_index;
896                 } else {
897                         snoop_dbg("Could not find context for vl %d", vl);
898                         return -EINVAL;
899                 }
900                 data += sizeof(pbc);
901                 count -= sizeof(pbc);
902         }
903         dpkt.len = count;
904         dpkt.data = (unsigned long)data;
905
906         snoop_dbg("PBC: vl=0x%llx Length=0x%llx",
907                   (pbc >> 12) & 0xf,
908                   (pbc & 0xfff));
909
910         dpkt.pbc = pbc;
911         ret = diagpkt_send(&dpkt);
912         /*
913          * diagpkt_send only returns number of bytes in the diagpkt so patch
914          * that up here before returning.
915          */
916         if (ret == sizeof(dpkt))
917                 return count;
918
919         return ret;
920 }
921
922 static ssize_t hfi1_snoop_read(struct file *fp, char __user *data,
923                                size_t pkt_len, loff_t *off)
924 {
925         ssize_t ret = 0;
926         unsigned long flags = 0;
927         struct snoop_packet *packet = NULL;
928         struct hfi1_devdata *dd;
929
930         dd = hfi1_dd_from_sc_inode(fp->f_inode);
931         if (dd == NULL)
932                 return -ENODEV;
933
934         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
935
936         while (list_empty(&dd->hfi1_snoop.queue)) {
937                 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
938
939                 if (fp->f_flags & O_NONBLOCK)
940                         return -EAGAIN;
941
942                 if (wait_event_interruptible(
943                                 dd->hfi1_snoop.waitq,
944                                 !list_empty(&dd->hfi1_snoop.queue)))
945                         return -EINTR;
946
947                 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
948         }
949
950         if (!list_empty(&dd->hfi1_snoop.queue)) {
951                 packet = list_entry(dd->hfi1_snoop.queue.next,
952                                     struct snoop_packet, list);
953                 list_del(&packet->list);
954                 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
955                 if (pkt_len >= packet->total_len) {
956                         if (copy_to_user(data, packet->data,
957                                 packet->total_len))
958                                 ret = -EFAULT;
959                         else
960                                 ret = packet->total_len;
961                 } else
962                         ret = -EINVAL;
963
964                 kfree(packet);
965         } else
966                 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
967
968         return ret;
969 }
970
971 static long hfi1_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
972 {
973         struct hfi1_devdata *dd;
974         void *filter_value = NULL;
975         long ret = 0;
976         int value = 0;
977         u8 physState = 0;
978         u8 linkState = 0;
979         u16 devState = 0;
980         unsigned long flags = 0;
981         unsigned long *argp = NULL;
982         struct hfi1_packet_filter_command filter_cmd = {0};
983         int mode_flag = 0;
984         struct hfi1_pportdata *ppd = NULL;
985         unsigned int index;
986         struct hfi1_link_info link_info;
987
988         dd = hfi1_dd_from_sc_inode(fp->f_inode);
989         if (dd == NULL)
990                 return -ENODEV;
991
992         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
993
994         mode_flag = dd->hfi1_snoop.mode_flag;
995
996         if (((_IOC_DIR(cmd) & _IOC_READ)
997             && !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd)))
998             || ((_IOC_DIR(cmd) & _IOC_WRITE)
999             && !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd)))) {
1000                 ret = -EFAULT;
1001         } else if (!capable(CAP_SYS_ADMIN)) {
1002                 ret = -EPERM;
1003         } else if ((mode_flag & HFI1_PORT_CAPTURE_MODE) &&
1004                    (cmd != HFI1_SNOOP_IOCCLEARQUEUE) &&
1005                    (cmd != HFI1_SNOOP_IOCCLEARFILTER) &&
1006                    (cmd != HFI1_SNOOP_IOCSETFILTER)) {
1007                 /* Capture devices are allowed only 3 operations
1008                  * 1.Clear capture queue
1009                  * 2.Clear capture filter
1010                  * 3.Set capture filter
1011                  * Other are invalid.
1012                  */
1013                 ret = -EINVAL;
1014         } else {
1015                 switch (cmd) {
1016                 case HFI1_SNOOP_IOCSETLINKSTATE:
1017                         snoop_dbg("HFI1_SNOOP_IOCSETLINKSTATE is not valid");
1018                         ret = -EINVAL;
1019                         break;
1020
1021                 case HFI1_SNOOP_IOCSETLINKSTATE_EXTRA:
1022                         memset(&link_info, 0, sizeof(link_info));
1023
1024                         if (copy_from_user(&link_info,
1025                                 (struct hfi1_link_info __user *)arg,
1026                                 sizeof(link_info)))
1027                                 ret = -EFAULT;
1028
1029                         value = link_info.port_state;
1030                         index = link_info.port_number;
1031                         if (index > dd->num_pports - 1) {
1032                                 ret = -EINVAL;
1033                                 break;
1034                         }
1035
1036                         ppd = &dd->pport[index];
1037                         if (!ppd) {
1038                                 ret = -EINVAL;
1039                                 break;
1040                         }
1041
1042                         /* What we want to transition to */
1043                         physState = (value >> 4) & 0xF;
1044                         linkState = value & 0xF;
1045                         snoop_dbg("Setting link state 0x%x", value);
1046
1047                         switch (linkState) {
1048                         case IB_PORT_NOP:
1049                                 if (physState == 0)
1050                                         break;
1051                                         /* fall through */
1052                         case IB_PORT_DOWN:
1053                                 switch (physState) {
1054                                 case 0:
1055                                         devState = HLS_DN_DOWNDEF;
1056                                         break;
1057                                 case 2:
1058                                         devState = HLS_DN_POLL;
1059                                         break;
1060                                 case 3:
1061                                         devState = HLS_DN_DISABLE;
1062                                         break;
1063                                 default:
1064                                         ret = -EINVAL;
1065                                         goto done;
1066                                 }
1067                                 ret = set_link_state(ppd, devState);
1068                                 break;
1069                         case IB_PORT_ARMED:
1070                                 ret = set_link_state(ppd, HLS_UP_ARMED);
1071                                 if (!ret)
1072                                         send_idle_sma(dd, SMA_IDLE_ARM);
1073                                 break;
1074                         case IB_PORT_ACTIVE:
1075                                 ret = set_link_state(ppd, HLS_UP_ACTIVE);
1076                                 if (!ret)
1077                                         send_idle_sma(dd, SMA_IDLE_ACTIVE);
1078                                 break;
1079                         default:
1080                                 ret = -EINVAL;
1081                                 break;
1082                         }
1083
1084                         if (ret)
1085                                 break;
1086                         /* fall through */
1087                 case HFI1_SNOOP_IOCGETLINKSTATE:
1088                 case HFI1_SNOOP_IOCGETLINKSTATE_EXTRA:
1089                         if (cmd == HFI1_SNOOP_IOCGETLINKSTATE_EXTRA) {
1090                                 memset(&link_info, 0, sizeof(link_info));
1091                                 if (copy_from_user(&link_info,
1092                                         (struct hfi1_link_info __user *)arg,
1093                                         sizeof(link_info)))
1094                                         ret = -EFAULT;
1095                                 index = link_info.port_number;
1096                         } else {
1097                                 ret = __get_user(index, (int __user *) arg);
1098                                 if (ret !=  0)
1099                                         break;
1100                         }
1101
1102                         if (index > dd->num_pports - 1) {
1103                                 ret = -EINVAL;
1104                                 break;
1105                         }
1106
1107                         ppd = &dd->pport[index];
1108                         if (!ppd) {
1109                                 ret = -EINVAL;
1110                                 break;
1111                         }
1112                         value = hfi1_ibphys_portstate(ppd);
1113                         value <<= 4;
1114                         value |= driver_lstate(ppd);
1115
1116                         snoop_dbg("Link port | Link State: %d", value);
1117
1118                         if ((cmd == HFI1_SNOOP_IOCGETLINKSTATE_EXTRA) ||
1119                             (cmd == HFI1_SNOOP_IOCSETLINKSTATE_EXTRA)) {
1120                                 link_info.port_state = value;
1121                                 link_info.node_guid = cpu_to_be64(ppd->guid);
1122                                 link_info.link_speed_active =
1123                                                         ppd->link_speed_active;
1124                                 link_info.link_width_active =
1125                                                         ppd->link_width_active;
1126                                 if (copy_to_user(
1127                                         (struct hfi1_link_info __user *)arg,
1128                                         &link_info, sizeof(link_info)))
1129                                         ret = -EFAULT;
1130                         } else {
1131                                 ret = __put_user(value, (int __user *)arg);
1132                         }
1133                         break;
1134
1135                 case HFI1_SNOOP_IOCCLEARQUEUE:
1136                         snoop_dbg("Clearing snoop queue");
1137                         drain_snoop_list(&dd->hfi1_snoop.queue);
1138                         break;
1139
1140                 case HFI1_SNOOP_IOCCLEARFILTER:
1141                         snoop_dbg("Clearing filter");
1142                         if (dd->hfi1_snoop.filter_callback) {
1143                                 /* Drain packets first */
1144                                 drain_snoop_list(&dd->hfi1_snoop.queue);
1145                                 dd->hfi1_snoop.filter_callback = NULL;
1146                         }
1147                         kfree(dd->hfi1_snoop.filter_value);
1148                         dd->hfi1_snoop.filter_value = NULL;
1149                         break;
1150
1151                 case HFI1_SNOOP_IOCSETFILTER:
1152                         snoop_dbg("Setting filter");
1153                         /* just copy command structure */
1154                         argp = (unsigned long *)arg;
1155                         if (copy_from_user(&filter_cmd, (void __user *)argp,
1156                                              sizeof(filter_cmd))) {
1157                                 ret = -EFAULT;
1158                                 break;
1159                         }
1160                         if (filter_cmd.opcode >= HFI1_MAX_FILTERS) {
1161                                 pr_alert("Invalid opcode in request\n");
1162                                 ret = -EINVAL;
1163                                 break;
1164                         }
1165
1166                         snoop_dbg("Opcode %d Len %d Ptr %p",
1167                                    filter_cmd.opcode, filter_cmd.length,
1168                                    filter_cmd.value_ptr);
1169
1170                         filter_value = kcalloc(filter_cmd.length, sizeof(u8),
1171                                                GFP_KERNEL);
1172                         if (!filter_value) {
1173                                 pr_alert("Not enough memory\n");
1174                                 ret = -ENOMEM;
1175                                 break;
1176                         }
1177                         /* copy remaining data from userspace */
1178                         if (copy_from_user((u8 *)filter_value,
1179                                         (void __user *)filter_cmd.value_ptr,
1180                                         filter_cmd.length)) {
1181                                 kfree(filter_value);
1182                                 ret = -EFAULT;
1183                                 break;
1184                         }
1185                         /* Drain packets first */
1186                         drain_snoop_list(&dd->hfi1_snoop.queue);
1187                         dd->hfi1_snoop.filter_callback =
1188                                 hfi1_filters[filter_cmd.opcode].filter;
1189                         /* just in case we see back to back sets */
1190                         kfree(dd->hfi1_snoop.filter_value);
1191                         dd->hfi1_snoop.filter_value = filter_value;
1192
1193                         break;
1194                 case HFI1_SNOOP_IOCGETVERSION:
1195                         value = SNOOP_CAPTURE_VERSION;
1196                         snoop_dbg("Getting version: %d", value);
1197                         ret = __put_user(value, (int __user *)arg);
1198                         break;
1199                 case HFI1_SNOOP_IOCSET_OPTS:
1200                         snoop_flags = 0;
1201                         ret = __get_user(value, (int __user *) arg);
1202                         if (ret != 0)
1203                                 break;
1204
1205                         snoop_dbg("Setting snoop option %d", value);
1206                         if (value & SNOOP_DROP_SEND)
1207                                 snoop_flags |= SNOOP_DROP_SEND;
1208                         if (value & SNOOP_USE_METADATA)
1209                                 snoop_flags |= SNOOP_USE_METADATA;
1210                         break;
1211                 default:
1212                         ret = -ENOTTY;
1213                         break;
1214                 }
1215         }
1216 done:
1217         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1218         return ret;
1219 }
1220
1221 static void snoop_list_add_tail(struct snoop_packet *packet,
1222                                 struct hfi1_devdata *dd)
1223 {
1224         unsigned long flags = 0;
1225
1226         spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
1227         if (likely((dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE) ||
1228                    (dd->hfi1_snoop.mode_flag & HFI1_PORT_CAPTURE_MODE))) {
1229                 list_add_tail(&packet->list, &dd->hfi1_snoop.queue);
1230                 snoop_dbg("Added packet to list");
1231         }
1232
1233         /*
1234          * Technically we can could have closed the snoop device while waiting
1235          * on the above lock and it is gone now. The snoop mode_flag will
1236          * prevent us from adding the packet to the queue though.
1237          */
1238
1239         spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1240         wake_up_interruptible(&dd->hfi1_snoop.waitq);
1241 }
1242
1243 static inline int hfi1_filter_check(void *val, const char *msg)
1244 {
1245         if (!val) {
1246                 snoop_dbg("Error invalid %s value for filter", msg);
1247                 return HFI1_FILTER_ERR;
1248         }
1249         return 0;
1250 }
1251
1252 static int hfi1_filter_lid(void *ibhdr, void *packet_data, void *value)
1253 {
1254         struct hfi1_ib_header *hdr;
1255         int ret;
1256
1257         ret = hfi1_filter_check(ibhdr, "header");
1258         if (ret)
1259                 return ret;
1260         ret = hfi1_filter_check(value, "user");
1261         if (ret)
1262                 return ret;
1263         hdr = (struct hfi1_ib_header *)ibhdr;
1264
1265         if (*((u16 *)value) == be16_to_cpu(hdr->lrh[3])) /* matches slid */
1266                 return HFI1_FILTER_HIT; /* matched */
1267
1268         return HFI1_FILTER_MISS; /* Not matched */
1269 }
1270
1271 static int hfi1_filter_dlid(void *ibhdr, void *packet_data, void *value)
1272 {
1273         struct hfi1_ib_header *hdr;
1274         int ret;
1275
1276         ret = hfi1_filter_check(ibhdr, "header");
1277         if (ret)
1278                 return ret;
1279         ret = hfi1_filter_check(value, "user");
1280         if (ret)
1281                 return ret;
1282
1283         hdr = (struct hfi1_ib_header *)ibhdr;
1284
1285         if (*((u16 *)value) == be16_to_cpu(hdr->lrh[1]))
1286                 return HFI1_FILTER_HIT;
1287
1288         return HFI1_FILTER_MISS;
1289 }
1290
1291 /* Not valid for outgoing packets, send handler passes null for data*/
1292 static int hfi1_filter_mad_mgmt_class(void *ibhdr, void *packet_data,
1293                                       void *value)
1294 {
1295         struct hfi1_ib_header *hdr;
1296         struct hfi1_other_headers *ohdr = NULL;
1297         struct ib_smp *smp = NULL;
1298         u32 qpn = 0;
1299         int ret;
1300
1301         ret = hfi1_filter_check(ibhdr, "header");
1302         if (ret)
1303                 return ret;
1304         ret = hfi1_filter_check(packet_data, "packet_data");
1305         if (ret)
1306                 return ret;
1307         ret = hfi1_filter_check(value, "user");
1308         if (ret)
1309                 return ret;
1310
1311         hdr = (struct hfi1_ib_header *)ibhdr;
1312
1313         /* Check for GRH */
1314         if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1315                 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
1316         else
1317                 ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
1318
1319         qpn = be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF;
1320         if (qpn <= 1) {
1321                 smp = (struct ib_smp *)packet_data;
1322                 if (*((u8 *)value) == smp->mgmt_class)
1323                         return HFI1_FILTER_HIT;
1324                 else
1325                         return HFI1_FILTER_MISS;
1326         }
1327         return HFI1_FILTER_ERR;
1328 }
1329
1330 static int hfi1_filter_qp_number(void *ibhdr, void *packet_data, void *value)
1331 {
1332
1333         struct hfi1_ib_header *hdr;
1334         struct hfi1_other_headers *ohdr = NULL;
1335         int ret;
1336
1337         ret = hfi1_filter_check(ibhdr, "header");
1338         if (ret)
1339                 return ret;
1340         ret = hfi1_filter_check(value, "user");
1341         if (ret)
1342                 return ret;
1343
1344         hdr = (struct hfi1_ib_header *)ibhdr;
1345
1346         /* Check for GRH */
1347         if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1348                 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
1349         else
1350                 ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
1351         if (*((u32 *)value) == (be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF))
1352                 return HFI1_FILTER_HIT;
1353
1354         return HFI1_FILTER_MISS;
1355 }
1356
1357 static int hfi1_filter_ibpacket_type(void *ibhdr, void *packet_data,
1358                                      void *value)
1359 {
1360         u32 lnh = 0;
1361         u8 opcode = 0;
1362         struct hfi1_ib_header *hdr;
1363         struct hfi1_other_headers *ohdr = NULL;
1364         int ret;
1365
1366         ret = hfi1_filter_check(ibhdr, "header");
1367         if (ret)
1368                 return ret;
1369         ret = hfi1_filter_check(value, "user");
1370         if (ret)
1371                 return ret;
1372
1373         hdr = (struct hfi1_ib_header *)ibhdr;
1374
1375         lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1376
1377         if (lnh == HFI1_LRH_BTH)
1378                 ohdr = &hdr->u.oth;
1379         else if (lnh == HFI1_LRH_GRH)
1380                 ohdr = &hdr->u.l.oth;
1381         else
1382                 return HFI1_FILTER_ERR;
1383
1384         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1385
1386         if (*((u8 *)value) == ((opcode >> 5) & 0x7))
1387                 return HFI1_FILTER_HIT;
1388
1389         return HFI1_FILTER_MISS;
1390 }
1391
1392 static int hfi1_filter_ib_service_level(void *ibhdr, void *packet_data,
1393                                         void *value)
1394 {
1395         struct hfi1_ib_header *hdr;
1396         int ret;
1397
1398         ret = hfi1_filter_check(ibhdr, "header");
1399         if (ret)
1400                 return ret;
1401         ret = hfi1_filter_check(value, "user");
1402         if (ret)
1403                 return ret;
1404
1405         hdr = (struct hfi1_ib_header *)ibhdr;
1406
1407         if ((*((u8 *)value)) == ((be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF))
1408                 return HFI1_FILTER_HIT;
1409
1410         return HFI1_FILTER_MISS;
1411 }
1412
1413 static int hfi1_filter_ib_pkey(void *ibhdr, void *packet_data, void *value)
1414 {
1415
1416         u32 lnh = 0;
1417         struct hfi1_ib_header *hdr;
1418         struct hfi1_other_headers *ohdr = NULL;
1419         int ret;
1420
1421         ret = hfi1_filter_check(ibhdr, "header");
1422         if (ret)
1423                 return ret;
1424         ret = hfi1_filter_check(value, "user");
1425         if (ret)
1426                 return ret;
1427
1428         hdr = (struct hfi1_ib_header *)ibhdr;
1429
1430         lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1431         if (lnh == HFI1_LRH_BTH)
1432                 ohdr = &hdr->u.oth;
1433         else if (lnh == HFI1_LRH_GRH)
1434                 ohdr = &hdr->u.l.oth;
1435         else
1436                 return HFI1_FILTER_ERR;
1437
1438         /* P_key is 16-bit entity, however top most bit indicates
1439          * type of membership. 0 for limited and 1 for Full.
1440          * Limited members cannot accept information from other
1441          * Limited members, but communication is allowed between
1442          * every other combination of membership.
1443          * Hence we'll omit comparing top-most bit while filtering
1444          */
1445
1446         if ((*(u16 *)value & 0x7FFF) ==
1447                 ((be32_to_cpu(ohdr->bth[0])) & 0x7FFF))
1448                 return HFI1_FILTER_HIT;
1449
1450         return HFI1_FILTER_MISS;
1451 }
1452
1453 /*
1454  * If packet_data is NULL then this is coming from one of the send functions.
1455  * Thus we know if its an ingressed or egressed packet.
1456  */
1457 static int hfi1_filter_direction(void *ibhdr, void *packet_data, void *value)
1458 {
1459         u8 user_dir = *(u8 *)value;
1460         int ret;
1461
1462         ret = hfi1_filter_check(value, "user");
1463         if (ret)
1464                 return ret;
1465
1466         if (packet_data) {
1467                 /* Incoming packet */
1468                 if (user_dir & HFI1_SNOOP_INGRESS)
1469                         return HFI1_FILTER_HIT;
1470         } else {
1471                 /* Outgoing packet */
1472                 if (user_dir & HFI1_SNOOP_EGRESS)
1473                         return HFI1_FILTER_HIT;
1474         }
1475
1476         return HFI1_FILTER_MISS;
1477 }
1478
1479 /*
1480  * Allocate a snoop packet. The structure that is stored in the ring buffer, not
1481  * to be confused with an hfi packet type.
1482  */
1483 static struct snoop_packet *allocate_snoop_packet(u32 hdr_len,
1484                                                   u32 data_len,
1485                                                   u32 md_len)
1486 {
1487
1488         struct snoop_packet *packet;
1489
1490         packet = kzalloc(sizeof(struct snoop_packet) + hdr_len + data_len
1491                          + md_len,
1492                          GFP_ATOMIC | __GFP_NOWARN);
1493         if (likely(packet))
1494                 INIT_LIST_HEAD(&packet->list);
1495
1496
1497         return packet;
1498 }
1499
1500 /*
1501  * Instead of having snoop and capture code intermixed with the recv functions,
1502  * both the interrupt handler and hfi1_ib_rcv() we are going to hijack the call
1503  * and land in here for snoop/capture but if not enabled the call will go
1504  * through as before. This gives us a single point to constrain all of the snoop
1505  * snoop recv logic. There is nothing special that needs to happen for bypass
1506  * packets. This routine should not try to look into the packet. It just copied
1507  * it. There is no guarantee for filters when it comes to bypass packets as
1508  * there is no specific support. Bottom line is this routine does now even know
1509  * what a bypass packet is.
1510  */
1511 int snoop_recv_handler(struct hfi1_packet *packet)
1512 {
1513         struct hfi1_pportdata *ppd = packet->rcd->ppd;
1514         struct hfi1_ib_header *hdr = packet->hdr;
1515         int header_size = packet->hlen;
1516         void *data = packet->ebuf;
1517         u32 tlen = packet->tlen;
1518         struct snoop_packet *s_packet = NULL;
1519         int ret;
1520         int snoop_mode = 0;
1521         u32 md_len = 0;
1522         struct capture_md md;
1523
1524         snoop_dbg("PACKET IN: hdr size %d tlen %d data %p", header_size, tlen,
1525                   data);
1526
1527         trace_snoop_capture(ppd->dd, header_size, hdr, tlen - header_size,
1528                             data);
1529
1530         if (!ppd->dd->hfi1_snoop.filter_callback) {
1531                 snoop_dbg("filter not set");
1532                 ret = HFI1_FILTER_HIT;
1533         } else {
1534                 ret = ppd->dd->hfi1_snoop.filter_callback(hdr, data,
1535                                         ppd->dd->hfi1_snoop.filter_value);
1536         }
1537
1538         switch (ret) {
1539         case HFI1_FILTER_ERR:
1540                 snoop_dbg("Error in filter call");
1541                 break;
1542         case HFI1_FILTER_MISS:
1543                 snoop_dbg("Filter Miss");
1544                 break;
1545         case HFI1_FILTER_HIT:
1546
1547                 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1548                         snoop_mode = 1;
1549                 if ((snoop_mode == 0) ||
1550                     unlikely(snoop_flags & SNOOP_USE_METADATA))
1551                         md_len = sizeof(struct capture_md);
1552
1553
1554                 s_packet = allocate_snoop_packet(header_size,
1555                                                  tlen - header_size,
1556                                                  md_len);
1557
1558                 if (unlikely(s_packet == NULL)) {
1559                         dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1560                         break;
1561                 }
1562
1563                 if (md_len > 0) {
1564                         memset(&md, 0, sizeof(struct capture_md));
1565                         md.port = 1;
1566                         md.dir = PKT_DIR_INGRESS;
1567                         md.u.rhf = packet->rhf;
1568                         memcpy(s_packet->data, &md, md_len);
1569                 }
1570
1571                 /* We should always have a header */
1572                 if (hdr) {
1573                         memcpy(s_packet->data + md_len, hdr, header_size);
1574                 } else {
1575                         dd_dev_err(ppd->dd, "Unable to copy header to snoop/capture packet\n");
1576                         kfree(s_packet);
1577                         break;
1578                 }
1579
1580                 /*
1581                  * Packets with no data are possible. If there is no data needed
1582                  * to take care of the last 4 bytes which are normally included
1583                  * with data buffers and are included in tlen.  Since we kzalloc
1584                  * the buffer we do not need to set any values but if we decide
1585                  * not to use kzalloc we should zero them.
1586                  */
1587                 if (data)
1588                         memcpy(s_packet->data + header_size + md_len, data,
1589                                tlen - header_size);
1590
1591                 s_packet->total_len = tlen + md_len;
1592                 snoop_list_add_tail(s_packet, ppd->dd);
1593
1594                 /*
1595                  * If we are snooping the packet not capturing then throw away
1596                  * after adding to the list.
1597                  */
1598                 snoop_dbg("Capturing packet");
1599                 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE) {
1600                         snoop_dbg("Throwing packet away");
1601                         /*
1602                          * If we are dropping the packet we still may need to
1603                          * handle the case where error flags are set, this is
1604                          * normally done by the type specific handler but that
1605                          * won't be called in this case.
1606                          */
1607                         if (unlikely(rhf_err_flags(packet->rhf)))
1608                                 handle_eflags(packet);
1609
1610                         /* throw the packet on the floor */
1611                         return RHF_RCV_CONTINUE;
1612                 }
1613                 break;
1614         default:
1615                 break;
1616         }
1617
1618         /*
1619          * We do not care what type of packet came in here - just pass it off
1620          * to the normal handler.
1621          */
1622         return ppd->dd->normal_rhf_rcv_functions[rhf_rcv_type(packet->rhf)]
1623                         (packet);
1624 }
1625
1626 /*
1627  * Handle snooping and capturing packets when sdma is being used.
1628  */
1629 int snoop_send_dma_handler(struct hfi1_qp *qp, struct hfi1_pkt_state *ps,
1630                            u64 pbc)
1631 {
1632         pr_alert("Snooping/Capture of Send DMA Packets Is Not Supported!\n");
1633         snoop_dbg("Unsupported Operation");
1634         return hfi1_verbs_send_dma(qp, ps, 0);
1635 }
1636
1637 /*
1638  * Handle snooping and capturing packets when pio is being used. Does not handle
1639  * bypass packets. The only way to send a bypass packet currently is to use the
1640  * diagpkt interface. When that interface is enable snoop/capture is not.
1641  */
1642 int snoop_send_pio_handler(struct hfi1_qp *qp, struct hfi1_pkt_state *ps,
1643                            u64 pbc)
1644 {
1645         struct ahg_ib_header *ahdr = qp->s_hdr;
1646         u32 hdrwords = qp->s_hdrwords;
1647         struct hfi1_sge_state *ss = qp->s_cur_sge;
1648         u32 len = qp->s_cur_size;
1649         u32 dwords = (len + 3) >> 2;
1650         u32 plen = hdrwords + dwords + 2; /* includes pbc */
1651         struct hfi1_pportdata *ppd = ps->ppd;
1652         struct snoop_packet *s_packet = NULL;
1653         u32 *hdr = (u32 *)&ahdr->ibh;
1654         u32 length = 0;
1655         struct hfi1_sge_state temp_ss;
1656         void *data = NULL;
1657         void *data_start = NULL;
1658         int ret;
1659         int snoop_mode = 0;
1660         int md_len = 0;
1661         struct capture_md md;
1662         u32 vl;
1663         u32 hdr_len = hdrwords << 2;
1664         u32 tlen = HFI1_GET_PKT_LEN(&ahdr->ibh);
1665
1666         md.u.pbc = 0;
1667
1668         snoop_dbg("PACKET OUT: hdrword %u len %u plen %u dwords %u tlen %u",
1669                   hdrwords, len, plen, dwords, tlen);
1670         if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1671                 snoop_mode = 1;
1672         if ((snoop_mode == 0) ||
1673             unlikely(snoop_flags & SNOOP_USE_METADATA))
1674                 md_len = sizeof(struct capture_md);
1675
1676         /* not using ss->total_len as arg 2 b/c that does not count CRC */
1677         s_packet = allocate_snoop_packet(hdr_len, tlen - hdr_len, md_len);
1678
1679         if (unlikely(s_packet == NULL)) {
1680                 dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1681                 goto out;
1682         }
1683
1684         s_packet->total_len = tlen + md_len;
1685
1686         if (md_len > 0) {
1687                 memset(&md, 0, sizeof(struct capture_md));
1688                 md.port = 1;
1689                 md.dir = PKT_DIR_EGRESS;
1690                 if (likely(pbc == 0)) {
1691                         vl = be16_to_cpu(ahdr->ibh.lrh[0]) >> 12;
1692                         md.u.pbc = create_pbc(ppd, 0, qp->s_srate, vl, plen);
1693                 } else {
1694                         md.u.pbc = 0;
1695                 }
1696                 memcpy(s_packet->data, &md, md_len);
1697         } else {
1698                 md.u.pbc = pbc;
1699         }
1700
1701         /* Copy header */
1702         if (likely(hdr)) {
1703                 memcpy(s_packet->data + md_len, hdr, hdr_len);
1704         } else {
1705                 dd_dev_err(ppd->dd,
1706                            "Unable to copy header to snoop/capture packet\n");
1707                 kfree(s_packet);
1708                 goto out;
1709         }
1710
1711         if (ss) {
1712                 data = s_packet->data + hdr_len + md_len;
1713                 data_start = data;
1714
1715                 /*
1716                  * Copy SGE State
1717                  * The update_sge() function below will not modify the
1718                  * individual SGEs in the array. It will make a copy each time
1719                  * and operate on that. So we only need to copy this instance
1720                  * and it won't impact PIO.
1721                  */
1722                 temp_ss = *ss;
1723                 length = len;
1724
1725                 snoop_dbg("Need to copy %d bytes", length);
1726                 while (length) {
1727                         void *addr = temp_ss.sge.vaddr;
1728                         u32 slen = temp_ss.sge.length;
1729
1730                         if (slen > length) {
1731                                 slen = length;
1732                                 snoop_dbg("slen %d > len %d", slen, length);
1733                         }
1734                         snoop_dbg("copy %d to %p", slen, addr);
1735                         memcpy(data, addr, slen);
1736                         update_sge(&temp_ss, slen);
1737                         length -= slen;
1738                         data += slen;
1739                         snoop_dbg("data is now %p bytes left %d", data, length);
1740                 }
1741                 snoop_dbg("Completed SGE copy");
1742         }
1743
1744         /*
1745          * Why do the filter check down here? Because the event tracing has its
1746          * own filtering and we need to have the walked the SGE list.
1747          */
1748         if (!ppd->dd->hfi1_snoop.filter_callback) {
1749                 snoop_dbg("filter not set\n");
1750                 ret = HFI1_FILTER_HIT;
1751         } else {
1752                 ret = ppd->dd->hfi1_snoop.filter_callback(
1753                                         &ahdr->ibh,
1754                                         NULL,
1755                                         ppd->dd->hfi1_snoop.filter_value);
1756         }
1757
1758         switch (ret) {
1759         case HFI1_FILTER_ERR:
1760                 snoop_dbg("Error in filter call");
1761                 /* fall through */
1762         case HFI1_FILTER_MISS:
1763                 snoop_dbg("Filter Miss");
1764                 kfree(s_packet);
1765                 break;
1766         case HFI1_FILTER_HIT:
1767                 snoop_dbg("Capturing packet");
1768                 snoop_list_add_tail(s_packet, ppd->dd);
1769
1770                 if (unlikely((snoop_flags & SNOOP_DROP_SEND) &&
1771                              (ppd->dd->hfi1_snoop.mode_flag &
1772                               HFI1_PORT_SNOOP_MODE))) {
1773                         unsigned long flags;
1774
1775                         snoop_dbg("Dropping packet");
1776                         if (qp->s_wqe) {
1777                                 spin_lock_irqsave(&qp->s_lock, flags);
1778                                 hfi1_send_complete(
1779                                         qp,
1780                                         qp->s_wqe,
1781                                         IB_WC_SUCCESS);
1782                                 spin_unlock_irqrestore(&qp->s_lock, flags);
1783                         } else if (qp->ibqp.qp_type == IB_QPT_RC) {
1784                                 spin_lock_irqsave(&qp->s_lock, flags);
1785                                 hfi1_rc_send_complete(qp, &ahdr->ibh);
1786                                 spin_unlock_irqrestore(&qp->s_lock, flags);
1787                         }
1788                         return 0;
1789                 }
1790                 break;
1791         default:
1792                 kfree(s_packet);
1793                 break;
1794         }
1795 out:
1796         return hfi1_verbs_send_pio(qp, ps, md.u.pbc);
1797 }
1798
1799 /*
1800  * Callers of this must pass a hfi1_ib_header type for the from ptr. Currently
1801  * this can be used anywhere, but the intention is for inline ACKs for RC and
1802  * CCA packets. We don't restrict this usage though.
1803  */
1804 void snoop_inline_pio_send(struct hfi1_devdata *dd, struct pio_buf *pbuf,
1805                            u64 pbc, const void *from, size_t count)
1806 {
1807         int snoop_mode = 0;
1808         int md_len = 0;
1809         struct capture_md md;
1810         struct snoop_packet *s_packet = NULL;
1811
1812         /*
1813          * count is in dwords so we need to convert to bytes.
1814          * We also need to account for CRC which would be tacked on by hardware.
1815          */
1816         int packet_len = (count << 2) + 4;
1817         int ret;
1818
1819         snoop_dbg("ACK OUT: len %d", packet_len);
1820
1821         if (!dd->hfi1_snoop.filter_callback) {
1822                 snoop_dbg("filter not set");
1823                 ret = HFI1_FILTER_HIT;
1824         } else {
1825                 ret = dd->hfi1_snoop.filter_callback(
1826                                 (struct hfi1_ib_header *)from,
1827                                 NULL,
1828                                 dd->hfi1_snoop.filter_value);
1829         }
1830
1831         switch (ret) {
1832         case HFI1_FILTER_ERR:
1833                 snoop_dbg("Error in filter call");
1834                 /* fall through */
1835         case HFI1_FILTER_MISS:
1836                 snoop_dbg("Filter Miss");
1837                 break;
1838         case HFI1_FILTER_HIT:
1839                 snoop_dbg("Capturing packet");
1840                 if (dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1841                         snoop_mode = 1;
1842                 if ((snoop_mode == 0) ||
1843                     unlikely(snoop_flags & SNOOP_USE_METADATA))
1844                         md_len = sizeof(struct capture_md);
1845
1846                 s_packet = allocate_snoop_packet(packet_len, 0, md_len);
1847
1848                 if (unlikely(s_packet == NULL)) {
1849                         dd_dev_warn_ratelimited(dd, "Unable to allocate snoop/capture packet\n");
1850                         goto inline_pio_out;
1851                 }
1852
1853                 s_packet->total_len = packet_len + md_len;
1854
1855                 /* Fill in the metadata for the packet */
1856                 if (md_len > 0) {
1857                         memset(&md, 0, sizeof(struct capture_md));
1858                         md.port = 1;
1859                         md.dir = PKT_DIR_EGRESS;
1860                         md.u.pbc = pbc;
1861                         memcpy(s_packet->data, &md, md_len);
1862                 }
1863
1864                 /* Add the packet data which is a single buffer */
1865                 memcpy(s_packet->data + md_len, from, packet_len);
1866
1867                 snoop_list_add_tail(s_packet, dd);
1868
1869                 if (unlikely((snoop_flags & SNOOP_DROP_SEND) && snoop_mode)) {
1870                         snoop_dbg("Dropping packet");
1871                         return;
1872                 }
1873                 break;
1874         default:
1875                 break;
1876         }
1877
1878 inline_pio_out:
1879         pio_copy(dd, pbuf, pbc, from, count);
1880
1881 }