18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2014 Cisco Systems, Inc. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This program is free software; you may redistribute it and/or modify 58c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by 68c2ecf20Sopenharmony_ci * the Free Software Foundation; version 2 of the License. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 98c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 108c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 118c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 128c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 148c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 158c2ecf20Sopenharmony_ci * SOFTWARE. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/errno.h> 198c2ecf20Sopenharmony_ci#include <linux/types.h> 208c2ecf20Sopenharmony_ci#include <linux/pci.h> 218c2ecf20Sopenharmony_ci#include <linux/delay.h> 228c2ecf20Sopenharmony_ci#include <linux/slab.h> 238c2ecf20Sopenharmony_ci#include "vnic_dev.h" 248c2ecf20Sopenharmony_ci#include "vnic_wq.h" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic inline int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq, 278c2ecf20Sopenharmony_ci unsigned int index, enum vnic_res_type res_type) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci wq->ctrl = svnic_dev_get_res(vdev, res_type, index); 308c2ecf20Sopenharmony_ci if (!wq->ctrl) 318c2ecf20Sopenharmony_ci return -EINVAL; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci return 0; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic inline int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq, 378c2ecf20Sopenharmony_ci unsigned int index, unsigned int desc_count, unsigned int desc_size) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci return svnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count, 408c2ecf20Sopenharmony_ci desc_size); 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic int vnic_wq_alloc_bufs(struct vnic_wq *wq) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci struct vnic_wq_buf *buf; 468c2ecf20Sopenharmony_ci unsigned int i, j, count = wq->ring.desc_count; 478c2ecf20Sopenharmony_ci unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci for (i = 0; i < blks; i++) { 508c2ecf20Sopenharmony_ci wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ, GFP_ATOMIC); 518c2ecf20Sopenharmony_ci if (!wq->bufs[i]) { 528c2ecf20Sopenharmony_ci pr_err("Failed to alloc wq_bufs\n"); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return -ENOMEM; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci } 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci for (i = 0; i < blks; i++) { 598c2ecf20Sopenharmony_ci buf = wq->bufs[i]; 608c2ecf20Sopenharmony_ci for (j = 0; j < VNIC_WQ_BUF_DFLT_BLK_ENTRIES; j++) { 618c2ecf20Sopenharmony_ci buf->index = i * VNIC_WQ_BUF_DFLT_BLK_ENTRIES + j; 628c2ecf20Sopenharmony_ci buf->desc = (u8 *)wq->ring.descs + 638c2ecf20Sopenharmony_ci wq->ring.desc_size * buf->index; 648c2ecf20Sopenharmony_ci if (buf->index + 1 == count) { 658c2ecf20Sopenharmony_ci buf->next = wq->bufs[0]; 668c2ecf20Sopenharmony_ci break; 678c2ecf20Sopenharmony_ci } else if (j + 1 == VNIC_WQ_BUF_DFLT_BLK_ENTRIES) { 688c2ecf20Sopenharmony_ci buf->next = wq->bufs[i + 1]; 698c2ecf20Sopenharmony_ci } else { 708c2ecf20Sopenharmony_ci buf->next = buf + 1; 718c2ecf20Sopenharmony_ci buf++; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci wq->to_use = wq->to_clean = wq->bufs[0]; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return 0; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_civoid svnic_wq_free(struct vnic_wq *wq) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci struct vnic_dev *vdev; 848c2ecf20Sopenharmony_ci unsigned int i; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci vdev = wq->vdev; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci svnic_dev_free_desc_ring(vdev, &wq->ring); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) { 918c2ecf20Sopenharmony_ci kfree(wq->bufs[i]); 928c2ecf20Sopenharmony_ci wq->bufs[i] = NULL; 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci wq->ctrl = NULL; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ciint vnic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, 1008c2ecf20Sopenharmony_ci unsigned int desc_count, unsigned int desc_size) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci int err; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci wq->index = 0; 1058c2ecf20Sopenharmony_ci wq->vdev = vdev; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci err = vnic_wq_get_ctrl(vdev, wq, 0, RES_TYPE_DEVCMD2); 1088c2ecf20Sopenharmony_ci if (err) { 1098c2ecf20Sopenharmony_ci pr_err("Failed to get devcmd2 resource\n"); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return err; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci svnic_wq_disable(wq); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci err = vnic_wq_alloc_ring(vdev, wq, 0, desc_count, desc_size); 1178c2ecf20Sopenharmony_ci if (err) 1188c2ecf20Sopenharmony_ci return err; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ciint svnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq, 1248c2ecf20Sopenharmony_ci unsigned int index, unsigned int desc_count, unsigned int desc_size) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci int err; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci wq->index = index; 1298c2ecf20Sopenharmony_ci wq->vdev = vdev; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ); 1328c2ecf20Sopenharmony_ci if (err) { 1338c2ecf20Sopenharmony_ci pr_err("Failed to hook WQ[%d] resource\n", index); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return err; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci svnic_wq_disable(wq); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci err = vnic_wq_alloc_ring(vdev, wq, index, desc_count, desc_size); 1418c2ecf20Sopenharmony_ci if (err) 1428c2ecf20Sopenharmony_ci return err; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci err = vnic_wq_alloc_bufs(wq); 1458c2ecf20Sopenharmony_ci if (err) { 1468c2ecf20Sopenharmony_ci svnic_wq_free(wq); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return err; 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return 0; 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_civoid vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index, 1558c2ecf20Sopenharmony_ci unsigned int fetch_index, unsigned int posted_index, 1568c2ecf20Sopenharmony_ci unsigned int error_interrupt_enable, 1578c2ecf20Sopenharmony_ci unsigned int error_interrupt_offset) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci u64 paddr; 1608c2ecf20Sopenharmony_ci unsigned int count = wq->ring.desc_count; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET; 1638c2ecf20Sopenharmony_ci writeq(paddr, &wq->ctrl->ring_base); 1648c2ecf20Sopenharmony_ci iowrite32(count, &wq->ctrl->ring_size); 1658c2ecf20Sopenharmony_ci iowrite32(fetch_index, &wq->ctrl->fetch_index); 1668c2ecf20Sopenharmony_ci iowrite32(posted_index, &wq->ctrl->posted_index); 1678c2ecf20Sopenharmony_ci iowrite32(cq_index, &wq->ctrl->cq_index); 1688c2ecf20Sopenharmony_ci iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable); 1698c2ecf20Sopenharmony_ci iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset); 1708c2ecf20Sopenharmony_ci iowrite32(0, &wq->ctrl->error_status); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci wq->to_use = wq->to_clean = 1738c2ecf20Sopenharmony_ci &wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)] 1748c2ecf20Sopenharmony_ci [fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)]; 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_civoid svnic_wq_init(struct vnic_wq *wq, unsigned int cq_index, 1788c2ecf20Sopenharmony_ci unsigned int error_interrupt_enable, 1798c2ecf20Sopenharmony_ci unsigned int error_interrupt_offset) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci vnic_wq_init_start(wq, cq_index, 0, 0, error_interrupt_enable, 1828c2ecf20Sopenharmony_ci error_interrupt_offset); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ciunsigned int svnic_wq_error_status(struct vnic_wq *wq) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci return ioread32(&wq->ctrl->error_status); 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_civoid svnic_wq_enable(struct vnic_wq *wq) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci iowrite32(1, &wq->ctrl->enable); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ciint svnic_wq_disable(struct vnic_wq *wq) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci unsigned int wait; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci iowrite32(0, &wq->ctrl->enable); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci /* Wait for HW to ACK disable request */ 2028c2ecf20Sopenharmony_ci for (wait = 0; wait < 100; wait++) { 2038c2ecf20Sopenharmony_ci if (!(ioread32(&wq->ctrl->running))) 2048c2ecf20Sopenharmony_ci return 0; 2058c2ecf20Sopenharmony_ci udelay(1); 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci pr_err("Failed to disable WQ[%d]\n", wq->index); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return -ETIMEDOUT; 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_civoid svnic_wq_clean(struct vnic_wq *wq, 2148c2ecf20Sopenharmony_ci void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf)) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci struct vnic_wq_buf *buf; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci BUG_ON(ioread32(&wq->ctrl->enable)); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci buf = wq->to_clean; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci while (svnic_wq_desc_used(wq) > 0) { 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci (*buf_clean)(wq, buf); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci buf = wq->to_clean = buf->next; 2278c2ecf20Sopenharmony_ci wq->ring.desc_avail++; 2288c2ecf20Sopenharmony_ci } 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci wq->to_use = wq->to_clean = wq->bufs[0]; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci iowrite32(0, &wq->ctrl->fetch_index); 2338c2ecf20Sopenharmony_ci iowrite32(0, &wq->ctrl->posted_index); 2348c2ecf20Sopenharmony_ci iowrite32(0, &wq->ctrl->error_status); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci svnic_dev_clear_desc_ring(&wq->ring); 2378c2ecf20Sopenharmony_ci} 238