]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_ib.c
Upgrade Unbound to 1.8.0. More to follow.
[FreeBSD/FreeBSD.git] / sys / ofed / drivers / infiniband / ulp / ipoib / ipoib_ib.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
7  * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include "ipoib.h"
42
43 #include <rdma/ib_cache.h>
44
45 #include <security/mac/mac_framework.h>
46
47 #include <linux/delay.h>
48 #include <linux/dma-mapping.h>
49
50 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA
51 static int data_debug_level;
52
53 module_param(data_debug_level, int, 0644);
54 MODULE_PARM_DESC(data_debug_level,
55                  "Enable data path debug tracing if > 0");
56 #endif
57
58 static DEFINE_MUTEX(pkey_mutex);
59
60 struct ipoib_ah *ipoib_create_ah(struct ipoib_dev_priv *priv,
61                                  struct ib_pd *pd, struct ib_ah_attr *attr)
62 {
63         struct ipoib_ah *ah;
64
65         ah = kmalloc(sizeof *ah, GFP_KERNEL);
66         if (!ah)
67                 return NULL;
68
69         ah->priv      = priv;
70         ah->last_send = 0;
71         kref_init(&ah->ref);
72
73         ah->ah = ib_create_ah(pd, attr);
74         if (IS_ERR(ah->ah)) {
75                 kfree(ah);
76                 ah = NULL;
77         } else
78                 ipoib_dbg(priv, "Created ah %p\n", ah->ah);
79
80         return ah;
81 }
82
83 void ipoib_free_ah(struct kref *kref)
84 {
85         struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);
86         struct ipoib_dev_priv *priv = ah->priv;
87
88         unsigned long flags;
89
90         spin_lock_irqsave(&priv->lock, flags);
91         list_add_tail(&ah->list, &priv->dead_ahs);
92         spin_unlock_irqrestore(&priv->lock, flags);
93 }
94
95 void
96 ipoib_dma_unmap_rx(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req)
97 {
98         struct mbuf *m;
99         int i;
100
101         for (i = 0, m = rx_req->mb; m != NULL; m = m->m_next, i++)
102                 ib_dma_unmap_single(priv->ca, rx_req->mapping[i], m->m_len,
103                     DMA_FROM_DEVICE);
104 }
105
106 void
107 ipoib_dma_mb(struct ipoib_dev_priv *priv, struct mbuf *mb, unsigned int length)
108 {
109
110         m_adj(mb, -(mb->m_pkthdr.len - length));
111 }
112
113 struct mbuf *
114 ipoib_alloc_map_mb(struct ipoib_dev_priv *priv, struct ipoib_rx_buf *rx_req,
115     int size)
116 {
117         struct mbuf *mb, *m;
118         int i, j;
119
120         rx_req->mb = NULL;
121         mb = m_getm2(NULL, size, M_NOWAIT, MT_DATA, M_PKTHDR);
122         if (mb == NULL)
123                 return (NULL);
124         for (i = 0, m = mb; m != NULL; m = m->m_next, i++) {
125                 m->m_len = M_SIZE(m);
126                 mb->m_pkthdr.len += m->m_len;
127                 rx_req->mapping[i] = ib_dma_map_single(priv->ca,
128                     mtod(m, void *), m->m_len, DMA_FROM_DEVICE);
129                 if (unlikely(ib_dma_mapping_error(priv->ca,
130                     rx_req->mapping[i])))
131                         goto error;
132
133         }
134         rx_req->mb = mb;
135         return (mb);
136 error:
137         for (j = 0, m = mb; j < i; m = m->m_next, j++)
138                 ib_dma_unmap_single(priv->ca, rx_req->mapping[j], m->m_len,
139                     DMA_FROM_DEVICE);
140         m_freem(mb);
141         return (NULL);
142
143 }
144
145 static int ipoib_ib_post_receive(struct ipoib_dev_priv *priv, int id)
146 {
147         struct ipoib_rx_buf *rx_req;
148         struct ib_recv_wr *bad_wr;
149         struct mbuf *m;
150         int ret;
151         int i;
152
153         rx_req = &priv->rx_ring[id];
154         for (m = rx_req->mb, i = 0; m != NULL; m = m->m_next, i++) {
155                 priv->rx_sge[i].addr = rx_req->mapping[i];
156                 priv->rx_sge[i].length = m->m_len;
157         }
158         priv->rx_wr.num_sge = i;
159         priv->rx_wr.wr_id = id | IPOIB_OP_RECV;
160
161         ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);
162         if (unlikely(ret)) {
163                 ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);
164                 ipoib_dma_unmap_rx(priv, &priv->rx_ring[id]);
165                 m_freem(priv->rx_ring[id].mb);
166                 priv->rx_ring[id].mb = NULL;
167         }
168
169         return ret;
170 }
171
172 static struct mbuf *
173 ipoib_alloc_rx_mb(struct ipoib_dev_priv *priv, int id)
174 {
175
176         return ipoib_alloc_map_mb(priv, &priv->rx_ring[id],
177             priv->max_ib_mtu + IB_GRH_BYTES);
178 }
179
180 static int ipoib_ib_post_receives(struct ipoib_dev_priv *priv)
181 {
182         int i;
183
184         for (i = 0; i < ipoib_recvq_size; ++i) {
185                 if (!ipoib_alloc_rx_mb(priv, i)) {
186                         ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);
187                         return -ENOMEM;
188                 }
189                 if (ipoib_ib_post_receive(priv, i)) {
190                         ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);
191                         return -EIO;
192                 }
193         }
194
195         return 0;
196 }
197
198 static void
199 ipoib_ib_handle_rx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
200 {
201         struct ipoib_rx_buf saverx;
202         unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;
203         struct ifnet *dev = priv->dev;
204         struct ipoib_header *eh;
205         struct mbuf *mb;
206
207         ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",
208                        wr_id, wc->status);
209
210         if (unlikely(wr_id >= ipoib_recvq_size)) {
211                 ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",
212                            wr_id, ipoib_recvq_size);
213                 return;
214         }
215
216         mb  = priv->rx_ring[wr_id].mb;
217
218         if (unlikely(wc->status != IB_WC_SUCCESS)) {
219                 if (wc->status != IB_WC_WR_FLUSH_ERR) {
220                         ipoib_warn(priv, "failed recv event "
221                                    "(status=%d, wrid=%d vend_err %x)\n",
222                                    wc->status, wr_id, wc->vendor_err);
223                         goto repost;
224                 }
225                 if (mb) {
226                         ipoib_dma_unmap_rx(priv, &priv->rx_ring[wr_id]);
227                         m_freem(mb);
228                         priv->rx_ring[wr_id].mb = NULL;
229                 }
230                 return;
231         }
232
233         /*
234          * Drop packets that this interface sent, ie multicast packets
235          * that the HCA has replicated.
236          */
237         if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)
238                 goto repost;
239
240         memcpy(&saverx, &priv->rx_ring[wr_id], sizeof(saverx));
241         /*
242          * If we can't allocate a new RX buffer, dump
243          * this packet and reuse the old buffer.
244          */
245         if (unlikely(!ipoib_alloc_rx_mb(priv, wr_id))) {
246                 memcpy(&priv->rx_ring[wr_id], &saverx, sizeof(saverx));
247                 if_inc_counter(dev, IFCOUNTER_IQDROPS, 1);
248                 goto repost;
249         }
250
251         ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",
252                        wc->byte_len, wc->slid);
253
254         ipoib_dma_unmap_rx(priv, &saverx);
255         ipoib_dma_mb(priv, mb, wc->byte_len);
256
257         if_inc_counter(dev, IFCOUNTER_IPACKETS, 1);
258         if_inc_counter(dev, IFCOUNTER_IBYTES, mb->m_pkthdr.len);
259         mb->m_pkthdr.rcvif = dev;
260         m_adj(mb, sizeof(struct ib_grh) - INFINIBAND_ALEN);
261         eh = mtod(mb, struct ipoib_header *);
262         bzero(eh->hwaddr, 4);   /* Zero the queue pair, only dgid is in grh */
263
264         if (test_bit(IPOIB_FLAG_CSUM, &priv->flags) && likely(wc->wc_flags & IB_WC_IP_CSUM_OK))
265                 mb->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
266
267         dev->if_input(dev, mb);
268
269 repost:
270         if (unlikely(ipoib_ib_post_receive(priv, wr_id)))
271                 ipoib_warn(priv, "ipoib_ib_post_receive failed "
272                            "for buf %d\n", wr_id);
273 }
274
275 int ipoib_dma_map_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req, int max)
276 {
277         struct mbuf *mb = tx_req->mb;
278         u64 *mapping = tx_req->mapping;
279         struct mbuf *m, *p;
280         int error;
281         int i;
282
283         for (m = mb, p = NULL, i = 0; m != NULL; p = m, m = m->m_next, i++) {
284                 if (m->m_len != 0)
285                         continue;
286                 if (p == NULL)
287                         panic("ipoib_dma_map_tx: First mbuf empty\n");
288                 p->m_next = m_free(m);
289                 m = p;
290                 i--;
291         }
292         i--;
293         if (i >= max) {
294                 tx_req->mb = mb = m_defrag(mb, M_NOWAIT);
295                 if (mb == NULL)
296                         return -EIO;
297                 for (m = mb, i = 0; m != NULL; m = m->m_next, i++);
298                 if (i >= max)
299                         return -EIO;
300         }
301         error = 0;
302         for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
303                 mapping[i] = ib_dma_map_single(ca, mtod(m, void *),
304                                                m->m_len, DMA_TO_DEVICE);
305                 if (unlikely(ib_dma_mapping_error(ca, mapping[i]))) {
306                         error = -EIO;
307                         break;
308                 }
309         }
310         if (error) {
311                 int end;
312
313                 end = i;
314                 for (m = mb, i = 0; i < end; m = m->m_next, i++)
315                         ib_dma_unmap_single(ca, mapping[i], m->m_len,
316                                             DMA_TO_DEVICE);
317         }
318         return error;
319 }
320
321 void ipoib_dma_unmap_tx(struct ib_device *ca, struct ipoib_tx_buf *tx_req)
322 {
323         struct mbuf *mb = tx_req->mb;
324         u64 *mapping = tx_req->mapping;
325         struct mbuf *m;
326         int i;
327
328         for (m = mb, i = 0; m != NULL; m = m->m_next, i++)
329                 ib_dma_unmap_single(ca, mapping[i], m->m_len, DMA_TO_DEVICE);
330 }
331
332 static void ipoib_ib_handle_tx_wc(struct ipoib_dev_priv *priv, struct ib_wc *wc)
333 {
334         struct ifnet *dev = priv->dev;
335         unsigned int wr_id = wc->wr_id;
336         struct ipoib_tx_buf *tx_req;
337
338         ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",
339                        wr_id, wc->status);
340
341         if (unlikely(wr_id >= ipoib_sendq_size)) {
342                 ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",
343                            wr_id, ipoib_sendq_size);
344                 return;
345         }
346
347         tx_req = &priv->tx_ring[wr_id];
348
349         ipoib_dma_unmap_tx(priv->ca, tx_req);
350
351         if_inc_counter(dev, IFCOUNTER_OPACKETS, 1);
352
353         m_freem(tx_req->mb);
354
355         ++priv->tx_tail;
356         if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&
357             (dev->if_drv_flags & IFF_DRV_OACTIVE) &&
358             test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
359                 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
360
361         if (wc->status != IB_WC_SUCCESS &&
362             wc->status != IB_WC_WR_FLUSH_ERR)
363                 ipoib_warn(priv, "failed send event "
364                            "(status=%d, wrid=%d vend_err %x)\n",
365                            wc->status, wr_id, wc->vendor_err);
366 }
367
368 int
369 ipoib_poll_tx(struct ipoib_dev_priv *priv)
370 {
371         int n, i;
372
373         n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);
374         for (i = 0; i < n; ++i) {
375                 struct ib_wc *wc = priv->send_wc + i;
376                 if (wc->wr_id & IPOIB_OP_CM)
377                         ipoib_cm_handle_tx_wc(priv, wc);
378                 else
379                         ipoib_ib_handle_tx_wc(priv, wc);
380         }
381
382         return n == MAX_SEND_CQE;
383 }
384
385 static void
386 ipoib_poll(struct ipoib_dev_priv *priv)
387 {
388         int n, i;
389
390 poll_more:
391         spin_lock(&priv->drain_lock);
392         for (;;) {
393                 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
394                 for (i = 0; i < n; i++) {
395                         struct ib_wc *wc = priv->ibwc + i;
396
397                         if ((wc->wr_id & IPOIB_OP_RECV) == 0)
398                                 panic("ipoib_poll: Bad wr_id 0x%jX\n",
399                                     (intmax_t)wc->wr_id);
400                         if (wc->wr_id & IPOIB_OP_CM)
401                                 ipoib_cm_handle_rx_wc(priv, wc);
402                         else
403                                 ipoib_ib_handle_rx_wc(priv, wc);
404                 }
405
406                 if (n != IPOIB_NUM_WC)
407                         break;
408         }
409         spin_unlock(&priv->drain_lock);
410
411         if (ib_req_notify_cq(priv->recv_cq,
412             IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS))
413                 goto poll_more;
414 }
415
416 void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)
417 {
418         struct ipoib_dev_priv *priv = dev_ptr;
419
420         ipoib_poll(priv);
421 }
422
423 static void drain_tx_cq(struct ipoib_dev_priv *priv)
424 {
425         struct ifnet *dev = priv->dev;
426
427         spin_lock(&priv->lock);
428         while (ipoib_poll_tx(priv))
429                 ; /* nothing */
430
431         if (dev->if_drv_flags & IFF_DRV_OACTIVE)
432                 mod_timer(&priv->poll_timer, jiffies + 1);
433
434         spin_unlock(&priv->lock);
435 }
436
437 void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)
438 {
439         struct ipoib_dev_priv *priv = dev_ptr;
440
441         mod_timer(&priv->poll_timer, jiffies);
442 }
443
444 static inline int
445 post_send(struct ipoib_dev_priv *priv, unsigned int wr_id,
446     struct ib_ah *address, u32 qpn, struct ipoib_tx_buf *tx_req, void *head,
447     int hlen)
448 {
449         struct ib_send_wr *bad_wr;
450         struct mbuf *mb = tx_req->mb;
451         u64 *mapping = tx_req->mapping;
452         struct mbuf *m;
453         int i;
454
455         for (m = mb, i = 0; m != NULL; m = m->m_next, i++) {
456                 priv->tx_sge[i].addr         = mapping[i];
457                 priv->tx_sge[i].length       = m->m_len;
458         }
459         priv->tx_wr.wr.num_sge  = i;
460         priv->tx_wr.wr.wr_id    = wr_id;
461         priv->tx_wr.remote_qpn  = qpn;
462         priv->tx_wr.ah          = address;
463
464         if (head) {
465                 priv->tx_wr.mss         = 0; /* XXX mb_shinfo(mb)->gso_size; */
466                 priv->tx_wr.header      = head;
467                 priv->tx_wr.hlen        = hlen;
468                 priv->tx_wr.wr.opcode   = IB_WR_LSO;
469         } else
470                 priv->tx_wr.wr.opcode   = IB_WR_SEND;
471
472         return ib_post_send(priv->qp, &priv->tx_wr.wr, &bad_wr);
473 }
474
475 void
476 ipoib_send(struct ipoib_dev_priv *priv, struct mbuf *mb,
477     struct ipoib_ah *address, u32 qpn)
478 {
479         struct ifnet *dev = priv->dev;
480         struct ipoib_tx_buf *tx_req;
481         int hlen;
482         void *phead;
483
484         if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))
485                 while (ipoib_poll_tx(priv))
486                         ; /* nothing */
487
488         m_adj(mb, sizeof (struct ipoib_pseudoheader));
489         if (0 /* XXX segment offload mb_is_gso(mb) */) {
490                 /* XXX hlen = mb_transport_offset(mb) + tcp_hdrlen(mb); */
491                 phead = mtod(mb, void *);
492                 if (mb->m_len < hlen) {
493                         ipoib_warn(priv, "linear data too small\n");
494                         if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
495                         m_freem(mb);
496                         return;
497                 }
498                 m_adj(mb, hlen);
499         } else {
500                 if (unlikely(mb->m_pkthdr.len - IPOIB_ENCAP_LEN > priv->mcast_mtu)) {
501                         ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",
502                                    mb->m_pkthdr.len, priv->mcast_mtu);
503                         if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
504                         ipoib_cm_mb_too_long(priv, mb, priv->mcast_mtu);
505                         return;
506                 }
507                 phead = NULL;
508                 hlen  = 0;
509         }
510
511         ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",
512                        mb->m_pkthdr.len, address, qpn);
513
514         /*
515          * We put the mb into the tx_ring _before_ we call post_send()
516          * because it's entirely possible that the completion handler will
517          * run before we execute anything after the post_send().  That
518          * means we have to make sure everything is properly recorded and
519          * our state is consistent before we call post_send().
520          */
521         tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];
522         tx_req->mb = mb;
523         if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req, IPOIB_UD_TX_SG))) {
524                 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
525                 if (tx_req->mb)
526                         m_freem(tx_req->mb);
527                 return;
528         }
529
530         if (mb->m_pkthdr.csum_flags & (CSUM_IP|CSUM_TCP|CSUM_UDP))
531                 priv->tx_wr.wr.send_flags |= IB_SEND_IP_CSUM;
532         else
533                 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
534
535         if (++priv->tx_outstanding == ipoib_sendq_size) {
536                 ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");
537                 if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
538                         ipoib_warn(priv, "request notify on send CQ failed\n");
539                 dev->if_drv_flags |= IFF_DRV_OACTIVE;
540         }
541
542         if (unlikely(post_send(priv,
543             priv->tx_head & (ipoib_sendq_size - 1), address->ah, qpn,
544             tx_req, phead, hlen))) {
545                 ipoib_warn(priv, "post_send failed\n");
546                 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
547                 --priv->tx_outstanding;
548                 ipoib_dma_unmap_tx(priv->ca, tx_req);
549                 m_freem(mb);
550                 if (dev->if_drv_flags & IFF_DRV_OACTIVE)
551                         dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
552         } else {
553                 address->last_send = priv->tx_head;
554                 ++priv->tx_head;
555         }
556 }
557
558 static void __ipoib_reap_ah(struct ipoib_dev_priv *priv)
559 {
560         struct ipoib_ah *ah, *tah;
561         LIST_HEAD(remove_list);
562         unsigned long flags;
563
564         spin_lock_irqsave(&priv->lock, flags);
565
566         list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)
567                 if ((int) priv->tx_tail - (int) ah->last_send >= 0) {
568                         list_del(&ah->list);
569                         ib_destroy_ah(ah->ah);
570                         kfree(ah);
571                 }
572
573         spin_unlock_irqrestore(&priv->lock, flags);
574 }
575
576 void ipoib_reap_ah(struct work_struct *work)
577 {
578         struct ipoib_dev_priv *priv =
579                 container_of(work, struct ipoib_dev_priv, ah_reap_task.work);
580
581         __ipoib_reap_ah(priv);
582
583         if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))
584                 queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,
585                                    HZ);
586 }
587
588 static void ipoib_ah_dev_cleanup(struct ipoib_dev_priv *priv)
589 {
590         unsigned long begin;
591
592         begin = jiffies;
593
594         while (!list_empty(&priv->dead_ahs)) {
595                 __ipoib_reap_ah(priv);
596
597                 if (time_after(jiffies, begin + HZ)) {
598                         ipoib_warn(priv, "timing out; will leak address handles\n");
599                         break;
600                 }
601
602                 msleep(1);
603         }
604 }
605
606 static void ipoib_ib_tx_timer_func(unsigned long ctx)
607 {
608         drain_tx_cq((struct ipoib_dev_priv *)ctx);
609 }
610
611 int ipoib_ib_dev_open(struct ipoib_dev_priv *priv)
612 {
613         int ret;
614
615         if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {
616                 ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);
617                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
618                 return -1;
619         }
620         set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
621
622         ret = ipoib_init_qp(priv);
623         if (ret) {
624                 ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);
625                 return -1;
626         }
627
628         ret = ipoib_ib_post_receives(priv);
629         if (ret) {
630                 ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);
631                 ipoib_ib_dev_stop(priv, 1);
632                 return -1;
633         }
634
635         ret = ipoib_cm_dev_open(priv);
636         if (ret) {
637                 ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);
638                 ipoib_ib_dev_stop(priv, 1);
639                 return -1;
640         }
641
642         clear_bit(IPOIB_STOP_REAPER, &priv->flags);
643         queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task, HZ);
644
645         set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
646
647         return 0;
648 }
649
650 static void ipoib_pkey_dev_check_presence(struct ipoib_dev_priv *priv)
651 {
652         u16 pkey_index = 0;
653
654         if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))
655                 clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
656         else
657                 set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
658 }
659
660 int ipoib_ib_dev_up(struct ipoib_dev_priv *priv)
661 {
662
663         ipoib_pkey_dev_check_presence(priv);
664
665         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
666                 ipoib_dbg(priv, "PKEY is not assigned.\n");
667                 return 0;
668         }
669
670         set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
671
672         return ipoib_mcast_start_thread(priv);
673 }
674
675 int ipoib_ib_dev_down(struct ipoib_dev_priv *priv, int flush)
676 {
677
678         ipoib_dbg(priv, "downing ib_dev\n");
679
680         clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);
681         if_link_state_change(priv->dev, LINK_STATE_DOWN);
682
683         /* Shutdown the P_Key thread if still active */
684         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
685                 mutex_lock(&pkey_mutex);
686                 set_bit(IPOIB_PKEY_STOP, &priv->flags);
687                 cancel_delayed_work(&priv->pkey_poll_task);
688                 mutex_unlock(&pkey_mutex);
689                 if (flush)
690                         flush_workqueue(ipoib_workqueue);
691         }
692
693         ipoib_mcast_stop_thread(priv, flush);
694         ipoib_mcast_dev_flush(priv);
695
696         ipoib_flush_paths(priv);
697
698         return 0;
699 }
700
701 static int recvs_pending(struct ipoib_dev_priv *priv)
702 {
703         int pending = 0;
704         int i;
705
706         for (i = 0; i < ipoib_recvq_size; ++i)
707                 if (priv->rx_ring[i].mb)
708                         ++pending;
709
710         return pending;
711 }
712
713 void ipoib_drain_cq(struct ipoib_dev_priv *priv)
714 {
715         int i, n;
716
717         spin_lock(&priv->drain_lock);
718         do {
719                 n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);
720                 for (i = 0; i < n; ++i) {
721                         /*
722                          * Convert any successful completions to flush
723                          * errors to avoid passing packets up the
724                          * stack after bringing the device down.
725                          */
726                         if (priv->ibwc[i].status == IB_WC_SUCCESS)
727                                 priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;
728
729                         if ((priv->ibwc[i].wr_id & IPOIB_OP_RECV) == 0)
730                                 panic("ipoib_drain_cq:  Bad wrid 0x%jX\n",
731                                     (intmax_t)priv->ibwc[i].wr_id);
732                         if (priv->ibwc[i].wr_id & IPOIB_OP_CM)
733                                 ipoib_cm_handle_rx_wc(priv, priv->ibwc + i);
734                         else
735                                 ipoib_ib_handle_rx_wc(priv, priv->ibwc + i);
736                 }
737         } while (n == IPOIB_NUM_WC);
738         spin_unlock(&priv->drain_lock);
739
740         spin_lock(&priv->lock);
741         while (ipoib_poll_tx(priv))
742                 ; /* nothing */
743
744         spin_unlock(&priv->lock);
745 }
746
747 int ipoib_ib_dev_stop(struct ipoib_dev_priv *priv, int flush)
748 {
749         struct ib_qp_attr qp_attr;
750         unsigned long begin;
751         struct ipoib_tx_buf *tx_req;
752         int i;
753
754         clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags);
755
756         ipoib_cm_dev_stop(priv);
757
758         /*
759          * Move our QP to the error state and then reinitialize in
760          * when all work requests have completed or have been flushed.
761          */
762         qp_attr.qp_state = IB_QPS_ERR;
763         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
764                 ipoib_warn(priv, "Failed to modify QP to ERROR state\n");
765
766         /* Wait for all sends and receives to complete */
767         begin = jiffies;
768
769         while (priv->tx_head != priv->tx_tail || recvs_pending(priv)) {
770                 if (time_after(jiffies, begin + 5 * HZ)) {
771                         ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",
772                                    priv->tx_head - priv->tx_tail, recvs_pending(priv));
773
774                         /*
775                          * assume the HW is wedged and just free up
776                          * all our pending work requests.
777                          */
778                         while ((int) priv->tx_tail - (int) priv->tx_head < 0) {
779                                 tx_req = &priv->tx_ring[priv->tx_tail &
780                                                         (ipoib_sendq_size - 1)];
781                                 ipoib_dma_unmap_tx(priv->ca, tx_req);
782                                 m_freem(tx_req->mb);
783                                 ++priv->tx_tail;
784                                 --priv->tx_outstanding;
785                         }
786
787                         for (i = 0; i < ipoib_recvq_size; ++i) {
788                                 struct ipoib_rx_buf *rx_req;
789
790                                 rx_req = &priv->rx_ring[i];
791                                 if (!rx_req->mb)
792                                         continue;
793                                 ipoib_dma_unmap_rx(priv, &priv->rx_ring[i]);
794                                 m_freem(rx_req->mb);
795                                 rx_req->mb = NULL;
796                         }
797
798                         goto timeout;
799                 }
800
801                 ipoib_drain_cq(priv);
802
803                 msleep(1);
804         }
805
806         ipoib_dbg(priv, "All sends and receives done.\n");
807
808 timeout:
809         del_timer_sync(&priv->poll_timer);
810         qp_attr.qp_state = IB_QPS_RESET;
811         if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
812                 ipoib_warn(priv, "Failed to modify QP to RESET state\n");
813
814         /* Wait for all AHs to be reaped */
815         set_bit(IPOIB_STOP_REAPER, &priv->flags);
816         cancel_delayed_work(&priv->ah_reap_task);
817         if (flush)
818                 flush_workqueue(ipoib_workqueue);
819
820         ipoib_ah_dev_cleanup(priv);
821
822         ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);
823
824         return 0;
825 }
826
827 int ipoib_ib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
828 {
829         struct ifnet *dev = priv->dev;
830
831         priv->ca = ca;
832         priv->port = port;
833         priv->qp = NULL;
834
835         if (ipoib_transport_dev_init(priv, ca)) {
836                 printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);
837                 return -ENODEV;
838         }
839
840         setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
841                     (unsigned long) priv);
842
843         if (dev->if_flags & IFF_UP) {
844                 if (ipoib_ib_dev_open(priv)) {
845                         ipoib_transport_dev_cleanup(priv);
846                         return -ENODEV;
847                 }
848         }
849
850         return 0;
851 }
852
853 static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,
854                                 enum ipoib_flush_level level)
855 {
856         struct ipoib_dev_priv *cpriv;
857         u16 new_index;
858
859         mutex_lock(&priv->vlan_mutex);
860
861         /*
862          * Flush any child interfaces too -- they might be up even if
863          * the parent is down.
864          */
865         list_for_each_entry(cpriv, &priv->child_intfs, list)
866                 __ipoib_ib_dev_flush(cpriv, level);
867
868         mutex_unlock(&priv->vlan_mutex);
869
870         if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {
871                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");
872                 return;
873         }
874
875         if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
876                 ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");
877                 return;
878         }
879
880         if (level == IPOIB_FLUSH_HEAVY) {
881                 if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {
882                         clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
883                         ipoib_ib_dev_down(priv, 0);
884                         ipoib_ib_dev_stop(priv, 0);
885                         if (ipoib_pkey_dev_delay_open(priv))
886                                 return;
887                 }
888
889                 /* restart QP only if P_Key index is changed */
890                 if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&
891                     new_index == priv->pkey_index) {
892                         ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");
893                         return;
894                 }
895                 priv->pkey_index = new_index;
896         }
897
898         if (level == IPOIB_FLUSH_LIGHT) {
899                 ipoib_mark_paths_invalid(priv);
900                 ipoib_mcast_dev_flush(priv);
901         }
902
903         if (level >= IPOIB_FLUSH_NORMAL)
904                 ipoib_ib_dev_down(priv, 0);
905
906         if (level == IPOIB_FLUSH_HEAVY) {
907                 ipoib_ib_dev_stop(priv, 0);
908                 ipoib_ib_dev_open(priv);
909         }
910
911         /*
912          * The device could have been brought down between the start and when
913          * we get here, don't bring it back up if it's not configured up
914          */
915         if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {
916                 if (level >= IPOIB_FLUSH_NORMAL)
917                         ipoib_ib_dev_up(priv);
918                 ipoib_mcast_restart_task(&priv->restart_task);
919         }
920 }
921
922 void ipoib_ib_dev_flush_light(struct work_struct *work)
923 {
924         struct ipoib_dev_priv *priv =
925                 container_of(work, struct ipoib_dev_priv, flush_light);
926
927         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);
928 }
929
930 void ipoib_ib_dev_flush_normal(struct work_struct *work)
931 {
932         struct ipoib_dev_priv *priv =
933                 container_of(work, struct ipoib_dev_priv, flush_normal);
934
935         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);
936 }
937
938 void ipoib_ib_dev_flush_heavy(struct work_struct *work)
939 {
940         struct ipoib_dev_priv *priv =
941                 container_of(work, struct ipoib_dev_priv, flush_heavy);
942
943         __ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);
944 }
945
946 void ipoib_ib_dev_cleanup(struct ipoib_dev_priv *priv)
947 {
948
949         ipoib_dbg(priv, "cleaning up ib_dev\n");
950
951         ipoib_mcast_stop_thread(priv, 1);
952         ipoib_mcast_dev_flush(priv);
953
954         ipoib_ah_dev_cleanup(priv);
955         ipoib_transport_dev_cleanup(priv);
956 }
957
958 /*
959  * Delayed P_Key Assigment Interim Support
960  *
961  * The following is initial implementation of delayed P_Key assigment
962  * mechanism. It is using the same approach implemented for the multicast
963  * group join. The single goal of this implementation is to quickly address
964  * Bug #2507. This implementation will probably be removed when the P_Key
965  * change async notification is available.
966  */
967
968 void ipoib_pkey_poll(struct work_struct *work)
969 {
970         struct ipoib_dev_priv *priv =
971                 container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);
972
973         ipoib_pkey_dev_check_presence(priv);
974
975         if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
976                 ipoib_open(priv);
977         else {
978                 mutex_lock(&pkey_mutex);
979                 if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))
980                         queue_delayed_work(ipoib_workqueue,
981                                            &priv->pkey_poll_task,
982                                            HZ);
983                 mutex_unlock(&pkey_mutex);
984         }
985 }
986
987 int ipoib_pkey_dev_delay_open(struct ipoib_dev_priv *priv)
988 {
989
990         /* Look for the interface pkey value in the IB Port P_Key table and */
991         /* set the interface pkey assigment flag                            */
992         ipoib_pkey_dev_check_presence(priv);
993
994         /* P_Key value not assigned yet - start polling */
995         if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {
996                 mutex_lock(&pkey_mutex);
997                 clear_bit(IPOIB_PKEY_STOP, &priv->flags);
998                 queue_delayed_work(ipoib_workqueue,
999                                    &priv->pkey_poll_task,
1000                                    HZ);
1001                 mutex_unlock(&pkey_mutex);
1002                 return 1;
1003         }
1004
1005         return 0;
1006 }