2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * 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/>.
20 * FILE: megaraid_sas_fp.c
22 * Authors: Avago Technologies
26 * Kashyap Desai <kashyap.desai@avagotech.com>
27 * Sumit Saxena <sumit.saxena@avagotech.com>
29 * Send feedback to: megaraidlinux.pdl@avagotech.com
31 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
32 * San Jose, California 95131
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/list.h>
39 #include <linux/moduleparam.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44 #include <linux/uio.h>
45 #include <linux/uaccess.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/poll.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_host.h>
56 #include "megaraid_sas_fusion.h"
57 #include "megaraid_sas.h"
58 #include <asm/div64.h>
60 #define LB_PENDING_CMDS_DEFAULT 4
61 static unsigned int lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
62 module_param(lb_pending_cmds, int, S_IRUGO);
63 MODULE_PARM_DESC(lb_pending_cmds, "Change raid-1 load balancing outstanding "
64 "threshold. Valid Values are 1-128. Default: 4");
67 #define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
68 #define MR_LD_STATE_OPTIMAL 3
81 #define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
82 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
83 #define SPAN_INVALID 0xff
86 static void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
87 PLD_SPAN_INFO ldSpanInfo);
88 static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
89 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);
94 u32 mega_mod64(u64 dividend, u32 divisor)
100 printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
102 remainder = do_div(d, divisor);
107 * @param dividend : Dividend
108 * @param divisor : Divisor
112 u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
118 printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");
121 remainder = do_div(d, divisor);
126 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
128 return &map->raidMap.ldSpanMap[ld].ldRaid;
131 static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
132 struct MR_DRV_RAID_MAP_ALL
135 return &map->raidMap.ldSpanMap[ld].spanBlock[0];
138 static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_DRV_RAID_MAP_ALL *map)
140 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
143 u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_DRV_RAID_MAP_ALL *map)
145 return le16_to_cpu(map->raidMap.arMapInfo[ar].pd[arm]);
148 u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_DRV_RAID_MAP_ALL *map)
150 return le16_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef);
153 __le16 MR_PdDevHandleGet(u32 pd, struct MR_DRV_RAID_MAP_ALL *map)
155 return map->raidMap.devHndlInfo[pd].curDevHdl;
158 u16 MR_GetLDTgtId(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
160 return le16_to_cpu(map->raidMap.ldSpanMap[ld].ldRaid.targetId);
163 u8 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_DRV_RAID_MAP_ALL *map)
165 return map->raidMap.ldTgtIdToLd[ldTgtId];
168 static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
169 struct MR_DRV_RAID_MAP_ALL *map)
171 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
175 * This function will Populate Driver Map using firmware raid map
177 void MR_PopulateDrvRaidMap(struct megasas_instance *instance)
179 struct fusion_context *fusion = instance->ctrl_context;
180 struct MR_FW_RAID_MAP_ALL *fw_map_old = NULL;
181 struct MR_FW_RAID_MAP *pFwRaidMap = NULL;
186 struct MR_DRV_RAID_MAP_ALL *drv_map =
187 fusion->ld_drv_map[(instance->map_id & 1)];
188 struct MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
190 if (instance->supportmax256vd) {
191 memcpy(fusion->ld_drv_map[instance->map_id & 1],
192 fusion->ld_map[instance->map_id & 1],
193 fusion->current_map_sz);
194 /* New Raid map will not set totalSize, so keep expected value
195 * for legacy code in ValidateMapInfo
197 pDrvRaidMap->totalSize =
198 cpu_to_le32(sizeof(struct MR_FW_RAID_MAP_EXT));
200 fw_map_old = (struct MR_FW_RAID_MAP_ALL *)
201 fusion->ld_map[(instance->map_id & 1)];
202 pFwRaidMap = &fw_map_old->raidMap;
203 ld_count = (u16)le32_to_cpu(pFwRaidMap->ldCount);
206 for (i = 0; i < ld_count; i++) {
207 dev_dbg(&instance->pdev->dev, "(%d) :Index 0x%x "
208 "Target Id 0x%x Seq Num 0x%x Size 0/%llx\n",
209 instance->unique_id, i,
210 fw_map_old->raidMap.ldSpanMap[i].ldRaid.targetId,
211 fw_map_old->raidMap.ldSpanMap[i].ldRaid.seqNum,
212 fw_map_old->raidMap.ldSpanMap[i].ldRaid.size);
216 memset(drv_map, 0, fusion->drv_map_sz);
217 pDrvRaidMap->totalSize = pFwRaidMap->totalSize;
218 pDrvRaidMap->ldCount = (__le16)cpu_to_le16(ld_count);
219 pDrvRaidMap->fpPdIoTimeoutSec = pFwRaidMap->fpPdIoTimeoutSec;
220 for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++)
221 pDrvRaidMap->ldTgtIdToLd[i] =
222 (u8)pFwRaidMap->ldTgtIdToLd[i];
223 for (i = (MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS);
224 i < MAX_LOGICAL_DRIVES_EXT; i++)
225 pDrvRaidMap->ldTgtIdToLd[i] = 0xff;
226 for (i = 0; i < ld_count; i++) {
227 pDrvRaidMap->ldSpanMap[i] = pFwRaidMap->ldSpanMap[i];
229 dev_dbg(&instance->pdev->dev,
230 "pFwRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
231 "pFwRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
233 pFwRaidMap->ldSpanMap[i].ldRaid.targetId,
234 pFwRaidMap->ldSpanMap[i].ldRaid.seqNum,
235 (u32)pFwRaidMap->ldSpanMap[i].ldRaid.rowSize);
236 dev_dbg(&instance->pdev->dev,
237 "pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
238 "pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
240 pDrvRaidMap->ldSpanMap[i].ldRaid.targetId,
241 pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum,
242 (u32)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);
243 dev_dbg(&instance->pdev->dev, "Driver raid map all %p "
244 "raid map %p LD RAID MAP %p/%p\n", drv_map,
245 pDrvRaidMap, &pFwRaidMap->ldSpanMap[i].ldRaid,
246 &pDrvRaidMap->ldSpanMap[i].ldRaid);
249 memcpy(pDrvRaidMap->arMapInfo, pFwRaidMap->arMapInfo,
250 sizeof(struct MR_ARRAY_INFO) * MAX_RAIDMAP_ARRAYS);
251 memcpy(pDrvRaidMap->devHndlInfo, pFwRaidMap->devHndlInfo,
252 sizeof(struct MR_DEV_HANDLE_INFO) *
253 MAX_RAIDMAP_PHYSICAL_DEVICES);
258 * This function will validate Map info data provided by FW
260 u8 MR_ValidateMapInfo(struct megasas_instance *instance)
262 struct fusion_context *fusion;
263 struct MR_DRV_RAID_MAP_ALL *drv_map;
264 struct MR_DRV_RAID_MAP *pDrvRaidMap;
265 struct LD_LOAD_BALANCE_INFO *lbInfo;
266 PLD_SPAN_INFO ldSpanInfo;
267 struct MR_LD_RAID *raid;
268 u16 ldCount, num_lds;
273 MR_PopulateDrvRaidMap(instance);
275 fusion = instance->ctrl_context;
276 drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
277 pDrvRaidMap = &drv_map->raidMap;
279 lbInfo = fusion->load_balance_info;
280 ldSpanInfo = fusion->log_to_span;
282 if (instance->supportmax256vd)
283 expected_size = sizeof(struct MR_FW_RAID_MAP_EXT);
286 (sizeof(struct MR_FW_RAID_MAP) - sizeof(struct MR_LD_SPAN_MAP) +
287 (sizeof(struct MR_LD_SPAN_MAP) * le16_to_cpu(pDrvRaidMap->ldCount)));
289 if (le32_to_cpu(pDrvRaidMap->totalSize) != expected_size) {
290 dev_err(&instance->pdev->dev, "map info structure size 0x%x is not matching with ld count\n",
291 (unsigned int) expected_size);
292 dev_err(&instance->pdev->dev, "megasas: span map %x, pDrvRaidMap->totalSize : %x\n",
293 (unsigned int)sizeof(struct MR_LD_SPAN_MAP),
294 le32_to_cpu(pDrvRaidMap->totalSize));
298 if (instance->UnevenSpanSupport)
299 mr_update_span_set(drv_map, ldSpanInfo);
301 mr_update_load_balance_params(drv_map, lbInfo);
303 num_lds = le16_to_cpu(drv_map->raidMap.ldCount);
305 /*Convert Raid capability values to CPU arch */
306 for (ldCount = 0; ldCount < num_lds; ldCount++) {
307 ld = MR_TargetIdToLdGet(ldCount, drv_map);
308 raid = MR_LdRaidGet(ld, drv_map);
309 le32_to_cpus((u32 *)&raid->capability);
315 u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
316 struct MR_DRV_RAID_MAP_ALL *map)
318 struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
319 struct MR_QUAD_ELEMENT *quad;
320 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
323 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
325 for (j = 0; j < le32_to_cpu(pSpanBlock->block_span_info.noElements); j++) {
326 quad = &pSpanBlock->block_span_info.quad[j];
328 if (le32_to_cpu(quad->diff) == 0)
330 if (le64_to_cpu(quad->logStart) <= row && row <=
331 le64_to_cpu(quad->logEnd) && (mega_mod64(row - le64_to_cpu(quad->logStart),
332 le32_to_cpu(quad->diff))) == 0) {
333 if (span_blk != NULL) {
335 blk = mega_div64_32((row-le64_to_cpu(quad->logStart)), le32_to_cpu(quad->diff));
338 blk = (blk + le64_to_cpu(quad->offsetInSpan)) << raid->stripeShift;
349 ******************************************************************************
351 * Function to print info about span set created in driver from FW raid map
355 * ldSpanInfo - ldSpanInfo per HBA instance
358 static int getSpanInfo(struct MR_DRV_RAID_MAP_ALL *map,
359 PLD_SPAN_INFO ldSpanInfo)
364 struct MR_LD_RAID *raid;
365 LD_SPAN_SET *span_set;
366 struct MR_QUAD_ELEMENT *quad;
370 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
371 ld = MR_TargetIdToLdGet(ldCount, map);
372 if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
374 raid = MR_LdRaidGet(ld, map);
375 dev_dbg(&instance->pdev->dev, "LD %x: span_depth=%x\n",
376 ld, raid->spanDepth);
377 for (span = 0; span < raid->spanDepth; span++)
378 dev_dbg(&instance->pdev->dev, "Span=%x,"
379 " number of quads=%x\n", span,
380 le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
381 block_span_info.noElements));
382 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
383 span_set = &(ldSpanInfo[ld].span_set[element]);
384 if (span_set->span_row_data_width == 0)
387 dev_dbg(&instance->pdev->dev, "Span Set %x:"
388 "width=%x, diff=%x\n", element,
389 (unsigned int)span_set->span_row_data_width,
390 (unsigned int)span_set->diff);
391 dev_dbg(&instance->pdev->dev, "logical LBA"
392 "start=0x%08lx, end=0x%08lx\n",
393 (long unsigned int)span_set->log_start_lba,
394 (long unsigned int)span_set->log_end_lba);
395 dev_dbg(&instance->pdev->dev, "span row start=0x%08lx,"
397 (long unsigned int)span_set->span_row_start,
398 (long unsigned int)span_set->span_row_end);
399 dev_dbg(&instance->pdev->dev, "data row start=0x%08lx,"
401 (long unsigned int)span_set->data_row_start,
402 (long unsigned int)span_set->data_row_end);
403 dev_dbg(&instance->pdev->dev, "data strip start=0x%08lx,"
405 (long unsigned int)span_set->data_strip_start,
406 (long unsigned int)span_set->data_strip_end);
408 for (span = 0; span < raid->spanDepth; span++) {
409 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
410 block_span_info.noElements) >=
412 quad = &map->raidMap.ldSpanMap[ld].
413 spanBlock[span].block_span_info.
415 dev_dbg(&instance->pdev->dev, "Span=%x,"
416 "Quad=%x, diff=%x\n", span,
417 element, le32_to_cpu(quad->diff));
418 dev_dbg(&instance->pdev->dev,
419 "offset_in_span=0x%08lx\n",
420 (long unsigned int)le64_to_cpu(quad->offsetInSpan));
421 dev_dbg(&instance->pdev->dev,
422 "logical start=0x%08lx, end=0x%08lx\n",
423 (long unsigned int)le64_to_cpu(quad->logStart),
424 (long unsigned int)le64_to_cpu(quad->logEnd));
434 ******************************************************************************
436 * This routine calculates the Span block for given row using spanset.
439 * instance - HBA instance
440 * ld - Logical drive number
447 * block - Absolute Block number in the physical disk
448 * div_error - Devide error code.
451 u32 mr_spanset_get_span_block(struct megasas_instance *instance,
452 u32 ld, u64 row, u64 *span_blk, struct MR_DRV_RAID_MAP_ALL *map)
454 struct fusion_context *fusion = instance->ctrl_context;
455 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
456 LD_SPAN_SET *span_set;
457 struct MR_QUAD_ELEMENT *quad;
459 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
461 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
462 span_set = &(ldSpanInfo[ld].span_set[info]);
464 if (span_set->span_row_data_width == 0)
467 if (row > span_set->data_row_end)
470 for (span = 0; span < raid->spanDepth; span++)
471 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
472 block_span_info.noElements) >= info+1) {
473 quad = &map->raidMap.ldSpanMap[ld].
475 block_span_info.quad[info];
476 if (le32_to_cpu(quad->diff) == 0)
478 if (le64_to_cpu(quad->logStart) <= row &&
479 row <= le64_to_cpu(quad->logEnd) &&
480 (mega_mod64(row - le64_to_cpu(quad->logStart),
481 le32_to_cpu(quad->diff))) == 0) {
482 if (span_blk != NULL) {
485 ((row - le64_to_cpu(quad->logStart)),
486 le32_to_cpu(quad->diff));
487 blk = (blk + le64_to_cpu(quad->offsetInSpan))
488 << raid->stripeShift;
499 ******************************************************************************
501 * This routine calculates the row for given strip using spanset.
504 * instance - HBA instance
505 * ld - Logical drive number
511 * row - row associated with strip
514 static u64 get_row_from_strip(struct megasas_instance *instance,
515 u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
517 struct fusion_context *fusion = instance->ctrl_context;
518 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
519 LD_SPAN_SET *span_set;
520 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
521 u32 info, strip_offset, span, span_offset;
522 u64 span_set_Strip, span_set_Row, retval;
524 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
525 span_set = &(ldSpanInfo[ld].span_set[info]);
527 if (span_set->span_row_data_width == 0)
529 if (strip > span_set->data_strip_end)
532 span_set_Strip = strip - span_set->data_strip_start;
533 strip_offset = mega_mod64(span_set_Strip,
534 span_set->span_row_data_width);
535 span_set_Row = mega_div64_32(span_set_Strip,
536 span_set->span_row_data_width) * span_set->diff;
537 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
538 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
539 block_span_info.noElements) >= info+1) {
541 span_set->strip_offset[span])
547 dev_info(&instance->pdev->dev, "Strip 0x%llx,"
548 "span_set_Strip 0x%llx, span_set_Row 0x%llx"
549 "data width 0x%llx span offset 0x%x\n", strip,
550 (unsigned long long)span_set_Strip,
551 (unsigned long long)span_set_Row,
552 (unsigned long long)span_set->span_row_data_width,
554 dev_info(&instance->pdev->dev, "For strip 0x%llx"
555 "row is 0x%llx\n", strip,
556 (unsigned long long) span_set->data_row_start +
557 (unsigned long long) span_set_Row + (span_offset - 1));
559 retval = (span_set->data_row_start + span_set_Row +
568 ******************************************************************************
570 * This routine calculates the Start Strip for given row using spanset.
573 * instance - HBA instance
574 * ld - Logical drive number
580 * Strip - Start strip associated with row
583 static u64 get_strip_from_row(struct megasas_instance *instance,
584 u32 ld, u64 row, struct MR_DRV_RAID_MAP_ALL *map)
586 struct fusion_context *fusion = instance->ctrl_context;
587 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
588 LD_SPAN_SET *span_set;
589 struct MR_QUAD_ELEMENT *quad;
590 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
594 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
595 span_set = &(ldSpanInfo[ld].span_set[info]);
597 if (span_set->span_row_data_width == 0)
599 if (row > span_set->data_row_end)
602 for (span = 0; span < raid->spanDepth; span++)
603 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
604 block_span_info.noElements) >= info+1) {
605 quad = &map->raidMap.ldSpanMap[ld].
606 spanBlock[span].block_span_info.quad[info];
607 if (le64_to_cpu(quad->logStart) <= row &&
608 row <= le64_to_cpu(quad->logEnd) &&
609 mega_mod64((row - le64_to_cpu(quad->logStart)),
610 le32_to_cpu(quad->diff)) == 0) {
611 strip = mega_div64_32
612 (((row - span_set->data_row_start)
613 - le64_to_cpu(quad->logStart)),
614 le32_to_cpu(quad->diff));
615 strip *= span_set->span_row_data_width;
616 strip += span_set->data_strip_start;
617 strip += span_set->strip_offset[span];
622 dev_err(&instance->pdev->dev, "get_strip_from_row"
623 "returns invalid strip for ld=%x, row=%lx\n",
624 ld, (long unsigned int)row);
629 ******************************************************************************
631 * This routine calculates the Physical Arm for given strip using spanset.
634 * instance - HBA instance
635 * ld - Logical drive number
641 * Phys Arm - Phys Arm associated with strip
644 static u32 get_arm_from_strip(struct megasas_instance *instance,
645 u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
647 struct fusion_context *fusion = instance->ctrl_context;
648 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
649 LD_SPAN_SET *span_set;
650 PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
651 u32 info, strip_offset, span, span_offset, retval;
653 for (info = 0 ; info < MAX_QUAD_DEPTH; info++) {
654 span_set = &(ldSpanInfo[ld].span_set[info]);
656 if (span_set->span_row_data_width == 0)
658 if (strip > span_set->data_strip_end)
661 strip_offset = (uint)mega_mod64
662 ((strip - span_set->data_strip_start),
663 span_set->span_row_data_width);
665 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
666 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
667 block_span_info.noElements) >= info+1) {
669 span_set->strip_offset[span])
671 span_set->strip_offset[span];
676 dev_info(&instance->pdev->dev, "get_arm_from_strip:"
677 "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld,
678 (long unsigned int)strip, (strip_offset - span_offset));
680 retval = (strip_offset - span_offset);
684 dev_err(&instance->pdev->dev, "get_arm_from_strip"
685 "returns invalid arm for ld=%x strip=%lx\n",
686 ld, (long unsigned int)strip);
691 /* This Function will return Phys arm */
692 u8 get_arm(struct megasas_instance *instance, u32 ld, u8 span, u64 stripe,
693 struct MR_DRV_RAID_MAP_ALL *map)
695 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
696 /* Need to check correct default value */
699 switch (raid->level) {
703 arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
706 /* start with logical arm */
707 arm = get_arm_from_strip(instance, ld, stripe, map);
718 ******************************************************************************
720 * This routine calculates the arm, span and block for the specified stripe and
721 * reference in stripe using spanset
725 * ld - Logical drive number
726 * stripRow - Stripe number
727 * stripRef - Reference in stripe
732 * block - Absolute Block number in the physical disk
734 static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
735 u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
736 struct RAID_CONTEXT *pRAID_Context,
737 struct MR_DRV_RAID_MAP_ALL *map)
739 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
744 u64 *pdBlock = &io_info->pdBlock;
745 __le16 *pDevHandle = &io_info->devHandle;
746 u32 logArm, rowMod, armQ, arm;
747 struct fusion_context *fusion;
749 fusion = instance->ctrl_context;
751 /*Get row and span from io_info for Uneven Span IO.*/
752 row = io_info->start_row;
753 span = io_info->start_span;
756 if (raid->level == 6) {
757 logArm = get_arm_from_strip(instance, ld, stripRow, map);
760 rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
761 armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
762 arm = armQ + 1 + logArm;
763 if (arm >= SPAN_ROW_SIZE(map, ld, span))
764 arm -= SPAN_ROW_SIZE(map, ld, span);
767 /* Calculate the arm */
768 physArm = get_arm(instance, ld, span, stripRow, map);
772 arRef = MR_LdSpanArrayGet(ld, span, map);
773 pd = MR_ArPdGet(arRef, physArm, map);
775 if (pd != MR_PD_INVALID)
776 *pDevHandle = MR_PdDevHandleGet(pd, map);
778 *pDevHandle = cpu_to_le16(MR_PD_INVALID);
779 if ((raid->level >= 5) &&
780 ((fusion->adapter_type == THUNDERBOLT_SERIES) ||
781 ((fusion->adapter_type == INVADER_SERIES) &&
782 (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
783 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
784 else if (raid->level == 1) {
785 physArm = physArm + 1;
786 pd = MR_ArPdGet(arRef, physArm, map);
787 if (pd != MR_PD_INVALID)
788 *pDevHandle = MR_PdDevHandleGet(pd, map);
792 *pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
793 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
795 io_info->span_arm = pRAID_Context->spanArm;
800 ******************************************************************************
802 * This routine calculates the arm, span and block for the specified stripe and
803 * reference in stripe.
807 * ld - Logical drive number
808 * stripRow - Stripe number
809 * stripRef - Reference in stripe
814 * block - Absolute Block number in the physical disk
816 u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
817 u16 stripRef, struct IO_REQUEST_INFO *io_info,
818 struct RAID_CONTEXT *pRAID_Context,
819 struct MR_DRV_RAID_MAP_ALL *map)
821 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
826 u64 *pdBlock = &io_info->pdBlock;
827 __le16 *pDevHandle = &io_info->devHandle;
828 struct fusion_context *fusion;
830 fusion = instance->ctrl_context;
833 row = mega_div64_32(stripRow, raid->rowDataSize);
835 if (raid->level == 6) {
836 /* logical arm within row */
837 u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
838 u32 rowMod, armQ, arm;
840 if (raid->rowSize == 0)
842 /* get logical row mod */
843 rowMod = mega_mod64(row, raid->rowSize);
844 armQ = raid->rowSize-1-rowMod; /* index of Q drive */
845 arm = armQ+1+logArm; /* data always logically follows Q */
846 if (arm >= raid->rowSize) /* handle wrap condition */
847 arm -= raid->rowSize;
850 if (raid->modFactor == 0)
852 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
857 if (raid->spanDepth == 1) {
859 *pdBlock = row << raid->stripeShift;
861 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map);
862 if (span == SPAN_INVALID)
866 /* Get the array on which this span is present */
867 arRef = MR_LdSpanArrayGet(ld, span, map);
868 pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
870 if (pd != MR_PD_INVALID)
871 /* Get dev handle from Pd. */
872 *pDevHandle = MR_PdDevHandleGet(pd, map);
874 /* set dev handle as invalid. */
875 *pDevHandle = cpu_to_le16(MR_PD_INVALID);
876 if ((raid->level >= 5) &&
877 ((fusion->adapter_type == THUNDERBOLT_SERIES) ||
878 ((fusion->adapter_type == INVADER_SERIES) &&
879 (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
880 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
881 else if (raid->level == 1) {
882 /* Get alternate Pd. */
883 physArm = physArm + 1;
884 pd = MR_ArPdGet(arRef, physArm, map);
885 if (pd != MR_PD_INVALID)
886 /* Get dev handle from Pd */
887 *pDevHandle = MR_PdDevHandleGet(pd, map);
891 *pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
892 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
894 io_info->span_arm = pRAID_Context->spanArm;
899 ******************************************************************************
901 * MR_BuildRaidContext function
903 * This function will initiate command processing. The start/end row and strip
904 * information is calculated then the lock is acquired.
905 * This function will return 0 if region lock was acquired OR return num strips
908 MR_BuildRaidContext(struct megasas_instance *instance,
909 struct IO_REQUEST_INFO *io_info,
910 struct RAID_CONTEXT *pRAID_Context,
911 struct MR_DRV_RAID_MAP_ALL *map, u8 **raidLUN)
913 struct fusion_context *fusion;
914 struct MR_LD_RAID *raid;
915 u32 ld, stripSize, stripe_mask;
916 u64 endLba, endStrip, endRow, start_row, start_strip;
919 u8 num_strips, numRows;
920 u16 ref_in_start_stripe, ref_in_end_stripe;
922 u32 numBlocks, ldTgtId;
925 u8 startlba_span = SPAN_INVALID;
926 u64 *pdBlock = &io_info->pdBlock;
928 ldStartBlock = io_info->ldStartBlock;
929 numBlocks = io_info->numBlocks;
930 ldTgtId = io_info->ldTgtId;
931 isRead = io_info->isRead;
932 io_info->IoforUnevenSpan = 0;
933 io_info->start_span = SPAN_INVALID;
934 fusion = instance->ctrl_context;
936 ld = MR_TargetIdToLdGet(ldTgtId, map);
937 raid = MR_LdRaidGet(ld, map);
940 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
943 if (raid->rowDataSize == 0) {
944 if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
946 else if (instance->UnevenSpanSupport) {
947 io_info->IoforUnevenSpan = 1;
949 dev_info(&instance->pdev->dev,
950 "raid->rowDataSize is 0, but has SPAN[0]"
951 "rowDataSize = 0x%0x,"
952 "but there is _NO_ UnevenSpanSupport\n",
953 MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
958 stripSize = 1 << raid->stripeShift;
959 stripe_mask = stripSize-1;
963 * calculate starting row and stripe, and number of strips and rows
965 start_strip = ldStartBlock >> raid->stripeShift;
966 ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
967 endLba = ldStartBlock + numBlocks - 1;
968 ref_in_end_stripe = (u16)(endLba & stripe_mask);
969 endStrip = endLba >> raid->stripeShift;
970 num_strips = (u8)(endStrip - start_strip + 1); /* End strip */
972 if (io_info->IoforUnevenSpan) {
973 start_row = get_row_from_strip(instance, ld, start_strip, map);
974 endRow = get_row_from_strip(instance, ld, endStrip, map);
975 if (start_row == -1ULL || endRow == -1ULL) {
976 dev_info(&instance->pdev->dev, "return from %s %d."
977 "Send IO w/o region lock.\n",
982 if (raid->spanDepth == 1) {
984 *pdBlock = start_row << raid->stripeShift;
986 startlba_span = (u8)mr_spanset_get_span_block(instance,
987 ld, start_row, pdBlock, map);
988 if (startlba_span == SPAN_INVALID) {
989 dev_info(&instance->pdev->dev, "return from %s %d"
990 "for row 0x%llx,start strip %llx"
991 "endSrip %llx\n", __func__, __LINE__,
992 (unsigned long long)start_row,
993 (unsigned long long)start_strip,
994 (unsigned long long)endStrip);
997 io_info->start_span = startlba_span;
998 io_info->start_row = start_row;
1000 dev_dbg(&instance->pdev->dev, "Check Span number from %s %d"
1001 "for row 0x%llx, start strip 0x%llx end strip 0x%llx"
1002 " span 0x%x\n", __func__, __LINE__,
1003 (unsigned long long)start_row,
1004 (unsigned long long)start_strip,
1005 (unsigned long long)endStrip, startlba_span);
1006 dev_dbg(&instance->pdev->dev, "start_row 0x%llx endRow 0x%llx"
1007 "Start span 0x%x\n", (unsigned long long)start_row,
1008 (unsigned long long)endRow, startlba_span);
1011 start_row = mega_div64_32(start_strip, raid->rowDataSize);
1012 endRow = mega_div64_32(endStrip, raid->rowDataSize);
1014 numRows = (u8)(endRow - start_row + 1);
1017 * calculate region info.
1020 /* assume region is at the start of the first row */
1021 regStart = start_row << raid->stripeShift;
1022 /* assume this IO needs the full row - we'll adjust if not true */
1023 regSize = stripSize;
1025 io_info->do_fp_rlbypass = raid->capability.fpBypassRegionLock;
1027 /* Check if we can send this I/O via FastPath */
1028 if (raid->capability.fpCapable) {
1030 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
1031 ((num_strips == 1) ||
1033 fpReadAcrossStripe));
1035 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
1036 ((num_strips == 1) ||
1038 fpWriteAcrossStripe));
1040 io_info->fpOkForIo = FALSE;
1043 /* single-strip IOs can always lock only the data needed */
1044 if (num_strips == 1) {
1045 regStart += ref_in_start_stripe;
1046 regSize = numBlocks;
1048 /* multi-strip IOs always need to full stripe locked */
1049 } else if (io_info->IoforUnevenSpan == 0) {
1051 * For Even span region lock optimization.
1052 * If the start strip is the last in the start row
1054 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
1055 regStart += ref_in_start_stripe;
1056 /* initialize count to sectors from startref to end
1058 regSize = stripSize - ref_in_start_stripe;
1061 /* add complete rows in the middle of the transfer */
1063 regSize += (numRows-2) << raid->stripeShift;
1065 /* if IO ends within first strip of last row*/
1066 if (endStrip == endRow*raid->rowDataSize)
1067 regSize += ref_in_end_stripe+1;
1069 regSize += stripSize;
1072 * For Uneven span region lock optimization.
1073 * If the start strip is the last in the start row
1075 if (start_strip == (get_strip_from_row(instance, ld, start_row, map) +
1076 SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
1077 regStart += ref_in_start_stripe;
1078 /* initialize count to sectors from
1079 * startRef to end of strip
1081 regSize = stripSize - ref_in_start_stripe;
1083 /* Add complete rows in the middle of the transfer*/
1086 /* Add complete rows in the middle of the transfer*/
1087 regSize += (numRows-2) << raid->stripeShift;
1089 /* if IO ends within first strip of last row */
1090 if (endStrip == get_strip_from_row(instance, ld, endRow, map))
1091 regSize += ref_in_end_stripe + 1;
1093 regSize += stripSize;
1096 pRAID_Context->timeoutValue =
1097 cpu_to_le16(raid->fpIoTimeoutForLd ?
1098 raid->fpIoTimeoutForLd :
1099 map->raidMap.fpPdIoTimeoutSec);
1100 if (fusion->adapter_type == INVADER_SERIES)
1101 pRAID_Context->regLockFlags = (isRead) ?
1102 raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
1104 pRAID_Context->regLockFlags = (isRead) ?
1105 REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
1106 pRAID_Context->VirtualDiskTgtId = raid->targetId;
1107 pRAID_Context->regLockRowLBA = cpu_to_le64(regStart);
1108 pRAID_Context->regLockLength = cpu_to_le32(regSize);
1109 pRAID_Context->configSeqNum = raid->seqNum;
1110 /* save pointer to raid->LUN array */
1111 *raidLUN = raid->LUN;
1114 /*Get Phy Params only if FP capable, or else leave it to MR firmware
1115 to do the calculation.*/
1116 if (io_info->fpOkForIo) {
1117 retval = io_info->IoforUnevenSpan ?
1118 mr_spanset_get_phy_params(instance, ld,
1119 start_strip, ref_in_start_stripe,
1120 io_info, pRAID_Context, map) :
1121 MR_GetPhyParams(instance, ld, start_strip,
1122 ref_in_start_stripe, io_info,
1123 pRAID_Context, map);
1124 /* If IO on an invalid Pd, then FP is not possible.*/
1125 if (io_info->devHandle == cpu_to_le16(MR_PD_INVALID))
1126 io_info->fpOkForIo = FALSE;
1128 } else if (isRead) {
1130 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
1131 retval = io_info->IoforUnevenSpan ?
1132 mr_spanset_get_phy_params(instance, ld,
1133 start_strip + stripIdx,
1134 ref_in_start_stripe, io_info,
1135 pRAID_Context, map) :
1136 MR_GetPhyParams(instance, ld,
1137 start_strip + stripIdx, ref_in_start_stripe,
1138 io_info, pRAID_Context, map);
1145 /* Just for testing what arm we get for strip.*/
1146 if (io_info->IoforUnevenSpan)
1147 get_arm_from_strip(instance, ld, start_strip, map);
1153 ******************************************************************************
1155 * This routine pepare spanset info from Valid Raid map and store it into
1156 * local copy of ldSpanInfo per instance data structure.
1160 * ldSpanInfo - ldSpanInfo per HBA instance
1163 void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
1164 PLD_SPAN_INFO ldSpanInfo)
1167 u32 element, span_row_width;
1169 struct MR_LD_RAID *raid;
1170 LD_SPAN_SET *span_set, *span_set_prev;
1171 struct MR_QUAD_ELEMENT *quad;
1176 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1177 ld = MR_TargetIdToLdGet(ldCount, map);
1178 if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
1180 raid = MR_LdRaidGet(ld, map);
1181 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
1182 for (span = 0; span < raid->spanDepth; span++) {
1183 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
1184 block_span_info.noElements) <
1187 span_set = &(ldSpanInfo[ld].span_set[element]);
1188 quad = &map->raidMap.ldSpanMap[ld].
1189 spanBlock[span].block_span_info.
1192 span_set->diff = le32_to_cpu(quad->diff);
1194 for (count = 0, span_row_width = 0;
1195 count < raid->spanDepth; count++) {
1196 if (le32_to_cpu(map->raidMap.ldSpanMap[ld].
1199 noElements) >= element + 1) {
1200 span_set->strip_offset[count] =
1204 (ld, count, map)->spanRowDataSize;
1208 span_set->span_row_data_width = span_row_width;
1209 span_row = mega_div64_32(((le64_to_cpu(quad->logEnd) -
1210 le64_to_cpu(quad->logStart)) + le32_to_cpu(quad->diff)),
1211 le32_to_cpu(quad->diff));
1214 span_set->log_start_lba = 0;
1215 span_set->log_end_lba =
1216 ((span_row << raid->stripeShift)
1217 * span_row_width) - 1;
1219 span_set->span_row_start = 0;
1220 span_set->span_row_end = span_row - 1;
1222 span_set->data_strip_start = 0;
1223 span_set->data_strip_end =
1224 (span_row * span_row_width) - 1;
1226 span_set->data_row_start = 0;
1227 span_set->data_row_end =
1228 (span_row * le32_to_cpu(quad->diff)) - 1;
1230 span_set_prev = &(ldSpanInfo[ld].
1231 span_set[element - 1]);
1232 span_set->log_start_lba =
1233 span_set_prev->log_end_lba + 1;
1234 span_set->log_end_lba =
1235 span_set->log_start_lba +
1236 ((span_row << raid->stripeShift)
1237 * span_row_width) - 1;
1239 span_set->span_row_start =
1240 span_set_prev->span_row_end + 1;
1241 span_set->span_row_end =
1242 span_set->span_row_start + span_row - 1;
1244 span_set->data_strip_start =
1245 span_set_prev->data_strip_end + 1;
1246 span_set->data_strip_end =
1247 span_set->data_strip_start +
1248 (span_row * span_row_width) - 1;
1250 span_set->data_row_start =
1251 span_set_prev->data_row_end + 1;
1252 span_set->data_row_end =
1253 span_set->data_row_start +
1254 (span_row * le32_to_cpu(quad->diff)) - 1;
1258 if (span == raid->spanDepth)
1263 getSpanInfo(map, ldSpanInfo);
1268 void mr_update_load_balance_params(struct MR_DRV_RAID_MAP_ALL *drv_map,
1269 struct LD_LOAD_BALANCE_INFO *lbInfo)
1273 struct MR_LD_RAID *raid;
1275 if (lb_pending_cmds > 128 || lb_pending_cmds < 1)
1276 lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
1278 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1279 ld = MR_TargetIdToLdGet(ldCount, drv_map);
1280 if (ld >= MAX_LOGICAL_DRIVES_EXT) {
1281 lbInfo[ldCount].loadBalanceFlag = 0;
1285 raid = MR_LdRaidGet(ld, drv_map);
1286 if ((raid->level != 1) ||
1287 (raid->ldState != MR_LD_STATE_OPTIMAL)) {
1288 lbInfo[ldCount].loadBalanceFlag = 0;
1291 lbInfo[ldCount].loadBalanceFlag = 1;
1295 u8 megasas_get_best_arm_pd(struct megasas_instance *instance,
1296 struct LD_LOAD_BALANCE_INFO *lbInfo, struct IO_REQUEST_INFO *io_info)
1298 struct fusion_context *fusion;
1299 struct MR_LD_RAID *raid;
1300 struct MR_DRV_RAID_MAP_ALL *drv_map;
1301 u16 pend0, pend1, ld;
1303 u8 bestArm, pd0, pd1, span, arm;
1304 u32 arRef, span_row_size;
1306 u64 block = io_info->ldStartBlock;
1307 u32 count = io_info->numBlocks;
1309 span = ((io_info->span_arm & RAID_CTX_SPANARM_SPAN_MASK)
1310 >> RAID_CTX_SPANARM_SPAN_SHIFT);
1311 arm = (io_info->span_arm & RAID_CTX_SPANARM_ARM_MASK);
1314 fusion = instance->ctrl_context;
1315 drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
1316 ld = MR_TargetIdToLdGet(io_info->ldTgtId, drv_map);
1317 raid = MR_LdRaidGet(ld, drv_map);
1318 span_row_size = instance->UnevenSpanSupport ?
1319 SPAN_ROW_SIZE(drv_map, ld, span) : raid->rowSize;
1321 arRef = MR_LdSpanArrayGet(ld, span, drv_map);
1322 pd0 = MR_ArPdGet(arRef, arm, drv_map);
1323 pd1 = MR_ArPdGet(arRef, (arm + 1) >= span_row_size ?
1324 (arm + 1 - span_row_size) : arm + 1, drv_map);
1326 /* get the pending cmds for the data and mirror arms */
1327 pend0 = atomic_read(&lbInfo->scsi_pending_cmds[pd0]);
1328 pend1 = atomic_read(&lbInfo->scsi_pending_cmds[pd1]);
1330 /* Determine the disk whose head is nearer to the req. block */
1331 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[pd0]);
1332 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[pd1]);
1333 bestArm = (diff0 <= diff1 ? arm : arm ^ 1);
1335 if ((bestArm == arm && pend0 > pend1 + lb_pending_cmds) ||
1336 (bestArm != arm && pend1 > pend0 + lb_pending_cmds))
1339 /* Update the last accessed block on the correct pd */
1340 io_info->pd_after_lb = (bestArm == arm) ? pd0 : pd1;
1341 lbInfo->last_accessed_block[io_info->pd_after_lb] = block + count - 1;
1342 io_info->span_arm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | bestArm;
1345 dev_dbg(&instance->pdev->dev, "LSI Debug R1 Load balance "
1346 "occur - span 0x%x arm 0x%x bestArm 0x%x "
1347 "io_info->span_arm 0x%x\n",
1348 span, arm, bestArm, io_info->span_arm);
1350 return io_info->pd_after_lb;
1353 __le16 get_updated_dev_handle(struct megasas_instance *instance,
1354 struct LD_LOAD_BALANCE_INFO *lbInfo, struct IO_REQUEST_INFO *io_info)
1358 struct fusion_context *fusion;
1359 struct MR_DRV_RAID_MAP_ALL *drv_map;
1361 fusion = instance->ctrl_context;
1362 drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
1364 /* get best new arm (PD ID) */
1365 arm_pd = megasas_get_best_arm_pd(instance, lbInfo, io_info);
1366 devHandle = MR_PdDevHandleGet(arm_pd, drv_map);
1367 atomic_inc(&lbInfo->scsi_pending_cmds[arm_pd]);