2 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * These are the routines used by layered drivers, currently just the
35 * layered ethernet driver and verbs layer.
39 #include <linux/pci.h>
40 #include <asm/byteorder.h>
42 #include "ipath_kernel.h"
43 #include "ips_common.h"
44 #include "ipath_layer.h"
46 /* Acquire before ipath_devs_lock. */
47 static DEFINE_MUTEX(ipath_layer_mutex);
49 static int ipath_verbs_registered;
51 u16 ipath_layer_rcv_opcode;
53 static int (*layer_intr)(void *, u32);
54 static int (*layer_rcv)(void *, void *, struct sk_buff *);
55 static int (*layer_rcv_lid)(void *, void *);
56 static int (*verbs_piobufavail)(void *);
57 static void (*verbs_rcv)(void *, void *, void *, u32);
59 static void *(*layer_add_one)(int, struct ipath_devdata *);
60 static void (*layer_remove_one)(void *);
61 static void *(*verbs_add_one)(int, struct ipath_devdata *);
62 static void (*verbs_remove_one)(void *);
63 static void (*verbs_timer_cb)(void *);
65 int __ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
69 if (dd->ipath_layer.l_arg && layer_intr)
70 ret = layer_intr(dd->ipath_layer.l_arg, arg);
75 int ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
79 mutex_lock(&ipath_layer_mutex);
81 ret = __ipath_layer_intr(dd, arg);
83 mutex_unlock(&ipath_layer_mutex);
88 int __ipath_layer_rcv(struct ipath_devdata *dd, void *hdr,
93 if (dd->ipath_layer.l_arg && layer_rcv)
94 ret = layer_rcv(dd->ipath_layer.l_arg, hdr, skb);
99 int __ipath_layer_rcv_lid(struct ipath_devdata *dd, void *hdr)
103 if (dd->ipath_layer.l_arg && layer_rcv_lid)
104 ret = layer_rcv_lid(dd->ipath_layer.l_arg, hdr);
109 int __ipath_verbs_piobufavail(struct ipath_devdata *dd)
113 if (dd->verbs_layer.l_arg && verbs_piobufavail)
114 ret = verbs_piobufavail(dd->verbs_layer.l_arg);
119 int __ipath_verbs_rcv(struct ipath_devdata *dd, void *rc, void *ebuf,
124 if (dd->verbs_layer.l_arg && verbs_rcv) {
125 verbs_rcv(dd->verbs_layer.l_arg, rc, ebuf, tlen);
132 int ipath_layer_set_linkstate(struct ipath_devdata *dd, u8 newstate)
138 case IPATH_IB_LINKDOWN:
139 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_POLL <<
140 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
145 case IPATH_IB_LINKDOWN_SLEEP:
146 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_SLEEP <<
147 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
152 case IPATH_IB_LINKDOWN_DISABLE:
153 ipath_set_ib_lstate(dd,
154 INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
155 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
160 case IPATH_IB_LINKINIT:
161 if (dd->ipath_flags & IPATH_LINKINIT) {
165 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_INIT <<
166 INFINIPATH_IBCC_LINKCMD_SHIFT);
167 lstate = IPATH_LINKINIT;
170 case IPATH_IB_LINKARM:
171 if (dd->ipath_flags & IPATH_LINKARMED) {
175 if (!(dd->ipath_flags &
176 (IPATH_LINKINIT | IPATH_LINKACTIVE))) {
180 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ARMED <<
181 INFINIPATH_IBCC_LINKCMD_SHIFT);
183 * Since the port can transition to ACTIVE by receiving
184 * a non VL 15 packet, wait for either state.
186 lstate = IPATH_LINKARMED | IPATH_LINKACTIVE;
189 case IPATH_IB_LINKACTIVE:
190 if (dd->ipath_flags & IPATH_LINKACTIVE) {
194 if (!(dd->ipath_flags & IPATH_LINKARMED)) {
198 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ACTIVE <<
199 INFINIPATH_IBCC_LINKCMD_SHIFT);
200 lstate = IPATH_LINKACTIVE;
204 ipath_dbg("Invalid linkstate 0x%x requested\n", newstate);
208 ret = ipath_wait_linkstate(dd, lstate, 2000);
214 EXPORT_SYMBOL_GPL(ipath_layer_set_linkstate);
217 * ipath_layer_set_mtu - set the MTU
218 * @dd: the infinipath device
221 * we can handle "any" incoming size, the issue here is whether we
222 * need to restrict our outgoing size. For now, we don't do any
223 * sanity checking on this, and we don't deal with what happens to
224 * programs that are already running when the size changes.
225 * NOTE: changing the MTU will usually cause the IBC to go back to
226 * link initialize (IPATH_IBSTATE_INIT) state...
228 int ipath_layer_set_mtu(struct ipath_devdata *dd, u16 arg)
235 * mtu is IB data payload max. It's the largest power of 2 less
236 * than piosize (or even larger, since it only really controls the
237 * largest we can receive; we can send the max of the mtu and
238 * piosize). We check that it's one of the valid IB sizes.
240 if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
242 ipath_dbg("Trying to set invalid mtu %u, failing\n", arg);
246 if (dd->ipath_ibmtu == arg) {
247 ret = 0; /* same as current */
251 piosize = dd->ipath_ibmaxlen;
252 dd->ipath_ibmtu = arg;
254 if (arg >= (piosize - IPATH_PIO_MAXIBHDR)) {
255 /* Only if it's not the initial value (or reset to it) */
256 if (piosize != dd->ipath_init_ibmaxlen) {
257 dd->ipath_ibmaxlen = piosize;
260 } else if ((arg + IPATH_PIO_MAXIBHDR) != dd->ipath_ibmaxlen) {
261 piosize = arg + IPATH_PIO_MAXIBHDR;
262 ipath_cdbg(VERBOSE, "ibmaxlen was 0x%x, setting to 0x%x "
263 "(mtu 0x%x)\n", dd->ipath_ibmaxlen, piosize,
265 dd->ipath_ibmaxlen = piosize;
271 * set the IBC maxpktlength to the size of our pio
274 u64 ibc = dd->ipath_ibcctrl;
275 ibc &= ~(INFINIPATH_IBCC_MAXPKTLEN_MASK <<
276 INFINIPATH_IBCC_MAXPKTLEN_SHIFT);
278 piosize = piosize - 2 * sizeof(u32); /* ignore pbc */
279 dd->ipath_ibmaxlen = piosize;
280 piosize /= sizeof(u32); /* in words */
282 * for ICRC, which we only send in diag test pkt mode, and
283 * we don't need to worry about that for mtu
287 ibc |= piosize << INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
288 dd->ipath_ibcctrl = ibc;
289 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
291 dd->ipath_f_tidtemplate(dd);
300 EXPORT_SYMBOL_GPL(ipath_layer_set_mtu);
302 int ipath_set_sps_lid(struct ipath_devdata *dd, u32 arg, u8 lmc)
304 ipath_stats.sps_lid[dd->ipath_unit] = arg;
308 mutex_lock(&ipath_layer_mutex);
310 if (dd->ipath_layer.l_arg && layer_intr)
311 layer_intr(dd->ipath_layer.l_arg, IPATH_LAYER_INT_LID);
313 mutex_unlock(&ipath_layer_mutex);
318 EXPORT_SYMBOL_GPL(ipath_set_sps_lid);
320 int ipath_layer_set_guid(struct ipath_devdata *dd, __be64 guid)
322 /* XXX - need to inform anyone who cares this just happened. */
323 dd->ipath_guid = guid;
327 EXPORT_SYMBOL_GPL(ipath_layer_set_guid);
329 __be64 ipath_layer_get_guid(struct ipath_devdata *dd)
331 return dd->ipath_guid;
334 EXPORT_SYMBOL_GPL(ipath_layer_get_guid);
336 u32 ipath_layer_get_nguid(struct ipath_devdata *dd)
338 return dd->ipath_nguid;
341 EXPORT_SYMBOL_GPL(ipath_layer_get_nguid);
343 int ipath_layer_query_device(struct ipath_devdata *dd, u32 * vendor,
344 u32 * boardrev, u32 * majrev, u32 * minrev)
346 *vendor = dd->ipath_vendorid;
347 *boardrev = dd->ipath_boardrev;
348 *majrev = dd->ipath_majrev;
349 *minrev = dd->ipath_minrev;
354 EXPORT_SYMBOL_GPL(ipath_layer_query_device);
356 u32 ipath_layer_get_flags(struct ipath_devdata *dd)
358 return dd->ipath_flags;
361 EXPORT_SYMBOL_GPL(ipath_layer_get_flags);
363 struct device *ipath_layer_get_device(struct ipath_devdata *dd)
365 return &dd->pcidev->dev;
368 EXPORT_SYMBOL_GPL(ipath_layer_get_device);
370 u16 ipath_layer_get_deviceid(struct ipath_devdata *dd)
372 return dd->ipath_deviceid;
375 EXPORT_SYMBOL_GPL(ipath_layer_get_deviceid);
377 u64 ipath_layer_get_lastibcstat(struct ipath_devdata *dd)
379 return dd->ipath_lastibcstat;
382 EXPORT_SYMBOL_GPL(ipath_layer_get_lastibcstat);
384 u32 ipath_layer_get_ibmtu(struct ipath_devdata *dd)
386 return dd->ipath_ibmtu;
389 EXPORT_SYMBOL_GPL(ipath_layer_get_ibmtu);
391 void ipath_layer_add(struct ipath_devdata *dd)
393 mutex_lock(&ipath_layer_mutex);
396 dd->ipath_layer.l_arg =
397 layer_add_one(dd->ipath_unit, dd);
400 dd->verbs_layer.l_arg =
401 verbs_add_one(dd->ipath_unit, dd);
403 mutex_unlock(&ipath_layer_mutex);
406 void ipath_layer_del(struct ipath_devdata *dd)
408 mutex_lock(&ipath_layer_mutex);
410 if (dd->ipath_layer.l_arg && layer_remove_one) {
411 layer_remove_one(dd->ipath_layer.l_arg);
412 dd->ipath_layer.l_arg = NULL;
415 if (dd->verbs_layer.l_arg && verbs_remove_one) {
416 verbs_remove_one(dd->verbs_layer.l_arg);
417 dd->verbs_layer.l_arg = NULL;
420 mutex_unlock(&ipath_layer_mutex);
423 int ipath_layer_register(void *(*l_add)(int, struct ipath_devdata *),
424 void (*l_remove)(void *),
425 int (*l_intr)(void *, u32),
426 int (*l_rcv)(void *, void *, struct sk_buff *),
428 int (*l_rcv_lid)(void *, void *))
430 struct ipath_devdata *dd, *tmp;
433 mutex_lock(&ipath_layer_mutex);
435 layer_add_one = l_add;
436 layer_remove_one = l_remove;
439 layer_rcv_lid = l_rcv_lid;
440 ipath_layer_rcv_opcode = l_rcv_opcode;
442 spin_lock_irqsave(&ipath_devs_lock, flags);
444 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
445 if (!(dd->ipath_flags & IPATH_INITTED))
448 if (dd->ipath_layer.l_arg)
451 if (!(*dd->ipath_statusp & IPATH_STATUS_SMA))
452 *dd->ipath_statusp |= IPATH_STATUS_OIB_SMA;
454 spin_unlock_irqrestore(&ipath_devs_lock, flags);
455 dd->ipath_layer.l_arg = l_add(dd->ipath_unit, dd);
456 spin_lock_irqsave(&ipath_devs_lock, flags);
459 spin_unlock_irqrestore(&ipath_devs_lock, flags);
460 mutex_unlock(&ipath_layer_mutex);
465 EXPORT_SYMBOL_GPL(ipath_layer_register);
467 void ipath_layer_unregister(void)
469 struct ipath_devdata *dd, *tmp;
472 mutex_lock(&ipath_layer_mutex);
473 spin_lock_irqsave(&ipath_devs_lock, flags);
475 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
476 if (dd->ipath_layer.l_arg && layer_remove_one) {
477 spin_unlock_irqrestore(&ipath_devs_lock, flags);
478 layer_remove_one(dd->ipath_layer.l_arg);
479 spin_lock_irqsave(&ipath_devs_lock, flags);
480 dd->ipath_layer.l_arg = NULL;
484 spin_unlock_irqrestore(&ipath_devs_lock, flags);
486 layer_add_one = NULL;
487 layer_remove_one = NULL;
490 layer_rcv_lid = NULL;
492 mutex_unlock(&ipath_layer_mutex);
495 EXPORT_SYMBOL_GPL(ipath_layer_unregister);
497 static void __ipath_verbs_timer(unsigned long arg)
499 struct ipath_devdata *dd = (struct ipath_devdata *) arg;
502 * If port 0 receive packet interrupts are not available, or
503 * can be missed, poll the receive queue
505 if (dd->ipath_flags & IPATH_POLL_RX_INTR)
508 /* Handle verbs layer timeouts. */
509 if (dd->verbs_layer.l_arg && verbs_timer_cb)
510 verbs_timer_cb(dd->verbs_layer.l_arg);
512 mod_timer(&dd->verbs_layer.l_timer, jiffies + 1);
516 * ipath_verbs_register - verbs layer registration
517 * @l_piobufavail: callback for when PIO buffers become available
518 * @l_rcv: callback for receiving a packet
519 * @l_timer_cb: timer callback
520 * @ipath_devdata: device data structure is put here
522 int ipath_verbs_register(void *(*l_add)(int, struct ipath_devdata *),
523 void (*l_remove)(void *arg),
524 int (*l_piobufavail) (void *arg),
525 void (*l_rcv) (void *arg, void *rhdr,
526 void *data, u32 tlen),
527 void (*l_timer_cb) (void *arg))
529 struct ipath_devdata *dd, *tmp;
532 mutex_lock(&ipath_layer_mutex);
534 verbs_add_one = l_add;
535 verbs_remove_one = l_remove;
536 verbs_piobufavail = l_piobufavail;
538 verbs_timer_cb = l_timer_cb;
540 spin_lock_irqsave(&ipath_devs_lock, flags);
542 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
543 if (!(dd->ipath_flags & IPATH_INITTED))
546 if (dd->verbs_layer.l_arg)
549 spin_unlock_irqrestore(&ipath_devs_lock, flags);
550 dd->verbs_layer.l_arg = l_add(dd->ipath_unit, dd);
551 spin_lock_irqsave(&ipath_devs_lock, flags);
554 spin_unlock_irqrestore(&ipath_devs_lock, flags);
555 mutex_unlock(&ipath_layer_mutex);
557 ipath_verbs_registered = 1;
562 EXPORT_SYMBOL_GPL(ipath_verbs_register);
564 void ipath_verbs_unregister(void)
566 struct ipath_devdata *dd, *tmp;
569 mutex_lock(&ipath_layer_mutex);
570 spin_lock_irqsave(&ipath_devs_lock, flags);
572 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
573 *dd->ipath_statusp &= ~IPATH_STATUS_OIB_SMA;
575 if (dd->verbs_layer.l_arg && verbs_remove_one) {
576 spin_unlock_irqrestore(&ipath_devs_lock, flags);
577 verbs_remove_one(dd->verbs_layer.l_arg);
578 spin_lock_irqsave(&ipath_devs_lock, flags);
579 dd->verbs_layer.l_arg = NULL;
583 spin_unlock_irqrestore(&ipath_devs_lock, flags);
585 verbs_add_one = NULL;
586 verbs_remove_one = NULL;
587 verbs_piobufavail = NULL;
589 verbs_timer_cb = NULL;
591 ipath_verbs_registered = 0;
593 mutex_unlock(&ipath_layer_mutex);
596 EXPORT_SYMBOL_GPL(ipath_verbs_unregister);
598 int ipath_layer_open(struct ipath_devdata *dd, u32 * pktmax)
603 mutex_lock(&ipath_layer_mutex);
605 if (!dd->ipath_layer.l_arg) {
610 ret = ipath_setrcvhdrsize(dd, NUM_OF_EXTRA_WORDS_IN_HEADER_QUEUE);
615 *pktmax = dd->ipath_ibmaxlen;
617 if (*dd->ipath_statusp & IPATH_STATUS_IB_READY)
618 intval |= IPATH_LAYER_INT_IF_UP;
619 if (ipath_stats.sps_lid[dd->ipath_unit])
620 intval |= IPATH_LAYER_INT_LID;
621 if (ipath_stats.sps_mlid[dd->ipath_unit])
622 intval |= IPATH_LAYER_INT_BCAST;
624 * do this on open, in case low level is already up and
625 * just layered driver was reloaded, etc.
628 layer_intr(dd->ipath_layer.l_arg, intval);
632 mutex_unlock(&ipath_layer_mutex);
637 EXPORT_SYMBOL_GPL(ipath_layer_open);
639 u16 ipath_layer_get_lid(struct ipath_devdata *dd)
641 return dd->ipath_lid;
644 EXPORT_SYMBOL_GPL(ipath_layer_get_lid);
647 * ipath_layer_get_mac - get the MAC address
648 * @dd: the infinipath device
649 * @mac: the MAC is put here
651 * This is the EUID-64 OUI octets (top 3), then
652 * skip the next 2 (which should both be zero or 0xff).
653 * The returned MAC is in network order
654 * mac points to at least 6 bytes of buffer
655 * We assume that by the time the LID is set, that the GUID is as valid
656 * as it's ever going to be, rather than adding yet another status bit.
659 int ipath_layer_get_mac(struct ipath_devdata *dd, u8 * mac)
663 guid = (u8 *) &dd->ipath_guid;
671 if ((guid[3] || guid[4]) && !(guid[3] == 0xff && guid[4] == 0xff))
672 ipath_dbg("Warning, guid bytes 3 and 4 not 0 or 0xffff: "
673 "%x %x\n", guid[3], guid[4]);
677 EXPORT_SYMBOL_GPL(ipath_layer_get_mac);
679 u16 ipath_layer_get_bcast(struct ipath_devdata *dd)
681 return dd->ipath_mlid;
684 EXPORT_SYMBOL_GPL(ipath_layer_get_bcast);
686 u32 ipath_layer_get_cr_errpkey(struct ipath_devdata *dd)
688 return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
691 EXPORT_SYMBOL_GPL(ipath_layer_get_cr_errpkey);
693 static void update_sge(struct ipath_sge_state *ss, u32 length)
695 struct ipath_sge *sge = &ss->sge;
697 sge->vaddr += length;
698 sge->length -= length;
699 sge->sge_length -= length;
700 if (sge->sge_length == 0) {
702 *sge = *ss->sg_list++;
703 } else if (sge->length == 0 && sge->mr != NULL) {
704 if (++sge->n >= IPATH_SEGSZ) {
705 if (++sge->m >= sge->mr->mapsz)
709 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
710 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
714 #ifdef __LITTLE_ENDIAN
715 static inline u32 get_upper_bits(u32 data, u32 shift)
717 return data >> shift;
720 static inline u32 set_upper_bits(u32 data, u32 shift)
722 return data << shift;
725 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
727 data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
728 data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
732 static inline u32 get_upper_bits(u32 data, u32 shift)
734 return data << shift;
737 static inline u32 set_upper_bits(u32 data, u32 shift)
739 return data >> shift;
742 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
744 data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
745 data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
750 static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
758 u32 len = ss->sge.length;
764 if (len > ss->sge.sge_length)
765 len = ss->sge.sge_length;
766 /* If the source address is not aligned, try to align it. */
767 off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
769 u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
771 u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
774 y = sizeof(u32) - off;
777 if (len + extra >= sizeof(u32)) {
778 data |= set_upper_bits(v, extra *
780 len = sizeof(u32) - extra;
785 __raw_writel(data, piobuf);
790 /* Clear unused upper bytes */
791 data |= clear_upper_bytes(v, len, extra);
799 /* Source address is aligned. */
800 u32 *addr = (u32 *) ss->sge.vaddr;
801 int shift = extra * BITS_PER_BYTE;
802 int ushift = 32 - shift;
805 while (l >= sizeof(u32)) {
808 data |= set_upper_bits(v, shift);
809 __raw_writel(data, piobuf);
810 data = get_upper_bits(v, ushift);
816 * We still have 'extra' number of bytes leftover.
821 if (l + extra >= sizeof(u32)) {
822 data |= set_upper_bits(v, shift);
823 len -= l + extra - sizeof(u32);
828 __raw_writel(data, piobuf);
833 /* Clear unused upper bytes */
834 data |= clear_upper_bytes(v, l,
842 } else if (len == length) {
846 } else if (len == length) {
850 * Need to round up for the last dword in the
854 __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
856 last = ((u32 *) ss->sge.vaddr)[w - 1];
861 __iowrite32_copy(piobuf, ss->sge.vaddr, w);
864 extra = len & (sizeof(u32) - 1);
866 u32 v = ((u32 *) ss->sge.vaddr)[w];
868 /* Clear unused upper bytes */
869 data = clear_upper_bytes(v, extra, 0);
875 /* Update address before sending packet. */
876 update_sge(ss, length);
877 /* must flush early everything before trigger word */
879 __raw_writel(last, piobuf);
880 /* be sure trigger word is written */
885 * ipath_verbs_send - send a packet from the verbs layer
886 * @dd: the infinipath device
887 * @hdrwords: the number of works in the header
888 * @hdr: the packet header
889 * @len: the length of the packet in bytes
890 * @ss: the SGE to send
892 * This is like ipath_sma_send_pkt() in that we need to be able to send
893 * packets after the chip is initialized (MADs) but also like
894 * ipath_layer_send_hdr() since its used by the verbs layer.
896 int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
897 u32 *hdr, u32 len, struct ipath_sge_state *ss)
903 /* +1 is for the qword padding of pbc */
904 plen = hdrwords + ((len + 3) >> 2) + 1;
905 if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
906 ipath_dbg("packet len 0x%x too long, failing\n", plen);
911 /* Get a PIO buffer to use. */
912 piobuf = ipath_getpiobuf(dd, NULL);
913 if (unlikely(piobuf == NULL)) {
919 * Write len to control qword, no flags.
920 * We have to flush after the PBC for correctness on some cpus
921 * or WC buffer can be written out of order.
923 writeq(plen, piobuf);
928 * If there is just the header portion, must flush before
929 * writing last word of header for correctness, and after
930 * the last header word (trigger word).
932 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
934 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
940 __iowrite32_copy(piobuf, hdr, hdrwords);
943 /* The common case is aligned and contained in one segment. */
944 if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
945 !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
947 u32 *addr = (u32 *) ss->sge.vaddr;
949 /* Update address before sending packet. */
951 /* Need to round up for the last dword in the packet. */
953 __iowrite32_copy(piobuf, addr, w - 1);
954 /* must flush early everything before trigger word */
956 __raw_writel(addr[w - 1], piobuf + w - 1);
957 /* be sure trigger word is written */
962 copy_io(piobuf, ss, len);
969 EXPORT_SYMBOL_GPL(ipath_verbs_send);
971 int ipath_layer_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
972 u64 *rwords, u64 *spkts, u64 *rpkts,
977 if (!(dd->ipath_flags & IPATH_INITTED)) {
978 /* no hardware, freeze, etc. */
979 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
983 *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
984 *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
985 *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
986 *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
987 *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
995 EXPORT_SYMBOL_GPL(ipath_layer_snapshot_counters);
998 * ipath_layer_get_counters - get various chip counters
999 * @dd: the infinipath device
1000 * @cntrs: counters are placed here
1002 * Return the counters needed by recv_pma_get_portcounters().
1004 int ipath_layer_get_counters(struct ipath_devdata *dd,
1005 struct ipath_layer_counters *cntrs)
1009 if (!(dd->ipath_flags & IPATH_INITTED)) {
1010 /* no hardware, freeze, etc. */
1011 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
1015 cntrs->symbol_error_counter =
1016 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
1017 cntrs->link_error_recovery_counter =
1018 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
1019 cntrs->link_downed_counter =
1020 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
1021 cntrs->port_rcv_errors =
1022 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
1023 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
1024 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
1025 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errrcvflowctrlcnt) +
1026 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
1027 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
1028 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
1029 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
1030 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
1031 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlinkcnt) +
1032 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt);
1033 cntrs->port_rcv_remphys_errors =
1034 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
1035 cntrs->port_xmit_discards =
1036 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
1037 cntrs->port_xmit_data =
1038 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
1039 cntrs->port_rcv_data =
1040 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
1041 cntrs->port_xmit_packets =
1042 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
1043 cntrs->port_rcv_packets =
1044 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
1052 EXPORT_SYMBOL_GPL(ipath_layer_get_counters);
1054 int ipath_layer_want_buffer(struct ipath_devdata *dd)
1056 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
1057 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1058 dd->ipath_sendctrl);
1063 EXPORT_SYMBOL_GPL(ipath_layer_want_buffer);
1065 int ipath_layer_send_hdr(struct ipath_devdata *dd, struct ether_header *hdr)
1068 u32 __iomem *piobuf;
1073 if (!(dd->ipath_flags & IPATH_RCVHDRSZ_SET)) {
1074 ipath_dbg("send while not open\n");
1077 if ((dd->ipath_flags & (IPATH_LINKUNK | IPATH_LINKDOWN)) ||
1078 dd->ipath_lid == 0) {
1080 * lid check is for when sma hasn't yet configured
1083 ipath_cdbg(VERBOSE, "send while not ready, "
1084 "mylid=%u, flags=0x%x\n",
1085 dd->ipath_lid, dd->ipath_flags);
1088 vlsllnh = *((__be16 *) hdr);
1089 if (vlsllnh != htons(IPS_LRH_BTH)) {
1090 ipath_dbg("Warning: lrh[0] wrong (%x, not %x); "
1091 "not sending\n", be16_to_cpu(vlsllnh),
1098 /* Get a PIO buffer to use. */
1099 piobuf = ipath_getpiobuf(dd, NULL);
1100 if (piobuf == NULL) {
1105 plen = (sizeof(*hdr) >> 2); /* actual length */
1106 ipath_cdbg(EPKT, "0x%x+1w pio %p\n", plen, piobuf);
1108 writeq(plen+1, piobuf); /* len (+1 for pad) to pbc, no flags */
1112 count = plen-1; /* amount we can copy before trigger word */
1113 __iowrite32_copy(piobuf, uhdr, count);
1115 __raw_writel(uhdr[count], piobuf + count);
1116 ipath_flush_wc(); /* ensure it's sent, now */
1118 ipath_stats.sps_ether_spkts++; /* ether packet sent */
1124 EXPORT_SYMBOL_GPL(ipath_layer_send_hdr);
1126 int ipath_layer_set_piointbufavail_int(struct ipath_devdata *dd)
1128 set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
1130 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1131 dd->ipath_sendctrl);
1135 EXPORT_SYMBOL_GPL(ipath_layer_set_piointbufavail_int);
1137 int ipath_layer_enable_timer(struct ipath_devdata *dd)
1140 * HT-400 has a design flaw where the chip and kernel idea
1141 * of the tail register don't always agree, and therefore we won't
1142 * get an interrupt on the next packet received.
1143 * If the board supports per packet receive interrupts, use it.
1144 * Otherwise, the timer function periodically checks for packets
1145 * to cover this case.
1146 * Either way, the timer is needed for verbs layer related
1149 if (dd->ipath_flags & IPATH_GPIO_INTR) {
1150 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1151 0x2074076542310ULL);
1152 /* Enable GPIO bit 2 interrupt */
1153 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1157 init_timer(&dd->verbs_layer.l_timer);
1158 dd->verbs_layer.l_timer.function = __ipath_verbs_timer;
1159 dd->verbs_layer.l_timer.data = (unsigned long)dd;
1160 dd->verbs_layer.l_timer.expires = jiffies + 1;
1161 add_timer(&dd->verbs_layer.l_timer);
1166 EXPORT_SYMBOL_GPL(ipath_layer_enable_timer);
1168 int ipath_layer_disable_timer(struct ipath_devdata *dd)
1170 /* Disable GPIO bit 2 interrupt */
1171 if (dd->ipath_flags & IPATH_GPIO_INTR)
1172 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask, 0);
1174 del_timer_sync(&dd->verbs_layer.l_timer);
1179 EXPORT_SYMBOL_GPL(ipath_layer_disable_timer);
1182 * ipath_layer_set_verbs_flags - set the verbs layer flags
1183 * @dd: the infinipath device
1184 * @flags: the flags to set
1186 int ipath_layer_set_verbs_flags(struct ipath_devdata *dd, unsigned flags)
1188 struct ipath_devdata *ss;
1189 unsigned long lflags;
1191 spin_lock_irqsave(&ipath_devs_lock, lflags);
1193 list_for_each_entry(ss, &ipath_dev_list, ipath_list) {
1194 if (!(ss->ipath_flags & IPATH_INITTED))
1196 if ((flags & IPATH_VERBS_KERNEL_SMA) &&
1197 !(*ss->ipath_statusp & IPATH_STATUS_SMA))
1198 *ss->ipath_statusp |= IPATH_STATUS_OIB_SMA;
1200 *ss->ipath_statusp &= ~IPATH_STATUS_OIB_SMA;
1203 spin_unlock_irqrestore(&ipath_devs_lock, lflags);
1208 EXPORT_SYMBOL_GPL(ipath_layer_set_verbs_flags);
1211 * ipath_layer_get_npkeys - return the size of the PKEY table for port 0
1212 * @dd: the infinipath device
1214 unsigned ipath_layer_get_npkeys(struct ipath_devdata *dd)
1216 return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1219 EXPORT_SYMBOL_GPL(ipath_layer_get_npkeys);
1222 * ipath_layer_get_pkey - return the indexed PKEY from the port 0 PKEY table
1223 * @dd: the infinipath device
1224 * @index: the PKEY index
1226 unsigned ipath_layer_get_pkey(struct ipath_devdata *dd, unsigned index)
1230 if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1233 ret = dd->ipath_pd[0]->port_pkeys[index];
1238 EXPORT_SYMBOL_GPL(ipath_layer_get_pkey);
1241 * ipath_layer_get_pkeys - return the PKEY table for port 0
1242 * @dd: the infinipath device
1243 * @pkeys: the pkey table is placed here
1245 int ipath_layer_get_pkeys(struct ipath_devdata *dd, u16 * pkeys)
1247 struct ipath_portdata *pd = dd->ipath_pd[0];
1249 memcpy(pkeys, pd->port_pkeys, sizeof(pd->port_pkeys));
1254 EXPORT_SYMBOL_GPL(ipath_layer_get_pkeys);
1257 * rm_pkey - decrecment the reference count for the given PKEY
1258 * @dd: the infinipath device
1259 * @key: the PKEY index
1261 * Return true if this was the last reference and the hardware table entry
1262 * needs to be changed.
1264 static int rm_pkey(struct ipath_devdata *dd, u16 key)
1269 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1270 if (dd->ipath_pkeys[i] != key)
1272 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[i])) {
1273 dd->ipath_pkeys[i] = 0;
1287 * add_pkey - add the given PKEY to the hardware table
1288 * @dd: the infinipath device
1291 * Return an error code if unable to add the entry, zero if no change,
1292 * or 1 if the hardware PKEY register needs to be updated.
1294 static int add_pkey(struct ipath_devdata *dd, u16 key)
1297 u16 lkey = key & 0x7FFF;
1301 if (lkey == 0x7FFF) {
1306 /* Look for an empty slot or a matching PKEY. */
1307 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1308 if (!dd->ipath_pkeys[i]) {
1312 /* If it matches exactly, try to increment the ref count */
1313 if (dd->ipath_pkeys[i] == key) {
1314 if (atomic_inc_return(&dd->ipath_pkeyrefs[i]) > 1) {
1318 /* Lost the race. Look for an empty slot below. */
1319 atomic_dec(&dd->ipath_pkeyrefs[i]);
1323 * It makes no sense to have both the limited and unlimited
1324 * PKEY set at the same time since the unlimited one will
1325 * disable the limited one.
1327 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
1336 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1337 if (!dd->ipath_pkeys[i] &&
1338 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
1339 /* for ipathstats, etc. */
1340 ipath_stats.sps_pkeys[i] = lkey;
1341 dd->ipath_pkeys[i] = key;
1353 * ipath_layer_set_pkeys - set the PKEY table for port 0
1354 * @dd: the infinipath device
1355 * @pkeys: the PKEY table
1357 int ipath_layer_set_pkeys(struct ipath_devdata *dd, u16 * pkeys)
1359 struct ipath_portdata *pd;
1363 pd = dd->ipath_pd[0];
1365 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
1367 u16 okey = pd->port_pkeys[i];
1372 * The value of this PKEY table entry is changing.
1373 * Remove the old entry in the hardware's array of PKEYs.
1376 changed |= rm_pkey(dd, okey);
1378 int ret = add_pkey(dd, key);
1385 pd->port_pkeys[i] = key;
1390 pkey = (u64) dd->ipath_pkeys[0] |
1391 ((u64) dd->ipath_pkeys[1] << 16) |
1392 ((u64) dd->ipath_pkeys[2] << 32) |
1393 ((u64) dd->ipath_pkeys[3] << 48);
1394 ipath_cdbg(VERBOSE, "p0 new pkey reg %llx\n",
1395 (unsigned long long) pkey);
1396 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
1402 EXPORT_SYMBOL_GPL(ipath_layer_set_pkeys);
1405 * ipath_layer_get_linkdowndefaultstate - get the default linkdown state
1406 * @dd: the infinipath device
1408 * Returns zero if the default is POLL, 1 if the default is SLEEP.
1410 int ipath_layer_get_linkdowndefaultstate(struct ipath_devdata *dd)
1412 return !!(dd->ipath_ibcctrl & INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE);
1415 EXPORT_SYMBOL_GPL(ipath_layer_get_linkdowndefaultstate);
1418 * ipath_layer_set_linkdowndefaultstate - set the default linkdown state
1419 * @dd: the infinipath device
1420 * @sleep: the new state
1422 * Note that this will only take effect when the link state changes.
1424 int ipath_layer_set_linkdowndefaultstate(struct ipath_devdata *dd,
1428 dd->ipath_ibcctrl |= INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
1430 dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
1431 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1436 EXPORT_SYMBOL_GPL(ipath_layer_set_linkdowndefaultstate);
1438 int ipath_layer_get_phyerrthreshold(struct ipath_devdata *dd)
1440 return (dd->ipath_ibcctrl >>
1441 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
1442 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
1445 EXPORT_SYMBOL_GPL(ipath_layer_get_phyerrthreshold);
1448 * ipath_layer_set_phyerrthreshold - set the physical error threshold
1449 * @dd: the infinipath device
1450 * @n: the new threshold
1452 * Note that this will only take effect when the link state changes.
1454 int ipath_layer_set_phyerrthreshold(struct ipath_devdata *dd, unsigned n)
1458 v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
1459 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
1461 dd->ipath_ibcctrl &=
1462 ~(INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK <<
1463 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT);
1464 dd->ipath_ibcctrl |=
1465 (u64) n << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
1466 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1472 EXPORT_SYMBOL_GPL(ipath_layer_set_phyerrthreshold);
1474 int ipath_layer_get_overrunthreshold(struct ipath_devdata *dd)
1476 return (dd->ipath_ibcctrl >>
1477 INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
1478 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
1481 EXPORT_SYMBOL_GPL(ipath_layer_get_overrunthreshold);
1484 * ipath_layer_set_overrunthreshold - set the overrun threshold
1485 * @dd: the infinipath device
1486 * @n: the new threshold
1488 * Note that this will only take effect when the link state changes.
1490 int ipath_layer_set_overrunthreshold(struct ipath_devdata *dd, unsigned n)
1494 v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
1495 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
1497 dd->ipath_ibcctrl &=
1498 ~(INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK <<
1499 INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT);
1500 dd->ipath_ibcctrl |=
1501 (u64) n << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
1502 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1508 EXPORT_SYMBOL_GPL(ipath_layer_set_overrunthreshold);
1510 int ipath_layer_get_boardname(struct ipath_devdata *dd, char *name,
1513 return dd->ipath_f_get_boardname(dd, name, namelen);
1515 EXPORT_SYMBOL_GPL(ipath_layer_get_boardname);
1517 u32 ipath_layer_get_rcvhdrentsize(struct ipath_devdata *dd)
1519 return dd->ipath_rcvhdrentsize;
1521 EXPORT_SYMBOL_GPL(ipath_layer_get_rcvhdrentsize);