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 u16
MR_ArPdGet(u32 ar
, u32 arm
, struct MR_FW_RAID_MAP_ALL
*map
)
131 return le16_to_cpu(map
->raidMap
.arMapInfo
[ar
].pd
[arm
]);
134 u16
MR_LdSpanArrayGet(u32 ld
, u32 span
, struct MR_FW_RAID_MAP_ALL
*map
)
136 return le16_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].span
.arrayRef
);
139 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 le16_to_cpu(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
;
170 struct MR_LD_RAID
*raid
;
171 int ldCount
, num_lds
;
175 if (le32_to_cpu(pFwRaidMap
->totalSize
) !=
176 (sizeof(struct MR_FW_RAID_MAP
) -sizeof(struct MR_LD_SPAN_MAP
) +
177 (sizeof(struct MR_LD_SPAN_MAP
) * le32_to_cpu(pFwRaidMap
->ldCount
)))) {
178 printk(KERN_ERR
"megasas: map info structure size 0x%x is not matching with ld count\n",
179 (unsigned int)((sizeof(struct MR_FW_RAID_MAP
) -
180 sizeof(struct MR_LD_SPAN_MAP
)) +
181 (sizeof(struct MR_LD_SPAN_MAP
) *
182 le32_to_cpu(pFwRaidMap
->ldCount
))));
183 printk(KERN_ERR
"megasas: span map %x, pFwRaidMap->totalSize "
184 ": %x\n", (unsigned int)sizeof(struct MR_LD_SPAN_MAP
),
185 le32_to_cpu(pFwRaidMap
->totalSize
));
189 if (instance
->UnevenSpanSupport
)
190 mr_update_span_set(map
, ldSpanInfo
);
192 mr_update_load_balance_params(map
, lbInfo
);
194 num_lds
= le32_to_cpu(map
->raidMap
.ldCount
);
196 /*Convert Raid capability values to CPU arch */
197 for (ldCount
= 0; ldCount
< num_lds
; ldCount
++) {
198 ld
= MR_TargetIdToLdGet(ldCount
, map
);
199 raid
= MR_LdRaidGet(ld
, map
);
200 le32_to_cpus((u32
*)&raid
->capability
);
206 u32
MR_GetSpanBlock(u32 ld
, u64 row
, u64
*span_blk
,
207 struct MR_FW_RAID_MAP_ALL
*map
)
209 struct MR_SPAN_BLOCK_INFO
*pSpanBlock
= MR_LdSpanInfoGet(ld
, map
);
210 struct MR_QUAD_ELEMENT
*quad
;
211 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
214 for (span
= 0; span
< raid
->spanDepth
; span
++, pSpanBlock
++) {
216 for (j
= 0; j
< le32_to_cpu(pSpanBlock
->block_span_info
.noElements
); j
++) {
217 quad
= &pSpanBlock
->block_span_info
.quad
[j
];
219 if (le32_to_cpu(quad
->diff
) == 0)
221 if (le64_to_cpu(quad
->logStart
) <= row
&& row
<=
222 le64_to_cpu(quad
->logEnd
) && (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
223 le32_to_cpu(quad
->diff
))) == 0) {
224 if (span_blk
!= NULL
) {
226 blk
= mega_div64_32((row
-le64_to_cpu(quad
->logStart
)), le32_to_cpu(quad
->diff
));
229 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
)) << raid
->stripeShift
;
240 ******************************************************************************
242 * Function to print info about span set created in driver from FW raid map
246 * ldSpanInfo - ldSpanInfo per HBA instance
249 static int getSpanInfo(struct MR_FW_RAID_MAP_ALL
*map
, PLD_SPAN_INFO ldSpanInfo
)
254 struct MR_LD_RAID
*raid
;
255 LD_SPAN_SET
*span_set
;
256 struct MR_QUAD_ELEMENT
*quad
;
260 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES
; ldCount
++) {
261 ld
= MR_TargetIdToLdGet(ldCount
, map
);
262 if (ld
>= MAX_LOGICAL_DRIVES
)
264 raid
= MR_LdRaidGet(ld
, map
);
265 dev_dbg(&instance
->pdev
->dev
, "LD %x: span_depth=%x\n",
266 ld
, raid
->spanDepth
);
267 for (span
= 0; span
< raid
->spanDepth
; span
++)
268 dev_dbg(&instance
->pdev
->dev
, "Span=%x,"
269 " number of quads=%x\n", span
,
270 le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
271 block_span_info
.noElements
));
272 for (element
= 0; element
< MAX_QUAD_DEPTH
; element
++) {
273 span_set
= &(ldSpanInfo
[ld
].span_set
[element
]);
274 if (span_set
->span_row_data_width
== 0)
277 dev_dbg(&instance
->pdev
->dev
, "Span Set %x:"
278 "width=%x, diff=%x\n", element
,
279 (unsigned int)span_set
->span_row_data_width
,
280 (unsigned int)span_set
->diff
);
281 dev_dbg(&instance
->pdev
->dev
, "logical LBA"
282 "start=0x%08lx, end=0x%08lx\n",
283 (long unsigned int)span_set
->log_start_lba
,
284 (long unsigned int)span_set
->log_end_lba
);
285 dev_dbg(&instance
->pdev
->dev
, "span row start=0x%08lx,"
287 (long unsigned int)span_set
->span_row_start
,
288 (long unsigned int)span_set
->span_row_end
);
289 dev_dbg(&instance
->pdev
->dev
, "data row start=0x%08lx,"
291 (long unsigned int)span_set
->data_row_start
,
292 (long unsigned int)span_set
->data_row_end
);
293 dev_dbg(&instance
->pdev
->dev
, "data strip start=0x%08lx,"
295 (long unsigned int)span_set
->data_strip_start
,
296 (long unsigned int)span_set
->data_strip_end
);
298 for (span
= 0; span
< raid
->spanDepth
; span
++) {
299 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
300 block_span_info
.noElements
) >=
302 quad
= &map
->raidMap
.ldSpanMap
[ld
].
303 spanBlock
[span
].block_span_info
.
305 dev_dbg(&instance
->pdev
->dev
, "Span=%x,"
306 "Quad=%x, diff=%x\n", span
,
307 element
, le32_to_cpu(quad
->diff
));
308 dev_dbg(&instance
->pdev
->dev
,
309 "offset_in_span=0x%08lx\n",
310 (long unsigned int)le64_to_cpu(quad
->offsetInSpan
));
311 dev_dbg(&instance
->pdev
->dev
,
312 "logical start=0x%08lx, end=0x%08lx\n",
313 (long unsigned int)le64_to_cpu(quad
->logStart
),
314 (long unsigned int)le64_to_cpu(quad
->logEnd
));
324 ******************************************************************************
326 * This routine calculates the Span block for given row using spanset.
329 * instance - HBA instance
330 * ld - Logical drive number
337 * block - Absolute Block number in the physical disk
338 * div_error - Devide error code.
341 u32
mr_spanset_get_span_block(struct megasas_instance
*instance
,
342 u32 ld
, u64 row
, u64
*span_blk
, struct MR_FW_RAID_MAP_ALL
*map
)
344 struct fusion_context
*fusion
= instance
->ctrl_context
;
345 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
346 LD_SPAN_SET
*span_set
;
347 struct MR_QUAD_ELEMENT
*quad
;
349 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
351 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
352 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
354 if (span_set
->span_row_data_width
== 0)
357 if (row
> span_set
->data_row_end
)
360 for (span
= 0; span
< raid
->spanDepth
; span
++)
361 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
362 block_span_info
.noElements
) >= info
+1) {
363 quad
= &map
->raidMap
.ldSpanMap
[ld
].
365 block_span_info
.quad
[info
];
366 if (le32_to_cpu(quad
->diff
== 0))
368 if (le64_to_cpu(quad
->logStart
) <= row
&&
369 row
<= le64_to_cpu(quad
->logEnd
) &&
370 (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
371 le32_to_cpu(quad
->diff
))) == 0) {
372 if (span_blk
!= NULL
) {
375 ((row
- le64_to_cpu(quad
->logStart
)),
376 le32_to_cpu(quad
->diff
));
377 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
))
378 << raid
->stripeShift
;
389 ******************************************************************************
391 * This routine calculates the row for given strip using spanset.
394 * instance - HBA instance
395 * ld - Logical drive number
401 * row - row associated with strip
404 static u64
get_row_from_strip(struct megasas_instance
*instance
,
405 u32 ld
, u64 strip
, struct MR_FW_RAID_MAP_ALL
*map
)
407 struct fusion_context
*fusion
= instance
->ctrl_context
;
408 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
409 LD_SPAN_SET
*span_set
;
410 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
411 u32 info
, strip_offset
, span
, span_offset
;
412 u64 span_set_Strip
, span_set_Row
, retval
;
414 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
415 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
417 if (span_set
->span_row_data_width
== 0)
419 if (strip
> span_set
->data_strip_end
)
422 span_set_Strip
= strip
- span_set
->data_strip_start
;
423 strip_offset
= mega_mod64(span_set_Strip
,
424 span_set
->span_row_data_width
);
425 span_set_Row
= mega_div64_32(span_set_Strip
,
426 span_set
->span_row_data_width
) * span_set
->diff
;
427 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
428 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
429 block_span_info
.noElements
>= info
+1)) {
431 span_set
->strip_offset
[span
])
437 dev_info(&instance
->pdev
->dev
, "Strip 0x%llx,"
438 "span_set_Strip 0x%llx, span_set_Row 0x%llx"
439 "data width 0x%llx span offset 0x%x\n", strip
,
440 (unsigned long long)span_set_Strip
,
441 (unsigned long long)span_set_Row
,
442 (unsigned long long)span_set
->span_row_data_width
,
444 dev_info(&instance
->pdev
->dev
, "For strip 0x%llx"
445 "row is 0x%llx\n", strip
,
446 (unsigned long long) span_set
->data_row_start
+
447 (unsigned long long) span_set_Row
+ (span_offset
- 1));
449 retval
= (span_set
->data_row_start
+ span_set_Row
+
458 ******************************************************************************
460 * This routine calculates the Start Strip for given row using spanset.
463 * instance - HBA instance
464 * ld - Logical drive number
470 * Strip - Start strip associated with row
473 static u64
get_strip_from_row(struct megasas_instance
*instance
,
474 u32 ld
, u64 row
, struct MR_FW_RAID_MAP_ALL
*map
)
476 struct fusion_context
*fusion
= instance
->ctrl_context
;
477 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
478 LD_SPAN_SET
*span_set
;
479 struct MR_QUAD_ELEMENT
*quad
;
480 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
484 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
485 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
487 if (span_set
->span_row_data_width
== 0)
489 if (row
> span_set
->data_row_end
)
492 for (span
= 0; span
< raid
->spanDepth
; span
++)
493 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
494 block_span_info
.noElements
) >= info
+1) {
495 quad
= &map
->raidMap
.ldSpanMap
[ld
].
496 spanBlock
[span
].block_span_info
.quad
[info
];
497 if (le64_to_cpu(quad
->logStart
) <= row
&&
498 row
<= le64_to_cpu(quad
->logEnd
) &&
499 mega_mod64((row
- le64_to_cpu(quad
->logStart
)),
500 le32_to_cpu(quad
->diff
)) == 0) {
501 strip
= mega_div64_32
502 (((row
- span_set
->data_row_start
)
503 - le64_to_cpu(quad
->logStart
)),
504 le32_to_cpu(quad
->diff
));
505 strip
*= span_set
->span_row_data_width
;
506 strip
+= span_set
->data_strip_start
;
507 strip
+= span_set
->strip_offset
[span
];
512 dev_err(&instance
->pdev
->dev
, "get_strip_from_row"
513 "returns invalid strip for ld=%x, row=%lx\n",
514 ld
, (long unsigned int)row
);
519 ******************************************************************************
521 * This routine calculates the Physical Arm for given strip using spanset.
524 * instance - HBA instance
525 * ld - Logical drive number
531 * Phys Arm - Phys Arm associated with strip
534 static u32
get_arm_from_strip(struct megasas_instance
*instance
,
535 u32 ld
, u64 strip
, struct MR_FW_RAID_MAP_ALL
*map
)
537 struct fusion_context
*fusion
= instance
->ctrl_context
;
538 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
539 LD_SPAN_SET
*span_set
;
540 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
541 u32 info
, strip_offset
, span
, span_offset
, retval
;
543 for (info
= 0 ; info
< MAX_QUAD_DEPTH
; info
++) {
544 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
546 if (span_set
->span_row_data_width
== 0)
548 if (strip
> span_set
->data_strip_end
)
551 strip_offset
= (uint
)mega_mod64
552 ((strip
- span_set
->data_strip_start
),
553 span_set
->span_row_data_width
);
555 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
556 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
557 block_span_info
.noElements
) >= info
+1) {
559 span_set
->strip_offset
[span
])
561 span_set
->strip_offset
[span
];
566 dev_info(&instance
->pdev
->dev
, "get_arm_from_strip:"
567 "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld
,
568 (long unsigned int)strip
, (strip_offset
- span_offset
));
570 retval
= (strip_offset
- span_offset
);
574 dev_err(&instance
->pdev
->dev
, "get_arm_from_strip"
575 "returns invalid arm for ld=%x strip=%lx\n",
576 ld
, (long unsigned int)strip
);
581 /* This Function will return Phys arm */
582 u8
get_arm(struct megasas_instance
*instance
, u32 ld
, u8 span
, u64 stripe
,
583 struct MR_FW_RAID_MAP_ALL
*map
)
585 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
586 /* Need to check correct default value */
589 switch (raid
->level
) {
593 arm
= mega_mod64(stripe
, SPAN_ROW_SIZE(map
, ld
, span
));
596 /* start with logical arm */
597 arm
= get_arm_from_strip(instance
, ld
, stripe
, map
);
608 ******************************************************************************
610 * This routine calculates the arm, span and block for the specified stripe and
611 * reference in stripe using spanset
615 * ld - Logical drive number
616 * stripRow - Stripe number
617 * stripRef - Reference in stripe
622 * block - Absolute Block number in the physical disk
624 static u8
mr_spanset_get_phy_params(struct megasas_instance
*instance
, u32 ld
,
625 u64 stripRow
, u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
626 struct RAID_CONTEXT
*pRAID_Context
,
627 struct MR_FW_RAID_MAP_ALL
*map
)
629 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
635 u64
*pdBlock
= &io_info
->pdBlock
;
636 u16
*pDevHandle
= &io_info
->devHandle
;
637 u32 logArm
, rowMod
, armQ
, arm
;
639 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
||
640 instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
643 /*Get row and span from io_info for Uneven Span IO.*/
644 row
= io_info
->start_row
;
645 span
= io_info
->start_span
;
648 if (raid
->level
== 6) {
649 logArm
= get_arm_from_strip(instance
, ld
, stripRow
, map
);
652 rowMod
= mega_mod64(row
, SPAN_ROW_SIZE(map
, ld
, span
));
653 armQ
= SPAN_ROW_SIZE(map
, ld
, span
) - 1 - rowMod
;
654 arm
= armQ
+ 1 + logArm
;
655 if (arm
>= SPAN_ROW_SIZE(map
, ld
, span
))
656 arm
-= SPAN_ROW_SIZE(map
, ld
, span
);
659 /* Calculate the arm */
660 physArm
= get_arm(instance
, ld
, span
, stripRow
, map
);
664 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
665 pd
= MR_ArPdGet(arRef
, physArm
, map
);
667 if (pd
!= MR_PD_INVALID
)
668 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
670 *pDevHandle
= MR_PD_INVALID
;
671 if ((raid
->level
>= 5) &&
672 (!do_invader
|| (do_invader
&&
673 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
674 pRAID_Context
->regLockFlags
= REGION_TYPE_EXCLUSIVE
;
675 else if (raid
->level
== 1) {
676 pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
677 if (pd
!= MR_PD_INVALID
)
678 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
682 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
683 pRAID_Context
->spanArm
= (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) |
689 ******************************************************************************
691 * This routine calculates the arm, span and block for the specified stripe and
692 * reference in stripe.
696 * ld - Logical drive number
697 * stripRow - Stripe number
698 * stripRef - Reference in stripe
703 * block - Absolute Block number in the physical disk
705 u8
MR_GetPhyParams(struct megasas_instance
*instance
, u32 ld
, u64 stripRow
,
706 u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
707 struct RAID_CONTEXT
*pRAID_Context
,
708 struct MR_FW_RAID_MAP_ALL
*map
)
710 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
716 u64
*pdBlock
= &io_info
->pdBlock
;
717 u16
*pDevHandle
= &io_info
->devHandle
;
719 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
||
720 instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
723 row
= mega_div64_32(stripRow
, raid
->rowDataSize
);
725 if (raid
->level
== 6) {
726 /* logical arm within row */
727 u32 logArm
= mega_mod64(stripRow
, raid
->rowDataSize
);
728 u32 rowMod
, armQ
, arm
;
730 if (raid
->rowSize
== 0)
732 /* get logical row mod */
733 rowMod
= mega_mod64(row
, raid
->rowSize
);
734 armQ
= raid
->rowSize
-1-rowMod
; /* index of Q drive */
735 arm
= armQ
+1+logArm
; /* data always logically follows Q */
736 if (arm
>= raid
->rowSize
) /* handle wrap condition */
737 arm
-= raid
->rowSize
;
740 if (raid
->modFactor
== 0)
742 physArm
= MR_LdDataArmGet(ld
, mega_mod64(stripRow
,
747 if (raid
->spanDepth
== 1) {
749 *pdBlock
= row
<< raid
->stripeShift
;
751 span
= (u8
)MR_GetSpanBlock(ld
, row
, pdBlock
, map
);
752 if (span
== SPAN_INVALID
)
756 /* Get the array on which this span is present */
757 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
758 pd
= MR_ArPdGet(arRef
, physArm
, map
); /* Get the pd */
760 if (pd
!= MR_PD_INVALID
)
761 /* Get dev handle from Pd. */
762 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
764 *pDevHandle
= MR_PD_INVALID
; /* set dev handle as invalid. */
765 if ((raid
->level
>= 5) &&
766 (!do_invader
|| (do_invader
&&
767 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
768 pRAID_Context
->regLockFlags
= REGION_TYPE_EXCLUSIVE
;
769 else if (raid
->level
== 1) {
770 /* Get alternate Pd. */
771 pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
772 if (pd
!= MR_PD_INVALID
)
773 /* Get dev handle from Pd */
774 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
778 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
779 pRAID_Context
->spanArm
= (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) |
785 ******************************************************************************
787 * MR_BuildRaidContext function
789 * This function will initiate command processing. The start/end row and strip
790 * information is calculated then the lock is acquired.
791 * This function will return 0 if region lock was acquired OR return num strips
794 MR_BuildRaidContext(struct megasas_instance
*instance
,
795 struct IO_REQUEST_INFO
*io_info
,
796 struct RAID_CONTEXT
*pRAID_Context
,
797 struct MR_FW_RAID_MAP_ALL
*map
, u8
**raidLUN
)
799 struct MR_LD_RAID
*raid
;
800 u32 ld
, stripSize
, stripe_mask
;
801 u64 endLba
, endStrip
, endRow
, start_row
, start_strip
;
804 u8 num_strips
, numRows
;
805 u16 ref_in_start_stripe
, ref_in_end_stripe
;
807 u32 numBlocks
, ldTgtId
;
810 u8 startlba_span
= SPAN_INVALID
;
811 u64
*pdBlock
= &io_info
->pdBlock
;
813 ldStartBlock
= io_info
->ldStartBlock
;
814 numBlocks
= io_info
->numBlocks
;
815 ldTgtId
= io_info
->ldTgtId
;
816 isRead
= io_info
->isRead
;
817 io_info
->IoforUnevenSpan
= 0;
818 io_info
->start_span
= SPAN_INVALID
;
820 ld
= MR_TargetIdToLdGet(ldTgtId
, map
);
821 raid
= MR_LdRaidGet(ld
, map
);
824 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
827 if (raid
->rowDataSize
== 0) {
828 if (MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
== 0)
830 else if (instance
->UnevenSpanSupport
) {
831 io_info
->IoforUnevenSpan
= 1;
833 dev_info(&instance
->pdev
->dev
,
834 "raid->rowDataSize is 0, but has SPAN[0]"
835 "rowDataSize = 0x%0x,"
836 "but there is _NO_ UnevenSpanSupport\n",
837 MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
);
842 stripSize
= 1 << raid
->stripeShift
;
843 stripe_mask
= stripSize
-1;
847 * calculate starting row and stripe, and number of strips and rows
849 start_strip
= ldStartBlock
>> raid
->stripeShift
;
850 ref_in_start_stripe
= (u16
)(ldStartBlock
& stripe_mask
);
851 endLba
= ldStartBlock
+ numBlocks
- 1;
852 ref_in_end_stripe
= (u16
)(endLba
& stripe_mask
);
853 endStrip
= endLba
>> raid
->stripeShift
;
854 num_strips
= (u8
)(endStrip
- start_strip
+ 1); /* End strip */
856 if (io_info
->IoforUnevenSpan
) {
857 start_row
= get_row_from_strip(instance
, ld
, start_strip
, map
);
858 endRow
= get_row_from_strip(instance
, ld
, endStrip
, map
);
859 if (start_row
== -1ULL || endRow
== -1ULL) {
860 dev_info(&instance
->pdev
->dev
, "return from %s %d."
861 "Send IO w/o region lock.\n",
866 if (raid
->spanDepth
== 1) {
868 *pdBlock
= start_row
<< raid
->stripeShift
;
870 startlba_span
= (u8
)mr_spanset_get_span_block(instance
,
871 ld
, start_row
, pdBlock
, map
);
872 if (startlba_span
== SPAN_INVALID
) {
873 dev_info(&instance
->pdev
->dev
, "return from %s %d"
874 "for row 0x%llx,start strip %llx"
875 "endSrip %llx\n", __func__
, __LINE__
,
876 (unsigned long long)start_row
,
877 (unsigned long long)start_strip
,
878 (unsigned long long)endStrip
);
881 io_info
->start_span
= startlba_span
;
882 io_info
->start_row
= start_row
;
884 dev_dbg(&instance
->pdev
->dev
, "Check Span number from %s %d"
885 "for row 0x%llx, start strip 0x%llx end strip 0x%llx"
886 " span 0x%x\n", __func__
, __LINE__
,
887 (unsigned long long)start_row
,
888 (unsigned long long)start_strip
,
889 (unsigned long long)endStrip
, startlba_span
);
890 dev_dbg(&instance
->pdev
->dev
, "start_row 0x%llx endRow 0x%llx"
891 "Start span 0x%x\n", (unsigned long long)start_row
,
892 (unsigned long long)endRow
, startlba_span
);
895 start_row
= mega_div64_32(start_strip
, raid
->rowDataSize
);
896 endRow
= mega_div64_32(endStrip
, raid
->rowDataSize
);
898 numRows
= (u8
)(endRow
- start_row
+ 1);
901 * calculate region info.
904 /* assume region is at the start of the first row */
905 regStart
= start_row
<< raid
->stripeShift
;
906 /* assume this IO needs the full row - we'll adjust if not true */
909 /* Check if we can send this I/O via FastPath */
910 if (raid
->capability
.fpCapable
) {
912 io_info
->fpOkForIo
= (raid
->capability
.fpReadCapable
&&
913 ((num_strips
== 1) ||
915 fpReadAcrossStripe
));
917 io_info
->fpOkForIo
= (raid
->capability
.fpWriteCapable
&&
918 ((num_strips
== 1) ||
920 fpWriteAcrossStripe
));
922 io_info
->fpOkForIo
= FALSE
;
925 /* single-strip IOs can always lock only the data needed */
926 if (num_strips
== 1) {
927 regStart
+= ref_in_start_stripe
;
930 /* multi-strip IOs always need to full stripe locked */
931 } else if (io_info
->IoforUnevenSpan
== 0) {
933 * For Even span region lock optimization.
934 * If the start strip is the last in the start row
936 if (start_strip
== (start_row
+ 1) * raid
->rowDataSize
- 1) {
937 regStart
+= ref_in_start_stripe
;
938 /* initialize count to sectors from startref to end
940 regSize
= stripSize
- ref_in_start_stripe
;
943 /* add complete rows in the middle of the transfer */
945 regSize
+= (numRows
-2) << raid
->stripeShift
;
947 /* if IO ends within first strip of last row*/
948 if (endStrip
== endRow
*raid
->rowDataSize
)
949 regSize
+= ref_in_end_stripe
+1;
951 regSize
+= stripSize
;
954 * For Uneven span region lock optimization.
955 * If the start strip is the last in the start row
957 if (start_strip
== (get_strip_from_row(instance
, ld
, start_row
, map
) +
958 SPAN_ROW_DATA_SIZE(map
, ld
, startlba_span
) - 1)) {
959 regStart
+= ref_in_start_stripe
;
960 /* initialize count to sectors from
961 * startRef to end of strip
963 regSize
= stripSize
- ref_in_start_stripe
;
965 /* Add complete rows in the middle of the transfer*/
968 /* Add complete rows in the middle of the transfer*/
969 regSize
+= (numRows
-2) << raid
->stripeShift
;
971 /* if IO ends within first strip of last row */
972 if (endStrip
== get_strip_from_row(instance
, ld
, endRow
, map
))
973 regSize
+= ref_in_end_stripe
+ 1;
975 regSize
+= stripSize
;
978 pRAID_Context
->timeoutValue
= cpu_to_le16(map
->raidMap
.fpPdIoTimeoutSec
);
979 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
980 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
981 pRAID_Context
->regLockFlags
= (isRead
) ?
982 raid
->regTypeReqOnRead
: raid
->regTypeReqOnWrite
;
984 pRAID_Context
->regLockFlags
= (isRead
) ?
985 REGION_TYPE_SHARED_READ
: raid
->regTypeReqOnWrite
;
986 pRAID_Context
->VirtualDiskTgtId
= raid
->targetId
;
987 pRAID_Context
->regLockRowLBA
= cpu_to_le64(regStart
);
988 pRAID_Context
->regLockLength
= cpu_to_le32(regSize
);
989 pRAID_Context
->configSeqNum
= raid
->seqNum
;
990 /* save pointer to raid->LUN array */
991 *raidLUN
= raid
->LUN
;
994 /*Get Phy Params only if FP capable, or else leave it to MR firmware
995 to do the calculation.*/
996 if (io_info
->fpOkForIo
) {
997 retval
= io_info
->IoforUnevenSpan
?
998 mr_spanset_get_phy_params(instance
, ld
,
999 start_strip
, ref_in_start_stripe
,
1000 io_info
, pRAID_Context
, map
) :
1001 MR_GetPhyParams(instance
, ld
, start_strip
,
1002 ref_in_start_stripe
, io_info
,
1003 pRAID_Context
, map
);
1004 /* If IO on an invalid Pd, then FP is not possible.*/
1005 if (io_info
->devHandle
== MR_PD_INVALID
)
1006 io_info
->fpOkForIo
= FALSE
;
1008 } else if (isRead
) {
1010 for (stripIdx
= 0; stripIdx
< num_strips
; stripIdx
++) {
1011 retval
= io_info
->IoforUnevenSpan
?
1012 mr_spanset_get_phy_params(instance
, ld
,
1013 start_strip
+ stripIdx
,
1014 ref_in_start_stripe
, io_info
,
1015 pRAID_Context
, map
) :
1016 MR_GetPhyParams(instance
, ld
,
1017 start_strip
+ stripIdx
, ref_in_start_stripe
,
1018 io_info
, pRAID_Context
, map
);
1025 /* Just for testing what arm we get for strip.*/
1026 if (io_info
->IoforUnevenSpan
)
1027 get_arm_from_strip(instance
, ld
, start_strip
, map
);
1033 ******************************************************************************
1035 * This routine pepare spanset info from Valid Raid map and store it into
1036 * local copy of ldSpanInfo per instance data structure.
1040 * ldSpanInfo - ldSpanInfo per HBA instance
1043 void mr_update_span_set(struct MR_FW_RAID_MAP_ALL
*map
,
1044 PLD_SPAN_INFO ldSpanInfo
)
1047 u32 element
, span_row_width
;
1049 struct MR_LD_RAID
*raid
;
1050 LD_SPAN_SET
*span_set
, *span_set_prev
;
1051 struct MR_QUAD_ELEMENT
*quad
;
1056 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES
; ldCount
++) {
1057 ld
= MR_TargetIdToLdGet(ldCount
, map
);
1058 if (ld
>= MAX_LOGICAL_DRIVES
)
1060 raid
= MR_LdRaidGet(ld
, map
);
1061 for (element
= 0; element
< MAX_QUAD_DEPTH
; element
++) {
1062 for (span
= 0; span
< raid
->spanDepth
; span
++) {
1063 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
1064 block_span_info
.noElements
) <
1067 span_set
= &(ldSpanInfo
[ld
].span_set
[element
]);
1068 quad
= &map
->raidMap
.ldSpanMap
[ld
].
1069 spanBlock
[span
].block_span_info
.
1072 span_set
->diff
= le32_to_cpu(quad
->diff
);
1074 for (count
= 0, span_row_width
= 0;
1075 count
< raid
->spanDepth
; count
++) {
1076 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].
1079 noElements
) >= element
+ 1) {
1080 span_set
->strip_offset
[count
] =
1084 (ld
, count
, map
)->spanRowDataSize
;
1085 printk(KERN_INFO
"megasas:"
1086 "span %x rowDataSize %x\n",
1087 count
, MR_LdSpanPtrGet
1088 (ld
, count
, map
)->spanRowDataSize
);
1092 span_set
->span_row_data_width
= span_row_width
;
1093 span_row
= mega_div64_32(((le64_to_cpu(quad
->logEnd
) -
1094 le64_to_cpu(quad
->logStart
)) + le32_to_cpu(quad
->diff
)),
1095 le32_to_cpu(quad
->diff
));
1098 span_set
->log_start_lba
= 0;
1099 span_set
->log_end_lba
=
1100 ((span_row
<< raid
->stripeShift
)
1101 * span_row_width
) - 1;
1103 span_set
->span_row_start
= 0;
1104 span_set
->span_row_end
= span_row
- 1;
1106 span_set
->data_strip_start
= 0;
1107 span_set
->data_strip_end
=
1108 (span_row
* span_row_width
) - 1;
1110 span_set
->data_row_start
= 0;
1111 span_set
->data_row_end
=
1112 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1114 span_set_prev
= &(ldSpanInfo
[ld
].
1115 span_set
[element
- 1]);
1116 span_set
->log_start_lba
=
1117 span_set_prev
->log_end_lba
+ 1;
1118 span_set
->log_end_lba
=
1119 span_set
->log_start_lba
+
1120 ((span_row
<< raid
->stripeShift
)
1121 * span_row_width
) - 1;
1123 span_set
->span_row_start
=
1124 span_set_prev
->span_row_end
+ 1;
1125 span_set
->span_row_end
=
1126 span_set
->span_row_start
+ span_row
- 1;
1128 span_set
->data_strip_start
=
1129 span_set_prev
->data_strip_end
+ 1;
1130 span_set
->data_strip_end
=
1131 span_set
->data_strip_start
+
1132 (span_row
* span_row_width
) - 1;
1134 span_set
->data_row_start
=
1135 span_set_prev
->data_row_end
+ 1;
1136 span_set
->data_row_end
=
1137 span_set
->data_row_start
+
1138 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1142 if (span
== raid
->spanDepth
)
1147 getSpanInfo(map
, ldSpanInfo
);
1153 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL
*map
,
1154 struct LD_LOAD_BALANCE_INFO
*lbInfo
)
1158 struct MR_LD_RAID
*raid
;
1160 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES
; ldCount
++) {
1161 ld
= MR_TargetIdToLdGet(ldCount
, map
);
1162 if (ld
>= MAX_LOGICAL_DRIVES
) {
1163 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1167 raid
= MR_LdRaidGet(ld
, map
);
1169 /* Two drive Optimal RAID 1 */
1170 if ((raid
->level
== 1) && (raid
->rowSize
== 2) &&
1171 (raid
->spanDepth
== 1) && raid
->ldState
==
1172 MR_LD_STATE_OPTIMAL
) {
1175 lbInfo
[ldCount
].loadBalanceFlag
= 1;
1177 /* Get the array on which this span is present */
1178 arRef
= MR_LdSpanArrayGet(ld
, 0, map
);
1181 pd
= MR_ArPdGet(arRef
, 0, map
);
1182 /* Get dev handle from Pd */
1183 lbInfo
[ldCount
].raid1DevHandle
[0] =
1184 MR_PdDevHandleGet(pd
, map
);
1186 pd
= MR_ArPdGet(arRef
, 1, map
);
1188 /* Get the dev handle from Pd */
1189 lbInfo
[ldCount
].raid1DevHandle
[1] =
1190 MR_PdDevHandleGet(pd
, map
);
1192 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1196 u8
megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO
*lbInfo
, u8 arm
, u64 block
,
1203 /* get the pending cmds for the data and mirror arms */
1204 pend0
= atomic_read(&lbInfo
->scsi_pending_cmds
[0]);
1205 pend1
= atomic_read(&lbInfo
->scsi_pending_cmds
[1]);
1207 /* Determine the disk whose head is nearer to the req. block */
1208 diff0
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[0]);
1209 diff1
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[1]);
1210 bestArm
= (diff0
<= diff1
? 0 : 1);
1212 /*Make balance count from 16 to 4 to keep driver in sync with Firmware*/
1213 if ((bestArm
== arm
&& pend0
> pend1
+ 4) ||
1214 (bestArm
!= arm
&& pend1
> pend0
+ 4))
1217 /* Update the last accessed block on the correct pd */
1218 lbInfo
->last_accessed_block
[bestArm
] = block
+ count
- 1;
1223 u16
get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO
*lbInfo
,
1224 struct IO_REQUEST_INFO
*io_info
)
1229 old_arm
= lbInfo
->raid1DevHandle
[0] == io_info
->devHandle
? 0 : 1;
1231 /* get best new arm */
1232 arm
= megasas_get_best_arm(lbInfo
, old_arm
, io_info
->ldStartBlock
,
1233 io_info
->numBlocks
);
1234 devHandle
= lbInfo
->raid1DevHandle
[arm
];
1235 atomic_inc(&lbInfo
->scsi_pending_cmds
[arm
]);