1 /* 2 * Linux MegaRAID driver for SAS based RAID controllers 3 * 4 * Copyright (c) 2009-2011 LSI Corporation. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 * FILE: megaraid_sas_fp.c 21 * 22 * Authors: LSI Corporation 23 * Sumant Patro 24 * Varad Talamacki 25 * Manoj Jose 26 * 27 * Send feedback to: <megaraidlinux@lsi.com> 28 * 29 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035 30 * ATTN: Linuxraid 31 */ 32 33 #include <linux/kernel.h> 34 #include <linux/types.h> 35 #include <linux/pci.h> 36 #include <linux/list.h> 37 #include <linux/moduleparam.h> 38 #include <linux/module.h> 39 #include <linux/spinlock.h> 40 #include <linux/interrupt.h> 41 #include <linux/delay.h> 42 #include <linux/smp_lock.h> 43 #include <linux/uio.h> 44 #include <linux/uaccess.h> 45 #include <linux/fs.h> 46 #include <linux/compat.h> 47 #include <linux/blkdev.h> 48 #include <linux/poll.h> 49 50 #include <scsi/scsi.h> 51 #include <scsi/scsi_cmnd.h> 52 #include <scsi/scsi_device.h> 53 #include <scsi/scsi_host.h> 54 55 #include "megaraid_sas_fusion.h" 56 #include <asm/div64.h> 57 58 #define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a))) 59 #define MR_LD_STATE_OPTIMAL 3 60 #define FALSE 0 61 #define TRUE 1 62 63 /* Prototypes */ 64 void 65 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map, 66 struct LD_LOAD_BALANCE_INFO *lbInfo); 67 68 u32 mega_mod64(u64 dividend, u32 divisor) 69 { 70 u64 d; 71 u32 remainder; 72 73 if (!divisor) 74 printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n"); 75 d = dividend; 76 remainder = do_div(d, divisor); 77 return remainder; 78 } 79 80 /** 81 * @param dividend : Dividend 82 * @param divisor : Divisor 83 * 84 * @return quotient 85 **/ 86 u64 mega_div64_32(uint64_t dividend, uint32_t divisor) 87 { 88 u32 remainder; 89 u64 d; 90 91 if (!divisor) 92 printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n"); 93 94 d = dividend; 95 remainder = do_div(d, divisor); 96 97 return d; 98 } 99 100 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map) 101 { 102 return &map->raidMap.ldSpanMap[ld].ldRaid; 103 } 104 105 static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld, 106 struct MR_FW_RAID_MAP_ALL 107 *map) 108 { 109 return &map->raidMap.ldSpanMap[ld].spanBlock[0]; 110 } 111 112 static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_FW_RAID_MAP_ALL *map) 113 { 114 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx]; 115 } 116 117 static u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_FW_RAID_MAP_ALL *map) 118 { 119 return map->raidMap.arMapInfo[ar].pd[arm]; 120 } 121 122 static u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_FW_RAID_MAP_ALL *map) 123 { 124 return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef; 125 } 126 127 static u16 MR_PdDevHandleGet(u32 pd, struct MR_FW_RAID_MAP_ALL *map) 128 { 129 return map->raidMap.devHndlInfo[pd].curDevHdl; 130 } 131 132 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map) 133 { 134 return map->raidMap.ldSpanMap[ld].ldRaid.targetId; 135 } 136 137 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map) 138 { 139 return map->raidMap.ldTgtIdToLd[ldTgtId]; 140 } 141 142 static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span, 143 struct MR_FW_RAID_MAP_ALL *map) 144 { 145 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span; 146 } 147 148 /* 149 * This function will validate Map info data provided by FW 150 */ 151 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map, 152 struct LD_LOAD_BALANCE_INFO *lbInfo) 153 { 154 struct MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap; 155 156 if (pFwRaidMap->totalSize != 157 (sizeof(struct MR_FW_RAID_MAP) -sizeof(struct MR_LD_SPAN_MAP) + 158 (sizeof(struct MR_LD_SPAN_MAP) *pFwRaidMap->ldCount))) { 159 printk(KERN_ERR "megasas: map info structure size 0x%x is not matching with ld count\n", 160 (unsigned int)((sizeof(struct MR_FW_RAID_MAP) - 161 sizeof(struct MR_LD_SPAN_MAP)) + 162 (sizeof(struct MR_LD_SPAN_MAP) * 163 pFwRaidMap->ldCount))); 164 printk(KERN_ERR "megasas: span map %x, pFwRaidMap->totalSize " 165 ": %x\n", (unsigned int)sizeof(struct MR_LD_SPAN_MAP), 166 pFwRaidMap->totalSize); 167 return 0; 168 } 169 170 mr_update_load_balance_params(map, lbInfo); 171 172 return 1; 173 } 174 175 u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk, 176 struct MR_FW_RAID_MAP_ALL *map, int *div_error) 177 { 178 struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map); 179 struct MR_QUAD_ELEMENT *quad; 180 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map); 181 u32 span, j; 182 183 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) { 184 185 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) { 186 quad = &pSpanBlock->block_span_info.quad[j]; 187 188 if (quad->diff == 0) { 189 *div_error = 1; 190 return span; 191 } 192 if (quad->logStart <= row && row <= quad->logEnd && 193 (mega_mod64(row-quad->logStart, quad->diff)) == 0) { 194 if (span_blk != NULL) { 195 u64 blk, debugBlk; 196 blk = 197 mega_div64_32( 198 (row-quad->logStart), 199 quad->diff); 200 debugBlk = blk; 201 202 blk = (blk + quad->offsetInSpan) << 203 raid->stripeShift; 204 *span_blk = blk; 205 } 206 return span; 207 } 208 } 209 } 210 return span; 211 } 212 213 /* 214 ****************************************************************************** 215 * 216 * This routine calculates the arm, span and block for the specified stripe and 217 * reference in stripe. 218 * 219 * Inputs : 220 * 221 * ld - Logical drive number 222 * stripRow - Stripe number 223 * stripRef - Reference in stripe 224 * 225 * Outputs : 226 * 227 * span - Span number 228 * block - Absolute Block number in the physical disk 229 */ 230 u8 MR_GetPhyParams(u32 ld, u64 stripRow, u16 stripRef, u64 *pdBlock, 231 u16 *pDevHandle, struct RAID_CONTEXT *pRAID_Context, 232 struct MR_FW_RAID_MAP_ALL *map) 233 { 234 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map); 235 u32 pd, arRef; 236 u8 physArm, span; 237 u64 row; 238 u8 retval = TRUE; 239 int error_code = 0; 240 241 row = mega_div64_32(stripRow, raid->rowDataSize); 242 243 if (raid->level == 6) { 244 /* logical arm within row */ 245 u32 logArm = mega_mod64(stripRow, raid->rowDataSize); 246 u32 rowMod, armQ, arm; 247 248 if (raid->rowSize == 0) 249 return FALSE; 250 /* get logical row mod */ 251 rowMod = mega_mod64(row, raid->rowSize); 252 armQ = raid->rowSize-1-rowMod; /* index of Q drive */ 253 arm = armQ+1+logArm; /* data always logically follows Q */ 254 if (arm >= raid->rowSize) /* handle wrap condition */ 255 arm -= raid->rowSize; 256 physArm = (u8)arm; 257 } else { 258 if (raid->modFactor == 0) 259 return FALSE; 260 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow, 261 raid->modFactor), 262 map); 263 } 264 265 if (raid->spanDepth == 1) { 266 span = 0; 267 *pdBlock = row << raid->stripeShift; 268 } else { 269 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code); 270 if (error_code == 1) 271 return FALSE; 272 } 273 274 /* Get the array on which this span is present */ 275 arRef = MR_LdSpanArrayGet(ld, span, map); 276 pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */ 277 278 if (pd != MR_PD_INVALID) 279 /* Get dev handle from Pd. */ 280 *pDevHandle = MR_PdDevHandleGet(pd, map); 281 else { 282 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */ 283 if (raid->level >= 5) 284 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE; 285 else if (raid->level == 1) { 286 /* Get alternate Pd. */ 287 pd = MR_ArPdGet(arRef, physArm + 1, map); 288 if (pd != MR_PD_INVALID) 289 /* Get dev handle from Pd */ 290 *pDevHandle = MR_PdDevHandleGet(pd, map); 291 } 292 retval = FALSE; 293 } 294 295 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk; 296 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | 297 physArm; 298 return retval; 299 } 300 301 /* 302 ****************************************************************************** 303 * 304 * MR_BuildRaidContext function 305 * 306 * This function will initiate command processing. The start/end row and strip 307 * information is calculated then the lock is acquired. 308 * This function will return 0 if region lock was acquired OR return num strips 309 */ 310 u8 311 MR_BuildRaidContext(struct IO_REQUEST_INFO *io_info, 312 struct RAID_CONTEXT *pRAID_Context, 313 struct MR_FW_RAID_MAP_ALL *map) 314 { 315 struct MR_LD_RAID *raid; 316 u32 ld, stripSize, stripe_mask; 317 u64 endLba, endStrip, endRow, start_row, start_strip; 318 u64 regStart; 319 u32 regSize; 320 u8 num_strips, numRows; 321 u16 ref_in_start_stripe, ref_in_end_stripe; 322 u64 ldStartBlock; 323 u32 numBlocks, ldTgtId; 324 u8 isRead; 325 u8 retval = 0; 326 327 ldStartBlock = io_info->ldStartBlock; 328 numBlocks = io_info->numBlocks; 329 ldTgtId = io_info->ldTgtId; 330 isRead = io_info->isRead; 331 332 ld = MR_TargetIdToLdGet(ldTgtId, map); 333 raid = MR_LdRaidGet(ld, map); 334 335 stripSize = 1 << raid->stripeShift; 336 stripe_mask = stripSize-1; 337 /* 338 * calculate starting row and stripe, and number of strips and rows 339 */ 340 start_strip = ldStartBlock >> raid->stripeShift; 341 ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask); 342 endLba = ldStartBlock + numBlocks - 1; 343 ref_in_end_stripe = (u16)(endLba & stripe_mask); 344 endStrip = endLba >> raid->stripeShift; 345 num_strips = (u8)(endStrip - start_strip + 1); /* End strip */ 346 if (raid->rowDataSize == 0) 347 return FALSE; 348 start_row = mega_div64_32(start_strip, raid->rowDataSize); 349 endRow = mega_div64_32(endStrip, raid->rowDataSize); 350 numRows = (u8)(endRow - start_row + 1); 351 352 /* 353 * calculate region info. 354 */ 355 356 /* assume region is at the start of the first row */ 357 regStart = start_row << raid->stripeShift; 358 /* assume this IO needs the full row - we'll adjust if not true */ 359 regSize = stripSize; 360 361 /* If IO spans more than 1 strip, fp is not possible 362 FP is not possible for writes on non-0 raid levels 363 FP is not possible if LD is not capable */ 364 if (num_strips > 1 || (!isRead && raid->level != 0) || 365 !raid->capability.fpCapable) { 366 io_info->fpOkForIo = FALSE; 367 } else { 368 io_info->fpOkForIo = TRUE; 369 } 370 371 if (numRows == 1) { 372 /* single-strip IOs can always lock only the data needed */ 373 if (num_strips == 1) { 374 regStart += ref_in_start_stripe; 375 regSize = numBlocks; 376 } 377 /* multi-strip IOs always need to full stripe locked */ 378 } else { 379 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) { 380 /* If the start strip is the last in the start row */ 381 regStart += ref_in_start_stripe; 382 regSize = stripSize - ref_in_start_stripe; 383 /* initialize count to sectors from startref to end 384 of strip */ 385 } 386 387 if (numRows > 2) 388 /* Add complete rows in the middle of the transfer */ 389 regSize += (numRows-2) << raid->stripeShift; 390 391 /* if IO ends within first strip of last row */ 392 if (endStrip == endRow*raid->rowDataSize) 393 regSize += ref_in_end_stripe+1; 394 else 395 regSize += stripSize; 396 } 397 398 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec; 399 pRAID_Context->regLockFlags = (isRead) ? REGION_TYPE_SHARED_READ : 400 raid->regTypeReqOnWrite; 401 pRAID_Context->VirtualDiskTgtId = raid->targetId; 402 pRAID_Context->regLockRowLBA = regStart; 403 pRAID_Context->regLockLength = regSize; 404 pRAID_Context->configSeqNum = raid->seqNum; 405 406 /*Get Phy Params only if FP capable, or else leave it to MR firmware 407 to do the calculation.*/ 408 if (io_info->fpOkForIo) { 409 retval = MR_GetPhyParams(ld, start_strip, ref_in_start_stripe, 410 &io_info->pdBlock, 411 &io_info->devHandle, pRAID_Context, 412 map); 413 /* If IO on an invalid Pd, then FP i snot possible */ 414 if (io_info->devHandle == MR_PD_INVALID) 415 io_info->fpOkForIo = FALSE; 416 return retval; 417 } else if (isRead) { 418 uint stripIdx; 419 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) { 420 if (!MR_GetPhyParams(ld, start_strip + stripIdx, 421 ref_in_start_stripe, 422 &io_info->pdBlock, 423 &io_info->devHandle, 424 pRAID_Context, map)) 425 return TRUE; 426 } 427 } 428 return TRUE; 429 } 430 431 void 432 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map, 433 struct LD_LOAD_BALANCE_INFO *lbInfo) 434 { 435 int ldCount; 436 u16 ld; 437 struct MR_LD_RAID *raid; 438 439 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) { 440 ld = MR_TargetIdToLdGet(ldCount, map); 441 if (ld >= MAX_LOGICAL_DRIVES) { 442 lbInfo[ldCount].loadBalanceFlag = 0; 443 continue; 444 } 445 446 raid = MR_LdRaidGet(ld, map); 447 448 /* Two drive Optimal RAID 1 */ 449 if ((raid->level == 1) && (raid->rowSize == 2) && 450 (raid->spanDepth == 1) && raid->ldState == 451 MR_LD_STATE_OPTIMAL) { 452 u32 pd, arRef; 453 454 lbInfo[ldCount].loadBalanceFlag = 1; 455 456 /* Get the array on which this span is present */ 457 arRef = MR_LdSpanArrayGet(ld, 0, map); 458 459 /* Get the Pd */ 460 pd = MR_ArPdGet(arRef, 0, map); 461 /* Get dev handle from Pd */ 462 lbInfo[ldCount].raid1DevHandle[0] = 463 MR_PdDevHandleGet(pd, map); 464 /* Get the Pd */ 465 pd = MR_ArPdGet(arRef, 1, map); 466 467 /* Get the dev handle from Pd */ 468 lbInfo[ldCount].raid1DevHandle[1] = 469 MR_PdDevHandleGet(pd, map); 470 } else 471 lbInfo[ldCount].loadBalanceFlag = 0; 472 } 473 } 474 475 u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block, 476 u32 count) 477 { 478 u16 pend0, pend1; 479 u64 diff0, diff1; 480 u8 bestArm; 481 482 /* get the pending cmds for the data and mirror arms */ 483 pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]); 484 pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]); 485 486 /* Determine the disk whose head is nearer to the req. block */ 487 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]); 488 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]); 489 bestArm = (diff0 <= diff1 ? 0 : 1); 490 491 if ((bestArm == arm && pend0 > pend1 + 16) || 492 (bestArm != arm && pend1 > pend0 + 16)) 493 bestArm ^= 1; 494 495 /* Update the last accessed block on the correct pd */ 496 lbInfo->last_accessed_block[bestArm] = block + count - 1; 497 498 return bestArm; 499 } 500 501 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo, 502 struct IO_REQUEST_INFO *io_info) 503 { 504 u8 arm, old_arm; 505 u16 devHandle; 506 507 old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1; 508 509 /* get best new arm */ 510 arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock, 511 io_info->numBlocks); 512 devHandle = lbInfo->raid1DevHandle[arm]; 513 atomic_inc(&lbInfo->scsi_pending_cmds[arm]); 514 515 return devHandle; 516 } 517