Path: blob/master/drivers/infiniband/ulp/ipoib/ipoib_ib.c
15112 views
/*1* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.3* Copyright (c) 2005 Mellanox Technologies. All rights reserved.4* Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.5*6* This software is available to you under a choice of one of two7* licenses. You may choose to be licensed under the terms of the GNU8* General Public License (GPL) Version 2, available from the file9* COPYING in the main directory of this source tree, or the10* OpenIB.org BSD license below:11*12* Redistribution and use in source and binary forms, with or13* without modification, are permitted provided that the following14* conditions are met:15*16* - Redistributions of source code must retain the above17* copyright notice, this list of conditions and the following18* disclaimer.19*20* - Redistributions in binary form must reproduce the above21* copyright notice, this list of conditions and the following22* disclaimer in the documentation and/or other materials23* provided with the distribution.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND28* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS29* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN30* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN31* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE32* SOFTWARE.33*/3435#include <linux/delay.h>36#include <linux/dma-mapping.h>37#include <linux/slab.h>3839#include <linux/ip.h>40#include <linux/tcp.h>4142#include "ipoib.h"4344#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG_DATA45static int data_debug_level;4647module_param(data_debug_level, int, 0644);48MODULE_PARM_DESC(data_debug_level,49"Enable data path debug tracing if > 0");50#endif5152static DEFINE_MUTEX(pkey_mutex);5354struct ipoib_ah *ipoib_create_ah(struct net_device *dev,55struct ib_pd *pd, struct ib_ah_attr *attr)56{57struct ipoib_ah *ah;5859ah = kmalloc(sizeof *ah, GFP_KERNEL);60if (!ah)61return NULL;6263ah->dev = dev;64ah->last_send = 0;65kref_init(&ah->ref);6667ah->ah = ib_create_ah(pd, attr);68if (IS_ERR(ah->ah)) {69kfree(ah);70ah = NULL;71} else72ipoib_dbg(netdev_priv(dev), "Created ah %p\n", ah->ah);7374return ah;75}7677void ipoib_free_ah(struct kref *kref)78{79struct ipoib_ah *ah = container_of(kref, struct ipoib_ah, ref);80struct ipoib_dev_priv *priv = netdev_priv(ah->dev);8182unsigned long flags;8384spin_lock_irqsave(&priv->lock, flags);85list_add_tail(&ah->list, &priv->dead_ahs);86spin_unlock_irqrestore(&priv->lock, flags);87}8889static void ipoib_ud_dma_unmap_rx(struct ipoib_dev_priv *priv,90u64 mapping[IPOIB_UD_RX_SG])91{92if (ipoib_ud_need_sg(priv->max_ib_mtu)) {93ib_dma_unmap_single(priv->ca, mapping[0], IPOIB_UD_HEAD_SIZE,94DMA_FROM_DEVICE);95ib_dma_unmap_page(priv->ca, mapping[1], PAGE_SIZE,96DMA_FROM_DEVICE);97} else98ib_dma_unmap_single(priv->ca, mapping[0],99IPOIB_UD_BUF_SIZE(priv->max_ib_mtu),100DMA_FROM_DEVICE);101}102103static void ipoib_ud_skb_put_frags(struct ipoib_dev_priv *priv,104struct sk_buff *skb,105unsigned int length)106{107if (ipoib_ud_need_sg(priv->max_ib_mtu)) {108skb_frag_t *frag = &skb_shinfo(skb)->frags[0];109unsigned int size;110/*111* There is only two buffers needed for max_payload = 4K,112* first buf size is IPOIB_UD_HEAD_SIZE113*/114skb->tail += IPOIB_UD_HEAD_SIZE;115skb->len += length;116117size = length - IPOIB_UD_HEAD_SIZE;118119frag->size = size;120skb->data_len += size;121skb->truesize += size;122} else123skb_put(skb, length);124125}126127static int ipoib_ib_post_receive(struct net_device *dev, int id)128{129struct ipoib_dev_priv *priv = netdev_priv(dev);130struct ib_recv_wr *bad_wr;131int ret;132133priv->rx_wr.wr_id = id | IPOIB_OP_RECV;134priv->rx_sge[0].addr = priv->rx_ring[id].mapping[0];135priv->rx_sge[1].addr = priv->rx_ring[id].mapping[1];136137138ret = ib_post_recv(priv->qp, &priv->rx_wr, &bad_wr);139if (unlikely(ret)) {140ipoib_warn(priv, "receive failed for buf %d (%d)\n", id, ret);141ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[id].mapping);142dev_kfree_skb_any(priv->rx_ring[id].skb);143priv->rx_ring[id].skb = NULL;144}145146return ret;147}148149static struct sk_buff *ipoib_alloc_rx_skb(struct net_device *dev, int id)150{151struct ipoib_dev_priv *priv = netdev_priv(dev);152struct sk_buff *skb;153int buf_size;154u64 *mapping;155156if (ipoib_ud_need_sg(priv->max_ib_mtu))157buf_size = IPOIB_UD_HEAD_SIZE;158else159buf_size = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);160161skb = dev_alloc_skb(buf_size + 4);162if (unlikely(!skb))163return NULL;164165/*166* IB will leave a 40 byte gap for a GRH and IPoIB adds a 4 byte167* header. So we need 4 more bytes to get to 48 and align the168* IP header to a multiple of 16.169*/170skb_reserve(skb, 4);171172mapping = priv->rx_ring[id].mapping;173mapping[0] = ib_dma_map_single(priv->ca, skb->data, buf_size,174DMA_FROM_DEVICE);175if (unlikely(ib_dma_mapping_error(priv->ca, mapping[0])))176goto error;177178if (ipoib_ud_need_sg(priv->max_ib_mtu)) {179struct page *page = alloc_page(GFP_ATOMIC);180if (!page)181goto partial_error;182skb_fill_page_desc(skb, 0, page, 0, PAGE_SIZE);183mapping[1] =184ib_dma_map_page(priv->ca, skb_shinfo(skb)->frags[0].page,1850, PAGE_SIZE, DMA_FROM_DEVICE);186if (unlikely(ib_dma_mapping_error(priv->ca, mapping[1])))187goto partial_error;188}189190priv->rx_ring[id].skb = skb;191return skb;192193partial_error:194ib_dma_unmap_single(priv->ca, mapping[0], buf_size, DMA_FROM_DEVICE);195error:196dev_kfree_skb_any(skb);197return NULL;198}199200static int ipoib_ib_post_receives(struct net_device *dev)201{202struct ipoib_dev_priv *priv = netdev_priv(dev);203int i;204205for (i = 0; i < ipoib_recvq_size; ++i) {206if (!ipoib_alloc_rx_skb(dev, i)) {207ipoib_warn(priv, "failed to allocate receive buffer %d\n", i);208return -ENOMEM;209}210if (ipoib_ib_post_receive(dev, i)) {211ipoib_warn(priv, "ipoib_ib_post_receive failed for buf %d\n", i);212return -EIO;213}214}215216return 0;217}218219static void ipoib_ib_handle_rx_wc(struct net_device *dev, struct ib_wc *wc)220{221struct ipoib_dev_priv *priv = netdev_priv(dev);222unsigned int wr_id = wc->wr_id & ~IPOIB_OP_RECV;223struct sk_buff *skb;224u64 mapping[IPOIB_UD_RX_SG];225union ib_gid *dgid;226227ipoib_dbg_data(priv, "recv completion: id %d, status: %d\n",228wr_id, wc->status);229230if (unlikely(wr_id >= ipoib_recvq_size)) {231ipoib_warn(priv, "recv completion event with wrid %d (> %d)\n",232wr_id, ipoib_recvq_size);233return;234}235236skb = priv->rx_ring[wr_id].skb;237238if (unlikely(wc->status != IB_WC_SUCCESS)) {239if (wc->status != IB_WC_WR_FLUSH_ERR)240ipoib_warn(priv, "failed recv event "241"(status=%d, wrid=%d vend_err %x)\n",242wc->status, wr_id, wc->vendor_err);243ipoib_ud_dma_unmap_rx(priv, priv->rx_ring[wr_id].mapping);244dev_kfree_skb_any(skb);245priv->rx_ring[wr_id].skb = NULL;246return;247}248249/*250* Drop packets that this interface sent, ie multicast packets251* that the HCA has replicated.252*/253if (wc->slid == priv->local_lid && wc->src_qp == priv->qp->qp_num)254goto repost;255256memcpy(mapping, priv->rx_ring[wr_id].mapping,257IPOIB_UD_RX_SG * sizeof *mapping);258259/*260* If we can't allocate a new RX buffer, dump261* this packet and reuse the old buffer.262*/263if (unlikely(!ipoib_alloc_rx_skb(dev, wr_id))) {264++dev->stats.rx_dropped;265goto repost;266}267268ipoib_dbg_data(priv, "received %d bytes, SLID 0x%04x\n",269wc->byte_len, wc->slid);270271ipoib_ud_dma_unmap_rx(priv, mapping);272ipoib_ud_skb_put_frags(priv, skb, wc->byte_len);273274/* First byte of dgid signals multicast when 0xff */275dgid = &((struct ib_grh *)skb->data)->dgid;276277if (!(wc->wc_flags & IB_WC_GRH) || dgid->raw[0] != 0xff)278skb->pkt_type = PACKET_HOST;279else if (memcmp(dgid, dev->broadcast + 4, sizeof(union ib_gid)) == 0)280skb->pkt_type = PACKET_BROADCAST;281else282skb->pkt_type = PACKET_MULTICAST;283284skb_pull(skb, IB_GRH_BYTES);285286skb->protocol = ((struct ipoib_header *) skb->data)->proto;287skb_reset_mac_header(skb);288skb_pull(skb, IPOIB_ENCAP_LEN);289290++dev->stats.rx_packets;291dev->stats.rx_bytes += skb->len;292293skb->dev = dev;294if ((dev->features & NETIF_F_RXCSUM) && likely(wc->csum_ok))295skb->ip_summed = CHECKSUM_UNNECESSARY;296297napi_gro_receive(&priv->napi, skb);298299repost:300if (unlikely(ipoib_ib_post_receive(dev, wr_id)))301ipoib_warn(priv, "ipoib_ib_post_receive failed "302"for buf %d\n", wr_id);303}304305static int ipoib_dma_map_tx(struct ib_device *ca,306struct ipoib_tx_buf *tx_req)307{308struct sk_buff *skb = tx_req->skb;309u64 *mapping = tx_req->mapping;310int i;311int off;312313if (skb_headlen(skb)) {314mapping[0] = ib_dma_map_single(ca, skb->data, skb_headlen(skb),315DMA_TO_DEVICE);316if (unlikely(ib_dma_mapping_error(ca, mapping[0])))317return -EIO;318319off = 1;320} else321off = 0;322323for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {324skb_frag_t *frag = &skb_shinfo(skb)->frags[i];325mapping[i + off] = ib_dma_map_page(ca, frag->page,326frag->page_offset, frag->size,327DMA_TO_DEVICE);328if (unlikely(ib_dma_mapping_error(ca, mapping[i + off])))329goto partial_error;330}331return 0;332333partial_error:334for (; i > 0; --i) {335skb_frag_t *frag = &skb_shinfo(skb)->frags[i - 1];336ib_dma_unmap_page(ca, mapping[i - !off], frag->size, DMA_TO_DEVICE);337}338339if (off)340ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);341342return -EIO;343}344345static void ipoib_dma_unmap_tx(struct ib_device *ca,346struct ipoib_tx_buf *tx_req)347{348struct sk_buff *skb = tx_req->skb;349u64 *mapping = tx_req->mapping;350int i;351int off;352353if (skb_headlen(skb)) {354ib_dma_unmap_single(ca, mapping[0], skb_headlen(skb), DMA_TO_DEVICE);355off = 1;356} else357off = 0;358359for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {360skb_frag_t *frag = &skb_shinfo(skb)->frags[i];361ib_dma_unmap_page(ca, mapping[i + off], frag->size,362DMA_TO_DEVICE);363}364}365366static void ipoib_ib_handle_tx_wc(struct net_device *dev, struct ib_wc *wc)367{368struct ipoib_dev_priv *priv = netdev_priv(dev);369unsigned int wr_id = wc->wr_id;370struct ipoib_tx_buf *tx_req;371372ipoib_dbg_data(priv, "send completion: id %d, status: %d\n",373wr_id, wc->status);374375if (unlikely(wr_id >= ipoib_sendq_size)) {376ipoib_warn(priv, "send completion event with wrid %d (> %d)\n",377wr_id, ipoib_sendq_size);378return;379}380381tx_req = &priv->tx_ring[wr_id];382383ipoib_dma_unmap_tx(priv->ca, tx_req);384385++dev->stats.tx_packets;386dev->stats.tx_bytes += tx_req->skb->len;387388dev_kfree_skb_any(tx_req->skb);389390++priv->tx_tail;391if (unlikely(--priv->tx_outstanding == ipoib_sendq_size >> 1) &&392netif_queue_stopped(dev) &&393test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))394netif_wake_queue(dev);395396if (wc->status != IB_WC_SUCCESS &&397wc->status != IB_WC_WR_FLUSH_ERR)398ipoib_warn(priv, "failed send event "399"(status=%d, wrid=%d vend_err %x)\n",400wc->status, wr_id, wc->vendor_err);401}402403static int poll_tx(struct ipoib_dev_priv *priv)404{405int n, i;406407n = ib_poll_cq(priv->send_cq, MAX_SEND_CQE, priv->send_wc);408for (i = 0; i < n; ++i)409ipoib_ib_handle_tx_wc(priv->dev, priv->send_wc + i);410411return n == MAX_SEND_CQE;412}413414int ipoib_poll(struct napi_struct *napi, int budget)415{416struct ipoib_dev_priv *priv = container_of(napi, struct ipoib_dev_priv, napi);417struct net_device *dev = priv->dev;418int done;419int t;420int n, i;421422done = 0;423424poll_more:425while (done < budget) {426int max = (budget - done);427428t = min(IPOIB_NUM_WC, max);429n = ib_poll_cq(priv->recv_cq, t, priv->ibwc);430431for (i = 0; i < n; i++) {432struct ib_wc *wc = priv->ibwc + i;433434if (wc->wr_id & IPOIB_OP_RECV) {435++done;436if (wc->wr_id & IPOIB_OP_CM)437ipoib_cm_handle_rx_wc(dev, wc);438else439ipoib_ib_handle_rx_wc(dev, wc);440} else441ipoib_cm_handle_tx_wc(priv->dev, wc);442}443444if (n != t)445break;446}447448if (done < budget) {449napi_complete(napi);450if (unlikely(ib_req_notify_cq(priv->recv_cq,451IB_CQ_NEXT_COMP |452IB_CQ_REPORT_MISSED_EVENTS)) &&453napi_reschedule(napi))454goto poll_more;455}456457return done;458}459460void ipoib_ib_completion(struct ib_cq *cq, void *dev_ptr)461{462struct net_device *dev = dev_ptr;463struct ipoib_dev_priv *priv = netdev_priv(dev);464465napi_schedule(&priv->napi);466}467468static void drain_tx_cq(struct net_device *dev)469{470struct ipoib_dev_priv *priv = netdev_priv(dev);471472netif_tx_lock(dev);473while (poll_tx(priv))474; /* nothing */475476if (netif_queue_stopped(dev))477mod_timer(&priv->poll_timer, jiffies + 1);478479netif_tx_unlock(dev);480}481482void ipoib_send_comp_handler(struct ib_cq *cq, void *dev_ptr)483{484struct ipoib_dev_priv *priv = netdev_priv(dev_ptr);485486mod_timer(&priv->poll_timer, jiffies);487}488489static inline int post_send(struct ipoib_dev_priv *priv,490unsigned int wr_id,491struct ib_ah *address, u32 qpn,492struct ipoib_tx_buf *tx_req,493void *head, int hlen)494{495struct ib_send_wr *bad_wr;496int i, off;497struct sk_buff *skb = tx_req->skb;498skb_frag_t *frags = skb_shinfo(skb)->frags;499int nr_frags = skb_shinfo(skb)->nr_frags;500u64 *mapping = tx_req->mapping;501502if (skb_headlen(skb)) {503priv->tx_sge[0].addr = mapping[0];504priv->tx_sge[0].length = skb_headlen(skb);505off = 1;506} else507off = 0;508509for (i = 0; i < nr_frags; ++i) {510priv->tx_sge[i + off].addr = mapping[i + off];511priv->tx_sge[i + off].length = frags[i].size;512}513priv->tx_wr.num_sge = nr_frags + off;514priv->tx_wr.wr_id = wr_id;515priv->tx_wr.wr.ud.remote_qpn = qpn;516priv->tx_wr.wr.ud.ah = address;517518if (head) {519priv->tx_wr.wr.ud.mss = skb_shinfo(skb)->gso_size;520priv->tx_wr.wr.ud.header = head;521priv->tx_wr.wr.ud.hlen = hlen;522priv->tx_wr.opcode = IB_WR_LSO;523} else524priv->tx_wr.opcode = IB_WR_SEND;525526return ib_post_send(priv->qp, &priv->tx_wr, &bad_wr);527}528529void ipoib_send(struct net_device *dev, struct sk_buff *skb,530struct ipoib_ah *address, u32 qpn)531{532struct ipoib_dev_priv *priv = netdev_priv(dev);533struct ipoib_tx_buf *tx_req;534int hlen, rc;535void *phead;536537if (skb_is_gso(skb)) {538hlen = skb_transport_offset(skb) + tcp_hdrlen(skb);539phead = skb->data;540if (unlikely(!skb_pull(skb, hlen))) {541ipoib_warn(priv, "linear data too small\n");542++dev->stats.tx_dropped;543++dev->stats.tx_errors;544dev_kfree_skb_any(skb);545return;546}547} else {548if (unlikely(skb->len > priv->mcast_mtu + IPOIB_ENCAP_LEN)) {549ipoib_warn(priv, "packet len %d (> %d) too long to send, dropping\n",550skb->len, priv->mcast_mtu + IPOIB_ENCAP_LEN);551++dev->stats.tx_dropped;552++dev->stats.tx_errors;553ipoib_cm_skb_too_long(dev, skb, priv->mcast_mtu);554return;555}556phead = NULL;557hlen = 0;558}559560ipoib_dbg_data(priv, "sending packet, length=%d address=%p qpn=0x%06x\n",561skb->len, address, qpn);562563/*564* We put the skb into the tx_ring _before_ we call post_send()565* because it's entirely possible that the completion handler will566* run before we execute anything after the post_send(). That567* means we have to make sure everything is properly recorded and568* our state is consistent before we call post_send().569*/570tx_req = &priv->tx_ring[priv->tx_head & (ipoib_sendq_size - 1)];571tx_req->skb = skb;572if (unlikely(ipoib_dma_map_tx(priv->ca, tx_req))) {573++dev->stats.tx_errors;574dev_kfree_skb_any(skb);575return;576}577578if (skb->ip_summed == CHECKSUM_PARTIAL)579priv->tx_wr.send_flags |= IB_SEND_IP_CSUM;580else581priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;582583if (++priv->tx_outstanding == ipoib_sendq_size) {584ipoib_dbg(priv, "TX ring full, stopping kernel net queue\n");585if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))586ipoib_warn(priv, "request notify on send CQ failed\n");587netif_stop_queue(dev);588}589590rc = post_send(priv, priv->tx_head & (ipoib_sendq_size - 1),591address->ah, qpn, tx_req, phead, hlen);592if (unlikely(rc)) {593ipoib_warn(priv, "post_send failed, error %d\n", rc);594++dev->stats.tx_errors;595--priv->tx_outstanding;596ipoib_dma_unmap_tx(priv->ca, tx_req);597dev_kfree_skb_any(skb);598if (netif_queue_stopped(dev))599netif_wake_queue(dev);600} else {601dev->trans_start = jiffies;602603address->last_send = priv->tx_head;604++priv->tx_head;605skb_orphan(skb);606607}608609if (unlikely(priv->tx_outstanding > MAX_SEND_CQE))610while (poll_tx(priv))611; /* nothing */612}613614static void __ipoib_reap_ah(struct net_device *dev)615{616struct ipoib_dev_priv *priv = netdev_priv(dev);617struct ipoib_ah *ah, *tah;618LIST_HEAD(remove_list);619unsigned long flags;620621netif_tx_lock_bh(dev);622spin_lock_irqsave(&priv->lock, flags);623624list_for_each_entry_safe(ah, tah, &priv->dead_ahs, list)625if ((int) priv->tx_tail - (int) ah->last_send >= 0) {626list_del(&ah->list);627ib_destroy_ah(ah->ah);628kfree(ah);629}630631spin_unlock_irqrestore(&priv->lock, flags);632netif_tx_unlock_bh(dev);633}634635void ipoib_reap_ah(struct work_struct *work)636{637struct ipoib_dev_priv *priv =638container_of(work, struct ipoib_dev_priv, ah_reap_task.work);639struct net_device *dev = priv->dev;640641__ipoib_reap_ah(dev);642643if (!test_bit(IPOIB_STOP_REAPER, &priv->flags))644queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,645round_jiffies_relative(HZ));646}647648static void ipoib_ib_tx_timer_func(unsigned long ctx)649{650drain_tx_cq((struct net_device *)ctx);651}652653int ipoib_ib_dev_open(struct net_device *dev)654{655struct ipoib_dev_priv *priv = netdev_priv(dev);656int ret;657658if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &priv->pkey_index)) {659ipoib_warn(priv, "P_Key 0x%04x not found\n", priv->pkey);660clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);661return -1;662}663set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);664665ret = ipoib_init_qp(dev);666if (ret) {667ipoib_warn(priv, "ipoib_init_qp returned %d\n", ret);668return -1;669}670671ret = ipoib_ib_post_receives(dev);672if (ret) {673ipoib_warn(priv, "ipoib_ib_post_receives returned %d\n", ret);674ipoib_ib_dev_stop(dev, 1);675return -1;676}677678ret = ipoib_cm_dev_open(dev);679if (ret) {680ipoib_warn(priv, "ipoib_cm_dev_open returned %d\n", ret);681ipoib_ib_dev_stop(dev, 1);682return -1;683}684685clear_bit(IPOIB_STOP_REAPER, &priv->flags);686queue_delayed_work(ipoib_workqueue, &priv->ah_reap_task,687round_jiffies_relative(HZ));688689if (!test_and_set_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))690napi_enable(&priv->napi);691692return 0;693}694695static void ipoib_pkey_dev_check_presence(struct net_device *dev)696{697struct ipoib_dev_priv *priv = netdev_priv(dev);698u16 pkey_index = 0;699700if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index))701clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);702else703set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);704}705706int ipoib_ib_dev_up(struct net_device *dev)707{708struct ipoib_dev_priv *priv = netdev_priv(dev);709710ipoib_pkey_dev_check_presence(dev);711712if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {713ipoib_dbg(priv, "PKEY is not assigned.\n");714return 0;715}716717set_bit(IPOIB_FLAG_OPER_UP, &priv->flags);718719return ipoib_mcast_start_thread(dev);720}721722int ipoib_ib_dev_down(struct net_device *dev, int flush)723{724struct ipoib_dev_priv *priv = netdev_priv(dev);725726ipoib_dbg(priv, "downing ib_dev\n");727728clear_bit(IPOIB_FLAG_OPER_UP, &priv->flags);729netif_carrier_off(dev);730731/* Shutdown the P_Key thread if still active */732if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {733mutex_lock(&pkey_mutex);734set_bit(IPOIB_PKEY_STOP, &priv->flags);735cancel_delayed_work(&priv->pkey_poll_task);736mutex_unlock(&pkey_mutex);737if (flush)738flush_workqueue(ipoib_workqueue);739}740741ipoib_mcast_stop_thread(dev, flush);742ipoib_mcast_dev_flush(dev);743744ipoib_flush_paths(dev);745746return 0;747}748749static int recvs_pending(struct net_device *dev)750{751struct ipoib_dev_priv *priv = netdev_priv(dev);752int pending = 0;753int i;754755for (i = 0; i < ipoib_recvq_size; ++i)756if (priv->rx_ring[i].skb)757++pending;758759return pending;760}761762void ipoib_drain_cq(struct net_device *dev)763{764struct ipoib_dev_priv *priv = netdev_priv(dev);765int i, n;766767/*768* We call completion handling routines that expect to be769* called from the BH-disabled NAPI poll context, so disable770* BHs here too.771*/772local_bh_disable();773774do {775n = ib_poll_cq(priv->recv_cq, IPOIB_NUM_WC, priv->ibwc);776for (i = 0; i < n; ++i) {777/*778* Convert any successful completions to flush779* errors to avoid passing packets up the780* stack after bringing the device down.781*/782if (priv->ibwc[i].status == IB_WC_SUCCESS)783priv->ibwc[i].status = IB_WC_WR_FLUSH_ERR;784785if (priv->ibwc[i].wr_id & IPOIB_OP_RECV) {786if (priv->ibwc[i].wr_id & IPOIB_OP_CM)787ipoib_cm_handle_rx_wc(dev, priv->ibwc + i);788else789ipoib_ib_handle_rx_wc(dev, priv->ibwc + i);790} else791ipoib_cm_handle_tx_wc(dev, priv->ibwc + i);792}793} while (n == IPOIB_NUM_WC);794795while (poll_tx(priv))796; /* nothing */797798local_bh_enable();799}800801int ipoib_ib_dev_stop(struct net_device *dev, int flush)802{803struct ipoib_dev_priv *priv = netdev_priv(dev);804struct ib_qp_attr qp_attr;805unsigned long begin;806struct ipoib_tx_buf *tx_req;807int i;808809if (test_and_clear_bit(IPOIB_FLAG_INITIALIZED, &priv->flags))810napi_disable(&priv->napi);811812ipoib_cm_dev_stop(dev);813814/*815* Move our QP to the error state and then reinitialize in816* when all work requests have completed or have been flushed.817*/818qp_attr.qp_state = IB_QPS_ERR;819if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))820ipoib_warn(priv, "Failed to modify QP to ERROR state\n");821822/* Wait for all sends and receives to complete */823begin = jiffies;824825while (priv->tx_head != priv->tx_tail || recvs_pending(dev)) {826if (time_after(jiffies, begin + 5 * HZ)) {827ipoib_warn(priv, "timing out; %d sends %d receives not completed\n",828priv->tx_head - priv->tx_tail, recvs_pending(dev));829830/*831* assume the HW is wedged and just free up832* all our pending work requests.833*/834while ((int) priv->tx_tail - (int) priv->tx_head < 0) {835tx_req = &priv->tx_ring[priv->tx_tail &836(ipoib_sendq_size - 1)];837ipoib_dma_unmap_tx(priv->ca, tx_req);838dev_kfree_skb_any(tx_req->skb);839++priv->tx_tail;840--priv->tx_outstanding;841}842843for (i = 0; i < ipoib_recvq_size; ++i) {844struct ipoib_rx_buf *rx_req;845846rx_req = &priv->rx_ring[i];847if (!rx_req->skb)848continue;849ipoib_ud_dma_unmap_rx(priv,850priv->rx_ring[i].mapping);851dev_kfree_skb_any(rx_req->skb);852rx_req->skb = NULL;853}854855goto timeout;856}857858ipoib_drain_cq(dev);859860msleep(1);861}862863ipoib_dbg(priv, "All sends and receives done.\n");864865timeout:866del_timer_sync(&priv->poll_timer);867qp_attr.qp_state = IB_QPS_RESET;868if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))869ipoib_warn(priv, "Failed to modify QP to RESET state\n");870871/* Wait for all AHs to be reaped */872set_bit(IPOIB_STOP_REAPER, &priv->flags);873cancel_delayed_work(&priv->ah_reap_task);874if (flush)875flush_workqueue(ipoib_workqueue);876877begin = jiffies;878879while (!list_empty(&priv->dead_ahs)) {880__ipoib_reap_ah(dev);881882if (time_after(jiffies, begin + HZ)) {883ipoib_warn(priv, "timing out; will leak address handles\n");884break;885}886887msleep(1);888}889890ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP);891892return 0;893}894895int ipoib_ib_dev_init(struct net_device *dev, struct ib_device *ca, int port)896{897struct ipoib_dev_priv *priv = netdev_priv(dev);898899priv->ca = ca;900priv->port = port;901priv->qp = NULL;902903if (ipoib_transport_dev_init(dev, ca)) {904printk(KERN_WARNING "%s: ipoib_transport_dev_init failed\n", ca->name);905return -ENODEV;906}907908setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,909(unsigned long) dev);910911if (dev->flags & IFF_UP) {912if (ipoib_ib_dev_open(dev)) {913ipoib_transport_dev_cleanup(dev);914return -ENODEV;915}916}917918return 0;919}920921static void __ipoib_ib_dev_flush(struct ipoib_dev_priv *priv,922enum ipoib_flush_level level)923{924struct ipoib_dev_priv *cpriv;925struct net_device *dev = priv->dev;926u16 new_index;927928mutex_lock(&priv->vlan_mutex);929930/*931* Flush any child interfaces too -- they might be up even if932* the parent is down.933*/934list_for_each_entry(cpriv, &priv->child_intfs, list)935__ipoib_ib_dev_flush(cpriv, level);936937mutex_unlock(&priv->vlan_mutex);938939if (!test_bit(IPOIB_FLAG_INITIALIZED, &priv->flags)) {940ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_INITIALIZED not set.\n");941return;942}943944if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {945ipoib_dbg(priv, "Not flushing - IPOIB_FLAG_ADMIN_UP not set.\n");946return;947}948949if (level == IPOIB_FLUSH_HEAVY) {950if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &new_index)) {951clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);952ipoib_ib_dev_down(dev, 0);953ipoib_ib_dev_stop(dev, 0);954if (ipoib_pkey_dev_delay_open(dev))955return;956}957958/* restart QP only if P_Key index is changed */959if (test_and_set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags) &&960new_index == priv->pkey_index) {961ipoib_dbg(priv, "Not flushing - P_Key index not changed.\n");962return;963}964priv->pkey_index = new_index;965}966967if (level == IPOIB_FLUSH_LIGHT) {968ipoib_mark_paths_invalid(dev);969ipoib_mcast_dev_flush(dev);970}971972if (level >= IPOIB_FLUSH_NORMAL)973ipoib_ib_dev_down(dev, 0);974975if (level == IPOIB_FLUSH_HEAVY) {976ipoib_ib_dev_stop(dev, 0);977ipoib_ib_dev_open(dev);978}979980/*981* The device could have been brought down between the start and when982* we get here, don't bring it back up if it's not configured up983*/984if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) {985if (level >= IPOIB_FLUSH_NORMAL)986ipoib_ib_dev_up(dev);987ipoib_mcast_restart_task(&priv->restart_task);988}989}990991void ipoib_ib_dev_flush_light(struct work_struct *work)992{993struct ipoib_dev_priv *priv =994container_of(work, struct ipoib_dev_priv, flush_light);995996__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_LIGHT);997}998999void ipoib_ib_dev_flush_normal(struct work_struct *work)1000{1001struct ipoib_dev_priv *priv =1002container_of(work, struct ipoib_dev_priv, flush_normal);10031004__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_NORMAL);1005}10061007void ipoib_ib_dev_flush_heavy(struct work_struct *work)1008{1009struct ipoib_dev_priv *priv =1010container_of(work, struct ipoib_dev_priv, flush_heavy);10111012__ipoib_ib_dev_flush(priv, IPOIB_FLUSH_HEAVY);1013}10141015void ipoib_ib_dev_cleanup(struct net_device *dev)1016{1017struct ipoib_dev_priv *priv = netdev_priv(dev);10181019ipoib_dbg(priv, "cleaning up ib_dev\n");10201021ipoib_mcast_stop_thread(dev, 1);1022ipoib_mcast_dev_flush(dev);10231024ipoib_transport_dev_cleanup(dev);1025}10261027/*1028* Delayed P_Key Assigment Interim Support1029*1030* The following is initial implementation of delayed P_Key assigment1031* mechanism. It is using the same approach implemented for the multicast1032* group join. The single goal of this implementation is to quickly address1033* Bug #2507. This implementation will probably be removed when the P_Key1034* change async notification is available.1035*/10361037void ipoib_pkey_poll(struct work_struct *work)1038{1039struct ipoib_dev_priv *priv =1040container_of(work, struct ipoib_dev_priv, pkey_poll_task.work);1041struct net_device *dev = priv->dev;10421043ipoib_pkey_dev_check_presence(dev);10441045if (test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))1046ipoib_open(dev);1047else {1048mutex_lock(&pkey_mutex);1049if (!test_bit(IPOIB_PKEY_STOP, &priv->flags))1050queue_delayed_work(ipoib_workqueue,1051&priv->pkey_poll_task,1052HZ);1053mutex_unlock(&pkey_mutex);1054}1055}10561057int ipoib_pkey_dev_delay_open(struct net_device *dev)1058{1059struct ipoib_dev_priv *priv = netdev_priv(dev);10601061/* Look for the interface pkey value in the IB Port P_Key table and */1062/* set the interface pkey assigment flag */1063ipoib_pkey_dev_check_presence(dev);10641065/* P_Key value not assigned yet - start polling */1066if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) {1067mutex_lock(&pkey_mutex);1068clear_bit(IPOIB_PKEY_STOP, &priv->flags);1069queue_delayed_work(ipoib_workqueue,1070&priv->pkey_poll_task,1071HZ);1072mutex_unlock(&pkey_mutex);1073return 1;1074}10751076return 0;1077}107810791080