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