Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pw1Q5-0000Dq-3o for mharc-grub-devel@gnu.org; Mon, 08 May 2023 10:00:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pw1Q3-0008WM-JY for grub-devel@gnu.org; Mon, 08 May 2023 10:00:51 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pw1Q1-0006Lt-2q for grub-devel@gnu.org; Mon, 08 May 2023 10:00:51 -0400 Received: from pps.filterd (m0353728.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 348DvxbE028598 for ; Mon, 8 May 2023 14:00:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=fpELL+WYRjkYAAIB1QpzELA6Ke614Pv0ZIGf21B503k=; b=rojr8ocskSCVcj/ND3aalU0mpJ/JiATaP18VPBaK42zIEgXK9eDW6Gkyoy9Nm48scNH7 S203emSclW95zQSGbSO/LAmbeY4hi7LLJABHPIholxFvGb+ztgFsUUZpl1WgglnsMuEg 2wgeZOf9xRGgcxKGZPPeA5CSY50h2xfs55ByEMZfWuX2zJg/hYNlSRITVruuvMlYE8KB 5FyZCBXt/mbilvnCbyFZrPr+rwZw2f7qH5mqR/Ts8d01A9zd11U5I6cUrO6nXTKhaqdE DjVsQ9EGl2I2YMM/+XDeaixN/Iwk9j4OdtfCWXFUuUDlldNMOcVmsq/ev34C4i1I0sxg kA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qf271gj94-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 08 May 2023 14:00:47 +0000 Received: from m0353728.ppops.net (m0353728.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 348DxeTo002831 for ; Mon, 8 May 2023 14:00:46 GMT Received: from ppma02fra.de.ibm.com (47.49.7a9f.ip4.static.sl-reverse.com [159.122.73.71]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3qf271gj76-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 08 May 2023 14:00:46 +0000 Received: from pps.filterd (ppma02fra.de.ibm.com [127.0.0.1]) by ppma02fra.de.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3489aDcZ011417; Mon, 8 May 2023 14:00:43 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma02fra.de.ibm.com (PPS) with ESMTPS id 3qdeh6gxjm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 08 May 2023 14:00:43 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 348E0dYB21299812 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 8 May 2023 14:00:40 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CF0AB20040; Mon, 8 May 2023 14:00:39 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2BA6C2005A; Mon, 8 May 2023 14:00:37 +0000 (GMT) Received: from li-5fd77d4c-2a67-11b2-a85c-de894c326bed.ibm.com.com (unknown [9.43.93.74]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 8 May 2023 14:00:36 +0000 (GMT) From: Avnish Chouhan To: grub-devel@gnu.org Cc: meghanaprakash@in.ibm.com, brking@linux.vnet.ibm.com, mchauras@linux.vnet.ibm.com, mamatha4@linux.vnet.ibm.com, rharwood@redhat.com, mlewando@redhat.com, Avnish Chouhan , Diego Domingos Subject: [PATCH 6/6] ieee1275: ofpath enable NVMeoF logical device translate Date: Mon, 8 May 2023 19:28:40 +0530 Message-Id: <20230508135840.47765-7-avnish@linux.vnet.ibm.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230508135840.47765-1-avnish@linux.vnet.ibm.com> References: <20230508135840.47765-1-avnish@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: Z917YmLNOOU_4wsZ7soBaze_-7Ww9tDm X-Proofpoint-ORIG-GUID: p04ql7qdJereH8MwAQq1Aer-2xXvTTgD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-08_10,2023-05-05_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 spamscore=0 phishscore=0 mlxlogscore=772 malwarescore=0 bulkscore=0 priorityscore=1501 impostorscore=0 lowpriorityscore=0 clxscore=1015 mlxscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2305080091 Received-SPF: none client-ip=148.163.156.1; envelope-from=avnish@linux.vnet.ibm.com; helo=mx0a-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 May 2023 14:00:51 -0000 This patch add code to enable the translation of logical devices to the of NVMeoFC paths. Signed-off-by: Diego Domingos Signed-off-by: Avnish Chouhan --- grub-core/osdep/linux/ofpath.c | 371 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- include/grub/util/ofpath.h | 28 ++++++++++++++++++++++++++++ 2 file changed, 390 insertions(+), 9 deletions(-) diff --git a/grub-core/osdep/linux/ofpath.c b/grub-core/osdep/linux/ofpath.c index cc849d9..a2b4eeb 100644 --- a/grub-core/osdep/linux/ofpath.c +++ b/grub-core/osdep/linux/ofpath.c @@ -137,7 +137,7 @@ trim_newline (char *path) *end-- = '\0'; } -#define MAX_DISK_CAT 64 +#define MAX_DISK_CAT 512 static char * find_obppath (const char *sysfs_path_orig) @@ -313,6 +313,92 @@ get_basename(char *p) return ret; } + +int +add_filename_to_pile(char *filename, struct ofpath_files_list_root* root) +{ + struct ofpath_files_list_node* file; + + file = malloc(sizeof(struct ofpath_files_list_node)); + if (!file) + return -1; + + file->filename = malloc(sizeof(char) * 1024); + if (!file->filename) + { + grub_free(file); + return -1; + } + + grub_strcpy (file->filename, filename); + if (root->first == NULL) + { + root->items = 1; + root->first = file; + file->next = NULL; + } + else + { + root->items++; + file->next = root->first; + root->first = file; + } + + return 0; +} + +void +find_file(char* filename, char* directory, struct ofpath_files_list_root* root, int max_depth, int depth) +{ + struct dirent *ep; + struct stat statbuf; + DIR *dp; + int ret_val=0; + char* full_path; + + if (depth > max_depth) + { + return; + } + + if ((dp = opendir(directory)) == NULL) + { + return; + } + + full_path = malloc(1024 * sizeof(char)); + if (!full_path) + return; + + while ((ep = readdir(dp)) != NULL) + { + snprintf(full_path,1024, "%s/%s", directory, ep->d_name); + lstat(full_path, &statbuf); + + if (S_ISLNK(statbuf.st_mode)) + { + continue; + } + + if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..")) + { + continue; + } + + if (!strcmp(ep->d_name, filename)) + { + ret_val = add_filename_to_pile(full_path, root); + if (ret_val == -1) + continue; + } + + find_file(filename, full_path, root, max_depth, depth+1); + } + + grub_free(full_path); + closedir(dp); +} + static char * of_path_of_vdisk(const char *sys_devname __attribute__((unused)), const char *device, @@ -383,7 +469,200 @@ of_fc_port_name(const char *path, const char *subpath, char *port_name) free(basepath); } -#ifdef __sparc__ +void +free_ofpath_files_list(struct ofpath_files_list_root* root) +{ + struct ofpath_files_list_node* node = root->first; + struct ofpath_files_list_node* next; + + while (node!=NULL) + { + next = node->next; + grub_free(node->filename); + grub_free(node); + node = next; + } + + grub_free(root); + return; +} + +char* +of_find_fc_host(char* host_wwpn) +{ + FILE* fp; + char *buf; + char *ret_val; + char portname_filename[sizeof("port_name")] = "port_name"; + char devices_path[sizeof("/sys/devices")] = "/sys/devices"; + struct ofpath_files_list_root* portnames_file_list; + struct ofpath_files_list_node* node; + + ret_val = malloc(sizeof(char)*1024); + if (!ret_val) + return NULL; + + portnames_file_list = malloc(sizeof(struct ofpath_files_list_root)); + if (!portnames_file_list) + { + grub_free(ret_val); + return NULL; + } + + portnames_file_list->items = 0; + portnames_file_list->first = NULL; + find_file(portname_filename, devices_path, portnames_file_list, 10, 0); + node = portnames_file_list->first; + + while (node != NULL) + { + fp = fopen(node->filename, "r"); + buf = malloc(sizeof(char) * 512); + if (!buf) + break; + + fscanf(fp, "%s", buf); + fclose(fp); + + if ((strcmp(buf,host_wwpn) == 0) && grub_strstr(node->filename, "fc_host")) + { + grub_free(buf); + grub_strcpy(ret_val, node->filename); + free_ofpath_files_list(portnames_file_list); + return ret_val; + } + + node = node->next; + grub_free(buf); + } + free_ofpath_files_list(portnames_file_list); + grub_free(ret_val); + return NULL; +} + +int +of_path_get_nvmeof_adapter_info(char* sysfs_path, + struct ofpath_nvmeof_info* nvmeof_info) +{ + FILE *fp; + char *buf, *buf2, *buf3; + + nvmeof_info->host_wwpn = malloc(sizeof(char) * 256); + nvmeof_info->target_wwpn = malloc(sizeof(char) * 256); + nvmeof_info->nqn = malloc(sizeof(char) * 256); + + if (nvmeof_info->host_wwpn == NULL || nvmeof_info->target_wwpn == NULL || nvmeof_info->nqn == NULL) + { + grub_free(nvmeof_info->host_wwpn); + grub_free(nvmeof_info->target_wwpn); + grub_free(nvmeof_info->nqn); + return -1; + } + + buf = malloc(sizeof(char) * 512); + if (!buf) + { + grub_free(nvmeof_info->host_wwpn); + grub_free(nvmeof_info->target_wwpn); + grub_free(nvmeof_info->nqn); + return -1; + } + + snprintf(buf, 512, "%s/subsysnqn", sysfs_path); + fp = fopen(buf, "r"); + fscanf(fp, "%s", nvmeof_info->nqn); + fclose(fp); + + snprintf(buf, 512, "%s/cntlid", sysfs_path); + fp = fopen(buf, "r"); + fscanf(fp, "%u", &(nvmeof_info->cntlid)); + fclose(fp); + + snprintf(buf, 512, "%s/address", sysfs_path); + fp = fopen(buf, "r"); + buf2 = malloc(sizeof(char) * 512); + + if (!buf2) + { + grub_free(nvmeof_info->host_wwpn); + grub_free(nvmeof_info->target_wwpn); + grub_free(nvmeof_info->nqn); + grub_free(buf); + return -1; + } + + fscanf(fp, "%s", buf2); + fclose(fp); + + buf3 = strrchr(buf2, '-') + 1; + grub_memcpy(nvmeof_info->host_wwpn, buf3, 256); + buf3=strchr(buf2, '-') + 1; + buf3=strchr(buf3, '-') + 1; + buf3=strchr(buf3, 'x') + 1; + grub_memcpy(nvmeof_info->target_wwpn, buf3, 256); + buf3 = strchr(nvmeof_info->target_wwpn, ','); + *buf3 = '\0'; + grub_free(buf); + grub_free(buf2); + return 0; +} + +#define MAX_NVME_NSID_DIGITS 6 + +static char * +of_path_get_nvme_controller_name_node(const char* devname) +{ + char *controller_node, *end; + + controller_node = strdup(devname); + end = grub_strchr(controller_node + 1, 'n'); + if (end != NULL) + { + *end = '\0'; + } + + return controller_node; +} + +unsigned int +of_path_get_nvme_nsid(const char* devname) +{ + unsigned int nsid; + char *sysfs_path, *buf; + FILE *fp; + + buf = malloc(sizeof(char) * 512); + if (!buf) + return 0; + + sysfs_path = block_device_get_sysfs_path_and_link(devname); + snprintf(buf, 512, "%s/%s/nsid", sysfs_path,devname); + fp = fopen(buf, "r"); + fscanf(fp, "%u", &(nsid)); + fclose(fp); + + free(sysfs_path); + free(buf); + return nsid; +} + +static char * +nvme_get_syspath(const char *nvmedev) +{ + char *sysfs_path, *controller_node; + + sysfs_path = block_device_get_sysfs_path_and_link (nvmedev); + if (strstr(sysfs_path,"nvme-subsystem")) + { + controller_node = of_path_get_nvme_controller_name_node(nvmedev); + strcat(sysfs_path, "/"); + strcat(sysfs_path, controller_node); + sysfs_path = xrealpath(sysfs_path); + } + + return sysfs_path; +} + static char * of_path_of_nvme(const char *sys_devname __attribute__((unused)), const char *device, @@ -392,6 +671,8 @@ of_path_of_nvme(const char *sys_devname __attribute__((unused)), { char *sysfs_path, *of_path, disk[MAX_DISK_CAT]; const char *digit_string, *part_end; + int chars_written, ret_val; + struct ofpath_nvmeof_info* nvmeof_info; digit_string = trailing_digits (device); part_end = devicenode + strlen (devicenode) - 1; @@ -411,15 +692,90 @@ of_path_of_nvme(const char *sys_devname __attribute__((unused)), /* Remove the p. */ *end = '\0'; sscanf (digit_string, "%d", &part); - snprintf (disk, sizeof (disk), "/disk@1:%c", 'a' + (part - 1)); - sysfs_path = block_device_get_sysfs_path_and_link (nvmedev); + sysfs_path = nvme_get_syspath(nvmedev); + + /* If is a NVMeoF */ + if (strstr(sysfs_path,"nvme-fabrics")) + { + nvmeof_info = malloc(sizeof(struct ofpath_nvmeof_info)); + if (!nvmeof_info) + { + grub_free (nvmedev); + return NULL; + } + + ret_val = of_path_get_nvmeof_adapter_info(sysfs_path, nvmeof_info); + if (ret_val == -1) + { + grub_free (nvmedev); + grub_free (nvmeof_info); + return NULL; + } + + sysfs_path = of_find_fc_host(nvmeof_info->host_wwpn); + if (!sysfs_path) + { + grub_free (nvmedev); + grub_free (nvmeof_info); + return NULL; + } + + chars_written = snprintf(disk,sizeof(disk), "/nvme-of/controller@%s,%x:nqn=%s", + nvmeof_info->target_wwpn,0xffff, + nvmeof_info->nqn); + unsigned int nsid = of_path_get_nvme_nsid(nvmedev); + if (nsid) + { + snprintf(disk+chars_written, sizeof("/namespace@") + MAX_NVME_NSID_DIGITS, + "/namespace@%x:%d", nsid, part); + } + grub_free(nvmeof_info); + } + else + { + snprintf (disk, sizeof (disk), "/disk@1:%c", 'a' + (part - 1)); + } free (nvmedev); } else { /* We do not have the parition. */ - snprintf (disk, sizeof (disk), "/disk@1"); - sysfs_path = block_device_get_sysfs_path_and_link (device); + sysfs_path = nvme_get_syspath (device); + if(strstr(sysfs_path, "nvme-fabrics")) + { + nvmeof_info = malloc(sizeof(struct ofpath_nvmeof_info)); + if (!nvmeof_info) + return NULL; + + ret_val = of_path_get_nvmeof_adapter_info(sysfs_path, nvmeof_info); + if (ret_val == -1) + { + grub_free (nvmeof_info); + return NULL; + } + + sysfs_path = of_find_fc_host(nvmeof_info->host_wwpn); + if (!sysfs_path) + { + grub_free (nvmeof_info); + return NULL; + } + + chars_written = snprintf(disk,sizeof(disk), "/nvme-of/controller@%s,%x:nqn=%s", + nvmeof_info->target_wwpn, 0xffff, + nvmeof_info->nqn); + unsigned int nsid = of_path_get_nvme_nsid(device); + if(nsid) + { + snprintf(disk+chars_written,sizeof("/namespace@") + sizeof(char) * MAX_NVME_NSID_DIGITS, + "/namespace@%x", nsid); + } + grub_free(nvmeof_info); + } + else + { + snprintf (disk, sizeof (disk), "/disk@1"); + } } of_path = find_obppath (sysfs_path); @@ -430,7 +786,6 @@ of_path_of_nvme(const char *sys_devname __attribute__((unused)), free (sysfs_path); return of_path; } -#endif static int vendor_is_ATA(const char *path) @@ -841,11 +1196,9 @@ grub_util_devname_to_ofpath (const char *sys_devname) /* All the models I've seen have a devalias "floppy". New models have no floppy at all. */ ofpath = xstrdup ("floppy"); -#ifdef __sparc__ else if (device[0] == 'n' && device[1] == 'v' && device[2] == 'm' && device[3] == 'e') ofpath = of_path_of_nvme (name_buf, device, devnode, devicenode); -#endif else { grub_util_warn (_("unknown device type %s"), device); diff --git a/include/grub/util/ofpath.h b/include/grub/util/ofpath.h index b43c523..85172bc 100644 --- a/include/grub/util/ofpath.h +++ b/include/grub/util/ofpath.h @@ -3,4 +3,32 @@ char *grub_util_devname_to_ofpath (const char *devname); +struct ofpath_files_list_node +{ + char* filename; + struct ofpath_files_list_node* next; +}; + +struct ofpath_files_list_root +{ + int items; + struct ofpath_files_list_node* first; +}; + +struct ofpath_nvmeof_info +{ + char* host_wwpn; + char* target_wwpn; + char* nqn; + int cntlid; + int nsid; +}; + +int of_path_get_nvmeof_adapter_info(char* sysfs_path, struct ofpath_nvmeof_info* nvmeof_info); +unsigned int of_path_get_nvme_nsid(const char* devname); +int add_filename_to_pile(char *filename, struct ofpath_files_list_root* root); +void find_file(char* filename, char* directory, struct ofpath_files_list_root* root, int max_depth, int depth); +char* of_find_fc_host(char* host_wwpn); +void free_ofpath_files_list(struct ofpath_files_list_root* root); + #endif /* ! GRUB_OFPATH_MACHINE_UTIL_HEADER */ -- 2.31.1