1 /*
2 * **********************************************************************
3 *
4 * ld_pd_map.c
5 *
6 * Solaris MegaRAID device driver for SAS2.0 controllers
7 * Copyright (c) 2008-2012, LSI Logic Corporation.
8 * All rights reserved.
9 *
10 * Version:
11 * Author:
12 * Swaminathan K S
13 * Arun Chandrashekhar
14 * Manju R
15 * Rasheed
16 * Shakeel Bukhari
17 *
18 *
19 * This module contains functions for device drivers
20 * to get pd-ld mapping information.
21 *
22 * **********************************************************************
23 */
24
25 #include <sys/scsi/scsi.h>
26 #include "mr_sas.h"
27 #include "ld_pd_map.h"
28
29 /*
30 * This function will check if FAST IO is possible on this logical drive
31 * by checking the EVENT information availabe in the driver
32 */
33 #define MR_LD_STATE_OPTIMAL 3
34 #define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
35
36 static void mr_update_load_balance_params(MR_FW_RAID_MAP_ALL *,
37 PLD_LOAD_BALANCE_INFO);
38
39 #define FALSE 0
40 #define TRUE 1
41
42 typedef U64 REGION_KEY;
43 typedef U32 REGION_LEN;
44 extern int debug_level_g;
45
46
47 MR_LD_RAID
48 *MR_LdRaidGet(U32 ld, MR_FW_RAID_MAP_ALL *map)
49 {
50 return (&map->raidMap.ldSpanMap[ld].ldRaid);
51 }
52
53 U16
54 MR_GetLDTgtId(U32 ld, MR_FW_RAID_MAP_ALL *map)
55 {
56 return (map->raidMap.ldSpanMap[ld].ldRaid.targetId);
57 }
58
59
60 static MR_SPAN_BLOCK_INFO *
61 MR_LdSpanInfoGet(U32 ld, MR_FW_RAID_MAP_ALL *map)
62 {
63 return (&map->raidMap.ldSpanMap[ld].spanBlock[0]);
64 }
65
66 static U8
67 MR_LdDataArmGet(U32 ld, U32 armIdx, MR_FW_RAID_MAP_ALL *map)
68 {
69 return (map->raidMap.ldSpanMap[ld].dataArmMap[armIdx]);
70 }
71
72 static U16
73 MR_ArPdGet(U32 ar, U32 arm, MR_FW_RAID_MAP_ALL *map)
74 {
75 return (map->raidMap.arMapInfo[ar].pd[arm]);
76 }
77
78 static U16
79 MR_LdSpanArrayGet(U32 ld, U32 span, MR_FW_RAID_MAP_ALL *map)
80 {
81 return (map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef);
82 }
83
84 static U16
85 MR_PdDevHandleGet(U32 pd, MR_FW_RAID_MAP_ALL *map)
86 {
87 return (map->raidMap.devHndlInfo[pd].curDevHdl);
88 }
89
90 U16
91 MR_TargetIdToLdGet(U32 ldTgtId, MR_FW_RAID_MAP_ALL *map)
92 {
93 return (map->raidMap.ldTgtIdToLd[ldTgtId]);
94 }
95
96 U16
97 MR_CheckDIF(U32 ldTgtId, MR_FW_RAID_MAP_ALL *map)
98 {
99 MR_LD_RAID *raid;
100 U32 ld;
101
102 ld = MR_TargetIdToLdGet(ldTgtId, map);
103
104 if (ld >= MAX_LOGICAL_DRIVES) {
105 return (FALSE);
106 }
107
108 raid = MR_LdRaidGet(ld, map);
109
110 return (raid->capability.ldPiMode == 0x8);
111 }
112
113 static MR_LD_SPAN *
114 MR_LdSpanPtrGet(U32 ld, U32 span, MR_FW_RAID_MAP_ALL *map)
115 {
116 return (&map->raidMap.ldSpanMap[ld].spanBlock[span].span);
117 }
118
119 /*
120 * This function will validate Map info data provided by FW
121 */
122 U8
123 MR_ValidateMapInfo(MR_FW_RAID_MAP_ALL *map, PLD_LOAD_BALANCE_INFO lbInfo)
124 {
125 MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap;
126 U32 fwsize = sizeof (MR_FW_RAID_MAP) - sizeof (MR_LD_SPAN_MAP) +
127 (sizeof (MR_LD_SPAN_MAP) * pFwRaidMap->ldCount);
128
129 if (pFwRaidMap->totalSize != fwsize) {
130
131 con_log(CL_ANN1, (CE_NOTE,
132 "map info structure size 0x%x is "
133 "not matching with ld count\n", fwsize));
134 /* sizeof (foo) returns size_t, which is *LONG*. */
135 con_log(CL_ANN1, (CE_NOTE, "span map 0x%x total size 0x%x\n",\
136 (int)sizeof (MR_LD_SPAN_MAP), pFwRaidMap->totalSize));
137
138 return (0);
139 }
140
141 mr_update_load_balance_params(map, lbInfo);
142
143 return (1);
144 }
145
146 U32
147 MR_GetSpanBlock(U32 ld, U64 row, U64 *span_blk, MR_FW_RAID_MAP_ALL *map,
148 int *div_error)
149 {
150 MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
151 MR_QUAD_ELEMENT *qe;
152 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
153 U32 span, j;
154
155 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
156 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
157 qe = &pSpanBlock->block_span_info.quads[j];
158 if (qe->diff == 0) {
159 *div_error = 1;
160 return (span);
161 }
162 if (qe->logStart <= row && row <= qe->logEnd &&
163 (((row - qe->logStart) % qe->diff)) == 0) {
164 if (span_blk != NULL) {
165 U64 blk;
166 blk = ((row - qe->logStart) /
167 (qe->diff));
168
169 blk = (blk + qe->offsetInSpan) <<
170 raid->stripeShift;
171 *span_blk = blk;
172 }
173 return (span);
174 }
175 }
176 }
177 return (span);
178 }
179
180
181 /*
182 * *************************************************************
183 *
184 * This routine calculates the arm, span and block for
185 * the specified stripe and reference in stripe.
186 *
187 * Inputs :
188 *
189 * ld - Logical drive number
190 * stripRow - Stripe number
191 * stripRef - Reference in stripe
192 *
193 * Outputs :
194 *
195 * span - Span number
196 * block - Absolute Block number in the physical disk
197 */
198 U8
199 MR_GetPhyParams(struct mrsas_instance *instance, U32 ld, U64 stripRow,
200 U16 stripRef, U64 *pdBlock, U16 *pDevHandle,
201 MPI2_SCSI_IO_VENDOR_UNIQUE *pRAID_Context, MR_FW_RAID_MAP_ALL *map)
202 {
203 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
204 U32 pd, arRef;
205 U8 physArm, span;
206 U64 row;
207 int error_code = 0;
208 U8 retval = TRUE;
209 U32 rowMod;
210 U32 armQ;
211 U32 arm;
212
213 row = (stripRow / raid->rowDataSize);
214
215 if (raid->level == 6) {
216 U32 logArm = (stripRow % (raid->rowDataSize));
217
218 if (raid->rowSize == 0) {
219 return (FALSE);
220 }
221 rowMod = (row % (raid->rowSize));
222 armQ = raid->rowSize-1-rowMod;
223 arm = armQ+1+logArm;
224 if (arm >= raid->rowSize)
225 arm -= raid->rowSize;
226 physArm = (U8)arm;
227 } else {
228 if (raid->modFactor == 0)
229 return (FALSE);
230 physArm = MR_LdDataArmGet(ld,
231 (stripRow % (raid->modFactor)), map);
232 }
233 if (raid->spanDepth == 1) {
234 span = 0;
235 *pdBlock = row << raid->stripeShift;
236 } else
237 span = (U8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
238
239 if (error_code == 1)
240 return (FALSE);
241
242 /* Get the array on which this span is present. */
243 arRef = MR_LdSpanArrayGet(ld, span, map);
244 /* Get the Pd. */
245 pd = MR_ArPdGet(arRef, physArm, map);
246 /* Get dev handle from Pd. */
247 if (pd != MR_PD_INVALID) {
248 *pDevHandle = MR_PdDevHandleGet(pd, map);
249 } else {
250 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
251 if ((raid->level >= 5) &&
252 ((instance->device_id != PCI_DEVICE_ID_LSI_INVADER) ||
253 (instance->device_id == PCI_DEVICE_ID_LSI_INVADER &&
254 raid->regTypeReqOnRead != REGION_TYPE_UNUSED))) {
255 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
256 } else if (raid->level == 1) {
257 /* Get Alternate Pd. */
258 pd = MR_ArPdGet(arRef, physArm + 1, map);
259 /* Get dev handle from Pd. */
260 if (pd != MR_PD_INVALID)
261 *pDevHandle = MR_PdDevHandleGet(pd, map);
262 }
263 }
264
265 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
266
267 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
268 physArm;
269
270 return (retval);
271 }
272
273
274
275 /*
276 * ***********************************************************************
277 *
278 * MR_BuildRaidContext function
279 *
280 * This function will initiate command processing. The start/end row and strip
281 * information is calculated then the lock is acquired.
282 * This function will return 0 if region lock
283 * was acquired OR return num strips ???
284 */
285
286 U8
287 MR_BuildRaidContext(struct mrsas_instance *instance,
288 struct IO_REQUEST_INFO *io_info, MPI2_SCSI_IO_VENDOR_UNIQUE *pRAID_Context,
289 MR_FW_RAID_MAP_ALL *map)
290 {
291 MR_LD_RAID *raid;
292 U32 ld, stripSize, stripe_mask;
293 U64 endLba, endStrip, endRow;
294 U64 start_row, start_strip;
295 REGION_KEY regStart;
296 REGION_LEN regSize;
297 U8 num_strips, numRows;
298 U16 ref_in_start_stripe;
299 U16 ref_in_end_stripe;
300
301 U64 ldStartBlock;
302 U32 numBlocks, ldTgtId;
303 U8 isRead;
304 U8 retval = 0;
305
306 ldStartBlock = io_info->ldStartBlock;
307 numBlocks = io_info->numBlocks;
308 ldTgtId = io_info->ldTgtId;
309 isRead = io_info->isRead;
310
311 if (map == NULL) {
312 io_info->fpOkForIo = FALSE;
313 return (FALSE);
314 }
315
316 ld = MR_TargetIdToLdGet(ldTgtId, map);
317
318 if (ld >= MAX_LOGICAL_DRIVES) {
319 io_info->fpOkForIo = FALSE;
320 return (FALSE);
321 }
322
323 raid = MR_LdRaidGet(ld, map);
324
325 stripSize = 1 << raid->stripeShift;
326 stripe_mask = stripSize-1;
327 /*
328 * calculate starting row and stripe, and number of strips and rows
329 */
330 start_strip = ldStartBlock >> raid->stripeShift;
331 ref_in_start_stripe = (U16)(ldStartBlock & stripe_mask);
332 endLba = ldStartBlock + numBlocks - 1;
333 ref_in_end_stripe = (U16)(endLba & stripe_mask);
334 endStrip = endLba >> raid->stripeShift;
335 num_strips = (U8)(endStrip - start_strip + 1);
336 /* Check to make sure is not deviding by zero */
337 if (raid->rowDataSize == 0)
338 return (FALSE);
339 start_row = (start_strip / raid->rowDataSize);
340 endRow = (endStrip / raid->rowDataSize);
341 /* get the row count */
342 numRows = (U8)(endRow - start_row + 1);
343
344 /*
345 * calculate region info.
346 */
347 regStart = start_row << raid->stripeShift;
348 regSize = stripSize;
349
350 /* Check if we can send this I/O via FastPath */
351 if (raid->capability.fpCapable) {
352 if (isRead)
353 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
354 ((num_strips == 1) ||
355 raid->capability.fpReadAcrossStripe));
356 else
357 io_info->fpOkForIo =
358 (raid->capability.fpWriteCapable &&
359 ((num_strips == 1) ||
360 raid->capability.fpWriteAcrossStripe));
361 } else
362 io_info->fpOkForIo = FALSE;
363
364
365 /*
366 * Check for DIF support
367 */
368 if (!raid->capability.ldPiMode) {
369 io_info->ldPI = FALSE;
370 } else {
371 io_info->ldPI = TRUE;
372 }
373
374 if (numRows == 1) {
375 if (num_strips == 1) {
376 regStart += ref_in_start_stripe;
377 regSize = numBlocks;
378 }
379 } else {
380 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
381 regStart += ref_in_start_stripe;
382 regSize = stripSize - ref_in_start_stripe;
383 }
384
385 if (numRows > 2) {
386 regSize += (numRows-2) << raid->stripeShift;
387 }
388
389 if (endStrip == endRow*raid->rowDataSize) {
390 regSize += ref_in_end_stripe+1;
391 } else {
392 regSize += stripSize;
393 }
394 }
395
396 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
397
398 if (instance->device_id == PCI_DEVICE_ID_LSI_INVADER) {
399 pRAID_Context->regLockFlags = (isRead) ?
400 raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
401 } else {
402 pRAID_Context->regLockFlags = (isRead) ?
403 REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
404 }
405
406 pRAID_Context->ldTargetId = raid->targetId;
407 pRAID_Context->regLockRowLBA = regStart;
408 pRAID_Context->regLockLength = regSize;
409 pRAID_Context->configSeqNum = raid->seqNum;
410
411 /*
412 * Get Phy Params only if FP capable,
413 * or else leave it to MR firmware to do the calculation.
414 */
415 if (io_info->fpOkForIo) {
416 /* if fast path possible then get the physical parameters */
417 retval = MR_GetPhyParams(instance, ld, start_strip,
418 ref_in_start_stripe, &io_info->pdBlock,
419 &io_info->devHandle, pRAID_Context, map);
420
421 /* If IO on an invalid Pd, then FP is not possible. */
422 if (io_info->devHandle == MR_PD_INVALID)
423 io_info->fpOkForIo = FALSE;
424
425 return (retval);
426
427 } else if (isRead) {
428 uint_t stripIdx;
429
430 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
431 if (!MR_GetPhyParams(instance, ld,
432 start_strip + stripIdx, ref_in_start_stripe,
433 &io_info->pdBlock, &io_info->devHandle,
434 pRAID_Context, map)) {
435 return (TRUE);
436 }
437 }
438 }
439 return (TRUE);
440 }
441
442
443 void
444 mr_update_load_balance_params(MR_FW_RAID_MAP_ALL *map,
445 PLD_LOAD_BALANCE_INFO lbInfo)
446 {
447 int ldCount;
448 U16 ld;
449 MR_LD_RAID *raid;
450
451 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
452 ld = MR_TargetIdToLdGet(ldCount, map);
453
454 if (ld >= MAX_LOGICAL_DRIVES) {
455 con_log(CL_ANN1,
456 (CE_NOTE, "mrsas: ld=%d Invalid ld \n", ld));
457 continue;
458 }
459
460 raid = MR_LdRaidGet(ld, map);
461
462 /* Two drive Optimal RAID 1 */
463 if ((raid->level == 1) && (raid->rowSize == 2) &&
464 (raid->spanDepth == 1) &&
465 raid->ldState == MR_LD_STATE_OPTIMAL) {
466 U32 pd, arRef;
467
468 lbInfo[ldCount].loadBalanceFlag = 1;
469
470 /* Get the array on which this span is present. */
471 arRef = MR_LdSpanArrayGet(ld, 0, map);
472
473 pd = MR_ArPdGet(arRef, 0, map); /* Get the Pd. */
474 /* Get dev handle from Pd. */
475 lbInfo[ldCount].raid1DevHandle[0] =
476 MR_PdDevHandleGet(pd, map);
477
478 pd = MR_ArPdGet(arRef, 1, map); /* Get the Pd. */
479 /* Get dev handle from Pd. */
480 lbInfo[ldCount].raid1DevHandle[1] =
481 MR_PdDevHandleGet(pd, map);
482 con_log(CL_ANN1, (CE_NOTE,
483 "mrsas: ld=%d load balancing enabled \n", ldCount));
484 } else {
485 lbInfo[ldCount].loadBalanceFlag = 0;
486 }
487 }
488 }
489
490
491 U8
492 megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, U8 arm, U64 block,
493 U32 count)
494 {
495 U16 pend0, pend1;
496 U64 diff0, diff1;
497 U8 bestArm;
498
499 /* get the pending cmds for the data and mirror arms */
500 pend0 = lbInfo->scsi_pending_cmds[0];
501 pend1 = lbInfo->scsi_pending_cmds[1];
502
503 /* Determine the disk whose head is nearer to the req. block */
504 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
505 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
506 bestArm = (diff0 <= diff1 ? 0 : 1);
507
508 if ((bestArm == arm && pend0 > pend1 + 16) ||
509 (bestArm != arm && pend1 > pend0 + 16)) {
510 bestArm ^= 1;
511 }
512
513 /* Update the last accessed block on the correct pd */
514 lbInfo->last_accessed_block[bestArm] = block + count - 1;
515 return (bestArm);
516 }
517
518 U16
519 get_updated_dev_handle(PLD_LOAD_BALANCE_INFO lbInfo,
520 struct IO_REQUEST_INFO *io_info)
521 {
522 U8 arm, old_arm;
523 U16 devHandle;
524
525 old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
526
527 /* get best new arm */
528 arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
529 io_info->numBlocks);
530
531 devHandle = lbInfo->raid1DevHandle[arm];
532
533 lbInfo->scsi_pending_cmds[arm]++;
534
535 return (devHandle);
536 }