aq_pci_func.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * aQuantia Corporation Network Driver
 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
 *
 * 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.
 */

/* File aq_pci_func.c: Definition of PCI functions. */

12 13 14
#include <linux/interrupt.h>
#include <linux/module.h>

15
#include "aq_main.h"
16 17 18
#include "aq_nic.h"
#include "aq_vec.h"
#include "aq_hw.h"
19
#include "aq_pci_func.h"
20 21
#include "hw_atl/hw_atl_a0.h"
#include "hw_atl/hw_atl_b0.h"
22

23
static const struct pci_device_id aq_pci_tbl[] = {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_0001), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_D100), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_D107), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_D108), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_D109), },

	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC100), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC107), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC108), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC109), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC111), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC112), },

	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC100S), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC107S), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC108S), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC109S), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC111S), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC112S), },

	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC111E), },
	{ PCI_VDEVICE(AQUANTIA, AQ_DEVICE_ID_AQC112E), },

47 48 49
	{}
};

50
static const struct aq_board_revision_s hw_atl_boards[] = {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	{ AQ_DEVICE_ID_0001,	AQ_HWREV_1,	&hw_atl_ops_a0, &hw_atl_a0_caps_aqc107, },
	{ AQ_DEVICE_ID_D100,	AQ_HWREV_1,	&hw_atl_ops_a0, &hw_atl_a0_caps_aqc100, },
	{ AQ_DEVICE_ID_D107,	AQ_HWREV_1,	&hw_atl_ops_a0, &hw_atl_a0_caps_aqc107, },
	{ AQ_DEVICE_ID_D108,	AQ_HWREV_1,	&hw_atl_ops_a0, &hw_atl_a0_caps_aqc108, },
	{ AQ_DEVICE_ID_D109,	AQ_HWREV_1,	&hw_atl_ops_a0, &hw_atl_a0_caps_aqc109, },

	{ AQ_DEVICE_ID_0001,	AQ_HWREV_2,	&hw_atl_ops_b0, &hw_atl_b0_caps_aqc107, },
	{ AQ_DEVICE_ID_D100,	AQ_HWREV_2,	&hw_atl_ops_b0, &hw_atl_b0_caps_aqc100, },
	{ AQ_DEVICE_ID_D107,	AQ_HWREV_2,	&hw_atl_ops_b0, &hw_atl_b0_caps_aqc107, },
	{ AQ_DEVICE_ID_D108,	AQ_HWREV_2,	&hw_atl_ops_b0, &hw_atl_b0_caps_aqc108, },
	{ AQ_DEVICE_ID_D109,	AQ_HWREV_2,	&hw_atl_ops_b0, &hw_atl_b0_caps_aqc109, },

	{ AQ_DEVICE_ID_AQC100,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc107, },
	{ AQ_DEVICE_ID_AQC107,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc107, },
	{ AQ_DEVICE_ID_AQC108,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc108, },
	{ AQ_DEVICE_ID_AQC109,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc109, },
	{ AQ_DEVICE_ID_AQC111,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc111, },
	{ AQ_DEVICE_ID_AQC112,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc112, },

	{ AQ_DEVICE_ID_AQC100S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc100s, },
	{ AQ_DEVICE_ID_AQC107S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc107s, },
	{ AQ_DEVICE_ID_AQC108S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc108s, },
	{ AQ_DEVICE_ID_AQC109S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc109s, },
	{ AQ_DEVICE_ID_AQC111S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc111s, },
	{ AQ_DEVICE_ID_AQC112S,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc112s, },

	{ AQ_DEVICE_ID_AQC111E,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc111e, },
	{ AQ_DEVICE_ID_AQC112E,	AQ_HWREV_ANY,	&hw_atl_ops_b1, &hw_atl_b0_caps_aqc112e, },
};

81 82
MODULE_DEVICE_TABLE(pci, aq_pci_tbl);

83 84 85
static int aq_pci_probe_get_hw_by_id(struct pci_dev *pdev,
				     const struct aq_hw_ops **ops,
				     const struct aq_hw_caps_s **caps)
86
{
87 88 89 90 91 92 93 94 95 96 97 98 99 100
	int i = 0;

	if (pdev->vendor != PCI_VENDOR_ID_AQUANTIA)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(hw_atl_boards); i++) {
		if (hw_atl_boards[i].devid == pdev->device &&
		    (hw_atl_boards[i].revision == AQ_HWREV_ANY ||
		     hw_atl_boards[i].revision == pdev->revision)) {
			*ops = hw_atl_boards[i].ops;
			*caps = hw_atl_boards[i].caps;
			break;
		}
	}
101

102 103
	if (i == ARRAY_SIZE(hw_atl_boards))
		return -EINVAL;
104

105
	return 0;
106 107
}

108
int aq_pci_func_init(struct pci_dev *pdev)
109 110 111
{
	int err = 0;

112
	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
113
	if (!err) {
114 115
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));

116 117
	}
	if (err) {
118
		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
119
		if (!err)
120
			err = pci_set_consistent_dma_mask(pdev,
121 122 123 124 125 126 127
							  DMA_BIT_MASK(32));
	}
	if (err != 0) {
		err = -ENOSR;
		goto err_exit;
	}

128
	err = pci_request_regions(pdev, AQ_CFG_DRV_NAME "_mmio");
129 130 131
	if (err < 0)
		goto err_exit;

132
	pci_set_master(pdev);
133

134
	return 0;
135 136 137 138 139

err_exit:
	return err;
}

140
int aq_pci_func_alloc_irq(struct aq_nic_s *self, unsigned int i,
141 142
			  char *name, void *aq_vec, cpumask_t *affinity_mask)
{
143
	struct pci_dev *pdev = self->pdev;
144 145
	int err = 0;

146 147
	if (pdev->msix_enabled || pdev->msi_enabled)
		err = request_irq(pci_irq_vector(pdev, i), aq_vec_isr, 0,
148
				  name, aq_vec);
149 150
	else
		err = request_irq(pci_irq_vector(pdev, i), aq_vec_isr_legacy,
151 152 153 154 155 156
				  IRQF_SHARED, name, aq_vec);

	if (err >= 0) {
		self->msix_entry_mask |= (1 << i);
		self->aq_vec[i] = aq_vec;

157 158
		if (pdev->msix_enabled)
			irq_set_affinity_hint(pci_irq_vector(pdev, i),
159 160 161 162 163
					      affinity_mask);
	}
	return err;
}

164
void aq_pci_func_free_irqs(struct aq_nic_s *self)
165
{
166
	struct pci_dev *pdev = self->pdev;
167 168 169 170 171 172
	unsigned int i = 0U;

	for (i = 32U; i--;) {
		if (!((1U << i) & self->msix_entry_mask))
			continue;

173 174
		if (pdev->msix_enabled)
			irq_set_affinity_hint(pci_irq_vector(pdev, i), NULL);
175
		free_irq(pci_irq_vector(pdev, i), self->aq_vec[i]);
176 177 178 179
		self->msix_entry_mask &= ~(1U << i);
	}
}

180
unsigned int aq_pci_func_get_irq_type(struct aq_nic_s *self)
181
{
182 183 184 185 186
	if (self->pdev->msix_enabled)
		return AQ_HW_IRQ_MSIX;
	if (self->pdev->msi_enabled)
		return AQ_HW_IRQ_MSIX;
	return AQ_HW_IRQ_LEGACY;
187 188
}

189
static void aq_pci_free_irq_vectors(struct aq_nic_s *self)
190
{
191
	pci_free_irq_vectors(self->pdev);
192
}
193

194 195 196 197 198 199 200 201 202
static int aq_pci_probe(struct pci_dev *pdev,
			const struct pci_device_id *pci_id)
{
	struct aq_nic_s *self = NULL;
	int err = 0;
	struct net_device *ndev;
	resource_size_t mmio_pa;
	u32 bar;
	u32 numvecs;
203

204 205 206
	err = pci_enable_device(pdev);
	if (err)
		return err;
207

208 209 210
	err = aq_pci_func_init(pdev);
	if (err)
		goto err_pci_func;
211

212
	ndev = aq_ndev_alloc();
213 214
	if (!ndev) {
		err = -ENOMEM;
215
		goto err_ndev;
216
	}
217

218 219 220 221
	self = netdev_priv(ndev);
	self->pdev = pdev;
	SET_NETDEV_DEV(ndev, &pdev->dev);
	pci_set_drvdata(pdev, self);
222

223 224 225 226
	err = aq_pci_probe_get_hw_by_id(pdev, &self->aq_hw_ops,
					&aq_nic_get_cfg(self)->aq_hw_caps);
	if (err)
		goto err_ioremap;
227

228
	self->aq_hw = kzalloc(sizeof(*self->aq_hw), GFP_KERNEL);
229 230 231 232
	if (!self->aq_hw) {
		err = -ENOMEM;
		goto err_ioremap;
	}
233
	self->aq_hw->aq_nic_cfg = aq_nic_get_cfg(self);
234

235 236 237
	for (bar = 0; bar < 4; ++bar) {
		if (IORESOURCE_MEM & pci_resource_flags(pdev, bar)) {
			resource_size_t reg_sz;
238

239 240 241
			mmio_pa = pci_resource_start(pdev, bar);
			if (mmio_pa == 0U) {
				err = -EIO;
242
				goto err_free_aq_hw;
243
			}
244

245 246 247
			reg_sz = pci_resource_len(pdev, bar);
			if ((reg_sz <= 24 /*ATL_REGS_SIZE*/)) {
				err = -EIO;
248
				goto err_free_aq_hw;
249
			}
250

251 252 253
			self->aq_hw->mmio = ioremap_nocache(mmio_pa, reg_sz);
			if (!self->aq_hw->mmio) {
				err = -EIO;
254
				goto err_free_aq_hw;
255 256 257 258
			}
			break;
		}
	}
259

260 261
	if (bar == 4) {
		err = -EIO;
262
		goto err_free_aq_hw;
263 264
	}

265 266 267 268 269 270 271 272 273 274 275 276 277
	numvecs = min((u8)AQ_CFG_VECS_DEF,
		      aq_nic_get_cfg(self)->aq_hw_caps->msix_irqs);
	numvecs = min(numvecs, num_online_cpus());
	/*enable interrupts */
#if !AQ_CFG_FORCE_LEGACY_INT
	err = pci_alloc_irq_vectors(self->pdev, numvecs, numvecs,
				    PCI_IRQ_MSIX);

	if (err < 0) {
		err = pci_alloc_irq_vectors(self->pdev, 1, 1,
					    PCI_IRQ_MSI | PCI_IRQ_LEGACY);
		if (err < 0)
			goto err_hwinit;
278
	}
279
#endif
280

281 282
	/* net device init */
	aq_nic_cfg_start(self);
283

284
	aq_nic_ndev_init(self);
285

286
	err = aq_nic_ndev_register(self);
287
	if (err < 0)
288
		goto err_register;
289

290 291 292 293 294 295 296
	return 0;

err_register:
	aq_nic_free_vectors(self);
	aq_pci_free_irq_vectors(self);
err_hwinit:
	iounmap(self->aq_hw->mmio);
297 298
err_free_aq_hw:
	kfree(self->aq_hw);
299 300 301 302 303 304
err_ioremap:
	free_netdev(ndev);
err_pci_func:
	pci_release_regions(pdev);
err_ndev:
	pci_disable_device(pdev);
305 306 307 308 309
	return err;
}

static void aq_pci_remove(struct pci_dev *pdev)
{
310 311 312 313 314 315 316 317 318 319 320 321
	struct aq_nic_s *self = pci_get_drvdata(pdev);

	if (self->ndev) {
		if (self->ndev->reg_state == NETREG_REGISTERED)
			unregister_netdev(self->ndev);
		aq_nic_free_vectors(self);
		aq_pci_free_irq_vectors(self);
		iounmap(self->aq_hw->mmio);
		kfree(self->aq_hw);
		pci_release_regions(pdev);
		free_netdev(self->ndev);
	}
322

323
	pci_disable_device(pdev);
324 325 326 327
}

static int aq_pci_suspend(struct pci_dev *pdev, pm_message_t pm_msg)
{
328
	struct aq_nic_s *self = pci_get_drvdata(pdev);
329

330
	return aq_nic_change_pm_state(self, &pm_msg);
331 332 333 334
}

static int aq_pci_resume(struct pci_dev *pdev)
{
335
	struct aq_nic_s *self = pci_get_drvdata(pdev);
336 337
	pm_message_t pm_msg = PMSG_RESTORE;

338
	return aq_nic_change_pm_state(self, &pm_msg);
339 340 341 342 343 344 345 346 347 348 349 350
}

static struct pci_driver aq_pci_ops = {
	.name = AQ_CFG_DRV_NAME,
	.id_table = aq_pci_tbl,
	.probe = aq_pci_probe,
	.remove = aq_pci_remove,
	.suspend = aq_pci_suspend,
	.resume = aq_pci_resume,
};

module_pci_driver(aq_pci_ops);