Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[pandora-kernel.git] / drivers / infiniband / hw / ipath / ipath_layer.c
1 /*
2  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 /*
34  * These are the routines used by layered drivers, currently just the
35  * layered ethernet driver and verbs layer.
36  */
37
38 #include <linux/io.h>
39 #include <linux/pci.h>
40 #include <asm/byteorder.h>
41
42 #include "ipath_kernel.h"
43 #include "ips_common.h"
44 #include "ipath_layer.h"
45
46 /* Acquire before ipath_devs_lock. */
47 static DEFINE_MUTEX(ipath_layer_mutex);
48
49 static int ipath_verbs_registered;
50
51 u16 ipath_layer_rcv_opcode;
52
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);
58
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 *);
64
65 int __ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
66 {
67         int ret = -ENODEV;
68
69         if (dd->ipath_layer.l_arg && layer_intr)
70                 ret = layer_intr(dd->ipath_layer.l_arg, arg);
71
72         return ret;
73 }
74
75 int ipath_layer_intr(struct ipath_devdata *dd, u32 arg)
76 {
77         int ret;
78
79         mutex_lock(&ipath_layer_mutex);
80
81         ret = __ipath_layer_intr(dd, arg);
82
83         mutex_unlock(&ipath_layer_mutex);
84
85         return ret;
86 }
87
88 int __ipath_layer_rcv(struct ipath_devdata *dd, void *hdr,
89                       struct sk_buff *skb)
90 {
91         int ret = -ENODEV;
92
93         if (dd->ipath_layer.l_arg && layer_rcv)
94                 ret = layer_rcv(dd->ipath_layer.l_arg, hdr, skb);
95
96         return ret;
97 }
98
99 int __ipath_layer_rcv_lid(struct ipath_devdata *dd, void *hdr)
100 {
101         int ret = -ENODEV;
102
103         if (dd->ipath_layer.l_arg && layer_rcv_lid)
104                 ret = layer_rcv_lid(dd->ipath_layer.l_arg, hdr);
105
106         return ret;
107 }
108
109 int __ipath_verbs_piobufavail(struct ipath_devdata *dd)
110 {
111         int ret = -ENODEV;
112
113         if (dd->verbs_layer.l_arg && verbs_piobufavail)
114                 ret = verbs_piobufavail(dd->verbs_layer.l_arg);
115
116         return ret;
117 }
118
119 int __ipath_verbs_rcv(struct ipath_devdata *dd, void *rc, void *ebuf,
120                       u32 tlen)
121 {
122         int ret = -ENODEV;
123
124         if (dd->verbs_layer.l_arg && verbs_rcv) {
125                 verbs_rcv(dd->verbs_layer.l_arg, rc, ebuf, tlen);
126                 ret = 0;
127         }
128
129         return ret;
130 }
131
132 int ipath_layer_set_linkstate(struct ipath_devdata *dd, u8 newstate)
133 {
134         u32 lstate;
135         int ret;
136
137         switch (newstate) {
138         case IPATH_IB_LINKDOWN:
139                 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_POLL <<
140                                     INFINIPATH_IBCC_LINKINITCMD_SHIFT);
141                 /* don't wait */
142                 ret = 0;
143                 goto bail;
144
145         case IPATH_IB_LINKDOWN_SLEEP:
146                 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_SLEEP <<
147                                     INFINIPATH_IBCC_LINKINITCMD_SHIFT);
148                 /* don't wait */
149                 ret = 0;
150                 goto bail;
151
152         case IPATH_IB_LINKDOWN_DISABLE:
153                 ipath_set_ib_lstate(dd,
154                                     INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
155                                     INFINIPATH_IBCC_LINKINITCMD_SHIFT);
156                 /* don't wait */
157                 ret = 0;
158                 goto bail;
159
160         case IPATH_IB_LINKINIT:
161                 if (dd->ipath_flags & IPATH_LINKINIT) {
162                         ret = 0;
163                         goto bail;
164                 }
165                 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_INIT <<
166                                     INFINIPATH_IBCC_LINKCMD_SHIFT);
167                 lstate = IPATH_LINKINIT;
168                 break;
169
170         case IPATH_IB_LINKARM:
171                 if (dd->ipath_flags & IPATH_LINKARMED) {
172                         ret = 0;
173                         goto bail;
174                 }
175                 if (!(dd->ipath_flags &
176                       (IPATH_LINKINIT | IPATH_LINKACTIVE))) {
177                         ret = -EINVAL;
178                         goto bail;
179                 }
180                 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ARMED <<
181                                     INFINIPATH_IBCC_LINKCMD_SHIFT);
182                 /*
183                  * Since the port can transition to ACTIVE by receiving
184                  * a non VL 15 packet, wait for either state.
185                  */
186                 lstate = IPATH_LINKARMED | IPATH_LINKACTIVE;
187                 break;
188
189         case IPATH_IB_LINKACTIVE:
190                 if (dd->ipath_flags & IPATH_LINKACTIVE) {
191                         ret = 0;
192                         goto bail;
193                 }
194                 if (!(dd->ipath_flags & IPATH_LINKARMED)) {
195                         ret = -EINVAL;
196                         goto bail;
197                 }
198                 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ACTIVE <<
199                                     INFINIPATH_IBCC_LINKCMD_SHIFT);
200                 lstate = IPATH_LINKACTIVE;
201                 break;
202
203         default:
204                 ipath_dbg("Invalid linkstate 0x%x requested\n", newstate);
205                 ret = -EINVAL;
206                 goto bail;
207         }
208         ret = ipath_wait_linkstate(dd, lstate, 2000);
209
210 bail:
211         return ret;
212 }
213
214 EXPORT_SYMBOL_GPL(ipath_layer_set_linkstate);
215
216 /**
217  * ipath_layer_set_mtu - set the MTU
218  * @dd: the infinipath device
219  * @arg: the new MTU
220  *
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...
227  */
228 int ipath_layer_set_mtu(struct ipath_devdata *dd, u16 arg)
229 {
230         u32 piosize;
231         int changed = 0;
232         int ret;
233
234         /*
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.
239          */
240         if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
241             arg != 4096) {
242                 ipath_dbg("Trying to set invalid mtu %u, failing\n", arg);
243                 ret = -EINVAL;
244                 goto bail;
245         }
246         if (dd->ipath_ibmtu == arg) {
247                 ret = 0;        /* same as current */
248                 goto bail;
249         }
250
251         piosize = dd->ipath_ibmaxlen;
252         dd->ipath_ibmtu = arg;
253
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;
258                         changed = 1;
259                 }
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,
264                            arg);
265                 dd->ipath_ibmaxlen = piosize;
266                 changed = 1;
267         }
268
269         if (changed) {
270                 /*
271                  * set the IBC maxpktlength to the size of our pio
272                  * buffers in words
273                  */
274                 u64 ibc = dd->ipath_ibcctrl;
275                 ibc &= ~(INFINIPATH_IBCC_MAXPKTLEN_MASK <<
276                          INFINIPATH_IBCC_MAXPKTLEN_SHIFT);
277
278                 piosize = piosize - 2 * sizeof(u32);    /* ignore pbc */
279                 dd->ipath_ibmaxlen = piosize;
280                 piosize /= sizeof(u32); /* in words */
281                 /*
282                  * for ICRC, which we only send in diag test pkt mode, and
283                  * we don't need to worry about that for mtu
284                  */
285                 piosize += 1;
286
287                 ibc |= piosize << INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
288                 dd->ipath_ibcctrl = ibc;
289                 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
290                                  dd->ipath_ibcctrl);
291                 dd->ipath_f_tidtemplate(dd);
292         }
293
294         ret = 0;
295
296 bail:
297         return ret;
298 }
299
300 EXPORT_SYMBOL_GPL(ipath_layer_set_mtu);
301
302 int ipath_set_sps_lid(struct ipath_devdata *dd, u32 arg, u8 lmc)
303 {
304         ipath_stats.sps_lid[dd->ipath_unit] = arg;
305         dd->ipath_lid = arg;
306         dd->ipath_lmc = lmc;
307
308         mutex_lock(&ipath_layer_mutex);
309
310         if (dd->ipath_layer.l_arg && layer_intr)
311                 layer_intr(dd->ipath_layer.l_arg, IPATH_LAYER_INT_LID);
312
313         mutex_unlock(&ipath_layer_mutex);
314
315         return 0;
316 }
317
318 EXPORT_SYMBOL_GPL(ipath_set_sps_lid);
319
320 int ipath_layer_set_guid(struct ipath_devdata *dd, __be64 guid)
321 {
322         /* XXX - need to inform anyone who cares this just happened. */
323         dd->ipath_guid = guid;
324         return 0;
325 }
326
327 EXPORT_SYMBOL_GPL(ipath_layer_set_guid);
328
329 __be64 ipath_layer_get_guid(struct ipath_devdata *dd)
330 {
331         return dd->ipath_guid;
332 }
333
334 EXPORT_SYMBOL_GPL(ipath_layer_get_guid);
335
336 u32 ipath_layer_get_nguid(struct ipath_devdata *dd)
337 {
338         return dd->ipath_nguid;
339 }
340
341 EXPORT_SYMBOL_GPL(ipath_layer_get_nguid);
342
343 int ipath_layer_query_device(struct ipath_devdata *dd, u32 * vendor,
344                              u32 * boardrev, u32 * majrev, u32 * minrev)
345 {
346         *vendor = dd->ipath_vendorid;
347         *boardrev = dd->ipath_boardrev;
348         *majrev = dd->ipath_majrev;
349         *minrev = dd->ipath_minrev;
350
351         return 0;
352 }
353
354 EXPORT_SYMBOL_GPL(ipath_layer_query_device);
355
356 u32 ipath_layer_get_flags(struct ipath_devdata *dd)
357 {
358         return dd->ipath_flags;
359 }
360
361 EXPORT_SYMBOL_GPL(ipath_layer_get_flags);
362
363 struct device *ipath_layer_get_device(struct ipath_devdata *dd)
364 {
365         return &dd->pcidev->dev;
366 }
367
368 EXPORT_SYMBOL_GPL(ipath_layer_get_device);
369
370 u16 ipath_layer_get_deviceid(struct ipath_devdata *dd)
371 {
372         return dd->ipath_deviceid;
373 }
374
375 EXPORT_SYMBOL_GPL(ipath_layer_get_deviceid);
376
377 u64 ipath_layer_get_lastibcstat(struct ipath_devdata *dd)
378 {
379         return dd->ipath_lastibcstat;
380 }
381
382 EXPORT_SYMBOL_GPL(ipath_layer_get_lastibcstat);
383
384 u32 ipath_layer_get_ibmtu(struct ipath_devdata *dd)
385 {
386         return dd->ipath_ibmtu;
387 }
388
389 EXPORT_SYMBOL_GPL(ipath_layer_get_ibmtu);
390
391 void ipath_layer_add(struct ipath_devdata *dd)
392 {
393         mutex_lock(&ipath_layer_mutex);
394
395         if (layer_add_one)
396                 dd->ipath_layer.l_arg =
397                         layer_add_one(dd->ipath_unit, dd);
398
399         if (verbs_add_one)
400                 dd->verbs_layer.l_arg =
401                         verbs_add_one(dd->ipath_unit, dd);
402
403         mutex_unlock(&ipath_layer_mutex);
404 }
405
406 void ipath_layer_del(struct ipath_devdata *dd)
407 {
408         mutex_lock(&ipath_layer_mutex);
409
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;
413         }
414
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;
418         }
419
420         mutex_unlock(&ipath_layer_mutex);
421 }
422
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 *),
427                          u16 l_rcv_opcode,
428                          int (*l_rcv_lid)(void *, void *))
429 {
430         struct ipath_devdata *dd, *tmp;
431         unsigned long flags;
432
433         mutex_lock(&ipath_layer_mutex);
434
435         layer_add_one = l_add;
436         layer_remove_one = l_remove;
437         layer_intr = l_intr;
438         layer_rcv = l_rcv;
439         layer_rcv_lid = l_rcv_lid;
440         ipath_layer_rcv_opcode = l_rcv_opcode;
441
442         spin_lock_irqsave(&ipath_devs_lock, flags);
443
444         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
445                 if (!(dd->ipath_flags & IPATH_INITTED))
446                         continue;
447
448                 if (dd->ipath_layer.l_arg)
449                         continue;
450
451                 if (!(*dd->ipath_statusp & IPATH_STATUS_SMA))
452                         *dd->ipath_statusp |= IPATH_STATUS_OIB_SMA;
453
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);
457         }
458
459         spin_unlock_irqrestore(&ipath_devs_lock, flags);
460         mutex_unlock(&ipath_layer_mutex);
461
462         return 0;
463 }
464
465 EXPORT_SYMBOL_GPL(ipath_layer_register);
466
467 void ipath_layer_unregister(void)
468 {
469         struct ipath_devdata *dd, *tmp;
470         unsigned long flags;
471
472         mutex_lock(&ipath_layer_mutex);
473         spin_lock_irqsave(&ipath_devs_lock, flags);
474
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;
481                 }
482         }
483
484         spin_unlock_irqrestore(&ipath_devs_lock, flags);
485
486         layer_add_one = NULL;
487         layer_remove_one = NULL;
488         layer_intr = NULL;
489         layer_rcv = NULL;
490         layer_rcv_lid = NULL;
491
492         mutex_unlock(&ipath_layer_mutex);
493 }
494
495 EXPORT_SYMBOL_GPL(ipath_layer_unregister);
496
497 static void __ipath_verbs_timer(unsigned long arg)
498 {
499         struct ipath_devdata *dd = (struct ipath_devdata *) arg;
500
501         /*
502          * If port 0 receive packet interrupts are not available, or
503          * can be missed, poll the receive queue
504          */
505         if (dd->ipath_flags & IPATH_POLL_RX_INTR)
506                 ipath_kreceive(dd);
507
508         /* Handle verbs layer timeouts. */
509         if (dd->verbs_layer.l_arg && verbs_timer_cb)
510                 verbs_timer_cb(dd->verbs_layer.l_arg);
511
512         mod_timer(&dd->verbs_layer.l_timer, jiffies + 1);
513 }
514
515 /**
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
521  */
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))
528 {
529         struct ipath_devdata *dd, *tmp;
530         unsigned long flags;
531
532         mutex_lock(&ipath_layer_mutex);
533
534         verbs_add_one = l_add;
535         verbs_remove_one = l_remove;
536         verbs_piobufavail = l_piobufavail;
537         verbs_rcv = l_rcv;
538         verbs_timer_cb = l_timer_cb;
539
540         spin_lock_irqsave(&ipath_devs_lock, flags);
541
542         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
543                 if (!(dd->ipath_flags & IPATH_INITTED))
544                         continue;
545
546                 if (dd->verbs_layer.l_arg)
547                         continue;
548
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);
552         }
553
554         spin_unlock_irqrestore(&ipath_devs_lock, flags);
555         mutex_unlock(&ipath_layer_mutex);
556
557         ipath_verbs_registered = 1;
558
559         return 0;
560 }
561
562 EXPORT_SYMBOL_GPL(ipath_verbs_register);
563
564 void ipath_verbs_unregister(void)
565 {
566         struct ipath_devdata *dd, *tmp;
567         unsigned long flags;
568
569         mutex_lock(&ipath_layer_mutex);
570         spin_lock_irqsave(&ipath_devs_lock, flags);
571
572         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
573                 *dd->ipath_statusp &= ~IPATH_STATUS_OIB_SMA;
574
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;
580                 }
581         }
582
583         spin_unlock_irqrestore(&ipath_devs_lock, flags);
584
585         verbs_add_one = NULL;
586         verbs_remove_one = NULL;
587         verbs_piobufavail = NULL;
588         verbs_rcv = NULL;
589         verbs_timer_cb = NULL;
590
591         ipath_verbs_registered = 0;
592
593         mutex_unlock(&ipath_layer_mutex);
594 }
595
596 EXPORT_SYMBOL_GPL(ipath_verbs_unregister);
597
598 int ipath_layer_open(struct ipath_devdata *dd, u32 * pktmax)
599 {
600         int ret;
601         u32 intval = 0;
602
603         mutex_lock(&ipath_layer_mutex);
604
605         if (!dd->ipath_layer.l_arg) {
606                 ret = -EINVAL;
607                 goto bail;
608         }
609
610         ret = ipath_setrcvhdrsize(dd, NUM_OF_EXTRA_WORDS_IN_HEADER_QUEUE);
611
612         if (ret < 0)
613                 goto bail;
614
615         *pktmax = dd->ipath_ibmaxlen;
616
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;
623         /*
624          * do this on open, in case low level is already up and
625          * just layered driver was reloaded, etc.
626          */
627         if (intval)
628                 layer_intr(dd->ipath_layer.l_arg, intval);
629
630         ret = 0;
631 bail:
632         mutex_unlock(&ipath_layer_mutex);
633
634         return ret;
635 }
636
637 EXPORT_SYMBOL_GPL(ipath_layer_open);
638
639 u16 ipath_layer_get_lid(struct ipath_devdata *dd)
640 {
641         return dd->ipath_lid;
642 }
643
644 EXPORT_SYMBOL_GPL(ipath_layer_get_lid);
645
646 /**
647  * ipath_layer_get_mac - get the MAC address
648  * @dd: the infinipath device
649  * @mac: the MAC is put here
650  *
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.
657  */
658
659 int ipath_layer_get_mac(struct ipath_devdata *dd, u8 * mac)
660 {
661         u8 *guid;
662
663         guid = (u8 *) &dd->ipath_guid;
664
665         mac[0] = guid[0];
666         mac[1] = guid[1];
667         mac[2] = guid[2];
668         mac[3] = guid[5];
669         mac[4] = guid[6];
670         mac[5] = guid[7];
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]);
674         return 0;
675 }
676
677 EXPORT_SYMBOL_GPL(ipath_layer_get_mac);
678
679 u16 ipath_layer_get_bcast(struct ipath_devdata *dd)
680 {
681         return dd->ipath_mlid;
682 }
683
684 EXPORT_SYMBOL_GPL(ipath_layer_get_bcast);
685
686 u32 ipath_layer_get_cr_errpkey(struct ipath_devdata *dd)
687 {
688         return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
689 }
690
691 EXPORT_SYMBOL_GPL(ipath_layer_get_cr_errpkey);
692
693 static void update_sge(struct ipath_sge_state *ss, u32 length)
694 {
695         struct ipath_sge *sge = &ss->sge;
696
697         sge->vaddr += length;
698         sge->length -= length;
699         sge->sge_length -= length;
700         if (sge->sge_length == 0) {
701                 if (--ss->num_sge)
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)
706                                 return;
707                         sge->n = 0;
708                 }
709                 sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
710                 sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
711         }
712 }
713
714 #ifdef __LITTLE_ENDIAN
715 static inline u32 get_upper_bits(u32 data, u32 shift)
716 {
717         return data >> shift;
718 }
719
720 static inline u32 set_upper_bits(u32 data, u32 shift)
721 {
722         return data << shift;
723 }
724
725 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
726 {
727         data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
728         data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
729         return data;
730 }
731 #else
732 static inline u32 get_upper_bits(u32 data, u32 shift)
733 {
734         return data << shift;
735 }
736
737 static inline u32 set_upper_bits(u32 data, u32 shift)
738 {
739         return data >> shift;
740 }
741
742 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
743 {
744         data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
745         data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
746         return data;
747 }
748 #endif
749
750 static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
751                     u32 length)
752 {
753         u32 extra = 0;
754         u32 data = 0;
755         u32 last;
756
757         while (1) {
758                 u32 len = ss->sge.length;
759                 u32 off;
760
761                 BUG_ON(len == 0);
762                 if (len > length)
763                         len = 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);
768                 if (off) {
769                         u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
770                                             ~(sizeof(u32) - 1));
771                         u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
772                         u32 y;
773
774                         y = sizeof(u32) - off;
775                         if (len > y)
776                                 len = y;
777                         if (len + extra >= sizeof(u32)) {
778                                 data |= set_upper_bits(v, extra *
779                                                        BITS_PER_BYTE);
780                                 len = sizeof(u32) - extra;
781                                 if (len == length) {
782                                         last = data;
783                                         break;
784                                 }
785                                 __raw_writel(data, piobuf);
786                                 piobuf++;
787                                 extra = 0;
788                                 data = 0;
789                         } else {
790                                 /* Clear unused upper bytes */
791                                 data |= clear_upper_bytes(v, len, extra);
792                                 if (len == length) {
793                                         last = data;
794                                         break;
795                                 }
796                                 extra += len;
797                         }
798                 } else if (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;
803                         u32 l = len;
804
805                         while (l >= sizeof(u32)) {
806                                 u32 v = *addr;
807
808                                 data |= set_upper_bits(v, shift);
809                                 __raw_writel(data, piobuf);
810                                 data = get_upper_bits(v, ushift);
811                                 piobuf++;
812                                 addr++;
813                                 l -= sizeof(u32);
814                         }
815                         /*
816                          * We still have 'extra' number of bytes leftover.
817                          */
818                         if (l) {
819                                 u32 v = *addr;
820
821                                 if (l + extra >= sizeof(u32)) {
822                                         data |= set_upper_bits(v, shift);
823                                         len -= l + extra - sizeof(u32);
824                                         if (len == length) {
825                                                 last = data;
826                                                 break;
827                                         }
828                                         __raw_writel(data, piobuf);
829                                         piobuf++;
830                                         extra = 0;
831                                         data = 0;
832                                 } else {
833                                         /* Clear unused upper bytes */
834                                         data |= clear_upper_bytes(v, l,
835                                                                   extra);
836                                         if (len == length) {
837                                                 last = data;
838                                                 break;
839                                         }
840                                         extra += l;
841                                 }
842                         } else if (len == length) {
843                                 last = data;
844                                 break;
845                         }
846                 } else if (len == length) {
847                         u32 w;
848
849                         /*
850                          * Need to round up for the last dword in the
851                          * packet.
852                          */
853                         w = (len + 3) >> 2;
854                         __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
855                         piobuf += w - 1;
856                         last = ((u32 *) ss->sge.vaddr)[w - 1];
857                         break;
858                 } else {
859                         u32 w = len >> 2;
860
861                         __iowrite32_copy(piobuf, ss->sge.vaddr, w);
862                         piobuf += w;
863
864                         extra = len & (sizeof(u32) - 1);
865                         if (extra) {
866                                 u32 v = ((u32 *) ss->sge.vaddr)[w];
867
868                                 /* Clear unused upper bytes */
869                                 data = clear_upper_bytes(v, extra, 0);
870                         }
871                 }
872                 update_sge(ss, len);
873                 length -= len;
874         }
875         /* must flush early everything before trigger word */
876         ipath_flush_wc();
877         __raw_writel(last, piobuf);
878         /* be sure trigger word is written */
879         ipath_flush_wc();
880         update_sge(ss, length);
881 }
882
883 /**
884  * ipath_verbs_send - send a packet from the verbs layer
885  * @dd: the infinipath device
886  * @hdrwords: the number of works in the header
887  * @hdr: the packet header
888  * @len: the length of the packet in bytes
889  * @ss: the SGE to send
890  *
891  * This is like ipath_sma_send_pkt() in that we need to be able to send
892  * packets after the chip is initialized (MADs) but also like
893  * ipath_layer_send_hdr() since its used by the verbs layer.
894  */
895 int ipath_verbs_send(struct ipath_devdata *dd, u32 hdrwords,
896                      u32 *hdr, u32 len, struct ipath_sge_state *ss)
897 {
898         u32 __iomem *piobuf;
899         u32 plen;
900         int ret;
901
902         /* +1 is for the qword padding of pbc */
903         plen = hdrwords + ((len + 3) >> 2) + 1;
904         if (unlikely((plen << 2) > dd->ipath_ibmaxlen)) {
905                 ipath_dbg("packet len 0x%x too long, failing\n", plen);
906                 ret = -EINVAL;
907                 goto bail;
908         }
909
910         /* Get a PIO buffer to use. */
911         piobuf = ipath_getpiobuf(dd, NULL);
912         if (unlikely(piobuf == NULL)) {
913                 ret = -EBUSY;
914                 goto bail;
915         }
916
917         /*
918          * Write len to control qword, no flags.
919          * We have to flush after the PBC for correctness on some cpus
920          * or WC buffer can be written out of order.
921          */
922         writeq(plen, piobuf);
923         ipath_flush_wc();
924         piobuf += 2;
925         if (len == 0) {
926                 /*
927                  * If there is just the header portion, must flush before
928                  * writing last word of header for correctness, and after
929                  * the last header word (trigger word).
930                  */
931                 __iowrite32_copy(piobuf, hdr, hdrwords - 1);
932                 ipath_flush_wc();
933                 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
934                 ipath_flush_wc();
935                 ret = 0;
936                 goto bail;
937         }
938
939         __iowrite32_copy(piobuf, hdr, hdrwords);
940         piobuf += hdrwords;
941
942         /* The common case is aligned and contained in one segment. */
943         if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
944                    !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
945                 u32 w;
946
947                 /* Need to round up for the last dword in the packet. */
948                 w = (len + 3) >> 2;
949                 __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
950                 /* must flush early everything before trigger word */
951                 ipath_flush_wc();
952                 __raw_writel(((u32 *) ss->sge.vaddr)[w - 1],
953                              piobuf + w - 1);
954                 /* be sure trigger word is written */
955                 ipath_flush_wc();
956                 update_sge(ss, len);
957                 ret = 0;
958                 goto bail;
959         }
960         copy_io(piobuf, ss, len);
961         ret = 0;
962
963 bail:
964         return ret;
965 }
966
967 EXPORT_SYMBOL_GPL(ipath_verbs_send);
968
969 int ipath_layer_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
970                                   u64 *rwords, u64 *spkts, u64 *rpkts,
971                                   u64 *xmit_wait)
972 {
973         int ret;
974
975         if (!(dd->ipath_flags & IPATH_INITTED)) {
976                 /* no hardware, freeze, etc. */
977                 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
978                 ret = -EINVAL;
979                 goto bail;
980         }
981         *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
982         *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
983         *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
984         *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
985         *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
986
987         ret = 0;
988
989 bail:
990         return ret;
991 }
992
993 EXPORT_SYMBOL_GPL(ipath_layer_snapshot_counters);
994
995 /**
996  * ipath_layer_get_counters - get various chip counters
997  * @dd: the infinipath device
998  * @cntrs: counters are placed here
999  *
1000  * Return the counters needed by recv_pma_get_portcounters().
1001  */
1002 int ipath_layer_get_counters(struct ipath_devdata *dd,
1003                               struct ipath_layer_counters *cntrs)
1004 {
1005         int ret;
1006
1007         if (!(dd->ipath_flags & IPATH_INITTED)) {
1008                 /* no hardware, freeze, etc. */
1009                 ipath_dbg("unit %u not usable\n", dd->ipath_unit);
1010                 ret = -EINVAL;
1011                 goto bail;
1012         }
1013         cntrs->symbol_error_counter =
1014                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_ibsymbolerrcnt);
1015         cntrs->link_error_recovery_counter =
1016                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkerrrecovcnt);
1017         cntrs->link_downed_counter =
1018                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_iblinkdowncnt);
1019         cntrs->port_rcv_errors =
1020                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rxdroppktcnt) +
1021                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvovflcnt) +
1022                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_portovflcnt) +
1023                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errrcvflowctrlcnt) +
1024                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_err_rlencnt) +
1025                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_invalidrlencnt) +
1026                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_erricrccnt) +
1027                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errvcrccnt) +
1028                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlpcrccnt) +
1029                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_errlinkcnt) +
1030                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_badformatcnt);
1031         cntrs->port_rcv_remphys_errors =
1032                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_rcvebpcnt);
1033         cntrs->port_xmit_discards =
1034                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_unsupvlcnt);
1035         cntrs->port_xmit_data =
1036                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
1037         cntrs->port_rcv_data =
1038                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
1039         cntrs->port_xmit_packets =
1040                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
1041         cntrs->port_rcv_packets =
1042                 ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
1043
1044         ret = 0;
1045
1046 bail:
1047         return ret;
1048 }
1049
1050 EXPORT_SYMBOL_GPL(ipath_layer_get_counters);
1051
1052 int ipath_layer_want_buffer(struct ipath_devdata *dd)
1053 {
1054         set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
1055         ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1056                          dd->ipath_sendctrl);
1057
1058         return 0;
1059 }
1060
1061 EXPORT_SYMBOL_GPL(ipath_layer_want_buffer);
1062
1063 int ipath_layer_send_hdr(struct ipath_devdata *dd, struct ether_header *hdr)
1064 {
1065         int ret = 0;
1066         u32 __iomem *piobuf;
1067         u32 plen, *uhdr;
1068         size_t count;
1069         __be16 vlsllnh;
1070
1071         if (!(dd->ipath_flags & IPATH_RCVHDRSZ_SET)) {
1072                 ipath_dbg("send while not open\n");
1073                 ret = -EINVAL;
1074         } else
1075                 if ((dd->ipath_flags & (IPATH_LINKUNK | IPATH_LINKDOWN)) ||
1076                     dd->ipath_lid == 0) {
1077                         /*
1078                          * lid check is for when sma hasn't yet configured
1079                          */
1080                         ret = -ENETDOWN;
1081                         ipath_cdbg(VERBOSE, "send while not ready, "
1082                                    "mylid=%u, flags=0x%x\n",
1083                                    dd->ipath_lid, dd->ipath_flags);
1084                 }
1085
1086         vlsllnh = *((__be16 *) hdr);
1087         if (vlsllnh != htons(IPS_LRH_BTH)) {
1088                 ipath_dbg("Warning: lrh[0] wrong (%x, not %x); "
1089                           "not sending\n", be16_to_cpu(vlsllnh),
1090                           IPS_LRH_BTH);
1091                 ret = -EINVAL;
1092         }
1093         if (ret)
1094                 goto done;
1095
1096         /* Get a PIO buffer to use. */
1097         piobuf = ipath_getpiobuf(dd, NULL);
1098         if (piobuf == NULL) {
1099                 ret = -EBUSY;
1100                 goto done;
1101         }
1102
1103         plen = (sizeof(*hdr) >> 2); /* actual length */
1104         ipath_cdbg(EPKT, "0x%x+1w pio %p\n", plen, piobuf);
1105
1106         writeq(plen+1, piobuf); /* len (+1 for pad) to pbc, no flags */
1107         ipath_flush_wc();
1108         piobuf += 2;
1109         uhdr = (u32 *)hdr;
1110         count = plen-1; /* amount we can copy before trigger word */
1111         __iowrite32_copy(piobuf, uhdr, count);
1112         ipath_flush_wc();
1113         __raw_writel(uhdr[count], piobuf + count);
1114         ipath_flush_wc(); /* ensure it's sent, now */
1115
1116         ipath_stats.sps_ether_spkts++;  /* ether packet sent */
1117
1118 done:
1119         return ret;
1120 }
1121
1122 EXPORT_SYMBOL_GPL(ipath_layer_send_hdr);
1123
1124 int ipath_layer_set_piointbufavail_int(struct ipath_devdata *dd)
1125 {
1126         set_bit(IPATH_S_PIOINTBUFAVAIL, &dd->ipath_sendctrl);
1127
1128         ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1129                          dd->ipath_sendctrl);
1130         return 0;
1131 }
1132
1133 EXPORT_SYMBOL_GPL(ipath_layer_set_piointbufavail_int);
1134
1135 int ipath_layer_enable_timer(struct ipath_devdata *dd)
1136 {
1137         /*
1138          * HT-400 has a design flaw where the chip and kernel idea
1139          * of the tail register don't always agree, and therefore we won't
1140          * get an interrupt on the next packet received.
1141          * If the board supports per packet receive interrupts, use it.
1142          * Otherwise, the timer function periodically checks for packets
1143          * to cover this case.
1144          * Either way, the timer is needed for verbs layer related
1145          * processing.
1146          */
1147         if (dd->ipath_flags & IPATH_GPIO_INTR) {
1148                 ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
1149                                  0x2074076542310ULL);
1150                 /* Enable GPIO bit 2 interrupt */
1151                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
1152                                  (u64) (1 << 2));
1153         }
1154
1155         init_timer(&dd->verbs_layer.l_timer);
1156         dd->verbs_layer.l_timer.function = __ipath_verbs_timer;
1157         dd->verbs_layer.l_timer.data = (unsigned long)dd;
1158         dd->verbs_layer.l_timer.expires = jiffies + 1;
1159         add_timer(&dd->verbs_layer.l_timer);
1160
1161         return 0;
1162 }
1163
1164 EXPORT_SYMBOL_GPL(ipath_layer_enable_timer);
1165
1166 int ipath_layer_disable_timer(struct ipath_devdata *dd)
1167 {
1168         /* Disable GPIO bit 2 interrupt */
1169         if (dd->ipath_flags & IPATH_GPIO_INTR)
1170                 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask, 0);
1171
1172         del_timer_sync(&dd->verbs_layer.l_timer);
1173
1174         return 0;
1175 }
1176
1177 EXPORT_SYMBOL_GPL(ipath_layer_disable_timer);
1178
1179 /**
1180  * ipath_layer_set_verbs_flags - set the verbs layer flags
1181  * @dd: the infinipath device
1182  * @flags: the flags to set
1183  */
1184 int ipath_layer_set_verbs_flags(struct ipath_devdata *dd, unsigned flags)
1185 {
1186         struct ipath_devdata *ss;
1187         unsigned long lflags;
1188
1189         spin_lock_irqsave(&ipath_devs_lock, lflags);
1190
1191         list_for_each_entry(ss, &ipath_dev_list, ipath_list) {
1192                 if (!(ss->ipath_flags & IPATH_INITTED))
1193                         continue;
1194                 if ((flags & IPATH_VERBS_KERNEL_SMA) &&
1195                     !(*ss->ipath_statusp & IPATH_STATUS_SMA))
1196                         *ss->ipath_statusp |= IPATH_STATUS_OIB_SMA;
1197                 else
1198                         *ss->ipath_statusp &= ~IPATH_STATUS_OIB_SMA;
1199         }
1200
1201         spin_unlock_irqrestore(&ipath_devs_lock, lflags);
1202
1203         return 0;
1204 }
1205
1206 EXPORT_SYMBOL_GPL(ipath_layer_set_verbs_flags);
1207
1208 /**
1209  * ipath_layer_get_npkeys - return the size of the PKEY table for port 0
1210  * @dd: the infinipath device
1211  */
1212 unsigned ipath_layer_get_npkeys(struct ipath_devdata *dd)
1213 {
1214         return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
1215 }
1216
1217 EXPORT_SYMBOL_GPL(ipath_layer_get_npkeys);
1218
1219 /**
1220  * ipath_layer_get_pkey - return the indexed PKEY from the port 0 PKEY table
1221  * @dd: the infinipath device
1222  * @index: the PKEY index
1223  */
1224 unsigned ipath_layer_get_pkey(struct ipath_devdata *dd, unsigned index)
1225 {
1226         unsigned ret;
1227
1228         if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
1229                 ret = 0;
1230         else
1231                 ret = dd->ipath_pd[0]->port_pkeys[index];
1232
1233         return ret;
1234 }
1235
1236 EXPORT_SYMBOL_GPL(ipath_layer_get_pkey);
1237
1238 /**
1239  * ipath_layer_get_pkeys - return the PKEY table for port 0
1240  * @dd: the infinipath device
1241  * @pkeys: the pkey table is placed here
1242  */
1243 int ipath_layer_get_pkeys(struct ipath_devdata *dd, u16 * pkeys)
1244 {
1245         struct ipath_portdata *pd = dd->ipath_pd[0];
1246
1247         memcpy(pkeys, pd->port_pkeys, sizeof(pd->port_pkeys));
1248
1249         return 0;
1250 }
1251
1252 EXPORT_SYMBOL_GPL(ipath_layer_get_pkeys);
1253
1254 /**
1255  * rm_pkey - decrecment the reference count for the given PKEY
1256  * @dd: the infinipath device
1257  * @key: the PKEY index
1258  *
1259  * Return true if this was the last reference and the hardware table entry
1260  * needs to be changed.
1261  */
1262 static int rm_pkey(struct ipath_devdata *dd, u16 key)
1263 {
1264         int i;
1265         int ret;
1266
1267         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1268                 if (dd->ipath_pkeys[i] != key)
1269                         continue;
1270                 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[i])) {
1271                         dd->ipath_pkeys[i] = 0;
1272                         ret = 1;
1273                         goto bail;
1274                 }
1275                 break;
1276         }
1277
1278         ret = 0;
1279
1280 bail:
1281         return ret;
1282 }
1283
1284 /**
1285  * add_pkey - add the given PKEY to the hardware table
1286  * @dd: the infinipath device
1287  * @key: the PKEY
1288  *
1289  * Return an error code if unable to add the entry, zero if no change,
1290  * or 1 if the hardware PKEY register needs to be updated.
1291  */
1292 static int add_pkey(struct ipath_devdata *dd, u16 key)
1293 {
1294         int i;
1295         u16 lkey = key & 0x7FFF;
1296         int any = 0;
1297         int ret;
1298
1299         if (lkey == 0x7FFF) {
1300                 ret = 0;
1301                 goto bail;
1302         }
1303
1304         /* Look for an empty slot or a matching PKEY. */
1305         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1306                 if (!dd->ipath_pkeys[i]) {
1307                         any++;
1308                         continue;
1309                 }
1310                 /* If it matches exactly, try to increment the ref count */
1311                 if (dd->ipath_pkeys[i] == key) {
1312                         if (atomic_inc_return(&dd->ipath_pkeyrefs[i]) > 1) {
1313                                 ret = 0;
1314                                 goto bail;
1315                         }
1316                         /* Lost the race. Look for an empty slot below. */
1317                         atomic_dec(&dd->ipath_pkeyrefs[i]);
1318                         any++;
1319                 }
1320                 /*
1321                  * It makes no sense to have both the limited and unlimited
1322                  * PKEY set at the same time since the unlimited one will
1323                  * disable the limited one.
1324                  */
1325                 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
1326                         ret = -EEXIST;
1327                         goto bail;
1328                 }
1329         }
1330         if (!any) {
1331                 ret = -EBUSY;
1332                 goto bail;
1333         }
1334         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
1335                 if (!dd->ipath_pkeys[i] &&
1336                     atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
1337                         /* for ipathstats, etc. */
1338                         ipath_stats.sps_pkeys[i] = lkey;
1339                         dd->ipath_pkeys[i] = key;
1340                         ret = 1;
1341                         goto bail;
1342                 }
1343         }
1344         ret = -EBUSY;
1345
1346 bail:
1347         return ret;
1348 }
1349
1350 /**
1351  * ipath_layer_set_pkeys - set the PKEY table for port 0
1352  * @dd: the infinipath device
1353  * @pkeys: the PKEY table
1354  */
1355 int ipath_layer_set_pkeys(struct ipath_devdata *dd, u16 * pkeys)
1356 {
1357         struct ipath_portdata *pd;
1358         int i;
1359         int changed = 0;
1360
1361         pd = dd->ipath_pd[0];
1362
1363         for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
1364                 u16 key = pkeys[i];
1365                 u16 okey = pd->port_pkeys[i];
1366
1367                 if (key == okey)
1368                         continue;
1369                 /*
1370                  * The value of this PKEY table entry is changing.
1371                  * Remove the old entry in the hardware's array of PKEYs.
1372                  */
1373                 if (okey & 0x7FFF)
1374                         changed |= rm_pkey(dd, okey);
1375                 if (key & 0x7FFF) {
1376                         int ret = add_pkey(dd, key);
1377
1378                         if (ret < 0)
1379                                 key = 0;
1380                         else
1381                                 changed |= ret;
1382                 }
1383                 pd->port_pkeys[i] = key;
1384         }
1385         if (changed) {
1386                 u64 pkey;
1387
1388                 pkey = (u64) dd->ipath_pkeys[0] |
1389                         ((u64) dd->ipath_pkeys[1] << 16) |
1390                         ((u64) dd->ipath_pkeys[2] << 32) |
1391                         ((u64) dd->ipath_pkeys[3] << 48);
1392                 ipath_cdbg(VERBOSE, "p0 new pkey reg %llx\n",
1393                            (unsigned long long) pkey);
1394                 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
1395                                  pkey);
1396         }
1397         return 0;
1398 }
1399
1400 EXPORT_SYMBOL_GPL(ipath_layer_set_pkeys);
1401
1402 /**
1403  * ipath_layer_get_linkdowndefaultstate - get the default linkdown state
1404  * @dd: the infinipath device
1405  *
1406  * Returns zero if the default is POLL, 1 if the default is SLEEP.
1407  */
1408 int ipath_layer_get_linkdowndefaultstate(struct ipath_devdata *dd)
1409 {
1410         return !!(dd->ipath_ibcctrl & INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE);
1411 }
1412
1413 EXPORT_SYMBOL_GPL(ipath_layer_get_linkdowndefaultstate);
1414
1415 /**
1416  * ipath_layer_set_linkdowndefaultstate - set the default linkdown state
1417  * @dd: the infinipath device
1418  * @sleep: the new state
1419  *
1420  * Note that this will only take effect when the link state changes.
1421  */
1422 int ipath_layer_set_linkdowndefaultstate(struct ipath_devdata *dd,
1423                                          int sleep)
1424 {
1425         if (sleep)
1426                 dd->ipath_ibcctrl |= INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
1427         else
1428                 dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
1429         ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1430                          dd->ipath_ibcctrl);
1431         return 0;
1432 }
1433
1434 EXPORT_SYMBOL_GPL(ipath_layer_set_linkdowndefaultstate);
1435
1436 int ipath_layer_get_phyerrthreshold(struct ipath_devdata *dd)
1437 {
1438         return (dd->ipath_ibcctrl >>
1439                 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
1440                 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
1441 }
1442
1443 EXPORT_SYMBOL_GPL(ipath_layer_get_phyerrthreshold);
1444
1445 /**
1446  * ipath_layer_set_phyerrthreshold - set the physical error threshold
1447  * @dd: the infinipath device
1448  * @n: the new threshold
1449  *
1450  * Note that this will only take effect when the link state changes.
1451  */
1452 int ipath_layer_set_phyerrthreshold(struct ipath_devdata *dd, unsigned n)
1453 {
1454         unsigned v;
1455
1456         v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
1457                 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
1458         if (v != n) {
1459                 dd->ipath_ibcctrl &=
1460                         ~(INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK <<
1461                           INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT);
1462                 dd->ipath_ibcctrl |=
1463                         (u64) n << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
1464                 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1465                                  dd->ipath_ibcctrl);
1466         }
1467         return 0;
1468 }
1469
1470 EXPORT_SYMBOL_GPL(ipath_layer_set_phyerrthreshold);
1471
1472 int ipath_layer_get_overrunthreshold(struct ipath_devdata *dd)
1473 {
1474         return (dd->ipath_ibcctrl >>
1475                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
1476                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
1477 }
1478
1479 EXPORT_SYMBOL_GPL(ipath_layer_get_overrunthreshold);
1480
1481 /**
1482  * ipath_layer_set_overrunthreshold - set the overrun threshold
1483  * @dd: the infinipath device
1484  * @n: the new threshold
1485  *
1486  * Note that this will only take effect when the link state changes.
1487  */
1488 int ipath_layer_set_overrunthreshold(struct ipath_devdata *dd, unsigned n)
1489 {
1490         unsigned v;
1491
1492         v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
1493                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
1494         if (v != n) {
1495                 dd->ipath_ibcctrl &=
1496                         ~(INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK <<
1497                           INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT);
1498                 dd->ipath_ibcctrl |=
1499                         (u64) n << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
1500                 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1501                                  dd->ipath_ibcctrl);
1502         }
1503         return 0;
1504 }
1505
1506 EXPORT_SYMBOL_GPL(ipath_layer_set_overrunthreshold);
1507
1508 int ipath_layer_get_boardname(struct ipath_devdata *dd, char *name,
1509                               size_t namelen)
1510 {
1511         return dd->ipath_f_get_boardname(dd, name, namelen);
1512 }
1513 EXPORT_SYMBOL_GPL(ipath_layer_get_boardname);
1514
1515 u32 ipath_layer_get_rcvhdrentsize(struct ipath_devdata *dd)
1516 {
1517         return dd->ipath_rcvhdrentsize;
1518 }
1519 EXPORT_SYMBOL_GPL(ipath_layer_get_rcvhdrentsize);