162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This software is available to you under a choice of one of two
662306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
962306a36Sopenharmony_ci * OpenIB.org BSD license below:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1262306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1362306a36Sopenharmony_ci *     conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1662306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1762306a36Sopenharmony_ci *        disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2262306a36Sopenharmony_ci *        provided with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3162306a36Sopenharmony_ci * SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include <linux/errno.h>
3562306a36Sopenharmony_ci#include <linux/pci.h>
3662306a36Sopenharmony_ci#include <linux/delay.h>
3762306a36Sopenharmony_ci#include <linux/slab.h>
3862306a36Sopenharmony_ci#include <linux/jiffies.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#include "mlx4.h"
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciint mlx4_reset(struct mlx4_dev *dev)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	void __iomem *reset;
4562306a36Sopenharmony_ci	u32 *hca_header = NULL;
4662306a36Sopenharmony_ci	int pcie_cap;
4762306a36Sopenharmony_ci	u16 devctl;
4862306a36Sopenharmony_ci	u16 linkctl;
4962306a36Sopenharmony_ci	u16 vendor;
5062306a36Sopenharmony_ci	unsigned long end;
5162306a36Sopenharmony_ci	u32 sem;
5262306a36Sopenharmony_ci	int i;
5362306a36Sopenharmony_ci	int err = 0;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define MLX4_RESET_BASE		0xf0000
5662306a36Sopenharmony_ci#define MLX4_RESET_SIZE		  0x400
5762306a36Sopenharmony_ci#define MLX4_SEM_OFFSET		  0x3fc
5862306a36Sopenharmony_ci#define MLX4_RESET_OFFSET	   0x10
5962306a36Sopenharmony_ci#define MLX4_RESET_VALUE	swab32(1)
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define MLX4_SEM_TIMEOUT_JIFFIES	(10 * HZ)
6262306a36Sopenharmony_ci#define MLX4_RESET_TIMEOUT_JIFFIES	(2 * HZ)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	/*
6562306a36Sopenharmony_ci	 * Reset the chip.  This is somewhat ugly because we have to
6662306a36Sopenharmony_ci	 * save off the PCI header before reset and then restore it
6762306a36Sopenharmony_ci	 * after the chip reboots.  We skip config space offsets 22
6862306a36Sopenharmony_ci	 * and 23 since those have a special meaning.
6962306a36Sopenharmony_ci	 */
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	/* Do we need to save off the full 4K PCI Express header?? */
7262306a36Sopenharmony_ci	hca_header = kmalloc(256, GFP_KERNEL);
7362306a36Sopenharmony_ci	if (!hca_header) {
7462306a36Sopenharmony_ci		err = -ENOMEM;
7562306a36Sopenharmony_ci		mlx4_err(dev, "Couldn't allocate memory to save HCA PCI header, aborting\n");
7662306a36Sopenharmony_ci		goto out;
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	pcie_cap = pci_pcie_cap(dev->persist->pdev);
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	for (i = 0; i < 64; ++i) {
8262306a36Sopenharmony_ci		if (i == 22 || i == 23)
8362306a36Sopenharmony_ci			continue;
8462306a36Sopenharmony_ci		if (pci_read_config_dword(dev->persist->pdev, i * 4,
8562306a36Sopenharmony_ci					  hca_header + i)) {
8662306a36Sopenharmony_ci			err = -ENODEV;
8762306a36Sopenharmony_ci			mlx4_err(dev, "Couldn't save HCA PCI header, aborting\n");
8862306a36Sopenharmony_ci			goto out;
8962306a36Sopenharmony_ci		}
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	reset = ioremap(pci_resource_start(dev->persist->pdev, 0) +
9362306a36Sopenharmony_ci			MLX4_RESET_BASE,
9462306a36Sopenharmony_ci			MLX4_RESET_SIZE);
9562306a36Sopenharmony_ci	if (!reset) {
9662306a36Sopenharmony_ci		err = -ENOMEM;
9762306a36Sopenharmony_ci		mlx4_err(dev, "Couldn't map HCA reset register, aborting\n");
9862306a36Sopenharmony_ci		goto out;
9962306a36Sopenharmony_ci	}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* grab HW semaphore to lock out flash updates */
10262306a36Sopenharmony_ci	end = jiffies + MLX4_SEM_TIMEOUT_JIFFIES;
10362306a36Sopenharmony_ci	do {
10462306a36Sopenharmony_ci		sem = readl(reset + MLX4_SEM_OFFSET);
10562306a36Sopenharmony_ci		if (!sem)
10662306a36Sopenharmony_ci			break;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci		msleep(1);
10962306a36Sopenharmony_ci	} while (time_before(jiffies, end));
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	if (sem) {
11262306a36Sopenharmony_ci		mlx4_err(dev, "Failed to obtain HW semaphore, aborting\n");
11362306a36Sopenharmony_ci		err = -EAGAIN;
11462306a36Sopenharmony_ci		iounmap(reset);
11562306a36Sopenharmony_ci		goto out;
11662306a36Sopenharmony_ci	}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	/* actually hit reset */
11962306a36Sopenharmony_ci	writel(MLX4_RESET_VALUE, reset + MLX4_RESET_OFFSET);
12062306a36Sopenharmony_ci	iounmap(reset);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	/* Docs say to wait one second before accessing device */
12362306a36Sopenharmony_ci	msleep(1000);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	end = jiffies + MLX4_RESET_TIMEOUT_JIFFIES;
12662306a36Sopenharmony_ci	do {
12762306a36Sopenharmony_ci		if (!pci_read_config_word(dev->persist->pdev, PCI_VENDOR_ID,
12862306a36Sopenharmony_ci					  &vendor) && vendor != 0xffff)
12962306a36Sopenharmony_ci			break;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci		msleep(1);
13262306a36Sopenharmony_ci	} while (time_before(jiffies, end));
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	if (vendor == 0xffff) {
13562306a36Sopenharmony_ci		err = -ENODEV;
13662306a36Sopenharmony_ci		mlx4_err(dev, "PCI device did not come back after reset, aborting\n");
13762306a36Sopenharmony_ci		goto out;
13862306a36Sopenharmony_ci	}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* Now restore the PCI headers */
14162306a36Sopenharmony_ci	if (pcie_cap) {
14262306a36Sopenharmony_ci		devctl = hca_header[(pcie_cap + PCI_EXP_DEVCTL) / 4];
14362306a36Sopenharmony_ci		if (pcie_capability_write_word(dev->persist->pdev,
14462306a36Sopenharmony_ci					       PCI_EXP_DEVCTL,
14562306a36Sopenharmony_ci					       devctl)) {
14662306a36Sopenharmony_ci			err = -ENODEV;
14762306a36Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA PCI Express Device Control register, aborting\n");
14862306a36Sopenharmony_ci			goto out;
14962306a36Sopenharmony_ci		}
15062306a36Sopenharmony_ci		linkctl = hca_header[(pcie_cap + PCI_EXP_LNKCTL) / 4];
15162306a36Sopenharmony_ci		if (pcie_capability_write_word(dev->persist->pdev,
15262306a36Sopenharmony_ci					       PCI_EXP_LNKCTL,
15362306a36Sopenharmony_ci					       linkctl)) {
15462306a36Sopenharmony_ci			err = -ENODEV;
15562306a36Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA PCI Express Link control register, aborting\n");
15662306a36Sopenharmony_ci			goto out;
15762306a36Sopenharmony_ci		}
15862306a36Sopenharmony_ci	}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	for (i = 0; i < 16; ++i) {
16162306a36Sopenharmony_ci		if (i * 4 == PCI_COMMAND)
16262306a36Sopenharmony_ci			continue;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci		if (pci_write_config_dword(dev->persist->pdev, i * 4,
16562306a36Sopenharmony_ci					   hca_header[i])) {
16662306a36Sopenharmony_ci			err = -ENODEV;
16762306a36Sopenharmony_ci			mlx4_err(dev, "Couldn't restore HCA reg %x, aborting\n",
16862306a36Sopenharmony_ci				 i);
16962306a36Sopenharmony_ci			goto out;
17062306a36Sopenharmony_ci		}
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	if (pci_write_config_dword(dev->persist->pdev, PCI_COMMAND,
17462306a36Sopenharmony_ci				   hca_header[PCI_COMMAND / 4])) {
17562306a36Sopenharmony_ci		err = -ENODEV;
17662306a36Sopenharmony_ci		mlx4_err(dev, "Couldn't restore HCA COMMAND, aborting\n");
17762306a36Sopenharmony_ci		goto out;
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciout:
18162306a36Sopenharmony_ci	kfree(hca_header);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	return err;
18462306a36Sopenharmony_ci}
185