megaraid_sas_fp.c 49.6 KB
Newer Older
1 2 3
/*
 *  Linux MegaRAID driver for SAS based RAID controllers
 *
4 5
 *  Copyright (c) 2009-2013  LSI Corporation
 *  Copyright (c) 2013-2014  Avago Technologies
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21
 *
 *  FILE: megaraid_sas_fp.c
 *
22
 *  Authors: Avago Technologies
23 24 25
 *           Sumant Patro
 *           Varad Talamacki
 *           Manoj Jose
26 27
 *           Kashyap Desai <kashyap.desai@avagotech.com>
 *           Sumit Saxena <sumit.saxena@avagotech.com>
28
 *
29
 *  Send feedback to: megaraidlinux.pdl@avagotech.com
30
 *
31 32
 *  Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
 *  San Jose, California 95131
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/list.h>
#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/uio.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/compat.h>
#include <linux/blkdev.h>
#include <linux/poll.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>

#include "megaraid_sas_fusion.h"
57
#include "megaraid_sas.h"
58 59
#include <asm/div64.h>

60 61 62 63 64 65 66
#define LB_PENDING_CMDS_DEFAULT 4
static unsigned int lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
module_param(lb_pending_cmds, int, S_IRUGO);
MODULE_PARM_DESC(lb_pending_cmds, "Change raid-1 load balancing outstanding "
	"threshold. Valid Values are 1-128. Default: 4");


67 68
#define ABS_DIFF(a, b)   (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
#define MR_LD_STATE_OPTIMAL 3
69 70 71 72

#ifdef FALSE
#undef FALSE
#endif
73
#define FALSE 0
74 75 76 77

#ifdef TRUE
#undef TRUE
#endif
78 79
#define TRUE 1

80 81 82 83 84
#define SPAN_DEBUG 0
#define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
#define SPAN_ROW_DATA_SIZE(map_, ld, index_)   (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
#define SPAN_INVALID  0xff

85
/* Prototypes */
86
static void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
87 88 89
	PLD_SPAN_INFO ldSpanInfo);
static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
	u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
90
	struct RAID_CONTEXT *pRAID_Context, struct MR_DRV_RAID_MAP_ALL *map);
91
static u64 get_row_from_strip(struct megasas_instance *instance, u32 ld,
92
	u64 strip, struct MR_DRV_RAID_MAP_ALL *map);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

u32 mega_mod64(u64 dividend, u32 divisor)
{
	u64 d;
	u32 remainder;

	if (!divisor)
		printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
	d = dividend;
	remainder = do_div(d, divisor);
	return remainder;
}

/**
 * @param dividend    : Dividend
 * @param divisor    : Divisor
 *
 * @return quotient
 **/
u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
{
	u32 remainder;
	u64 d;

	if (!divisor)
		printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");

	d = dividend;
	remainder = do_div(d, divisor);

	return d;
}

126
struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
127 128 129 130 131
{
	return &map->raidMap.ldSpanMap[ld].ldRaid;
}

static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
132
						   struct MR_DRV_RAID_MAP_ALL
133 134 135 136 137
						   *map)
{
	return &map->raidMap.ldSpanMap[ld].spanBlock[0];
}

138
static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_DRV_RAID_MAP_ALL *map)
139 140 141 142
{
	return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
}

143
u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_DRV_RAID_MAP_ALL *map)
144
{
145
	return le16_to_cpu(map->raidMap.arMapInfo[ar].pd[arm]);
146 147
}

148
u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_DRV_RAID_MAP_ALL *map)
149
{
150
	return le16_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef);
151 152
}

153
__le16 MR_PdDevHandleGet(u32 pd, struct MR_DRV_RAID_MAP_ALL *map)
154 155 156 157
{
	return map->raidMap.devHndlInfo[pd].curDevHdl;
}

158 159 160 161 162
static u8 MR_PdInterfaceTypeGet(u32 pd, struct MR_DRV_RAID_MAP_ALL *map)
{
	return map->raidMap.devHndlInfo[pd].interfaceType;
}

163
u16 MR_GetLDTgtId(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
164
{
165
	return le16_to_cpu(map->raidMap.ldSpanMap[ld].ldRaid.targetId);
166 167
}

168
u8 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_DRV_RAID_MAP_ALL *map)
169
{
170
	return map->raidMap.ldTgtIdToLd[ldTgtId];
171 172 173
}

static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
174
					  struct MR_DRV_RAID_MAP_ALL *map)
175 176 177 178
{
	return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
}

179 180 181 182 183 184 185 186
/*
 * This function will Populate Driver Map using firmware raid map
 */
void MR_PopulateDrvRaidMap(struct megasas_instance *instance)
{
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_FW_RAID_MAP_ALL     *fw_map_old    = NULL;
	struct MR_FW_RAID_MAP         *pFwRaidMap    = NULL;
187
	int i, j;
188
	u16 ld_count;
189 190 191
	struct MR_FW_RAID_MAP_DYNAMIC *fw_map_dyn;
	struct MR_FW_RAID_MAP_EXT *fw_map_ext;
	struct MR_RAID_MAP_DESC_TABLE *desc_table;
192 193 194 195 196


	struct MR_DRV_RAID_MAP_ALL *drv_map =
			fusion->ld_drv_map[(instance->map_id & 1)];
	struct MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	void *raid_map_data = NULL;

	memset(drv_map, 0, fusion->drv_map_sz);
	memset(pDrvRaidMap->ldTgtIdToLd,
		0xff, (sizeof(u16) * MAX_LOGICAL_DRIVES_DYN));

	if (instance->max_raid_mapsize) {
		fw_map_dyn = fusion->ld_map[(instance->map_id & 1)];
#if VD_EXT_DEBUG
		dev_dbg(&instance->pdev->dev, "raidMapSize 0x%x fw_map_dyn->descTableOffset 0x%x\n",
			le32_to_cpu(fw_map_dyn->raid_map_size),
			le32_to_cpu(fw_map_dyn->desc_table_offset));
		dev_dbg(&instance->pdev->dev, "descTableSize 0x%x descTableNumElements 0x%x\n",
			le32_to_cpu(fw_map_dyn->desc_table_size),
			le32_to_cpu(fw_map_dyn->desc_table_num_elements));
		dev_dbg(&instance->pdev->dev, "drv map %p ldCount %d\n",
			drv_map, fw_map_dyn->ld_count);
#endif
		desc_table =
		(struct MR_RAID_MAP_DESC_TABLE *)((void *)fw_map_dyn + le32_to_cpu(fw_map_dyn->desc_table_offset));
		if (desc_table != fw_map_dyn->raid_map_desc_table)
			dev_dbg(&instance->pdev->dev, "offsets of desc table are not matching desc %p original %p\n",
				desc_table, fw_map_dyn->raid_map_desc_table);

		ld_count = (u16)le16_to_cpu(fw_map_dyn->ld_count);
		pDrvRaidMap->ldCount = (__le16)cpu_to_le16(ld_count);
		pDrvRaidMap->fpPdIoTimeoutSec =
			fw_map_dyn->fp_pd_io_timeout_sec;
		pDrvRaidMap->totalSize = sizeof(struct MR_DRV_RAID_MAP_ALL);
		/* point to actual data starting point*/
		raid_map_data = (void *)fw_map_dyn +
			le32_to_cpu(fw_map_dyn->desc_table_offset) +
			le32_to_cpu(fw_map_dyn->desc_table_size);

		for (i = 0; i < le32_to_cpu(fw_map_dyn->desc_table_num_elements); ++i) {

#if VD_EXT_DEBUG
			dev_dbg(&instance->pdev->dev, "desc table %p\n",
				desc_table);
			dev_dbg(&instance->pdev->dev, "raidmap type %d, raidmapOffset 0x%x\n",
				desc_table->raid_map_desc_type,
				desc_table->raid_map_desc_offset);
			dev_dbg(&instance->pdev->dev, "raid map number of elements 0%x, raidmapsize 0x%x\n",
				desc_table->raid_map_desc_elements,
				desc_table->raid_map_desc_buffer_size);
#endif
			switch (le32_to_cpu(desc_table->raid_map_desc_type)) {
			case RAID_MAP_DESC_TYPE_DEVHDL_INFO:
				fw_map_dyn->dev_hndl_info =
				(struct MR_DEV_HANDLE_INFO *)(raid_map_data + le32_to_cpu(desc_table->raid_map_desc_offset));
#if VD_EXT_DEBUG
				dev_dbg(&instance->pdev->dev, "devHndlInfo  address %p\n",
					fw_map_dyn->dev_hndl_info);
#endif
				memcpy(pDrvRaidMap->devHndlInfo,
				fw_map_dyn->dev_hndl_info,
				sizeof(struct MR_DEV_HANDLE_INFO) *
				le32_to_cpu(desc_table->raid_map_desc_elements));
			break;
			case RAID_MAP_DESC_TYPE_TGTID_INFO:
				fw_map_dyn->ld_tgt_id_to_ld =
				(u16 *) (raid_map_data +
				le32_to_cpu(desc_table->raid_map_desc_offset));
#if VD_EXT_DEBUG
			dev_dbg(&instance->pdev->dev, "ldTgtIdToLd  address %p\n",
				fw_map_dyn->ld_tgt_id_to_ld);
#endif
			for (j = 0; j < le32_to_cpu(desc_table->raid_map_desc_elements); j++) {
				pDrvRaidMap->ldTgtIdToLd[j] =
				fw_map_dyn->ld_tgt_id_to_ld[j];
#if VD_EXT_DEBUG
				dev_dbg(&instance->pdev->dev, " %d drv ldTgtIdToLd %d\n",
					j, pDrvRaidMap->ldTgtIdToLd[j]);
#endif
			}
			break;
			case RAID_MAP_DESC_TYPE_ARRAY_INFO:
				fw_map_dyn->ar_map_info =
				(struct MR_ARRAY_INFO *)
				(raid_map_data + le32_to_cpu(desc_table->raid_map_desc_offset));
#if VD_EXT_DEBUG
				dev_dbg(&instance->pdev->dev, "arMapInfo  address %p\n",
					fw_map_dyn->ar_map_info);
#endif

				memcpy(pDrvRaidMap->arMapInfo,
				fw_map_dyn->ar_map_info,
				sizeof(struct MR_ARRAY_INFO) * le32_to_cpu(desc_table->raid_map_desc_elements));
			break;
			case RAID_MAP_DESC_TYPE_SPAN_INFO:
				fw_map_dyn->ld_span_map =
				(struct MR_LD_SPAN_MAP *)
				(raid_map_data + le32_to_cpu(desc_table->raid_map_desc_offset));
				memcpy(pDrvRaidMap->ldSpanMap,
				fw_map_dyn->ld_span_map,
				sizeof(struct MR_LD_SPAN_MAP) * le32_to_cpu(desc_table->raid_map_desc_elements));
#if VD_EXT_DEBUG
				dev_dbg(&instance->pdev->dev, "ldSpanMap  address %p\n",
					fw_map_dyn->ld_span_map);
				dev_dbg(&instance->pdev->dev, "MR_LD_SPAN_MAP size 0x%lx\n",
					sizeof(struct MR_LD_SPAN_MAP));
				for (j = 0; j < ld_count; j++) {
					dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) : fw_map_dyn->ldSpanMap[%d].ldRaid.targetId 0x%x\n",
					j, j, fw_map_dyn->ld_span_map[j].ldRaid.targetId);
					dev_dbg(&instance->pdev->dev, "fw_map_dyn->ldSpanMap[%d].ldRaid.seqNum 0x%x\n",
					j, fw_map_dyn->ld_span_map[j].ldRaid.seqNum);
					dev_dbg(&instance->pdev->dev, "fw_map_dyn->ld_span_map[%d].ldRaid.rowSize 0x%x\n",
					j, (u32)fw_map_dyn->ld_span_map[j].ldRaid.rowSize);

					dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) :pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x\n",
					j, j, pDrvRaidMap->ldSpanMap[j].ldRaid.targetId);
					dev_dbg(&instance->pdev->dev, "DrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x\n",
					j, pDrvRaidMap->ldSpanMap[j].ldRaid.seqNum);
					dev_dbg(&instance->pdev->dev, "pDrvRaidMap->ldSpanMap[%d].ldRaid.rowSize 0x%x\n",
					j, (u32)pDrvRaidMap->ldSpanMap[j].ldRaid.rowSize);

					dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) : drv raid map all %p\n",
					instance->unique_id, drv_map);
					dev_dbg(&instance->pdev->dev, "raid map %p LD RAID MAP %p/%p\n",
					pDrvRaidMap,
					&fw_map_dyn->ld_span_map[j].ldRaid,
					&pDrvRaidMap->ldSpanMap[j].ldRaid);
				}
#endif
			break;
			default:
				dev_dbg(&instance->pdev->dev, "wrong number of desctableElements %d\n",
					fw_map_dyn->desc_table_num_elements);
			}
			++desc_table;
		}

	} else if (instance->supportmax256vd) {
		fw_map_ext =
		(struct MR_FW_RAID_MAP_EXT *) fusion->ld_map[(instance->map_id & 1)];
		ld_count = (u16)le16_to_cpu(fw_map_ext->ldCount);
		if (ld_count > MAX_LOGICAL_DRIVES_EXT) {
			dev_dbg(&instance->pdev->dev, "megaraid_sas: LD count exposed in RAID map in not valid\n");
			return;
		}
#if VD_EXT_DEBUG
		for (i = 0; i < ld_count; i++) {
			dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) :Index 0x%x\n",
				instance->unique_id, i);
			dev_dbg(&instance->pdev->dev, "Target Id 0x%x\n",
				fw_map_ext->ldSpanMap[i].ldRaid.targetId);
			dev_dbg(&instance->pdev->dev, "Seq Num 0x%x Size 0/%llx\n",
				fw_map_ext->ldSpanMap[i].ldRaid.seqNum,
				fw_map_ext->ldSpanMap[i].ldRaid.size);
		}
#endif

		pDrvRaidMap->ldCount = (__le16)cpu_to_le16(ld_count);
		pDrvRaidMap->fpPdIoTimeoutSec = fw_map_ext->fpPdIoTimeoutSec;
		for (i = 0; i < (MAX_LOGICAL_DRIVES_EXT); i++)
			pDrvRaidMap->ldTgtIdToLd[i] =
				(u16)fw_map_ext->ldTgtIdToLd[i];
		memcpy(pDrvRaidMap->ldSpanMap, fw_map_ext->ldSpanMap,
				sizeof(struct MR_LD_SPAN_MAP) * ld_count);
#if VD_EXT_DEBUG
		for (i = 0; i < ld_count; i++) {
			dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) : fw_map_ext->ldSpanMap[%d].ldRaid.targetId 0x%x\n",
			i, i, fw_map_ext->ldSpanMap[i].ldRaid.targetId);
			dev_dbg(&instance->pdev->dev, "fw_map_ext->ldSpanMap[%d].ldRaid.seqNum 0x%x\n",
			i, fw_map_ext->ldSpanMap[i].ldRaid.seqNum);
			dev_dbg(&instance->pdev->dev, "fw_map_ext->ldSpanMap[%d].ldRaid.rowSize 0x%x\n",
			i, (u32)fw_map_ext->ldSpanMap[i].ldRaid.rowSize);

			dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) : pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x\n",
			i, i, pDrvRaidMap->ldSpanMap[i].ldRaid.targetId);
			dev_dbg(&instance->pdev->dev, "pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x\n",
			i, pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum);
			dev_dbg(&instance->pdev->dev, "pDrvRaidMap->ldSpanMap[%d].ldRaid.rowSize 0x%x\n",
			i, (u32)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);

			dev_dbg(&instance->pdev->dev, "megaraid_sas(%d) : drv raid map all %p\n",
			instance->unique_id, drv_map);
			dev_dbg(&instance->pdev->dev, "raid map %p LD RAID MAP %p %p\n",
			pDrvRaidMap, &fw_map_ext->ldSpanMap[i].ldRaid,
			&pDrvRaidMap->ldSpanMap[i].ldRaid);
		}
#endif
		memcpy(pDrvRaidMap->arMapInfo, fw_map_ext->arMapInfo,
			sizeof(struct MR_ARRAY_INFO) * MAX_API_ARRAYS_EXT);
		memcpy(pDrvRaidMap->devHndlInfo, fw_map_ext->devHndlInfo,
			sizeof(struct MR_DEV_HANDLE_INFO) *
					MAX_RAIDMAP_PHYSICAL_DEVICES);
384 385 386 387

		/* New Raid map will not set totalSize, so keep expected value
		 * for legacy code in ValidateMapInfo
		 */
388 389
		pDrvRaidMap->totalSize =
			cpu_to_le32(sizeof(struct MR_FW_RAID_MAP_EXT));
390 391 392 393
	} else {
		fw_map_old = (struct MR_FW_RAID_MAP_ALL *)
			fusion->ld_map[(instance->map_id & 1)];
		pFwRaidMap = &fw_map_old->raidMap;
394
		ld_count = (u16)le32_to_cpu(pFwRaidMap->ldCount);
395 396

#if VD_EXT_DEBUG
397
		for (i = 0; i < ld_count; i++) {
398 399 400 401 402 403 404 405 406 407
			dev_dbg(&instance->pdev->dev, "(%d) :Index 0x%x "
				"Target Id 0x%x Seq Num 0x%x Size 0/%llx\n",
				instance->unique_id, i,
				fw_map_old->raidMap.ldSpanMap[i].ldRaid.targetId,
				fw_map_old->raidMap.ldSpanMap[i].ldRaid.seqNum,
				fw_map_old->raidMap.ldSpanMap[i].ldRaid.size);
		}
#endif

		pDrvRaidMap->totalSize = pFwRaidMap->totalSize;
408
		pDrvRaidMap->ldCount = (__le16)cpu_to_le16(ld_count);
409 410 411 412
		pDrvRaidMap->fpPdIoTimeoutSec = pFwRaidMap->fpPdIoTimeoutSec;
		for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++)
			pDrvRaidMap->ldTgtIdToLd[i] =
				(u8)pFwRaidMap->ldTgtIdToLd[i];
413
		for (i = 0; i < ld_count; i++) {
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
			pDrvRaidMap->ldSpanMap[i] = pFwRaidMap->ldSpanMap[i];
#if VD_EXT_DEBUG
			dev_dbg(&instance->pdev->dev,
				"pFwRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
				"pFwRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
				"size 0x%x\n", i, i,
				pFwRaidMap->ldSpanMap[i].ldRaid.targetId,
				pFwRaidMap->ldSpanMap[i].ldRaid.seqNum,
				(u32)pFwRaidMap->ldSpanMap[i].ldRaid.rowSize);
			dev_dbg(&instance->pdev->dev,
				"pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
				"pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
				"size 0x%x\n", i, i,
				pDrvRaidMap->ldSpanMap[i].ldRaid.targetId,
				pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum,
				(u32)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);
			dev_dbg(&instance->pdev->dev, "Driver raid map all %p "
				"raid map %p LD RAID MAP %p/%p\n", drv_map,
				pDrvRaidMap, &pFwRaidMap->ldSpanMap[i].ldRaid,
				&pDrvRaidMap->ldSpanMap[i].ldRaid);
#endif
		}
		memcpy(pDrvRaidMap->arMapInfo, pFwRaidMap->arMapInfo,
			sizeof(struct MR_ARRAY_INFO) * MAX_RAIDMAP_ARRAYS);
		memcpy(pDrvRaidMap->devHndlInfo, pFwRaidMap->devHndlInfo,
			sizeof(struct MR_DEV_HANDLE_INFO) *
			MAX_RAIDMAP_PHYSICAL_DEVICES);
	}
}

444 445 446
/*
 * This function will validate Map info data provided by FW
 */
447
u8 MR_ValidateMapInfo(struct megasas_instance *instance)
448
{
449 450 451 452 453
	struct fusion_context *fusion;
	struct MR_DRV_RAID_MAP_ALL *drv_map;
	struct MR_DRV_RAID_MAP *pDrvRaidMap;
	struct LD_LOAD_BALANCE_INFO *lbInfo;
	PLD_SPAN_INFO ldSpanInfo;
454
	struct MR_LD_RAID         *raid;
455
	u16 ldCount, num_lds;
456
	u16 ld;
457
	u32 expected_size;
458

459

460 461 462 463 464 465 466 467 468
	MR_PopulateDrvRaidMap(instance);

	fusion = instance->ctrl_context;
	drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
	pDrvRaidMap = &drv_map->raidMap;

	lbInfo = fusion->load_balance_info;
	ldSpanInfo = fusion->log_to_span;

469 470 471
	if (instance->max_raid_mapsize)
		expected_size = sizeof(struct MR_DRV_RAID_MAP_ALL);
	else if (instance->supportmax256vd)
472 473 474 475
		expected_size = sizeof(struct MR_FW_RAID_MAP_EXT);
	else
		expected_size =
			(sizeof(struct MR_FW_RAID_MAP) - sizeof(struct MR_LD_SPAN_MAP) +
476
			(sizeof(struct MR_LD_SPAN_MAP) * le16_to_cpu(pDrvRaidMap->ldCount)));
477 478

	if (le32_to_cpu(pDrvRaidMap->totalSize) != expected_size) {
479 480 481 482
		dev_dbg(&instance->pdev->dev, "megasas: map info structure size 0x%x",
			le32_to_cpu(pDrvRaidMap->totalSize));
		dev_dbg(&instance->pdev->dev, "is not matching expected size 0x%x\n",
			(unsigned int) expected_size);
483 484 485
		dev_err(&instance->pdev->dev, "megasas: span map %x, pDrvRaidMap->totalSize : %x\n",
			(unsigned int)sizeof(struct MR_LD_SPAN_MAP),
			le32_to_cpu(pDrvRaidMap->totalSize));
486 487 488
		return 0;
	}

489
	if (instance->UnevenSpanSupport)
490
		mr_update_span_set(drv_map, ldSpanInfo);
491

492
	mr_update_load_balance_params(drv_map, lbInfo);
493

494
	num_lds = le16_to_cpu(drv_map->raidMap.ldCount);
495 496 497

	/*Convert Raid capability values to CPU arch */
	for (ldCount = 0; ldCount < num_lds; ldCount++) {
498 499
		ld = MR_TargetIdToLdGet(ldCount, drv_map);
		raid = MR_LdRaidGet(ld, drv_map);
500 501 502
		le32_to_cpus((u32 *)&raid->capability);
	}

503 504 505 506
	return 1;
}

u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
507
		    struct MR_DRV_RAID_MAP_ALL *map)
508 509 510 511 512 513 514 515
{
	struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
	struct MR_QUAD_ELEMENT    *quad;
	struct MR_LD_RAID         *raid = MR_LdRaidGet(ld, map);
	u32                span, j;

	for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {

516
		for (j = 0; j < le32_to_cpu(pSpanBlock->block_span_info.noElements); j++) {
517 518
			quad = &pSpanBlock->block_span_info.quad[j];

519
			if (le32_to_cpu(quad->diff) == 0)
520
				return SPAN_INVALID;
521 522 523
			if (le64_to_cpu(quad->logStart) <= row && row <=
				le64_to_cpu(quad->logEnd) && (mega_mod64(row - le64_to_cpu(quad->logStart),
				le32_to_cpu(quad->diff))) == 0) {
524 525
				if (span_blk != NULL) {
					u64  blk, debugBlk;
526
					blk =  mega_div64_32((row-le64_to_cpu(quad->logStart)), le32_to_cpu(quad->diff));
527 528
					debugBlk = blk;

529
					blk = (blk + le64_to_cpu(quad->offsetInSpan)) << raid->stripeShift;
530 531 532 533 534 535
					*span_blk = blk;
				}
				return span;
			}
		}
	}
536 537 538 539 540 541 542 543 544 545 546 547 548
	return SPAN_INVALID;
}

/*
******************************************************************************
*
* Function to print info about span set created in driver from FW raid map
*
* Inputs :
* map    - LD map
* ldSpanInfo - ldSpanInfo per HBA instance
*/
#if SPAN_DEBUG
549 550
static int getSpanInfo(struct MR_DRV_RAID_MAP_ALL *map,
	PLD_SPAN_INFO ldSpanInfo)
551 552 553 554 555 556 557 558 559 560
{

	u8   span;
	u32    element;
	struct MR_LD_RAID *raid;
	LD_SPAN_SET *span_set;
	struct MR_QUAD_ELEMENT    *quad;
	int ldCount;
	u16 ld;

561
	for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
562
		ld = MR_TargetIdToLdGet(ldCount, map);
563
			if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
564 565 566 567 568 569 570
				continue;
		raid = MR_LdRaidGet(ld, map);
		dev_dbg(&instance->pdev->dev, "LD %x: span_depth=%x\n",
			ld, raid->spanDepth);
		for (span = 0; span < raid->spanDepth; span++)
			dev_dbg(&instance->pdev->dev, "Span=%x,"
			" number of quads=%x\n", span,
571 572
			le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
			block_span_info.noElements));
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
		for (element = 0; element < MAX_QUAD_DEPTH; element++) {
			span_set = &(ldSpanInfo[ld].span_set[element]);
			if (span_set->span_row_data_width == 0)
				break;

			dev_dbg(&instance->pdev->dev, "Span Set %x:"
				"width=%x, diff=%x\n", element,
				(unsigned int)span_set->span_row_data_width,
				(unsigned int)span_set->diff);
			dev_dbg(&instance->pdev->dev, "logical LBA"
				"start=0x%08lx, end=0x%08lx\n",
				(long unsigned int)span_set->log_start_lba,
				(long unsigned int)span_set->log_end_lba);
			dev_dbg(&instance->pdev->dev, "span row start=0x%08lx,"
				" end=0x%08lx\n",
				(long unsigned int)span_set->span_row_start,
				(long unsigned int)span_set->span_row_end);
			dev_dbg(&instance->pdev->dev, "data row start=0x%08lx,"
				" end=0x%08lx\n",
				(long unsigned int)span_set->data_row_start,
				(long unsigned int)span_set->data_row_end);
			dev_dbg(&instance->pdev->dev, "data strip start=0x%08lx,"
				" end=0x%08lx\n",
				(long unsigned int)span_set->data_strip_start,
				(long unsigned int)span_set->data_strip_end);

			for (span = 0; span < raid->spanDepth; span++) {
600 601
				if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
					block_span_info.noElements) >=
602 603 604 605 606 607
					element + 1) {
					quad = &map->raidMap.ldSpanMap[ld].
						spanBlock[span].block_span_info.
						quad[element];
				dev_dbg(&instance->pdev->dev, "Span=%x,"
					"Quad=%x, diff=%x\n", span,
608
					element, le32_to_cpu(quad->diff));
609 610
				dev_dbg(&instance->pdev->dev,
					"offset_in_span=0x%08lx\n",
611
					(long unsigned int)le64_to_cpu(quad->offsetInSpan));
612 613
				dev_dbg(&instance->pdev->dev,
					"logical start=0x%08lx, end=0x%08lx\n",
614 615
					(long unsigned int)le64_to_cpu(quad->logStart),
					(long unsigned int)le64_to_cpu(quad->logEnd));
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
				}
			}
		}
	}
	return 0;
}
#endif

/*
******************************************************************************
*
* This routine calculates the Span block for given row using spanset.
*
* Inputs :
*    instance - HBA instance
*    ld   - Logical drive number
*    row        - Row number
*    map    - LD map
*
* Outputs :
*
*    span          - Span number
*    block         - Absolute Block number in the physical disk
*    div_error	   - Devide error code.
*/

u32 mr_spanset_get_span_block(struct megasas_instance *instance,
643
		u32 ld, u64 row, u64 *span_blk, struct MR_DRV_RAID_MAP_ALL *map)
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
{
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_LD_RAID         *raid = MR_LdRaidGet(ld, map);
	LD_SPAN_SET *span_set;
	struct MR_QUAD_ELEMENT    *quad;
	u32    span, info;
	PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;

	for (info = 0; info < MAX_QUAD_DEPTH; info++) {
		span_set = &(ldSpanInfo[ld].span_set[info]);

		if (span_set->span_row_data_width == 0)
			break;

		if (row > span_set->data_row_end)
			continue;

		for (span = 0; span < raid->spanDepth; span++)
662 663
			if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
				block_span_info.noElements) >= info+1) {
664 665 666
				quad = &map->raidMap.ldSpanMap[ld].
					spanBlock[span].
					block_span_info.quad[info];
667
				if (le32_to_cpu(quad->diff) == 0)
668
					return SPAN_INVALID;
669 670 671 672
				if (le64_to_cpu(quad->logStart) <= row  &&
					row <= le64_to_cpu(quad->logEnd)  &&
					(mega_mod64(row - le64_to_cpu(quad->logStart),
						le32_to_cpu(quad->diff))) == 0) {
673 674 675
					if (span_blk != NULL) {
						u64  blk;
						blk = mega_div64_32
676 677 678
						    ((row - le64_to_cpu(quad->logStart)),
						    le32_to_cpu(quad->diff));
						blk = (blk + le64_to_cpu(quad->offsetInSpan))
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
							 << raid->stripeShift;
						*span_blk = blk;
					}
					return span;
				}
			}
	}
	return SPAN_INVALID;
}

/*
******************************************************************************
*
* This routine calculates the row for given strip using spanset.
*
* Inputs :
*    instance - HBA instance
*    ld   - Logical drive number
*    Strip        - Strip
*    map    - LD map
*
* Outputs :
*
*    row         - row associated with strip
*/

static u64  get_row_from_strip(struct megasas_instance *instance,
706
	u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
{
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_LD_RAID	*raid = MR_LdRaidGet(ld, map);
	LD_SPAN_SET	*span_set;
	PLD_SPAN_INFO	ldSpanInfo = fusion->log_to_span;
	u32		info, strip_offset, span, span_offset;
	u64		span_set_Strip, span_set_Row, retval;

	for (info = 0; info < MAX_QUAD_DEPTH; info++) {
		span_set = &(ldSpanInfo[ld].span_set[info]);

		if (span_set->span_row_data_width == 0)
			break;
		if (strip > span_set->data_strip_end)
			continue;

		span_set_Strip = strip - span_set->data_strip_start;
		strip_offset = mega_mod64(span_set_Strip,
				span_set->span_row_data_width);
		span_set_Row = mega_div64_32(span_set_Strip,
				span_set->span_row_data_width) * span_set->diff;
		for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
729
			if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
730
				block_span_info.noElements) >= info+1) {
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
				if (strip_offset >=
					span_set->strip_offset[span])
					span_offset++;
				else
					break;
			}
#if SPAN_DEBUG
		dev_info(&instance->pdev->dev, "Strip 0x%llx,"
			"span_set_Strip 0x%llx, span_set_Row 0x%llx"
			"data width 0x%llx span offset 0x%x\n", strip,
			(unsigned long long)span_set_Strip,
			(unsigned long long)span_set_Row,
			(unsigned long long)span_set->span_row_data_width,
			span_offset);
		dev_info(&instance->pdev->dev, "For strip 0x%llx"
			"row is 0x%llx\n", strip,
			(unsigned long long) span_set->data_row_start +
			(unsigned long long) span_set_Row + (span_offset - 1));
#endif
		retval = (span_set->data_row_start + span_set_Row +
				(span_offset - 1));
		return retval;
	}
	return -1LLU;
}


/*
******************************************************************************
*
* This routine calculates the Start Strip for given row using spanset.
*
* Inputs :
*    instance - HBA instance
*    ld   - Logical drive number
*    row        - Row number
*    map    - LD map
*
* Outputs :
*
*    Strip         - Start strip associated with row
*/

static u64 get_strip_from_row(struct megasas_instance *instance,
775
		u32 ld, u64 row, struct MR_DRV_RAID_MAP_ALL *map)
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
{
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_LD_RAID         *raid = MR_LdRaidGet(ld, map);
	LD_SPAN_SET *span_set;
	struct MR_QUAD_ELEMENT    *quad;
	PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
	u32    span, info;
	u64  strip;

	for (info = 0; info < MAX_QUAD_DEPTH; info++) {
		span_set = &(ldSpanInfo[ld].span_set[info]);

		if (span_set->span_row_data_width == 0)
			break;
		if (row > span_set->data_row_end)
			continue;

		for (span = 0; span < raid->spanDepth; span++)
794 795
			if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
				block_span_info.noElements) >= info+1) {
796 797
				quad = &map->raidMap.ldSpanMap[ld].
					spanBlock[span].block_span_info.quad[info];
798 799 800 801
				if (le64_to_cpu(quad->logStart) <= row  &&
					row <= le64_to_cpu(quad->logEnd)  &&
					mega_mod64((row - le64_to_cpu(quad->logStart)),
					le32_to_cpu(quad->diff)) == 0) {
802 803
					strip = mega_div64_32
						(((row - span_set->data_row_start)
804 805
							- le64_to_cpu(quad->logStart)),
							le32_to_cpu(quad->diff));
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
					strip *= span_set->span_row_data_width;
					strip += span_set->data_strip_start;
					strip += span_set->strip_offset[span];
					return strip;
				}
			}
	}
	dev_err(&instance->pdev->dev, "get_strip_from_row"
		"returns invalid strip for ld=%x, row=%lx\n",
		ld, (long unsigned int)row);
	return -1;
}

/*
******************************************************************************
*
* This routine calculates the Physical Arm for given strip using spanset.
*
* Inputs :
*    instance - HBA instance
*    ld   - Logical drive number
*    strip      - Strip
*    map    - LD map
*
* Outputs :
*
*    Phys Arm         - Phys Arm associated with strip
*/

static u32 get_arm_from_strip(struct megasas_instance *instance,
836
	u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
{
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_LD_RAID         *raid = MR_LdRaidGet(ld, map);
	LD_SPAN_SET *span_set;
	PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
	u32    info, strip_offset, span, span_offset, retval;

	for (info = 0 ; info < MAX_QUAD_DEPTH; info++) {
		span_set = &(ldSpanInfo[ld].span_set[info]);

		if (span_set->span_row_data_width == 0)
			break;
		if (strip > span_set->data_strip_end)
			continue;

		strip_offset = (uint)mega_mod64
				((strip - span_set->data_strip_start),
				span_set->span_row_data_width);

		for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
857 858
			if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
				block_span_info.noElements) >= info+1) {
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
				if (strip_offset >=
					span_set->strip_offset[span])
					span_offset =
						span_set->strip_offset[span];
				else
					break;
			}
#if SPAN_DEBUG
		dev_info(&instance->pdev->dev, "get_arm_from_strip:"
			"for ld=0x%x strip=0x%lx arm is  0x%x\n", ld,
			(long unsigned int)strip, (strip_offset - span_offset));
#endif
		retval = (strip_offset - span_offset);
		return retval;
	}

	dev_err(&instance->pdev->dev, "get_arm_from_strip"
		"returns invalid arm for ld=%x strip=%lx\n",
		ld, (long unsigned int)strip);

	return -1;
}

/* This Function will return Phys arm */
u8 get_arm(struct megasas_instance *instance, u32 ld, u8 span, u64 stripe,
884
		struct MR_DRV_RAID_MAP_ALL *map)
885 886 887 888 889 890 891 892 893 894 895 896 897 898
{
	struct MR_LD_RAID  *raid = MR_LdRaidGet(ld, map);
	/* Need to check correct default value */
	u32    arm = 0;

	switch (raid->level) {
	case 0:
	case 5:
	case 6:
		arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
		break;
	case 1:
		/* start with logical arm */
		arm = get_arm_from_strip(instance, ld, stripe, map);
899
		if (arm != -1U)
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
			arm *= 2;
		break;
	}

	return arm;
}


/*
******************************************************************************
*
* This routine calculates the arm, span and block for the specified stripe and
* reference in stripe using spanset
*
* Inputs :
*
*    ld   - Logical drive number
*    stripRow        - Stripe number
*    stripRef    - Reference in stripe
*
* Outputs :
*
*    span          - Span number
*    block         - Absolute Block number in the physical disk
*/
static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
		u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
		struct RAID_CONTEXT *pRAID_Context,
928
		struct MR_DRV_RAID_MAP_ALL *map)
929 930
{
	struct MR_LD_RAID  *raid = MR_LdRaidGet(ld, map);
931
	u32     pd, arRef, r1_alt_pd;
932 933 934 935
	u8      physArm, span;
	u64     row;
	u8	retval = TRUE;
	u64	*pdBlock = &io_info->pdBlock;
936
	__le16	*pDevHandle = &io_info->devHandle;
937
	u8	*pPdInterface = &io_info->pd_interface;
938
	u32	logArm, rowMod, armQ, arm;
939
	struct fusion_context *fusion;
940

941
	fusion = instance->ctrl_context;
942 943 944 945 946 947 948 949

	/*Get row and span from io_info for Uneven Span IO.*/
	row	    = io_info->start_row;
	span	    = io_info->start_span;


	if (raid->level == 6) {
		logArm = get_arm_from_strip(instance, ld, stripRow, map);
950
		if (logArm == -1U)
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
			return FALSE;
		rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
		armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
		arm = armQ + 1 + logArm;
		if (arm >= SPAN_ROW_SIZE(map, ld, span))
			arm -= SPAN_ROW_SIZE(map, ld, span);
		physArm = (u8)arm;
	} else
		/* Calculate the arm */
		physArm = get_arm(instance, ld, span, stripRow, map);
	if (physArm == 0xFF)
		return FALSE;

	arRef       = MR_LdSpanArrayGet(ld, span, map);
	pd          = MR_ArPdGet(arRef, physArm, map);

967
	if (pd != MR_PD_INVALID) {
968
		*pDevHandle = MR_PdDevHandleGet(pd, map);
969
		*pPdInterface = MR_PdInterfaceTypeGet(pd, map);
970
		/* get second pd also for raid 1/10 fast path writes*/
971 972 973
		if (instance->is_ventura &&
		    (raid->level == 1) &&
		    !io_info->isRead) {
974 975 976 977 978 979
			r1_alt_pd = MR_ArPdGet(arRef, physArm + 1, map);
			if (r1_alt_pd != MR_PD_INVALID)
				io_info->r1_alt_dev_handle =
				MR_PdDevHandleGet(r1_alt_pd, map);
		}
	} else {
980
		*pDevHandle = cpu_to_le16(MR_DEVHANDLE_INVALID);
981
		if ((raid->level >= 5) &&
982 983
			((fusion->adapter_type == THUNDERBOLT_SERIES)  ||
			((fusion->adapter_type == INVADER_SERIES) &&
984
			(raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
985
			pRAID_Context->reg_lock_flags = REGION_TYPE_EXCLUSIVE;
986
		else if (raid->level == 1) {
987 988
			physArm = physArm + 1;
			pd = MR_ArPdGet(arRef, physArm, map);
989
			if (pd != MR_PD_INVALID) {
990
				*pDevHandle = MR_PdDevHandleGet(pd, map);
991 992
				*pPdInterface = MR_PdInterfaceTypeGet(pd, map);
			}
993 994 995
		}
	}

996
	*pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
997 998 999 1000 1001 1002 1003 1004 1005 1006
	if (instance->is_ventura) {
		((struct RAID_CONTEXT_G35 *) pRAID_Context)->span_arm =
			(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
		io_info->span_arm =
			(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
	} else {
		pRAID_Context->span_arm =
			(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
		io_info->span_arm = pRAID_Context->span_arm;
	}
1007
	return retval;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
}

/*
******************************************************************************
*
* This routine calculates the arm, span and block for the specified stripe and
* reference in stripe.
*
* Inputs :
*
*    ld   - Logical drive number
*    stripRow        - Stripe number
*    stripRef    - Reference in stripe
*
* Outputs :
*
*    span          - Span number
*    block         - Absolute Block number in the physical disk
*/
1027
u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
1028 1029
		u16 stripRef, struct IO_REQUEST_INFO *io_info,
		struct RAID_CONTEXT *pRAID_Context,
1030
		struct MR_DRV_RAID_MAP_ALL *map)
1031 1032
{
	struct MR_LD_RAID  *raid = MR_LdRaidGet(ld, map);
1033
	u32         pd, arRef, r1_alt_pd;
1034 1035 1036
	u8          physArm, span;
	u64         row;
	u8	    retval = TRUE;
1037
	u64	    *pdBlock = &io_info->pdBlock;
1038
	__le16	    *pDevHandle = &io_info->devHandle;
1039
	u8	    *pPdInterface = &io_info->pd_interface;
1040 1041 1042
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;
1043

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

	row =  mega_div64_32(stripRow, raid->rowDataSize);

	if (raid->level == 6) {
		/* logical arm within row */
		u32 logArm =  mega_mod64(stripRow, raid->rowDataSize);
		u32 rowMod, armQ, arm;

		if (raid->rowSize == 0)
			return FALSE;
		/* get logical row mod */
		rowMod = mega_mod64(row, raid->rowSize);
		armQ = raid->rowSize-1-rowMod; /* index of Q drive */
		arm = armQ+1+logArm; /* data always logically follows Q */
		if (arm >= raid->rowSize) /* handle wrap condition */
			arm -= raid->rowSize;
		physArm = (u8)arm;
	} else  {
		if (raid->modFactor == 0)
			return FALSE;
		physArm = MR_LdDataArmGet(ld,  mega_mod64(stripRow,
							  raid->modFactor),
					  map);
	}

	if (raid->spanDepth == 1) {
		span = 0;
		*pdBlock = row << raid->stripeShift;
	} else {
1073 1074
		span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map);
		if (span == SPAN_INVALID)
1075 1076 1077 1078 1079 1080 1081
			return FALSE;
	}

	/* Get the array on which this span is present */
	arRef       = MR_LdSpanArrayGet(ld, span, map);
	pd          = MR_ArPdGet(arRef, physArm, map); /* Get the pd */

1082
	if (pd != MR_PD_INVALID) {
1083 1084
		/* Get dev handle from Pd. */
		*pDevHandle = MR_PdDevHandleGet(pd, map);
1085
		*pPdInterface = MR_PdInterfaceTypeGet(pd, map);
1086
		/* get second pd also for raid 1/10 fast path writes*/
1087 1088 1089
		if (instance->is_ventura &&
		    (raid->level == 1) &&
		    !io_info->isRead) {
1090 1091 1092
			r1_alt_pd = MR_ArPdGet(arRef, physArm + 1, map);
			if (r1_alt_pd != MR_PD_INVALID)
				io_info->r1_alt_dev_handle =
1093
					MR_PdDevHandleGet(r1_alt_pd, map);
1094 1095
		}
	} else {
1096
		/* set dev handle as invalid. */
1097
		*pDevHandle = cpu_to_le16(MR_DEVHANDLE_INVALID);
1098
		if ((raid->level >= 5) &&
1099 1100
			((fusion->adapter_type == THUNDERBOLT_SERIES)  ||
			((fusion->adapter_type == INVADER_SERIES) &&
1101
			(raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
1102
			pRAID_Context->reg_lock_flags = REGION_TYPE_EXCLUSIVE;
1103 1104
		else if (raid->level == 1) {
			/* Get alternate Pd. */
1105 1106
			physArm = physArm + 1;
			pd = MR_ArPdGet(arRef, physArm, map);
1107
			if (pd != MR_PD_INVALID) {
1108 1109
				/* Get dev handle from Pd */
				*pDevHandle = MR_PdDevHandleGet(pd, map);
1110 1111
				*pPdInterface = MR_PdInterfaceTypeGet(pd, map);
			}
1112 1113 1114
		}
	}

1115
	*pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	if (instance->is_ventura) {
		((struct RAID_CONTEXT_G35 *) pRAID_Context)->span_arm =
				(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
		io_info->span_arm =
				(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
	} else {
		pRAID_Context->span_arm =
			(span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
		io_info->span_arm = pRAID_Context->span_arm;
	}
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	return retval;
}

/*
******************************************************************************
*
* MR_BuildRaidContext function
*
* This function will initiate command processing.  The start/end row and strip
* information is calculated then the lock is acquired.
* This function will return 0 if region lock was acquired OR return num strips
*/
u8
1139 1140
MR_BuildRaidContext(struct megasas_instance *instance,
		    struct IO_REQUEST_INFO *io_info,
1141
		    struct RAID_CONTEXT *pRAID_Context,
1142
		    struct MR_DRV_RAID_MAP_ALL *map, u8 **raidLUN)
1143
{
1144
	struct fusion_context *fusion;
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	struct MR_LD_RAID  *raid;
	u32         ld, stripSize, stripe_mask;
	u64         endLba, endStrip, endRow, start_row, start_strip;
	u64         regStart;
	u32         regSize;
	u8          num_strips, numRows;
	u16         ref_in_start_stripe, ref_in_end_stripe;
	u64         ldStartBlock;
	u32         numBlocks, ldTgtId;
	u8          isRead;
	u8	    retval = 0;
1156 1157
	u8	    startlba_span = SPAN_INVALID;
	u64 *pdBlock = &io_info->pdBlock;
1158 1159 1160 1161 1162

	ldStartBlock = io_info->ldStartBlock;
	numBlocks = io_info->numBlocks;
	ldTgtId = io_info->ldTgtId;
	isRead = io_info->isRead;
1163 1164
	io_info->IoforUnevenSpan = 0;
	io_info->start_span	= SPAN_INVALID;
1165
	fusion = instance->ctrl_context;
1166 1167 1168

	ld = MR_TargetIdToLdGet(ldTgtId, map);
	raid = MR_LdRaidGet(ld, map);
1169 1170
	/*check read ahead bit*/
	io_info->ra_capable = raid->capability.ra_capable;
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	/*
	 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
	 * return FALSE
	 */
	if (raid->rowDataSize == 0) {
		if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
			return FALSE;
		else if (instance->UnevenSpanSupport) {
			io_info->IoforUnevenSpan = 1;
		} else {
			dev_info(&instance->pdev->dev,
				"raid->rowDataSize is 0, but has SPAN[0]"
				"rowDataSize = 0x%0x,"
				"but there is _NO_ UnevenSpanSupport\n",
				MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
			return FALSE;
		}
	}

1191 1192
	stripSize = 1 << raid->stripeShift;
	stripe_mask = stripSize-1;
1193 1194


1195 1196 1197 1198 1199 1200 1201 1202 1203
	/*
	 * calculate starting row and stripe, and number of strips and rows
	 */
	start_strip         = ldStartBlock >> raid->stripeShift;
	ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
	endLba              = ldStartBlock + numBlocks - 1;
	ref_in_end_stripe   = (u16)(endLba & stripe_mask);
	endStrip            = endLba >> raid->stripeShift;
	num_strips          = (u8)(endStrip - start_strip + 1); /* End strip */
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

	if (io_info->IoforUnevenSpan) {
		start_row = get_row_from_strip(instance, ld, start_strip, map);
		endRow	  = get_row_from_strip(instance, ld, endStrip, map);
		if (start_row == -1ULL || endRow == -1ULL) {
			dev_info(&instance->pdev->dev, "return from %s %d."
				"Send IO w/o region lock.\n",
				__func__, __LINE__);
			return FALSE;
		}

		if (raid->spanDepth == 1) {
			startlba_span = 0;
			*pdBlock = start_row << raid->stripeShift;
		} else
			startlba_span = (u8)mr_spanset_get_span_block(instance,
						ld, start_row, pdBlock, map);
		if (startlba_span == SPAN_INVALID) {
			dev_info(&instance->pdev->dev, "return from %s %d"
				"for row 0x%llx,start strip %llx"
				"endSrip %llx\n", __func__, __LINE__,
				(unsigned long long)start_row,
				(unsigned long long)start_strip,
				(unsigned long long)endStrip);
			return FALSE;
		}
		io_info->start_span	= startlba_span;
		io_info->start_row	= start_row;
#if SPAN_DEBUG
		dev_dbg(&instance->pdev->dev, "Check Span number from %s %d"
			"for row 0x%llx, start strip 0x%llx end strip 0x%llx"
			" span 0x%x\n", __func__, __LINE__,
			(unsigned long long)start_row,
			(unsigned long long)start_strip,
			(unsigned long long)endStrip, startlba_span);
		dev_dbg(&instance->pdev->dev, "start_row 0x%llx endRow 0x%llx"
			"Start span 0x%x\n", (unsigned long long)start_row,
			(unsigned long long)endRow, startlba_span);
#endif
	} else {
		start_row = mega_div64_32(start_strip, raid->rowDataSize);
		endRow    = mega_div64_32(endStrip, raid->rowDataSize);
	}
	numRows = (u8)(endRow - start_row + 1);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	/*
	 * calculate region info.
	 */

	/* assume region is at the start of the first row */
	regStart            = start_row << raid->stripeShift;
	/* assume this IO needs the full row - we'll adjust if not true */
	regSize             = stripSize;

1258 1259
	io_info->do_fp_rlbypass = raid->capability.fpBypassRegionLock;

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	/* Check if we can send this I/O via FastPath */
	if (raid->capability.fpCapable) {
		if (isRead)
			io_info->fpOkForIo = (raid->capability.fpReadCapable &&
					      ((num_strips == 1) ||
					       raid->capability.
					       fpReadAcrossStripe));
		else
			io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
					      ((num_strips == 1) ||
					       raid->capability.
					       fpWriteAcrossStripe));
	} else
1273 1274 1275 1276 1277 1278 1279 1280 1281
		io_info->fpOkForIo = FALSE;

	if (numRows == 1) {
		/* single-strip IOs can always lock only the data needed */
		if (num_strips == 1) {
			regStart += ref_in_start_stripe;
			regSize = numBlocks;
		}
		/* multi-strip IOs always need to full stripe locked */
1282 1283 1284 1285 1286
	} else if (io_info->IoforUnevenSpan == 0) {
		/*
		 * For Even span region lock optimization.
		 * If the start strip is the last in the start row
		 */
1287 1288 1289 1290
		if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
			regStart += ref_in_start_stripe;
			/* initialize count to sectors from startref to end
			   of strip */
1291
			regSize = stripSize - ref_in_start_stripe;
1292 1293
		}

1294
		/* add complete rows in the middle of the transfer */
1295 1296 1297
		if (numRows > 2)
			regSize += (numRows-2) << raid->stripeShift;

1298
		/* if IO ends within first strip of last row*/
1299 1300 1301 1302
		if (endStrip == endRow*raid->rowDataSize)
			regSize += ref_in_end_stripe+1;
		else
			regSize += stripSize;
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
	} else {
		/*
		 * For Uneven span region lock optimization.
		 * If the start strip is the last in the start row
		 */
		if (start_strip == (get_strip_from_row(instance, ld, start_row, map) +
				SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
			regStart += ref_in_start_stripe;
			/* initialize count to sectors from
			 * startRef to end of strip
			 */
			regSize = stripSize - ref_in_start_stripe;
		}
		/* Add complete rows in the middle of the transfer*/

		if (numRows > 2)
			/* Add complete rows in the middle of the transfer*/
			regSize += (numRows-2) << raid->stripeShift;

		/* if IO ends within first strip of last row */
		if (endStrip == get_strip_from_row(instance, ld, endRow, map))
			regSize += ref_in_end_stripe + 1;
		else
			regSize += stripSize;
1327 1328
	}

1329
	pRAID_Context->timeout_value =
1330 1331 1332
		cpu_to_le16(raid->fpIoTimeoutForLd ?
			    raid->fpIoTimeoutForLd :
			    map->raidMap.fpPdIoTimeoutSec);
1333
	if (fusion->adapter_type == INVADER_SERIES)
1334
		pRAID_Context->reg_lock_flags = (isRead) ?
1335
			raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
1336 1337
	else if (!instance->is_ventura)
		pRAID_Context->reg_lock_flags = (isRead) ?
1338
			REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
1339 1340 1341 1342
	pRAID_Context->virtual_disk_tgt_id = raid->targetId;
	pRAID_Context->reg_lock_row_lba    = cpu_to_le64(regStart);
	pRAID_Context->reg_lock_length    = cpu_to_le32(regSize);
	pRAID_Context->config_seq_num	= raid->seqNum;
1343 1344 1345
	/* save pointer to raid->LUN array */
	*raidLUN = raid->LUN;

1346 1347 1348 1349

	/*Get Phy Params only if FP capable, or else leave it to MR firmware
	  to do the calculation.*/
	if (io_info->fpOkForIo) {
1350 1351 1352 1353 1354 1355 1356 1357
		retval = io_info->IoforUnevenSpan ?
				mr_spanset_get_phy_params(instance, ld,
					start_strip, ref_in_start_stripe,
					io_info, pRAID_Context, map) :
				MR_GetPhyParams(instance, ld, start_strip,
					ref_in_start_stripe, io_info,
					pRAID_Context, map);
		/* If IO on an invalid Pd, then FP is not possible.*/
1358
		if (io_info->devHandle == MR_DEVHANDLE_INVALID)
1359 1360 1361 1362 1363
			io_info->fpOkForIo = FALSE;
		return retval;
	} else if (isRead) {
		uint stripIdx;
		for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
1364 1365 1366 1367 1368 1369 1370 1371 1372
			retval = io_info->IoforUnevenSpan ?
				mr_spanset_get_phy_params(instance, ld,
				    start_strip + stripIdx,
				    ref_in_start_stripe, io_info,
				    pRAID_Context, map) :
				MR_GetPhyParams(instance, ld,
				    start_strip + stripIdx, ref_in_start_stripe,
				    io_info, pRAID_Context, map);
			if (!retval)
1373 1374 1375
				return TRUE;
		}
	}
1376 1377 1378 1379 1380 1381

#if SPAN_DEBUG
	/* Just for testing what arm we get for strip.*/
	if (io_info->IoforUnevenSpan)
		get_arm_from_strip(instance, ld, start_strip, map);
#endif
1382 1383 1384
	return TRUE;
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
/*
******************************************************************************
*
* This routine pepare spanset info from Valid Raid map and store it into
* local copy of ldSpanInfo per instance data structure.
*
* Inputs :
* map    - LD map
* ldSpanInfo - ldSpanInfo per HBA instance
*
*/
1396
void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
1397
	PLD_SPAN_INFO ldSpanInfo)
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
{
	u8   span, count;
	u32  element, span_row_width;
	u64  span_row;
	struct MR_LD_RAID *raid;
	LD_SPAN_SET *span_set, *span_set_prev;
	struct MR_QUAD_ELEMENT    *quad;
	int ldCount;
	u16 ld;


1409
	for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1410
		ld = MR_TargetIdToLdGet(ldCount, map);
1411
		if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
1412 1413 1414 1415
			continue;
		raid = MR_LdRaidGet(ld, map);
		for (element = 0; element < MAX_QUAD_DEPTH; element++) {
			for (span = 0; span < raid->spanDepth; span++) {
1416 1417
				if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
					block_span_info.noElements) <
1418 1419 1420 1421 1422 1423 1424
					element + 1)
					continue;
				span_set = &(ldSpanInfo[ld].span_set[element]);
				quad = &map->raidMap.ldSpanMap[ld].
					spanBlock[span].block_span_info.
					quad[element];

1425
				span_set->diff = le32_to_cpu(quad->diff);
1426 1427 1428

				for (count = 0, span_row_width = 0;
					count < raid->spanDepth; count++) {
1429
					if (le32_to_cpu(map->raidMap.ldSpanMap[ld].
1430 1431
						spanBlock[count].
						block_span_info.
1432
						noElements) >= element + 1) {
1433 1434 1435 1436 1437 1438 1439 1440 1441
						span_set->strip_offset[count] =
							span_row_width;
						span_row_width +=
							MR_LdSpanPtrGet
							(ld, count, map)->spanRowDataSize;
					}
				}

				span_set->span_row_data_width = span_row_width;
1442 1443 1444
				span_row = mega_div64_32(((le64_to_cpu(quad->logEnd) -
					le64_to_cpu(quad->logStart)) + le32_to_cpu(quad->diff)),
					le32_to_cpu(quad->diff));
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

				if (element == 0) {
					span_set->log_start_lba = 0;
					span_set->log_end_lba =
						((span_row << raid->stripeShift)
						* span_row_width) - 1;

					span_set->span_row_start = 0;
					span_set->span_row_end = span_row - 1;

					span_set->data_strip_start = 0;
					span_set->data_strip_end =
						(span_row * span_row_width) - 1;

					span_set->data_row_start = 0;
					span_set->data_row_end =
1461
						(span_row * le32_to_cpu(quad->diff)) - 1;
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
				} else {
					span_set_prev = &(ldSpanInfo[ld].
							span_set[element - 1]);
					span_set->log_start_lba =
						span_set_prev->log_end_lba + 1;
					span_set->log_end_lba =
						span_set->log_start_lba +
						((span_row << raid->stripeShift)
						* span_row_width) - 1;

					span_set->span_row_start =
						span_set_prev->span_row_end + 1;
					span_set->span_row_end =
					span_set->span_row_start + span_row - 1;

					span_set->data_strip_start =
					span_set_prev->data_strip_end + 1;
					span_set->data_strip_end =
						span_set->data_strip_start +
						(span_row * span_row_width) - 1;

					span_set->data_row_start =
						span_set_prev->data_row_end + 1;
					span_set->data_row_end =
						span_set->data_row_start +
1487
						(span_row * le32_to_cpu(quad->diff)) - 1;
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
				}
				break;
		}
		if (span == raid->spanDepth)
			break;
	    }
	}
#if SPAN_DEBUG
	getSpanInfo(map, ldSpanInfo);
#endif

}

1501 1502
void mr_update_load_balance_params(struct MR_DRV_RAID_MAP_ALL *drv_map,
	struct LD_LOAD_BALANCE_INFO *lbInfo)
1503 1504 1505 1506 1507
{
	int ldCount;
	u16 ld;
	struct MR_LD_RAID *raid;

1508 1509 1510
	if (lb_pending_cmds > 128 || lb_pending_cmds < 1)
		lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;

1511
	for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1512
		ld = MR_TargetIdToLdGet(ldCount, drv_map);
1513
		if (ld >= MAX_LOGICAL_DRIVES_EXT) {
1514 1515 1516 1517
			lbInfo[ldCount].loadBalanceFlag = 0;
			continue;
		}

1518 1519 1520
		raid = MR_LdRaidGet(ld, drv_map);
		if ((raid->level != 1) ||
			(raid->ldState != MR_LD_STATE_OPTIMAL)) {
1521
			lbInfo[ldCount].loadBalanceFlag = 0;
1522 1523 1524
			continue;
		}
		lbInfo[ldCount].loadBalanceFlag = 1;
1525 1526 1527
	}
}

1528
u8 megasas_get_best_arm_pd(struct megasas_instance *instance,
1529 1530 1531
			   struct LD_LOAD_BALANCE_INFO *lbInfo,
			   struct IO_REQUEST_INFO *io_info,
			   struct MR_DRV_RAID_MAP_ALL *drv_map)
1532
{
1533
	struct MR_LD_RAID  *raid;
1534
	u16	pd1_dev_handle;
1535
	u16     pend0, pend1, ld;
1536
	u64     diff0, diff1;
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
	u8      bestArm, pd0, pd1, span, arm;
	u32     arRef, span_row_size;

	u64 block = io_info->ldStartBlock;
	u32 count = io_info->numBlocks;

	span = ((io_info->span_arm & RAID_CTX_SPANARM_SPAN_MASK)
			>> RAID_CTX_SPANARM_SPAN_SHIFT);
	arm = (io_info->span_arm & RAID_CTX_SPANARM_ARM_MASK);

	ld = MR_TargetIdToLdGet(io_info->ldTgtId, drv_map);
	raid = MR_LdRaidGet(ld, drv_map);
	span_row_size = instance->UnevenSpanSupport ?
			SPAN_ROW_SIZE(drv_map, ld, span) : raid->rowSize;

	arRef = MR_LdSpanArrayGet(ld, span, drv_map);
	pd0 = MR_ArPdGet(arRef, arm, drv_map);
	pd1 = MR_ArPdGet(arRef, (arm + 1) >= span_row_size ?
		(arm + 1 - span_row_size) : arm + 1, drv_map);
1556

1557 1558 1559 1560
	/* Get PD1 Dev Handle */

	pd1_dev_handle = MR_PdDevHandleGet(pd1, drv_map);

1561
	if (pd1_dev_handle == MR_DEVHANDLE_INVALID) {
1562 1563 1564 1565 1566
		bestArm = arm;
	} else {
		/* get the pending cmds for the data and mirror arms */
		pend0 = atomic_read(&lbInfo->scsi_pending_cmds[pd0]);
		pend1 = atomic_read(&lbInfo->scsi_pending_cmds[pd1]);
1567

1568 1569 1570 1571
		/* Determine the disk whose head is nearer to the req. block */
		diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[pd0]);
		diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[pd1]);
		bestArm = (diff0 <= diff1 ? arm : arm ^ 1);
1572

1573 1574 1575 1576
		/* Make balance count from 16 to 4 to
		 *  keep driver in sync with Firmware
		 */
		if ((bestArm == arm && pend0 > pend1 + lb_pending_cmds)  ||
1577
			(bestArm != arm && pend1 > pend0 + lb_pending_cmds))
1578 1579 1580 1581 1582 1583 1584
			bestArm ^= 1;

		/* Update the last accessed block on the correct pd */
		io_info->span_arm =
			(span << RAID_CTX_SPANARM_SPAN_SHIFT) | bestArm;
		io_info->pd_after_lb = (bestArm == arm) ? pd0 : pd1;
	}
1585

1586 1587 1588 1589 1590 1591 1592 1593 1594
	lbInfo->last_accessed_block[io_info->pd_after_lb] = block + count - 1;
#if SPAN_DEBUG
	if (arm != bestArm)
		dev_dbg(&instance->pdev->dev, "LSI Debug R1 Load balance "
			"occur - span 0x%x arm 0x%x bestArm 0x%x "
			"io_info->span_arm 0x%x\n",
			span, arm, bestArm, io_info->span_arm);
#endif
	return io_info->pd_after_lb;
1595 1596
}

1597
__le16 get_updated_dev_handle(struct megasas_instance *instance,
1598 1599 1600
			      struct LD_LOAD_BALANCE_INFO *lbInfo,
			      struct IO_REQUEST_INFO *io_info,
			      struct MR_DRV_RAID_MAP_ALL *drv_map)
1601
{
1602
	u8 arm_pd;
1603
	__le16 devHandle;
1604

1605
	/* get best new arm (PD ID) */
1606
	arm_pd  = megasas_get_best_arm_pd(instance, lbInfo, io_info, drv_map);
1607
	devHandle = MR_PdDevHandleGet(arm_pd, drv_map);
1608
	io_info->pd_interface = MR_PdInterfaceTypeGet(arm_pd, drv_map);
1609
	atomic_inc(&lbInfo->scsi_pending_cmds[arm_pd]);
1610

1611 1612
	return devHandle;
}