Back to home page

OSCL-LXR

 
 

    


0001 /*******************************************************************
0002  * This file is part of the Emulex Linux Device Driver for         *
0003  * Fibre Channel Host Bus Adapters.                                *
0004  * Copyright (C) 2017-2022 Broadcom. All Rights Reserved. The term *
0005  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
0006  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
0007  * EMULEX and SLI are trademarks of Emulex.                        *
0008  * www.broadcom.com                                                *
0009  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
0010  *                                                                 *
0011  * This program is free software; you can redistribute it and/or   *
0012  * modify it under the terms of version 2 of the GNU General       *
0013  * Public License as published by the Free Software Foundation.    *
0014  * This program is distributed in the hope that it will be useful. *
0015  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
0016  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
0017  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
0018  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
0019  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
0020  * more details, a copy of which can be found in the file COPYING  *
0021  * included with this package.                                     *
0022  *******************************************************************/
0023 
0024 #include <linux/interrupt.h>
0025 #include <linux/dma-direction.h>
0026 
0027 #include <scsi/scsi_transport_fc.h>
0028 
0029 #include "lpfc_hw4.h"
0030 #include "lpfc_hw.h"
0031 #include "lpfc_sli.h"
0032 #include "lpfc_sli4.h"
0033 #include "lpfc_nl.h"
0034 #include "lpfc_disc.h"
0035 #include "lpfc.h"
0036 #include "lpfc_crtn.h"
0037 
0038 
0039 /*
0040  * lpfc_get_vmid_from_hashtable - search the UUID in the hash table
0041  * @vport: The virtual port for which this call is being executed.
0042  * @hash: calculated hash value
0043  * @buf: uuid associated with the VE
0044  * Return the VMID entry associated with the UUID
0045  * Make sure to acquire the appropriate lock before invoking this routine.
0046  */
0047 struct lpfc_vmid *lpfc_get_vmid_from_hashtable(struct lpfc_vport *vport,
0048                            u32 hash, u8 *buf)
0049 {
0050     struct lpfc_vmid *vmp;
0051 
0052     hash_for_each_possible(vport->hash_table, vmp, hnode, hash) {
0053         if (memcmp(&vmp->host_vmid[0], buf, 16) == 0)
0054             return vmp;
0055     }
0056     return NULL;
0057 }
0058 
0059 /*
0060  * lpfc_put_vmid_in_hashtable - put the VMID in the hash table
0061  * @vport: The virtual port for which this call is being executed.
0062  * @hash - calculated hash value
0063  * @vmp: Pointer to a VMID entry representing a VM sending I/O
0064  *
0065  * This routine will insert the newly acquired VMID entity in the hash table.
0066  * Make sure to acquire the appropriate lock before invoking this routine.
0067  */
0068 static void
0069 lpfc_put_vmid_in_hashtable(struct lpfc_vport *vport, u32 hash,
0070                struct lpfc_vmid *vmp)
0071 {
0072     hash_add(vport->hash_table, &vmp->hnode, hash);
0073 }
0074 
0075 /*
0076  * lpfc_vmid_hash_fn - create a hash value of the UUID
0077  * @vmid: uuid associated with the VE
0078  * @len: length of the VMID string
0079  * Returns the calculated hash value
0080  */
0081 int lpfc_vmid_hash_fn(const char *vmid, int len)
0082 {
0083     int c;
0084     int hash = 0;
0085 
0086     if (len == 0)
0087         return 0;
0088     while (len--) {
0089         c = *vmid++;
0090         if (c >= 'A' && c <= 'Z')
0091             c += 'a' - 'A';
0092 
0093         hash = (hash + (c << LPFC_VMID_HASH_SHIFT) +
0094             (c >> LPFC_VMID_HASH_SHIFT)) * 19;
0095     }
0096 
0097     return hash & LPFC_VMID_HASH_MASK;
0098 }
0099 
0100 /*
0101  * lpfc_vmid_update_entry - update the vmid entry in the hash table
0102  * @vport: The virtual port for which this call is being executed.
0103  * @iodir: io direction
0104  * @vmp: Pointer to a VMID entry representing a VM sending I/O
0105  * @tag: VMID tag
0106  */
0107 static void lpfc_vmid_update_entry(struct lpfc_vport *vport,
0108                    enum dma_data_direction iodir,
0109                    struct lpfc_vmid *vmp,
0110                    union lpfc_vmid_io_tag *tag)
0111 {
0112     u64 *lta;
0113 
0114     if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO)
0115         tag->cs_ctl_vmid = vmp->un.cs_ctl_vmid;
0116     else if (vport->phba->cfg_vmid_app_header)
0117         tag->app_id = vmp->un.app_id;
0118 
0119     if (iodir == DMA_TO_DEVICE)
0120         vmp->io_wr_cnt++;
0121     else if (iodir == DMA_FROM_DEVICE)
0122         vmp->io_rd_cnt++;
0123 
0124     /* update the last access timestamp in the table */
0125     lta = per_cpu_ptr(vmp->last_io_time, raw_smp_processor_id());
0126     *lta = jiffies;
0127 }
0128 
0129 static void lpfc_vmid_assign_cs_ctl(struct lpfc_vport *vport,
0130                     struct lpfc_vmid *vmid)
0131 {
0132     u32 hash;
0133     struct lpfc_vmid *pvmid;
0134 
0135     if (vport->port_type == LPFC_PHYSICAL_PORT) {
0136         vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
0137     } else {
0138         hash = lpfc_vmid_hash_fn(vmid->host_vmid, vmid->vmid_len);
0139         pvmid =
0140             lpfc_get_vmid_from_hashtable(vport->phba->pport, hash,
0141                          vmid->host_vmid);
0142         if (pvmid)
0143             vmid->un.cs_ctl_vmid = pvmid->un.cs_ctl_vmid;
0144         else
0145             vmid->un.cs_ctl_vmid = lpfc_vmid_get_cs_ctl(vport);
0146     }
0147 }
0148 
0149 /*
0150  * lpfc_vmid_get_appid - get the VMID associated with the UUID
0151  * @vport: The virtual port for which this call is being executed.
0152  * @uuid: UUID associated with the VE
0153  * @cmd: address of scsi_cmd descriptor
0154  * @iodir: io direction
0155  * @tag: VMID tag
0156  * Returns status of the function
0157  */
0158 int lpfc_vmid_get_appid(struct lpfc_vport *vport, char *uuid,
0159             enum dma_data_direction iodir,
0160             union lpfc_vmid_io_tag *tag)
0161 {
0162     struct lpfc_vmid *vmp = NULL;
0163     int hash, len, rc = -EPERM, i;
0164 
0165     /* check if QFPA is complete */
0166     if (lpfc_vmid_is_type_priority_tag(vport) &&
0167         !(vport->vmid_flag & LPFC_VMID_QFPA_CMPL) &&
0168         (vport->vmid_flag & LPFC_VMID_ISSUE_QFPA)) {
0169         vport->work_port_events |= WORKER_CHECK_VMID_ISSUE_QFPA;
0170         return -EAGAIN;
0171     }
0172 
0173     /* search if the UUID has already been mapped to the VMID */
0174     len = strlen(uuid);
0175     hash = lpfc_vmid_hash_fn(uuid, len);
0176 
0177     /* search for the VMID in the table */
0178     read_lock(&vport->vmid_lock);
0179     vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
0180 
0181     /* if found, check if its already registered  */
0182     if (vmp  && vmp->flag & LPFC_VMID_REGISTERED) {
0183         read_unlock(&vport->vmid_lock);
0184         lpfc_vmid_update_entry(vport, iodir, vmp, tag);
0185         rc = 0;
0186     } else if (vmp && (vmp->flag & LPFC_VMID_REQ_REGISTER ||
0187                vmp->flag & LPFC_VMID_DE_REGISTER)) {
0188         /* else if register or dereg request has already been sent */
0189         /* Hence VMID tag will not be added for this I/O */
0190         read_unlock(&vport->vmid_lock);
0191         rc = -EBUSY;
0192     } else {
0193         /* The VMID was not found in the hashtable. At this point, */
0194         /* drop the read lock first before proceeding further */
0195         read_unlock(&vport->vmid_lock);
0196         /* start the process to obtain one as per the */
0197         /* type of the VMID indicated */
0198         write_lock(&vport->vmid_lock);
0199         vmp = lpfc_get_vmid_from_hashtable(vport, hash, uuid);
0200 
0201         /* while the read lock was released, in case the entry was */
0202         /* added by other context or is in process of being added */
0203         if (vmp && vmp->flag & LPFC_VMID_REGISTERED) {
0204             lpfc_vmid_update_entry(vport, iodir, vmp, tag);
0205             write_unlock(&vport->vmid_lock);
0206             return 0;
0207         } else if (vmp && vmp->flag & LPFC_VMID_REQ_REGISTER) {
0208             write_unlock(&vport->vmid_lock);
0209             return -EBUSY;
0210         }
0211 
0212         /* else search and allocate a free slot in the hash table */
0213         if (vport->cur_vmid_cnt < vport->max_vmid) {
0214             for (i = 0; i < vport->max_vmid; i++) {
0215                 vmp = vport->vmid + i;
0216                 if (vmp->flag == LPFC_VMID_SLOT_FREE)
0217                     break;
0218             }
0219             if (i == vport->max_vmid)
0220                 vmp = NULL;
0221         } else {
0222             vmp = NULL;
0223         }
0224 
0225         if (!vmp) {
0226             write_unlock(&vport->vmid_lock);
0227             return -ENOMEM;
0228         }
0229 
0230         /* Add the vmid and register */
0231         lpfc_put_vmid_in_hashtable(vport, hash, vmp);
0232         vmp->vmid_len = len;
0233         memcpy(vmp->host_vmid, uuid, vmp->vmid_len);
0234         vmp->io_rd_cnt = 0;
0235         vmp->io_wr_cnt = 0;
0236         vmp->flag = LPFC_VMID_SLOT_USED;
0237 
0238         vmp->delete_inactive =
0239             vport->vmid_inactivity_timeout ? 1 : 0;
0240 
0241         /* if type priority tag, get next available VMID */
0242         if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO)
0243             lpfc_vmid_assign_cs_ctl(vport, vmp);
0244 
0245         /* allocate the per cpu variable for holding */
0246         /* the last access time stamp only if VMID is enabled */
0247         if (!vmp->last_io_time)
0248             vmp->last_io_time = __alloc_percpu(sizeof(u64),
0249                                __alignof__(struct
0250                                lpfc_vmid));
0251         if (!vmp->last_io_time) {
0252             hash_del(&vmp->hnode);
0253             vmp->flag = LPFC_VMID_SLOT_FREE;
0254             write_unlock(&vport->vmid_lock);
0255             return -EIO;
0256         }
0257 
0258         write_unlock(&vport->vmid_lock);
0259 
0260         /* complete transaction with switch */
0261         if (vport->phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO)
0262             rc = lpfc_vmid_uvem(vport, vmp, true);
0263         else if (vport->phba->cfg_vmid_app_header)
0264             rc = lpfc_vmid_cmd(vport, SLI_CTAS_RAPP_IDENT, vmp);
0265         if (!rc) {
0266             write_lock(&vport->vmid_lock);
0267             vport->cur_vmid_cnt++;
0268             vmp->flag |= LPFC_VMID_REQ_REGISTER;
0269             write_unlock(&vport->vmid_lock);
0270         } else {
0271             write_lock(&vport->vmid_lock);
0272             hash_del(&vmp->hnode);
0273             vmp->flag = LPFC_VMID_SLOT_FREE;
0274             free_percpu(vmp->last_io_time);
0275             write_unlock(&vport->vmid_lock);
0276             return -EIO;
0277         }
0278 
0279         /* finally, enable the idle timer once */
0280         if (!(vport->phba->pport->vmid_flag & LPFC_VMID_TIMER_ENBLD)) {
0281             mod_timer(&vport->phba->inactive_vmid_poll,
0282                   jiffies +
0283                   msecs_to_jiffies(1000 * LPFC_VMID_TIMER));
0284             vport->phba->pport->vmid_flag |= LPFC_VMID_TIMER_ENBLD;
0285         }
0286     }
0287     return rc;
0288 }