From 72a45777041901f37d187920d03462ee6d5e655f Mon Sep 17 00:00:00 2001
From: Pawel Baldysiak <pawel.baldysiak@intel.com>
Date: Wed, 8 Apr 2015 11:42:18 +0200
Subject: [PATCH] IMSM: Count arrays per orom

Active arrays with IMSM metadata are counted per hba so far.
This is bad due to new functionality of orom shared between multiple
controllers i.e. more arrays can be created than is supported by orom.
This patch changes the way of counting arrays, so the result will be
sum of arrays under every hba supported by specific orom.

Signed-off-by: Pawel Baldysiak <pawel.baldysiak@intel.com>
Signed-off-by: NeilBrown <neilb@suse.de>
---
 platform-intel.c |   14 +++++++-
 platform-intel.h |    1 
 super-intel.c    |   91 ++++++++++++++++++++++++++++++++++++-------------------
 3 files changed, 74 insertions(+), 32 deletions(-)

--- mdadm-3.3.1.orig/platform-intel.c
+++ mdadm-3.3.1/platform-intel.c
@@ -236,7 +236,7 @@ const struct orom_entry *get_oroms(void)
 	return (const struct orom_entry *)&oroms;
 }
 
-const struct imsm_orom *get_orom_by_device_id(__u16 dev_id)
+const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id)
 {
 	int i;
 	struct devid_list *list;
@@ -244,12 +244,22 @@ const struct imsm_orom *get_orom_by_devi
 	for (i = 0; i < SYS_DEV_MAX; i++) {
 		for (list = oroms[i].devid_list; list; list = list->next) {
 			if (list->devid == dev_id)
-				return &oroms[i].orom;
+				return &oroms[i];
 		}
 	}
 	return NULL;
 }
 
+const struct imsm_orom *get_orom_by_device_id(__u16 dev_id)
+{
+	const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id);
+
+	if (entry)
+		return &entry->orom;
+
+	return NULL;
+}
+
 static const struct imsm_orom *add_orom(const struct imsm_orom *orom)
 {
 	int i;
--- mdadm-3.3.1.orig/platform-intel.h
+++ mdadm-3.3.1/platform-intel.h
@@ -237,5 +237,6 @@ char *devt_to_devpath(dev_t dev);
 int path_attached_to_hba(const char *disk_path, const char *hba_path);
 const char *get_sys_dev_type(enum sys_dev_type);
 const struct orom_entry * get_oroms(void);
+const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id);
 const struct imsm_orom *get_orom_by_device_id(__u16 device_id);
 struct sys_dev *device_by_id(__u16 device_id);
--- mdadm-3.3.1.orig/super-intel.c
+++ mdadm-3.3.1/super-intel.c
@@ -5865,37 +5865,68 @@ count_volumes_list(struct md_list *devli
 }
 
 static int
-count_volumes(char *hba, int dpa, int verbose)
+count_volumes(struct intel_hba *hba, int dpa, int verbose)
 {
-	struct md_list *devlist = NULL;
+	struct sys_dev *idev, *intel_devices = find_intel_devices();
 	int count = 0;
-	int found = 0;;
+	const struct orom_entry *entry;
+	struct devid_list *dv, *devid_list;
 
-	devlist = get_devices(hba);
-	/* if no intel devices return zero volumes */
-	if (devlist == NULL)
-		return 0;
-
-	count = active_arrays_by_format("imsm", hba, &devlist, dpa, verbose);
-	dprintf(" path: %s active arrays: %d\n", hba, count);
-	if (devlist == NULL)
-		return 0;
-	do  {
-		found = 0;
-		count += count_volumes_list(devlist,
-					    NULL,
-					    verbose,
-					    &found);
-		dprintf("found %d count: %d\n", found, count);
-	} while (found);
-
-	dprintf("path: %s total number of volumes: %d\n", hba, count);
-
-	while(devlist) {
-		struct md_list *dv = devlist;
-		devlist = devlist->next;
-		free(dv->devname);
-		free(dv);
+	if (!hba || !hba->path)
+		return 0;
+
+	for (idev = intel_devices; idev; idev = idev->next) {
+		if (strstr(idev->path, hba->path))
+				break;
+	}
+
+	if (!idev || !idev->dev_id)
+		return 0;
+
+	entry = get_orom_entry_by_device_id(idev->dev_id);
+
+	if (!entry || !entry->devid_list)
+		return 0;
+
+	devid_list = entry->devid_list;
+	for (dv = devid_list; dv; dv = dv->next) {
+
+		struct md_list *devlist = NULL;
+		struct sys_dev *device = device_by_id(dv->devid);
+		char *hba_path;
+		int found = 0;
+
+		if (device)
+			hba_path = device->path;
+		else
+			return 0;
+
+		devlist = get_devices(hba_path);
+		/* if no intel devices return zero volumes */
+		if (devlist == NULL)
+			return 0;
+
+		count += active_arrays_by_format("imsm", hba_path, &devlist, dpa, verbose);
+		dprintf(" path: %s active arrays: %d\n", hba_path, count);
+		if (devlist == NULL)
+			return 0;
+		do  {
+			found = 0;
+			count += count_volumes_list(devlist,
+							NULL,
+							verbose,
+							&found);
+			dprintf("found %d count: %d\n", found, count);
+		} while (found);
+
+		dprintf("path: %s total number of volumes: %d\n", hba_path, count);
+
+		while (devlist) {
+			struct md_list *dv = devlist;
+			devlist = devlist->next;
+			free(dv->devname);
+			free(dv);
+		}
 	}
 	return count;
 }
@@ -6130,7 +6161,7 @@ static int validate_geometry_imsm_volume
 	*freesize = maxsize;
 
 	if (super->orom) {
-		int count = count_volumes(super->hba->path,
+		int count = count_volumes(super->hba,
 				      super->orom->dpa, verbose);
 		if (super->orom->vphba <= count) {
 			pr_vrb(": platform does not support more than %d raid volumes.\n",
@@ -6288,7 +6319,7 @@ static int validate_geometry_imsm(struct
 			   created */
 			if (super->orom && freesize) {
 				int count;
-				count = count_volumes(super->hba->path,
+				count = count_volumes(super->hba,
 						      super->orom->dpa, verbose);
 				if (super->orom->vphba <= count) {
 					pr_vrb(": platform does not support more"
