Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[linux-2.6-microblaze.git] / drivers / staging / rdma / ipath / ipath_kernel.h
1 #ifndef _IPATH_KERNEL_H
2 #define _IPATH_KERNEL_H
3 /*
4  * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
5  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35
36 /*
37  * This header file is the base header file for infinipath kernel code
38  * ipath_user.h serves a similar purpose for user code.
39  */
40
41 #include <linux/interrupt.h>
42 #include <linux/pci.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/mutex.h>
45 #include <linux/list.h>
46 #include <linux/scatterlist.h>
47 #include <asm/io.h>
48 #include <rdma/ib_verbs.h>
49
50 #include "ipath_common.h"
51 #include "ipath_debug.h"
52 #include "ipath_registers.h"
53
54 /* only s/w major version of InfiniPath we can handle */
55 #define IPATH_CHIP_VERS_MAJ 2U
56
57 /* don't care about this except printing */
58 #define IPATH_CHIP_VERS_MIN 0U
59
60 /* temporary, maybe always */
61 extern struct infinipath_stats ipath_stats;
62
63 #define IPATH_CHIP_SWVERSION IPATH_CHIP_VERS_MAJ
64 /*
65  * First-cut critierion for "device is active" is
66  * two thousand dwords combined Tx, Rx traffic per
67  * 5-second interval. SMA packets are 64 dwords,
68  * and occur "a few per second", presumably each way.
69  */
70 #define IPATH_TRAFFIC_ACTIVE_THRESHOLD (2000)
71 /*
72  * Struct used to indicate which errors are logged in each of the
73  * error-counters that are logged to EEPROM. A counter is incremented
74  * _once_ (saturating at 255) for each event with any bits set in
75  * the error or hwerror register masks below.
76  */
77 #define IPATH_EEP_LOG_CNT (4)
78 struct ipath_eep_log_mask {
79         u64 errs_to_log;
80         u64 hwerrs_to_log;
81 };
82
83 struct ipath_portdata {
84         void **port_rcvegrbuf;
85         dma_addr_t *port_rcvegrbuf_phys;
86         /* rcvhdrq base, needs mmap before useful */
87         void *port_rcvhdrq;
88         /* kernel virtual address where hdrqtail is updated */
89         void *port_rcvhdrtail_kvaddr;
90         /*
91          * temp buffer for expected send setup, allocated at open, instead
92          * of each setup call
93          */
94         void *port_tid_pg_list;
95         /* when waiting for rcv or pioavail */
96         wait_queue_head_t port_wait;
97         /*
98          * rcvegr bufs base, physical, must fit
99          * in 44 bits so 32 bit programs mmap64 44 bit works)
100          */
101         dma_addr_t port_rcvegr_phys;
102         /* mmap of hdrq, must fit in 44 bits */
103         dma_addr_t port_rcvhdrq_phys;
104         dma_addr_t port_rcvhdrqtailaddr_phys;
105         /*
106          * number of opens (including slave subports) on this instance
107          * (ignoring forks, dup, etc. for now)
108          */
109         int port_cnt;
110         /*
111          * how much space to leave at start of eager TID entries for
112          * protocol use, on each TID
113          */
114         /* instead of calculating it */
115         unsigned port_port;
116         /* non-zero if port is being shared. */
117         u16 port_subport_cnt;
118         /* non-zero if port is being shared. */
119         u16 port_subport_id;
120         /* number of pio bufs for this port (all procs, if shared) */
121         u32 port_piocnt;
122         /* first pio buffer for this port */
123         u32 port_pio_base;
124         /* chip offset of PIO buffers for this port */
125         u32 port_piobufs;
126         /* how many alloc_pages() chunks in port_rcvegrbuf_pages */
127         u32 port_rcvegrbuf_chunks;
128         /* how many egrbufs per chunk */
129         u32 port_rcvegrbufs_perchunk;
130         /* order for port_rcvegrbuf_pages */
131         size_t port_rcvegrbuf_size;
132         /* rcvhdrq size (for freeing) */
133         size_t port_rcvhdrq_size;
134         /* next expected TID to check when looking for free */
135         u32 port_tidcursor;
136         /* next expected TID to check */
137         unsigned long port_flag;
138         /* what happened */
139         unsigned long int_flag;
140         /* WAIT_RCV that timed out, no interrupt */
141         u32 port_rcvwait_to;
142         /* WAIT_PIO that timed out, no interrupt */
143         u32 port_piowait_to;
144         /* WAIT_RCV already happened, no wait */
145         u32 port_rcvnowait;
146         /* WAIT_PIO already happened, no wait */
147         u32 port_pionowait;
148         /* total number of rcvhdrqfull errors */
149         u32 port_hdrqfull;
150         /*
151          * Used to suppress multiple instances of same
152          * port staying stuck at same point.
153          */
154         u32 port_lastrcvhdrqtail;
155         /* saved total number of rcvhdrqfull errors for poll edge trigger */
156         u32 port_hdrqfull_poll;
157         /* total number of polled urgent packets */
158         u32 port_urgent;
159         /* saved total number of polled urgent packets for poll edge trigger */
160         u32 port_urgent_poll;
161         /* pid of process using this port */
162         struct pid *port_pid;
163         struct pid *port_subpid[INFINIPATH_MAX_SUBPORT];
164         /* same size as task_struct .comm[] */
165         char port_comm[16];
166         /* pkeys set by this use of this port */
167         u16 port_pkeys[4];
168         /* so file ops can get at unit */
169         struct ipath_devdata *port_dd;
170         /* A page of memory for rcvhdrhead, rcvegrhead, rcvegrtail * N */
171         void *subport_uregbase;
172         /* An array of pages for the eager receive buffers * N */
173         void *subport_rcvegrbuf;
174         /* An array of pages for the eager header queue entries * N */
175         void *subport_rcvhdr_base;
176         /* The version of the library which opened this port */
177         u32 userversion;
178         /* Bitmask of active slaves */
179         u32 active_slaves;
180         /* Type of packets or conditions we want to poll for */
181         u16 poll_type;
182         /* port rcvhdrq head offset */
183         u32 port_head;
184         /* receive packet sequence counter */
185         u32 port_seq_cnt;
186 };
187
188 struct sk_buff;
189 struct ipath_sge_state;
190 struct ipath_verbs_txreq;
191
192 /*
193  * control information for layered drivers
194  */
195 struct _ipath_layer {
196         void *l_arg;
197 };
198
199 struct ipath_skbinfo {
200         struct sk_buff *skb;
201         dma_addr_t phys;
202 };
203
204 struct ipath_sdma_txreq {
205         int                 flags;
206         int                 sg_count;
207         union {
208                 struct scatterlist *sg;
209                 void *map_addr;
210         };
211         void              (*callback)(void *, int);
212         void               *callback_cookie;
213         int                 callback_status;
214         u16                 start_idx;  /* sdma private */
215         u16                 next_descq_idx;  /* sdma private */
216         struct list_head    list;       /* sdma private */
217 };
218
219 struct ipath_sdma_desc {
220         __le64 qw[2];
221 };
222
223 #define IPATH_SDMA_TXREQ_F_USELARGEBUF  0x1
224 #define IPATH_SDMA_TXREQ_F_HEADTOHOST   0x2
225 #define IPATH_SDMA_TXREQ_F_INTREQ       0x4
226 #define IPATH_SDMA_TXREQ_F_FREEBUF      0x8
227 #define IPATH_SDMA_TXREQ_F_FREEDESC     0x10
228 #define IPATH_SDMA_TXREQ_F_VL15         0x20
229
230 #define IPATH_SDMA_TXREQ_S_OK        0
231 #define IPATH_SDMA_TXREQ_S_SENDERROR 1
232 #define IPATH_SDMA_TXREQ_S_ABORTED   2
233 #define IPATH_SDMA_TXREQ_S_SHUTDOWN  3
234
235 #define IPATH_SDMA_STATUS_SCORE_BOARD_DRAIN_IN_PROG     (1ull << 63)
236 #define IPATH_SDMA_STATUS_ABORT_IN_PROG                 (1ull << 62)
237 #define IPATH_SDMA_STATUS_INTERNAL_SDMA_ENABLE          (1ull << 61)
238 #define IPATH_SDMA_STATUS_SCB_EMPTY                     (1ull << 30)
239
240 /* max dwords in small buffer packet */
241 #define IPATH_SMALLBUF_DWORDS (dd->ipath_piosize2k >> 2)
242
243 /*
244  * Possible IB config parameters for ipath_f_get/set_ib_cfg()
245  */
246 #define IPATH_IB_CFG_LIDLMC 0 /* Get/set LID (LS16b) and Mask (MS16b) */
247 #define IPATH_IB_CFG_HRTBT 1 /* Get/set Heartbeat off/enable/auto */
248 #define IPATH_IB_HRTBT_ON 3 /* Heartbeat enabled, sent every 100msec */
249 #define IPATH_IB_HRTBT_OFF 0 /* Heartbeat off */
250 #define IPATH_IB_CFG_LWID_ENB 2 /* Get/set allowed Link-width */
251 #define IPATH_IB_CFG_LWID 3 /* Get currently active Link-width */
252 #define IPATH_IB_CFG_SPD_ENB 4 /* Get/set allowed Link speeds */
253 #define IPATH_IB_CFG_SPD 5 /* Get current Link spd */
254 #define IPATH_IB_CFG_RXPOL_ENB 6 /* Get/set Auto-RX-polarity enable */
255 #define IPATH_IB_CFG_LREV_ENB 7 /* Get/set Auto-Lane-reversal enable */
256 #define IPATH_IB_CFG_LINKLATENCY 8 /* Get Auto-Lane-reversal enable */
257
258
259 struct ipath_devdata {
260         struct list_head ipath_list;
261
262         struct ipath_kregs const *ipath_kregs;
263         struct ipath_cregs const *ipath_cregs;
264
265         /* mem-mapped pointer to base of chip regs */
266         u64 __iomem *ipath_kregbase;
267         /* end of mem-mapped chip space; range checking */
268         u64 __iomem *ipath_kregend;
269         /* physical address of chip for io_remap, etc. */
270         unsigned long ipath_physaddr;
271         /* base of memory alloced for ipath_kregbase, for free */
272         u64 *ipath_kregalloc;
273         /* ipath_cfgports pointers */
274         struct ipath_portdata **ipath_pd;
275         /* sk_buffs used by port 0 eager receive queue */
276         struct ipath_skbinfo *ipath_port0_skbinfo;
277         /* kvirt address of 1st 2k pio buffer */
278         void __iomem *ipath_pio2kbase;
279         /* kvirt address of 1st 4k pio buffer */
280         void __iomem *ipath_pio4kbase;
281         /*
282          * points to area where PIOavail registers will be DMA'ed.
283          * Has to be on a page of it's own, because the page will be
284          * mapped into user program space.  This copy is *ONLY* ever
285          * written by DMA, not by the driver!  Need a copy per device
286          * when we get to multiple devices
287          */
288         volatile __le64 *ipath_pioavailregs_dma;
289         /* physical address where updates occur */
290         dma_addr_t ipath_pioavailregs_phys;
291         struct _ipath_layer ipath_layer;
292         /* setup intr */
293         int (*ipath_f_intrsetup)(struct ipath_devdata *);
294         /* fallback to alternate interrupt type if possible */
295         int (*ipath_f_intr_fallback)(struct ipath_devdata *);
296         /* setup on-chip bus config */
297         int (*ipath_f_bus)(struct ipath_devdata *, struct pci_dev *);
298         /* hard reset chip */
299         int (*ipath_f_reset)(struct ipath_devdata *);
300         int (*ipath_f_get_boardname)(struct ipath_devdata *, char *,
301                                      size_t);
302         void (*ipath_f_init_hwerrors)(struct ipath_devdata *);
303         void (*ipath_f_handle_hwerrors)(struct ipath_devdata *, char *,
304                                         size_t);
305         void (*ipath_f_quiet_serdes)(struct ipath_devdata *);
306         int (*ipath_f_bringup_serdes)(struct ipath_devdata *);
307         int (*ipath_f_early_init)(struct ipath_devdata *);
308         void (*ipath_f_clear_tids)(struct ipath_devdata *, unsigned);
309         void (*ipath_f_put_tid)(struct ipath_devdata *, u64 __iomem*,
310                                 u32, unsigned long);
311         void (*ipath_f_tidtemplate)(struct ipath_devdata *);
312         void (*ipath_f_cleanup)(struct ipath_devdata *);
313         void (*ipath_f_setextled)(struct ipath_devdata *, u64, u64);
314         /* fill out chip-specific fields */
315         int (*ipath_f_get_base_info)(struct ipath_portdata *, void *);
316         /* free irq */
317         void (*ipath_f_free_irq)(struct ipath_devdata *);
318         struct ipath_message_header *(*ipath_f_get_msgheader)
319                                         (struct ipath_devdata *, __le32 *);
320         void (*ipath_f_config_ports)(struct ipath_devdata *, ushort);
321         int (*ipath_f_get_ib_cfg)(struct ipath_devdata *, int);
322         int (*ipath_f_set_ib_cfg)(struct ipath_devdata *, int, u32);
323         void (*ipath_f_config_jint)(struct ipath_devdata *, u16 , u16);
324         void (*ipath_f_read_counters)(struct ipath_devdata *,
325                                         struct infinipath_counters *);
326         void (*ipath_f_xgxs_reset)(struct ipath_devdata *);
327         /* per chip actions needed for IB Link up/down changes */
328         int (*ipath_f_ib_updown)(struct ipath_devdata *, int, u64);
329
330         unsigned ipath_lastegr_idx;
331         struct ipath_ibdev *verbs_dev;
332         struct timer_list verbs_timer;
333         /* total dwords sent (summed from counter) */
334         u64 ipath_sword;
335         /* total dwords rcvd (summed from counter) */
336         u64 ipath_rword;
337         /* total packets sent (summed from counter) */
338         u64 ipath_spkts;
339         /* total packets rcvd (summed from counter) */
340         u64 ipath_rpkts;
341         /* ipath_statusp initially points to this. */
342         u64 _ipath_status;
343         /* GUID for this interface, in network order */
344         __be64 ipath_guid;
345         /*
346          * aggregrate of error bits reported since last cleared, for
347          * limiting of error reporting
348          */
349         ipath_err_t ipath_lasterror;
350         /*
351          * aggregrate of error bits reported since last cleared, for
352          * limiting of hwerror reporting
353          */
354         ipath_err_t ipath_lasthwerror;
355         /* errors masked because they occur too fast */
356         ipath_err_t ipath_maskederrs;
357         u64 ipath_lastlinkrecov; /* link recoveries at last ACTIVE */
358         /* these 5 fields are used to establish deltas for IB Symbol
359          * errors and linkrecovery errors. They can be reported on
360          * some chips during link negotiation prior to INIT, and with
361          * DDR when faking DDR negotiations with non-IBTA switches.
362          * The chip counters are adjusted at driver unload if there is
363          * a non-zero delta.
364          */
365         u64 ibdeltainprog;
366         u64 ibsymdelta;
367         u64 ibsymsnap;
368         u64 iblnkerrdelta;
369         u64 iblnkerrsnap;
370
371         /* time in jiffies at which to re-enable maskederrs */
372         unsigned long ipath_unmasktime;
373         /* count of egrfull errors, combined for all ports */
374         u64 ipath_last_tidfull;
375         /* for ipath_qcheck() */
376         u64 ipath_lastport0rcv_cnt;
377         /* template for writing TIDs  */
378         u64 ipath_tidtemplate;
379         /* value to write to free TIDs */
380         u64 ipath_tidinvalid;
381         /* IBA6120 rcv interrupt setup */
382         u64 ipath_rhdrhead_intr_off;
383
384         /* size of memory at ipath_kregbase */
385         u32 ipath_kregsize;
386         /* number of registers used for pioavail */
387         u32 ipath_pioavregs;
388         /* IPATH_POLL, etc. */
389         u32 ipath_flags;
390         /* ipath_flags driver is waiting for */
391         u32 ipath_state_wanted;
392         /* last buffer for user use, first buf for kernel use is this
393          * index. */
394         u32 ipath_lastport_piobuf;
395         /* is a stats timer active */
396         u32 ipath_stats_timer_active;
397         /* number of interrupts for this device -- saturates... */
398         u32 ipath_int_counter;
399         /* dwords sent read from counter */
400         u32 ipath_lastsword;
401         /* dwords received read from counter */
402         u32 ipath_lastrword;
403         /* sent packets read from counter */
404         u32 ipath_lastspkts;
405         /* received packets read from counter */
406         u32 ipath_lastrpkts;
407         /* pio bufs allocated per port */
408         u32 ipath_pbufsport;
409         /* if remainder on bufs/port, ports < extrabuf get 1 extra */
410         u32 ipath_ports_extrabuf;
411         u32 ipath_pioupd_thresh; /* update threshold, some chips */
412         /*
413          * number of ports configured as max; zero is set to number chip
414          * supports, less gives more pio bufs/port, etc.
415          */
416         u32 ipath_cfgports;
417         /* count of port 0 hdrqfull errors */
418         u32 ipath_p0_hdrqfull;
419         /* port 0 number of receive eager buffers */
420         u32 ipath_p0_rcvegrcnt;
421
422         /*
423          * index of last piobuffer we used.  Speeds up searching, by
424          * starting at this point.  Doesn't matter if multiple cpu's use and
425          * update, last updater is only write that matters.  Whenever it
426          * wraps, we update shadow copies.  Need a copy per device when we
427          * get to multiple devices
428          */
429         u32 ipath_lastpioindex;
430         u32 ipath_lastpioindexl;
431         /* max length of freezemsg */
432         u32 ipath_freezelen;
433         /*
434          * consecutive times we wanted a PIO buffer but were unable to
435          * get one
436          */
437         u32 ipath_consec_nopiobuf;
438         /*
439          * hint that we should update ipath_pioavailshadow before
440          * looking for a PIO buffer
441          */
442         u32 ipath_upd_pio_shadow;
443         /* so we can rewrite it after a chip reset */
444         u32 ipath_pcibar0;
445         /* so we can rewrite it after a chip reset */
446         u32 ipath_pcibar1;
447         u32 ipath_x1_fix_tries;
448         u32 ipath_autoneg_tries;
449         u32 serdes_first_init_done;
450
451         struct ipath_relock {
452                 atomic_t ipath_relock_timer_active;
453                 struct timer_list ipath_relock_timer;
454                 unsigned int ipath_relock_interval; /* in jiffies */
455         } ipath_relock_singleton;
456
457         /* interrupt number */
458         int ipath_irq;
459         /* HT/PCI Vendor ID (here for NodeInfo) */
460         u16 ipath_vendorid;
461         /* HT/PCI Device ID (here for NodeInfo) */
462         u16 ipath_deviceid;
463         /* offset in HT config space of slave/primary interface block */
464         u8 ipath_ht_slave_off;
465         /* for write combining settings */
466         int wc_cookie;
467         /* ref count for each pkey */
468         atomic_t ipath_pkeyrefs[4];
469         /* shadow copy of struct page *'s for exp tid pages */
470         struct page **ipath_pageshadow;
471         /* shadow copy of dma handles for exp tid pages */
472         dma_addr_t *ipath_physshadow;
473         u64 __iomem *ipath_egrtidbase;
474         /* lock to workaround chip bug 9437 and others */
475         spinlock_t ipath_kernel_tid_lock;
476         spinlock_t ipath_user_tid_lock;
477         spinlock_t ipath_sendctrl_lock;
478         /* around ipath_pd and (user ports) port_cnt use (intr vs free) */
479         spinlock_t ipath_uctxt_lock;
480
481         /*
482          * IPATH_STATUS_*,
483          * this address is mapped readonly into user processes so they can
484          * get status cheaply, whenever they want.
485          */
486         u64 *ipath_statusp;
487         /* freeze msg if hw error put chip in freeze */
488         char *ipath_freezemsg;
489         /* pci access data structure */
490         struct pci_dev *pcidev;
491         struct cdev *user_cdev;
492         struct cdev *diag_cdev;
493         struct device *user_dev;
494         struct device *diag_dev;
495         /* timer used to prevent stats overflow, error throttling, etc. */
496         struct timer_list ipath_stats_timer;
497         /* timer to verify interrupts work, and fallback if possible */
498         struct timer_list ipath_intrchk_timer;
499         void *ipath_dummy_hdrq; /* used after port close */
500         dma_addr_t ipath_dummy_hdrq_phys;
501
502         /* SendDMA related entries */
503         spinlock_t            ipath_sdma_lock;
504         unsigned long         ipath_sdma_status;
505         unsigned long         ipath_sdma_abort_jiffies;
506         unsigned long         ipath_sdma_abort_intr_timeout;
507         unsigned long         ipath_sdma_buf_jiffies;
508         struct ipath_sdma_desc *ipath_sdma_descq;
509         u64                   ipath_sdma_descq_added;
510         u64                   ipath_sdma_descq_removed;
511         int                   ipath_sdma_desc_nreserved;
512         u16                   ipath_sdma_descq_cnt;
513         u16                   ipath_sdma_descq_tail;
514         u16                   ipath_sdma_descq_head;
515         u16                   ipath_sdma_next_intr;
516         u16                   ipath_sdma_reset_wait;
517         u8                    ipath_sdma_generation;
518         struct tasklet_struct ipath_sdma_abort_task;
519         struct tasklet_struct ipath_sdma_notify_task;
520         struct list_head      ipath_sdma_activelist;
521         struct list_head      ipath_sdma_notifylist;
522         atomic_t              ipath_sdma_vl15_count;
523         struct timer_list     ipath_sdma_vl15_timer;
524
525         dma_addr_t       ipath_sdma_descq_phys;
526         volatile __le64 *ipath_sdma_head_dma;
527         dma_addr_t       ipath_sdma_head_phys;
528
529         unsigned long ipath_ureg_align; /* user register alignment */
530
531         struct delayed_work ipath_autoneg_work;
532         wait_queue_head_t ipath_autoneg_wait;
533
534         /* HoL blocking / user app forward-progress state */
535         unsigned          ipath_hol_state;
536         unsigned          ipath_hol_next;
537         struct timer_list ipath_hol_timer;
538
539         /*
540          * Shadow copies of registers; size indicates read access size.
541          * Most of them are readonly, but some are write-only register,
542          * where we manipulate the bits in the shadow copy, and then write
543          * the shadow copy to infinipath.
544          *
545          * We deliberately make most of these 32 bits, since they have
546          * restricted range.  For any that we read, we won't to generate 32
547          * bit accesses, since Opteron will generate 2 separate 32 bit HT
548          * transactions for a 64 bit read, and we want to avoid unnecessary
549          * HT transactions.
550          */
551
552         /* This is the 64 bit group */
553
554         /*
555          * shadow of pioavail, check to be sure it's large enough at
556          * init time.
557          */
558         unsigned long ipath_pioavailshadow[8];
559         /* bitmap of send buffers available for the kernel to use with PIO. */
560         unsigned long ipath_pioavailkernel[8];
561         /* shadow of kr_gpio_out, for rmw ops */
562         u64 ipath_gpio_out;
563         /* shadow the gpio mask register */
564         u64 ipath_gpio_mask;
565         /* shadow the gpio output enable, etc... */
566         u64 ipath_extctrl;
567         /* kr_revision shadow */
568         u64 ipath_revision;
569         /*
570          * shadow of ibcctrl, for interrupt handling of link changes,
571          * etc.
572          */
573         u64 ipath_ibcctrl;
574         /*
575          * last ibcstatus, to suppress "duplicate" status change messages,
576          * mostly from 2 to 3
577          */
578         u64 ipath_lastibcstat;
579         /* hwerrmask shadow */
580         ipath_err_t ipath_hwerrmask;
581         ipath_err_t ipath_errormask; /* errormask shadow */
582         /* interrupt config reg shadow */
583         u64 ipath_intconfig;
584         /* kr_sendpiobufbase value */
585         u64 ipath_piobufbase;
586         /* kr_ibcddrctrl shadow */
587         u64 ipath_ibcddrctrl;
588
589         /* these are the "32 bit" regs */
590
591         /*
592          * number of GUIDs in the flash for this interface; may need some
593          * rethinking for setting on other ifaces
594          */
595         u32 ipath_nguid;
596         /*
597          * the following two are 32-bit bitmasks, but {test,clear,set}_bit
598          * all expect bit fields to be "unsigned long"
599          */
600         /* shadow kr_rcvctrl */
601         unsigned long ipath_rcvctrl;
602         /* shadow kr_sendctrl */
603         unsigned long ipath_sendctrl;
604         /* to not count armlaunch after cancel */
605         unsigned long ipath_lastcancel;
606         /* count cases where special trigger was needed (double write) */
607         unsigned long ipath_spectriggerhit;
608
609         /* value we put in kr_rcvhdrcnt */
610         u32 ipath_rcvhdrcnt;
611         /* value we put in kr_rcvhdrsize */
612         u32 ipath_rcvhdrsize;
613         /* value we put in kr_rcvhdrentsize */
614         u32 ipath_rcvhdrentsize;
615         /* offset of last entry in rcvhdrq */
616         u32 ipath_hdrqlast;
617         /* kr_portcnt value */
618         u32 ipath_portcnt;
619         /* kr_pagealign value */
620         u32 ipath_palign;
621         /* number of "2KB" PIO buffers */
622         u32 ipath_piobcnt2k;
623         /* size in bytes of "2KB" PIO buffers */
624         u32 ipath_piosize2k;
625         /* number of "4KB" PIO buffers */
626         u32 ipath_piobcnt4k;
627         /* size in bytes of "4KB" PIO buffers */
628         u32 ipath_piosize4k;
629         u32 ipath_pioreserved; /* reserved special-inkernel; */
630         /* kr_rcvegrbase value */
631         u32 ipath_rcvegrbase;
632         /* kr_rcvegrcnt value */
633         u32 ipath_rcvegrcnt;
634         /* kr_rcvtidbase value */
635         u32 ipath_rcvtidbase;
636         /* kr_rcvtidcnt value */
637         u32 ipath_rcvtidcnt;
638         /* kr_sendregbase */
639         u32 ipath_sregbase;
640         /* kr_userregbase */
641         u32 ipath_uregbase;
642         /* kr_counterregbase */
643         u32 ipath_cregbase;
644         /* shadow the control register contents */
645         u32 ipath_control;
646         /* PCI revision register (HTC rev on FPGA) */
647         u32 ipath_pcirev;
648
649         /* chip address space used by 4k pio buffers */
650         u32 ipath_4kalign;
651         /* The MTU programmed for this unit */
652         u32 ipath_ibmtu;
653         /*
654          * The max size IB packet, included IB headers that we can send.
655          * Starts same as ipath_piosize, but is affected when ibmtu is
656          * changed, or by size of eager buffers
657          */
658         u32 ipath_ibmaxlen;
659         /*
660          * ibmaxlen at init time, limited by chip and by receive buffer
661          * size.  Not changed after init.
662          */
663         u32 ipath_init_ibmaxlen;
664         /* size of each rcvegrbuffer */
665         u32 ipath_rcvegrbufsize;
666         /* localbus width (1, 2,4,8,16,32) from config space  */
667         u32 ipath_lbus_width;
668         /* localbus speed (HT: 200,400,800,1000; PCIe 2500) */
669         u32 ipath_lbus_speed;
670         /*
671          * number of sequential ibcstatus change for polling active/quiet
672          * (i.e., link not coming up).
673          */
674         u32 ipath_ibpollcnt;
675         /* low and high portions of MSI capability/vector */
676         u32 ipath_msi_lo;
677         /* saved after PCIe init for restore after reset */
678         u32 ipath_msi_hi;
679         /* MSI data (vector) saved for restore */
680         u16 ipath_msi_data;
681         /* MLID programmed for this instance */
682         u16 ipath_mlid;
683         /* LID programmed for this instance */
684         u16 ipath_lid;
685         /* list of pkeys programmed; 0 if not set */
686         u16 ipath_pkeys[4];
687         /*
688          * ASCII serial number, from flash, large enough for original
689          * all digit strings, and longer QLogic serial number format
690          */
691         u8 ipath_serial[16];
692         /* human readable board version */
693         u8 ipath_boardversion[96];
694         u8 ipath_lbus_info[32]; /* human readable localbus info */
695         /* chip major rev, from ipath_revision */
696         u8 ipath_majrev;
697         /* chip minor rev, from ipath_revision */
698         u8 ipath_minrev;
699         /* board rev, from ipath_revision */
700         u8 ipath_boardrev;
701         /* saved for restore after reset */
702         u8 ipath_pci_cacheline;
703         /* LID mask control */
704         u8 ipath_lmc;
705         /* link width supported */
706         u8 ipath_link_width_supported;
707         /* link speed supported */
708         u8 ipath_link_speed_supported;
709         u8 ipath_link_width_enabled;
710         u8 ipath_link_speed_enabled;
711         u8 ipath_link_width_active;
712         u8 ipath_link_speed_active;
713         /* Rx Polarity inversion (compensate for ~tx on partner) */
714         u8 ipath_rx_pol_inv;
715
716         u8 ipath_r_portenable_shift;
717         u8 ipath_r_intravail_shift;
718         u8 ipath_r_tailupd_shift;
719         u8 ipath_r_portcfg_shift;
720
721         /* unit # of this chip, if present */
722         int ipath_unit;
723
724         /* local link integrity counter */
725         u32 ipath_lli_counter;
726         /* local link integrity errors */
727         u32 ipath_lli_errors;
728         /*
729          * Above counts only cases where _successive_ LocalLinkIntegrity
730          * errors were seen in the receive headers of kern-packets.
731          * Below are the three (monotonically increasing) counters
732          * maintained via GPIO interrupts on iba6120-rev2.
733          */
734         u32 ipath_rxfc_unsupvl_errs;
735         u32 ipath_overrun_thresh_errs;
736         u32 ipath_lli_errs;
737
738         /*
739          * Not all devices managed by a driver instance are the same
740          * type, so these fields must be per-device.
741          */
742         u64 ipath_i_bitsextant;
743         ipath_err_t ipath_e_bitsextant;
744         ipath_err_t ipath_hwe_bitsextant;
745
746         /*
747          * Below should be computable from number of ports,
748          * since they are never modified.
749          */
750         u64 ipath_i_rcvavail_mask;
751         u64 ipath_i_rcvurg_mask;
752         u16 ipath_i_rcvurg_shift;
753         u16 ipath_i_rcvavail_shift;
754
755         /*
756          * Register bits for selecting i2c direction and values, used for
757          * I2C serial flash.
758          */
759         u8 ipath_gpio_sda_num;
760         u8 ipath_gpio_scl_num;
761         u8 ipath_i2c_chain_type;
762         u64 ipath_gpio_sda;
763         u64 ipath_gpio_scl;
764
765         /* lock for doing RMW of shadows/regs for ExtCtrl and GPIO */
766         spinlock_t ipath_gpio_lock;
767
768         /*
769          * IB link and linktraining states and masks that vary per chip in
770          * some way.  Set at init, to avoid each IB status change interrupt
771          */
772         u8 ibcs_ls_shift;
773         u8 ibcs_lts_mask;
774         u32 ibcs_mask;
775         u32 ib_init;
776         u32 ib_arm;
777         u32 ib_active;
778
779         u16 ipath_rhf_offset; /* offset of RHF within receive header entry */
780
781         /*
782          * shift/mask for linkcmd, linkinitcmd, maxpktlen in ibccontol
783          * reg. Changes for IBA7220
784          */
785         u8 ibcc_lic_mask; /* LinkInitCmd */
786         u8 ibcc_lc_shift; /* LinkCmd */
787         u8 ibcc_mpl_shift; /* Maxpktlen */
788
789         u8 delay_mult;
790
791         /* used to override LED behavior */
792         u8 ipath_led_override;  /* Substituted for normal value, if non-zero */
793         u16 ipath_led_override_timeoff; /* delta to next timer event */
794         u8 ipath_led_override_vals[2]; /* Alternates per blink-frame */
795         u8 ipath_led_override_phase; /* Just counts, LSB picks from vals[] */
796         atomic_t ipath_led_override_timer_active;
797         /* Used to flash LEDs in override mode */
798         struct timer_list ipath_led_override_timer;
799
800         /* Support (including locks) for EEPROM logging of errors and time */
801         /* control access to actual counters, timer */
802         spinlock_t ipath_eep_st_lock;
803         /* control high-level access to EEPROM */
804         struct mutex ipath_eep_lock;
805         /* Below inc'd by ipath_snap_cntrs(), locked by ipath_eep_st_lock */
806         uint64_t ipath_traffic_wds;
807         /* active time is kept in seconds, but logged in hours */
808         atomic_t ipath_active_time;
809         /* Below are nominal shadow of EEPROM, new since last EEPROM update */
810         uint8_t ipath_eep_st_errs[IPATH_EEP_LOG_CNT];
811         uint8_t ipath_eep_st_new_errs[IPATH_EEP_LOG_CNT];
812         uint16_t ipath_eep_hrs;
813         /*
814          * masks for which bits of errs, hwerrs that cause
815          * each of the counters to increment.
816          */
817         struct ipath_eep_log_mask ipath_eep_st_masks[IPATH_EEP_LOG_CNT];
818
819         /* interrupt mitigation reload register info */
820         u16 ipath_jint_idle_ticks;      /* idle clock ticks */
821         u16 ipath_jint_max_packets;     /* max packets across all ports */
822
823         /*
824          * lock for access to SerDes, and flags to sequence preset
825          * versus steady-state. 7220-only at the moment.
826          */
827         spinlock_t ipath_sdepb_lock;
828         u8 ipath_presets_needed; /* Set if presets to be restored next DOWN */
829 };
830
831 /* ipath_hol_state values (stopping/starting user proc, send flushing) */
832 #define IPATH_HOL_UP       0
833 #define IPATH_HOL_DOWN     1
834 /* ipath_hol_next toggle values, used when hol_state IPATH_HOL_DOWN */
835 #define IPATH_HOL_DOWNSTOP 0
836 #define IPATH_HOL_DOWNCONT 1
837
838 /* bit positions for sdma_status */
839 #define IPATH_SDMA_ABORTING  0
840 #define IPATH_SDMA_DISARMED  1
841 #define IPATH_SDMA_DISABLED  2
842 #define IPATH_SDMA_LAYERBUF  3
843 #define IPATH_SDMA_RUNNING  30
844 #define IPATH_SDMA_SHUTDOWN 31
845
846 /* bit combinations that correspond to abort states */
847 #define IPATH_SDMA_ABORT_NONE 0
848 #define IPATH_SDMA_ABORT_ABORTING (1UL << IPATH_SDMA_ABORTING)
849 #define IPATH_SDMA_ABORT_DISARMED ((1UL << IPATH_SDMA_ABORTING) | \
850         (1UL << IPATH_SDMA_DISARMED))
851 #define IPATH_SDMA_ABORT_DISABLED ((1UL << IPATH_SDMA_ABORTING) | \
852         (1UL << IPATH_SDMA_DISABLED))
853 #define IPATH_SDMA_ABORT_ABORTED ((1UL << IPATH_SDMA_ABORTING) | \
854         (1UL << IPATH_SDMA_DISARMED) | (1UL << IPATH_SDMA_DISABLED))
855 #define IPATH_SDMA_ABORT_MASK ((1UL<<IPATH_SDMA_ABORTING) | \
856         (1UL << IPATH_SDMA_DISARMED) | (1UL << IPATH_SDMA_DISABLED))
857
858 #define IPATH_SDMA_BUF_NONE 0
859 #define IPATH_SDMA_BUF_MASK (1UL<<IPATH_SDMA_LAYERBUF)
860
861 /* Private data for file operations */
862 struct ipath_filedata {
863         struct ipath_portdata *pd;
864         unsigned subport;
865         unsigned tidcursor;
866         struct ipath_user_sdma_queue *pq;
867 };
868 extern struct list_head ipath_dev_list;
869 extern spinlock_t ipath_devs_lock;
870 extern struct ipath_devdata *ipath_lookup(int unit);
871
872 int ipath_init_chip(struct ipath_devdata *, int);
873 int ipath_enable_wc(struct ipath_devdata *dd);
874 void ipath_disable_wc(struct ipath_devdata *dd);
875 int ipath_count_units(int *npresentp, int *nupp, int *maxportsp);
876 void ipath_shutdown_device(struct ipath_devdata *);
877 void ipath_clear_freeze(struct ipath_devdata *);
878
879 struct file_operations;
880 int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
881                     struct cdev **cdevp, struct device **devp);
882 void ipath_cdev_cleanup(struct cdev **cdevp,
883                         struct device **devp);
884
885 int ipath_diag_add(struct ipath_devdata *);
886 void ipath_diag_remove(struct ipath_devdata *);
887
888 extern wait_queue_head_t ipath_state_wait;
889
890 int ipath_user_add(struct ipath_devdata *dd);
891 void ipath_user_remove(struct ipath_devdata *dd);
892
893 struct sk_buff *ipath_alloc_skb(struct ipath_devdata *dd, gfp_t);
894
895 extern int ipath_diag_inuse;
896
897 irqreturn_t ipath_intr(int irq, void *devid);
898 int ipath_decode_err(struct ipath_devdata *dd, char *buf, size_t blen,
899                      ipath_err_t err);
900 #if __IPATH_INFO || __IPATH_DBG
901 extern const char *ipath_ibcstatus_str[];
902 #endif
903
904 /* clean up any per-chip chip-specific stuff */
905 void ipath_chip_cleanup(struct ipath_devdata *);
906 /* clean up any chip type-specific stuff */
907 void ipath_chip_done(void);
908
909 void ipath_disarm_piobufs(struct ipath_devdata *, unsigned first,
910                           unsigned cnt);
911 void ipath_cancel_sends(struct ipath_devdata *, int);
912
913 int ipath_create_rcvhdrq(struct ipath_devdata *, struct ipath_portdata *);
914 void ipath_free_pddata(struct ipath_devdata *, struct ipath_portdata *);
915
916 int ipath_parse_ushort(const char *str, unsigned short *valp);
917
918 void ipath_kreceive(struct ipath_portdata *);
919 int ipath_setrcvhdrsize(struct ipath_devdata *, unsigned);
920 int ipath_reset_device(int);
921 void ipath_get_faststats(unsigned long);
922 int ipath_wait_linkstate(struct ipath_devdata *, u32, int);
923 int ipath_set_linkstate(struct ipath_devdata *, u8);
924 int ipath_set_mtu(struct ipath_devdata *, u16);
925 int ipath_set_lid(struct ipath_devdata *, u32, u8);
926 int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv);
927 void ipath_enable_armlaunch(struct ipath_devdata *);
928 void ipath_disable_armlaunch(struct ipath_devdata *);
929 void ipath_hol_down(struct ipath_devdata *);
930 void ipath_hol_up(struct ipath_devdata *);
931 void ipath_hol_event(unsigned long);
932 void ipath_toggle_rclkrls(struct ipath_devdata *);
933 void ipath_sd7220_clr_ibpar(struct ipath_devdata *);
934 void ipath_set_relock_poll(struct ipath_devdata *, int);
935 void ipath_shutdown_relock_poll(struct ipath_devdata *);
936
937 /* for use in system calls, where we want to know device type, etc. */
938 #define port_fp(fp) ((struct ipath_filedata *)(fp)->private_data)->pd
939 #define subport_fp(fp) \
940         ((struct ipath_filedata *)(fp)->private_data)->subport
941 #define tidcursor_fp(fp) \
942         ((struct ipath_filedata *)(fp)->private_data)->tidcursor
943 #define user_sdma_queue_fp(fp) \
944         ((struct ipath_filedata *)(fp)->private_data)->pq
945
946 /*
947  * values for ipath_flags
948  */
949                 /* chip can report link latency (IB 1.2) */
950 #define IPATH_HAS_LINK_LATENCY 0x1
951                 /* The chip is up and initted */
952 #define IPATH_INITTED       0x2
953                 /* set if any user code has set kr_rcvhdrsize */
954 #define IPATH_RCVHDRSZ_SET  0x4
955                 /* The chip is present and valid for accesses */
956 #define IPATH_PRESENT       0x8
957                 /* HT link0 is only 8 bits wide, ignore upper byte crc
958                  * errors, etc. */
959 #define IPATH_8BIT_IN_HT0   0x10
960                 /* HT link1 is only 8 bits wide, ignore upper byte crc
961                  * errors, etc. */
962 #define IPATH_8BIT_IN_HT1   0x20
963                 /* The link is down */
964 #define IPATH_LINKDOWN      0x40
965                 /* The link level is up (0x11) */
966 #define IPATH_LINKINIT      0x80
967                 /* The link is in the armed (0x21) state */
968 #define IPATH_LINKARMED     0x100
969                 /* The link is in the active (0x31) state */
970 #define IPATH_LINKACTIVE    0x200
971                 /* link current state is unknown */
972 #define IPATH_LINKUNK       0x400
973                 /* Write combining flush needed for PIO */
974 #define IPATH_PIO_FLUSH_WC  0x1000
975                 /* DMA Receive tail pointer */
976 #define IPATH_NODMA_RTAIL   0x2000
977                 /* no IB cable, or no device on IB cable */
978 #define IPATH_NOCABLE       0x4000
979                 /* Supports port zero per packet receive interrupts via
980                  * GPIO */
981 #define IPATH_GPIO_INTR     0x8000
982                 /* uses the coded 4byte TID, not 8 byte */
983 #define IPATH_4BYTE_TID     0x10000
984                 /* packet/word counters are 32 bit, else those 4 counters
985                  * are 64bit */
986 #define IPATH_32BITCOUNTERS 0x20000
987                 /* Interrupt register is 64 bits */
988 #define IPATH_INTREG_64     0x40000
989                 /* can miss port0 rx interrupts */
990 #define IPATH_DISABLED      0x80000 /* administratively disabled */
991                 /* Use GPIO interrupts for new counters */
992 #define IPATH_GPIO_ERRINTRS 0x100000
993 #define IPATH_SWAP_PIOBUFS  0x200000
994                 /* Supports Send DMA */
995 #define IPATH_HAS_SEND_DMA  0x400000
996                 /* Supports Send Count (not just word count) in PBC */
997 #define IPATH_HAS_PBC_CNT   0x800000
998                 /* Suppress heartbeat, even if turning off loopback */
999 #define IPATH_NO_HRTBT      0x1000000
1000 #define IPATH_HAS_THRESH_UPDATE 0x4000000
1001 #define IPATH_HAS_MULT_IB_SPEED 0x8000000
1002 #define IPATH_IB_AUTONEG_INPROG 0x10000000
1003 #define IPATH_IB_AUTONEG_FAILED 0x20000000
1004                 /* Linkdown-disable intentionally, Do not attempt to bring up */
1005 #define IPATH_IB_LINK_DISABLED 0x40000000
1006 #define IPATH_IB_FORCE_NOTIFY 0x80000000 /* force notify on next ib change */
1007
1008 /* Bits in GPIO for the added interrupts */
1009 #define IPATH_GPIO_PORT0_BIT 2
1010 #define IPATH_GPIO_RXUVL_BIT 3
1011 #define IPATH_GPIO_OVRUN_BIT 4
1012 #define IPATH_GPIO_LLI_BIT 5
1013 #define IPATH_GPIO_ERRINTR_MASK 0x38
1014
1015 /* portdata flag bit offsets */
1016                 /* waiting for a packet to arrive */
1017 #define IPATH_PORT_WAITING_RCV   2
1018                 /* master has not finished initializing */
1019 #define IPATH_PORT_MASTER_UNINIT 4
1020                 /* waiting for an urgent packet to arrive */
1021 #define IPATH_PORT_WAITING_URG 5
1022
1023 /* free up any allocated data at closes */
1024 void ipath_free_data(struct ipath_portdata *dd);
1025 u32 __iomem *ipath_getpiobuf(struct ipath_devdata *, u32, u32 *);
1026 void ipath_chg_pioavailkernel(struct ipath_devdata *dd, unsigned start,
1027                                 unsigned len, int avail);
1028 void ipath_init_iba6110_funcs(struct ipath_devdata *);
1029 void ipath_get_eeprom_info(struct ipath_devdata *);
1030 int ipath_update_eeprom_log(struct ipath_devdata *dd);
1031 void ipath_inc_eeprom_err(struct ipath_devdata *dd, u32 eidx, u32 incr);
1032 u64 ipath_snap_cntr(struct ipath_devdata *, ipath_creg);
1033 void ipath_disarm_senderrbufs(struct ipath_devdata *);
1034 void ipath_force_pio_avail_update(struct ipath_devdata *);
1035 void signal_ib_event(struct ipath_devdata *dd, enum ib_event_type ev);
1036
1037 /*
1038  * Set LED override, only the two LSBs have "public" meaning, but
1039  * any non-zero value substitutes them for the Link and LinkTrain
1040  * LED states.
1041  */
1042 #define IPATH_LED_PHYS 1 /* Physical (linktraining) GREEN LED */
1043 #define IPATH_LED_LOG 2  /* Logical (link) YELLOW LED */
1044 void ipath_set_led_override(struct ipath_devdata *dd, unsigned int val);
1045
1046 /* send dma routines */
1047 int setup_sdma(struct ipath_devdata *);
1048 void teardown_sdma(struct ipath_devdata *);
1049 void ipath_restart_sdma(struct ipath_devdata *);
1050 void ipath_sdma_intr(struct ipath_devdata *);
1051 int ipath_sdma_verbs_send(struct ipath_devdata *, struct ipath_sge_state *,
1052                           u32, struct ipath_verbs_txreq *);
1053 /* ipath_sdma_lock should be locked before calling this. */
1054 int ipath_sdma_make_progress(struct ipath_devdata *dd);
1055
1056 /* must be called under ipath_sdma_lock */
1057 static inline u16 ipath_sdma_descq_freecnt(const struct ipath_devdata *dd)
1058 {
1059         return dd->ipath_sdma_descq_cnt -
1060                 (dd->ipath_sdma_descq_added - dd->ipath_sdma_descq_removed) -
1061                 1 - dd->ipath_sdma_desc_nreserved;
1062 }
1063
1064 static inline void ipath_sdma_desc_reserve(struct ipath_devdata *dd, u16 cnt)
1065 {
1066         dd->ipath_sdma_desc_nreserved += cnt;
1067 }
1068
1069 static inline void ipath_sdma_desc_unreserve(struct ipath_devdata *dd, u16 cnt)
1070 {
1071         dd->ipath_sdma_desc_nreserved -= cnt;
1072 }
1073
1074 /*
1075  * number of words used for protocol header if not set by ipath_userinit();
1076  */
1077 #define IPATH_DFLT_RCVHDRSIZE 9
1078
1079 int ipath_get_user_pages(unsigned long, size_t, struct page **);
1080 void ipath_release_user_pages(struct page **, size_t);
1081 void ipath_release_user_pages_on_close(struct page **, size_t);
1082 int ipath_eeprom_read(struct ipath_devdata *, u8, void *, int);
1083 int ipath_eeprom_write(struct ipath_devdata *, u8, const void *, int);
1084 int ipath_tempsense_read(struct ipath_devdata *, u8 regnum);
1085 int ipath_tempsense_write(struct ipath_devdata *, u8 regnum, u8 data);
1086
1087 /* these are used for the registers that vary with port */
1088 void ipath_write_kreg_port(const struct ipath_devdata *, ipath_kreg,
1089                            unsigned, u64);
1090
1091 /*
1092  * We could have a single register get/put routine, that takes a group type,
1093  * but this is somewhat clearer and cleaner.  It also gives us some error
1094  * checking.  64 bit register reads should always work, but are inefficient
1095  * on opteron (the northbridge always generates 2 separate HT 32 bit reads),
1096  * so we use kreg32 wherever possible.  User register and counter register
1097  * reads are always 32 bit reads, so only one form of those routines.
1098  */
1099
1100 /*
1101  * At the moment, none of the s-registers are writable, so no
1102  * ipath_write_sreg().
1103  */
1104
1105 /**
1106  * ipath_read_ureg32 - read 32-bit virtualized per-port register
1107  * @dd: device
1108  * @regno: register number
1109  * @port: port number
1110  *
1111  * Return the contents of a register that is virtualized to be per port.
1112  * Returns -1 on errors (not distinguishable from valid contents at
1113  * runtime; we may add a separate error variable at some point).
1114  */
1115 static inline u32 ipath_read_ureg32(const struct ipath_devdata *dd,
1116                                     ipath_ureg regno, int port)
1117 {
1118         if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT))
1119                 return 0;
1120
1121         return readl(regno + (u64 __iomem *)
1122                      (dd->ipath_uregbase +
1123                       (char __iomem *)dd->ipath_kregbase +
1124                       dd->ipath_ureg_align * port));
1125 }
1126
1127 /**
1128  * ipath_write_ureg - write 32-bit virtualized per-port register
1129  * @dd: device
1130  * @regno: register number
1131  * @value: value
1132  * @port: port
1133  *
1134  * Write the contents of a register that is virtualized to be per port.
1135  */
1136 static inline void ipath_write_ureg(const struct ipath_devdata *dd,
1137                                     ipath_ureg regno, u64 value, int port)
1138 {
1139         u64 __iomem *ubase = (u64 __iomem *)
1140                 (dd->ipath_uregbase + (char __iomem *) dd->ipath_kregbase +
1141                  dd->ipath_ureg_align * port);
1142         if (dd->ipath_kregbase)
1143                 writeq(value, &ubase[regno]);
1144 }
1145
1146 static inline u32 ipath_read_kreg32(const struct ipath_devdata *dd,
1147                                     ipath_kreg regno)
1148 {
1149         if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT))
1150                 return -1;
1151         return readl((u32 __iomem *) & dd->ipath_kregbase[regno]);
1152 }
1153
1154 static inline u64 ipath_read_kreg64(const struct ipath_devdata *dd,
1155                                     ipath_kreg regno)
1156 {
1157         if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT))
1158                 return -1;
1159
1160         return readq(&dd->ipath_kregbase[regno]);
1161 }
1162
1163 static inline void ipath_write_kreg(const struct ipath_devdata *dd,
1164                                     ipath_kreg regno, u64 value)
1165 {
1166         if (dd->ipath_kregbase)
1167                 writeq(value, &dd->ipath_kregbase[regno]);
1168 }
1169
1170 static inline u64 ipath_read_creg(const struct ipath_devdata *dd,
1171                                   ipath_sreg regno)
1172 {
1173         if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT))
1174                 return 0;
1175
1176         return readq(regno + (u64 __iomem *)
1177                      (dd->ipath_cregbase +
1178                       (char __iomem *)dd->ipath_kregbase));
1179 }
1180
1181 static inline u32 ipath_read_creg32(const struct ipath_devdata *dd,
1182                                          ipath_sreg regno)
1183 {
1184         if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT))
1185                 return 0;
1186         return readl(regno + (u64 __iomem *)
1187                      (dd->ipath_cregbase +
1188                       (char __iomem *)dd->ipath_kregbase));
1189 }
1190
1191 static inline void ipath_write_creg(const struct ipath_devdata *dd,
1192                                     ipath_creg regno, u64 value)
1193 {
1194         if (dd->ipath_kregbase)
1195                 writeq(value, regno + (u64 __iomem *)
1196                        (dd->ipath_cregbase +
1197                         (char __iomem *)dd->ipath_kregbase));
1198 }
1199
1200 static inline void ipath_clear_rcvhdrtail(const struct ipath_portdata *pd)
1201 {
1202         *((u64 *) pd->port_rcvhdrtail_kvaddr) = 0ULL;
1203 }
1204
1205 static inline u32 ipath_get_rcvhdrtail(const struct ipath_portdata *pd)
1206 {
1207         return (u32) le64_to_cpu(*((volatile __le64 *)
1208                                 pd->port_rcvhdrtail_kvaddr));
1209 }
1210
1211 static inline u32 ipath_get_hdrqtail(const struct ipath_portdata *pd)
1212 {
1213         const struct ipath_devdata *dd = pd->port_dd;
1214         u32 hdrqtail;
1215
1216         if (dd->ipath_flags & IPATH_NODMA_RTAIL) {
1217                 __le32 *rhf_addr;
1218                 u32 seq;
1219
1220                 rhf_addr = (__le32 *) pd->port_rcvhdrq +
1221                         pd->port_head + dd->ipath_rhf_offset;
1222                 seq = ipath_hdrget_seq(rhf_addr);
1223                 hdrqtail = pd->port_head;
1224                 if (seq == pd->port_seq_cnt)
1225                         hdrqtail++;
1226         } else
1227                 hdrqtail = ipath_get_rcvhdrtail(pd);
1228
1229         return hdrqtail;
1230 }
1231
1232 static inline u64 ipath_read_ireg(const struct ipath_devdata *dd, ipath_kreg r)
1233 {
1234         return (dd->ipath_flags & IPATH_INTREG_64) ?
1235                 ipath_read_kreg64(dd, r) : ipath_read_kreg32(dd, r);
1236 }
1237
1238 /*
1239  * from contents of IBCStatus (or a saved copy), return linkstate
1240  * Report ACTIVE_DEFER as ACTIVE, because we treat them the same
1241  * everywhere, anyway (and should be, for almost all purposes).
1242  */
1243 static inline u32 ipath_ib_linkstate(struct ipath_devdata *dd, u64 ibcs)
1244 {
1245         u32 state = (u32)(ibcs >> dd->ibcs_ls_shift) &
1246                 INFINIPATH_IBCS_LINKSTATE_MASK;
1247         if (state == INFINIPATH_IBCS_L_STATE_ACT_DEFER)
1248                 state = INFINIPATH_IBCS_L_STATE_ACTIVE;
1249         return state;
1250 }
1251
1252 /* from contents of IBCStatus (or a saved copy), return linktrainingstate */
1253 static inline u32 ipath_ib_linktrstate(struct ipath_devdata *dd, u64 ibcs)
1254 {
1255         return (u32)(ibcs >> INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
1256                 dd->ibcs_lts_mask;
1257 }
1258
1259 /*
1260  * from contents of IBCStatus (or a saved copy), return logical link state
1261  * combination of link state and linktraining state (down, active, init,
1262  * arm, etc.
1263  */
1264 static inline u32 ipath_ib_state(struct ipath_devdata *dd, u64 ibcs)
1265 {
1266         u32 ibs;
1267         ibs = (u32)(ibcs >> INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
1268                 dd->ibcs_lts_mask;
1269         ibs |= (u32)(ibcs &
1270                 (INFINIPATH_IBCS_LINKSTATE_MASK << dd->ibcs_ls_shift));
1271         return ibs;
1272 }
1273
1274 /*
1275  * sysfs interface.
1276  */
1277
1278 struct device_driver;
1279
1280 extern const char ib_ipath_version[];
1281
1282 extern const struct attribute_group *ipath_driver_attr_groups[];
1283
1284 int ipath_device_create_group(struct device *, struct ipath_devdata *);
1285 void ipath_device_remove_group(struct device *, struct ipath_devdata *);
1286 int ipath_expose_reset(struct device *);
1287
1288 int ipath_init_ipathfs(void);
1289 void ipath_exit_ipathfs(void);
1290 int ipathfs_add_device(struct ipath_devdata *);
1291 int ipathfs_remove_device(struct ipath_devdata *);
1292
1293 /*
1294  * dma_addr wrappers - all 0's invalid for hw
1295  */
1296 dma_addr_t ipath_map_page(struct pci_dev *, struct page *, unsigned long,
1297                           size_t, int);
1298 dma_addr_t ipath_map_single(struct pci_dev *, void *, size_t, int);
1299 const char *ipath_get_unit_name(int unit);
1300
1301 /*
1302  * Flush write combining store buffers (if present) and perform a write
1303  * barrier.
1304  */
1305 #if defined(CONFIG_X86_64)
1306 #define ipath_flush_wc() asm volatile("sfence" ::: "memory")
1307 #else
1308 #define ipath_flush_wc() wmb()
1309 #endif
1310
1311 extern unsigned ipath_debug; /* debugging bit mask */
1312 extern unsigned ipath_linkrecovery;
1313 extern unsigned ipath_mtu4096;
1314 extern struct mutex ipath_mutex;
1315
1316 #define IPATH_DRV_NAME          "ib_ipath"
1317 #define IPATH_MAJOR             233
1318 #define IPATH_USER_MINOR_BASE   0
1319 #define IPATH_DIAGPKT_MINOR     127
1320 #define IPATH_DIAG_MINOR_BASE   129
1321 #define IPATH_NMINORS           255
1322
1323 #define ipath_dev_err(dd,fmt,...) \
1324         do { \
1325                 const struct ipath_devdata *__dd = (dd); \
1326                 if (__dd->pcidev) \
1327                         dev_err(&__dd->pcidev->dev, "%s: " fmt, \
1328                                 ipath_get_unit_name(__dd->ipath_unit), \
1329                                 ##__VA_ARGS__); \
1330                 else \
1331                         printk(KERN_ERR IPATH_DRV_NAME ": %s: " fmt, \
1332                                ipath_get_unit_name(__dd->ipath_unit), \
1333                                ##__VA_ARGS__); \
1334         } while (0)
1335
1336 #if _IPATH_DEBUGGING
1337
1338 # define __IPATH_DBG_WHICH(which,fmt,...) \
1339         do { \
1340                 if (unlikely(ipath_debug & (which))) \
1341                         printk(KERN_DEBUG IPATH_DRV_NAME ": %s: " fmt, \
1342                                __func__,##__VA_ARGS__); \
1343         } while(0)
1344
1345 # define ipath_dbg(fmt,...) \
1346         __IPATH_DBG_WHICH(__IPATH_DBG,fmt,##__VA_ARGS__)
1347 # define ipath_cdbg(which,fmt,...) \
1348         __IPATH_DBG_WHICH(__IPATH_##which##DBG,fmt,##__VA_ARGS__)
1349
1350 #else /* ! _IPATH_DEBUGGING */
1351
1352 # define ipath_dbg(fmt,...)
1353 # define ipath_cdbg(which,fmt,...)
1354
1355 #endif /* _IPATH_DEBUGGING */
1356
1357 /*
1358  * this is used for formatting hw error messages...
1359  */
1360 struct ipath_hwerror_msgs {
1361         u64 mask;
1362         const char *msg;
1363 };
1364
1365 #define INFINIPATH_HWE_MSG(a, b) { .mask = INFINIPATH_HWE_##a, .msg = b }
1366
1367 /* in ipath_intr.c... */
1368 void ipath_format_hwerrors(u64 hwerrs,
1369                            const struct ipath_hwerror_msgs *hwerrmsgs,
1370                            size_t nhwerrmsgs,
1371                            char *msg, size_t lmsg);
1372
1373 #endif                          /* _IPATH_KERNEL_H */