blob: 170e8e60cdb7fe3d47307d4b9146039e3af7f3a8 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * BSD LICENSE
14 *
15 * Copyright(c) 2012 Intel Corporation. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * * Neither the name of Intel Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * Intel PCIe NTB Linux driver
44 *
45 * Contact Information:
46 * Jon Mason <jon.mason@intel.com>
47 */
48#include <linux/debugfs.h>
Jon Mason113bf1c2012-11-16 18:52:57 -070049#include <linux/delay.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070050#include <linux/init.h>
51#include <linux/interrupt.h>
52#include <linux/module.h>
53#include <linux/pci.h>
Jon Mason113bf1c2012-11-16 18:52:57 -070054#include <linux/random.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070055#include <linux/slab.h>
56#include "ntb_hw.h"
57#include "ntb_regs.h"
58
59#define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
Jon Masondb3bb3f2013-07-30 15:44:05 -070060#define NTB_VER "1.0"
Jon Masonfce8a7b2012-11-16 19:27:12 -070061
62MODULE_DESCRIPTION(NTB_NAME);
63MODULE_VERSION(NTB_VER);
64MODULE_LICENSE("Dual BSD/GPL");
65MODULE_AUTHOR("Intel Corporation");
66
Jon Mason948d3a62013-04-18 17:07:36 -070067static bool xeon_errata_workaround = true;
68module_param(xeon_errata_workaround, bool, 0644);
69MODULE_PARM_DESC(xeon_errata_workaround, "Workaround for the Xeon Errata");
70
Jon Masonfce8a7b2012-11-16 19:27:12 -070071enum {
Jon Masoned6c24e2013-07-15 16:43:54 -070072 NTB_CONN_TRANSPARENT = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -070073 NTB_CONN_B2B,
74 NTB_CONN_RP,
75};
76
77enum {
78 NTB_DEV_USD = 0,
79 NTB_DEV_DSD,
80};
81
82enum {
83 SNB_HW = 0,
84 BWD_HW,
85};
86
Jon Mason1517a3f2013-07-30 15:58:49 -070087static struct dentry *debugfs_dir;
88
Jon Mason113bf1c2012-11-16 18:52:57 -070089#define BWD_LINK_RECOVERY_TIME 500
90
Jon Masonfce8a7b2012-11-16 19:27:12 -070091/* Translate memory window 0,1 to BAR 2,4 */
Jon Mason948d3a62013-04-18 17:07:36 -070092#define MW_TO_BAR(mw) (mw * NTB_MAX_NUM_MW + 2)
Jon Masonfce8a7b2012-11-16 19:27:12 -070093
94static DEFINE_PCI_DEVICE_TABLE(ntb_pci_tbl) = {
95 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
96 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
Jon Masonfce8a7b2012-11-16 19:27:12 -070097 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
Jon Masonbe4dac02012-09-28 11:38:48 -070098 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
99 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
100 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
101 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
102 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
103 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
104 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
105 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
106 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
107 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
Jon Masonfce8a7b2012-11-16 19:27:12 -0700108 {0}
109};
110MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
111
112/**
113 * ntb_register_event_callback() - register event callback
114 * @ndev: pointer to ntb_device instance
115 * @func: callback function to register
116 *
117 * This function registers a callback for any HW driver events such as link
118 * up/down, power management notices and etc.
119 *
120 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
121 */
122int ntb_register_event_callback(struct ntb_device *ndev,
Jon Mason74465642013-01-21 15:28:52 -0700123 void (*func)(void *handle, enum ntb_hw_event event))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700124{
125 if (ndev->event_cb)
126 return -EINVAL;
127
128 ndev->event_cb = func;
129
130 return 0;
131}
132
133/**
134 * ntb_unregister_event_callback() - unregisters the event callback
135 * @ndev: pointer to ntb_device instance
136 *
137 * This function unregisters the existing callback from transport
138 */
139void ntb_unregister_event_callback(struct ntb_device *ndev)
140{
141 ndev->event_cb = NULL;
142}
143
Jon Masone8aeb602013-04-18 17:59:44 -0700144static void ntb_irq_work(unsigned long data)
145{
146 struct ntb_db_cb *db_cb = (struct ntb_db_cb *)data;
147 int rc;
148
149 rc = db_cb->callback(db_cb->data, db_cb->db_num);
150 if (rc)
151 tasklet_schedule(&db_cb->irq_work);
152 else {
153 struct ntb_device *ndev = db_cb->ndev;
154 unsigned long mask;
155
156 mask = readw(ndev->reg_ofs.ldb_mask);
157 clear_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
158 writew(mask, ndev->reg_ofs.ldb_mask);
159 }
160}
161
Jon Masonfce8a7b2012-11-16 19:27:12 -0700162/**
163 * ntb_register_db_callback() - register a callback for doorbell interrupt
164 * @ndev: pointer to ntb_device instance
165 * @idx: doorbell index to register callback, zero based
Jon Masonf9a2cf82013-07-29 16:46:43 -0700166 * @data: pointer to be returned to caller with every callback
Jon Masonfce8a7b2012-11-16 19:27:12 -0700167 * @func: callback function to register
168 *
169 * This function registers a callback function for the doorbell interrupt
170 * on the primary side. The function will unmask the doorbell as well to
171 * allow interrupt.
172 *
173 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
174 */
175int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
Jon Masone8aeb602013-04-18 17:59:44 -0700176 void *data, int (*func)(void *data, int db_num))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700177{
178 unsigned long mask;
179
180 if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
181 dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
182 return -EINVAL;
183 }
184
185 ndev->db_cb[idx].callback = func;
186 ndev->db_cb[idx].data = data;
Jon Masone8aeb602013-04-18 17:59:44 -0700187 ndev->db_cb[idx].ndev = ndev;
188
189 tasklet_init(&ndev->db_cb[idx].irq_work, ntb_irq_work,
190 (unsigned long) &ndev->db_cb[idx]);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700191
192 /* unmask interrupt */
Jon Mason49793882013-07-15 15:53:54 -0700193 mask = readw(ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700194 clear_bit(idx * ndev->bits_per_vector, &mask);
Jon Mason49793882013-07-15 15:53:54 -0700195 writew(mask, ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700196
197 return 0;
198}
199
200/**
201 * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
202 * @ndev: pointer to ntb_device instance
203 * @idx: doorbell index to register callback, zero based
204 *
205 * This function unregisters a callback function for the doorbell interrupt
206 * on the primary side. The function will also mask the said doorbell.
207 */
208void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
209{
210 unsigned long mask;
211
212 if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
213 return;
214
Jon Mason49793882013-07-15 15:53:54 -0700215 mask = readw(ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700216 set_bit(idx * ndev->bits_per_vector, &mask);
Jon Mason49793882013-07-15 15:53:54 -0700217 writew(mask, ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700218
Jon Masone8aeb602013-04-18 17:59:44 -0700219 tasklet_disable(&ndev->db_cb[idx].irq_work);
220
Jon Masonfce8a7b2012-11-16 19:27:12 -0700221 ndev->db_cb[idx].callback = NULL;
222}
223
224/**
225 * ntb_find_transport() - find the transport pointer
226 * @transport: pointer to pci device
227 *
228 * Given the pci device pointer, return the transport pointer passed in when
229 * the transport attached when it was inited.
230 *
231 * RETURNS: pointer to transport.
232 */
233void *ntb_find_transport(struct pci_dev *pdev)
234{
235 struct ntb_device *ndev = pci_get_drvdata(pdev);
236 return ndev->ntb_transport;
237}
238
239/**
240 * ntb_register_transport() - Register NTB transport with NTB HW driver
241 * @transport: transport identifier
242 *
243 * This function allows a transport to reserve the hardware driver for
244 * NTB usage.
245 *
246 * RETURNS: pointer to ntb_device, NULL on error.
247 */
248struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
249{
250 struct ntb_device *ndev = pci_get_drvdata(pdev);
251
252 if (ndev->ntb_transport)
253 return NULL;
254
255 ndev->ntb_transport = transport;
256 return ndev;
257}
258
259/**
260 * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
261 * @ndev - ntb_device of the transport to be freed
262 *
263 * This function unregisters the transport from the HW driver and performs any
264 * necessary cleanups.
265 */
266void ntb_unregister_transport(struct ntb_device *ndev)
267{
268 int i;
269
270 if (!ndev->ntb_transport)
271 return;
272
273 for (i = 0; i < ndev->max_cbs; i++)
274 ntb_unregister_db_callback(ndev, i);
275
276 ntb_unregister_event_callback(ndev);
277 ndev->ntb_transport = NULL;
278}
279
280/**
Jon Masonfce8a7b2012-11-16 19:27:12 -0700281 * ntb_write_local_spad() - write to the secondary scratchpad register
282 * @ndev: pointer to ntb_device instance
283 * @idx: index to the scratchpad register, 0 based
284 * @val: the data value to put into the register
285 *
286 * This function allows writing of a 32bit value to the indexed scratchpad
287 * register. This writes over the data mirrored to the local scratchpad register
288 * by the remote system.
289 *
290 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
291 */
292int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
293{
294 if (idx >= ndev->limits.max_spads)
295 return -EINVAL;
296
297 dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
298 val, idx);
299 writel(val, ndev->reg_ofs.spad_read + idx * 4);
300
301 return 0;
302}
303
304/**
305 * ntb_read_local_spad() - read from the primary scratchpad register
306 * @ndev: pointer to ntb_device instance
307 * @idx: index to scratchpad register, 0 based
308 * @val: pointer to 32bit integer for storing the register value
309 *
310 * This function allows reading of the 32bit scratchpad register on
311 * the primary (internal) side. This allows the local system to read data
312 * written and mirrored to the scratchpad register by the remote system.
313 *
314 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
315 */
316int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
317{
318 if (idx >= ndev->limits.max_spads)
319 return -EINVAL;
320
321 *val = readl(ndev->reg_ofs.spad_write + idx * 4);
322 dev_dbg(&ndev->pdev->dev,
323 "Reading %x from local scratch pad index %d\n", *val, idx);
324
325 return 0;
326}
327
328/**
329 * ntb_write_remote_spad() - write to the secondary scratchpad register
330 * @ndev: pointer to ntb_device instance
331 * @idx: index to the scratchpad register, 0 based
332 * @val: the data value to put into the register
333 *
334 * This function allows writing of a 32bit value to the indexed scratchpad
335 * register. The register resides on the secondary (external) side. This allows
336 * the local system to write data to be mirrored to the remote systems
337 * scratchpad register.
338 *
339 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
340 */
341int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
342{
343 if (idx >= ndev->limits.max_spads)
344 return -EINVAL;
345
346 dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
347 val, idx);
348 writel(val, ndev->reg_ofs.spad_write + idx * 4);
349
350 return 0;
351}
352
353/**
354 * ntb_read_remote_spad() - read from the primary scratchpad register
355 * @ndev: pointer to ntb_device instance
356 * @idx: index to scratchpad register, 0 based
357 * @val: pointer to 32bit integer for storing the register value
358 *
359 * This function allows reading of the 32bit scratchpad register on
360 * the primary (internal) side. This alloows the local system to read the data
361 * it wrote to be mirrored on the remote system.
362 *
363 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
364 */
365int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
366{
367 if (idx >= ndev->limits.max_spads)
368 return -EINVAL;
369
370 *val = readl(ndev->reg_ofs.spad_read + idx * 4);
371 dev_dbg(&ndev->pdev->dev,
372 "Reading %x from remote scratch pad index %d\n", *val, idx);
373
374 return 0;
375}
376
377/**
Jon Mason282a2fe2013-02-12 09:52:50 -0700378 * ntb_get_mw_base() - get addr for the NTB memory window
379 * @ndev: pointer to ntb_device instance
380 * @mw: memory window number
381 *
382 * This function provides the base address of the memory window specified.
383 *
384 * RETURNS: address, or NULL on error.
385 */
386resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw)
387{
388 if (mw >= ntb_max_mw(ndev))
389 return 0;
390
391 return pci_resource_start(ndev->pdev, MW_TO_BAR(mw));
392}
393
394/**
Jon Masonfce8a7b2012-11-16 19:27:12 -0700395 * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
396 * @ndev: pointer to ntb_device instance
397 * @mw: memory window number
398 *
399 * This function provides the base virtual address of the memory window
400 * specified.
401 *
402 * RETURNS: pointer to virtual address, or NULL on error.
403 */
Jon Mason74465642013-01-21 15:28:52 -0700404void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700405{
Jon Mason948d3a62013-04-18 17:07:36 -0700406 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407 return NULL;
408
409 return ndev->mw[mw].vbase;
410}
411
412/**
413 * ntb_get_mw_size() - return size of NTB memory window
414 * @ndev: pointer to ntb_device instance
415 * @mw: memory window number
416 *
417 * This function provides the physical size of the memory window specified
418 *
419 * RETURNS: the size of the memory window or zero on error
420 */
Jon Masonac477af2013-01-21 16:40:39 -0700421u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700422{
Jon Mason948d3a62013-04-18 17:07:36 -0700423 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700424 return 0;
425
426 return ndev->mw[mw].bar_sz;
427}
428
429/**
430 * ntb_set_mw_addr - set the memory window address
431 * @ndev: pointer to ntb_device instance
432 * @mw: memory window number
433 * @addr: base address for data
434 *
435 * This function sets the base physical address of the memory window. This
436 * memory address is where data from the remote system will be transfered into
437 * or out of depending on how the transport is configured.
438 */
439void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
440{
Jon Mason948d3a62013-04-18 17:07:36 -0700441 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700442 return;
443
444 dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
445 MW_TO_BAR(mw));
446
447 ndev->mw[mw].phys_addr = addr;
448
449 switch (MW_TO_BAR(mw)) {
450 case NTB_BAR_23:
Jon Mason49793882013-07-15 15:53:54 -0700451 writeq(addr, ndev->reg_ofs.bar2_xlat);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700452 break;
453 case NTB_BAR_45:
Jon Mason49793882013-07-15 15:53:54 -0700454 writeq(addr, ndev->reg_ofs.bar4_xlat);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700455 break;
456 }
457}
458
459/**
Jon Mason49793882013-07-15 15:53:54 -0700460 * ntb_ring_doorbell() - Set the doorbell on the secondary/external side
Jon Masonfce8a7b2012-11-16 19:27:12 -0700461 * @ndev: pointer to ntb_device instance
462 * @db: doorbell to ring
463 *
464 * This function allows triggering of a doorbell on the secondary/external
465 * side that will initiate an interrupt on the remote host
466 *
467 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
468 */
Jon Mason49793882013-07-15 15:53:54 -0700469void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int db)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700470{
471 dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
472
473 if (ndev->hw_type == BWD_HW)
Jon Mason49793882013-07-15 15:53:54 -0700474 writeq((u64) 1 << db, ndev->reg_ofs.rdb);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700475 else
476 writew(((1 << ndev->bits_per_vector) - 1) <<
Jon Mason49793882013-07-15 15:53:54 -0700477 (db * ndev->bits_per_vector), ndev->reg_ofs.rdb);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700478}
479
Jon Mason113bf1c2012-11-16 18:52:57 -0700480static void bwd_recover_link(struct ntb_device *ndev)
481{
482 u32 status;
483
484 /* Driver resets the NTB ModPhy lanes - magic! */
485 writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
486 writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
487 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
488 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
489
490 /* Driver waits 100ms to allow the NTB ModPhy to settle */
491 msleep(100);
492
493 /* Clear AER Errors, write to clear */
494 status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
495 dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
496 status &= PCI_ERR_COR_REP_ROLL;
497 writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
498
499 /* Clear unexpected electrical idle event in LTSSM, write to clear */
500 status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
501 dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
502 status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
503 writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
504
505 /* Clear DeSkew Buffer error, write to clear */
506 status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
507 dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
508 status |= BWD_DESKEWSTS_DBERR;
509 writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
510
511 status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
512 dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
513 status &= BWD_IBIST_ERR_OFLOW;
514 writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
515
516 /* Releases the NTB state machine to allow the link to retrain */
517 status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
518 dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
519 status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
520 writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
521}
522
Jon Masonfce8a7b2012-11-16 19:27:12 -0700523static void ntb_link_event(struct ntb_device *ndev, int link_state)
524{
525 unsigned int event;
526
527 if (ndev->link_status == link_state)
528 return;
529
530 if (link_state == NTB_LINK_UP) {
531 u16 status;
532
533 dev_info(&ndev->pdev->dev, "Link Up\n");
534 ndev->link_status = NTB_LINK_UP;
535 event = NTB_EVENT_HW_LINK_UP;
536
Jon Masoned6c24e2013-07-15 16:43:54 -0700537 if (ndev->hw_type == BWD_HW ||
538 ndev->conn_type == NTB_CONN_TRANSPARENT)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700539 status = readw(ndev->reg_ofs.lnk_stat);
540 else {
541 int rc = pci_read_config_word(ndev->pdev,
542 SNB_LINK_STATUS_OFFSET,
543 &status);
544 if (rc)
545 return;
546 }
Jon Mason113bf1c2012-11-16 18:52:57 -0700547
548 ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
549 ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700550 dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
Jon Mason113bf1c2012-11-16 18:52:57 -0700551 ndev->link_width, ndev->link_speed);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700552 } else {
553 dev_info(&ndev->pdev->dev, "Link Down\n");
554 ndev->link_status = NTB_LINK_DOWN;
555 event = NTB_EVENT_HW_LINK_DOWN;
Jon Mason113bf1c2012-11-16 18:52:57 -0700556 /* Don't modify link width/speed, we need it in link recovery */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700557 }
558
559 /* notify the upper layer if we have an event change */
560 if (ndev->event_cb)
561 ndev->event_cb(ndev->ntb_transport, event);
562}
563
564static int ntb_link_status(struct ntb_device *ndev)
565{
566 int link_state;
567
568 if (ndev->hw_type == BWD_HW) {
569 u32 ntb_cntl;
570
571 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
572 if (ntb_cntl & BWD_CNTL_LINK_DOWN)
573 link_state = NTB_LINK_DOWN;
574 else
575 link_state = NTB_LINK_UP;
576 } else {
577 u16 status;
578 int rc;
579
580 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
581 &status);
582 if (rc)
583 return rc;
584
585 if (status & NTB_LINK_STATUS_ACTIVE)
586 link_state = NTB_LINK_UP;
587 else
588 link_state = NTB_LINK_DOWN;
589 }
590
591 ntb_link_event(ndev, link_state);
592
593 return 0;
594}
595
Jon Mason113bf1c2012-11-16 18:52:57 -0700596static void bwd_link_recovery(struct work_struct *work)
597{
598 struct ntb_device *ndev = container_of(work, struct ntb_device,
599 lr_timer.work);
600 u32 status32;
601
602 bwd_recover_link(ndev);
603 /* There is a potential race between the 2 NTB devices recovering at the
604 * same time. If the times are the same, the link will not recover and
605 * the driver will be stuck in this loop forever. Add a random interval
606 * to the recovery time to prevent this race.
607 */
608 msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
609
610 status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
611 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
612 goto retry;
613
614 status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
615 if (status32 & BWD_IBIST_ERR_OFLOW)
616 goto retry;
617
618 status32 = readl(ndev->reg_ofs.lnk_cntl);
619 if (!(status32 & BWD_CNTL_LINK_DOWN)) {
620 unsigned char speed, width;
621 u16 status16;
622
623 status16 = readw(ndev->reg_ofs.lnk_stat);
624 width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
625 speed = (status16 & NTB_LINK_SPEED_MASK);
626 if (ndev->link_width != width || ndev->link_speed != speed)
627 goto retry;
628 }
629
630 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
631 return;
632
633retry:
634 schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
635}
636
Jon Masonfce8a7b2012-11-16 19:27:12 -0700637/* BWD doesn't have link status interrupt, poll on that platform */
638static void bwd_link_poll(struct work_struct *work)
639{
640 struct ntb_device *ndev = container_of(work, struct ntb_device,
641 hb_timer.work);
642 unsigned long ts = jiffies;
643
644 /* If we haven't gotten an interrupt in a while, check the BWD link
645 * status bit
646 */
647 if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
648 int rc = ntb_link_status(ndev);
649 if (rc)
650 dev_err(&ndev->pdev->dev,
651 "Error determining link status\n");
Jon Mason113bf1c2012-11-16 18:52:57 -0700652
653 /* Check to see if a link error is the cause of the link down */
654 if (ndev->link_status == NTB_LINK_DOWN) {
655 u32 status32 = readl(ndev->reg_base +
656 BWD_LTSSMSTATEJMP_OFFSET);
657 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
658 schedule_delayed_work(&ndev->lr_timer, 0);
659 return;
660 }
661 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700662 }
663
664 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
665}
666
667static int ntb_xeon_setup(struct ntb_device *ndev)
668{
669 int rc;
670 u8 val;
671
672 ndev->hw_type = SNB_HW;
673
674 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
675 if (rc)
676 return rc;
677
Jon Masonfce8a7b2012-11-16 19:27:12 -0700678 if (val & SNB_PPD_DEV_TYPE)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700679 ndev->dev_type = NTB_DEV_USD;
Jon Masonb6750cf2013-05-31 14:05:53 -0700680 else
681 ndev->dev_type = NTB_DEV_DSD;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700682
Jon Masoned6c24e2013-07-15 16:43:54 -0700683 switch (val & SNB_PPD_CONN_TYPE) {
684 case NTB_CONN_B2B:
685 dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
686 ndev->conn_type = NTB_CONN_B2B;
687 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
688 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
689 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
690 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
691 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
692 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
693
694 /* There is a Xeon hardware errata related to writes to
695 * SDOORBELL or B2BDOORBELL in conjunction with inbound access
696 * to NTB MMIO Space, which may hang the system. To workaround
697 * this use the second memory window to access the interrupt and
698 * scratch pad registers on the remote system.
699 */
700 if (xeon_errata_workaround) {
701 if (!ndev->mw[1].bar_sz)
702 return -EINVAL;
703
704 ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
Jon Masonc529aa32013-09-06 16:51:16 -0700705 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
Jon Masoned6c24e2013-07-15 16:43:54 -0700706 ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
707 SNB_SPAD_OFFSET;
708 ndev->reg_ofs.rdb = ndev->mw[1].vbase +
709 SNB_PDOORBELL_OFFSET;
710
711 /* Set the Limit register to 4k, the minimum size, to
712 * prevent an illegal access
713 */
714 writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
715 SNB_PBAR4LMT_OFFSET);
Jon Mason58b88922013-11-01 15:08:19 -0700716 /* HW errata on the Limit registers. They can only be
717 * written when the base register is 4GB aligned and
718 * < 32bit. This should already be the case based on the
719 * driver defaults, but write the Limit registers first
720 * just in case.
721 */
Jon Masoned6c24e2013-07-15 16:43:54 -0700722 } else {
723 ndev->limits.max_mw = SNB_MAX_MW;
Jon Masonc529aa32013-09-06 16:51:16 -0700724
725 /* HW Errata on bit 14 of b2bdoorbell register. Writes
726 * will not be mirrored to the remote system. Shrink
727 * the number of bits by one, since bit 14 is the last
728 * bit.
729 */
730 ndev->limits.max_db_bits = SNB_MAX_DB_BITS - 1;
Jon Masoned6c24e2013-07-15 16:43:54 -0700731 ndev->reg_ofs.spad_write = ndev->reg_base +
732 SNB_B2B_SPAD_OFFSET;
733 ndev->reg_ofs.rdb = ndev->reg_base +
734 SNB_B2B_DOORBELL_OFFSET;
735
736 /* Disable the Limit register, just incase it is set to
737 * something silly
738 */
739 writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
Jon Mason58b88922013-11-01 15:08:19 -0700740 /* HW errata on the Limit registers. They can only be
741 * written when the base register is 4GB aligned and
742 * < 32bit. This should already be the case based on the
743 * driver defaults, but write the Limit registers first
744 * just in case.
745 */
Jon Masoned6c24e2013-07-15 16:43:54 -0700746 }
747
748 /* The Xeon errata workaround requires setting SBAR Base
749 * addresses to known values, so that the PBAR XLAT can be
750 * pointed at SBAR0 of the remote system.
751 */
752 if (ndev->dev_type == NTB_DEV_USD) {
753 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
754 SNB_PBAR2XLAT_OFFSET);
755 if (xeon_errata_workaround)
756 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
757 SNB_PBAR4XLAT_OFFSET);
758 else {
759 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
760 SNB_PBAR4XLAT_OFFSET);
761 /* B2B_XLAT_OFFSET is a 64bit register, but can
762 * only take 32bit writes
763 */
764 writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
765 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
766 writel(SNB_MBAR01_DSD_ADDR >> 32,
767 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
768 }
769
770 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
771 SNB_SBAR0BASE_OFFSET);
772 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
773 SNB_SBAR2BASE_OFFSET);
774 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
775 SNB_SBAR4BASE_OFFSET);
776 } else {
777 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
778 SNB_PBAR2XLAT_OFFSET);
779 if (xeon_errata_workaround)
780 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
781 SNB_PBAR4XLAT_OFFSET);
782 else {
783 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
784 SNB_PBAR4XLAT_OFFSET);
785 /* B2B_XLAT_OFFSET is a 64bit register, but can
786 * only take 32bit writes
787 */
788 writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
789 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
790 writel(SNB_MBAR01_USD_ADDR >> 32,
791 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
792 }
793 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
794 SNB_SBAR0BASE_OFFSET);
795 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
796 SNB_SBAR2BASE_OFFSET);
797 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
798 SNB_SBAR4BASE_OFFSET);
799 }
800 break;
801 case NTB_CONN_RP:
802 dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
803 ndev->conn_type = NTB_CONN_RP;
804
805 if (xeon_errata_workaround) {
806 dev_err(&ndev->pdev->dev,
807 "NTB-RP disabled due to hardware errata. To disregard this warning and potentially lock-up the system, add the parameter 'xeon_errata_workaround=0'.\n");
808 return -EINVAL;
809 }
810
811 /* Scratch pads need to have exclusive access from the primary
812 * or secondary side. Halve the num spads so that each side can
813 * have an equal amount.
814 */
815 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
Jon Masonc529aa32013-09-06 16:51:16 -0700816 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
Jon Masoned6c24e2013-07-15 16:43:54 -0700817 /* Note: The SDOORBELL is the cause of the errata. You REALLY
818 * don't want to touch it.
819 */
820 ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
821 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
822 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
823 /* Offset the start of the spads to correspond to whether it is
824 * primary or secondary
825 */
826 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
827 ndev->limits.max_spads * 4;
828 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
829 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
830 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
831 ndev->limits.max_mw = SNB_MAX_MW;
832 break;
833 case NTB_CONN_TRANSPARENT:
834 dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
835 ndev->conn_type = NTB_CONN_TRANSPARENT;
836 /* Scratch pads need to have exclusive access from the primary
837 * or secondary side. Halve the num spads so that each side can
838 * have an equal amount.
839 */
840 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
Jon Masonc529aa32013-09-06 16:51:16 -0700841 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
Jon Masoned6c24e2013-07-15 16:43:54 -0700842 ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
843 ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
844 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
845 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
846 /* Offset the start of the spads to correspond to whether it is
847 * primary or secondary
848 */
849 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
850 ndev->limits.max_spads * 4;
851 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
852 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
853
854 ndev->limits.max_mw = SNB_MAX_MW;
855 break;
856 default:
857 /* Most likely caused by the remote NTB-RP device not being
858 * configured
859 */
860 dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", val);
861 return -EINVAL;
862 }
863
Jon Masonfce8a7b2012-11-16 19:27:12 -0700864 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
Jon Masoned6c24e2013-07-15 16:43:54 -0700865 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700866 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
867
Jon Masonfce8a7b2012-11-16 19:27:12 -0700868 ndev->limits.msix_cnt = SNB_MSIX_CNT;
869 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
870
871 return 0;
872}
873
874static int ntb_bwd_setup(struct ntb_device *ndev)
875{
876 int rc;
877 u32 val;
878
879 ndev->hw_type = BWD_HW;
880
881 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
882 if (rc)
883 return rc;
884
885 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
886 case NTB_CONN_B2B:
887 ndev->conn_type = NTB_CONN_B2B;
888 break;
889 case NTB_CONN_RP:
890 default:
Jon Masonb1ef0042013-07-15 15:33:18 -0700891 dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892 return -EINVAL;
893 }
894
895 if (val & BWD_PPD_DEV_TYPE)
896 ndev->dev_type = NTB_DEV_DSD;
897 else
898 ndev->dev_type = NTB_DEV_USD;
899
900 /* Initiate PCI-E link training */
901 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
902 val | BWD_PPD_INIT_LINK);
903 if (rc)
904 return rc;
905
Jon Mason49793882013-07-15 15:53:54 -0700906 ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
907 ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
Jon Masonb1ef0042013-07-15 15:33:18 -0700908 ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
Jon Mason49793882013-07-15 15:53:54 -0700909 ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
910 ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700911 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
912 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
913 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
Jon Masonb1ef0042013-07-15 15:33:18 -0700914 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700915 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
Jon Mason948d3a62013-04-18 17:07:36 -0700916 ndev->limits.max_mw = BWD_MAX_MW;
Jon Masonb1ef0042013-07-15 15:33:18 -0700917 ndev->limits.max_spads = BWD_MAX_SPADS;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700918 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
919 ndev->limits.msix_cnt = BWD_MSIX_CNT;
920 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
921
922 /* Since bwd doesn't have a link interrupt, setup a poll timer */
923 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
Jon Mason113bf1c2012-11-16 18:52:57 -0700924 INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700925 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
926
927 return 0;
928}
929
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800930static int ntb_device_setup(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700931{
932 int rc;
933
934 switch (ndev->pdev->device) {
Jon Masonbe4dac02012-09-28 11:38:48 -0700935 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
936 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
937 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
938 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
939 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
940 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
941 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
942 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700943 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
944 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
Jon Masonbe4dac02012-09-28 11:38:48 -0700945 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
946 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700947 rc = ntb_xeon_setup(ndev);
948 break;
949 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
950 rc = ntb_bwd_setup(ndev);
951 break;
952 default:
953 rc = -ENODEV;
954 }
955
Jon Mason3b12a0d2013-07-15 13:23:47 -0700956 if (rc)
957 return rc;
958
Jon Masonb6750cf2013-05-31 14:05:53 -0700959 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
960 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
961
Jon Masoned6c24e2013-07-15 16:43:54 -0700962 if (ndev->conn_type == NTB_CONN_B2B)
963 /* Enable Bus Master and Memory Space on the secondary side */
964 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
965 ndev->reg_ofs.spci_cmd);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966
Jon Mason3b12a0d2013-07-15 13:23:47 -0700967 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700968}
969
970static void ntb_device_free(struct ntb_device *ndev)
971{
Jon Mason113bf1c2012-11-16 18:52:57 -0700972 if (ndev->hw_type == BWD_HW) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700973 cancel_delayed_work_sync(&ndev->hb_timer);
Jon Mason113bf1c2012-11-16 18:52:57 -0700974 cancel_delayed_work_sync(&ndev->lr_timer);
975 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700976}
977
978static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
979{
980 struct ntb_db_cb *db_cb = data;
981 struct ntb_device *ndev = db_cb->ndev;
Jon Masone8aeb602013-04-18 17:59:44 -0700982 unsigned long mask;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700983
984 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
985 db_cb->db_num);
986
Jon Masone8aeb602013-04-18 17:59:44 -0700987 mask = readw(ndev->reg_ofs.ldb_mask);
988 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
989 writew(mask, ndev->reg_ofs.ldb_mask);
990
991 tasklet_schedule(&db_cb->irq_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700992
993 /* No need to check for the specific HB irq, any interrupt means
994 * we're connected.
995 */
996 ndev->last_ts = jiffies;
997
Jon Mason49793882013-07-15 15:53:54 -0700998 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700999
1000 return IRQ_HANDLED;
1001}
1002
1003static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
1004{
1005 struct ntb_db_cb *db_cb = data;
1006 struct ntb_device *ndev = db_cb->ndev;
Jon Masone8aeb602013-04-18 17:59:44 -07001007 unsigned long mask;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001008
1009 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
1010 db_cb->db_num);
1011
Jon Masone8aeb602013-04-18 17:59:44 -07001012 mask = readw(ndev->reg_ofs.ldb_mask);
1013 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
1014 writew(mask, ndev->reg_ofs.ldb_mask);
1015
1016 tasklet_schedule(&db_cb->irq_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001017
1018 /* On Sandybridge, there are 16 bits in the interrupt register
1019 * but only 4 vectors. So, 5 bits are assigned to the first 3
1020 * vectors, with the 4th having a single bit for link
1021 * interrupts.
1022 */
1023 writew(((1 << ndev->bits_per_vector) - 1) <<
Jon Mason49793882013-07-15 15:53:54 -07001024 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001025
1026 return IRQ_HANDLED;
1027}
1028
1029/* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
1030static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
1031{
1032 struct ntb_device *ndev = dev;
1033 int rc;
1034
1035 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
1036
1037 rc = ntb_link_status(ndev);
1038 if (rc)
1039 dev_err(&ndev->pdev->dev, "Error determining link status\n");
1040
1041 /* bit 15 is always the link bit */
Jon Masonc529aa32013-09-06 16:51:16 -07001042 writew(1 << SNB_LINK_DB, ndev->reg_ofs.ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001043
1044 return IRQ_HANDLED;
1045}
1046
1047static irqreturn_t ntb_interrupt(int irq, void *dev)
1048{
1049 struct ntb_device *ndev = dev;
1050 unsigned int i = 0;
1051
1052 if (ndev->hw_type == BWD_HW) {
Jon Mason49793882013-07-15 15:53:54 -07001053 u64 ldb = readq(ndev->reg_ofs.ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001054
Jon Mason49793882013-07-15 15:53:54 -07001055 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056
Jon Mason49793882013-07-15 15:53:54 -07001057 while (ldb) {
1058 i = __ffs(ldb);
1059 ldb &= ldb - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001060 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
1061 }
1062 } else {
Jon Mason49793882013-07-15 15:53:54 -07001063 u16 ldb = readw(ndev->reg_ofs.ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001064
Jon Mason49793882013-07-15 15:53:54 -07001065 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001066
Jon Mason49793882013-07-15 15:53:54 -07001067 if (ldb & SNB_DB_HW_LINK) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001068 xeon_event_msix_irq(irq, dev);
Jon Mason49793882013-07-15 15:53:54 -07001069 ldb &= ~SNB_DB_HW_LINK;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001070 }
1071
Jon Mason49793882013-07-15 15:53:54 -07001072 while (ldb) {
1073 i = __ffs(ldb);
1074 ldb &= ldb - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001075 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
1076 }
1077 }
1078
1079 return IRQ_HANDLED;
1080}
1081
1082static int ntb_setup_msix(struct ntb_device *ndev)
1083{
1084 struct pci_dev *pdev = ndev->pdev;
1085 struct msix_entry *msix;
1086 int msix_entries;
Yijing Wang73f47ca2013-08-08 21:09:34 +08001087 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001088 u16 val;
1089
Yijing Wang73f47ca2013-08-08 21:09:34 +08001090 if (!pdev->msix_cap) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001091 rc = -EIO;
1092 goto err;
1093 }
1094
Yijing Wang73f47ca2013-08-08 21:09:34 +08001095 rc = pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &val);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001096 if (rc)
1097 goto err;
1098
1099 msix_entries = msix_table_size(val);
1100 if (msix_entries > ndev->limits.msix_cnt) {
1101 rc = -EINVAL;
1102 goto err;
1103 }
1104
1105 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
1106 GFP_KERNEL);
1107 if (!ndev->msix_entries) {
1108 rc = -ENOMEM;
1109 goto err;
1110 }
1111
1112 for (i = 0; i < msix_entries; i++)
1113 ndev->msix_entries[i].entry = i;
1114
1115 rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
1116 if (rc < 0)
1117 goto err1;
1118 if (rc > 0) {
1119 /* On SNB, the link interrupt is always tied to 4th vector. If
1120 * we can't get all 4, then we can't use MSI-X.
1121 */
1122 if (ndev->hw_type != BWD_HW) {
1123 rc = -EIO;
1124 goto err1;
1125 }
1126
1127 dev_warn(&pdev->dev,
1128 "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
1129 rc);
1130 msix_entries = rc;
Alexander Gordeev97390472013-10-02 12:49:09 +02001131
1132 rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
1133 if (rc)
1134 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001135 }
1136
1137 for (i = 0; i < msix_entries; i++) {
1138 msix = &ndev->msix_entries[i];
1139 WARN_ON(!msix->vector);
1140
1141 /* Use the last MSI-X vector for Link status */
1142 if (ndev->hw_type == BWD_HW) {
1143 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
1144 "ntb-callback-msix", &ndev->db_cb[i]);
1145 if (rc)
1146 goto err2;
1147 } else {
1148 if (i == msix_entries - 1) {
1149 rc = request_irq(msix->vector,
1150 xeon_event_msix_irq, 0,
1151 "ntb-event-msix", ndev);
1152 if (rc)
1153 goto err2;
1154 } else {
1155 rc = request_irq(msix->vector,
1156 xeon_callback_msix_irq, 0,
1157 "ntb-callback-msix",
1158 &ndev->db_cb[i]);
1159 if (rc)
1160 goto err2;
1161 }
1162 }
1163 }
1164
1165 ndev->num_msix = msix_entries;
1166 if (ndev->hw_type == BWD_HW)
1167 ndev->max_cbs = msix_entries;
1168 else
1169 ndev->max_cbs = msix_entries - 1;
1170
1171 return 0;
1172
1173err2:
1174 while (--i >= 0) {
1175 msix = &ndev->msix_entries[i];
1176 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
1177 free_irq(msix->vector, ndev);
1178 else
1179 free_irq(msix->vector, &ndev->db_cb[i]);
1180 }
1181 pci_disable_msix(pdev);
1182err1:
1183 kfree(ndev->msix_entries);
1184 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
1185err:
1186 ndev->num_msix = 0;
1187 return rc;
1188}
1189
1190static int ntb_setup_msi(struct ntb_device *ndev)
1191{
1192 struct pci_dev *pdev = ndev->pdev;
1193 int rc;
1194
1195 rc = pci_enable_msi(pdev);
1196 if (rc)
1197 return rc;
1198
1199 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
1200 if (rc) {
1201 pci_disable_msi(pdev);
1202 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
1203 return rc;
1204 }
1205
1206 return 0;
1207}
1208
1209static int ntb_setup_intx(struct ntb_device *ndev)
1210{
1211 struct pci_dev *pdev = ndev->pdev;
1212 int rc;
1213
1214 pci_msi_off(pdev);
1215
1216 /* Verify intx is enabled */
1217 pci_intx(pdev, 1);
1218
1219 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
1220 ndev);
1221 if (rc)
1222 return rc;
1223
1224 return 0;
1225}
1226
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001227static int ntb_setup_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001228{
1229 int rc;
1230
1231 /* On BWD, disable all interrupts. On SNB, disable all but Link
1232 * Interrupt. The rest will be unmasked as callbacks are registered.
1233 */
1234 if (ndev->hw_type == BWD_HW)
Jon Mason49793882013-07-15 15:53:54 -07001235 writeq(~0, ndev->reg_ofs.ldb_mask);
Jon Masonc529aa32013-09-06 16:51:16 -07001236 else {
1237 u16 var = 1 << SNB_LINK_DB;
1238 writew(~var, ndev->reg_ofs.ldb_mask);
1239 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001240
1241 rc = ntb_setup_msix(ndev);
1242 if (!rc)
1243 goto done;
1244
1245 ndev->bits_per_vector = 1;
1246 ndev->max_cbs = ndev->limits.max_db_bits;
1247
1248 rc = ntb_setup_msi(ndev);
1249 if (!rc)
1250 goto done;
1251
1252 rc = ntb_setup_intx(ndev);
1253 if (rc) {
1254 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
1255 return rc;
1256 }
1257
1258done:
1259 return 0;
1260}
1261
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001262static void ntb_free_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001263{
1264 struct pci_dev *pdev = ndev->pdev;
1265
1266 /* mask interrupts */
1267 if (ndev->hw_type == BWD_HW)
Jon Mason49793882013-07-15 15:53:54 -07001268 writeq(~0, ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001269 else
Jon Mason49793882013-07-15 15:53:54 -07001270 writew(~0, ndev->reg_ofs.ldb_mask);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001271
1272 if (ndev->num_msix) {
1273 struct msix_entry *msix;
1274 u32 i;
1275
1276 for (i = 0; i < ndev->num_msix; i++) {
1277 msix = &ndev->msix_entries[i];
1278 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
1279 free_irq(msix->vector, ndev);
1280 else
1281 free_irq(msix->vector, &ndev->db_cb[i]);
1282 }
1283 pci_disable_msix(pdev);
1284 } else {
1285 free_irq(pdev->irq, ndev);
1286
1287 if (pci_dev_msi_enabled(pdev))
1288 pci_disable_msi(pdev);
1289 }
1290}
1291
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001292static int ntb_create_callbacks(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001293{
1294 int i;
1295
Jon Masonf9a2cf82013-07-29 16:46:43 -07001296 /* Chicken-egg issue. We won't know how many callbacks are necessary
Jon Masonfce8a7b2012-11-16 19:27:12 -07001297 * until we see how many MSI-X vectors we get, but these pointers need
Jon Masonf9a2cf82013-07-29 16:46:43 -07001298 * to be passed into the MSI-X register function. So, we allocate the
Jon Masonfce8a7b2012-11-16 19:27:12 -07001299 * max, knowing that they might not all be used, to work around this.
1300 */
1301 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
1302 sizeof(struct ntb_db_cb),
1303 GFP_KERNEL);
1304 if (!ndev->db_cb)
1305 return -ENOMEM;
1306
1307 for (i = 0; i < ndev->limits.max_db_bits; i++) {
1308 ndev->db_cb[i].db_num = i;
1309 ndev->db_cb[i].ndev = ndev;
1310 }
1311
1312 return 0;
1313}
1314
1315static void ntb_free_callbacks(struct ntb_device *ndev)
1316{
1317 int i;
1318
1319 for (i = 0; i < ndev->limits.max_db_bits; i++)
1320 ntb_unregister_db_callback(ndev, i);
1321
1322 kfree(ndev->db_cb);
1323}
1324
Jon Mason1517a3f2013-07-30 15:58:49 -07001325static void ntb_setup_debugfs(struct ntb_device *ndev)
1326{
1327 if (!debugfs_initialized())
1328 return;
1329
1330 if (!debugfs_dir)
1331 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1332
1333 ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
1334 debugfs_dir);
1335}
1336
1337static void ntb_free_debugfs(struct ntb_device *ndev)
1338{
1339 debugfs_remove_recursive(ndev->debugfs_dir);
1340
1341 if (debugfs_dir && simple_empty(debugfs_dir)) {
1342 debugfs_remove_recursive(debugfs_dir);
1343 debugfs_dir = NULL;
1344 }
1345}
1346
Jon Mason9fec60c2013-09-13 17:05:23 -07001347static void ntb_hw_link_up(struct ntb_device *ndev)
1348{
1349 if (ndev->conn_type == NTB_CONN_TRANSPARENT)
1350 ntb_link_event(ndev, NTB_LINK_UP);
Jon Mason78958432013-10-03 17:24:03 -07001351 else {
1352 u32 ntb_cntl;
1353
Jon Mason9fec60c2013-09-13 17:05:23 -07001354 /* Let's bring the NTB link up */
Jon Mason78958432013-10-03 17:24:03 -07001355 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1356 ntb_cntl &= ~(NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK);
1357 ntb_cntl |= NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP;
1358 ntb_cntl |= NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP;
1359 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1360 }
Jon Mason9fec60c2013-09-13 17:05:23 -07001361}
1362
1363static void ntb_hw_link_down(struct ntb_device *ndev)
1364{
1365 u32 ntb_cntl;
1366
1367 if (ndev->conn_type == NTB_CONN_TRANSPARENT) {
1368 ntb_link_event(ndev, NTB_LINK_DOWN);
1369 return;
1370 }
1371
1372 /* Bring NTB link down */
1373 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
Jon Mason78958432013-10-03 17:24:03 -07001374 ntb_cntl &= ~(NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP);
1375 ntb_cntl &= ~(NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP);
1376 ntb_cntl |= NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK;
Jon Mason9fec60c2013-09-13 17:05:23 -07001377 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1378}
1379
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001380static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001381{
1382 struct ntb_device *ndev;
1383 int rc, i;
1384
1385 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1386 if (!ndev)
1387 return -ENOMEM;
1388
1389 ndev->pdev = pdev;
1390 ndev->link_status = NTB_LINK_DOWN;
1391 pci_set_drvdata(pdev, ndev);
Jon Mason1517a3f2013-07-30 15:58:49 -07001392 ntb_setup_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001393
1394 rc = pci_enable_device(pdev);
1395 if (rc)
1396 goto err;
1397
1398 pci_set_master(ndev->pdev);
1399
1400 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1401 if (rc)
1402 goto err1;
1403
1404 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1405 if (!ndev->reg_base) {
1406 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1407 rc = -EIO;
1408 goto err2;
1409 }
1410
Jon Mason948d3a62013-04-18 17:07:36 -07001411 for (i = 0; i < NTB_MAX_NUM_MW; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001412 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1413 ndev->mw[i].vbase =
1414 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1415 ndev->mw[i].bar_sz);
Jon Mason113fc502013-01-30 11:40:52 -07001416 dev_info(&pdev->dev, "MW %d size %llu\n", i,
Jon Masonac477af2013-01-21 16:40:39 -07001417 (unsigned long long) ndev->mw[i].bar_sz);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001418 if (!ndev->mw[i].vbase) {
1419 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1420 MW_TO_BAR(i));
1421 rc = -EIO;
1422 goto err3;
1423 }
1424 }
1425
1426 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1427 if (rc) {
1428 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1429 if (rc)
1430 goto err3;
1431
1432 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1433 }
1434
1435 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1436 if (rc) {
1437 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1438 if (rc)
1439 goto err3;
1440
1441 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1442 }
1443
1444 rc = ntb_device_setup(ndev);
1445 if (rc)
1446 goto err3;
1447
1448 rc = ntb_create_callbacks(ndev);
1449 if (rc)
1450 goto err4;
1451
1452 rc = ntb_setup_interrupts(ndev);
1453 if (rc)
1454 goto err5;
1455
1456 /* The scratchpad registers keep the values between rmmod/insmod,
1457 * blast them now
1458 */
1459 for (i = 0; i < ndev->limits.max_spads; i++) {
1460 ntb_write_local_spad(ndev, i, 0);
1461 ntb_write_remote_spad(ndev, i, 0);
1462 }
1463
1464 rc = ntb_transport_init(pdev);
1465 if (rc)
1466 goto err6;
1467
Jon Mason9fec60c2013-09-13 17:05:23 -07001468 ntb_hw_link_up(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001469
1470 return 0;
1471
1472err6:
1473 ntb_free_interrupts(ndev);
1474err5:
1475 ntb_free_callbacks(ndev);
1476err4:
1477 ntb_device_free(ndev);
1478err3:
1479 for (i--; i >= 0; i--)
1480 iounmap(ndev->mw[i].vbase);
1481 iounmap(ndev->reg_base);
1482err2:
1483 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1484err1:
1485 pci_disable_device(pdev);
1486err:
Jon Mason1517a3f2013-07-30 15:58:49 -07001487 ntb_free_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001488 kfree(ndev);
1489
1490 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1491 return rc;
1492}
1493
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001494static void ntb_pci_remove(struct pci_dev *pdev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001495{
1496 struct ntb_device *ndev = pci_get_drvdata(pdev);
1497 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001498
Jon Mason9fec60c2013-09-13 17:05:23 -07001499 ntb_hw_link_down(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001500
1501 ntb_transport_free(ndev->ntb_transport);
1502
1503 ntb_free_interrupts(ndev);
1504 ntb_free_callbacks(ndev);
1505 ntb_device_free(ndev);
1506
Jon Mason948d3a62013-04-18 17:07:36 -07001507 for (i = 0; i < NTB_MAX_NUM_MW; i++)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001508 iounmap(ndev->mw[i].vbase);
1509
1510 iounmap(ndev->reg_base);
1511 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1512 pci_disable_device(pdev);
Jon Mason1517a3f2013-07-30 15:58:49 -07001513 ntb_free_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001514 kfree(ndev);
1515}
1516
1517static struct pci_driver ntb_pci_driver = {
1518 .name = KBUILD_MODNAME,
1519 .id_table = ntb_pci_tbl,
1520 .probe = ntb_pci_probe,
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001521 .remove = ntb_pci_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001522};
1523module_pci_driver(ntb_pci_driver);