2 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
3 * Support: freebsdraid@lsi.com
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer. 2. Redistributions
11 * in binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other
13 * materials provided with the distribution. 3. Neither the name of the
14 * <ORGANIZATION> nor the names of its contributors may be used to endorse or
15 * promote products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation are
31 * those of the authors and should not be interpreted as representing
32 * official policies,either expressed or implied, of the FreeBSD Project.
34 * Send feedback to: <megaraidfbsd@lsi.com> Mail to: LSI Corporation, 1621
35 * Barber Lane, Milpitas, CA 95035 ATTN: MegaRaid FreeBSD
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <dev/mrsas/mrsas.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_sim.h>
47 #include <cam/cam_xpt_sim.h>
48 #include <cam/cam_debug.h>
49 #include <cam/cam_periph.h>
50 #include <cam/cam_xpt_periph.h>
56 u_int8_t MR_ValidateMapInfo(struct mrsas_softc *sc);
57 u_int8_t mrsas_get_best_arm_pd(struct mrsas_softc *sc,
58 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
60 MR_BuildRaidContext(struct mrsas_softc *sc,
61 struct IO_REQUEST_INFO *io_info,
62 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map);
64 MR_GetPhyParams(struct mrsas_softc *sc, u_int32_t ld,
65 u_int64_t stripRow, u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
66 RAID_CONTEXT * pRAID_Context,
67 MR_DRV_RAID_MAP_ALL * map);
68 u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
69 u_int32_t MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
70 u_int16_t MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
71 u_int16_t mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
72 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
73 u_int32_t mega_mod64(u_int64_t dividend, u_int32_t divisor);
75 MR_GetSpanBlock(u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
76 MR_DRV_RAID_MAP_ALL * map, int *div_error);
77 u_int64_t mega_div64_32(u_int64_t dividend, u_int32_t divisor);
78 void mrsas_update_load_balance_params(struct mrsas_softc *sc,
79 MR_DRV_RAID_MAP_ALL *map, PLD_LOAD_BALANCE_INFO lbInfo);
81 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request,
82 u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb,
83 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
84 u_int32_t ld_block_size);
86 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
87 MR_DRV_RAID_MAP_ALL * map);
88 static u_int16_t MR_PdDevHandleGet(u_int32_t pd, MR_DRV_RAID_MAP_ALL * map);
90 MR_ArPdGet(u_int32_t ar, u_int32_t arm,
91 MR_DRV_RAID_MAP_ALL * map);
93 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span,
94 MR_DRV_RAID_MAP_ALL * map);
96 MR_LdDataArmGet(u_int32_t ld, u_int32_t armIdx,
97 MR_DRV_RAID_MAP_ALL * map);
98 static MR_SPAN_BLOCK_INFO *
99 MR_LdSpanInfoGet(u_int32_t ld,
100 MR_DRV_RAID_MAP_ALL * map);
101 MR_LD_RAID *MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
102 void MR_PopulateDrvRaidMap(struct mrsas_softc *sc);
106 * Spanset related function prototypes Added for PRL11 configuration (Uneven
109 void mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo);
111 mr_spanset_get_phy_params(struct mrsas_softc *sc, u_int32_t ld,
112 u_int64_t stripRow, u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
113 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map);
115 get_row_from_strip(struct mrsas_softc *sc, u_int32_t ld,
116 u_int64_t strip, MR_DRV_RAID_MAP_ALL * map);
118 mr_spanset_get_span_block(struct mrsas_softc *sc,
119 u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
120 MR_DRV_RAID_MAP_ALL * map, int *div_error);
122 get_arm(struct mrsas_softc *sc, u_int32_t ld, u_int8_t span,
123 u_int64_t stripe, MR_DRV_RAID_MAP_ALL * map);
127 * Spanset related defines Added for PRL11 configuration(Uneven span support)
129 #define SPAN_ROW_SIZE(map, ld, index_) MR_LdSpanPtrGet(ld, index_, map)->spanRowSize
130 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) \
131 MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize
132 #define SPAN_INVALID 0xff
139 typedef u_int64_t REGION_KEY;
140 typedef u_int32_t REGION_LEN;
142 #define MR_LD_STATE_OPTIMAL 3
146 #define LB_PENDING_CMDS_DEFAULT 4
153 #define ABS_DIFF(a,b) ( ((a) > (b)) ? ((a) - (b)) : ((b) - (a)) )
157 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
158 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
159 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
160 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
164 * In-line functions for mod and divide of 64-bit dividend and 32-bit
165 * divisor. Assumes a check for a divisor of zero is not possible.
167 * @param dividend: Dividend
168 * @param divisor: Divisor
172 #define mega_mod64(dividend, divisor) ({ \
174 remainder = ((u_int64_t) (dividend)) % (u_int32_t) (divisor); \
177 #define mega_div64_32(dividend, divisor) ({ \
179 quotient = ((u_int64_t) (dividend)) / (u_int32_t) (divisor); \
184 * Various RAID map access functions. These functions access the various
185 * parts of the RAID map and returns the appropriate parameters.
189 MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
191 return (&map->raidMap.ldSpanMap[ld].ldRaid);
195 MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
197 return (map->raidMap.ldSpanMap[ld].ldRaid.targetId);
201 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
203 return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
207 MR_LdDataArmGet(u_int32_t ld, u_int32_t armIdx, MR_DRV_RAID_MAP_ALL * map)
209 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
213 MR_PdDevHandleGet(u_int32_t pd, MR_DRV_RAID_MAP_ALL * map)
215 return map->raidMap.devHndlInfo[pd].curDevHdl;
219 MR_ArPdGet(u_int32_t ar, u_int32_t arm, MR_DRV_RAID_MAP_ALL * map)
221 return map->raidMap.arMapInfo[ar].pd[arm];
225 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
227 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
230 static MR_SPAN_BLOCK_INFO *
231 MR_LdSpanInfoGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
233 return &map->raidMap.ldSpanMap[ld].spanBlock[0];
237 MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
239 return map->raidMap.ldTgtIdToLd[ldTgtId];
243 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
246 u_int32_t ld, ldBlockSize = MRSAS_SCSIBLOCKSIZE;
248 ld = MR_TargetIdToLdGet(ldTgtId, map);
251 * Check if logical drive was removed.
253 if (ld >= MAX_LOGICAL_DRIVES)
256 raid = MR_LdRaidGet(ld, map);
257 ldBlockSize = raid->logicalBlockLength;
259 ldBlockSize = MRSAS_SCSIBLOCKSIZE;
265 * This function will Populate Driver Map using firmware raid map
268 MR_PopulateDrvRaidMap(struct mrsas_softc *sc)
270 MR_FW_RAID_MAP_ALL *fw_map_old = NULL;
271 MR_FW_RAID_MAP *pFwRaidMap = NULL;
274 MR_DRV_RAID_MAP_ALL *drv_map = sc->ld_drv_map[(sc->map_id & 1)];
275 MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
277 if (sc->max256vdSupport) {
278 memcpy(sc->ld_drv_map[sc->map_id & 1],
279 sc->raidmap_mem[sc->map_id & 1],
282 * New Raid map will not set totalSize, so keep expected
283 * value for legacy code in ValidateMapInfo
285 pDrvRaidMap->totalSize = sizeof(MR_FW_RAID_MAP_EXT);
287 fw_map_old = (MR_FW_RAID_MAP_ALL *) sc->raidmap_mem[(sc->map_id & 1)];
288 pFwRaidMap = &fw_map_old->raidMap;
291 for (i = 0; i < pFwRaidMap->ldCount; i++) {
292 device_printf(sc->mrsas_dev,
293 "Index 0x%x Target Id 0x%x Seq Num 0x%x Size 0/%lx\n", i,
294 fw_map_old->raidMap.ldSpanMap[i].ldRaid.targetId,
295 fw_map_old->raidMap.ldSpanMap[i].ldRaid.seqNum,
296 fw_map_old->raidMap.ldSpanMap[i].ldRaid.size);
300 memset(drv_map, 0, sc->drv_map_sz);
301 pDrvRaidMap->totalSize = pFwRaidMap->totalSize;
302 pDrvRaidMap->ldCount = pFwRaidMap->ldCount;
303 pDrvRaidMap->fpPdIoTimeoutSec =
304 pFwRaidMap->fpPdIoTimeoutSec;
306 for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++) {
307 pDrvRaidMap->ldTgtIdToLd[i] =
308 (u_int8_t)pFwRaidMap->ldTgtIdToLd[i];
311 for (i = 0; i < pDrvRaidMap->ldCount; i++) {
312 pDrvRaidMap->ldSpanMap[i] =
313 pFwRaidMap->ldSpanMap[i];
316 device_printf(sc->mrsas_dev, "pFwRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
317 "pFwRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x size 0x%x\n",
318 i, i, pFwRaidMap->ldSpanMap[i].ldRaid.targetId,
319 pFwRaidMap->ldSpanMap[i].ldRaid.seqNum,
320 (u_int32_t)pFwRaidMap->ldSpanMap[i].ldRaid.rowSize);
321 device_printf(sc->mrsas_dev, "pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x"
322 "pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x size 0x%x\n", i, i,
323 pDrvRaidMap->ldSpanMap[i].ldRaid.targetId,
324 pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum,
325 (u_int32_t)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);
326 device_printf(sc->mrsas_dev, "drv raid map all %p raid map %p LD RAID MAP %p/%p\n",
327 drv_map, pDrvRaidMap,
328 &pFwRaidMap->ldSpanMap[i].ldRaid, &pDrvRaidMap->ldSpanMap[i].ldRaid);
332 memcpy(pDrvRaidMap->arMapInfo, pFwRaidMap->arMapInfo,
333 sizeof(MR_ARRAY_INFO) * MAX_RAIDMAP_ARRAYS);
334 memcpy(pDrvRaidMap->devHndlInfo, pFwRaidMap->devHndlInfo,
335 sizeof(MR_DEV_HANDLE_INFO) *
336 MAX_RAIDMAP_PHYSICAL_DEVICES);
341 * MR_ValidateMapInfo: Validate RAID map
342 * input: Adapter instance soft state
344 * This function checks and validates the loaded RAID map. It returns 0 if
345 * successful, and 1 otherwise.
348 MR_ValidateMapInfo(struct mrsas_softc *sc)
353 MR_PopulateDrvRaidMap(sc);
355 MR_DRV_RAID_MAP_ALL *drv_map = sc->ld_drv_map[(sc->map_id & 1)];
356 MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
358 u_int32_t expected_map_size;
360 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
361 pDrvRaidMap = &drv_map->raidMap;
362 PLD_SPAN_INFO ldSpanInfo = (PLD_SPAN_INFO) & sc->log_to_span;
364 if (sc->max256vdSupport)
365 expected_map_size = sizeof(MR_FW_RAID_MAP_EXT);
368 (sizeof(MR_FW_RAID_MAP) - sizeof(MR_LD_SPAN_MAP)) +
369 (sizeof(MR_LD_SPAN_MAP) * pDrvRaidMap->ldCount);
371 if (pDrvRaidMap->totalSize != expected_map_size) {
372 device_printf(sc->mrsas_dev, "map size %x not matching ld count\n", expected_map_size);
373 device_printf(sc->mrsas_dev, "span map= %x\n", (unsigned int)sizeof(MR_LD_SPAN_MAP));
374 device_printf(sc->mrsas_dev, "pDrvRaidMap->totalSize=%x\n", pDrvRaidMap->totalSize);
377 if (sc->UnevenSpanSupport) {
378 printf("Updating span set\n\n");
379 mr_update_span_set(drv_map, ldSpanInfo);
381 mrsas_update_load_balance_params(sc, drv_map, sc->load_balance_info);
388 * Function to print info about span set created in driver from FW raid map
391 * ldSpanInfo: ld map span info per HBA instance
397 getSpanInfo(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
403 LD_SPAN_SET *span_set;
404 MR_QUAD_ELEMENT *quad;
408 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
409 ld = MR_TargetIdToLdGet(ldCount, map);
410 if (ld >= MAX_LOGICAL_DRIVES) {
413 raid = MR_LdRaidGet(ld, map);
414 printf("LD %x: span_depth=%x\n", ld, raid->spanDepth);
415 for (span = 0; span < raid->spanDepth; span++)
416 printf("Span=%x, number of quads=%x\n", span,
417 map->raidMap.ldSpanMap[ld].spanBlock[span].
418 block_span_info.noElements);
419 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
420 span_set = &(ldSpanInfo[ld].span_set[element]);
421 if (span_set->span_row_data_width == 0)
424 printf("Span Set %x: width=%x, diff=%x\n", element,
425 (unsigned int)span_set->span_row_data_width,
426 (unsigned int)span_set->diff);
427 printf("logical LBA start=0x%08lx, end=0x%08lx\n",
428 (long unsigned int)span_set->log_start_lba,
429 (long unsigned int)span_set->log_end_lba);
430 printf("span row start=0x%08lx, end=0x%08lx\n",
431 (long unsigned int)span_set->span_row_start,
432 (long unsigned int)span_set->span_row_end);
433 printf("data row start=0x%08lx, end=0x%08lx\n",
434 (long unsigned int)span_set->data_row_start,
435 (long unsigned int)span_set->data_row_end);
436 printf("data strip start=0x%08lx, end=0x%08lx\n",
437 (long unsigned int)span_set->data_strip_start,
438 (long unsigned int)span_set->data_strip_end);
440 for (span = 0; span < raid->spanDepth; span++) {
441 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
442 block_span_info.noElements >= element + 1) {
443 quad = &map->raidMap.ldSpanMap[ld].
444 spanBlock[span].block_span_info.
446 printf("Span=%x, Quad=%x, diff=%x\n", span,
447 element, quad->diff);
448 printf("offset_in_span=0x%08lx\n",
449 (long unsigned int)quad->offsetInSpan);
450 printf("logical start=0x%08lx, end=0x%08lx\n",
451 (long unsigned int)quad->logStart,
452 (long unsigned int)quad->logEnd);
463 * This routine calculates the Span block for given row using spanset.
465 * Inputs : HBA instance
466 * ld: Logical drive number
470 * Outputs : span - Span number block
471 * - Absolute Block number in the physical disk
472 * div_error - Devide error code.
476 mr_spanset_get_span_block(struct mrsas_softc *sc, u_int32_t ld, u_int64_t row,
477 u_int64_t *span_blk, MR_DRV_RAID_MAP_ALL * map, int *div_error)
479 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
480 LD_SPAN_SET *span_set;
481 MR_QUAD_ELEMENT *quad;
482 u_int32_t span, info;
483 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
485 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
486 span_set = &(ldSpanInfo[ld].span_set[info]);
488 if (span_set->span_row_data_width == 0)
490 if (row > span_set->data_row_end)
493 for (span = 0; span < raid->spanDepth; span++)
494 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
495 block_span_info.noElements >= info + 1) {
496 quad = &map->raidMap.ldSpanMap[ld].
498 block_span_info.quad[info];
499 if (quad->diff == 0) {
503 if (quad->logStart <= row &&
504 row <= quad->logEnd &&
505 (mega_mod64(row - quad->logStart,
507 if (span_blk != NULL) {
511 ((row - quad->logStart),
513 blk = (blk + quad->offsetInSpan)
514 << raid->stripeShift;
526 * This routine calculates the row for given strip using spanset.
528 * Inputs : HBA instance
529 * ld: Logical drive number
533 * Outputs : row - row associated with strip
537 get_row_from_strip(struct mrsas_softc *sc,
538 u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
540 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
541 LD_SPAN_SET *span_set;
542 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
543 u_int32_t info, strip_offset, span, span_offset;
544 u_int64_t span_set_Strip, span_set_Row;
546 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
547 span_set = &(ldSpanInfo[ld].span_set[info]);
549 if (span_set->span_row_data_width == 0)
551 if (strip > span_set->data_strip_end)
554 span_set_Strip = strip - span_set->data_strip_start;
555 strip_offset = mega_mod64(span_set_Strip,
556 span_set->span_row_data_width);
557 span_set_Row = mega_div64_32(span_set_Strip,
558 span_set->span_row_data_width) * span_set->diff;
559 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
560 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
561 block_span_info.noElements >= info + 1) {
563 span_set->strip_offset[span])
568 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug : Strip 0x%llx, span_set_Strip 0x%llx, span_set_Row 0x%llx "
569 "data width 0x%llx span offset 0x%llx\n", (unsigned long long)strip,
570 (unsigned long long)span_set_Strip,
571 (unsigned long long)span_set_Row,
572 (unsigned long long)span_set->span_row_data_width, (unsigned long long)span_offset);
573 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug : For strip 0x%llx row is 0x%llx\n", (unsigned long long)strip,
574 (unsigned long long)span_set->data_row_start +
575 (unsigned long long)span_set_Row + (span_offset - 1));
576 return (span_set->data_row_start + span_set_Row + (span_offset - 1));
584 * This routine calculates the Start Strip for given row using spanset.
586 * Inputs: HBA instance
587 * ld: Logical drive number
591 * Outputs : Strip - Start strip associated with row
595 get_strip_from_row(struct mrsas_softc *sc,
596 u_int32_t ld, u_int64_t row, MR_DRV_RAID_MAP_ALL * map)
598 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
599 LD_SPAN_SET *span_set;
600 MR_QUAD_ELEMENT *quad;
601 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
602 u_int32_t span, info;
605 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
606 span_set = &(ldSpanInfo[ld].span_set[info]);
608 if (span_set->span_row_data_width == 0)
610 if (row > span_set->data_row_end)
613 for (span = 0; span < raid->spanDepth; span++)
614 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
615 block_span_info.noElements >= info + 1) {
616 quad = &map->raidMap.ldSpanMap[ld].
617 spanBlock[span].block_span_info.quad[info];
618 if (quad->logStart <= row &&
619 row <= quad->logEnd &&
620 mega_mod64((row - quad->logStart),
622 strip = mega_div64_32
623 (((row - span_set->data_row_start)
626 strip *= span_set->span_row_data_width;
627 strip += span_set->data_strip_start;
628 strip += span_set->strip_offset[span];
633 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug - get_strip_from_row: returns invalid "
634 "strip for ld=%x, row=%lx\n", ld, (long unsigned int)row);
639 * *****************************************************************************
642 * This routine calculates the Physical Arm for given strip using spanset.
644 * Inputs : HBA instance
645 * Logical drive number
649 * Outputs : Phys Arm - Phys Arm associated with strip
653 get_arm_from_strip(struct mrsas_softc *sc,
654 u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
656 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
657 LD_SPAN_SET *span_set;
658 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
659 u_int32_t info, strip_offset, span, span_offset;
661 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
662 span_set = &(ldSpanInfo[ld].span_set[info]);
664 if (span_set->span_row_data_width == 0)
666 if (strip > span_set->data_strip_end)
669 strip_offset = (u_int32_t)mega_mod64
670 ((strip - span_set->data_strip_start),
671 span_set->span_row_data_width);
673 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
674 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
675 block_span_info.noElements >= info + 1) {
676 if (strip_offset >= span_set->strip_offset[span])
677 span_offset = span_set->strip_offset[span];
681 mrsas_dprint(sc, MRSAS_PRL11, "LSI PRL11: get_arm_from_strip: "
682 "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld,
683 (long unsigned int)strip, (strip_offset - span_offset));
684 return (strip_offset - span_offset);
687 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug: - get_arm_from_strip: returns invalid arm"
688 " for ld=%x strip=%lx\n", ld, (long unsigned int)strip);
694 /* This Function will return Phys arm */
696 get_arm(struct mrsas_softc *sc, u_int32_t ld, u_int8_t span, u_int64_t stripe,
697 MR_DRV_RAID_MAP_ALL * map)
699 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
701 /* Need to check correct default value */
704 switch (raid->level) {
708 arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
711 /* start with logical arm */
712 arm = get_arm_from_strip(sc, ld, stripe, map);
722 * This routine calculates the arm, span and block for the specified stripe and
723 * reference in stripe using spanset
727 * ld - Logical drive number
728 * stripRow: Stripe number
729 * stripRef: Reference in stripe
731 * Outputs : span - Span number block - Absolute Block
732 * number in the physical disk
735 mr_spanset_get_phy_params(struct mrsas_softc *sc, u_int32_t ld, u_int64_t stripRow,
736 u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
737 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
739 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
741 u_int8_t physArm, span;
743 u_int8_t retval = TRUE;
744 u_int64_t *pdBlock = &io_info->pdBlock;
745 u_int16_t *pDevHandle = &io_info->devHandle;
746 u_int32_t logArm, rowMod, armQ, arm;
747 u_int8_t do_invader = 0;
749 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
752 /* Get row and span from io_info for Uneven Span IO. */
753 row = io_info->start_row;
754 span = io_info->start_span;
757 if (raid->level == 6) {
758 logArm = get_arm_from_strip(sc, ld, stripRow, map);
759 rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
760 armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
761 arm = armQ + 1 + logArm;
762 if (arm >= SPAN_ROW_SIZE(map, ld, span))
763 arm -= SPAN_ROW_SIZE(map, ld, span);
764 physArm = (u_int8_t)arm;
766 /* Calculate the arm */
767 physArm = get_arm(sc, ld, span, stripRow, map);
770 arRef = MR_LdSpanArrayGet(ld, span, map);
771 pd = MR_ArPdGet(arRef, physArm, map);
773 if (pd != MR_PD_INVALID)
774 *pDevHandle = MR_PdDevHandleGet(pd, map);
776 *pDevHandle = MR_PD_INVALID;
777 if ((raid->level >= 5) && ((!do_invader) || (do_invader &&
778 raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
779 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
780 else if (raid->level == 1) {
781 pd = MR_ArPdGet(arRef, physArm + 1, map);
782 if (pd != MR_PD_INVALID)
783 *pDevHandle = MR_PdDevHandleGet(pd, map);
787 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
788 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
789 io_info->span_arm = pRAID_Context->spanArm;
794 * MR_BuildRaidContext: Set up Fast path RAID context
796 * This function will initiate command processing. The start/end row and strip
797 * information is calculated then the lock is acquired. This function will
798 * return 0 if region lock was acquired OR return num strips.
801 MR_BuildRaidContext(struct mrsas_softc *sc, struct IO_REQUEST_INFO *io_info,
802 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
805 u_int32_t ld, stripSize, stripe_mask;
806 u_int64_t endLba, endStrip, endRow, start_row, start_strip;
809 u_int8_t num_strips, numRows;
810 u_int16_t ref_in_start_stripe, ref_in_end_stripe;
811 u_int64_t ldStartBlock;
812 u_int32_t numBlocks, ldTgtId;
813 u_int8_t isRead, stripIdx;
815 u_int8_t startlba_span = SPAN_INVALID;
816 u_int64_t *pdBlock = &io_info->pdBlock;
819 ldStartBlock = io_info->ldStartBlock;
820 numBlocks = io_info->numBlocks;
821 ldTgtId = io_info->ldTgtId;
822 isRead = io_info->isRead;
824 io_info->IoforUnevenSpan = 0;
825 io_info->start_span = SPAN_INVALID;
827 ld = MR_TargetIdToLdGet(ldTgtId, map);
828 raid = MR_LdRaidGet(ld, map);
830 if (raid->rowDataSize == 0) {
831 if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
833 else if (sc->UnevenSpanSupport) {
834 io_info->IoforUnevenSpan = 1;
836 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug: raid->rowDataSize is 0, but has SPAN[0] rowDataSize = 0x%0x,"
837 " but there is _NO_ UnevenSpanSupport\n",
838 MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
842 stripSize = 1 << raid->stripeShift;
843 stripe_mask = stripSize - 1;
845 * calculate starting row and stripe, and number of strips and rows
847 start_strip = ldStartBlock >> raid->stripeShift;
848 ref_in_start_stripe = (u_int16_t)(ldStartBlock & stripe_mask);
849 endLba = ldStartBlock + numBlocks - 1;
850 ref_in_end_stripe = (u_int16_t)(endLba & stripe_mask);
851 endStrip = endLba >> raid->stripeShift;
852 num_strips = (u_int8_t)(endStrip - start_strip + 1); /* End strip */
853 if (io_info->IoforUnevenSpan) {
854 start_row = get_row_from_strip(sc, ld, start_strip, map);
855 endRow = get_row_from_strip(sc, ld, endStrip, map);
856 if (raid->spanDepth == 1) {
858 *pdBlock = start_row << raid->stripeShift;
860 startlba_span = (u_int8_t)mr_spanset_get_span_block(sc, ld, start_row,
861 pdBlock, map, &error_code);
862 if (error_code == 1) {
863 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug: return from %s %d. Send IO w/o region lock.\n",
868 if (startlba_span == SPAN_INVALID) {
869 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug: return from %s %d for row 0x%llx,"
870 "start strip %llx endSrip %llx\n", __func__,
871 __LINE__, (unsigned long long)start_row,
872 (unsigned long long)start_strip,
873 (unsigned long long)endStrip);
876 io_info->start_span = startlba_span;
877 io_info->start_row = start_row;
878 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug: Check Span number from %s %d for row 0x%llx, "
879 " start strip 0x%llx endSrip 0x%llx span 0x%x\n",
880 __func__, __LINE__, (unsigned long long)start_row,
881 (unsigned long long)start_strip,
882 (unsigned long long)endStrip, startlba_span);
883 mrsas_dprint(sc, MRSAS_PRL11, "LSI Debug : 1. start_row 0x%llx endRow 0x%llx Start span 0x%x\n",
884 (unsigned long long)start_row, (unsigned long long)endRow, startlba_span);
886 start_row = mega_div64_32(start_strip, raid->rowDataSize);
887 endRow = mega_div64_32(endStrip, raid->rowDataSize);
890 numRows = (u_int8_t)(endRow - start_row + 1); /* get the row count */
893 * Calculate region info. (Assume region at start of first row, and
894 * assume this IO needs the full row - will adjust if not true.)
896 regStart = start_row << raid->stripeShift;
899 /* Check if we can send this I/O via FastPath */
900 if (raid->capability.fpCapable) {
902 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
903 ((num_strips == 1) ||
904 raid->capability.fpReadAcrossStripe));
906 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
907 ((num_strips == 1) ||
908 raid->capability.fpWriteAcrossStripe));
910 io_info->fpOkForIo = FALSE;
913 if (num_strips == 1) {
914 regStart += ref_in_start_stripe;
917 } else if (io_info->IoforUnevenSpan == 0) {
919 * For Even span region lock optimization. If the start strip
920 * is the last in the start row
922 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
923 regStart += ref_in_start_stripe;
925 * initialize count to sectors from startRef to end
928 regSize = stripSize - ref_in_start_stripe;
930 /* add complete rows in the middle of the transfer */
932 regSize += (numRows - 2) << raid->stripeShift;
934 /* if IO ends within first strip of last row */
935 if (endStrip == endRow * raid->rowDataSize)
936 regSize += ref_in_end_stripe + 1;
938 regSize += stripSize;
940 if (start_strip == (get_strip_from_row(sc, ld, start_row, map) +
941 SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
942 regStart += ref_in_start_stripe;
944 * initialize count to sectors from startRef to end
947 regSize = stripSize - ref_in_start_stripe;
949 /* add complete rows in the middle of the transfer */
951 regSize += (numRows - 2) << raid->stripeShift;
953 /* if IO ends within first strip of last row */
954 if (endStrip == get_strip_from_row(sc, ld, endRow, map))
955 regSize += ref_in_end_stripe + 1;
957 regSize += stripSize;
959 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
960 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
961 pRAID_Context->regLockFlags = (isRead) ? raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
963 pRAID_Context->regLockFlags = (isRead) ? REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
964 pRAID_Context->VirtualDiskTgtId = raid->targetId;
965 pRAID_Context->regLockRowLBA = regStart;
966 pRAID_Context->regLockLength = regSize;
967 pRAID_Context->configSeqNum = raid->seqNum;
970 * Get Phy Params only if FP capable, or else leave it to MR firmware
971 * to do the calculation.
973 if (io_info->fpOkForIo) {
974 retval = io_info->IoforUnevenSpan ?
975 mr_spanset_get_phy_params(sc, ld, start_strip,
976 ref_in_start_stripe, io_info, pRAID_Context, map) :
977 MR_GetPhyParams(sc, ld, start_strip,
978 ref_in_start_stripe, io_info, pRAID_Context, map);
979 /* If IO on an invalid Pd, then FP is not possible */
980 if (io_info->devHandle == MR_PD_INVALID)
981 io_info->fpOkForIo = FALSE;
984 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
985 retval = io_info->IoforUnevenSpan ?
986 mr_spanset_get_phy_params(sc, ld, start_strip + stripIdx,
987 ref_in_start_stripe, io_info, pRAID_Context, map) :
988 MR_GetPhyParams(sc, ld, start_strip + stripIdx,
989 ref_in_start_stripe, io_info, pRAID_Context, map);
995 /* Just for testing what arm we get for strip. */
996 get_arm_from_strip(sc, ld, start_strip, map);
1003 * This routine pepare spanset info from Valid Raid map and store it into local
1004 * copy of ldSpanInfo per instance data structure.
1007 * ldSpanInfo per HBA instance
1011 mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
1013 u_int8_t span, count;
1014 u_int32_t element, span_row_width;
1017 LD_SPAN_SET *span_set, *span_set_prev;
1018 MR_QUAD_ELEMENT *quad;
1022 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
1023 ld = MR_TargetIdToLdGet(ldCount, map);
1024 if (ld >= MAX_LOGICAL_DRIVES)
1026 raid = MR_LdRaidGet(ld, map);
1027 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
1028 for (span = 0; span < raid->spanDepth; span++) {
1029 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
1030 block_span_info.noElements < element + 1)
1033 span_set = &(ldSpanInfo[ld].span_set[element]);
1034 quad = &map->raidMap.ldSpanMap[ld].
1035 spanBlock[span].block_span_info.quad[element];
1037 span_set->diff = quad->diff;
1039 for (count = 0, span_row_width = 0;
1040 count < raid->spanDepth; count++) {
1041 if (map->raidMap.ldSpanMap[ld].spanBlock[count].
1042 block_span_info.noElements >= element + 1) {
1043 span_set->strip_offset[count] = span_row_width;
1045 MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize;
1047 printf("LSI Debug span %x rowDataSize %x\n", count,
1048 MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize);
1053 span_set->span_row_data_width = span_row_width;
1054 span_row = mega_div64_32(((quad->logEnd -
1055 quad->logStart) + quad->diff), quad->diff);
1058 span_set->log_start_lba = 0;
1059 span_set->log_end_lba =
1060 ((span_row << raid->stripeShift) * span_row_width) - 1;
1062 span_set->span_row_start = 0;
1063 span_set->span_row_end = span_row - 1;
1065 span_set->data_strip_start = 0;
1066 span_set->data_strip_end = (span_row * span_row_width) - 1;
1068 span_set->data_row_start = 0;
1069 span_set->data_row_end = (span_row * quad->diff) - 1;
1071 span_set_prev = &(ldSpanInfo[ld].span_set[element - 1]);
1072 span_set->log_start_lba = span_set_prev->log_end_lba + 1;
1073 span_set->log_end_lba = span_set->log_start_lba +
1074 ((span_row << raid->stripeShift) * span_row_width) - 1;
1076 span_set->span_row_start = span_set_prev->span_row_end + 1;
1077 span_set->span_row_end =
1078 span_set->span_row_start + span_row - 1;
1080 span_set->data_strip_start =
1081 span_set_prev->data_strip_end + 1;
1082 span_set->data_strip_end = span_set->data_strip_start +
1083 (span_row * span_row_width) - 1;
1085 span_set->data_row_start = span_set_prev->data_row_end + 1;
1086 span_set->data_row_end = span_set->data_row_start +
1087 (span_row * quad->diff) - 1;
1091 if (span == raid->spanDepth)
1092 break; /* no quads remain */
1096 getSpanInfo(map, ldSpanInfo); /* to get span set info */
1101 * mrsas_update_load_balance_params: Update load balance parmas
1103 * sc - driver softc instance
1104 * drv_map - driver RAID map
1105 * lbInfo - Load balance info
1107 * This function updates the load balance parameters for the LD config of a two
1108 * drive optimal RAID-1.
1111 mrsas_update_load_balance_params(struct mrsas_softc *sc,
1112 MR_DRV_RAID_MAP_ALL * drv_map, PLD_LOAD_BALANCE_INFO lbInfo)
1118 if(sc->lb_pending_cmds > 128 || sc->lb_pending_cmds < 1)
1119 sc-> lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
1121 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1122 ld = MR_TargetIdToLdGet(ldCount, drv_map);
1123 if (ld >= MAX_LOGICAL_DRIVES_EXT) {
1124 lbInfo[ldCount].loadBalanceFlag = 0;
1128 raid = MR_LdRaidGet(ld, drv_map);
1129 if ((raid->level != 1) ||
1130 (raid->ldState != MR_LD_STATE_OPTIMAL)) {
1131 lbInfo[ldCount].loadBalanceFlag = 0;
1134 lbInfo[ldCount].loadBalanceFlag = 1;
1140 * mrsas_set_pd_lba: Sets PD LBA
1141 * input: io_request pointer
1145 * Local RAID map pointer
1146 * Start block of IO Block Size
1148 * Used to set the PD logical block address in CDB for FP IOs.
1151 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request, u_int8_t cdb_len,
1152 struct IO_REQUEST_INFO *io_info, union ccb *ccb,
1153 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
1154 u_int32_t ld_block_size)
1158 u_int64_t start_blk = io_info->pdBlock;
1159 u_int8_t *cdb = io_request->CDB.CDB32;
1160 u_int32_t num_blocks = io_info->numBlocks;
1161 u_int8_t opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1162 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
1164 /* Check if T10 PI (DIF) is enabled for this LD */
1165 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1166 raid = MR_LdRaidGet(ld, local_map_ptr);
1167 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1168 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1169 cdb[0] = MRSAS_SCSI_VARIABLE_LENGTH_CMD;
1170 cdb[7] = MRSAS_SCSI_ADDL_CDB_LEN;
1172 if (ccb_h->flags == CAM_DIR_OUT)
1173 cdb[9] = MRSAS_SCSI_SERVICE_ACTION_READ32;
1175 cdb[9] = MRSAS_SCSI_SERVICE_ACTION_WRITE32;
1176 cdb[10] = MRSAS_RD_WR_PROTECT_CHECK_ALL;
1179 cdb[12] = (u_int8_t)((start_blk >> 56) & 0xff);
1180 cdb[13] = (u_int8_t)((start_blk >> 48) & 0xff);
1181 cdb[14] = (u_int8_t)((start_blk >> 40) & 0xff);
1182 cdb[15] = (u_int8_t)((start_blk >> 32) & 0xff);
1183 cdb[16] = (u_int8_t)((start_blk >> 24) & 0xff);
1184 cdb[17] = (u_int8_t)((start_blk >> 16) & 0xff);
1185 cdb[18] = (u_int8_t)((start_blk >> 8) & 0xff);
1186 cdb[19] = (u_int8_t)(start_blk & 0xff);
1188 /* Logical block reference tag */
1189 io_request->CDB.EEDP32.PrimaryReferenceTag = swap32(ref_tag);
1190 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1191 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1193 /* Transfer length */
1194 cdb[28] = (u_int8_t)((num_blocks >> 24) & 0xff);
1195 cdb[29] = (u_int8_t)((num_blocks >> 16) & 0xff);
1196 cdb[30] = (u_int8_t)((num_blocks >> 8) & 0xff);
1197 cdb[31] = (u_int8_t)(num_blocks & 0xff);
1199 /* set SCSI IO EEDP Flags */
1200 if (ccb_h->flags == CAM_DIR_OUT) {
1201 io_request->EEDPFlags =
1202 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1203 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1204 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1205 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1206 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1208 io_request->EEDPFlags =
1209 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1210 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1212 io_request->Control |= (0x4 << 26);
1213 io_request->EEDPBlockSize = ld_block_size;
1215 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1216 if (((cdb_len == 12) || (cdb_len == 16)) &&
1217 (start_blk <= 0xffffffff)) {
1218 if (cdb_len == 16) {
1219 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1224 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1230 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1237 /* Transfer length */
1238 cdb[8] = (u_int8_t)(num_blocks & 0xff);
1239 cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1241 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1243 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1244 /* Convert to 16 byte CDB for large LBA's */
1247 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1251 opcode = cdb[0] == READ_10 ? READ_16 : WRITE_16;
1257 opcode = cdb[0] == READ_12 ? READ_16 : WRITE_16;
1264 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1271 /* Transfer length */
1272 cdb[13] = (u_int8_t)(num_blocks & 0xff);
1273 cdb[12] = (u_int8_t)((num_blocks >> 8) & 0xff);
1274 cdb[11] = (u_int8_t)((num_blocks >> 16) & 0xff);
1275 cdb[10] = (u_int8_t)((num_blocks >> 24) & 0xff);
1277 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1279 } else if ((cdb_len == 6) && (start_blk > 0x1fffff)) {
1280 /* convert to 10 byte CDB */
1281 opcode = cdb[0] == READ_6 ? READ_10 : WRITE_10;
1284 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1288 /* Set transfer length */
1289 cdb[8] = (u_int8_t)(num_blocks & 0xff);
1290 cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1292 /* Specify 10-byte cdb */
1295 /* Fall through normal case, just load LBA here */
1296 u_int8_t val = cdb[1] & 0xE0;
1299 cdb[3] = (u_int8_t)(start_blk & 0xff);
1300 cdb[2] = (u_int8_t)((start_blk >> 8) & 0xff);
1301 cdb[1] = val | ((u_int8_t)(start_blk >> 16) & 0x1f);
1304 cdb[5] = (u_int8_t)(start_blk & 0xff);
1305 cdb[4] = (u_int8_t)((start_blk >> 8) & 0xff);
1306 cdb[3] = (u_int8_t)((start_blk >> 16) & 0xff);
1307 cdb[2] = (u_int8_t)((start_blk >> 24) & 0xff);
1310 cdb[5] = (u_int8_t)(start_blk & 0xff);
1311 cdb[4] = (u_int8_t)((start_blk >> 8) & 0xff);
1312 cdb[3] = (u_int8_t)((start_blk >> 16) & 0xff);
1313 cdb[2] = (u_int8_t)((start_blk >> 24) & 0xff);
1316 cdb[9] = (u_int8_t)(start_blk & 0xff);
1317 cdb[8] = (u_int8_t)((start_blk >> 8) & 0xff);
1318 cdb[7] = (u_int8_t)((start_blk >> 16) & 0xff);
1319 cdb[6] = (u_int8_t)((start_blk >> 24) & 0xff);
1320 cdb[5] = (u_int8_t)((start_blk >> 32) & 0xff);
1321 cdb[4] = (u_int8_t)((start_blk >> 40) & 0xff);
1322 cdb[3] = (u_int8_t)((start_blk >> 48) & 0xff);
1323 cdb[2] = (u_int8_t)((start_blk >> 56) & 0xff);
1330 * mrsas_get_best_arm_pd: Determine the best spindle arm
1333 * lbInfo - Load balance info
1334 * io_info - IO request info
1336 * This function determines and returns the best arm by looking at the
1337 * parameters of the last PD access.
1339 u_int8_t mrsas_get_best_arm_pd(struct mrsas_softc *sc,
1340 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info)
1343 MR_DRV_RAID_MAP_ALL *drv_map;
1344 u_int16_t pend0, pend1, ld;
1345 u_int64_t diff0, diff1;
1346 u_int8_t bestArm, pd0, pd1, span, arm;
1347 u_int32_t arRef, span_row_size;
1349 u_int64_t block = io_info->ldStartBlock;
1350 u_int32_t count = io_info->numBlocks;
1352 span = ((io_info->span_arm & RAID_CTX_SPANARM_SPAN_MASK)
1353 >> RAID_CTX_SPANARM_SPAN_SHIFT);
1354 arm = (io_info->span_arm & RAID_CTX_SPANARM_ARM_MASK);
1356 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
1357 ld = MR_TargetIdToLdGet(io_info->ldTgtId, drv_map);
1358 raid = MR_LdRaidGet(ld, drv_map);
1359 span_row_size = sc->UnevenSpanSupport ?
1360 SPAN_ROW_SIZE(drv_map, ld, span) : raid->rowSize;
1362 arRef = MR_LdSpanArrayGet(ld, span, drv_map);
1363 pd0 = MR_ArPdGet(arRef, arm, drv_map);
1364 pd1 = MR_ArPdGet(arRef, (arm + 1) >= span_row_size ?
1365 (arm + 1 - span_row_size): arm + 1, drv_map);
1367 /* get the pending cmds for the data and mirror arms */
1368 pend0 = mrsas_atomic_read(&lbInfo->scsi_pending_cmds[pd0]);
1369 pend1 = mrsas_atomic_read(&lbInfo->scsi_pending_cmds[pd1]);
1371 /* Determine the disk whose head is nearer to the req. block */
1372 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[pd0]);
1373 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[pd1]);
1374 bestArm = (diff0 <= diff1 ? arm : arm ^ 1);
1376 if ((bestArm == arm && pend0 > pend1 + sc->lb_pending_cmds) ||
1377 (bestArm != arm && pend1 > pend0 + sc->lb_pending_cmds))
1380 /* Update the last accessed block on the correct pd */
1381 lbInfo->last_accessed_block[bestArm==arm ? pd0 : pd1] = block + count - 1;
1382 io_info->span_arm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | bestArm;
1383 io_info->pd_after_lb = (bestArm == arm) ? pd0:pd1;
1386 printf("LSI Debug R1 Load balance occur - span 0x%x arm 0x%x bestArm 0x%x "
1387 "io_info->span_arm 0x%x\n",
1388 span, arm, bestArm, io_info->span_arm);
1391 return io_info->pd_after_lb;
1395 * mrsas_get_updated_dev_handle: Get the update dev handle
1397 * sc - Adapter instance soft state
1398 * lbInfo - Load balance info
1399 * io_info - io_info pointer
1401 * This function determines and returns the updated dev handle.
1403 u_int16_t mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
1404 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info)
1407 u_int16_t devHandle;
1408 MR_DRV_RAID_MAP_ALL *drv_map;
1410 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
1412 /* get best new arm */
1413 arm_pd = mrsas_get_best_arm_pd(sc, lbInfo, io_info);
1414 devHandle = MR_PdDevHandleGet(arm_pd, drv_map);
1415 mrsas_atomic_inc(&lbInfo->scsi_pending_cmds[arm_pd]);
1421 * MR_GetPhyParams: Calculates arm, span, and block
1422 * Inputs: Adapter soft state
1423 * Logical drive number (LD)
1424 * Stripe number(stripRow)
1425 * Reference in stripe (stripRef)
1427 * Outputs: Absolute Block number in the physical disk
1429 * This routine calculates the arm, span and block for the specified stripe and
1430 * reference in stripe.
1433 MR_GetPhyParams(struct mrsas_softc *sc, u_int32_t ld,
1435 u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
1436 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
1438 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1439 u_int32_t pd, arRef;
1440 u_int8_t physArm, span;
1442 u_int8_t retval = TRUE;
1444 u_int64_t *pdBlock = &io_info->pdBlock;
1445 u_int16_t *pDevHandle = &io_info->devHandle;
1446 u_int32_t rowMod, armQ, arm, logArm;
1447 u_int8_t do_invader = 0;
1449 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1452 row = mega_div64_32(stripRow, raid->rowDataSize);
1454 if (raid->level == 6) {
1455 /* logical arm within row */
1456 logArm = mega_mod64(stripRow, raid->rowDataSize);
1457 if (raid->rowSize == 0)
1459 rowMod = mega_mod64(row, raid->rowSize); /* get logical row mod */
1460 armQ = raid->rowSize - 1 - rowMod; /* index of Q drive */
1461 arm = armQ + 1 + logArm;/* data always logically follows Q */
1462 if (arm >= raid->rowSize) /* handle wrap condition */
1463 arm -= raid->rowSize;
1464 physArm = (u_int8_t)arm;
1466 if (raid->modFactor == 0)
1468 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow, raid->modFactor), map);
1471 if (raid->spanDepth == 1) {
1473 *pdBlock = row << raid->stripeShift;
1475 span = (u_int8_t)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
1476 if (error_code == 1)
1480 /* Get the array on which this span is present */
1481 arRef = MR_LdSpanArrayGet(ld, span, map);
1483 pd = MR_ArPdGet(arRef, physArm, map); /* Get the Pd. */
1485 if (pd != MR_PD_INVALID)
1486 /* Get dev handle from Pd */
1487 *pDevHandle = MR_PdDevHandleGet(pd, map);
1489 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
1490 if ((raid->level >= 5) && ((!do_invader) || (do_invader &&
1491 raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
1492 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
1493 else if (raid->level == 1) {
1494 /* Get Alternate Pd. */
1495 pd = MR_ArPdGet(arRef, physArm + 1, map);
1496 if (pd != MR_PD_INVALID)
1497 /* Get dev handle from Pd. */
1498 *pDevHandle = MR_PdDevHandleGet(pd, map);
1502 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
1503 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
1504 io_info->span_arm = pRAID_Context->spanArm;
1509 * MR_GetSpanBlock: Calculates span block
1515 * Outputs: Span number Error code
1517 * This routine calculates the span from the span block info.
1520 MR_GetSpanBlock(u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
1521 MR_DRV_RAID_MAP_ALL * map, int *div_error)
1523 MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
1524 MR_QUAD_ELEMENT *quad;
1525 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1527 u_int64_t blk, debugBlk;
1529 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
1530 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
1531 quad = &pSpanBlock->block_span_info.quad[j];
1532 if (quad->diff == 0) {
1536 if (quad->logStart <= row && row <= quad->logEnd &&
1537 (mega_mod64(row - quad->logStart, quad->diff)) == 0) {
1538 if (span_blk != NULL) {
1539 blk = mega_div64_32((row - quad->logStart), quad->diff);
1541 blk = (blk + quad->offsetInSpan) << raid->stripeShift;