Add OPA gen1 driver
Subsystems (2)
PATCH 20/41 IB/hfi1: add memory region handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/mr.c | 546 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 546 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/mr.c
drivers/infiniband/hw/hfi1/mr.c new file +546 -0
@@ -0,0 +1,546 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <rdma/ib_umem.h> +#include <rdma/ib_smi.h> + +#include "hfi.h" + +/* Fast memory region */ +struct hfi1_fmr { +struct ib_fmr ibfmr; +struct hfi1_mregion mr;/* must be last */ +}; + +static inline struct hfi1_fmr *to_ifmr (struct ib_fmr *ibfmr) +{ + return container_of(ibfmr, struct hfi1_fmr, ibfmr); +} + +static int init_mregion (struct hfi1_mregion *mr,struct ib_pd *pd, +int count) +{ +int m, i =0 ; +int rval =0 ; + + m = (count +HFI1_SEGSZ -1 ) /HFI1_SEGSZ ; +for (; i < m; i++) { + mr->map[i] =kzalloc (sizeof (*mr->map[0 ]),GFP_KERNEL ); +if (!mr->map[i]) + goto bail; + } + mr->mapsz = m; +init_completion (&mr->comp); +/* count returning the ptr to user */ +atomic_set (&mr->refcount,1 ); + mr->pd = pd; + mr->max_segs = count; +out: +return rval; +bail: +while (i) +kfree (mr->map[--i]); + rval = -ENOMEM ; + goto out; +} + +static void deinit_mregion (struct hfi1_mregion *mr) +{ +int i = mr->mapsz; + + mr->mapsz =0 ; +while (i) +kfree (mr->map[--i]); +} + + +/** +* hfi1_get_dma_mr - get a DMA memory region +* @pd: protection domain for this memory region +* @acc: access flags +* +* Returns the memory region on success, otherwise returns an errno. +* Note that all DMA addresses should be created via the +* struct ib_dma_mapping_ops functions (see dma.c). +*/ +struct ib_mr *hfi1_get_dma_mr (struct ib_pd *pd,int acc) +{ +struct hfi1_mr *mr =NULL ; +struct ib_mr *ret; +int rval; + +if (to_ipd (pd)->user) { + ret =ERR_PTR (-EPERM ); + goto bail; + } + + mr =kzalloc (sizeof (*mr),GFP_KERNEL ); +if (!mr) { + ret =ERR_PTR (-ENOMEM ); + goto bail; + } + + rval =init_mregion (&mr->mr, pd,0 ); +if (rval) { + ret =ERR_PTR (rval); + goto bail; + } + + + rval =hfi1_alloc_lkey (&mr->mr,1 ); +if (rval) { + ret =ERR_PTR (rval); + goto bail_mregion; + } + + mr->mr.access_flags = acc; + ret = &mr->ibmr; +done: +return ret; + +bail_mregion: +deinit_mregion (&mr->mr); +bail: +kfree (mr); + goto done; +} + +static struct hfi1_mr *alloc_mr (int count,struct ib_pd *pd) +{ +struct hfi1_mr *mr; +int rval = -ENOMEM ; +int m; + +/* Allocate struct plus pointers to first level page tables. */ + m = (count +HFI1_SEGSZ -1 ) /HFI1_SEGSZ ; + mr =kzalloc (sizeof (*mr) + m *sizeof (mr->mr.map[0 ]),GFP_KERNEL ); +if (!mr) + goto bail; + + rval =init_mregion (&mr->mr, pd, count); +if (rval) + goto bail; +/* +* ib_reg_phys_mr() will initialize mr->ibmr except for +* lkey and rkey. +*/ + rval =hfi1_alloc_lkey (&mr->mr,0 ); +if (rval) + goto bail_mregion; + mr->ibmr.lkey = mr->mr.lkey; + mr->ibmr.rkey = mr->mr.lkey; +done: +return mr; + +bail_mregion: +deinit_mregion (&mr->mr); +bail: +kfree (mr); + mr =ERR_PTR (rval); + goto done; +} + +/** +* hfi1_reg_phys_mr - register a physical memory region +* @pd: protection domain for this memory region +* @buffer_list: pointer to the list of physical buffers to register +* @num_phys_buf: the number of physical buffers to register +* @iova_start: the starting address passed over IB which maps to this MR +* +* Returns the memory region on success, otherwise returns an errno. +*/ +struct ib_mr *hfi1_reg_phys_mr (struct ib_pd *pd, +struct ib_phys_buf *buffer_list, +int num_phys_buf,int acc,u64 *iova_start) +{ +struct hfi1_mr *mr; +int n, m, i; +struct ib_mr *ret; + + mr =alloc_mr (num_phys_buf, pd); +if (IS_ERR (mr)) { + ret = (struct ib_mr *)mr; + goto bail; + } + + mr->mr.user_base = *iova_start; + mr->mr.iova = *iova_start; + mr->mr.access_flags = acc; + + m =0 ; + n =0 ; +for (i =0 ; i < num_phys_buf; i++) { + mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr; + mr->mr.map[m]->segs[n].length = buffer_list[i].size; + mr->mr.length += buffer_list[i].size; + n++; +if (n ==HFI1_SEGSZ ) { + m++; + n =0 ; + } + } + + ret = &mr->ibmr; + +bail: +return ret; +} + +/** +* hfi1_reg_user_mr - register a userspace memory region +* @pd: protection domain for this memory region +* @start: starting userspace address +* @length: length of region to register +* @mr_access_flags: access flags for this memory region +* @udata: unused by the driver +* +* Returns the memory region on success, otherwise returns an errno. +*/ +struct ib_mr *hfi1_reg_user_mr (struct ib_pd *pd,u64 start,u64 length, +u64 virt_addr,int mr_access_flags, +struct ib_udata *udata) +{ +struct hfi1_mr *mr; +struct ib_umem *umem; +struct scatterlist *sg; +int n, m, entry; +struct ib_mr *ret; + +if (length ==0 ) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } + + umem =ib_umem_get (pd->uobject->context, start, length, + mr_access_flags,0 ); +if (IS_ERR (umem)) +return (void *) umem; + + n = umem->nmap; + + mr =alloc_mr (n, pd); +if (IS_ERR (mr)) { + ret = (struct ib_mr *)mr; +ib_umem_release (umem); + goto bail; + } + + mr->mr.user_base = start; + mr->mr.iova = virt_addr; + mr->mr.length = length; + mr->mr.offset =ib_umem_offset (umem); + mr->mr.access_flags = mr_access_flags; + mr->umem = umem; + +if (is_power_of_2 (umem->page_size)) + mr->mr.page_shift =ilog2 (umem->page_size); + m =0 ; + n =0 ; +for_each_sg (umem->sg_head.sgl, sg, umem->nmap, entry) { +void *vaddr; + + vaddr =page_address (sg_page (sg)); +if (!vaddr) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } + mr->mr.map[m]->segs[n].vaddr = vaddr; + mr->mr.map[m]->segs[n].length = umem->page_size; + n++; +if (n ==HFI1_SEGSZ ) { + m++; + n =0 ; + } + } + ret = &mr->ibmr; + +bail: +return ret; +} + +/** +* hfi1_dereg_mr - unregister and free a memory region +* @ibmr: the memory region to free +* +* Returns 0 on success. +* +* Note that this is called to free MRs created by hfi1_get_dma_mr() +* or hfi1_reg_user_mr(). +*/ +int hfi1_dereg_mr (struct ib_mr *ibmr) +{ +struct hfi1_mr *mr =to_imr (ibmr); +int ret =0 ; +unsigned long timeout; + +hfi1_free_lkey (&mr->mr); + +hfi1_put_mr (&mr->mr);/* will set completion if last */ + timeout =wait_for_completion_timeout (&mr->mr.comp, +5 *HZ ); +if (!timeout) { +dd_dev_err ( +dd_from_ibdev (mr->mr.pd->device), +"hfi1_dereg_mr timeout mr %p pd %p refcount %u\n" , + mr, mr->mr.pd,atomic_read (&mr->mr.refcount)); +hfi1_get_mr (&mr->mr); + ret = -EBUSY ; + goto out; + } +deinit_mregion (&mr->mr); +if (mr->umem) +ib_umem_release (mr->umem); +kfree (mr); +out: +return ret; +} + +/* +* Allocate a memory region usable with the +* IB_WR_FAST_REG_MR send work request. +* +* Return the memory region on success, otherwise return an errno. +*/ +struct ib_mr *hfi1_alloc_fast_reg_mr (struct ib_pd *pd,int max_page_list_len) +{ +struct hfi1_mr *mr; + + mr =alloc_mr (max_page_list_len, pd); +if (IS_ERR (mr)) +return (struct ib_mr *)mr; + +return &mr->ibmr; +} + +struct ib_fast_reg_page_list * +hfi1_alloc_fast_reg_page_list (struct ib_device *ibdev,int page_list_len) +{ +unsigned size = page_list_len *sizeof (u64); +struct ib_fast_reg_page_list *pl; + +if (size >PAGE_SIZE ) +return ERR_PTR (-EINVAL ); + + pl =kzalloc (sizeof (*pl),GFP_KERNEL ); +if (!pl) +return ERR_PTR (-ENOMEM ); + + pl->page_list =kzalloc (size,GFP_KERNEL ); +if (!pl->page_list) + goto err_free; + +return pl; + +err_free: +kfree (pl); +return ERR_PTR (-ENOMEM ); +} + +void hfi1_free_fast_reg_page_list (struct ib_fast_reg_page_list *pl) +{ +kfree (pl->page_list); +kfree (pl); +} + +/** +* hfi1_alloc_fmr - allocate a fast memory region +* @pd: the protection domain for this memory region +* @mr_access_flags: access flags for this memory region +* @fmr_attr: fast memory region attributes +* +* Returns the memory region on success, otherwise returns an errno. +*/ +struct ib_fmr *hfi1_alloc_fmr (struct ib_pd *pd,int mr_access_flags, +struct ib_fmr_attr *fmr_attr) +{ +struct hfi1_fmr *fmr; +int m; +struct ib_fmr *ret; +int rval = -ENOMEM ; + +/* Allocate struct plus pointers to first level page tables. */ + m = (fmr_attr->max_pages +HFI1_SEGSZ -1 ) /HFI1_SEGSZ ; + fmr =kzalloc (sizeof (*fmr) + m *sizeof (fmr->mr.map[0 ]),GFP_KERNEL ); +if (!fmr) + goto bail; + + rval =init_mregion (&fmr->mr, pd, fmr_attr->max_pages); +if (rval) + goto bail; + +/* +* ib_alloc_fmr() will initialize fmr->ibfmr except for lkey & +* rkey. +*/ + rval =hfi1_alloc_lkey (&fmr->mr,0 ); +if (rval) + goto bail_mregion; + fmr->ibfmr.rkey = fmr->mr.lkey; + fmr->ibfmr.lkey = fmr->mr.lkey; +/* +* Resources are allocated but no valid mapping (RKEY can't be +* used). +*/ + fmr->mr.access_flags = mr_access_flags; + fmr->mr.max_segs = fmr_attr->max_pages; + fmr->mr.page_shift = fmr_attr->page_shift; + + ret = &fmr->ibfmr; +done: +return ret; + +bail_mregion: +deinit_mregion (&fmr->mr); +bail: +kfree (fmr); + ret =ERR_PTR (rval); + goto done; +} + +/** +* hfi1_map_phys_fmr - set up a fast memory region +* @ibmfr: the fast memory region to set up +* @page_list: the list of pages to associate with the fast memory region +* @list_len: the number of pages to associate with the fast memory region +* @iova: the virtual address of the start of the fast memory region +* +* This may be called from interrupt context. +*/ + +int hfi1_map_phys_fmr (struct ib_fmr *ibfmr,u64 *page_list, +int list_len,u64 iova) +{ +struct hfi1_fmr *fmr =to_ifmr (ibfmr); +struct hfi1_lkey_table *rkt; +unsigned long flags; +int m, n, i; +u32 ps; +int ret; + + i =atomic_read (&fmr->mr.refcount); +if (i >2 ) +return -EBUSY ; + +if (list_len > fmr->mr.max_segs) { + ret = -EINVAL ; + goto bail; + } + rkt = &to_idev (ibfmr->device)->lk_table; +spin_lock_irqsave (&rkt->lock, flags); + fmr->mr.user_base = iova; + fmr->mr.iova = iova; + ps =1 << fmr->mr.page_shift; + fmr->mr.length = list_len * ps; + m =0 ; + n =0 ; +for (i =0 ; i < list_len; i++) { + fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i]; + fmr->mr.map[m]->segs[n].length = ps; +if (++n ==HFI1_SEGSZ ) { + m++; + n =0 ; + } + } +spin_unlock_irqrestore (&rkt->lock, flags); + ret =0 ; + +bail: +return ret; +} + +/** +* hfi1_unmap_fmr - unmap fast memory regions +* @fmr_list: the list of fast memory regions to unmap +* +* Returns 0 on success. +*/ +int hfi1_unmap_fmr (struct list_head *fmr_list) +{ +struct hfi1_fmr *fmr; +struct hfi1_lkey_table *rkt; +unsigned long flags; + +list_for_each_entry (fmr, fmr_list, ibfmr.list) { + rkt = &to_idev (fmr->ibfmr.device)->lk_table; +spin_lock_irqsave (&rkt->lock, flags); + fmr->mr.user_base =0 ; + fmr->mr.iova =0 ; + fmr->mr.length =0 ; +spin_unlock_irqrestore (&rkt->lock, flags); + } +return 0 ; +} + +/** +* hfi1_dealloc_fmr - deallocate a fast memory region +* @ibfmr: the fast memory region to deallocate +* +* Returns 0 on success. +*/ +int hfi1_dealloc_fmr (struct ib_fmr *ibfmr) +{ +struct hfi1_fmr *fmr =to_ifmr (ibfmr); +int ret =0 ; +unsigned long timeout; + +hfi1_free_lkey (&fmr->mr); +hfi1_put_mr (&fmr->mr);/* will set completion if last */ + timeout =wait_for_completion_timeout (&fmr->mr.comp, +5 *HZ ); +if (!timeout) { +hfi1_get_mr (&fmr->mr); + ret = -EBUSY ; + goto out; + } +deinit_mregion (&fmr->mr); +kfree (fmr); +out: +return ret; +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 21/41 IB/hfi1: add misc OPA defines
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/opa_compat.h | 132 +++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/opa_compat.h
drivers/infiniband/hw/hfi1/opa_compat.h new file +132 -0
@@ -0,0 +1,132 @@ +#ifndef _LINUX_H +#define _LINUX_H +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +/* +* This header file is for OPA-specific definitions which are +* required by the HFI driver, and which aren't yet in the Linux +* IB core. We'll collect these all here, then merge them into +* the kernel when that's convenient. +*/ + +/* OPA SMA attribute IDs */ +#define OPA_ATTRIB_ID_CONGESTION_INFO cpu_to_be16(0x008b) +#define OPA_ATTRIB_ID_HFI_CONGESTION_LOG cpu_to_be16(0x008f) +#define OPA_ATTRIB_ID_HFI_CONGESTION_SETTING cpu_to_be16(0x0090) +#define OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE cpu_to_be16(0x0091) + +/* OPA PMA attribute IDs */ +#define OPA_PM_ATTRIB_ID_PORT_STATUS cpu_to_be16(0x0040) +#define OPA_PM_ATTRIB_ID_CLEAR_PORT_STATUS cpu_to_be16(0x0041) +#define OPA_PM_ATTRIB_ID_DATA_PORT_COUNTERS cpu_to_be16(0x0042) +#define OPA_PM_ATTRIB_ID_ERROR_PORT_COUNTERS cpu_to_be16(0x0043) +#define OPA_PM_ATTRIB_ID_ERROR_INFO cpu_to_be16(0x0044) + +/* OPA status codes */ +#define OPA_PM_STATUS_REQUEST_TOO_LARGE cpu_to_be16(0x100) + +static inline u8 port_states_to_logical_state (struct opa_port_states *ps) +{ +return ps->portphysstate_portstate &OPA_PI_MASK_PORT_STATE ; +} + +static inline u8 port_states_to_phys_state (struct opa_port_states *ps) +{ +return ((ps->portphysstate_portstate & +OPA_PI_MASK_PORT_PHYSICAL_STATE ) >>4 ) &0xf ; +} + +/* +* OPA port physical states +* IB Volume 1, Table 146 PortInfo/IB Volume 2 Section 5.4.2(1) PortPhysState +* values. +* +* When writing, only values 0-3 are valid, other values are ignored. +* When reading, 0 is reserved. +* +* Returned by the ibphys_portstate() routine. +*/ +enum opa_port_phys_state { +IB_PORTPHYSSTATE_NOP =0 , +/* 1 is reserved */ +IB_PORTPHYSSTATE_POLLING =2 , +IB_PORTPHYSSTATE_DISABLED =3 , +IB_PORTPHYSSTATE_TRAINING =4 , +IB_PORTPHYSSTATE_LINKUP =5 , +IB_PORTPHYSSTATE_LINK_ERROR_RECOVERY =6 , +IB_PORTPHYSSTATE_PHY_TEST =7 , +/* 8 is reserved */ +OPA_PORTPHYSSTATE_OFFLINE =9 , +OPA_PORTPHYSSTATE_GANGED =10 , +OPA_PORTPHYSSTATE_TEST =11 , +OPA_PORTPHYSSTATE_MAX =11 , +/* values 12-15 are reserved/ignored */ +}; + +/* where best to put this opcode? */ +#define CNP_OPCODE 0x80 + +/* OPA_PORT_TYPE_* definitions - these belong in opa_port_info.h */ +#define OPA_PORT_TYPE_UNKNOWN 0 +#define OPA_PORT_TYPE_DISCONNECTED 1 +/* port is not currently usable, CableInfo not available */ +#define OPA_PORT_TYPE_FIXED 2 +/* A fixed backplane port in a director class switch. All OPA ASICS */ +#define OPA_PORT_TYPE_VARIABLE 3 +/* A backplane port in a blade system, possibly mixed configuration */ +#define OPA_PORT_TYPE_STANDARD 4 +/* implies a SFF-8636 defined format for CableInfo (QSFP) */ +#define OPA_PORT_TYPE_SI_PHOTONICS 5 +/* A silicon photonics module implies TBD defined format for CableInfo +* as defined by Intel SFO group */ +/* 6 - 15 are reserved */ + +#endif /* _LINUX_H */
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 22/41 IB/hfi1: add pcie routines
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/pcie.c | 1254 +++++++++++++++++++++++++++++++++++++ 1 file changed, 1254 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/pcie.c
drivers/infiniband/hw/hfi1/pcie.c new file +1254 -0
@@ -0,0 +1,1254 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/pci.h> +#include <linux/io.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include <linux/aer.h> +#include <linux/module.h> + +#include "hfi.h" +#include "chip_registers.h" + +/* link speed vector for Gen3 speed - not in Linux headers */ +#define GEN1_SPEED_VECTOR 0x1 +#define GEN2_SPEED_VECTOR 0x2 +#define GEN3_SPEED_VECTOR 0x3 + +/* +* This file contains PCIe utility routines. +*/ + +/* +* Code to adjust PCIe capabilities. +*/ +static void tune_pcie_caps (struct hfi1_devdata *); + +/* +* Do all the common PCIe setup and initialization. +* devdata is not yet allocated, and is not allocated until after this +* routine returns success. Therefore dd_dev_err() can't be used for error +* printing. +*/ +int hfi1_pcie_init (struct pci_dev *pdev,const struct pci_device_id *ent) +{ +int ret; + + ret =pci_enable_device (pdev); +if (ret) { +/* +* This can happen (in theory) iff: +* We did a chip reset, and then failed to reprogram the +* BAR, or the chip reset due to an internal error. We then +* unloaded the driver and reloaded it. +* +* Both reset cases set the BAR back to initial state. For +* the latter case, the AER sticky error bit at offset 0x718 +* should be set, but the Linux kernel doesn't yet know +* about that, it appears. If the original BAR was retained +* in the kernel data structures, this may be OK. +*/ +hfi1_early_err (&pdev->dev,"pci enable failed: error %d\n" , + -ret); + goto done; + } + + ret =pci_request_regions (pdev,DRIVER_NAME ); +if (ret) { +hfi1_early_err (&pdev->dev, +"pci_request_regions fails: err %d\n" , -ret); + goto bail; + } + + ret =pci_set_dma_mask (pdev,DMA_BIT_MASK (64 )); +if (ret) { +/* +* If the 64 bit setup fails, try 32 bit. Some systems +* do not setup 64 bit maps on systems with 2GB or less +* memory installed. +*/ + ret =pci_set_dma_mask (pdev,DMA_BIT_MASK (32 )); +if (ret) { +hfi1_early_err (&pdev->dev, +"Unable to set DMA mask: %d\n" , ret); + goto bail; + } + ret =pci_set_consistent_dma_mask (pdev,DMA_BIT_MASK (32 )); + }else + ret =pci_set_consistent_dma_mask (pdev,DMA_BIT_MASK (64 )); +if (ret) { +hfi1_early_err (&pdev->dev, +"Unable to set DMA consistent mask: %d\n" , ret); + goto bail; + } + +pci_set_master (pdev); + ret =pci_enable_pcie_error_reporting (pdev); +if (ret) { +hfi1_early_err (&pdev->dev, +"Unable to enable pcie error reporting: %d\n" , + ret); + ret =0 ; + } + goto done; + +bail: +hfi1_pcie_cleanup (pdev); +done: +return ret; +} + +/* +* Clean what was done in hfi1_pcie_init() +*/ +void hfi1_pcie_cleanup (struct pci_dev *pdev) +{ +pci_disable_device (pdev); +/* +* Release regions should be called after the disable. OK to +* call if request regions has not been called or failed. +*/ +pci_release_regions (pdev); +} + +/* +* Do remaining PCIe setup, once dd is allocated, and save away +* fields required to re-initialize after a chip reset, or for +* various other purposes +*/ +int hfi1_pcie_ddinit (struct hfi1_devdata *dd,struct pci_dev *pdev, +const struct pci_device_id *ent) +{ +unsigned long len; +resource_size_t addr; + + dd->pcidev = pdev; +pci_set_drvdata (pdev, dd); + + addr =pci_resource_start (pdev,0 ); + len =pci_resource_len (pdev,0 ); + +/* +* The TXE PIO buffers are at the tail end of the chip space. +* Cut them off and map them separately. +*/ + +/* sanity check vs expectations */ +if (len !=TXE_PIO_SEND +TXE_PIO_SIZE ) { +dd_dev_err (dd,"chip PIO range does not match\n" ); +return -EINVAL ; + } + + dd->kregbase =ioremap_nocache (addr,TXE_PIO_SEND ); +if (!dd->kregbase) +return -ENOMEM ; + + dd->piobase =ioremap_wc (addr +TXE_PIO_SEND ,TXE_PIO_SIZE ); +if (!dd->piobase) { +iounmap (dd->kregbase); +return -ENOMEM ; + } + + dd->flags |=HFI1_PRESENT ;/* now register routines work */ + + dd->kregend = dd->kregbase +TXE_PIO_SEND ; + dd->physaddr = addr;/* used for io_remap, etc. */ + +/* +* Re-map the chip's RcvArray as write-combining to allow us +* to write an entire cacheline worth of entries in one shot. +* If this re-map fails, just continue - the RcvArray programming +* function will handle both cases. +*/ + dd->chip_rcv_array_count =read_csr (dd,RCV_ARRAY_CNT ); + dd->rcvarray_wc =ioremap_wc (addr +RCV_ARRAY , + dd->chip_rcv_array_count *8 ); +dd_dev_info (dd,"WC Remapped RcvArray: %p\n" , dd->rcvarray_wc); +/* +* Save BARs and command to rewrite after device reset. +*/ + dd->pcibar0 = addr; + dd->pcibar1 = addr >>32 ; +pci_read_config_dword (dd->pcidev,PCI_ROM_ADDRESS , &dd->pci_rom); +pci_read_config_word (dd->pcidev,PCI_COMMAND , &dd->pci_command); +pcie_capability_read_word (dd->pcidev,PCI_EXP_DEVCTL , &dd->pcie_devctl); +pcie_capability_read_word (dd->pcidev,PCI_EXP_LNKCTL , &dd->pcie_lnkctl); +pcie_capability_read_word (dd->pcidev,PCI_EXP_DEVCTL2 , + &dd->pcie_devctl2); +pci_read_config_dword (dd->pcidev,PCI_CFG_MSIX0 , &dd->pci_msix0); +pci_read_config_dword (dd->pcidev,PCIE_CFG_SPCIE1 , + &dd->pci_lnkctl3); +pci_read_config_dword (dd->pcidev,PCIE_CFG_TPH2 , &dd->pci_tph2); + +return 0 ; +} + +/* +* Do PCIe cleanup related to dd, after chip-specific cleanup, etc. Just prior +* to releasing the dd memory. +* Void because all of the core pcie cleanup functions are void. +*/ +void hfi1_pcie_ddcleanup (struct hfi1_devdata *dd) +{ +u64 __iomem *base = (void __iomem *) dd->kregbase; + + dd->flags &= ~HFI1_PRESENT ; + dd->kregbase =NULL ; +iounmap (base); +if (dd->rcvarray_wc) +iounmap (dd->rcvarray_wc); +if (dd->piobase) +iounmap (dd->piobase); + +pci_set_drvdata (dd->pcidev,NULL ); +} + +/* +* Do a Function Level Reset (FLR) on the device. +* Based on static function drivers/pci/pci.c:pcie_flr(). +*/ +void hfi1_pcie_flr (struct hfi1_devdata *dd) +{ +int i; +u16 status; + +/* no need to check for the capability - we know the device has it */ + +/* wait for Transaction Pending bit to clear, at most a few ms */ +for (i =0 ; i <4 ; i++) { +if (i) +msleep ((1 << (i -1 )) *100 ); + +pcie_capability_read_word (dd->pcidev,PCI_EXP_DEVSTA , &status); +if (!(status &PCI_EXP_DEVSTA_TRPND )) + goto clear; + } + +dd_dev_err (dd,"Transaction Pending bit is not clearing, proceeding with reset anyway\n" ); + +clear: +pcie_capability_set_word (dd->pcidev,PCI_EXP_DEVCTL , +PCI_EXP_DEVCTL_BCR_FLR ); +/* PCIe spec requires the function to be back within 100ms */ +msleep (100 ); +} + +static void msix_setup (struct hfi1_devdata *dd,int pos,u32 *msixcnt, +struct hfi1_msix_entry *hfi1_msix_entry) +{ +int ret; +int nvec = *msixcnt; +struct msix_entry *msix_entry; +int i; + +/* We can't pass hfi1_msix_entry array to msix_setup +* so use a dummy msix_entry array and copy the allocated +* irq back to the hfi1_msix_entry array. */ + msix_entry =kmalloc_array (nvec,sizeof (*msix_entry),GFP_KERNEL ); +if (!msix_entry) { + ret = -ENOMEM ; + goto do_intx; + } + +for (i =0 ; i < nvec; i++) + msix_entry[i] = hfi1_msix_entry[i].msix; + + ret =pci_enable_msix_range (dd->pcidev, msix_entry,1 , nvec); +if (ret <0 ) + goto free_msix_entry; + nvec = ret; + +for (i =0 ; i < nvec; i++) + hfi1_msix_entry[i].msix = msix_entry[i]; + +kfree (msix_entry); + *msixcnt = nvec; +return ; + +free_msix_entry: +kfree (msix_entry); + +do_intx: +dd_dev_err (dd,"pci_enable_msix_range %d vectors failed: %d, falling back to INTx\n" , + nvec, ret); + *msixcnt =0 ; +hfi1_enable_intx (dd->pcidev); + +} + +/* return the PCIe link speed from the given link status */ +static u32 extract_speed (u16 linkstat) +{ +u32 speed; + +switch (linkstat &PCI_EXP_LNKSTA_CLS ) { +default :/* not defined, assume Gen1 */ +case PCI_EXP_LNKSTA_CLS_2_5GB : + speed =2500 ;/* Gen 1, 2.5GHz */ +break ; +case PCI_EXP_LNKSTA_CLS_5_0GB : + speed =5000 ;/* Gen 2, 5GHz */ +break ; +case GEN3_SPEED_VECTOR : + speed =8000 ;/* Gen 3, 8GHz */ +break ; + } +return speed; +} + +/* return the PCIe link speed from the given link status */ +static u32 extract_width (u16 linkstat) +{ +return (linkstat &PCI_EXP_LNKSTA_NLW ) >>PCI_EXP_LNKSTA_NLW_SHIFT ; +} + +/* read the link status and set dd->{lbus_width,lbus_speed,lbus_info} */ +static void update_lbus_info (struct hfi1_devdata *dd) +{ +u16 linkstat; + +pcie_capability_read_word (dd->pcidev,PCI_EXP_LNKSTA , &linkstat); + dd->lbus_width =extract_width (linkstat); + dd->lbus_speed =extract_speed (linkstat); +snprintf (dd->lbus_info,sizeof (dd->lbus_info), +"PCIe,%uMHz,x%u" , dd->lbus_speed, dd->lbus_width); +} + +/* +* Read in the current PCIe link width and speed. Find if the link is +* Gen3 capable. +*/ +int pcie_speeds (struct hfi1_devdata *dd) +{ +u32 linkcap; + +if (!pci_is_pcie (dd->pcidev)) { +dd_dev_err (dd,"Can't find PCI Express capability!\n" ); +return -EINVAL ; + } + +/* find if our max speed is Gen3 and parent supports Gen3 speeds */ + dd->link_gen3_capable =1 ; + +pcie_capability_read_dword (dd->pcidev,PCI_EXP_LNKCAP , &linkcap); +if ((linkcap &PCI_EXP_LNKCAP_SLS ) !=GEN3_SPEED_VECTOR ) { +dd_dev_info (dd, +"This HFI is not Gen3 capable, max speed 0x%x, need 0x3\n" , + linkcap &PCI_EXP_LNKCAP_SLS ); + dd->link_gen3_capable =0 ; + } + +/* +* bus->max_bus_speed is set from the bridge's linkcap Max Link Speed +*/ +if (dd->pcidev->bus->max_bus_speed !=PCIE_SPEED_8_0GT ) { +dd_dev_info (dd,"Parent PCIe bridge does not support Gen3\n" ); + dd->link_gen3_capable =0 ; + } + +/* obtain the link width and current speed */ +update_lbus_info (dd); + +/* check against expected pcie width and complain if "wrong" */ +if (dd->lbus_width <16 ) +dd_dev_err (dd,"PCIe width %u (x16 HCA), performance reduced\n" , + dd->lbus_width); + +return 0 ; +} + +/* +* Returns in *nent: +* - actual number of interrupts allocated +* - 0 if fell back to INTx. +*/ +void request_msix (struct hfi1_devdata *dd,u32 *nent, +struct hfi1_msix_entry *entry) +{ +int pos; + + pos = dd->pcidev->msix_cap; +if (*nent && pos) { +msix_setup (dd, pos, nent, entry); +/* did it, either MSI-X or INTx */ + }else { + *nent =0 ; +hfi1_enable_intx (dd->pcidev); + } + +tune_pcie_caps (dd); +} + +/* +* Disable MSI-X. +*/ +void hfi1_nomsix (struct hfi1_devdata *dd) +{ +pci_disable_msix (dd->pcidev); +} + +void hfi1_enable_intx (struct pci_dev *pdev) +{ +/* first, turn on INTx */ +pci_intx (pdev,1 ); +/* then turn off MSI-X */ +pci_disable_msix (pdev); +} + +/* restore command and BARs after a reset has wiped them out */ +void restore_pci_variables (struct hfi1_devdata *dd) +{ +pci_write_config_word (dd->pcidev,PCI_COMMAND , dd->pci_command); +pci_write_config_dword (dd->pcidev, +PCI_BASE_ADDRESS_0 , dd->pcibar0); +pci_write_config_dword (dd->pcidev, +PCI_BASE_ADDRESS_1 , dd->pcibar1); +pci_write_config_dword (dd->pcidev, +PCI_ROM_ADDRESS , dd->pci_rom); +pcie_capability_write_word (dd->pcidev,PCI_EXP_DEVCTL , dd->pcie_devctl); +pcie_capability_write_word (dd->pcidev,PCI_EXP_LNKCTL , dd->pcie_lnkctl); +pcie_capability_write_word (dd->pcidev,PCI_EXP_DEVCTL2 , + dd->pcie_devctl2); +pci_write_config_dword (dd->pcidev,PCI_CFG_MSIX0 , dd->pci_msix0); +pci_write_config_dword (dd->pcidev,PCIE_CFG_SPCIE1 , + dd->pci_lnkctl3); +pci_write_config_dword (dd->pcidev,PCIE_CFG_TPH2 , dd->pci_tph2); +} + + +/* +* BIOS may not set PCIe bus-utilization parameters for best performance. +* Check and optionally adjust them to maximize our throughput. +*/ +static int hfi1_pcie_caps; +module_param_named (pcie_caps, hfi1_pcie_caps, int,S_IRUGO ); +MODULE_PARM_DESC (pcie_caps,"Max PCIe tuning: Payload (0..3), ReadReq (4..7)" ); + +static void tune_pcie_caps (struct hfi1_devdata *dd) +{ +struct pci_dev *parent; +u16 rc_mpss, rc_mps, ep_mpss, ep_mps; +u16 rc_mrrs, ep_mrrs, max_mrrs; + +/* Find out supported and configured values for parent (root) */ + parent = dd->pcidev->bus->self; +if (!pci_is_root_bus (parent->bus)) { +dd_dev_info (dd,"Parent not root\n" ); +return ; + } + +if (!pci_is_pcie (parent) || !pci_is_pcie (dd->pcidev)) +return ; + rc_mpss = parent->pcie_mpss; + rc_mps =ffs (pcie_get_mps (parent)) -8 ; +/* Find out supported and configured values for endpoint (us) */ + ep_mpss = dd->pcidev->pcie_mpss; + ep_mps =ffs (pcie_get_mps (dd->pcidev)) -8 ; + +/* Find max payload supported by root, endpoint */ +if (rc_mpss > ep_mpss) + rc_mpss = ep_mpss; + +/* If Supported greater than limit in module param, limit it */ +if (rc_mpss > (hfi1_pcie_caps &7 )) + rc_mpss = hfi1_pcie_caps &7 ; +/* If less than (allowed, supported), bump root payload */ +if (rc_mpss > rc_mps) { + rc_mps = rc_mpss; +pcie_set_mps (parent,128 << rc_mps); + } +/* If less than (allowed, supported), bump endpoint payload */ +if (rc_mpss > ep_mps) { + ep_mps = rc_mpss; +pcie_set_mps (dd->pcidev,128 << ep_mps); + } + +/* +* Now the Read Request size. +* No field for max supported, but PCIe spec limits it to 4096, +* which is code '5' (log2(4096) - 7) +*/ + max_mrrs =5 ; +if (max_mrrs > ((hfi1_pcie_caps >>4 ) &7 )) + max_mrrs = (hfi1_pcie_caps >>4 ) &7 ; + + max_mrrs =128 << max_mrrs; + rc_mrrs =pcie_get_readrq (parent); + ep_mrrs =pcie_get_readrq (dd->pcidev); + +if (max_mrrs > rc_mrrs) { + rc_mrrs = max_mrrs; +pcie_set_readrq (parent, rc_mrrs); + } +if (max_mrrs > ep_mrrs) { + ep_mrrs = max_mrrs; +pcie_set_readrq (dd->pcidev, ep_mrrs); + } +} +/* End of PCIe capability tuning */ + +/* +* From here through hfi1_pci_err_handler definition is invoked via +* PCI error infrastructure, registered via pci +*/ +static pci_ers_result_t +pci_error_detected (struct pci_dev *pdev,pci_channel_state_t state) +{ +struct hfi1_devdata *dd =pci_get_drvdata (pdev); +pci_ers_result_t ret =PCI_ERS_RESULT_RECOVERED ; + +switch (state) { +case pci_channel_io_normal: +dd_dev_info (dd,"State Normal, ignoring\n" ); +break ; + +case pci_channel_io_frozen: +dd_dev_info (dd,"State Frozen, requesting reset\n" ); +pci_disable_device (pdev); + ret =PCI_ERS_RESULT_NEED_RESET ; +break ; + +case pci_channel_io_perm_failure: +if (dd) { +dd_dev_info (dd,"State Permanent Failure, disabling\n" ); +/* no more register accesses! */ + dd->flags &= ~HFI1_PRESENT ; +hfi1_disable_after_error (dd); + } +/* else early, or other problem */ + ret =PCI_ERS_RESULT_DISCONNECT ; +break ; + +default :/* shouldn't happen */ +dd_dev_info (dd,"HFI1 PCI errors detected (state %d)\n" , + state); +break ; + } +return ret; +} + +static pci_ers_result_t +pci_mmio_enabled (struct pci_dev *pdev) +{ +u64 words =0U ; +struct hfi1_devdata *dd =pci_get_drvdata (pdev); +pci_ers_result_t ret =PCI_ERS_RESULT_RECOVERED ; + +if (dd && dd->pport) { + words =read_port_cntr (dd->pport,C_RX_WORDS ,CNTR_INVALID_VL ); +if (words == ~0ULL ) + ret =PCI_ERS_RESULT_NEED_RESET ; +dd_dev_info (dd, +"HFI1 mmio_enabled function called, read wordscntr %Lx, returning %d\n" , + words, ret); + } +return ret; +} + +static pci_ers_result_t +pci_slot_reset (struct pci_dev *pdev) +{ +struct hfi1_devdata *dd =pci_get_drvdata (pdev); + +dd_dev_info (dd,"HFI1 slot_reset function called, ignored\n" ); +return PCI_ERS_RESULT_CAN_RECOVER ; +} + +static pci_ers_result_t +pci_link_reset (struct pci_dev *pdev) +{ +struct hfi1_devdata *dd =pci_get_drvdata (pdev); + +dd_dev_info (dd,"HFI1 link_reset function called, ignored\n" ); +return PCI_ERS_RESULT_CAN_RECOVER ; +} + +static void +pci_resume (struct pci_dev *pdev) +{ +struct hfi1_devdata *dd =pci_get_drvdata (pdev); + +dd_dev_info (dd,"HFI1 resume function called\n" ); +pci_cleanup_aer_uncorrect_error_status (pdev); +/* +* Running jobs will fail, since it's asynchronous +* unlike sysfs-requested reset. Better than +* doing nothing. +*/ +hfi1_init (dd,1 );/* same as re-init after reset */ +} + +const struct pci_error_handlers hfi1_pci_err_handler = { + .error_detected = pci_error_detected, + .mmio_enabled = pci_mmio_enabled, + .link_reset = pci_link_reset, + .slot_reset = pci_slot_reset, + .resume = pci_resume, +}; + +/*============================================================================*/ +/* PCIe Gen3 support */ + +/* +* This code is separated out because it is expected to be removed in the +* final shipping product. If not, then it will be revisited and items +* will be moved to more standard locations. +*/ + +/* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_STS field values */ +#define DL_STATUS_HFI0 0x1/* hfi0 firmware download complete */ +#define DL_STATUS_HFI1 0x2/* hfi1 firmware download complete */ +#define DL_STATUS_BOTH 0x3/* hfi0 and hfi1 firmware download complete */ + +/* ASIC_PCI_SD_HOST_STATUS.FW_DNLD_ERR field values */ +#define DL_ERR_NONE 0x0/* no error */ +#define DL_ERR_SWAP_PARITY 0x1/* parity error in SerDes interrupt */ +/* or response data */ +#define DL_ERR_DISABLED 0x2/* hfi disabled */ +#define DL_ERR_SECURITY 0x3/* security check failed */ +#define DL_ERR_SBUS 0x4/* SBus status error */ +#define DL_ERR_XFR_PARITY 0x5/* parity error during ROM transfer*/ + +/* gasket block secondary bus reset delay */ +#define SBR_DELAY_US 200000/* 200ms */ + +/* mask for PCIe capability register lnkctl2 target link speed */ +#define LNKCTL2_TARGET_LINK_SPEED_MASK 0xf + +static uint pcie_target =3 ; +module_param (pcie_target, uint,S_IRUGO ); +MODULE_PARM_DESC (pcie_target,"PCIe target speed (0 skip, 1-3 Gen1-3)" ); + +static uint pcie_force; +module_param (pcie_force, uint,S_IRUGO ); +MODULE_PARM_DESC (pcie_force,"Force driver to do a PCIe firmware download even if already at target speed" ); + +static uint pcie_retry =5 ; +module_param (pcie_retry, uint,S_IRUGO ); +MODULE_PARM_DESC (pcie_retry,"Driver will try this many times to reach requested speed" ); + +#define UNSET_PSET 255 +#define DEFAULT_DISCRETE_PSET 2/* discrete HFI */ +#define DEFAULT_MCP_PSET 4/* MCP HFI */ +static uint pcie_pset =UNSET_PSET ; +module_param (pcie_pset, uint,S_IRUGO ); +MODULE_PARM_DESC (pcie_pset,"PCIe Eq Pset value to use, range is 0-10" ); + +/* equalization columns */ +#define PREC 0 +#define ATTN 1 +#define POST 2 + +/* discrete silicon preliminary equalization values */ +static const u8 discrete_preliminary_eq[11 ][3 ] = { +/* prec attn post */ + {0x00 ,0x00 ,0x12 },/* p0 */ + {0x00 ,0x00 ,0x0c },/* p1 */ + {0x00 ,0x00 ,0x0f },/* p2 */ + {0x00 ,0x00 ,0x09 },/* p3 */ + {0x00 ,0x00 ,0x00 },/* p4 */ + {0x06 ,0x00 ,0x00 },/* p5 */ + {0x09 ,0x00 ,0x00 },/* p6 */ + {0x06 ,0x00 ,0x0f },/* p7 */ + {0x09 ,0x00 ,0x09 },/* p8 */ + {0x0c ,0x00 ,0x00 },/* p9 */ + {0x00 ,0x00 ,0x18 },/* p10 */ +}; + +/* integrated silicon preliminary equalization values */ +static const u8 integrated_preliminary_eq[11 ][3 ] = { +/* prec attn post */ + {0x00 ,0x1e ,0x07 },/* p0 */ + {0x00 ,0x1e ,0x05 },/* p1 */ + {0x00 ,0x1e ,0x06 },/* p2 */ + {0x00 ,0x1e ,0x04 },/* p3 */ + {0x00 ,0x1e ,0x00 },/* p4 */ + {0x03 ,0x1e ,0x00 },/* p5 */ + {0x04 ,0x1e ,0x00 },/* p6 */ + {0x03 ,0x1e ,0x06 },/* p7 */ + {0x03 ,0x1e ,0x04 },/* p8 */ + {0x05 ,0x1e ,0x00 },/* p9 */ + {0x00 ,0x1e ,0x0a },/* p10 */ +}; + +/* helper to format the value to write to hardware */ +#define eq_value (pre, curr, post) \ + ((((u32)(pre)) << \ + PCIE_CFG_REG_PL102_GEN3_EQ_PRE_CURSOR_PSET_SHIFT) \ + | (((u32)(curr)) << PCIE_CFG_REG_PL102_GEN3_EQ_CURSOR_PSET_SHIFT) \ + | (((u32)(post)) << \ + PCIE_CFG_REG_PL102_GEN3_EQ_POST_CURSOR_PSET_SHIFT)) + +/* +* Load the given EQ preset table into the PCIe hardware. +*/ +static int load_eq_table (struct hfi1_devdata *dd,const u8 eq[11 ][3 ],u8 fs, +u8 div) +{ +struct pci_dev *pdev = dd->pcidev; +u32 hit_error =0 ; +u32 violation; +u32 i; +u8 c_minus1, c0, c_plus1; + +for (i =0 ; i <11 ; i++) { +/* set index */ +pci_write_config_dword (pdev,PCIE_CFG_REG_PL103 , i); +/* write the value */ + c_minus1 = eq[i][PREC ] / div; + c0 = fs - (eq[i][PREC ] / div) - (eq[i][POST ] / div); + c_plus1 = eq[i][POST ] / div; +pci_write_config_dword (pdev,PCIE_CFG_REG_PL102 , +eq_value (c_minus1, c0, c_plus1)); +/* check if these coefficients violate EQ rules */ +pci_read_config_dword (dd->pcidev,PCIE_CFG_REG_PL105 , + &violation); +if (violation + &PCIE_CFG_REG_PL105_GEN3_EQ_VIOLATE_COEF_RULES_SMASK ){ +if (hit_error ==0 ) { +dd_dev_err (dd, +"Gen3 EQ Table Coefficient rule violations\n" ); +dd_dev_err (dd," prec attn post\n" ); + } +dd_dev_err (dd," p%02d: %02x %02x %02x\n" , + i, (u32 )eq[i][0 ], (u32 )eq[i][1 ], (u32 )eq[i][2 ]); +dd_dev_err (dd," %02x %02x %02x\n" , + (u32 )c_minus1, (u32 )c0, (u32 )c_plus1); + hit_error =1 ; + } + } +if (hit_error) +return -EINVAL ; +return 0 ; +} + +/* +* Steps to be done after the PCIe firmware is downloaded and +* before the SBR for the Pcie Gen3. +* The hardware mutex is already being held. +*/ +static void pcie_post_steps (struct hfi1_devdata *dd) +{ +int i; + +set_sbus_fast_mode (dd); +/* +* Write to the PCIe PCSes to set the G3_LOCKED_NEXT bits to 1. +* This avoids a spurious framing error that can otherwise be +* generated by the MAC layer. +* +* Use individual addresses since no broadcast is set up. +*/ +for (i =0 ; i <NUM_PCIE_SERDES ; i++) { +sbus_request (dd, pcie_pcs_addrs[dd->hfi1_id][i], +0x03 ,WRITE_SBUS_RECEIVER ,0x00022132 ); + } + +clear_sbus_fast_mode (dd); +} + +/* +* Trigger a secondary bus reset (SBR) on ourselves using our parent. +* +* Based on pci_parent_bus_reset() which is not exported by the +* kernel core. +*/ +static int trigger_sbr (struct hfi1_devdata *dd) +{ +struct pci_dev *dev = dd->pcidev; +struct pci_dev *pdev; + +/* need a parent */ +if (!dev->bus->self) { +dd_dev_err (dd,"%s: no parent device\n" , __func__); +return -ENOTTY ; + } + +/* should not be anyone else on the bus */ +list_for_each_entry (pdev, &dev->bus->devices, bus_list) +if (pdev != dev) { +dd_dev_err (dd, +"%s: another device is on the same bus\n" , + __func__); +return -ENOTTY ; + } + +/* +* A secondary bus reset (SBR) issues a hot reset to our device. +* The following routine does a 1s wait after the reset is dropped +* per PCI Trhfa (recovery time). PCIe 3.0 section 6.6.1 - +* Conventional Reset, paragraph 3, line 35 also says that a 1s +* delay after a reset is required. Per spec requirements, +* the link is either working or not after that point. +*/ +pci_reset_bridge_secondary_bus (dev->bus->self); + +return 0 ; +} + +/* +* Write the given gasket interrupt register. +*/ +static void write_gasket_interrupt (struct hfi1_devdata *dd,int index, +u16 code,u16 data) +{ +write_csr (dd,ASIC_PCIE_SD_INTRPT_LIST + (index *8 ), + (((u64 )code <<ASIC_PCIE_SD_INTRPT_LIST_INTRPT_CODE_SHIFT ) + |((u64 )data <<ASIC_PCIE_SD_INTRPT_LIST_INTRPT_DATA_SHIFT ))); +} + +/* +* Tell the gasket logic how to react to the reset. +*/ +static void arm_gasket_logic (struct hfi1_devdata *dd) +{ +u64 reg; + + reg = (((u64 )1 << dd->hfi1_id) + <<ASIC_PCIE_SD_HOST_CMD_INTRPT_CMD_SHIFT ) + | ((u64 )pcie_serdes_broadcast[dd->hfi1_id] + <<ASIC_PCIE_SD_HOST_CMD_SBUS_RCVR_ADDR_SHIFT + |ASIC_PCIE_SD_HOST_CMD_SBR_MODE_SMASK + | ((u64 )SBR_DELAY_US &ASIC_PCIE_SD_HOST_CMD_TIMER_MASK ) + <<ASIC_PCIE_SD_HOST_CMD_TIMER_SHIFT + ); +write_csr (dd,ASIC_PCIE_SD_HOST_CMD , reg); +/* read back to push the write */ +read_csr (dd,ASIC_PCIE_SD_HOST_CMD ); +} + +/* +* Do all the steps needed to transition the PCIe link to Gen3 speed. +*/ +int do_pcie_gen3_transition (struct hfi1_devdata *dd) +{ +struct pci_dev *parent; +u64 fw_ctrl; +u64 reg, therm; +u32 reg32, fs, lf; +u32 status, err; +int ret; +int do_retry, retry_count =0 ; +uint default_pset; +u16 target_vector, target_speed; +u16 lnkctl, lnkctl2, vendor; +u8 nsbr =1 ; +u8 div; +const u8 (*eq)[3 ]; +int return_error =0 ; + +/* PCIe Gen3 is for the ASIC only */ +if (dd->icode !=ICODE_RTL_SILICON ) +return 0 ; + +if (pcie_target ==1 ) {/* target Gen1 */ + target_vector =GEN1_SPEED_VECTOR ; + target_speed =2500 ; + }else if (pcie_target ==2 ) {/* target Gen2 */ + target_vector =GEN2_SPEED_VECTOR ; + target_speed =5000 ; + }else if (pcie_target ==3 ) {/* target Gen3 */ + target_vector =GEN3_SPEED_VECTOR ; + target_speed =8000 ; + }else { +/* off or invalid target - skip */ +dd_dev_info (dd,"%s: Skipping PCIe transition\n" , __func__); +return 0 ; + } + +/* if already at target speed, done (unless forced) */ +if (dd->lbus_speed == target_speed) { +dd_dev_info (dd,"%s: PCIe already at gen%d, %s\n" , __func__, + pcie_target, + pcie_force ?"re-doing anyway" :"skipping" ); +if (!pcie_force) +return 0 ; + } + +/* +* A0 needs an additional SBR +*/ +if (is_a0 (dd)) + nsbr++; + +/* +* Do the Gen3 transition. Steps are those of the PCIe Gen3 +* recipe. +*/ + +/* step 1: pcie link working in gen1/gen2 */ + +/* step 2: if either side is not capable of Gen3, done */ +if (pcie_target ==3 && !dd->link_gen3_capable) { +dd_dev_err (dd,"The PCIe link is not Gen3 capable\n" ); + ret = -ENOSYS ; + goto done_no_mutex; + } + +/* hold the HW mutex across the firmware download and SBR */ + ret =acquire_hw_mutex (dd); +if (ret) +return ret; + +/* make sure thermal polling is not causing interrupts */ + therm =read_csr (dd,ASIC_CFG_THERM_POLL_EN ); +if (therm) { +write_csr (dd,ASIC_CFG_THERM_POLL_EN ,0x0 ); +msleep (100 ); +dd_dev_info (dd,"%s: Disabled therm polling\n" , + __func__); + } + +/* step 3: download SBus Master firmware */ +/* step 4: download PCIe Gen3 SerDes firmware */ +retry: +dd_dev_info (dd,"%s: downloading firmware\n" , __func__); + ret =load_pcie_firmware (dd); +if (ret) + goto done; + +/* step 5: set up device parameter settings */ +dd_dev_info (dd,"%s: setting PCIe registers\n" , __func__); + +/* +* PcieCfgSpcie1 - Link Control 3 +* Leave at reset value. No need to set PerfEq - link equalization +* will be performed automatically after the SBR when the target +* speed is 8GT/s. +*/ + +/* clear all 16 per-lane error bits (PCIe: Lane Error Status) */ +pci_write_config_dword (dd->pcidev,PCIE_CFG_SPCIE2 ,0xffff ); + +/* step 5a: Set Synopsys Port Logic registers */ + +/* +* PcieCfgRegPl2 - Port Force Link +* +* Set the low power field to 0x10 to avoid unnecessary power +* management messages. All other fields are zero. +*/ + reg32 =0x10ul <<PCIE_CFG_REG_PL2_LOW_PWR_ENT_CNT_SHIFT ; +pci_write_config_dword (dd->pcidev,PCIE_CFG_REG_PL2 , reg32); + +/* +* PcieCfgRegPl100 - Gen3 Control +* +* turn off PcieCfgRegPl100.Gen3ZRxDcNonCompl +* turn on PcieCfgRegPl100.EqEieosCnt (erratum) +* Everything else zero. +*/ + reg32 =PCIE_CFG_REG_PL100_EQ_EIEOS_CNT_SMASK ; +pci_write_config_dword (dd->pcidev,PCIE_CFG_REG_PL100 , reg32); + +/* +* PcieCfgRegPl101 - Gen3 EQ FS and LF +* PcieCfgRegPl102 - Gen3 EQ Presets to Coefficients Mapping +* PcieCfgRegPl103 - Gen3 EQ Preset Index +* PcieCfgRegPl105 - Gen3 EQ Status +* +* Give initial EQ settings. +*/ +if (dd->pcidev->device ==PCI_DEVICE_ID_INTEL0 ) {/* discrete */ +/* 1000mV, FS=24, LF = 8 */ + fs =24 ; + lf =8 ; + div =3 ; + eq = discrete_preliminary_eq; + default_pset =DEFAULT_DISCRETE_PSET ; + }else { +/* 400mV, FS=29, LF = 9 */ + fs =29 ; + lf =9 ; + div =1 ; + eq = integrated_preliminary_eq; + default_pset =DEFAULT_MCP_PSET ; + } +pci_write_config_dword (dd->pcidev,PCIE_CFG_REG_PL101 , + (fs <<PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_FS_SHIFT ) + | (lf <<PCIE_CFG_REG_PL101_GEN3_EQ_LOCAL_LF_SHIFT )); + ret =load_eq_table (dd, eq, fs, div); +if (ret) + goto done; + +/* +* PcieCfgRegPl106 - Gen3 EQ Control +* +* Set Gen3EqPsetReqVec, leave other fields 0. +*/ +if (pcie_pset ==UNSET_PSET ) + pcie_pset = default_pset; +if (pcie_pset >10 ) {/* valid range is 0-10, inclusive */ +dd_dev_err (dd,"%s: Invalid Eq Pset %u, setting to %d\n" , + __func__, pcie_pset, default_pset); + pcie_pset = default_pset; + } +dd_dev_info (dd,"%s: using EQ Pset %u\n" , __func__, pcie_pset); +pci_write_config_dword (dd->pcidev,PCIE_CFG_REG_PL106 , + ((1 << pcie_pset) + <<PCIE_CFG_REG_PL106_GEN3_EQ_PSET_REQ_VEC_SHIFT ) + |PCIE_CFG_REG_PL106_GEN3_EQ_EVAL2MS_DISABLE_SMASK + |PCIE_CFG_REG_PL106_GEN3_EQ_PHASE23_EXIT_MODE_SMASK ); + +/* +* step 5b: Do post firmware download steps via SBus +*/ +dd_dev_info (dd,"%s: doing pcie post steps\n" , __func__); +pcie_post_steps (dd); + +/* +* step 5c: Program gasket interrupts +*/ +/* set the Rx Bit Rate to REFCLK ratio */ +write_gasket_interrupt (dd,0 ,0x0006 ,0x0050 ); +/* disable pCal for PCIe Gen3 RX equalization */ +write_gasket_interrupt (dd,1 ,0x0026 ,0x5b01 ); +/* +* Enable iCal for PCIe Gen3 RX equalization, and set which +* evaluation of RX_EQ_EVAL will launch the iCal procedure. +*/ +write_gasket_interrupt (dd,2 ,0x0026 ,0x5202 ); +/* terminate list */ +write_gasket_interrupt (dd,3 ,0x0000 ,0x0000 ); + +/* +* step 5d: program XMT margin +* Right now, leave the default alone. To change, do a +* read-modify-write of: +* CcePcieCtrl.XmtMargin +* CcePcieCtrl.XmitMarginOverwriteEnable +*/ + +/* step 5e: disable active state power management (ASPM) */ +dd_dev_info (dd,"%s: clearing ASPM\n" , __func__); +pcie_capability_read_word (dd->pcidev,PCI_EXP_LNKCTL , &lnkctl); + lnkctl &= ~PCI_EXP_LNKCTL_ASPMC ; +pcie_capability_write_word (dd->pcidev,PCI_EXP_LNKCTL , lnkctl); + +/* +* step 5f: clear DirectSpeedChange +* PcieCfgRegPl67.DirectSpeedChange must be zero to prevent the +* change in the speed target from starting before we are ready. +* This field defaults to 0 and we are not changing it, so nothing +* needs to be done. +*/ + +/* step 5g: Set target link speed */ +/* +* Set target link speed to be target on both device and parent. +* On setting the parent: Some system BIOSs "helpfully" set the +* parent target speed to Gen2 to match the ASIC's initial speed. +* We can set the target Gen3 because we have already checked +* that it is Gen3 capable earlier. +*/ +dd_dev_info (dd,"%s: setting parent target link speed\n" , __func__); + parent = dd->pcidev->bus->self; +pcie_capability_read_word (parent,PCI_EXP_LNKCTL2 , &lnkctl2); +dd_dev_info (dd,"%s: ..old link control2: 0x%x\n" , __func__, + (u32 )lnkctl2); +/* only write to parent if target is not as high as ours */ +if ((lnkctl2 &LNKCTL2_TARGET_LINK_SPEED_MASK ) < target_vector) { + lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK ; + lnkctl2 |= target_vector; +dd_dev_info (dd,"%s: ..new link control2: 0x%x\n" , __func__, + (u32 )lnkctl2); +pcie_capability_write_word (parent,PCI_EXP_LNKCTL2 , lnkctl2); + }else { +dd_dev_info (dd,"%s: ..target speed is OK\n" , __func__); + } + +dd_dev_info (dd,"%s: setting target link speed\n" , __func__); +pcie_capability_read_word (dd->pcidev,PCI_EXP_LNKCTL2 , &lnkctl2); +dd_dev_info (dd,"%s: ..old link control2: 0x%x\n" , __func__, + (u32 )lnkctl2); + lnkctl2 &= ~LNKCTL2_TARGET_LINK_SPEED_MASK ; + lnkctl2 |= target_vector; +dd_dev_info (dd,"%s: ..new link control2: 0x%x\n" , __func__, + (u32 )lnkctl2); +pcie_capability_write_word (dd->pcidev,PCI_EXP_LNKCTL2 , lnkctl2); + +/* step 5h: arm gasket logic */ +/* hold DC in reset across the SBR */ +write_csr (dd,CCE_DC_CTRL ,CCE_DC_CTRL_DC_RESET_SMASK ); + (void )read_csr (dd,CCE_DC_CTRL );/* DC reset hold */ +/* save firmware control across the SBR */ + fw_ctrl =read_csr (dd,MISC_CFG_FW_CTRL ); + +dd_dev_info (dd,"%s: arming gasket logic\n" , __func__); +arm_gasket_logic (dd); + +/* +* step 6: quiesce PCIe link +* The chip has already been reset, so there will be no traffic +* from the chip. Linux has no easy way to enforce that it will +* not try to access the device, so we just need to hope it doesn't +* do it while we are doing the reset. +*/ + +/* +* step 7: initiate the secondary bus reset (SBR) +* step 8: hardware brings the links back up +* step 9: wait for link speed transition to be complete +*/ +dd_dev_info (dd,"%s: calling trigger_sbr\n" , __func__); + ret =trigger_sbr (dd); +if (ret) + goto done; + +/* step 10: decide what to do next */ + +/* check if we can read PCI space */ + ret =pci_read_config_word (dd->pcidev,PCI_VENDOR_ID , &vendor); +if (ret) { +dd_dev_info (dd, +"%s: read of VendorID failed after SBR, err %d\n" , + __func__, ret); + return_error =1 ; + goto done; + } +if (vendor ==0xffff ) { +dd_dev_info (dd,"%s: VendorID is all 1s after SBR\n" , __func__); + return_error =1 ; + ret = -EIO ; + goto done; + } + +/* restore PCI space registers we know were reset */ +dd_dev_info (dd,"%s: calling restore_pci_variables\n" , __func__); +restore_pci_variables (dd); +/* restore firmware control */ +write_csr (dd,MISC_CFG_FW_CTRL , fw_ctrl); + +/* +* Check the gasket block status. +* +* This is the first CSR read after the SBR. If the read returns +* all 1s (fails), the link did not make it back. +* +* Once we're sure we can read and write, clear the DC reset after +* the SBR. Then check for any per-lane errors. Then look over +* the status. +*/ + reg =read_csr (dd,ASIC_PCIE_SD_HOST_STATUS ); +dd_dev_info (dd,"%s: gasket block status: 0x%llx\n" , __func__, reg); +if (reg == ~0ull ) {/* PCIe read failed/timeout */ +dd_dev_err (dd,"SBR failed - unable to read from device\n" ); + return_error =1 ; + ret = -ENOSYS ; + goto done; + } + +/* clear the DC reset */ +write_csr (dd,CCE_DC_CTRL ,0 ); +/* Set the LED off */ +if (is_a0 (dd)) +setextled (dd,0 ); + +/* check for any per-lane errors */ +pci_read_config_dword (dd->pcidev,PCIE_CFG_SPCIE2 , ®32); +dd_dev_info (dd,"%s: per-lane errors: 0x%x\n" , __func__, reg32); + +/* extract status, look for our HFI */ + status = (reg >>ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_SHIFT ) + &ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_STS_MASK ; +if ((status & (1 << dd->hfi1_id)) ==0 ) { +dd_dev_err (dd, +"%s: gasket status 0x%x, expecting 0x%x\n" , + __func__, status,1 << dd->hfi1_id); + ret = -EIO ; + goto done; + } + +/* extract error */ + err = (reg >>ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_SHIFT ) + &ASIC_PCIE_SD_HOST_STATUS_FW_DNLD_ERR_MASK ; +if (err) { +dd_dev_err (dd,"%s: gasket error %d\n" , __func__, err); + ret = -EIO ; + goto done; + } + +/* update our link information cache */ +update_lbus_info (dd); +dd_dev_info (dd,"%s: new speed and width: %s\n" , __func__, + dd->lbus_info); + +if (dd->lbus_speed != target_speed) {/* not target */ +/* maybe retry */ + do_retry = retry_count < pcie_retry; +dd_dev_err (dd,"PCIe link speed did not switch to Gen%d%s\n" , + pcie_target, do_retry ?", retrying" :"" ); + retry_count++; +if (do_retry) { +msleep (100 );/* allow time to settle */ + goto retry; + } + ret = -EIO ; + } + +done: +if (therm) { +write_csr (dd,ASIC_CFG_THERM_POLL_EN ,0x1 ); +msleep (100 ); +dd_dev_info (dd,"%s: Re-enable therm polling\n" , + __func__); + } +release_hw_mutex (dd); +done_no_mutex: +/* return no error if it is OK to be at current speed */ +if (ret && !return_error) { +dd_dev_err (dd,"Proceeding at current speed PCIe speed\n" ); + ret =0 ; + } + +dd_dev_info (dd,"%s: done\n" , __func__); +return ret; +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 23/41 IB/hfi1: add pio handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/pio.c | 1764 +++++++++++++++++++++++++++++++++ drivers/infiniband/hw/hfi1/pio.h | 224 ++++ drivers/infiniband/hw/hfi1/pio_copy.c | 867 ++++++++++++++++ 3 files changed, 2855 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/pio.c create mode 100644 drivers/infiniband/hw/hfi1/pio.h create mode 100644 drivers/infiniband/hw/hfi1/pio_copy.c
drivers/infiniband/hw/hfi1/pio.c new file +1764 -0
@@ -0,0 +1,1764 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/delay.h> +#include "hfi.h" +#include "qp.h" +#include "trace.h" + +#define SC_CTXT_PACKET_EGRESS_TIMEOUT 350/* in chip cycles */ + +#define SC (name) SEND_CTXT_##name +/* +* Send Context functions +*/ +static void sc_wait_for_packet_egress (struct send_context *sc,int pause); + +/* +* Set the CM reset bit and wait for it to clear. Use the provided +* sendctrl register. This routine has no locking. +*/ +void __cm_reset (struct hfi1_devdata *dd,u64 sendctrl) +{ +write_csr (dd,SEND_CTRL , sendctrl |SEND_CTRL_CM_RESET_SMASK ); +while (1 ) { +udelay (1 ); + sendctrl =read_csr (dd,SEND_CTRL ); +if ((sendctrl &SEND_CTRL_CM_RESET_SMASK ) ==0 ) +break ; + } +} + +/* defined in header release 48 and higher */ +#ifndef SEND_CTRL_UNSUPPORTED_VL_SHIFT +#define SEND_CTRL_UNSUPPORTED_VL_SHIFT 3 +#define SEND_CTRL_UNSUPPORTED_VL_MASK 0xffull +#define SEND_CTRL_UNSUPPORTED_VL_SMASK (SEND_CTRL_UNSUPPORTED_VL_MASK \ + << SEND_CTRL_UNSUPPORTED_VL_SHIFT) +#endif + +/* global control of PIO send */ +void pio_send_control (struct hfi1_devdata *dd,int op) +{ +u64 reg; +unsigned long flags; +int write =1 ;/* write sendctrl back */ +int flush =0 ;/* re-read sendctrl to make sure it is flushed */ + +spin_lock_irqsave (&dd->sendctrl_lock, flags); + + reg =read_csr (dd,SEND_CTRL ); +switch (op) { +case PSC_GLOBAL_ENABLE : + reg |=SEND_CTRL_SEND_ENABLE_SMASK ; +break ; +case PSC_GLOBAL_DISABLE : + reg &= ~SEND_CTRL_SEND_ENABLE_SMASK ; +break ; +case PSC_GLOBAL_VLARB_ENABLE : + reg |=SEND_CTRL_VL_ARBITER_ENABLE_SMASK ; +break ; +case PSC_GLOBAL_VLARB_DISABLE : + reg &= ~SEND_CTRL_VL_ARBITER_ENABLE_SMASK ; +break ; +case PSC_CM_RESET : +__cm_reset (dd, reg); + write =0 ;/* CSR already written (and flushed) */ +break ; +case PSC_DATA_VL_ENABLE : + reg &= ~SEND_CTRL_UNSUPPORTED_VL_SMASK ; +break ; +case PSC_DATA_VL_DISABLE : + reg |=SEND_CTRL_UNSUPPORTED_VL_SMASK ; + flush =1 ; +break ; +default : +dd_dev_err (dd,"%s: invalid control %d\n" , __func__, op); +break ; + } + +if (write) { +write_csr (dd,SEND_CTRL , reg); +if (flush) + (void )read_csr (dd,SEND_CTRL );/* flush write */ + } + +spin_unlock_irqrestore (&dd->sendctrl_lock, flags); +} + +/* number of send context memory pools */ +#define NUM_SC_POOLS 2 + +/* Send Context Size (SCS) wildcards */ +#define SCS_POOL_0 -1 +#define SCS_POOL_1 -2 +/* Send Context Count (SCC) wildcards */ +#define SCC_PER_VL -1 +#define SCC_PER_CPU -2 + +#define SCC_PER_KRCVQ -3 +#define SCC_ACK_CREDITS 32 + +#define PIO_WAIT_BATCH_SIZE 5 + +/* default send context sizes */ +static struct sc_config_sizes sc_config_sizes[SC_MAX ] = { + [SC_KERNEL ] = { .size =SCS_POOL_0 ,/* even divide, pool 0 */ + .count =SCC_PER_VL },/* one per NUMA */ + [SC_ACK ] = { .size =SCC_ACK_CREDITS , + .count =SCC_PER_KRCVQ }, + [SC_USER ] = { .size =SCS_POOL_0 ,/* even divide, pool 0 */ + .count =SCC_PER_CPU },/* one per CPU */ + +}; + +/* send context memory pool configuration */ +struct mem_pool_config { +int centipercent;/* % of memory, in 100ths of 1% */ +int absolute_blocks;/* absolute block count */ +}; + +/* default memory pool configuration: 100% in pool 0 */ +static struct mem_pool_config sc_mem_pool_config[NUM_SC_POOLS ] = { +/* centi%, abs blocks */ + {10000 ,-1 },/* pool 0 */ + {0 ,-1 },/* pool 1 */ +}; + +/* memory pool information, used when calculating final sizes */ +struct mem_pool_info { +int centipercent;/* 100th of 1% of memory to use, -1 if blocks +already set */ +int count;/* count of contexts in the pool */ +int blocks;/* block size of the pool */ +int size;/* context size, in blocks */ +}; + +/* +* Convert a pool wildcard to a valid pool index. The wildcards +* start at -1 and increase negatively. Map them as: +* -1 => 0 +* -2 => 1 +* etc. +* +* Return -1 on non-wildcard input, otherwise convert to a pool number. +*/ +static int wildcard_to_pool (int wc) +{ +if (wc >=0 ) +return -1 ;/* non-wildcard */ +return -wc -1 ; +} + +static const char *sc_type_names[SC_MAX ] = { +"kernel" , +"ack" , +"user" +}; + +static const char *sc_type_name (int index) +{ +if (index <0 || index >=SC_MAX ) +return "unknown" ; +return sc_type_names[index]; +} + +/* +* Read the send context memory pool configuration and send context +* size configuration. Replace any wildcards and come up with final +* counts and sizes for the send context types. +*/ +int init_sc_pools_and_sizes (struct hfi1_devdata *dd) +{ +struct mem_pool_info mem_pool_info[NUM_SC_POOLS ] = { {0 } }; +int total_blocks = (dd->chip_pio_mem_size /PIO_BLOCK_SIZE ) -1 ; +int total_contexts =0 ; +int fixed_blocks; +int pool_blocks; +int used_blocks; +int cp_total;/* centipercent total */ +int ab_total;/* absolute block total */ +int extra; +int i; + +/* +* Step 0: +* - copy the centipercents/absolute sizes from the pool config +* - sanity check these values +* - add up centipercents, then later check for full value +* - add up absolute blocks, then later check for over-commit +*/ + cp_total =0 ; + ab_total =0 ; +for (i =0 ; i <NUM_SC_POOLS ; i++) { +int cp = sc_mem_pool_config[i].centipercent; +int ab = sc_mem_pool_config[i].absolute_blocks; + +/* +* A negative value is "unused" or "invalid". Both *can* +* be valid, but centipercent wins, so check that first +*/ +if (cp >=0 ) {/* centipercent valid */ + cp_total += cp; + }else if (ab >=0 ) {/* absolute blocks valid */ + ab_total += ab; + }else {/* neither valid */ +dd_dev_err ( + dd, +"Send context memory pool %d: both the block count and centipercent are invalid\n" , + i); +return -EINVAL ; + } + + mem_pool_info[i].centipercent = cp; + mem_pool_info[i].blocks = ab; + } + +/* do not use both % and absolute blocks for different pools */ +if (cp_total !=0 && ab_total !=0 ) { +dd_dev_err ( + dd, +"All send context memory pools must be described as either centipercent or blocks, no mixing between pools\n" ); +return -EINVAL ; + } + +/* if any percentages are present, they must add up to 100% x 100 */ +if (cp_total !=0 && cp_total !=10000 ) { +dd_dev_err ( + dd, +"Send context memory pool centipercent is %d, expecting 10000\n" , + cp_total); +return -EINVAL ; + } + +/* the absolute pool total cannot be more than the mem total */ +if (ab_total > total_blocks) { +dd_dev_err ( + dd, +"Send context memory pool absolute block count %d is larger than the memory size %d\n" , + ab_total, total_blocks); +return -EINVAL ; + } + +/* +* Step 2: +* - copy from the context size config +* - replace context type wildcard counts with real values +* - add up non-memory pool block sizes +* - add up memory pool user counts +*/ + fixed_blocks =0 ; +for (i =0 ; i <SC_MAX ; i++) { +int count = sc_config_sizes[i].count; +int size = sc_config_sizes[i].size; +int pool; + +/* +* Sanity check count: Either a positive value or +* one of the expected wildcards is valid. The positive +* value is checked later when we compare against total +* memory available. +*/ +if (i ==SC_ACK ) { + count = dd->n_krcv_queues; + }else if (i ==SC_KERNEL ) { + count = num_vls +1 /* VL15 */ ; + }else if (count ==SCC_PER_CPU ) { + count =num_online_cpus (); + }else if (count <0 ) { +dd_dev_err ( + dd, +"%s send context invalid count wildcard %d\n" , +sc_type_name (i), count); +return -EINVAL ; + } +if (total_contexts + count > dd->chip_send_contexts) + count = dd->chip_send_contexts - total_contexts; + + total_contexts += count; + +/* +* Sanity check pool: The conversion will return a pool +* number or -1 if a fixed (non-negative) value. The fixed +* value is checked later when we compare against +* total memory available. +*/ + pool =wildcard_to_pool (size); +if (pool ==-1 ) {/* non-wildcard */ + fixed_blocks += size * count; + }else if (pool <NUM_SC_POOLS ) {/* valid wildcard */ + mem_pool_info[pool].count += count; + }else {/* invalid wildcard */ +dd_dev_err ( + dd, +"%s send context invalid pool wildcard %d\n" , +sc_type_name (i), size); +return -EINVAL ; + } + + dd->sc_sizes[i].count = count; + dd->sc_sizes[i].size = size; + } +if (fixed_blocks > total_blocks) { +dd_dev_err ( + dd, +"Send context fixed block count, %u, larger than total block count %u\n" , + fixed_blocks, total_blocks); +return -EINVAL ; + } + +/* step 3: calculate the blocks in the pools, and pool context sizes */ + pool_blocks = total_blocks - fixed_blocks; +if (ab_total > pool_blocks) { +dd_dev_err ( + dd, +"Send context fixed pool sizes, %u, larger than pool block count %u\n" , + ab_total, pool_blocks); +return -EINVAL ; + } +/* subtract off the fixed pool blocks */ + pool_blocks -= ab_total; + +for (i =0 ; i <NUM_SC_POOLS ; i++) { +struct mem_pool_info *pi = &mem_pool_info[i]; + +/* % beats absolute blocks */ +if (pi->centipercent >=0 ) + pi->blocks = (pool_blocks * pi->centipercent) /10000 ; + +if (pi->blocks ==0 && pi->count !=0 ) { +dd_dev_err ( + dd, +"Send context memory pool %d has %u contexts, but no blocks\n" , + i, pi->count); +return -EINVAL ; + } +if (pi->count ==0 ) { +/* warn about wasted blocks */ +if (pi->blocks !=0 ) +dd_dev_err ( + dd, +"Send context memory pool %d has %u blocks, but zero contexts\n" , + i, pi->blocks); + pi->size =0 ; + }else { + pi->size = pi->blocks / pi->count; + } + } + +/* step 4: fill in the context type sizes from the pool sizes */ + used_blocks =0 ; +for (i =0 ; i <SC_MAX ; i++) { +if (dd->sc_sizes[i].size <0 ) { +unsigned pool =wildcard_to_pool (dd->sc_sizes[i].size); + +BUG_ON (pool >=NUM_SC_POOLS ); + dd->sc_sizes[i].size = mem_pool_info[pool].size; + } +/* make sure we are not larger than what is allowed by the HW */ +#define PIO_MAX_BLOCKS 1024 +if (dd->sc_sizes[i].size >PIO_MAX_BLOCKS ) + dd->sc_sizes[i].size =PIO_MAX_BLOCKS ; + +/* calculate our total usage */ + used_blocks += dd->sc_sizes[i].size * dd->sc_sizes[i].count; + } + extra = total_blocks - used_blocks; +if (extra !=0 ) +dd_dev_info (dd,"unused send context blocks: %d\n" , extra); + +return total_contexts; +} + +int init_send_contexts (struct hfi1_devdata *dd) +{ +u16 base; +int ret, i, j, context; + + ret =init_credit_return (dd); +if (ret) +return ret; + + dd->hw_to_sw =kmalloc_array (TXE_NUM_CONTEXTS ,sizeof (u8), +GFP_KERNEL ); + dd->send_contexts =kcalloc (dd->num_send_contexts, +sizeof (struct send_context_info ), +GFP_KERNEL ); +if (!dd->send_contexts || !dd->hw_to_sw) { +dd_dev_err (dd,"Unable to allocate send context arrays\n" ); +kfree (dd->hw_to_sw); +kfree (dd->send_contexts); +free_credit_return (dd); +return -ENOMEM ; + } + +/* hardware context map starts with invalid send context indices */ +for (i =0 ; i <TXE_NUM_CONTEXTS ; i++) + dd->hw_to_sw[i] =INVALID_SCI ; + +/* +* All send contexts have their credit sizes. Allocate credits +* for each context one after another from the global space. +*/ + context =0 ; + base =1 ; +for (i =0 ; i <SC_MAX ; i++) { +struct sc_config_sizes *scs = &dd->sc_sizes[i]; + +for (j =0 ; j < scs->count; j++) { +struct send_context_info *sci = + &dd->send_contexts[context]; + sci->type = i; + sci->base = base; + sci->credits = scs->size; + + context++; + base += scs->size; + } + } + +return 0 ; +} + +/* +* Allocate a software index and hardware context of the given type. +* +* Must be called with dd->sc_lock held. +*/ +static int sc_hw_alloc (struct hfi1_devdata *dd,int type,u32 *sw_index, +u32 *hw_context) +{ +struct send_context_info *sci; +u32 index; +u32 context; + +for (index =0 , sci = &dd->send_contexts[0 ]; + index < dd->num_send_contexts; index++, sci++) { +if (sci->type == type && sci->allocated ==0 ) { + sci->allocated =1 ; +/* use a 1:1 mapping, but make them non-equal */ + context = dd->chip_send_contexts - index -1 ; + dd->hw_to_sw[context] = index; + *sw_index = index; + *hw_context = context; +return 0 ;/* success */ + } + } +dd_dev_err (dd,"Unable to locate a free type %d send context\n" , type); +return -ENOSPC ; +} + +/* +* Free the send context given by its software index. +* +* Must be called with dd->sc_lock held. +*/ +static void sc_hw_free (struct hfi1_devdata *dd,u32 sw_index,u32 hw_context) +{ +struct send_context_info *sci; + + sci = &dd->send_contexts[sw_index]; +if (!sci->allocated) { +dd_dev_err (dd,"%s: sw_index %u not allocated? hw_context %u\n" , + __func__, sw_index, hw_context); + } + sci->allocated =0 ; + dd->hw_to_sw[hw_context] =INVALID_SCI ; +} + +/* return the base context of a context in a group */ +static inline u32 group_context (u32 context,u32 group) +{ +return (context >> group) << group; +} + +/* return the size of a group */ +static inline u32 group_size (u32 group) +{ +return 1 << group; +} + +/* +* Obtain the credit return addresses, kernel virtual and physical, for the +* given sc. +* +* To understand this routine: +* o va and pa are arrays of struct credit_return. One for each physical +* send context, per NUMA. +* o Each send context always looks in its relative location in a struct +* credit_return for its credit return. +* o Each send context in a group must have its return address CSR programmed +* with the same value. Use the address of the first send context in the +* group. +*/ +static void cr_group_addresses (struct send_context *sc,dma_addr_t *pa) +{ +u32 gc =group_context (sc->hw_context, sc->group); +u32 index = sc->hw_context &0x7 ; + + sc->hw_free = &sc->dd->cr_base[sc->node].va[gc].cr[index]; + *pa = (unsigned long ) + &((struct credit_return *)sc->dd->cr_base[sc->node].pa)[gc]; +} + +/* +* Work queue function triggered in error interrupt routine for +* kernel contexts. +*/ +static void sc_halted (struct work_struct *work) +{ +struct send_context *sc; + + sc = container_of(work, struct send_context, halt_work); +sc_restart (sc); +} + +/* +* Calculate PIO block threshold for this send context using the given MTU. +* Trigger a return when one MTU plus optional header of credits remain. +* +* Parameter mtu is in bytes. +* Parameter hdrqentsize is in DWORDs. +* +* Return value is what to write into the CSR: trigger return when +* unreturned credits pass this count. +*/ +u32 sc_mtu_to_threshold (struct send_context *sc,u32 mtu,u32 hdrqentsize) +{ +u32 release_credits; +u32 threshold; + +/* add in the header size, then divide by the PIO block size */ + mtu += hdrqentsize <<2 ; + release_credits =DIV_ROUND_UP (mtu,PIO_BLOCK_SIZE ); + +/* check against this context's credits */ +if (sc->credits <= release_credits) + threshold =1 ; +else + threshold = sc->credits - release_credits; + +return threshold; +} + +/* +* Calculate credit threshold in terms of percent of the allocated credits. +* Trigger when unreturned credits equal or exceed the percentage of the whole. +* +* Return value is what to write into the CSR: trigger return when +* unreturned credits pass this count. +*/ +static u32 sc_percent_to_threshold (struct send_context *sc,u32 percent) +{ +return (sc->credits * percent) /100 ; +} + +/* +* Set the credit return threshold. +*/ +void sc_set_cr_threshold (struct send_context *sc,u32 new_threshold) +{ +unsigned long flags; +u32 old_threshold; +int force_return =0 ; + +spin_lock_irqsave (&sc->credit_ctrl_lock, flags); + + old_threshold = (sc->credit_ctrl >> +SC (CREDIT_CTRL_THRESHOLD_SHIFT )) + &SC (CREDIT_CTRL_THRESHOLD_MASK ); + +if (new_threshold != old_threshold) { + sc->credit_ctrl = + (sc->credit_ctrl + & ~SC (CREDIT_CTRL_THRESHOLD_SMASK )) + | ((new_threshold + &SC (CREDIT_CTRL_THRESHOLD_MASK )) + <<SC (CREDIT_CTRL_THRESHOLD_SHIFT )); +write_kctxt_csr (sc->dd, sc->hw_context, +SC (CREDIT_CTRL ), sc->credit_ctrl); + +/* force a credit return on change to avoid a possible stall */ + force_return =1 ; + } + +spin_unlock_irqrestore (&sc->credit_ctrl_lock, flags); + +if (force_return) +sc_return_credits (sc); +} + +/* +* set_pio_integrity +* +* Set the CHECK_ENABLE register for the send context 'sc'. +*/ +void set_pio_integrity (struct send_context *sc) +{ +struct hfi1_devdata *dd = sc->dd; +u64 reg =0 ; +u32 hw_context = sc->hw_context; +int type = sc->type; + +/* +* No integrity checks if HFI1_CAP_NO_INTEGRITY is set, or if +* we're snooping. +*/ +if (likely (!HFI1_CAP_IS_KSET (NO_INTEGRITY )) && + dd->hfi1_snoop.mode_flag !=HFI1_PORT_SNOOP_MODE ) + reg =hfi1_pkt_default_send_ctxt_mask (dd, type); + +write_kctxt_csr (dd, hw_context,SC (CHECK_ENABLE ), reg); +} + +/* +* Allocate a NUMA relative send context structure of the given type along +* with a HW context. +*/ +struct send_context *sc_alloc (struct hfi1_devdata *dd,int type, +uint hdrqentsize,int numa) +{ +struct send_context_info *sci; +struct send_context *sc; +dma_addr_t pa; +unsigned long flags; +u64 reg; +u32 thresh; +u32 sw_index; +u32 hw_context; +int ret; +u8 opval, opmask; + +/* do not allocate while frozen */ +if (dd->flags &HFI1_FROZEN ) +return NULL ; + + sc =kzalloc_node (sizeof (struct send_context ),GFP_KERNEL , numa); +if (!sc) { +dd_dev_err (dd,"Cannot allocate send context structure\n" ); +return NULL ; + } + +spin_lock_irqsave (&dd->sc_lock, flags); + ret =sc_hw_alloc (dd, type, &sw_index, &hw_context); +if (ret) { +spin_unlock_irqrestore (&dd->sc_lock, flags); +kfree (sc); +return NULL ; + } + + sci = &dd->send_contexts[sw_index]; + sci->sc = sc; + + sc->dd = dd; + sc->node = numa; + sc->type = type; +spin_lock_init (&sc->alloc_lock); +spin_lock_init (&sc->release_lock); +spin_lock_init (&sc->credit_ctrl_lock); +INIT_LIST_HEAD (&sc->piowait); +INIT_WORK (&sc->halt_work, sc_halted); +atomic_set (&sc->buffers_allocated,0 ); +init_waitqueue_head (&sc->halt_wait); + +/* grouping is always single context for now */ + sc->group =0 ; + + sc->sw_index = sw_index; + sc->hw_context = hw_context; +cr_group_addresses (sc, &pa); + sc->credits = sci->credits; + +/* PIO Send Memory Address details */ +#define PIO_ADDR_CONTEXT_MASK 0xfful +#define PIO_ADDR_CONTEXT_SHIFT 16 + sc->base_addr = dd->piobase + ((hw_context &PIO_ADDR_CONTEXT_MASK ) + <<PIO_ADDR_CONTEXT_SHIFT ); + +/* set base and credits */ + reg = ((sci->credits &SC (CTRL_CTXT_DEPTH_MASK )) + <<SC (CTRL_CTXT_DEPTH_SHIFT )) + | ((sci->base &SC (CTRL_CTXT_BASE_MASK )) + <<SC (CTRL_CTXT_BASE_SHIFT )); +write_kctxt_csr (dd, hw_context,SC (CTRL ), reg); + +set_pio_integrity (sc); + +/* unmask all errors */ +write_kctxt_csr (dd, hw_context,SC (ERR_MASK ), (u64)-1 ); + +/* set the default partition key */ +write_kctxt_csr (dd, hw_context,SC (CHECK_PARTITION_KEY ), + (DEFAULT_PKEY & +SC (CHECK_PARTITION_KEY_VALUE_MASK )) + <<SC (CHECK_PARTITION_KEY_VALUE_SHIFT )); + +/* per context type checks */ +if (type ==SC_USER ) { + opval =USER_OPCODE_CHECK_VAL ; + opmask =USER_OPCODE_CHECK_MASK ; + }else { + opval =OPCODE_CHECK_VAL_DISABLED ; + opmask =OPCODE_CHECK_MASK_DISABLED ; + } + +/* set the send context check opcode mask and value */ +write_kctxt_csr (dd, hw_context,SC (CHECK_OPCODE ), + ((u64 )opmask <<SC (CHECK_OPCODE_MASK_SHIFT )) | + ((u64 )opval <<SC (CHECK_OPCODE_VALUE_SHIFT ))); + +/* set up credit return */ + reg = pa &SC (CREDIT_RETURN_ADDR_ADDRESS_SMASK ); +write_kctxt_csr (dd, hw_context,SC (CREDIT_RETURN_ADDR ), reg); + +/* +* Calculate the initial credit return threshold. +* +* For Ack contexts, set a threshold for half the credits. +* For User contexts use the given percentage. This has been +* sanitized on driver start-up. +* For Kernel contexts, use the default MTU plus a header. +*/ +if (type ==SC_ACK ) { + thresh =sc_percent_to_threshold (sc,50 ); + }else if (type ==SC_USER ) { + thresh =sc_percent_to_threshold (sc, + user_credit_return_threshold); + }else {/* kernel */ + thresh =sc_mtu_to_threshold (sc, hfi1_max_mtu, hdrqentsize); + } + reg = thresh <<SC (CREDIT_CTRL_THRESHOLD_SHIFT ); +/* add in early return */ +if (type ==SC_USER &&HFI1_CAP_IS_USET (EARLY_CREDIT_RETURN )) + reg |=SC (CREDIT_CTRL_EARLY_RETURN_SMASK ); +else if (HFI1_CAP_IS_KSET (EARLY_CREDIT_RETURN ))/* kernel, ack */ + reg |=SC (CREDIT_CTRL_EARLY_RETURN_SMASK ); + +/* set up write-through credit_ctrl */ + sc->credit_ctrl = reg; +write_kctxt_csr (dd, hw_context,SC (CREDIT_CTRL ), reg); + +spin_unlock_irqrestore (&dd->sc_lock, flags); + +/* +* Allocate shadow ring to track outstanding PIO buffers _after_ +* unlocking. We don't know the size until the lock is held and +* we can't allocate while the lock is held. No one is using +* the context yet, so allocate it now. +* +* User contexts do not get a shadow ring. +*/ +if (type !=SC_USER ) { +/* +* Size the shadow ring 1 larger than the number of credits +* so head == tail can mean empty. +*/ + sc->sr_size = sci->credits +1 ; + sc->sr =kzalloc_node (sizeof (union pio_shadow_ring ) * + sc->sr_size,GFP_KERNEL , numa); +if (!sc->sr) { +dd_dev_err (dd, +"Cannot allocate send context shadow ring structure\n" ); +sc_free (sc); +return NULL ; + } + } + +dd_dev_info (dd, +"Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n" , + sw_index, + hw_context, +sc_type_name (type), + sc->group, + sc->credits, + sc->credit_ctrl, + thresh); + +return sc; +} + +/* free a per-NUMA send context structure */ +void sc_free (struct send_context *sc) +{ +struct hfi1_devdata *dd; +unsigned long flags; +u32 sw_index; +u32 hw_context; + +if (!sc) +return ; + + sc->flags |=SCF_IN_FREE ;/* ensure no restarts */ + dd = sc->dd; +if (!list_empty (&sc->piowait)) +dd_dev_err (dd,"piowait list not empty!\n" ); + sw_index = sc->sw_index; + hw_context = sc->hw_context; +sc_disable (sc);/* make sure the HW is disabled */ +flush_work (&sc->halt_work); + +spin_lock_irqsave (&dd->sc_lock, flags); + dd->send_contexts[sw_index].sc =NULL ; + +/* clear/disable all registers set in sc_alloc */ +write_kctxt_csr (dd, hw_context,SC (CTRL ),0 ); +write_kctxt_csr (dd, hw_context,SC (CHECK_ENABLE ),0 ); +write_kctxt_csr (dd, hw_context,SC (ERR_MASK ),0 ); +write_kctxt_csr (dd, hw_context,SC (CHECK_PARTITION_KEY ),0 ); +write_kctxt_csr (dd, hw_context,SC (CHECK_OPCODE ),0 ); +write_kctxt_csr (dd, hw_context,SC (CREDIT_RETURN_ADDR ),0 ); +write_kctxt_csr (dd, hw_context,SC (CREDIT_CTRL ),0 ); + +/* release the index and context for re-use */ +sc_hw_free (dd, sw_index, hw_context); +spin_unlock_irqrestore (&dd->sc_lock, flags); + +kfree (sc->sr); +kfree (sc); +} + +/* disable the context */ +void sc_disable (struct send_context *sc) +{ +u64 reg; +unsigned long flags; +struct pio_buf *pbuf; + +if (!sc) +return ; + +/* do all steps, even if already disabled */ +spin_lock_irqsave (&sc->alloc_lock, flags); + reg =read_kctxt_csr (sc->dd, sc->hw_context,SC (CTRL )); + reg &= ~SC (CTRL_CTXT_ENABLE_SMASK ); + sc->flags &= ~SCF_ENABLED ; +sc_wait_for_packet_egress (sc,1 ); +write_kctxt_csr (sc->dd, sc->hw_context,SC (CTRL ), reg); +spin_unlock_irqrestore (&sc->alloc_lock, flags); + +/* +* Flush any waiters. Once the context is disabled, +* credit return interrupts are stopped (although there +* could be one in-process when the context is disabled). +* Wait one microsecond for any lingering interrupts, then +* proceed with the flush. +*/ +udelay (1 ); +spin_lock_irqsave (&sc->release_lock, flags); +if (sc->sr) {/* this context has a shadow ring */ +while (sc->sr_tail != sc->sr_head) { + pbuf = &sc->sr[sc->sr_tail].pbuf; +if (pbuf->cb) + (*pbuf->cb)(pbuf->arg,PRC_SC_DISABLE ); + sc->sr_tail++; +if (sc->sr_tail >= sc->sr_size) + sc->sr_tail =0 ; + } + } +spin_unlock_irqrestore (&sc->release_lock, flags); +} + +/* return SendEgressCtxtStatus.PacketOccupancy */ +#define packet_occupancy (r) \ + (((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)\ + >> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT) + +/* is egress halted on the context? */ +#define egress_halted (r) \ + ((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK) + +/* wait for packet egress, optionally pause for credit return */ +static void sc_wait_for_packet_egress (struct send_context *sc,int pause) +{ +struct hfi1_devdata *dd = sc->dd; +u64 reg; +u32 loop =0 ; + +while (1 ) { + reg =read_csr (dd, sc->hw_context *8 + +SEND_EGRESS_CTXT_STATUS ); +/* done if egress is stopped */ +if (egress_halted (reg)) +break ; + reg =packet_occupancy (reg); +if (reg ==0 ) +break ; +if (loop >100 ) { +dd_dev_err (dd, +"%s: context %u(%u) timeout waiting for packets to egress, remaining count %u\n" , + __func__, sc->sw_index, + sc->hw_context, (u32 )reg); +break ; + } + loop++; +udelay (1 ); + } + +if (pause) +/* Add additional delay to ensure chip returns all credits */ +pause_for_credit_return (dd); +} + +void sc_wait (struct hfi1_devdata *dd) +{ +int i; + +for (i =0 ; i < dd->num_send_contexts; i++) { +struct send_context *sc = dd->send_contexts[i].sc; + +if (!sc) +continue ; +sc_wait_for_packet_egress (sc,0 ); + } +} + +/* +* Restart a context after it has been halted due to error. +* +* If the first step fails - wait for the halt to be asserted, return early. +* Otherwise complain about timeouts but keep going. +* +* It is expected that allocations (enabled flag bit) have been shut off +* already (only applies to kernel contexts). +*/ +int sc_restart (struct send_context *sc) +{ +struct hfi1_devdata *dd = sc->dd; +u64 reg; +u32 loop; +int count; + +/* bounce off if not halted, or being free'd */ +if (!(sc->flags &SCF_HALTED ) || (sc->flags &SCF_IN_FREE )) +return -EINVAL ; + +dd_dev_info (dd,"restarting send context %u(%u)\n" , sc->sw_index, + sc->hw_context); + +/* +* Step 1: Wait for the context to actually halt. +* +* The error interrupt is asynchronous to actually setting halt +* on the context. +*/ + loop =0 ; +while (1 ) { + reg =read_kctxt_csr (dd, sc->hw_context,SC (STATUS )); +if (reg &SC (STATUS_CTXT_HALTED_SMASK )) +break ; +if (loop >100 ) { +dd_dev_err (dd,"%s: context %u(%u) not halting, skipping\n" , + __func__, sc->sw_index, sc->hw_context); +return -ETIME ; + } + loop++; +udelay (1 ); + } + +/* +* Step 2: Ensure no users are still trying to write to PIO. +* +* For kernel contexts, we have already turned off buffer allocation. +* Now wait for the buffer count to go to zero. +* +* For user contexts, the user handling code has cut off write access +* to the context's PIO pages before calling this routine and will +* restore write access after this routine returns. +*/ +if (sc->type !=SC_USER ) { +/* kernel context */ + loop =0 ; +while (1 ) { + count =atomic_read (&sc->buffers_allocated); +if (count ==0 ) +break ; +if (loop >100 ) { +dd_dev_err (dd, +"%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n" , + __func__, sc->sw_index, + sc->hw_context, count); + } + loop++; +udelay (1 ); + } + } + +/* +* Step 3: Wait for all packets to egress. +* This is done while disabling the send context +* +* Step 4: Disable the context +* +* This is a superset of the halt. After the disable, the +* errors can be cleared. +*/ +sc_disable (sc); + +/* +* Step 5: Enable the context +* +* This enable will clear the halted flag and per-send context +* error flags. +*/ +return sc_enable (sc); +} + +/* +* PIO freeze processing. To be called after the TXE block is fully frozen. +* Go through all frozen send contexts and disable them. The contexts are +* already stopped by the freeze. +*/ +void pio_freeze (struct hfi1_devdata *dd) +{ +struct send_context *sc; +int i; + +for (i =0 ; i < dd->num_send_contexts; i++) { + sc = dd->send_contexts[i].sc; +/* +* Don't disable unallocated, unfrozen, or user send contexts. +* User send contexts will be disabled when the process +* calls into the driver to reset its context. +*/ +if (!sc || !(sc->flags &SCF_FROZEN ) || sc->type ==SC_USER ) +continue ; + +/* only need to disable, the context is already stopped */ +sc_disable (sc); + } +} + +/* +* Unfreeze PIO for kernel send contexts. The precondition for calling this +* is that all PIO send contexts have been disabled and the SPC freeze has +* been cleared. Now perform the last step and re-enable each kernel context. +* User (PSM) processing will occur when PSM calls into the kernel to +* acknowledge the freeze. +*/ +void pio_kernel_unfreeze (struct hfi1_devdata *dd) +{ +struct send_context *sc; +int i; + +for (i =0 ; i < dd->num_send_contexts; i++) { + sc = dd->send_contexts[i].sc; +if (!sc || !(sc->flags &SCF_FROZEN ) || sc->type ==SC_USER ) +continue ; + +sc_enable (sc);/* will clear the sc frozen flag */ + } +} + +/* +* Wait for the SendPioInitCtxt.PioInitInProgress bit to clear. +* Returns: +* -ETIMEDOUT - if we wait too long +* -EIO - if there was an error +*/ +static int pio_init_wait_progress (struct hfi1_devdata *dd) +{ +u64 reg; +int count =0 ; + +while (1 ) { + reg =read_csr (dd,SEND_PIO_INIT_CTXT ); +if (!(reg &SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK )) +break ; +mdelay (20 ); +if (count++ >10 ) +return -ETIMEDOUT ; + } + +return reg &SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO :0 ; +} + +/* +* Reset all of the send contexts to their power-on state. Used +* only during manual init - no lock against sc_enable needed. +*/ +void pio_reset_all (struct hfi1_devdata *dd) +{ +int ret; + +/* make sure the init engine is not busy */ + ret =pio_init_wait_progress (dd); +if (ret == -ETIMEDOUT ) { +dd_dev_err (dd, +"PIO send context init is stuck, not initializing PIO blocks\n" ); +return ; + } + +if (ret == -EIO ) { +/* clear the error */ +write_csr (dd,SEND_PIO_ERR_CLEAR , +SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK ); + } + +/* reset init all */ +write_csr (dd,SEND_PIO_INIT_CTXT , +SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK ); +udelay (2 ); + ret =pio_init_wait_progress (dd); +if (ret <0 ) { +dd_dev_err (dd, +"PIO send context init %s while initializing all PIO blocks\n" , + ret == -ETIMEDOUT ?"is stuck" :"had an error" ); + } +} + +/* enable the context */ +int sc_enable (struct send_context *sc) +{ +u64 sc_ctrl, reg, pio; +struct hfi1_devdata *dd; +unsigned long flags; +int ret =0 ; + +if (!sc) +return -EINVAL ; + dd = sc->dd; + +/* +* Obtain the allocator lock to guard against any allocation +* attempts (which should not happen prior to context being +* enabled). On the release/disable side we don't need to +* worry about locking since the releaser will not do anything +* if the context accounting values have not changed. +*/ +spin_lock_irqsave (&sc->alloc_lock, flags); + sc_ctrl =read_kctxt_csr (dd, sc->hw_context,SC (CTRL )); +if ((sc_ctrl &SC (CTRL_CTXT_ENABLE_SMASK ))) + goto unlock;/* already enabled */ + +/* IMPORTANT: only clear free and fill if transitioning 0 -> 1 */ + + *sc->hw_free =0 ; + sc->free =0 ; + sc->alloc_free =0 ; + sc->fill =0 ; + sc->sr_head =0 ; + sc->sr_tail =0 ; + sc->flags =0 ; +atomic_set (&sc->buffers_allocated,0 ); + +/* +* Clear all per-context errors. Some of these will be set when +* we are re-enabling after a context halt. Now that the context +* is disabled, the halt will not clear until after the PIO init +* engine runs below. +*/ + reg =read_kctxt_csr (dd, sc->hw_context,SC (ERR_STATUS )); +if (reg) +write_kctxt_csr (dd, sc->hw_context,SC (ERR_CLEAR ), + reg); + +/* +* The HW PIO initialization engine can handle only one init +* request at a time. Serialize access to each device's engine. +*/ +spin_lock (&dd->sc_init_lock); +/* +* Since access to this code block is serialized and +* each access waits for the initialization to complete +* before releasing the lock, the PIO initialization engine +* should not be in use, so we don't have to wait for the +* InProgress bit to go down. +*/ + pio = ((sc->hw_context &SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK ) << +SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT ) | +SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK ; +write_csr (dd,SEND_PIO_INIT_CTXT , pio); +/* +* Wait until the engine is done. Give the chip the required time +* so, hopefully, we read the register just once. +*/ +udelay (2 ); + ret =pio_init_wait_progress (dd); +spin_unlock (&dd->sc_init_lock); +if (ret) { +dd_dev_err (dd, +"sctxt%u(%u): Context not enabled due to init failure %d\n" , + sc->sw_index, sc->hw_context, ret); + goto unlock; + } + +/* +* All is well. Enable the context. +*/ + sc_ctrl |=SC (CTRL_CTXT_ENABLE_SMASK ); +write_kctxt_csr (dd, sc->hw_context,SC (CTRL ), sc_ctrl); +/* +* Read SendCtxtCtrl to force the write out and prevent a timing +* hazard where a PIO write may reach the context before the enable. +*/ +read_kctxt_csr (dd, sc->hw_context,SC (CTRL )); + sc->flags |=SCF_ENABLED ; + +unlock: +spin_unlock_irqrestore (&sc->alloc_lock, flags); + +return ret; +} + +/* force a credit return on the context */ +void sc_return_credits (struct send_context *sc) +{ +if (!sc) +return ; + +/* a 0->1 transition schedules a credit return */ +write_kctxt_csr (sc->dd, sc->hw_context,SC (CREDIT_FORCE ), +SC (CREDIT_FORCE_FORCE_RETURN_SMASK )); +/* +* Ensure that the write is flushed and the credit return is +* scheduled. We care more about the 0 -> 1 transition. +*/ +read_kctxt_csr (sc->dd, sc->hw_context,SC (CREDIT_FORCE )); +/* set back to 0 for next time */ +write_kctxt_csr (sc->dd, sc->hw_context,SC (CREDIT_FORCE ),0 ); +} + +/* allow all in-flight packets to drain on the context */ +void sc_flush (struct send_context *sc) +{ +if (!sc) +return ; + +sc_wait_for_packet_egress (sc,1 ); +} + +/* drop all packets on the context, no waiting until they are sent */ +void sc_drop (struct send_context *sc) +{ +if (!sc) +return ; + +dd_dev_info (sc->dd,"%s: context %u(%u) - not implemented\n" , + __func__, sc->sw_index, sc->hw_context); +} + +/* +* Start the software reaction to a context halt or SPC freeze: +* - mark the context as halted or frozen +* - stop buffer allocations +* +* Called from the error interrupt. Other work is deferred until +* out of the interrupt. +*/ +void sc_stop (struct send_context *sc,int flag) +{ +unsigned long flags; + +/* mark the context */ + sc->flags |= flag; + +/* stop buffer allocations */ +spin_lock_irqsave (&sc->alloc_lock, flags); + sc->flags &= ~SCF_ENABLED ; +spin_unlock_irqrestore (&sc->alloc_lock, flags); +wake_up (&sc->halt_wait); +} + +#define BLOCK_DWORDS (PIO_BLOCK_SIZE/sizeof(u32)) +#define dwords_to_blocks (x) DIV_ROUND_UP(x, BLOCK_DWORDS) + +/* +* The send context buffer "allocator". +* +* @sc: the PIO send context we are allocating from +* @len: length of whole packet - including PBC - in dwords +* @cb: optional callback to call when the buffer is finished sending +* @arg: argument for cb +* +* Return a pointer to a PIO buffer if successful, NULL if not enough room. +*/ +struct pio_buf *sc_buffer_alloc (struct send_context *sc,u32 dw_len, +pio_release_cb cb,void *arg) +{ +struct pio_buf *pbuf =NULL ; +unsigned long flags; +unsigned long avail; +unsigned long blocks =dwords_to_blocks (dw_len); +unsigned long start_fill; +int trycount =0 ; +u32 head, next; + +spin_lock_irqsave (&sc->alloc_lock, flags); +if (!(sc->flags &SCF_ENABLED )) { +spin_unlock_irqrestore (&sc->alloc_lock, flags); + goto done; + } + +retry: + avail = (unsigned long )sc->credits - (sc->fill - sc->alloc_free); +if (blocks > avail) { +/* not enough room */ +if (unlikely (trycount)) {/* already tried to get more room */ +spin_unlock_irqrestore (&sc->alloc_lock, flags); + goto done; + } +/* copy from receiver cache line and recalculate */ + sc->alloc_free =ACCESS_ONCE (sc->free); + avail = + (unsigned long )sc->credits - + (sc->fill - sc->alloc_free); +if (blocks > avail) { +/* still no room, actively update */ +spin_unlock_irqrestore (&sc->alloc_lock, flags); +sc_release_update (sc); +spin_lock_irqsave (&sc->alloc_lock, flags); + sc->alloc_free =ACCESS_ONCE (sc->free); + trycount++; + goto retry; + } + } + +/* there is enough room */ + +atomic_inc (&sc->buffers_allocated); + +/* read this once */ + head = sc->sr_head; + +/* "allocate" the buffer */ + start_fill = sc->fill; + sc->fill += blocks; + +/* +* Fill the parts that the releaser looks at before moving the head. +* The only necessary piece is the sent_at field. The credits +* we have just allocated cannot have been returned yet, so the +* cb and arg will not be looked at for a "while". Put them +* on this side of the memory barrier anyway. +*/ + pbuf = &sc->sr[head].pbuf; + pbuf->sent_at = sc->fill; + pbuf->cb = cb; + pbuf->arg = arg; + pbuf->sc = sc;/* could be filled in at sc->sr init time */ +/* make sure this is in memory before updating the head */ + +/* calculate next head index, do not store */ + next = head +1 ; +if (next >= sc->sr_size) + next =0 ; +/* update the head - must be last! - the releaser can look at fields +in pbuf once we move the head */ +smp_wmb (); + sc->sr_head = next; +spin_unlock_irqrestore (&sc->alloc_lock, flags); + +/* finish filling in the buffer outside the lock */ + pbuf->start = sc->base_addr + ((start_fill % sc->credits) + *PIO_BLOCK_SIZE ); + pbuf->size = sc->credits *PIO_BLOCK_SIZE ; + pbuf->end = sc->base_addr + pbuf->size; + pbuf->block_count = blocks; + pbuf->qw_written =0 ; + pbuf->carry_bytes =0 ; + pbuf->carry.val64 =0 ; +done: +return pbuf; +} + +/* +* There are at least two entities that can turn on credit return +* interrupts and they can overlap. Avoid problems by implementing +* a count scheme that is enforced by a lock. The lock is needed because +* the count and CSR write must be paired. +*/ + +/* +* Start credit return interrupts. This is managed by a count. If already +* on, just increment the count. +*/ +void sc_add_credit_return_intr (struct send_context *sc) +{ +unsigned long flags; + +/* lock must surround both the count change and the CSR update */ +spin_lock_irqsave (&sc->credit_ctrl_lock, flags); +if (sc->credit_intr_count ==0 ) { + sc->credit_ctrl |=SC (CREDIT_CTRL_CREDIT_INTR_SMASK ); +write_kctxt_csr (sc->dd, sc->hw_context, +SC (CREDIT_CTRL ), sc->credit_ctrl); + } + sc->credit_intr_count++; +spin_unlock_irqrestore (&sc->credit_ctrl_lock, flags); +} + +/* +* Stop credit return interrupts. This is managed by a count. Decrement the +* count, if the last user, then turn the credit interrupts off. +*/ +void sc_del_credit_return_intr (struct send_context *sc) +{ +unsigned long flags; + +BUG_ON (sc->credit_intr_count ==0 ); + +/* lock must surround both the count change and the CSR update */ +spin_lock_irqsave (&sc->credit_ctrl_lock, flags); + sc->credit_intr_count--; +if (sc->credit_intr_count ==0 ) { + sc->credit_ctrl &= ~SC (CREDIT_CTRL_CREDIT_INTR_SMASK ); +write_kctxt_csr (sc->dd, sc->hw_context, +SC (CREDIT_CTRL ), sc->credit_ctrl); + } +spin_unlock_irqrestore (&sc->credit_ctrl_lock, flags); +} + +/* +* The caller must be careful when calling this. All needint calls +* must be paired with !needint. +*/ +void hfi1_sc_wantpiobuf_intr (struct send_context *sc,u32 needint) +{ +if (needint) +sc_add_credit_return_intr (sc); +else +sc_del_credit_return_intr (sc); +trace_hfi1_wantpiointr (sc, needint, sc->credit_ctrl); +if (needint) { +mmiowb (); +sc_return_credits (sc); + } +} + +/** +* sc_piobufavail - callback when a PIO buffer is available +* @sc: the send context +* +* This is called from the interrupt handler when a PIO buffer is +* available after hfi1_verbs_send() returned an error that no buffers were +* available. Disable the interrupt if there are no more QPs waiting. +*/ +static void sc_piobufavail (struct send_context *sc) +{ +struct hfi1_devdata *dd = sc->dd; +struct hfi1_ibdev *dev = &dd->verbs_dev; +struct list_head *list; +struct hfi1_qp *qps[PIO_WAIT_BATCH_SIZE ]; +struct hfi1_qp *qp; +unsigned long flags; +unsigned i, n =0 ; + +if (dd->send_contexts[sc->sw_index].type !=SC_KERNEL ) +return ; + list = &sc->piowait; +/* +* Note: checking that the piowait list is empty and clearing +* the buffer available interrupt needs to be atomic or we +* could end up with QPs on the wait list with the interrupt +* disabled. +*/ +spin_lock_irqsave (&dev->pending_lock, flags); +while (!list_empty (list)) { +struct iowait *wait; + +if (n ==ARRAY_SIZE (qps)) + goto full; + wait = list_first_entry(list, struct iowait, list); + qp = container_of(wait, struct hfi1_qp, s_iowait); +list_del_init (&qp->s_iowait.list); +/* refcount held until actual wake up */ + qps[n++] = qp; + } +/* +* Counting: only call wantpiobuf_intr() if there were waiters and they +* are now all gone. +*/ +if (n) +hfi1_sc_wantpiobuf_intr (sc,0 ); +full: +spin_unlock_irqrestore (&dev->pending_lock, flags); + +for (i =0 ; i < n; i++) +hfi1_qp_wakeup (qps[i],HFI1_S_WAIT_PIO ); +} + +/* translate a send credit update to a bit code of reasons */ +static inline int fill_code (u64 hw_free) +{ +int code =0 ; + +if (hw_free &CR_STATUS_SMASK ) + code |=PRC_STATUS_ERR ; +if (hw_free &CR_CREDIT_RETURN_DUE_TO_PBC_SMASK ) + code |=PRC_PBC ; +if (hw_free &CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK ) + code |=PRC_THRESHOLD ; +if (hw_free &CR_CREDIT_RETURN_DUE_TO_ERR_SMASK ) + code |=PRC_FILL_ERR ; +if (hw_free &CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK ) + code |=PRC_SC_DISABLE ; +return code; +} + +/* use the jiffies compare to get the wrap right */ +#define sent_before (a, b) time_before(a, b)/* a < b */ + +/* +* The send context buffer "releaser". +*/ +void sc_release_update (struct send_context *sc) +{ +struct pio_buf *pbuf; +u64 hw_free; +u32 head, tail; +unsigned long old_free; +unsigned long extra; +unsigned long flags; +int code; + +if (!sc) +return ; + +spin_lock_irqsave (&sc->release_lock, flags); +/* update free */ + hw_free =le64_to_cpu (*sc->hw_free);/* volatile read */ + old_free = sc->free; + extra = (((hw_free &CR_COUNTER_SMASK ) >>CR_COUNTER_SHIFT ) + - (old_free &CR_COUNTER_MASK )) + &CR_COUNTER_MASK ; + sc->free = old_free + extra; +trace_hfi1_piofree (sc, extra); + +/* call sent buffer callbacks */ + code =-1 ;/* code not yet set */ + head =ACCESS_ONCE (sc->sr_head);/* snapshot the head */ + tail = sc->sr_tail; +while (head != tail) { + pbuf = &sc->sr[tail].pbuf; + +if (sent_before (sc->free, pbuf->sent_at)) { +/* not sent yet */ +break ; + } +if (pbuf->cb) { +if (code <0 )/* fill in code on first user */ + code =fill_code (hw_free); + (*pbuf->cb)(pbuf->arg, code); + } + + tail++; +if (tail >= sc->sr_size) + tail =0 ; + } +/* update tail, in case we moved it */ + sc->sr_tail = tail; +spin_unlock_irqrestore (&sc->release_lock, flags); +sc_piobufavail (sc); +} + +/* +* Send context group releaser. Argument is the send context that caused +* the interrupt. Called from the send context interrupt handler. +* +* Call release on all contexts in the group. +* +* This routine takes the sc_lock without an irqsave because it is only +* called from an interrupt handler. Adjust if that changes. +*/ +void sc_group_release_update (struct hfi1_devdata *dd,u32 hw_context) +{ +struct send_context *sc; +u32 sw_index; +u32 gc, gc_end; + +spin_lock (&dd->sc_lock); + sw_index = dd->hw_to_sw[hw_context]; +if (unlikely (sw_index >= dd->num_send_contexts)) { +dd_dev_err (dd,"%s: invalid hw (%u) to sw (%u) mapping\n" , + __func__, hw_context, sw_index); + goto done; + } + sc = dd->send_contexts[sw_index].sc; +if (unlikely (!sc)) + goto done; + + gc =group_context (hw_context, sc->group); + gc_end = gc +group_size (sc->group); +for (; gc < gc_end; gc++) { + sw_index = dd->hw_to_sw[gc]; +if (unlikely (sw_index >= dd->num_send_contexts)) { +dd_dev_err (dd, +"%s: invalid hw (%u) to sw (%u) mapping\n" , + __func__, hw_context, sw_index); +continue ; + } +sc_release_update (dd->send_contexts[sw_index].sc); + } +done: +spin_unlock (&dd->sc_lock); +} + +int init_pervl_scs (struct hfi1_devdata *dd) +{ +int i; +u64 mask, all_vl_mask = (u64 )0x80ff ;/* VLs 0-7, 15 */ +u32 ctxt; + + dd->vld[15 ].sc =sc_alloc (dd,SC_KERNEL , + dd->rcd[0 ]->rcvhdrqentsize, dd->node); +if (!dd->vld[15 ].sc) + goto nomem; +hfi1_init_ctxt (dd->vld[15 ].sc); + dd->vld[15 ].mtu =enum_to_mtu (OPA_MTU_2048 ); +for (i =0 ; i < num_vls; i++) { +/* +* Since this function does not deal with a specific +* receive context but we need the RcvHdrQ entry size, +* use the size from rcd[0]. It is guaranteed to be +* valid at this point and will remain the same for all +* receive contexts. +*/ + dd->vld[i].sc =sc_alloc (dd,SC_KERNEL , + dd->rcd[0 ]->rcvhdrqentsize, dd->node); +if (!dd->vld[i].sc) + goto nomem; + +hfi1_init_ctxt (dd->vld[i].sc); + +/* non VL15 start with the max MTU */ + dd->vld[i].mtu = hfi1_max_mtu; + } +sc_enable (dd->vld[15 ].sc); + ctxt = dd->vld[15 ].sc->hw_context; + mask = all_vl_mask & ~(1LL <<15 ); +write_kctxt_csr (dd, ctxt,SC (CHECK_VL ), mask); +dd_dev_info (dd, +"Using send context %u(%u) for VL15\n" , + dd->vld[15 ].sc->sw_index, ctxt); +for (i =0 ; i < num_vls; i++) { +sc_enable (dd->vld[i].sc); + ctxt = dd->vld[i].sc->hw_context; + mask = all_vl_mask & ~(1LL << i); +write_kctxt_csr (dd, ctxt,SC (CHECK_VL ), mask); + } +return 0 ; +nomem: +sc_free (dd->vld[15 ].sc); +for (i =0 ; i < num_vls; i++) +sc_free (dd->vld[i].sc); +return -ENOMEM ; +} + +int init_credit_return (struct hfi1_devdata *dd) +{ +int ret; +int num_numa; +int i; + + num_numa =num_online_nodes (); +/* enforce the expectation that the numas are compact */ +for (i =0 ; i < num_numa; i++) { +if (!node_online (i)) { +dd_dev_err (dd,"NUMA nodes are not compact\n" ); + ret = -EINVAL ; + goto done; + } + } + + dd->cr_base =kcalloc ( + num_numa, +sizeof (struct credit_return_base ), +GFP_KERNEL ); +if (!dd->cr_base) { +dd_dev_err (dd,"Unable to allocate credit return base\n" ); + ret = -ENOMEM ; + goto done; + } +for (i =0 ; i < num_numa; i++) { +int bytes =TXE_NUM_CONTEXTS *sizeof (struct credit_return ); + +set_dev_node (&dd->pcidev->dev, i); + dd->cr_base[i].va =dma_zalloc_coherent ( + &dd->pcidev->dev, + bytes, + &dd->cr_base[i].pa, +GFP_KERNEL ); +if (dd->cr_base[i].va ==NULL ) { +set_dev_node (&dd->pcidev->dev, dd->node); +dd_dev_err (dd, +"Unable to allocate credit return DMA range for NUMA %d\n" , + i); + ret = -ENOMEM ; + goto done; + } + } +set_dev_node (&dd->pcidev->dev, dd->node); + + ret =0 ; +done: +return ret; +} + +void free_credit_return (struct hfi1_devdata *dd) +{ +int num_numa; +int i; + +if (!dd->cr_base) +return ; + + num_numa =num_online_nodes (); +for (i =0 ; i < num_numa; i++) { +if (dd->cr_base[i].va) { +dma_free_coherent (&dd->pcidev->dev, +TXE_NUM_CONTEXTS + *sizeof (struct credit_return ), + dd->cr_base[i].va, + dd->cr_base[i].pa); + } + } +kfree (dd->cr_base); + dd->cr_base =NULL ; +}
drivers/infiniband/hw/hfi1/pio.h new file +224 -0
224 lines not on this page · raw message
drivers/infiniband/hw/hfi1/pio_copy.c new file +867 -0
867 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 24/41 IB/hfi1: add platform config definitions
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/platform_config.h | 286 ++++++++++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/platform_config.h
drivers/infiniband/hw/hfi1/platform_config.h new file +286 -0
@@ -0,0 +1,286 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ +#ifndef __PLATFORM_CONFIG_H +#define __PLATFORM_CONFIG_H + +#define METADATA_TABLE_FIELD_START_SHIFT 0 +#define METADATA_TABLE_FIELD_START_LEN_BITS 15 +#define METADATA_TABLE_FIELD_LEN_SHIFT 16 +#define METADATA_TABLE_FIELD_LEN_LEN_BITS 16 + +/* Header structure */ +#define PLATFORM_CONFIG_HEADER_RECORD_IDX_SHIFT 0 +#define PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS 6 +#define PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT 16 +#define PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS 12 +#define PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT 28 +#define PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS 4 + +enum platform_config_table_type_encoding { +PLATFORM_CONFIG_TABLE_RESERVED , +PLATFORM_CONFIG_SYSTEM_TABLE , +PLATFORM_CONFIG_PORT_TABLE , +PLATFORM_CONFIG_RX_PRESET_TABLE , +PLATFORM_CONFIG_TX_PRESET_TABLE , +PLATFORM_CONFIG_QSFP_ATTEN_TABLE , +PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE , +PLATFORM_CONFIG_TABLE_MAX +}; + +enum platform_config_system_table_fields { +SYSTEM_TABLE_RESERVED , +SYSTEM_TABLE_NODE_STRING , +SYSTEM_TABLE_SYSTEM_IMAGE_GUID , +SYSTEM_TABLE_NODE_GUID , +SYSTEM_TABLE_REVISION , +SYSTEM_TABLE_VENDOR_OUI , +SYSTEM_TABLE_META_VERSION , +SYSTEM_TABLE_DEVICE_ID , +SYSTEM_TABLE_PARTITION_ENFORCEMENT_CAP , +SYSTEM_TABLE_QSFP_POWER_CLASS_MAX , +SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_12G , +SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_25G , +SYSTEM_TABLE_VARIABLE_TABLE_ENTRIES_PER_PORT , +SYSTEM_TABLE_MAX +}; + +enum platform_config_port_table_fields { +PORT_TABLE_RESERVED , +PORT_TABLE_PORT_TYPE , +PORT_TABLE_ATTENUATION_12G , +PORT_TABLE_ATTENUATION_25G , +PORT_TABLE_LINK_SPEED_SUPPORTED , +PORT_TABLE_LINK_WIDTH_SUPPORTED , +PORT_TABLE_VL_CAP , +PORT_TABLE_MTU_CAP , +PORT_TABLE_TX_LANE_ENABLE_MASK , +PORT_TABLE_LOCAL_MAX_TIMEOUT , +PORT_TABLE_AUTO_LANE_SHEDDING_ENABLED , +PORT_TABLE_EXTERNAL_LOOPBACK_ALLOWED , +PORT_TABLE_TX_PRESET_IDX_PASSIVE_CU , +PORT_TABLE_TX_PRESET_IDX_ACTIVE_NO_EQ , +PORT_TABLE_TX_PRESET_IDX_ACTIVE_EQ , +PORT_TABLE_RX_PRESET_IDX , +PORT_TABLE_CABLE_REACH_CLASS , +PORT_TABLE_MAX +}; + +enum platform_config_rx_preset_table_fields { +RX_PRESET_TABLE_RESERVED , +RX_PRESET_TABLE_QSFP_RX_CDR_APPLY , +RX_PRESET_TABLE_QSFP_RX_EQ_APPLY , +RX_PRESET_TABLE_QSFP_RX_AMP_APPLY , +RX_PRESET_TABLE_QSFP_RX_CDR , +RX_PRESET_TABLE_QSFP_RX_EQ , +RX_PRESET_TABLE_QSFP_RX_AMP , +RX_PRESET_TABLE_MAX +}; + +enum platform_config_tx_preset_table_fields { +TX_PRESET_TABLE_RESERVED , +TX_PRESET_TABLE_PRECUR , +TX_PRESET_TABLE_ATTN , +TX_PRESET_TABLE_POSTCUR , +TX_PRESET_TABLE_QSFP_TX_CDR_APPLY , +TX_PRESET_TABLE_QSFP_TX_EQ_APPLY , +TX_PRESET_TABLE_QSFP_TX_CDR , +TX_PRESET_TABLE_QSFP_TX_EQ , +TX_PRESET_TABLE_MAX +}; + +enum platform_config_qsfp_attn_table_fields { +QSFP_ATTEN_TABLE_RESERVED , +QSFP_ATTEN_TABLE_TX_PRESET_IDX , +QSFP_ATTEN_TABLE_RX_PRESET_IDX , +QSFP_ATTEN_TABLE_MAX +}; + +enum platform_config_variable_settings_table_fields { +VARIABLE_SETTINGS_TABLE_RESERVED , +VARIABLE_SETTINGS_TABLE_TX_PRESET_IDX , +VARIABLE_SETTINGS_TABLE_RX_PRESET_IDX , +VARIABLE_SETTINGS_TABLE_MAX +}; + +struct platform_config_data { +u32 *table; +u32 *table_metadata; +u32 num_table; +}; + +/* +* This struct acts as a quick reference into the platform_data binary image +* and is populated by parse_platform_config(...) depending on the specific +* META_VERSION +*/ +struct platform_config_cache { +u8 cache_valid; +struct platform_config_data config_tables[PLATFORM_CONFIG_TABLE_MAX ]; +}; + +static const u32 platform_config_table_limits[PLATFORM_CONFIG_TABLE_MAX ] = { +0 , +SYSTEM_TABLE_MAX , +PORT_TABLE_MAX , +RX_PRESET_TABLE_MAX , +TX_PRESET_TABLE_MAX , +QSFP_ATTEN_TABLE_MAX , +VARIABLE_SETTINGS_TABLE_MAX +}; + +/* This section defines default values and encodings for the +* fields defined for each table above +*/ + +/*===================================================== +* System table encodings +*====================================================*/ +#define PLATFORM_CONFIG_MAGIC_NUM 0x3d4f5041 +#define PLATFORM_CONFIG_MAGIC_NUMBER_LEN 4 + +/* +* These power classes are the same as defined in SFF 8636 spec rev 2.4 +* describing byte 129 in table 6-16, except enumerated in a different order +*/ +enum platform_config_qsfp_power_class_encoding { +QSFP_POWER_CLASS_1 =1 , +QSFP_POWER_CLASS_2 , +QSFP_POWER_CLASS_3 , +QSFP_POWER_CLASS_4 , +QSFP_POWER_CLASS_5 , +QSFP_POWER_CLASS_6 , +QSFP_POWER_CLASS_7 +}; + + +/*===================================================== +* Port table encodings +*==================================================== */ +enum platform_config_port_type_encoding { +PORT_TYPE_RESERVED , +PORT_TYPE_DISCONNECTED , +PORT_TYPE_FIXED , +PORT_TYPE_VARIABLE , +PORT_TYPE_QSFP , +PORT_TYPE_MAX +}; + +enum platform_config_link_speed_supported_encoding { +LINK_SPEED_SUPP_12G =1 , +LINK_SPEED_SUPP_25G , +LINK_SPEED_SUPP_12G_25G , +LINK_SPEED_SUPP_MAX +}; + +/* +* This is a subset (not strict) of the link downgrades +* supported. The link downgrades supported are expected +* to be supplied to the driver by another entity such as +* the fabric manager +*/ +enum platform_config_link_width_supported_encoding { +LINK_WIDTH_SUPP_1X =1 , +LINK_WIDTH_SUPP_2X , +LINK_WIDTH_SUPP_2X_1X , +LINK_WIDTH_SUPP_3X , +LINK_WIDTH_SUPP_3X_1X , +LINK_WIDTH_SUPP_3X_2X , +LINK_WIDTH_SUPP_3X_2X_1X , +LINK_WIDTH_SUPP_4X , +LINK_WIDTH_SUPP_4X_1X , +LINK_WIDTH_SUPP_4X_2X , +LINK_WIDTH_SUPP_4X_2X_1X , +LINK_WIDTH_SUPP_4X_3X , +LINK_WIDTH_SUPP_4X_3X_1X , +LINK_WIDTH_SUPP_4X_3X_2X , +LINK_WIDTH_SUPP_4X_3X_2X_1X , +LINK_WIDTH_SUPP_MAX +}; + +enum platform_config_virtual_lane_capability_encoding { +VL_CAP_VL0 =1 , +VL_CAP_VL0_1 , +VL_CAP_VL0_2 , +VL_CAP_VL0_3 , +VL_CAP_VL0_4 , +VL_CAP_VL0_5 , +VL_CAP_VL0_6 , +VL_CAP_VL0_7 , +VL_CAP_VL0_8 , +VL_CAP_VL0_9 , +VL_CAP_VL0_10 , +VL_CAP_VL0_11 , +VL_CAP_VL0_12 , +VL_CAP_VL0_13 , +VL_CAP_VL0_14 , +VL_CAP_MAX +}; + +/* Max MTU */ +enum platform_config_mtu_capability_encoding { +MTU_CAP_256 =1 , +MTU_CAP_512 =2 , +MTU_CAP_1024 =3 , +MTU_CAP_2048 =4 , +MTU_CAP_4096 =5 , +MTU_CAP_8192 =6 , +MTU_CAP_10240 =7 +}; + +enum platform_config_local_max_timeout_encoding { +LOCAL_MAX_TIMEOUT_10_MS =1 , +LOCAL_MAX_TIMEOUT_100_MS , +LOCAL_MAX_TIMEOUT_1_S , +LOCAL_MAX_TIMEOUT_10_S , +LOCAL_MAX_TIMEOUT_100_S , +LOCAL_MAX_TIMEOUT_1000_S +}; + +#endif /*__PLATFORM_CONFIG_H*/
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 25/41 IB/hfi1: add qp handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/qp.c | 1692 +++++++++++++++++++++++++++++++++++++++ drivers/infiniband/hw/hfi1/qp.h | 206 +++++ 2 files changed, 1898 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/qp.c create mode 100644 drivers/infiniband/hw/hfi1/qp.h
drivers/infiniband/hw/hfi1/qp.c new file +1692 -0
@@ -0,0 +1,1692 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/err.h> +#include <linux/vmalloc.h> +#include <linux/jhash.h> +#include <linux/module.h> +#include <linux/random.h> +#include <linux/seq_file.h> + +#include "hfi.h" +#include "qp.h" +#include "trace.h" +#include "sdma.h" + +#define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE) +#define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1) + +static unsigned int hfi1_qp_table_size =256 ; +module_param_named (qp_table_size, hfi1_qp_table_size, uint,S_IRUGO ); +MODULE_PARM_DESC (qp_table_size,"QP table size" ); + +static void flush_tx_list (struct hfi1_qp *qp); +static int iowait_sleep ( +struct sdma_engine *sde, +struct iowait *wait, +struct sdma_txreq *stx, +unsigned seq); +static void iowait_wakeup (struct iowait *wait,int reason); + +static inline unsigned mk_qpn (struct hfi1_qpn_table *qpt, +struct qpn_map *map,unsigned off) +{ +return (map - qpt->map) *BITS_PER_PAGE + off; +} + +/* +* Convert the AETH credit code into the number of credits. +*/ +static u32 credit_table[31 ] = { +0 ,/* 0 */ +1 ,/* 1 */ +2 ,/* 2 */ +3 ,/* 3 */ +4 ,/* 4 */ +6 ,/* 5 */ +8 ,/* 6 */ +12 ,/* 7 */ +16 ,/* 8 */ +24 ,/* 9 */ +32 ,/* A */ +48 ,/* B */ +64 ,/* C */ +96 ,/* D */ +128 ,/* E */ +192 ,/* F */ +256 ,/* 10 */ +384 ,/* 11 */ +512 ,/* 12 */ +768 ,/* 13 */ +1024 ,/* 14 */ +1536 ,/* 15 */ +2048 ,/* 16 */ +3072 ,/* 17 */ +4096 ,/* 18 */ +6144 ,/* 19 */ +8192 ,/* 1A */ +12288 ,/* 1B */ +16384 ,/* 1C */ +24576 ,/* 1D */ +32768 /* 1E */ +}; + +static void get_map_page (struct hfi1_qpn_table *qpt,struct qpn_map *map) +{ +unsigned long page =get_zeroed_page (GFP_KERNEL ); + +/* +* Free the page if someone raced with us installing it. +*/ + +spin_lock (&qpt->lock); +if (map->page) +free_page (page); +else + map->page = (void *)page; +spin_unlock (&qpt->lock); +} + +/* +* Allocate the next available QPN or +* zero/one for QP type IB_QPT_SMI/IB_QPT_GSI. +*/ +static int alloc_qpn (struct hfi1_devdata *dd,struct hfi1_qpn_table *qpt, +enum ib_qp_type type,u8 port) +{ +u32 i, offset, max_scan, qpn; +struct qpn_map *map; +u32 ret; + +if (type ==IB_QPT_SMI || type ==IB_QPT_GSI ) { +unsigned n; + + ret = type ==IB_QPT_GSI ; + n =1 << (ret +2 * (port -1 )); +spin_lock (&qpt->lock); +if (qpt->flags & n) + ret = -EINVAL ; +else + qpt->flags |= n; +spin_unlock (&qpt->lock); + goto bail; + } + + qpn = qpt->last + qpt->incr; +if (qpn >=QPN_MAX ) + qpn = qpt->incr | ((qpt->last &1 ) ^1 ); +/* offset carries bit 0 */ + offset = qpn &BITS_PER_PAGE_MASK ; + map = &qpt->map[qpn /BITS_PER_PAGE ]; + max_scan = qpt->nmaps - !offset; +for (i =0 ;;) { +if (unlikely (!map->page)) { +get_map_page (qpt, map); +if (unlikely (!map->page)) +break ; + } +do { +if (!test_and_set_bit (offset, map->page)) { + qpt->last = qpn; + ret = qpn; + goto bail; + } + offset += qpt->incr; +/* +* This qpn might be bogus if offset >= BITS_PER_PAGE. +* That is OK. It gets re-assigned below +*/ + qpn =mk_qpn (qpt, map, offset); + }while (offset <BITS_PER_PAGE && qpn <QPN_MAX ); +/* +* In order to keep the number of pages allocated to a +* minimum, we scan the all existing pages before increasing +* the size of the bitmap table. +*/ +if (++i > max_scan) { +if (qpt->nmaps ==QPNMAP_ENTRIES ) +break ; + map = &qpt->map[qpt->nmaps++]; +/* start at incr with current bit 0 */ + offset = qpt->incr | (offset &1 ); + }else if (map < &qpt->map[qpt->nmaps]) { + ++map; +/* start at incr with current bit 0 */ + offset = qpt->incr | (offset &1 ); + }else { + map = &qpt->map[0 ]; +/* wrap to first map page, invert bit 0 */ + offset = qpt->incr | ((offset &1 ) ^1 ); + } +/* there can be no bits at shift and below */ +BUG_ON (offset & (dd->qos_shift -1 )); + qpn =mk_qpn (qpt, map, offset); + } + + ret = -ENOMEM ; + +bail: +return ret; +} + +static void free_qpn (struct hfi1_qpn_table *qpt,u32 qpn) +{ +struct qpn_map *map; + + map = qpt->map + qpn /BITS_PER_PAGE ; +if (map->page) +clear_bit (qpn &BITS_PER_PAGE_MASK , map->page); +} + +static inline unsigned qpn_hash (struct hfi1_qp_ibdev *dev,u32 qpn) +{ +return jhash_1word (qpn, dev->qp_rnd) & + (dev->qp_table_size -1 ); +} + + +/* +* Put the QP into the hash table. +* The hash table holds a reference to the QP. +*/ +static void insert_qp (struct hfi1_ibdev *dev,struct hfi1_qp *qp) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +unsigned long flags; +unsigned n =qpn_hash (dev->qp_dev, qp->ibqp.qp_num); + +atomic_inc (&qp->refcount); +spin_lock_irqsave (&dev->qp_dev->qpt_lock, flags); + +if (qp->ibqp.qp_num ==0 ) +rcu_assign_pointer (ibp->qp0, qp); +else if (qp->ibqp.qp_num ==1 ) +rcu_assign_pointer (ibp->qp1, qp); +else { + qp->next = dev->qp_dev->qp_table[n]; +rcu_assign_pointer (dev->qp_dev->qp_table[n], qp); + } + +spin_unlock_irqrestore (&dev->qp_dev->qpt_lock, flags); +} + +/* +* Remove the QP from the table so it can't be found asynchronously by +* the receive interrupt routine. +*/ +static void remove_qp (struct hfi1_ibdev *dev,struct hfi1_qp *qp) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +unsigned n =qpn_hash (dev->qp_dev, qp->ibqp.qp_num); +unsigned long flags; +int removed =1 ; + +spin_lock_irqsave (&dev->qp_dev->qpt_lock, flags); + +if (rcu_dereference_protected (ibp->qp0, +lockdep_is_held (&dev->qp_dev->qpt_lock)) == qp) { +RCU_INIT_POINTER (ibp->qp0,NULL ); + }else if (rcu_dereference_protected (ibp->qp1, +lockdep_is_held (&dev->qp_dev->qpt_lock)) == qp) { +RCU_INIT_POINTER (ibp->qp1,NULL ); + }else { +struct hfi1_qp *q; +struct hfi1_qp __rcu **qpp; + + removed =0 ; + qpp = &dev->qp_dev->qp_table[n]; +for (; (q =rcu_dereference_protected (*qpp, +lockdep_is_held (&dev->qp_dev->qpt_lock))) + !=NULL ; + qpp = &q->next) +if (q == qp) { +RCU_INIT_POINTER (*qpp, +rcu_dereference_protected (qp->next, +lockdep_is_held (&dev->qp_dev->qpt_lock))); + removed =1 ; +break ; + } + } + +spin_unlock_irqrestore (&dev->qp_dev->qpt_lock, flags); +if (removed) { +synchronize_rcu (); +if (atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); + } +} + +/** +* free_all_qps - check for QPs still in use +* @qpt: the QP table to empty +* +* There should not be any QPs still in use. +* Free memory for table. +*/ +static unsigned free_all_qps (struct hfi1_devdata *dd) +{ +struct hfi1_ibdev *dev = &dd->verbs_dev; +unsigned long flags; +struct hfi1_qp *qp; +unsigned n, qp_inuse =0 ; + +for (n =0 ; n < dd->num_pports; n++) { +struct hfi1_ibport *ibp = &dd->pport[n].ibport_data; + +if (!hfi1_mcast_tree_empty (ibp)) + qp_inuse++; +rcu_read_lock (); +if (rcu_dereference (ibp->qp0)) + qp_inuse++; +if (rcu_dereference (ibp->qp1)) + qp_inuse++; +rcu_read_unlock (); + } + +if (!dev->qp_dev) + goto bail; +spin_lock_irqsave (&dev->qp_dev->qpt_lock, flags); +for (n =0 ; n < dev->qp_dev->qp_table_size; n++) { + qp =rcu_dereference_protected (dev->qp_dev->qp_table[n], +lockdep_is_held (&dev->qp_dev->qpt_lock)); +RCU_INIT_POINTER (dev->qp_dev->qp_table[n],NULL ); + +for (; qp; qp =rcu_dereference_protected (qp->next, +lockdep_is_held (&dev->qp_dev->qpt_lock))) + qp_inuse++; + } +spin_unlock_irqrestore (&dev->qp_dev->qpt_lock, flags); +synchronize_rcu (); +bail: +return qp_inuse; +} + +/** +* hfi1_lookup_qpn - return the QP with the given QPN +* @qpt: the QP table +* @qpn: the QP number to look up +* +* The caller is responsible for decrementing the QP reference count +* when done. +*/ +struct hfi1_qp *hfi1_lookup_qpn (struct hfi1_ibport *ibp,u32 qpn) +{ +struct hfi1_qp *qp =NULL ; + +rcu_read_lock (); +if (unlikely (qpn <=1 )) { +if (qpn ==0 ) + qp =rcu_dereference (ibp->qp0); +else + qp =rcu_dereference (ibp->qp1); +if (qp) +atomic_inc (&qp->refcount); + }else { +struct hfi1_ibdev *dev = &ppd_from_ibp (ibp)->dd->verbs_dev; +unsigned n =qpn_hash (dev->qp_dev, qpn); + +for (qp =rcu_dereference (dev->qp_dev->qp_table[n]); qp; + qp =rcu_dereference (qp->next)) +if (qp->ibqp.qp_num == qpn) { +atomic_inc (&qp->refcount); +break ; + } + } +rcu_read_unlock (); +return qp; +} + +/** +* reset_qp - initialize the QP state to the reset state +* @qp: the QP to reset +* @type: the QP type +*/ +static void reset_qp (struct hfi1_qp *qp,enum ib_qp_type type) +{ + qp->remote_qpn =0 ; + qp->qkey =0 ; + qp->qp_access_flags =0 ; +iowait_init ( + &qp->s_iowait, +1 , + hfi1_do_send, + iowait_sleep, + iowait_wakeup); + qp->s_flags &=HFI1_S_SIGNAL_REQ_WR ; + qp->s_hdrwords =0 ; + qp->s_wqe =NULL ; + qp->s_draining =0 ; + qp->s_next_psn =0 ; + qp->s_last_psn =0 ; + qp->s_sending_psn =0 ; + qp->s_sending_hpsn =0 ; + qp->s_psn =0 ; + qp->r_psn =0 ; + qp->r_msn =0 ; +if (type ==IB_QPT_RC ) { + qp->s_state =IB_OPCODE_RC_SEND_LAST ; + qp->r_state =IB_OPCODE_RC_SEND_LAST ; + }else { + qp->s_state =IB_OPCODE_UC_SEND_LAST ; + qp->r_state =IB_OPCODE_UC_SEND_LAST ; + } + qp->s_ack_state =IB_OPCODE_RC_ACKNOWLEDGE ; + qp->r_nak_state =0 ; + qp->r_aflags =0 ; + qp->r_flags =0 ; + qp->s_head =0 ; + qp->s_tail =0 ; + qp->s_cur =0 ; + qp->s_acked =0 ; + qp->s_last =0 ; + qp->s_ssn =1 ; + qp->s_lsn =0 ; +clear_ahg (qp); + qp->s_mig_state =IB_MIG_MIGRATED ; +memset (qp->s_ack_queue,0 ,sizeof (qp->s_ack_queue)); + qp->r_head_ack_queue =0 ; + qp->s_tail_ack_queue =0 ; + qp->s_num_rd_atomic =0 ; +if (qp->r_rq.wq) { + qp->r_rq.wq->head =0 ; + qp->r_rq.wq->tail =0 ; + } + qp->r_sge.num_sge =0 ; +} + +static void clear_mr_refs (struct hfi1_qp *qp,int clr_sends) +{ +unsigned n; + +if (test_and_clear_bit (HFI1_R_REWIND_SGE , &qp->r_aflags)) +hfi1_put_ss (&qp->s_rdma_read_sge); + +hfi1_put_ss (&qp->r_sge); + +if (clr_sends) { +while (qp->s_last != qp->s_head) { +struct hfi1_swqe *wqe =get_swqe_ptr (qp, qp->s_last); +unsigned i; + +for (i =0 ; i < wqe->wr.num_sge; i++) { +struct hfi1_sge *sge = &wqe->sg_list[i]; + +hfi1_put_mr (sge->mr); + } +if (qp->ibqp.qp_type ==IB_QPT_UD || + qp->ibqp.qp_type ==IB_QPT_SMI || + qp->ibqp.qp_type ==IB_QPT_GSI ) +atomic_dec (&to_iah (wqe->wr.wr.ud.ah)->refcount); +if (++qp->s_last >= qp->s_size) + qp->s_last =0 ; + } +if (qp->s_rdma_mr) { +hfi1_put_mr (qp->s_rdma_mr); + qp->s_rdma_mr =NULL ; + } + } + +if (qp->ibqp.qp_type !=IB_QPT_RC ) +return ; + +for (n =0 ; n <ARRAY_SIZE (qp->s_ack_queue); n++) { +struct hfi1_ack_entry *e = &qp->s_ack_queue[n]; + +if (e->opcode ==IB_OPCODE_RC_RDMA_READ_REQUEST && + e->rdma_sge.mr) { +hfi1_put_mr (e->rdma_sge.mr); + e->rdma_sge.mr =NULL ; + } + } +} + +/** +* hfi1_error_qp - put a QP into the error state +* @qp: the QP to put into the error state +* @err: the receive completion error to signal if a RWQE is active +* +* Flushes both send and receive work queues. +* Returns true if last WQE event should be generated. +* The QP r_lock and s_lock should be held and interrupts disabled. +* If we are already in error state, just return. +*/ +int hfi1_error_qp (struct hfi1_qp *qp,enum ib_wc_status err) +{ +struct hfi1_ibdev *dev =to_idev (qp->ibqp.device); +struct ib_wc wc; +int ret =0 ; + +if (qp->state ==IB_QPS_ERR || qp->state ==IB_QPS_RESET ) + goto bail; + + qp->state =IB_QPS_ERR ; + +if (qp->s_flags & (HFI1_S_TIMER |HFI1_S_WAIT_RNR )) { + qp->s_flags &= ~(HFI1_S_TIMER |HFI1_S_WAIT_RNR ); +del_timer (&qp->s_timer); + } + +if (qp->s_flags &HFI1_S_ANY_WAIT_SEND ) + qp->s_flags &= ~HFI1_S_ANY_WAIT_SEND ; + +spin_lock (&dev->pending_lock); +if (!list_empty (&qp->s_iowait.list) && !(qp->s_flags &HFI1_S_BUSY )) { + qp->s_flags &= ~HFI1_S_ANY_WAIT_IO ; +list_del_init (&qp->s_iowait.list); +if (atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); + } +spin_unlock (&dev->pending_lock); + +if (!(qp->s_flags &HFI1_S_BUSY )) { + qp->s_hdrwords =0 ; +if (qp->s_rdma_mr) { +hfi1_put_mr (qp->s_rdma_mr); + qp->s_rdma_mr =NULL ; + } +flush_tx_list (qp); + } + +/* Schedule the sending tasklet to drain the send work queue. */ +if (qp->s_last != qp->s_head) +hfi1_schedule_send (qp); + +clear_mr_refs (qp,0 ); + +memset (&wc,0 ,sizeof (wc)); + wc.qp = &qp->ibqp; + wc.opcode =IB_WC_RECV ; + +if (test_and_clear_bit (HFI1_R_WRID_VALID , &qp->r_aflags)) { + wc.wr_id = qp->r_wr_id; + wc.status = err; +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc,1 ); + } + wc.status =IB_WC_WR_FLUSH_ERR ; + +if (qp->r_rq.wq) { +struct hfi1_rwq *wq; +u32 head; +u32 tail; + +spin_lock (&qp->r_rq.lock); + +/* sanity check pointers before trusting them */ + wq = qp->r_rq.wq; + head = wq->head; +if (head >= qp->r_rq.size) + head =0 ; + tail = wq->tail; +if (tail >= qp->r_rq.size) + tail =0 ; +while (tail != head) { + wc.wr_id =get_rwqe_ptr (&qp->r_rq, tail)->wr_id; +if (++tail >= qp->r_rq.size) + tail =0 ; +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc,1 ); + } + wq->tail = tail; + +spin_unlock (&qp->r_rq.lock); + }else if (qp->ibqp.event_handler) + ret =1 ; + +bail: +return ret; +} + +static void flush_tx_list (struct hfi1_qp *qp) +{ +while (!list_empty (&qp->s_iowait.tx_head)) { +struct sdma_txreq *tx; + + tx =list_first_entry ( + &qp->s_iowait.tx_head, + struct sdma_txreq, + list); +list_del_init (&tx->list); +hfi1_put_txreq ( + container_of(tx ,struct verbs_txreq ,txreq )); + } +} + +static void flush_iowait (struct hfi1_qp *qp) +{ +struct hfi1_ibdev *dev =to_idev (qp->ibqp.device); +unsigned long flags; + +spin_lock_irqsave (&dev->pending_lock, flags); +if (!list_empty (&qp->s_iowait.list)) { +list_del_init (&qp->s_iowait.list); +if (atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); + } +spin_unlock_irqrestore (&dev->pending_lock, flags); +} + +static inline int opa_mtu_enum_to_int (int mtu) +{ +switch (mtu) { +case OPA_MTU_8192 :return 8192 ; +case OPA_MTU_10240 :return 10240 ; +default :return -1 ; + } +} + +/** +* This function is what we would push to the core layer if we wanted to be a +* "first class citizen". Instead we hide this here and rely on Verbs ULPs +* to blindly pass the MTU enum value from the PathRecord to us. +* +* The actual flag used to determine "8k MTU" will change and is currently +* unknown. +*/ +static inline int verbs_mtu_enum_to_int (struct ib_device *dev,enum ib_mtu mtu) +{ +int val =opa_mtu_enum_to_int ((int )mtu); + +if (val >0 ) +return val; +return ib_mtu_enum_to_int (mtu); +} + + +/** +* hfi1_modify_qp - modify the attributes of a queue pair +* @ibqp: the queue pair who's attributes we're modifying +* @attr: the new attributes +* @attr_mask: the mask of attributes to modify +* @udata: user data for libibverbs.so +* +* Returns 0 on success, otherwise returns an errno. +*/ +int hfi1_modify_qp (struct ib_qp *ibqp,struct ib_qp_attr *attr, +int attr_mask,struct ib_udata *udata) +{ +struct hfi1_ibdev *dev =to_idev (ibqp->device); +struct hfi1_qp *qp =to_iqp (ibqp); +enum ib_qp_state cur_state, new_state; +struct ib_event ev; +int lastwqe =0 ; +int mig =0 ; +int ret; +u32 pmtu =0 ;/* for gcc warning only */ +struct hfi1_devdata *dd; + +spin_lock_irq (&qp->r_lock); +spin_lock (&qp->s_lock); + + cur_state = attr_mask &IB_QP_CUR_STATE ? + attr->cur_qp_state : qp->state; + new_state = attr_mask &IB_QP_STATE ? attr->qp_state : cur_state; + +if (!ib_modify_qp_is_ok (cur_state, new_state, ibqp->qp_type, + attr_mask,IB_LINK_LAYER_UNSPECIFIED )) + goto inval; + +if (attr_mask &IB_QP_AV ) { +if (attr->ah_attr.dlid >=HFI1_MULTICAST_LID_BASE ) + goto inval; +if (hfi1_check_ah (qp->ibqp.device, &attr->ah_attr)) + goto inval; + } + +if (attr_mask &IB_QP_ALT_PATH ) { +if (attr->alt_ah_attr.dlid >=HFI1_MULTICAST_LID_BASE ) + goto inval; +if (hfi1_check_ah (qp->ibqp.device, &attr->alt_ah_attr)) + goto inval; +if (attr->alt_pkey_index >=hfi1_get_npkeys (dd_from_dev (dev))) + goto inval; + } + +if (attr_mask &IB_QP_PKEY_INDEX ) +if (attr->pkey_index >=hfi1_get_npkeys (dd_from_dev (dev))) + goto inval; + +if (attr_mask &IB_QP_MIN_RNR_TIMER ) +if (attr->min_rnr_timer >31 ) + goto inval; + +if (attr_mask &IB_QP_PORT ) +if (qp->ibqp.qp_type ==IB_QPT_SMI || + qp->ibqp.qp_type ==IB_QPT_GSI || + attr->port_num ==0 || + attr->port_num > ibqp->device->phys_port_cnt) + goto inval; + +if (attr_mask &IB_QP_DEST_QPN ) +if (attr->dest_qp_num >HFI1_QPN_MASK ) + goto inval; + +if (attr_mask &IB_QP_RETRY_CNT ) +if (attr->retry_cnt >7 ) + goto inval; + +if (attr_mask &IB_QP_RNR_RETRY ) +if (attr->rnr_retry >7 ) + goto inval; + +/* +* Don't allow invalid path_mtu values. OK to set greater +* than the active mtu (or even the max_cap, if we have tuned +* that to a small mtu. We'll set qp->path_mtu +* to the lesser of requested attribute mtu and active, +* for packetizing messages. +* Note that the QP port has to be set in INIT and MTU in RTR. +*/ +if (attr_mask &IB_QP_PATH_MTU ) { +int mtu, pidx = qp->port_num -1 ; + + dd =dd_from_dev (dev); + mtu =verbs_mtu_enum_to_int (ibqp->device, attr->path_mtu); +if (mtu ==-1 ) + goto inval; + +if (mtu > dd->pport[pidx].ibmtu) + pmtu =mtu_to_enum (dd->pport[pidx].ibmtu,IB_MTU_2048 ); +else + pmtu = attr->path_mtu; + } + +if (attr_mask &IB_QP_PATH_MIG_STATE ) { +if (attr->path_mig_state ==IB_MIG_REARM ) { +if (qp->s_mig_state ==IB_MIG_ARMED ) + goto inval; +if (new_state !=IB_QPS_RTS ) + goto inval; + }else if (attr->path_mig_state ==IB_MIG_MIGRATED ) { +if (qp->s_mig_state ==IB_MIG_REARM ) + goto inval; +if (new_state !=IB_QPS_RTS && new_state !=IB_QPS_SQD ) + goto inval; +if (qp->s_mig_state ==IB_MIG_ARMED ) + mig =1 ; + }else + goto inval; + } + +if (attr_mask &IB_QP_MAX_DEST_RD_ATOMIC ) +if (attr->max_dest_rd_atomic >HFI1_MAX_RDMA_ATOMIC ) + goto inval; + +switch (new_state) { +case IB_QPS_RESET : +if (qp->state !=IB_QPS_RESET ) { + qp->state =IB_QPS_RESET ; +flush_iowait (qp); + qp->s_flags &= ~(HFI1_S_TIMER |HFI1_S_ANY_WAIT ); +spin_unlock (&qp->s_lock); +spin_unlock_irq (&qp->r_lock); +/* Stop the sending work queue and retry timer */ +cancel_work_sync (&qp->s_iowait.iowork); +del_timer_sync (&qp->s_timer); +iowait_sdma_drain (&qp->s_iowait); +flush_tx_list (qp); +remove_qp (dev, qp); +wait_event (qp->wait, !atomic_read (&qp->refcount)); +spin_lock_irq (&qp->r_lock); +spin_lock (&qp->s_lock); +clear_mr_refs (qp,1 ); +clear_ahg (qp); +reset_qp (qp, ibqp->qp_type); + } +break ; + +case IB_QPS_RTR : +/* Allow event to re-trigger if QP set to RTR more than once */ + qp->r_flags &= ~HFI1_R_COMM_EST ; + qp->state = new_state; +break ; + +case IB_QPS_SQD : + qp->s_draining = qp->s_last != qp->s_cur; + qp->state = new_state; +break ; + +case IB_QPS_SQE : +if (qp->ibqp.qp_type ==IB_QPT_RC ) + goto inval; + qp->state = new_state; +break ; + +case IB_QPS_ERR : + lastwqe =hfi1_error_qp (qp,IB_WC_WR_FLUSH_ERR ); +break ; + +default : + qp->state = new_state; +break ; + } + +if (attr_mask &IB_QP_PKEY_INDEX ) + qp->s_pkey_index = attr->pkey_index; + +if (attr_mask &IB_QP_PORT ) + qp->port_num = attr->port_num; + +if (attr_mask &IB_QP_DEST_QPN ) + qp->remote_qpn = attr->dest_qp_num; + +if (attr_mask &IB_QP_SQ_PSN ) { + qp->s_next_psn = attr->sq_psn &PSN_MODIFY_MASK ; + qp->s_psn = qp->s_next_psn; + qp->s_sending_psn = qp->s_next_psn; + qp->s_last_psn = qp->s_next_psn -1 ; + qp->s_sending_hpsn = qp->s_last_psn; + } + +if (attr_mask &IB_QP_RQ_PSN ) + qp->r_psn = attr->rq_psn &PSN_MODIFY_MASK ; + +if (attr_mask &IB_QP_ACCESS_FLAGS ) + qp->qp_access_flags = attr->qp_access_flags; + +if (attr_mask &IB_QP_AV ) { + qp->remote_ah_attr = attr->ah_attr; + qp->s_srate = attr->ah_attr.static_rate; + qp->srate_mbps =ib_rate_to_mbps (qp->s_srate); + } + +if (attr_mask &IB_QP_ALT_PATH ) { + qp->alt_ah_attr = attr->alt_ah_attr; + qp->s_alt_pkey_index = attr->alt_pkey_index; + } + +if (attr_mask &IB_QP_PATH_MIG_STATE ) { + qp->s_mig_state = attr->path_mig_state; +if (mig) { + qp->remote_ah_attr = qp->alt_ah_attr; + qp->port_num = qp->alt_ah_attr.port_num; + qp->s_pkey_index = qp->s_alt_pkey_index; + qp->s_flags |=HFI1_S_AHG_CLEAR ; + } + } + +if (attr_mask &IB_QP_PATH_MTU ) { +struct hfi1_ibport *ibp; +u8 sc, vl; +u32 mtu; + + dd =dd_from_dev (dev); + ibp = &dd->pport[qp->port_num -1 ].ibport_data; + + sc = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + vl =sc_to_vlt (dd, sc); + + mtu =verbs_mtu_enum_to_int (ibqp->device, pmtu); +if (vl <PER_VL_SEND_CONTEXTS ) + mtu =min_t (u32, mtu, dd->vld[vl].mtu); + pmtu =mtu_to_enum (mtu,OPA_MTU_8192 ); + + qp->path_mtu = pmtu; + qp->pmtu = mtu; + } + +if (attr_mask &IB_QP_RETRY_CNT ) { + qp->s_retry_cnt = attr->retry_cnt; + qp->s_retry = attr->retry_cnt; + } + +if (attr_mask &IB_QP_RNR_RETRY ) { + qp->s_rnr_retry_cnt = attr->rnr_retry; + qp->s_rnr_retry = attr->rnr_retry; + } + +if (attr_mask &IB_QP_MIN_RNR_TIMER ) + qp->r_min_rnr_timer = attr->min_rnr_timer; + +if (attr_mask &IB_QP_TIMEOUT ) { + qp->timeout = attr->timeout; + qp->timeout_jiffies = +usecs_to_jiffies ((4096UL * (1UL << qp->timeout)) / +1000UL ); + } + +if (attr_mask &IB_QP_QKEY ) + qp->qkey = attr->qkey; + +if (attr_mask &IB_QP_MAX_DEST_RD_ATOMIC ) + qp->r_max_rd_atomic = attr->max_dest_rd_atomic; + +if (attr_mask &IB_QP_MAX_QP_RD_ATOMIC ) + qp->s_max_rd_atomic = attr->max_rd_atomic; + +spin_unlock (&qp->s_lock); +spin_unlock_irq (&qp->r_lock); + +if (cur_state ==IB_QPS_RESET && new_state ==IB_QPS_INIT ) +insert_qp (dev, qp); + +if (lastwqe) { + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_QP_LAST_WQE_REACHED ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); + } +if (mig) { + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_PATH_MIG ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); + } + ret =0 ; + goto bail; + +inval: +spin_unlock (&qp->s_lock); +spin_unlock_irq (&qp->r_lock); + ret = -EINVAL ; + +bail: +return ret; +} + +int hfi1_query_qp (struct ib_qp *ibqp,struct ib_qp_attr *attr, +int attr_mask,struct ib_qp_init_attr *init_attr) +{ +struct hfi1_qp *qp =to_iqp (ibqp); + + attr->qp_state = qp->state; + attr->cur_qp_state = attr->qp_state; + attr->path_mtu = qp->path_mtu; + attr->path_mig_state = qp->s_mig_state; + attr->qkey = qp->qkey; + attr->rq_psn =mask_psn (qp->r_psn); + attr->sq_psn =mask_psn (qp->s_next_psn); + attr->dest_qp_num = qp->remote_qpn; + attr->qp_access_flags = qp->qp_access_flags; + attr->cap.max_send_wr = qp->s_size -1 ; + attr->cap.max_recv_wr = qp->ibqp.srq ?0 : qp->r_rq.size -1 ; + attr->cap.max_send_sge = qp->s_max_sge; + attr->cap.max_recv_sge = qp->r_rq.max_sge; + attr->cap.max_inline_data =0 ; + attr->ah_attr = qp->remote_ah_attr; + attr->alt_ah_attr = qp->alt_ah_attr; + attr->pkey_index = qp->s_pkey_index; + attr->alt_pkey_index = qp->s_alt_pkey_index; + attr->en_sqd_async_notify =0 ; + attr->sq_draining = qp->s_draining; + attr->max_rd_atomic = qp->s_max_rd_atomic; + attr->max_dest_rd_atomic = qp->r_max_rd_atomic; + attr->min_rnr_timer = qp->r_min_rnr_timer; + attr->port_num = qp->port_num; + attr->timeout = qp->timeout; + attr->retry_cnt = qp->s_retry_cnt; + attr->rnr_retry = qp->s_rnr_retry_cnt; + attr->alt_port_num = qp->alt_ah_attr.port_num; + attr->alt_timeout = qp->alt_timeout; + + init_attr->event_handler = qp->ibqp.event_handler; + init_attr->qp_context = qp->ibqp.qp_context; + init_attr->send_cq = qp->ibqp.send_cq; + init_attr->recv_cq = qp->ibqp.recv_cq; + init_attr->srq = qp->ibqp.srq; + init_attr->cap = attr->cap; +if (qp->s_flags &HFI1_S_SIGNAL_REQ_WR ) + init_attr->sq_sig_type =IB_SIGNAL_REQ_WR ; +else + init_attr->sq_sig_type =IB_SIGNAL_ALL_WR ; + init_attr->qp_type = qp->ibqp.qp_type; + init_attr->port_num = qp->port_num; +return 0 ; +} + +/** +* hfi1_compute_aeth - compute the AETH (syndrome + MSN) +* @qp: the queue pair to compute the AETH for +* +* Returns the AETH. +*/ +__be32 hfi1_compute_aeth (struct hfi1_qp *qp) +{ +u32 aeth = qp->r_msn &HFI1_MSN_MASK ; + +if (qp->ibqp.srq) { +/* +* Shared receive queues don't generate credits. +* Set the credit field to the invalid value. +*/ + aeth |=HFI1_AETH_CREDIT_INVAL <<HFI1_AETH_CREDIT_SHIFT ; + }else { +u32 min, max, x; +u32 credits; +struct hfi1_rwq *wq = qp->r_rq.wq; +u32 head; +u32 tail; + +/* sanity check pointers before trusting them */ + head = wq->head; +if (head >= qp->r_rq.size) + head =0 ; + tail = wq->tail; +if (tail >= qp->r_rq.size) + tail =0 ; +/* +* Compute the number of credits available (RWQEs). +* There is a small chance that the pair of reads are +* not atomic, which is OK, since the fuzziness is +* resolved as further ACKs go out. +*/ + credits = head - tail; +if ((int )credits <0 ) + credits += qp->r_rq.size; +/* +* Binary search the credit table to find the code to +* use. +*/ + min =0 ; + max =31 ; +for (;;) { + x = (min + max) /2 ; +if (credit_table[x] == credits) +break ; +if (credit_table[x] > credits) + max = x; +else if (min == x) +break ; +else + min = x; + } + aeth |= x <<HFI1_AETH_CREDIT_SHIFT ; + } +return cpu_to_be32 (aeth); +} + +/** +* hfi1_create_qp - create a queue pair for a device +* @ibpd: the protection domain who's device we create the queue pair for +* @init_attr: the attributes of the queue pair +* @udata: user data for libibverbs.so +* +* Returns the queue pair on success, otherwise returns an errno. +* +* Called by the ib_create_qp() core verbs function. +*/ +struct ib_qp *hfi1_create_qp (struct ib_pd *ibpd, +struct ib_qp_init_attr *init_attr, +struct ib_udata *udata) +{ +struct hfi1_qp *qp; +int err; +struct hfi1_swqe *swq =NULL ; +struct hfi1_ibdev *dev; +struct hfi1_devdata *dd; +size_t sz; +size_t sg_list_sz; +struct ib_qp *ret; + +if (init_attr->cap.max_send_sge > hfi1_max_sges || + init_attr->cap.max_send_wr > hfi1_max_qp_wrs || + init_attr->create_flags) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } + +/* Check receive queue parameters if no SRQ is specified. */ +if (!init_attr->srq) { +if (init_attr->cap.max_recv_sge > hfi1_max_sges || + init_attr->cap.max_recv_wr > hfi1_max_qp_wrs) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } +if (init_attr->cap.max_send_sge + + init_attr->cap.max_send_wr + + init_attr->cap.max_recv_sge + + init_attr->cap.max_recv_wr ==0 ) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } + } + +switch (init_attr->qp_type) { +case IB_QPT_SMI : +case IB_QPT_GSI : +if (init_attr->port_num ==0 || + init_attr->port_num > ibpd->device->phys_port_cnt) { + ret =ERR_PTR (-EINVAL ); + goto bail; + } +case IB_QPT_UC : +case IB_QPT_RC : +case IB_QPT_UD : + sz =sizeof (struct hfi1_sge ) * + init_attr->cap.max_send_sge + +sizeof (struct hfi1_swqe ); + swq =vmalloc ((init_attr->cap.max_send_wr +1 ) * sz); +if (swq ==NULL ) { + ret =ERR_PTR (-ENOMEM ); + goto bail; + } + sz =sizeof (*qp); + sg_list_sz =0 ; +if (init_attr->srq) { +struct hfi1_srq *srq =to_isrq (init_attr->srq); + +if (srq->rq.max_sge >1 ) + sg_list_sz =sizeof (*qp->r_sg_list) * + (srq->rq.max_sge -1 ); + }else if (init_attr->cap.max_recv_sge >1 ) + sg_list_sz =sizeof (*qp->r_sg_list) * + (init_attr->cap.max_recv_sge -1 ); + qp =kzalloc (sz + sg_list_sz,GFP_KERNEL ); +if (!qp) { + ret =ERR_PTR (-ENOMEM ); + goto bail_swq; + } +RCU_INIT_POINTER (qp->next,NULL ); + qp->s_hdr =kzalloc (sizeof (*qp->s_hdr),GFP_KERNEL ); +if (!qp->s_hdr) { + ret =ERR_PTR (-ENOMEM ); + goto bail_qp; + } + qp->timeout_jiffies = +usecs_to_jiffies ((4096UL * (1UL << qp->timeout)) / +1000UL ); +if (init_attr->srq) + sz =0 ; +else { + qp->r_rq.size = init_attr->cap.max_recv_wr +1 ; + qp->r_rq.max_sge = init_attr->cap.max_recv_sge; + sz = (sizeof (struct ib_sge ) * qp->r_rq.max_sge) + +sizeof (struct hfi1_rwqe ); + qp->r_rq.wq =vmalloc_user (sizeof (struct hfi1_rwq ) + + qp->r_rq.size * sz); +if (!qp->r_rq.wq) { + ret =ERR_PTR (-ENOMEM ); + goto bail_qp; + } + } + +/* +* ib_create_qp() will initialize qp->ibqp +* except for qp->ibqp.qp_num. +*/ +spin_lock_init (&qp->r_lock); +spin_lock_init (&qp->s_lock); +spin_lock_init (&qp->r_rq.lock); +atomic_set (&qp->refcount,0 ); +init_waitqueue_head (&qp->wait); +init_timer (&qp->s_timer); + qp->s_timer.data = (unsigned long )qp; +INIT_LIST_HEAD (&qp->rspwait); + qp->state =IB_QPS_RESET ; + qp->s_wq = swq; + qp->s_size = init_attr->cap.max_send_wr +1 ; + qp->s_max_sge = init_attr->cap.max_send_sge; +if (init_attr->sq_sig_type ==IB_SIGNAL_REQ_WR ) + qp->s_flags =HFI1_S_SIGNAL_REQ_WR ; + dev =to_idev (ibpd->device); + dd =dd_from_dev (dev); + err =alloc_qpn (dd, &dev->qp_dev->qpn_table, init_attr->qp_type, + init_attr->port_num); +if (err <0 ) { + ret =ERR_PTR (err); +vfree (qp->r_rq.wq); + goto bail_qp; + } + qp->ibqp.qp_num = err; + qp->port_num = init_attr->port_num; +reset_qp (qp, init_attr->qp_type); +break ; + +default : +/* Don't support raw QPs */ + ret =ERR_PTR (-ENOSYS ); + goto bail; + } + + init_attr->cap.max_inline_data =0 ; + +/* +* Return the address of the RWQ as the offset to mmap. +* See hfi1_mmap() for details. +*/ +if (udata && udata->outlen >=sizeof (__u64)) { +if (!qp->r_rq.wq) { +__u64 offset =0 ; + + err =ib_copy_to_udata (udata, &offset, +sizeof (offset)); +if (err) { + ret =ERR_PTR (err); + goto bail_ip; + } + }else { +u32 s =sizeof (struct hfi1_rwq ) + qp->r_rq.size * sz; + + qp->ip =hfi1_create_mmap_info (dev, s, + ibpd->uobject->context, + qp->r_rq.wq); +if (!qp->ip) { + ret =ERR_PTR (-ENOMEM ); + goto bail_ip; + } + + err =ib_copy_to_udata (udata, &(qp->ip->offset), +sizeof (qp->ip->offset)); +if (err) { + ret =ERR_PTR (err); + goto bail_ip; + } + } + } + +spin_lock (&dev->n_qps_lock); +if (dev->n_qps_allocated == hfi1_max_qps) { +spin_unlock (&dev->n_qps_lock); + ret =ERR_PTR (-ENOMEM ); + goto bail_ip; + } + + dev->n_qps_allocated++; +spin_unlock (&dev->n_qps_lock); + +if (qp->ip) { +spin_lock_irq (&dev->pending_lock); +list_add (&qp->ip->pending_mmaps, &dev->pending_mmaps); +spin_unlock_irq (&dev->pending_lock); + } + + ret = &qp->ibqp; + goto bail; + +bail_ip: +if (qp->ip) +kref_put (&qp->ip->ref, hfi1_release_mmap_info); +else +vfree (qp->r_rq.wq); +free_qpn (&dev->qp_dev->qpn_table, qp->ibqp.qp_num); +bail_qp: +kfree (qp->s_hdr); +kfree (qp); +bail_swq: +vfree (swq); +bail: +return ret; +} + +/** +* hfi1_destroy_qp - destroy a queue pair +* @ibqp: the queue pair to destroy +* +* Returns 0 on success. +* +* Note that this can be called while the QP is actively sending or +* receiving! +*/ +int hfi1_destroy_qp (struct ib_qp *ibqp) +{ +struct hfi1_qp *qp =to_iqp (ibqp); +struct hfi1_ibdev *dev =to_idev (ibqp->device); + +/* Make sure HW and driver activity is stopped. */ +spin_lock_irq (&qp->r_lock); +spin_lock (&qp->s_lock); +if (qp->state !=IB_QPS_RESET ) { + qp->state =IB_QPS_RESET ; +flush_iowait (qp); + qp->s_flags &= ~(HFI1_S_TIMER |HFI1_S_ANY_WAIT ); +spin_unlock (&qp->s_lock); +spin_unlock_irq (&qp->r_lock); +cancel_work_sync (&qp->s_iowait.iowork); +del_timer_sync (&qp->s_timer); +iowait_sdma_drain (&qp->s_iowait); +flush_tx_list (qp); +remove_qp (dev, qp); +wait_event (qp->wait, !atomic_read (&qp->refcount)); +spin_lock_irq (&qp->r_lock); +spin_lock (&qp->s_lock); +clear_mr_refs (qp,1 ); +clear_ahg (qp); + } +spin_unlock (&qp->s_lock); +spin_unlock_irq (&qp->r_lock); + +/* all user's cleaned up, mark it available */ +free_qpn (&dev->qp_dev->qpn_table, qp->ibqp.qp_num); +spin_lock (&dev->n_qps_lock); + dev->n_qps_allocated--; +spin_unlock (&dev->n_qps_lock); + +if (qp->ip) +kref_put (&qp->ip->ref, hfi1_release_mmap_info); +else +vfree (qp->r_rq.wq); +vfree (qp->s_wq); +kfree (qp->s_hdr); +kfree (qp); +return 0 ; +} + +/** +* init_qpn_table - initialize the QP number table for a device +* @qpt: the QPN table +*/ +static int init_qpn_table (struct hfi1_devdata *dd,struct hfi1_qpn_table *qpt) +{ +u32 offset, qpn, i; +struct qpn_map *map; +int ret =0 ; + +spin_lock_init (&qpt->lock); + + qpt->last =0 ; + qpt->incr =1 << dd->qos_shift; + +/* insure we don't assign QPs from KDETH 64K window */ + qpn = kdeth_qp <<16 ; + qpt->nmaps = qpn /BITS_PER_PAGE ; +/* This should always be zero */ + offset = qpn &BITS_PER_PAGE_MASK ; + map = &qpt->map[qpt->nmaps]; +dd_dev_info (dd,"Reserving QPNs for KDETH window from 0x%x to 0x%x\n" , + qpn, qpn +65535 ); +for (i =0 ; i <65536 ; i++) { +if (!map->page) { +get_map_page (qpt, map); +if (!map->page) { + ret = -ENOMEM ; +break ; + } + } +set_bit (offset, map->page); + offset++; +if (offset ==BITS_PER_PAGE ) { +/* next page */ + qpt->nmaps++; + map++; + offset =0 ; + } + } +return ret; +} + +/** +* free_qpn_table - free the QP number table for a device +* @qpt: the QPN table +*/ +static void free_qpn_table (struct hfi1_qpn_table *qpt) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (qpt->map); i++) +free_page ((unsigned long ) qpt->map[i].page); +} + +/** +* hfi1_get_credit - flush the send work queue of a QP +* @qp: the qp who's send work queue to flush +* @aeth: the Acknowledge Extended Transport Header +* +* The QP s_lock should be held. +*/ +void hfi1_get_credit (struct hfi1_qp *qp,u32 aeth) +{ +u32 credit = (aeth >>HFI1_AETH_CREDIT_SHIFT ) &HFI1_AETH_CREDIT_MASK ; + +/* +* If the credit is invalid, we can send +* as many packets as we like. Otherwise, we have to +* honor the credit field. +*/ +if (credit ==HFI1_AETH_CREDIT_INVAL ) { +if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT )) { + qp->s_flags |=HFI1_S_UNLIMITED_CREDIT ; +if (qp->s_flags &HFI1_S_WAIT_SSN_CREDIT ) { + qp->s_flags &= ~HFI1_S_WAIT_SSN_CREDIT ; +hfi1_schedule_send (qp); + } + } + }else if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT )) { +/* Compute new LSN (i.e., MSN + credit) */ + credit = (aeth + credit_table[credit]) &HFI1_MSN_MASK ; +if (cmp_msn (credit, qp->s_lsn) >0 ) { + qp->s_lsn = credit; +if (qp->s_flags &HFI1_S_WAIT_SSN_CREDIT ) { + qp->s_flags &= ~HFI1_S_WAIT_SSN_CREDIT ; +hfi1_schedule_send (qp); + } + } + } +} + +void hfi1_qp_wakeup (struct hfi1_qp *qp,u32 flag) +{ +unsigned long flags; + +spin_lock_irqsave (&qp->s_lock, flags); +if (qp->s_flags & flag) { + qp->s_flags &= ~flag; +trace_hfi1_qpwakeup (qp, flag); +hfi1_schedule_send (qp); + } +spin_unlock_irqrestore (&qp->s_lock, flags); +/* Notify hfi1_destroy_qp() if it is waiting. */ +if (atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); +} + +static int iowait_sleep ( +struct sdma_engine *sde, +struct iowait *wait, +struct sdma_txreq *stx, +unsigned seq) +{ + struct verbs_txreq *tx = container_of(stx, struct verbs_txreq, txreq); +struct hfi1_qp *qp; +unsigned long flags; +int ret =0 ; +struct hfi1_ibdev *dev; + + qp = tx->qp; + +spin_lock_irqsave (&qp->s_lock, flags); +if (ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK ) { + +/* +* If we couldn't queue the DMA request, save the info +* and try again later rather than destroying the +* buffer and undoing the side effects of the copy. +*/ +/* Make a common routine? */ + dev = &sde->dd->verbs_dev; +list_add_tail (&stx->list, &wait->tx_head); +spin_lock (&dev->pending_lock); +if (sdma_progress (sde, seq, stx)) + goto eagain; +if (list_empty (&qp->s_iowait.list)) { +struct hfi1_ibport *ibp = +to_iport (qp->ibqp.device, qp->port_num); + + ibp->n_dmawait++; + qp->s_flags |=HFI1_S_WAIT_DMA_DESC ; +list_add_tail (&qp->s_iowait.list, &sde->dmawait); +trace_hfi1_qpsleep (qp,HFI1_S_WAIT_DMA_DESC ); +atomic_inc (&qp->refcount); + } +spin_unlock (&dev->pending_lock); + qp->s_flags &= ~HFI1_S_BUSY ; +spin_unlock_irqrestore (&qp->s_lock, flags); + ret = -EBUSY ; + }else { +spin_unlock_irqrestore (&qp->s_lock, flags); +hfi1_put_txreq (tx); + } +return ret; +eagain: +spin_unlock (&dev->pending_lock); +spin_unlock_irqrestore (&qp->s_lock, flags); +list_del_init (&stx->list); +return -EAGAIN ; +} + +static void iowait_wakeup (struct iowait *wait,int reason) +{ + struct hfi1_qp *qp = container_of(wait, struct hfi1_qp, s_iowait); + +BUG_ON (reason !=SDMA_AVAIL_REASON ); +hfi1_qp_wakeup (qp,HFI1_S_WAIT_DMA_DESC ); +} + +int hfi1_qp_init (struct hfi1_ibdev *dev) +{ +struct hfi1_devdata *dd =dd_from_dev (dev); +int i; +int ret = -ENOMEM ; + +/* allocate parent object */ + dev->qp_dev =kzalloc (sizeof (*dev->qp_dev),GFP_KERNEL ); +if (!dev->qp_dev) + goto nomem; +/* allocate hash table */ + dev->qp_dev->qp_table_size = hfi1_qp_table_size; +get_random_bytes (&dev->qp_dev->qp_rnd, +sizeof (dev->qp_dev->qp_rnd)); + dev->qp_dev->qp_table = +kmalloc (dev->qp_dev->qp_table_size * +sizeof (*dev->qp_dev->qp_table), +GFP_KERNEL ); +if (!dev->qp_dev->qp_table) + goto nomem; +for (i =0 ; i < dev->qp_dev->qp_table_size; i++) +RCU_INIT_POINTER (dev->qp_dev->qp_table[i],NULL ); +spin_lock_init (&dev->qp_dev->qpt_lock); +/* initialize qpn map */ + ret =init_qpn_table (dd, &dev->qp_dev->qpn_table); +if (ret) + goto nomem; +return ret; +nomem: +if (dev->qp_dev) { +kfree (dev->qp_dev->qp_table); +free_qpn_table (&dev->qp_dev->qpn_table); +kfree (dev->qp_dev); + } +return ret; +} + +void hfi1_qp_exit (struct hfi1_ibdev *dev) +{ +struct hfi1_devdata *dd =dd_from_dev (dev); +u32 qps_inuse; + + qps_inuse =free_all_qps (dd); +if (qps_inuse) +dd_dev_err (dd,"QP memory leak! %u still in use\n" , + qps_inuse); +if (dev->qp_dev) { +kfree (dev->qp_dev->qp_table); +free_qpn_table (&dev->qp_dev->qpn_table); +kfree (dev->qp_dev); + } +} + +/** +* +* qp_to_sdma_engine - map a qp to a send engine +* @qp: the QP +* @sc5: the 5 bit sc +* +* Return: +* A send engine for the qp or NULL for SMI type qp. +*/ +struct sdma_engine *qp_to_sdma_engine (struct hfi1_qp *qp,u8 sc5) +{ +struct hfi1_devdata *dd =dd_from_ibdev (qp->ibqp.device); +struct sdma_engine *sde; + +if (!(dd->flags &HFI1_HAS_SEND_DMA )) +return NULL ; +switch (qp->ibqp.qp_type) { +case IB_QPT_UC : +case IB_QPT_RC : +break ; +case IB_QPT_SMI : +return NULL ; +default : +break ; + } + sde =sdma_select_engine_sc (dd, qp->ibqp.qp_num >> dd->qos_shift, sc5); +return sde; +} + +struct qp_iter { +struct hfi1_ibdev *dev; +struct hfi1_qp *qp; +int specials; +int n; +}; + +struct qp_iter *qp_iter_init (struct hfi1_ibdev *dev) +{ +struct qp_iter *iter; + + iter =kzalloc (sizeof (*iter),GFP_KERNEL ); +if (!iter) +return NULL ; + + iter->dev = dev; + iter->specials = dev->ibdev.phys_port_cnt *2 ; +if (qp_iter_next (iter)) { +kfree (iter); +return NULL ; + } + +return iter; +} + +int qp_iter_next (struct qp_iter *iter) +{ +struct hfi1_ibdev *dev = iter->dev; +int n = iter->n; +int ret =1 ; +struct hfi1_qp *pqp = iter->qp; +struct hfi1_qp *qp; + +/* +* The approach is to consider the special qps +* as an additional table entries before the +* real hash table. Since the qp code sets +* the qp->next hash link to NULL, this works just fine. +* +* iter->specials is 2 * # ports +* +* n = 0..iter->specials is the special qp indices +* +* n = iter->specials..dev->qp_dev->qp_table_size+iter->specials are +* the potential hash bucket entries +* +*/ +for (; n < dev->qp_dev->qp_table_size + iter->specials; n++) { +if (pqp) { + qp =rcu_dereference (pqp->next); + }else { +if (n < iter->specials) { +struct hfi1_pportdata *ppd; +struct hfi1_ibport *ibp; +int pidx; + + pidx = n % dev->ibdev.phys_port_cnt; + ppd = &dd_from_dev (dev)->pport[pidx]; + ibp = &ppd->ibport_data; + +if (!(n &1 )) + qp =rcu_dereference (ibp->qp0); +else + qp =rcu_dereference (ibp->qp1); + }else { + qp =rcu_dereference ( + dev->qp_dev->qp_table[ + (n - iter->specials)]); + } + } + pqp = qp; +if (qp) { + iter->qp = qp; + iter->n = n; +return 0 ; + } + } +return ret; +} + +static const char *const qp_type_str[] = { +"SMI" ,"GSI" ,"RC" ,"UC" ,"UD" , +}; + +static int qp_idle (struct hfi1_qp *qp) +{ +return + qp->s_last == qp->s_acked && + qp->s_acked == qp->s_cur && + qp->s_cur == qp->s_tail && + qp->s_tail == qp->s_head; +} + +void qp_iter_print (struct seq_file *s,struct qp_iter *iter) +{ +struct hfi1_swqe *wqe; +struct hfi1_qp *qp = iter->qp; +struct sdma_engine *sde; + + sde =qp_to_sdma_engine (qp, qp->s_sc); + wqe =get_swqe_ptr (qp, qp->s_last); +seq_printf (s, +"N %d %s QP%u R %u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x SL %u MTU %d %u %u %u SDE %p,%u\n" , + iter->n, +qp_idle (qp) ?"I" :"B" , + qp->ibqp.qp_num, +atomic_read (&qp->refcount), + qp_type_str[qp->ibqp.qp_type], + qp->state, + wqe ? wqe->wr.opcode :0 , + qp->s_hdrwords, + qp->s_flags, +atomic_read (&qp->s_iowait.sdma_busy), + !list_empty (&qp->s_iowait.list), + qp->timeout, + wqe ? wqe->ssn :0 , + qp->s_lsn, + qp->s_last_psn, + qp->s_psn, qp->s_next_psn, + qp->s_sending_psn, qp->s_sending_hpsn, + qp->s_last, qp->s_acked, qp->s_cur, + qp->s_tail, qp->s_head, qp->s_size, + qp->remote_qpn, + qp->remote_ah_attr.dlid, + qp->remote_ah_attr.sl, + qp->pmtu, + qp->s_retry_cnt, + qp->timeout, + qp->s_rnr_retry_cnt, + sde, + sde ? sde->this_idx :0 ); +}
drivers/infiniband/hw/hfi1/qp.h new file +206 -0
206 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 26/41 IB/hfi1: add qsfp handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/qsfp.c | 546 +++++++++++++++++++++++++++++++++++++ drivers/infiniband/hw/hfi1/qsfp.h | 222 +++++++++++++++ 2 files changed, 768 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/qsfp.c create mode 100644 drivers/infiniband/hw/hfi1/qsfp.h
drivers/infiniband/hw/hfi1/qsfp.c new file +546 -0
@@ -0,0 +1,546 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/delay.h> +#include <linux/pci.h> +#include <linux/vmalloc.h> + +#include "hfi.h" +#include "twsi.h" + +/* +* QSFP support for hfi driver, using "Two Wire Serial Interface" driver +* in twsi.c +*/ +#define I2C_MAX_RETRY 4 + +/* +* Unlocked i2c write. Must hold dd->qsfp_i2c_mutex. +*/ +static int __i2c_write (struct hfi1_pportdata *ppd,u32 target,int i2c_addr, +int offset,void *bp,int len) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret, cnt; +u8 *buff = bp; + +/* Make sure TWSI bus is in sane state. */ + ret =hfi1_twsi_reset (dd, target); +if (ret) { +hfi1_dev_porterr (dd, ppd->port, +"I2C interface Reset for write failed\n" ); +return -EIO ; + } + + cnt =0 ; +while (cnt < len) { +int wlen = len - cnt; + + ret =hfi1_twsi_blk_wr (dd, target, i2c_addr, offset, + buff + cnt, wlen); +if (ret) { +/* hfi1_twsi_blk_wr() 1 for error, else 0 */ +return -EIO ; + } + offset += wlen; + cnt += wlen; + } + +/* Must wait min 20us between qsfp i2c transactions */ +udelay (20 ); + +return cnt; +} + +int i2c_write (struct hfi1_pportdata *ppd,u32 target,int i2c_addr,int offset, +void *bp,int len) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret; + + ret =mutex_lock_interruptible (&dd->qsfp_i2c_mutex); +if (!ret) { + ret =__i2c_write (ppd, target, i2c_addr, offset, bp, len); +mutex_unlock (&dd->qsfp_i2c_mutex); + } + +return ret; +} + +/* +* Unlocked i2c read. Must hold dd->qsfp_i2c_mutex. +*/ +static int __i2c_read (struct hfi1_pportdata *ppd,u32 target,int i2c_addr, +int offset,void *bp,int len) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret, cnt, pass =0 ; +int stuck =0 ; +u8 *buff = bp; + +/* Make sure TWSI bus is in sane state. */ + ret =hfi1_twsi_reset (dd, target); +if (ret) { +hfi1_dev_porterr (dd, ppd->port, +"I2C interface Reset for read failed\n" ); + ret = -EIO ; + stuck =1 ; + goto exit; + } + + cnt =0 ; +while (cnt < len) { +int rlen = len - cnt; + + ret =hfi1_twsi_blk_rd (dd, target, i2c_addr, offset, + buff + cnt, rlen); +/* Some QSFP's fail first try. Retry as experiment */ +if (ret && cnt ==0 && ++pass <I2C_MAX_RETRY ) +continue ; +if (ret) { +/* hfi1_twsi_blk_rd() 1 for error, else 0 */ + ret = -EIO ; + goto exit; + } + offset += rlen; + cnt += rlen; + } + + ret = cnt; + +exit: +if (stuck) +dd_dev_err (dd,"I2C interface bus stuck non-idle\n" ); + +if (pass >=I2C_MAX_RETRY && ret) +hfi1_dev_porterr (dd, ppd->port, +"I2C failed even retrying\n" ); +else if (pass) +hfi1_dev_porterr (dd, ppd->port,"I2C retries: %d\n" , pass); + +/* Must wait min 20us between qsfp i2c transactions */ +udelay (20 ); + +return ret; +} + +int i2c_read (struct hfi1_pportdata *ppd,u32 target,int i2c_addr,int offset, +void *bp,int len) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret; + + ret =mutex_lock_interruptible (&dd->qsfp_i2c_mutex); +if (!ret) { + ret =__i2c_read (ppd, target, i2c_addr, offset, bp, len); +mutex_unlock (&dd->qsfp_i2c_mutex); + } + +return ret; +} + +int qsfp_write (struct hfi1_pportdata *ppd,u32 target,int addr,void *bp, +int len) +{ +int count =0 ; +int offset; +int nwrite; +int ret; +u8 page; + + ret =mutex_lock_interruptible (&ppd->dd->qsfp_i2c_mutex); +if (ret) +return ret; + +while (count < len) { +/* +* Set the qsfp page based on a zero-based addresss +* and a page size of QSFP_PAGESIZE bytes. +*/ + page = (u8)(addr /QSFP_PAGESIZE ); + + ret =__i2c_write (ppd, target,QSFP_DEV , +QSFP_PAGE_SELECT_BYTE_OFFS , &page,1 ); +if (ret !=1 ) { +hfi1_dev_porterr ( + ppd->dd, + ppd->port, +"can't write QSFP_PAGE_SELECT_BYTE: %d\n" , ret); + ret = -EIO ; +break ; + } + +/* truncate write to end of page if crossing page boundary */ + offset = addr %QSFP_PAGESIZE ; + nwrite = len - count; +if ((offset + nwrite) >QSFP_PAGESIZE ) + nwrite =QSFP_PAGESIZE - offset; + + ret =__i2c_write (ppd, target,QSFP_DEV , offset, bp + count, + nwrite); +if (ret <=0 )/* stop on error or nothing read */ +break ; + + count += ret; + addr += ret; + } + +mutex_unlock (&ppd->dd->qsfp_i2c_mutex); + +if (ret <0 ) +return ret; +return count; +} + +int qsfp_read (struct hfi1_pportdata *ppd,u32 target,int addr,void *bp, +int len) +{ +int count =0 ; +int offset; +int nread; +int ret; +u8 page; + + ret =mutex_lock_interruptible (&ppd->dd->qsfp_i2c_mutex); +if (ret) +return ret; + +while (count < len) { +/* +* Set the qsfp page based on a zero-based address +* and a page size of QSFP_PAGESIZE bytes. +*/ + page = (u8)(addr /QSFP_PAGESIZE ); + ret =__i2c_write (ppd, target,QSFP_DEV , +QSFP_PAGE_SELECT_BYTE_OFFS , &page,1 ); +if (ret !=1 ) { +hfi1_dev_porterr ( + ppd->dd, + ppd->port, +"can't write QSFP_PAGE_SELECT_BYTE: %d\n" , ret); + ret = -EIO ; +break ; + } + +/* truncate read to end of page if crossing page boundary */ + offset = addr %QSFP_PAGESIZE ; + nread = len - count; +if ((offset + nread) >QSFP_PAGESIZE ) + nread =QSFP_PAGESIZE - offset; + + ret =__i2c_read (ppd, target,QSFP_DEV , offset, bp + count, + nread); +if (ret <=0 )/* stop on error or nothing read */ +break ; + + count += ret; + addr += ret; + } + +mutex_unlock (&ppd->dd->qsfp_i2c_mutex); + +if (ret <0 ) +return ret; +return count; +} + +/* +* This function caches the QSFP memory range in 128 byte chunks. +* As an example, the next byte after address 255 is byte 128 from +* upper page 01H (if existing) rather than byte 0 from lower page 00H. +*/ +int refresh_qsfp_cache (struct hfi1_pportdata *ppd,struct qsfp_data *cp) +{ +u32 target = ppd->dd->hfi1_id; +int ret; +unsigned long flags; +u8 *cache = &cp->cache[0 ]; + +/* ensure sane contents on invalid reads, for cable swaps */ +memset (cache,0 , (QSFP_MAX_NUM_PAGES *128 )); +dd_dev_info (ppd->dd,"%s: called\n" , __func__); +if (!qsfp_mod_present (ppd)) { + ret = -ENODEV ; + goto bail; + } + + ret =qsfp_read (ppd, target,0 , cache,256 ); +if (ret !=256 ) { +dd_dev_info (ppd->dd, +"%s: Read of pages 00H failed, expected 256, got %d\n" , + __func__, ret); + goto bail; + } + +if (cache[0 ] !=0x0C && cache[0 ] !=0x0D ) + goto bail; + +/* Is paging enabled? */ +if (!(cache[2 ] &4 )) { + +/* Paging enabled, page 03 required */ +if ((cache[195 ] &0xC0 ) ==0xC0 ) { +/* all */ + ret =qsfp_read (ppd, target,384 , cache +256 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + ret =qsfp_read (ppd, target,640 , cache +384 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + ret =qsfp_read (ppd, target,896 , cache +512 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + }else if ((cache[195 ] &0x80 ) ==0x80 ) { +/* only page 2 and 3 */ + ret =qsfp_read (ppd, target,640 , cache +384 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + ret =qsfp_read (ppd, target,896 , cache +512 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + }else if ((cache[195 ] &0x40 ) ==0x40 ) { +/* only page 1 and 3 */ + ret =qsfp_read (ppd, target,384 , cache +256 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + ret =qsfp_read (ppd, target,896 , cache +512 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + }else { +/* only page 3 */ + ret =qsfp_read (ppd, target,896 , cache +512 ,128 ); +if (ret <=0 || ret !=128 ) { +dd_dev_info (ppd->dd,"%s: failed\n" , __func__); + goto bail; + } + } + } + +spin_lock_irqsave (&ppd->qsfp_info.qsfp_lock, flags); + ppd->qsfp_info.cache_valid =1 ; + ppd->qsfp_info.cache_refresh_required =0 ; +spin_unlock_irqrestore (&ppd->qsfp_info.qsfp_lock, flags); + +return 0 ; + +bail: +memset (cache,0 , (QSFP_MAX_NUM_PAGES *128 )); +return ret; +} + +const char *const hfi1_qsfp_devtech[16 ] = { +"850nm VCSEL" ,"1310nm VCSEL" ,"1550nm VCSEL" ,"1310nm FP" , +"1310nm DFB" ,"1550nm DFB" ,"1310nm EML" ,"1550nm EML" , +"Cu Misc" ,"1490nm DFB" ,"Cu NoEq" ,"Cu Eq" , +"Undef" ,"Cu Active BothEq" ,"Cu FarEq" ,"Cu NearEq" +}; + +#define QSFP_DUMP_CHUNK 16/* Holds longest string */ +#define QSFP_DEFAULT_HDR_CNT 224 + +static const char *pwr_codes ="1.5W2.0W2.5W3.5W" ; + +int qsfp_mod_present (struct hfi1_pportdata *ppd) +{ +if (HFI1_CAP_IS_KSET (QSFP_ENABLED )) { +struct hfi1_devdata *dd = ppd->dd; +u64 reg; + + reg =read_csr (dd, + dd->hfi1_id ?ASIC_QSFP2_IN :ASIC_QSFP1_IN ); +return !(reg &QSFP_HFI0_MODPRST_N ); + } +/* always return cable present */ +return 1 ; +} + +/* +* This function maps QSFP memory addresses in 128 byte chunks in the following +* fashion per the CableInfo SMA query definition in the IBA 1.3 spec/OPA Gen 1 +* spec +* For addr 000-127, lower page 00h +* For addr 128-255, upper page 00h +* For addr 256-383, upper page 01h +* For addr 384-511, upper page 02h +* For addr 512-639, upper page 03h +* +* For addresses beyond this range, it returns the invalid range of data buffer +* set to 0. +* For upper pages that are optional, if they are not valid, returns the +* particular range of bytes in the data buffer set to 0. +*/ +int get_cable_info (struct hfi1_devdata *dd,u32 port_num,u32 addr,u32 len, +u8 *data) +{ +struct hfi1_pportdata *ppd; +u32 excess_len = len; +int ret =0 ; + +if (port_num > dd->num_pports || port_num <1 ) { +dd_dev_info (dd,"%s: Invalid port number %d\n" , + __func__, port_num); + ret = -EINVAL ; + goto set_zeroes; + } + + ppd = dd->pport + (port_num -1 ); +if (!qsfp_mod_present (ppd)) { + ret = -ENODEV ; + goto set_zeroes; + } + +if (!ppd->qsfp_info.cache_valid) { + ret = -EINVAL ; + goto set_zeroes; + } + +if (addr >= (QSFP_MAX_NUM_PAGES *128 )) { + ret = -ERANGE ; + goto set_zeroes; + } + +if ((addr + len) >= (QSFP_MAX_NUM_PAGES *128 )) { + excess_len = (addr + len) - (QSFP_MAX_NUM_PAGES *128 ); +memcpy (data, &ppd->qsfp_info.cache[addr], (len - excess_len)); + data += (len - excess_len); + goto set_zeroes; + } + +memcpy (data, &ppd->qsfp_info.cache[addr], len); +return 0 ; + +set_zeroes: +memset (data,0 , excess_len); +return ret; +} + +int qsfp_dump (struct hfi1_pportdata *ppd,char *buf,int len) +{ +u8 *cache = &ppd->qsfp_info.cache[0 ]; +u8 bin_buff[QSFP_DUMP_CHUNK ]; +char lenstr[6 ]; +int sofar, ret; +int bidx =0 ; +u8 *atten = &cache[QSFP_ATTEN_OFFS ]; +u8 *vendor_oui = &cache[QSFP_VOUI_OFFS ]; + + sofar =0 ; + lenstr[0 ] =' ' ; + lenstr[1 ] ='\0' ; + +if (ppd->qsfp_info.cache_valid) { + +if (QSFP_IS_CU (cache[QSFP_MOD_TECH_OFFS ])) +sprintf (lenstr,"%dM " , cache[QSFP_MOD_LEN_OFFS ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"PWR:%.3sW\n" , + pwr_codes + + (QSFP_PWR (cache[QSFP_MOD_PWR_OFFS ]) *4 )); + + sofar +=scnprintf (buf + sofar, len - sofar,"TECH:%s%s\n" , + lenstr, + hfi1_qsfp_devtech[(cache[QSFP_MOD_TECH_OFFS ]) >>4 ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"Vendor:%.*s\n" , +QSFP_VEND_LEN , &cache[QSFP_VEND_OFFS ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"OUI:%06X\n" , +QSFP_OUI (vendor_oui)); + + sofar +=scnprintf (buf + sofar, len - sofar,"Part#:%.*s\n" , +QSFP_PN_LEN , &cache[QSFP_PN_OFFS ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"Rev:%.*s\n" , +QSFP_REV_LEN , &cache[QSFP_REV_OFFS ]); + +if (QSFP_IS_CU (cache[QSFP_MOD_TECH_OFFS ])) + sofar +=scnprintf (buf + sofar, len - sofar, +"Atten:%d, %d\n" , +QSFP_ATTEN_SDR (atten), +QSFP_ATTEN_DDR (atten)); + + sofar +=scnprintf (buf + sofar, len - sofar,"Serial:%.*s\n" , +QSFP_SN_LEN , &cache[QSFP_SN_OFFS ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"Date:%.*s\n" , +QSFP_DATE_LEN , &cache[QSFP_DATE_OFFS ]); + + sofar +=scnprintf (buf + sofar, len - sofar,"Lot:%.*s\n" , +QSFP_LOT_LEN , &cache[QSFP_LOT_OFFS ]); + +while (bidx <QSFP_DEFAULT_HDR_CNT ) { +int iidx; + +memcpy (bin_buff, &cache[bidx],QSFP_DUMP_CHUNK ); +for (iidx =0 ; iidx <QSFP_DUMP_CHUNK ; ++iidx) { + sofar +=scnprintf (buf + sofar, len-sofar, +" %02X" , bin_buff[iidx]); + } + sofar +=scnprintf (buf + sofar, len - sofar,"\n" ); + bidx +=QSFP_DUMP_CHUNK ; + } + } + ret = sofar; +return ret; +}
drivers/infiniband/hw/hfi1/qsfp.h new file +222 -0
222 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 27/41 IB/hfi1: add RC QP handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/rc.c | 2456 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 2456 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/rc.c
drivers/infiniband/hw/hfi1/rc.c new file +2456 -0
@@ -0,0 +1,2456 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/io.h> + +#include "hfi.h" +#include "qp.h" +#include "sdma.h" +#include "trace.h" + +/* cut down ridiculously long IB macro names */ +#define OP (x) IB_OPCODE_RC_##x + +static void rc_timeout (unsigned long arg); + +static u32 restart_sge (struct hfi1_sge_state *ss,struct hfi1_swqe *wqe, +u32 psn,u32 pmtu) +{ +u32 len; + + len =delta_psn (psn, wqe->psn) * pmtu; + ss->sge = wqe->sg_list[0 ]; + ss->sg_list = wqe->sg_list +1 ; + ss->num_sge = wqe->wr.num_sge; + ss->total_len = wqe->length; +hfi1_skip_sge (ss, len,0 ); +return wqe->length - len; +} + +static void start_timer (struct hfi1_qp *qp) +{ + qp->s_flags |=HFI1_S_TIMER ; + qp->s_timer.function = rc_timeout; +/* 4.096 usec. * (1 << qp->timeout) */ + qp->s_timer.expires = jiffies + qp->timeout_jiffies; +add_timer (&qp->s_timer); +} + +/** +* make_rc_ack - construct a response packet (ACK, NAK, or RDMA read) +* @dev: the device for this QP +* @qp: a pointer to the QP +* @ohdr: a pointer to the IB header being constructed +* @pmtu: the path MTU +* +* Return 1 if constructed; otherwise, return 0. +* Note that we are in the responder's side of the QP context. +* Note the QP s_lock must be held. +*/ +static int make_rc_ack (struct hfi1_ibdev *dev,struct hfi1_qp *qp, +struct hfi1_other_headers *ohdr,u32 pmtu) +{ +struct hfi1_ack_entry *e; +u32 hwords; +u32 len; +u32 bth0; +u32 bth2; +int middle =0 ; + +/* Don't send an ACK if we aren't supposed to. */ +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) + goto bail; + +/* header size in 32-bit words LRH+BTH = (8+12)/4. */ + hwords =5 ; + +switch (qp->s_ack_state) { +case OP (RDMA_READ_RESPONSE_LAST ): +case OP (RDMA_READ_RESPONSE_ONLY ): + e = &qp->s_ack_queue[qp->s_tail_ack_queue]; +if (e->rdma_sge.mr) { +hfi1_put_mr (e->rdma_sge.mr); + e->rdma_sge.mr =NULL ; + } +/* FALLTHROUGH */ +case OP (ATOMIC_ACKNOWLEDGE ): +/* +* We can increment the tail pointer now that the last +* response has been sent instead of only being +* constructed. +*/ +if (++qp->s_tail_ack_queue >HFI1_MAX_RDMA_ATOMIC ) + qp->s_tail_ack_queue =0 ; +/* FALLTHROUGH */ +case OP (SEND_ONLY ): +case OP (ACKNOWLEDGE ): +/* Check for no next entry in the queue. */ +if (qp->r_head_ack_queue == qp->s_tail_ack_queue) { +if (qp->s_flags &HFI1_S_ACK_PENDING ) + goto normal; + goto bail; + } + + e = &qp->s_ack_queue[qp->s_tail_ack_queue]; +if (e->opcode ==OP (RDMA_READ_REQUEST )) { +/* +* If a RDMA read response is being resent and +* we haven't seen the duplicate request yet, +* then stop sending the remaining responses the +* responder has seen until the requester re-sends it. +*/ + len = e->rdma_sge.sge_length; +if (len && !e->rdma_sge.mr) { + qp->s_tail_ack_queue = qp->r_head_ack_queue; + goto bail; + } +/* Copy SGE state in case we need to resend */ + qp->s_rdma_mr = e->rdma_sge.mr; +if (qp->s_rdma_mr) +hfi1_get_mr (qp->s_rdma_mr); + qp->s_ack_rdma_sge.sge = e->rdma_sge; + qp->s_ack_rdma_sge.num_sge =1 ; + qp->s_cur_sge = &qp->s_ack_rdma_sge; +if (len > pmtu) { + len = pmtu; + qp->s_ack_state =OP (RDMA_READ_RESPONSE_FIRST ); + }else { + qp->s_ack_state =OP (RDMA_READ_RESPONSE_ONLY ); + e->sent =1 ; + } + ohdr->u.aeth =hfi1_compute_aeth (qp); + hwords++; + qp->s_ack_rdma_psn = e->psn; + bth2 =mask_psn (qp->s_ack_rdma_psn++); + }else { +/* COMPARE_SWAP or FETCH_ADD */ + qp->s_cur_sge =NULL ; + len =0 ; + qp->s_ack_state =OP (ATOMIC_ACKNOWLEDGE ); + ohdr->u.at.aeth =hfi1_compute_aeth (qp); + ohdr->u.at.atomic_ack_eth[0 ] = +cpu_to_be32 (e->atomic_data >>32 ); + ohdr->u.at.atomic_ack_eth[1 ] = +cpu_to_be32 (e->atomic_data); + hwords +=sizeof (ohdr->u.at) /sizeof (u32); + bth2 =mask_psn (e->psn); + e->sent =1 ; + } + bth0 = qp->s_ack_state <<24 ; +break ; + +case OP (RDMA_READ_RESPONSE_FIRST ): + qp->s_ack_state =OP (RDMA_READ_RESPONSE_MIDDLE ); +/* FALLTHROUGH */ +case OP (RDMA_READ_RESPONSE_MIDDLE ): + qp->s_cur_sge = &qp->s_ack_rdma_sge; + qp->s_rdma_mr = qp->s_ack_rdma_sge.sge.mr; +if (qp->s_rdma_mr) +hfi1_get_mr (qp->s_rdma_mr); + len = qp->s_ack_rdma_sge.sge.sge_length; +if (len > pmtu) { + len = pmtu; + middle =HFI1_CAP_IS_KSET (SDMA_AHG ); + }else { + ohdr->u.aeth =hfi1_compute_aeth (qp); + hwords++; + qp->s_ack_state =OP (RDMA_READ_RESPONSE_LAST ); + e = &qp->s_ack_queue[qp->s_tail_ack_queue]; + e->sent =1 ; + } + bth0 = qp->s_ack_state <<24 ; + bth2 =mask_psn (qp->s_ack_rdma_psn++); +break ; + +default : +normal: +/* +* Send a regular ACK. +* Set the s_ack_state so we wait until after sending +* the ACK before setting s_ack_state to ACKNOWLEDGE +* (see above). +*/ + qp->s_ack_state =OP (SEND_ONLY ); + qp->s_flags &= ~HFI1_S_ACK_PENDING ; + qp->s_cur_sge =NULL ; +if (qp->s_nak_state) + ohdr->u.aeth = +cpu_to_be32 ((qp->r_msn &HFI1_MSN_MASK ) | + (qp->s_nak_state << +HFI1_AETH_CREDIT_SHIFT )); +else + ohdr->u.aeth =hfi1_compute_aeth (qp); + hwords++; + len =0 ; + bth0 =OP (ACKNOWLEDGE ) <<24 ; + bth2 =mask_psn (qp->s_ack_psn); + } + qp->s_rdma_ack_cnt++; + qp->s_hdrwords = hwords; + qp->s_cur_size = len; +hfi1_make_ruc_header (qp, ohdr, bth0, bth2, middle); +return 1 ; + +bail: + qp->s_ack_state =OP (ACKNOWLEDGE ); + qp->s_flags &= ~(HFI1_S_RESP_PENDING + |HFI1_S_ACK_PENDING + |HFI1_S_AHG_VALID ); +return 0 ; +} + +/** +* hfi1_make_rc_req - construct a request packet (SEND, RDMA r/w, ATOMIC) +* @qp: a pointer to the QP +* +* Return 1 if constructed; otherwise, return 0. +*/ +int hfi1_make_rc_req (struct hfi1_qp *qp) +{ +struct hfi1_ibdev *dev =to_idev (qp->ibqp.device); +struct hfi1_other_headers *ohdr; +struct hfi1_sge_state *ss; +struct hfi1_swqe *wqe; +/* header size in 32-bit words LRH+BTH = (8+12)/4. */ +u32 hwords =5 ; +u32 len; +u32 bth0 =0 ; +u32 bth2; +u32 pmtu = qp->pmtu; +char newreq; +unsigned long flags; +int ret =0 ; +int middle =0 ; +int delta; + + ohdr = &qp->s_hdr->ibh.u.oth; +if (qp->remote_ah_attr.ah_flags &IB_AH_GRH ) + ohdr = &qp->s_hdr->ibh.u.l.oth; + +/* +* The lock is needed to synchronize between the sending tasklet, +* the receive interrupt handler, and timeout re-sends. +*/ +spin_lock_irqsave (&qp->s_lock, flags); + +/* Sending responses has higher priority over sending requests. */ +if ((qp->s_flags &HFI1_S_RESP_PENDING ) && +make_rc_ack (dev, qp, ohdr, pmtu)) + goto done; + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_SEND_OK )) { +if (!(ib_hfi1_state_ops[qp->state] &HFI1_FLUSH_SEND )) + goto bail; +/* We are in the error state, flush the work request. */ +if (qp->s_last == qp->s_head) + goto bail; +/* If DMAs are in progress, we can't flush immediately. */ +if (atomic_read (&qp->s_iowait.sdma_busy)) { + qp->s_flags |=HFI1_S_WAIT_DMA ; + goto bail; + } +clear_ahg (qp); + wqe =get_swqe_ptr (qp, qp->s_last); +hfi1_send_complete (qp, wqe, qp->s_last != qp->s_acked ? +IB_WC_SUCCESS :IB_WC_WR_FLUSH_ERR ); +/* will get called again */ + goto done; + } + +if (qp->s_flags & (HFI1_S_WAIT_RNR |HFI1_S_WAIT_ACK )) + goto bail; + +if (cmp_psn (qp->s_psn, qp->s_sending_hpsn) <=0 ) { +if (cmp_psn (qp->s_sending_psn, qp->s_sending_hpsn) <=0 ) { + qp->s_flags |=HFI1_S_WAIT_PSN ; + goto bail; + } + qp->s_sending_psn = qp->s_psn; + qp->s_sending_hpsn = qp->s_psn -1 ; + } + +/* Send a request. */ + wqe =get_swqe_ptr (qp, qp->s_cur); +switch (qp->s_state) { +default : +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_NEXT_SEND_OK )) + goto bail; +/* +* Resend an old request or start a new one. +* +* We keep track of the current SWQE so that +* we don't reset the "furthest progress" state +* if we need to back up. +*/ + newreq =0 ; +if (qp->s_cur == qp->s_tail) { +/* Check if send work queue is empty. */ +if (qp->s_tail == qp->s_head) { +clear_ahg (qp); + goto bail; + } +/* +* If a fence is requested, wait for previous +* RDMA read and atomic operations to finish. +*/ +if ((wqe->wr.send_flags &IB_SEND_FENCE ) && + qp->s_num_rd_atomic) { + qp->s_flags |=HFI1_S_WAIT_FENCE ; + goto bail; + } + wqe->psn = qp->s_next_psn; + newreq =1 ; + } +/* +* Note that we have to be careful not to modify the +* original work request since we may need to resend +* it. +*/ + len = wqe->length; + ss = &qp->s_sge; + bth2 =mask_psn (qp->s_psn); +switch (wqe->wr.opcode) { +case IB_WR_SEND : +case IB_WR_SEND_WITH_IMM : +/* If no credit, return. */ +if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT ) && +cmp_msn (wqe->ssn, qp->s_lsn +1 ) >0 ) { + qp->s_flags |=HFI1_S_WAIT_SSN_CREDIT ; + goto bail; + } + wqe->lpsn = wqe->psn; +if (len > pmtu) { + wqe->lpsn += (len -1 ) / pmtu; + qp->s_state =OP (SEND_FIRST ); + len = pmtu; +break ; + } +if (wqe->wr.opcode ==IB_WR_SEND ) + qp->s_state =OP (SEND_ONLY ); +else { + qp->s_state =OP (SEND_ONLY_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; + } +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + bth2 |=IB_BTH_REQ_ACK ; +if (++qp->s_cur == qp->s_size) + qp->s_cur =0 ; +break ; + +case IB_WR_RDMA_WRITE : +if (newreq && !(qp->s_flags &HFI1_S_UNLIMITED_CREDIT )) + qp->s_lsn++; +/* FALLTHROUGH */ +case IB_WR_RDMA_WRITE_WITH_IMM : +/* If no credit, return. */ +if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT ) && +cmp_msn (wqe->ssn, qp->s_lsn +1 ) >0 ) { + qp->s_flags |=HFI1_S_WAIT_SSN_CREDIT ; + goto bail; + } + ohdr->u.rc.reth.vaddr = +cpu_to_be64 (wqe->wr.wr.rdma.remote_addr); + ohdr->u.rc.reth.rkey = +cpu_to_be32 (wqe->wr.wr.rdma.rkey); + ohdr->u.rc.reth.length =cpu_to_be32 (len); + hwords +=sizeof (struct ib_reth ) /sizeof (u32); + wqe->lpsn = wqe->psn; +if (len > pmtu) { + wqe->lpsn += (len -1 ) / pmtu; + qp->s_state =OP (RDMA_WRITE_FIRST ); + len = pmtu; +break ; + } +if (wqe->wr.opcode ==IB_WR_RDMA_WRITE ) + qp->s_state =OP (RDMA_WRITE_ONLY ); +else { + qp->s_state = +OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE ); +/* Immediate data comes after RETH */ + ohdr->u.rc.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + } + bth2 |=IB_BTH_REQ_ACK ; +if (++qp->s_cur == qp->s_size) + qp->s_cur =0 ; +break ; + +case IB_WR_RDMA_READ : +/* +* Don't allow more operations to be started +* than the QP limits allow. +*/ +if (newreq) { +if (qp->s_num_rd_atomic >= + qp->s_max_rd_atomic) { + qp->s_flags |=HFI1_S_WAIT_RDMAR ; + goto bail; + } + qp->s_num_rd_atomic++; +if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT )) + qp->s_lsn++; +/* +* Adjust s_next_psn to count the +* expected number of responses. +*/ +if (len > pmtu) + qp->s_next_psn += (len -1 ) / pmtu; + wqe->lpsn = qp->s_next_psn++; + } + ohdr->u.rc.reth.vaddr = +cpu_to_be64 (wqe->wr.wr.rdma.remote_addr); + ohdr->u.rc.reth.rkey = +cpu_to_be32 (wqe->wr.wr.rdma.rkey); + ohdr->u.rc.reth.length =cpu_to_be32 (len); + qp->s_state =OP (RDMA_READ_REQUEST ); + hwords +=sizeof (ohdr->u.rc.reth) /sizeof (u32); + ss =NULL ; + len =0 ; + bth2 |=IB_BTH_REQ_ACK ; +if (++qp->s_cur == qp->s_size) + qp->s_cur =0 ; +break ; + +case IB_WR_ATOMIC_CMP_AND_SWP : +case IB_WR_ATOMIC_FETCH_AND_ADD : +/* +* Don't allow more operations to be started +* than the QP limits allow. +*/ +if (newreq) { +if (qp->s_num_rd_atomic >= + qp->s_max_rd_atomic) { + qp->s_flags |=HFI1_S_WAIT_RDMAR ; + goto bail; + } + qp->s_num_rd_atomic++; +if (!(qp->s_flags &HFI1_S_UNLIMITED_CREDIT )) + qp->s_lsn++; + wqe->lpsn = wqe->psn; + } +if (wqe->wr.opcode ==IB_WR_ATOMIC_CMP_AND_SWP ) { + qp->s_state =OP (COMPARE_SWAP ); + ohdr->u.atomic_eth.swap_data =cpu_to_be64 ( + wqe->wr.wr.atomic.swap); + ohdr->u.atomic_eth.compare_data =cpu_to_be64 ( + wqe->wr.wr.atomic.compare_add); + }else { + qp->s_state =OP (FETCH_ADD ); + ohdr->u.atomic_eth.swap_data =cpu_to_be64 ( + wqe->wr.wr.atomic.compare_add); + ohdr->u.atomic_eth.compare_data =0 ; + } + ohdr->u.atomic_eth.vaddr[0 ] =cpu_to_be32 ( + wqe->wr.wr.atomic.remote_addr >>32 ); + ohdr->u.atomic_eth.vaddr[1 ] =cpu_to_be32 ( + wqe->wr.wr.atomic.remote_addr); + ohdr->u.atomic_eth.rkey =cpu_to_be32 ( + wqe->wr.wr.atomic.rkey); + hwords +=sizeof (struct ib_atomic_eth ) /sizeof (u32); + ss =NULL ; + len =0 ; + bth2 |=IB_BTH_REQ_ACK ; +if (++qp->s_cur == qp->s_size) + qp->s_cur =0 ; +break ; + +default : + goto bail; + } + qp->s_sge.sge = wqe->sg_list[0 ]; + qp->s_sge.sg_list = wqe->sg_list +1 ; + qp->s_sge.num_sge = wqe->wr.num_sge; + qp->s_sge.total_len = wqe->length; + qp->s_len = wqe->length; +if (newreq) { + qp->s_tail++; +if (qp->s_tail >= qp->s_size) + qp->s_tail =0 ; + } +if (wqe->wr.opcode ==IB_WR_RDMA_READ ) + qp->s_psn = wqe->lpsn +1 ; +else { + qp->s_psn++; +if (cmp_psn (qp->s_psn, qp->s_next_psn) >0 ) + qp->s_next_psn = qp->s_psn; + } +break ; + +case OP (RDMA_READ_RESPONSE_FIRST ): +/* +* qp->s_state is normally set to the opcode of the +* last packet constructed for new requests and therefore +* is never set to RDMA read response. +* RDMA_READ_RESPONSE_FIRST is used by the ACK processing +* thread to indicate a SEND needs to be restarted from an +* earlier PSN without interfering with the sending thread. +* See restart_rc(). +*/ + qp->s_len =restart_sge (&qp->s_sge, wqe, qp->s_psn, pmtu); +/* FALLTHROUGH */ +case OP (SEND_FIRST ): + qp->s_state =OP (SEND_MIDDLE ); +/* FALLTHROUGH */ +case OP (SEND_MIDDLE ): + bth2 =mask_psn (qp->s_psn++); +if (cmp_psn (qp->s_psn, qp->s_next_psn) >0 ) + qp->s_next_psn = qp->s_psn; + ss = &qp->s_sge; + len = qp->s_len; +if (len > pmtu) { + len = pmtu; + middle =HFI1_CAP_IS_KSET (SDMA_AHG ); +break ; + } +if (wqe->wr.opcode ==IB_WR_SEND ) + qp->s_state =OP (SEND_LAST ); +else { + qp->s_state =OP (SEND_LAST_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; + } +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + bth2 |=IB_BTH_REQ_ACK ; + qp->s_cur++; +if (qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + +case OP (RDMA_READ_RESPONSE_LAST ): +/* +* qp->s_state is normally set to the opcode of the +* last packet constructed for new requests and therefore +* is never set to RDMA read response. +* RDMA_READ_RESPONSE_LAST is used by the ACK processing +* thread to indicate a RDMA write needs to be restarted from +* an earlier PSN without interfering with the sending thread. +* See restart_rc(). +*/ + qp->s_len =restart_sge (&qp->s_sge, wqe, qp->s_psn, pmtu); +/* FALLTHROUGH */ +case OP (RDMA_WRITE_FIRST ): + qp->s_state =OP (RDMA_WRITE_MIDDLE ); +/* FALLTHROUGH */ +case OP (RDMA_WRITE_MIDDLE ): + bth2 =mask_psn (qp->s_psn++); +if (cmp_psn (qp->s_psn, qp->s_next_psn) >0 ) + qp->s_next_psn = qp->s_psn; + ss = &qp->s_sge; + len = qp->s_len; +if (len > pmtu) { + len = pmtu; + middle =HFI1_CAP_IS_KSET (SDMA_AHG ); +break ; + } +if (wqe->wr.opcode ==IB_WR_RDMA_WRITE ) + qp->s_state =OP (RDMA_WRITE_LAST ); +else { + qp->s_state =OP (RDMA_WRITE_LAST_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + } + bth2 |=IB_BTH_REQ_ACK ; + qp->s_cur++; +if (qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + +case OP (RDMA_READ_RESPONSE_MIDDLE ): +/* +* qp->s_state is normally set to the opcode of the +* last packet constructed for new requests and therefore +* is never set to RDMA read response. +* RDMA_READ_RESPONSE_MIDDLE is used by the ACK processing +* thread to indicate a RDMA read needs to be restarted from +* an earlier PSN without interfering with the sending thread. +* See restart_rc(). +*/ + len = (delta_psn (qp->s_psn, wqe->psn)) * pmtu; + ohdr->u.rc.reth.vaddr = +cpu_to_be64 (wqe->wr.wr.rdma.remote_addr + len); + ohdr->u.rc.reth.rkey = +cpu_to_be32 (wqe->wr.wr.rdma.rkey); + ohdr->u.rc.reth.length =cpu_to_be32 (wqe->length - len); + qp->s_state =OP (RDMA_READ_REQUEST ); + hwords +=sizeof (ohdr->u.rc.reth) /sizeof (u32); + bth2 =mask_psn (qp->s_psn) |IB_BTH_REQ_ACK ; + qp->s_psn = wqe->lpsn +1 ; + ss =NULL ; + len =0 ; + qp->s_cur++; +if (qp->s_cur == qp->s_size) + qp->s_cur =0 ; +break ; + } + qp->s_sending_hpsn = bth2; + delta =delta_psn (bth2, wqe->psn); +if (delta && delta %HFI1_PSN_CREDIT ==0 ) + bth2 |=IB_BTH_REQ_ACK ; +if (qp->s_flags &HFI1_S_SEND_ONE ) { + qp->s_flags &= ~HFI1_S_SEND_ONE ; + qp->s_flags |=HFI1_S_WAIT_ACK ; + bth2 |=IB_BTH_REQ_ACK ; + } + qp->s_len -= len; + qp->s_hdrwords = hwords; + qp->s_cur_sge = ss; + qp->s_cur_size = len; +hfi1_make_ruc_header ( + qp, + ohdr, + bth0 | (qp->s_state <<24 ), + bth2, + middle); +done: + ret =1 ; + goto unlock; + +bail: + qp->s_flags &= ~HFI1_S_BUSY ; +unlock: +spin_unlock_irqrestore (&qp->s_lock, flags); +return ret; +} + +/** +* hfi1_send_rc_ack - Construct an ACK packet and send it +* @qp: a pointer to the QP +* +* This is called from hfi1_rc_rcv() and handle_receive_interrupt(). +* Note that RDMA reads and atomics are handled in the +* send side QP state and tasklet. +*/ +void hfi1_send_rc_ack (struct hfi1_ctxtdata *rcd,struct hfi1_qp *qp, +int is_fecn) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +u64 pbc, pbc_flags =0 ; +u16 lrh0; +u16 sc5; +u32 bth0; +u32 hwords; +u32 vl, plen; +struct send_context *sc; +struct pio_buf *pbuf; +struct hfi1_ib_header hdr; +struct hfi1_other_headers *ohdr; +unsigned long flags; + +spin_lock_irqsave (&qp->s_lock, flags); + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) + goto unlock; + +/* Don't send ACK or NAK if a RDMA read or atomic is pending. */ +if ((qp->s_flags &HFI1_S_RESP_PENDING ) || qp->s_rdma_ack_cnt) + goto queue_ack; + +/* Construct the header with s_lock held so APM doesn't change it. */ +/* header size in 32-bit words LRH+BTH+AETH = (8+12+4)/4 */ + hwords =6 ; +if (unlikely (qp->remote_ah_attr.ah_flags &IB_AH_GRH )) { + hwords +=hfi1_make_grh (ibp, &hdr.u.l.grh, + &qp->remote_ah_attr.grh, hwords,0 ); + ohdr = &hdr.u.l.oth; + lrh0 =HFI1_LRH_GRH ; + }else { + ohdr = &hdr.u.oth; + lrh0 =HFI1_LRH_BTH ; + } +/* read pkey_index w/o lock (its atomic) */ + bth0 =hfi1_get_pkey (ibp, qp->s_pkey_index) | (OP (ACKNOWLEDGE ) <<24 ); +if (qp->s_mig_state ==IB_MIG_MIGRATED ) + bth0 |=IB_BTH_MIG_REQ ; +if (qp->r_nak_state) + ohdr->u.aeth =cpu_to_be32 ((qp->r_msn &HFI1_MSN_MASK ) | + (qp->r_nak_state << +HFI1_AETH_CREDIT_SHIFT )); +else + ohdr->u.aeth =hfi1_compute_aeth (qp); + sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; +/* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */ + pbc_flags |= ((!!(sc5 &0x10 )) <<PBC_DC_INFO_SHIFT ); + lrh0 |= (sc5 &0xf ) <<12 | (qp->remote_ah_attr.sl &0xf ) <<4 ; + hdr.lrh[0 ] =cpu_to_be16 (lrh0); + hdr.lrh[1 ] =cpu_to_be16 (qp->remote_ah_attr.dlid); + hdr.lrh[2 ] =cpu_to_be16 (hwords +SIZE_OF_CRC ); + hdr.lrh[3 ] =cpu_to_be16 (ppd->lid | qp->remote_ah_attr.src_path_bits); + ohdr->bth[0 ] =cpu_to_be32 (bth0); + ohdr->bth[1 ] =cpu_to_be32 (qp->remote_qpn); + ohdr->bth[1 ] |=cpu_to_be32 ((!!is_fecn) <<HFI1_BECN_SHIFT ); + ohdr->bth[2 ] =cpu_to_be32 (mask_psn (qp->r_ack_psn)); + +spin_unlock_irqrestore (&qp->s_lock, flags); + +/* Don't try to send ACKs if the link isn't ACTIVE */ +if (driver_lstate (ppd) !=IB_PORT_ACTIVE ) + goto done; + + sc = rcd->sc; + plen =2 /* PBC */ + hwords; + vl =sc_to_vlt (ppd->dd, sc5); + pbc =create_pbc (ppd, pbc_flags, qp->srate_mbps, vl, plen); + + pbuf =sc_buffer_alloc (sc, plen,NULL ,NULL ); +if (!pbuf) { +/* +* We have no room to send at the moment. Pass +* responsibility for sending the ACK to the send tasklet +* so that when enough buffer space becomes available, +* the ACK is sent ahead of other outgoing packets. +*/ +spin_lock_irqsave (&qp->s_lock, flags); + goto queue_ack; + } + +trace_output_ibhdr (dd_from_ibdev (qp->ibqp.device), &hdr); + +/* write the pbc and data */ + ppd->dd->pio_inline_send (ppd->dd, pbuf, pbc, &hdr, hwords); + + goto done; + +queue_ack: +if (ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK ) { +this_cpu_inc (*ibp->rc_qacks); + qp->s_flags |=HFI1_S_ACK_PENDING |HFI1_S_RESP_PENDING ; + qp->s_nak_state = qp->r_nak_state; + qp->s_ack_psn = qp->r_ack_psn; +if (is_fecn) +set_bit (HFI1_S_ECN , &qp->s_aflags); + +/* Schedule the send tasklet. */ +hfi1_schedule_send (qp); + } +unlock: +spin_unlock_irqrestore (&qp->s_lock, flags); +done: +return ; +} + +/** +* reset_psn - reset the QP state to send starting from PSN +* @qp: the QP +* @psn: the packet sequence number to restart at +* +* This is called from hfi1_rc_rcv() to process an incoming RC ACK +* for the given QP. +* Called at interrupt level with the QP s_lock held. +*/ +static void reset_psn (struct hfi1_qp *qp,u32 psn) +{ +u32 n = qp->s_acked; +struct hfi1_swqe *wqe =get_swqe_ptr (qp, n); +u32 opcode; + + qp->s_cur = n; + +/* +* If we are starting the request from the beginning, +* let the normal send code handle initialization. +*/ +if (cmp_psn (psn, wqe->psn) <=0 ) { + qp->s_state =OP (SEND_LAST ); + goto done; + } + +/* Find the work request opcode corresponding to the given PSN. */ + opcode = wqe->wr.opcode; +for (;;) { +int diff; + +if (++n == qp->s_size) + n =0 ; +if (n == qp->s_tail) +break ; + wqe =get_swqe_ptr (qp, n); + diff =cmp_psn (psn, wqe->psn); +if (diff <0 ) +break ; + qp->s_cur = n; +/* +* If we are starting the request from the beginning, +* let the normal send code handle initialization. +*/ +if (diff ==0 ) { + qp->s_state =OP (SEND_LAST ); + goto done; + } + opcode = wqe->wr.opcode; + } + +/* +* Set the state to restart in the middle of a request. +* Don't change the s_sge, s_cur_sge, or s_cur_size. +* See hfi1_make_rc_req(). +*/ +switch (opcode) { +case IB_WR_SEND : +case IB_WR_SEND_WITH_IMM : + qp->s_state =OP (RDMA_READ_RESPONSE_FIRST ); +break ; + +case IB_WR_RDMA_WRITE : +case IB_WR_RDMA_WRITE_WITH_IMM : + qp->s_state =OP (RDMA_READ_RESPONSE_LAST ); +break ; + +case IB_WR_RDMA_READ : + qp->s_state =OP (RDMA_READ_RESPONSE_MIDDLE ); +break ; + +default : +/* +* This case shouldn't happen since its only +* one PSN per req. +*/ + qp->s_state =OP (SEND_LAST ); + } +done: + qp->s_psn = psn; +/* +* Set HFI1_S_WAIT_PSN as rc_complete() may start the timer +* asynchronously before the send tasklet can get scheduled. +* Doing it in hfi1_make_rc_req() is too late. +*/ +if ((cmp_psn (qp->s_psn, qp->s_sending_hpsn) <=0 ) && + (cmp_psn (qp->s_sending_psn, qp->s_sending_hpsn) <=0 )) + qp->s_flags |=HFI1_S_WAIT_PSN ; + qp->s_flags &= ~HFI1_S_AHG_VALID ; +} + +/* +* Back up requester to resend the last un-ACKed request. +* The QP r_lock and s_lock should be held and interrupts disabled. +*/ +static void restart_rc (struct hfi1_qp *qp,u32 psn,int wait) +{ +struct hfi1_swqe *wqe =get_swqe_ptr (qp, qp->s_acked); +struct hfi1_ibport *ibp; + +if (qp->s_retry ==0 ) { +if (qp->s_mig_state ==IB_MIG_ARMED ) { +hfi1_migrate_qp (qp); + qp->s_retry = qp->s_retry_cnt; + }else if (qp->s_last == qp->s_acked) { +hfi1_send_complete (qp, wqe,IB_WC_RETRY_EXC_ERR ); +hfi1_error_qp (qp,IB_WC_WR_FLUSH_ERR ); +return ; + }else /* need to handle delayed completion */ +return ; + }else + qp->s_retry--; + + ibp =to_iport (qp->ibqp.device, qp->port_num); +if (wqe->wr.opcode ==IB_WR_RDMA_READ ) + ibp->n_rc_resends++; +else + ibp->n_rc_resends +=delta_psn (qp->s_psn, psn); + + qp->s_flags &= ~(HFI1_S_WAIT_FENCE |HFI1_S_WAIT_RDMAR | +HFI1_S_WAIT_SSN_CREDIT |HFI1_S_WAIT_PSN | +HFI1_S_WAIT_ACK ); +if (wait) + qp->s_flags |=HFI1_S_SEND_ONE ; +reset_psn (qp, psn); +} + +/* +* This is called from s_timer for missing responses. +*/ +static void rc_timeout (unsigned long arg) +{ +struct hfi1_qp *qp = (struct hfi1_qp *)arg; +struct hfi1_ibport *ibp; +unsigned long flags; + +spin_lock_irqsave (&qp->r_lock, flags); +spin_lock (&qp->s_lock); +if (qp->s_flags &HFI1_S_TIMER ) { + ibp =to_iport (qp->ibqp.device, qp->port_num); + ibp->n_rc_timeouts++; + qp->s_flags &= ~HFI1_S_TIMER ; +del_timer (&qp->s_timer); +restart_rc (qp, qp->s_last_psn +1 ,1 ); +hfi1_schedule_send (qp); + } +spin_unlock (&qp->s_lock); +spin_unlock_irqrestore (&qp->r_lock, flags); +} + +/* +* This is called from s_timer for RNR timeouts. +*/ +void hfi1_rc_rnr_retry (unsigned long arg) +{ +struct hfi1_qp *qp = (struct hfi1_qp *)arg; +unsigned long flags; + +spin_lock_irqsave (&qp->s_lock, flags); +if (qp->s_flags &HFI1_S_WAIT_RNR ) { + qp->s_flags &= ~HFI1_S_WAIT_RNR ; +del_timer (&qp->s_timer); +hfi1_schedule_send (qp); + } +spin_unlock_irqrestore (&qp->s_lock, flags); +} + +/* +* Set qp->s_sending_psn to the next PSN after the given one. +* This would be psn+1 except when RDMA reads are present. +*/ +static void reset_sending_psn (struct hfi1_qp *qp,u32 psn) +{ +struct hfi1_swqe *wqe; +u32 n = qp->s_last; + +/* Find the work request corresponding to the given PSN. */ +for (;;) { + wqe =get_swqe_ptr (qp, n); +if (cmp_psn (psn, wqe->lpsn) <=0 ) { +if (wqe->wr.opcode ==IB_WR_RDMA_READ ) + qp->s_sending_psn = wqe->lpsn +1 ; +else + qp->s_sending_psn = psn +1 ; +break ; + } +if (++n == qp->s_size) + n =0 ; +if (n == qp->s_tail) +break ; + } +} + +/* +* This should be called with the QP s_lock held and interrupts disabled. +*/ +void hfi1_rc_send_complete (struct hfi1_qp *qp,struct hfi1_ib_header *hdr) +{ +struct hfi1_other_headers *ohdr; +struct hfi1_swqe *wqe; +struct ib_wc wc; +unsigned i; +u32 opcode; +u32 psn; + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_OR_FLUSH_SEND )) +return ; + +/* Find out where the BTH is */ +if ((be16_to_cpu (hdr->lrh[0 ]) &3 ) ==HFI1_LRH_BTH ) + ohdr = &hdr->u.oth; +else + ohdr = &hdr->u.l.oth; + + opcode =be32_to_cpu (ohdr->bth[0 ]) >>24 ; +if (opcode >=OP (RDMA_READ_RESPONSE_FIRST ) && + opcode <=OP (ATOMIC_ACKNOWLEDGE )) { +WARN_ON (!qp->s_rdma_ack_cnt); + qp->s_rdma_ack_cnt--; +return ; + } + + psn =be32_to_cpu (ohdr->bth[2 ]); +reset_sending_psn (qp, psn); + +/* +* Start timer after a packet requesting an ACK has been sent and +* there are still requests that haven't been acked. +*/ +if ((psn &IB_BTH_REQ_ACK ) && qp->s_acked != qp->s_tail && + !(qp->s_flags & + (HFI1_S_TIMER |HFI1_S_WAIT_RNR |HFI1_S_WAIT_PSN )) && + (ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) +start_timer (qp); + +while (qp->s_last != qp->s_acked) { + wqe =get_swqe_ptr (qp, qp->s_last); +if (cmp_psn (wqe->lpsn, qp->s_sending_psn) >=0 && +cmp_psn (qp->s_sending_psn, qp->s_sending_hpsn) <=0 ) +break ; +for (i =0 ; i < wqe->wr.num_sge; i++) { +struct hfi1_sge *sge = &wqe->sg_list[i]; + +hfi1_put_mr (sge->mr); + } +/* Post a send completion queue entry if requested. */ +if (!(qp->s_flags &HFI1_S_SIGNAL_REQ_WR ) || + (wqe->wr.send_flags &IB_SEND_SIGNALED )) { +memset (&wc,0 ,sizeof (wc)); + wc.wr_id = wqe->wr.wr_id; + wc.status =IB_WC_SUCCESS ; + wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode]; + wc.byte_len = wqe->length; + wc.qp = &qp->ibqp; +hfi1_cq_enter (to_icq (qp->ibqp.send_cq), &wc,0 ); + } +if (++qp->s_last >= qp->s_size) + qp->s_last =0 ; + } +/* +* If we were waiting for sends to complete before re-sending, +* and they are now complete, restart sending. +*/ +trace_hfi1_rc_sendcomplete (qp, psn); +if (qp->s_flags &HFI1_S_WAIT_PSN && +cmp_psn (qp->s_sending_psn, qp->s_sending_hpsn) >0 ) { + qp->s_flags &= ~HFI1_S_WAIT_PSN ; + qp->s_sending_psn = qp->s_psn; + qp->s_sending_hpsn = qp->s_psn -1 ; +hfi1_schedule_send (qp); + } +} + +static inline void update_last_psn (struct hfi1_qp *qp,u32 psn) +{ + qp->s_last_psn = psn; +} + +/* +* Generate a SWQE completion. +* This is similar to hfi1_send_complete but has to check to be sure +* that the SGEs are not being referenced if the SWQE is being resent. +*/ +static struct hfi1_swqe *do_rc_completion (struct hfi1_qp *qp, +struct hfi1_swqe *wqe, +struct hfi1_ibport *ibp) +{ +struct ib_wc wc; +unsigned i; + +/* +* Don't decrement refcount and don't generate a +* completion if the SWQE is being resent until the send +* is finished. +*/ +if (cmp_psn (wqe->lpsn, qp->s_sending_psn) <0 || +cmp_psn (qp->s_sending_psn, qp->s_sending_hpsn) >0 ) { +for (i =0 ; i < wqe->wr.num_sge; i++) { +struct hfi1_sge *sge = &wqe->sg_list[i]; + +hfi1_put_mr (sge->mr); + } +/* Post a send completion queue entry if requested. */ +if (!(qp->s_flags &HFI1_S_SIGNAL_REQ_WR ) || + (wqe->wr.send_flags &IB_SEND_SIGNALED )) { +memset (&wc,0 ,sizeof (wc)); + wc.wr_id = wqe->wr.wr_id; + wc.status =IB_WC_SUCCESS ; + wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode]; + wc.byte_len = wqe->length; + wc.qp = &qp->ibqp; +hfi1_cq_enter (to_icq (qp->ibqp.send_cq), &wc,0 ); + } +if (++qp->s_last >= qp->s_size) + qp->s_last =0 ; + }else { +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); + +this_cpu_inc (*ibp->rc_delayed_comp); +/* +* If send progress not running attempt to progress +* SDMA queue. +*/ +if (ppd->dd->flags &HFI1_HAS_SEND_DMA ) { +struct sdma_engine *engine; +u8 sc5; + +/* For now use sc to find engine */ + sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + engine =qp_to_sdma_engine (qp, sc5); +sdma_engine_progress_schedule (engine); + } + } + + qp->s_retry = qp->s_retry_cnt; +update_last_psn (qp, wqe->lpsn); + +/* +* If we are completing a request which is in the process of +* being resent, we can stop re-sending it since we know the +* responder has already seen it. +*/ +if (qp->s_acked == qp->s_cur) { +if (++qp->s_cur >= qp->s_size) + qp->s_cur =0 ; + qp->s_acked = qp->s_cur; + wqe =get_swqe_ptr (qp, qp->s_cur); +if (qp->s_acked != qp->s_tail) { + qp->s_state =OP (SEND_LAST ); + qp->s_psn = wqe->psn; + } + }else { +if (++qp->s_acked >= qp->s_size) + qp->s_acked =0 ; +if (qp->state ==IB_QPS_SQD && qp->s_acked == qp->s_cur) + qp->s_draining =0 ; + wqe =get_swqe_ptr (qp, qp->s_acked); + } +return wqe; +} + +/** +* do_rc_ack - process an incoming RC ACK +* @qp: the QP the ACK came in on +* @psn: the packet sequence number of the ACK +* @opcode: the opcode of the request that resulted in the ACK +* +* This is called from rc_rcv_resp() to process an incoming RC ACK +* for the given QP. +* Called at interrupt level with the QP s_lock held. +* Returns 1 if OK, 0 if current operation should be aborted (NAK). +*/ +static int do_rc_ack (struct hfi1_qp *qp,u32 aeth,u32 psn,int opcode, +u64 val,struct hfi1_ctxtdata *rcd) +{ +struct hfi1_ibport *ibp; +enum ib_wc_status status; +struct hfi1_swqe *wqe; +int ret =0 ; +u32 ack_psn; +int diff; + +/* Remove QP from retry timer */ +if (qp->s_flags & (HFI1_S_TIMER |HFI1_S_WAIT_RNR )) { + qp->s_flags &= ~(HFI1_S_TIMER |HFI1_S_WAIT_RNR ); +del_timer (&qp->s_timer); + } + +/* +* Note that NAKs implicitly ACK outstanding SEND and RDMA write +* requests and implicitly NAK RDMA read and atomic requests issued +* before the NAK'ed request. The MSN won't include the NAK'ed +* request but will include an ACK'ed request(s). +*/ + ack_psn = psn; +if (aeth >>29 ) + ack_psn--; + wqe =get_swqe_ptr (qp, qp->s_acked); + ibp =to_iport (qp->ibqp.device, qp->port_num); + +/* +* The MSN might be for a later WQE than the PSN indicates so +* only complete WQEs that the PSN finishes. +*/ +while ((diff =delta_psn (ack_psn, wqe->lpsn)) >=0 ) { +/* +* RDMA_READ_RESPONSE_ONLY is a special case since +* we want to generate completion events for everything +* before the RDMA read, copy the data, then generate +* the completion for the read. +*/ +if (wqe->wr.opcode ==IB_WR_RDMA_READ && + opcode ==OP (RDMA_READ_RESPONSE_ONLY ) && + diff ==0 ) { + ret =1 ; + goto bail; + } +/* +* If this request is a RDMA read or atomic, and the ACK is +* for a later operation, this ACK NAKs the RDMA read or +* atomic. In other words, only a RDMA_READ_LAST or ONLY +* can ACK a RDMA read and likewise for atomic ops. Note +* that the NAK case can only happen if relaxed ordering is +* used and requests are sent after an RDMA read or atomic +* is sent but before the response is received. +*/ +if ((wqe->wr.opcode ==IB_WR_RDMA_READ && + (opcode !=OP (RDMA_READ_RESPONSE_LAST ) || diff !=0 )) || + ((wqe->wr.opcode ==IB_WR_ATOMIC_CMP_AND_SWP || + wqe->wr.opcode ==IB_WR_ATOMIC_FETCH_AND_ADD ) && + (opcode !=OP (ATOMIC_ACKNOWLEDGE ) || diff !=0 ))) { +/* Retry this request. */ +if (!(qp->r_flags &HFI1_R_RDMAR_SEQ )) { + qp->r_flags |=HFI1_R_RDMAR_SEQ ; +restart_rc (qp, qp->s_last_psn +1 ,0 ); +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_SEND ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, + &rcd->qp_wait_list); + } + } +/* +* No need to process the ACK/NAK since we are +* restarting an earlier request. +*/ + goto bail; + } +if (wqe->wr.opcode ==IB_WR_ATOMIC_CMP_AND_SWP || + wqe->wr.opcode ==IB_WR_ATOMIC_FETCH_AND_ADD ) { +u64 *vaddr = wqe->sg_list[0 ].vaddr; + *vaddr = val; + } +if (qp->s_num_rd_atomic && + (wqe->wr.opcode ==IB_WR_RDMA_READ || + wqe->wr.opcode ==IB_WR_ATOMIC_CMP_AND_SWP || + wqe->wr.opcode ==IB_WR_ATOMIC_FETCH_AND_ADD )) { + qp->s_num_rd_atomic--; +/* Restart sending task if fence is complete */ +if ((qp->s_flags &HFI1_S_WAIT_FENCE ) && + !qp->s_num_rd_atomic) { + qp->s_flags &= ~(HFI1_S_WAIT_FENCE | +HFI1_S_WAIT_ACK ); +hfi1_schedule_send (qp); + }else if (qp->s_flags &HFI1_S_WAIT_RDMAR ) { + qp->s_flags &= ~(HFI1_S_WAIT_RDMAR | +HFI1_S_WAIT_ACK ); +hfi1_schedule_send (qp); + } + } + wqe =do_rc_completion (qp, wqe, ibp); +if (qp->s_acked == qp->s_tail) +break ; + } + +switch (aeth >>29 ) { +case 0 :/* ACK */ +this_cpu_inc (*ibp->rc_acks); +if (qp->s_acked != qp->s_tail) { +/* +* We are expecting more ACKs so +* reset the re-transmit timer. +*/ +start_timer (qp); +/* +* We can stop re-sending the earlier packets and +* continue with the next packet the receiver wants. +*/ +if (cmp_psn (qp->s_psn, psn) <=0 ) +reset_psn (qp, psn +1 ); + }else if (cmp_psn (qp->s_psn, psn) <=0 ) { + qp->s_state =OP (SEND_LAST ); + qp->s_psn = psn +1 ; + } +if (qp->s_flags &HFI1_S_WAIT_ACK ) { + qp->s_flags &= ~HFI1_S_WAIT_ACK ; +hfi1_schedule_send (qp); + } +hfi1_get_credit (qp, aeth); + qp->s_rnr_retry = qp->s_rnr_retry_cnt; + qp->s_retry = qp->s_retry_cnt; +update_last_psn (qp, psn); + ret =1 ; + goto bail; + +case 1 :/* RNR NAK */ + ibp->n_rnr_naks++; +if (qp->s_acked == qp->s_tail) + goto bail; +if (qp->s_flags &HFI1_S_WAIT_RNR ) + goto bail; +if (qp->s_rnr_retry ==0 ) { + status =IB_WC_RNR_RETRY_EXC_ERR ; + goto class_b; + } +if (qp->s_rnr_retry_cnt <7 ) + qp->s_rnr_retry--; + +/* The last valid PSN is the previous PSN. */ +update_last_psn (qp, psn -1 ); + + ibp->n_rc_resends +=delta_psn (qp->s_psn, psn); + +reset_psn (qp, psn); + + qp->s_flags &= ~(HFI1_S_WAIT_SSN_CREDIT |HFI1_S_WAIT_ACK ); + qp->s_flags |=HFI1_S_WAIT_RNR ; + qp->s_timer.function = hfi1_rc_rnr_retry; + qp->s_timer.expires = jiffies +usecs_to_jiffies ( + ib_hfi1_rnr_table[(aeth >>HFI1_AETH_CREDIT_SHIFT ) & +HFI1_AETH_CREDIT_MASK ]); +add_timer (&qp->s_timer); + goto bail; + +case 3 :/* NAK */ +if (qp->s_acked == qp->s_tail) + goto bail; +/* The last valid PSN is the previous PSN. */ +update_last_psn (qp, psn -1 ); +switch ((aeth >>HFI1_AETH_CREDIT_SHIFT ) & +HFI1_AETH_CREDIT_MASK ) { +case 0 :/* PSN sequence error */ + ibp->n_seq_naks++; +/* +* Back up to the responder's expected PSN. +* Note that we might get a NAK in the middle of an +* RDMA READ response which terminates the RDMA +* READ. +*/ +restart_rc (qp, psn,0 ); +hfi1_schedule_send (qp); +break ; + +case 1 :/* Invalid Request */ + status =IB_WC_REM_INV_REQ_ERR ; + ibp->n_other_naks++; + goto class_b; + +case 2 :/* Remote Access Error */ + status =IB_WC_REM_ACCESS_ERR ; + ibp->n_other_naks++; + goto class_b; + +case 3 :/* Remote Operation Error */ + status =IB_WC_REM_OP_ERR ; + ibp->n_other_naks++; +class_b: +if (qp->s_last == qp->s_acked) { +hfi1_send_complete (qp, wqe, status); +hfi1_error_qp (qp,IB_WC_WR_FLUSH_ERR ); + } +break ; + +default : +/* Ignore other reserved NAK error codes */ + goto reserved; + } + qp->s_retry = qp->s_retry_cnt; + qp->s_rnr_retry = qp->s_rnr_retry_cnt; + goto bail; + +default :/* 2: reserved */ +reserved: +/* Ignore reserved NAK codes. */ + goto bail; + } + +bail: +return ret; +} + +/* +* We have seen an out of sequence RDMA read middle or last packet. +* This ACKs SENDs and RDMA writes up to the first RDMA read or atomic SWQE. +*/ +static void rdma_seq_err (struct hfi1_qp *qp,struct hfi1_ibport *ibp,u32 psn, +struct hfi1_ctxtdata *rcd) +{ +struct hfi1_swqe *wqe; + +/* Remove QP from retry timer */ +if (qp->s_flags & (HFI1_S_TIMER |HFI1_S_WAIT_RNR )) { + qp->s_flags &= ~(HFI1_S_TIMER |HFI1_S_WAIT_RNR ); +del_timer (&qp->s_timer); + } + + wqe =get_swqe_ptr (qp, qp->s_acked); + +while (cmp_psn (psn, wqe->lpsn) >0 ) { +if (wqe->wr.opcode ==IB_WR_RDMA_READ || + wqe->wr.opcode ==IB_WR_ATOMIC_CMP_AND_SWP || + wqe->wr.opcode ==IB_WR_ATOMIC_FETCH_AND_ADD ) +break ; + wqe =do_rc_completion (qp, wqe, ibp); + } + + ibp->n_rdma_seq++; + qp->r_flags |=HFI1_R_RDMAR_SEQ ; +restart_rc (qp, qp->s_last_psn +1 ,0 ); +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_SEND ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, &rcd->qp_wait_list); + } +} + +/** +* rc_rcv_resp - process an incoming RC response packet +* @ibp: the port this packet came in on +* @ohdr: the other headers for this packet +* @data: the packet data +* @tlen: the packet length +* @qp: the QP for this packet +* @opcode: the opcode for this packet +* @psn: the packet sequence number for this packet +* @hdrsize: the header length +* @pmtu: the path MTU +* +* This is called from hfi1_rc_rcv() to process an incoming RC response +* packet for the given QP. +* Called at interrupt level. +*/ +static void rc_rcv_resp (struct hfi1_ibport *ibp, +struct hfi1_other_headers *ohdr, +void *data,u32 tlen,struct hfi1_qp *qp, +u32 opcode,u32 psn,u32 hdrsize,u32 pmtu, +struct hfi1_ctxtdata *rcd) +{ +struct hfi1_swqe *wqe; +enum ib_wc_status status; +unsigned long flags; +int diff; +u32 pad; +u32 aeth; +u64 val; + +spin_lock_irqsave (&qp->s_lock, flags); +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) + goto ack_done; + +/* Ignore invalid responses. */ +if (cmp_psn (psn, qp->s_next_psn) >=0 ) + goto ack_done; + +/* Ignore duplicate responses. */ + diff =cmp_psn (psn, qp->s_last_psn); +if (unlikely (diff <=0 )) { +/* Update credits for "ghost" ACKs */ +if (diff ==0 && opcode ==OP (ACKNOWLEDGE )) { + aeth =be32_to_cpu (ohdr->u.aeth); +if ((aeth >>29 ) ==0 ) +hfi1_get_credit (qp, aeth); + } + goto ack_done; + } + +/* +* Skip everything other than the PSN we expect, if we are waiting +* for a reply to a restarted RDMA read or atomic op. +*/ +if (qp->r_flags &HFI1_R_RDMAR_SEQ ) { +if (cmp_psn (psn, qp->s_last_psn +1 ) !=0 ) + goto ack_done; + qp->r_flags &= ~HFI1_R_RDMAR_SEQ ; + } + +if (unlikely (qp->s_acked == qp->s_tail)) + goto ack_done; + wqe =get_swqe_ptr (qp, qp->s_acked); + status =IB_WC_SUCCESS ; + +switch (opcode) { +case OP (ACKNOWLEDGE ): +case OP (ATOMIC_ACKNOWLEDGE ): +case OP (RDMA_READ_RESPONSE_FIRST ): + aeth =be32_to_cpu (ohdr->u.aeth); +if (opcode ==OP (ATOMIC_ACKNOWLEDGE )) { +__be32 *p = ohdr->u.at.atomic_ack_eth; + + val = ((u64 )be32_to_cpu (p[0 ]) <<32 ) | +be32_to_cpu (p[1 ]); + }else + val =0 ; +if (!do_rc_ack (qp, aeth, psn, opcode, val, rcd) || + opcode !=OP (RDMA_READ_RESPONSE_FIRST )) + goto ack_done; + hdrsize +=4 ; + wqe =get_swqe_ptr (qp, qp->s_acked); +if (unlikely (wqe->wr.opcode !=IB_WR_RDMA_READ )) + goto ack_op_err; +/* +* If this is a response to a resent RDMA read, we +* have to be careful to copy the data to the right +* location. +*/ + qp->s_rdma_read_len =restart_sge (&qp->s_rdma_read_sge, + wqe, psn, pmtu); + goto read_middle; + +case OP (RDMA_READ_RESPONSE_MIDDLE ): +/* no AETH, no ACK */ +if (unlikely (cmp_psn (psn, qp->s_last_psn +1 ))) + goto ack_seq_err; +if (unlikely (wqe->wr.opcode !=IB_WR_RDMA_READ )) + goto ack_op_err; +read_middle: +if (unlikely (tlen != (hdrsize + pmtu +4 ))) + goto ack_len_err; +if (unlikely (pmtu >= qp->s_rdma_read_len)) + goto ack_len_err; + +/* +* We got a response so update the timeout. +* 4.096 usec. * (1 << qp->timeout) +*/ + qp->s_flags |=HFI1_S_TIMER ; +mod_timer (&qp->s_timer, jiffies + qp->timeout_jiffies); +if (qp->s_flags &HFI1_S_WAIT_ACK ) { + qp->s_flags &= ~HFI1_S_WAIT_ACK ; +hfi1_schedule_send (qp); + } + +if (opcode ==OP (RDMA_READ_RESPONSE_MIDDLE )) + qp->s_retry = qp->s_retry_cnt; + +/* +* Update the RDMA receive state but do the copy w/o +* holding the locks and blocking interrupts. +*/ + qp->s_rdma_read_len -= pmtu; +update_last_psn (qp, psn); +spin_unlock_irqrestore (&qp->s_lock, flags); +hfi1_copy_sge (&qp->s_rdma_read_sge, data, pmtu,0 ); + goto bail; + +case OP (RDMA_READ_RESPONSE_ONLY ): + aeth =be32_to_cpu (ohdr->u.aeth); +if (!do_rc_ack (qp, aeth, psn, opcode,0 , rcd)) + goto ack_done; +/* Get the number of bytes the message was padded by. */ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +/* +* Check that the data size is >= 0 && <= pmtu. +* Remember to account for the AETH header (4) and +* ICRC (4). +*/ +if (unlikely (tlen < (hdrsize + pad +8 ))) + goto ack_len_err; +/* +* If this is a response to a resent RDMA read, we +* have to be careful to copy the data to the right +* location. +*/ + wqe =get_swqe_ptr (qp, qp->s_acked); + qp->s_rdma_read_len =restart_sge (&qp->s_rdma_read_sge, + wqe, psn, pmtu); + goto read_last; + +case OP (RDMA_READ_RESPONSE_LAST ): +/* ACKs READ req. */ +if (unlikely (cmp_psn (psn, qp->s_last_psn +1 ))) + goto ack_seq_err; +if (unlikely (wqe->wr.opcode !=IB_WR_RDMA_READ )) + goto ack_op_err; +/* Get the number of bytes the message was padded by. */ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +/* +* Check that the data size is >= 1 && <= pmtu. +* Remember to account for the AETH header (4) and +* ICRC (4). +*/ +if (unlikely (tlen <= (hdrsize + pad +8 ))) + goto ack_len_err; +read_last: + tlen -= hdrsize + pad +8 ; +if (unlikely (tlen != qp->s_rdma_read_len)) + goto ack_len_err; + aeth =be32_to_cpu (ohdr->u.aeth); +hfi1_copy_sge (&qp->s_rdma_read_sge, data, tlen,0 ); +WARN_ON (qp->s_rdma_read_sge.num_sge); + (void )do_rc_ack (qp, aeth, psn, +OP (RDMA_READ_RESPONSE_LAST ),0 , rcd); + goto ack_done; + } + +ack_op_err: + status =IB_WC_LOC_QP_OP_ERR ; + goto ack_err; + +ack_seq_err: +rdma_seq_err (qp, ibp, psn, rcd); + goto ack_done; + +ack_len_err: + status =IB_WC_LOC_LEN_ERR ; +ack_err: +if (qp->s_last == qp->s_acked) { +hfi1_send_complete (qp, wqe, status); +hfi1_error_qp (qp,IB_WC_WR_FLUSH_ERR ); + } +ack_done: +spin_unlock_irqrestore (&qp->s_lock, flags); +bail: +return ; +} + +/** +* rc_rcv_error - process an incoming duplicate or error RC packet +* @ohdr: the other headers for this packet +* @data: the packet data +* @qp: the QP for this packet +* @opcode: the opcode for this packet +* @psn: the packet sequence number for this packet +* @diff: the difference between the PSN and the expected PSN +* +* This is called from hfi1_rc_rcv() to process an unexpected +* incoming RC packet for the given QP. +* Called at interrupt level. +* Return 1 if no more processing is needed; otherwise return 0 to +* schedule a response to be sent. +*/ +static int rc_rcv_error (struct hfi1_other_headers *ohdr,void *data, +struct hfi1_qp *qp,u32 opcode,u32 psn,int diff, +struct hfi1_ctxtdata *rcd) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +struct hfi1_ack_entry *e; +unsigned long flags; +u8 i, prev; +int old_req; + +if (diff >0 ) { +/* +* Packet sequence error. +* A NAK will ACK earlier sends and RDMA writes. +* Don't queue the NAK if we already sent one. +*/ +if (!qp->r_nak_state) { + ibp->n_rc_seqnak++; + qp->r_nak_state =IB_NAK_PSN_ERROR ; +/* Use the expected PSN. */ + qp->r_ack_psn = qp->r_psn; +/* +* Wait to send the sequence NAK until all packets +* in the receive queue have been processed. +* Otherwise, we end up propagating congestion. +*/ +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_NAK ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, &rcd->qp_wait_list); + } + } + goto done; + } + +/* +* Handle a duplicate request. Don't re-execute SEND, RDMA +* write or atomic op. Don't NAK errors, just silently drop +* the duplicate request. Note that r_sge, r_len, and +* r_rcv_len may be in use so don't modify them. +* +* We are supposed to ACK the earliest duplicate PSN but we +* can coalesce an outstanding duplicate ACK. We have to +* send the earliest so that RDMA reads can be restarted at +* the requester's expected PSN. +* +* First, find where this duplicate PSN falls within the +* ACKs previously sent. +* old_req is true if there is an older response that is scheduled +* to be sent before sending this one. +*/ + e =NULL ; + old_req =1 ; + ibp->n_rc_dupreq++; + +spin_lock_irqsave (&qp->s_lock, flags); + +for (i = qp->r_head_ack_queue; ; i = prev) { +if (i == qp->s_tail_ack_queue) + old_req =0 ; +if (i) + prev = i -1 ; +else + prev =HFI1_MAX_RDMA_ATOMIC ; +if (prev == qp->r_head_ack_queue) { + e =NULL ; +break ; + } + e = &qp->s_ack_queue[prev]; +if (!e->opcode) { + e =NULL ; +break ; + } +if (cmp_psn (psn, e->psn) >=0 ) { +if (prev == qp->s_tail_ack_queue && +cmp_psn (psn, e->lpsn) <=0 ) + old_req =0 ; +break ; + } + } +switch (opcode) { +case OP (RDMA_READ_REQUEST ): { +struct ib_reth *reth; +u32 offset; +u32 len; + +/* +* If we didn't find the RDMA read request in the ack queue, +* we can ignore this request. +*/ +if (!e || e->opcode !=OP (RDMA_READ_REQUEST )) + goto unlock_done; +/* RETH comes after BTH */ + reth = &ohdr->u.rc.reth; +/* +* Address range must be a subset of the original +* request and start on pmtu boundaries. +* We reuse the old ack_queue slot since the requester +* should not back up and request an earlier PSN for the +* same request. +*/ + offset =delta_psn (psn, e->psn) * qp->pmtu; + len =be32_to_cpu (reth->length); +if (unlikely (offset + len != e->rdma_sge.sge_length)) + goto unlock_done; +if (e->rdma_sge.mr) { +hfi1_put_mr (e->rdma_sge.mr); + e->rdma_sge.mr =NULL ; + } +if (len !=0 ) { +u32 rkey =be32_to_cpu (reth->rkey); +u64 vaddr =be64_to_cpu (reth->vaddr); +int ok; + + ok =hfi1_rkey_ok (qp, &e->rdma_sge, len, vaddr, rkey, +IB_ACCESS_REMOTE_READ ); +if (unlikely (!ok)) + goto unlock_done; + }else { + e->rdma_sge.vaddr =NULL ; + e->rdma_sge.length =0 ; + e->rdma_sge.sge_length =0 ; + } + e->psn = psn; +if (old_req) + goto unlock_done; + qp->s_tail_ack_queue = prev; +break ; + } + +case OP (COMPARE_SWAP ): +case OP (FETCH_ADD ): { +/* +* If we didn't find the atomic request in the ack queue +* or the send tasklet is already backed up to send an +* earlier entry, we can ignore this request. +*/ +if (!e || e->opcode != (u8 ) opcode || old_req) + goto unlock_done; + qp->s_tail_ack_queue = prev; +break ; + } + +default : +/* +* Ignore this operation if it doesn't request an ACK +* or an earlier RDMA read or atomic is going to be resent. +*/ +if (!(psn &IB_BTH_REQ_ACK ) || old_req) + goto unlock_done; +/* +* Resend the most recent ACK if this request is +* after all the previous RDMA reads and atomics. +*/ +if (i == qp->r_head_ack_queue) { +spin_unlock_irqrestore (&qp->s_lock, flags); + qp->r_nak_state =0 ; + qp->r_ack_psn = qp->r_psn -1 ; + goto send_ack; + } + +/* +* Resend the RDMA read or atomic op which +* ACKs this duplicate request. +*/ + qp->s_tail_ack_queue = i; +break ; + } + qp->s_ack_state =OP (ACKNOWLEDGE ); + qp->s_flags |=HFI1_S_RESP_PENDING ; + qp->r_nak_state =0 ; +hfi1_schedule_send (qp); + +unlock_done: +spin_unlock_irqrestore (&qp->s_lock, flags); +done: +return 1 ; + +send_ack: +return 0 ; +} + +void hfi1_rc_error (struct hfi1_qp *qp,enum ib_wc_status err) +{ +unsigned long flags; +int lastwqe; + +spin_lock_irqsave (&qp->s_lock, flags); + lastwqe =hfi1_error_qp (qp, err); +spin_unlock_irqrestore (&qp->s_lock, flags); + +if (lastwqe) { +struct ib_event ev; + + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_QP_LAST_WQE_REACHED ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); + } +} + +static inline void update_ack_queue (struct hfi1_qp *qp,unsigned n) +{ +unsigned next; + + next = n +1 ; +if (next >HFI1_MAX_RDMA_ATOMIC ) + next =0 ; + qp->s_tail_ack_queue = next; + qp->s_ack_state =OP (ACKNOWLEDGE ); +} + +static void log_cca_event (struct hfi1_pportdata *ppd,u8 sl,u32 rlid, +u32 lqpn,u32 rqpn,u8 svc_type) +{ +struct opa_hfi1_cong_log_event_internal *cc_event; + +if (sl >=OPA_MAX_SLS ) +return ; + +spin_lock (&ppd->cc_log_lock); + + ppd->threshold_cong_event_map[sl/8 ] |=1 << (sl %8 ); + ppd->threshold_event_counter++; + + cc_event = &ppd->cc_events[ppd->cc_log_idx++]; +if (ppd->cc_log_idx ==OPA_CONG_LOG_ELEMS ) + ppd->cc_log_idx =0 ; + cc_event->lqpn = lqpn &HFI1_QPN_MASK ; + cc_event->rqpn = rqpn &HFI1_QPN_MASK ; + cc_event->sl = sl; + cc_event->svc_type = svc_type; + cc_event->rlid = rlid; +/* keep timestamp in units of 1.024 usec */ + cc_event->timestamp =ktime_to_ns (ktime_get ()) /1024 ; + +spin_unlock (&ppd->cc_log_lock); +} + +void process_becn (struct hfi1_pportdata *ppd,u8 sl,u16 rlid,u32 lqpn, +u32 rqpn,u8 svc_type) +{ +struct cca_timer *cca_timer; +u16 ccti, ccti_incr, ccti_timer, ccti_limit; +u8 trigger_threshold; +struct cc_state *cc_state; + +if (sl >=OPA_MAX_SLS ) +return ; + + cca_timer = &ppd->cca_timer[sl]; + +rcu_read_lock (); + + cc_state =get_cc_state (ppd); + +if (cc_state ==NULL ) { +rcu_read_unlock (); +return ; + } + +/* +* 1) increase CCTI (for this SL) +* 2) select IPG (i.e., call set_link_ipg()) +* 3) start timer +*/ + ccti_limit = cc_state->cct.ccti_limit; + ccti_incr = cc_state->cong_setting.entries[sl].ccti_increase; + ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer; + trigger_threshold = + cc_state->cong_setting.entries[sl].trigger_threshold; + +spin_lock (&ppd->cca_timer_lock); + +if (cca_timer->ccti < ccti_limit) { +if (cca_timer->ccti + ccti_incr <= ccti_limit) + cca_timer->ccti += ccti_incr; +else + cca_timer->ccti = ccti_limit; +set_link_ipg (ppd); + } + +spin_unlock (&ppd->cca_timer_lock); + +rcu_read_unlock (); + + ccti = cca_timer->ccti; + +if (!hrtimer_active (&cca_timer->hrtimer)) { +/* ccti_timer is in units of 1.024 usec */ +unsigned long nsec =1024 * ccti_timer; + +hrtimer_start (&cca_timer->hrtimer,ns_to_ktime (nsec), +HRTIMER_MODE_REL ); + } + +if ((trigger_threshold !=0 ) && (ccti >= trigger_threshold)) +log_cca_event (ppd, sl, rlid, lqpn, rqpn, svc_type); +} + +/** +* hfi1_rc_rcv - process an incoming RC packet +* @rcd: the context pointer +* @hdr: the header of this packet +* @rcv_flags: flags relevant to rcv processing +* @data: the packet data +* @tlen: the packet length +* @qp: the QP for this packet +* +* This is called from qp_rcv() to process an incoming RC packet +* for the given QP. +* Called at interrupt level. +*/ +void hfi1_rc_rcv (struct hfi1_ctxtdata *rcd,struct hfi1_ib_header *hdr, +u32 rcv_flags,void *data,u32 tlen,struct hfi1_qp *qp) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +struct hfi1_other_headers *ohdr; +u32 bth0, opcode; +u32 hdrsize; +u32 psn; +u32 pad; +u8 sl; +struct ib_wc wc; +u32 pmtu = qp->pmtu; +int diff; +struct ib_reth *reth; +unsigned long flags; +int has_grh = rcv_flags &HFI1_HAS_GRH ; +int ret, is_becn, is_fecn; + +/* Check for GRH */ +if (!has_grh) { + ohdr = &hdr->u.oth; + hdrsize =8 +12 ;/* LRH + BTH */ + }else { + ohdr = &hdr->u.l.oth; + hdrsize =8 +40 +12 ;/* LRH + GRH + BTH */ + } + + sl = qp->remote_ah_attr.sl; + + bth0 =be32_to_cpu (ohdr->bth[0 ]); +if (hfi1_ruc_check_hdr (ibp, hdr, has_grh, qp, bth0)) +return ; + + is_becn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_BECN_SHIFT ) & +HFI1_BECN_MASK ; +if (is_becn) { +u16 rlid = qp->remote_ah_attr.dlid; +u32 lqpn, rqpn; + + lqpn = qp->ibqp.qp_num; + rqpn = qp->remote_qpn; +process_becn (ppd, sl, rlid, lqpn, rqpn,IB_CC_SVCTYPE_RC ); + } + + is_fecn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_FECN_SHIFT ) & +HFI1_FECN_MASK ; + + psn =be32_to_cpu (ohdr->bth[2 ]); + opcode = bth0 >>24 ; + +/* +* Process responses (ACKs) before anything else. Note that the +* packet sequence number will be for something in the send work +* queue rather than the expected receive packet sequence number. +* In other words, this QP is the requester. +*/ +if (opcode >=OP (RDMA_READ_RESPONSE_FIRST ) && + opcode <=OP (ATOMIC_ACKNOWLEDGE )) { +rc_rcv_resp (ibp, ohdr, data, tlen, qp, opcode, psn, + hdrsize, pmtu, rcd); +if (is_fecn) + goto send_ack; +return ; + } + +/* Compute 24 bits worth of difference. */ + diff =delta_psn (psn, qp->r_psn); +if (unlikely (diff)) { +if (rc_rcv_error (ohdr, data, qp, opcode, psn, diff, rcd)) +return ; + goto send_ack; + } + +/* Check for opcode sequence errors. */ +switch (qp->r_state) { +case OP (SEND_FIRST ): +case OP (SEND_MIDDLE ): +if (opcode ==OP (SEND_MIDDLE ) || + opcode ==OP (SEND_LAST ) || + opcode ==OP (SEND_LAST_WITH_IMMEDIATE )) +break ; + goto nack_inv; + +case OP (RDMA_WRITE_FIRST ): +case OP (RDMA_WRITE_MIDDLE ): +if (opcode ==OP (RDMA_WRITE_MIDDLE ) || + opcode ==OP (RDMA_WRITE_LAST ) || + opcode ==OP (RDMA_WRITE_LAST_WITH_IMMEDIATE )) +break ; + goto nack_inv; + +default : +if (opcode ==OP (SEND_MIDDLE ) || + opcode ==OP (SEND_LAST ) || + opcode ==OP (SEND_LAST_WITH_IMMEDIATE ) || + opcode ==OP (RDMA_WRITE_MIDDLE ) || + opcode ==OP (RDMA_WRITE_LAST ) || + opcode ==OP (RDMA_WRITE_LAST_WITH_IMMEDIATE )) + goto nack_inv; +/* +* Note that it is up to the requester to not send a new +* RDMA read or atomic operation before receiving an ACK +* for the previous operation. +*/ +break ; + } + +if (qp->state ==IB_QPS_RTR && !(qp->r_flags &HFI1_R_COMM_EST )) { + qp->r_flags |=HFI1_R_COMM_EST ; +if (qp->ibqp.event_handler) { +struct ib_event ev; + + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_COMM_EST ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); + } + } + +/* OK, process the packet. */ +switch (opcode) { +case OP (SEND_FIRST ): + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) + goto nack_op_err; +if (!ret) + goto rnr_nak; + qp->r_rcv_len =0 ; +/* FALLTHROUGH */ +case OP (SEND_MIDDLE ): +case OP (RDMA_WRITE_MIDDLE ): +send_middle: +/* Check for invalid length PMTU or posted rwqe len. */ +if (unlikely (tlen != (hdrsize + pmtu +4 ))) + goto nack_inv; + qp->r_rcv_len += pmtu; +if (unlikely (qp->r_rcv_len > qp->r_len)) + goto nack_inv; +hfi1_copy_sge (&qp->r_sge, data, pmtu,1 ); +break ; + +case OP (RDMA_WRITE_LAST_WITH_IMMEDIATE ): +/* consume RWQE */ + ret =hfi1_get_rwqe (qp,1 ); +if (ret <0 ) + goto nack_op_err; +if (!ret) + goto rnr_nak; + goto send_last_imm; + +case OP (SEND_ONLY ): +case OP (SEND_ONLY_WITH_IMMEDIATE ): + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) + goto nack_op_err; +if (!ret) + goto rnr_nak; + qp->r_rcv_len =0 ; +if (opcode ==OP (SEND_ONLY )) + goto no_immediate_data; +/* FALLTHROUGH for SEND_ONLY_WITH_IMMEDIATE */ +case OP (SEND_LAST_WITH_IMMEDIATE ): +send_last_imm: + wc.ex.imm_data = ohdr->u.imm_data; + hdrsize +=4 ; + wc.wc_flags =IB_WC_WITH_IMM ; + goto send_last; +case OP (SEND_LAST ): +case OP (RDMA_WRITE_LAST ): +no_immediate_data: + wc.wc_flags =0 ; + wc.ex.imm_data =0 ; +send_last: +/* Get the number of bytes the message was padded by. */ + pad = (bth0 >>20 ) &3 ; +/* Check for invalid length. */ +/* LAST len should be >= 1 */ +if (unlikely (tlen < (hdrsize + pad +4 ))) + goto nack_inv; +/* Don't count the CRC. */ + tlen -= (hdrsize + pad +4 ); + wc.byte_len = tlen + qp->r_rcv_len; +if (unlikely (wc.byte_len > qp->r_len)) + goto nack_inv; +hfi1_copy_sge (&qp->r_sge, data, tlen,1 ); +hfi1_put_ss (&qp->r_sge); + qp->r_msn++; +if (!test_and_clear_bit (HFI1_R_WRID_VALID , &qp->r_aflags)) +break ; + wc.wr_id = qp->r_wr_id; + wc.status =IB_WC_SUCCESS ; +if (opcode ==OP (RDMA_WRITE_LAST_WITH_IMMEDIATE ) || + opcode ==OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE )) + wc.opcode =IB_WC_RECV_RDMA_WITH_IMM ; +else + wc.opcode =IB_WC_RECV ; + wc.qp = &qp->ibqp; + wc.src_qp = qp->remote_qpn; + wc.slid = qp->remote_ah_attr.dlid; +/* +* It seems that IB mandates the presence of an SL in a +* work completion only for the UD transport (see section +* 11.4.2 of IBTA Vol. 1). +* +* However, the way the SL is chosen below is consistent +* with the way that IB/qib works and is trying avoid +* introducing incompatibilities. +* +* See also OPA Vol. 1, section 9.7.6, and table 9-17. +*/ + wc.sl = qp->remote_ah_attr.sl; +/* zero fields that are N/A */ + wc.vendor_err =0 ; + wc.pkey_index =0 ; + wc.dlid_path_bits =0 ; + wc.port_num =0 ; +/* Signal completion event if the solicited bit is set. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc, + (bth0 &IB_BTH_SOLICITED ) !=0 ); +break ; + +case OP (RDMA_WRITE_FIRST ): +case OP (RDMA_WRITE_ONLY ): +case OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE ): +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_WRITE ))) + goto nack_inv; +/* consume RWQE */ + reth = &ohdr->u.rc.reth; + hdrsize +=sizeof (*reth); + qp->r_len =be32_to_cpu (reth->length); + qp->r_rcv_len =0 ; + qp->r_sge.sg_list =NULL ; +if (qp->r_len !=0 ) { +u32 rkey =be32_to_cpu (reth->rkey); +u64 vaddr =be64_to_cpu (reth->vaddr); +int ok; + +/* Check rkey & NAK */ + ok =hfi1_rkey_ok (qp, &qp->r_sge.sge, qp->r_len, vaddr, + rkey,IB_ACCESS_REMOTE_WRITE ); +if (unlikely (!ok)) + goto nack_acc; + qp->r_sge.num_sge =1 ; + }else { + qp->r_sge.num_sge =0 ; + qp->r_sge.sge.mr =NULL ; + qp->r_sge.sge.vaddr =NULL ; + qp->r_sge.sge.length =0 ; + qp->r_sge.sge.sge_length =0 ; + } +if (opcode ==OP (RDMA_WRITE_FIRST )) + goto send_middle; +else if (opcode ==OP (RDMA_WRITE_ONLY )) + goto no_immediate_data; + ret =hfi1_get_rwqe (qp,1 ); +if (ret <0 ) + goto nack_op_err; +if (!ret) + goto rnr_nak; + wc.ex.imm_data = ohdr->u.rc.imm_data; + hdrsize +=4 ; + wc.wc_flags =IB_WC_WITH_IMM ; + goto send_last; + +case OP (RDMA_READ_REQUEST ): { +struct hfi1_ack_entry *e; +u32 len; +u8 next; + +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_READ ))) + goto nack_inv; + next = qp->r_head_ack_queue +1 ; +/* s_ack_queue is size HFI1_MAX_RDMA_ATOMIC+1 so use > not >= */ +if (next >HFI1_MAX_RDMA_ATOMIC ) + next =0 ; +spin_lock_irqsave (&qp->s_lock, flags); +if (unlikely (next == qp->s_tail_ack_queue)) { +if (!qp->s_ack_queue[next].sent) + goto nack_inv_unlck; +update_ack_queue (qp, next); + } + e = &qp->s_ack_queue[qp->r_head_ack_queue]; +if (e->opcode ==OP (RDMA_READ_REQUEST ) && e->rdma_sge.mr) { +hfi1_put_mr (e->rdma_sge.mr); + e->rdma_sge.mr =NULL ; + } + reth = &ohdr->u.rc.reth; + len =be32_to_cpu (reth->length); +if (len) { +u32 rkey =be32_to_cpu (reth->rkey); +u64 vaddr =be64_to_cpu (reth->vaddr); +int ok; + +/* Check rkey & NAK */ + ok =hfi1_rkey_ok (qp, &e->rdma_sge, len, vaddr, + rkey,IB_ACCESS_REMOTE_READ ); +if (unlikely (!ok)) + goto nack_acc_unlck; +/* +* Update the next expected PSN. We add 1 later +* below, so only add the remainder here. +*/ +if (len > pmtu) + qp->r_psn += (len -1 ) / pmtu; + }else { + e->rdma_sge.mr =NULL ; + e->rdma_sge.vaddr =NULL ; + e->rdma_sge.length =0 ; + e->rdma_sge.sge_length =0 ; + } + e->opcode = opcode; + e->sent =0 ; + e->psn = psn; + e->lpsn = qp->r_psn; +/* +* We need to increment the MSN here instead of when we +* finish sending the result since a duplicate request would +* increment it more than once. +*/ + qp->r_msn++; + qp->r_psn++; + qp->r_state = opcode; + qp->r_nak_state =0 ; + qp->r_head_ack_queue = next; + +/* Schedule the send tasklet. */ + qp->s_flags |=HFI1_S_RESP_PENDING ; +hfi1_schedule_send (qp); + +spin_unlock_irqrestore (&qp->s_lock, flags); +if (is_fecn) + goto send_ack; +return ; + } + +case OP (COMPARE_SWAP ): +case OP (FETCH_ADD ): { +struct ib_atomic_eth *ateth; +struct hfi1_ack_entry *e; +u64 vaddr; +atomic64_t *maddr; +u64 sdata; +u32 rkey; +u8 next; + +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_ATOMIC ))) + goto nack_inv; + next = qp->r_head_ack_queue +1 ; +if (next >HFI1_MAX_RDMA_ATOMIC ) + next =0 ; +spin_lock_irqsave (&qp->s_lock, flags); +if (unlikely (next == qp->s_tail_ack_queue)) { +if (!qp->s_ack_queue[next].sent) + goto nack_inv_unlck; +update_ack_queue (qp, next); + } + e = &qp->s_ack_queue[qp->r_head_ack_queue]; +if (e->opcode ==OP (RDMA_READ_REQUEST ) && e->rdma_sge.mr) { +hfi1_put_mr (e->rdma_sge.mr); + e->rdma_sge.mr =NULL ; + } + ateth = &ohdr->u.atomic_eth; + vaddr = ((u64 )be32_to_cpu (ateth->vaddr[0 ]) <<32 ) | +be32_to_cpu (ateth->vaddr[1 ]); +if (unlikely (vaddr & (sizeof (u64) -1 ))) + goto nack_inv_unlck; + rkey =be32_to_cpu (ateth->rkey); +/* Check rkey & NAK */ +if (unlikely (!hfi1_rkey_ok (qp, &qp->r_sge.sge,sizeof (u64), + vaddr, rkey, +IB_ACCESS_REMOTE_ATOMIC ))) + goto nack_acc_unlck; +/* Perform atomic OP and save result. */ + maddr = (atomic64_t *) qp->r_sge.sge.vaddr; + sdata =be64_to_cpu (ateth->swap_data); + e->atomic_data = (opcode ==OP (FETCH_ADD )) ? + (u64 )atomic64_add_return (sdata, maddr) - sdata : + (u64 )cmpxchg ((u64 *) qp->r_sge.sge.vaddr, +be64_to_cpu (ateth->compare_data), + sdata); +hfi1_put_mr (qp->r_sge.sge.mr); + qp->r_sge.num_sge =0 ; + e->opcode = opcode; + e->sent =0 ; + e->psn = psn; + e->lpsn = psn; + qp->r_msn++; + qp->r_psn++; + qp->r_state = opcode; + qp->r_nak_state =0 ; + qp->r_head_ack_queue = next; + +/* Schedule the send tasklet. */ + qp->s_flags |=HFI1_S_RESP_PENDING ; +hfi1_schedule_send (qp); + +spin_unlock_irqrestore (&qp->s_lock, flags); +if (is_fecn) + goto send_ack; +return ; + } + +default : +/* NAK unknown opcodes. */ + goto nack_inv; + } + qp->r_psn++; + qp->r_state = opcode; + qp->r_ack_psn = psn; + qp->r_nak_state =0 ; +/* Send an ACK if requested or required. */ +if (psn & (1 <<31 )) + goto send_ack; +return ; + +rnr_nak: + qp->r_nak_state =IB_RNR_NAK | qp->r_min_rnr_timer; + qp->r_ack_psn = qp->r_psn; +/* Queue RNR NAK for later */ +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_NAK ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, &rcd->qp_wait_list); + } +return ; + +nack_op_err: +hfi1_rc_error (qp,IB_WC_LOC_QP_OP_ERR ); + qp->r_nak_state =IB_NAK_REMOTE_OPERATIONAL_ERROR ; + qp->r_ack_psn = qp->r_psn; +/* Queue NAK for later */ +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_NAK ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, &rcd->qp_wait_list); + } +return ; + +nack_inv_unlck: +spin_unlock_irqrestore (&qp->s_lock, flags); +nack_inv: +hfi1_rc_error (qp,IB_WC_LOC_QP_OP_ERR ); + qp->r_nak_state =IB_NAK_INVALID_REQUEST ; + qp->r_ack_psn = qp->r_psn; +/* Queue NAK for later */ +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_NAK ; +atomic_inc (&qp->refcount); +list_add_tail (&qp->rspwait, &rcd->qp_wait_list); + } +return ; + +nack_acc_unlck: +spin_unlock_irqrestore (&qp->s_lock, flags); +nack_acc: +hfi1_rc_error (qp,IB_WC_LOC_PROT_ERR ); + qp->r_nak_state =IB_NAK_REMOTE_ACCESS_ERROR ; + qp->r_ack_psn = qp->r_psn; +send_ack: +hfi1_send_rc_ack (rcd, qp, is_fecn); +} + +void hfi1_rc_hdrerr ( +struct hfi1_ctxtdata *rcd, +struct hfi1_ib_header *hdr, +u32 rcv_flags, +struct hfi1_qp *qp) +{ +int has_grh = rcv_flags &HFI1_HAS_GRH ; +struct hfi1_other_headers *ohdr; +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +int diff; +u8 opcode; +u32 psn; + +/* Check for GRH */ + ohdr = &hdr->u.oth; +if (has_grh) + ohdr = &hdr->u.l.oth; + + opcode =be32_to_cpu (ohdr->bth[0 ]); +if (hfi1_ruc_check_hdr (ibp, hdr, has_grh, qp, opcode)) +return ; + + psn =be32_to_cpu (ohdr->bth[2 ]); + opcode >>=24 ; + +/* Only deal with RDMA Writes for now */ +if (opcode <IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST ) { + diff =delta_psn (psn, qp->r_psn); +if (!qp->r_nak_state && diff >=0 ) { + ibp->n_rc_seqnak++; + qp->r_nak_state =IB_NAK_PSN_ERROR ; +/* Use the expected PSN. */ + qp->r_ack_psn = qp->r_psn; +/* +* Wait to send the sequence +* NAK until all packets +* in the receive queue have +* been processed. +* Otherwise, we end up +* propagating congestion. +*/ +if (list_empty (&qp->rspwait)) { + qp->r_flags |=HFI1_R_RSP_NAK ; +atomic_inc (&qp->refcount); +list_add_tail ( + &qp->rspwait, + &rcd->qp_wait_list); + } + }/* Out of sequence NAK */ + }/* QP Request NAKs */ +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 28/41 IB/hfi1: add routines for RC/UC
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/ruc.c | 944 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 944 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/ruc.c
drivers/infiniband/hw/hfi1/ruc.c new file +944 -0
@@ -0,0 +1,944 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/spinlock.h> + +#include "hfi.h" +#include "mad.h" +#include "qp.h" +#include "sdma.h" + +/* +* Convert the AETH RNR timeout code into the number of microseconds. +*/ +const u32 ib_hfi1_rnr_table[32 ] = { +655360 ,/* 00: 655.36 */ +10 ,/* 01: .01 */ +20 ,/* 02 .02 */ +30 ,/* 03: .03 */ +40 ,/* 04: .04 */ +60 ,/* 05: .06 */ +80 ,/* 06: .08 */ +120 ,/* 07: .12 */ +160 ,/* 08: .16 */ +240 ,/* 09: .24 */ +320 ,/* 0A: .32 */ +480 ,/* 0B: .48 */ +640 ,/* 0C: .64 */ +960 ,/* 0D: .96 */ +1280 ,/* 0E: 1.28 */ +1920 ,/* 0F: 1.92 */ +2560 ,/* 10: 2.56 */ +3840 ,/* 11: 3.84 */ +5120 ,/* 12: 5.12 */ +7680 ,/* 13: 7.68 */ +10240 ,/* 14: 10.24 */ +15360 ,/* 15: 15.36 */ +20480 ,/* 16: 20.48 */ +30720 ,/* 17: 30.72 */ +40960 ,/* 18: 40.96 */ +61440 ,/* 19: 61.44 */ +81920 ,/* 1A: 81.92 */ +122880 ,/* 1B: 122.88 */ +163840 ,/* 1C: 163.84 */ +245760 ,/* 1D: 245.76 */ +327680 ,/* 1E: 327.68 */ +491520 /* 1F: 491.52 */ +}; + +/* +* Validate a RWQE and fill in the SGE state. +* Return 1 if OK. +*/ +static int init_sge (struct hfi1_qp *qp,struct hfi1_rwqe *wqe) +{ +int i, j, ret; +struct ib_wc wc; +struct hfi1_lkey_table *rkt; +struct hfi1_pd *pd; +struct hfi1_sge_state *ss; + + rkt = &to_idev (qp->ibqp.device)->lk_table; + pd =to_ipd (qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd); + ss = &qp->r_sge; + ss->sg_list = qp->r_sg_list; + qp->r_len =0 ; +for (i = j =0 ; i < wqe->num_sge; i++) { +if (wqe->sg_list[i].length ==0 ) +continue ; +/* Check LKEY */ +if (!hfi1_lkey_ok (rkt, pd, j ? &ss->sg_list[j -1 ] : &ss->sge, + &wqe->sg_list[i],IB_ACCESS_LOCAL_WRITE )) + goto bad_lkey; + qp->r_len += wqe->sg_list[i].length; + j++; + } + ss->num_sge = j; + ss->total_len = qp->r_len; + ret =1 ; + goto bail; + +bad_lkey: +while (j) { +struct hfi1_sge *sge = --j ? &ss->sg_list[j -1 ] : &ss->sge; + +hfi1_put_mr (sge->mr); + } + ss->num_sge =0 ; +memset (&wc,0 ,sizeof (wc)); + wc.wr_id = wqe->wr_id; + wc.status =IB_WC_LOC_PROT_ERR ; + wc.opcode =IB_WC_RECV ; + wc.qp = &qp->ibqp; +/* Signal solicited completion event. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc,1 ); + ret =0 ; +bail: +return ret; +} + +/** +* hfi1_get_rwqe - copy the next RWQE into the QP's RWQE +* @qp: the QP +* @wr_id_only: update qp->r_wr_id only, not qp->r_sge +* +* Return -1 if there is a local error, 0 if no RWQE is available, +* otherwise return 1. +* +* Can be called from interrupt level. +*/ +int hfi1_get_rwqe (struct hfi1_qp *qp,int wr_id_only) +{ +unsigned long flags; +struct hfi1_rq *rq; +struct hfi1_rwq *wq; +struct hfi1_srq *srq; +struct hfi1_rwqe *wqe; +void (*handler)(struct ib_event *,void *); +u32 tail; +int ret; + +if (qp->ibqp.srq) { + srq =to_isrq (qp->ibqp.srq); + handler = srq->ibsrq.event_handler; + rq = &srq->rq; + }else { + srq =NULL ; + handler =NULL ; + rq = &qp->r_rq; + } + +spin_lock_irqsave (&rq->lock, flags); +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) { + ret =0 ; + goto unlock; + } + + wq = rq->wq; + tail = wq->tail; +/* Validate tail before using it since it is user writable. */ +if (tail >= rq->size) + tail =0 ; +if (unlikely (tail == wq->head)) { + ret =0 ; + goto unlock; + } +/* Make sure entry is read after head index is read. */ +smp_rmb (); + wqe =get_rwqe_ptr (rq, tail); +/* +* Even though we update the tail index in memory, the verbs +* consumer is not supposed to post more entries until a +* completion is generated. +*/ +if (++tail >= rq->size) + tail =0 ; + wq->tail = tail; +if (!wr_id_only && !init_sge (qp, wqe)) { + ret =-1 ; + goto unlock; + } + qp->r_wr_id = wqe->wr_id; + + ret =1 ; +set_bit (HFI1_R_WRID_VALID , &qp->r_aflags); +if (handler) { +u32 n; + +/* +* Validate head pointer value and compute +* the number of remaining WQEs. +*/ + n = wq->head; +if (n >= rq->size) + n =0 ; +if (n < tail) + n += rq->size - tail; +else + n -= tail; +if (n < srq->limit) { +struct ib_event ev; + + srq->limit =0 ; +spin_unlock_irqrestore (&rq->lock, flags); + ev.device = qp->ibqp.device; + ev.element.srq = qp->ibqp.srq; + ev.event =IB_EVENT_SRQ_LIMIT_REACHED ; +handler (&ev, srq->ibsrq.srq_context); + goto bail; + } + } +unlock: +spin_unlock_irqrestore (&rq->lock, flags); +bail: +return ret; +} + +/* +* Switch to alternate path. +* The QP s_lock should be held and interrupts disabled. +*/ +void hfi1_migrate_qp (struct hfi1_qp *qp) +{ +struct ib_event ev; + + qp->s_mig_state =IB_MIG_MIGRATED ; + qp->remote_ah_attr = qp->alt_ah_attr; + qp->port_num = qp->alt_ah_attr.port_num; + qp->s_pkey_index = qp->s_alt_pkey_index; + qp->s_flags |=HFI1_S_AHG_CLEAR ; + + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_PATH_MIG ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); +} + +static __be64 get_sguid (struct hfi1_ibport *ibp,unsigned index) +{ +if (!index) { +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); + +return ppd->guid; + } +return ibp->guids[index -1 ]; +} + +static int gid_ok (union ib_gid *gid,__be64 gid_prefix,__be64 id) +{ +return (gid->global.interface_id == id && + (gid->global.subnet_prefix == gid_prefix || + gid->global.subnet_prefix ==IB_DEFAULT_GID_PREFIX )); +} + +/* +* +* This should be called with the QP r_lock held. +* +* The s_lock will be acquired around the hfi1_migrate_qp() call. +*/ +int hfi1_ruc_check_hdr (struct hfi1_ibport *ibp,struct hfi1_ib_header *hdr, +int has_grh,struct hfi1_qp *qp,u32 bth0) +{ +__be64 guid; +unsigned long flags; +u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + +if (qp->s_mig_state ==IB_MIG_ARMED && (bth0 &IB_BTH_MIG_REQ )) { +if (!has_grh) { +if (qp->alt_ah_attr.ah_flags &IB_AH_GRH ) + goto err; + }else { +if (!(qp->alt_ah_attr.ah_flags &IB_AH_GRH )) + goto err; + guid =get_sguid (ibp, qp->alt_ah_attr.grh.sgid_index); +if (!gid_ok (&hdr->u.l.grh.dgid, ibp->gid_prefix, guid)) + goto err; +if (!gid_ok (&hdr->u.l.grh.sgid, + qp->alt_ah_attr.grh.dgid.global.subnet_prefix, + qp->alt_ah_attr.grh.dgid.global.interface_id)) + goto err; + } +if (unlikely (ingress_pkey_check (ppd_from_ibp (ibp), (u16 )bth0, + sc5, qp->s_alt_pkey_index, +be16_to_cpu (hdr->lrh[3 ])))) { +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_PKEY , + (u16 )bth0, + (be16_to_cpu (hdr->lrh[0 ]) >>4 ) &0xF , +0 , qp->ibqp.qp_num, + hdr->lrh[3 ], hdr->lrh[1 ]); + goto err; + } +/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */ +if (be16_to_cpu (hdr->lrh[3 ]) != qp->alt_ah_attr.dlid || +ppd_from_ibp (ibp)->port != qp->alt_ah_attr.port_num) + goto err; +spin_lock_irqsave (&qp->s_lock, flags); +hfi1_migrate_qp (qp); +spin_unlock_irqrestore (&qp->s_lock, flags); + }else { +if (!has_grh) { +if (qp->remote_ah_attr.ah_flags &IB_AH_GRH ) + goto err; + }else { +if (!(qp->remote_ah_attr.ah_flags &IB_AH_GRH )) + goto err; + guid =get_sguid (ibp, + qp->remote_ah_attr.grh.sgid_index); +if (!gid_ok (&hdr->u.l.grh.dgid, ibp->gid_prefix, guid)) + goto err; +if (!gid_ok (&hdr->u.l.grh.sgid, + qp->remote_ah_attr.grh.dgid.global.subnet_prefix, + qp->remote_ah_attr.grh.dgid.global.interface_id)) + goto err; + } +if (unlikely (ingress_pkey_check (ppd_from_ibp (ibp), (u16 )bth0, + sc5, qp->s_pkey_index, +be16_to_cpu (hdr->lrh[3 ])))) { +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_PKEY , + (u16 )bth0, + (be16_to_cpu (hdr->lrh[0 ]) >>4 ) &0xF , +0 , qp->ibqp.qp_num, + hdr->lrh[3 ], hdr->lrh[1 ]); + goto err; + } +/* Validate the SLID. See Ch. 9.6.1.5 */ +if (be16_to_cpu (hdr->lrh[3 ]) != qp->remote_ah_attr.dlid || +ppd_from_ibp (ibp)->port != qp->port_num) + goto err; +if (qp->s_mig_state ==IB_MIG_REARM && + !(bth0 &IB_BTH_MIG_REQ )) + qp->s_mig_state =IB_MIG_ARMED ; + } + +return 0 ; + +err: +return 1 ; +} + +/** +* ruc_loopback - handle UC and RC loopback requests +* @sqp: the sending QP +* +* This is called from hfi1_do_send() to +* forward a WQE addressed to the same HCA. +* Note that although we are single threaded due to the tasklet, we still +* have to protect against post_send(). We don't have to worry about +* receive interrupts since this is a connected protocol and all packets +* will pass through here. +*/ +static void ruc_loopback (struct hfi1_qp *sqp) +{ +struct hfi1_ibport *ibp =to_iport (sqp->ibqp.device, sqp->port_num); +struct hfi1_qp *qp; +struct hfi1_swqe *wqe; +struct hfi1_sge *sge; +unsigned long flags; +struct ib_wc wc; +u64 sdata; +atomic64_t *maddr; +enum ib_wc_status send_status; +int release; +int ret; + +/* +* Note that we check the responder QP state after +* checking the requester's state. +*/ + qp =hfi1_lookup_qpn (ibp, sqp->remote_qpn); + +spin_lock_irqsave (&sqp->s_lock, flags); + +/* Return if we are already busy processing a work request. */ +if ((sqp->s_flags & (HFI1_S_BUSY |HFI1_S_ANY_WAIT )) || + !(ib_hfi1_state_ops[sqp->state] &HFI1_PROCESS_OR_FLUSH_SEND )) + goto unlock; + + sqp->s_flags |=HFI1_S_BUSY ; + +again: +if (sqp->s_last == sqp->s_head) + goto clr_busy; + wqe =get_swqe_ptr (sqp, sqp->s_last); + +/* Return if it is not OK to start a new work request. */ +if (!(ib_hfi1_state_ops[sqp->state] &HFI1_PROCESS_NEXT_SEND_OK )) { +if (!(ib_hfi1_state_ops[sqp->state] &HFI1_FLUSH_SEND )) + goto clr_busy; +/* We are in the error state, flush the work request. */ + send_status =IB_WC_WR_FLUSH_ERR ; + goto flush_send; + } + +/* +* We can rely on the entry not changing without the s_lock +* being held until we update s_last. +* We increment s_cur to indicate s_last is in progress. +*/ +if (sqp->s_last == sqp->s_cur) { +if (++sqp->s_cur >= sqp->s_size) + sqp->s_cur =0 ; + } +spin_unlock_irqrestore (&sqp->s_lock, flags); + +if (!qp || !(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK ) || + qp->ibqp.qp_type != sqp->ibqp.qp_type) { + ibp->n_pkt_drops++; +/* +* For RC, the requester would timeout and retry so +* shortcut the timeouts and just signal too many retries. +*/ +if (sqp->ibqp.qp_type ==IB_QPT_RC ) + send_status =IB_WC_RETRY_EXC_ERR ; +else + send_status =IB_WC_SUCCESS ; + goto serr; + } + +memset (&wc,0 ,sizeof (wc)); + send_status =IB_WC_SUCCESS ; + + release =1 ; + sqp->s_sge.sge = wqe->sg_list[0 ]; + sqp->s_sge.sg_list = wqe->sg_list +1 ; + sqp->s_sge.num_sge = wqe->wr.num_sge; + sqp->s_len = wqe->length; +switch (wqe->wr.opcode) { +case IB_WR_SEND_WITH_IMM : + wc.wc_flags =IB_WC_WITH_IMM ; + wc.ex.imm_data = wqe->wr.ex.imm_data; +/* FALLTHROUGH */ +case IB_WR_SEND : + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) + goto op_err; +if (!ret) + goto rnr_nak; +break ; + +case IB_WR_RDMA_WRITE_WITH_IMM : +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_WRITE ))) + goto inv_err; + wc.wc_flags =IB_WC_WITH_IMM ; + wc.ex.imm_data = wqe->wr.ex.imm_data; + ret =hfi1_get_rwqe (qp,1 ); +if (ret <0 ) + goto op_err; +if (!ret) + goto rnr_nak; +/* FALLTHROUGH */ +case IB_WR_RDMA_WRITE : +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_WRITE ))) + goto inv_err; +if (wqe->length ==0 ) +break ; +if (unlikely (!hfi1_rkey_ok (qp, &qp->r_sge.sge, wqe->length, + wqe->wr.wr.rdma.remote_addr, + wqe->wr.wr.rdma.rkey, +IB_ACCESS_REMOTE_WRITE ))) + goto acc_err; + qp->r_sge.sg_list =NULL ; + qp->r_sge.num_sge =1 ; + qp->r_sge.total_len = wqe->length; +break ; + +case IB_WR_RDMA_READ : +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_READ ))) + goto inv_err; +if (unlikely (!hfi1_rkey_ok (qp, &sqp->s_sge.sge, wqe->length, + wqe->wr.wr.rdma.remote_addr, + wqe->wr.wr.rdma.rkey, +IB_ACCESS_REMOTE_READ ))) + goto acc_err; + release =0 ; + sqp->s_sge.sg_list =NULL ; + sqp->s_sge.num_sge =1 ; + qp->r_sge.sge = wqe->sg_list[0 ]; + qp->r_sge.sg_list = wqe->sg_list +1 ; + qp->r_sge.num_sge = wqe->wr.num_sge; + qp->r_sge.total_len = wqe->length; +break ; + +case IB_WR_ATOMIC_CMP_AND_SWP : +case IB_WR_ATOMIC_FETCH_AND_ADD : +if (unlikely (!(qp->qp_access_flags &IB_ACCESS_REMOTE_ATOMIC ))) + goto inv_err; +if (unlikely (!hfi1_rkey_ok (qp, &qp->r_sge.sge,sizeof (u64), + wqe->wr.wr.atomic.remote_addr, + wqe->wr.wr.atomic.rkey, +IB_ACCESS_REMOTE_ATOMIC ))) + goto acc_err; +/* Perform atomic OP and save result. */ + maddr = (atomic64_t *) qp->r_sge.sge.vaddr; + sdata = wqe->wr.wr.atomic.compare_add; + *(u64 *) sqp->s_sge.sge.vaddr = + (wqe->wr.opcode ==IB_WR_ATOMIC_FETCH_AND_ADD ) ? + (u64 )atomic64_add_return (sdata, maddr) - sdata : + (u64 )cmpxchg ((u64 *) qp->r_sge.sge.vaddr, + sdata, wqe->wr.wr.atomic.swap); +hfi1_put_mr (qp->r_sge.sge.mr); + qp->r_sge.num_sge =0 ; + goto send_comp; + +default : + send_status =IB_WC_LOC_QP_OP_ERR ; + goto serr; + } + + sge = &sqp->s_sge.sge; +while (sqp->s_len) { +u32 len = sqp->s_len; + +if (len > sge->length) + len = sge->length; +if (len > sge->sge_length) + len = sge->sge_length; +BUG_ON (len ==0 ); +hfi1_copy_sge (&qp->r_sge, sge->vaddr, len, release); + sge->vaddr += len; + sge->length -= len; + sge->sge_length -= len; +if (sge->sge_length ==0 ) { +if (!release) +hfi1_put_mr (sge->mr); +if (--sqp->s_sge.num_sge) + *sge = *sqp->s_sge.sg_list++; + }else if (sge->length ==0 && sge->mr->lkey) { +if (++sge->n >=HFI1_SEGSZ ) { +if (++sge->m >= sge->mr->mapsz) +break ; + sge->n =0 ; + } + sge->vaddr = + sge->mr->map[sge->m]->segs[sge->n].vaddr; + sge->length = + sge->mr->map[sge->m]->segs[sge->n].length; + } + sqp->s_len -= len; + } +if (release) +hfi1_put_ss (&qp->r_sge); + +if (!test_and_clear_bit (HFI1_R_WRID_VALID , &qp->r_aflags)) + goto send_comp; + +if (wqe->wr.opcode ==IB_WR_RDMA_WRITE_WITH_IMM ) + wc.opcode =IB_WC_RECV_RDMA_WITH_IMM ; +else + wc.opcode =IB_WC_RECV ; + wc.wr_id = qp->r_wr_id; + wc.status =IB_WC_SUCCESS ; + wc.byte_len = wqe->length; + wc.qp = &qp->ibqp; + wc.src_qp = qp->remote_qpn; + wc.slid = qp->remote_ah_attr.dlid; + wc.sl = qp->remote_ah_attr.sl; + wc.port_num =1 ; +/* Signal completion event if the solicited bit is set. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc, + wqe->wr.send_flags &IB_SEND_SOLICITED ); + +send_comp: +spin_lock_irqsave (&sqp->s_lock, flags); + ibp->n_loop_pkts++; +flush_send: + sqp->s_rnr_retry = sqp->s_rnr_retry_cnt; +hfi1_send_complete (sqp, wqe, send_status); + goto again; + +rnr_nak: +/* Handle RNR NAK */ +if (qp->ibqp.qp_type ==IB_QPT_UC ) + goto send_comp; + ibp->n_rnr_naks++; +/* +* Note: we don't need the s_lock held since the BUSY flag +* makes this single threaded. +*/ +if (sqp->s_rnr_retry ==0 ) { + send_status =IB_WC_RNR_RETRY_EXC_ERR ; + goto serr; + } +if (sqp->s_rnr_retry_cnt <7 ) + sqp->s_rnr_retry--; +spin_lock_irqsave (&sqp->s_lock, flags); +if (!(ib_hfi1_state_ops[sqp->state] &HFI1_PROCESS_RECV_OK )) + goto clr_busy; + sqp->s_flags |=HFI1_S_WAIT_RNR ; + sqp->s_timer.function = hfi1_rc_rnr_retry; + sqp->s_timer.expires = jiffies + +usecs_to_jiffies (ib_hfi1_rnr_table[qp->r_min_rnr_timer]); +add_timer (&sqp->s_timer); + goto clr_busy; + +op_err: + send_status =IB_WC_REM_OP_ERR ; + wc.status =IB_WC_LOC_QP_OP_ERR ; + goto err; + +inv_err: + send_status =IB_WC_REM_INV_REQ_ERR ; + wc.status =IB_WC_LOC_QP_OP_ERR ; + goto err; + +acc_err: + send_status =IB_WC_REM_ACCESS_ERR ; + wc.status =IB_WC_LOC_PROT_ERR ; +err: +/* responder goes to error state */ +hfi1_rc_error (qp, wc.status); + +serr: +spin_lock_irqsave (&sqp->s_lock, flags); +hfi1_send_complete (sqp, wqe, send_status); +if (sqp->ibqp.qp_type ==IB_QPT_RC ) { +int lastwqe =hfi1_error_qp (sqp,IB_WC_WR_FLUSH_ERR ); + + sqp->s_flags &= ~HFI1_S_BUSY ; +spin_unlock_irqrestore (&sqp->s_lock, flags); +if (lastwqe) { +struct ib_event ev; + + ev.device = sqp->ibqp.device; + ev.element.qp = &sqp->ibqp; + ev.event =IB_EVENT_QP_LAST_WQE_REACHED ; + sqp->ibqp.event_handler (&ev, sqp->ibqp.qp_context); + } + goto done; + } +clr_busy: + sqp->s_flags &= ~HFI1_S_BUSY ; +unlock: +spin_unlock_irqrestore (&sqp->s_lock, flags); +done: +if (qp &&atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); +} + +/** +* hfi1_make_grh - construct a GRH header +* @ibp: a pointer to the IB port +* @hdr: a pointer to the GRH header being constructed +* @grh: the global route address to send to +* @hwords: the number of 32 bit words of header being sent +* @nwords: the number of 32 bit words of data being sent +* +* Return the size of the header in 32 bit words. +*/ +u32 hfi1_make_grh (struct hfi1_ibport *ibp,struct ib_grh *hdr, +struct ib_global_route *grh,u32 hwords,u32 nwords) +{ + hdr->version_tclass_flow = +cpu_to_be32 ((IB_GRH_VERSION <<IB_GRH_VERSION_SHIFT ) | + (grh->traffic_class <<IB_GRH_TCLASS_SHIFT ) | + (grh->flow_label <<IB_GRH_FLOW_SHIFT )); + hdr->paylen =cpu_to_be16 ((hwords -2 + nwords +SIZE_OF_CRC ) <<2 ); +/* next_hdr is defined by C8-7 in ch. 8.4.1 */ + hdr->next_hdr =IB_GRH_NEXT_HDR ; + hdr->hop_limit = grh->hop_limit; +/* The SGID is 32-bit aligned. */ + hdr->sgid.global.subnet_prefix = ibp->gid_prefix; + hdr->sgid.global.interface_id = + grh->sgid_index && grh->sgid_index <ARRAY_SIZE (ibp->guids) ? + ibp->guids[grh->sgid_index -1 ] :ppd_from_ibp (ibp)->guid; + hdr->dgid = grh->dgid; + +/* GRH header size in 32-bit words. */ +return sizeof (struct ib_grh ) /sizeof (u32); +} + +/* +* free_ahg - clear ahg from QP +*/ +void clear_ahg (struct hfi1_qp *qp) +{ + qp->s_hdr->ahgcount =0 ; + qp->s_flags &= ~(HFI1_S_AHG_VALID |HFI1_S_AHG_CLEAR ); +if (qp->s_sde) +sdma_ahg_free (qp->s_sde, qp->s_ahgidx); + qp->s_ahgidx =-1 ; + qp->s_sde =NULL ; +} + +#define BTH2_OFFSET (offsetof(struct hfi1_pio_header, hdr.u.oth.bth[2]) / 4) + +/** +* build_ahg - create ahg in s_hdr +* @qp: a pointer to QP +* @npsn: the next PSN for the request/response +* +* This routine handles the AHG by allocating an ahg entry and causing the +* copy of the first middle. +* +* Subsequent middles use the copied entry, editing the +* PSN with 1 or 2 edits. +*/ +static inline void build_ahg (struct hfi1_qp *qp,u32 npsn) +{ +if (unlikely (qp->s_flags &HFI1_S_AHG_CLEAR )) +clear_ahg (qp); +if (!(qp->s_flags &HFI1_S_AHG_VALID )) { +/* first middle that needs copy */ +if (qp->s_ahgidx <0 ) { +if (!qp->s_sde) + qp->s_sde =qp_to_sdma_engine (qp, qp->s_sc); + qp->s_ahgidx =sdma_ahg_alloc (qp->s_sde); + } +if (qp->s_ahgidx >=0 ) { + qp->s_ahgpsn = npsn; + qp->s_hdr->tx_flags |=SDMA_TXREQ_F_AHG_COPY ; +/* save to protect a change in another thread */ + qp->s_hdr->sde = qp->s_sde; + qp->s_hdr->ahgidx = qp->s_ahgidx; + qp->s_flags |=HFI1_S_AHG_VALID ; + } + }else { +/* subsequent middle after valid */ +if (qp->s_ahgidx >=0 ) { + qp->s_hdr->tx_flags |=SDMA_TXREQ_F_USE_AHG ; + qp->s_hdr->ahgidx = qp->s_ahgidx; + qp->s_hdr->ahgcount++; + qp->s_hdr->ahgdesc[0 ] = +sdma_build_ahg_descriptor ( + (__force u16 )cpu_to_be16 ((u16 )npsn), +BTH2_OFFSET , +16 , +16 ); +if ((npsn &0xffff0000 ) != + (qp->s_ahgpsn &0xffff0000 )) { + qp->s_hdr->ahgcount++; + qp->s_hdr->ahgdesc[1 ] = +sdma_build_ahg_descriptor ( + (__force u16 )cpu_to_be16 ( + (u16)(npsn >>16 )), +BTH2_OFFSET , +0 , +16 ); + } + } + } +} + +void hfi1_make_ruc_header (struct hfi1_qp *qp,struct hfi1_other_headers *ohdr, +u32 bth0,u32 bth2,int middle) +{ +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +u16 lrh0; +u32 nwords; +u32 extra_bytes; +u8 sc5; + +/* Construct the header. */ + extra_bytes = -qp->s_cur_size &3 ; + nwords = (qp->s_cur_size + extra_bytes) >>2 ; + lrh0 =HFI1_LRH_BTH ; +if (unlikely (qp->remote_ah_attr.ah_flags &IB_AH_GRH )) { + qp->s_hdrwords +=hfi1_make_grh (ibp, &qp->s_hdr->ibh.u.l.grh, + &qp->remote_ah_attr.grh, + qp->s_hdrwords, nwords); + lrh0 =HFI1_LRH_GRH ; + middle =0 ; + } + sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + lrh0 |= (sc5 &0xf ) <<12 | (qp->remote_ah_attr.sl &0xf ) <<4 ; + qp->s_sc = sc5; +/* +* reset s_hdr/AHG fields +* +* This insures that the ahgentry/ahgcount +* are at a non-AHG default to protect +* build_verbs_tx_desc() from using +* an include ahgidx. +* +* build_ahg() will modify as appropriate +* to use the AHG feature. +*/ + qp->s_hdr->tx_flags =0 ; + qp->s_hdr->ahgcount =0 ; + qp->s_hdr->ahgidx =0 ; + qp->s_hdr->sde =NULL ; +if (qp->s_mig_state ==IB_MIG_MIGRATED ) + bth0 |=IB_BTH_MIG_REQ ; +else + middle =0 ; +if (middle) +build_ahg (qp, bth2); +else + qp->s_flags &= ~HFI1_S_AHG_VALID ; + qp->s_hdr->ibh.lrh[0 ] =cpu_to_be16 (lrh0); + qp->s_hdr->ibh.lrh[1 ] =cpu_to_be16 (qp->remote_ah_attr.dlid); + qp->s_hdr->ibh.lrh[2 ] = +cpu_to_be16 (qp->s_hdrwords + nwords +SIZE_OF_CRC ); + qp->s_hdr->ibh.lrh[3 ] =cpu_to_be16 (ppd_from_ibp (ibp)->lid | + qp->remote_ah_attr.src_path_bits); + bth0 |=hfi1_get_pkey (ibp, qp->s_pkey_index); + bth0 |= extra_bytes <<20 ; + ohdr->bth[0 ] =cpu_to_be32 (bth0); + ohdr->bth[1 ] =cpu_to_be32 (qp->remote_qpn); +if (test_and_clear_bit (HFI1_S_ECN , &qp->s_aflags)) +/* we recently received a FECN, so return a BECN */ + ohdr->bth[1 ] |=cpu_to_be32 (HFI1_BECN_MASK <<HFI1_BECN_SHIFT ); + ohdr->bth[2 ] =cpu_to_be32 (bth2); +} + +/** +* hfi1_do_send - perform a send on a QP +* @work: contains a pointer to the QP +* +* Process entries in the send work queue until credit or queue is +* exhausted. Only allow one CPU to send a packet per QP (tasklet). +* Otherwise, two threads could send packets out of order. +*/ +void hfi1_do_send (struct work_struct *work) +{ + struct iowait *wait = container_of(work, struct iowait, iowork); + struct hfi1_qp *qp = container_of(wait, struct hfi1_qp, s_iowait); +struct hfi1_ibport *ibp =to_iport (qp->ibqp.device, qp->port_num); +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +int (*make_req)(struct hfi1_qp *qp); +unsigned long flags; + +if ((qp->ibqp.qp_type ==IB_QPT_RC || + qp->ibqp.qp_type ==IB_QPT_UC ) && + !loopback && + (qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) -1 )) == ppd->lid) { +ruc_loopback (qp); +return ; + } + +if (qp->ibqp.qp_type ==IB_QPT_RC ) + make_req = hfi1_make_rc_req; +else if (qp->ibqp.qp_type ==IB_QPT_UC ) + make_req = hfi1_make_uc_req; +else + make_req = hfi1_make_ud_req; + +spin_lock_irqsave (&qp->s_lock, flags); + +/* Return if we are already busy processing a work request. */ +if (!hfi1_send_ok (qp)) { +spin_unlock_irqrestore (&qp->s_lock, flags); +return ; + } + + qp->s_flags |=HFI1_S_BUSY ; + +spin_unlock_irqrestore (&qp->s_lock, flags); + +do { +/* Check for a constructed packet to be sent. */ +if (qp->s_hdrwords !=0 ) { +/* +* If the packet cannot be sent now, return and +* the send tasklet will be woken up later. +*/ +if (hfi1_verbs_send (qp, qp->s_hdr, qp->s_hdrwords, + qp->s_cur_sge, qp->s_cur_size)) +break ; +/* Record that s_hdr is empty. */ + qp->s_hdrwords =0 ; + } + }while (make_req (qp)); +} + +/* +* This should be called with s_lock held. +*/ +void hfi1_send_complete (struct hfi1_qp *qp,struct hfi1_swqe *wqe, +enum ib_wc_status status) +{ +u32 old_last, last; +unsigned i; + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_OR_FLUSH_SEND )) +return ; + +for (i =0 ; i < wqe->wr.num_sge; i++) { +struct hfi1_sge *sge = &wqe->sg_list[i]; + +hfi1_put_mr (sge->mr); + } +if (qp->ibqp.qp_type ==IB_QPT_UD || + qp->ibqp.qp_type ==IB_QPT_SMI || + qp->ibqp.qp_type ==IB_QPT_GSI ) +atomic_dec (&to_iah (wqe->wr.wr.ud.ah)->refcount); + +/* See ch. 11.2.4.1 and 10.7.3.1 */ +if (!(qp->s_flags &HFI1_S_SIGNAL_REQ_WR ) || + (wqe->wr.send_flags &IB_SEND_SIGNALED ) || + status !=IB_WC_SUCCESS ) { +struct ib_wc wc; + +memset (&wc,0 ,sizeof (wc)); + wc.wr_id = wqe->wr.wr_id; + wc.status = status; + wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode]; + wc.qp = &qp->ibqp; +if (status ==IB_WC_SUCCESS ) + wc.byte_len = wqe->length; +hfi1_cq_enter (to_icq (qp->ibqp.send_cq), &wc, + status !=IB_WC_SUCCESS ); + } + + last = qp->s_last; + old_last = last; +if (++last >= qp->s_size) + last =0 ; + qp->s_last = last; +if (qp->s_acked == old_last) + qp->s_acked = last; +if (qp->s_cur == old_last) + qp->s_cur = last; +if (qp->s_tail == old_last) + qp->s_tail = last; +if (qp->state ==IB_QPS_SQD && last == qp->s_cur) + qp->s_draining =0 ; +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 30/41 IB/hfi1: add SRQ handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/srq.c | 397 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 397 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/srq.c
drivers/infiniband/hw/hfi1/srq.c new file +397 -0
@@ -0,0 +1,397 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/err.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> + +#include "verbs.h" + +/** +* hfi1_post_srq_receive - post a receive on a shared receive queue +* @ibsrq: the SRQ to post the receive on +* @wr: the list of work requests to post +* @bad_wr: A pointer to the first WR to cause a problem is put here +* +* This may be called from interrupt context. +*/ +int hfi1_post_srq_receive (struct ib_srq *ibsrq,struct ib_recv_wr *wr, +struct ib_recv_wr **bad_wr) +{ +struct hfi1_srq *srq =to_isrq (ibsrq); +struct hfi1_rwq *wq; +unsigned long flags; +int ret; + +for (; wr; wr = wr->next) { +struct hfi1_rwqe *wqe; +u32 next; +int i; + +if ((unsigned ) wr->num_sge > srq->rq.max_sge) { + *bad_wr = wr; + ret = -EINVAL ; + goto bail; + } + +spin_lock_irqsave (&srq->rq.lock, flags); + wq = srq->rq.wq; + next = wq->head +1 ; +if (next >= srq->rq.size) + next =0 ; +if (next == wq->tail) { +spin_unlock_irqrestore (&srq->rq.lock, flags); + *bad_wr = wr; + ret = -ENOMEM ; + goto bail; + } + + wqe =get_rwqe_ptr (&srq->rq, wq->head); + wqe->wr_id = wr->wr_id; + wqe->num_sge = wr->num_sge; +for (i =0 ; i < wr->num_sge; i++) + wqe->sg_list[i] = wr->sg_list[i]; +/* Make sure queue entry is written before the head index. */ +smp_wmb (); + wq->head = next; +spin_unlock_irqrestore (&srq->rq.lock, flags); + } + ret =0 ; + +bail: +return ret; +} + +/** +* hfi1_create_srq - create a shared receive queue +* @ibpd: the protection domain of the SRQ to create +* @srq_init_attr: the attributes of the SRQ +* @udata: data from libibverbs when creating a user SRQ +*/ +struct ib_srq *hfi1_create_srq (struct ib_pd *ibpd, +struct ib_srq_init_attr *srq_init_attr, +struct ib_udata *udata) +{ +struct hfi1_ibdev *dev =to_idev (ibpd->device); +struct hfi1_srq *srq; +u32 sz; +struct ib_srq *ret; + +if (srq_init_attr->srq_type !=IB_SRQT_BASIC ) { + ret =ERR_PTR (-ENOSYS ); + goto done; + } + +if (srq_init_attr->attr.max_sge ==0 || + srq_init_attr->attr.max_sge > hfi1_max_srq_sges || + srq_init_attr->attr.max_wr ==0 || + srq_init_attr->attr.max_wr > hfi1_max_srq_wrs) { + ret =ERR_PTR (-EINVAL ); + goto done; + } + + srq =kmalloc (sizeof (*srq),GFP_KERNEL ); +if (!srq) { + ret =ERR_PTR (-ENOMEM ); + goto done; + } + +/* +* Need to use vmalloc() if we want to support large #s of entries. +*/ + srq->rq.size = srq_init_attr->attr.max_wr +1 ; + srq->rq.max_sge = srq_init_attr->attr.max_sge; + sz =sizeof (struct ib_sge ) * srq->rq.max_sge + +sizeof (struct hfi1_rwqe ); + srq->rq.wq =vmalloc_user (sizeof (struct hfi1_rwq ) + srq->rq.size * sz); +if (!srq->rq.wq) { + ret =ERR_PTR (-ENOMEM ); + goto bail_srq; + } + +/* +* Return the address of the RWQ as the offset to mmap. +* See hfi1_mmap() for details. +*/ +if (udata && udata->outlen >=sizeof (__u64)) { +int err; +u32 s =sizeof (struct hfi1_rwq ) + srq->rq.size * sz; + + srq->ip = +hfi1_create_mmap_info (dev, s, ibpd->uobject->context, + srq->rq.wq); +if (!srq->ip) { + ret =ERR_PTR (-ENOMEM ); + goto bail_wq; + } + + err =ib_copy_to_udata (udata, &srq->ip->offset, +sizeof (srq->ip->offset)); +if (err) { + ret =ERR_PTR (err); + goto bail_ip; + } + }else + srq->ip =NULL ; + +/* +* ib_create_srq() will initialize srq->ibsrq. +*/ +spin_lock_init (&srq->rq.lock); + srq->rq.wq->head =0 ; + srq->rq.wq->tail =0 ; + srq->limit = srq_init_attr->attr.srq_limit; + +spin_lock (&dev->n_srqs_lock); +if (dev->n_srqs_allocated == hfi1_max_srqs) { +spin_unlock (&dev->n_srqs_lock); + ret =ERR_PTR (-ENOMEM ); + goto bail_ip; + } + + dev->n_srqs_allocated++; +spin_unlock (&dev->n_srqs_lock); + +if (srq->ip) { +spin_lock_irq (&dev->pending_lock); +list_add (&srq->ip->pending_mmaps, &dev->pending_mmaps); +spin_unlock_irq (&dev->pending_lock); + } + + ret = &srq->ibsrq; + goto done; + +bail_ip: +kfree (srq->ip); +bail_wq: +vfree (srq->rq.wq); +bail_srq: +kfree (srq); +done: +return ret; +} + +/** +* hfi1_modify_srq - modify a shared receive queue +* @ibsrq: the SRQ to modify +* @attr: the new attributes of the SRQ +* @attr_mask: indicates which attributes to modify +* @udata: user data for libibverbs.so +*/ +int hfi1_modify_srq (struct ib_srq *ibsrq,struct ib_srq_attr *attr, +enum ib_srq_attr_mask attr_mask, +struct ib_udata *udata) +{ +struct hfi1_srq *srq =to_isrq (ibsrq); +struct hfi1_rwq *wq; +int ret =0 ; + +if (attr_mask &IB_SRQ_MAX_WR ) { +struct hfi1_rwq *owq; +struct hfi1_rwqe *p; +u32 sz, size, n, head, tail; + +/* Check that the requested sizes are below the limits. */ +if ((attr->max_wr > hfi1_max_srq_wrs) || + ((attr_mask &IB_SRQ_LIMIT ) ? + attr->srq_limit : srq->limit) > attr->max_wr) { + ret = -EINVAL ; + goto bail; + } + + sz =sizeof (struct hfi1_rwqe ) + + srq->rq.max_sge *sizeof (struct ib_sge ); + size = attr->max_wr +1 ; + wq =vmalloc_user (sizeof (struct hfi1_rwq ) + size * sz); +if (!wq) { + ret = -ENOMEM ; + goto bail; + } + +/* Check that we can write the offset to mmap. */ +if (udata && udata->inlen >=sizeof (__u64)) { +__u64 offset_addr; +__u64 offset =0 ; + + ret =ib_copy_from_udata (&offset_addr, udata, +sizeof (offset_addr)); +if (ret) + goto bail_free; + udata->outbuf = + (void __user *) (unsigned long ) offset_addr; + ret =ib_copy_to_udata (udata, &offset, +sizeof (offset)); +if (ret) + goto bail_free; + } + +spin_lock_irq (&srq->rq.lock); +/* +* validate head and tail pointer values and compute +* the number of remaining WQEs. +*/ + owq = srq->rq.wq; + head = owq->head; + tail = owq->tail; +if (head >= srq->rq.size || tail >= srq->rq.size) { + ret = -EINVAL ; + goto bail_unlock; + } + n = head; +if (n < tail) + n += srq->rq.size - tail; +else + n -= tail; +if (size <= n) { + ret = -EINVAL ; + goto bail_unlock; + } + n =0 ; + p = wq->wq; +while (tail != head) { +struct hfi1_rwqe *wqe; +int i; + + wqe =get_rwqe_ptr (&srq->rq, tail); + p->wr_id = wqe->wr_id; + p->num_sge = wqe->num_sge; +for (i =0 ; i < wqe->num_sge; i++) + p->sg_list[i] = wqe->sg_list[i]; + n++; + p = (struct hfi1_rwqe *)((char *)p + sz); +if (++tail >= srq->rq.size) + tail =0 ; + } + srq->rq.wq = wq; + srq->rq.size = size; + wq->head = n; + wq->tail =0 ; +if (attr_mask &IB_SRQ_LIMIT ) + srq->limit = attr->srq_limit; +spin_unlock_irq (&srq->rq.lock); + +vfree (owq); + +if (srq->ip) { +struct hfi1_mmap_info *ip = srq->ip; +struct hfi1_ibdev *dev =to_idev (srq->ibsrq.device); +u32 s =sizeof (struct hfi1_rwq ) + size * sz; + +hfi1_update_mmap_info (dev, ip, s, wq); + +/* +* Return the offset to mmap. +* See hfi1_mmap() for details. +*/ +if (udata && udata->inlen >=sizeof (__u64)) { + ret =ib_copy_to_udata (udata, &ip->offset, +sizeof (ip->offset)); +if (ret) + goto bail; + } + +/* +* Put user mapping info onto the pending list +* unless it already is on the list. +*/ +spin_lock_irq (&dev->pending_lock); +if (list_empty (&ip->pending_mmaps)) +list_add (&ip->pending_mmaps, + &dev->pending_mmaps); +spin_unlock_irq (&dev->pending_lock); + } + }else if (attr_mask &IB_SRQ_LIMIT ) { +spin_lock_irq (&srq->rq.lock); +if (attr->srq_limit >= srq->rq.size) + ret = -EINVAL ; +else + srq->limit = attr->srq_limit; +spin_unlock_irq (&srq->rq.lock); + } + goto bail; + +bail_unlock: +spin_unlock_irq (&srq->rq.lock); +bail_free: +vfree (wq); +bail: +return ret; +} + +int hfi1_query_srq (struct ib_srq *ibsrq,struct ib_srq_attr *attr) +{ +struct hfi1_srq *srq =to_isrq (ibsrq); + + attr->max_wr = srq->rq.size -1 ; + attr->max_sge = srq->rq.max_sge; + attr->srq_limit = srq->limit; +return 0 ; +} + +/** +* hfi1_destroy_srq - destroy a shared receive queue +* @ibsrq: the SRQ to destroy +*/ +int hfi1_destroy_srq (struct ib_srq *ibsrq) +{ +struct hfi1_srq *srq =to_isrq (ibsrq); +struct hfi1_ibdev *dev =to_idev (ibsrq->device); + +spin_lock (&dev->n_srqs_lock); + dev->n_srqs_allocated--; +spin_unlock (&dev->n_srqs_lock); +if (srq->ip) +kref_put (&srq->ip->ref, hfi1_release_mmap_info); +else +vfree (srq->rq.wq); +kfree (srq); + +return 0 ; +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 31/41 IB/hfi1: add sysfs routines
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/sysfs.c | 1083 ++++++++++++++++++++++++++++++++++++ 1 file changed, 1083 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/sysfs.c
drivers/infiniband/hw/hfi1/sysfs.c new file +1083 -0
@@ -0,0 +1,1083 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ +#include <linux/ctype.h> + +#include "hfi.h" +#include "mad.h" +#include "trace.h" + +/* start of per-port functions */ +/* +* Get/Set heartbeat enable. OR of 1=enabled, 2=auto +*/ +static ssize_t show_hrtbt_enb (struct hfi1_pportdata *ppd,char *buf) +{ +int ret; + + ret =hfi1_get_ib_cfg (ppd,HFI1_IB_CFG_HRTBT ); + ret =scnprintf (buf,PAGE_SIZE ,"%d\n" , ret); +return ret; +} + +static ssize_t store_hrtbt_enb (struct hfi1_pportdata *ppd,const char *buf, +size_t count) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret; +u16 val; + + ret =kstrtou16 (buf,0 , &val); +if (ret) { +dd_dev_err (dd,"attempt to set invalid Heartbeat enable\n" ); +return ret; + } + +/* +* Set the "intentional" heartbeat enable per either of +* "Enable" and "Auto", as these are normally set together. +* This bit is consulted when leaving loopback mode, +* because entering loopback mode overrides it and automatically +* disables heartbeat. +*/ + ret =hfi1_set_ib_cfg (ppd,HFI1_IB_CFG_HRTBT , val); +return ret <0 ? ret : count; +} + +static ssize_t store_led_override (struct hfi1_pportdata *ppd,const char *buf, +size_t count) +{ +struct hfi1_devdata *dd = ppd->dd; +int ret; +u16 val; + + ret =kstrtou16 (buf,0 , &val); +if (ret) { +dd_dev_err (dd,"attempt to set invalid LED override\n" ); +return ret; + } + +hfi1_set_led_override (ppd, val); +return count; +} + +static ssize_t show_status (struct hfi1_pportdata *ppd,char *buf) +{ +ssize_t ret; + +if (!ppd->statusp) + ret = -EINVAL ; +else + ret =scnprintf (buf,PAGE_SIZE ,"0x%llx\n" , + (unsigned long long ) *(ppd->statusp)); +return ret; +} + +/* +* For userland compatibility, these offsets must remain fixed. +* They are strings for HFI1_STATUS_* +*/ +static const char *const hfi1_status_str[] = { +"Initted" , +"" , +"" , +"" , +"" , +"Present" , +"IB_link_up" , +"IB_configured" , +"" , +"Fatal_Hardware_Error" , +NULL , +}; + +static ssize_t show_status_str (struct hfi1_pportdata *ppd,char *buf) +{ +int i, any; +u64 s; +ssize_t ret; + +if (!ppd->statusp) { + ret = -EINVAL ; + goto bail; + } + + s = *(ppd->statusp); + *buf ='\0' ; +for (any = i =0 ; s && hfi1_status_str[i]; i++) { +if (s &1 ) { +/* if overflow */ +if (any &&strlcat (buf," " ,PAGE_SIZE ) >=PAGE_SIZE ) +break ; +if (strlcat (buf, hfi1_status_str[i],PAGE_SIZE ) >= +PAGE_SIZE ) +break ; + any =1 ; + } + s >>=1 ; + } +if (any) +strlcat (buf,"\n" ,PAGE_SIZE ); + + ret =strlen (buf); + +bail: +return ret; +} + +/* end of per-port functions */ + +/* +* Start of per-port file structures and support code +* Because we are fitting into other infrastructure, we have to supply the +* full set of kobject/sysfs_ops structures and routines. +*/ +#define HFI1_PORT_ATTR (name, mode, show, store) \ + static struct hfi1_port_attr port_attr_##name = \ + __ATTR(name, mode, show, store) + +struct hfi1_port_attr { +struct attribute attr; +ssize_t (*show)(struct hfi1_pportdata *,char *); +ssize_t (*store)(struct hfi1_pportdata *,const char *,size_t ); +}; + +HFI1_PORT_ATTR (led_override,S_IWUSR ,NULL , store_led_override); +HFI1_PORT_ATTR (hrtbt_enable,S_IWUSR |S_IRUGO , show_hrtbt_enb, + store_hrtbt_enb); +HFI1_PORT_ATTR (status,S_IRUGO , show_status,NULL ); +HFI1_PORT_ATTR (status_str,S_IRUGO , show_status_str,NULL ); + +static struct attribute *port_default_attributes[] = { + &port_attr_led_override.attr, + &port_attr_hrtbt_enable.attr, + &port_attr_status.attr, + &port_attr_status_str.attr, +NULL +}; + +/* +* Start of per-port congestion control structures and support code +*/ + +/* +* Congestion control table size followed by table entries +*/ +static ssize_t read_cc_table_bin (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t pos,size_t count) +{ +int ret; +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, pport_cc_kobj); +struct cc_state *cc_state; + + ret = ppd->total_cct_entry *sizeof (struct ib_cc_table_entry_shadow ) + +sizeof (__be16); + +if (pos > ret) +return -EINVAL ; + +if (count > ret - pos) + count = ret - pos; + +if (!count) +return count; + +rcu_read_lock (); + cc_state =get_cc_state (ppd); +if (cc_state ==NULL ) { +rcu_read_unlock (); +return -EINVAL ; + } +memcpy (buf, &cc_state->cct, count); +rcu_read_unlock (); + +return count; +} + +static void port_release (struct kobject *kobj) +{ +/* nothing to do since memory is freed by hfi1_free_devdata() */ +} + +static struct kobj_type port_cc_ktype = { + .release = port_release, +}; + +static struct bin_attribute cc_table_bin_attr = { + .attr = {.name ="cc_table_bin" , .mode =0444 }, + .read = read_cc_table_bin, + .size =PAGE_SIZE , +}; + +/* +* Congestion settings: port control, control map and an array of 16 +* entries for the congestion entries - increase, timer, event log +* trigger threshold and the minimum injection rate delay. +*/ +static ssize_t read_cc_setting_bin (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t pos,size_t count) +{ +int ret; +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, pport_cc_kobj); +struct cc_state *cc_state; + + ret =sizeof (struct opa_congestion_setting_attr_shadow ); + +if (pos > ret) +return -EINVAL ; +if (count > ret - pos) + count = ret - pos; + +if (!count) +return count; + +rcu_read_lock (); + cc_state =get_cc_state (ppd); +if (cc_state ==NULL ) { +rcu_read_unlock (); +return -EINVAL ; + } +memcpy (buf, &cc_state->cong_setting, count); +rcu_read_unlock (); + +return count; +} + +static struct bin_attribute cc_setting_bin_attr = { + .attr = {.name ="cc_settings_bin" , .mode =0444 }, + .read = read_cc_setting_bin, + .size =PAGE_SIZE , +}; + + +static ssize_t portattr_show (struct kobject *kobj, +struct attribute *attr,char *buf) +{ +struct hfi1_port_attr *pattr = + container_of(attr, struct hfi1_port_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, pport_kobj); + +return pattr->show (ppd, buf); +} + +static ssize_t portattr_store (struct kobject *kobj, +struct attribute *attr, +const char *buf,size_t len) +{ +struct hfi1_port_attr *pattr = + container_of(attr, struct hfi1_port_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, pport_kobj); + +return pattr->store (ppd, buf, len); +} + + +static const struct sysfs_ops hfi1_port_ops = { + .show = portattr_show, + .store = portattr_store, +}; + +static struct kobj_type hfi1_port_ktype = { + .release = port_release, + .sysfs_ops = &hfi1_port_ops, + .default_attrs = port_default_attributes +}; + +/* Start sc2vl */ +#define HFI1_SC2VL_ATTR (N ) \ + static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = { \ + .attr = { .name = __stringify(N), .mode = 0444 }, \ + .sc = N \ + } + +struct hfi1_sc2vl_attr { +struct attribute attr; +int sc; +}; + +HFI1_SC2VL_ATTR (0 ); +HFI1_SC2VL_ATTR (1 ); +HFI1_SC2VL_ATTR (2 ); +HFI1_SC2VL_ATTR (3 ); +HFI1_SC2VL_ATTR (4 ); +HFI1_SC2VL_ATTR (5 ); +HFI1_SC2VL_ATTR (6 ); +HFI1_SC2VL_ATTR (7 ); +HFI1_SC2VL_ATTR (8 ); +HFI1_SC2VL_ATTR (9 ); +HFI1_SC2VL_ATTR (10 ); +HFI1_SC2VL_ATTR (11 ); +HFI1_SC2VL_ATTR (12 ); +HFI1_SC2VL_ATTR (13 ); +HFI1_SC2VL_ATTR (14 ); +HFI1_SC2VL_ATTR (15 ); +HFI1_SC2VL_ATTR (16 ); +HFI1_SC2VL_ATTR (17 ); +HFI1_SC2VL_ATTR (18 ); +HFI1_SC2VL_ATTR (19 ); +HFI1_SC2VL_ATTR (20 ); +HFI1_SC2VL_ATTR (21 ); +HFI1_SC2VL_ATTR (22 ); +HFI1_SC2VL_ATTR (23 ); +HFI1_SC2VL_ATTR (24 ); +HFI1_SC2VL_ATTR (25 ); +HFI1_SC2VL_ATTR (26 ); +HFI1_SC2VL_ATTR (27 ); +HFI1_SC2VL_ATTR (28 ); +HFI1_SC2VL_ATTR (29 ); +HFI1_SC2VL_ATTR (30 ); +HFI1_SC2VL_ATTR (31 ); + + +static struct attribute *sc2vl_default_attributes[] = { + &hfi1_sc2vl_attr_0.attr, + &hfi1_sc2vl_attr_1.attr, + &hfi1_sc2vl_attr_2.attr, + &hfi1_sc2vl_attr_3.attr, + &hfi1_sc2vl_attr_4.attr, + &hfi1_sc2vl_attr_5.attr, + &hfi1_sc2vl_attr_6.attr, + &hfi1_sc2vl_attr_7.attr, + &hfi1_sc2vl_attr_8.attr, + &hfi1_sc2vl_attr_9.attr, + &hfi1_sc2vl_attr_10.attr, + &hfi1_sc2vl_attr_11.attr, + &hfi1_sc2vl_attr_12.attr, + &hfi1_sc2vl_attr_13.attr, + &hfi1_sc2vl_attr_14.attr, + &hfi1_sc2vl_attr_15.attr, + &hfi1_sc2vl_attr_16.attr, + &hfi1_sc2vl_attr_17.attr, + &hfi1_sc2vl_attr_18.attr, + &hfi1_sc2vl_attr_19.attr, + &hfi1_sc2vl_attr_20.attr, + &hfi1_sc2vl_attr_21.attr, + &hfi1_sc2vl_attr_22.attr, + &hfi1_sc2vl_attr_23.attr, + &hfi1_sc2vl_attr_24.attr, + &hfi1_sc2vl_attr_25.attr, + &hfi1_sc2vl_attr_26.attr, + &hfi1_sc2vl_attr_27.attr, + &hfi1_sc2vl_attr_28.attr, + &hfi1_sc2vl_attr_29.attr, + &hfi1_sc2vl_attr_30.attr, + &hfi1_sc2vl_attr_31.attr, +NULL +}; + +static ssize_t sc2vl_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ +struct hfi1_sc2vl_attr *sattr = + container_of(attr, struct hfi1_sc2vl_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, sc2vl_kobj); +struct hfi1_devdata *dd = ppd->dd; + +return sprintf (buf,"%u\n" , *((u8 *)dd->sc2vl + sattr->sc)); +} + +static const struct sysfs_ops hfi1_sc2vl_ops = { + .show = sc2vl_attr_show, +}; + +static struct kobj_type hfi1_sc2vl_ktype = { + .release = port_release, + .sysfs_ops = &hfi1_sc2vl_ops, + .default_attrs = sc2vl_default_attributes +}; + +/* End sc2vl */ + +/* Start sl2sc */ +#define HFI1_SL2SC_ATTR (N ) \ + static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = { \ + .attr = { .name = __stringify(N), .mode = 0444 }, \ + .sl = N \ + } + +struct hfi1_sl2sc_attr { +struct attribute attr; +int sl; +}; + +HFI1_SL2SC_ATTR (0 ); +HFI1_SL2SC_ATTR (1 ); +HFI1_SL2SC_ATTR (2 ); +HFI1_SL2SC_ATTR (3 ); +HFI1_SL2SC_ATTR (4 ); +HFI1_SL2SC_ATTR (5 ); +HFI1_SL2SC_ATTR (6 ); +HFI1_SL2SC_ATTR (7 ); +HFI1_SL2SC_ATTR (8 ); +HFI1_SL2SC_ATTR (9 ); +HFI1_SL2SC_ATTR (10 ); +HFI1_SL2SC_ATTR (11 ); +HFI1_SL2SC_ATTR (12 ); +HFI1_SL2SC_ATTR (13 ); +HFI1_SL2SC_ATTR (14 ); +HFI1_SL2SC_ATTR (15 ); +HFI1_SL2SC_ATTR (16 ); +HFI1_SL2SC_ATTR (17 ); +HFI1_SL2SC_ATTR (18 ); +HFI1_SL2SC_ATTR (19 ); +HFI1_SL2SC_ATTR (20 ); +HFI1_SL2SC_ATTR (21 ); +HFI1_SL2SC_ATTR (22 ); +HFI1_SL2SC_ATTR (23 ); +HFI1_SL2SC_ATTR (24 ); +HFI1_SL2SC_ATTR (25 ); +HFI1_SL2SC_ATTR (26 ); +HFI1_SL2SC_ATTR (27 ); +HFI1_SL2SC_ATTR (28 ); +HFI1_SL2SC_ATTR (29 ); +HFI1_SL2SC_ATTR (30 ); +HFI1_SL2SC_ATTR (31 ); + + +static struct attribute *sl2sc_default_attributes[] = { + &hfi1_sl2sc_attr_0.attr, + &hfi1_sl2sc_attr_1.attr, + &hfi1_sl2sc_attr_2.attr, + &hfi1_sl2sc_attr_3.attr, + &hfi1_sl2sc_attr_4.attr, + &hfi1_sl2sc_attr_5.attr, + &hfi1_sl2sc_attr_6.attr, + &hfi1_sl2sc_attr_7.attr, + &hfi1_sl2sc_attr_8.attr, + &hfi1_sl2sc_attr_9.attr, + &hfi1_sl2sc_attr_10.attr, + &hfi1_sl2sc_attr_11.attr, + &hfi1_sl2sc_attr_12.attr, + &hfi1_sl2sc_attr_13.attr, + &hfi1_sl2sc_attr_14.attr, + &hfi1_sl2sc_attr_15.attr, + &hfi1_sl2sc_attr_16.attr, + &hfi1_sl2sc_attr_17.attr, + &hfi1_sl2sc_attr_18.attr, + &hfi1_sl2sc_attr_19.attr, + &hfi1_sl2sc_attr_20.attr, + &hfi1_sl2sc_attr_21.attr, + &hfi1_sl2sc_attr_22.attr, + &hfi1_sl2sc_attr_23.attr, + &hfi1_sl2sc_attr_24.attr, + &hfi1_sl2sc_attr_25.attr, + &hfi1_sl2sc_attr_26.attr, + &hfi1_sl2sc_attr_27.attr, + &hfi1_sl2sc_attr_28.attr, + &hfi1_sl2sc_attr_29.attr, + &hfi1_sl2sc_attr_30.attr, + &hfi1_sl2sc_attr_31.attr, +NULL +}; + +static ssize_t sl2sc_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ +struct hfi1_sl2sc_attr *sattr = + container_of(attr, struct hfi1_sl2sc_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, sl2sc_kobj); +struct hfi1_ibport *ibp = &ppd->ibport_data; + +return sprintf (buf,"%u\n" , ibp->sl_to_sc[sattr->sl]); +} + +static const struct sysfs_ops hfi1_sl2sc_ops = { + .show = sl2sc_attr_show, +}; + +static struct kobj_type hfi1_sl2sc_ktype = { + .release = port_release, + .sysfs_ops = &hfi1_sl2sc_ops, + .default_attrs = sl2sc_default_attributes +}; + +/* End sl2sc */ + +/* Start vl2mtu */ + +#define HFI1_VL2MTU_ATTR (N ) \ + static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = { \ + .attr = { .name = __stringify(N), .mode = 0444 }, \ + .vl = N \ + } + +struct hfi1_vl2mtu_attr { +struct attribute attr; +int vl; +}; + +HFI1_VL2MTU_ATTR (0 ); +HFI1_VL2MTU_ATTR (1 ); +HFI1_VL2MTU_ATTR (2 ); +HFI1_VL2MTU_ATTR (3 ); +HFI1_VL2MTU_ATTR (4 ); +HFI1_VL2MTU_ATTR (5 ); +HFI1_VL2MTU_ATTR (6 ); +HFI1_VL2MTU_ATTR (7 ); +HFI1_VL2MTU_ATTR (8 ); +HFI1_VL2MTU_ATTR (9 ); +HFI1_VL2MTU_ATTR (10 ); +HFI1_VL2MTU_ATTR (11 ); +HFI1_VL2MTU_ATTR (12 ); +HFI1_VL2MTU_ATTR (13 ); +HFI1_VL2MTU_ATTR (14 ); +HFI1_VL2MTU_ATTR (15 ); + +static struct attribute *vl2mtu_default_attributes[] = { + &hfi1_vl2mtu_attr_0.attr, + &hfi1_vl2mtu_attr_1.attr, + &hfi1_vl2mtu_attr_2.attr, + &hfi1_vl2mtu_attr_3.attr, + &hfi1_vl2mtu_attr_4.attr, + &hfi1_vl2mtu_attr_5.attr, + &hfi1_vl2mtu_attr_6.attr, + &hfi1_vl2mtu_attr_7.attr, + &hfi1_vl2mtu_attr_8.attr, + &hfi1_vl2mtu_attr_9.attr, + &hfi1_vl2mtu_attr_10.attr, + &hfi1_vl2mtu_attr_11.attr, + &hfi1_vl2mtu_attr_12.attr, + &hfi1_vl2mtu_attr_13.attr, + &hfi1_vl2mtu_attr_14.attr, + &hfi1_vl2mtu_attr_15.attr, +NULL +}; + +static ssize_t vl2mtu_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ +struct hfi1_vl2mtu_attr *vlattr = + container_of(attr, struct hfi1_vl2mtu_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj); +struct hfi1_devdata *dd = ppd->dd; + +return sprintf (buf,"%u\n" , dd->vld[vlattr->vl].mtu); +} + +static const struct sysfs_ops hfi1_vl2mtu_ops = { + .show = vl2mtu_attr_show, +}; + +static struct kobj_type hfi1_vl2mtu_ktype = { + .release = port_release, + .sysfs_ops = &hfi1_vl2mtu_ops, + .default_attrs = vl2mtu_default_attributes +}; + +/* Start diag_counters */ +#define HFI1_DIAGC_NORMAL 0x0 +#define HFI1_DIAGC_PCPU 0x1 + +#define HFI1_DIAGC_ATTR (N ) \ + static struct hfi1_diagc_attr diagc_attr_##N = { \ + .attr = { .name = __stringify(N), .mode = 0664 }, \ + .counter = offsetof(struct hfi1_ibport, n_##N), \ + .type = HFI1_DIAGC_NORMAL, \ + .vl = CNTR_INVALID_VL \ + } + +#define HFI1_DIAGC_ATTR_PCPU (N ,V ,L ) \ + static struct hfi1_diagc_attr diagc_attr_##N = { \ + .attr = { .name = __stringify(N), .mode = 0664 }, \ + .counter = V, \ + .type = HFI1_DIAGC_PCPU, \ + .vl = L \ + } + +struct hfi1_diagc_attr { +struct attribute attr; +size_t counter; +int type; +int vl; +}; + +HFI1_DIAGC_ATTR (rc_resends); +HFI1_DIAGC_ATTR_PCPU (rc_acks,C_SW_CPU_RC_ACKS ,CNTR_INVALID_VL ); +HFI1_DIAGC_ATTR_PCPU (rc_qacks,C_SW_CPU_RC_QACKS ,CNTR_INVALID_VL ); +HFI1_DIAGC_ATTR_PCPU (rc_delayed_comp,C_SW_CPU_RC_DELAYED_COMP , +CNTR_INVALID_VL ); +HFI1_DIAGC_ATTR (seq_naks); +HFI1_DIAGC_ATTR (rdma_seq); +HFI1_DIAGC_ATTR (rnr_naks); +HFI1_DIAGC_ATTR (other_naks); +HFI1_DIAGC_ATTR (rc_timeouts); +HFI1_DIAGC_ATTR (loop_pkts); +HFI1_DIAGC_ATTR (pkt_drops); +HFI1_DIAGC_ATTR (dmawait); +HFI1_DIAGC_ATTR (unaligned); +HFI1_DIAGC_ATTR (rc_dupreq); +HFI1_DIAGC_ATTR (rc_seqnak); + +static struct attribute *diagc_default_attributes[] = { + &diagc_attr_rc_resends.attr, + &diagc_attr_rc_acks.attr, + &diagc_attr_rc_qacks.attr, + &diagc_attr_rc_delayed_comp.attr, + &diagc_attr_seq_naks.attr, + &diagc_attr_rdma_seq.attr, + &diagc_attr_rnr_naks.attr, + &diagc_attr_other_naks.attr, + &diagc_attr_rc_timeouts.attr, + &diagc_attr_loop_pkts.attr, + &diagc_attr_pkt_drops.attr, + &diagc_attr_dmawait.attr, + &diagc_attr_unaligned.attr, + &diagc_attr_rc_dupreq.attr, + &diagc_attr_rc_seqnak.attr, +NULL +}; + +static ssize_t diagc_attr_show (struct kobject *kobj,struct attribute *attr, +char *buf) +{ +struct hfi1_diagc_attr *dattr = + container_of(attr, struct hfi1_diagc_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, diagc_kobj); +struct hfi1_ibport *hfip = &ppd->ibport_data; + +switch (dattr->type) { +case (HFI1_DIAGC_PCPU ): +return (sprintf (buf,"%lld\n" , +read_port_cntr (ppd, + dattr->counter, + dattr->vl))); +case (HFI1_DIAGC_NORMAL ): +/* Fall through */ +default : +return sprintf (buf,"%u\n" , + *(u32 *)((char *)hfip + dattr->counter)); + } +} + +static ssize_t diagc_attr_store (struct kobject *kobj,struct attribute *attr, +const char *buf,size_t size) +{ +struct hfi1_diagc_attr *dattr = + container_of(attr, struct hfi1_diagc_attr, attr); +struct hfi1_pportdata *ppd = + container_of(kobj, struct hfi1_pportdata, diagc_kobj); +struct hfi1_ibport *hfip = &ppd->ibport_data; +u32 val; +int ret; + + ret =kstrtou32 (buf,0 , &val); +if (ret) +return ret; + *(u32 *)((char *)hfip + dattr->counter) = val; +return size; +} + +static const struct sysfs_ops hfi1_diagc_ops = { + .show = diagc_attr_show, + .store = diagc_attr_store, +}; + +static struct kobj_type diagc_ktype = { + .release = port_release, + .sysfs_ops = &hfi1_diagc_ops, + .default_attrs = diagc_default_attributes +}; + +/* End diag_counters */ + +/* end of per-port file structures and support code */ + +/* +* Start of per-unit (or driver, in some cases, but replicated +* per unit) functions (these get a device *) +*/ +static ssize_t show_rev (struct device *device,struct device_attribute *attr, +char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); + +return sprintf (buf,"%x\n" ,dd_from_dev (dev)->minrev); +} + +static ssize_t show_hca (struct device *device,struct device_attribute *attr, +char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); +int ret; + +if (!dd->boardname) + ret = -EINVAL ; +else + ret =scnprintf (buf,PAGE_SIZE ,"%s\n" , dd->boardname); +return ret; +} + +static ssize_t show_version (struct device *device, +struct device_attribute *attr,char *buf) +{ +/* The string printed here is already newline-terminated. */ +return scnprintf (buf,PAGE_SIZE ,"%s" , (char *)ib_hfi1_version); +} + +static ssize_t show_boardversion (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); + +/* The string printed here is already newline-terminated. */ +return scnprintf (buf,PAGE_SIZE ,"%s" , dd->boardversion); +} + + +static ssize_t show_localbus_info (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); + +return scnprintf (buf,PAGE_SIZE ,"%s\n" , dd->lbus_info); +} + + +static ssize_t show_nctxts (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); + +/* +* Return the smaller of send and receive contexts. +* Normally, user level applications would require both a send +* and a receive context, so returning the smaller of the two counts +* give a more accurate picture of total contexts available. +*/ +return scnprintf (buf,PAGE_SIZE ,"%u\n" , +min (dd->num_rcv_contexts - dd->first_user_ctxt, + (u32 )dd->sc_sizes[SC_USER ].count)); +} + +static ssize_t show_nfreectxts (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); + +/* Return the number of free user ports (contexts) available. */ +return scnprintf (buf,PAGE_SIZE ,"%u\n" , dd->freectxts); +} + +static ssize_t show_serial (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); + +return scnprintf (buf,PAGE_SIZE ,"%s" , dd->serial); + +} + +static ssize_t store_chip_reset (struct device *device, +struct device_attribute *attr,const char *buf, +size_t count) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); +int ret; + +if (count <5 ||memcmp (buf,"reset" ,5 ) || !dd->diag_client) { + ret = -EINVAL ; + goto bail; + } + + ret =hfi1_reset_device (dd->unit); +bail: +return ret <0 ? ret : count; +} + +/* +* Convert the reported temperature from an integer (reported in +* units of 0.25C) to a floating point number. +*/ +#define temp2str (temp, buf, size, idx) \ + scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ", \ + ((temp) >> 2), ((temp) & 0x3) * 25) + +/* +* Dump tempsense values, in decimal, to ease shell-scripts. +*/ +static ssize_t show_tempsense (struct device *device, +struct device_attribute *attr,char *buf) +{ +struct hfi1_ibdev *dev = + container_of(device, struct hfi1_ibdev, ibdev.dev); +struct hfi1_devdata *dd =dd_from_dev (dev); +struct hfi1_temp temp; +int ret = -ENXIO ; + + ret =hfi1_tempsense_rd (dd, &temp); +if (!ret) { +int idx =0 ; + + idx +=temp2str (temp.curr, buf,PAGE_SIZE , idx); + idx +=temp2str (temp.lo_lim, buf,PAGE_SIZE , idx); + idx +=temp2str (temp.hi_lim, buf,PAGE_SIZE , idx); + idx +=temp2str (temp.crit_lim, buf,PAGE_SIZE , idx); + idx +=scnprintf (buf + idx,PAGE_SIZE - idx, +"%u %u %u\n" , temp.triggers &0x1 , + temp.triggers &0x2 , temp.triggers &0x4 ); + ret = idx; + } +return ret; +} + +/* +* end of per-unit (or driver, in some cases, but replicated +* per unit) functions +*/ + +/* start of per-unit file structures and support code */ +static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); +static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); +static DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL); +static DEVICE_ATTR(version, S_IRUGO, show_version, NULL); +static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL); +static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL); +static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL); +static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL); +static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL); +static DEVICE_ATTR(localbus_info, S_IRUGO, show_localbus_info, NULL); +static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset); + +static struct device_attribute *hfi1_attributes[] = { + &dev_attr_hw_rev, + &dev_attr_hca_type, + &dev_attr_board_id, + &dev_attr_version, + &dev_attr_nctxts, + &dev_attr_nfreectxts, + &dev_attr_serial, + &dev_attr_boardversion, + &dev_attr_tempsense, + &dev_attr_localbus_info, + &dev_attr_chip_reset, +}; + +int hfi1_create_port_files (struct ib_device *ibdev,u8 port_num, +struct kobject *kobj) +{ +struct hfi1_pportdata *ppd; +struct hfi1_devdata *dd =dd_from_ibdev (ibdev); +int ret; + +if (!port_num || port_num > dd->num_pports) { +dd_dev_err (dd, +"Skipping infiniband class with invalid port %u\n" , + port_num); + ret = -ENODEV ; + goto bail; + } + ppd = &dd->pport[port_num -1 ]; + + ret =kobject_init_and_add (&ppd->pport_kobj, &hfi1_port_ktype, kobj, +"linkcontrol" ); +if (ret) { +dd_dev_err (dd, +"Skipping linkcontrol sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail; + } +kobject_uevent (&ppd->pport_kobj,KOBJ_ADD ); + + ret =kobject_init_and_add (&ppd->sc2vl_kobj, &hfi1_sc2vl_ktype, kobj, +"sc2vl" ); +if (ret) { +dd_dev_err (dd, +"Skipping sc2vl sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_link; + } +kobject_uevent (&ppd->sc2vl_kobj,KOBJ_ADD ); + ret =kobject_init_and_add (&ppd->sl2sc_kobj, &hfi1_sl2sc_ktype, kobj, +"sl2sc" ); +if (ret) { +dd_dev_err (dd, +"Skipping sl2sc sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_link; + } +kobject_uevent (&ppd->sl2sc_kobj,KOBJ_ADD ); + + ret =kobject_init_and_add (&ppd->vl2mtu_kobj, &hfi1_vl2mtu_ktype, kobj, +"vl2mtu" ); +if (ret) { +dd_dev_err (dd, +"Skipping vl2mtu sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_sl; + } +kobject_uevent (&ppd->vl2mtu_kobj,KOBJ_ADD ); + + ret =kobject_init_and_add (&ppd->diagc_kobj, &diagc_ktype, kobj, +"diag_counters" ); +if (ret) { +dd_dev_err (dd, +"Skipping diag_counters sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_mtu; + } +kobject_uevent (&ppd->diagc_kobj,KOBJ_ADD ); + + ret =kobject_init_and_add (&ppd->pport_cc_kobj, &port_cc_ktype, + kobj,"CCMgtA" ); +if (ret) { +dd_dev_err (dd, +"Skipping Congestion Control sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_diagc; + } + +kobject_uevent (&ppd->pport_cc_kobj,KOBJ_ADD ); + + ret =sysfs_create_bin_file (&ppd->pport_cc_kobj, + &cc_setting_bin_attr); +if (ret) { +dd_dev_err (dd, +"Skipping Congestion Control setting sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_cc; + } + + ret =sysfs_create_bin_file (&ppd->pport_cc_kobj, + &cc_table_bin_attr); +if (ret) { +dd_dev_err (dd, +"Skipping Congestion Control table sysfs info, (err %d) port %u\n" , + ret, port_num); + goto bail_cc_entry_bin; + } + +dd_dev_info (dd, +"IB%u: Congestion Control Agent enabled for port %d\n" , + dd->unit, port_num); + +return 0 ; + +bail_cc_entry_bin: +sysfs_remove_bin_file (&ppd->pport_cc_kobj, &cc_setting_bin_attr); +bail_cc: +kobject_put (&ppd->pport_cc_kobj); +bail_diagc: +kobject_put (&ppd->diagc_kobj); +bail_mtu: +kobject_put (&ppd->vl2mtu_kobj); +bail_sl: +kobject_put (&ppd->sc2vl_kobj); +bail_link: +kobject_put (&ppd->pport_kobj); +bail: +return ret; +} + +/* +* Register and create our files in /sys/class/infiniband. +*/ +int hfi1_verbs_register_sysfs (struct hfi1_devdata *dd) +{ +struct ib_device *dev = &dd->verbs_dev.ibdev; +int i, ret; + +for (i =0 ; i <ARRAY_SIZE (hfi1_attributes); ++i) { + ret =device_create_file (&dev->dev, hfi1_attributes[i]); +if (ret) + goto bail; + } + +return 0 ; +bail: +for (i =0 ; i <ARRAY_SIZE (hfi1_attributes); ++i) +device_remove_file (&dev->dev, hfi1_attributes[i]); +return ret; +} + +/* +* Unregister and remove our files in /sys/class/infiniband. +*/ +void hfi1_verbs_unregister_sysfs (struct hfi1_devdata *dd) +{ +struct hfi1_pportdata *ppd; +int i; + +for (i =0 ; i < dd->num_pports; i++) { + ppd = &dd->pport[i]; + +sysfs_remove_bin_file (&ppd->pport_cc_kobj, + &cc_setting_bin_attr); +sysfs_remove_bin_file (&ppd->pport_cc_kobj, + &cc_table_bin_attr); +kobject_put (&ppd->pport_cc_kobj); +kobject_put (&ppd->vl2mtu_kobj); +kobject_put (&ppd->sc2vl_kobj); +kobject_put (&ppd->pport_kobj); + } +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 32/41 IB/hfi1: add tracepoint debug routines
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/trace.c | 211 +++++ drivers/infiniband/hw/hfi1/trace.h | 1390 ++++++++++++++++++++++++++++++++++++ 2 files changed, 1601 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/trace.c create mode 100644 drivers/infiniband/hw/hfi1/trace.h
drivers/infiniband/hw/hfi1/trace.c new file +211 -0
@@ -0,0 +1,211 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ +#define CREATE_TRACE_POINTS +#define HFI1_TRACE_DO_NOT_CREATE_INLINES +#include "trace.h" + +u8 ibhdr_exhdr_len (struct hfi1_ib_header *hdr) +{ +struct hfi1_other_headers *ohdr; +u8 opcode; +u8 lnh = (u8)(be16_to_cpu (hdr->lrh[0 ]) &3 ); + +if (lnh ==HFI1_LRH_BTH ) + ohdr = &hdr->u.oth; +else + ohdr = &hdr->u.l.oth; + opcode =be32_to_cpu (ohdr->bth[0 ]) >>24 ; +return hdr_len_by_opcode[opcode] ==0 ? +0 : hdr_len_by_opcode[opcode] - (12 +8 ); +} + +#define IMM_PRN "imm %d" +#define RETH_PRN "reth vaddr 0x%.16llx rkey 0x%.8x dlen 0x%.8x" +#define AETH_PRN "aeth syn 0x%.2x msn 0x%.8x" +#define DETH_PRN "deth qkey 0x%.8x sqpn 0x%.6x" +#define ATOMICACKETH_PRN "origdata %lld" +#define ATOMICETH_PRN "vaddr 0x%llx rkey 0x%.8x sdata %lld cdata %lld" + +#define OP (transport, op) IB_OPCODE_## transport ## _ ## op + +static u64 ib_u64_get (__be32 *p) +{ +return ((u64 )be32_to_cpu (p[0 ]) <<32 ) |be32_to_cpu (p[1 ]); +} + +const char *parse_everbs_hdrs ( +struct trace_seq *p, +u8 opcode, +void *ehdrs) +{ +union ib_ehdrs *eh = ehdrs; +const char *ret =trace_seq_buffer_ptr (p); + +switch (opcode) { +/* imm */ +case OP (RC ,SEND_LAST_WITH_IMMEDIATE ): +case OP (UC ,SEND_LAST_WITH_IMMEDIATE ): +case OP (RC ,SEND_ONLY_WITH_IMMEDIATE ): +case OP (UC ,SEND_ONLY_WITH_IMMEDIATE ): +case OP (RC ,RDMA_WRITE_LAST_WITH_IMMEDIATE ): +case OP (UC ,RDMA_WRITE_LAST_WITH_IMMEDIATE ): +trace_seq_printf (p,IMM_PRN , +be32_to_cpu (eh->imm_data)); +break ; +/* reth + imm */ +case OP (RC ,RDMA_WRITE_ONLY_WITH_IMMEDIATE ): +case OP (UC ,RDMA_WRITE_ONLY_WITH_IMMEDIATE ): +trace_seq_printf (p,RETH_PRN " " IMM_PRN , + (unsigned long long )ib_u64_get ( + (__be32 *)&eh->rc.reth.vaddr), +be32_to_cpu (eh->rc.reth.rkey), +be32_to_cpu (eh->rc.reth.length), +be32_to_cpu (eh->rc.imm_data)); +break ; +/* reth */ +case OP (RC ,RDMA_READ_REQUEST ): +case OP (RC ,RDMA_WRITE_FIRST ): +case OP (UC ,RDMA_WRITE_FIRST ): +case OP (RC ,RDMA_WRITE_ONLY ): +case OP (UC ,RDMA_WRITE_ONLY ): +trace_seq_printf (p,RETH_PRN , + (unsigned long long )ib_u64_get ( + (__be32 *)&eh->rc.reth.vaddr), +be32_to_cpu (eh->rc.reth.rkey), +be32_to_cpu (eh->rc.reth.length)); +break ; +case OP (RC ,RDMA_READ_RESPONSE_FIRST ): +case OP (RC ,RDMA_READ_RESPONSE_LAST ): +case OP (RC ,RDMA_READ_RESPONSE_ONLY ): +case OP (RC ,ACKNOWLEDGE ): +trace_seq_printf (p,AETH_PRN , +be32_to_cpu (eh->aeth) >>24 , +be32_to_cpu (eh->aeth) &HFI1_QPN_MASK ); +break ; +/* aeth + atomicacketh */ +case OP (RC ,ATOMIC_ACKNOWLEDGE ): +trace_seq_printf (p,AETH_PRN " " ATOMICACKETH_PRN , + (be32_to_cpu (eh->at.aeth) >>24 ) &0xff , +be32_to_cpu (eh->at.aeth) &HFI1_QPN_MASK , + (unsigned long long )ib_u64_get (eh->at.atomic_ack_eth)); +break ; +/* atomiceth */ +case OP (RC ,COMPARE_SWAP ): +case OP (RC ,FETCH_ADD ): +trace_seq_printf (p,ATOMICETH_PRN , + (unsigned long long )ib_u64_get (eh->atomic_eth.vaddr), + eh->atomic_eth.rkey, + (unsigned long long )ib_u64_get ( + (__be32 *)&eh->atomic_eth.swap_data), + (unsigned long long )ib_u64_get ( + (__be32 *)&eh->atomic_eth.compare_data)); +break ; +/* deth */ +case OP (UD ,SEND_ONLY ): +case OP (UD ,SEND_ONLY_WITH_IMMEDIATE ): +trace_seq_printf (p,DETH_PRN , +be32_to_cpu (eh->ud.deth[0 ]), +be32_to_cpu (eh->ud.deth[1 ]) &HFI1_QPN_MASK ); +break ; + } +trace_seq_putc (p,0 ); +return ret; +} + +const char *parse_sdma_flags ( +struct trace_seq *p, +u64 desc0,u64 desc1) +{ +const char *ret =trace_seq_buffer_ptr (p); +char flags[5 ] = {'x' ,'x' ,'x' ,'x' ,0 }; + + flags[0 ] = (desc1 &SDMA_DESC1_INT_REQ_FLAG ) ?'I' :'-' ; + flags[1 ] = (desc1 &SDMA_DESC1_HEAD_TO_HOST_FLAG ) ?'H' :'-' ; + flags[2 ] = (desc0 &SDMA_DESC0_FIRST_DESC_FLAG ) ?'F' :'-' ; + flags[3 ] = (desc0 &SDMA_DESC0_LAST_DESC_FLAG ) ?'L' :'-' ; +trace_seq_printf (p,"%s" , flags); +if (desc0 &SDMA_DESC0_FIRST_DESC_FLAG ) +trace_seq_printf (p," amode:%u aidx:%u alen:%u" , + (u8)((desc1 >>SDMA_DESC1_HEADER_MODE_SHIFT ) + &SDMA_DESC1_HEADER_MODE_MASK ), + (u8)((desc1 >>SDMA_DESC1_HEADER_INDEX_SHIFT ) + &SDMA_DESC1_HEADER_INDEX_MASK ), + (u8)((desc1 >>SDMA_DESC1_HEADER_DWS_SHIFT ) + &SDMA_DESC1_HEADER_DWS_MASK )); +return ret; +} + +const char *print_u32_array ( +struct trace_seq *p, +u32 *arr,int len) +{ +int i; +const char *ret =trace_seq_buffer_ptr (p); + +for (i =0 ; i < len ; i++) +trace_seq_printf (p,"%s%#x" , i ==0 ?"" :" " , arr[i]); +trace_seq_putc (p,0 ); +return ret; +} + +const char *print_u64_array ( +struct trace_seq *p, +u64 *arr,int len) +{ +int i; +const char *ret =trace_seq_buffer_ptr (p); + +for (i =0 ; i < len; i++) +trace_seq_printf (p,"%s0x%016llx" , i ==0 ?"" :" " , arr[i]); +trace_seq_putc (p,0 ); +return ret; +} + +#undef HFI1_TRACE_DO_NOT_CREATE_INLINES
drivers/infiniband/hw/hfi1/trace.h new file +1390 -0
1,390 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 33/41 IB/hfi1: add QSFP twsi routines
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/twsi.c | 518 +++++++++++++++++++++++++++++++++++++ drivers/infiniband/hw/hfi1/twsi.h | 68 +++++ 2 files changed, 586 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/twsi.c create mode 100644 drivers/infiniband/hw/hfi1/twsi.h
drivers/infiniband/hw/hfi1/twsi.c new file +518 -0
@@ -0,0 +1,518 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/delay.h> +#include <linux/pci.h> +#include <linux/vmalloc.h> + +#include "hfi.h" +#include "twsi.h" + +/* +* "Two Wire Serial Interface" support. +* +* Originally written for a not-quite-i2c serial eeprom, which is +* still used on some supported boards. Later boards have added a +* variety of other uses, most board-specific, so the bit-boffing +* part has been split off to this file, while the other parts +* have been moved to chip-specific files. +* +* We have also dropped all pretense of fully generic (e.g. pretend +* we don't know whether '1' is the higher voltage) interface, as +* the restrictions of the generic i2c interface (e.g. no access from +* driver itself) make it unsuitable for this use. +*/ + +#define READ_CMD 1 +#define WRITE_CMD 0 + +/** +* i2c_wait_for_writes - wait for a write +* @dd: the hfi1_ib device +* +* We use this instead of udelay directly, so we can make sure +* that previous register writes have been flushed all the way +* to the chip. Since we are delaying anyway, the cost doesn't +* hurt, and makes the bit twiddling more regular +*/ +static void i2c_wait_for_writes (struct hfi1_devdata *dd,u32 target) +{ +/* +* implicit read of EXTStatus is as good as explicit +* read of scratch, if all we want to do is flush +* writes. +*/ +hfi1_gpio_mod (dd, target,0 ,0 ,0 ); +rmb ();/* inlined, so prevent compiler reordering */ +} + +/* +* QSFP modules are allowed to hold SCL low for 500uSec. Allow twice that +* for "almost compliant" modules +*/ +#define SCL_WAIT_USEC 1000 + +/* BUF_WAIT is time bus must be free between STOP or ACK and to next START. +* Should be 20, but some chips need more. +*/ +#define TWSI_BUF_WAIT_USEC 60 + +static void scl_out (struct hfi1_devdata *dd,u32 target,u8 bit) +{ +u32 mask; + +udelay (1 ); + + mask =QSFP_HFI0_I2CCLK ; + +/* SCL is meant to be bare-drain, so never set "OUT", just DIR */ +hfi1_gpio_mod (dd, target,0 , bit ?0 : mask, mask); + +/* +* Allow for slow slaves by simple +* delay for falling edge, sampling on rise. +*/ +if (!bit) +udelay (2 ); +else { +int rise_usec; + +for (rise_usec =SCL_WAIT_USEC ; rise_usec >0 ; rise_usec -=2 ) { +if (mask &hfi1_gpio_mod (dd, target,0 ,0 ,0 )) +break ; +udelay (2 ); + } +if (rise_usec <=0 ) +dd_dev_err (dd,"SCL interface stuck low > %d uSec\n" , +SCL_WAIT_USEC ); + } +i2c_wait_for_writes (dd, target); +} + +static void sda_out (struct hfi1_devdata *dd,u32 target,u8 bit) +{ +u32 mask; + + mask =QSFP_HFI0_I2CDAT ; + +/* SDA is meant to be bare-drain, so never set "OUT", just DIR */ +hfi1_gpio_mod (dd, target,0 , bit ?0 : mask, mask); + +i2c_wait_for_writes (dd, target); +udelay (2 ); +} + +static u8 sda_in (struct hfi1_devdata *dd,u32 target,int wait) +{ +u32 read_val, mask; + + mask =QSFP_HFI0_I2CDAT ; +/* SDA is meant to be bare-drain, so never set "OUT", just DIR */ +hfi1_gpio_mod (dd, target,0 ,0 , mask); + read_val =hfi1_gpio_mod (dd, target,0 ,0 ,0 ); +if (wait) +i2c_wait_for_writes (dd, target); +return (read_val & mask) >>GPIO_SDA_NUM ; +} + +/** +* i2c_ackrcv - see if ack following write is true +* @dd: the hfi1_ib device +*/ +static int i2c_ackrcv (struct hfi1_devdata *dd,u32 target) +{ +u8 ack_received; + +/* AT ENTRY SCL = LOW */ +/* change direction, ignore data */ + ack_received =sda_in (dd, target,1 ); +scl_out (dd, target,1 ); + ack_received =sda_in (dd, target,1 ) ==0 ; +scl_out (dd, target,0 ); +return ack_received; +} + +static void stop_cmd (struct hfi1_devdata *dd,u32 target); + +/** +* rd_byte - read a byte, sending STOP on last, else ACK +* @dd: the hfi1_ib device +* +* Returns byte shifted out of device +*/ +static int rd_byte (struct hfi1_devdata *dd,u32 target,int last) +{ +int bit_cntr, data; + + data =0 ; + +for (bit_cntr =7 ; bit_cntr >=0 ; --bit_cntr) { + data <<=1 ; +scl_out (dd, target,1 ); + data |=sda_in (dd, target,0 ); +scl_out (dd, target,0 ); + } +if (last) { +scl_out (dd, target,1 ); +stop_cmd (dd, target); + }else { +sda_out (dd, target,0 ); +scl_out (dd, target,1 ); +scl_out (dd, target,0 ); +sda_out (dd, target,1 ); + } +return data; +} + +/** +* wr_byte - write a byte, one bit at a time +* @dd: the hfi1_ib device +* @data: the byte to write +* +* Returns 0 if we got the following ack, otherwise 1 +*/ +static int wr_byte (struct hfi1_devdata *dd,u32 target,u8 data) +{ +int bit_cntr; +u8 bit; + +for (bit_cntr =7 ; bit_cntr >=0 ; bit_cntr--) { + bit = (data >> bit_cntr) &1 ; +sda_out (dd, target, bit); +scl_out (dd, target,1 ); +scl_out (dd, target,0 ); + } +return (!i2c_ackrcv (dd, target)) ?1 :0 ; +} + +/* +* issue TWSI start sequence: +* (both clock/data high, clock high, data low while clock is high) +*/ +static void start_seq (struct hfi1_devdata *dd,u32 target) +{ +sda_out (dd, target,1 ); +scl_out (dd, target,1 ); +sda_out (dd, target,0 ); +udelay (1 ); +scl_out (dd, target,0 ); +} + +/** +* stop_seq - transmit the stop sequence +* @dd: the hfi1_ib device +* +* (both clock/data low, clock high, data high while clock is high) +*/ +static void stop_seq (struct hfi1_devdata *dd,u32 target) +{ +scl_out (dd, target,0 ); +sda_out (dd, target,0 ); +scl_out (dd, target,1 ); +sda_out (dd, target,1 ); +} + +/** +* stop_cmd - transmit the stop condition +* @dd: the hfi1_ib device +* +* (both clock/data low, clock high, data high while clock is high) +*/ +static void stop_cmd (struct hfi1_devdata *dd,u32 target) +{ +stop_seq (dd, target); +udelay (TWSI_BUF_WAIT_USEC ); +} + +/** +* hfi1_twsi_reset - reset I2C communication +* @dd: the hfi1_ib device +*/ + +int hfi1_twsi_reset (struct hfi1_devdata *dd,u32 target) +{ +int clock_cycles_left =9 ; +int was_high =0 ; +u32 pins, mask; + +/* Both SCL and SDA should be high. If not, there +* is something wrong. +*/ + mask =QSFP_HFI0_I2CCLK |QSFP_HFI0_I2CDAT ; + +/* +* Force pins to desired innocuous state. +* This is the default power-on state with out=0 and dir=0, +* So tri-stated and should be floating high (barring HW problems) +*/ +hfi1_gpio_mod (dd, target,0 ,0 , mask); + +/* +* Clock nine times to get all listeners into a sane state. +* If SDA does not go high at any point, we are wedged. +* One vendor recommends then issuing START followed by STOP. +* we cannot use our "normal" functions to do that, because +* if SCL drops between them, another vendor's part will +* wedge, dropping SDA and keeping it low forever, at the end of +* the next transaction (even if it was not the device addressed). +* So our START and STOP take place with SCL held high. +*/ +while (clock_cycles_left--) { +scl_out (dd, target,0 ); +scl_out (dd, target,1 ); +/* Note if SDA is high, but keep clocking to sync slave */ + was_high |=sda_in (dd, target,0 ); + } + +if (was_high) { +/* +* We saw a high, which we hope means the slave is sync'd. +* Issue START, STOP, pause for T_BUF. +*/ + + pins =hfi1_gpio_mod (dd, target,0 ,0 ,0 ); +if ((pins & mask) != mask) +dd_dev_err (dd,"GPIO pins not at rest: %d\n" , + pins & mask); +/* Drop SDA to issue START */ +udelay (1 );/* Guarantee .6 uSec setup */ +sda_out (dd, target,0 ); +udelay (1 );/* Guarantee .6 uSec hold */ +/* At this point, SCL is high, SDA low. Raise SDA for STOP */ +sda_out (dd, target,1 ); +udelay (TWSI_BUF_WAIT_USEC ); + } + +return !was_high; +} + +#define HFI1_TWSI_START 0x100 +#define HFI1_TWSI_STOP 0x200 + +/* Write byte to TWSI, optionally prefixed with START or suffixed with +* STOP. +* returns 0 if OK (ACK received), else != 0 +*/ +static int twsi_wr (struct hfi1_devdata *dd,u32 target,int data,int flags) +{ +int ret =1 ; + +if (flags &HFI1_TWSI_START ) +start_seq (dd, target); + +/* Leaves SCL low (from i2c_ackrcv()) */ + ret =wr_byte (dd, target, data); + +if (flags &HFI1_TWSI_STOP ) +stop_cmd (dd, target); +return ret; +} + +/* Added functionality for IBA7220-based cards */ +#define HFI1_TEMP_DEV 0x98 + +/* +* hfi1_twsi_blk_rd +* General interface for data transfer from twsi devices. +* One vestige of its former role is that it recognizes a device +* HFI1_TWSI_NO_DEV and does the correct operation for the legacy part, +* which responded to all TWSI device codes, interpreting them as +* address within device. On all other devices found on board handled by +* this driver, the device is followed by a one-byte "address" which selects +* the "register" or "offset" within the device from which data should +* be read. +*/ +int hfi1_twsi_blk_rd (struct hfi1_devdata *dd,u32 target,int dev,int addr, +void *buffer,int len) +{ +int ret; +u8 *bp = buffer; + + ret =1 ; + +if (dev ==HFI1_TWSI_NO_DEV ) { +/* legacy not-really-I2C */ + addr = (addr <<1 ) |READ_CMD ; + ret =twsi_wr (dd, target, addr,HFI1_TWSI_START ); + }else { +/* Actual I2C */ + ret =twsi_wr (dd, target, dev |WRITE_CMD ,HFI1_TWSI_START ); +if (ret) { +stop_cmd (dd, target); + ret =1 ; + goto bail; + } +/* +* SFF spec claims we do _not_ stop after the addr +* but simply issue a start with the "read" dev-addr. +* Since we are implicitly waiting for ACK here, +* we need t_buf (nominally 20uSec) before that start, +* and cannot rely on the delay built in to the STOP +*/ + ret =twsi_wr (dd, target, addr,0 ); +udelay (TWSI_BUF_WAIT_USEC ); + +if (ret) { +dd_dev_err (dd, +"Failed to write interface read addr %02X\n" , + addr); + ret =1 ; + goto bail; + } + ret =twsi_wr (dd, target, dev |READ_CMD ,HFI1_TWSI_START ); + } +if (ret) { +stop_cmd (dd, target); + ret =1 ; + goto bail; + } + +/* +* block devices keeps clocking data out as long as we ack, +* automatically incrementing the address. Some have "pages" +* whose boundaries will not be crossed, but the handling +* of these is left to the caller, who is in a better +* position to know. +*/ +while (len-- >0 ) { +/* +* Get and store data, sending ACK if length remaining, +* else STOP +*/ + *bp++ =rd_byte (dd, target, !len); + } + + ret =0 ; + +bail: +return ret; +} + +/* +* hfi1_twsi_blk_wr +* General interface for data transfer to twsi devices. +* One vestige of its former role is that it recognizes a device +* HFI1_TWSI_NO_DEV and does the correct operation for the legacy part, +* which responded to all TWSI device codes, interpreting them as +* address within device. On all other devices found on board handled by +* this driver, the device is followed by a one-byte "address" which selects +* the "register" or "offset" within the device to which data should +* be written. +*/ +int hfi1_twsi_blk_wr (struct hfi1_devdata *dd,u32 target,int dev,int addr, +const void *buffer,int len) +{ +int sub_len; +const u8 *bp = buffer; +int max_wait_time, i; +int ret =1 ; + +while (len >0 ) { +if (dev ==HFI1_TWSI_NO_DEV ) { +if (twsi_wr (dd, target, (addr <<1 ) |WRITE_CMD , +HFI1_TWSI_START )) { + goto failed_write; + } + }else { +/* Real I2C */ +if (twsi_wr (dd, target, + dev |WRITE_CMD ,HFI1_TWSI_START )) + goto failed_write; + ret =twsi_wr (dd, target, addr,0 ); +if (ret) { +dd_dev_err (dd, +"Failed to write interface write addr %02X\n" , + addr); + goto failed_write; + } + } + + sub_len =min (len,4 ); + addr += sub_len; + len -= sub_len; + +for (i =0 ; i < sub_len; i++) +if (twsi_wr (dd, target, *bp++,0 )) + goto failed_write; + +stop_cmd (dd, target); + +/* +* Wait for write complete by waiting for a successful +* read (the chip replies with a zero after the write +* cmd completes, and before it writes to the eeprom. +* The startcmd for the read will fail the ack until +* the writes have completed. We do this inline to avoid +* the debug prints that are in the real read routine +* if the startcmd fails. +* We also use the proper device address, so it doesn't matter +* whether we have real eeprom_dev. Legacy likes any address. +*/ + max_wait_time =100 ; +while (twsi_wr (dd, target, + dev |READ_CMD ,HFI1_TWSI_START )) { +stop_cmd (dd, target); +if (!--max_wait_time) + goto failed_write; + } +/* now read (and ignore) the resulting byte */ +rd_byte (dd, target,1 ); + } + + ret =0 ; + goto bail; + +failed_write: +stop_cmd (dd, target); + ret =1 ; + +bail: +return ret; +}
drivers/infiniband/hw/hfi1/twsi.h new file +68 -0
68 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 34/41 IB/hfi1: add UC QP handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/uc.c | 601 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 601 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/uc.c
drivers/infiniband/hw/hfi1/uc.c new file +601 -0
@@ -0,0 +1,601 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include "hfi.h" +#include "sdma.h" + +/* cut down ridiculously long IB macro names */ +#define OP (x) IB_OPCODE_UC_##x + +/** +* hfi1_make_uc_req - construct a request packet (SEND, RDMA write) +* @qp: a pointer to the QP +* +* Return 1 if constructed; otherwise, return 0. +*/ +int hfi1_make_uc_req (struct hfi1_qp *qp) +{ +struct hfi1_other_headers *ohdr; +struct hfi1_swqe *wqe; +unsigned long flags; +u32 hwords =5 ; +u32 bth0 =0 ; +u32 len; +u32 pmtu = qp->pmtu; +int ret =0 ; +int middle =0 ; + +spin_lock_irqsave (&qp->s_lock, flags); + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_SEND_OK )) { +if (!(ib_hfi1_state_ops[qp->state] &HFI1_FLUSH_SEND )) + goto bail; +/* We are in the error state, flush the work request. */ +if (qp->s_last == qp->s_head) + goto bail; +/* If DMAs are in progress, we can't flush immediately. */ +if (atomic_read (&qp->s_iowait.sdma_busy)) { + qp->s_flags |=HFI1_S_WAIT_DMA ; + goto bail; + } +clear_ahg (qp); + wqe =get_swqe_ptr (qp, qp->s_last); +hfi1_send_complete (qp, wqe,IB_WC_WR_FLUSH_ERR ); + goto done; + } + + ohdr = &qp->s_hdr->ibh.u.oth; +if (qp->remote_ah_attr.ah_flags &IB_AH_GRH ) + ohdr = &qp->s_hdr->ibh.u.l.oth; + +/* Get the next send request. */ + wqe =get_swqe_ptr (qp, qp->s_cur); + qp->s_wqe =NULL ; +switch (qp->s_state) { +default : +if (!(ib_hfi1_state_ops[qp->state] & +HFI1_PROCESS_NEXT_SEND_OK )) + goto bail; +/* Check if send work queue is empty. */ +if (qp->s_cur == qp->s_head) { +clear_ahg (qp); + goto bail; + } +/* +* Start a new request. +*/ + wqe->psn = qp->s_next_psn; + qp->s_psn = qp->s_next_psn; + qp->s_sge.sge = wqe->sg_list[0 ]; + qp->s_sge.sg_list = wqe->sg_list +1 ; + qp->s_sge.num_sge = wqe->wr.num_sge; + qp->s_sge.total_len = wqe->length; + len = wqe->length; + qp->s_len = len; +switch (wqe->wr.opcode) { +case IB_WR_SEND : +case IB_WR_SEND_WITH_IMM : +if (len > pmtu) { + qp->s_state =OP (SEND_FIRST ); + len = pmtu; +break ; + } +if (wqe->wr.opcode ==IB_WR_SEND ) + qp->s_state =OP (SEND_ONLY ); +else { + qp->s_state = +OP (SEND_ONLY_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; + } +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + qp->s_wqe = wqe; +if (++qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + +case IB_WR_RDMA_WRITE : +case IB_WR_RDMA_WRITE_WITH_IMM : + ohdr->u.rc.reth.vaddr = +cpu_to_be64 (wqe->wr.wr.rdma.remote_addr); + ohdr->u.rc.reth.rkey = +cpu_to_be32 (wqe->wr.wr.rdma.rkey); + ohdr->u.rc.reth.length =cpu_to_be32 (len); + hwords +=sizeof (struct ib_reth ) /4 ; +if (len > pmtu) { + qp->s_state =OP (RDMA_WRITE_FIRST ); + len = pmtu; +break ; + } +if (wqe->wr.opcode ==IB_WR_RDMA_WRITE ) + qp->s_state =OP (RDMA_WRITE_ONLY ); +else { + qp->s_state = +OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE ); +/* Immediate data comes after the RETH */ + ohdr->u.rc.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + } + qp->s_wqe = wqe; +if (++qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + +default : + goto bail; + } +break ; + +case OP (SEND_FIRST ): + qp->s_state =OP (SEND_MIDDLE ); +/* FALLTHROUGH */ +case OP (SEND_MIDDLE ): + len = qp->s_len; +if (len > pmtu) { + len = pmtu; + middle =HFI1_CAP_IS_KSET (SDMA_AHG ); +break ; + } +if (wqe->wr.opcode ==IB_WR_SEND ) + qp->s_state =OP (SEND_LAST ); +else { + qp->s_state =OP (SEND_LAST_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; + } +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + qp->s_wqe = wqe; +if (++qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + +case OP (RDMA_WRITE_FIRST ): + qp->s_state =OP (RDMA_WRITE_MIDDLE ); +/* FALLTHROUGH */ +case OP (RDMA_WRITE_MIDDLE ): + len = qp->s_len; +if (len > pmtu) { + len = pmtu; + middle =HFI1_CAP_IS_KSET (SDMA_AHG ); +break ; + } +if (wqe->wr.opcode ==IB_WR_RDMA_WRITE ) + qp->s_state =OP (RDMA_WRITE_LAST ); +else { + qp->s_state = +OP (RDMA_WRITE_LAST_WITH_IMMEDIATE ); +/* Immediate data comes after the BTH */ + ohdr->u.imm_data = wqe->wr.ex.imm_data; + hwords +=1 ; +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + } + qp->s_wqe = wqe; +if (++qp->s_cur >= qp->s_size) + qp->s_cur =0 ; +break ; + } + qp->s_len -= len; + qp->s_hdrwords = hwords; + qp->s_cur_sge = &qp->s_sge; + qp->s_cur_size = len; +hfi1_make_ruc_header (qp, ohdr, bth0 | (qp->s_state <<24 ), +mask_psn (qp->s_next_psn++), middle); +done: + ret =1 ; + goto unlock; + +bail: + qp->s_flags &= ~HFI1_S_BUSY ; +unlock: +spin_unlock_irqrestore (&qp->s_lock, flags); +return ret; +} + +/** +* hfi1_uc_rcv - handle an incoming UC packet +* @ibp: the port the packet came in on +* @hdr: the header of the packet +* @rcv_flags: flags relevant to rcv processing +* @data: the packet data +* @tlen: the length of the packet +* @qp: the QP for this packet. +* +* This is called from qp_rcv() to process an incoming UC packet +* for the given QP. +* Called at interrupt level. +*/ +void hfi1_uc_rcv (struct hfi1_ibport *ibp,struct hfi1_ib_header *hdr, +u32 rcv_flags,void *data,u32 tlen,struct hfi1_qp *qp) +{ +struct hfi1_other_headers *ohdr; +u32 opcode; +u32 hdrsize; +u32 psn; +u32 pad; +struct ib_wc wc; +u32 pmtu = qp->pmtu; +struct ib_reth *reth; +int has_grh = !!(rcv_flags &HFI1_HAS_GRH ); +int is_becn, is_fecn, ret; +struct ib_grh *grh =NULL ; + +/* Check for GRH */ +if (!has_grh) { + ohdr = &hdr->u.oth; + hdrsize =8 +12 ;/* LRH + BTH */ + }else { + ohdr = &hdr->u.l.oth; + hdrsize =8 +40 +12 ;/* LRH + GRH + BTH */ + grh = &hdr->u.l.grh; + } + + opcode =be32_to_cpu (ohdr->bth[0 ]); +if (hfi1_ruc_check_hdr (ibp, hdr, has_grh, qp, opcode)) +return ; + + is_becn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_BECN_SHIFT ) + &HFI1_BECN_MASK ; + is_fecn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_FECN_SHIFT ) + &HFI1_FECN_MASK ; + +if (is_becn) { +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +u32 rqpn, lqpn; +u16 rlid =be16_to_cpu (hdr->lrh[3 ]); +u8 sl, sc5; + + lqpn =be32_to_cpu (ohdr->bth[1 ]) &HFI1_QPN_MASK ; + rqpn = qp->remote_qpn; + + sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + sl = ibp->sc_to_sl[sc5]; + +process_becn (ppd, sl, rlid, lqpn, rqpn,IB_CC_SVCTYPE_UC ); + } + +if (is_fecn) { +u16 pkey = (u16 )be32_to_cpu (ohdr->bth[0 ]); +u16 slid =be16_to_cpu (hdr->lrh[3 ]); +u16 dlid =be16_to_cpu (hdr->lrh[1 ]); +u32 src_qp = qp->remote_qpn; +u8 sc5; + + sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl]; + +return_cnp (ibp, qp, src_qp, pkey, dlid, slid, sc5, grh); + } + + psn =be32_to_cpu (ohdr->bth[2 ]); + opcode >>=24 ; + +/* Compare the PSN verses the expected PSN. */ +if (unlikely (cmp_psn (psn, qp->r_psn) !=0 )) { +/* +* Handle a sequence error. +* Silently drop any current message. +*/ + qp->r_psn = psn; +inv: +if (qp->r_state ==OP (SEND_FIRST ) || + qp->r_state ==OP (SEND_MIDDLE )) { +set_bit (HFI1_R_REWIND_SGE , &qp->r_aflags); + qp->r_sge.num_sge =0 ; + }else +hfi1_put_ss (&qp->r_sge); + qp->r_state =OP (SEND_LAST ); +switch (opcode) { +case OP (SEND_FIRST ): +case OP (SEND_ONLY ): +case OP (SEND_ONLY_WITH_IMMEDIATE ): + goto send_first; + +case OP (RDMA_WRITE_FIRST ): +case OP (RDMA_WRITE_ONLY ): +case OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE ): + goto rdma_first; + +default : + goto drop; + } + } + +/* Check for opcode sequence errors. */ +switch (qp->r_state) { +case OP (SEND_FIRST ): +case OP (SEND_MIDDLE ): +if (opcode ==OP (SEND_MIDDLE ) || + opcode ==OP (SEND_LAST ) || + opcode ==OP (SEND_LAST_WITH_IMMEDIATE )) +break ; + goto inv; + +case OP (RDMA_WRITE_FIRST ): +case OP (RDMA_WRITE_MIDDLE ): +if (opcode ==OP (RDMA_WRITE_MIDDLE ) || + opcode ==OP (RDMA_WRITE_LAST ) || + opcode ==OP (RDMA_WRITE_LAST_WITH_IMMEDIATE )) +break ; + goto inv; + +default : +if (opcode ==OP (SEND_FIRST ) || + opcode ==OP (SEND_ONLY ) || + opcode ==OP (SEND_ONLY_WITH_IMMEDIATE ) || + opcode ==OP (RDMA_WRITE_FIRST ) || + opcode ==OP (RDMA_WRITE_ONLY ) || + opcode ==OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE )) +break ; + goto inv; + } + +if (qp->state ==IB_QPS_RTR && !(qp->r_flags &HFI1_R_COMM_EST )) { + qp->r_flags |=HFI1_R_COMM_EST ; +if (qp->ibqp.event_handler) { +struct ib_event ev; + + ev.device = qp->ibqp.device; + ev.element.qp = &qp->ibqp; + ev.event =IB_EVENT_COMM_EST ; + qp->ibqp.event_handler (&ev, qp->ibqp.qp_context); + } + } + +/* OK, process the packet. */ +switch (opcode) { +case OP (SEND_FIRST ): +case OP (SEND_ONLY ): +case OP (SEND_ONLY_WITH_IMMEDIATE ): +send_first: +if (test_and_clear_bit (HFI1_R_REWIND_SGE , &qp->r_aflags)) + qp->r_sge = qp->s_rdma_read_sge; +else { + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) + goto op_err; +if (!ret) + goto drop; +/* +* qp->s_rdma_read_sge will be the owner +* of the mr references. +*/ + qp->s_rdma_read_sge = qp->r_sge; + } + qp->r_rcv_len =0 ; +if (opcode ==OP (SEND_ONLY )) + goto no_immediate_data; +else if (opcode ==OP (SEND_ONLY_WITH_IMMEDIATE )) + goto send_last_imm; +/* FALLTHROUGH */ +case OP (SEND_MIDDLE ): +/* Check for invalid length PMTU or posted rwqe len. */ +if (unlikely (tlen != (hdrsize + pmtu +4 ))) + goto rewind; + qp->r_rcv_len += pmtu; +if (unlikely (qp->r_rcv_len > qp->r_len)) + goto rewind; +hfi1_copy_sge (&qp->r_sge, data, pmtu,0 ); +break ; + +case OP (SEND_LAST_WITH_IMMEDIATE ): +send_last_imm: + wc.ex.imm_data = ohdr->u.imm_data; + hdrsize +=4 ; + wc.wc_flags =IB_WC_WITH_IMM ; + goto send_last; +case OP (SEND_LAST ): +no_immediate_data: + wc.ex.imm_data =0 ; + wc.wc_flags =0 ; +send_last: +/* Get the number of bytes the message was padded by. */ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +/* Check for invalid length. */ +/* LAST len should be >= 1 */ +if (unlikely (tlen < (hdrsize + pad +4 ))) + goto rewind; +/* Don't count the CRC. */ + tlen -= (hdrsize + pad +4 ); + wc.byte_len = tlen + qp->r_rcv_len; +if (unlikely (wc.byte_len > qp->r_len)) + goto rewind; + wc.opcode =IB_WC_RECV ; +hfi1_copy_sge (&qp->r_sge, data, tlen,0 ); +hfi1_put_ss (&qp->s_rdma_read_sge); +last_imm: + wc.wr_id = qp->r_wr_id; + wc.status =IB_WC_SUCCESS ; + wc.qp = &qp->ibqp; + wc.src_qp = qp->remote_qpn; + wc.slid = qp->remote_ah_attr.dlid; +/* +* It seems that IB mandates the presence of an SL in a +* work completion only for the UD transport (see section +* 11.4.2 of IBTA Vol. 1). +* +* However, the way the SL is chosen below is consistent +* with the way that IB/qib works and is trying avoid +* introducing incompatibilities. +* +* See also OPA Vol. 1, section 9.7.6, and table 9-17. +*/ + wc.sl = qp->remote_ah_attr.sl; +/* zero fields that are N/A */ + wc.vendor_err =0 ; + wc.pkey_index =0 ; + wc.dlid_path_bits =0 ; + wc.port_num =0 ; +/* Signal completion event if the solicited bit is set. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc, + (ohdr->bth[0 ] & +cpu_to_be32 (IB_BTH_SOLICITED )) !=0 ); +break ; + +case OP (RDMA_WRITE_FIRST ): +case OP (RDMA_WRITE_ONLY ): +case OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE ):/* consume RWQE */ +rdma_first: +if (unlikely (!(qp->qp_access_flags & +IB_ACCESS_REMOTE_WRITE ))) { + goto drop; + } + reth = &ohdr->u.rc.reth; + hdrsize +=sizeof (*reth); + qp->r_len =be32_to_cpu (reth->length); + qp->r_rcv_len =0 ; + qp->r_sge.sg_list =NULL ; +if (qp->r_len !=0 ) { +u32 rkey =be32_to_cpu (reth->rkey); +u64 vaddr =be64_to_cpu (reth->vaddr); +int ok; + +/* Check rkey */ + ok =hfi1_rkey_ok (qp, &qp->r_sge.sge, qp->r_len, + vaddr, rkey,IB_ACCESS_REMOTE_WRITE ); +if (unlikely (!ok)) + goto drop; + qp->r_sge.num_sge =1 ; + }else { + qp->r_sge.num_sge =0 ; + qp->r_sge.sge.mr =NULL ; + qp->r_sge.sge.vaddr =NULL ; + qp->r_sge.sge.length =0 ; + qp->r_sge.sge.sge_length =0 ; + } +if (opcode ==OP (RDMA_WRITE_ONLY )) + goto rdma_last; +else if (opcode ==OP (RDMA_WRITE_ONLY_WITH_IMMEDIATE )) { + wc.ex.imm_data = ohdr->u.rc.imm_data; + goto rdma_last_imm; + } +/* FALLTHROUGH */ +case OP (RDMA_WRITE_MIDDLE ): +/* Check for invalid length PMTU or posted rwqe len. */ +if (unlikely (tlen != (hdrsize + pmtu +4 ))) + goto drop; + qp->r_rcv_len += pmtu; +if (unlikely (qp->r_rcv_len > qp->r_len)) + goto drop; +hfi1_copy_sge (&qp->r_sge, data, pmtu,1 ); +break ; + +case OP (RDMA_WRITE_LAST_WITH_IMMEDIATE ): + wc.ex.imm_data = ohdr->u.imm_data; +rdma_last_imm: + hdrsize +=4 ; + wc.wc_flags =IB_WC_WITH_IMM ; + +/* Get the number of bytes the message was padded by. */ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +/* Check for invalid length. */ +/* LAST len should be >= 1 */ +if (unlikely (tlen < (hdrsize + pad +4 ))) + goto drop; +/* Don't count the CRC. */ + tlen -= (hdrsize + pad +4 ); +if (unlikely (tlen + qp->r_rcv_len != qp->r_len)) + goto drop; +if (test_and_clear_bit (HFI1_R_REWIND_SGE , &qp->r_aflags)) +hfi1_put_ss (&qp->s_rdma_read_sge); +else { + ret =hfi1_get_rwqe (qp,1 ); +if (ret <0 ) + goto op_err; +if (!ret) + goto drop; + } + wc.byte_len = qp->r_len; + wc.opcode =IB_WC_RECV_RDMA_WITH_IMM ; +hfi1_copy_sge (&qp->r_sge, data, tlen,1 ); +hfi1_put_ss (&qp->r_sge); + goto last_imm; + +case OP (RDMA_WRITE_LAST ): +rdma_last: +/* Get the number of bytes the message was padded by. */ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +/* Check for invalid length. */ +/* LAST len should be >= 1 */ +if (unlikely (tlen < (hdrsize + pad +4 ))) + goto drop; +/* Don't count the CRC. */ + tlen -= (hdrsize + pad +4 ); +if (unlikely (tlen + qp->r_rcv_len != qp->r_len)) + goto drop; +hfi1_copy_sge (&qp->r_sge, data, tlen,1 ); +hfi1_put_ss (&qp->r_sge); +break ; + +default : +/* Drop packet for unknown opcodes. */ + goto drop; + } + qp->r_psn++; + qp->r_state = opcode; +return ; + +rewind: +set_bit (HFI1_R_REWIND_SGE , &qp->r_aflags); + qp->r_sge.num_sge =0 ; +drop: + ibp->n_pkt_drops++; +return ; + +op_err: +hfi1_rc_error (qp,IB_WC_LOC_QP_OP_ERR ); +return ; + +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 35/41 IB/hfi1: add UD QP handling
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/ud.c | 886 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 886 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/ud.c
drivers/infiniband/hw/hfi1/ud.c new file +886 -0
@@ -0,0 +1,886 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/net.h> +#include <rdma/ib_smi.h> + +#include "hfi.h" +#include "mad.h" +#include "qp.h" + +/** +* ud_loopback - handle send on loopback QPs +* @sqp: the sending QP +* @swqe: the send work request +* +* This is called from hfi1_make_ud_req() to forward a WQE addressed +* to the same HCA. +* Note that the receive interrupt handler may be calling hfi1_ud_rcv() +* while this is being called. +*/ +static void ud_loopback (struct hfi1_qp *sqp,struct hfi1_swqe *swqe) +{ +struct hfi1_ibport *ibp =to_iport (sqp->ibqp.device, sqp->port_num); +struct hfi1_pportdata *ppd; +struct hfi1_qp *qp; +struct ib_ah_attr *ah_attr; +unsigned long flags; +struct hfi1_sge_state ssge; +struct hfi1_sge *sge; +struct ib_wc wc; +u32 length; +enum ib_qp_type sqptype, dqptype; + + qp =hfi1_lookup_qpn (ibp, swqe->wr.wr.ud.remote_qpn); +if (!qp) { + ibp->n_pkt_drops++; +return ; + } + + sqptype = sqp->ibqp.qp_type ==IB_QPT_GSI ? +IB_QPT_UD : sqp->ibqp.qp_type; + dqptype = qp->ibqp.qp_type ==IB_QPT_GSI ? +IB_QPT_UD : qp->ibqp.qp_type; + +if (dqptype != sqptype || + !(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_RECV_OK )) { + ibp->n_pkt_drops++; + goto drop; + } + + ah_attr = &to_iah (swqe->wr.wr.ud.ah)->attr; + ppd =ppd_from_ibp (ibp); + +if (qp->ibqp.qp_num >1 ) { +u16 pkey; +u16 slid; +u8 sc5 = ibp->sl_to_sc[ah_attr->sl]; + + pkey =hfi1_get_pkey (ibp, sqp->s_pkey_index); + slid = ppd->lid | (ah_attr->src_path_bits & + ((1 << ppd->lmc) -1 )); +if (unlikely (ingress_pkey_check (ppd, pkey, sc5, + qp->s_pkey_index, slid))) { +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_PKEY , pkey, + ah_attr->sl, + sqp->ibqp.qp_num, qp->ibqp.qp_num, +cpu_to_be16 (slid), +cpu_to_be16 (ah_attr->dlid)); + goto drop; + } + } + +/* +* Check that the qkey matches (except for QP0, see 9.6.1.4.1). +* Qkeys with the high order bit set mean use the +* qkey from the QP context instead of the WR (see 10.2.5). +*/ +if (qp->ibqp.qp_num) { +u32 qkey; + + qkey = (int )swqe->wr.wr.ud.remote_qkey <0 ? + sqp->qkey : swqe->wr.wr.ud.remote_qkey; +if (unlikely (qkey != qp->qkey)) { +u16 lid; + + lid = ppd->lid | (ah_attr->src_path_bits & + ((1 << ppd->lmc) -1 )); +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_QKEY , qkey, + ah_attr->sl, + sqp->ibqp.qp_num, qp->ibqp.qp_num, +cpu_to_be16 (lid), +cpu_to_be16 (ah_attr->dlid)); + goto drop; + } + } + +/* +* A GRH is expected to precede the data even if not +* present on the wire. +*/ + length = swqe->length; +memset (&wc,0 ,sizeof (wc)); + wc.byte_len = length +sizeof (struct ib_grh ); + +if (swqe->wr.opcode ==IB_WR_SEND_WITH_IMM ) { + wc.wc_flags =IB_WC_WITH_IMM ; + wc.ex.imm_data = swqe->wr.ex.imm_data; + } + +spin_lock_irqsave (&qp->r_lock, flags); + +/* +* Get the next work request entry to find where to put the data. +*/ +if (qp->r_flags &HFI1_R_REUSE_SGE ) + qp->r_flags &= ~HFI1_R_REUSE_SGE ; +else { +int ret; + + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) { +hfi1_rc_error (qp,IB_WC_LOC_QP_OP_ERR ); + goto bail_unlock; + } +if (!ret) { +if (qp->ibqp.qp_num ==0 ) + ibp->n_vl15_dropped++; + goto bail_unlock; + } + } +/* Silently drop packets which are too big. */ +if (unlikely (wc.byte_len > qp->r_len)) { + qp->r_flags |=HFI1_R_REUSE_SGE ; + ibp->n_pkt_drops++; + goto bail_unlock; + } + +if (ah_attr->ah_flags &IB_AH_GRH ) { +hfi1_copy_sge (&qp->r_sge, &ah_attr->grh, +sizeof (struct ib_grh ),1 ); + wc.wc_flags |=IB_WC_GRH ; + }else +hfi1_skip_sge (&qp->r_sge,sizeof (struct ib_grh ),1 ); + ssge.sg_list = swqe->sg_list +1 ; + ssge.sge = *swqe->sg_list; + ssge.num_sge = swqe->wr.num_sge; + sge = &ssge.sge; +while (length) { +u32 len = sge->length; + +if (len > length) + len = length; +if (len > sge->sge_length) + len = sge->sge_length; +BUG_ON (len ==0 ); +hfi1_copy_sge (&qp->r_sge, sge->vaddr, len,1 ); + sge->vaddr += len; + sge->length -= len; + sge->sge_length -= len; +if (sge->sge_length ==0 ) { +if (--ssge.num_sge) + *sge = *ssge.sg_list++; + }else if (sge->length ==0 && sge->mr->lkey) { +if (++sge->n >=HFI1_SEGSZ ) { +if (++sge->m >= sge->mr->mapsz) +break ; + sge->n =0 ; + } + sge->vaddr = + sge->mr->map[sge->m]->segs[sge->n].vaddr; + sge->length = + sge->mr->map[sge->m]->segs[sge->n].length; + } + length -= len; + } +hfi1_put_ss (&qp->r_sge); +if (!test_and_clear_bit (HFI1_R_WRID_VALID , &qp->r_aflags)) + goto bail_unlock; + wc.wr_id = qp->r_wr_id; + wc.status =IB_WC_SUCCESS ; + wc.opcode =IB_WC_RECV ; + wc.qp = &qp->ibqp; + wc.src_qp = sqp->ibqp.qp_num; +if (qp->ibqp.qp_type ==IB_QPT_GSI || qp->ibqp.qp_type ==IB_QPT_SMI ) { +if (sqp->ibqp.qp_type ==IB_QPT_GSI || + sqp->ibqp.qp_type ==IB_QPT_SMI ) + wc.pkey_index = swqe->wr.wr.ud.pkey_index; +else + wc.pkey_index = sqp->s_pkey_index; + }else { + wc.pkey_index =0 ; + } + wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) -1 )); +/* Check for loopback when the port lid is not set */ +if (wc.slid ==0 && sqp->ibqp.qp_type ==IB_QPT_GSI ) + wc.slid =HFI1_PERMISSIVE_LID ; + wc.sl = ah_attr->sl; + wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) -1 ); + wc.port_num = qp->port_num; +/* Signal completion event if the solicited bit is set. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc, + swqe->wr.send_flags &IB_SEND_SOLICITED ); + ibp->n_loop_pkts++; +bail_unlock: +spin_unlock_irqrestore (&qp->r_lock, flags); +drop: +if (atomic_dec_and_test (&qp->refcount)) +wake_up (&qp->wait); +} + +/** +* hfi1_make_ud_req - construct a UD request packet +* @qp: the QP +* +* Return 1 if constructed; otherwise, return 0. +*/ +int hfi1_make_ud_req (struct hfi1_qp *qp) +{ +struct hfi1_other_headers *ohdr; +struct ib_ah_attr *ah_attr; +struct hfi1_pportdata *ppd; +struct hfi1_ibport *ibp; +struct hfi1_swqe *wqe; +unsigned long flags; +u32 nwords; +u32 extra_bytes; +u32 bth0; +u16 lrh0; +u16 lid; +int ret =0 ; +int next_cur; +u8 sc5; + +spin_lock_irqsave (&qp->s_lock, flags); + +if (!(ib_hfi1_state_ops[qp->state] &HFI1_PROCESS_NEXT_SEND_OK )) { +if (!(ib_hfi1_state_ops[qp->state] &HFI1_FLUSH_SEND )) + goto bail; +/* We are in the error state, flush the work request. */ +if (qp->s_last == qp->s_head) + goto bail; +/* If DMAs are in progress, we can't flush immediately. */ +if (atomic_read (&qp->s_iowait.sdma_busy)) { + qp->s_flags |=HFI1_S_WAIT_DMA ; + goto bail; + } + wqe =get_swqe_ptr (qp, qp->s_last); +hfi1_send_complete (qp, wqe,IB_WC_WR_FLUSH_ERR ); + goto done; + } + +if (qp->s_cur == qp->s_head) + goto bail; + + wqe =get_swqe_ptr (qp, qp->s_cur); + next_cur = qp->s_cur +1 ; +if (next_cur >= qp->s_size) + next_cur =0 ; + +/* Construct the header. */ + ibp =to_iport (qp->ibqp.device, qp->port_num); + ppd =ppd_from_ibp (ibp); + ah_attr = &to_iah (wqe->wr.wr.ud.ah)->attr; +if (ah_attr->dlid <HFI1_MULTICAST_LID_BASE || + ah_attr->dlid ==HFI1_PERMISSIVE_LID ) { + lid = ah_attr->dlid & ~((1 << ppd->lmc) -1 ); +if (unlikely (!loopback && (lid == ppd->lid || + (lid ==HFI1_PERMISSIVE_LID && + qp->ibqp.qp_type ==IB_QPT_GSI )))) { +/* +* If DMAs are in progress, we can't generate +* a completion for the loopback packet since +* it would be out of order. +* Instead of waiting, we could queue a +* zero length descriptor so we get a callback. +*/ +if (atomic_read (&qp->s_iowait.sdma_busy)) { + qp->s_flags |=HFI1_S_WAIT_DMA ; + goto bail; + } + qp->s_cur = next_cur; +spin_unlock_irqrestore (&qp->s_lock, flags); +ud_loopback (qp, wqe); +spin_lock_irqsave (&qp->s_lock, flags); +hfi1_send_complete (qp, wqe,IB_WC_SUCCESS ); + goto done; + } + } + + qp->s_cur = next_cur; + extra_bytes = -wqe->length &3 ; + nwords = (wqe->length + extra_bytes) >>2 ; + +/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */ + qp->s_hdrwords =7 ; + qp->s_cur_size = wqe->length; + qp->s_cur_sge = &qp->s_sge; + qp->s_srate = ah_attr->static_rate; + qp->srate_mbps =ib_rate_to_mbps (qp->s_srate); + qp->s_wqe = wqe; + qp->s_sge.sge = wqe->sg_list[0 ]; + qp->s_sge.sg_list = wqe->sg_list +1 ; + qp->s_sge.num_sge = wqe->wr.num_sge; + qp->s_sge.total_len = wqe->length; + +if (ah_attr->ah_flags &IB_AH_GRH ) { +/* Header size in 32-bit words. */ + qp->s_hdrwords +=hfi1_make_grh (ibp, &qp->s_hdr->ibh.u.l.grh, + &ah_attr->grh, + qp->s_hdrwords, nwords); + lrh0 =HFI1_LRH_GRH ; + ohdr = &qp->s_hdr->ibh.u.l.oth; +/* +* Don't worry about sending to locally attached multicast +* QPs. It is unspecified by the spec. what happens. +*/ + }else { +/* Header size in 32-bit words. */ + lrh0 =HFI1_LRH_BTH ; + ohdr = &qp->s_hdr->ibh.u.oth; + } +if (wqe->wr.opcode ==IB_WR_SEND_WITH_IMM ) { + qp->s_hdrwords++; + ohdr->u.ud.imm_data = wqe->wr.ex.imm_data; + bth0 =IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE <<24 ; + }else + bth0 =IB_OPCODE_UD_SEND_ONLY <<24 ; + sc5 = ibp->sl_to_sc[ah_attr->sl]; + lrh0 |= (ah_attr->sl &0xf ) <<4 ; +if (qp->ibqp.qp_type ==IB_QPT_SMI ) { + lrh0 |=0xF000 ;/* Set VL (see ch. 13.5.3.1) */ + qp->s_sc =0xf ; + }else { + lrh0 |= (sc5 &0xf ) <<12 ; + qp->s_sc = sc5; + } + qp->s_hdr->ibh.lrh[0 ] =cpu_to_be16 (lrh0); + qp->s_hdr->ibh.lrh[1 ] =cpu_to_be16 (ah_attr->dlid);/* DEST LID */ + qp->s_hdr->ibh.lrh[2 ] = +cpu_to_be16 (qp->s_hdrwords + nwords +SIZE_OF_CRC ); +if (ah_attr->dlid ==be16_to_cpu (IB_LID_PERMISSIVE )) + qp->s_hdr->ibh.lrh[3 ] =IB_LID_PERMISSIVE ; +else { + lid = ppd->lid; +if (lid) { + lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) -1 ); + qp->s_hdr->ibh.lrh[3 ] =cpu_to_be16 (lid); + }else + qp->s_hdr->ibh.lrh[3 ] =IB_LID_PERMISSIVE ; + } +if (wqe->wr.send_flags &IB_SEND_SOLICITED ) + bth0 |=IB_BTH_SOLICITED ; + bth0 |= extra_bytes <<20 ; +if (qp->ibqp.qp_type ==IB_QPT_GSI || qp->ibqp.qp_type ==IB_QPT_SMI ) + bth0 |=hfi1_get_pkey (ibp, wqe->wr.wr.ud.pkey_index); +else + bth0 |=hfi1_get_pkey (ibp, qp->s_pkey_index); + ohdr->bth[0 ] =cpu_to_be32 (bth0); + ohdr->bth[1 ] =cpu_to_be32 (wqe->wr.wr.ud.remote_qpn); + ohdr->bth[2 ] =cpu_to_be32 (mask_psn (qp->s_next_psn++)); +/* +* Qkeys with the high order bit set mean use the +* qkey from the QP context instead of the WR (see 10.2.5). +*/ + ohdr->u.ud.deth[0 ] =cpu_to_be32 ((int )wqe->wr.wr.ud.remote_qkey <0 ? + qp->qkey : wqe->wr.wr.ud.remote_qkey); + ohdr->u.ud.deth[1 ] =cpu_to_be32 (qp->ibqp.qp_num); +/* disarm any ahg */ + qp->s_hdr->ahgcount =0 ; + qp->s_hdr->ahgidx =0 ; + qp->s_hdr->tx_flags =0 ; + qp->s_hdr->sde =NULL ; + +done: + ret =1 ; + goto unlock; + +bail: + qp->s_flags &= ~HFI1_S_BUSY ; +unlock: +spin_unlock_irqrestore (&qp->s_lock, flags); +return ret; +} + +/* +* Hardware can't check this so we do it here. +* +* This is a slightly different algorithm than the standard pkey check. It +* special cases the management keys and allows for 0x7fff and 0xffff to be in +* the table at the same time. +* +* @returns the index found or -1 if not found +*/ +int hfi1_lookup_pkey_idx (struct hfi1_ibport *ibp,u16 pkey) +{ +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +unsigned i; + +if (pkey ==FULL_MGMT_P_KEY || pkey ==LIM_MGMT_P_KEY ) { +unsigned lim_idx =-1 ; + +for (i =0 ; i <ARRAY_SIZE (ppd->pkeys); ++i) { +/* here we look for an exact match */ +if (ppd->pkeys[i] == pkey) +return i; +if (ppd->pkeys[i] ==LIM_MGMT_P_KEY ) + lim_idx = i; + } + +/* did not find 0xffff return 0x7fff idx if found */ +if (pkey ==FULL_MGMT_P_KEY ) +return lim_idx; + +/* no match... */ +return -1 ; + } + + pkey &=0x7fff ;/* remove limited/full membership bit */ + +for (i =0 ; i <ARRAY_SIZE (ppd->pkeys); ++i) +if ((ppd->pkeys[i] &0x7fff ) == pkey) +return i; + +/* +* Should not get here, this means hardware failed to validate pkeys. +*/ +return -1 ; +} + +void return_cnp (struct hfi1_ibport *ibp,struct hfi1_qp *qp,u32 remote_qpn, +u32 pkey,u32 slid,u32 dlid,u8 sc5, +const struct ib_grh *old_grh) +{ +u64 pbc, pbc_flags =0 ; +u32 bth0, plen, vl, hwords =5 ; +u16 lrh0; +u8 sl = ibp->sc_to_sl[sc5]; +struct hfi1_ib_header hdr; +struct hfi1_other_headers *ohdr; +struct pio_buf *pbuf; +struct send_context *ctxt =qp_to_send_context (qp, sc5); +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); + +if (old_grh) { +struct ib_grh *grh = &hdr.u.l.grh; + + grh->version_tclass_flow = old_grh->version_tclass_flow; + grh->paylen =cpu_to_be16 ((hwords -2 +SIZE_OF_CRC ) <<2 ); + grh->hop_limit =0xff ; + grh->sgid = old_grh->dgid; + grh->dgid = old_grh->sgid; + ohdr = &hdr.u.l.oth; + lrh0 =HFI1_LRH_GRH ; + hwords +=sizeof (struct ib_grh ) /sizeof (u32); + }else { + ohdr = &hdr.u.oth; + lrh0 =HFI1_LRH_BTH ; + } + + lrh0 |= (sc5 &0xf ) <<12 | sl <<4 ; + + bth0 = pkey | (CNP_OPCODE <<24 ); + ohdr->bth[0 ] =cpu_to_be32 (bth0); + + ohdr->bth[1 ] =cpu_to_be32 (remote_qpn | (1 <<HFI1_BECN_SHIFT )); + ohdr->bth[2 ] =0 ;/* PSN 0 */ + + hdr.lrh[0 ] =cpu_to_be16 (lrh0); + hdr.lrh[1 ] =cpu_to_be16 (dlid); + hdr.lrh[2 ] =cpu_to_be16 (hwords +SIZE_OF_CRC ); + hdr.lrh[3 ] =cpu_to_be16 (slid); + + plen =2 /* PBC */ + hwords; + pbc_flags |= (!!(sc5 &0x10 )) <<PBC_DC_INFO_SHIFT ; + vl =sc_to_vlt (ppd->dd, sc5); + pbc =create_pbc (ppd, pbc_flags, qp->srate_mbps, vl, plen); +if (ctxt) { + pbuf =sc_buffer_alloc (ctxt, plen,NULL ,NULL ); +if (pbuf) + ppd->dd->pio_inline_send (ppd->dd, pbuf, pbc, + &hdr, hwords); + } +} + +/* +* opa_smp_check() - Do the regular pkey checking, and the additional +* checks for SMPs specified in OPAv1 rev 0.90, section 9.10.26 +* ("SMA Packet Checks"). +* +* Note that: +* - Checks are done using the pkey directly from the packet's BTH, +* and specifically _not_ the pkey that we attach to the completion, +* which may be different. +* - These checks are specifically for "non-local" SMPs (i.e., SMPs +* which originated on another node). SMPs which are sent from, and +* destined to this node are checked in opa_local_smp_check(). +* +* At the point where opa_smp_check() is called, we know: +* - destination QP is QP0 +* +* opa_smp_check() returns 0 if all checks succeed, 1 otherwise. +*/ +static int opa_smp_check (struct hfi1_ibport *ibp,u16 pkey,u8 sc5, +struct hfi1_qp *qp,u16 slid,struct opa_smp *smp) +{ +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); + +/* +* I don't think it's possible for us to get here with sc != 0xf, +* but check it to be certain. +*/ +if (sc5 !=0xf ) +return 1 ; + +if (ingress_pkey_check (ppd, pkey, sc5, qp->s_pkey_index, slid)) +return 1 ; + +/* +* At this point we know (and so don't need to check again) that +* the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY +* (see ingress_pkey_check). +*/ +if (smp->mgmt_class !=IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE && + smp->mgmt_class !=IB_MGMT_CLASS_SUBN_LID_ROUTED ) { +ingress_pkey_table_fail (ppd, pkey, slid); +return 1 ; + } + +/* +* SMPs fall into one of four (disjoint) categories: +* SMA request, SMA response, trap, or trap repress. +* Our response depends, in part, on which type of +* SMP we're processing. +* +* If this is not an SMA request, or trap repress: +* - accept MAD if the port is running an SM +* - pkey == FULL_MGMT_P_KEY => +* reply with unsupported method (i.e., just mark +* the smp's status field here, and let it be +* processed normally) +* - pkey != LIM_MGMT_P_KEY => +* increment port recv constraint errors, drop MAD +* If this is an SMA request or trap repress: +* - pkey != FULL_MGMT_P_KEY => +* increment port recv constraint errors, drop MAD +*/ +switch (smp->method) { +case IB_MGMT_METHOD_GET : +case IB_MGMT_METHOD_SET : +case IB_MGMT_METHOD_REPORT : +case IB_MGMT_METHOD_TRAP_REPRESS : +if (pkey !=FULL_MGMT_P_KEY ) { +ingress_pkey_table_fail (ppd, pkey, slid); +return 1 ; + } +break ; +case IB_MGMT_METHOD_SEND : +case IB_MGMT_METHOD_TRAP : +case IB_MGMT_METHOD_GET_RESP : +case IB_MGMT_METHOD_REPORT_RESP : +if (ibp->port_cap_flags &IB_PORT_SM ) +return 0 ; +if (pkey ==FULL_MGMT_P_KEY ) { + smp->status |=IB_SMP_UNSUP_METHOD ; +return 0 ; + } +if (pkey !=LIM_MGMT_P_KEY ) { +ingress_pkey_table_fail (ppd, pkey, slid); +return 1 ; + } +break ; +default : +break ; + } +return 0 ; +} + + +/** +* hfi1_ud_rcv - receive an incoming UD packet +* @ibp: the port the packet came in on +* @hdr: the packet header +* @rcv_flags: flags relevant to rcv processing +* @data: the packet data +* @tlen: the packet length +* @qp: the QP the packet came on +* +* This is called from qp_rcv() to process an incoming UD packet +* for the given QP. +* Called at interrupt level. +*/ +void hfi1_ud_rcv (struct hfi1_ibport *ibp,struct hfi1_ib_header *hdr, +u32 rcv_flags,void *data,u32 tlen,struct hfi1_qp *qp) +{ +struct hfi1_other_headers *ohdr; +int opcode; +u32 hdrsize; +u32 pad; +struct ib_wc wc; +u32 qkey; +u32 src_qp; +u16 dlid, pkey; +int mgmt_pkey_idx =-1 ; +int has_grh = !!(rcv_flags &HFI1_HAS_GRH ); +int sc4_bit = (!!(rcv_flags &HFI1_SC4_BIT )) <<4 ; +u8 sc; +int is_becn, is_fecn, is_mcast; +struct ib_grh *grh =NULL ; + +/* Check for GRH */ +if (!has_grh) { + ohdr = &hdr->u.oth; + hdrsize =8 +12 +8 ;/* LRH + BTH + DETH */ + }else { + ohdr = &hdr->u.l.oth; + hdrsize =8 +40 +12 +8 ;/* LRH + GRH + BTH + DETH */ + grh = &hdr->u.l.grh; + } + qkey =be32_to_cpu (ohdr->u.ud.deth[0 ]); + src_qp =be32_to_cpu (ohdr->u.ud.deth[1 ]) &HFI1_QPN_MASK ; + dlid =be16_to_cpu (hdr->lrh[1 ]); + is_mcast = (dlid >HFI1_MULTICAST_LID_BASE ) && + (dlid !=HFI1_PERMISSIVE_LID ); + is_becn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_BECN_SHIFT ) + &HFI1_BECN_MASK ; + is_fecn = (be32_to_cpu (ohdr->bth[1 ]) >>HFI1_FECN_SHIFT ) + &HFI1_FECN_MASK ; + +if (is_becn) { +/* +* In pre-B0 h/w the CNP_OPCODE is handled via an +* error path (errata 291394). +*/ +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +u32 lqpn =be32_to_cpu (ohdr->bth[1 ]) &HFI1_QPN_MASK ; +u8 sl, sc5; + + sc5 = (be16_to_cpu (hdr->lrh[0 ]) >>12 ) &0xf ; + sc5 |= sc4_bit; + sl = ibp->sc_to_sl[sc5]; + +process_becn (ppd, sl,0 , lqpn,0 ,IB_CC_SVCTYPE_UD ); + } + +/* +* The opcode is in the low byte when its in network order +* (top byte when in host order). +*/ + opcode =be32_to_cpu (ohdr->bth[0 ]) >>24 ; + opcode &=0xff ; + + pkey = (u16 )be32_to_cpu (ohdr->bth[0 ]); + +if (!is_mcast && (opcode !=CNP_OPCODE ) && is_fecn) { +u16 slid =be16_to_cpu (hdr->lrh[3 ]); +u8 sc5; + + sc5 = (be16_to_cpu (hdr->lrh[0 ]) >>12 ) &0xf ; + sc5 |= sc4_bit; + +return_cnp (ibp, qp, src_qp, pkey, dlid, slid, sc5, grh); + } +/* +* Get the number of bytes the message was padded by +* and drop incomplete packets. +*/ + pad = (be32_to_cpu (ohdr->bth[0 ]) >>20 ) &3 ; +if (unlikely (tlen < (hdrsize + pad +4 ))) + goto drop; + + tlen -= hdrsize + pad +4 ; + +/* +* Check that the permissive LID is only used on QP0 +* and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1). +*/ +if (qp->ibqp.qp_num) { +if (unlikely (hdr->lrh[1 ] ==IB_LID_PERMISSIVE || + hdr->lrh[3 ] ==IB_LID_PERMISSIVE )) + goto drop; +if (qp->ibqp.qp_num >1 ) { +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +u16 slid; +u8 sc5; + + sc5 = (be16_to_cpu (hdr->lrh[0 ]) >>12 ) &0xf ; + sc5 |= sc4_bit; + + slid =be16_to_cpu (hdr->lrh[3 ]); +if (unlikely (ingress_pkey_check (ppd, pkey, sc5, + qp->s_pkey_index, + slid))) { +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_PKEY , + pkey, + (be16_to_cpu (hdr->lrh[0 ]) >>4 ) & +0xF , + src_qp, qp->ibqp.qp_num, + hdr->lrh[3 ], hdr->lrh[1 ]); +return ; + } + }else { +/* GSI packet */ + mgmt_pkey_idx =hfi1_lookup_pkey_idx (ibp, pkey); +if (mgmt_pkey_idx <0 ) + goto drop; + + } +if (unlikely (qkey != qp->qkey)) { +hfi1_bad_pqkey (ibp,IB_NOTICE_TRAP_BAD_QKEY , qkey, + (be16_to_cpu (hdr->lrh[0 ]) >>4 ) &0xF , + src_qp, qp->ibqp.qp_num, + hdr->lrh[3 ], hdr->lrh[1 ]); +return ; + } +/* Drop invalid MAD packets (see 13.5.3.1). */ +if (unlikely (qp->ibqp.qp_num ==1 && + (tlen >2048 || + (be16_to_cpu (hdr->lrh[0 ]) >>12 ) ==15 ))) + goto drop; + }else { +/* Received on QP0, and so by definition, this is an SMP */ +struct opa_smp *smp = (struct opa_smp *)data; +u16 slid =be16_to_cpu (hdr->lrh[3 ]); +u8 sc5; + + sc5 = (be16_to_cpu (hdr->lrh[0 ]) >>12 ) &0xf ; + sc5 |= sc4_bit; + +if (opa_smp_check (ibp, pkey, sc5, qp, slid, smp)) + goto drop; + +if (tlen >2048 ) + goto drop; +if ((hdr->lrh[1 ] ==IB_LID_PERMISSIVE || + hdr->lrh[3 ] ==IB_LID_PERMISSIVE ) && + smp->mgmt_class !=IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) + goto drop; + +/* look up SMI pkey */ + mgmt_pkey_idx =hfi1_lookup_pkey_idx (ibp, pkey); +if (mgmt_pkey_idx <0 ) + goto drop; + + } + +if (qp->ibqp.qp_num >1 && + opcode ==IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE ) { + wc.ex.imm_data = ohdr->u.ud.imm_data; + wc.wc_flags =IB_WC_WITH_IMM ; + tlen -=sizeof (u32); + }else if (opcode ==IB_OPCODE_UD_SEND_ONLY ) { + wc.ex.imm_data =0 ; + wc.wc_flags =0 ; + }else + goto drop; + +/* +* A GRH is expected to precede the data even if not +* present on the wire. +*/ + wc.byte_len = tlen +sizeof (struct ib_grh ); + +/* +* Get the next work request entry to find where to put the data. +*/ +if (qp->r_flags &HFI1_R_REUSE_SGE ) + qp->r_flags &= ~HFI1_R_REUSE_SGE ; +else { +int ret; + + ret =hfi1_get_rwqe (qp,0 ); +if (ret <0 ) { +hfi1_rc_error (qp,IB_WC_LOC_QP_OP_ERR ); +return ; + } +if (!ret) { +if (qp->ibqp.qp_num ==0 ) + ibp->n_vl15_dropped++; +return ; + } + } +/* Silently drop packets which are too big. */ +if (unlikely (wc.byte_len > qp->r_len)) { + qp->r_flags |=HFI1_R_REUSE_SGE ; + goto drop; + } +if (has_grh) { +hfi1_copy_sge (&qp->r_sge, &hdr->u.l.grh, +sizeof (struct ib_grh ),1 ); + wc.wc_flags |=IB_WC_GRH ; + }else +hfi1_skip_sge (&qp->r_sge,sizeof (struct ib_grh ),1 ); +hfi1_copy_sge (&qp->r_sge, data, wc.byte_len -sizeof (struct ib_grh ),1 ); +hfi1_put_ss (&qp->r_sge); +if (!test_and_clear_bit (HFI1_R_WRID_VALID , &qp->r_aflags)) +return ; + wc.wr_id = qp->r_wr_id; + wc.status =IB_WC_SUCCESS ; + wc.opcode =IB_WC_RECV ; + wc.vendor_err =0 ; + wc.qp = &qp->ibqp; + wc.src_qp = src_qp; + +if (qp->ibqp.qp_type ==IB_QPT_GSI || + qp->ibqp.qp_type ==IB_QPT_SMI ) { +if (mgmt_pkey_idx <0 ) { +if (net_ratelimit ()) { +struct hfi1_pportdata *ppd =ppd_from_ibp (ibp); +struct hfi1_devdata *dd = ppd->dd; + +dd_dev_err (dd,"QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n" , + qp->ibqp.qp_type); + mgmt_pkey_idx =0 ; + } + } + wc.pkey_index = (unsigned )mgmt_pkey_idx; + }else + wc.pkey_index =0 ; + + wc.slid =be16_to_cpu (hdr->lrh[3 ]); + sc = (be16_to_cpu (hdr->lrh[0 ]) >>4 ) &0xf ; + sc |= sc4_bit; + wc.sl = ibp->sc_to_sl[sc]; + +/* +* Save the LMC lower bits if the destination LID is a unicast LID. +*/ + wc.dlid_path_bits = dlid >=HFI1_MULTICAST_LID_BASE ?0 : + dlid & ((1 <<ppd_from_ibp (ibp)->lmc) -1 ); + wc.port_num = qp->port_num; +/* Signal completion event if the solicited bit is set. */ +hfi1_cq_enter (to_icq (qp->ibqp.recv_cq), &wc, + (ohdr->bth[0 ] & +cpu_to_be32 (IB_BTH_SOLICITED )) !=0 ); +return ; + +drop: + ibp->n_pkt_drops++; +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 36/41 IB/hfi1: add low level page locking
In reply to a message not in the archive--- drivers/infiniband/hw/hfi1/user_pages.c | 156 +++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 drivers/infiniband/hw/hfi1/user_pages.c
drivers/infiniband/hw/hfi1/user_pages.c new file +156 -0
@@ -0,0 +1,156 @@ +/* +* +* This file is provided under a dual BSD/GPLv2 license. When using or +* redistributing this file, you may do so under either license. +* +* GPL LICENSE SUMMARY +* +* Copyright(c) 2015 Intel Corporation. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* BSD LICENSE +* +* Copyright(c) 2015 Intel Corporation. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include <linux/mm.h> +#include <linux/device.h> + +#include "hfi.h" + +static void __hfi1_release_user_pages (struct page **p,size_t num_pages, +int dirty) +{ +size_t i; + +for (i =0 ; i < num_pages; i++) { +if (dirty) +set_page_dirty_lock (p[i]); +put_page (p[i]); + } +} + +/* +* Call with current->mm->mmap_sem held. +*/ +static int __hfi1_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) +{ +unsigned long lock_limit; +size_t got; +int ret; + + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; + +if (num_pages > lock_limit && !capable (CAP_IPC_LOCK )) { + ret = -ENOMEM ; + goto bail; + } + +for (got =0 ; got < num_pages; got += ret) { + ret =get_user_pages (current, current->mm, + start_page + got *PAGE_SIZE , + num_pages - got,1 ,1 , + p + got,NULL ); +if (ret <0 ) + goto bail_release; + } + + current->mm->pinned_vm += num_pages; + + ret =0 ; + goto bail; + +bail_release: +__hfi1_release_user_pages (p, got,0 ); +bail: +return ret; +} + +/** +* hfi1_map_page - a safety wrapper around pci_map_page() +* +*/ +dma_addr_t hfi1_map_page (struct pci_dev *hwdev,struct page *page, +unsigned long offset,size_t size,int direction) +{ +dma_addr_t phys; + + phys =pci_map_page (hwdev, page, offset, size, direction); + +return phys; +} + +/** +* hfi1_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. For +* now, num_pages is always 1, but that will probably change at some point +* (because caller is doing expected sends on a single virtually contiguous +* buffer, so we can do all pages at once). +*/ +int hfi1_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) +{ +int ret; + +down_write (¤t->mm->mmap_sem); + + ret =__hfi1_get_user_pages (start_page, num_pages, p); + +up_write (¤t->mm->mmap_sem); + +return ret; +} + +void hfi1_release_user_pages (struct page **p,size_t num_pages) +{ +if (current->mm)/* during close after signal, mm can be NULL */ +down_write (¤t->mm->mmap_sem); + +__hfi1_release_user_pages (p, num_pages,1 ); + +if (current->mm) { + current->mm->pinned_vm -= num_pages; +up_write (¤t->mm->mmap_sem); + } +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 20150611231142.16479.41039.stgit-K+u1se/DcYrLESAwzcoQNrvm/[email protected]
Re: [PATCH 36/41] IB/hfi1: add low level page locking
Quoted 18 lines · Mike Marciniszyn
+++
b/drivers/infiniband/hw/hfi1/user_pages.cdrivers/infiniband/hw/hfi1/user_pages.c new file +16
-0+/** +* hfi1_get_user_pages - lock user pages into memory +* @start_page: the start page +* @num_pages: the number of pages +* @p: the output page structures +* +* This function takes a given start page (page aligned user virtual +* address) and pins it and the following specified number of pages. For +* now, num_pages is always 1, but that will probably change at some point +* (because caller is doing expected sends on a single virtually contiguous +* buffer, so we can do all pages at once). +*/ +int hfi1_get_user_pages (unsigned long start_page,size_t num_pages, +struct page **p) +{ +int ret;
drivers/infiniband/hw/hfi1/user_pages.c line 144
Quoted 10 lines
drivers/infiniband/hw/hfi1/user_pages.c new file +10
-0+ +down_write (¤t->mm->mmap_sem); + + ret =__hfi1_get_user_pages (start_page, num_pages, p); + +up_write (¤t->mm->mmap_sem); + +return ret; +} +
anything wrong with the umem services provided by the IB core that requires this implementation? what?
drivers/infiniband/hw/hfi1/user_pages.c line 156
Quoted 13 lines
drivers/infiniband/hw/hfi1/user_pages.c new file +12
-0+void hfi1_release_user_pages (struct page **p,size_t num_pages) +{ +if (current->mm)/* during close after signal, mm can be NULL */ +down_write (¤t->mm->mmap_sem); + +__hfi1_release_user_pages (p, num_pages,1 ); + +if (current->mm) { + current->mm->pinned_vm -= num_pages; +up_write (¤t->mm->mmap_sem); + } +}
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): CAJ3xEMjM9kRrnCdJmKR2i9VySOLfsZMkU=ZnJE0rXs0tTPSbVw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
RE: [PATCH 36/41] IB/hfi1: add low level page locking
PiA+ICsgKiBoZmkxX2dldF91c2VyX3BhZ2VzIC0gbG9jayB1c2VyIHBhZ2VzIGludG8gbWVtb3J5 DQo+ID4gKyAqIEBzdGFydF9wYWdlOiB0aGUgc3RhcnQgcGFnZQ0KPiA+ICsgKiBAbnVtX3BhZ2Vz OiB0aGUgbnVtYmVyIG9mIHBhZ2VzDQo+ID4gKyAqIEBwOiB0aGUgb3V0cHV0IHBhZ2Ugc3RydWN0 dXJlcw0KPiA+ICsgKg0KPiA+ICsgKiBUaGlzIGZ1bmN0aW9uIHRha2VzIGEgZ2l2ZW4gc3RhcnQg cGFnZSAocGFnZSBhbGlnbmVkIHVzZXIgdmlydHVhbA0KPiA+ICsgKiBhZGRyZXNzKSBhbmQgcGlu cyBpdCBhbmQgdGhlIGZvbGxvd2luZyBzcGVjaWZpZWQgbnVtYmVyIG9mIHBhZ2VzLg0KPiA+ICtG b3INCj4gPiArICogbm93LCBudW1fcGFnZXMgaXMgYWx3YXlzIDEsIGJ1dCB0aGF0IHdpbGwgcHJv YmFibHkgY2hhbmdlIGF0IHNvbWUNCj4gPiArcG9pbnQNCj4gPiArICogKGJlY2F1c2UgY2FsbGVy IGlzIGRvaW5nIGV4cGVjdGVkIHNlbmRzIG9uIGEgc2luZ2xlIHZpcnR1YWxseQ0KPiA+ICtjb250 aWd1b3VzDQo+ID4gKyAqIGJ1ZmZlciwgc28gd2UgY2FuIGRvIGFsbCBwYWdlcyBhdCBvbmNlKS4N Cj4gPiArICovDQo+ID4gK2ludCBoZmkxX2dldF91c2VyX3BhZ2VzKHVuc2lnbmVkIGxvbmcgc3Rh cnRfcGFnZSwgc2l6ZV90IG51bV9wYWdlcywNCj4gPiArICAgICAgICAgICAgICAgICAgICAgICBz dHJ1Y3QgcGFnZSAqKnApIHsNCj4gPiArICAgICAgIGludCByZXQ7DQo+ID4gKw0KPiA+ICsgICAg ICAgZG93bl93cml0ZSgmY3VycmVudC0+bW0tPm1tYXBfc2VtKTsNCj4gPiArDQo+ID4gKyAgICAg ICByZXQgPSBfX2hmaTFfZ2V0X3VzZXJfcGFnZXMoc3RhcnRfcGFnZSwgbnVtX3BhZ2VzLCBwKTsN Cj4gPiArDQo+ID4gKyAgICAgICB1cF93cml0ZSgmY3VycmVudC0+bW0tPm1tYXBfc2VtKTsNCj4g PiArDQo+ID4gKyAgICAgICByZXR1cm4gcmV0Ow0KPiA+ICt9DQo+ID4gKw0KPiANCj4gYW55dGhp bmcgd3Jvbmcgd2l0aCB0aGUgdW1lbSBzZXJ2aWNlcyBwcm92aWRlZCBieSB0aGUgSUIgY29yZSB0 aGF0DQo+IHJlcXVpcmVzIHRoaXMgaW1wbGVtZW50YXRpb24/IHdoYXQ/DQo+IA0KDQpXZSBhcmUg Y3VycmVudGx5IGludmVzdGlnYXRpbmcgdGhpcy4NCg0KTWlrZQ0K
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
RE: [PATCH 36/41] IB/hfi1: add low level page locking
In reply to a message not in the archivePiBhbnl0aGluZyB3cm9uZyB3aXRoIHRoZSB1bWVtIHNlcnZpY2VzIHByb3ZpZGVkIGJ5IHRoZSBJ QiBjb3JlIHRoYXQNCj4gcmVxdWlyZXMgdGhpcyBpbXBsZW1lbnRhdGlvbj8gd2hhdD8NCj4gDQoN ClRoZSBjdXJyZW50IGxldmVsIG9mIHRoZSBBUEkgaXMgbWlzbWF0Y2hlZCB3aXRoIHRoZSBQU00g U0RNQS4NCg0KVGhlIGliX3VtZW0gYXBpOg0KLSBtYXBzIGFuIFNHIGxpc3Qgd2hpY2ggaXNuJ3Qg cmVxdWlyZWQgYnkgUFNNIHNpbmNlIERNQSBtYXBwaW5nIGlzIGRvbmUgYnkgdGhlIGxvdyBsZXZl bCBTRE1BDQotIHRoZSBtYXBwaW5nIGlzIGJpLWRpcmVjdGlvbmFsIHZzLiBmcm9tIGRldmljZQ0K LSBUaGUgcGQncyBjb250ZXh0IGlzIG5vdCB0aGVyZQ0KDQpDZXJ0YWlubHkgd2Ugd291bGQgY29u c2lkZXIgYSBjb3JlIGNoYW5nZSB0aGF0IHByb3ZpZGVzIGp1c3QgdGhlIHBhZ2UgbG9ja2luZyBh bmQgd2UgY291bGQgZmFrZSBhIGNvbnRleHQuDQoNCk1pa2UNCg==
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 32E1700B9017364D9B60AED9960492BC2575DAA8-RjuIdWtd+YbTXloPLtfHfbfspsVTdybXVpNB7YpNyf8@public.gmane.org
Re: [PATCH 36/41] IB/hfi1: add low level page locking
Quoted 8 lines · Mike
Quoted 3 lines
anything wrong with the umem services provided by the IB core that requires this implementation? what?
The current level of the API is mismatched with the PSM SDMA.
The
ib_umemapi:
- maps an SG list which isn't required by PSM since DMA mapping is done by the low level SDMA
Can I ask why do you prefer mapping page by page over using a single sg list?
Quoted 6 lines
- the mapping is bi-directional vs. from device
- The pd's context is not thereCertainly we would consider a core change that provides just the page locking and we could fake a context.
Mike
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html