blob: 784446e1de054b59f91303c8d44805147d2ca780 [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 Mason50228c52013-01-19 02:02:29 -070060#define NTB_VER "0.25"
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 {
72 NTB_CONN_CLASSIC = 0,
73 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
144/**
145 * ntb_register_db_callback() - register a callback for doorbell interrupt
146 * @ndev: pointer to ntb_device instance
147 * @idx: doorbell index to register callback, zero based
148 * @func: callback function to register
149 *
150 * This function registers a callback function for the doorbell interrupt
151 * on the primary side. The function will unmask the doorbell as well to
152 * allow interrupt.
153 *
154 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
155 */
156int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
157 void *data, void (*func)(void *data, int db_num))
158{
159 unsigned long mask;
160
161 if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
162 dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
163 return -EINVAL;
164 }
165
166 ndev->db_cb[idx].callback = func;
167 ndev->db_cb[idx].data = data;
168
169 /* unmask interrupt */
170 mask = readw(ndev->reg_ofs.pdb_mask);
171 clear_bit(idx * ndev->bits_per_vector, &mask);
172 writew(mask, ndev->reg_ofs.pdb_mask);
173
174 return 0;
175}
176
177/**
178 * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
179 * @ndev: pointer to ntb_device instance
180 * @idx: doorbell index to register callback, zero based
181 *
182 * This function unregisters a callback function for the doorbell interrupt
183 * on the primary side. The function will also mask the said doorbell.
184 */
185void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
186{
187 unsigned long mask;
188
189 if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
190 return;
191
192 mask = readw(ndev->reg_ofs.pdb_mask);
193 set_bit(idx * ndev->bits_per_vector, &mask);
194 writew(mask, ndev->reg_ofs.pdb_mask);
195
196 ndev->db_cb[idx].callback = NULL;
197}
198
199/**
200 * ntb_find_transport() - find the transport pointer
201 * @transport: pointer to pci device
202 *
203 * Given the pci device pointer, return the transport pointer passed in when
204 * the transport attached when it was inited.
205 *
206 * RETURNS: pointer to transport.
207 */
208void *ntb_find_transport(struct pci_dev *pdev)
209{
210 struct ntb_device *ndev = pci_get_drvdata(pdev);
211 return ndev->ntb_transport;
212}
213
214/**
215 * ntb_register_transport() - Register NTB transport with NTB HW driver
216 * @transport: transport identifier
217 *
218 * This function allows a transport to reserve the hardware driver for
219 * NTB usage.
220 *
221 * RETURNS: pointer to ntb_device, NULL on error.
222 */
223struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
224{
225 struct ntb_device *ndev = pci_get_drvdata(pdev);
226
227 if (ndev->ntb_transport)
228 return NULL;
229
230 ndev->ntb_transport = transport;
231 return ndev;
232}
233
234/**
235 * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
236 * @ndev - ntb_device of the transport to be freed
237 *
238 * This function unregisters the transport from the HW driver and performs any
239 * necessary cleanups.
240 */
241void ntb_unregister_transport(struct ntb_device *ndev)
242{
243 int i;
244
245 if (!ndev->ntb_transport)
246 return;
247
248 for (i = 0; i < ndev->max_cbs; i++)
249 ntb_unregister_db_callback(ndev, i);
250
251 ntb_unregister_event_callback(ndev);
252 ndev->ntb_transport = NULL;
253}
254
255/**
Jon Masonfce8a7b2012-11-16 19:27:12 -0700256 * ntb_write_local_spad() - write to the secondary scratchpad register
257 * @ndev: pointer to ntb_device instance
258 * @idx: index to the scratchpad register, 0 based
259 * @val: the data value to put into the register
260 *
261 * This function allows writing of a 32bit value to the indexed scratchpad
262 * register. This writes over the data mirrored to the local scratchpad register
263 * by the remote system.
264 *
265 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
266 */
267int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
268{
269 if (idx >= ndev->limits.max_spads)
270 return -EINVAL;
271
272 dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
273 val, idx);
274 writel(val, ndev->reg_ofs.spad_read + idx * 4);
275
276 return 0;
277}
278
279/**
280 * ntb_read_local_spad() - read from the primary scratchpad register
281 * @ndev: pointer to ntb_device instance
282 * @idx: index to scratchpad register, 0 based
283 * @val: pointer to 32bit integer for storing the register value
284 *
285 * This function allows reading of the 32bit scratchpad register on
286 * the primary (internal) side. This allows the local system to read data
287 * written and mirrored to the scratchpad register by the remote system.
288 *
289 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
290 */
291int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
292{
293 if (idx >= ndev->limits.max_spads)
294 return -EINVAL;
295
296 *val = readl(ndev->reg_ofs.spad_write + idx * 4);
297 dev_dbg(&ndev->pdev->dev,
298 "Reading %x from local scratch pad index %d\n", *val, idx);
299
300 return 0;
301}
302
303/**
304 * ntb_write_remote_spad() - write to the secondary scratchpad register
305 * @ndev: pointer to ntb_device instance
306 * @idx: index to the scratchpad register, 0 based
307 * @val: the data value to put into the register
308 *
309 * This function allows writing of a 32bit value to the indexed scratchpad
310 * register. The register resides on the secondary (external) side. This allows
311 * the local system to write data to be mirrored to the remote systems
312 * scratchpad register.
313 *
314 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
315 */
316int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
317{
318 if (idx >= ndev->limits.max_spads)
319 return -EINVAL;
320
321 dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
322 val, idx);
323 writel(val, ndev->reg_ofs.spad_write + idx * 4);
324
325 return 0;
326}
327
328/**
329 * ntb_read_remote_spad() - read from the primary scratchpad register
330 * @ndev: pointer to ntb_device instance
331 * @idx: index to scratchpad register, 0 based
332 * @val: pointer to 32bit integer for storing the register value
333 *
334 * This function allows reading of the 32bit scratchpad register on
335 * the primary (internal) side. This alloows the local system to read the data
336 * it wrote to be mirrored on the remote system.
337 *
338 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
339 */
340int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
341{
342 if (idx >= ndev->limits.max_spads)
343 return -EINVAL;
344
345 *val = readl(ndev->reg_ofs.spad_read + idx * 4);
346 dev_dbg(&ndev->pdev->dev,
347 "Reading %x from remote scratch pad index %d\n", *val, idx);
348
349 return 0;
350}
351
352/**
353 * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
354 * @ndev: pointer to ntb_device instance
355 * @mw: memory window number
356 *
357 * This function provides the base virtual address of the memory window
358 * specified.
359 *
360 * RETURNS: pointer to virtual address, or NULL on error.
361 */
Jon Mason74465642013-01-21 15:28:52 -0700362void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700363{
Jon Mason948d3a62013-04-18 17:07:36 -0700364 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700365 return NULL;
366
367 return ndev->mw[mw].vbase;
368}
369
370/**
371 * ntb_get_mw_size() - return size of NTB memory window
372 * @ndev: pointer to ntb_device instance
373 * @mw: memory window number
374 *
375 * This function provides the physical size of the memory window specified
376 *
377 * RETURNS: the size of the memory window or zero on error
378 */
379resource_size_t ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
380{
Jon Mason948d3a62013-04-18 17:07:36 -0700381 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700382 return 0;
383
384 return ndev->mw[mw].bar_sz;
385}
386
387/**
388 * ntb_set_mw_addr - set the memory window address
389 * @ndev: pointer to ntb_device instance
390 * @mw: memory window number
391 * @addr: base address for data
392 *
393 * This function sets the base physical address of the memory window. This
394 * memory address is where data from the remote system will be transfered into
395 * or out of depending on how the transport is configured.
396 */
397void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
398{
Jon Mason948d3a62013-04-18 17:07:36 -0700399 if (mw >= ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700400 return;
401
402 dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
403 MW_TO_BAR(mw));
404
405 ndev->mw[mw].phys_addr = addr;
406
407 switch (MW_TO_BAR(mw)) {
408 case NTB_BAR_23:
409 writeq(addr, ndev->reg_ofs.sbar2_xlat);
410 break;
411 case NTB_BAR_45:
412 writeq(addr, ndev->reg_ofs.sbar4_xlat);
413 break;
414 }
415}
416
417/**
418 * ntb_ring_sdb() - Set the doorbell on the secondary/external side
419 * @ndev: pointer to ntb_device instance
420 * @db: doorbell to ring
421 *
422 * This function allows triggering of a doorbell on the secondary/external
423 * side that will initiate an interrupt on the remote host
424 *
425 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
426 */
427void ntb_ring_sdb(struct ntb_device *ndev, unsigned int db)
428{
429 dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
430
431 if (ndev->hw_type == BWD_HW)
432 writeq((u64) 1 << db, ndev->reg_ofs.sdb);
433 else
434 writew(((1 << ndev->bits_per_vector) - 1) <<
435 (db * ndev->bits_per_vector), ndev->reg_ofs.sdb);
436}
437
Jon Mason113bf1c2012-11-16 18:52:57 -0700438static void bwd_recover_link(struct ntb_device *ndev)
439{
440 u32 status;
441
442 /* Driver resets the NTB ModPhy lanes - magic! */
443 writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
444 writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
445 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
446 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
447
448 /* Driver waits 100ms to allow the NTB ModPhy to settle */
449 msleep(100);
450
451 /* Clear AER Errors, write to clear */
452 status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
453 dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
454 status &= PCI_ERR_COR_REP_ROLL;
455 writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
456
457 /* Clear unexpected electrical idle event in LTSSM, write to clear */
458 status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
459 dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
460 status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
461 writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
462
463 /* Clear DeSkew Buffer error, write to clear */
464 status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
465 dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
466 status |= BWD_DESKEWSTS_DBERR;
467 writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
468
469 status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
470 dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
471 status &= BWD_IBIST_ERR_OFLOW;
472 writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
473
474 /* Releases the NTB state machine to allow the link to retrain */
475 status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
476 dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
477 status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
478 writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
479}
480
Jon Masonfce8a7b2012-11-16 19:27:12 -0700481static void ntb_link_event(struct ntb_device *ndev, int link_state)
482{
483 unsigned int event;
484
485 if (ndev->link_status == link_state)
486 return;
487
488 if (link_state == NTB_LINK_UP) {
489 u16 status;
490
491 dev_info(&ndev->pdev->dev, "Link Up\n");
492 ndev->link_status = NTB_LINK_UP;
493 event = NTB_EVENT_HW_LINK_UP;
494
495 if (ndev->hw_type == BWD_HW)
496 status = readw(ndev->reg_ofs.lnk_stat);
497 else {
498 int rc = pci_read_config_word(ndev->pdev,
499 SNB_LINK_STATUS_OFFSET,
500 &status);
501 if (rc)
502 return;
503 }
Jon Mason113bf1c2012-11-16 18:52:57 -0700504
505 ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
506 ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700507 dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
Jon Mason113bf1c2012-11-16 18:52:57 -0700508 ndev->link_width, ndev->link_speed);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700509 } else {
510 dev_info(&ndev->pdev->dev, "Link Down\n");
511 ndev->link_status = NTB_LINK_DOWN;
512 event = NTB_EVENT_HW_LINK_DOWN;
Jon Mason113bf1c2012-11-16 18:52:57 -0700513 /* Don't modify link width/speed, we need it in link recovery */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700514 }
515
516 /* notify the upper layer if we have an event change */
517 if (ndev->event_cb)
518 ndev->event_cb(ndev->ntb_transport, event);
519}
520
521static int ntb_link_status(struct ntb_device *ndev)
522{
523 int link_state;
524
525 if (ndev->hw_type == BWD_HW) {
526 u32 ntb_cntl;
527
528 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
529 if (ntb_cntl & BWD_CNTL_LINK_DOWN)
530 link_state = NTB_LINK_DOWN;
531 else
532 link_state = NTB_LINK_UP;
533 } else {
534 u16 status;
535 int rc;
536
537 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
538 &status);
539 if (rc)
540 return rc;
541
542 if (status & NTB_LINK_STATUS_ACTIVE)
543 link_state = NTB_LINK_UP;
544 else
545 link_state = NTB_LINK_DOWN;
546 }
547
548 ntb_link_event(ndev, link_state);
549
550 return 0;
551}
552
Jon Mason113bf1c2012-11-16 18:52:57 -0700553static void bwd_link_recovery(struct work_struct *work)
554{
555 struct ntb_device *ndev = container_of(work, struct ntb_device,
556 lr_timer.work);
557 u32 status32;
558
559 bwd_recover_link(ndev);
560 /* There is a potential race between the 2 NTB devices recovering at the
561 * same time. If the times are the same, the link will not recover and
562 * the driver will be stuck in this loop forever. Add a random interval
563 * to the recovery time to prevent this race.
564 */
565 msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
566
567 status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
568 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
569 goto retry;
570
571 status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
572 if (status32 & BWD_IBIST_ERR_OFLOW)
573 goto retry;
574
575 status32 = readl(ndev->reg_ofs.lnk_cntl);
576 if (!(status32 & BWD_CNTL_LINK_DOWN)) {
577 unsigned char speed, width;
578 u16 status16;
579
580 status16 = readw(ndev->reg_ofs.lnk_stat);
581 width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
582 speed = (status16 & NTB_LINK_SPEED_MASK);
583 if (ndev->link_width != width || ndev->link_speed != speed)
584 goto retry;
585 }
586
587 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
588 return;
589
590retry:
591 schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
592}
593
Jon Masonfce8a7b2012-11-16 19:27:12 -0700594/* BWD doesn't have link status interrupt, poll on that platform */
595static void bwd_link_poll(struct work_struct *work)
596{
597 struct ntb_device *ndev = container_of(work, struct ntb_device,
598 hb_timer.work);
599 unsigned long ts = jiffies;
600
601 /* If we haven't gotten an interrupt in a while, check the BWD link
602 * status bit
603 */
604 if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
605 int rc = ntb_link_status(ndev);
606 if (rc)
607 dev_err(&ndev->pdev->dev,
608 "Error determining link status\n");
Jon Mason113bf1c2012-11-16 18:52:57 -0700609
610 /* Check to see if a link error is the cause of the link down */
611 if (ndev->link_status == NTB_LINK_DOWN) {
612 u32 status32 = readl(ndev->reg_base +
613 BWD_LTSSMSTATEJMP_OFFSET);
614 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
615 schedule_delayed_work(&ndev->lr_timer, 0);
616 return;
617 }
618 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700619 }
620
621 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
622}
623
624static int ntb_xeon_setup(struct ntb_device *ndev)
625{
626 int rc;
627 u8 val;
628
629 ndev->hw_type = SNB_HW;
630
631 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
632 if (rc)
633 return rc;
634
635 switch (val & SNB_PPD_CONN_TYPE) {
636 case NTB_CONN_B2B:
637 ndev->conn_type = NTB_CONN_B2B;
638 break;
639 case NTB_CONN_CLASSIC:
640 case NTB_CONN_RP:
641 default:
642 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
643 return -EINVAL;
644 }
645
646 if (val & SNB_PPD_DEV_TYPE)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700647 ndev->dev_type = NTB_DEV_USD;
Jon Masonb6750cf2013-05-31 14:05:53 -0700648 else
649 ndev->dev_type = NTB_DEV_DSD;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700650
651 ndev->reg_ofs.pdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
652 ndev->reg_ofs.pdb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
653 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
654 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
655 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
656 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_LINK_STATUS_OFFSET;
657 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
658 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
659
Jon Mason948d3a62013-04-18 17:07:36 -0700660 /* There is a Xeon hardware errata related to writes to
661 * SDOORBELL or B2BDOORBELL in conjunction with inbound access
662 * to NTB MMIO Space, which may hang the system. To workaround
663 * this use the second memory window to access the interrupt and
664 * scratch pad registers on the remote system.
665 */
666 if (xeon_errata_workaround) {
667 if (!ndev->mw[1].bar_sz)
668 return -EINVAL;
669
670 ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
671 ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
672 SNB_SPAD_OFFSET;
673 ndev->reg_ofs.sdb = ndev->mw[1].vbase +
674 SNB_PDOORBELL_OFFSET;
675
676 /* Set the Limit register to 4k, the minimum size, to
677 * prevent an illegal access
678 */
679 writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
680 SNB_PBAR4LMT_OFFSET);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700681 } else {
Jon Mason948d3a62013-04-18 17:07:36 -0700682 ndev->limits.max_mw = SNB_MAX_MW;
683 ndev->reg_ofs.spad_write = ndev->reg_base +
684 SNB_B2B_SPAD_OFFSET;
685 ndev->reg_ofs.sdb = ndev->reg_base +
686 SNB_B2B_DOORBELL_OFFSET;
687
688 /* Disable the Limit register, just incase it is set to
689 * something silly
690 */
691 writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700692 }
693
Jon Mason948d3a62013-04-18 17:07:36 -0700694 /* The Xeon errata workaround requires setting SBAR Base
695 * addresses to known values, so that the PBAR XLAT can be
696 * pointed at SBAR0 of the remote system.
697 */
698 if (ndev->dev_type == NTB_DEV_USD) {
699 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
700 SNB_PBAR2XLAT_OFFSET);
701 if (xeon_errata_workaround)
702 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
703 SNB_PBAR4XLAT_OFFSET);
704 else {
705 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
706 SNB_PBAR4XLAT_OFFSET);
707 /* B2B_XLAT_OFFSET is a 64bit register, but can
708 * only take 32bit writes
709 */
710 writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
711 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
712 writel(SNB_MBAR01_DSD_ADDR >> 32,
713 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
714 }
715
716 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
717 SNB_SBAR0BASE_OFFSET);
718 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
719 SNB_SBAR2BASE_OFFSET);
720 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
721 SNB_SBAR4BASE_OFFSET);
722 } else {
723 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
724 SNB_PBAR2XLAT_OFFSET);
725 if (xeon_errata_workaround)
726 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
727 SNB_PBAR4XLAT_OFFSET);
728 else {
729 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
730 SNB_PBAR4XLAT_OFFSET);
731 /* B2B_XLAT_OFFSET is a 64bit register, but can
732 * only take 32bit writes
733 */
734 writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
735 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
736 writel(SNB_MBAR01_USD_ADDR >> 32,
737 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
738 }
739 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
740 SNB_SBAR0BASE_OFFSET);
741 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
742 SNB_SBAR2BASE_OFFSET);
743 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
744 SNB_SBAR4BASE_OFFSET);
745 }
746
747 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700748 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
749 ndev->limits.msix_cnt = SNB_MSIX_CNT;
750 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
751
752 return 0;
753}
754
755static int ntb_bwd_setup(struct ntb_device *ndev)
756{
757 int rc;
758 u32 val;
759
760 ndev->hw_type = BWD_HW;
761
762 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
763 if (rc)
764 return rc;
765
766 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
767 case NTB_CONN_B2B:
768 ndev->conn_type = NTB_CONN_B2B;
769 break;
770 case NTB_CONN_RP:
771 default:
772 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
773 return -EINVAL;
774 }
775
776 if (val & BWD_PPD_DEV_TYPE)
777 ndev->dev_type = NTB_DEV_DSD;
778 else
779 ndev->dev_type = NTB_DEV_USD;
780
781 /* Initiate PCI-E link training */
782 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
783 val | BWD_PPD_INIT_LINK);
784 if (rc)
785 return rc;
786
787 ndev->reg_ofs.pdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
788 ndev->reg_ofs.pdb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
789 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
790 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
791 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
792 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
793 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
794 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
795
796 if (ndev->conn_type == NTB_CONN_B2B) {
797 ndev->reg_ofs.sdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
798 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
799 ndev->limits.max_spads = BWD_MAX_SPADS;
800 } else {
801 ndev->reg_ofs.sdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
802 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
803 ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
804 }
805
Jon Mason948d3a62013-04-18 17:07:36 -0700806 ndev->limits.max_mw = BWD_MAX_MW;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700807 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
808 ndev->limits.msix_cnt = BWD_MSIX_CNT;
809 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
810
811 /* Since bwd doesn't have a link interrupt, setup a poll timer */
812 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
Jon Mason113bf1c2012-11-16 18:52:57 -0700813 INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700814 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
815
816 return 0;
817}
818
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800819static int ntb_device_setup(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700820{
821 int rc;
822
823 switch (ndev->pdev->device) {
Jon Masonbe4dac02012-09-28 11:38:48 -0700824 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
825 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
826 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
827 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
828 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
829 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
830 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
831 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700832 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
833 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
Jon Masonbe4dac02012-09-28 11:38:48 -0700834 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
835 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700836 rc = ntb_xeon_setup(ndev);
837 break;
838 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
839 rc = ntb_bwd_setup(ndev);
840 break;
841 default:
842 rc = -ENODEV;
843 }
844
Jon Mason3b12a0d2013-07-15 13:23:47 -0700845 if (rc)
846 return rc;
847
Jon Masonb6750cf2013-05-31 14:05:53 -0700848 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
849 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
850
Jon Masonfce8a7b2012-11-16 19:27:12 -0700851 /* Enable Bus Master and Memory Space on the secondary side */
852 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
853
Jon Mason3b12a0d2013-07-15 13:23:47 -0700854 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700855}
856
857static void ntb_device_free(struct ntb_device *ndev)
858{
Jon Mason113bf1c2012-11-16 18:52:57 -0700859 if (ndev->hw_type == BWD_HW) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700860 cancel_delayed_work_sync(&ndev->hb_timer);
Jon Mason113bf1c2012-11-16 18:52:57 -0700861 cancel_delayed_work_sync(&ndev->lr_timer);
862 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700863}
864
865static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
866{
867 struct ntb_db_cb *db_cb = data;
868 struct ntb_device *ndev = db_cb->ndev;
869
870 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
871 db_cb->db_num);
872
873 if (db_cb->callback)
874 db_cb->callback(db_cb->data, db_cb->db_num);
875
876 /* No need to check for the specific HB irq, any interrupt means
877 * we're connected.
878 */
879 ndev->last_ts = jiffies;
880
881 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
882
883 return IRQ_HANDLED;
884}
885
886static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
887{
888 struct ntb_db_cb *db_cb = data;
889 struct ntb_device *ndev = db_cb->ndev;
890
891 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
892 db_cb->db_num);
893
894 if (db_cb->callback)
895 db_cb->callback(db_cb->data, db_cb->db_num);
896
897 /* On Sandybridge, there are 16 bits in the interrupt register
898 * but only 4 vectors. So, 5 bits are assigned to the first 3
899 * vectors, with the 4th having a single bit for link
900 * interrupts.
901 */
902 writew(((1 << ndev->bits_per_vector) - 1) <<
903 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
904
905 return IRQ_HANDLED;
906}
907
908/* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
909static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
910{
911 struct ntb_device *ndev = dev;
912 int rc;
913
914 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
915
916 rc = ntb_link_status(ndev);
917 if (rc)
918 dev_err(&ndev->pdev->dev, "Error determining link status\n");
919
920 /* bit 15 is always the link bit */
921 writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
922
923 return IRQ_HANDLED;
924}
925
926static irqreturn_t ntb_interrupt(int irq, void *dev)
927{
928 struct ntb_device *ndev = dev;
929 unsigned int i = 0;
930
931 if (ndev->hw_type == BWD_HW) {
932 u64 pdb = readq(ndev->reg_ofs.pdb);
933
934 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
935
936 while (pdb) {
937 i = __ffs(pdb);
938 pdb &= pdb - 1;
939 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
940 }
941 } else {
942 u16 pdb = readw(ndev->reg_ofs.pdb);
943
944 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
945 pdb, readw(ndev->reg_ofs.sdb));
946
947 if (pdb & SNB_DB_HW_LINK) {
948 xeon_event_msix_irq(irq, dev);
949 pdb &= ~SNB_DB_HW_LINK;
950 }
951
952 while (pdb) {
953 i = __ffs(pdb);
954 pdb &= pdb - 1;
955 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
956 }
957 }
958
959 return IRQ_HANDLED;
960}
961
962static int ntb_setup_msix(struct ntb_device *ndev)
963{
964 struct pci_dev *pdev = ndev->pdev;
965 struct msix_entry *msix;
966 int msix_entries;
967 int rc, i, pos;
968 u16 val;
969
970 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
971 if (!pos) {
972 rc = -EIO;
973 goto err;
974 }
975
976 rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
977 if (rc)
978 goto err;
979
980 msix_entries = msix_table_size(val);
981 if (msix_entries > ndev->limits.msix_cnt) {
982 rc = -EINVAL;
983 goto err;
984 }
985
986 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
987 GFP_KERNEL);
988 if (!ndev->msix_entries) {
989 rc = -ENOMEM;
990 goto err;
991 }
992
993 for (i = 0; i < msix_entries; i++)
994 ndev->msix_entries[i].entry = i;
995
996 rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
997 if (rc < 0)
998 goto err1;
999 if (rc > 0) {
1000 /* On SNB, the link interrupt is always tied to 4th vector. If
1001 * we can't get all 4, then we can't use MSI-X.
1002 */
1003 if (ndev->hw_type != BWD_HW) {
1004 rc = -EIO;
1005 goto err1;
1006 }
1007
1008 dev_warn(&pdev->dev,
1009 "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
1010 rc);
1011 msix_entries = rc;
1012 }
1013
1014 for (i = 0; i < msix_entries; i++) {
1015 msix = &ndev->msix_entries[i];
1016 WARN_ON(!msix->vector);
1017
1018 /* Use the last MSI-X vector for Link status */
1019 if (ndev->hw_type == BWD_HW) {
1020 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
1021 "ntb-callback-msix", &ndev->db_cb[i]);
1022 if (rc)
1023 goto err2;
1024 } else {
1025 if (i == msix_entries - 1) {
1026 rc = request_irq(msix->vector,
1027 xeon_event_msix_irq, 0,
1028 "ntb-event-msix", ndev);
1029 if (rc)
1030 goto err2;
1031 } else {
1032 rc = request_irq(msix->vector,
1033 xeon_callback_msix_irq, 0,
1034 "ntb-callback-msix",
1035 &ndev->db_cb[i]);
1036 if (rc)
1037 goto err2;
1038 }
1039 }
1040 }
1041
1042 ndev->num_msix = msix_entries;
1043 if (ndev->hw_type == BWD_HW)
1044 ndev->max_cbs = msix_entries;
1045 else
1046 ndev->max_cbs = msix_entries - 1;
1047
1048 return 0;
1049
1050err2:
1051 while (--i >= 0) {
1052 msix = &ndev->msix_entries[i];
1053 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
1054 free_irq(msix->vector, ndev);
1055 else
1056 free_irq(msix->vector, &ndev->db_cb[i]);
1057 }
1058 pci_disable_msix(pdev);
1059err1:
1060 kfree(ndev->msix_entries);
1061 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
1062err:
1063 ndev->num_msix = 0;
1064 return rc;
1065}
1066
1067static int ntb_setup_msi(struct ntb_device *ndev)
1068{
1069 struct pci_dev *pdev = ndev->pdev;
1070 int rc;
1071
1072 rc = pci_enable_msi(pdev);
1073 if (rc)
1074 return rc;
1075
1076 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
1077 if (rc) {
1078 pci_disable_msi(pdev);
1079 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
1080 return rc;
1081 }
1082
1083 return 0;
1084}
1085
1086static int ntb_setup_intx(struct ntb_device *ndev)
1087{
1088 struct pci_dev *pdev = ndev->pdev;
1089 int rc;
1090
1091 pci_msi_off(pdev);
1092
1093 /* Verify intx is enabled */
1094 pci_intx(pdev, 1);
1095
1096 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
1097 ndev);
1098 if (rc)
1099 return rc;
1100
1101 return 0;
1102}
1103
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001104static int ntb_setup_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001105{
1106 int rc;
1107
1108 /* On BWD, disable all interrupts. On SNB, disable all but Link
1109 * Interrupt. The rest will be unmasked as callbacks are registered.
1110 */
1111 if (ndev->hw_type == BWD_HW)
1112 writeq(~0, ndev->reg_ofs.pdb_mask);
1113 else
1114 writew(~(1 << ndev->limits.max_db_bits),
1115 ndev->reg_ofs.pdb_mask);
1116
1117 rc = ntb_setup_msix(ndev);
1118 if (!rc)
1119 goto done;
1120
1121 ndev->bits_per_vector = 1;
1122 ndev->max_cbs = ndev->limits.max_db_bits;
1123
1124 rc = ntb_setup_msi(ndev);
1125 if (!rc)
1126 goto done;
1127
1128 rc = ntb_setup_intx(ndev);
1129 if (rc) {
1130 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
1131 return rc;
1132 }
1133
1134done:
1135 return 0;
1136}
1137
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001138static void ntb_free_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001139{
1140 struct pci_dev *pdev = ndev->pdev;
1141
1142 /* mask interrupts */
1143 if (ndev->hw_type == BWD_HW)
1144 writeq(~0, ndev->reg_ofs.pdb_mask);
1145 else
1146 writew(~0, ndev->reg_ofs.pdb_mask);
1147
1148 if (ndev->num_msix) {
1149 struct msix_entry *msix;
1150 u32 i;
1151
1152 for (i = 0; i < ndev->num_msix; i++) {
1153 msix = &ndev->msix_entries[i];
1154 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
1155 free_irq(msix->vector, ndev);
1156 else
1157 free_irq(msix->vector, &ndev->db_cb[i]);
1158 }
1159 pci_disable_msix(pdev);
1160 } else {
1161 free_irq(pdev->irq, ndev);
1162
1163 if (pci_dev_msi_enabled(pdev))
1164 pci_disable_msi(pdev);
1165 }
1166}
1167
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001168static int ntb_create_callbacks(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001169{
1170 int i;
1171
1172 /* Checken-egg issue. We won't know how many callbacks are necessary
1173 * until we see how many MSI-X vectors we get, but these pointers need
1174 * to be passed into the MSI-X register fucntion. So, we allocate the
1175 * max, knowing that they might not all be used, to work around this.
1176 */
1177 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
1178 sizeof(struct ntb_db_cb),
1179 GFP_KERNEL);
1180 if (!ndev->db_cb)
1181 return -ENOMEM;
1182
1183 for (i = 0; i < ndev->limits.max_db_bits; i++) {
1184 ndev->db_cb[i].db_num = i;
1185 ndev->db_cb[i].ndev = ndev;
1186 }
1187
1188 return 0;
1189}
1190
1191static void ntb_free_callbacks(struct ntb_device *ndev)
1192{
1193 int i;
1194
1195 for (i = 0; i < ndev->limits.max_db_bits; i++)
1196 ntb_unregister_db_callback(ndev, i);
1197
1198 kfree(ndev->db_cb);
1199}
1200
Jon Mason1517a3f2013-07-30 15:58:49 -07001201static void ntb_setup_debugfs(struct ntb_device *ndev)
1202{
1203 if (!debugfs_initialized())
1204 return;
1205
1206 if (!debugfs_dir)
1207 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1208
1209 ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
1210 debugfs_dir);
1211}
1212
1213static void ntb_free_debugfs(struct ntb_device *ndev)
1214{
1215 debugfs_remove_recursive(ndev->debugfs_dir);
1216
1217 if (debugfs_dir && simple_empty(debugfs_dir)) {
1218 debugfs_remove_recursive(debugfs_dir);
1219 debugfs_dir = NULL;
1220 }
1221}
1222
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001223static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001224{
1225 struct ntb_device *ndev;
1226 int rc, i;
1227
1228 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1229 if (!ndev)
1230 return -ENOMEM;
1231
1232 ndev->pdev = pdev;
1233 ndev->link_status = NTB_LINK_DOWN;
1234 pci_set_drvdata(pdev, ndev);
Jon Mason1517a3f2013-07-30 15:58:49 -07001235 ntb_setup_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001236
1237 rc = pci_enable_device(pdev);
1238 if (rc)
1239 goto err;
1240
1241 pci_set_master(ndev->pdev);
1242
1243 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1244 if (rc)
1245 goto err1;
1246
1247 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1248 if (!ndev->reg_base) {
1249 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1250 rc = -EIO;
1251 goto err2;
1252 }
1253
Jon Mason948d3a62013-04-18 17:07:36 -07001254 for (i = 0; i < NTB_MAX_NUM_MW; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001255 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1256 ndev->mw[i].vbase =
1257 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1258 ndev->mw[i].bar_sz);
Jon Mason113fc502013-01-30 11:40:52 -07001259 dev_info(&pdev->dev, "MW %d size %llu\n", i,
1260 pci_resource_len(pdev, MW_TO_BAR(i)));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001261 if (!ndev->mw[i].vbase) {
1262 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1263 MW_TO_BAR(i));
1264 rc = -EIO;
1265 goto err3;
1266 }
1267 }
1268
1269 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1270 if (rc) {
1271 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1272 if (rc)
1273 goto err3;
1274
1275 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1276 }
1277
1278 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1279 if (rc) {
1280 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1281 if (rc)
1282 goto err3;
1283
1284 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1285 }
1286
1287 rc = ntb_device_setup(ndev);
1288 if (rc)
1289 goto err3;
1290
1291 rc = ntb_create_callbacks(ndev);
1292 if (rc)
1293 goto err4;
1294
1295 rc = ntb_setup_interrupts(ndev);
1296 if (rc)
1297 goto err5;
1298
1299 /* The scratchpad registers keep the values between rmmod/insmod,
1300 * blast them now
1301 */
1302 for (i = 0; i < ndev->limits.max_spads; i++) {
1303 ntb_write_local_spad(ndev, i, 0);
1304 ntb_write_remote_spad(ndev, i, 0);
1305 }
1306
1307 rc = ntb_transport_init(pdev);
1308 if (rc)
1309 goto err6;
1310
1311 /* Let's bring the NTB link up */
1312 writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
1313 ndev->reg_ofs.lnk_cntl);
1314
1315 return 0;
1316
1317err6:
1318 ntb_free_interrupts(ndev);
1319err5:
1320 ntb_free_callbacks(ndev);
1321err4:
1322 ntb_device_free(ndev);
1323err3:
1324 for (i--; i >= 0; i--)
1325 iounmap(ndev->mw[i].vbase);
1326 iounmap(ndev->reg_base);
1327err2:
1328 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1329err1:
1330 pci_disable_device(pdev);
1331err:
Jon Mason1517a3f2013-07-30 15:58:49 -07001332 ntb_free_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001333 kfree(ndev);
1334
1335 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1336 return rc;
1337}
1338
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001339static void ntb_pci_remove(struct pci_dev *pdev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001340{
1341 struct ntb_device *ndev = pci_get_drvdata(pdev);
1342 int i;
1343 u32 ntb_cntl;
1344
1345 /* Bring NTB link down */
1346 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1347 ntb_cntl |= NTB_LINK_DISABLE;
1348 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1349
1350 ntb_transport_free(ndev->ntb_transport);
1351
1352 ntb_free_interrupts(ndev);
1353 ntb_free_callbacks(ndev);
1354 ntb_device_free(ndev);
1355
Jon Mason948d3a62013-04-18 17:07:36 -07001356 for (i = 0; i < NTB_MAX_NUM_MW; i++)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001357 iounmap(ndev->mw[i].vbase);
1358
1359 iounmap(ndev->reg_base);
1360 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1361 pci_disable_device(pdev);
Jon Mason1517a3f2013-07-30 15:58:49 -07001362 ntb_free_debugfs(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001363 kfree(ndev);
1364}
1365
1366static struct pci_driver ntb_pci_driver = {
1367 .name = KBUILD_MODNAME,
1368 .id_table = ntb_pci_tbl,
1369 .probe = ntb_pci_probe,
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001370 .remove = ntb_pci_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001371};
1372module_pci_driver(ntb_pci_driver);