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