/*******************************************************************************
*
* Intel Ethernet Controller XL710 Family Linux Driver
* Copyright(c) 2013 Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* The full GNU General Public License is included in this distribution in
* the file called "COPYING".
*
* Contact Information:
* e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
* Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
*
******************************************************************************/
#ifdef CONFIG_DEBUG_FS
#include <linux/fs.h>
#include <linux/debugfs.h>
#include "i40e.h"
static struct dentry *i40e_dbg_root;
/**
* i40e_dbg_find_vsi - searches for the vsi with the given seid
* @pf - the pf structure to search for the vsi
* @seid - seid of the vsi it is searching for
**/
static struct i40e_vsi *i40e_dbg_find_vsi(struct i40e_pf *pf, int seid)
{
int i;
if (seid < 0)
dev_info(&pf->pdev->dev, "%d: bad seid\n", seid);
else
for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
if (pf->vsi[i] && (pf->vsi[i]->seid == seid))
return pf->vsi[i];
return NULL;
}
/**
* i40e_dbg_find_veb - searches for the veb with the given seid
* @pf - the pf structure to search for the veb
* @seid - seid of the veb it is searching for
**/
static struct i40e_veb *i40e_dbg_find_veb(struct i40e_pf *pf, int seid)
{
int i;
if ((seid < I40E_BASE_VEB_SEID) ||
(seid > (I40E_BASE_VEB_SEID + I40E_MAX_VEB)))
dev_info(&pf->pdev->dev, "%d: bad seid\n", seid);
else
for (i = 0; i < I40E_MAX_VEB; i++)
if (pf->veb[i] && pf->veb[i]->seid == seid)
return pf->veb[i];
return NULL;
}
/**************************************************************
* dump
* The dump entry in debugfs is for getting a data snapshow of
* the driver's current configuration and runtime details.
* When the filesystem entry is written, a snapshot is taken.
* When the entry is read, the most recent snapshot data is dumped.
**************************************************************/
static char *i40e_dbg_dump_buf;
static ssize_t i40e_dbg_dump_data_len;
static ssize_t i40e_dbg_dump_buffer_len;
/**
* i40e_dbg_dump_read - read the dump data
* @filp: the opened file
* @buffer: where to write the data for the user to read
* @count: the size of the user's buffer
* @ppos: file position offset
**/
static ssize_t i40e_dbg_dump_read(struct file *filp, char __user *buffer,
size_t count, loff_t *ppos)
{
int bytes_not_copied;
int len;
/* is *ppos bigger than the available data? */
if (*ppos >= i40e_dbg_dump_data_len || !i40e_dbg_dump_buf)
return 0;
/* be sure to not read beyond the end of available data */
len = min_t(int, count, (i40e_dbg_dump_data_len - *ppos));
bytes_not_copied = copy_to_user(buffer, &i40e_dbg_dump_buf[*ppos], len);
if (bytes_not_copied < 0)
return bytes_not_copied;
*ppos += len;
return len;
}
/**
* i40e_dbg_prep_dump_buf
* @pf: the pf we're working with
* @buflen: the desired buffer length
*
* Return positive if success, 0 if failed
**/
static int i40e_dbg_prep_dump_buf(struct i40e_pf *pf, int buflen)
{
/* if not already big enough, prep for re alloc */
if (i40e_dbg_dump_buffer_len && i40e_dbg_dump_buffer_len < buflen) {
kfree(i40e_dbg_dump_buf);
i40e_dbg_dump_buffer_len = 0;
i40e_dbg_dump_buf = NULL;
}
/* get a new buffer if needed */
if (!i40e_dbg_dump_buf) {
i40e_dbg_dump_buf = kzalloc(buflen, GFP_KERNEL);
if (i40e_dbg_dump_buf != NULL)
i40e_dbg_dump_buffer_len = buflen;
}
return i40e_dbg_dump_buffer_len;
}
/**
* i40e_dbg_dump_write - trigger a datadump snapshot
* @filp: the opened file
* @buffer: where to find the user's data
* @count: the length of the user's data
* @ppos: file position offset
*
* Any write clears the stats
**/
static ssize_t i40e_dbg_dump_write(struct file *filp,
const char __user *buffer,
size_t count, loff_t *ppos)
{
struct i40e_pf *pf = filp->private_data;
bool seid_found = false;
long seid = -1;
int buflen = 0;
int i, ret;
int len;
u8 *p;
/* don't allow partial writes */
if (*ppos != 0)
return 0;
/* decode the SEID given to be dumped */
ret = kstrtol_from_user(buffer, count, 0, &seid);
if (ret) {
dev_info(&pf->pdev->dev, "bad seid value\n");
} else if (seid == 0) {
seid_found = true;
kfree(i40e_dbg_dump_buf);
i40e_dbg_dump_buffer_len = 0;
i40e_dbg_dump_data_len = 0;
i40e_dbg_dump_buf = NULL;
dev_info(&pf->pdev->dev, "debug buffer freed\n");
} else if (seid == pf->pf_seid || seid == 1) {
seid_found = true;
buflen = sizeof(struct i40e_pf);
buflen += (sizeof(struct i40e_aq_desc)
* (pf->hw.aq.num_arq_entries + pf->hw.aq.num_asq_entries));
if (i40e_dbg_prep_dump_buf(pf, buflen)) {
p = i40e_dbg_dump_buf;
len = sizeof(struct i40e_pf);
memcpy(p, pf, len);
p += len;
len = (sizeof(struct i40e_aq_desc)
* pf->hw.aq.num_asq_entries);
memcpy(p, pf->hw.aq.asq.desc, len);
p += len;
len = (sizeof(struct i40e_aq_desc)
* pf->hw.aq.num_arq_entries);
memcpy(p, pf->hw.aq.arq.desc, len);
p += len;
i40e_dbg_dump_data_len = buflen;
dev_info(&pf->pdev->dev,
"PF seid %ld dumped %d bytes\n",
seid, (int)i40e_dbg_dump_data_len);
}
} else if (seid >= I40E_BASE_VSI_SEID) {
struct i40e_vsi *vsi = NULL;
struct i40e_mac_filter *f;
int filter_count = 0;
mutex_lock(&pf->switch_mutex);
vsi = i40e_dbg_find_vsi(pf, seid);
if (!vsi) {
mutex_unlock(&pf->switch_mutex);
goto write_exit;
}
buflen = sizeof(struct i40e_vsi);
buflen += sizeof(struct i40e_q_vector) * vsi->num_q_vectors;
buflen += sizeof(struct i40e_ring) * 2 * vsi->num_queue_pairs;
buflen += sizeof(struct i40e_tx_buffer) * vsi->num_queue_pairs;
buflen += sizeof(struct i40e_rx_buffer) * vsi->num_queue_pairs;
list_for_each_entry(f, &vsi->mac_filter_list, list)
filter_count++;
buflen += sizeof(struct i40e_mac_filter) * filter_count;
if (i40e_dbg_prep_dump_buf(pf, buflen)) {
p = i40e_dbg_dump_buf;
seid_found = true;
len = sizeof(struct i40e_vsi);
memcpy(p, vsi, len);
p += len;
if (vsi->num_q_vectors) {
len = (sizeof(struct i40e_q_vector)
* vsi->num_q_vectors);
memcpy(p, vsi->q_vectors, len);
p += len;
}
if (vsi->num_queue_pairs) {
len = (sizeof(struct i40e_ring) *
vsi->num_queue_pairs);
memcpy(p, vsi->tx_rings, len);
p += len;
memcpy(p, vsi->rx_rings, len);
p += len;
}
if (vsi->tx_rings[0]) {
len = sizeof(struct i40e_tx_buffer);
for (i = 0; i < vsi->num_queue_pairs; i++) {
memcpy(p, vsi->tx_rings[i]->tx_bi, len);
p += len;
}
len = sizeof(struct i40e_rx_buffer);
for (i = 0; i < vsi->num_queue_pairs; i++) {
memcpy(p, vsi->rx_rings[i]->rx_bi, len);
p += len;
}
}
/* macvlan filter list */
len = sizeof(struct i40e_mac_filter);
list_for_each_entry(f, &vsi->mac_filter_list, list) {
memcpy(p, f, len);
p += len;
}
i40e_dbg_dump_data_len = buflen;
dev_info(&pf->pdev->dev,
"VSI seid %ld dumped %d bytes\n",
seid, (int)i40e_dbg_dump_data_len);
}
mutex_unlock(&pf->switch_mutex);
} else if (seid >= I40E_BASE_VEB_SEID) {
struct i40e_veb *veb = NULL;
mutex_lock(&pf->switch_mutex);
veb = i40e_dbg_find_veb(pf, seid);
if (!veb) {