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 (1 << 0)
82 #define SNOOP_USE_METADATA (1 << 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,
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;
383 dd = hfi1_lookup(dp->unit);
384 if (!dd || !(dd->flags & HFI1_PRESENT) || !dd->kregbase) {
388 if (!(dd->flags & HFI1_INITTED)) {
389 /* no hardware, freeze, etc. */
394 if (dp->version != _DIAG_PKT_VERS) {
395 dd_dev_err(dd, "Invalid version %u for diagpkt_write\n",
401 /* send count must be an exact number of dwords */
407 /* there is only port 1 */
413 /* need a valid context */
414 if (dp->sw_index >= dd->num_send_contexts) {
418 /* can only use kernel contexts */
419 if (dd->send_contexts[dp->sw_index].type != SC_KERNEL) {
423 /* must be allocated */
424 sc = dd->send_contexts[dp->sw_index].sc;
429 /* must be enabled */
430 if (!(sc->flags & SCF_ENABLED)) {
435 /* allocate a buffer and copy the data in */
436 tmpbuf = vmalloc(dp->len);
442 if (copy_from_user(tmpbuf,
443 (const void __user *) (unsigned long) dp->data,
450 * pkt_len is how much data we have to write, includes header and data.
451 * total_len is length of the packet in Dwords plus the PBC should not
454 pkt_len = dp->len >> 2;
455 total_len = pkt_len + 2; /* PBC + packet */
457 /* if 0, fill in a default */
459 struct hfi1_pportdata *ppd = dd->pport;
461 hfi1_cdbg(PKT, "Generating PBC");
462 dp->pbc = create_pbc(ppd, 0, 0, 0, total_len);
464 hfi1_cdbg(PKT, "Using passed in PBC");
467 hfi1_cdbg(PKT, "Egress PBC content is 0x%llx", dp->pbc);
470 * The caller wants to wait until the packet is sent and to
471 * check for errors. The best we can do is wait until
472 * the buffer credits are returned and check if any packet
473 * error has occurred. If there are any late errors, this
474 * could miss it. If there are other senders who generate
475 * an error, this may find it. However, in general, it
478 if (dp->flags & F_DIAGPKT_WAIT) {
479 /* always force a credit return */
480 dp->pbc |= PBC_CREDIT_RETURN;
481 /* turn on credit return interrupts */
482 sc_add_credit_return_intr(sc);
483 wait = kmalloc(sizeof(*wait), GFP_KERNEL);
488 init_completion(&wait->credits_returned);
489 atomic_set(&wait->count, 2);
492 credit_cb = diagpkt_complete;
496 pbuf = sc_buffer_alloc(sc, total_len, credit_cb, credit_arg);
499 * No send buffer means no credit callback. Undo
500 * the wait set-up that was done above. We free wait
501 * because the callback will never be called.
503 if (dp->flags & F_DIAGPKT_WAIT) {
504 sc_del_credit_return_intr(sc);
512 pio_copy(dd, pbuf, dp->pbc, tmpbuf, pkt_len);
513 /* no flush needed as the HW knows the packet size */
517 if (dp->flags & F_DIAGPKT_WAIT) {
518 /* wait for credit return */
519 ret = wait_for_completion_interruptible(
520 &wait->credits_returned);
522 * If the wait returns an error, the wait was interrupted,
523 * e.g. with a ^C in the user program. The callback is
524 * still pending. This is OK as the wait structure is
525 * kmalloc'ed and the structure will free itself when
526 * all users are done with it.
528 * A context disable occurs on a send context restart, so
529 * include that in the list of errors below to check for.
530 * NOTE: PRC_FILL_ERR is at best informational and cannot
533 if (!ret && (((wait->code & PRC_STATUS_ERR)
534 || (wait->code & PRC_FILL_ERR)
535 || (wait->code & PRC_SC_DISABLE))))
538 put_diagpkt_wait(wait); /* finished with the structure */
539 sc_del_credit_return_intr(sc);
547 static ssize_t diagpkt_write(struct file *fp, const char __user *data,
548 size_t count, loff_t *off)
550 struct hfi1_devdata *dd;
551 struct send_context *sc;
556 if (count != sizeof(dp))
559 if (copy_from_user(&dp, data, sizeof(dp)))
563 * The Send Context is derived from the PbcVL value
564 * if PBC is populated
567 dd = hfi1_lookup(dp.unit);
570 vl = (dp.pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
573 dp.sw_index = sc->sw_index;
576 "Packet sent over VL %d via Send Context %u(%u)",
577 vl, sc->sw_index, sc->hw_context);
581 return diagpkt_send(&dp);
584 static int hfi1_snoop_add(struct hfi1_devdata *dd, const char *name)
588 dd->hfi1_snoop.mode_flag = 0;
589 spin_lock_init(&dd->hfi1_snoop.snoop_lock);
590 INIT_LIST_HEAD(&dd->hfi1_snoop.queue);
591 init_waitqueue_head(&dd->hfi1_snoop.waitq);
593 ret = hfi1_cdev_init(HFI1_SNOOP_CAPTURE_BASE + dd->unit, name,
595 &dd->hfi1_snoop.cdev, &dd->hfi1_snoop.class_dev);
598 dd_dev_err(dd, "Couldn't create %s device: %d", name, ret);
599 hfi1_cdev_cleanup(&dd->hfi1_snoop.cdev,
600 &dd->hfi1_snoop.class_dev);
606 static struct hfi1_devdata *hfi1_dd_from_sc_inode(struct inode *in)
608 int unit = iminor(in) - HFI1_SNOOP_CAPTURE_BASE;
609 struct hfi1_devdata *dd = NULL;
611 dd = hfi1_lookup(unit);
616 /* clear or restore send context integrity checks */
617 static void adjust_integrity_checks(struct hfi1_devdata *dd)
619 struct send_context *sc;
620 unsigned long sc_flags;
623 spin_lock_irqsave(&dd->sc_lock, sc_flags);
624 for (i = 0; i < dd->num_send_contexts; i++) {
627 sc = dd->send_contexts[i].sc;
630 continue; /* not allocated */
632 enable = likely(!HFI1_CAP_IS_KSET(NO_INTEGRITY)) &&
633 dd->hfi1_snoop.mode_flag != HFI1_PORT_SNOOP_MODE;
635 set_pio_integrity(sc);
637 if (enable) /* take HFI_CAP_* flags into account */
640 spin_unlock_irqrestore(&dd->sc_lock, sc_flags);
643 static int hfi1_snoop_open(struct inode *in, struct file *fp)
647 unsigned long flags = 0;
648 struct hfi1_devdata *dd;
649 struct list_head *queue;
651 mutex_lock(&hfi1_mutex);
653 dd = hfi1_dd_from_sc_inode(in);
660 * File mode determines snoop or capture. Some existing user
661 * applications expect the capture device to be able to be opened RDWR
662 * because they expect a dedicated capture device. For this reason we
663 * support a module param to force capture mode even if the file open
664 * mode matches snoop.
666 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) {
667 snoop_dbg("Capture Enabled");
668 mode_flag = HFI1_PORT_CAPTURE_MODE;
669 } else if ((fp->f_flags & O_ACCMODE) == O_RDWR) {
670 snoop_dbg("Snoop Enabled");
671 mode_flag = HFI1_PORT_SNOOP_MODE;
673 snoop_dbg("Invalid");
677 queue = &dd->hfi1_snoop.queue;
680 * We are not supporting snoop and capture at the same time.
682 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
683 if (dd->hfi1_snoop.mode_flag) {
685 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
689 dd->hfi1_snoop.mode_flag = mode_flag;
690 drain_snoop_list(queue);
692 dd->hfi1_snoop.filter_callback = NULL;
693 dd->hfi1_snoop.filter_value = NULL;
696 * Send side packet integrity checks are not helpful when snooping so
697 * disable and re-enable when we stop snooping.
699 if (mode_flag == HFI1_PORT_SNOOP_MODE) {
700 /* clear after snoop mode is on */
701 adjust_integrity_checks(dd); /* clear */
704 * We also do not want to be doing the DLID LMC check for
707 dd->hfi1_snoop.dcc_cfg = read_csr(dd, DCC_CFG_PORT_CONFIG1);
708 write_csr(dd, DCC_CFG_PORT_CONFIG1,
709 (dd->hfi1_snoop.dcc_cfg >> 32) << 32);
713 * As soon as we set these function pointers the recv and send handlers
714 * are active. This is a race condition so we must make sure to drain
715 * the queue and init filter values above. Technically we should add
716 * locking here but all that will happen is on recv a packet will get
717 * allocated and get stuck on the snoop_lock before getting added to the
718 * queue. Same goes for send.
720 dd->rhf_rcv_function_map = snoop_rhf_rcv_functions;
721 dd->process_pio_send = snoop_send_pio_handler;
722 dd->process_dma_send = snoop_send_pio_handler;
723 dd->pio_inline_send = snoop_inline_pio_send;
725 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
729 mutex_unlock(&hfi1_mutex);
734 static int hfi1_snoop_release(struct inode *in, struct file *fp)
736 unsigned long flags = 0;
737 struct hfi1_devdata *dd;
740 dd = hfi1_dd_from_sc_inode(in);
744 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
746 /* clear the snoop mode before re-adjusting send context CSRs */
747 mode_flag = dd->hfi1_snoop.mode_flag;
748 dd->hfi1_snoop.mode_flag = 0;
751 * Drain the queue and clear the filters we are done with it. Don't
752 * forget to restore the packet integrity checks
754 drain_snoop_list(&dd->hfi1_snoop.queue);
755 if (mode_flag == HFI1_PORT_SNOOP_MODE) {
756 /* restore after snoop mode is clear */
757 adjust_integrity_checks(dd); /* restore */
760 * Also should probably reset the DCC_CONFIG1 register for DLID
761 * checking on incoming packets again. Use the value saved when
762 * opening the snoop device.
764 write_csr(dd, DCC_CFG_PORT_CONFIG1, dd->hfi1_snoop.dcc_cfg);
767 dd->hfi1_snoop.filter_callback = NULL;
768 kfree(dd->hfi1_snoop.filter_value);
769 dd->hfi1_snoop.filter_value = NULL;
772 * User is done snooping and capturing, return control to the normal
773 * handler. Re-enable SDMA handling.
775 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
776 dd->process_pio_send = hfi1_verbs_send_pio;
777 dd->process_dma_send = hfi1_verbs_send_dma;
778 dd->pio_inline_send = pio_copy;
780 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
782 snoop_dbg("snoop/capture device released");
787 static unsigned int hfi1_snoop_poll(struct file *fp,
788 struct poll_table_struct *wait)
791 unsigned long flags = 0;
793 struct hfi1_devdata *dd;
795 dd = hfi1_dd_from_sc_inode(fp->f_inode);
799 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
801 poll_wait(fp, &dd->hfi1_snoop.waitq, wait);
802 if (!list_empty(&dd->hfi1_snoop.queue))
803 ret |= POLLIN | POLLRDNORM;
805 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
810 static ssize_t hfi1_snoop_write(struct file *fp, const char __user *data,
811 size_t count, loff_t *off)
813 struct diag_pkt dpkt;
814 struct hfi1_devdata *dd;
816 u8 byte_two, sl, sc5, sc4, vl, byte_one;
817 struct send_context *sc;
820 struct hfi1_ibport *ibp;
821 struct hfi1_pportdata *ppd;
823 dd = hfi1_dd_from_sc_inode(fp->f_inode);
828 snoop_dbg("received %lu bytes from user", count);
830 memset(&dpkt, 0, sizeof(struct diag_pkt));
831 dpkt.version = _DIAG_PKT_VERS;
832 dpkt.unit = dd->unit;
835 if (likely(!(snoop_flags & SNOOP_USE_METADATA))) {
837 * We need to generate the PBC and not let diagpkt_send do it,
838 * to do this we need the VL and the length in dwords.
839 * The VL can be determined by using the SL and looking up the
840 * SC. Then the SC can be converted into VL. The exception to
841 * this is those packets which are from an SMI queue pair.
842 * Since we can't detect anything about the QP here we have to
843 * rely on the SC. If its 0xF then we assume its SMI and
844 * do not look at the SL.
846 if (copy_from_user(&byte_one, data, 1))
849 if (copy_from_user(&byte_two, data+1, 1))
852 sc4 = (byte_one >> 4) & 0xf;
854 snoop_dbg("Detected VL15 packet ignoring SL in packet");
857 sl = (byte_two >> 4) & 0xf;
858 ibp = to_iport(&dd->verbs_dev.ibdev, 1);
859 sc5 = ibp->sl_to_sc[sl];
860 vl = sc_to_vlt(dd, sc5);
862 snoop_dbg("VL %d does not match SC %d of packet",
868 sc = dd->vld[vl].sc; /* Look up the context based on VL */
870 dpkt.sw_index = sc->sw_index;
871 snoop_dbg("Sending on context %u(%u)", sc->sw_index,
874 snoop_dbg("Could not find context for vl %d", vl);
878 len = (count >> 2) + 2; /* Add in PBC */
879 pbc = create_pbc(ppd, 0, 0, vl, len);
881 if (copy_from_user(&pbc, data, sizeof(pbc)))
883 vl = (pbc >> PBC_VL_SHIFT) & PBC_VL_MASK;
884 sc = dd->vld[vl].sc; /* Look up the context based on VL */
886 dpkt.sw_index = sc->sw_index;
888 snoop_dbg("Could not find context for vl %d", vl);
892 count -= sizeof(pbc);
895 dpkt.data = (unsigned long)data;
897 snoop_dbg("PBC: vl=0x%llx Length=0x%llx",
902 ret = diagpkt_send(&dpkt);
904 * diagpkt_send only returns number of bytes in the diagpkt so patch
905 * that up here before returning.
907 if (ret == sizeof(dpkt))
913 static ssize_t hfi1_snoop_read(struct file *fp, char __user *data,
914 size_t pkt_len, loff_t *off)
917 unsigned long flags = 0;
918 struct snoop_packet *packet = NULL;
919 struct hfi1_devdata *dd;
921 dd = hfi1_dd_from_sc_inode(fp->f_inode);
925 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
927 while (list_empty(&dd->hfi1_snoop.queue)) {
928 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
930 if (fp->f_flags & O_NONBLOCK)
933 if (wait_event_interruptible(
934 dd->hfi1_snoop.waitq,
935 !list_empty(&dd->hfi1_snoop.queue)))
938 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
941 if (!list_empty(&dd->hfi1_snoop.queue)) {
942 packet = list_entry(dd->hfi1_snoop.queue.next,
943 struct snoop_packet, list);
944 list_del(&packet->list);
945 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
946 if (pkt_len >= packet->total_len) {
947 if (copy_to_user(data, packet->data,
951 ret = packet->total_len;
957 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
962 static long hfi1_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
964 struct hfi1_devdata *dd;
965 void *filter_value = NULL;
971 unsigned long flags = 0;
972 unsigned long *argp = NULL;
973 struct hfi1_packet_filter_command filter_cmd = {0};
975 struct hfi1_pportdata *ppd = NULL;
977 struct hfi1_link_info link_info;
979 dd = hfi1_dd_from_sc_inode(fp->f_inode);
983 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
985 mode_flag = dd->hfi1_snoop.mode_flag;
987 if (((_IOC_DIR(cmd) & _IOC_READ)
988 && !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd)))
989 || ((_IOC_DIR(cmd) & _IOC_WRITE)
990 && !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd)))) {
992 } else if (!capable(CAP_SYS_ADMIN)) {
994 } else if ((mode_flag & HFI1_PORT_CAPTURE_MODE) &&
995 (cmd != HFI1_SNOOP_IOCCLEARQUEUE) &&
996 (cmd != HFI1_SNOOP_IOCCLEARFILTER) &&
997 (cmd != HFI1_SNOOP_IOCSETFILTER)) {
998 /* Capture devices are allowed only 3 operations
999 * 1.Clear capture queue
1000 * 2.Clear capture filter
1001 * 3.Set capture filter
1002 * Other are invalid.
1007 case HFI1_SNOOP_IOCSETLINKSTATE:
1008 snoop_dbg("HFI1_SNOOP_IOCSETLINKSTATE is not valid");
1012 case HFI1_SNOOP_IOCSETLINKSTATE_EXTRA:
1013 memset(&link_info, 0, sizeof(link_info));
1015 ret = copy_from_user(&link_info,
1016 (struct hfi1_link_info __user *)arg,
1021 value = link_info.port_state;
1022 index = link_info.port_number;
1023 if (index > dd->num_pports - 1) {
1028 ppd = &dd->pport[index];
1034 /* What we want to transition to */
1035 physState = (value >> 4) & 0xF;
1036 linkState = value & 0xF;
1037 snoop_dbg("Setting link state 0x%x", value);
1039 switch (linkState) {
1045 switch (physState) {
1047 devState = HLS_DN_DOWNDEF;
1050 devState = HLS_DN_POLL;
1053 devState = HLS_DN_DISABLE;
1059 ret = set_link_state(ppd, devState);
1062 ret = set_link_state(ppd, HLS_UP_ARMED);
1064 send_idle_sma(dd, SMA_IDLE_ARM);
1066 case IB_PORT_ACTIVE:
1067 ret = set_link_state(ppd, HLS_UP_ACTIVE);
1069 send_idle_sma(dd, SMA_IDLE_ACTIVE);
1079 case HFI1_SNOOP_IOCGETLINKSTATE:
1080 case HFI1_SNOOP_IOCGETLINKSTATE_EXTRA:
1081 if (cmd == HFI1_SNOOP_IOCGETLINKSTATE_EXTRA) {
1082 memset(&link_info, 0, sizeof(link_info));
1083 ret = copy_from_user(&link_info,
1084 (struct hfi1_link_info __user *)arg,
1086 index = link_info.port_number;
1088 ret = __get_user(index, (int __user *) arg);
1093 if (index > dd->num_pports - 1) {
1098 ppd = &dd->pport[index];
1103 value = hfi1_ibphys_portstate(ppd);
1105 value |= driver_lstate(ppd);
1107 snoop_dbg("Link port | Link State: %d", value);
1109 if ((cmd == HFI1_SNOOP_IOCGETLINKSTATE_EXTRA) ||
1110 (cmd == HFI1_SNOOP_IOCSETLINKSTATE_EXTRA)) {
1111 link_info.port_state = value;
1112 link_info.node_guid = cpu_to_be64(ppd->guid);
1113 link_info.link_speed_active =
1114 ppd->link_speed_active;
1115 link_info.link_width_active =
1116 ppd->link_width_active;
1118 (struct hfi1_link_info __user *)arg,
1119 &link_info, sizeof(link_info));
1121 ret = __put_user(value, (int __user *)arg);
1125 case HFI1_SNOOP_IOCCLEARQUEUE:
1126 snoop_dbg("Clearing snoop queue");
1127 drain_snoop_list(&dd->hfi1_snoop.queue);
1130 case HFI1_SNOOP_IOCCLEARFILTER:
1131 snoop_dbg("Clearing filter");
1132 if (dd->hfi1_snoop.filter_callback) {
1133 /* Drain packets first */
1134 drain_snoop_list(&dd->hfi1_snoop.queue);
1135 dd->hfi1_snoop.filter_callback = NULL;
1137 kfree(dd->hfi1_snoop.filter_value);
1138 dd->hfi1_snoop.filter_value = NULL;
1141 case HFI1_SNOOP_IOCSETFILTER:
1142 snoop_dbg("Setting filter");
1143 /* just copy command structure */
1144 argp = (unsigned long *)arg;
1145 ret = copy_from_user(&filter_cmd, (void __user *)argp,
1146 sizeof(filter_cmd));
1148 pr_alert("Error copying filter command\n");
1151 if (filter_cmd.opcode >= HFI1_MAX_FILTERS) {
1152 pr_alert("Invalid opcode in request\n");
1157 snoop_dbg("Opcode %d Len %d Ptr %p",
1158 filter_cmd.opcode, filter_cmd.length,
1159 filter_cmd.value_ptr);
1161 filter_value = kzalloc(
1162 filter_cmd.length * sizeof(u8),
1164 if (!filter_value) {
1165 pr_alert("Not enough memory\n");
1169 /* copy remaining data from userspace */
1170 ret = copy_from_user((u8 *)filter_value,
1171 (void __user *)filter_cmd.value_ptr,
1174 kfree(filter_value);
1175 pr_alert("Error copying filter data\n");
1178 /* Drain packets first */
1179 drain_snoop_list(&dd->hfi1_snoop.queue);
1180 dd->hfi1_snoop.filter_callback =
1181 hfi1_filters[filter_cmd.opcode].filter;
1182 /* just in case we see back to back sets */
1183 kfree(dd->hfi1_snoop.filter_value);
1184 dd->hfi1_snoop.filter_value = filter_value;
1187 case HFI1_SNOOP_IOCGETVERSION:
1188 value = SNOOP_CAPTURE_VERSION;
1189 snoop_dbg("Getting version: %d", value);
1190 ret = __put_user(value, (int __user *)arg);
1192 case HFI1_SNOOP_IOCSET_OPTS:
1194 ret = __get_user(value, (int __user *) arg);
1198 snoop_dbg("Setting snoop option %d", value);
1199 if (value & SNOOP_DROP_SEND)
1200 snoop_flags |= SNOOP_DROP_SEND;
1201 if (value & SNOOP_USE_METADATA)
1202 snoop_flags |= SNOOP_USE_METADATA;
1210 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1214 static void snoop_list_add_tail(struct snoop_packet *packet,
1215 struct hfi1_devdata *dd)
1217 unsigned long flags = 0;
1219 spin_lock_irqsave(&dd->hfi1_snoop.snoop_lock, flags);
1220 if (likely((dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE) ||
1221 (dd->hfi1_snoop.mode_flag & HFI1_PORT_CAPTURE_MODE))) {
1222 list_add_tail(&packet->list, &dd->hfi1_snoop.queue);
1223 snoop_dbg("Added packet to list");
1227 * Technically we can could have closed the snoop device while waiting
1228 * on the above lock and it is gone now. The snoop mode_flag will
1229 * prevent us from adding the packet to the queue though.
1232 spin_unlock_irqrestore(&dd->hfi1_snoop.snoop_lock, flags);
1233 wake_up_interruptible(&dd->hfi1_snoop.waitq);
1236 static inline int hfi1_filter_check(void *val, const char *msg)
1239 snoop_dbg("Error invalid %s value for filter", msg);
1240 return HFI1_FILTER_ERR;
1245 static int hfi1_filter_lid(void *ibhdr, void *packet_data, void *value)
1247 struct hfi1_ib_header *hdr;
1250 ret = hfi1_filter_check(ibhdr, "header");
1253 ret = hfi1_filter_check(value, "user");
1256 hdr = (struct hfi1_ib_header *)ibhdr;
1258 if (*((u16 *)value) == be16_to_cpu(hdr->lrh[3])) /* matches slid */
1259 return HFI1_FILTER_HIT; /* matched */
1261 return HFI1_FILTER_MISS; /* Not matched */
1264 static int hfi1_filter_dlid(void *ibhdr, void *packet_data, void *value)
1266 struct hfi1_ib_header *hdr;
1269 ret = hfi1_filter_check(ibhdr, "header");
1272 ret = hfi1_filter_check(value, "user");
1276 hdr = (struct hfi1_ib_header *)ibhdr;
1278 if (*((u16 *)value) == be16_to_cpu(hdr->lrh[1]))
1279 return HFI1_FILTER_HIT;
1281 return HFI1_FILTER_MISS;
1284 /* Not valid for outgoing packets, send handler passes null for data*/
1285 static int hfi1_filter_mad_mgmt_class(void *ibhdr, void *packet_data,
1288 struct hfi1_ib_header *hdr;
1289 struct hfi1_other_headers *ohdr = NULL;
1290 struct ib_smp *smp = NULL;
1294 ret = hfi1_filter_check(ibhdr, "header");
1297 ret = hfi1_filter_check(packet_data, "packet_data");
1300 ret = hfi1_filter_check(value, "user");
1304 hdr = (struct hfi1_ib_header *)ibhdr;
1307 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1308 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
1310 ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
1312 qpn = be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF;
1314 smp = (struct ib_smp *)packet_data;
1315 if (*((u8 *)value) == smp->mgmt_class)
1316 return HFI1_FILTER_HIT;
1318 return HFI1_FILTER_MISS;
1320 return HFI1_FILTER_ERR;
1323 static int hfi1_filter_qp_number(void *ibhdr, void *packet_data, void *value)
1326 struct hfi1_ib_header *hdr;
1327 struct hfi1_other_headers *ohdr = NULL;
1330 ret = hfi1_filter_check(ibhdr, "header");
1333 ret = hfi1_filter_check(value, "user");
1337 hdr = (struct hfi1_ib_header *)ibhdr;
1340 if ((be16_to_cpu(hdr->lrh[0]) & 3) == HFI1_LRH_BTH)
1341 ohdr = &hdr->u.oth; /* LRH + BTH + DETH */
1343 ohdr = &hdr->u.l.oth; /* LRH + GRH + BTH + DETH */
1344 if (*((u32 *)value) == (be32_to_cpu(ohdr->bth[1]) & 0x00FFFFFF))
1345 return HFI1_FILTER_HIT;
1347 return HFI1_FILTER_MISS;
1350 static int hfi1_filter_ibpacket_type(void *ibhdr, void *packet_data,
1355 struct hfi1_ib_header *hdr;
1356 struct hfi1_other_headers *ohdr = NULL;
1359 ret = hfi1_filter_check(ibhdr, "header");
1362 ret = hfi1_filter_check(value, "user");
1366 hdr = (struct hfi1_ib_header *)ibhdr;
1368 lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1370 if (lnh == HFI1_LRH_BTH)
1372 else if (lnh == HFI1_LRH_GRH)
1373 ohdr = &hdr->u.l.oth;
1375 return HFI1_FILTER_ERR;
1377 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
1379 if (*((u8 *)value) == ((opcode >> 5) & 0x7))
1380 return HFI1_FILTER_HIT;
1382 return HFI1_FILTER_MISS;
1385 static int hfi1_filter_ib_service_level(void *ibhdr, void *packet_data,
1388 struct hfi1_ib_header *hdr;
1391 ret = hfi1_filter_check(ibhdr, "header");
1394 ret = hfi1_filter_check(value, "user");
1398 hdr = (struct hfi1_ib_header *)ibhdr;
1400 if ((*((u8 *)value)) == ((be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF))
1401 return HFI1_FILTER_HIT;
1403 return HFI1_FILTER_MISS;
1406 static int hfi1_filter_ib_pkey(void *ibhdr, void *packet_data, void *value)
1410 struct hfi1_ib_header *hdr;
1411 struct hfi1_other_headers *ohdr = NULL;
1414 ret = hfi1_filter_check(ibhdr, "header");
1417 ret = hfi1_filter_check(value, "user");
1421 hdr = (struct hfi1_ib_header *)ibhdr;
1423 lnh = (be16_to_cpu(hdr->lrh[0]) & 3);
1424 if (lnh == HFI1_LRH_BTH)
1426 else if (lnh == HFI1_LRH_GRH)
1427 ohdr = &hdr->u.l.oth;
1429 return HFI1_FILTER_ERR;
1431 /* P_key is 16-bit entity, however top most bit indicates
1432 * type of membership. 0 for limited and 1 for Full.
1433 * Limited members cannot accept information from other
1434 * Limited members, but communication is allowed between
1435 * every other combination of membership.
1436 * Hence we'll omit comparing top-most bit while filtering
1439 if ((*(u16 *)value & 0x7FFF) ==
1440 ((be32_to_cpu(ohdr->bth[0])) & 0x7FFF))
1441 return HFI1_FILTER_HIT;
1443 return HFI1_FILTER_MISS;
1447 * If packet_data is NULL then this is coming from one of the send functions.
1448 * Thus we know if its an ingressed or egressed packet.
1450 static int hfi1_filter_direction(void *ibhdr, void *packet_data, void *value)
1452 u8 user_dir = *(u8 *)value;
1455 ret = hfi1_filter_check(value, "user");
1460 /* Incoming packet */
1461 if (user_dir & HFI1_SNOOP_INGRESS)
1462 return HFI1_FILTER_HIT;
1464 /* Outgoing packet */
1465 if (user_dir & HFI1_SNOOP_EGRESS)
1466 return HFI1_FILTER_HIT;
1469 return HFI1_FILTER_MISS;
1473 * Allocate a snoop packet. The structure that is stored in the ring buffer, not
1474 * to be confused with an hfi packet type.
1476 static struct snoop_packet *allocate_snoop_packet(u32 hdr_len,
1481 struct snoop_packet *packet = NULL;
1483 packet = kzalloc(sizeof(struct snoop_packet) + hdr_len + data_len
1485 GFP_ATOMIC | __GFP_NOWARN);
1487 INIT_LIST_HEAD(&packet->list);
1494 * Instead of having snoop and capture code intermixed with the recv functions,
1495 * both the interrupt handler and hfi1_ib_rcv() we are going to hijack the call
1496 * and land in here for snoop/capture but if not enabled the call will go
1497 * through as before. This gives us a single point to constrain all of the snoop
1498 * snoop recv logic. There is nothing special that needs to happen for bypass
1499 * packets. This routine should not try to look into the packet. It just copied
1500 * it. There is no guarantee for filters when it comes to bypass packets as
1501 * there is no specific support. Bottom line is this routine does now even know
1502 * what a bypass packet is.
1504 int snoop_recv_handler(struct hfi1_packet *packet)
1506 struct hfi1_pportdata *ppd = packet->rcd->ppd;
1507 struct hfi1_ib_header *hdr = packet->hdr;
1508 int header_size = packet->hlen;
1509 void *data = packet->ebuf;
1510 u32 tlen = packet->tlen;
1511 struct snoop_packet *s_packet = NULL;
1515 struct capture_md md;
1517 snoop_dbg("PACKET IN: hdr size %d tlen %d data %p", header_size, tlen,
1520 trace_snoop_capture(ppd->dd, header_size, hdr, tlen - header_size,
1523 if (!ppd->dd->hfi1_snoop.filter_callback) {
1524 snoop_dbg("filter not set");
1525 ret = HFI1_FILTER_HIT;
1527 ret = ppd->dd->hfi1_snoop.filter_callback(hdr, data,
1528 ppd->dd->hfi1_snoop.filter_value);
1532 case HFI1_FILTER_ERR:
1533 snoop_dbg("Error in filter call");
1535 case HFI1_FILTER_MISS:
1536 snoop_dbg("Filter Miss");
1538 case HFI1_FILTER_HIT:
1540 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1542 if ((snoop_mode == 0) ||
1543 unlikely(snoop_flags & SNOOP_USE_METADATA))
1544 md_len = sizeof(struct capture_md);
1547 s_packet = allocate_snoop_packet(header_size,
1551 if (unlikely(s_packet == NULL)) {
1552 dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1557 memset(&md, 0, sizeof(struct capture_md));
1559 md.dir = PKT_DIR_INGRESS;
1560 md.u.rhf = packet->rhf;
1561 memcpy(s_packet->data, &md, md_len);
1564 /* We should always have a header */
1566 memcpy(s_packet->data + md_len, hdr, header_size);
1568 dd_dev_err(ppd->dd, "Unable to copy header to snoop/capture packet\n");
1574 * Packets with no data are possible. If there is no data needed
1575 * to take care of the last 4 bytes which are normally included
1576 * with data buffers and are included in tlen. Since we kzalloc
1577 * the buffer we do not need to set any values but if we decide
1578 * not to use kzalloc we should zero them.
1581 memcpy(s_packet->data + header_size + md_len, data,
1582 tlen - header_size);
1584 s_packet->total_len = tlen + md_len;
1585 snoop_list_add_tail(s_packet, ppd->dd);
1588 * If we are snooping the packet not capturing then throw away
1589 * after adding to the list.
1591 snoop_dbg("Capturing packet");
1592 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE) {
1593 snoop_dbg("Throwing packet away");
1595 * If we are dropping the packet we still may need to
1596 * handle the case where error flags are set, this is
1597 * normally done by the type specific handler but that
1598 * won't be called in this case.
1600 if (unlikely(rhf_err_flags(packet->rhf)))
1601 handle_eflags(packet);
1603 /* throw the packet on the floor */
1604 return RHF_RCV_CONTINUE;
1612 * We do not care what type of packet came in here - just pass it off
1613 * to the normal handler.
1615 return ppd->dd->normal_rhf_rcv_functions[rhf_rcv_type(packet->rhf)]
1620 * Handle snooping and capturing packets when sdma is being used.
1622 int snoop_send_dma_handler(struct hfi1_qp *qp, struct ahg_ib_header *ibhdr,
1623 u32 hdrwords, struct hfi1_sge_state *ss, u32 len,
1624 u32 plen, u32 dwords, u64 pbc)
1626 pr_alert("Snooping/Capture of Send DMA Packets Is Not Supported!\n");
1627 snoop_dbg("Unsupported Operation");
1628 return hfi1_verbs_send_dma(qp, ibhdr, hdrwords, ss, len, plen, dwords,
1633 * Handle snooping and capturing packets when pio is being used. Does not handle
1634 * bypass packets. The only way to send a bypass packet currently is to use the
1635 * diagpkt interface. When that interface is enable snoop/capture is not.
1637 int snoop_send_pio_handler(struct hfi1_qp *qp, struct ahg_ib_header *ahdr,
1638 u32 hdrwords, struct hfi1_sge_state *ss, u32 len,
1639 u32 plen, u32 dwords, u64 pbc)
1641 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1642 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
1643 struct snoop_packet *s_packet = NULL;
1644 u32 *hdr = (u32 *)&ahdr->ibh;
1646 struct hfi1_sge_state temp_ss;
1648 void *data_start = NULL;
1652 struct capture_md md;
1654 u32 hdr_len = hdrwords << 2;
1655 u32 tlen = HFI1_GET_PKT_LEN(&ahdr->ibh);
1659 snoop_dbg("PACKET OUT: hdrword %u len %u plen %u dwords %u tlen %u",
1660 hdrwords, len, plen, dwords, tlen);
1661 if (ppd->dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1663 if ((snoop_mode == 0) ||
1664 unlikely(snoop_flags & SNOOP_USE_METADATA))
1665 md_len = sizeof(struct capture_md);
1667 /* not using ss->total_len as arg 2 b/c that does not count CRC */
1668 s_packet = allocate_snoop_packet(hdr_len, tlen - hdr_len, md_len);
1670 if (unlikely(s_packet == NULL)) {
1671 dd_dev_warn_ratelimited(ppd->dd, "Unable to allocate snoop/capture packet\n");
1675 s_packet->total_len = tlen + md_len;
1678 memset(&md, 0, sizeof(struct capture_md));
1680 md.dir = PKT_DIR_EGRESS;
1681 if (likely(pbc == 0)) {
1682 vl = be16_to_cpu(ahdr->ibh.lrh[0]) >> 12;
1683 md.u.pbc = create_pbc(ppd, 0, qp->s_srate, vl, plen);
1687 memcpy(s_packet->data, &md, md_len);
1694 memcpy(s_packet->data + md_len, hdr, hdr_len);
1697 "Unable to copy header to snoop/capture packet\n");
1703 data = s_packet->data + hdr_len + md_len;
1708 * The update_sge() function below will not modify the
1709 * individual SGEs in the array. It will make a copy each time
1710 * and operate on that. So we only need to copy this instance
1711 * and it won't impact PIO.
1716 snoop_dbg("Need to copy %d bytes", length);
1718 void *addr = temp_ss.sge.vaddr;
1719 u32 slen = temp_ss.sge.length;
1721 if (slen > length) {
1723 snoop_dbg("slen %d > len %d", slen, length);
1725 snoop_dbg("copy %d to %p", slen, addr);
1726 memcpy(data, addr, slen);
1727 update_sge(&temp_ss, slen);
1730 snoop_dbg("data is now %p bytes left %d", data, length);
1732 snoop_dbg("Completed SGE copy");
1736 * Why do the filter check down here? Because the event tracing has its
1737 * own filtering and we need to have the walked the SGE list.
1739 if (!ppd->dd->hfi1_snoop.filter_callback) {
1740 snoop_dbg("filter not set\n");
1741 ret = HFI1_FILTER_HIT;
1743 ret = ppd->dd->hfi1_snoop.filter_callback(
1746 ppd->dd->hfi1_snoop.filter_value);
1750 case HFI1_FILTER_ERR:
1751 snoop_dbg("Error in filter call");
1753 case HFI1_FILTER_MISS:
1754 snoop_dbg("Filter Miss");
1757 case HFI1_FILTER_HIT:
1758 snoop_dbg("Capturing packet");
1759 snoop_list_add_tail(s_packet, ppd->dd);
1761 if (unlikely((snoop_flags & SNOOP_DROP_SEND) &&
1762 (ppd->dd->hfi1_snoop.mode_flag &
1763 HFI1_PORT_SNOOP_MODE))) {
1764 unsigned long flags;
1766 snoop_dbg("Dropping packet");
1768 spin_lock_irqsave(&qp->s_lock, flags);
1773 spin_unlock_irqrestore(&qp->s_lock, flags);
1774 } else if (qp->ibqp.qp_type == IB_QPT_RC) {
1775 spin_lock_irqsave(&qp->s_lock, flags);
1776 hfi1_rc_send_complete(qp, &ahdr->ibh);
1777 spin_unlock_irqrestore(&qp->s_lock, flags);
1787 return hfi1_verbs_send_pio(qp, ahdr, hdrwords, ss, len, plen, dwords,
1792 * Callers of this must pass a hfi1_ib_header type for the from ptr. Currently
1793 * this can be used anywhere, but the intention is for inline ACKs for RC and
1794 * CCA packets. We don't restrict this usage though.
1796 void snoop_inline_pio_send(struct hfi1_devdata *dd, struct pio_buf *pbuf,
1797 u64 pbc, const void *from, size_t count)
1801 struct capture_md md;
1802 struct snoop_packet *s_packet = NULL;
1805 * count is in dwords so we need to convert to bytes.
1806 * We also need to account for CRC which would be tacked on by hardware.
1808 int packet_len = (count << 2) + 4;
1811 snoop_dbg("ACK OUT: len %d", packet_len);
1813 if (!dd->hfi1_snoop.filter_callback) {
1814 snoop_dbg("filter not set");
1815 ret = HFI1_FILTER_HIT;
1817 ret = dd->hfi1_snoop.filter_callback(
1818 (struct hfi1_ib_header *)from,
1820 dd->hfi1_snoop.filter_value);
1824 case HFI1_FILTER_ERR:
1825 snoop_dbg("Error in filter call");
1827 case HFI1_FILTER_MISS:
1828 snoop_dbg("Filter Miss");
1830 case HFI1_FILTER_HIT:
1831 snoop_dbg("Capturing packet");
1832 if (dd->hfi1_snoop.mode_flag & HFI1_PORT_SNOOP_MODE)
1834 if ((snoop_mode == 0) ||
1835 unlikely(snoop_flags & SNOOP_USE_METADATA))
1836 md_len = sizeof(struct capture_md);
1838 s_packet = allocate_snoop_packet(packet_len, 0, md_len);
1840 if (unlikely(s_packet == NULL)) {
1841 dd_dev_warn_ratelimited(dd, "Unable to allocate snoop/capture packet\n");
1842 goto inline_pio_out;
1845 s_packet->total_len = packet_len + md_len;
1847 /* Fill in the metadata for the packet */
1849 memset(&md, 0, sizeof(struct capture_md));
1851 md.dir = PKT_DIR_EGRESS;
1853 memcpy(s_packet->data, &md, md_len);
1856 /* Add the packet data which is a single buffer */
1857 memcpy(s_packet->data + md_len, from, packet_len);
1859 snoop_list_add_tail(s_packet, dd);
1861 if (unlikely((snoop_flags & SNOOP_DROP_SEND) && snoop_mode)) {
1862 snoop_dbg("Dropping packet");
1871 pio_copy(dd, pbuf, pbc, from, count);