summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/mr_sas/ld_pd_map.c
blob: 0f2798a790a4c7b6beb9b7d28e9d395c48722514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * **********************************************************************
 *
 * ld_pd_map.c
 *
 * Solaris MegaRAID device driver for SAS2.0 controllers
 * Copyright (c) 2008-2012, LSI Logic Corporation.
 * All rights reserved.
 *
 * Version:
 * Author:
 *		Swaminathan K S
 *		Arun Chandrashekhar
 *		Manju R
 *		Rasheed
 *		Shakeel Bukhari
 *
 *
 * This module contains functions for device drivers
 * to get pd-ld mapping information.
 *
 * **********************************************************************
 */
/*
 * Copyright 2015 Garrett D'Amore <garrett@damore.org>
 * Copyright 2017 Citrus IT Limited. All rights reserved.
 */

#include <sys/scsi/scsi.h>
#include "mr_sas.h"
#include "ld_pd_map.h"

/*
 * This function will check if FAST IO is possible on this logical drive
 * by checking the EVENT information available in the driver
 */
#define	MR_LD_STATE_OPTIMAL 3
#define	ABS_DIFF(a, b)   (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))

static void mr_update_load_balance_params(MR_FW_RAID_MAP_ALL *,
    PLD_LOAD_BALANCE_INFO);

#define	FALSE 0
#define	TRUE 1

typedef	U64	REGION_KEY;
typedef	U32	REGION_LEN;
extern int 	debug_level_g;


MR_LD_RAID
*MR_LdRaidGet(U32 ld, MR_FW_RAID_MAP_ALL *map)
{
	return (&map->raidMap.ldSpanMap[ld].ldRaid);
}

U16
MR_GetLDTgtId(U32 ld, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.ldSpanMap[ld].ldRaid.targetId);
}


static MR_SPAN_BLOCK_INFO *
MR_LdSpanInfoGet(U32 ld, MR_FW_RAID_MAP_ALL *map)
{
	return (&map->raidMap.ldSpanMap[ld].spanBlock[0]);
}

static U8
MR_LdDataArmGet(U32 ld, U32 armIdx, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.ldSpanMap[ld].dataArmMap[armIdx]);
}

static U16
MR_ArPdGet(U32 ar, U32 arm, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.arMapInfo[ar].pd[arm]);
}

static U16
MR_LdSpanArrayGet(U32 ld, U32 span, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef);
}

static U16
MR_PdDevHandleGet(U32 pd, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.devHndlInfo[pd].curDevHdl);
}

U16
MR_TargetIdToLdGet(U32 ldTgtId, MR_FW_RAID_MAP_ALL *map)
{
	return (map->raidMap.ldTgtIdToLd[ldTgtId]);
}

U16
MR_CheckDIF(U32 ldTgtId, MR_FW_RAID_MAP_ALL *map)
{
	MR_LD_RAID	*raid;
	U32		ld;

	ld = MR_TargetIdToLdGet(ldTgtId, map);

	if (ld >= MAX_LOGICAL_DRIVES) {
		return (FALSE);
	}

	raid = MR_LdRaidGet(ld, map);

	return (raid->capability.ldPiMode == 0x8);
}

static MR_LD_SPAN *
MR_LdSpanPtrGet(U32 ld, U32 span, MR_FW_RAID_MAP_ALL *map)
{
	return (&map->raidMap.ldSpanMap[ld].spanBlock[span].span);
}

/*
 * This function will validate Map info data provided by FW
 */
U8
MR_ValidateMapInfo(MR_FW_RAID_MAP_ALL *map, PLD_LOAD_BALANCE_INFO lbInfo)
{
	MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap;
	U32 fwsize = sizeof (MR_FW_RAID_MAP) - sizeof (MR_LD_SPAN_MAP) +
	    (sizeof (MR_LD_SPAN_MAP) * pFwRaidMap->ldCount);

	if (pFwRaidMap->totalSize != fwsize) {

		con_log(CL_ANN1, (CE_NOTE,
		    "map info structure size 0x%x is "
		    "not matching with ld count\n", fwsize));
		/* sizeof (foo) returns size_t, which is *LONG*. */
		con_log(CL_ANN1, (CE_NOTE, "span map 0x%x total size 0x%x\n",\
		    (int)sizeof (MR_LD_SPAN_MAP), pFwRaidMap->totalSize));

		return (0);
	}

	mr_update_load_balance_params(map, lbInfo);

	return (1);
}

U32
MR_GetSpanBlock(U32 ld, U64 row, U64 *span_blk, MR_FW_RAID_MAP_ALL *map,
    int *div_error)
{
	MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
	MR_QUAD_ELEMENT	*qe;
	MR_LD_RAID	*raid = MR_LdRaidGet(ld, map);
	U32		span, j;

	for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
		for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
			qe = &pSpanBlock->block_span_info.quads[j];
			if (qe->diff == 0) {
				*div_error = 1;
				return (span);
			}
			if (qe->logStart <= row && row <= qe->logEnd &&
			    (((row - qe->logStart) % qe->diff)) == 0) {
				if (span_blk != NULL) {
					U64	blk;
					blk = ((row - qe->logStart) /
					    (qe->diff));

					blk = (blk + qe->offsetInSpan) <<
					    raid->stripeShift;
					*span_blk = blk;
				}
				return (span);
			}
		}
	}
	return (span);
}


/*
 * *************************************************************
 *
 * 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
 */
U8
MR_GetPhyParams(struct mrsas_instance *instance, U32 ld, U64 stripRow,
    U16 stripRef, U64 *pdBlock, U16 *pDevHandle,
    MPI2_SCSI_IO_VENDOR_UNIQUE *pRAID_Context, MR_FW_RAID_MAP_ALL *map)
{
	MR_LD_RAID	*raid = MR_LdRaidGet(ld, map);
	U32		pd, arRef;
	U8		physArm, span;
	U64		row;
	int		error_code = 0;
	U8		retval = TRUE;
	U32		rowMod;
	U32		armQ;
	U32		arm;

	ASSERT(raid->rowDataSize != 0);

	row = (stripRow / raid->rowDataSize);

	if (raid->level == 6) {
		U32 logArm =  (stripRow % (raid->rowDataSize));

		if (raid->rowSize == 0) {
			return (FALSE);
		}
		rowMod = (row % (raid->rowSize));
		armQ = raid->rowSize-1-rowMod;
		arm = armQ + 1 + logArm;
		if (arm >= raid->rowSize)
			arm -= raid->rowSize;
		physArm = (U8)arm;
	} else {
		if (raid->modFactor == 0)
			return (FALSE);
		physArm = MR_LdDataArmGet(ld,
		    (stripRow % (raid->modFactor)), map);
	}
	if (raid->spanDepth == 1) {
		span = 0;
		*pdBlock = row << raid->stripeShift;
	} else
		span = (U8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);

	if (error_code == 1)
		return (FALSE);

	/* Get the array on which this span is present. */
	arRef		= MR_LdSpanArrayGet(ld, span, map);
	/* Get the Pd. */
	pd		= MR_ArPdGet(arRef, physArm, map);
	/* Get dev handle from Pd. */
	if (pd != MR_PD_INVALID) {
		*pDevHandle	= MR_PdDevHandleGet(pd, map);
	} else {
		*pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
		if (raid->level >= 5 && (!instance->gen3 ||
		    raid->regTypeReqOnRead != REGION_TYPE_UNUSED)) {
			pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
		} else if (raid->level == 1) {
			/* Get Alternate Pd. */
			pd = MR_ArPdGet(arRef, physArm + 1, map);
			/* Get dev handle from Pd. */
			if (pd != MR_PD_INVALID)
				*pDevHandle = MR_PdDevHandleGet(pd, map);
		}
	}

	*pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;

	pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
	    physArm;

	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
MR_BuildRaidContext(struct mrsas_instance *instance,
    struct IO_REQUEST_INFO *io_info, MPI2_SCSI_IO_VENDOR_UNIQUE *pRAID_Context,
    MR_FW_RAID_MAP_ALL *map)
{
	MR_LD_RAID	*raid;
	U32		ld, stripSize, stripe_mask;
	U64		endLba, endStrip, endRow;
	U64		start_row, start_strip;
	REGION_KEY	regStart;
	REGION_LEN	regSize;
	U8		num_strips, numRows;
	U16		ref_in_start_stripe;
	U16		ref_in_end_stripe;

	U64		ldStartBlock;
	U32		numBlocks, ldTgtId;
	U8		isRead;
	U8		retval = 0;

	ldStartBlock = io_info->ldStartBlock;
	numBlocks = io_info->numBlocks;
	ldTgtId = io_info->ldTgtId;
	isRead = io_info->isRead;

	if (map == NULL) {
		io_info->fpOkForIo = FALSE;
		return (FALSE);
	}

	ld = MR_TargetIdToLdGet(ldTgtId, map);

	if (ld >= MAX_LOGICAL_DRIVES) {
		io_info->fpOkForIo = FALSE;
		return (FALSE);
	}

	raid = MR_LdRaidGet(ld, map);

	stripSize = 1 << raid->stripeShift;
	stripe_mask = stripSize-1;
	/*
	 * 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);
	/* Check to make sure is not dividing by zero */
	if (raid->rowDataSize == 0)
		return (FALSE);
	start_row		=  (start_strip / raid->rowDataSize);
	endRow			=  (endStrip  / raid->rowDataSize);
	/* get the row count */
	numRows			= (U8)(endRow - start_row + 1);

	/*
	 * calculate region info.
	 */
	regStart	= start_row << raid->stripeShift;
	regSize		= stripSize;

	/* 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
		io_info->fpOkForIo = FALSE;


	/*
	 * Check for DIF support
	 */
	if (!raid->capability.ldPiMode) {
		io_info->ldPI = FALSE;
	} else {
		io_info->ldPI = TRUE;
	}

	if (numRows == 1) {
		if (num_strips == 1) {
			regStart += ref_in_start_stripe;
			regSize = numBlocks;
		}
	} else {
		if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
			regStart += ref_in_start_stripe;
		regSize = stripSize - ref_in_start_stripe;
		}

		if (numRows > 2) {
			regSize += (numRows - 2) << raid->stripeShift;
		}

		if (endStrip == endRow * raid->rowDataSize) {
			regSize += ref_in_end_stripe + 1;
		} else {
			regSize += stripSize;
		}
	}

	pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;

	if (instance->gen3) {
		pRAID_Context->regLockFlags = (isRead) ?
		    raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
	} else {
		pRAID_Context->regLockFlags = (isRead) ?
		    REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
	}

	pRAID_Context->ldTargetId = raid->targetId;
	pRAID_Context->regLockRowLBA = regStart;
	pRAID_Context->regLockLength = regSize;
	pRAID_Context->configSeqNum = raid->seqNum;

	/*
	 * Get Phy Params only if FP capable,
	 * or else leave it to MR firmware to do the calculation.
	 */
	if (io_info->fpOkForIo) {
		/* if fast path possible then get the physical parameters */
		retval = MR_GetPhyParams(instance, ld, start_strip,
		    ref_in_start_stripe, &io_info->pdBlock,
		    &io_info->devHandle, pRAID_Context, map);

		/* If IO on an invalid Pd, then FP is not possible. */
		if (io_info->devHandle == MR_PD_INVALID)
			io_info->fpOkForIo = FALSE;

		return (retval);

	} else if (isRead) {
		uint_t stripIdx;

		for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
			if (!MR_GetPhyParams(instance, ld,
			    start_strip + stripIdx, ref_in_start_stripe,
			    &io_info->pdBlock, &io_info->devHandle,
			    pRAID_Context, map)) {
				return (TRUE);
			}
		}
	}
	return (TRUE);
}


void
mr_update_load_balance_params(MR_FW_RAID_MAP_ALL *map,
    PLD_LOAD_BALANCE_INFO lbInfo)
{
	int ldCount;
	U16 ld;
	MR_LD_RAID *raid;

	for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
		ld = MR_TargetIdToLdGet(ldCount, map);

		if (ld >= MAX_LOGICAL_DRIVES) {
			con_log(CL_ANN1,
			    (CE_NOTE, "mrsas: ld=%d Invalid ld \n", ld));
			continue;
		}

		raid = MR_LdRaidGet(ld, map);

		/* Two drive Optimal RAID 1 */
		if ((raid->level == 1) && (raid->rowSize == 2) &&
		    (raid->spanDepth == 1) &&
		    raid->ldState == MR_LD_STATE_OPTIMAL) {
			U32 pd, arRef;

			lbInfo[ldCount].loadBalanceFlag = 1;

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

			pd = MR_ArPdGet(arRef, 0, map);	    /* Get the Pd. */
			/* Get dev handle from Pd. */
			lbInfo[ldCount].raid1DevHandle[0] =
			    MR_PdDevHandleGet(pd, map);

			pd = MR_ArPdGet(arRef, 1, map);	    /* Get the Pd. */
			/* Get dev handle from Pd. */
			lbInfo[ldCount].raid1DevHandle[1] =
			    MR_PdDevHandleGet(pd, map);
			con_log(CL_ANN1, (CE_NOTE,
			    "mrsas: ld=%d load balancing enabled \n", ldCount));
		} else {
			lbInfo[ldCount].loadBalanceFlag = 0;
		}
	}
}


U8
megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, U8 arm, U64 block,
    U32 count)
{
	U16 pend0, pend1;
	U64 diff0, diff1;
	U8 bestArm;

	/* get the pending cmds for the data and mirror arms */
	pend0 = lbInfo->scsi_pending_cmds[0];
	pend1 = lbInfo->scsi_pending_cmds[1];

	/* Determine the disk whose head is nearer to the req. block */
	diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
	diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
	bestArm = (diff0 <= diff1 ? 0 : 1);

	if ((bestArm == arm && pend0 > pend1 + 16) ||
	    (bestArm != arm && pend1 > pend0 + 16)) {
		bestArm ^= 1;
	}

	/* Update the last accessed block on the correct pd */
	lbInfo->last_accessed_block[bestArm] = block + count - 1;
	return (bestArm);
}

U16
get_updated_dev_handle(PLD_LOAD_BALANCE_INFO lbInfo,
    struct IO_REQUEST_INFO *io_info)
{
	U8 arm, old_arm;
	U16 devHandle;

	old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;

	/* get best new arm */
	arm  = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
	    io_info->numBlocks);

	devHandle = lbInfo->raid1DevHandle[arm];

	lbInfo->scsi_pending_cmds[arm]++;

	return (devHandle);
}