2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2012 LSI Corporation.
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.
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.
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
20 * FILE: megaraid_sas_fp.c
22 * Authors: LSI Corporation
27 * Send feedback to: <megaraidlinux@lsi.com>
29 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
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/uio.h>
43 #include <linux/uaccess.h>
45 #include <linux/compat.h>
46 #include <linux/blkdev.h>
47 #include <linux/poll.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_host.h>
54 #include "megaraid_sas_fusion.h"
55 #include "megaraid_sas.h"
56 #include <asm/div64.h>
58 #define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
59 #define MR_LD_STATE_OPTIMAL 3
64 #define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
65 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
66 #define SPAN_INVALID 0xff
69 void mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
70 struct LD_LOAD_BALANCE_INFO *lbInfo);
72 static void mr_update_span_set(struct MR_FW_RAID_MAP_ALL *map,
73 PLD_SPAN_INFO ldSpanInfo);
74 static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
75 u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
76 struct RAID_CONTEXT *pRAID_Context, struct MR_FW_RAID_MAP_ALL *map);
77 static u64 get_row_from_strip(struct megasas_instance *instance, u32 ld,
78 u64 strip, struct MR_FW_RAID_MAP_ALL *map);
80 u32 mega_mod64(u64 dividend, u32 divisor)
86 printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
88 remainder = do_div(d, divisor);
93 * @param dividend : Dividend
94 * @param divisor : Divisor
98 u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
104 printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");
107 remainder = do_div(d, divisor);
112 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
114 return &map->raidMap.ldSpanMap[ld].ldRaid;
117 static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
118 struct MR_FW_RAID_MAP_ALL
121 return &map->raidMap.ldSpanMap[ld].spanBlock[0];
124 static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_FW_RAID_MAP_ALL *map)
126 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
129 static u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_FW_RAID_MAP_ALL *map)
131 return map->raidMap.arMapInfo[ar].pd[arm];
134 static u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_FW_RAID_MAP_ALL *map)
136 return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
139 static u16 MR_PdDevHandleGet(u32 pd, struct MR_FW_RAID_MAP_ALL *map)
141 return map->raidMap.devHndlInfo[pd].curDevHdl;
144 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
146 return map->raidMap.ldSpanMap[ld].ldRaid.targetId;
149 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map)
151 return map->raidMap.ldTgtIdToLd[ldTgtId];
154 static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
155 struct MR_FW_RAID_MAP_ALL *map)
157 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
161 * This function will validate Map info data provided by FW
163 u8 MR_ValidateMapInfo(struct megasas_instance *instance)
165 struct fusion_context *fusion = instance->ctrl_context;
166 struct MR_FW_RAID_MAP_ALL *map = fusion->ld_map[(instance->map_id & 1)];
167 struct LD_LOAD_BALANCE_INFO *lbInfo = fusion->load_balance_info;
168 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
169 struct MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap;
171 if (pFwRaidMap->totalSize !=
172 (sizeof(struct MR_FW_RAID_MAP) -sizeof(struct MR_LD_SPAN_MAP) +
173 (sizeof(struct MR_LD_SPAN_MAP) *pFwRaidMap->ldCount))) {
174 printk(KERN_ERR "megasas: map info structure size 0x%x is not matching with ld count\n",
175 (unsigned int)((sizeof(struct MR_FW_RAID_MAP) -
176 sizeof(struct MR_LD_SPAN_MAP)) +
177 (sizeof(struct MR_LD_SPAN_MAP) *
178 pFwRaidMap->ldCount)));
179 printk(KERN_ERR "megasas: span map %x, pFwRaidMap->totalSize "
180 ": %x\n", (unsigned int)sizeof(struct MR_LD_SPAN_MAP),
181 pFwRaidMap->totalSize);
185 if (instance->UnevenSpanSupport)
186 mr_update_span_set(map, ldSpanInfo);
188 mr_update_load_balance_params(map, lbInfo);
193 u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
194 struct MR_FW_RAID_MAP_ALL *map)
196 struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
197 struct MR_QUAD_ELEMENT *quad;
198 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
201 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
203 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
204 quad = &pSpanBlock->block_span_info.quad[j];
208 if (quad->logStart <= row && row <= quad->logEnd &&
209 (mega_mod64(row-quad->logStart, quad->diff)) == 0) {
210 if (span_blk != NULL) {
214 (row-quad->logStart),
218 blk = (blk + quad->offsetInSpan) <<
230 ******************************************************************************
232 * Function to print info about span set created in driver from FW raid map
236 * ldSpanInfo - ldSpanInfo per HBA instance
239 static int getSpanInfo(struct MR_FW_RAID_MAP_ALL *map, PLD_SPAN_INFO ldSpanInfo)
244 struct MR_LD_RAID *raid;
245 LD_SPAN_SET *span_set;
246 struct MR_QUAD_ELEMENT *quad;
250 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
251 ld = MR_TargetIdToLdGet(ldCount, map);
252 if (ld >= MAX_LOGICAL_DRIVES)
254 raid = MR_LdRaidGet(ld, map);
255 dev_dbg(&instance->pdev->dev, "LD %x: span_depth=%x\n",
256 ld, raid->spanDepth);
257 for (span = 0; span < raid->spanDepth; span++)
258 dev_dbg(&instance->pdev->dev, "Span=%x,"
259 " number of quads=%x\n", span,
260 map->raidMap.ldSpanMap[ld].spanBlock[span].
261 block_span_info.noElements);
262 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
263 span_set = &(ldSpanInfo[ld].span_set[element]);
264 if (span_set->span_row_data_width == 0)
267 dev_dbg(&instance->pdev->dev, "Span Set %x:"
268 "width=%x, diff=%x\n", element,
269 (unsigned int)span_set->span_row_data_width,
270 (unsigned int)span_set->diff);
271 dev_dbg(&instance->pdev->dev, "logical LBA"
272 "start=0x%08lx, end=0x%08lx\n",
273 (long unsigned int)span_set->log_start_lba,
274 (long unsigned int)span_set->log_end_lba);
275 dev_dbg(&instance->pdev->dev, "span row start=0x%08lx,"
277 (long unsigned int)span_set->span_row_start,
278 (long unsigned int)span_set->span_row_end);
279 dev_dbg(&instance->pdev->dev, "data row start=0x%08lx,"
281 (long unsigned int)span_set->data_row_start,
282 (long unsigned int)span_set->data_row_end);
283 dev_dbg(&instance->pdev->dev, "data strip start=0x%08lx,"
285 (long unsigned int)span_set->data_strip_start,
286 (long unsigned int)span_set->data_strip_end);
288 for (span = 0; span < raid->spanDepth; span++) {
289 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
290 block_span_info.noElements >=
292 quad = &map->raidMap.ldSpanMap[ld].
293 spanBlock[span].block_span_info.
295 dev_dbg(&instance->pdev->dev, "Span=%x,"
296 "Quad=%x, diff=%x\n", span,
297 element, quad->diff);
298 dev_dbg(&instance->pdev->dev,
299 "offset_in_span=0x%08lx\n",
300 (long unsigned int)quad->offsetInSpan);
301 dev_dbg(&instance->pdev->dev,
302 "logical start=0x%08lx, end=0x%08lx\n",
303 (long unsigned int)quad->logStart,
304 (long unsigned int)quad->logEnd);
314 ******************************************************************************
316 * This routine calculates the Span block for given row using spanset.
319 * instance - HBA instance
320 * ld - Logical drive number
327 * block - Absolute Block number in the physical disk
328 * div_error - Devide error code.
331 u32 mr_spanset_get_span_block(struct megasas_instance *instance,
332 u32 ld, u64 row, u64 *span_blk, struct MR_FW_RAID_MAP_ALL *map)
334 struct fusion_context *fusion = instance->ctrl_context;
335 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
336 LD_SPAN_SET *span_set;
337 struct MR_QUAD_ELEMENT *quad;
339 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
341 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
342 span_set = &(ldSpanInfo[ld].span_set[info]);
344 if (span_set->span_row_data_width == 0)
347 if (row > span_set->data_row_end)
350 for (span = 0; span < raid->spanDepth; span++)
351 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
352 block_span_info.noElements >= info+1) {
353 quad = &map->raidMap.ldSpanMap[ld].
355 block_span_info.quad[info];
358 if (quad->logStart <= row &&
359 row <= quad->logEnd &&
360 (mega_mod64(row - quad->logStart,
362 if (span_blk != NULL) {
365 ((row - quad->logStart),
367 blk = (blk + quad->offsetInSpan)
368 << raid->stripeShift;
379 ******************************************************************************
381 * This routine calculates the row for given strip using spanset.
384 * instance - HBA instance
385 * ld - Logical drive number
391 * row - row associated with strip
394 static u64 get_row_from_strip(struct megasas_instance *instance,
395 u32 ld, u64 strip, struct MR_FW_RAID_MAP_ALL *map)
397 struct fusion_context *fusion = instance->ctrl_context;
398 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
399 LD_SPAN_SET *span_set;
400 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
401 u32 info, strip_offset, span, span_offset;
402 u64 span_set_Strip, span_set_Row, retval;
404 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
405 span_set = &(ldSpanInfo[ld].span_set[info]);
407 if (span_set->span_row_data_width == 0)
409 if (strip > span_set->data_strip_end)
412 span_set_Strip = strip - span_set->data_strip_start;
413 strip_offset = mega_mod64(span_set_Strip,
414 span_set->span_row_data_width);
415 span_set_Row = mega_div64_32(span_set_Strip,
416 span_set->span_row_data_width) * span_set->diff;
417 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
418 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
419 block_span_info.noElements >= info+1) {
421 span_set->strip_offset[span])
427 dev_info(&instance->pdev->dev, "Strip 0x%llx,"
428 "span_set_Strip 0x%llx, span_set_Row 0x%llx"
429 "data width 0x%llx span offset 0x%x\n", strip,
430 (unsigned long long)span_set_Strip,
431 (unsigned long long)span_set_Row,
432 (unsigned long long)span_set->span_row_data_width,
434 dev_info(&instance->pdev->dev, "For strip 0x%llx"
435 "row is 0x%llx\n", strip,
436 (unsigned long long) span_set->data_row_start +
437 (unsigned long long) span_set_Row + (span_offset - 1));
439 retval = (span_set->data_row_start + span_set_Row +
448 ******************************************************************************
450 * This routine calculates the Start Strip for given row using spanset.
453 * instance - HBA instance
454 * ld - Logical drive number
460 * Strip - Start strip associated with row
463 static u64 get_strip_from_row(struct megasas_instance *instance,
464 u32 ld, u64 row, struct MR_FW_RAID_MAP_ALL *map)
466 struct fusion_context *fusion = instance->ctrl_context;
467 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
468 LD_SPAN_SET *span_set;
469 struct MR_QUAD_ELEMENT *quad;
470 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
474 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
475 span_set = &(ldSpanInfo[ld].span_set[info]);
477 if (span_set->span_row_data_width == 0)
479 if (row > span_set->data_row_end)
482 for (span = 0; span < raid->spanDepth; span++)
483 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
484 block_span_info.noElements >= info+1) {
485 quad = &map->raidMap.ldSpanMap[ld].
486 spanBlock[span].block_span_info.quad[info];
487 if (quad->logStart <= row &&
488 row <= quad->logEnd &&
489 mega_mod64((row - quad->logStart),
491 strip = mega_div64_32
492 (((row - span_set->data_row_start)
495 strip *= span_set->span_row_data_width;
496 strip += span_set->data_strip_start;
497 strip += span_set->strip_offset[span];
502 dev_err(&instance->pdev->dev, "get_strip_from_row"
503 "returns invalid strip for ld=%x, row=%lx\n",
504 ld, (long unsigned int)row);
509 ******************************************************************************
511 * This routine calculates the Physical Arm for given strip using spanset.
514 * instance - HBA instance
515 * ld - Logical drive number
521 * Phys Arm - Phys Arm associated with strip
524 static u32 get_arm_from_strip(struct megasas_instance *instance,
525 u32 ld, u64 strip, struct MR_FW_RAID_MAP_ALL *map)
527 struct fusion_context *fusion = instance->ctrl_context;
528 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
529 LD_SPAN_SET *span_set;
530 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
531 u32 info, strip_offset, span, span_offset, retval;
533 for (info = 0 ; info < MAX_QUAD_DEPTH; info++) {
534 span_set = &(ldSpanInfo[ld].span_set[info]);
536 if (span_set->span_row_data_width == 0)
538 if (strip > span_set->data_strip_end)
541 strip_offset = (uint)mega_mod64
542 ((strip - span_set->data_strip_start),
543 span_set->span_row_data_width);
545 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
546 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
547 block_span_info.noElements >= info+1) {
549 span_set->strip_offset[span])
551 span_set->strip_offset[span];
556 dev_info(&instance->pdev->dev, "get_arm_from_strip:"
557 "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld,
558 (long unsigned int)strip, (strip_offset - span_offset));
560 retval = (strip_offset - span_offset);
564 dev_err(&instance->pdev->dev, "get_arm_from_strip"
565 "returns invalid arm for ld=%x strip=%lx\n",
566 ld, (long unsigned int)strip);
571 /* This Function will return Phys arm */
572 u8 get_arm(struct megasas_instance *instance, u32 ld, u8 span, u64 stripe,
573 struct MR_FW_RAID_MAP_ALL *map)
575 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
576 /* Need to check correct default value */
579 switch (raid->level) {
583 arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
586 /* start with logical arm */
587 arm = get_arm_from_strip(instance, ld, stripe, map);
598 ******************************************************************************
600 * This routine calculates the arm, span and block for the specified stripe and
601 * reference in stripe using spanset
605 * ld - Logical drive number
606 * stripRow - Stripe number
607 * stripRef - Reference in stripe
612 * block - Absolute Block number in the physical disk
614 static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
615 u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
616 struct RAID_CONTEXT *pRAID_Context,
617 struct MR_FW_RAID_MAP_ALL *map)
619 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
625 u64 *pdBlock = &io_info->pdBlock;
626 u16 *pDevHandle = &io_info->devHandle;
627 u32 logArm, rowMod, armQ, arm;
629 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER ||
630 instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
633 /*Get row and span from io_info for Uneven Span IO.*/
634 row = io_info->start_row;
635 span = io_info->start_span;
638 if (raid->level == 6) {
639 logArm = get_arm_from_strip(instance, ld, stripRow, map);
642 rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
643 armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
644 arm = armQ + 1 + logArm;
645 if (arm >= SPAN_ROW_SIZE(map, ld, span))
646 arm -= SPAN_ROW_SIZE(map, ld, span);
649 /* Calculate the arm */
650 physArm = get_arm(instance, ld, span, stripRow, map);
654 arRef = MR_LdSpanArrayGet(ld, span, map);
655 pd = MR_ArPdGet(arRef, physArm, map);
657 if (pd != MR_PD_INVALID)
658 *pDevHandle = MR_PdDevHandleGet(pd, map);
660 *pDevHandle = MR_PD_INVALID;
661 if ((raid->level >= 5) &&
662 (!do_invader || (do_invader &&
663 (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
664 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
665 else if (raid->level == 1) {
666 pd = MR_ArPdGet(arRef, physArm + 1, map);
667 if (pd != MR_PD_INVALID)
668 *pDevHandle = MR_PdDevHandleGet(pd, map);
672 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
673 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
679 ******************************************************************************
681 * This routine calculates the arm, span and block for the specified stripe and
682 * reference in stripe.
686 * ld - Logical drive number
687 * stripRow - Stripe number
688 * stripRef - Reference in stripe
693 * block - Absolute Block number in the physical disk
695 u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
696 u16 stripRef, struct IO_REQUEST_INFO *io_info,
697 struct RAID_CONTEXT *pRAID_Context,
698 struct MR_FW_RAID_MAP_ALL *map)
700 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
706 u64 *pdBlock = &io_info->pdBlock;
707 u16 *pDevHandle = &io_info->devHandle;
709 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER ||
710 instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
713 row = mega_div64_32(stripRow, raid->rowDataSize);
715 if (raid->level == 6) {
716 /* logical arm within row */
717 u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
718 u32 rowMod, armQ, arm;
720 if (raid->rowSize == 0)
722 /* get logical row mod */
723 rowMod = mega_mod64(row, raid->rowSize);
724 armQ = raid->rowSize-1-rowMod; /* index of Q drive */
725 arm = armQ+1+logArm; /* data always logically follows Q */
726 if (arm >= raid->rowSize) /* handle wrap condition */
727 arm -= raid->rowSize;
730 if (raid->modFactor == 0)
732 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
737 if (raid->spanDepth == 1) {
739 *pdBlock = row << raid->stripeShift;
741 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map);
742 if (span == SPAN_INVALID)
746 /* Get the array on which this span is present */
747 arRef = MR_LdSpanArrayGet(ld, span, map);
748 pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
750 if (pd != MR_PD_INVALID)
751 /* Get dev handle from Pd. */
752 *pDevHandle = MR_PdDevHandleGet(pd, map);
754 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
755 if ((raid->level >= 5) &&
756 (!do_invader || (do_invader &&
757 (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
758 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
759 else if (raid->level == 1) {
760 /* Get alternate Pd. */
761 pd = MR_ArPdGet(arRef, physArm + 1, map);
762 if (pd != MR_PD_INVALID)
763 /* Get dev handle from Pd */
764 *pDevHandle = MR_PdDevHandleGet(pd, map);
768 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
769 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
775 ******************************************************************************
777 * MR_BuildRaidContext function
779 * This function will initiate command processing. The start/end row and strip
780 * information is calculated then the lock is acquired.
781 * This function will return 0 if region lock was acquired OR return num strips
784 MR_BuildRaidContext(struct megasas_instance *instance,
785 struct IO_REQUEST_INFO *io_info,
786 struct RAID_CONTEXT *pRAID_Context,
787 struct MR_FW_RAID_MAP_ALL *map)
789 struct MR_LD_RAID *raid;
790 u32 ld, stripSize, stripe_mask;
791 u64 endLba, endStrip, endRow, start_row, start_strip;
794 u8 num_strips, numRows;
795 u16 ref_in_start_stripe, ref_in_end_stripe;
797 u32 numBlocks, ldTgtId;
800 u8 startlba_span = SPAN_INVALID;
801 u64 *pdBlock = &io_info->pdBlock;
803 ldStartBlock = io_info->ldStartBlock;
804 numBlocks = io_info->numBlocks;
805 ldTgtId = io_info->ldTgtId;
806 isRead = io_info->isRead;
807 io_info->IoforUnevenSpan = 0;
808 io_info->start_span = SPAN_INVALID;
810 ld = MR_TargetIdToLdGet(ldTgtId, map);
811 raid = MR_LdRaidGet(ld, map);
814 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
817 if (raid->rowDataSize == 0) {
818 if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
820 else if (instance->UnevenSpanSupport) {
821 io_info->IoforUnevenSpan = 1;
823 dev_info(&instance->pdev->dev,
824 "raid->rowDataSize is 0, but has SPAN[0]"
825 "rowDataSize = 0x%0x,"
826 "but there is _NO_ UnevenSpanSupport\n",
827 MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
832 stripSize = 1 << raid->stripeShift;
833 stripe_mask = stripSize-1;
837 * calculate starting row and stripe, and number of strips and rows
839 start_strip = ldStartBlock >> raid->stripeShift;
840 ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
841 endLba = ldStartBlock + numBlocks - 1;
842 ref_in_end_stripe = (u16)(endLba & stripe_mask);
843 endStrip = endLba >> raid->stripeShift;
844 num_strips = (u8)(endStrip - start_strip + 1); /* End strip */
846 if (io_info->IoforUnevenSpan) {
847 start_row = get_row_from_strip(instance, ld, start_strip, map);
848 endRow = get_row_from_strip(instance, ld, endStrip, map);
849 if (start_row == -1ULL || endRow == -1ULL) {
850 dev_info(&instance->pdev->dev, "return from %s %d."
851 "Send IO w/o region lock.\n",
856 if (raid->spanDepth == 1) {
858 *pdBlock = start_row << raid->stripeShift;
860 startlba_span = (u8)mr_spanset_get_span_block(instance,
861 ld, start_row, pdBlock, map);
862 if (startlba_span == SPAN_INVALID) {
863 dev_info(&instance->pdev->dev, "return from %s %d"
864 "for row 0x%llx,start strip %llx"
865 "endSrip %llx\n", __func__, __LINE__,
866 (unsigned long long)start_row,
867 (unsigned long long)start_strip,
868 (unsigned long long)endStrip);
871 io_info->start_span = startlba_span;
872 io_info->start_row = start_row;
874 dev_dbg(&instance->pdev->dev, "Check Span number from %s %d"
875 "for row 0x%llx, start strip 0x%llx end strip 0x%llx"
876 " span 0x%x\n", __func__, __LINE__,
877 (unsigned long long)start_row,
878 (unsigned long long)start_strip,
879 (unsigned long long)endStrip, startlba_span);
880 dev_dbg(&instance->pdev->dev, "start_row 0x%llx endRow 0x%llx"
881 "Start span 0x%x\n", (unsigned long long)start_row,
882 (unsigned long long)endRow, startlba_span);
885 start_row = mega_div64_32(start_strip, raid->rowDataSize);
886 endRow = mega_div64_32(endStrip, raid->rowDataSize);
888 numRows = (u8)(endRow - start_row + 1);
891 * calculate region info.
894 /* assume region is at the start of the first row */
895 regStart = start_row << raid->stripeShift;
896 /* assume this IO needs the full row - we'll adjust if not true */
899 /* Check if we can send this I/O via FastPath */
900 if (raid->capability.fpCapable) {
902 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
903 ((num_strips == 1) ||
905 fpReadAcrossStripe));
907 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
908 ((num_strips == 1) ||
910 fpWriteAcrossStripe));
912 io_info->fpOkForIo = FALSE;
915 /* single-strip IOs can always lock only the data needed */
916 if (num_strips == 1) {
917 regStart += ref_in_start_stripe;
920 /* multi-strip IOs always need to full stripe locked */
921 } else if (io_info->IoforUnevenSpan == 0) {
923 * For Even span region lock optimization.
924 * If the start strip is the last in the start row
926 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
927 regStart += ref_in_start_stripe;
928 /* initialize count to sectors from startref to end
930 regSize = stripSize - ref_in_start_stripe;
933 /* add complete rows in the middle of the transfer */
935 regSize += (numRows-2) << raid->stripeShift;
937 /* if IO ends within first strip of last row*/
938 if (endStrip == endRow*raid->rowDataSize)
939 regSize += ref_in_end_stripe+1;
941 regSize += stripSize;
944 * For Uneven span region lock optimization.
945 * If the start strip is the last in the start row
947 if (start_strip == (get_strip_from_row(instance, ld, start_row, map) +
948 SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
949 regStart += ref_in_start_stripe;
950 /* initialize count to sectors from
951 * startRef to end of strip
953 regSize = stripSize - ref_in_start_stripe;
955 /* Add complete rows in the middle of the transfer*/
958 /* Add complete rows in the middle of the transfer*/
959 regSize += (numRows-2) << raid->stripeShift;
961 /* if IO ends within first strip of last row */
962 if (endStrip == get_strip_from_row(instance, ld, endRow, map))
963 regSize += ref_in_end_stripe + 1;
965 regSize += stripSize;
968 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
969 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
970 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
971 pRAID_Context->regLockFlags = (isRead) ?
972 raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
974 pRAID_Context->regLockFlags = (isRead) ?
975 REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
976 pRAID_Context->VirtualDiskTgtId = raid->targetId;
977 pRAID_Context->regLockRowLBA = regStart;
978 pRAID_Context->regLockLength = regSize;
979 pRAID_Context->configSeqNum = raid->seqNum;
981 /*Get Phy Params only if FP capable, or else leave it to MR firmware
982 to do the calculation.*/
983 if (io_info->fpOkForIo) {
984 retval = io_info->IoforUnevenSpan ?
985 mr_spanset_get_phy_params(instance, ld,
986 start_strip, ref_in_start_stripe,
987 io_info, pRAID_Context, map) :
988 MR_GetPhyParams(instance, ld, start_strip,
989 ref_in_start_stripe, io_info,
991 /* If IO on an invalid Pd, then FP is not possible.*/
992 if (io_info->devHandle == MR_PD_INVALID)
993 io_info->fpOkForIo = FALSE;
997 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
998 retval = io_info->IoforUnevenSpan ?
999 mr_spanset_get_phy_params(instance, ld,
1000 start_strip + stripIdx,
1001 ref_in_start_stripe, io_info,
1002 pRAID_Context, map) :
1003 MR_GetPhyParams(instance, ld,
1004 start_strip + stripIdx, ref_in_start_stripe,
1005 io_info, pRAID_Context, map);
1012 /* Just for testing what arm we get for strip.*/
1013 if (io_info->IoforUnevenSpan)
1014 get_arm_from_strip(instance, ld, start_strip, map);
1020 ******************************************************************************
1022 * This routine pepare spanset info from Valid Raid map and store it into
1023 * local copy of ldSpanInfo per instance data structure.
1027 * ldSpanInfo - ldSpanInfo per HBA instance
1030 void mr_update_span_set(struct MR_FW_RAID_MAP_ALL *map,
1031 PLD_SPAN_INFO ldSpanInfo)
1034 u32 element, span_row_width;
1036 struct MR_LD_RAID *raid;
1037 LD_SPAN_SET *span_set, *span_set_prev;
1038 struct MR_QUAD_ELEMENT *quad;
1043 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
1044 ld = MR_TargetIdToLdGet(ldCount, map);
1045 if (ld >= MAX_LOGICAL_DRIVES)
1047 raid = MR_LdRaidGet(ld, map);
1048 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
1049 for (span = 0; span < raid->spanDepth; span++) {
1050 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
1051 block_span_info.noElements <
1054 span_set = &(ldSpanInfo[ld].span_set[element]);
1055 quad = &map->raidMap.ldSpanMap[ld].
1056 spanBlock[span].block_span_info.
1059 span_set->diff = quad->diff;
1061 for (count = 0, span_row_width = 0;
1062 count < raid->spanDepth; count++) {
1063 if (map->raidMap.ldSpanMap[ld].
1066 noElements >= element + 1) {
1067 span_set->strip_offset[count] =
1071 (ld, count, map)->spanRowDataSize;
1072 printk(KERN_INFO "megasas:"
1073 "span %x rowDataSize %x\n",
1074 count, MR_LdSpanPtrGet
1075 (ld, count, map)->spanRowDataSize);
1079 span_set->span_row_data_width = span_row_width;
1080 span_row = mega_div64_32(((quad->logEnd -
1081 quad->logStart) + quad->diff),
1085 span_set->log_start_lba = 0;
1086 span_set->log_end_lba =
1087 ((span_row << raid->stripeShift)
1088 * span_row_width) - 1;
1090 span_set->span_row_start = 0;
1091 span_set->span_row_end = span_row - 1;
1093 span_set->data_strip_start = 0;
1094 span_set->data_strip_end =
1095 (span_row * span_row_width) - 1;
1097 span_set->data_row_start = 0;
1098 span_set->data_row_end =
1099 (span_row * quad->diff) - 1;
1101 span_set_prev = &(ldSpanInfo[ld].
1102 span_set[element - 1]);
1103 span_set->log_start_lba =
1104 span_set_prev->log_end_lba + 1;
1105 span_set->log_end_lba =
1106 span_set->log_start_lba +
1107 ((span_row << raid->stripeShift)
1108 * span_row_width) - 1;
1110 span_set->span_row_start =
1111 span_set_prev->span_row_end + 1;
1112 span_set->span_row_end =
1113 span_set->span_row_start + span_row - 1;
1115 span_set->data_strip_start =
1116 span_set_prev->data_strip_end + 1;
1117 span_set->data_strip_end =
1118 span_set->data_strip_start +
1119 (span_row * span_row_width) - 1;
1121 span_set->data_row_start =
1122 span_set_prev->data_row_end + 1;
1123 span_set->data_row_end =
1124 span_set->data_row_start +
1125 (span_row * quad->diff) - 1;
1129 if (span == raid->spanDepth)
1134 getSpanInfo(map, ldSpanInfo);
1140 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
1141 struct LD_LOAD_BALANCE_INFO *lbInfo)
1145 struct MR_LD_RAID *raid;
1147 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
1148 ld = MR_TargetIdToLdGet(ldCount, map);
1149 if (ld >= MAX_LOGICAL_DRIVES) {
1150 lbInfo[ldCount].loadBalanceFlag = 0;
1154 raid = MR_LdRaidGet(ld, map);
1156 /* Two drive Optimal RAID 1 */
1157 if ((raid->level == 1) && (raid->rowSize == 2) &&
1158 (raid->spanDepth == 1) && raid->ldState ==
1159 MR_LD_STATE_OPTIMAL) {
1162 lbInfo[ldCount].loadBalanceFlag = 1;
1164 /* Get the array on which this span is present */
1165 arRef = MR_LdSpanArrayGet(ld, 0, map);
1168 pd = MR_ArPdGet(arRef, 0, map);
1169 /* Get dev handle from Pd */
1170 lbInfo[ldCount].raid1DevHandle[0] =
1171 MR_PdDevHandleGet(pd, map);
1173 pd = MR_ArPdGet(arRef, 1, map);
1175 /* Get the dev handle from Pd */
1176 lbInfo[ldCount].raid1DevHandle[1] =
1177 MR_PdDevHandleGet(pd, map);
1179 lbInfo[ldCount].loadBalanceFlag = 0;
1183 u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block,
1190 /* get the pending cmds for the data and mirror arms */
1191 pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]);
1192 pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]);
1194 /* Determine the disk whose head is nearer to the req. block */
1195 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
1196 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
1197 bestArm = (diff0 <= diff1 ? 0 : 1);
1199 /*Make balance count from 16 to 4 to keep driver in sync with Firmware*/
1200 if ((bestArm == arm && pend0 > pend1 + 4) ||
1201 (bestArm != arm && pend1 > pend0 + 4))
1204 /* Update the last accessed block on the correct pd */
1205 lbInfo->last_accessed_block[bestArm] = block + count - 1;
1210 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
1211 struct IO_REQUEST_INFO *io_info)
1216 old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
1218 /* get best new arm */
1219 arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
1220 io_info->numBlocks);
1221 devHandle = lbInfo->raid1DevHandle[arm];
1222 atomic_inc(&lbInfo->scsi_pending_cmds[arm]);