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
70 #define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
71 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
72 #define SPAN_INVALID 0xff
75 static void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL
*map
,
76 PLD_SPAN_INFO ldSpanInfo
);
77 static u8
mr_spanset_get_phy_params(struct megasas_instance
*instance
, u32 ld
,
78 u64 stripRow
, u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
79 struct RAID_CONTEXT
*pRAID_Context
, struct MR_DRV_RAID_MAP_ALL
*map
);
80 static u64
get_row_from_strip(struct megasas_instance
*instance
, u32 ld
,
81 u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
);
83 u32
mega_mod64(u64 dividend
, u32 divisor
)
89 printk(KERN_ERR
"megasas : DIVISOR is zero, in div fn\n");
91 remainder
= do_div(d
, divisor
);
96 * @param dividend : Dividend
97 * @param divisor : Divisor
101 u64
mega_div64_32(uint64_t dividend
, uint32_t divisor
)
107 printk(KERN_ERR
"megasas : DIVISOR is zero in mod fn\n");
110 remainder
= do_div(d
, divisor
);
115 struct MR_LD_RAID
*MR_LdRaidGet(u32 ld
, struct MR_DRV_RAID_MAP_ALL
*map
)
117 return &map
->raidMap
.ldSpanMap
[ld
].ldRaid
;
120 static struct MR_SPAN_BLOCK_INFO
*MR_LdSpanInfoGet(u32 ld
,
121 struct MR_DRV_RAID_MAP_ALL
124 return &map
->raidMap
.ldSpanMap
[ld
].spanBlock
[0];
127 static u8
MR_LdDataArmGet(u32 ld
, u32 armIdx
, struct MR_DRV_RAID_MAP_ALL
*map
)
129 return map
->raidMap
.ldSpanMap
[ld
].dataArmMap
[armIdx
];
132 u16
MR_ArPdGet(u32 ar
, u32 arm
, struct MR_DRV_RAID_MAP_ALL
*map
)
134 return le16_to_cpu(map
->raidMap
.arMapInfo
[ar
].pd
[arm
]);
137 u16
MR_LdSpanArrayGet(u32 ld
, u32 span
, struct MR_DRV_RAID_MAP_ALL
*map
)
139 return le16_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].span
.arrayRef
);
142 __le16
MR_PdDevHandleGet(u32 pd
, struct MR_DRV_RAID_MAP_ALL
*map
)
144 return map
->raidMap
.devHndlInfo
[pd
].curDevHdl
;
147 static u8
MR_PdInterfaceTypeGet(u32 pd
, struct MR_DRV_RAID_MAP_ALL
*map
)
149 return map
->raidMap
.devHndlInfo
[pd
].interfaceType
;
152 u16
MR_GetLDTgtId(u32 ld
, struct MR_DRV_RAID_MAP_ALL
*map
)
154 return le16_to_cpu(map
->raidMap
.ldSpanMap
[ld
].ldRaid
.targetId
);
157 u16
MR_TargetIdToLdGet(u32 ldTgtId
, struct MR_DRV_RAID_MAP_ALL
*map
)
159 return map
->raidMap
.ldTgtIdToLd
[ldTgtId
];
162 static struct MR_LD_SPAN
*MR_LdSpanPtrGet(u32 ld
, u32 span
,
163 struct MR_DRV_RAID_MAP_ALL
*map
)
165 return &map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].span
;
169 * This function will Populate Driver Map using firmware raid map
171 static int MR_PopulateDrvRaidMap(struct megasas_instance
*instance
, u64 map_id
)
173 struct fusion_context
*fusion
= instance
->ctrl_context
;
174 struct MR_FW_RAID_MAP_ALL
*fw_map_old
= NULL
;
175 struct MR_FW_RAID_MAP
*pFwRaidMap
= NULL
;
178 struct MR_FW_RAID_MAP_DYNAMIC
*fw_map_dyn
;
179 struct MR_FW_RAID_MAP_EXT
*fw_map_ext
;
180 struct MR_RAID_MAP_DESC_TABLE
*desc_table
;
183 struct MR_DRV_RAID_MAP_ALL
*drv_map
=
184 fusion
->ld_drv_map
[(map_id
& 1)];
185 struct MR_DRV_RAID_MAP
*pDrvRaidMap
= &drv_map
->raidMap
;
186 void *raid_map_data
= NULL
;
188 memset(drv_map
, 0, fusion
->drv_map_sz
);
189 memset(pDrvRaidMap
->ldTgtIdToLd
,
190 0xff, (sizeof(u16
) * MAX_LOGICAL_DRIVES_DYN
));
192 if (instance
->max_raid_mapsize
) {
193 fw_map_dyn
= fusion
->ld_map
[(map_id
& 1)];
195 (struct MR_RAID_MAP_DESC_TABLE
*)((void *)fw_map_dyn
+ le32_to_cpu(fw_map_dyn
->desc_table_offset
));
196 if (desc_table
!= fw_map_dyn
->raid_map_desc_table
)
197 dev_dbg(&instance
->pdev
->dev
, "offsets of desc table are not matching desc %p original %p\n",
198 desc_table
, fw_map_dyn
->raid_map_desc_table
);
200 ld_count
= (u16
)le16_to_cpu(fw_map_dyn
->ld_count
);
201 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
202 pDrvRaidMap
->fpPdIoTimeoutSec
=
203 fw_map_dyn
->fp_pd_io_timeout_sec
;
204 pDrvRaidMap
->totalSize
=
205 cpu_to_le32(sizeof(struct MR_DRV_RAID_MAP_ALL
));
206 /* point to actual data starting point*/
207 raid_map_data
= (void *)fw_map_dyn
+
208 le32_to_cpu(fw_map_dyn
->desc_table_offset
) +
209 le32_to_cpu(fw_map_dyn
->desc_table_size
);
211 for (i
= 0; i
< le32_to_cpu(fw_map_dyn
->desc_table_num_elements
); ++i
) {
212 switch (le32_to_cpu(desc_table
->raid_map_desc_type
)) {
213 case RAID_MAP_DESC_TYPE_DEVHDL_INFO
:
214 fw_map_dyn
->dev_hndl_info
=
215 (struct MR_DEV_HANDLE_INFO
*)(raid_map_data
+ le32_to_cpu(desc_table
->raid_map_desc_offset
));
216 memcpy(pDrvRaidMap
->devHndlInfo
,
217 fw_map_dyn
->dev_hndl_info
,
218 sizeof(struct MR_DEV_HANDLE_INFO
) *
219 le32_to_cpu(desc_table
->raid_map_desc_elements
));
221 case RAID_MAP_DESC_TYPE_TGTID_INFO
:
222 fw_map_dyn
->ld_tgt_id_to_ld
=
223 (u16
*)(raid_map_data
+
224 le32_to_cpu(desc_table
->raid_map_desc_offset
));
225 for (j
= 0; j
< le32_to_cpu(desc_table
->raid_map_desc_elements
); j
++) {
226 pDrvRaidMap
->ldTgtIdToLd
[j
] =
227 le16_to_cpu(fw_map_dyn
->ld_tgt_id_to_ld
[j
]);
230 case RAID_MAP_DESC_TYPE_ARRAY_INFO
:
231 fw_map_dyn
->ar_map_info
=
232 (struct MR_ARRAY_INFO
*)
233 (raid_map_data
+ le32_to_cpu(desc_table
->raid_map_desc_offset
));
234 memcpy(pDrvRaidMap
->arMapInfo
,
235 fw_map_dyn
->ar_map_info
,
236 sizeof(struct MR_ARRAY_INFO
) *
237 le32_to_cpu(desc_table
->raid_map_desc_elements
));
239 case RAID_MAP_DESC_TYPE_SPAN_INFO
:
240 fw_map_dyn
->ld_span_map
=
241 (struct MR_LD_SPAN_MAP
*)
243 le32_to_cpu(desc_table
->raid_map_desc_offset
));
244 memcpy(pDrvRaidMap
->ldSpanMap
,
245 fw_map_dyn
->ld_span_map
,
246 sizeof(struct MR_LD_SPAN_MAP
) *
247 le32_to_cpu(desc_table
->raid_map_desc_elements
));
250 dev_dbg(&instance
->pdev
->dev
, "wrong number of desctableElements %d\n",
251 fw_map_dyn
->desc_table_num_elements
);
256 } else if (instance
->supportmax256vd
) {
258 (struct MR_FW_RAID_MAP_EXT
*)fusion
->ld_map
[(map_id
& 1)];
259 ld_count
= (u16
)le16_to_cpu(fw_map_ext
->ldCount
);
260 if (ld_count
> MAX_LOGICAL_DRIVES_EXT
) {
261 dev_dbg(&instance
->pdev
->dev
, "megaraid_sas: LD count exposed in RAID map in not valid\n");
265 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
266 pDrvRaidMap
->fpPdIoTimeoutSec
= fw_map_ext
->fpPdIoTimeoutSec
;
267 for (i
= 0; i
< (MAX_LOGICAL_DRIVES_EXT
); i
++)
268 pDrvRaidMap
->ldTgtIdToLd
[i
] =
269 (u16
)fw_map_ext
->ldTgtIdToLd
[i
];
270 memcpy(pDrvRaidMap
->ldSpanMap
, fw_map_ext
->ldSpanMap
,
271 sizeof(struct MR_LD_SPAN_MAP
) * ld_count
);
272 memcpy(pDrvRaidMap
->arMapInfo
, fw_map_ext
->arMapInfo
,
273 sizeof(struct MR_ARRAY_INFO
) * MAX_API_ARRAYS_EXT
);
274 memcpy(pDrvRaidMap
->devHndlInfo
, fw_map_ext
->devHndlInfo
,
275 sizeof(struct MR_DEV_HANDLE_INFO
) *
276 MAX_RAIDMAP_PHYSICAL_DEVICES
);
278 /* New Raid map will not set totalSize, so keep expected value
279 * for legacy code in ValidateMapInfo
281 pDrvRaidMap
->totalSize
=
282 cpu_to_le32(sizeof(struct MR_FW_RAID_MAP_EXT
));
284 fw_map_old
= (struct MR_FW_RAID_MAP_ALL
*)
285 fusion
->ld_map
[(map_id
& 1)];
286 pFwRaidMap
= &fw_map_old
->raidMap
;
287 ld_count
= (u16
)le32_to_cpu(pFwRaidMap
->ldCount
);
288 if (ld_count
> MAX_LOGICAL_DRIVES
) {
289 dev_dbg(&instance
->pdev
->dev
,
290 "LD count exposed in RAID map in not valid\n");
294 pDrvRaidMap
->totalSize
= pFwRaidMap
->totalSize
;
295 pDrvRaidMap
->ldCount
= (__le16
)cpu_to_le16(ld_count
);
296 pDrvRaidMap
->fpPdIoTimeoutSec
= pFwRaidMap
->fpPdIoTimeoutSec
;
297 for (i
= 0; i
< MAX_RAIDMAP_LOGICAL_DRIVES
+ MAX_RAIDMAP_VIEWS
; i
++)
298 pDrvRaidMap
->ldTgtIdToLd
[i
] =
299 (u8
)pFwRaidMap
->ldTgtIdToLd
[i
];
300 for (i
= 0; i
< ld_count
; i
++) {
301 pDrvRaidMap
->ldSpanMap
[i
] = pFwRaidMap
->ldSpanMap
[i
];
303 memcpy(pDrvRaidMap
->arMapInfo
, pFwRaidMap
->arMapInfo
,
304 sizeof(struct MR_ARRAY_INFO
) * MAX_RAIDMAP_ARRAYS
);
305 memcpy(pDrvRaidMap
->devHndlInfo
, pFwRaidMap
->devHndlInfo
,
306 sizeof(struct MR_DEV_HANDLE_INFO
) *
307 MAX_RAIDMAP_PHYSICAL_DEVICES
);
314 * This function will validate Map info data provided by FW
316 u8
MR_ValidateMapInfo(struct megasas_instance
*instance
, u64 map_id
)
318 struct fusion_context
*fusion
;
319 struct MR_DRV_RAID_MAP_ALL
*drv_map
;
320 struct MR_DRV_RAID_MAP
*pDrvRaidMap
;
321 struct LD_LOAD_BALANCE_INFO
*lbInfo
;
322 PLD_SPAN_INFO ldSpanInfo
;
323 struct MR_LD_RAID
*raid
;
328 if (MR_PopulateDrvRaidMap(instance
, map_id
))
331 fusion
= instance
->ctrl_context
;
332 drv_map
= fusion
->ld_drv_map
[(map_id
& 1)];
333 pDrvRaidMap
= &drv_map
->raidMap
;
335 lbInfo
= fusion
->load_balance_info
;
336 ldSpanInfo
= fusion
->log_to_span
;
338 if (instance
->max_raid_mapsize
)
339 expected_size
= sizeof(struct MR_DRV_RAID_MAP_ALL
);
340 else if (instance
->supportmax256vd
)
341 expected_size
= sizeof(struct MR_FW_RAID_MAP_EXT
);
344 (sizeof(struct MR_FW_RAID_MAP
) - sizeof(struct MR_LD_SPAN_MAP
) +
345 (sizeof(struct MR_LD_SPAN_MAP
) * le16_to_cpu(pDrvRaidMap
->ldCount
)));
347 if (le32_to_cpu(pDrvRaidMap
->totalSize
) != expected_size
) {
348 dev_dbg(&instance
->pdev
->dev
, "megasas: map info structure size 0x%x",
349 le32_to_cpu(pDrvRaidMap
->totalSize
));
350 dev_dbg(&instance
->pdev
->dev
, "is not matching expected size 0x%x\n",
351 (unsigned int)expected_size
);
352 dev_err(&instance
->pdev
->dev
, "megasas: span map %x, pDrvRaidMap->totalSize : %x\n",
353 (unsigned int)sizeof(struct MR_LD_SPAN_MAP
),
354 le32_to_cpu(pDrvRaidMap
->totalSize
));
358 if (instance
->UnevenSpanSupport
)
359 mr_update_span_set(drv_map
, ldSpanInfo
);
362 mr_update_load_balance_params(drv_map
, lbInfo
);
364 num_lds
= le16_to_cpu(drv_map
->raidMap
.ldCount
);
366 /*Convert Raid capability values to CPU arch */
367 for (i
= 0; (num_lds
> 0) && (i
< MAX_LOGICAL_DRIVES_EXT
); i
++) {
368 ld
= MR_TargetIdToLdGet(i
, drv_map
);
370 /* For non existing VDs, iterate to next VD*/
371 if (ld
>= (MAX_LOGICAL_DRIVES_EXT
- 1))
374 raid
= MR_LdRaidGet(ld
, drv_map
);
375 le32_to_cpus((u32
*)&raid
->capability
);
383 u32
MR_GetSpanBlock(u32 ld
, u64 row
, u64
*span_blk
,
384 struct MR_DRV_RAID_MAP_ALL
*map
)
386 struct MR_SPAN_BLOCK_INFO
*pSpanBlock
= MR_LdSpanInfoGet(ld
, map
);
387 struct MR_QUAD_ELEMENT
*quad
;
388 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
391 for (span
= 0; span
< raid
->spanDepth
; span
++, pSpanBlock
++) {
393 for (j
= 0; j
< le32_to_cpu(pSpanBlock
->block_span_info
.noElements
); j
++) {
394 quad
= &pSpanBlock
->block_span_info
.quad
[j
];
396 if (le32_to_cpu(quad
->diff
) == 0)
398 if (le64_to_cpu(quad
->logStart
) <= row
&& row
<=
399 le64_to_cpu(quad
->logEnd
) && (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
400 le32_to_cpu(quad
->diff
))) == 0) {
401 if (span_blk
!= NULL
) {
403 blk
= mega_div64_32((row
-le64_to_cpu(quad
->logStart
)), le32_to_cpu(quad
->diff
));
406 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
)) << raid
->stripeShift
;
417 ******************************************************************************
419 * This routine calculates the Span block for given row using spanset.
422 * instance - HBA instance
423 * ld - Logical drive number
430 * block - Absolute Block number in the physical disk
431 * div_error - Devide error code.
434 u32
mr_spanset_get_span_block(struct megasas_instance
*instance
,
435 u32 ld
, u64 row
, u64
*span_blk
, struct MR_DRV_RAID_MAP_ALL
*map
)
437 struct fusion_context
*fusion
= instance
->ctrl_context
;
438 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
439 LD_SPAN_SET
*span_set
;
440 struct MR_QUAD_ELEMENT
*quad
;
442 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
444 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
445 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
447 if (span_set
->span_row_data_width
== 0)
450 if (row
> span_set
->data_row_end
)
453 for (span
= 0; span
< raid
->spanDepth
; span
++)
454 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
455 block_span_info
.noElements
) >= info
+1) {
456 quad
= &map
->raidMap
.ldSpanMap
[ld
].
458 block_span_info
.quad
[info
];
459 if (le32_to_cpu(quad
->diff
) == 0)
461 if (le64_to_cpu(quad
->logStart
) <= row
&&
462 row
<= le64_to_cpu(quad
->logEnd
) &&
463 (mega_mod64(row
- le64_to_cpu(quad
->logStart
),
464 le32_to_cpu(quad
->diff
))) == 0) {
465 if (span_blk
!= NULL
) {
468 ((row
- le64_to_cpu(quad
->logStart
)),
469 le32_to_cpu(quad
->diff
));
470 blk
= (blk
+ le64_to_cpu(quad
->offsetInSpan
))
471 << raid
->stripeShift
;
482 ******************************************************************************
484 * This routine calculates the row for given strip using spanset.
487 * instance - HBA instance
488 * ld - Logical drive number
494 * row - row associated with strip
497 static u64
get_row_from_strip(struct megasas_instance
*instance
,
498 u32 ld
, u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
)
500 struct fusion_context
*fusion
= instance
->ctrl_context
;
501 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
502 LD_SPAN_SET
*span_set
;
503 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
504 u32 info
, strip_offset
, span
, span_offset
;
505 u64 span_set_Strip
, span_set_Row
, retval
;
507 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
508 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
510 if (span_set
->span_row_data_width
== 0)
512 if (strip
> span_set
->data_strip_end
)
515 span_set_Strip
= strip
- span_set
->data_strip_start
;
516 strip_offset
= mega_mod64(span_set_Strip
,
517 span_set
->span_row_data_width
);
518 span_set_Row
= mega_div64_32(span_set_Strip
,
519 span_set
->span_row_data_width
) * span_set
->diff
;
520 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
521 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
522 block_span_info
.noElements
) >= info
+1) {
524 span_set
->strip_offset
[span
])
530 retval
= (span_set
->data_row_start
+ span_set_Row
+
539 ******************************************************************************
541 * This routine calculates the Start Strip for given row using spanset.
544 * instance - HBA instance
545 * ld - Logical drive number
551 * Strip - Start strip associated with row
554 static u64
get_strip_from_row(struct megasas_instance
*instance
,
555 u32 ld
, u64 row
, struct MR_DRV_RAID_MAP_ALL
*map
)
557 struct fusion_context
*fusion
= instance
->ctrl_context
;
558 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
559 LD_SPAN_SET
*span_set
;
560 struct MR_QUAD_ELEMENT
*quad
;
561 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
565 for (info
= 0; info
< MAX_QUAD_DEPTH
; info
++) {
566 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
568 if (span_set
->span_row_data_width
== 0)
570 if (row
> span_set
->data_row_end
)
573 for (span
= 0; span
< raid
->spanDepth
; span
++)
574 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
575 block_span_info
.noElements
) >= info
+1) {
576 quad
= &map
->raidMap
.ldSpanMap
[ld
].
577 spanBlock
[span
].block_span_info
.quad
[info
];
578 if (le64_to_cpu(quad
->logStart
) <= row
&&
579 row
<= le64_to_cpu(quad
->logEnd
) &&
580 mega_mod64((row
- le64_to_cpu(quad
->logStart
)),
581 le32_to_cpu(quad
->diff
)) == 0) {
582 strip
= mega_div64_32
583 (((row
- span_set
->data_row_start
)
584 - le64_to_cpu(quad
->logStart
)),
585 le32_to_cpu(quad
->diff
));
586 strip
*= span_set
->span_row_data_width
;
587 strip
+= span_set
->data_strip_start
;
588 strip
+= span_set
->strip_offset
[span
];
593 dev_err(&instance
->pdev
->dev
, "get_strip_from_row"
594 "returns invalid strip for ld=%x, row=%lx\n",
595 ld
, (long unsigned int)row
);
600 ******************************************************************************
602 * This routine calculates the Physical Arm for given strip using spanset.
605 * instance - HBA instance
606 * ld - Logical drive number
612 * Phys Arm - Phys Arm associated with strip
615 static u32
get_arm_from_strip(struct megasas_instance
*instance
,
616 u32 ld
, u64 strip
, struct MR_DRV_RAID_MAP_ALL
*map
)
618 struct fusion_context
*fusion
= instance
->ctrl_context
;
619 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
620 LD_SPAN_SET
*span_set
;
621 PLD_SPAN_INFO ldSpanInfo
= fusion
->log_to_span
;
622 u32 info
, strip_offset
, span
, span_offset
, retval
;
624 for (info
= 0 ; info
< MAX_QUAD_DEPTH
; info
++) {
625 span_set
= &(ldSpanInfo
[ld
].span_set
[info
]);
627 if (span_set
->span_row_data_width
== 0)
629 if (strip
> span_set
->data_strip_end
)
632 strip_offset
= (uint
)mega_mod64
633 ((strip
- span_set
->data_strip_start
),
634 span_set
->span_row_data_width
);
636 for (span
= 0, span_offset
= 0; span
< raid
->spanDepth
; span
++)
637 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
638 block_span_info
.noElements
) >= info
+1) {
640 span_set
->strip_offset
[span
])
642 span_set
->strip_offset
[span
];
647 retval
= (strip_offset
- span_offset
);
651 dev_err(&instance
->pdev
->dev
, "get_arm_from_strip"
652 "returns invalid arm for ld=%x strip=%lx\n",
653 ld
, (long unsigned int)strip
);
658 /* This Function will return Phys arm */
659 u8
get_arm(struct megasas_instance
*instance
, u32 ld
, u8 span
, u64 stripe
,
660 struct MR_DRV_RAID_MAP_ALL
*map
)
662 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
663 /* Need to check correct default value */
666 switch (raid
->level
) {
670 arm
= mega_mod64(stripe
, SPAN_ROW_SIZE(map
, ld
, span
));
673 /* start with logical arm */
674 arm
= get_arm_from_strip(instance
, ld
, stripe
, map
);
685 ******************************************************************************
687 * This routine calculates the arm, span and block for the specified stripe and
688 * reference in stripe using spanset
692 * ld - Logical drive number
693 * stripRow - Stripe number
694 * stripRef - Reference in stripe
699 * block - Absolute Block number in the physical disk
701 static u8
mr_spanset_get_phy_params(struct megasas_instance
*instance
, u32 ld
,
702 u64 stripRow
, u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
703 struct RAID_CONTEXT
*pRAID_Context
,
704 struct MR_DRV_RAID_MAP_ALL
*map
)
706 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
707 u32 pd
, arRef
, r1_alt_pd
;
711 u64
*pdBlock
= &io_info
->pdBlock
;
712 __le16
*pDevHandle
= &io_info
->devHandle
;
713 u8
*pPdInterface
= &io_info
->pd_interface
;
714 u32 logArm
, rowMod
, armQ
, arm
;
715 struct fusion_context
*fusion
;
717 fusion
= instance
->ctrl_context
;
718 *pDevHandle
= cpu_to_le16(MR_DEVHANDLE_INVALID
);
720 /*Get row and span from io_info for Uneven Span IO.*/
721 row
= io_info
->start_row
;
722 span
= io_info
->start_span
;
725 if (raid
->level
== 6) {
726 logArm
= get_arm_from_strip(instance
, ld
, stripRow
, map
);
729 rowMod
= mega_mod64(row
, SPAN_ROW_SIZE(map
, ld
, span
));
730 armQ
= SPAN_ROW_SIZE(map
, ld
, span
) - 1 - rowMod
;
731 arm
= armQ
+ 1 + logArm
;
732 if (arm
>= SPAN_ROW_SIZE(map
, ld
, span
))
733 arm
-= SPAN_ROW_SIZE(map
, ld
, span
);
736 /* Calculate the arm */
737 physArm
= get_arm(instance
, ld
, span
, stripRow
, map
);
741 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
742 pd
= MR_ArPdGet(arRef
, physArm
, map
);
744 if (pd
!= MR_PD_INVALID
) {
745 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
746 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
747 /* get second pd also for raid 1/10 fast path writes*/
748 if ((instance
->adapter_type
== VENTURA_SERIES
) &&
749 (raid
->level
== 1) &&
751 r1_alt_pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
752 if (r1_alt_pd
!= MR_PD_INVALID
)
753 io_info
->r1_alt_dev_handle
=
754 MR_PdDevHandleGet(r1_alt_pd
, map
);
757 if ((raid
->level
>= 5) &&
758 ((instance
->adapter_type
== THUNDERBOLT_SERIES
) ||
759 ((instance
->adapter_type
== INVADER_SERIES
) &&
760 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
761 pRAID_Context
->reg_lock_flags
= REGION_TYPE_EXCLUSIVE
;
762 else if (raid
->level
== 1) {
763 physArm
= physArm
+ 1;
764 pd
= MR_ArPdGet(arRef
, physArm
, map
);
765 if (pd
!= MR_PD_INVALID
) {
766 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
767 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
772 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
773 if (instance
->adapter_type
== VENTURA_SERIES
) {
774 ((struct RAID_CONTEXT_G35
*)pRAID_Context
)->span_arm
=
775 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
777 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
779 pRAID_Context
->span_arm
=
780 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
781 io_info
->span_arm
= pRAID_Context
->span_arm
;
783 io_info
->pd_after_lb
= pd
;
788 ******************************************************************************
790 * This routine calculates the arm, span and block for the specified stripe and
791 * reference in stripe.
795 * ld - Logical drive number
796 * stripRow - Stripe number
797 * stripRef - Reference in stripe
802 * block - Absolute Block number in the physical disk
804 u8
MR_GetPhyParams(struct megasas_instance
*instance
, u32 ld
, u64 stripRow
,
805 u16 stripRef
, struct IO_REQUEST_INFO
*io_info
,
806 struct RAID_CONTEXT
*pRAID_Context
,
807 struct MR_DRV_RAID_MAP_ALL
*map
)
809 struct MR_LD_RAID
*raid
= MR_LdRaidGet(ld
, map
);
810 u32 pd
, arRef
, r1_alt_pd
;
814 u64
*pdBlock
= &io_info
->pdBlock
;
815 __le16
*pDevHandle
= &io_info
->devHandle
;
816 u8
*pPdInterface
= &io_info
->pd_interface
;
817 struct fusion_context
*fusion
;
819 fusion
= instance
->ctrl_context
;
820 *pDevHandle
= cpu_to_le16(MR_DEVHANDLE_INVALID
);
822 row
= mega_div64_32(stripRow
, raid
->rowDataSize
);
824 if (raid
->level
== 6) {
825 /* logical arm within row */
826 u32 logArm
= mega_mod64(stripRow
, raid
->rowDataSize
);
827 u32 rowMod
, armQ
, arm
;
829 if (raid
->rowSize
== 0)
831 /* get logical row mod */
832 rowMod
= mega_mod64(row
, raid
->rowSize
);
833 armQ
= raid
->rowSize
-1-rowMod
; /* index of Q drive */
834 arm
= armQ
+1+logArm
; /* data always logically follows Q */
835 if (arm
>= raid
->rowSize
) /* handle wrap condition */
836 arm
-= raid
->rowSize
;
839 if (raid
->modFactor
== 0)
841 physArm
= MR_LdDataArmGet(ld
, mega_mod64(stripRow
,
846 if (raid
->spanDepth
== 1) {
848 *pdBlock
= row
<< raid
->stripeShift
;
850 span
= (u8
)MR_GetSpanBlock(ld
, row
, pdBlock
, map
);
851 if (span
== SPAN_INVALID
)
855 /* Get the array on which this span is present */
856 arRef
= MR_LdSpanArrayGet(ld
, span
, map
);
857 pd
= MR_ArPdGet(arRef
, physArm
, map
); /* Get the pd */
859 if (pd
!= MR_PD_INVALID
) {
860 /* Get dev handle from Pd. */
861 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
862 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
863 /* get second pd also for raid 1/10 fast path writes*/
864 if ((instance
->adapter_type
== VENTURA_SERIES
) &&
865 (raid
->level
== 1) &&
867 r1_alt_pd
= MR_ArPdGet(arRef
, physArm
+ 1, map
);
868 if (r1_alt_pd
!= MR_PD_INVALID
)
869 io_info
->r1_alt_dev_handle
=
870 MR_PdDevHandleGet(r1_alt_pd
, map
);
873 if ((raid
->level
>= 5) &&
874 ((instance
->adapter_type
== THUNDERBOLT_SERIES
) ||
875 ((instance
->adapter_type
== INVADER_SERIES
) &&
876 (raid
->regTypeReqOnRead
!= REGION_TYPE_UNUSED
))))
877 pRAID_Context
->reg_lock_flags
= REGION_TYPE_EXCLUSIVE
;
878 else if (raid
->level
== 1) {
879 /* Get alternate Pd. */
880 physArm
= physArm
+ 1;
881 pd
= MR_ArPdGet(arRef
, physArm
, map
);
882 if (pd
!= MR_PD_INVALID
) {
883 /* Get dev handle from Pd */
884 *pDevHandle
= MR_PdDevHandleGet(pd
, map
);
885 *pPdInterface
= MR_PdInterfaceTypeGet(pd
, map
);
890 *pdBlock
+= stripRef
+ le64_to_cpu(MR_LdSpanPtrGet(ld
, span
, map
)->startBlk
);
891 if (instance
->adapter_type
== VENTURA_SERIES
) {
892 ((struct RAID_CONTEXT_G35
*)pRAID_Context
)->span_arm
=
893 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
895 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
897 pRAID_Context
->span_arm
=
898 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | physArm
;
899 io_info
->span_arm
= pRAID_Context
->span_arm
;
901 io_info
->pd_after_lb
= pd
;
906 ******************************************************************************
908 * MR_BuildRaidContext function
910 * This function will initiate command processing. The start/end row and strip
911 * information is calculated then the lock is acquired.
912 * This function will return 0 if region lock was acquired OR return num strips
915 MR_BuildRaidContext(struct megasas_instance
*instance
,
916 struct IO_REQUEST_INFO
*io_info
,
917 struct RAID_CONTEXT
*pRAID_Context
,
918 struct MR_DRV_RAID_MAP_ALL
*map
, u8
**raidLUN
)
920 struct fusion_context
*fusion
;
921 struct MR_LD_RAID
*raid
;
922 u32 stripSize
, stripe_mask
;
923 u64 endLba
, endStrip
, endRow
, start_row
, start_strip
;
926 u8 num_strips
, numRows
;
927 u16 ref_in_start_stripe
, ref_in_end_stripe
;
929 u32 numBlocks
, ldTgtId
;
932 u8 startlba_span
= SPAN_INVALID
;
933 u64
*pdBlock
= &io_info
->pdBlock
;
936 ldStartBlock
= io_info
->ldStartBlock
;
937 numBlocks
= io_info
->numBlocks
;
938 ldTgtId
= io_info
->ldTgtId
;
939 isRead
= io_info
->isRead
;
940 io_info
->IoforUnevenSpan
= 0;
941 io_info
->start_span
= SPAN_INVALID
;
942 fusion
= instance
->ctrl_context
;
944 ld
= MR_TargetIdToLdGet(ldTgtId
, map
);
945 raid
= MR_LdRaidGet(ld
, map
);
946 /*check read ahead bit*/
947 io_info
->ra_capable
= raid
->capability
.ra_capable
;
950 * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
953 if (raid
->rowDataSize
== 0) {
954 if (MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
== 0)
956 else if (instance
->UnevenSpanSupport
) {
957 io_info
->IoforUnevenSpan
= 1;
959 dev_info(&instance
->pdev
->dev
,
960 "raid->rowDataSize is 0, but has SPAN[0]"
961 "rowDataSize = 0x%0x,"
962 "but there is _NO_ UnevenSpanSupport\n",
963 MR_LdSpanPtrGet(ld
, 0, map
)->spanRowDataSize
);
968 stripSize
= 1 << raid
->stripeShift
;
969 stripe_mask
= stripSize
-1;
973 * calculate starting row and stripe, and number of strips and rows
975 start_strip
= ldStartBlock
>> raid
->stripeShift
;
976 ref_in_start_stripe
= (u16
)(ldStartBlock
& stripe_mask
);
977 endLba
= ldStartBlock
+ numBlocks
- 1;
978 ref_in_end_stripe
= (u16
)(endLba
& stripe_mask
);
979 endStrip
= endLba
>> raid
->stripeShift
;
980 num_strips
= (u8
)(endStrip
- start_strip
+ 1); /* End strip */
982 if (io_info
->IoforUnevenSpan
) {
983 start_row
= get_row_from_strip(instance
, ld
, start_strip
, map
);
984 endRow
= get_row_from_strip(instance
, ld
, endStrip
, map
);
985 if (start_row
== -1ULL || endRow
== -1ULL) {
986 dev_info(&instance
->pdev
->dev
, "return from %s %d."
987 "Send IO w/o region lock.\n",
992 if (raid
->spanDepth
== 1) {
994 *pdBlock
= start_row
<< raid
->stripeShift
;
996 startlba_span
= (u8
)mr_spanset_get_span_block(instance
,
997 ld
, start_row
, pdBlock
, map
);
998 if (startlba_span
== SPAN_INVALID
) {
999 dev_info(&instance
->pdev
->dev
, "return from %s %d"
1000 "for row 0x%llx,start strip %llx"
1001 "endSrip %llx\n", __func__
, __LINE__
,
1002 (unsigned long long)start_row
,
1003 (unsigned long long)start_strip
,
1004 (unsigned long long)endStrip
);
1007 io_info
->start_span
= startlba_span
;
1008 io_info
->start_row
= start_row
;
1010 start_row
= mega_div64_32(start_strip
, raid
->rowDataSize
);
1011 endRow
= mega_div64_32(endStrip
, raid
->rowDataSize
);
1013 numRows
= (u8
)(endRow
- start_row
+ 1);
1016 * calculate region info.
1019 /* assume region is at the start of the first row */
1020 regStart
= start_row
<< raid
->stripeShift
;
1021 /* assume this IO needs the full row - we'll adjust if not true */
1022 regSize
= stripSize
;
1024 io_info
->do_fp_rlbypass
= raid
->capability
.fpBypassRegionLock
;
1026 /* Check if we can send this I/O via FastPath */
1027 if (raid
->capability
.fpCapable
) {
1029 io_info
->fpOkForIo
= (raid
->capability
.fpReadCapable
&&
1030 ((num_strips
== 1) ||
1032 fpReadAcrossStripe
));
1034 io_info
->fpOkForIo
= (raid
->capability
.fpWriteCapable
&&
1035 ((num_strips
== 1) ||
1037 fpWriteAcrossStripe
));
1039 io_info
->fpOkForIo
= false;
1042 /* single-strip IOs can always lock only the data needed */
1043 if (num_strips
== 1) {
1044 regStart
+= ref_in_start_stripe
;
1045 regSize
= numBlocks
;
1047 /* multi-strip IOs always need to full stripe locked */
1048 } else if (io_info
->IoforUnevenSpan
== 0) {
1050 * For Even span region lock optimization.
1051 * If the start strip is the last in the start row
1053 if (start_strip
== (start_row
+ 1) * raid
->rowDataSize
- 1) {
1054 regStart
+= ref_in_start_stripe
;
1055 /* initialize count to sectors from startref to end
1057 regSize
= stripSize
- ref_in_start_stripe
;
1060 /* add complete rows in the middle of the transfer */
1062 regSize
+= (numRows
-2) << raid
->stripeShift
;
1064 /* if IO ends within first strip of last row*/
1065 if (endStrip
== endRow
*raid
->rowDataSize
)
1066 regSize
+= ref_in_end_stripe
+1;
1068 regSize
+= stripSize
;
1071 * For Uneven span region lock optimization.
1072 * If the start strip is the last in the start row
1074 if (start_strip
== (get_strip_from_row(instance
, ld
, start_row
, map
) +
1075 SPAN_ROW_DATA_SIZE(map
, ld
, startlba_span
) - 1)) {
1076 regStart
+= ref_in_start_stripe
;
1077 /* initialize count to sectors from
1078 * startRef to end of strip
1080 regSize
= stripSize
- ref_in_start_stripe
;
1082 /* Add complete rows in the middle of the transfer*/
1085 /* Add complete rows in the middle of the transfer*/
1086 regSize
+= (numRows
-2) << raid
->stripeShift
;
1088 /* if IO ends within first strip of last row */
1089 if (endStrip
== get_strip_from_row(instance
, ld
, endRow
, map
))
1090 regSize
+= ref_in_end_stripe
+ 1;
1092 regSize
+= stripSize
;
1095 pRAID_Context
->timeout_value
=
1096 cpu_to_le16(raid
->fpIoTimeoutForLd
?
1097 raid
->fpIoTimeoutForLd
:
1098 map
->raidMap
.fpPdIoTimeoutSec
);
1099 if (instance
->adapter_type
== INVADER_SERIES
)
1100 pRAID_Context
->reg_lock_flags
= (isRead
) ?
1101 raid
->regTypeReqOnRead
: raid
->regTypeReqOnWrite
;
1102 else if (instance
->adapter_type
== THUNDERBOLT_SERIES
)
1103 pRAID_Context
->reg_lock_flags
= (isRead
) ?
1104 REGION_TYPE_SHARED_READ
: raid
->regTypeReqOnWrite
;
1105 pRAID_Context
->virtual_disk_tgt_id
= raid
->targetId
;
1106 pRAID_Context
->reg_lock_row_lba
= cpu_to_le64(regStart
);
1107 pRAID_Context
->reg_lock_length
= cpu_to_le32(regSize
);
1108 pRAID_Context
->config_seq_num
= raid
->seqNum
;
1109 /* save pointer to raid->LUN array */
1110 *raidLUN
= raid
->LUN
;
1113 /*Get Phy Params only if FP capable, or else leave it to MR firmware
1114 to do the calculation.*/
1115 if (io_info
->fpOkForIo
) {
1116 retval
= io_info
->IoforUnevenSpan
?
1117 mr_spanset_get_phy_params(instance
, ld
,
1118 start_strip
, ref_in_start_stripe
,
1119 io_info
, pRAID_Context
, map
) :
1120 MR_GetPhyParams(instance
, ld
, start_strip
,
1121 ref_in_start_stripe
, io_info
,
1122 pRAID_Context
, map
);
1123 /* If IO on an invalid Pd, then FP is not possible.*/
1124 if (io_info
->devHandle
== MR_DEVHANDLE_INVALID
)
1125 io_info
->fpOkForIo
= false;
1127 } else if (isRead
) {
1129 for (stripIdx
= 0; stripIdx
< num_strips
; stripIdx
++) {
1130 retval
= io_info
->IoforUnevenSpan
?
1131 mr_spanset_get_phy_params(instance
, ld
,
1132 start_strip
+ stripIdx
,
1133 ref_in_start_stripe
, io_info
,
1134 pRAID_Context
, map
) :
1135 MR_GetPhyParams(instance
, ld
,
1136 start_strip
+ stripIdx
, ref_in_start_stripe
,
1137 io_info
, pRAID_Context
, map
);
1146 ******************************************************************************
1148 * This routine pepare spanset info from Valid Raid map and store it into
1149 * local copy of ldSpanInfo per instance data structure.
1153 * ldSpanInfo - ldSpanInfo per HBA instance
1156 void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL
*map
,
1157 PLD_SPAN_INFO ldSpanInfo
)
1160 u32 element
, span_row_width
;
1162 struct MR_LD_RAID
*raid
;
1163 LD_SPAN_SET
*span_set
, *span_set_prev
;
1164 struct MR_QUAD_ELEMENT
*quad
;
1169 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES_EXT
; ldCount
++) {
1170 ld
= MR_TargetIdToLdGet(ldCount
, map
);
1171 if (ld
>= (MAX_LOGICAL_DRIVES_EXT
- 1))
1173 raid
= MR_LdRaidGet(ld
, map
);
1174 for (element
= 0; element
< MAX_QUAD_DEPTH
; element
++) {
1175 for (span
= 0; span
< raid
->spanDepth
; span
++) {
1176 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].spanBlock
[span
].
1177 block_span_info
.noElements
) <
1180 span_set
= &(ldSpanInfo
[ld
].span_set
[element
]);
1181 quad
= &map
->raidMap
.ldSpanMap
[ld
].
1182 spanBlock
[span
].block_span_info
.
1185 span_set
->diff
= le32_to_cpu(quad
->diff
);
1187 for (count
= 0, span_row_width
= 0;
1188 count
< raid
->spanDepth
; count
++) {
1189 if (le32_to_cpu(map
->raidMap
.ldSpanMap
[ld
].
1192 noElements
) >= element
+ 1) {
1193 span_set
->strip_offset
[count
] =
1197 (ld
, count
, map
)->spanRowDataSize
;
1201 span_set
->span_row_data_width
= span_row_width
;
1202 span_row
= mega_div64_32(((le64_to_cpu(quad
->logEnd
) -
1203 le64_to_cpu(quad
->logStart
)) + le32_to_cpu(quad
->diff
)),
1204 le32_to_cpu(quad
->diff
));
1207 span_set
->log_start_lba
= 0;
1208 span_set
->log_end_lba
=
1209 ((span_row
<< raid
->stripeShift
)
1210 * span_row_width
) - 1;
1212 span_set
->span_row_start
= 0;
1213 span_set
->span_row_end
= span_row
- 1;
1215 span_set
->data_strip_start
= 0;
1216 span_set
->data_strip_end
=
1217 (span_row
* span_row_width
) - 1;
1219 span_set
->data_row_start
= 0;
1220 span_set
->data_row_end
=
1221 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1223 span_set_prev
= &(ldSpanInfo
[ld
].
1224 span_set
[element
- 1]);
1225 span_set
->log_start_lba
=
1226 span_set_prev
->log_end_lba
+ 1;
1227 span_set
->log_end_lba
=
1228 span_set
->log_start_lba
+
1229 ((span_row
<< raid
->stripeShift
)
1230 * span_row_width
) - 1;
1232 span_set
->span_row_start
=
1233 span_set_prev
->span_row_end
+ 1;
1234 span_set
->span_row_end
=
1235 span_set
->span_row_start
+ span_row
- 1;
1237 span_set
->data_strip_start
=
1238 span_set_prev
->data_strip_end
+ 1;
1239 span_set
->data_strip_end
=
1240 span_set
->data_strip_start
+
1241 (span_row
* span_row_width
) - 1;
1243 span_set
->data_row_start
=
1244 span_set_prev
->data_row_end
+ 1;
1245 span_set
->data_row_end
=
1246 span_set
->data_row_start
+
1247 (span_row
* le32_to_cpu(quad
->diff
)) - 1;
1251 if (span
== raid
->spanDepth
)
1257 void mr_update_load_balance_params(struct MR_DRV_RAID_MAP_ALL
*drv_map
,
1258 struct LD_LOAD_BALANCE_INFO
*lbInfo
)
1262 struct MR_LD_RAID
*raid
;
1264 if (lb_pending_cmds
> 128 || lb_pending_cmds
< 1)
1265 lb_pending_cmds
= LB_PENDING_CMDS_DEFAULT
;
1267 for (ldCount
= 0; ldCount
< MAX_LOGICAL_DRIVES_EXT
; ldCount
++) {
1268 ld
= MR_TargetIdToLdGet(ldCount
, drv_map
);
1269 if (ld
>= MAX_LOGICAL_DRIVES_EXT
) {
1270 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1274 raid
= MR_LdRaidGet(ld
, drv_map
);
1275 if ((raid
->level
!= 1) ||
1276 (raid
->ldState
!= MR_LD_STATE_OPTIMAL
)) {
1277 lbInfo
[ldCount
].loadBalanceFlag
= 0;
1280 lbInfo
[ldCount
].loadBalanceFlag
= 1;
1284 u8
megasas_get_best_arm_pd(struct megasas_instance
*instance
,
1285 struct LD_LOAD_BALANCE_INFO
*lbInfo
,
1286 struct IO_REQUEST_INFO
*io_info
,
1287 struct MR_DRV_RAID_MAP_ALL
*drv_map
)
1289 struct MR_LD_RAID
*raid
;
1291 u16 pend0
, pend1
, ld
;
1293 u8 bestArm
, pd0
, pd1
, span
, arm
;
1294 u32 arRef
, span_row_size
;
1296 u64 block
= io_info
->ldStartBlock
;
1297 u32 count
= io_info
->numBlocks
;
1299 span
= ((io_info
->span_arm
& RAID_CTX_SPANARM_SPAN_MASK
)
1300 >> RAID_CTX_SPANARM_SPAN_SHIFT
);
1301 arm
= (io_info
->span_arm
& RAID_CTX_SPANARM_ARM_MASK
);
1303 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, drv_map
);
1304 raid
= MR_LdRaidGet(ld
, drv_map
);
1305 span_row_size
= instance
->UnevenSpanSupport
?
1306 SPAN_ROW_SIZE(drv_map
, ld
, span
) : raid
->rowSize
;
1308 arRef
= MR_LdSpanArrayGet(ld
, span
, drv_map
);
1309 pd0
= MR_ArPdGet(arRef
, arm
, drv_map
);
1310 pd1
= MR_ArPdGet(arRef
, (arm
+ 1) >= span_row_size
?
1311 (arm
+ 1 - span_row_size
) : arm
+ 1, drv_map
);
1313 /* Get PD1 Dev Handle */
1315 pd1_dev_handle
= MR_PdDevHandleGet(pd1
, drv_map
);
1317 if (pd1_dev_handle
== MR_DEVHANDLE_INVALID
) {
1320 /* get the pending cmds for the data and mirror arms */
1321 pend0
= atomic_read(&lbInfo
->scsi_pending_cmds
[pd0
]);
1322 pend1
= atomic_read(&lbInfo
->scsi_pending_cmds
[pd1
]);
1324 /* Determine the disk whose head is nearer to the req. block */
1325 diff0
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[pd0
]);
1326 diff1
= ABS_DIFF(block
, lbInfo
->last_accessed_block
[pd1
]);
1327 bestArm
= (diff0
<= diff1
? arm
: arm
^ 1);
1329 /* Make balance count from 16 to 4 to
1330 * keep driver in sync with Firmware
1332 if ((bestArm
== arm
&& pend0
> pend1
+ lb_pending_cmds
) ||
1333 (bestArm
!= arm
&& pend1
> pend0
+ lb_pending_cmds
))
1336 /* Update the last accessed block on the correct pd */
1338 (span
<< RAID_CTX_SPANARM_SPAN_SHIFT
) | bestArm
;
1339 io_info
->pd_after_lb
= (bestArm
== arm
) ? pd0
: pd1
;
1342 lbInfo
->last_accessed_block
[io_info
->pd_after_lb
] = block
+ count
- 1;
1343 return io_info
->pd_after_lb
;
1346 __le16
get_updated_dev_handle(struct megasas_instance
*instance
,
1347 struct LD_LOAD_BALANCE_INFO
*lbInfo
,
1348 struct IO_REQUEST_INFO
*io_info
,
1349 struct MR_DRV_RAID_MAP_ALL
*drv_map
)
1354 /* get best new arm (PD ID) */
1355 arm_pd
= megasas_get_best_arm_pd(instance
, lbInfo
, io_info
, drv_map
);
1356 devHandle
= MR_PdDevHandleGet(arm_pd
, drv_map
);
1357 io_info
->pd_interface
= MR_PdInterfaceTypeGet(arm_pd
, drv_map
);
1358 atomic_inc(&lbInfo
->scsi_pending_cmds
[arm_pd
]);