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
80 #define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
81 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
82 #define SPAN_INVALID 0xff
85 static void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL
*map
,
86 PLD_SPAN_INFO ldSpanInfo
);
87 static u8
mr_spanset_get_phy_params(struct megasas_instance
*instance
, u32 ld
,
88 u64 stripRow
, u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
89 struct RAID_CONTEXT
*pRAID_Context
, struct MR_DRV_RAID_MAP_ALL
*map
);
90 static u64
get_row_from_strip(struct megasas_instance
*instance
, u32 ld
,
91 u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
);
93 u32
mega_mod64(u64 dividend
, u32 divisor
)
99 printk(KERN_ERR
"megasas : DIVISOR is zero, in div fn\n");
101 remainder
= do_div(d
, divisor
);
106 * @param dividend : Dividend
107 * @param divisor : Divisor
111 u64
mega_div64_32(uint64_t dividend
, uint32_t divisor
)
117 printk(KERN_ERR
"megasas : DIVISOR is zero in mod fn\n");
120 remainder
= do_div(d
, divisor
);
125 struct MR_LD_RAID
*MR_LdRaidGet(u32 ld
, struct MR_DRV_RAID_MAP_ALL
*map
)
127 return &map
->raidMap
.ldSpanMap
[ld
].ldRaid
;
130 static struct MR_SPAN_BLOCK_INFO
*MR_LdSpanInfoGet(u32 ld
,
131 struct MR_DRV_RAID_MAP_ALL
134 return &map
->raidMap
.ldSpanMap
[ld
].spanBlock
[0];
137 static u8
MR_LdDataArmGet(u32 ld
, u32 armIdx
, struct MR_DRV_RAID_MAP_ALL
*map
)
139 return map
->raidMap
.ldSpanMap
[ld
].dataArmMap
[armIdx
];
142 u16
MR_ArPdGet(u32 ar
, u32 arm
, struct MR_DRV_RAID_MAP_ALL
*map
)
144 return le16_to_cpu(map
->raidMap
.arMapInfo
[ar
].pd
[arm
]);
147 u16
MR_LdSpanArrayGet(u32 ld
, u32 span
, struct MR_DRV_RAID_MAP_ALL
*map
)
149 return le16_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].span
.arrayRef
);
152 __le16
MR_PdDevHandleGet(u32 pd
, struct MR_DRV_RAID_MAP_ALL
*map
)
154 return map
->raidMap
.devHndlInfo
[pd
].curDevHdl
;
157 static u8
MR_PdInterfaceTypeGet(u32 pd
, struct MR_DRV_RAID_MAP_ALL
*map
)
159 return map
->raidMap
.devHndlInfo
[pd
].interfaceType
;
162 u16
MR_GetLDTgtId(u32 ld
, struct MR_DRV_RAID_MAP_ALL
*map
)
164 return le16_to_cpu(map
->raidMap
.ldSpanMap
[ld
].ldRaid
.targetId
);
167 u16
MR_TargetIdToLdGet(u32 ldTgtId
, struct MR_DRV_RAID_MAP_ALL
*map
)
169 return map
->raidMap
.ldTgtIdToLd
[ldTgtId
];
172 static struct MR_LD_SPAN
*MR_LdSpanPtrGet(u32 ld
, u32 span
,
173 struct MR_DRV_RAID_MAP_ALL
*map
)
175 return &map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].span
;
179 * This function will Populate Driver Map using firmware raid map
181 void MR_PopulateDrvRaidMap(struct megasas_instance
*instance
)
183 struct fusion_context
*fusion
= instance
->ctrl_context
;
184 struct MR_FW_RAID_MAP_ALL
*fw_map_old
= NULL
;
185 struct MR_FW_RAID_MAP
*pFwRaidMap
= NULL
;
188 struct MR_FW_RAID_MAP_DYNAMIC
*fw_map_dyn
;
189 struct MR_FW_RAID_MAP_EXT
*fw_map_ext
;
190 struct MR_RAID_MAP_DESC_TABLE
*desc_table
;
193 struct MR_DRV_RAID_MAP_ALL
*drv_map
=
194 fusion
->ld_drv_map
[(instance
->map_id
& 1)];
195 struct MR_DRV_RAID_MAP
*pDrvRaidMap
= &drv_map
->raidMap
;
196 void *raid_map_data
= NULL
;
198 memset(drv_map
, 0, fusion
->drv_map_sz
);
199 memset(pDrvRaidMap
->ldTgtIdToLd
,
200 0xff, (sizeof(u16
) * MAX_LOGICAL_DRIVES_DYN
));
202 if (instance
->max_raid_mapsize
) {
203 fw_map_dyn
= fusion
->ld_map
[(instance
->map_id
& 1)];
205 (struct MR_RAID_MAP_DESC_TABLE
*)((void *)fw_map_dyn
+ le32_to_cpu(fw_map_dyn
->desc_table_offset
));
206 if (desc_table
!= fw_map_dyn
->raid_map_desc_table
)
207 dev_dbg(&instance
->pdev
->dev
, "offsets of desc table are not matching desc %p original %p\n",
208 desc_table
, fw_map_dyn
->raid_map_desc_table
);
210 ld_count
= (u16
)le16_to_cpu(fw_map_dyn
->ld_count
);
211 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
212 pDrvRaidMap
->fpPdIoTimeoutSec
=
213 fw_map_dyn
->fp_pd_io_timeout_sec
;
214 pDrvRaidMap
->totalSize
=
215 cpu_to_le32(sizeof(struct MR_DRV_RAID_MAP_ALL
));
216 /* point to actual data starting point*/
217 raid_map_data
= (void *)fw_map_dyn
+
218 le32_to_cpu(fw_map_dyn
->desc_table_offset
) +
219 le32_to_cpu(fw_map_dyn
->desc_table_size
);
221 for (i
= 0; i
< le32_to_cpu(fw_map_dyn
->desc_table_num_elements
); ++i
) {
222 switch (le32_to_cpu(desc_table
->raid_map_desc_type
)) {
223 case RAID_MAP_DESC_TYPE_DEVHDL_INFO
:
224 fw_map_dyn
->dev_hndl_info
=
225 (struct MR_DEV_HANDLE_INFO
*)(raid_map_data
+ le32_to_cpu(desc_table
->raid_map_desc_offset
));
226 memcpy(pDrvRaidMap
->devHndlInfo
,
227 fw_map_dyn
->dev_hndl_info
,
228 sizeof(struct MR_DEV_HANDLE_INFO
) *
229 le32_to_cpu(desc_table
->raid_map_desc_elements
));
231 case RAID_MAP_DESC_TYPE_TGTID_INFO
:
232 fw_map_dyn
->ld_tgt_id_to_ld
=
233 (u16
*)(raid_map_data
+
234 le32_to_cpu(desc_table
->raid_map_desc_offset
));
235 for (j
= 0; j
< le32_to_cpu(desc_table
->raid_map_desc_elements
); j
++) {
236 pDrvRaidMap
->ldTgtIdToLd
[j
] =
237 le16_to_cpu(fw_map_dyn
->ld_tgt_id_to_ld
[j
]);
240 case RAID_MAP_DESC_TYPE_ARRAY_INFO
:
241 fw_map_dyn
->ar_map_info
=
242 (struct MR_ARRAY_INFO
*)
243 (raid_map_data
+ le32_to_cpu(desc_table
->raid_map_desc_offset
));
244 memcpy(pDrvRaidMap
->arMapInfo
,
245 fw_map_dyn
->ar_map_info
,
246 sizeof(struct MR_ARRAY_INFO
) *
247 le32_to_cpu(desc_table
->raid_map_desc_elements
));
249 case RAID_MAP_DESC_TYPE_SPAN_INFO
:
250 fw_map_dyn
->ld_span_map
=
251 (struct MR_LD_SPAN_MAP
*)
253 le32_to_cpu(desc_table
->raid_map_desc_offset
));
254 memcpy(pDrvRaidMap
->ldSpanMap
,
255 fw_map_dyn
->ld_span_map
,
256 sizeof(struct MR_LD_SPAN_MAP
) *
257 le32_to_cpu(desc_table
->raid_map_desc_elements
));
260 dev_dbg(&instance
->pdev
->dev
, "wrong number of desctableElements %d\n",
261 fw_map_dyn
->desc_table_num_elements
);
266 } else if (instance
->supportmax256vd
) {
268 (struct MR_FW_RAID_MAP_EXT
*)fusion
->ld_map
[(instance
->map_id
& 1)];
269 ld_count
= (u16
)le16_to_cpu(fw_map_ext
->ldCount
);
270 if (ld_count
> MAX_LOGICAL_DRIVES_EXT
) {
271 dev_dbg(&instance
->pdev
->dev
, "megaraid_sas: LD count exposed in RAID map in not valid\n");
275 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
276 pDrvRaidMap
->fpPdIoTimeoutSec
= fw_map_ext
->fpPdIoTimeoutSec
;
277 for (i
= 0; i
< (MAX_LOGICAL_DRIVES_EXT
); i
++)
278 pDrvRaidMap
->ldTgtIdToLd
[i
] =
279 (u16
)fw_map_ext
->ldTgtIdToLd
[i
];
280 memcpy(pDrvRaidMap
->ldSpanMap
, fw_map_ext
->ldSpanMap
,
281 sizeof(struct MR_LD_SPAN_MAP
) * ld_count
);
282 memcpy(pDrvRaidMap
->arMapInfo
, fw_map_ext
->arMapInfo
,
283 sizeof(struct MR_ARRAY_INFO
) * MAX_API_ARRAYS_EXT
);
284 memcpy(pDrvRaidMap
->devHndlInfo
, fw_map_ext
->devHndlInfo
,
285 sizeof(struct MR_DEV_HANDLE_INFO
) *
286 MAX_RAIDMAP_PHYSICAL_DEVICES
);
288 /* New Raid map will not set totalSize, so keep expected value
289 * for legacy code in ValidateMapInfo
291 pDrvRaidMap
->totalSize
=
292 cpu_to_le32(sizeof(struct MR_FW_RAID_MAP_EXT
));
294 fw_map_old
= (struct MR_FW_RAID_MAP_ALL
*)
295 fusion
->ld_map
[(instance
->map_id
& 1)];
296 pFwRaidMap
= &fw_map_old
->raidMap
;
297 ld_count
= (u16
)le32_to_cpu(pFwRaidMap
->ldCount
);
298 pDrvRaidMap
->totalSize
= pFwRaidMap
->totalSize
;
299 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
300 pDrvRaidMap
->fpPdIoTimeoutSec
= pFwRaidMap
->fpPdIoTimeoutSec
;
301 for (i
= 0; i
< MAX_RAIDMAP_LOGICAL_DRIVES
+ MAX_RAIDMAP_VIEWS
; i
++)
302 pDrvRaidMap
->ldTgtIdToLd
[i
] =
303 (u8
)pFwRaidMap
->ldTgtIdToLd
[i
];
304 for (i
= 0; i
< ld_count
; i
++) {
305 pDrvRaidMap
->ldSpanMap
[i
] = pFwRaidMap
->ldSpanMap
[i
];
307 memcpy(pDrvRaidMap
->arMapInfo
, pFwRaidMap
->arMapInfo
,
308 sizeof(struct MR_ARRAY_INFO
) * MAX_RAIDMAP_ARRAYS
);
309 memcpy(pDrvRaidMap
->devHndlInfo
, pFwRaidMap
->devHndlInfo
,
310 sizeof(struct MR_DEV_HANDLE_INFO
) *
311 MAX_RAIDMAP_PHYSICAL_DEVICES
);
316 * This function will validate Map info data provided by FW
318 u8
MR_ValidateMapInfo(struct megasas_instance
*instance
)
320 struct fusion_context
*fusion
;
321 struct MR_DRV_RAID_MAP_ALL
*drv_map
;
322 struct MR_DRV_RAID_MAP
*pDrvRaidMap
;
323 struct LD_LOAD_BALANCE_INFO
*lbInfo
;
324 PLD_SPAN_INFO ldSpanInfo
;
325 struct MR_LD_RAID
*raid
;
331 MR_PopulateDrvRaidMap(instance
);
333 fusion
= instance
->ctrl_context
;
334 drv_map
= fusion
->ld_drv_map
[(instance
->map_id
& 1)];
335 pDrvRaidMap
= &drv_map
->raidMap
;
337 lbInfo
= fusion
->load_balance_info
;
338 ldSpanInfo
= fusion
->log_to_span
;
340 if (instance
->max_raid_mapsize
)
341 expected_size
= sizeof(struct MR_DRV_RAID_MAP_ALL
);
342 else if (instance
->supportmax256vd
)
343 expected_size
= sizeof(struct MR_FW_RAID_MAP_EXT
);
346 (sizeof(struct MR_FW_RAID_MAP
) - sizeof(struct MR_LD_SPAN_MAP
) +
347 (sizeof(struct MR_LD_SPAN_MAP
) * le16_to_cpu(pDrvRaidMap
->ldCount
)));
349 if (le32_to_cpu(pDrvRaidMap
->totalSize
) != expected_size
) {
350 dev_dbg(&instance
->pdev
->dev
, "megasas: map info structure size 0x%x",
351 le32_to_cpu(pDrvRaidMap
->totalSize
));
352 dev_dbg(&instance
->pdev
->dev
, "is not matching expected size 0x%x\n",
353 (unsigned int)expected_size
);
354 dev_err(&instance
->pdev
->dev
, "megasas: span map %x, pDrvRaidMap->totalSize : %x\n",
355 (unsigned int)sizeof(struct MR_LD_SPAN_MAP
),
356 le32_to_cpu(pDrvRaidMap
->totalSize
));
360 if (instance
->UnevenSpanSupport
)
361 mr_update_span_set(drv_map
, ldSpanInfo
);
364 mr_update_load_balance_params(drv_map
, lbInfo
);
366 num_lds
= le16_to_cpu(drv_map
->raidMap
.ldCount
);
368 /*Convert Raid capability values to CPU arch */
369 for (i
= 0; (num_lds
> 0) && (i
< MAX_LOGICAL_DRIVES_EXT
); i
++) {
370 ld
= MR_TargetIdToLdGet(i
, drv_map
);
372 /* For non existing VDs, iterate to next VD*/
373 if (ld
>= (MAX_LOGICAL_DRIVES_EXT
- 1))
376 raid
= MR_LdRaidGet(ld
, drv_map
);
377 le32_to_cpus((u32
*)&raid
->capability
);
385 u32
MR_GetSpanBlock(u32 ld
, u64 row
, u64
*span_blk
,
386 struct MR_DRV_RAID_MAP_ALL
*map
)
388 struct MR_SPAN_BLOCK_INFO
*pSpanBlock
= MR_LdSpanInfoGet(ld
, map
);
389 struct MR_QUAD_ELEMENT
*quad
;
390 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
393 for (span
= 0; span
< raid
->spanDepth
; span
++, pSpanBlock
++) {
395 for (j
= 0; j
< le32_to_cpu(pSpanBlock
->block_span_info
.noElements
); j
++) {
396 quad
= &pSpanBlock
->block_span_info
.quad
[j
];
398 if (le32_to_cpu(quad
->diff
) == 0)
400 if (le64_to_cpu(quad
->logStart
) <= row
&& row
<=
401 le64_to_cpu(quad
->logEnd
) && (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
402 le32_to_cpu(quad
->diff
))) == 0) {
403 if (span_blk
!= NULL
) {
405 blk
= mega_div64_32((row
-le64_to_cpu(quad
->logStart
)), le32_to_cpu(quad
->diff
));
408 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
)) << raid
->stripeShift
;
419 ******************************************************************************
421 * This routine calculates the Span block for given row using spanset.
424 * instance - HBA instance
425 * ld - Logical drive number
432 * block - Absolute Block number in the physical disk
433 * div_error - Devide error code.
436 u32
mr_spanset_get_span_block(struct megasas_instance
*instance
,
437 u32 ld
, u64 row
, u64
*span_blk
, struct MR_DRV_RAID_MAP_ALL
*map
)
439 struct fusion_context
*fusion
= instance
->ctrl_context
;
440 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
441 LD_SPAN_SET
*span_set
;
442 struct MR_QUAD_ELEMENT
*quad
;
444 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
446 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
447 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
449 if (span_set
->span_row_data_width
== 0)
452 if (row
> span_set
->data_row_end
)
455 for (span
= 0; span
< raid
->spanDepth
; span
++)
456 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
457 block_span_info
.noElements
) >= info
+1) {
458 quad
= &map
->raidMap
.ldSpanMap
[ld
].
460 block_span_info
.quad
[info
];
461 if (le32_to_cpu(quad
->diff
) == 0)
463 if (le64_to_cpu(quad
->logStart
) <= row
&&
464 row
<= le64_to_cpu(quad
->logEnd
) &&
465 (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
466 le32_to_cpu(quad
->diff
))) == 0) {
467 if (span_blk
!= NULL
) {
470 ((row
- le64_to_cpu(quad
->logStart
)),
471 le32_to_cpu(quad
->diff
));
472 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
))
473 << raid
->stripeShift
;
484 ******************************************************************************
486 * This routine calculates the row for given strip using spanset.
489 * instance - HBA instance
490 * ld - Logical drive number
496 * row - row associated with strip
499 static u64
get_row_from_strip(struct megasas_instance
*instance
,
500 u32 ld
, u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
)
502 struct fusion_context
*fusion
= instance
->ctrl_context
;
503 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
504 LD_SPAN_SET
*span_set
;
505 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
506 u32 info
, strip_offset
, span
, span_offset
;
507 u64 span_set_Strip
, span_set_Row
, retval
;
509 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
510 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
512 if (span_set
->span_row_data_width
== 0)
514 if (strip
> span_set
->data_strip_end
)
517 span_set_Strip
= strip
- span_set
->data_strip_start
;
518 strip_offset
= mega_mod64(span_set_Strip
,
519 span_set
->span_row_data_width
);
520 span_set_Row
= mega_div64_32(span_set_Strip
,
521 span_set
->span_row_data_width
) * span_set
->diff
;
522 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
523 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
524 block_span_info
.noElements
) >= info
+1) {
526 span_set
->strip_offset
[span
])
532 retval
= (span_set
->data_row_start
+ span_set_Row
+
541 ******************************************************************************
543 * This routine calculates the Start Strip for given row using spanset.
546 * instance - HBA instance
547 * ld - Logical drive number
553 * Strip - Start strip associated with row
556 static u64
get_strip_from_row(struct megasas_instance
*instance
,
557 u32 ld
, u64 row
, struct MR_DRV_RAID_MAP_ALL
*map
)
559 struct fusion_context
*fusion
= instance
->ctrl_context
;
560 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
561 LD_SPAN_SET
*span_set
;
562 struct MR_QUAD_ELEMENT
*quad
;
563 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
567 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
568 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
570 if (span_set
->span_row_data_width
== 0)
572 if (row
> span_set
->data_row_end
)
575 for (span
= 0; span
< raid
->spanDepth
; span
++)
576 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
577 block_span_info
.noElements
) >= info
+1) {
578 quad
= &map
->raidMap
.ldSpanMap
[ld
].
579 spanBlock
[span
].block_span_info
.quad
[info
];
580 if (le64_to_cpu(quad
->logStart
) <= row
&&
581 row
<= le64_to_cpu(quad
->logEnd
) &&
582 mega_mod64((row
- le64_to_cpu(quad
->logStart
)),
583 le32_to_cpu(quad
->diff
)) == 0) {
584 strip
= mega_div64_32
585 (((row
- span_set
->data_row_start
)
586 - le64_to_cpu(quad
->logStart
)),
587 le32_to_cpu(quad
->diff
));
588 strip
*= span_set
->span_row_data_width
;
589 strip
+= span_set
->data_strip_start
;
590 strip
+= span_set
->strip_offset
[span
];
595 dev_err(&instance
->pdev
->dev
, "get_strip_from_row"
596 "returns invalid strip for ld=%x, row=%lx\n",
597 ld
, (long unsigned int)row
);
602 ******************************************************************************
604 * This routine calculates the Physical Arm for given strip using spanset.
607 * instance - HBA instance
608 * ld - Logical drive number
614 * Phys Arm - Phys Arm associated with strip
617 static u32
get_arm_from_strip(struct megasas_instance
*instance
,
618 u32 ld
, u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
)
620 struct fusion_context
*fusion
= instance
->ctrl_context
;
621 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
622 LD_SPAN_SET
*span_set
;
623 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
624 u32 info
, strip_offset
, span
, span_offset
, retval
;
626 for (info
= 0 ; info
< MAX_QUAD_DEPTH
; info
++) {
627 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
629 if (span_set
->span_row_data_width
== 0)
631 if (strip
> span_set
->data_strip_end
)
634 strip_offset
= (uint
)mega_mod64
635 ((strip
- span_set
->data_strip_start
),
636 span_set
->span_row_data_width
);
638 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
639 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
640 block_span_info
.noElements
) >= info
+1) {
642 span_set
->strip_offset
[span
])
644 span_set
->strip_offset
[span
];
649 retval
= (strip_offset
- span_offset
);
653 dev_err(&instance
->pdev
->dev
, "get_arm_from_strip"
654 "returns invalid arm for ld=%x strip=%lx\n",
655 ld
, (long unsigned int)strip
);
660 /* This Function will return Phys arm */
661 u8
get_arm(struct megasas_instance
*instance
, u32 ld
, u8 span
, u64 stripe
,
662 struct MR_DRV_RAID_MAP_ALL
*map
)
664 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
665 /* Need to check correct default value */
668 switch (raid
->level
) {
672 arm
= mega_mod64(stripe
, SPAN_ROW_SIZE(map
, ld
, span
));
675 /* start with logical arm */
676 arm
= get_arm_from_strip(instance
, ld
, stripe
, map
);
687 ******************************************************************************
689 * This routine calculates the arm, span and block for the specified stripe and
690 * reference in stripe using spanset
694 * ld - Logical drive number
695 * stripRow - Stripe number
696 * stripRef - Reference in stripe
701 * block - Absolute Block number in the physical disk
703 static u8
mr_spanset_get_phy_params(struct megasas_instance
*instance
, u32 ld
,
704 u64 stripRow
, u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
705 struct RAID_CONTEXT
*pRAID_Context
,
706 struct MR_DRV_RAID_MAP_ALL
*map
)
708 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
709 u32 pd
, arRef
, r1_alt_pd
;
713 u64
*pdBlock
= &io_info
->pdBlock
;
714 __le16
*pDevHandle
= &io_info
->devHandle
;
715 u8
*pPdInterface
= &io_info
->pd_interface
;
716 u32 logArm
, rowMod
, armQ
, arm
;
717 struct fusion_context
*fusion
;
719 fusion
= instance
->ctrl_context
;
720 *pDevHandle
= cpu_to_le16(MR_DEVHANDLE_INVALID
);
722 /*Get row and span from io_info for Uneven Span IO.*/
723 row
= io_info
->start_row
;
724 span
= io_info
->start_span
;
727 if (raid
->level
== 6) {
728 logArm
= get_arm_from_strip(instance
, ld
, stripRow
, map
);
731 rowMod
= mega_mod64(row
, SPAN_ROW_SIZE(map
, ld
, span
));
732 armQ
= SPAN_ROW_SIZE(map
, ld
, span
) - 1 - rowMod
;
733 arm
= armQ
+ 1 + logArm
;
734 if (arm
>= SPAN_ROW_SIZE(map
, ld
, span
))
735 arm
-= SPAN_ROW_SIZE(map
, ld
, span
);
738 /* Calculate the arm */
739 physArm
= get_arm(instance
, ld
, span
, stripRow
, map
);
743 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
744 pd
= MR_ArPdGet(arRef
, physArm
, map
);
746 if (pd
!= MR_PD_INVALID
) {
747 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
748 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
749 /* get second pd also for raid 1/10 fast path writes*/
750 if (instance
->is_ventura
&&
751 (raid
->level
== 1) &&
753 r1_alt_pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
754 if (r1_alt_pd
!= MR_PD_INVALID
)
755 io_info
->r1_alt_dev_handle
=
756 MR_PdDevHandleGet(r1_alt_pd
, map
);
759 if ((raid
->level
>= 5) &&
760 ((fusion
->adapter_type
== THUNDERBOLT_SERIES
) ||
761 ((fusion
->adapter_type
== INVADER_SERIES
) &&
762 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
763 pRAID_Context
->reg_lock_flags
= REGION_TYPE_EXCLUSIVE
;
764 else if (raid
->level
== 1) {
765 physArm
= physArm
+ 1;
766 pd
= MR_ArPdGet(arRef
, physArm
, map
);
767 if (pd
!= MR_PD_INVALID
) {
768 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
769 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
774 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
775 if (instance
->is_ventura
) {
776 ((struct RAID_CONTEXT_G35
*)pRAID_Context
)->span_arm
=
777 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
779 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
781 pRAID_Context
->span_arm
=
782 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
783 io_info
->span_arm
= pRAID_Context
->span_arm
;
785 io_info
->pd_after_lb
= pd
;
790 ******************************************************************************
792 * This routine calculates the arm, span and block for the specified stripe and
793 * reference in stripe.
797 * ld - Logical drive number
798 * stripRow - Stripe number
799 * stripRef - Reference in stripe
804 * block - Absolute Block number in the physical disk
806 u8
MR_GetPhyParams(struct megasas_instance
*instance
, u32 ld
, u64 stripRow
,
807 u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
808 struct RAID_CONTEXT
*pRAID_Context
,
809 struct MR_DRV_RAID_MAP_ALL
*map
)
811 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
812 u32 pd
, arRef
, r1_alt_pd
;
816 u64
*pdBlock
= &io_info
->pdBlock
;
817 __le16
*pDevHandle
= &io_info
->devHandle
;
818 u8
*pPdInterface
= &io_info
->pd_interface
;
819 struct fusion_context
*fusion
;
821 fusion
= instance
->ctrl_context
;
822 *pDevHandle
= cpu_to_le16(MR_DEVHANDLE_INVALID
);
824 row
= mega_div64_32(stripRow
, raid
->rowDataSize
);
826 if (raid
->level
== 6) {
827 /* logical arm within row */
828 u32 logArm
= mega_mod64(stripRow
, raid
->rowDataSize
);
829 u32 rowMod
, armQ
, arm
;
831 if (raid
->rowSize
== 0)
833 /* get logical row mod */
834 rowMod
= mega_mod64(row
, raid
->rowSize
);
835 armQ
= raid
->rowSize
-1-rowMod
; /* index of Q drive */
836 arm
= armQ
+1+logArm
; /* data always logically follows Q */
837 if (arm
>= raid
->rowSize
) /* handle wrap condition */
838 arm
-= raid
->rowSize
;
841 if (raid
->modFactor
== 0)
843 physArm
= MR_LdDataArmGet(ld
, mega_mod64(stripRow
,
848 if (raid
->spanDepth
== 1) {
850 *pdBlock
= row
<< raid
->stripeShift
;
852 span
= (u8
)MR_GetSpanBlock(ld
, row
, pdBlock
, map
);
853 if (span
== SPAN_INVALID
)
857 /* Get the array on which this span is present */
858 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
859 pd
= MR_ArPdGet(arRef
, physArm
, map
); /* Get the pd */
861 if (pd
!= MR_PD_INVALID
) {
862 /* Get dev handle from Pd. */
863 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
864 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
865 /* get second pd also for raid 1/10 fast path writes*/
866 if (instance
->is_ventura
&&
867 (raid
->level
== 1) &&
869 r1_alt_pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
870 if (r1_alt_pd
!= MR_PD_INVALID
)
871 io_info
->r1_alt_dev_handle
=
872 MR_PdDevHandleGet(r1_alt_pd
, map
);
875 if ((raid
->level
>= 5) &&
876 ((fusion
->adapter_type
== THUNDERBOLT_SERIES
) ||
877 ((fusion
->adapter_type
== INVADER_SERIES
) &&
878 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
879 pRAID_Context
->reg_lock_flags
= REGION_TYPE_EXCLUSIVE
;
880 else if (raid
->level
== 1) {
881 /* Get alternate Pd. */
882 physArm
= physArm
+ 1;
883 pd
= MR_ArPdGet(arRef
, physArm
, map
);
884 if (pd
!= MR_PD_INVALID
) {
885 /* Get dev handle from Pd */
886 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
887 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
892 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
893 if (instance
->is_ventura
) {
894 ((struct RAID_CONTEXT_G35
*)pRAID_Context
)->span_arm
=
895 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
897 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
899 pRAID_Context
->span_arm
=
900 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
901 io_info
->span_arm
= pRAID_Context
->span_arm
;
903 io_info
->pd_after_lb
= pd
;
908 ******************************************************************************
910 * MR_BuildRaidContext function
912 * This function will initiate command processing. The start/end row and strip
913 * information is calculated then the lock is acquired.
914 * This function will return 0 if region lock was acquired OR return num strips
917 MR_BuildRaidContext(struct megasas_instance
*instance
,
918 struct IO_REQUEST_INFO
*io_info
,
919 struct RAID_CONTEXT
*pRAID_Context
,
920 struct MR_DRV_RAID_MAP_ALL
*map
, u8
**raidLUN
)
922 struct fusion_context
*fusion
;
923 struct MR_LD_RAID
*raid
;
924 u32 stripSize
, stripe_mask
;
925 u64 endLba
, endStrip
, endRow
, start_row
, start_strip
;
928 u8 num_strips
, numRows
;
929 u16 ref_in_start_stripe
, ref_in_end_stripe
;
931 u32 numBlocks
, ldTgtId
;
934 u8 startlba_span
= SPAN_INVALID
;
935 u64
*pdBlock
= &io_info
->pdBlock
;
938 ldStartBlock
= io_info
->ldStartBlock
;
939 numBlocks
= io_info
->numBlocks
;
940 ldTgtId
= io_info
->ldTgtId
;
941 isRead
= io_info
->isRead
;
942 io_info
->IoforUnevenSpan
= 0;
943 io_info
->start_span
= SPAN_INVALID
;
944 fusion
= instance
->ctrl_context
;
946 ld
= MR_TargetIdToLdGet(ldTgtId
, map
);
947 raid
= MR_LdRaidGet(ld
, map
);
948 /*check read ahead bit*/
949 io_info
->ra_capable
= raid
->capability
.ra_capable
;
952 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
955 if (raid
->rowDataSize
== 0) {
956 if (MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
== 0)
958 else if (instance
->UnevenSpanSupport
) {
959 io_info
->IoforUnevenSpan
= 1;
961 dev_info(&instance
->pdev
->dev
,
962 "raid->rowDataSize is 0, but has SPAN[0]"
963 "rowDataSize = 0x%0x,"
964 "but there is _NO_ UnevenSpanSupport\n",
965 MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
);
970 stripSize
= 1 << raid
->stripeShift
;
971 stripe_mask
= stripSize
-1;
975 * calculate starting row and stripe, and number of strips and rows
977 start_strip
= ldStartBlock
>> raid
->stripeShift
;
978 ref_in_start_stripe
= (u16
)(ldStartBlock
& stripe_mask
);
979 endLba
= ldStartBlock
+ numBlocks
- 1;
980 ref_in_end_stripe
= (u16
)(endLba
& stripe_mask
);
981 endStrip
= endLba
>> raid
->stripeShift
;
982 num_strips
= (u8
)(endStrip
- start_strip
+ 1); /* End strip */
984 if (io_info
->IoforUnevenSpan
) {
985 start_row
= get_row_from_strip(instance
, ld
, start_strip
, map
);
986 endRow
= get_row_from_strip(instance
, ld
, endStrip
, map
);
987 if (start_row
== -1ULL || endRow
== -1ULL) {
988 dev_info(&instance
->pdev
->dev
, "return from %s %d."
989 "Send IO w/o region lock.\n",
994 if (raid
->spanDepth
== 1) {
996 *pdBlock
= start_row
<< raid
->stripeShift
;
998 startlba_span
= (u8
)mr_spanset_get_span_block(instance
,
999 ld
, start_row
, pdBlock
, map
);
1000 if (startlba_span
== SPAN_INVALID
) {
1001 dev_info(&instance
->pdev
->dev
, "return from %s %d"
1002 "for row 0x%llx,start strip %llx"
1003 "endSrip %llx\n", __func__
, __LINE__
,
1004 (unsigned long long)start_row
,
1005 (unsigned long long)start_strip
,
1006 (unsigned long long)endStrip
);
1009 io_info
->start_span
= startlba_span
;
1010 io_info
->start_row
= start_row
;
1012 start_row
= mega_div64_32(start_strip
, raid
->rowDataSize
);
1013 endRow
= mega_div64_32(endStrip
, raid
->rowDataSize
);
1015 numRows
= (u8
)(endRow
- start_row
+ 1);
1018 * calculate region info.
1021 /* assume region is at the start of the first row */
1022 regStart
= start_row
<< raid
->stripeShift
;
1023 /* assume this IO needs the full row - we'll adjust if not true */
1024 regSize
= stripSize
;
1026 io_info
->do_fp_rlbypass
= raid
->capability
.fpBypassRegionLock
;
1028 /* Check if we can send this I/O via FastPath */
1029 if (raid
->capability
.fpCapable
) {
1031 io_info
->fpOkForIo
= (raid
->capability
.fpReadCapable
&&
1032 ((num_strips
== 1) ||
1034 fpReadAcrossStripe
));
1036 io_info
->fpOkForIo
= (raid
->capability
.fpWriteCapable
&&
1037 ((num_strips
== 1) ||
1039 fpWriteAcrossStripe
));
1041 io_info
->fpOkForIo
= FALSE
;
1044 /* single-strip IOs can always lock only the data needed */
1045 if (num_strips
== 1) {
1046 regStart
+= ref_in_start_stripe
;
1047 regSize
= numBlocks
;
1049 /* multi-strip IOs always need to full stripe locked */
1050 } else if (io_info
->IoforUnevenSpan
== 0) {
1052 * For Even span region lock optimization.
1053 * If the start strip is the last in the start row
1055 if (start_strip
== (start_row
+ 1) * raid
->rowDataSize
- 1) {
1056 regStart
+= ref_in_start_stripe
;
1057 /* initialize count to sectors from startref to end
1059 regSize
= stripSize
- ref_in_start_stripe
;
1062 /* add complete rows in the middle of the transfer */
1064 regSize
+= (numRows
-2) << raid
->stripeShift
;
1066 /* if IO ends within first strip of last row*/
1067 if (endStrip
== endRow
*raid
->rowDataSize
)
1068 regSize
+= ref_in_end_stripe
+1;
1070 regSize
+= stripSize
;
1073 * For Uneven span region lock optimization.
1074 * If the start strip is the last in the start row
1076 if (start_strip
== (get_strip_from_row(instance
, ld
, start_row
, map
) +
1077 SPAN_ROW_DATA_SIZE(map
, ld
, startlba_span
) - 1)) {
1078 regStart
+= ref_in_start_stripe
;
1079 /* initialize count to sectors from
1080 * startRef to end of strip
1082 regSize
= stripSize
- ref_in_start_stripe
;
1084 /* Add complete rows in the middle of the transfer*/
1087 /* Add complete rows in the middle of the transfer*/
1088 regSize
+= (numRows
-2) << raid
->stripeShift
;
1090 /* if IO ends within first strip of last row */
1091 if (endStrip
== get_strip_from_row(instance
, ld
, endRow
, map
))
1092 regSize
+= ref_in_end_stripe
+ 1;
1094 regSize
+= stripSize
;
1097 pRAID_Context
->timeout_value
=
1098 cpu_to_le16(raid
->fpIoTimeoutForLd
?
1099 raid
->fpIoTimeoutForLd
:
1100 map
->raidMap
.fpPdIoTimeoutSec
);
1101 if (fusion
->adapter_type
== INVADER_SERIES
)
1102 pRAID_Context
->reg_lock_flags
= (isRead
) ?
1103 raid
->regTypeReqOnRead
: raid
->regTypeReqOnWrite
;
1104 else if (!instance
->is_ventura
)
1105 pRAID_Context
->reg_lock_flags
= (isRead
) ?
1106 REGION_TYPE_SHARED_READ
: raid
->regTypeReqOnWrite
;
1107 pRAID_Context
->virtual_disk_tgt_id
= raid
->targetId
;
1108 pRAID_Context
->reg_lock_row_lba
= cpu_to_le64(regStart
);
1109 pRAID_Context
->reg_lock_length
= cpu_to_le32(regSize
);
1110 pRAID_Context
->config_seq_num
= raid
->seqNum
;
1111 /* save pointer to raid->LUN array */
1112 *raidLUN
= raid
->LUN
;
1115 /*Get Phy Params only if FP capable, or else leave it to MR firmware
1116 to do the calculation.*/
1117 if (io_info
->fpOkForIo
) {
1118 retval
= io_info
->IoforUnevenSpan
?
1119 mr_spanset_get_phy_params(instance
, ld
,
1120 start_strip
, ref_in_start_stripe
,
1121 io_info
, pRAID_Context
, map
) :
1122 MR_GetPhyParams(instance
, ld
, start_strip
,
1123 ref_in_start_stripe
, io_info
,
1124 pRAID_Context
, map
);
1125 /* If IO on an invalid Pd, then FP is not possible.*/
1126 if (io_info
->devHandle
== MR_DEVHANDLE_INVALID
)
1127 io_info
->fpOkForIo
= FALSE
;
1129 } else if (isRead
) {
1131 for (stripIdx
= 0; stripIdx
< num_strips
; stripIdx
++) {
1132 retval
= io_info
->IoforUnevenSpan
?
1133 mr_spanset_get_phy_params(instance
, ld
,
1134 start_strip
+ stripIdx
,
1135 ref_in_start_stripe
, io_info
,
1136 pRAID_Context
, map
) :
1137 MR_GetPhyParams(instance
, ld
,
1138 start_strip
+ stripIdx
, ref_in_start_stripe
,
1139 io_info
, pRAID_Context
, map
);
1148 ******************************************************************************
1150 * This routine pepare spanset info from Valid Raid map and store it into
1151 * local copy of ldSpanInfo per instance data structure.
1155 * ldSpanInfo - ldSpanInfo per HBA instance
1158 void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL
*map
,
1159 PLD_SPAN_INFO ldSpanInfo
)
1162 u32 element
, span_row_width
;
1164 struct MR_LD_RAID
*raid
;
1165 LD_SPAN_SET
*span_set
, *span_set_prev
;
1166 struct MR_QUAD_ELEMENT
*quad
;
1171 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES_EXT
; ldCount
++) {
1172 ld
= MR_TargetIdToLdGet(ldCount
, map
);
1173 if (ld
>= (MAX_LOGICAL_DRIVES_EXT
- 1))
1175 raid
= MR_LdRaidGet(ld
, map
);
1176 for (element
= 0; element
< MAX_QUAD_DEPTH
; element
++) {
1177 for (span
= 0; span
< raid
->spanDepth
; span
++) {
1178 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
1179 block_span_info
.noElements
) <
1182 span_set
= &(ldSpanInfo
[ld
].span_set
[element
]);
1183 quad
= &map
->raidMap
.ldSpanMap
[ld
].
1184 spanBlock
[span
].block_span_info
.
1187 span_set
->diff
= le32_to_cpu(quad
->diff
);
1189 for (count
= 0, span_row_width
= 0;
1190 count
< raid
->spanDepth
; count
++) {
1191 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].
1194 noElements
) >= element
+ 1) {
1195 span_set
->strip_offset
[count
] =
1199 (ld
, count
, map
)->spanRowDataSize
;
1203 span_set
->span_row_data_width
= span_row_width
;
1204 span_row
= mega_div64_32(((le64_to_cpu(quad
->logEnd
) -
1205 le64_to_cpu(quad
->logStart
)) + le32_to_cpu(quad
->diff
)),
1206 le32_to_cpu(quad
->diff
));
1209 span_set
->log_start_lba
= 0;
1210 span_set
->log_end_lba
=
1211 ((span_row
<< raid
->stripeShift
)
1212 * span_row_width
) - 1;
1214 span_set
->span_row_start
= 0;
1215 span_set
->span_row_end
= span_row
- 1;
1217 span_set
->data_strip_start
= 0;
1218 span_set
->data_strip_end
=
1219 (span_row
* span_row_width
) - 1;
1221 span_set
->data_row_start
= 0;
1222 span_set
->data_row_end
=
1223 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1225 span_set_prev
= &(ldSpanInfo
[ld
].
1226 span_set
[element
- 1]);
1227 span_set
->log_start_lba
=
1228 span_set_prev
->log_end_lba
+ 1;
1229 span_set
->log_end_lba
=
1230 span_set
->log_start_lba
+
1231 ((span_row
<< raid
->stripeShift
)
1232 * span_row_width
) - 1;
1234 span_set
->span_row_start
=
1235 span_set_prev
->span_row_end
+ 1;
1236 span_set
->span_row_end
=
1237 span_set
->span_row_start
+ span_row
- 1;
1239 span_set
->data_strip_start
=
1240 span_set_prev
->data_strip_end
+ 1;
1241 span_set
->data_strip_end
=
1242 span_set
->data_strip_start
+
1243 (span_row
* span_row_width
) - 1;
1245 span_set
->data_row_start
=
1246 span_set_prev
->data_row_end
+ 1;
1247 span_set
->data_row_end
=
1248 span_set
->data_row_start
+
1249 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1253 if (span
== raid
->spanDepth
)
1259 void mr_update_load_balance_params(struct MR_DRV_RAID_MAP_ALL
*drv_map
,
1260 struct LD_LOAD_BALANCE_INFO
*lbInfo
)
1264 struct MR_LD_RAID
*raid
;
1266 if (lb_pending_cmds
> 128 || lb_pending_cmds
< 1)
1267 lb_pending_cmds
= LB_PENDING_CMDS_DEFAULT
;
1269 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES_EXT
; ldCount
++) {
1270 ld
= MR_TargetIdToLdGet(ldCount
, drv_map
);
1271 if (ld
>= MAX_LOGICAL_DRIVES_EXT
) {
1272 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1276 raid
= MR_LdRaidGet(ld
, drv_map
);
1277 if ((raid
->level
!= 1) ||
1278 (raid
->ldState
!= MR_LD_STATE_OPTIMAL
)) {
1279 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1282 lbInfo
[ldCount
].loadBalanceFlag
= 1;
1286 u8
megasas_get_best_arm_pd(struct megasas_instance
*instance
,
1287 struct LD_LOAD_BALANCE_INFO
*lbInfo
,
1288 struct IO_REQUEST_INFO
*io_info
,
1289 struct MR_DRV_RAID_MAP_ALL
*drv_map
)
1291 struct MR_LD_RAID
*raid
;
1293 u16 pend0
, pend1
, ld
;
1295 u8 bestArm
, pd0
, pd1
, span
, arm
;
1296 u32 arRef
, span_row_size
;
1298 u64 block
= io_info
->ldStartBlock
;
1299 u32 count
= io_info
->numBlocks
;
1301 span
= ((io_info
->span_arm
& RAID_CTX_SPANARM_SPAN_MASK
)
1302 >> RAID_CTX_SPANARM_SPAN_SHIFT
);
1303 arm
= (io_info
->span_arm
& RAID_CTX_SPANARM_ARM_MASK
);
1305 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, drv_map
);
1306 raid
= MR_LdRaidGet(ld
, drv_map
);
1307 span_row_size
= instance
->UnevenSpanSupport
?
1308 SPAN_ROW_SIZE(drv_map
, ld
, span
) : raid
->rowSize
;
1310 arRef
= MR_LdSpanArrayGet(ld
, span
, drv_map
);
1311 pd0
= MR_ArPdGet(arRef
, arm
, drv_map
);
1312 pd1
= MR_ArPdGet(arRef
, (arm
+ 1) >= span_row_size
?
1313 (arm
+ 1 - span_row_size
) : arm
+ 1, drv_map
);
1315 /* Get PD1 Dev Handle */
1317 pd1_dev_handle
= MR_PdDevHandleGet(pd1
, drv_map
);
1319 if (pd1_dev_handle
== MR_DEVHANDLE_INVALID
) {
1322 /* get the pending cmds for the data and mirror arms */
1323 pend0
= atomic_read(&lbInfo
->scsi_pending_cmds
[pd0
]);
1324 pend1
= atomic_read(&lbInfo
->scsi_pending_cmds
[pd1
]);
1326 /* Determine the disk whose head is nearer to the req. block */
1327 diff0
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[pd0
]);
1328 diff1
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[pd1
]);
1329 bestArm
= (diff0
<= diff1
? arm
: arm
^ 1);
1331 /* Make balance count from 16 to 4 to
1332 * keep driver in sync with Firmware
1334 if ((bestArm
== arm
&& pend0
> pend1
+ lb_pending_cmds
) ||
1335 (bestArm
!= arm
&& pend1
> pend0
+ lb_pending_cmds
))
1338 /* Update the last accessed block on the correct pd */
1340 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | bestArm
;
1341 io_info
->pd_after_lb
= (bestArm
== arm
) ? pd0
: pd1
;
1344 lbInfo
->last_accessed_block
[io_info
->pd_after_lb
] = block
+ count
- 1;
1345 return io_info
->pd_after_lb
;
1348 __le16
get_updated_dev_handle(struct megasas_instance
*instance
,
1349 struct LD_LOAD_BALANCE_INFO
*lbInfo
,
1350 struct IO_REQUEST_INFO
*io_info
,
1351 struct MR_DRV_RAID_MAP_ALL
*drv_map
)
1356 /* get best new arm (PD ID) */
1357 arm_pd
= megasas_get_best_arm_pd(instance
, lbInfo
, io_info
, drv_map
);
1358 devHandle
= MR_PdDevHandleGet(arm_pd
, drv_map
);
1359 io_info
->pd_interface
= MR_PdInterfaceTypeGet(arm_pd
, drv_map
);
1360 atomic_inc(&lbInfo
->scsi_pending_cmds
[arm_pd
]);