blob: 26b808b4d3a0b6e61b3ca8f16acb62bc14a748c4 [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>
49#include <linux/init.h>
50#include <linux/interrupt.h>
51#include <linux/module.h>
52#include <linux/pci.h>
53#include <linux/slab.h>
54#include "ntb_hw.h"
55#include "ntb_regs.h"
56
57#define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
Jon Mason50228c52013-01-19 02:02:29 -070058#define NTB_VER "0.25"
Jon Masonfce8a7b2012-11-16 19:27:12 -070059
60MODULE_DESCRIPTION(NTB_NAME);
61MODULE_VERSION(NTB_VER);
62MODULE_LICENSE("Dual BSD/GPL");
63MODULE_AUTHOR("Intel Corporation");
64
65enum {
66 NTB_CONN_CLASSIC = 0,
67 NTB_CONN_B2B,
68 NTB_CONN_RP,
69};
70
71enum {
72 NTB_DEV_USD = 0,
73 NTB_DEV_DSD,
74};
75
76enum {
77 SNB_HW = 0,
78 BWD_HW,
79};
80
81/* Translate memory window 0,1 to BAR 2,4 */
82#define MW_TO_BAR(mw) (mw * 2 + 2)
83
84static DEFINE_PCI_DEVICE_TABLE(ntb_pci_tbl) = {
85 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
86 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
87 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF)},
88 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_JSF)},
89 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_SNB)},
90 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
91 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB)},
92 {0}
93};
94MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
95
96/**
97 * ntb_register_event_callback() - register event callback
98 * @ndev: pointer to ntb_device instance
99 * @func: callback function to register
100 *
101 * This function registers a callback for any HW driver events such as link
102 * up/down, power management notices and etc.
103 *
104 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
105 */
106int ntb_register_event_callback(struct ntb_device *ndev,
Jon Mason74465642013-01-21 15:28:52 -0700107 void (*func)(void *handle, enum ntb_hw_event event))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700108{
109 if (ndev->event_cb)
110 return -EINVAL;
111
112 ndev->event_cb = func;
113
114 return 0;
115}
116
117/**
118 * ntb_unregister_event_callback() - unregisters the event callback
119 * @ndev: pointer to ntb_device instance
120 *
121 * This function unregisters the existing callback from transport
122 */
123void ntb_unregister_event_callback(struct ntb_device *ndev)
124{
125 ndev->event_cb = NULL;
126}
127
128/**
129 * ntb_register_db_callback() - register a callback for doorbell interrupt
130 * @ndev: pointer to ntb_device instance
131 * @idx: doorbell index to register callback, zero based
132 * @func: callback function to register
133 *
134 * This function registers a callback function for the doorbell interrupt
135 * on the primary side. The function will unmask the doorbell as well to
136 * allow interrupt.
137 *
138 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
139 */
140int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
141 void *data, void (*func)(void *data, int db_num))
142{
143 unsigned long mask;
144
145 if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
146 dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
147 return -EINVAL;
148 }
149
150 ndev->db_cb[idx].callback = func;
151 ndev->db_cb[idx].data = data;
152
153 /* unmask interrupt */
154 mask = readw(ndev->reg_ofs.pdb_mask);
155 clear_bit(idx * ndev->bits_per_vector, &mask);
156 writew(mask, ndev->reg_ofs.pdb_mask);
157
158 return 0;
159}
160
161/**
162 * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
163 * @ndev: pointer to ntb_device instance
164 * @idx: doorbell index to register callback, zero based
165 *
166 * This function unregisters a callback function for the doorbell interrupt
167 * on the primary side. The function will also mask the said doorbell.
168 */
169void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
170{
171 unsigned long mask;
172
173 if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
174 return;
175
176 mask = readw(ndev->reg_ofs.pdb_mask);
177 set_bit(idx * ndev->bits_per_vector, &mask);
178 writew(mask, ndev->reg_ofs.pdb_mask);
179
180 ndev->db_cb[idx].callback = NULL;
181}
182
183/**
184 * ntb_find_transport() - find the transport pointer
185 * @transport: pointer to pci device
186 *
187 * Given the pci device pointer, return the transport pointer passed in when
188 * the transport attached when it was inited.
189 *
190 * RETURNS: pointer to transport.
191 */
192void *ntb_find_transport(struct pci_dev *pdev)
193{
194 struct ntb_device *ndev = pci_get_drvdata(pdev);
195 return ndev->ntb_transport;
196}
197
198/**
199 * ntb_register_transport() - Register NTB transport with NTB HW driver
200 * @transport: transport identifier
201 *
202 * This function allows a transport to reserve the hardware driver for
203 * NTB usage.
204 *
205 * RETURNS: pointer to ntb_device, NULL on error.
206 */
207struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
208{
209 struct ntb_device *ndev = pci_get_drvdata(pdev);
210
211 if (ndev->ntb_transport)
212 return NULL;
213
214 ndev->ntb_transport = transport;
215 return ndev;
216}
217
218/**
219 * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
220 * @ndev - ntb_device of the transport to be freed
221 *
222 * This function unregisters the transport from the HW driver and performs any
223 * necessary cleanups.
224 */
225void ntb_unregister_transport(struct ntb_device *ndev)
226{
227 int i;
228
229 if (!ndev->ntb_transport)
230 return;
231
232 for (i = 0; i < ndev->max_cbs; i++)
233 ntb_unregister_db_callback(ndev, i);
234
235 ntb_unregister_event_callback(ndev);
236 ndev->ntb_transport = NULL;
237}
238
239/**
Jon Masonfce8a7b2012-11-16 19:27:12 -0700240 * ntb_write_local_spad() - write to the secondary scratchpad register
241 * @ndev: pointer to ntb_device instance
242 * @idx: index to the scratchpad register, 0 based
243 * @val: the data value to put into the register
244 *
245 * This function allows writing of a 32bit value to the indexed scratchpad
246 * register. This writes over the data mirrored to the local scratchpad register
247 * by the remote system.
248 *
249 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
250 */
251int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
252{
253 if (idx >= ndev->limits.max_spads)
254 return -EINVAL;
255
256 dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
257 val, idx);
258 writel(val, ndev->reg_ofs.spad_read + idx * 4);
259
260 return 0;
261}
262
263/**
264 * ntb_read_local_spad() - read from the primary scratchpad register
265 * @ndev: pointer to ntb_device instance
266 * @idx: index to scratchpad register, 0 based
267 * @val: pointer to 32bit integer for storing the register value
268 *
269 * This function allows reading of the 32bit scratchpad register on
270 * the primary (internal) side. This allows the local system to read data
271 * written and mirrored to the scratchpad register by the remote system.
272 *
273 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
274 */
275int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
276{
277 if (idx >= ndev->limits.max_spads)
278 return -EINVAL;
279
280 *val = readl(ndev->reg_ofs.spad_write + idx * 4);
281 dev_dbg(&ndev->pdev->dev,
282 "Reading %x from local scratch pad index %d\n", *val, idx);
283
284 return 0;
285}
286
287/**
288 * ntb_write_remote_spad() - write to the secondary scratchpad register
289 * @ndev: pointer to ntb_device instance
290 * @idx: index to the scratchpad register, 0 based
291 * @val: the data value to put into the register
292 *
293 * This function allows writing of a 32bit value to the indexed scratchpad
294 * register. The register resides on the secondary (external) side. This allows
295 * the local system to write data to be mirrored to the remote systems
296 * scratchpad register.
297 *
298 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
299 */
300int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
301{
302 if (idx >= ndev->limits.max_spads)
303 return -EINVAL;
304
305 dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
306 val, idx);
307 writel(val, ndev->reg_ofs.spad_write + idx * 4);
308
309 return 0;
310}
311
312/**
313 * ntb_read_remote_spad() - read from the primary scratchpad register
314 * @ndev: pointer to ntb_device instance
315 * @idx: index to scratchpad register, 0 based
316 * @val: pointer to 32bit integer for storing the register value
317 *
318 * This function allows reading of the 32bit scratchpad register on
319 * the primary (internal) side. This alloows the local system to read the data
320 * it wrote to be mirrored on the remote system.
321 *
322 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
323 */
324int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
325{
326 if (idx >= ndev->limits.max_spads)
327 return -EINVAL;
328
329 *val = readl(ndev->reg_ofs.spad_read + idx * 4);
330 dev_dbg(&ndev->pdev->dev,
331 "Reading %x from remote scratch pad index %d\n", *val, idx);
332
333 return 0;
334}
335
336/**
337 * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
338 * @ndev: pointer to ntb_device instance
339 * @mw: memory window number
340 *
341 * This function provides the base virtual address of the memory window
342 * specified.
343 *
344 * RETURNS: pointer to virtual address, or NULL on error.
345 */
Jon Mason74465642013-01-21 15:28:52 -0700346void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700347{
Dan Carpenterad3e2752013-01-22 10:19:14 +0300348 if (mw >= NTB_NUM_MW)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700349 return NULL;
350
351 return ndev->mw[mw].vbase;
352}
353
354/**
355 * ntb_get_mw_size() - return size of NTB memory window
356 * @ndev: pointer to ntb_device instance
357 * @mw: memory window number
358 *
359 * This function provides the physical size of the memory window specified
360 *
361 * RETURNS: the size of the memory window or zero on error
362 */
363resource_size_t ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
364{
Dan Carpenterad3e2752013-01-22 10:19:14 +0300365 if (mw >= NTB_NUM_MW)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700366 return 0;
367
368 return ndev->mw[mw].bar_sz;
369}
370
371/**
372 * ntb_set_mw_addr - set the memory window address
373 * @ndev: pointer to ntb_device instance
374 * @mw: memory window number
375 * @addr: base address for data
376 *
377 * This function sets the base physical address of the memory window. This
378 * memory address is where data from the remote system will be transfered into
379 * or out of depending on how the transport is configured.
380 */
381void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
382{
Dan Carpenterad3e2752013-01-22 10:19:14 +0300383 if (mw >= NTB_NUM_MW)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700384 return;
385
386 dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
387 MW_TO_BAR(mw));
388
389 ndev->mw[mw].phys_addr = addr;
390
391 switch (MW_TO_BAR(mw)) {
392 case NTB_BAR_23:
393 writeq(addr, ndev->reg_ofs.sbar2_xlat);
394 break;
395 case NTB_BAR_45:
396 writeq(addr, ndev->reg_ofs.sbar4_xlat);
397 break;
398 }
399}
400
401/**
402 * ntb_ring_sdb() - Set the doorbell on the secondary/external side
403 * @ndev: pointer to ntb_device instance
404 * @db: doorbell to ring
405 *
406 * This function allows triggering of a doorbell on the secondary/external
407 * side that will initiate an interrupt on the remote host
408 *
409 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
410 */
411void ntb_ring_sdb(struct ntb_device *ndev, unsigned int db)
412{
413 dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
414
415 if (ndev->hw_type == BWD_HW)
416 writeq((u64) 1 << db, ndev->reg_ofs.sdb);
417 else
418 writew(((1 << ndev->bits_per_vector) - 1) <<
419 (db * ndev->bits_per_vector), ndev->reg_ofs.sdb);
420}
421
422static void ntb_link_event(struct ntb_device *ndev, int link_state)
423{
424 unsigned int event;
425
426 if (ndev->link_status == link_state)
427 return;
428
429 if (link_state == NTB_LINK_UP) {
430 u16 status;
431
432 dev_info(&ndev->pdev->dev, "Link Up\n");
433 ndev->link_status = NTB_LINK_UP;
434 event = NTB_EVENT_HW_LINK_UP;
435
436 if (ndev->hw_type == BWD_HW)
437 status = readw(ndev->reg_ofs.lnk_stat);
438 else {
439 int rc = pci_read_config_word(ndev->pdev,
440 SNB_LINK_STATUS_OFFSET,
441 &status);
442 if (rc)
443 return;
444 }
445 dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
446 (status & NTB_LINK_WIDTH_MASK) >> 4,
447 (status & NTB_LINK_SPEED_MASK));
448 } else {
449 dev_info(&ndev->pdev->dev, "Link Down\n");
450 ndev->link_status = NTB_LINK_DOWN;
451 event = NTB_EVENT_HW_LINK_DOWN;
452 }
453
454 /* notify the upper layer if we have an event change */
455 if (ndev->event_cb)
456 ndev->event_cb(ndev->ntb_transport, event);
457}
458
459static int ntb_link_status(struct ntb_device *ndev)
460{
461 int link_state;
462
463 if (ndev->hw_type == BWD_HW) {
464 u32 ntb_cntl;
465
466 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
467 if (ntb_cntl & BWD_CNTL_LINK_DOWN)
468 link_state = NTB_LINK_DOWN;
469 else
470 link_state = NTB_LINK_UP;
471 } else {
472 u16 status;
473 int rc;
474
475 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
476 &status);
477 if (rc)
478 return rc;
479
480 if (status & NTB_LINK_STATUS_ACTIVE)
481 link_state = NTB_LINK_UP;
482 else
483 link_state = NTB_LINK_DOWN;
484 }
485
486 ntb_link_event(ndev, link_state);
487
488 return 0;
489}
490
491/* BWD doesn't have link status interrupt, poll on that platform */
492static void bwd_link_poll(struct work_struct *work)
493{
494 struct ntb_device *ndev = container_of(work, struct ntb_device,
495 hb_timer.work);
496 unsigned long ts = jiffies;
497
498 /* If we haven't gotten an interrupt in a while, check the BWD link
499 * status bit
500 */
501 if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
502 int rc = ntb_link_status(ndev);
503 if (rc)
504 dev_err(&ndev->pdev->dev,
505 "Error determining link status\n");
506 }
507
508 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
509}
510
511static int ntb_xeon_setup(struct ntb_device *ndev)
512{
513 int rc;
514 u8 val;
515
516 ndev->hw_type = SNB_HW;
517
518 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
519 if (rc)
520 return rc;
521
522 switch (val & SNB_PPD_CONN_TYPE) {
523 case NTB_CONN_B2B:
524 ndev->conn_type = NTB_CONN_B2B;
525 break;
526 case NTB_CONN_CLASSIC:
527 case NTB_CONN_RP:
528 default:
529 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
530 return -EINVAL;
531 }
532
533 if (val & SNB_PPD_DEV_TYPE)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700534 ndev->dev_type = NTB_DEV_USD;
Jon Masonb6750cf2013-05-31 14:05:53 -0700535 else
536 ndev->dev_type = NTB_DEV_DSD;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700537
538 ndev->reg_ofs.pdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
539 ndev->reg_ofs.pdb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
540 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
541 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
542 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
543 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_LINK_STATUS_OFFSET;
544 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
545 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
546
547 if (ndev->conn_type == NTB_CONN_B2B) {
548 ndev->reg_ofs.sdb = ndev->reg_base + SNB_B2B_DOORBELL_OFFSET;
549 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_B2B_SPAD_OFFSET;
Jon Mason87034512013-07-15 15:26:14 -0700550 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700551 } else {
552 ndev->reg_ofs.sdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
553 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
554 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS;
555 }
556
557 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
558 ndev->limits.msix_cnt = SNB_MSIX_CNT;
559 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
560
561 return 0;
562}
563
564static int ntb_bwd_setup(struct ntb_device *ndev)
565{
566 int rc;
567 u32 val;
568
569 ndev->hw_type = BWD_HW;
570
571 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
572 if (rc)
573 return rc;
574
575 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
576 case NTB_CONN_B2B:
577 ndev->conn_type = NTB_CONN_B2B;
578 break;
579 case NTB_CONN_RP:
580 default:
581 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
582 return -EINVAL;
583 }
584
585 if (val & BWD_PPD_DEV_TYPE)
586 ndev->dev_type = NTB_DEV_DSD;
587 else
588 ndev->dev_type = NTB_DEV_USD;
589
590 /* Initiate PCI-E link training */
591 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
592 val | BWD_PPD_INIT_LINK);
593 if (rc)
594 return rc;
595
596 ndev->reg_ofs.pdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
597 ndev->reg_ofs.pdb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
598 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
599 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
600 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
601 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
602 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
603 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
604
605 if (ndev->conn_type == NTB_CONN_B2B) {
606 ndev->reg_ofs.sdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
607 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
608 ndev->limits.max_spads = BWD_MAX_SPADS;
609 } else {
610 ndev->reg_ofs.sdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
611 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
612 ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
613 }
614
615 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
616 ndev->limits.msix_cnt = BWD_MSIX_CNT;
617 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
618
619 /* Since bwd doesn't have a link interrupt, setup a poll timer */
620 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
621 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
622
623 return 0;
624}
625
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800626static int ntb_device_setup(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700627{
628 int rc;
629
630 switch (ndev->pdev->device) {
631 case PCI_DEVICE_ID_INTEL_NTB_2ND_SNB:
632 case PCI_DEVICE_ID_INTEL_NTB_RP_JSF:
633 case PCI_DEVICE_ID_INTEL_NTB_RP_SNB:
634 case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF:
635 case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB:
636 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
637 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
638 rc = ntb_xeon_setup(ndev);
639 break;
640 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
641 rc = ntb_bwd_setup(ndev);
642 break;
643 default:
644 rc = -ENODEV;
645 }
646
Jon Mason3b12a0d2013-07-15 13:23:47 -0700647 if (rc)
648 return rc;
649
Jon Masonb6750cf2013-05-31 14:05:53 -0700650 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
651 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
652
Jon Masonfce8a7b2012-11-16 19:27:12 -0700653 /* Enable Bus Master and Memory Space on the secondary side */
654 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
655
Jon Mason3b12a0d2013-07-15 13:23:47 -0700656 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700657}
658
659static void ntb_device_free(struct ntb_device *ndev)
660{
661 if (ndev->hw_type == BWD_HW)
662 cancel_delayed_work_sync(&ndev->hb_timer);
663}
664
665static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
666{
667 struct ntb_db_cb *db_cb = data;
668 struct ntb_device *ndev = db_cb->ndev;
669
670 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
671 db_cb->db_num);
672
673 if (db_cb->callback)
674 db_cb->callback(db_cb->data, db_cb->db_num);
675
676 /* No need to check for the specific HB irq, any interrupt means
677 * we're connected.
678 */
679 ndev->last_ts = jiffies;
680
681 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
682
683 return IRQ_HANDLED;
684}
685
686static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
687{
688 struct ntb_db_cb *db_cb = data;
689 struct ntb_device *ndev = db_cb->ndev;
690
691 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
692 db_cb->db_num);
693
694 if (db_cb->callback)
695 db_cb->callback(db_cb->data, db_cb->db_num);
696
697 /* On Sandybridge, there are 16 bits in the interrupt register
698 * but only 4 vectors. So, 5 bits are assigned to the first 3
699 * vectors, with the 4th having a single bit for link
700 * interrupts.
701 */
702 writew(((1 << ndev->bits_per_vector) - 1) <<
703 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
704
705 return IRQ_HANDLED;
706}
707
708/* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
709static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
710{
711 struct ntb_device *ndev = dev;
712 int rc;
713
714 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
715
716 rc = ntb_link_status(ndev);
717 if (rc)
718 dev_err(&ndev->pdev->dev, "Error determining link status\n");
719
720 /* bit 15 is always the link bit */
721 writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
722
723 return IRQ_HANDLED;
724}
725
726static irqreturn_t ntb_interrupt(int irq, void *dev)
727{
728 struct ntb_device *ndev = dev;
729 unsigned int i = 0;
730
731 if (ndev->hw_type == BWD_HW) {
732 u64 pdb = readq(ndev->reg_ofs.pdb);
733
734 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
735
736 while (pdb) {
737 i = __ffs(pdb);
738 pdb &= pdb - 1;
739 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
740 }
741 } else {
742 u16 pdb = readw(ndev->reg_ofs.pdb);
743
744 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
745 pdb, readw(ndev->reg_ofs.sdb));
746
747 if (pdb & SNB_DB_HW_LINK) {
748 xeon_event_msix_irq(irq, dev);
749 pdb &= ~SNB_DB_HW_LINK;
750 }
751
752 while (pdb) {
753 i = __ffs(pdb);
754 pdb &= pdb - 1;
755 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
756 }
757 }
758
759 return IRQ_HANDLED;
760}
761
762static int ntb_setup_msix(struct ntb_device *ndev)
763{
764 struct pci_dev *pdev = ndev->pdev;
765 struct msix_entry *msix;
766 int msix_entries;
767 int rc, i, pos;
768 u16 val;
769
770 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
771 if (!pos) {
772 rc = -EIO;
773 goto err;
774 }
775
776 rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
777 if (rc)
778 goto err;
779
780 msix_entries = msix_table_size(val);
781 if (msix_entries > ndev->limits.msix_cnt) {
782 rc = -EINVAL;
783 goto err;
784 }
785
786 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
787 GFP_KERNEL);
788 if (!ndev->msix_entries) {
789 rc = -ENOMEM;
790 goto err;
791 }
792
793 for (i = 0; i < msix_entries; i++)
794 ndev->msix_entries[i].entry = i;
795
796 rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
797 if (rc < 0)
798 goto err1;
799 if (rc > 0) {
800 /* On SNB, the link interrupt is always tied to 4th vector. If
801 * we can't get all 4, then we can't use MSI-X.
802 */
803 if (ndev->hw_type != BWD_HW) {
804 rc = -EIO;
805 goto err1;
806 }
807
808 dev_warn(&pdev->dev,
809 "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
810 rc);
811 msix_entries = rc;
812 }
813
814 for (i = 0; i < msix_entries; i++) {
815 msix = &ndev->msix_entries[i];
816 WARN_ON(!msix->vector);
817
818 /* Use the last MSI-X vector for Link status */
819 if (ndev->hw_type == BWD_HW) {
820 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
821 "ntb-callback-msix", &ndev->db_cb[i]);
822 if (rc)
823 goto err2;
824 } else {
825 if (i == msix_entries - 1) {
826 rc = request_irq(msix->vector,
827 xeon_event_msix_irq, 0,
828 "ntb-event-msix", ndev);
829 if (rc)
830 goto err2;
831 } else {
832 rc = request_irq(msix->vector,
833 xeon_callback_msix_irq, 0,
834 "ntb-callback-msix",
835 &ndev->db_cb[i]);
836 if (rc)
837 goto err2;
838 }
839 }
840 }
841
842 ndev->num_msix = msix_entries;
843 if (ndev->hw_type == BWD_HW)
844 ndev->max_cbs = msix_entries;
845 else
846 ndev->max_cbs = msix_entries - 1;
847
848 return 0;
849
850err2:
851 while (--i >= 0) {
852 msix = &ndev->msix_entries[i];
853 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
854 free_irq(msix->vector, ndev);
855 else
856 free_irq(msix->vector, &ndev->db_cb[i]);
857 }
858 pci_disable_msix(pdev);
859err1:
860 kfree(ndev->msix_entries);
861 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
862err:
863 ndev->num_msix = 0;
864 return rc;
865}
866
867static int ntb_setup_msi(struct ntb_device *ndev)
868{
869 struct pci_dev *pdev = ndev->pdev;
870 int rc;
871
872 rc = pci_enable_msi(pdev);
873 if (rc)
874 return rc;
875
876 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
877 if (rc) {
878 pci_disable_msi(pdev);
879 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
880 return rc;
881 }
882
883 return 0;
884}
885
886static int ntb_setup_intx(struct ntb_device *ndev)
887{
888 struct pci_dev *pdev = ndev->pdev;
889 int rc;
890
891 pci_msi_off(pdev);
892
893 /* Verify intx is enabled */
894 pci_intx(pdev, 1);
895
896 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
897 ndev);
898 if (rc)
899 return rc;
900
901 return 0;
902}
903
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800904static int ntb_setup_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700905{
906 int rc;
907
908 /* On BWD, disable all interrupts. On SNB, disable all but Link
909 * Interrupt. The rest will be unmasked as callbacks are registered.
910 */
911 if (ndev->hw_type == BWD_HW)
912 writeq(~0, ndev->reg_ofs.pdb_mask);
913 else
914 writew(~(1 << ndev->limits.max_db_bits),
915 ndev->reg_ofs.pdb_mask);
916
917 rc = ntb_setup_msix(ndev);
918 if (!rc)
919 goto done;
920
921 ndev->bits_per_vector = 1;
922 ndev->max_cbs = ndev->limits.max_db_bits;
923
924 rc = ntb_setup_msi(ndev);
925 if (!rc)
926 goto done;
927
928 rc = ntb_setup_intx(ndev);
929 if (rc) {
930 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
931 return rc;
932 }
933
934done:
935 return 0;
936}
937
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800938static void ntb_free_interrupts(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700939{
940 struct pci_dev *pdev = ndev->pdev;
941
942 /* mask interrupts */
943 if (ndev->hw_type == BWD_HW)
944 writeq(~0, ndev->reg_ofs.pdb_mask);
945 else
946 writew(~0, ndev->reg_ofs.pdb_mask);
947
948 if (ndev->num_msix) {
949 struct msix_entry *msix;
950 u32 i;
951
952 for (i = 0; i < ndev->num_msix; i++) {
953 msix = &ndev->msix_entries[i];
954 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
955 free_irq(msix->vector, ndev);
956 else
957 free_irq(msix->vector, &ndev->db_cb[i]);
958 }
959 pci_disable_msix(pdev);
960 } else {
961 free_irq(pdev->irq, ndev);
962
963 if (pci_dev_msi_enabled(pdev))
964 pci_disable_msi(pdev);
965 }
966}
967
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800968static int ntb_create_callbacks(struct ntb_device *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700969{
970 int i;
971
972 /* Checken-egg issue. We won't know how many callbacks are necessary
973 * until we see how many MSI-X vectors we get, but these pointers need
974 * to be passed into the MSI-X register fucntion. So, we allocate the
975 * max, knowing that they might not all be used, to work around this.
976 */
977 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
978 sizeof(struct ntb_db_cb),
979 GFP_KERNEL);
980 if (!ndev->db_cb)
981 return -ENOMEM;
982
983 for (i = 0; i < ndev->limits.max_db_bits; i++) {
984 ndev->db_cb[i].db_num = i;
985 ndev->db_cb[i].ndev = ndev;
986 }
987
988 return 0;
989}
990
991static void ntb_free_callbacks(struct ntb_device *ndev)
992{
993 int i;
994
995 for (i = 0; i < ndev->limits.max_db_bits; i++)
996 ntb_unregister_db_callback(ndev, i);
997
998 kfree(ndev->db_cb);
999}
1000
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001001static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001002{
1003 struct ntb_device *ndev;
1004 int rc, i;
1005
1006 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1007 if (!ndev)
1008 return -ENOMEM;
1009
1010 ndev->pdev = pdev;
1011 ndev->link_status = NTB_LINK_DOWN;
1012 pci_set_drvdata(pdev, ndev);
1013
1014 rc = pci_enable_device(pdev);
1015 if (rc)
1016 goto err;
1017
1018 pci_set_master(ndev->pdev);
1019
1020 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1021 if (rc)
1022 goto err1;
1023
1024 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1025 if (!ndev->reg_base) {
1026 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1027 rc = -EIO;
1028 goto err2;
1029 }
1030
1031 for (i = 0; i < NTB_NUM_MW; i++) {
1032 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1033 ndev->mw[i].vbase =
1034 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1035 ndev->mw[i].bar_sz);
Jon Mason113fc502013-01-30 11:40:52 -07001036 dev_info(&pdev->dev, "MW %d size %llu\n", i,
1037 pci_resource_len(pdev, MW_TO_BAR(i)));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038 if (!ndev->mw[i].vbase) {
1039 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1040 MW_TO_BAR(i));
1041 rc = -EIO;
1042 goto err3;
1043 }
1044 }
1045
1046 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1047 if (rc) {
1048 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1049 if (rc)
1050 goto err3;
1051
1052 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1053 }
1054
1055 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1056 if (rc) {
1057 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1058 if (rc)
1059 goto err3;
1060
1061 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1062 }
1063
1064 rc = ntb_device_setup(ndev);
1065 if (rc)
1066 goto err3;
1067
1068 rc = ntb_create_callbacks(ndev);
1069 if (rc)
1070 goto err4;
1071
1072 rc = ntb_setup_interrupts(ndev);
1073 if (rc)
1074 goto err5;
1075
1076 /* The scratchpad registers keep the values between rmmod/insmod,
1077 * blast them now
1078 */
1079 for (i = 0; i < ndev->limits.max_spads; i++) {
1080 ntb_write_local_spad(ndev, i, 0);
1081 ntb_write_remote_spad(ndev, i, 0);
1082 }
1083
1084 rc = ntb_transport_init(pdev);
1085 if (rc)
1086 goto err6;
1087
1088 /* Let's bring the NTB link up */
1089 writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
1090 ndev->reg_ofs.lnk_cntl);
1091
1092 return 0;
1093
1094err6:
1095 ntb_free_interrupts(ndev);
1096err5:
1097 ntb_free_callbacks(ndev);
1098err4:
1099 ntb_device_free(ndev);
1100err3:
1101 for (i--; i >= 0; i--)
1102 iounmap(ndev->mw[i].vbase);
1103 iounmap(ndev->reg_base);
1104err2:
1105 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1106err1:
1107 pci_disable_device(pdev);
1108err:
1109 kfree(ndev);
1110
1111 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1112 return rc;
1113}
1114
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001115static void ntb_pci_remove(struct pci_dev *pdev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001116{
1117 struct ntb_device *ndev = pci_get_drvdata(pdev);
1118 int i;
1119 u32 ntb_cntl;
1120
1121 /* Bring NTB link down */
1122 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1123 ntb_cntl |= NTB_LINK_DISABLE;
1124 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1125
1126 ntb_transport_free(ndev->ntb_transport);
1127
1128 ntb_free_interrupts(ndev);
1129 ntb_free_callbacks(ndev);
1130 ntb_device_free(ndev);
1131
1132 for (i = 0; i < NTB_NUM_MW; i++)
1133 iounmap(ndev->mw[i].vbase);
1134
1135 iounmap(ndev->reg_base);
1136 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1137 pci_disable_device(pdev);
1138 kfree(ndev);
1139}
1140
1141static struct pci_driver ntb_pci_driver = {
1142 .name = KBUILD_MODNAME,
1143 .id_table = ntb_pci_tbl,
1144 .probe = ntb_pci_probe,
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -08001145 .remove = ntb_pci_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001146};
1147module_pci_driver(ntb_pci_driver);