diff --git a/sys/compat/linuxkpi/common/include/asm/uaccess.h b/sys/compat/linuxkpi/common/include/asm/uaccess.h index 0ae20b09dc8c..102373634ba3 100644 --- a/sys/compat/linuxkpi/common/include/asm/uaccess.h +++ b/sys/compat/linuxkpi/common/include/asm/uaccess.h @@ -1,72 +1,68 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _ASM_UACCESS_H_ #define _ASM_UACCESS_H_ #include static inline long copy_to_user(void *to, const void *from, unsigned long n) { if (linux_copyout(from, to, n) != 0) return n; return 0; } #define __copy_to_user(...) copy_to_user(__VA_ARGS__) static inline long copy_from_user(void *to, const void *from, unsigned long n) { if (linux_copyin(from, to, n) != 0) return n; return 0; } #define __copy_from_user(...) copy_from_user(__VA_ARGS__) #define __copy_in_user(...) copy_from_user(__VA_ARGS__) -#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000 #define user_access_begin(ptr, len) access_ok(ptr, len) -#else -#define user_access_begin() do { } while (0) -#endif #define user_access_end() do { } while (0) #define unsafe_get_user(x, ptr, err) do { \ if (unlikely(__get_user(x, ptr))) \ goto err; \ } while (0) #define unsafe_put_user(x, ptr, err) do { \ if (unlikely(__put_user(x, ptr))) \ goto err; \ } while (0) #endif /* _ASM_UACCESS_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/compiler.h b/sys/compat/linuxkpi/common/include/linux/compiler.h index 1177674aa68f..d8aef4fa7988 100644 --- a/sys/compat/linuxkpi/common/include/linux/compiler.h +++ b/sys/compat/linuxkpi/common/include/linux/compiler.h @@ -1,117 +1,114 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * Copyright (c) 2015 François Tigeot * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _LINUX_COMPILER_H_ #define _LINUX_COMPILER_H_ #include #define __user #define __kernel #define __safe #define __force #define __nocast #define __iomem #define __chk_user_ptr(x) ((void)0) #define __chk_io_ptr(x) ((void)0) #define __builtin_warning(x, y...) (1) #define __acquires(x) #define __releases(x) #define __acquire(x) do { } while (0) #define __release(x) do { } while (0) #define __cond_lock(x,c) (c) #define __bitwise #define __devinitdata #define __deprecated #define __init #define __initconst #define __devinit #define __devexit #define __exit #define __rcu #define __percpu #define __weak __weak_symbol #define __malloc #define ___stringify(...) #__VA_ARGS__ #define __stringify(...) ___stringify(__VA_ARGS__) #define __attribute_const__ __attribute__((__const__)) #undef __always_inline #define __always_inline inline #define noinline __noinline #define ____cacheline_aligned __aligned(CACHE_LINE_SIZE) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #define typeof(x) __typeof(x) #define uninitialized_var(x) x = x #define __maybe_unused __unused #define __always_unused __unused #define __must_check __result_use_check #define __printf(a,b) __printflike(a,b) #define barrier() __asm__ __volatile__("": : :"memory") -#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000 -/* Moved from drm_os_freebsd.h */ #define lower_32_bits(n) ((u32)(n)) #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) -#endif #define ___PASTE(a,b) a##b #define __PASTE(a,b) ___PASTE(a,b) #define ACCESS_ONCE(x) (*(volatile __typeof(x) *)&(x)) #define WRITE_ONCE(x,v) do { \ barrier(); \ ACCESS_ONCE(x) = (v); \ barrier(); \ } while (0) #define READ_ONCE(x) ({ \ __typeof(x) __var = ({ \ barrier(); \ ACCESS_ONCE(x); \ }); \ barrier(); \ __var; \ }) #define lockless_dereference(p) READ_ONCE(p) #define _AT(T,X) ((T)(X)) #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) #define __must_be_array(a) __same_type(a, &(a)[0]) #endif /* _LINUX_COMPILER_H_ */ diff --git a/sys/compat/linuxkpi/common/include/linux/uaccess.h b/sys/compat/linuxkpi/common/include/linux/uaccess.h index c09c363a98a7..10a06a220f86 100644 --- a/sys/compat/linuxkpi/common/include/linux/uaccess.h +++ b/sys/compat/linuxkpi/common/include/linux/uaccess.h @@ -1,96 +1,92 @@ /*- * Copyright (c) 2010 Isilon Systems, Inc. * Copyright (c) 2010 iX Systems, Inc. * Copyright (c) 2010 Panasas, Inc. * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. * Copyright (c) 2015 François Tigeot * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _LINUX_UACCESS_H_ #define _LINUX_UACCESS_H_ #include #include #include #include #include #include #define VERIFY_READ VM_PROT_READ #define VERIFY_WRITE VM_PROT_WRITE #define __get_user(_x, _p) ({ \ int __err; \ __typeof(*(_p)) __x; \ __err = linux_copyin((_p), &(__x), sizeof(*(_p))); \ (_x) = __x; \ __err; \ }) #define __put_user(_x, _p) ({ \ __typeof(*(_p)) __x = (_x); \ linux_copyout(&(__x), (_p), sizeof(*(_p))); \ }) #define get_user(_x, _p) linux_copyin((_p), &(_x), sizeof(*(_p))) #define put_user(_x, _p) __put_user(_x, _p) #define clear_user(...) linux_clear_user(__VA_ARGS__) -#if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000 #define access_ok(a,b) linux_access_ok(a,b) -#else -#define access_ok(a,b,c) linux_access_ok(b,c) -#endif extern int linux_copyin(const void *uaddr, void *kaddr, size_t len); extern int linux_copyout(const void *kaddr, void *uaddr, size_t len); extern size_t linux_clear_user(void *uaddr, size_t len); extern int linux_access_ok(const void *uaddr, size_t len); /* * NOTE: Each pagefault_disable() call must have a corresponding * pagefault_enable() call in the same scope. The former creates a new * block and defines a temporary variable, and the latter uses the * temporary variable and closes the block. Failure to balance the * calls will result in a compile-time error. */ #define pagefault_disable(void) do { \ int __saved_pflags = \ vm_fault_disable_pagefaults() #define pagefault_enable(void) \ vm_fault_enable_pagefaults(__saved_pflags); \ } while (0) static inline bool pagefault_disabled(void) { return ((curthread->td_pflags & TDP_NOFAULTING) != 0); } #endif /* _LINUX_UACCESS_H_ */ diff --git a/sys/dev/pms/freebsd/driver/common/lxencrypt.c b/sys/dev/pms/freebsd/driver/common/lxencrypt.c index c53c52fb2378..0d469f9fdbf7 100644 --- a/sys/dev/pms/freebsd/driver/common/lxencrypt.c +++ b/sys/dev/pms/freebsd/driver/common/lxencrypt.c @@ -1,1165 +1,1165 @@ /******************************************************************************* *Copyright (c) 2014 PMC-Sierra, Inc. All rights reserved. * *Redistribution and use in source and binary forms, with or without modification, are permitted provided *that the following conditions are met: *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the *following disclaimer. *2. Redistributions in binary form must reproduce the above copyright notice, *this list of conditions and the following disclaimer in the documentation and/or other materials provided *with the distribution. * *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE * * $FreeBSD$ * *******************************************************************************/ /******************************************************************************* ** ** Version Control Information: ** ** $Revision: 113920 $ ** $Author: mcleanda $ ** $Date: 2012-05-08 11:30:44 -0700 (Tue, 08 May 2012) $ ** $Id: lxencrypt.c 113920 2012-05-08 18:30:44Z mcleanda $ ** *******************************************************************************/ #include #include #include #include #include #include #ifdef ENCRYPT_ENHANCE static atomic_t ioerr_queue_count; /****************************************************************************** careful_write(): Purpose: Parameters: Return: Note: ******************************************************************************/ static int careful_write(char *buf, int offset, int max, const char *fmt, ...) { static char s[PAGE_SIZE]; /* Assumes serialization */ va_list args; int i; if(offset > max) return 0; s[PAGE_SIZE - 1] = '\0'; va_start(args, fmt); i = vsnprintf(s, PAGE_SIZE - 1, fmt, args); if((offset + i) > max) return 0; memcpy(buf + offset, s, i); va_end(args); return i; } /****************************************************************************** set_dek_table_entry(): Purpose: Parameters: Return: Note: ******************************************************************************/ static inline int set_dek_table_entry(struct device *dev, const char *buf, size_t len, dek_table_e table) { int index; struct Scsi_Host *shost = class_to_shost(dev); struct agtiapi_softc *pCard = (struct agtiapi_softc *) shost->hostdata; /* Check permissions */ if(!capable(CAP_SYS_ADMIN)) return -EACCES; if(!pCard->encrypt) return -EINVAL; if(table != DEK_TABLE_0 && table != DEK_TABLE_1) return -EINVAL; sscanf(buf, "%d", &index); if(index >= 0 && index < DEK_MAX_TABLE_ITEMS) { pCard->dek_index[table] = index; return strlen(buf); } return -EINVAL; } /****************************************************************************** set_dek_table_entry0(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t set_dek_table_entry0(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { return set_dek_table_entry(dev, buf, len, DEK_TABLE_0); } /****************************************************************************** set_dek_table_entry1(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t set_dek_table_entry1(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { return set_dek_table_entry(dev, buf, len, DEK_TABLE_1); } /****************************************************************************** show_dek_table_entry(): Purpose: Parameters: Return: Note: ******************************************************************************/ static inline int show_dek_table_entry(struct device *dev, char *buf, unsigned int table) { int i = 0, j; unsigned char *p; struct Scsi_Host *sh = class_to_shost(dev); ag_card_t *pCard = (ag_card_t *) sh->hostdata; ag_card_info_t *pCardInfo = pCard->pCardInfo; ag_resource_info_t *pRscInfo = &pCardInfo->tiRscInfo; tiEncryptDekBlob_t *pDekTable = NULL; if(!pCard->encrypt) return -EINVAL; if(table == DEK_TABLE_0) pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_1].virtPtr; else if(table == DEK_TABLE_1) pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_2].virtPtr; if(pDekTable == NULL) return -EINVAL; if(pCard->dek_index[table] >= 0 || pCard->dek_index[table] < DEK_MAX_TABLE_ITEMS) { i += careful_write(buf, i, PAGE_SIZE, "%4d: ", pCard->dek_index[table]); p = (unsigned char *) &pDekTable[pCard->dek_index[table]]; for(j = 0; j < sizeof(tiEncryptDekBlob_t); j++) { i += careful_write(buf, i, PAGE_SIZE, "%02x", p[j]); } i += careful_write(buf, i, PAGE_SIZE, "\n"); } else { i += careful_write(buf, i, PAGE_SIZE, "Bad DEK index %d; range: 0 - %d\n", pCard->dek_index[table], DEK_MAX_TABLE_ITEMS); } /* BUG if we return more than a single page of data */ //BUG_ON(i > PAGE_SIZE); if (i > PAGE_SIZE) i = PAGE_SIZE; return i; } /****************************************************************************** show_dek_table_entry0(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_dek_table_entry0(struct device *dev, struct device_attribute *attr, char *buf) { return show_dek_table_entry(dev, buf, DEK_TABLE_0); } /****************************************************************************** show_dek_table_entry1(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_dek_table_entry1(struct device *dev, struct device_attribute *attr, char *buf) { return show_dek_table_entry(dev, buf, DEK_TABLE_1); } /****************************************************************************** show_kek_table(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_kek_table(struct device *dev, struct device_attribute *attr, char *buf) { int i = 0, j, kek_index; unsigned char *p; struct Scsi_Host *sh = class_to_shost(dev); ag_card_t *pCard = (ag_card_t *) sh->hostdata; if(!pCard->encrypt) return -EINVAL; for(kek_index = 0; kek_index < KEK_TABLE_MAX_ENTRY; kek_index++) { i += careful_write(buf, i, PAGE_SIZE, " %4d: %08x ", kek_index, pCard->kek_table[kek_index].wrapperIndex); p = (unsigned char *) &pCard->kek_table[kek_index].kekBlob; for(j = 0; j < sizeof(tiEncryptKekBlob_t); j++) { i += careful_write(buf, i, PAGE_SIZE, "%02x", p[j]); } i += careful_write(buf, i, PAGE_SIZE, "\n"); } i += careful_write(buf, i, PAGE_SIZE, "\n"); /* BUG if we return more than a single page of data */ //BUG_ON(i > PAGE_SIZE); if (i > PAGE_SIZE) i = PAGE_SIZE; return i; } /****************************************************************************** show_dek_kek_map(): Purpose: Parameters: Return: Note: ******************************************************************************/ static inline int show_dek_kek_map(struct device *dev, char *buf, unsigned int table) { int i = 0, dek_index; struct Scsi_Host *sh = class_to_shost(dev); ag_card_t *pCard = (ag_card_t *) sh->hostdata; if(!pCard->encrypt) return -EINVAL; if(table != DEK_TABLE_0 && table != DEK_TABLE_1) return -EINVAL; i += careful_write(buf, i, PAGE_SIZE, "Table %d\n", table); i += careful_write(buf, i, PAGE_SIZE, "=======\n"); for(dek_index = 0; dek_index < DEK_MAX_TABLE_ITEMS; dek_index++) { i += careful_write(buf, i, PAGE_SIZE, " %4d: %08x\n", dek_index, pCard->dek_kek_map[table][dek_index].kekIndex); } i += sprintf(buf + i, "\n"); /* BUG if we return more than a single page of data */ //BUG_ON(i > PAGE_SIZE); if (i > PAGE_SIZE) i = PAGE_SIZE; return i; } /****************************************************************************** show_dek_kek_map0(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_dek_kek_map0(struct device *dev, struct device_attribute *attr, char *buf) { return show_dek_kek_map(dev, buf, 0); } /****************************************************************************** show_dek_kek_map1(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_dek_kek_map1(struct device *dev, struct device_attribute *attr, char *buf) { return show_dek_kek_map(dev, buf, 1); } /****************************************************************************** show_target_dek_map(): Purpose: Parameters: Return: Note: ******************************************************************************/ ssize_t show_target_dek_map(struct device *dev, struct device_attribute *attr, char *buf) { int i = 0; unsigned int chan, device, lun = 0; ag_encrypt_map_t *p; struct list_head *lh; struct Scsi_Host *sh = class_to_shost(dev); ag_card_t *pCard = (ag_card_t *) sh->hostdata; if(!pCard->encrypt) return -EINVAL; for(chan = 0; chan <= AGTIAPI_MAX_CHANNEL_NUM; chan++) { for(device = 0; device < pCard->devDiscover; device++) { #ifdef REPORT_ALL_LUNS for(lun = 0; lun < AGTIAPI_MAX_LUN; lun++) { #endif lh = MAP_TABLE_ENTRY(pCard, chan, device, lun); if(lh) { list_for_each_entry(p, lh, list) { if(p->dekIndex != DEK_INDEX_INVALID) i += careful_write(buf, i, PAGE_SIZE, " %u:%u:%u: %x %8x %8x %16lx %16lx %08x:%08x %1x\n", chan, device, lun, p->dekTable, p->dekIndex, p->kekIndex, p->lbaMin, p->lbaMax, p->keyTag[1], p->keyTag[0], p->keyTagCheck); } } #ifdef REPORT_ALL_LUNS } #endif } } if (i > PAGE_SIZE) i = PAGE_SIZE; return i; } /****************************************************************************** agtiapi_AddDek(): Purpose: Parameters: Return: Note: ******************************************************************************/ static int agtiapi_AddDek(ag_card_t *pCard, bit32 dek_table, bit32 dek_index, bit32 blob_format, bit32 entry_sz, tiEncryptDekBlob_t *dek_blob, U32_64 *addr) { ag_resource_info_t *pRscInfo = &pCard->pCardInfo->tiRscInfo; tiEncryptDekBlob_t *pDekTable; char *p; if (dek_index >= DEK_MAX_TABLE_ITEMS) { printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS); return -E_DEK_INDEX; } switch(dek_table) { case DEK_TABLE_0: pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_1].virtPtr; break; case DEK_TABLE_1: pDekTable = pRscInfo->tiLoLevelResource.loLevelMem.mem[DEK_MEM_INDEX_2].virtPtr; break; default: printf("%s: Unknown dek table %d\n", __FUNCTION__, dek_table); return -E_DEK_TABLE; } #ifdef __VMKLNX__ *addr = (U32_64) __pa(&pDekTable[0]); #else *addr = (U32_64) virt_to_phys(&pDekTable[0]); #endif p = (char *) &pDekTable[0] + (dek_index * pCard->dek_size); printf("%s: Base: %p, Index: %08x, Virt: %p Size: %d\n", __FUNCTION__, pDekTable, dek_index, &pDekTable[dek_index], pCard->dek_size); memcpy(p, dek_blob, pCard->dek_size); wmb(); /* Flush entry */ ostiCacheFlush(&pCard->tiRoot, NULL, p, pCard->dek_size); return 0; } /****************************************************************************** agtiapi_MapDekKek(): Purpose: Parameters: Return: Note: ******************************************************************************/ static int agtiapi_MapDekKek(ag_card_t *pCard, bit32 dek_table, bit32 dek_index, bit32 kek_index) { if (dek_index >= DEK_MAX_TABLE_ITEMS) { printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS); return -E_DEK_INDEX; } if (dek_table >= DEK_MAX_TABLES) { printf("%s: Bad dek table.\n", __FUNCTION__); return -E_DEK_TABLE; } if (kek_index >= KEK_TABLE_MAX_ENTRY) { printf("%s: Bad kek index.\n", __FUNCTION__); return -E_KEK_INDEX; } pCard->dek_kek_map[dek_table][dek_index].kekIndex = kek_index; return 0; } /****************************************************************************** agtiapi_AddKek(): Purpose: Parameters: Return: Note: ******************************************************************************/ static int agtiapi_AddKek(ag_card_t *pCard, bit32 kek_index, bit32 wrapper_kek_index, tiEncryptKekBlob_t *kek_blob) { if (kek_index >= KEK_TABLE_MAX_ENTRY) { printf("%s: Bad kek index.\n", __FUNCTION__); return -E_KEK_INDEX; } if (wrapper_kek_index >= KEK_TABLE_MAX_ENTRY) { printf("%s: Bad kek wrapper index.\n", __FUNCTION__); return -E_KEK_INDEX; } pCard->kek_table[kek_index].wrapperIndex = wrapper_kek_index; memcpy(&pCard->kek_table[kek_index].kekBlob, kek_blob, sizeof(tiEncryptKekBlob_t)); return 0; } /****************************************************************************** agtiapi_MapDek(): Purpose: Parameters: Return: Note: ******************************************************************************/ static int agtiapi_MapDek(ag_card_t *pCard, EncryptDeviceDekMap_t *dek_map) { int found = 0; bit32 chan, device, lun; bit32 dek_table, dek_index, kek_index; unsigned long long lba_min, lba_max; ag_encrypt_map_t *p, *n; struct list_head *lh; chan = dek_map->channel; device = dek_map->device; lun = dek_map->lun; lba_min = dek_map->dekMapEntry[0].startLBA; lba_max = dek_map->dekMapEntry[0].endLBA; dek_table = dek_map->dekMapEntry[0].dek.dekTable; dek_index = dek_map->dekMapEntry[0].dek.dekIndex; /* Sanity check channel, device, lun */ if (chan > AGTIAPI_MAX_CHANNEL_NUM) { printf("%s: Bad channel %d.\n", __FUNCTION__, chan); return -E_CHANNEL_INDEX; } if (device >= pCard->devDiscover) { printf("%s: Bad device %d.\n", __FUNCTION__, device); return -E_DEVICE_INDEX; } if (lun >= AGTIAPI_MAX_LUN) { printf("%s: Bad lun %d.\n", __FUNCTION__, lun); return -E_LUN_INDEX; } /* Sanity check dek index */ if (dek_index >= DEK_MAX_TABLE_ITEMS) { printf("%s: Bad dek index 0x%x (MAX: 0x%x).\n", __FUNCTION__, dek_index, DEK_MAX_TABLE_ITEMS); return -E_DEK_INDEX; } /* Sanity check dek table */ if (dek_table >= DEK_MAX_TABLES) { printf("%s: Bad dek table %d.\n", __FUNCTION__, dek_table); return -E_DEK_TABLE; } /* Check that lba min and lba max are sane */ if (lba_min >= lba_max) { printf("%s: Bad lba min and lba max: %llx %llx.\n", __FUNCTION__, lba_min, lba_max); return -E_LBA_RANGE; } /* dek_table and dek_index are valid, look up kek */ kek_index = pCard->dek_kek_map[dek_table][dek_index].kekIndex; lh = MAP_TABLE_ENTRY(pCard, chan, device, lun); if (dek_map->dekMapEntry[0].flags & ENCRYPT_DEK_MAP_ENTRY_CLEAR) { /* Delete the entry */ found = 0; list_for_each_entry_safe(p, n, lh, list) { if (p->lbaMin == lba_min && p->lbaMax == lba_max && p->dekTable == dek_table && p->dekIndex == dek_index && p->kekIndex == kek_index) { /* Entry found, unlink and reclaim it */ found = 1; list_del(&p->list); mempool_free(p, pCard->map_mempool); } } if (!found) { printf("%s: Entry %x %x %x %llx %llx not found.\n", __FUNCTION__, dek_table, dek_index, kek_index, lba_min, lba_max); return -E_NOT_FOUND; } } else if (dek_map->dekMapEntry[0].flags & ENCRYPT_DEK_MAP_ENTRY_VALID) { /* Add the entry */ p = (ag_encrypt_map_t *)uma_zalloc(pCard->map_cache, M_WAITOK); //Encryption if (!p) { printf("%s: Unable to allocate from memory pool.\n", __FUNCTION__); return -E_MEMPOOL_ALLOC; } /* Populate it */ p->lbaMin = lba_min; p->lbaMax = lba_max; p->dekTable = dek_table; p->dekIndex = dek_index; p->kekIndex = kek_index; p->keyTagCheck = dek_map->keytag_check; memcpy(&p->keyTag, &dek_map->keytag, sizeof(p->keyTag)); /* Test to see if this new mapping overlaps an existing mapping */ list_for_each_entry(n, lh, list) { /* * Check if the start lba falls in existing range || * Check if the end lba falls in existing range || * Check if the start lba of the existing range falls in the new range */ if (((p->lbaMin >= n->lbaMin) && (p->lbaMin <= n->lbaMax)) || ((p->lbaMax >= n->lbaMin) && (p->lbaMax <= n->lbaMax)) || ((n->lbaMin >= p->lbaMin) && (n->lbaMin <= p->lbaMax))) { printf("%s: WARNING: New entry lba range overlap: %llx - %llx vs %llx - %llx.\n", __FUNCTION__, p->lbaMin, p->lbaMax, n->lbaMin, n->lbaMax); } } /* Link it in to list at the head so it takes precedence */ list_add(&p->list, lh); /* TODO: Decide if/how to refcount each dek/kek index used by the mapping */ } else { printf("%s: Bad flags %08x\n", __FUNCTION__, dek_map->dekMapEntry[0].flags); return -E_FLAGS; } return 0; } #endif #ifdef HIALEAH_ENCRYPTION /****************************************************************************** agtiapi_SetupEncryption(): Purpose: Parameters: Return: Note: ******************************************************************************/ int agtiapi_SetupEncryption(struct agtiapi_softc *pCard) { tiRoot_t *tiRoot = (tiRoot_t *) &pCard->tiRoot; bit32 status = tiSuccess; printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION\n"); if (pCard->encrypt == agTRUE) { status = tiCOMEncryptGetInfo(tiRoot); printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION tiCOMEncryptGetInfo Status 0x%x\n",status); if(status == 1 ) { status = tiCOMEncryptHilSet(tiRoot ); if (status) { pCard->encrypt = agFALSE; printf("agtiapi_SetupEncryption: HIALEAH_ENCRYPTION not set\n"); } } } return 0; } #ifdef ENCRYPT_ENHANCE /****************************************************************************** agtiapi_SetupEncryptionPools(): Purpose: Parameters: Return: Note: ******************************************************************************/ int agtiapi_SetupEncryptionPools(struct agtiapi_softc *pCard) { /* Configure encryption memory pool */ memset(pCard->map_cache_name, 0, sizeof(pCard->map_cache_name)); snprintf(pCard->map_cache_name, sizeof(pCard->map_cache_name) - 1, "map_cache_%d", pCard->cardNo); //zone allocation pCard->map_cache = uma_zcreate(pCard->map_cache_name, sizeof(ag_encrypt_map_t),NULL, NULL, NULL, NULL, 0, 0); if(!pCard->map_cache) { /* * This error may be due to an existing cache in the kernel * from an earlier kmem_cache that wasn't properly freed */ printf("Unable to create uma_zcreate cache for encryption map mempool.\n"); return -EFAULT; } uma_zone_set_max(pCard->map_cache, ENCRYPTION_MAP_MEMPOOL_SIZE); /* Configure encryption IO error pool */ INIT_LIST_HEAD(&pCard->ioerr_queue); /*#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)) // #### pCard->ioerr_queue_lock = SPIN_LOCK_UNLOCKED; #else */ pCard->ioerr_queue_lock = AG_SPIN_UNLOCK(pCard->ioerr_queue_lock); //#endif memset(pCard->ioerr_cache_name, 0, sizeof(pCard->ioerr_cache_name)); snprintf(pCard->ioerr_cache_name, sizeof(pCard->ioerr_cache_name) - 1, "ioerr_cache_%d", pCard->cardNo); pCard->ioerr_cache = uma_zcreate(pCard->ioerr_cache_name, sizeof(ag_encrypt_ioerr_t), NULL, NULL, NULL, NULL, 0, 0); if(!pCard->ioerr_cache) { /* * This error may be due to an existing cache in the kernel * from an earlier kmem_cache that wasn't properly freed */ printf("Unable to create kmem cache for encryption IO error mempool.\n"); return -EFAULT; } uma_zone_set_max(pCard->ioerr_cache, ENCRYPTION_IO_ERR_MEMPOOL_SIZE); /* Set cipher mode to something invalid */ pCard->cipher_mode = CIPHER_MODE_INVALID; return 0; } #endif /****************************************************************************** agtiapi_CleanupEncryption(): Purpose: Parameters: Return: Note: ******************************************************************************/ void agtiapi_CleanupEncryption(struct agtiapi_softc *pCard) { #ifdef ENCRYPT_ENHANCE if(pCard->encrypt_map) { int chan, device, lun; struct list_head *lh; ag_encrypt_map_t *p, *n; for (chan = 0; chan < (AGTIAPI_MAX_CHANNEL_NUM + 1); chan++) { for (device = 0; device < pCard->devDiscover; device++) { for (lun = 0; lun < AGTIAPI_MAX_LUN; lun++) { lh = MAP_TABLE_ENTRY(pCard, chan, device, lun); list_for_each_entry_safe(p, n, lh, list) { // mempool_free(p, pCard->map_mempool); } } } } vfree(pCard->encrypt_map); pCard->encrypt_map = NULL; } #endif } #ifdef ENCRYPT_ENHANCE /****************************************************************************** agtiapi_CleanupEncryptionPools(): Purpose: Parameters: Return: Note: ******************************************************************************/ void agtiapi_CleanupEncryptionPools(struct agtiapi_softc *pCard) { ag_encrypt_ioerr_t *ioerr, *tmp; atomic_set(&ioerr_queue_count); /* * TODO: check "outstanding_encrypted_io_count" for non-zero * and free all mempool items prior to destroying pool */ /* Clean up memory pools */ if (pCard->map_mempool) { mempool_destroy(pCard->map_mempool); printf("Encryption Map mempool released.\n"); pCard->map_mempool = NULL; } /* Clean up kmem cache */ if (pCard->map_cache) { kmem_cache_destroy(pCard->map_cache); printf("Kernel memory cache %s released.\n", pCard->map_cache_name); pCard->map_cache = NULL; } /* Clean up memory pools */ list_for_each_entry_safe(ioerr, tmp, &pCard->ioerr_queue, list) { list_del_init(&ioerr->list); mempool_free(ioerr, pCard->ioerr_mempool); atomic_dec(&ioerr_queue_count); } if (pCard->ioerr_mempool) { mempool_destroy(pCard->ioerr_mempool); printf("Encryption IO Error mempool released.\n"); pCard->ioerr_mempool = NULL; } /* Clean up kmem cache */ if (pCard->ioerr_cache) { kmem_cache_destroy(pCard->ioerr_cache); printf("Kernel memory cache %s released.\n", pCard->ioerr_cache_name); pCard->ioerr_cache = NULL; } } /****************************************************************************** agtiapi_EncryptionIoctl(): Purpose: Parameters: Return: Note: ******************************************************************************/ int agtiapi_EncryptionIoctl(struct agtiapi_softc *pCard, IoctlEncrypt_t *pIoctlPayload) { int rv, rc = 0, skip_wait = 0; tiRoot_t *tiRoot = (tiRoot_t *) &pCard->tiRoot; IoctlTISAEncrypt_t *ioctl_data = &pIoctlPayload->body; pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE; pCard->ioctl_data = (void *) ioctl_data; init_completion(&pCard->ioctl_completion); /* Check that the system is quiesced */ if (atomic_read(&outstanding_encrypted_io_count) != 0) printf("%s: WARNING: Attempting encryption management update with outstanding encrypted IOs!\n", __FUNCTION__); printf("%s: Minor %d\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction); switch(pIoctlPayload->hdr.MinorFunction) { case IOCTL_MN_ENCRYPTION_GET_INFO: { //IoctlEncryptGetInfo_t *get_info = (IoctlEncryptGetInfo_t *) &ioctl_data->request; rc = tiCOMEncryptGetInfo(tiRoot); } break; case IOCTL_MN_ENCRYPTION_SET_MODE: { u32 reg_val = 0, new_cipher_mode = 0; IoctlEncryptSetMode_t *set_mode = (IoctlEncryptSetMode_t *) &ioctl_data->request; printf("%s: input %08x\n", __FUNCTION__, set_mode->securityCipherMode); /* Set security mode */ if(TI_ENCRYPT_SEC_MODE_FACT_INIT) if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_FACT_INIT) { reg_val |= TI_ENCRYPT_SEC_MODE_FACT_INIT; pCard->dek_size = DEK_SIZE_PLAIN; } if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_A) { reg_val |= TI_ENCRYPT_SEC_MODE_A; pCard->dek_size = DEK_SIZE_ENCRYPT; } else if(set_mode->securityCipherMode & TI_ENCRYPT_SEC_MODE_B) { reg_val |= TI_ENCRYPT_SEC_MODE_B; pCard->dek_size = DEK_SIZE_ENCRYPT; } /* Set cipher mode */ if(set_mode->securityCipherMode & TI_ENCRYPT_ATTRIB_CIPHER_XTS) { reg_val |= TI_ENCRYPT_ATTRIB_CIPHER_XTS; new_cipher_mode = TI_ENCRYPT_MODE_XTS_AES; } printf("%s: Setting security cipher mode to: 0x%08x\n", __FUNCTION__, reg_val); pCard->cipher_mode = new_cipher_mode; rc = tiCOMEncryptSetMode(tiRoot, reg_val); } break; case IOCTL_MN_ENCRYPTION_KEK_ADD: { tiEncryptKekBlob_t kek_blob; IoctlEncryptKekAdd_t *kek_add = (IoctlEncryptKekAdd_t *) &ioctl_data->request; printf("%s: Add kek at index 0x%x wrapper 0x%x format 0x%x\n", __FUNCTION__, kek_add->kekIndex, kek_add->wrapperKekIndex, kek_add->blobFormat); /* Copy kek_blob from user pointer to local buffer */ - if(access_ok(VERIFY_READ, kek_add->EncryptKekBlob, sizeof(kek_blob))) { + if(access_ok(kek_add->EncryptKekBlob, sizeof(kek_blob))) { printf("%s: Starting copy from user %p to kernel %p\n", __FUNCTION__, kek_add->EncryptKekBlob, &kek_blob); if((rv = copy_from_user(&kek_blob, kek_add->EncryptKekBlob, sizeof(kek_blob))) != 0) { printf("%s: Copy error, %d left\n", __FUNCTION__, rv); return IOCTL_CALL_FAIL; } rc = tiCOMEncryptKekAdd(tiRoot, kek_add->kekIndex, kek_add->wrapperKekIndex, kek_add->blobFormat, &kek_blob); /* Add kek to local kek table (in case of chip reset) */ if(rc == tiSuccess) { if(agtiapi_AddKek(pCard, kek_add->kekIndex, kek_add->wrapperKekIndex, &kek_blob) < 0) { return IOCTL_CALL_FAIL; } } } else { return IOCTL_CALL_FAIL; } } break; case IOCTL_MN_ENCRYPTION_DEK_ADD: { tiEncryptDekBlob_t dek_blob; /* Copied in */ IoctlEncryptDekAdd_t *dek_add = (IoctlEncryptDekAdd_t *) &ioctl_data->request; bit32 kek_index = dek_add->kekIndex; bit32 dek_index = dek_add->dekIndex; bit32 dek_table = dek_add->dekTable; bit32 blob_format = dek_add->dekBlobFormat; bit32 entry_sz = dek_add->dekTableKeyEntrySize; U32_64 addr = 0; bit32 addr_table[2]; memset(addr_table, 0, sizeof(addr_table)); printf("%s: Add dek at index 0x%x, table %x, kek index %x, blob format %x, entry size %x\n", __FUNCTION__, dek_index, dek_table, kek_index, blob_format, entry_sz); /* Copy dek_blob from user pointer to local buffer */ - if(access_ok(VERIFY_READ, dek_add->dekBlob, sizeof(dek_blob))) { + if(access_ok(dek_add->dekBlob, sizeof(dek_blob))) { printf("%s: Starting copy from user %p to kernel %p\n", __FUNCTION__, dek_add->dekBlob, &dek_blob); if((rv = copy_from_user(&dek_blob, dek_add->dekBlob, sizeof(dek_blob))) != 0) { printf("%s: Copy error, %d left\n", __FUNCTION__, rv); return IOCTL_CALL_FAIL; } /* Add DEK to local table */ if (agtiapi_AddDek(pCard, dek_table, dek_index, blob_format, entry_sz, &dek_blob, &addr) < 0) { return IOCTL_CALL_FAIL; } memcpy(addr_table, &addr, sizeof(addr)); /* Add DEK-KEK association in local table */ if (agtiapi_MapDekKek(pCard, dek_table, dek_index, kek_index) < 0) { return IOCTL_CALL_FAIL; } /* Push DEK to chip */ rc = tiCOMEncryptDekAdd(tiRoot, kek_index, dek_table, addr_table[1], addr_table[0], dek_index, 1, blob_format, entry_sz); } else { return IOCTL_CALL_FAIL; } } break; case IOCTL_MN_ENCRYPTION_DEK_INVALID: { IoctlEncryptDekInvalidate_t *dek_to_invalidate = (IoctlEncryptDekInvalidate_t *) &ioctl_data->request; printf("%s: Invalidating dek at index 0x%x, table %x\n", __FUNCTION__, dek_to_invalidate->dek.dekIndex, dek_to_invalidate->dek.dekTable); rc = tiCOMEncryptDekInvalidate(tiRoot, dek_to_invalidate->dek.dekTable, dek_to_invalidate->dek.dekIndex); /* TODO: What to do in local tables? Mark it? */ } break; case IOCTL_MN_ENCRYPTION_KEK_NVRAM: { rc = tiError; } break; case IOCTL_MN_ENCRYPTION_DEK_ASSIGN: { IoctlEncryptDekMapTable_t *p_dek_map = (IoctlEncryptDekMapTable_t *) &ioctl_data->request; /* Fill in host */ p_dek_map->dekMap[0].host = (bit32) pCard->pHost->host_no; printf("%s: Host %u: Mapping %u:%u:%u (%llx to %llx) to dek at index 0x%x, table %x, keytag %08x:%08x\n", __FUNCTION__, p_dek_map->dekMap[0].host, p_dek_map->dekMap[0].channel, p_dek_map->dekMap[0].device, p_dek_map->dekMap[0].lun, p_dek_map->dekMap[0].dekMapEntry[0].startLBA, p_dek_map->dekMap[0].dekMapEntry[0].endLBA, p_dek_map->dekMap[0].dekMapEntry[0].dek.dekIndex, p_dek_map->dekMap[0].dekMapEntry[0].dek.dekTable, p_dek_map->dekMap[0].keytag[1], p_dek_map->dekMap[0].keytag[0]); /* Create a mapping in local tables */ if (agtiapi_MapDek(pCard, &p_dek_map->dekMap[0]) < 0) { pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE; return IOCTL_CALL_FAIL; } rc = tiSuccess; skip_wait = 1; ioctl_data->encryptFunction = encryptSetDekMap; ioctl_data->status = tiSuccess; ioctl_data->subEvent = 0; } break; case IOCTL_MN_ENCRYPTION_ERROR_QUERY: { unsigned long flags, i, query_flag; ag_encrypt_ioerr_t *ioerr, *tmp; IoctlEncryptErrorQuery_t *perr = (IoctlEncryptErrorQuery_t *) &ioctl_data->request; printf("%s: query flag %x\n", __FUNCTION__, perr->query_flag); query_flag = perr->query_flag; /* initialize */ memset(perr, 0, sizeof(IoctlEncryptErrorQuery_t)); error_query_restart: /* Take spinlock */ // spin_lock_irqsave(&pCard->ioerr_queue_lock, flags); AG_SPIN_LOCK_IRQ(&pCard->ioerr_queue_lock, flags); /* Walk list */ i = 0; list_for_each_entry_safe(ioerr, tmp, &pCard->ioerr_queue, list) { if (i >= 32) break; perr->valid_mask |= (1 << i); memcpy(&perr->error[i], &ioerr->ioerr, sizeof(IoctlEncryptIOError_t)); list_del_init(&ioerr->list); mempool_free(ioerr, pCard->ioerr_mempool); i++; atomic_dec(&ioerr_queue_count); } /* Release spinlock */ // spin_unlock_irqrestore(&pCard->ioerr_queue_lock, flags); AG_SPIN_UNLOCK_IRQ(&pCard->ioerr_queue_lock, flags); //for test if (!perr->valid_mask) { /* No encryption IO error events, check flags to see if blocking wait OK */ if (query_flag == ERROR_QUERY_FLAG_BLOCK) { if (wait_event_interruptible(ioerr_waitq, (atomic_read(&ioerr_queue_count)))) { /* Awoken by signal */ return IOCTL_CALL_FAIL; } else { /* Awoken by IO error */ goto error_query_restart; } } } rc = tiSuccess; skip_wait = 1; ioctl_data->encryptFunction = encryptErrorQuery; ioctl_data->status = tiSuccess; ioctl_data->subEvent = 0; } break; default: printf("%s: Unrecognized Minor Function %d\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction); pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE; return IOCTL_CALL_FAIL; break; } /* Demux rc */ switch(rc) { case tiSuccess: if(!skip_wait) wait_for_completion(&pCard->ioctl_completion); /* Maybe: wait_for_completion_timeout() */ pIoctlPayload->hdr.Status = ioctl_data->status; break; case tiNotSupported: pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_NOT_SUPPORTED; break; default: printf("%s: Status: %d\n", __FUNCTION__, rc); pIoctlPayload->hdr.Status = IOCTL_ERR_STATUS_INVALID_CODE; break; } printf("%s: Encryption ioctl %d successful.\n", __FUNCTION__, pIoctlPayload->hdr.MinorFunction); return IOCTL_CALL_SUCCESS; } #endif /****************************************************************************** agtiapi_SetupEncryptedIO(): Purpose: Parameters: Return: Note: ******************************************************************************/ int agtiapi_SetupEncryptedIO(struct agtiapi_softc *pCard, ccb_t *pccb, unsigned long long block) { pCard->cipher_mode = TI_ENCRYPT_ATTRIB_CIPHER_XTS; /* Check that cipher mode is set properly */ if (pCard->cipher_mode == CIPHER_MODE_INVALID) { printf("%s: Cipher mode not yet set.\n", __FUNCTION__); return -E_BAD_CIPHER_MODE; } memset(&(pccb->tiSuperScsiRequest.Encrypt), 0, sizeof(pccb->tiSuperScsiRequest.Encrypt)); pccb->tiSuperScsiRequest.Encrypt.keyTagCheck = FALSE; pccb->tiSuperScsiRequest.Encrypt.encryptMode = pCard->cipher_mode; pccb->tiSuperScsiRequest.Encrypt.tweakVal_W0 = block; if(pccb->tiSuperScsiRequest.scsiCmnd.cdb[0] == READ_16 || pccb->tiSuperScsiRequest.scsiCmnd.cdb[0] == WRITE_16) { pccb->tiSuperScsiRequest.Encrypt.tweakVal_W0 = ((pccb->tiSuperScsiRequest.scsiCmnd.cdb[6] << 24 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[7] << 16 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[8] << 8 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[9])); pccb->tiSuperScsiRequest.Encrypt.tweakVal_W1 = ((pccb->tiSuperScsiRequest.scsiCmnd.cdb[2] << 24 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[3] << 16 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[4] << 8 ) | (pccb->tiSuperScsiRequest.scsiCmnd.cdb[5])); } /* Mark IO as valid encrypted IO */ pccb->flags |= ENCRYPTED_IO; pccb->tiSuperScsiRequest.flags = TI_SCSI_INITIATOR_ENCRYPT; /* Bump refcount (atomic) */ atomic_inc(&outstanding_encrypted_io_count); return 0; } /****************************************************************************** agtiapi_CleanupEncryptedIO(): Purpose: Parameters: Return: Note: ******************************************************************************/ void agtiapi_CleanupEncryptedIO(struct agtiapi_softc *pCard, ccb_t *pccb) { if ((pccb->flags & ENCRYPTED_IO)) { /* Decrement refcount */ atomic_dec(&outstanding_encrypted_io_count); } pccb->tiSuperScsiRequest.flags &= ~TI_SCSI_INITIATOR_ENCRYPT; pccb->flags &= ~ENCRYPTED_IO; } #ifdef ENCRYPT_ENHANCE /****************************************************************************** agtiapi_HandleEncryptedIOFailure(): Purpose: Parameters: Return: Note: ******************************************************************************/ void agtiapi_HandleEncryptedIOFailure(ag_device_t *pDev, ccb_t *pccb) { unsigned long flags, qdepth; struct scsi_cmnd *cmd; ag_encrypt_ioerr_t *perr; ag_card_t *pCard; cmd = pccb->cmd; if (!cmd) { printf("%s: Malformed pccb %p.\n", __FUNCTION__, pccb); return; } pCard = pDev->pCard; /* Sanity check */ if (!(pccb->flags & ENCRYPTED_IO)) { printf("%s: Skipping IO %lx: Not Encrypted.\n", __FUNCTION__, cmd->serial_number); return; } /* Check queue depth against max */ qdepth = atomic_read(&ioerr_queue_count); if (qdepth >= IOERR_QUEUE_DEPTH_MAX) { printf("%s: Not queueing IO error due to queue full: %lu entries.\n", __FUNCTION__, qdepth); return; } /* Get a container for the ag_encrypt_ioerr_t item from the mempool */ // perr = mempool_alloc(pCard->ioerr_mempool, GFP_ATOMIC); p = (ag_encrypt_map_t *)uma_zalloc(pCard->map_cache, M_WAITOK); //Encryption if (!perr) { printf("%s: Mempool allocation failure.\n", __FUNCTION__); return; } /* Populate ag_encrypt_ioerr_t container */ perr->ioerr.error_id = cmd->serial_number; perr->ioerr.timestamp = cmd->jiffies_at_alloc; perr->ioerr.host = (unsigned int) cmd->device->host->host_no; perr->ioerr.channel = cmd->device->channel; perr->ioerr.device = cmd->device->id; perr->ioerr.lun = cmd->device->lun; perr->ioerr.scsi_cmd = (unsigned int) cmd->cmnd[0]; perr->ioerr.dek_index = pccb->tiSuperScsiRequest.Encrypt.dekInfo.dekIndex; perr->ioerr.dek_table = pccb->tiSuperScsiRequest.Encrypt.dekInfo.dekTable; perr->ioerr.kek_index = pccb->tiSuperScsiRequest.Encrypt.kekIndex; perr->ioerr.keytag_check = pccb->tiSuperScsiRequest.Encrypt.keyTagCheck; perr->ioerr.encrypt_mode = pccb->tiSuperScsiRequest.Encrypt.encryptMode; perr->ioerr.keytag[0] = pccb->tiSuperScsiRequest.Encrypt.keyTag_W0; perr->ioerr.keytag[1] = pccb->tiSuperScsiRequest.Encrypt.keyTag_W1; switch(pccb->scsiStatus) { case tiDetailDekKeyCacheMiss: case tiDetailDekIVMismatch: perr->ioerr.error_type = pccb->scsiStatus; break; default: printf("%s: Unrecognized encrypted IO completion error status: %d\n", __FUNCTION__, pccb->scsiStatus); perr->ioerr.error_type = 0xffffffff; break; } /* Link IO err into queue */ AG_SPIN_LOCK_IRQ(&pCard->ioerr_queue_lock, flags); list_add_tail(&perr->list, &pCard->ioerr_queue); AG_SPIN_UNLOCK_IRQ(&pCard->ioerr_queue_lock, flags); /* Notify any wait queue waiters that an IO error has occurred */ atomic_inc(&ioerr_queue_count); wake_up_interruptible(&ioerr_waitq); } #endif #endif diff --git a/sys/ofed/drivers/infiniband/core/ib_uverbs_main.c b/sys/ofed/drivers/infiniband/core/ib_uverbs_main.c index 8aab2ac3ff8d..e1b560774fbc 100644 --- a/sys/ofed/drivers/infiniband/core/ib_uverbs_main.c +++ b/sys/ofed/drivers/infiniband/core/ib_uverbs_main.c @@ -1,1435 +1,1434 @@ /*- * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0 * * Copyright (c) 2005 Topspin Communications. All rights reserved. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. * Copyright (c) 2005 Mellanox Technologies. All rights reserved. * Copyright (c) 2005 Voltaire, Inc. All rights reserved. * Copyright (c) 2005 PathScale, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include "uverbs.h" MODULE_AUTHOR("Roland Dreier"); MODULE_DESCRIPTION("InfiniBand userspace verbs access"); MODULE_LICENSE("Dual BSD/GPL"); enum { IB_UVERBS_MAJOR = 231, IB_UVERBS_BASE_MINOR = 192, IB_UVERBS_MAX_DEVICES = 32 }; #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR) static struct class *uverbs_class; DEFINE_SPINLOCK(ib_uverbs_idr_lock); DEFINE_IDR(ib_uverbs_pd_idr); DEFINE_IDR(ib_uverbs_mr_idr); DEFINE_IDR(ib_uverbs_mw_idr); DEFINE_IDR(ib_uverbs_ah_idr); DEFINE_IDR(ib_uverbs_cq_idr); DEFINE_IDR(ib_uverbs_qp_idr); DEFINE_IDR(ib_uverbs_srq_idr); DEFINE_IDR(ib_uverbs_xrcd_idr); DEFINE_IDR(ib_uverbs_rule_idr); DEFINE_IDR(ib_uverbs_wq_idr); DEFINE_IDR(ib_uverbs_rwq_ind_tbl_idr); static DEFINE_SPINLOCK(map_lock); static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES); static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file, struct ib_device *ib_dev, const char __user *buf, int in_len, int out_len) = { [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context, [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device, [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port, [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd, [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd, [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr, [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr, [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr, [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw, [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw, [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel, [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq, [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq, [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq, [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq, [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq, [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp, [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp, [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp, [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp, [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send, [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv, [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv, [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah, [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah, [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast, [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast, [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq, [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq, [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq, [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq, [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd, [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd, [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq, [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp, }; static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file, struct ib_device *ib_dev, struct ib_udata *ucore, struct ib_udata *uhw) = { [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow, [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow, [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device, [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq, [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp, [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq, [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq, [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq, [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table, [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table, }; static void ib_uverbs_add_one(struct ib_device *device); static void ib_uverbs_remove_one(struct ib_device *device, void *client_data); int uverbs_dealloc_mw(struct ib_mw *mw) { struct ib_pd *pd = mw->pd; int ret; ret = mw->device->dealloc_mw(mw); if (!ret) atomic_dec(&pd->usecnt); return ret; } static void ib_uverbs_release_dev(struct kobject *kobj) { struct ib_uverbs_device *dev = container_of(kobj, struct ib_uverbs_device, kobj); cleanup_srcu_struct(&dev->disassociate_srcu); kfree(dev); } static struct kobj_type ib_uverbs_dev_ktype = { .release = ib_uverbs_release_dev, }; static void ib_uverbs_release_event_file(struct kref *ref) { struct ib_uverbs_event_file *file = container_of(ref, struct ib_uverbs_event_file, ref); kfree(file); } void ib_uverbs_release_ucq(struct ib_uverbs_file *file, struct ib_uverbs_event_file *ev_file, struct ib_ucq_object *uobj) { struct ib_uverbs_event *evt, *tmp; if (ev_file) { spin_lock_irq(&ev_file->lock); list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) { list_del(&evt->list); kfree(evt); } spin_unlock_irq(&ev_file->lock); kref_put(&ev_file->ref, ib_uverbs_release_event_file); } spin_lock_irq(&file->async_file->lock); list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) { list_del(&evt->list); kfree(evt); } spin_unlock_irq(&file->async_file->lock); } void ib_uverbs_release_uevent(struct ib_uverbs_file *file, struct ib_uevent_object *uobj) { struct ib_uverbs_event *evt, *tmp; spin_lock_irq(&file->async_file->lock); list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) { list_del(&evt->list); kfree(evt); } spin_unlock_irq(&file->async_file->lock); } static void ib_uverbs_detach_umcast(struct ib_qp *qp, struct ib_uqp_object *uobj) { struct ib_uverbs_mcast_entry *mcast, *tmp; list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) { ib_detach_mcast(qp, &mcast->gid, mcast->lid); list_del(&mcast->list); kfree(mcast); } } static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file, struct ib_ucontext *context) { struct ib_uobject *uobj, *tmp; context->closing = 1; list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) { struct ib_ah *ah = uobj->object; idr_remove_uobj(&ib_uverbs_ah_idr, uobj); ib_destroy_ah(ah); kfree(uobj); } /* Remove MWs before QPs, in order to support type 2A MWs. */ list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) { struct ib_mw *mw = uobj->object; idr_remove_uobj(&ib_uverbs_mw_idr, uobj); uverbs_dealloc_mw(mw); kfree(uobj); } list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) { struct ib_flow *flow_id = uobj->object; idr_remove_uobj(&ib_uverbs_rule_idr, uobj); ib_destroy_flow(flow_id); kfree(uobj); } list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) { struct ib_qp *qp = uobj->object; struct ib_uqp_object *uqp = container_of(uobj, struct ib_uqp_object, uevent.uobject); idr_remove_uobj(&ib_uverbs_qp_idr, uobj); if (qp == qp->real_qp) ib_uverbs_detach_umcast(qp, uqp); ib_destroy_qp(qp); ib_uverbs_release_uevent(file, &uqp->uevent); kfree(uqp); } list_for_each_entry_safe(uobj, tmp, &context->rwq_ind_tbl_list, list) { struct ib_rwq_ind_table *rwq_ind_tbl = uobj->object; struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl; idr_remove_uobj(&ib_uverbs_rwq_ind_tbl_idr, uobj); ib_destroy_rwq_ind_table(rwq_ind_tbl); kfree(ind_tbl); kfree(uobj); } list_for_each_entry_safe(uobj, tmp, &context->wq_list, list) { struct ib_wq *wq = uobj->object; struct ib_uwq_object *uwq = container_of(uobj, struct ib_uwq_object, uevent.uobject); idr_remove_uobj(&ib_uverbs_wq_idr, uobj); ib_destroy_wq(wq); ib_uverbs_release_uevent(file, &uwq->uevent); kfree(uwq); } list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) { struct ib_srq *srq = uobj->object; struct ib_uevent_object *uevent = container_of(uobj, struct ib_uevent_object, uobject); idr_remove_uobj(&ib_uverbs_srq_idr, uobj); ib_destroy_srq(srq); ib_uverbs_release_uevent(file, uevent); kfree(uevent); } list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) { struct ib_cq *cq = uobj->object; struct ib_uverbs_event_file *ev_file = cq->cq_context; struct ib_ucq_object *ucq = container_of(uobj, struct ib_ucq_object, uobject); idr_remove_uobj(&ib_uverbs_cq_idr, uobj); ib_destroy_cq(cq); ib_uverbs_release_ucq(file, ev_file, ucq); kfree(ucq); } list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) { struct ib_mr *mr = uobj->object; idr_remove_uobj(&ib_uverbs_mr_idr, uobj); ib_dereg_mr(mr); kfree(uobj); } mutex_lock(&file->device->xrcd_tree_mutex); list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) { struct ib_xrcd *xrcd = uobj->object; struct ib_uxrcd_object *uxrcd = container_of(uobj, struct ib_uxrcd_object, uobject); idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj); ib_uverbs_dealloc_xrcd(file->device, xrcd); kfree(uxrcd); } mutex_unlock(&file->device->xrcd_tree_mutex); list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) { struct ib_pd *pd = uobj->object; idr_remove_uobj(&ib_uverbs_pd_idr, uobj); ib_dealloc_pd(pd); kfree(uobj); } put_pid(context->tgid); return context->device->dealloc_ucontext(context); } static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev) { complete(&dev->comp); } static void ib_uverbs_release_file(struct kref *ref) { struct ib_uverbs_file *file = container_of(ref, struct ib_uverbs_file, ref); struct ib_device *ib_dev; int srcu_key; srcu_key = srcu_read_lock(&file->device->disassociate_srcu); ib_dev = srcu_dereference(file->device->ib_dev, &file->device->disassociate_srcu); if (ib_dev && !ib_dev->disassociate_ucontext) module_put(ib_dev->owner); srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); if (atomic_dec_and_test(&file->device->refcount)) ib_uverbs_comp_dev(file->device); kfree(file); } static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf, size_t count, loff_t *pos) { struct ib_uverbs_event_file *file = filp->private_data; struct ib_uverbs_event *event; int eventsz; int ret = 0; spin_lock_irq(&file->lock); while (list_empty(&file->event_list)) { spin_unlock_irq(&file->lock); if (filp->f_flags & O_NONBLOCK) return -EAGAIN; if (wait_event_interruptible(file->poll_wait, (!list_empty(&file->event_list) || /* The barriers built into wait_event_interruptible() * and wake_up() guarentee this will see the null set * without using RCU */ !file->uverbs_file->device->ib_dev))) return -ERESTARTSYS; /* If device was disassociated and no event exists set an error */ if (list_empty(&file->event_list) && !file->uverbs_file->device->ib_dev) return -EIO; spin_lock_irq(&file->lock); } event = list_entry(file->event_list.next, struct ib_uverbs_event, list); if (file->is_async) eventsz = sizeof (struct ib_uverbs_async_event_desc); else eventsz = sizeof (struct ib_uverbs_comp_event_desc); if (eventsz > count) { ret = -EINVAL; event = NULL; } else { list_del(file->event_list.next); if (event->counter) { ++(*event->counter); list_del(&event->obj_list); } } spin_unlock_irq(&file->lock); if (event) { if (copy_to_user(buf, event, eventsz)) ret = -EFAULT; else ret = eventsz; } kfree(event); return ret; } static unsigned int ib_uverbs_event_poll(struct file *filp, struct poll_table_struct *wait) { unsigned int pollflags = 0; struct ib_uverbs_event_file *file = filp->private_data; poll_wait(filp, &file->poll_wait, wait); spin_lock_irq(&file->lock); if (!list_empty(&file->event_list)) pollflags = POLLIN | POLLRDNORM; spin_unlock_irq(&file->lock); return pollflags; } static int ib_uverbs_event_fasync(int fd, struct file *filp, int on) { struct ib_uverbs_event_file *file = filp->private_data; return fasync_helper(fd, filp, on, &file->async_queue); } static int ib_uverbs_event_close(struct inode *inode, struct file *filp) { struct ib_uverbs_event_file *file = filp->private_data; struct ib_uverbs_event *entry, *tmp; int closed_already = 0; mutex_lock(&file->uverbs_file->device->lists_mutex); spin_lock_irq(&file->lock); closed_already = file->is_closed; file->is_closed = 1; list_for_each_entry_safe(entry, tmp, &file->event_list, list) { if (entry->counter) list_del(&entry->obj_list); kfree(entry); } spin_unlock_irq(&file->lock); if (!closed_already) { list_del(&file->list); if (file->is_async) ib_unregister_event_handler(&file->uverbs_file-> event_handler); } mutex_unlock(&file->uverbs_file->device->lists_mutex); kref_put(&file->uverbs_file->ref, ib_uverbs_release_file); kref_put(&file->ref, ib_uverbs_release_event_file); return 0; } static const struct file_operations uverbs_event_fops = { .owner = THIS_MODULE, .read = ib_uverbs_event_read, .poll = ib_uverbs_event_poll, .release = ib_uverbs_event_close, .fasync = ib_uverbs_event_fasync, .llseek = no_llseek, }; void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context) { struct ib_uverbs_event_file *file = cq_context; struct ib_ucq_object *uobj; struct ib_uverbs_event *entry; unsigned long flags; if (!file) return; spin_lock_irqsave(&file->lock, flags); if (file->is_closed) { spin_unlock_irqrestore(&file->lock, flags); return; } entry = kmalloc(sizeof *entry, GFP_ATOMIC); if (!entry) { spin_unlock_irqrestore(&file->lock, flags); return; } uobj = container_of(cq->uobject, struct ib_ucq_object, uobject); entry->desc.comp.cq_handle = cq->uobject->user_handle; entry->counter = &uobj->comp_events_reported; list_add_tail(&entry->list, &file->event_list); list_add_tail(&entry->obj_list, &uobj->comp_list); spin_unlock_irqrestore(&file->lock, flags); wake_up_interruptible(&file->poll_wait); kill_fasync(&file->async_queue, SIGIO, POLL_IN); } static void ib_uverbs_async_handler(struct ib_uverbs_file *file, __u64 element, __u64 event, struct list_head *obj_list, u32 *counter) { struct ib_uverbs_event *entry; unsigned long flags; spin_lock_irqsave(&file->async_file->lock, flags); if (file->async_file->is_closed) { spin_unlock_irqrestore(&file->async_file->lock, flags); return; } entry = kmalloc(sizeof *entry, GFP_ATOMIC); if (!entry) { spin_unlock_irqrestore(&file->async_file->lock, flags); return; } entry->desc.async.element = element; entry->desc.async.event_type = event; entry->desc.async.reserved = 0; entry->counter = counter; list_add_tail(&entry->list, &file->async_file->event_list); if (obj_list) list_add_tail(&entry->obj_list, obj_list); spin_unlock_irqrestore(&file->async_file->lock, flags); wake_up_interruptible(&file->async_file->poll_wait); kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN); } void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr) { struct ib_ucq_object *uobj = container_of(event->element.cq->uobject, struct ib_ucq_object, uobject); ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle, event->event, &uobj->async_list, &uobj->async_events_reported); } void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr) { struct ib_uevent_object *uobj; /* for XRC target qp's, check that qp is live */ if (!event->element.qp->uobject || !event->element.qp->uobject->live) return; uobj = container_of(event->element.qp->uobject, struct ib_uevent_object, uobject); ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, event->event, &uobj->event_list, &uobj->events_reported); } void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr) { struct ib_uevent_object *uobj = container_of(event->element.wq->uobject, struct ib_uevent_object, uobject); ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, event->event, &uobj->event_list, &uobj->events_reported); } void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr) { struct ib_uevent_object *uobj; uobj = container_of(event->element.srq->uobject, struct ib_uevent_object, uobject); ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, event->event, &uobj->event_list, &uobj->events_reported); } void ib_uverbs_event_handler(struct ib_event_handler *handler, struct ib_event *event) { struct ib_uverbs_file *file = container_of(handler, struct ib_uverbs_file, event_handler); ib_uverbs_async_handler(file, event->element.port_num, event->event, NULL, NULL); } void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file) { kref_put(&file->async_file->ref, ib_uverbs_release_event_file); file->async_file = NULL; } struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file, struct ib_device *ib_dev, int is_async) { struct ib_uverbs_event_file *ev_file; struct file *filp; int ret; ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL); if (!ev_file) return ERR_PTR(-ENOMEM); kref_init(&ev_file->ref); spin_lock_init(&ev_file->lock); INIT_LIST_HEAD(&ev_file->event_list); init_waitqueue_head(&ev_file->poll_wait); ev_file->uverbs_file = uverbs_file; kref_get(&ev_file->uverbs_file->ref); ev_file->async_queue = NULL; ev_file->is_closed = 0; /* * fops_get() can't fail here, because we're coming from a * system call on a uverbs file, which will already have a * module reference. */ filp = alloc_file(FMODE_READ, fops_get(&uverbs_event_fops)); if (IS_ERR(filp)) goto err_put_refs; filp->private_data = ev_file; mutex_lock(&uverbs_file->device->lists_mutex); list_add_tail(&ev_file->list, &uverbs_file->device->uverbs_events_file_list); mutex_unlock(&uverbs_file->device->lists_mutex); if (is_async) { WARN_ON(uverbs_file->async_file); uverbs_file->async_file = ev_file; kref_get(&uverbs_file->async_file->ref); INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler, ib_dev, ib_uverbs_event_handler); ret = ib_register_event_handler(&uverbs_file->event_handler); if (ret) goto err_put_file; /* At that point async file stuff was fully set */ ev_file->is_async = 1; } return filp; err_put_file: fput(filp); kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file); uverbs_file->async_file = NULL; return ERR_PTR(ret); err_put_refs: kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file); kref_put(&ev_file->ref, ib_uverbs_release_event_file); return filp; } /* * Look up a completion event file by FD. If lookup is successful, * takes a ref to the event file struct that it returns; if * unsuccessful, returns NULL. */ struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd) { struct ib_uverbs_event_file *ev_file = NULL; struct fd f = fdget(fd); if (!f.file) return NULL; if (f.file->f_op != &uverbs_event_fops) goto out; ev_file = f.file->private_data; if (ev_file->is_async) { ev_file = NULL; goto out; } kref_get(&ev_file->ref); out: fdput(f); return ev_file; } static int verify_command_mask(struct ib_device *ib_dev, __u32 command) { u64 mask; if (command <= IB_USER_VERBS_CMD_OPEN_QP) mask = ib_dev->uverbs_cmd_mask; else mask = ib_dev->uverbs_ex_cmd_mask; if (mask & ((u64)1 << command)) return 0; return -1; } static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos) { struct ib_uverbs_file *file = filp->private_data; struct ib_device *ib_dev; struct ib_uverbs_cmd_hdr hdr; __u32 command; __u32 flags; int srcu_key; ssize_t ret; if (WARN_ON_ONCE(!ib_safe_file_access(filp))) return -EACCES; if (count < sizeof hdr) return -EINVAL; if (copy_from_user(&hdr, buf, sizeof hdr)) return -EFAULT; srcu_key = srcu_read_lock(&file->device->disassociate_srcu); ib_dev = srcu_dereference(file->device->ib_dev, &file->device->disassociate_srcu); if (!ib_dev) { ret = -EIO; goto out; } if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK | IB_USER_VERBS_CMD_COMMAND_MASK)) { ret = -EINVAL; goto out; } command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK; if (verify_command_mask(ib_dev, command)) { ret = -EOPNOTSUPP; goto out; } if (!file->ucontext && command != IB_USER_VERBS_CMD_GET_CONTEXT) { ret = -EINVAL; goto out; } flags = (hdr.command & IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT; if (!flags) { if (command >= ARRAY_SIZE(uverbs_cmd_table) || !uverbs_cmd_table[command]) { ret = -EINVAL; goto out; } if (hdr.in_words * 4 != count) { ret = -EINVAL; goto out; } ret = uverbs_cmd_table[command](file, ib_dev, buf + sizeof(hdr), hdr.in_words * 4, hdr.out_words * 4); } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) { struct ib_uverbs_ex_cmd_hdr ex_hdr; struct ib_udata ucore; struct ib_udata uhw; size_t written_count = count; if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) || !uverbs_ex_cmd_table[command]) { ret = -ENOSYS; goto out; } if (!file->ucontext) { ret = -EINVAL; goto out; } if (count < (sizeof(hdr) + sizeof(ex_hdr))) { ret = -EINVAL; goto out; } if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) { ret = -EFAULT; goto out; } count -= sizeof(hdr) + sizeof(ex_hdr); buf += sizeof(hdr) + sizeof(ex_hdr); if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) { ret = -EINVAL; goto out; } if (ex_hdr.cmd_hdr_reserved) { ret = -EINVAL; goto out; } if (ex_hdr.response) { if (!hdr.out_words && !ex_hdr.provider_out_words) { ret = -EINVAL; goto out; } - if (!access_ok(VERIFY_WRITE, - (void __user *) (unsigned long) ex_hdr.response, + if (!access_ok((void __user *) (unsigned long) ex_hdr.response, (hdr.out_words + ex_hdr.provider_out_words) * 8)) { ret = -EFAULT; goto out; } } else { if (hdr.out_words || ex_hdr.provider_out_words) { ret = -EINVAL; goto out; } } INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response, hdr.in_words * 8, hdr.out_words * 8); INIT_UDATA_BUF_OR_NULL(&uhw, buf + ucore.inlen, (unsigned long) ex_hdr.response + ucore.outlen, ex_hdr.provider_in_words * 8, ex_hdr.provider_out_words * 8); ret = uverbs_ex_cmd_table[command](file, ib_dev, &ucore, &uhw); if (!ret) ret = written_count; } else { ret = -ENOSYS; } out: srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); return ret; } static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) { struct ib_uverbs_file *file = filp->private_data; struct ib_device *ib_dev; int ret = 0; int srcu_key; srcu_key = srcu_read_lock(&file->device->disassociate_srcu); ib_dev = srcu_dereference(file->device->ib_dev, &file->device->disassociate_srcu); if (!ib_dev) { ret = -EIO; goto out; } if (!file->ucontext) ret = -ENODEV; else ret = ib_dev->mmap(file->ucontext, vma); out: srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); return ret; } /* * ib_uverbs_open() does not need the BKL: * * - the ib_uverbs_device structures are properly reference counted and * everything else is purely local to the file being created, so * races against other open calls are not a problem; * - there is no ioctl method to race against; * - the open method will either immediately run -ENXIO, or all * required initialization will be done. */ static int ib_uverbs_open(struct inode *inode, struct file *filp) { struct ib_uverbs_device *dev; struct ib_uverbs_file *file; struct ib_device *ib_dev; int ret; int module_dependent; int srcu_key; dev = container_of(inode->i_cdev->si_drv1, struct ib_uverbs_device, cdev); if (!atomic_inc_not_zero(&dev->refcount)) return -ENXIO; srcu_key = srcu_read_lock(&dev->disassociate_srcu); mutex_lock(&dev->lists_mutex); ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); if (!ib_dev) { ret = -EIO; goto err; } /* In case IB device supports disassociate ucontext, there is no hard * dependency between uverbs device and its low level device. */ module_dependent = !(ib_dev->disassociate_ucontext); if (module_dependent) { if (!try_module_get(ib_dev->owner)) { ret = -ENODEV; goto err; } } file = kzalloc(sizeof(*file), GFP_KERNEL); if (!file) { ret = -ENOMEM; if (module_dependent) goto err_module; goto err; } file->device = dev; file->ucontext = NULL; file->async_file = NULL; kref_init(&file->ref); mutex_init(&file->mutex); mutex_init(&file->cleanup_mutex); filp->private_data = file; kobject_get(&dev->kobj); list_add_tail(&file->list, &dev->uverbs_file_list); mutex_unlock(&dev->lists_mutex); srcu_read_unlock(&dev->disassociate_srcu, srcu_key); return nonseekable_open(inode, filp); err_module: module_put(ib_dev->owner); err: mutex_unlock(&dev->lists_mutex); srcu_read_unlock(&dev->disassociate_srcu, srcu_key); if (atomic_dec_and_test(&dev->refcount)) ib_uverbs_comp_dev(dev); return ret; } static int ib_uverbs_close(struct inode *inode, struct file *filp) { struct ib_uverbs_file *file = filp->private_data; struct ib_uverbs_device *dev = file->device; mutex_lock(&file->cleanup_mutex); if (file->ucontext) { ib_uverbs_cleanup_ucontext(file, file->ucontext); file->ucontext = NULL; } mutex_unlock(&file->cleanup_mutex); mutex_lock(&file->device->lists_mutex); if (!file->is_closed) { list_del(&file->list); file->is_closed = 1; } mutex_unlock(&file->device->lists_mutex); if (file->async_file) kref_put(&file->async_file->ref, ib_uverbs_release_event_file); kref_put(&file->ref, ib_uverbs_release_file); kobject_put(&dev->kobj); return 0; } static const struct file_operations uverbs_fops = { .owner = THIS_MODULE, .write = ib_uverbs_write, .open = ib_uverbs_open, .release = ib_uverbs_close, .llseek = no_llseek, }; static const struct file_operations uverbs_mmap_fops = { .owner = THIS_MODULE, .write = ib_uverbs_write, .mmap = ib_uverbs_mmap, .open = ib_uverbs_open, .release = ib_uverbs_close, .llseek = no_llseek, }; static struct ib_client uverbs_client = { .name = "uverbs", .add = ib_uverbs_add_one, .remove = ib_uverbs_remove_one }; static ssize_t show_ibdev(struct device *device, struct device_attribute *attr, char *buf) { int ret = -ENODEV; int srcu_key; struct ib_uverbs_device *dev = dev_get_drvdata(device); struct ib_device *ib_dev; if (!dev) return -ENODEV; srcu_key = srcu_read_lock(&dev->disassociate_srcu); ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); if (ib_dev) ret = sprintf(buf, "%s\n", ib_dev->name); srcu_read_unlock(&dev->disassociate_srcu, srcu_key); return ret; } static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); static ssize_t show_dev_abi_version(struct device *device, struct device_attribute *attr, char *buf) { struct ib_uverbs_device *dev = dev_get_drvdata(device); int ret = -ENODEV; int srcu_key; struct ib_device *ib_dev; if (!dev) return -ENODEV; srcu_key = srcu_read_lock(&dev->disassociate_srcu); ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); if (ib_dev) ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver); srcu_read_unlock(&dev->disassociate_srcu, srcu_key); return ret; } static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL); static CLASS_ATTR_STRING(abi_version, S_IRUGO, __stringify(IB_USER_VERBS_ABI_VERSION)); static dev_t overflow_maj; static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES); /* * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by * requesting a new major number and doubling the number of max devices we * support. It's stupid, but simple. */ static int find_overflow_devnum(void) { int ret; if (!overflow_maj) { ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES, "infiniband_verbs"); if (ret) { pr_err("user_verbs: couldn't register dynamic device number\n"); return ret; } } ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES); if (ret >= IB_UVERBS_MAX_DEVICES) return -1; return ret; } static ssize_t show_dev_device(struct device *device, struct device_attribute *attr, char *buf) { struct ib_uverbs_device *dev = dev_get_drvdata(device); if (!dev || !dev->ib_dev->dma_device) return -ENODEV; return sprintf(buf, "0x%04x\n", ((struct pci_dev *)dev->ib_dev->dma_device)->device); } static DEVICE_ATTR(device, S_IRUGO, show_dev_device, NULL); static ssize_t show_dev_vendor(struct device *device, struct device_attribute *attr, char *buf) { struct ib_uverbs_device *dev = dev_get_drvdata(device); if (!dev || !dev->ib_dev->dma_device) return -ENODEV; return sprintf(buf, "0x%04x\n", ((struct pci_dev *)dev->ib_dev->dma_device)->vendor); } static DEVICE_ATTR(vendor, S_IRUGO, show_dev_vendor, NULL); struct attribute *device_attrs[] = { &dev_attr_device.attr, &dev_attr_vendor.attr, NULL }; static struct attribute_group device_group = { .name = "device", .attrs = device_attrs }; static void ib_uverbs_add_one(struct ib_device *device) { int devnum; dev_t base; struct ib_uverbs_device *uverbs_dev; int ret; if (!device->alloc_ucontext) return; uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL); if (!uverbs_dev) return; ret = init_srcu_struct(&uverbs_dev->disassociate_srcu); if (ret) { kfree(uverbs_dev); return; } atomic_set(&uverbs_dev->refcount, 1); init_completion(&uverbs_dev->comp); uverbs_dev->xrcd_tree = RB_ROOT; mutex_init(&uverbs_dev->xrcd_tree_mutex); kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype); mutex_init(&uverbs_dev->lists_mutex); INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list); INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list); spin_lock(&map_lock); devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES); if (devnum >= IB_UVERBS_MAX_DEVICES) { spin_unlock(&map_lock); devnum = find_overflow_devnum(); if (devnum < 0) goto err; spin_lock(&map_lock); uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES; base = devnum + overflow_maj; set_bit(devnum, overflow_map); } else { uverbs_dev->devnum = devnum; base = devnum + IB_UVERBS_BASE_DEV; set_bit(devnum, dev_map); } spin_unlock(&map_lock); rcu_assign_pointer(uverbs_dev->ib_dev, device); uverbs_dev->num_comp_vectors = device->num_comp_vectors; cdev_init(&uverbs_dev->cdev, NULL); uverbs_dev->cdev.owner = THIS_MODULE; uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops; uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj; kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum); if (cdev_add(&uverbs_dev->cdev, base, 1)) goto err_cdev; uverbs_dev->dev = device_create(uverbs_class, device->dma_device, uverbs_dev->cdev.dev, uverbs_dev, "uverbs%d", uverbs_dev->devnum); if (IS_ERR(uverbs_dev->dev)) goto err_cdev; if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev)) goto err_class; if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version)) goto err_class; if (sysfs_create_group(&uverbs_dev->dev->kobj, &device_group)) goto err_class; ib_set_client_data(device, &uverbs_client, uverbs_dev); return; err_class: device_destroy(uverbs_class, uverbs_dev->cdev.dev); err_cdev: cdev_del(&uverbs_dev->cdev); if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES) clear_bit(devnum, dev_map); else clear_bit(devnum, overflow_map); err: if (atomic_dec_and_test(&uverbs_dev->refcount)) ib_uverbs_comp_dev(uverbs_dev); wait_for_completion(&uverbs_dev->comp); kobject_put(&uverbs_dev->kobj); return; } static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, struct ib_device *ib_dev) { struct ib_uverbs_file *file; struct ib_uverbs_event_file *event_file; struct ib_event event; /* Pending running commands to terminate */ synchronize_srcu(&uverbs_dev->disassociate_srcu); event.event = IB_EVENT_DEVICE_FATAL; event.element.port_num = 0; event.device = ib_dev; mutex_lock(&uverbs_dev->lists_mutex); while (!list_empty(&uverbs_dev->uverbs_file_list)) { struct ib_ucontext *ucontext; file = list_first_entry(&uverbs_dev->uverbs_file_list, struct ib_uverbs_file, list); file->is_closed = 1; list_del(&file->list); kref_get(&file->ref); mutex_unlock(&uverbs_dev->lists_mutex); mutex_lock(&file->cleanup_mutex); ucontext = file->ucontext; file->ucontext = NULL; mutex_unlock(&file->cleanup_mutex); /* At this point ib_uverbs_close cannot be running * ib_uverbs_cleanup_ucontext */ if (ucontext) { /* We must release the mutex before going ahead and * calling disassociate_ucontext. disassociate_ucontext * might end up indirectly calling uverbs_close, * for example due to freeing the resources * (e.g mmput). */ ib_uverbs_event_handler(&file->event_handler, &event); ib_dev->disassociate_ucontext(ucontext); ib_uverbs_cleanup_ucontext(file, ucontext); } mutex_lock(&uverbs_dev->lists_mutex); kref_put(&file->ref, ib_uverbs_release_file); } while (!list_empty(&uverbs_dev->uverbs_events_file_list)) { event_file = list_first_entry(&uverbs_dev-> uverbs_events_file_list, struct ib_uverbs_event_file, list); spin_lock_irq(&event_file->lock); event_file->is_closed = 1; spin_unlock_irq(&event_file->lock); list_del(&event_file->list); if (event_file->is_async) { ib_unregister_event_handler(&event_file->uverbs_file-> event_handler); event_file->uverbs_file->event_handler.device = NULL; } wake_up_interruptible(&event_file->poll_wait); kill_fasync(&event_file->async_queue, SIGIO, POLL_IN); } mutex_unlock(&uverbs_dev->lists_mutex); } static void ib_uverbs_remove_one(struct ib_device *device, void *client_data) { struct ib_uverbs_device *uverbs_dev = client_data; int wait_clients = 1; if (!uverbs_dev) return; sysfs_remove_group(&uverbs_dev->dev->kobj, &device_group); dev_set_drvdata(uverbs_dev->dev, NULL); device_destroy(uverbs_class, uverbs_dev->cdev.dev); cdev_del(&uverbs_dev->cdev); if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES) clear_bit(uverbs_dev->devnum, dev_map); else clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map); if (device->disassociate_ucontext) { /* We disassociate HW resources and immediately return. * Userspace will see a EIO errno for all future access. * Upon returning, ib_device may be freed internally and is not * valid any more. * uverbs_device is still available until all clients close * their files, then the uverbs device ref count will be zero * and its resources will be freed. * Note: At this point no more files can be opened since the * cdev was deleted, however active clients can still issue * commands and close their open files. */ rcu_assign_pointer(uverbs_dev->ib_dev, NULL); ib_uverbs_free_hw_resources(uverbs_dev, device); wait_clients = 0; } if (atomic_dec_and_test(&uverbs_dev->refcount)) ib_uverbs_comp_dev(uverbs_dev); if (wait_clients) wait_for_completion(&uverbs_dev->comp); kobject_put(&uverbs_dev->kobj); } static char *uverbs_devnode(struct device *dev, umode_t *mode) { if (mode) *mode = 0666; return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev)); } static int __init ib_uverbs_init(void) { int ret; ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES, "infiniband_verbs"); if (ret) { pr_err("user_verbs: couldn't register device number\n"); goto out; } uverbs_class = class_create(THIS_MODULE, "infiniband_verbs"); if (IS_ERR(uverbs_class)) { ret = PTR_ERR(uverbs_class); pr_err("user_verbs: couldn't create class infiniband_verbs\n"); goto out_chrdev; } uverbs_class->devnode = uverbs_devnode; ret = class_create_file(uverbs_class, &class_attr_abi_version.attr); if (ret) { pr_err("user_verbs: couldn't create abi_version attribute\n"); goto out_class; } ret = ib_register_client(&uverbs_client); if (ret) { pr_err("user_verbs: couldn't register client\n"); goto out_class; } return 0; out_class: class_destroy(uverbs_class); out_chrdev: unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); out: return ret; } static void __exit ib_uverbs_cleanup(void) { ib_unregister_client(&uverbs_client); class_destroy(uverbs_class); unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); if (overflow_maj) unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES); idr_destroy(&ib_uverbs_pd_idr); idr_destroy(&ib_uverbs_mr_idr); idr_destroy(&ib_uverbs_mw_idr); idr_destroy(&ib_uverbs_ah_idr); idr_destroy(&ib_uverbs_cq_idr); idr_destroy(&ib_uverbs_qp_idr); idr_destroy(&ib_uverbs_srq_idr); } module_init_order(ib_uverbs_init, SI_ORDER_FIFTH); module_exit_order(ib_uverbs_cleanup, SI_ORDER_FIFTH);