18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2008 Cisco Systems, Inc. All rights reserved. 38c2ecf20Sopenharmony_ci * Copyright 2007 Nuova Systems, Inc. All rights reserved. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This program is free software; you may redistribute it and/or modify 68c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License as published by 78c2ecf20Sopenharmony_ci * the Free Software Foundation; version 2 of the License. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 108c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 118c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 128c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 138c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 148c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 158c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 168c2ecf20Sopenharmony_ci * SOFTWARE. 178c2ecf20Sopenharmony_ci */ 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/errno.h> 208c2ecf20Sopenharmony_ci#include <linux/types.h> 218c2ecf20Sopenharmony_ci#include <linux/pci.h> 228c2ecf20Sopenharmony_ci#include <linux/delay.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci#include "vnic_dev.h" 258c2ecf20Sopenharmony_ci#include "vnic_rq.h" 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic int vnic_rq_alloc_bufs(struct vnic_rq *rq) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct vnic_rq_buf *buf; 308c2ecf20Sopenharmony_ci unsigned int i, j, count = rq->ring.desc_count; 318c2ecf20Sopenharmony_ci unsigned int blks = VNIC_RQ_BUF_BLKS_NEEDED(count); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci for (i = 0; i < blks; i++) { 348c2ecf20Sopenharmony_ci rq->bufs[i] = kzalloc(VNIC_RQ_BUF_BLK_SZ, GFP_ATOMIC); 358c2ecf20Sopenharmony_ci if (!rq->bufs[i]) { 368c2ecf20Sopenharmony_ci printk(KERN_ERR "Failed to alloc rq_bufs\n"); 378c2ecf20Sopenharmony_ci return -ENOMEM; 388c2ecf20Sopenharmony_ci } 398c2ecf20Sopenharmony_ci } 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci for (i = 0; i < blks; i++) { 428c2ecf20Sopenharmony_ci buf = rq->bufs[i]; 438c2ecf20Sopenharmony_ci for (j = 0; j < VNIC_RQ_BUF_BLK_ENTRIES; j++) { 448c2ecf20Sopenharmony_ci buf->index = i * VNIC_RQ_BUF_BLK_ENTRIES + j; 458c2ecf20Sopenharmony_ci buf->desc = (u8 *)rq->ring.descs + 468c2ecf20Sopenharmony_ci rq->ring.desc_size * buf->index; 478c2ecf20Sopenharmony_ci if (buf->index + 1 == count) { 488c2ecf20Sopenharmony_ci buf->next = rq->bufs[0]; 498c2ecf20Sopenharmony_ci break; 508c2ecf20Sopenharmony_ci } else if (j + 1 == VNIC_RQ_BUF_BLK_ENTRIES) { 518c2ecf20Sopenharmony_ci buf->next = rq->bufs[i + 1]; 528c2ecf20Sopenharmony_ci } else { 538c2ecf20Sopenharmony_ci buf->next = buf + 1; 548c2ecf20Sopenharmony_ci buf++; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci } 578c2ecf20Sopenharmony_ci } 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci rq->to_use = rq->to_clean = rq->bufs[0]; 608c2ecf20Sopenharmony_ci rq->buf_index = 0; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return 0; 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_civoid vnic_rq_free(struct vnic_rq *rq) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct vnic_dev *vdev; 688c2ecf20Sopenharmony_ci unsigned int i; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci vdev = rq->vdev; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci vnic_dev_free_desc_ring(vdev, &rq->ring); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci for (i = 0; i < VNIC_RQ_BUF_BLKS_MAX; i++) { 758c2ecf20Sopenharmony_ci kfree(rq->bufs[i]); 768c2ecf20Sopenharmony_ci rq->bufs[i] = NULL; 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci rq->ctrl = NULL; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciint vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index, 838c2ecf20Sopenharmony_ci unsigned int desc_count, unsigned int desc_size) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci int err; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci rq->index = index; 888c2ecf20Sopenharmony_ci rq->vdev = vdev; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci rq->ctrl = vnic_dev_get_res(vdev, RES_TYPE_RQ, index); 918c2ecf20Sopenharmony_ci if (!rq->ctrl) { 928c2ecf20Sopenharmony_ci printk(KERN_ERR "Failed to hook RQ[%d] resource\n", index); 938c2ecf20Sopenharmony_ci return -EINVAL; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci vnic_rq_disable(rq); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci err = vnic_dev_alloc_desc_ring(vdev, &rq->ring, desc_count, desc_size); 998c2ecf20Sopenharmony_ci if (err) 1008c2ecf20Sopenharmony_ci return err; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci err = vnic_rq_alloc_bufs(rq); 1038c2ecf20Sopenharmony_ci if (err) { 1048c2ecf20Sopenharmony_ci vnic_rq_free(rq); 1058c2ecf20Sopenharmony_ci return err; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return 0; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_civoid vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index, 1128c2ecf20Sopenharmony_ci unsigned int error_interrupt_enable, 1138c2ecf20Sopenharmony_ci unsigned int error_interrupt_offset) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci u64 paddr; 1168c2ecf20Sopenharmony_ci u32 fetch_index; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci paddr = (u64)rq->ring.base_addr | VNIC_PADDR_TARGET; 1198c2ecf20Sopenharmony_ci writeq(paddr, &rq->ctrl->ring_base); 1208c2ecf20Sopenharmony_ci iowrite32(rq->ring.desc_count, &rq->ctrl->ring_size); 1218c2ecf20Sopenharmony_ci iowrite32(cq_index, &rq->ctrl->cq_index); 1228c2ecf20Sopenharmony_ci iowrite32(error_interrupt_enable, &rq->ctrl->error_interrupt_enable); 1238c2ecf20Sopenharmony_ci iowrite32(error_interrupt_offset, &rq->ctrl->error_interrupt_offset); 1248c2ecf20Sopenharmony_ci iowrite32(0, &rq->ctrl->dropped_packet_count); 1258c2ecf20Sopenharmony_ci iowrite32(0, &rq->ctrl->error_status); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* Use current fetch_index as the ring starting point */ 1288c2ecf20Sopenharmony_ci fetch_index = ioread32(&rq->ctrl->fetch_index); 1298c2ecf20Sopenharmony_ci rq->to_use = rq->to_clean = 1308c2ecf20Sopenharmony_ci &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES] 1318c2ecf20Sopenharmony_ci [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES]; 1328c2ecf20Sopenharmony_ci iowrite32(fetch_index, &rq->ctrl->posted_index); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci rq->buf_index = 0; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ciunsigned int vnic_rq_error_status(struct vnic_rq *rq) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci return ioread32(&rq->ctrl->error_status); 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_civoid vnic_rq_enable(struct vnic_rq *rq) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci iowrite32(1, &rq->ctrl->enable); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ciint vnic_rq_disable(struct vnic_rq *rq) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci unsigned int wait; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci iowrite32(0, &rq->ctrl->enable); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* Wait for HW to ACK disable request */ 1548c2ecf20Sopenharmony_ci for (wait = 0; wait < 100; wait++) { 1558c2ecf20Sopenharmony_ci if (!(ioread32(&rq->ctrl->running))) 1568c2ecf20Sopenharmony_ci return 0; 1578c2ecf20Sopenharmony_ci udelay(1); 1588c2ecf20Sopenharmony_ci } 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci printk(KERN_ERR "Failed to disable RQ[%d]\n", rq->index); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return -ETIMEDOUT; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_civoid vnic_rq_clean(struct vnic_rq *rq, 1668c2ecf20Sopenharmony_ci void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf)) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci struct vnic_rq_buf *buf; 1698c2ecf20Sopenharmony_ci u32 fetch_index; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci WARN_ON(ioread32(&rq->ctrl->enable)); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci buf = rq->to_clean; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci while (vnic_rq_desc_used(rq) > 0) { 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci (*buf_clean)(rq, buf); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci buf = rq->to_clean = buf->next; 1808c2ecf20Sopenharmony_ci rq->ring.desc_avail++; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* Use current fetch_index as the ring starting point */ 1848c2ecf20Sopenharmony_ci fetch_index = ioread32(&rq->ctrl->fetch_index); 1858c2ecf20Sopenharmony_ci rq->to_use = rq->to_clean = 1868c2ecf20Sopenharmony_ci &rq->bufs[fetch_index / VNIC_RQ_BUF_BLK_ENTRIES] 1878c2ecf20Sopenharmony_ci [fetch_index % VNIC_RQ_BUF_BLK_ENTRIES]; 1888c2ecf20Sopenharmony_ci iowrite32(fetch_index, &rq->ctrl->posted_index); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci rq->buf_index = 0; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci vnic_dev_clear_desc_ring(&rq->ring); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 195