Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 495bbe94e5126512132d69685e96e0d7 > files > 9

fcoe-utils-1.0.18-2.fc15.src.rpm

From 48677372717fcbfcff17dbcd13441d9049f027e8 Mon Sep 17 00:00:00 2001
From: Parikh, Neerav <neerav.parikh@intel.com>
Date: Wed, 9 Mar 2011 08:16:46 +0000
Subject: [PATCH 03/16] fcoe-utils: Handle more than 128 adapters in fcoeadm
 display commands

Currently fcoeadm only supports 128 adapters for display routines.
It does not handle scenario if there are more than 128 adapters
available on the system.

This patch removes that limitation and would allow display of more
than 128 adapters using fcoeadm display commands.

Signed-off-by: Neerav Parikh <Neerav.Parikh@intel.com>
Tested-by: Ross Brattain <ross.b.brattain@intel.com>
Signed-off-by: Robert Love <robert.w.love@intel.com>
Signed-off-by: Petr Sabata <psabata@redhat.com>
---
 fcoeadm_display.c |  211 ++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 136 insertions(+), 75 deletions(-)

diff --git a/fcoeadm_display.c b/fcoeadm_display.c
index 6b69148..8f95530 100644
--- a/fcoeadm_display.c
+++ b/fcoeadm_display.c
@@ -53,9 +53,6 @@
 /* #define TEST_READ_CAP_V1 */
 /* #define TEST_DEV_SERIAL_NO */
 
-/* Maximum number of HBA the display routines support */
-#define MAX_HBA_COUNT      128
-
 /* Define FC4 Type */
 #define FC_TYPE_FCP        0x08 /* SCSI FCP */
 
@@ -84,6 +81,14 @@ struct hba_name_table {
 	int                   displayed;
 };
 
+/*
+ * List of HBA objects.
+ */
+struct hba_name_table_list {
+	int			hba_count;
+	struct hba_name_table	hba_table[1];
+};
+
 struct sa_nameval port_states[] = {
 	{ "Not Present",    HBA_PORTSTATE_UNKNOWN },
 	{ "Online",         HBA_PORTSTATE_ONLINE },
@@ -1040,17 +1045,18 @@ show_port_stats_in_row(HBA_INT64 start_time,
 	printf("\n");
 }
 
-static void hba_table_destroy(struct hba_name_table *hba_table)
+static void hba_table_list_destroy(struct hba_name_table_list *hba_table_list)
 {
 	int i;
 
-	/*
-	 * This is inefficiant as is closes adapter handles
-	 * (presumedly 0) that weren't open. However, it allows
-	 * us to avoid maintaining a count.
-	 */
-	for (i = 0 ; i < MAX_HBA_COUNT ; i++)
-		HBA_CloseAdapter(hba_table[i].hba_handle);
+	if (!hba_table_list)
+		return;
+
+	for (i = 0 ; i < hba_table_list->hba_count ; i++)
+		HBA_CloseAdapter(hba_table_list->hba_table[i].hba_handle);
+
+	free(hba_table_list);
+	hba_table_list = NULL;
 }
 
 static enum fcoe_status fcoeadm_loadhba()
@@ -1064,19 +1070,14 @@ static enum fcoe_status fcoeadm_loadhba()
 /*
  * This routine leaves all adapters fd's open.
  */
-static int hba_table_init(struct hba_name_table *hba_table)
+static int hba_table_list_init(struct hba_name_table_list **hba_table_list)
 {
 	HBA_STATUS retval;
 	char namebuf[1024];
 	int i, num_hbas = 0;
-
-	/*
-	 * Initialize the table.
-	 */
-	for (i = 0 ; i < MAX_HBA_COUNT ; i++) {
-		memset(&hba_table[i], 0,
-		       sizeof(struct hba_name_table));
-	}
+	struct hba_name_table_list *hba_table_list_temp = NULL;
+	struct hba_name_table *hba_table = NULL;
+	int size = 0;
 
 	num_hbas = HBA_GetNumberOfAdapters();
 	if (!num_hbas) {
@@ -1084,6 +1085,18 @@ static int hba_table_init(struct hba_name_table *hba_table)
 		return num_hbas;
 	}
 
+	size = sizeof(struct hba_name_table_list) + \
+			(num_hbas - 1)*sizeof(struct hba_name_table);
+
+	hba_table_list_temp = (struct hba_name_table_list *)calloc(1, size);
+	if (!hba_table_list_temp) {
+		fprintf(stderr,
+			"Failure allocating memory.\n");
+		return -1;
+	}
+
+	hba_table_list_temp->hba_count = num_hbas;
+
 	/*
 	 * Fill out the HBA table.
 	 */
@@ -1095,19 +1108,20 @@ static int hba_table_init(struct hba_name_table *hba_table)
 			continue;
 		}
 
-		hba_table[i].hba_handle = HBA_OpenAdapter(namebuf);
-		if (!hba_table[i].hba_handle) {
-			hba_table[i].failed = 1;
+		hba_table = &hba_table_list_temp->hba_table[i];
+		hba_table->hba_handle = HBA_OpenAdapter(namebuf);
+		if (!hba_table->hba_handle) {
+			hba_table->failed = 1;
 			fprintf(stderr, "HBA_OpenAdapter failed\n");
 			perror("HBA_OpenAdapter");
 			continue;
 		}
 
-		retval = HBA_GetAdapterAttributes(hba_table[i].hba_handle,
-						  &hba_table[i].hba_attrs);
+		retval = HBA_GetAdapterAttributes(hba_table->hba_handle,
+						  &hba_table->hba_attrs);
 		if (retval != HBA_STATUS_OK) {
-			HBA_CloseAdapter(hba_table[i].hba_handle);
-			hba_table[i].failed = 1;
+			HBA_CloseAdapter(hba_table->hba_handle);
+			hba_table->failed = 1;
 			fprintf(stderr,
 				"HBA_GetAdapterAttributes failed, retval=%d\n",
 				retval);
@@ -1115,12 +1129,12 @@ static int hba_table_init(struct hba_name_table *hba_table)
 			continue;
 		}
 
-		retval = HBA_GetAdapterPortAttributes(hba_table[i].hba_handle,
+		retval = HBA_GetAdapterPortAttributes(hba_table->hba_handle,
 						      0,
-						      &hba_table[i].port_attrs);
+						      &hba_table->port_attrs);
 		if (retval != HBA_STATUS_OK) {
-			HBA_CloseAdapter(hba_table[i].hba_handle);
-			hba_table[i].failed = 1;
+			HBA_CloseAdapter(hba_table->hba_handle);
+			hba_table->failed = 1;
 			fprintf(stderr,
 				"HBA_GetAdapterPortAttributes failed, "
 				"retval=%d\n", retval);
@@ -1128,20 +1142,26 @@ static int hba_table_init(struct hba_name_table *hba_table)
 		}
 	}
 
+	*hba_table_list = hba_table_list_temp;
+
 	return num_hbas;
 }
 
 /*
  * This routine expects a valid interface name.
  */
-static int get_index_for_ifname(struct hba_name_table *hba_table,
-				int num_hbas, const char *ifname)
+static int get_index_for_ifname(struct hba_name_table_list *hba_table_list,
+				const char *ifname)
 {
+	HBA_PORTATTRIBUTES *port_attrs;
 	int i;
 
-	for (i = 0 ; i < num_hbas ; i++) {
+	for (i = 0 ; i < hba_table_list->hba_count ; i++) {
+
+		port_attrs = &hba_table_list->hba_table[i].port_attrs;
+
 		if (!check_symbolic_name_for_interface(
-			    hba_table[i].port_attrs.PortSymbolicName,
+			    port_attrs->PortSymbolicName,
 			    ifname))
 			return i;
 	}
@@ -1157,28 +1177,35 @@ enum fcoe_status display_port_stats(const char *ifname, int interval)
 	HBA_PORTSTATISTICS port_stats;
 	HBA_FC4STATISTICS port_fc4stats;
 	HBA_INT64 start_time = 0;
-	struct hba_name_table hba_table[MAX_HBA_COUNT];
+	struct hba_name_table_list *hba_table_list = NULL;
 	enum fcoe_status rc = SUCCESS;
 	int i, num_hbas;
 
 	if (fcoeadm_loadhba())
 		return EHBAAPIERR;
 
-	num_hbas = hba_table_init(hba_table);
+	num_hbas = hba_table_list_init(&hba_table_list);
+	if (!num_hbas)
+		goto out;
+
+	if (num_hbas < 0) {
+		rc = EINTERR;
+		goto out;
+	}
 
-	i = get_index_for_ifname(hba_table, num_hbas, ifname);
+	i = get_index_for_ifname(hba_table_list, ifname);
 
 	/*
 	 * Return error code if a valid index wasn't returned.
 	 */
 	if (i < 0) {
-		hba_table_destroy(hba_table);
+		hba_table_list_destroy(hba_table_list);
 		HBA_FreeLibrary();
 		return EHBAAPIERR;
 	}
 
-	hba_handle = hba_table[i].hba_handle;
-	port_attrs = &hba_table[i].port_attrs;
+	hba_handle = hba_table_list->hba_table[i].hba_handle;
+	port_attrs = &hba_table_list->hba_table[i].port_attrs;
 
 	i = 0;
 	while (1) {
@@ -1231,21 +1258,34 @@ enum fcoe_status display_port_stats(const char *ifname, int interval)
 		} while (secs_left);
 	}
 
-	hba_table_destroy(hba_table);
+	hba_table_list_destroy(hba_table_list);
+out:
 	HBA_FreeLibrary();
 	return rc;
 }
 
 enum fcoe_status display_adapter_info(const char *ifname)
 {
-	struct hba_name_table hba_table[MAX_HBA_COUNT];
+	struct hba_name_table_list *hba_table_list = NULL;
 	enum fcoe_status rc = SUCCESS;
 	int i, j, num_hbas = 0;
+	HBA_HANDLE hba_handle;
+	HBA_PORTATTRIBUTES *port_attrs;
+	HBA_PORTATTRIBUTES *sport_attrs;
+	HBA_ADAPTERATTRIBUTES *hba_attrs;
+	HBA_ADAPTERATTRIBUTES *shba_attrs;
 
 	if (fcoeadm_loadhba())
 		return EHBAAPIERR;
 
-	num_hbas = hba_table_init(hba_table);
+	num_hbas = hba_table_list_init(&hba_table_list);
+	if (!num_hbas)
+		goto out;
+
+	if (num_hbas < 0) {
+		rc = EINTERR;
+		goto out;
+	}
 
 	/*
 	 * Loop through each HBA entry and for each serial number
@@ -1253,52 +1293,59 @@ enum fcoe_status display_adapter_info(const char *ifname)
 	 * on that adapter.
 	 */
 	for (i = 0 ; i < num_hbas ; i++) {
-		if (hba_table[i].failed ||
-		    hba_table[i].displayed)
+		if (hba_table_list->hba_table[i].failed ||
+		    hba_table_list->hba_table[i].displayed)
 			continue;
 
+		hba_handle = hba_table_list->hba_table[i].hba_handle;
+		port_attrs = &hba_table_list->hba_table[i].port_attrs;
+		hba_attrs = &hba_table_list->hba_table[i].hba_attrs;
+
 		if (ifname && check_symbolic_name_for_interface(
-			    hba_table[i].port_attrs.PortSymbolicName,
+			    port_attrs->PortSymbolicName,
 			    ifname)) {
 			/*
 			 * Overloading 'displayed' to indicate
 			 * that the HBA/Port should be skipped.
 			 */
-			hba_table[i].displayed = 1;
+			hba_table_list->hba_table[i].displayed = 1;
 			continue;
 		}
 
 		/*
 		 * Display the adapter header.
 		 */
-		show_hba_info(&hba_table[i].hba_attrs);
+		show_hba_info(hba_attrs);
 
 		/*
 		 * Loop through HBAs again to print sub-ports.
 		 */
 		for (j = 0; j < num_hbas ; j++) {
+			sport_attrs = &hba_table_list->hba_table[j].port_attrs;
+			shba_attrs = &hba_table_list->hba_table[j].hba_attrs;
 			if (ifname && check_symbolic_name_for_interface(
-				    hba_table[j].port_attrs.PortSymbolicName,
+				    sport_attrs->PortSymbolicName,
 				    ifname)) {
 				/*
 				 * Overloading 'displayed' to indicate
 				 * that the HBA/Port should be skipped.
 				 */
-				hba_table[i].displayed = 1;
+				hba_table_list->hba_table[i].displayed = 1;
 				continue;
 			}
 
-			if (!strncmp(hba_table[i].hba_attrs.SerialNumber,
-				     hba_table[j].hba_attrs.SerialNumber,
-				     strlen(hba_table[i].hba_attrs.SerialNumber))) {
-				show_port_info(&hba_table[j].hba_attrs,
-					       &hba_table[j].port_attrs);
-				hba_table[j].displayed = 1;
+			if (!strncmp(hba_attrs->SerialNumber,
+				     shba_attrs->SerialNumber,
+				     strlen(hba_attrs->SerialNumber))) {
+				show_port_info(shba_attrs,
+					       sport_attrs);
+				hba_table_list->hba_table[j].displayed = 1;
 			}
 		}
 	}
 
-	hba_table_destroy(hba_table);
+	hba_table_list_destroy(hba_table_list);
+out:
 	HBA_FreeLibrary();
 
 	return rc;
@@ -1309,14 +1356,24 @@ enum fcoe_status display_target_info(const char *ifname,
 {
 	HBA_STATUS retval;
 	HBA_PORTATTRIBUTES rport_attrs;
-	struct hba_name_table hba_table[MAX_HBA_COUNT];
+	struct hba_name_table_list *hba_table_list = NULL;
 	int i, target_index, num_hbas = 0;
 	enum fcoe_status rc = SUCCESS;
+	HBA_HANDLE hba_handle;
+	HBA_PORTATTRIBUTES *port_attrs;
+	HBA_ADAPTERATTRIBUTES *hba_attrs;
 
 	if (fcoeadm_loadhba())
 		return EHBAAPIERR;
 
-	num_hbas = hba_table_init(hba_table);
+	num_hbas = hba_table_list_init(&hba_table_list);
+	if (!num_hbas)
+		goto out;
+
+	if (num_hbas < 0) {
+		rc = EINTERR;
+		goto out;
+	}
 
 	/*
 	 * Loop through each HBA entry and for each serial number
@@ -1324,28 +1381,32 @@ enum fcoe_status display_target_info(const char *ifname,
 	 * on that adapter.
 	 */
 	for (i = 0 ; i < num_hbas ; i++) {
-		if (hba_table[i].failed ||
-		    hba_table[i].displayed)
+		if (hba_table_list->hba_table[i].failed ||
+		    hba_table_list->hba_table[i].displayed)
 			continue;
 
+		hba_handle = hba_table_list->hba_table[i].hba_handle;
+		port_attrs = &hba_table_list->hba_table[i].port_attrs;
+		hba_attrs = &hba_table_list->hba_table[i].hba_attrs;
+
 		if (ifname && check_symbolic_name_for_interface(
-			    hba_table[i].port_attrs.PortSymbolicName,
+			    port_attrs->PortSymbolicName,
 			    ifname)) {
 			/*
 			 * Overloading 'displayed' to indicate
 			 * that the HBA/Port should be skipped.
 			 */
-			hba_table[i].displayed = 1;
+			hba_table_list->hba_table[i].displayed = 1;
 			continue;
 		}
 
 		for (target_index = 0;
-		     target_index < hba_table[i].port_attrs.NumberofDiscoveredPorts;
+		     target_index < port_attrs->NumberofDiscoveredPorts;
 		     target_index++) {
 
 			/* TODO: Second arg might be incorrect */
 			retval = HBA_GetDiscoveredPortAttributes(
-				hba_table[i].hba_handle,
+				hba_handle,
 				0, target_index,
 				&rport_attrs);
 
@@ -1354,7 +1415,7 @@ enum fcoe_status display_target_info(const char *ifname,
 					"HBA_GetDiscoveredPortAttributes "
 					"failed for target_index=%d, "
 					"status=%d\n", target_index, retval);
-				hba_table[i].failed = 1;
+				hba_table_list->hba_table[i].failed = 1;
 				continue;
 			}
 
@@ -1365,26 +1426,26 @@ enum fcoe_status display_target_info(const char *ifname,
 				continue;
 
 			show_target_info(
-				hba_table[i].port_attrs.PortSymbolicName,
-				&hba_table[i].hba_attrs,
+				port_attrs->PortSymbolicName,
+				hba_attrs,
 				&rport_attrs);
 
-			if (hba_table[i].port_attrs.PortState !=
-			    HBA_PORTSTATE_ONLINE)
+			if (port_attrs->PortState != HBA_PORTSTATE_ONLINE)
 				continue;
 
 			/*
 			 * This will print the LUN table
 			 * under the target.
 			 */
-			scan_device_map(hba_table[i].hba_handle,
-					&hba_table[i].hba_attrs,
-					&hba_table[i].port_attrs,
+			scan_device_map(hba_handle,
+					hba_attrs,
+					port_attrs,
 					&rport_attrs, ifname, style);
 		}
 	}
 
-	hba_table_destroy(hba_table);
+	hba_table_list_destroy(hba_table_list);
+out:
 	HBA_FreeLibrary();
 
 	return rc;
-- 
1.7.4.4