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.
8 * Copyright(c) 2015 Intel Corporation.
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.
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.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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
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.
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.
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.
57 * Accesses to the chip through this interface are not similar to going
58 * through the /sys/bus/pci resource mmap interface.
62 #include <linux/pci.h>
63 #include <linux/poll.h>
64 #include <linux/vmalloc.h>
65 #include <linux/export.h>
67 #include <linux/uaccess.h>
68 #include <linux/module.h>
69 #include <rdma/ib_smi.h>
76 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
77 #define snoop_dbg(fmt, ...) \
78 hfi1_cdbg(SNOOP, fmt, ##__VA_ARGS__)
80 /* Snoop option mask */
81 #define SNOOP_DROP_SEND BIT(0)
82 #define SNOOP_USE_METADATA BIT(1)
84 static u8 snoop_flags;
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
91 #define HFI1_GET_PKT_LEN(x) (((be16_to_cpu((x)->lrh[2]) & 0x7FF)) << 2)
93 enum hfi1_filter_status {
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
111 /* Snoop packet structure */
112 struct snoop_packet {
113 struct list_head list;
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
122 /* Packet capture metadata returned to the user with the packet. */
133 static atomic_t diagpkt_count = ATOMIC_INIT(0);
134 static struct cdev diagpkt_cdev;
135 static struct device *diagpkt_device;
137 static ssize_t diagpkt_write(struct file *fp, const char __user *data,
138 size_t count, loff_t *off);
140 static const struct file_operations diagpkt_file_ops = {
141 .owner = THIS_MODULE,
142 .write = diagpkt_write,
143 .llseek = noop_llseek,
147 * This is used for communication with user space for snoop extended IOCTLs
149 struct hfi1_link_info {
153 u16 link_speed_active;
154 u16 link_width_active;
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
161 * Be sure to run tests on all IOCTLs when making changes to this
168 * This starts our ioctl sequence numbers *way* off from the ones
169 * defined in ib_core.
171 #define SNOOP_CAPTURE_VERSION 0x1
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
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)
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.
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)
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);
213 struct hfi1_packet_filter_command {
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
223 enum hfi1_packet_filter_opcodes {
226 FILTER_BY_MAD_MGMT_CLASS,
229 FILTER_BY_SERVICE_LEVEL,
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
244 struct hfi1_filter_array {
245 int (*filter)(void *, void *, void *);
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,
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,
255 static int hfi1_filter_ib_service_level(void *ibhdr, void *packet_data,
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);
260 static struct hfi1_filter_array hfi1_filters[] = {
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 },
271 #define HFI1_MAX_FILTERS ARRAY_SIZE(hfi1_filters)
272 #define HFI1_DIAG_MINOR_BASE 129
274 static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name);
276 int hfi1_diag_add(struct hfi1_devdata *dd)
281 snprintf(name, sizeof(name), "%s_diagpkt%d", class_name(),
284 * Do this for each device as opposed to the normal diagpkt
285 * interface which is one per host
287 ret = hfi1_snoop_add(dd, name);
289 dd_dev_err(dd, "Unable to init snoop/capture device");
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);
301 /* this must be called w/ dd->snoop_in_lock held */
302 static void drain_snoop_list(struct list_head *queue)
304 struct list_head *pos, *q;
305 struct snoop_packet *packet;
307 list_for_each_safe(pos, q, queue) {
308 packet = list_entry(pos, struct snoop_packet, list);
314 static void hfi1_snoop_remove(struct hfi1_devdata *dd)
316 unsigned long flags = 0;
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);
324 void hfi1_diag_remove(struct hfi1_devdata *dd)
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);
335 * Allocated structure shared between the credit return mechanism and
338 struct diagpkt_wait {
339 struct completion credits_returned;
345 * When each side is finished with the structure, they call this.
346 * The last user frees the structure.
348 static void put_diagpkt_wait(struct diagpkt_wait *wait)
350 if (atomic_dec_and_test(&wait->count))
355 * Callback from the credit return code. Set the complete, which
356 * will let diapkt_send() continue.
358 static void diagpkt_complete(void *arg, int code)
360 struct diagpkt_wait *wait = (struct diagpkt_wait *)arg;
363 complete(&wait->credits_returned);
364 put_diagpkt_wait(wait); /* finished with the structure */
368 * diagpkt_send - send a packet
369 * @dp: diag packet descriptor
371 static ssize_t diagpkt_send(struct diag_pkt *dp)
373 struct hfi1_devdata *dd;
374 struct send_context *sc;
375 struct pio_buf *pbuf;
378 u32 pkt_len, total_len;
379 pio_release_cb credit_cb = NULL;
380 void *credit_arg = NULL;
381 struct diagpkt_wait *wait = NULL;
384 dd = hfi1_lookup(dp->unit);
385 if (!dd || !(dd->flags & HFI1_PRESENT) || !dd->kregbase) {
389 if (!(dd->flags & HFI1_INITTED)) {
390 /* no hardware, freeze, etc. */
395 if (dp->version != _DIAG_PKT_VERS) {
396 dd_dev_err(dd, "Invalid version %u for diagpkt_write\n",
402 /* send count must be an exact number of dwords */
408 /* there is only port 1 */
414 /* need a valid context */
415 if (dp->sw_index >= dd->num_send_contexts) {
419 /* can only use kernel contexts */
420 if (dd->send_contexts[dp->sw_index].type != SC_KERNEL) {
424 /* must be allocated */
425 sc = dd->send_contexts[dp->sw_index].sc;
430 /* must be enabled */
431 if (!(sc->flags & SCF_ENABLED)) {
436 /* allocate a buffer and copy the data in */
437 tmpbuf = vmalloc(dp->len);
443 if (copy_from_user(tmpbuf,
444 (const void __user *) (unsigned long) dp->data,
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
455 pkt_len = dp->len >> 2;
456 total_len = pkt_len + 2; /* PBC + packet */
458 /* if 0, fill in a default */
460 struct hfi1_pportdata *ppd = dd->pport;
462 hfi1_cdbg(PKT, "Generating PBC");
463 dp->pbc = create_pbc(ppd, 0, 0, 0, total_len);
465 hfi1_cdbg(PKT, "Using passed in PBC");
468 hfi1_cdbg(PKT, "Egress PBC content is 0x%llx", dp->pbc);
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
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);
489 init_completion(&wait->credits_returned);
490 atomic_set(&wait->count, 2);
493 credit_cb = diagpkt_complete;
498 pbuf = sc_buffer_alloc(sc, total_len, credit_cb, credit_arg);
501 /* force a credit return and try again */
502 sc_return_credits(sc);
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.
511 if (dp->flags & F_DIAGPKT_WAIT) {
512 sc_del_credit_return_intr(sc);
520 pio_copy(dd, pbuf, dp->pbc, tmpbuf, pkt_len);
521 /* no flush needed as the HW knows the packet size */
525 if (dp->flags & F_DIAGPKT_WAIT) {
526 /* wait for credit return */
527 ret = wait_for_completion_interruptible(
528 &wait->credits_returned);
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.
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
541 if (!ret && (((wait->code & PRC_STATUS_ERR)
542 || (wait->code & PRC_FILL_ERR)
543 || (wait->code & PRC_SC_DISABLE))))
546 put_diagpkt_wait(wait); /* finished with the structure */
547 sc_del_credit_return_intr(sc);
555 static ssize_t diagpkt_write(struct file *fp, const char __user *data,
556 size_t count, loff_t *off)
558 struct hfi1_devdata *dd;
559 struct send_context *sc;
564 if (count != sizeof(dp))
567 if (copy_from_user(&dp, data, sizeof(dp)))
571 * The Send Context is derived from the PbcVL value
572 * if PBC is populated
575 dd = hfi1_lookup(dp.unit);
578 vl = (dp.pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
581 dp.sw_index = sc->sw_index;
584 "Packet sent over VL %d via Send Context %u(%u)",
585 vl, sc->sw_index, sc->hw_context);
589 return diagpkt_send(&dp);
592 static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name)
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);
601 ret = hfi1_cdev_init(HFI1_SNOOP_CAPTURE_BASE + dd->unit, name,
603 &dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev,
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);
615 static struct hfi1_devdata *hfi1_dd_from_sc_inode(struct inode *in)
617 int unit = iminor(in) - HFI1_SNOOP_CAPTURE_BASE;
618 struct hfi1_devdata *dd;
620 dd = hfi1_lookup(unit);
625 /* clear or restore send context integrity checks */
626 static void adjust_integrity_checks(struct hfi1_devdata *dd)
628 struct send_context *sc;
629 unsigned long sc_flags;
632 spin_lock_irqsave(&dd->sc_lock, sc_flags);
633 for (i = 0; i < dd->num_send_contexts; i++) {
636 sc = dd->send_contexts[i].sc;
639 continue; /* not allocated */
641 enable = likely(!HFI1_CAP_IS_KSET(NO_INTEGRITY)) &&
642 dd->hfi1_snoop.mode_flag != HFI1_PORT_SNOOP_MODE;
644 set_pio_integrity(sc);
646 if (enable) /* take HFI_CAP_* flags into account */
649 spin_unlock_irqrestore(&dd->sc_lock, sc_flags);
652 static int hfi1_snoop_open(struct inode *in, struct file *fp)
656 unsigned long flags = 0;
657 struct hfi1_devdata *dd;
658 struct list_head *queue;
660 mutex_lock(&hfi1_mutex);
662 dd = hfi1_dd_from_sc_inode(in);
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.
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;
682 snoop_dbg("Invalid");
686 queue = &dd->hfi1_snoop.queue;
689 * We are not supporting snoop and capture at the same time.
691 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
692 if (dd->hfi1_snoop.mode_flag) {
694 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
698 dd->hfi1_snoop.mode_flag = mode_flag;
699 drain_snoop_list(queue);
701 dd->hfi1_snoop.filter_callback = NULL;
702 dd->hfi1_snoop.filter_value = NULL;
705 * Send side packet integrity checks are not helpful when snooping so
706 * disable and re-enable when we stop snooping.
708 if (mode_flag == HFI1_PORT_SNOOP_MODE) {
709 /* clear after snoop mode is on */
710 adjust_integrity_checks(dd); /* clear */
713 * We also do not want to be doing the DLID LMC check for
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);
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.
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;
734 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
738 mutex_unlock(&hfi1_mutex);
743 static int hfi1_snoop_release(struct inode *in, struct file *fp)
745 unsigned long flags = 0;
746 struct hfi1_devdata *dd;
749 dd = hfi1_dd_from_sc_inode(in);
753 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
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;
760 * Drain the queue and clear the filters we are done with it. Don't
761 * forget to restore the packet integrity checks
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 */
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.
773 write_csr(dd, DCC_CFG_PORT_CONFIG1, dd->hfi1_snoop.dcc_cfg);
776 dd->hfi1_snoop.filter_callback = NULL;
777 kfree(dd->hfi1_snoop.filter_value);
778 dd->hfi1_snoop.filter_value = NULL;
781 * User is done snooping and capturing, return control to the normal
782 * handler. Re-enable SDMA handling.
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;
789 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
791 snoop_dbg("snoop/capture device released");
796 static unsigned int hfi1_snoop_poll(struct file *fp,
797 struct poll_table_struct *wait)
800 unsigned long flags = 0;
802 struct hfi1_devdata *dd;
804 dd = hfi1_dd_from_sc_inode(fp->f_inode);
808 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
810 poll_wait(fp, &dd->hfi1_snoop.waitq, wait);
811 if (!list_empty(&dd->hfi1_snoop.queue))
812 ret |= POLLIN | POLLRDNORM;
814 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
819 static ssize_t hfi1_snoop_write(struct file *fp, const char __user *data,
820 size_t count, loff_t *off)
822 struct diag_pkt dpkt;
823 struct hfi1_devdata *dd;
825 u8 byte_two, sl, sc5, sc4, vl, byte_one;
826 struct send_context *sc;
829 struct hfi1_ibport *ibp;
830 struct hfi1_pportdata *ppd;
832 dd = hfi1_dd_from_sc_inode(fp->f_inode);
837 snoop_dbg("received %lu bytes from user", count);
839 memset(&dpkt, 0, sizeof(struct diag_pkt));
840 dpkt.version = _DIAG_PKT_VERS;
841 dpkt.unit = dd->unit;
844 if (likely(!(snoop_flags & SNOOP_USE_METADATA))) {
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.
855 if (copy_from_user(&byte_one, data, 1))
858 if (copy_from_user(&byte_two, data+1, 1))
861 sc4 = (byte_one >> 4) & 0xf;
863 snoop_dbg("Detected VL15 packet ignoring SL in packet");
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);
871 snoop_dbg("VL %d does not match SC %d of packet",
877 sc = dd->vld[vl].sc; /* Look up the context based on VL */
879 dpkt.sw_index = sc->sw_index;
880 snoop_dbg("Sending on context %u(%u)", sc->sw_index,
883 snoop_dbg("Could not find context for vl %d", vl);
887 len = (count >> 2) + 2; /* Add in PBC */
888 pbc = create_pbc(ppd, 0, 0, vl, len);
890 if (copy_from_user(&pbc, data, sizeof(pbc)))
892 vl = (pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
893 sc = dd->vld[vl].sc; /* Look up the context based on VL */
895 dpkt.sw_index = sc->sw_index;
897 snoop_dbg("Could not find context for vl %d", vl);
901 count -= sizeof(pbc);
904 dpkt.data = (unsigned long)data;
906 snoop_dbg("PBC: vl=0x%llx Length=0x%llx",
911 ret = diagpkt_send(&dpkt);
913 * diagpkt_send only returns number of bytes in the diagpkt so patch
914 * that up here before returning.
916 if (ret == sizeof(dpkt))
922 static ssize_t hfi1_snoop_read(struct file *fp, char __user *data,
923 size_t pkt_len, loff_t *off)
926 unsigned long flags = 0;
927 struct snoop_packet *packet = NULL;
928 struct hfi1_devdata *dd;
930 dd = hfi1_dd_from_sc_inode(fp->f_inode);
934 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
936 while (list_empty(&dd->hfi1_snoop.queue)) {
937 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
939 if (fp->f_flags & O_NONBLOCK)
942 if (wait_event_interruptible(
943 dd->hfi1_snoop.waitq,
944 !list_empty(&dd->hfi1_snoop.queue)))
947 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
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,
960 ret = packet->total_len;
966 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
971 static long hfi1_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
973 struct hfi1_devdata *dd;
974 void *filter_value = NULL;
980 unsigned long flags = 0;
981 unsigned long *argp = NULL;
982 struct hfi1_packet_filter_command filter_cmd = {0};
984 struct hfi1_pportdata *ppd = NULL;
986 struct hfi1_link_info link_info;
988 dd = hfi1_dd_from_sc_inode(fp->f_inode);
992 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
994 mode_flag = dd->hfi1_snoop.mode_flag;
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)))) {
1001 } else if (!capable(CAP_SYS_ADMIN)) {
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.
1016 case HFI1_SNOOP_IOCSETLINKSTATE:
1017 snoop_dbg("HFI1_SNOOP_IOCSETLINKSTATE is not valid");
1021 case HFI1_SNOOP_IOCSETLINKSTATE_EXTRA:
1022 memset(&link_info, 0, sizeof(link_info));
1024 if (copy_from_user(&link_info,
1025 (struct hfi1_link_info __user *)arg,
1029 value = link_info.port_state;
1030 index = link_info.port_number;
1031 if (index > dd->num_pports - 1) {
1036 ppd = &dd->pport[index];
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);
1047 switch (linkState) {
1053 switch (physState) {
1055 devState = HLS_DN_DOWNDEF;
1058 devState = HLS_DN_POLL;
1061 devState = HLS_DN_DISABLE;
1067 ret = set_link_state(ppd, devState);
1070 ret = set_link_state(ppd, HLS_UP_ARMED);
1072 send_idle_sma(dd, SMA_IDLE_ARM);
1074 case IB_PORT_ACTIVE:
1075 ret = set_link_state(ppd, HLS_UP_ACTIVE);
1077 send_idle_sma(dd, SMA_IDLE_ACTIVE);
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,
1095 index = link_info.port_number;
1097 ret = __get_user(index, (int __user *) arg);
1102 if (index > dd->num_pports - 1) {
1107 ppd = &dd->pport[index];
1112 value = hfi1_ibphys_portstate(ppd);
1114 value |= driver_lstate(ppd);
1116 snoop_dbg("Link port | Link State: %d", value);
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;
1127 (struct hfi1_link_info __user *)arg,
1128 &link_info, sizeof(link_info)))
1131 ret = __put_user(value, (int __user *)arg);
1135 case HFI1_SNOOP_IOCCLEARQUEUE:
1136 snoop_dbg("Clearing snoop queue");
1137 drain_snoop_list(&dd->hfi1_snoop.queue);
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;
1147 kfree(dd->hfi1_snoop.filter_value);
1148 dd->hfi1_snoop.filter_value = NULL;
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))) {
1160 if (filter_cmd.opcode >= HFI1_MAX_FILTERS) {
1161 pr_alert("Invalid opcode in request\n");
1166 snoop_dbg("Opcode %d Len %d Ptr %p",
1167 filter_cmd.opcode, filter_cmd.length,
1168 filter_cmd.value_ptr);
1170 filter_value = kcalloc(filter_cmd.length, sizeof(u8),
1172 if (!filter_value) {
1173 pr_alert("Not enough memory\n");
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);
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;
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);
1199 case HFI1_SNOOP_IOCSET_OPTS:
1201 ret = __get_user(value, (int __user *) arg);
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;
1217 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1221 static void snoop_list_add_tail(struct snoop_packet *packet,
1222 struct hfi1_devdata *dd)
1224 unsigned long flags = 0;
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");
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.
1239 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1240 wake_up_interruptible(&dd->hfi1_snoop.waitq);
1243 static inline int hfi1_filter_check(void *val, const char *msg)
1246 snoop_dbg("Error invalid %s value for filter", msg);
1247 return HFI1_FILTER_ERR;
1252 static int hfi1_filter_lid(void *ibhdr, void *packet_data, void *value)
1254 struct hfi1_ib_header *hdr;
1257 ret = hfi1_filter_check(ibhdr, "header");
1260 ret = hfi1_filter_check(value, "user");
1263 hdr = (struct hfi1_ib_header *)ibhdr;
1265 if (*((u16 *)value) == be16_to_cpu(hdr->lrh[3])) /* matches slid */
1266 return HFI1_FILTER_HIT; /* matched */
1268 return HFI1_FILTER_MISS; /* Not matched */
1271 static int hfi1_filter_dlid(void *ibhdr, void *packet_data, void *value)
1273 struct hfi1_ib_header *hdr;
1276 ret = hfi1_filter_check(ibhdr, "header");
1279 ret = hfi1_filter_check(value, "user");
1283 hdr = (struct hfi1_ib_header *)ibhdr;
1285 if (*((u16 *)value) == be16_to_cpu(hdr->lrh[1]))
1286 return HFI1_FILTER_HIT;
1288 return HFI1_FILTER_MISS;
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,
1295 struct hfi1_ib_header *hdr;
1296 struct hfi1_other_headers *ohdr = NULL;
1297 struct ib_smp *smp = NULL;
1301 ret = hfi1_filter_check(ibhdr, "header");
1304 ret = hfi1_filter_check(packet_data, "packet_data");
1307 ret = hfi1_filter_check(value, "user");
1311 hdr = (struct hfi1_ib_header *)ibhdr;
1314 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1315 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
1317 ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
1319 qpn = be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF;
1321 smp = (struct ib_smp *)packet_data;
1322 if (*((u8 *)value) == smp->mgmt_class)
1323 return HFI1_FILTER_HIT;
1325 return HFI1_FILTER_MISS;
1327 return HFI1_FILTER_ERR;
1330 static int hfi1_filter_qp_number(void *ibhdr, void *packet_data, void *value)
1333 struct hfi1_ib_header *hdr;
1334 struct hfi1_other_headers *ohdr = NULL;
1337 ret = hfi1_filter_check(ibhdr, "header");
1340 ret = hfi1_filter_check(value, "user");
1344 hdr = (struct hfi1_ib_header *)ibhdr;
1347 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1348 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
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;
1354 return HFI1_FILTER_MISS;
1357 static int hfi1_filter_ibpacket_type(void *ibhdr, void *packet_data,
1362 struct hfi1_ib_header *hdr;
1363 struct hfi1_other_headers *ohdr = NULL;
1366 ret = hfi1_filter_check(ibhdr, "header");
1369 ret = hfi1_filter_check(value, "user");
1373 hdr = (struct hfi1_ib_header *)ibhdr;
1375 lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1377 if (lnh == HFI1_LRH_BTH)
1379 else if (lnh == HFI1_LRH_GRH)
1380 ohdr = &hdr->u.l.oth;
1382 return HFI1_FILTER_ERR;
1384 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1386 if (*((u8 *)value) == ((opcode >> 5) & 0x7))
1387 return HFI1_FILTER_HIT;
1389 return HFI1_FILTER_MISS;
1392 static int hfi1_filter_ib_service_level(void *ibhdr, void *packet_data,
1395 struct hfi1_ib_header *hdr;
1398 ret = hfi1_filter_check(ibhdr, "header");
1401 ret = hfi1_filter_check(value, "user");
1405 hdr = (struct hfi1_ib_header *)ibhdr;
1407 if ((*((u8 *)value)) == ((be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF))
1408 return HFI1_FILTER_HIT;
1410 return HFI1_FILTER_MISS;
1413 static int hfi1_filter_ib_pkey(void *ibhdr, void *packet_data, void *value)
1417 struct hfi1_ib_header *hdr;
1418 struct hfi1_other_headers *ohdr = NULL;
1421 ret = hfi1_filter_check(ibhdr, "header");
1424 ret = hfi1_filter_check(value, "user");
1428 hdr = (struct hfi1_ib_header *)ibhdr;
1430 lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1431 if (lnh == HFI1_LRH_BTH)
1433 else if (lnh == HFI1_LRH_GRH)
1434 ohdr = &hdr->u.l.oth;
1436 return HFI1_FILTER_ERR;
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
1446 if ((*(u16 *)value & 0x7FFF) ==
1447 ((be32_to_cpu(ohdr->bth[0])) & 0x7FFF))
1448 return HFI1_FILTER_HIT;
1450 return HFI1_FILTER_MISS;
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.
1457 static int hfi1_filter_direction(void *ibhdr, void *packet_data, void *value)
1459 u8 user_dir = *(u8 *)value;
1462 ret = hfi1_filter_check(value, "user");
1467 /* Incoming packet */
1468 if (user_dir & HFI1_SNOOP_INGRESS)
1469 return HFI1_FILTER_HIT;
1471 /* Outgoing packet */
1472 if (user_dir & HFI1_SNOOP_EGRESS)
1473 return HFI1_FILTER_HIT;
1476 return HFI1_FILTER_MISS;
1480 * Allocate a snoop packet. The structure that is stored in the ring buffer, not
1481 * to be confused with an hfi packet type.
1483 static struct snoop_packet *allocate_snoop_packet(u32 hdr_len,
1488 struct snoop_packet *packet;
1490 packet = kzalloc(sizeof(struct snoop_packet) + hdr_len + data_len
1492 GFP_ATOMIC | __GFP_NOWARN);
1494 INIT_LIST_HEAD(&packet->list);
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.
1511 int snoop_recv_handler(struct hfi1_packet *packet)
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;
1522 struct capture_md md;
1524 snoop_dbg("PACKET IN: hdr size %d tlen %d data %p", header_size, tlen,
1527 trace_snoop_capture(ppd->dd, header_size, hdr, tlen - header_size,
1530 if (!ppd->dd->hfi1_snoop.filter_callback) {
1531 snoop_dbg("filter not set");
1532 ret = HFI1_FILTER_HIT;
1534 ret = ppd->dd->hfi1_snoop.filter_callback(hdr, data,
1535 ppd->dd->hfi1_snoop.filter_value);
1539 case HFI1_FILTER_ERR:
1540 snoop_dbg("Error in filter call");
1542 case HFI1_FILTER_MISS:
1543 snoop_dbg("Filter Miss");
1545 case HFI1_FILTER_HIT:
1547 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1549 if ((snoop_mode == 0) ||
1550 unlikely(snoop_flags & SNOOP_USE_METADATA))
1551 md_len = sizeof(struct capture_md);
1554 s_packet = allocate_snoop_packet(header_size,
1558 if (unlikely(s_packet == NULL)) {
1559 dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1564 memset(&md, 0, sizeof(struct capture_md));
1566 md.dir = PKT_DIR_INGRESS;
1567 md.u.rhf = packet->rhf;
1568 memcpy(s_packet->data, &md, md_len);
1571 /* We should always have a header */
1573 memcpy(s_packet->data + md_len, hdr, header_size);
1575 dd_dev_err(ppd->dd, "Unable to copy header to snoop/capture packet\n");
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.
1588 memcpy(s_packet->data + header_size + md_len, data,
1589 tlen - header_size);
1591 s_packet->total_len = tlen + md_len;
1592 snoop_list_add_tail(s_packet, ppd->dd);
1595 * If we are snooping the packet not capturing then throw away
1596 * after adding to the list.
1598 snoop_dbg("Capturing packet");
1599 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE) {
1600 snoop_dbg("Throwing packet away");
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.
1607 if (unlikely(rhf_err_flags(packet->rhf)))
1608 handle_eflags(packet);
1610 /* throw the packet on the floor */
1611 return RHF_RCV_CONTINUE;
1619 * We do not care what type of packet came in here - just pass it off
1620 * to the normal handler.
1622 return ppd->dd->normal_rhf_rcv_functions[rhf_rcv_type(packet->rhf)]
1627 * Handle snooping and capturing packets when sdma is being used.
1629 int snoop_send_dma_handler(struct hfi1_qp *qp, struct hfi1_pkt_state *ps,
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);
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.
1642 int snoop_send_pio_handler(struct hfi1_qp *qp, struct hfi1_pkt_state *ps,
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;
1655 struct hfi1_sge_state temp_ss;
1657 void *data_start = NULL;
1661 struct capture_md md;
1663 u32 hdr_len = hdrwords << 2;
1664 u32 tlen = HFI1_GET_PKT_LEN(&ahdr->ibh);
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)
1672 if ((snoop_mode == 0) ||
1673 unlikely(snoop_flags & SNOOP_USE_METADATA))
1674 md_len = sizeof(struct capture_md);
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);
1679 if (unlikely(s_packet == NULL)) {
1680 dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1684 s_packet->total_len = tlen + md_len;
1687 memset(&md, 0, sizeof(struct capture_md));
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);
1696 memcpy(s_packet->data, &md, md_len);
1703 memcpy(s_packet->data + md_len, hdr, hdr_len);
1706 "Unable to copy header to snoop/capture packet\n");
1712 data = s_packet->data + hdr_len + md_len;
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.
1725 snoop_dbg("Need to copy %d bytes", length);
1727 void *addr = temp_ss.sge.vaddr;
1728 u32 slen = temp_ss.sge.length;
1730 if (slen > length) {
1732 snoop_dbg("slen %d > len %d", slen, length);
1734 snoop_dbg("copy %d to %p", slen, addr);
1735 memcpy(data, addr, slen);
1736 update_sge(&temp_ss, slen);
1739 snoop_dbg("data is now %p bytes left %d", data, length);
1741 snoop_dbg("Completed SGE copy");
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.
1748 if (!ppd->dd->hfi1_snoop.filter_callback) {
1749 snoop_dbg("filter not set\n");
1750 ret = HFI1_FILTER_HIT;
1752 ret = ppd->dd->hfi1_snoop.filter_callback(
1755 ppd->dd->hfi1_snoop.filter_value);
1759 case HFI1_FILTER_ERR:
1760 snoop_dbg("Error in filter call");
1762 case HFI1_FILTER_MISS:
1763 snoop_dbg("Filter Miss");
1766 case HFI1_FILTER_HIT:
1767 snoop_dbg("Capturing packet");
1768 snoop_list_add_tail(s_packet, ppd->dd);
1770 if (unlikely((snoop_flags & SNOOP_DROP_SEND) &&
1771 (ppd->dd->hfi1_snoop.mode_flag &
1772 HFI1_PORT_SNOOP_MODE))) {
1773 unsigned long flags;
1775 snoop_dbg("Dropping packet");
1777 spin_lock_irqsave(&qp->s_lock, flags);
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);
1796 return hfi1_verbs_send_pio(qp, ps, md.u.pbc);
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.
1804 void snoop_inline_pio_send(struct hfi1_devdata *dd, struct pio_buf *pbuf,
1805 u64 pbc, const void *from, size_t count)
1809 struct capture_md md;
1810 struct snoop_packet *s_packet = NULL;
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.
1816 int packet_len = (count << 2) + 4;
1819 snoop_dbg("ACK OUT: len %d", packet_len);
1821 if (!dd->hfi1_snoop.filter_callback) {
1822 snoop_dbg("filter not set");
1823 ret = HFI1_FILTER_HIT;
1825 ret = dd->hfi1_snoop.filter_callback(
1826 (struct hfi1_ib_header *)from,
1828 dd->hfi1_snoop.filter_value);
1832 case HFI1_FILTER_ERR:
1833 snoop_dbg("Error in filter call");
1835 case HFI1_FILTER_MISS:
1836 snoop_dbg("Filter Miss");
1838 case HFI1_FILTER_HIT:
1839 snoop_dbg("Capturing packet");
1840 if (dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1842 if ((snoop_mode == 0) ||
1843 unlikely(snoop_flags & SNOOP_USE_METADATA))
1844 md_len = sizeof(struct capture_md);
1846 s_packet = allocate_snoop_packet(packet_len, 0, md_len);
1848 if (unlikely(s_packet == NULL)) {
1849 dd_dev_warn_ratelimited(dd, "Unable to allocate snoop/capture packet\n");
1850 goto inline_pio_out;
1853 s_packet->total_len = packet_len + md_len;
1855 /* Fill in the metadata for the packet */
1857 memset(&md, 0, sizeof(struct capture_md));
1859 md.dir = PKT_DIR_EGRESS;
1861 memcpy(s_packet->data, &md, md_len);
1864 /* Add the packet data which is a single buffer */
1865 memcpy(s_packet->data + md_len, from, packet_len);
1867 snoop_list_add_tail(s_packet, dd);
1869 if (unlikely((snoop_flags & SNOOP_DROP_SEND) && snoop_mode)) {
1870 snoop_dbg("Dropping packet");
1879 pio_copy(dd, pbuf, pbc, from, count);