From patchwork Tue Dec 4 12:04:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gal Pressman X-Patchwork-Id: 10711641 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2A0D213AF for ; Tue, 4 Dec 2018 12:05:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C7F22A35B for ; Tue, 4 Dec 2018 12:05:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 10B1A2AE4E; Tue, 4 Dec 2018 12:05:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AF96E2A35B for ; Tue, 4 Dec 2018 12:05:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726152AbeLDMF3 (ORCPT ); Tue, 4 Dec 2018 07:05:29 -0500 Received: from smtp-fw-2101.amazon.com ([72.21.196.25]:31252 "EHLO smtp-fw-2101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726097AbeLDMF3 (ORCPT ); Tue, 4 Dec 2018 07:05:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1543925128; x=1575461128; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=w/dER8QoYguplwOH8reFdZEVsBcMeXkMiMNLzS3caUU=; b=uNzBeiXs+4tpU5nX1Bo4rNKpHhSWZUDNlxkTEt6LM04GcDAxuCAxHpqT GNEpslytX0vtDO9MOggd+fEdBMKwmP5EQ5fZhRidNcS43/QeUjwWNZjke ymbfDgRH+1JSe0UQYxWXLWWpGpumxrjHkwEaz368Pe7Cd3rk/AgUgGg0Y Y=; X-IronPort-AV: E=Sophos;i="5.56,253,1539648000"; d="scan'208";a="707203105" Received: from iad6-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com) ([10.124.125.2]) by smtp-border-fw-out-2101.iad2.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 04 Dec 2018 12:05:28 +0000 Received: from EX13MTAUEA001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1e-97fdccfd.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id wB4C5IWN071272 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL); Tue, 4 Dec 2018 12:05:27 GMT Received: from EX13D02EUC003.ant.amazon.com (10.43.164.10) by EX13MTAUEA001.ant.amazon.com (10.43.61.82) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 4 Dec 2018 12:05:14 +0000 Received: from EX13MTAUEB001.ant.amazon.com (10.43.60.96) by EX13D02EUC003.ant.amazon.com (10.43.164.10) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Tue, 4 Dec 2018 12:05:13 +0000 Received: from galpress-VirtualBox.hfa16.amazon.com (10.218.62.26) by mail-relay.amazon.com (10.43.60.129) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Tue, 4 Dec 2018 12:05:10 +0000 From: Gal Pressman To: Doug Ledford , Jason Gunthorpe CC: Alexander Matushevsky , Yossi Leybovich , , Tom Tucker , Gal Pressman Subject: [PATCH rdma-next 10/13] RDMA/efa: Add bitmap allocation service Date: Tue, 4 Dec 2018 14:04:26 +0200 Message-ID: <1543925069-8838-11-git-send-email-galpress@amazon.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1543925069-8838-1-git-send-email-galpress@amazon.com> References: <1543925069-8838-1-git-send-email-galpress@amazon.com> MIME-Version: 1.0 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Bitmap allocation service is currently used for assigning Protection Domain (PD) numbers. Signed-off-by: Gal Pressman --- drivers/infiniband/hw/efa/efa_bitmap.c | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 drivers/infiniband/hw/efa/efa_bitmap.c diff --git a/drivers/infiniband/hw/efa/efa_bitmap.c b/drivers/infiniband/hw/efa/efa_bitmap.c new file mode 100644 index 000000000000..251cc68d25f5 --- /dev/null +++ b/drivers/infiniband/hw/efa/efa_bitmap.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB +/* + * Copyright 2006, 2007 Cisco Systems, Inc. All rights reserved. + * Copyright 2007, 2008 Mellanox Technologies. All rights reserved. + * Copyright 2018 Amazon.com, Inc. or its affiliates. + */ + +#include + +#include "efa.h" + +u32 efa_bitmap_alloc(struct efa_bitmap *bitmap) +{ + u32 obj; + + spin_lock(&bitmap->lock); + + obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last); + if (obj >= bitmap->max) + obj = find_first_zero_bit(bitmap->table, bitmap->max); + + if (obj < bitmap->max) { + set_bit(obj, bitmap->table); + bitmap->last = obj + 1; + if (bitmap->last == bitmap->max) + bitmap->last = 0; + } else { + obj = EFA_BITMAP_INVAL; + } + + if (obj != EFA_BITMAP_INVAL) + --bitmap->avail; + + spin_unlock(&bitmap->lock); + + return obj; +} + +void efa_bitmap_free(struct efa_bitmap *bitmap, u32 obj) +{ + obj &= bitmap->mask; + + spin_lock(&bitmap->lock); + bitmap_clear(bitmap->table, obj, 1); + bitmap->avail++; + spin_unlock(&bitmap->lock); +} + +u32 efa_bitmap_avail(struct efa_bitmap *bitmap) +{ + return bitmap->avail; +} + +int efa_bitmap_init(struct efa_bitmap *bitmap, u32 num) +{ + /* num must be a power of 2 */ + if (!is_power_of_2(num)) + return -EINVAL; + + bitmap->last = 0; + bitmap->max = num; + bitmap->mask = num - 1; + bitmap->avail = num; + spin_lock_init(&bitmap->lock); + bitmap->table = kcalloc(BITS_TO_LONGS(bitmap->max), + sizeof(long), GFP_KERNEL); + if (!bitmap->table) + return -ENOMEM; + + return 0; +} + +void efa_bitmap_cleanup(struct efa_bitmap *bitmap) +{ + kfree(bitmap->table); +}