2 * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Marian Choy
3 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
4 * Support: freebsdraid@avagotech.com
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 2. Redistributions
12 * in binary form must reproduce the above copyright notice, this list of
13 * conditions and the following disclaimer in the documentation and/or other
14 * materials provided with the distribution. 3. Neither the name of the
15 * <ORGANIZATION> nor the names of its contributors may be used to endorse or
16 * promote products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation are
32 * those of the authors and should not be interpreted as representing
33 * official policies,either expressed or implied, of the FreeBSD Project.
35 * Send feedback to: <megaraidfbsd@avagotech.com> Mail to: AVAGO TECHNOLOGIES, 1621
36 * Barber Lane, Milpitas, CA 95035 ATTN: MegaRaid FreeBSD
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <dev/mrsas/mrsas.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_sim.h>
48 #include <cam/cam_xpt_sim.h>
49 #include <cam/cam_debug.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_xpt_periph.h>
57 u_int8_t MR_ValidateMapInfo(struct mrsas_softc *sc);
59 mrsas_get_best_arm_pd(struct mrsas_softc *sc,
60 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
62 MR_BuildRaidContext(struct mrsas_softc *sc,
63 struct IO_REQUEST_INFO *io_info,
64 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map);
66 MR_GetPhyParams(struct mrsas_softc *sc, u_int32_t ld,
67 u_int64_t stripRow, u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
68 RAID_CONTEXT * pRAID_Context,
69 MR_DRV_RAID_MAP_ALL * map);
70 u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
71 u_int32_t MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
72 u_int16_t MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
74 mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
75 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
76 u_int32_t mega_mod64(u_int64_t dividend, u_int32_t divisor);
78 MR_GetSpanBlock(u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
79 MR_DRV_RAID_MAP_ALL * map, int *div_error);
80 u_int64_t mega_div64_32(u_int64_t dividend, u_int32_t divisor);
82 mrsas_update_load_balance_params(struct mrsas_softc *sc,
83 MR_DRV_RAID_MAP_ALL * map, PLD_LOAD_BALANCE_INFO lbInfo);
85 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request,
86 u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb,
87 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
88 u_int32_t ld_block_size);
90 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
91 MR_DRV_RAID_MAP_ALL * map);
92 static u_int16_t MR_PdDevHandleGet(u_int32_t pd, MR_DRV_RAID_MAP_ALL * map);
94 MR_ArPdGet(u_int32_t ar, u_int32_t arm,
95 MR_DRV_RAID_MAP_ALL * map);
97 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span,
98 MR_DRV_RAID_MAP_ALL * map);
100 MR_LdDataArmGet(u_int32_t ld, u_int32_t armIdx,
101 MR_DRV_RAID_MAP_ALL * map);
102 static MR_SPAN_BLOCK_INFO *
103 MR_LdSpanInfoGet(u_int32_t ld,
104 MR_DRV_RAID_MAP_ALL * map);
105 MR_LD_RAID *MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
106 void MR_PopulateDrvRaidMap(struct mrsas_softc *sc);
110 * Spanset related function prototypes Added for PRL11 configuration (Uneven
113 void mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo);
115 mr_spanset_get_phy_params(struct mrsas_softc *sc, u_int32_t ld,
116 u_int64_t stripRow, u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
117 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map);
119 get_row_from_strip(struct mrsas_softc *sc, u_int32_t ld,
120 u_int64_t strip, MR_DRV_RAID_MAP_ALL * map);
122 mr_spanset_get_span_block(struct mrsas_softc *sc,
123 u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
124 MR_DRV_RAID_MAP_ALL * map, int *div_error);
126 get_arm(struct mrsas_softc *sc, u_int32_t ld, u_int8_t span,
127 u_int64_t stripe, MR_DRV_RAID_MAP_ALL * map);
131 * Spanset related defines Added for PRL11 configuration(Uneven span support)
133 #define SPAN_ROW_SIZE(map, ld, index_) MR_LdSpanPtrGet(ld, index_, map)->spanRowSize
134 #define SPAN_ROW_DATA_SIZE(map_, ld, index_) \
135 MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize
136 #define SPAN_INVALID 0xff
143 typedef u_int64_t REGION_KEY;
144 typedef u_int32_t REGION_LEN;
146 #define MR_LD_STATE_OPTIMAL 3
150 #define LB_PENDING_CMDS_DEFAULT 4
157 #define ABS_DIFF(a,b) ( ((a) > (b)) ? ((a) - (b)) : ((b) - (a)) )
161 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
162 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
163 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
164 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
168 * In-line functions for mod and divide of 64-bit dividend and 32-bit
169 * divisor. Assumes a check for a divisor of zero is not possible.
171 * @param dividend: Dividend
172 * @param divisor: Divisor
176 #define mega_mod64(dividend, divisor) ({ \
178 remainder = ((u_int64_t) (dividend)) % (u_int32_t) (divisor); \
181 #define mega_div64_32(dividend, divisor) ({ \
183 quotient = ((u_int64_t) (dividend)) / (u_int32_t) (divisor); \
188 * Various RAID map access functions. These functions access the various
189 * parts of the RAID map and returns the appropriate parameters.
193 MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
195 return (&map->raidMap.ldSpanMap[ld].ldRaid);
199 MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
201 return (map->raidMap.ldSpanMap[ld].ldRaid.targetId);
205 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
207 return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
211 MR_LdDataArmGet(u_int32_t ld, u_int32_t armIdx, MR_DRV_RAID_MAP_ALL * map)
213 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
217 MR_PdDevHandleGet(u_int32_t pd, MR_DRV_RAID_MAP_ALL * map)
219 return map->raidMap.devHndlInfo[pd].curDevHdl;
223 MR_ArPdGet(u_int32_t ar, u_int32_t arm, MR_DRV_RAID_MAP_ALL * map)
225 return map->raidMap.arMapInfo[ar].pd[arm];
229 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
231 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
234 static MR_SPAN_BLOCK_INFO *
235 MR_LdSpanInfoGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
237 return &map->raidMap.ldSpanMap[ld].spanBlock[0];
241 MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
243 return map->raidMap.ldTgtIdToLd[ldTgtId];
247 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
250 u_int32_t ld, ldBlockSize = MRSAS_SCSIBLOCKSIZE;
252 ld = MR_TargetIdToLdGet(ldTgtId, map);
255 * Check if logical drive was removed.
257 if (ld >= MAX_LOGICAL_DRIVES)
260 raid = MR_LdRaidGet(ld, map);
261 ldBlockSize = raid->logicalBlockLength;
263 ldBlockSize = MRSAS_SCSIBLOCKSIZE;
269 * This function will Populate Driver Map using firmware raid map
272 MR_PopulateDrvRaidMap(struct mrsas_softc *sc)
274 MR_FW_RAID_MAP_ALL *fw_map_old = NULL;
275 MR_FW_RAID_MAP *pFwRaidMap = NULL;
278 MR_DRV_RAID_MAP_ALL *drv_map = sc->ld_drv_map[(sc->map_id & 1)];
279 MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
281 if (sc->max256vdSupport) {
282 memcpy(sc->ld_drv_map[sc->map_id & 1],
283 sc->raidmap_mem[sc->map_id & 1],
286 * New Raid map will not set totalSize, so keep expected
287 * value for legacy code in ValidateMapInfo
289 pDrvRaidMap->totalSize = sizeof(MR_FW_RAID_MAP_EXT);
291 fw_map_old = (MR_FW_RAID_MAP_ALL *) sc->raidmap_mem[(sc->map_id & 1)];
292 pFwRaidMap = &fw_map_old->raidMap;
295 for (i = 0; i < pFwRaidMap->ldCount; i++) {
296 device_printf(sc->mrsas_dev,
297 "Index 0x%x Target Id 0x%x Seq Num 0x%x Size 0/%lx\n", i,
298 fw_map_old->raidMap.ldSpanMap[i].ldRaid.targetId,
299 fw_map_old->raidMap.ldSpanMap[i].ldRaid.seqNum,
300 fw_map_old->raidMap.ldSpanMap[i].ldRaid.size);
304 memset(drv_map, 0, sc->drv_map_sz);
305 pDrvRaidMap->totalSize = pFwRaidMap->totalSize;
306 pDrvRaidMap->ldCount = pFwRaidMap->ldCount;
307 pDrvRaidMap->fpPdIoTimeoutSec =
308 pFwRaidMap->fpPdIoTimeoutSec;
310 for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++) {
311 pDrvRaidMap->ldTgtIdToLd[i] =
312 (u_int8_t)pFwRaidMap->ldTgtIdToLd[i];
315 for (i = 0; i < pDrvRaidMap->ldCount; i++) {
316 pDrvRaidMap->ldSpanMap[i] =
317 pFwRaidMap->ldSpanMap[i];
320 device_printf(sc->mrsas_dev, "pFwRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
321 "pFwRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x size 0x%x\n",
322 i, i, pFwRaidMap->ldSpanMap[i].ldRaid.targetId,
323 pFwRaidMap->ldSpanMap[i].ldRaid.seqNum,
324 (u_int32_t)pFwRaidMap->ldSpanMap[i].ldRaid.rowSize);
325 device_printf(sc->mrsas_dev, "pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x"
326 "pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x size 0x%x\n", i, i,
327 pDrvRaidMap->ldSpanMap[i].ldRaid.targetId,
328 pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum,
329 (u_int32_t)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);
330 device_printf(sc->mrsas_dev, "drv raid map all %p raid map %p LD RAID MAP %p/%p\n",
331 drv_map, pDrvRaidMap,
332 &pFwRaidMap->ldSpanMap[i].ldRaid, &pDrvRaidMap->ldSpanMap[i].ldRaid);
336 memcpy(pDrvRaidMap->arMapInfo, pFwRaidMap->arMapInfo,
337 sizeof(MR_ARRAY_INFO) * MAX_RAIDMAP_ARRAYS);
338 memcpy(pDrvRaidMap->devHndlInfo, pFwRaidMap->devHndlInfo,
339 sizeof(MR_DEV_HANDLE_INFO) *
340 MAX_RAIDMAP_PHYSICAL_DEVICES);
345 * MR_ValidateMapInfo: Validate RAID map
346 * input: Adapter instance soft state
348 * This function checks and validates the loaded RAID map. It returns 0 if
349 * successful, and 1 otherwise.
352 MR_ValidateMapInfo(struct mrsas_softc *sc)
357 MR_PopulateDrvRaidMap(sc);
359 MR_DRV_RAID_MAP_ALL *drv_map = sc->ld_drv_map[(sc->map_id & 1)];
360 MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
362 u_int32_t expected_map_size;
364 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
365 pDrvRaidMap = &drv_map->raidMap;
366 PLD_SPAN_INFO ldSpanInfo = (PLD_SPAN_INFO) & sc->log_to_span;
368 if (sc->max256vdSupport)
369 expected_map_size = sizeof(MR_FW_RAID_MAP_EXT);
372 (sizeof(MR_FW_RAID_MAP) - sizeof(MR_LD_SPAN_MAP)) +
373 (sizeof(MR_LD_SPAN_MAP) * pDrvRaidMap->ldCount);
375 if (pDrvRaidMap->totalSize != expected_map_size) {
376 device_printf(sc->mrsas_dev, "map size %x not matching ld count\n", expected_map_size);
377 device_printf(sc->mrsas_dev, "span map= %x\n", (unsigned int)sizeof(MR_LD_SPAN_MAP));
378 device_printf(sc->mrsas_dev, "pDrvRaidMap->totalSize=%x\n", pDrvRaidMap->totalSize);
381 if (sc->UnevenSpanSupport) {
382 mr_update_span_set(drv_map, ldSpanInfo);
384 mrsas_update_load_balance_params(sc, drv_map, sc->load_balance_info);
391 * Function to print info about span set created in driver from FW raid map
394 * ldSpanInfo: ld map span info per HBA instance
400 getSpanInfo(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
406 LD_SPAN_SET *span_set;
407 MR_QUAD_ELEMENT *quad;
411 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
412 ld = MR_TargetIdToLdGet(ldCount, map);
413 if (ld >= MAX_LOGICAL_DRIVES) {
416 raid = MR_LdRaidGet(ld, map);
417 printf("LD %x: span_depth=%x\n", ld, raid->spanDepth);
418 for (span = 0; span < raid->spanDepth; span++)
419 printf("Span=%x, number of quads=%x\n", span,
420 map->raidMap.ldSpanMap[ld].spanBlock[span].
421 block_span_info.noElements);
422 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
423 span_set = &(ldSpanInfo[ld].span_set[element]);
424 if (span_set->span_row_data_width == 0)
427 printf("Span Set %x: width=%x, diff=%x\n", element,
428 (unsigned int)span_set->span_row_data_width,
429 (unsigned int)span_set->diff);
430 printf("logical LBA start=0x%08lx, end=0x%08lx\n",
431 (long unsigned int)span_set->log_start_lba,
432 (long unsigned int)span_set->log_end_lba);
433 printf("span row start=0x%08lx, end=0x%08lx\n",
434 (long unsigned int)span_set->span_row_start,
435 (long unsigned int)span_set->span_row_end);
436 printf("data row start=0x%08lx, end=0x%08lx\n",
437 (long unsigned int)span_set->data_row_start,
438 (long unsigned int)span_set->data_row_end);
439 printf("data strip start=0x%08lx, end=0x%08lx\n",
440 (long unsigned int)span_set->data_strip_start,
441 (long unsigned int)span_set->data_strip_end);
443 for (span = 0; span < raid->spanDepth; span++) {
444 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
445 block_span_info.noElements >= element + 1) {
446 quad = &map->raidMap.ldSpanMap[ld].
447 spanBlock[span].block_span_info.
449 printf("Span=%x, Quad=%x, diff=%x\n", span,
450 element, quad->diff);
451 printf("offset_in_span=0x%08lx\n",
452 (long unsigned int)quad->offsetInSpan);
453 printf("logical start=0x%08lx, end=0x%08lx\n",
454 (long unsigned int)quad->logStart,
455 (long unsigned int)quad->logEnd);
466 * This routine calculates the Span block for given row using spanset.
468 * Inputs : HBA instance
469 * ld: Logical drive number
473 * Outputs : span - Span number block
474 * - Absolute Block number in the physical disk
475 * div_error - Devide error code.
479 mr_spanset_get_span_block(struct mrsas_softc *sc, u_int32_t ld, u_int64_t row,
480 u_int64_t *span_blk, MR_DRV_RAID_MAP_ALL * map, int *div_error)
482 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
483 LD_SPAN_SET *span_set;
484 MR_QUAD_ELEMENT *quad;
485 u_int32_t span, info;
486 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
488 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
489 span_set = &(ldSpanInfo[ld].span_set[info]);
491 if (span_set->span_row_data_width == 0)
493 if (row > span_set->data_row_end)
496 for (span = 0; span < raid->spanDepth; span++)
497 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
498 block_span_info.noElements >= info + 1) {
499 quad = &map->raidMap.ldSpanMap[ld].
501 block_span_info.quad[info];
502 if (quad->diff == 0) {
506 if (quad->logStart <= row &&
507 row <= quad->logEnd &&
508 (mega_mod64(row - quad->logStart,
510 if (span_blk != NULL) {
514 ((row - quad->logStart),
516 blk = (blk + quad->offsetInSpan)
517 << raid->stripeShift;
529 * This routine calculates the row for given strip using spanset.
531 * Inputs : HBA instance
532 * ld: Logical drive number
536 * Outputs : row - row associated with strip
540 get_row_from_strip(struct mrsas_softc *sc,
541 u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
543 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
544 LD_SPAN_SET *span_set;
545 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
546 u_int32_t info, strip_offset, span, span_offset;
547 u_int64_t span_set_Strip, span_set_Row;
549 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
550 span_set = &(ldSpanInfo[ld].span_set[info]);
552 if (span_set->span_row_data_width == 0)
554 if (strip > span_set->data_strip_end)
557 span_set_Strip = strip - span_set->data_strip_start;
558 strip_offset = mega_mod64(span_set_Strip,
559 span_set->span_row_data_width);
560 span_set_Row = mega_div64_32(span_set_Strip,
561 span_set->span_row_data_width) * span_set->diff;
562 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
563 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
564 block_span_info.noElements >= info + 1) {
566 span_set->strip_offset[span])
571 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug : Strip 0x%llx, span_set_Strip 0x%llx, span_set_Row 0x%llx "
572 "data width 0x%llx span offset 0x%llx\n", (unsigned long long)strip,
573 (unsigned long long)span_set_Strip,
574 (unsigned long long)span_set_Row,
575 (unsigned long long)span_set->span_row_data_width, (unsigned long long)span_offset);
576 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug : For strip 0x%llx row is 0x%llx\n", (unsigned long long)strip,
577 (unsigned long long)span_set->data_row_start +
578 (unsigned long long)span_set_Row + (span_offset - 1));
579 return (span_set->data_row_start + span_set_Row + (span_offset - 1));
587 * This routine calculates the Start Strip for given row using spanset.
589 * Inputs: HBA instance
590 * ld: Logical drive number
594 * Outputs : Strip - Start strip associated with row
598 get_strip_from_row(struct mrsas_softc *sc,
599 u_int32_t ld, u_int64_t row, MR_DRV_RAID_MAP_ALL * map)
601 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
602 LD_SPAN_SET *span_set;
603 MR_QUAD_ELEMENT *quad;
604 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
605 u_int32_t span, info;
608 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
609 span_set = &(ldSpanInfo[ld].span_set[info]);
611 if (span_set->span_row_data_width == 0)
613 if (row > span_set->data_row_end)
616 for (span = 0; span < raid->spanDepth; span++)
617 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
618 block_span_info.noElements >= info + 1) {
619 quad = &map->raidMap.ldSpanMap[ld].
620 spanBlock[span].block_span_info.quad[info];
621 if (quad->logStart <= row &&
622 row <= quad->logEnd &&
623 mega_mod64((row - quad->logStart),
625 strip = mega_div64_32
626 (((row - span_set->data_row_start)
629 strip *= span_set->span_row_data_width;
630 strip += span_set->data_strip_start;
631 strip += span_set->strip_offset[span];
636 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug - get_strip_from_row: returns invalid "
637 "strip for ld=%x, row=%lx\n", ld, (long unsigned int)row);
642 * *****************************************************************************
645 * This routine calculates the Physical Arm for given strip using spanset.
647 * Inputs : HBA instance
648 * Logical drive number
652 * Outputs : Phys Arm - Phys Arm associated with strip
656 get_arm_from_strip(struct mrsas_softc *sc,
657 u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
659 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
660 LD_SPAN_SET *span_set;
661 PLD_SPAN_INFO ldSpanInfo = sc->log_to_span;
662 u_int32_t info, strip_offset, span, span_offset;
664 for (info = 0; info < MAX_QUAD_DEPTH; info++) {
665 span_set = &(ldSpanInfo[ld].span_set[info]);
667 if (span_set->span_row_data_width == 0)
669 if (strip > span_set->data_strip_end)
672 strip_offset = (u_int32_t)mega_mod64
673 ((strip - span_set->data_strip_start),
674 span_set->span_row_data_width);
676 for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
677 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
678 block_span_info.noElements >= info + 1) {
679 if (strip_offset >= span_set->strip_offset[span])
680 span_offset = span_set->strip_offset[span];
684 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO PRL11: get_arm_from_strip: "
685 "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld,
686 (long unsigned int)strip, (strip_offset - span_offset));
687 return (strip_offset - span_offset);
690 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug: - get_arm_from_strip: returns invalid arm"
691 " for ld=%x strip=%lx\n", ld, (long unsigned int)strip);
697 /* This Function will return Phys arm */
699 get_arm(struct mrsas_softc *sc, u_int32_t ld, u_int8_t span, u_int64_t stripe,
700 MR_DRV_RAID_MAP_ALL * map)
702 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
704 /* Need to check correct default value */
707 switch (raid->level) {
711 arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
714 /* start with logical arm */
715 arm = get_arm_from_strip(sc, ld, stripe, map);
725 * This routine calculates the arm, span and block for the specified stripe and
726 * reference in stripe using spanset
730 * ld - Logical drive number
731 * stripRow: Stripe number
732 * stripRef: Reference in stripe
734 * Outputs : span - Span number block - Absolute Block
735 * number in the physical disk
738 mr_spanset_get_phy_params(struct mrsas_softc *sc, u_int32_t ld, u_int64_t stripRow,
739 u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
740 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
742 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
744 u_int8_t physArm, span;
746 u_int8_t retval = TRUE;
747 u_int64_t *pdBlock = &io_info->pdBlock;
748 u_int16_t *pDevHandle = &io_info->devHandle;
749 u_int32_t logArm, rowMod, armQ, arm;
750 u_int8_t do_invader = 0;
752 if ((sc->device_id == MRSAS_INVADER) ||
753 (sc->device_id == MRSAS_FURY) ||
754 (sc->device_id == MRSAS_INTRUDER) ||
755 (sc->device_id == MRSAS_INTRUDER_24) ||
756 (sc->device_id == MRSAS_CUTLASS_52) ||
757 (sc->device_id == MRSAS_CUTLASS_53))
760 /* Get row and span from io_info for Uneven Span IO. */
761 row = io_info->start_row;
762 span = io_info->start_span;
765 if (raid->level == 6) {
766 logArm = get_arm_from_strip(sc, ld, stripRow, map);
767 rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
768 armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
769 arm = armQ + 1 + logArm;
770 if (arm >= SPAN_ROW_SIZE(map, ld, span))
771 arm -= SPAN_ROW_SIZE(map, ld, span);
772 physArm = (u_int8_t)arm;
774 /* Calculate the arm */
775 physArm = get_arm(sc, ld, span, stripRow, map);
778 arRef = MR_LdSpanArrayGet(ld, span, map);
779 pd = MR_ArPdGet(arRef, physArm, map);
781 if (pd != MR_PD_INVALID)
782 *pDevHandle = MR_PdDevHandleGet(pd, map);
784 *pDevHandle = MR_PD_INVALID;
785 if ((raid->level >= 5) && ((!do_invader) || (do_invader &&
786 raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
787 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
788 else if (raid->level == 1) {
789 pd = MR_ArPdGet(arRef, physArm + 1, map);
790 if (pd != MR_PD_INVALID)
791 *pDevHandle = MR_PdDevHandleGet(pd, map);
795 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
796 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
797 io_info->span_arm = pRAID_Context->spanArm;
802 * MR_BuildRaidContext: Set up Fast path RAID context
804 * This function will initiate command processing. The start/end row and strip
805 * information is calculated then the lock is acquired. This function will
806 * return 0 if region lock was acquired OR return num strips.
809 MR_BuildRaidContext(struct mrsas_softc *sc, struct IO_REQUEST_INFO *io_info,
810 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
813 u_int32_t ld, stripSize, stripe_mask;
814 u_int64_t endLba, endStrip, endRow, start_row, start_strip;
817 u_int8_t num_strips, numRows;
818 u_int16_t ref_in_start_stripe, ref_in_end_stripe;
819 u_int64_t ldStartBlock;
820 u_int32_t numBlocks, ldTgtId;
821 u_int8_t isRead, stripIdx;
823 u_int8_t startlba_span = SPAN_INVALID;
824 u_int64_t *pdBlock = &io_info->pdBlock;
827 ldStartBlock = io_info->ldStartBlock;
828 numBlocks = io_info->numBlocks;
829 ldTgtId = io_info->ldTgtId;
830 isRead = io_info->isRead;
832 io_info->IoforUnevenSpan = 0;
833 io_info->start_span = SPAN_INVALID;
835 ld = MR_TargetIdToLdGet(ldTgtId, map);
836 raid = MR_LdRaidGet(ld, map);
838 if (raid->rowDataSize == 0) {
839 if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
841 else if (sc->UnevenSpanSupport) {
842 io_info->IoforUnevenSpan = 1;
844 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug: raid->rowDataSize is 0, but has SPAN[0] rowDataSize = 0x%0x,"
845 " but there is _NO_ UnevenSpanSupport\n",
846 MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
850 stripSize = 1 << raid->stripeShift;
851 stripe_mask = stripSize - 1;
853 * calculate starting row and stripe, and number of strips and rows
855 start_strip = ldStartBlock >> raid->stripeShift;
856 ref_in_start_stripe = (u_int16_t)(ldStartBlock & stripe_mask);
857 endLba = ldStartBlock + numBlocks - 1;
858 ref_in_end_stripe = (u_int16_t)(endLba & stripe_mask);
859 endStrip = endLba >> raid->stripeShift;
860 num_strips = (u_int8_t)(endStrip - start_strip + 1); /* End strip */
861 if (io_info->IoforUnevenSpan) {
862 start_row = get_row_from_strip(sc, ld, start_strip, map);
863 endRow = get_row_from_strip(sc, ld, endStrip, map);
864 if (raid->spanDepth == 1) {
866 *pdBlock = start_row << raid->stripeShift;
868 startlba_span = (u_int8_t)mr_spanset_get_span_block(sc, ld, start_row,
869 pdBlock, map, &error_code);
870 if (error_code == 1) {
871 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug: return from %s %d. Send IO w/o region lock.\n",
876 if (startlba_span == SPAN_INVALID) {
877 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug: return from %s %d for row 0x%llx,"
878 "start strip %llx endSrip %llx\n", __func__,
879 __LINE__, (unsigned long long)start_row,
880 (unsigned long long)start_strip,
881 (unsigned long long)endStrip);
884 io_info->start_span = startlba_span;
885 io_info->start_row = start_row;
886 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug: Check Span number from %s %d for row 0x%llx, "
887 " start strip 0x%llx endSrip 0x%llx span 0x%x\n",
888 __func__, __LINE__, (unsigned long long)start_row,
889 (unsigned long long)start_strip,
890 (unsigned long long)endStrip, startlba_span);
891 mrsas_dprint(sc, MRSAS_PRL11, "AVAGO Debug : 1. start_row 0x%llx endRow 0x%llx Start span 0x%x\n",
892 (unsigned long long)start_row, (unsigned long long)endRow, startlba_span);
894 start_row = mega_div64_32(start_strip, raid->rowDataSize);
895 endRow = mega_div64_32(endStrip, raid->rowDataSize);
898 numRows = (u_int8_t)(endRow - start_row + 1); /* get the row count */
901 * Calculate region info. (Assume region at start of first row, and
902 * assume this IO needs the full row - will adjust if not true.)
904 regStart = start_row << raid->stripeShift;
907 /* Check if we can send this I/O via FastPath */
908 if (raid->capability.fpCapable) {
910 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
911 ((num_strips == 1) ||
912 raid->capability.fpReadAcrossStripe));
914 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
915 ((num_strips == 1) ||
916 raid->capability.fpWriteAcrossStripe));
918 io_info->fpOkForIo = FALSE;
921 if (num_strips == 1) {
922 regStart += ref_in_start_stripe;
925 } else if (io_info->IoforUnevenSpan == 0) {
927 * For Even span region lock optimization. If the start strip
928 * is the last in the start row
930 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
931 regStart += ref_in_start_stripe;
933 * initialize count to sectors from startRef to end
936 regSize = stripSize - ref_in_start_stripe;
938 /* add complete rows in the middle of the transfer */
940 regSize += (numRows - 2) << raid->stripeShift;
942 /* if IO ends within first strip of last row */
943 if (endStrip == endRow * raid->rowDataSize)
944 regSize += ref_in_end_stripe + 1;
946 regSize += stripSize;
948 if (start_strip == (get_strip_from_row(sc, ld, start_row, map) +
949 SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
950 regStart += ref_in_start_stripe;
952 * initialize count to sectors from startRef to end
955 regSize = stripSize - ref_in_start_stripe;
957 /* add complete rows in the middle of the transfer */
959 regSize += (numRows - 2) << raid->stripeShift;
961 /* if IO ends within first strip of last row */
962 if (endStrip == get_strip_from_row(sc, ld, endRow, map))
963 regSize += ref_in_end_stripe + 1;
965 regSize += stripSize;
967 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
968 if ((sc->device_id == MRSAS_INVADER) ||
969 (sc->device_id == MRSAS_FURY) ||
970 (sc->device_id == MRSAS_INTRUDER) ||
971 (sc->device_id == MRSAS_INTRUDER_24) ||
972 (sc->device_id == MRSAS_CUTLASS_52) ||
973 (sc->device_id == MRSAS_CUTLASS_53))
974 pRAID_Context->regLockFlags = (isRead) ? raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
976 pRAID_Context->regLockFlags = (isRead) ? REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
977 pRAID_Context->VirtualDiskTgtId = raid->targetId;
978 pRAID_Context->regLockRowLBA = regStart;
979 pRAID_Context->regLockLength = regSize;
980 pRAID_Context->configSeqNum = raid->seqNum;
983 * Get Phy Params only if FP capable, or else leave it to MR firmware
984 * to do the calculation.
986 if (io_info->fpOkForIo) {
987 retval = io_info->IoforUnevenSpan ?
988 mr_spanset_get_phy_params(sc, ld, start_strip,
989 ref_in_start_stripe, io_info, pRAID_Context, map) :
990 MR_GetPhyParams(sc, ld, start_strip,
991 ref_in_start_stripe, io_info, pRAID_Context, map);
992 /* If IO on an invalid Pd, then FP is not possible */
993 if (io_info->devHandle == MR_PD_INVALID)
994 io_info->fpOkForIo = FALSE;
997 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
998 retval = io_info->IoforUnevenSpan ?
999 mr_spanset_get_phy_params(sc, ld, start_strip + stripIdx,
1000 ref_in_start_stripe, io_info, pRAID_Context, map) :
1001 MR_GetPhyParams(sc, ld, start_strip + stripIdx,
1002 ref_in_start_stripe, io_info, pRAID_Context, map);
1008 /* Just for testing what arm we get for strip. */
1009 get_arm_from_strip(sc, ld, start_strip, map);
1016 * This routine pepare spanset info from Valid Raid map and store it into local
1017 * copy of ldSpanInfo per instance data structure.
1020 * ldSpanInfo per HBA instance
1024 mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
1026 u_int8_t span, count;
1027 u_int32_t element, span_row_width;
1030 LD_SPAN_SET *span_set, *span_set_prev;
1031 MR_QUAD_ELEMENT *quad;
1035 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
1036 ld = MR_TargetIdToLdGet(ldCount, map);
1037 if (ld >= MAX_LOGICAL_DRIVES)
1039 raid = MR_LdRaidGet(ld, map);
1040 for (element = 0; element < MAX_QUAD_DEPTH; element++) {
1041 for (span = 0; span < raid->spanDepth; span++) {
1042 if (map->raidMap.ldSpanMap[ld].spanBlock[span].
1043 block_span_info.noElements < element + 1)
1046 span_set = &(ldSpanInfo[ld].span_set[element]);
1047 quad = &map->raidMap.ldSpanMap[ld].
1048 spanBlock[span].block_span_info.quad[element];
1050 span_set->diff = quad->diff;
1052 for (count = 0, span_row_width = 0;
1053 count < raid->spanDepth; count++) {
1054 if (map->raidMap.ldSpanMap[ld].spanBlock[count].
1055 block_span_info.noElements >= element + 1) {
1056 span_set->strip_offset[count] = span_row_width;
1058 MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize;
1060 printf("AVAGO Debug span %x rowDataSize %x\n", count,
1061 MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize);
1066 span_set->span_row_data_width = span_row_width;
1067 span_row = mega_div64_32(((quad->logEnd -
1068 quad->logStart) + quad->diff), quad->diff);
1071 span_set->log_start_lba = 0;
1072 span_set->log_end_lba =
1073 ((span_row << raid->stripeShift) * span_row_width) - 1;
1075 span_set->span_row_start = 0;
1076 span_set->span_row_end = span_row - 1;
1078 span_set->data_strip_start = 0;
1079 span_set->data_strip_end = (span_row * span_row_width) - 1;
1081 span_set->data_row_start = 0;
1082 span_set->data_row_end = (span_row * quad->diff) - 1;
1084 span_set_prev = &(ldSpanInfo[ld].span_set[element - 1]);
1085 span_set->log_start_lba = span_set_prev->log_end_lba + 1;
1086 span_set->log_end_lba = span_set->log_start_lba +
1087 ((span_row << raid->stripeShift) * span_row_width) - 1;
1089 span_set->span_row_start = span_set_prev->span_row_end + 1;
1090 span_set->span_row_end =
1091 span_set->span_row_start + span_row - 1;
1093 span_set->data_strip_start =
1094 span_set_prev->data_strip_end + 1;
1095 span_set->data_strip_end = span_set->data_strip_start +
1096 (span_row * span_row_width) - 1;
1098 span_set->data_row_start = span_set_prev->data_row_end + 1;
1099 span_set->data_row_end = span_set->data_row_start +
1100 (span_row * quad->diff) - 1;
1104 if (span == raid->spanDepth)
1105 break; /* no quads remain */
1109 getSpanInfo(map, ldSpanInfo); /* to get span set info */
1114 * mrsas_update_load_balance_params: Update load balance parmas
1116 * sc - driver softc instance
1117 * drv_map - driver RAID map
1118 * lbInfo - Load balance info
1120 * This function updates the load balance parameters for the LD config of a two
1121 * drive optimal RAID-1.
1124 mrsas_update_load_balance_params(struct mrsas_softc *sc,
1125 MR_DRV_RAID_MAP_ALL * drv_map, PLD_LOAD_BALANCE_INFO lbInfo)
1131 if (sc->lb_pending_cmds > 128 || sc->lb_pending_cmds < 1)
1132 sc->lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
1134 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
1135 ld = MR_TargetIdToLdGet(ldCount, drv_map);
1136 if (ld >= MAX_LOGICAL_DRIVES_EXT) {
1137 lbInfo[ldCount].loadBalanceFlag = 0;
1140 raid = MR_LdRaidGet(ld, drv_map);
1141 if ((raid->level != 1) ||
1142 (raid->ldState != MR_LD_STATE_OPTIMAL)) {
1143 lbInfo[ldCount].loadBalanceFlag = 0;
1146 lbInfo[ldCount].loadBalanceFlag = 1;
1152 * mrsas_set_pd_lba: Sets PD LBA
1153 * input: io_request pointer
1157 * Local RAID map pointer
1158 * Start block of IO Block Size
1160 * Used to set the PD logical block address in CDB for FP IOs.
1163 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request, u_int8_t cdb_len,
1164 struct IO_REQUEST_INFO *io_info, union ccb *ccb,
1165 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
1166 u_int32_t ld_block_size)
1170 u_int64_t start_blk = io_info->pdBlock;
1171 u_int8_t *cdb = io_request->CDB.CDB32;
1172 u_int32_t num_blocks = io_info->numBlocks;
1173 u_int8_t opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1174 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
1176 /* Check if T10 PI (DIF) is enabled for this LD */
1177 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1178 raid = MR_LdRaidGet(ld, local_map_ptr);
1179 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1180 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1181 cdb[0] = MRSAS_SCSI_VARIABLE_LENGTH_CMD;
1182 cdb[7] = MRSAS_SCSI_ADDL_CDB_LEN;
1184 if (ccb_h->flags == CAM_DIR_OUT)
1185 cdb[9] = MRSAS_SCSI_SERVICE_ACTION_READ32;
1187 cdb[9] = MRSAS_SCSI_SERVICE_ACTION_WRITE32;
1188 cdb[10] = MRSAS_RD_WR_PROTECT_CHECK_ALL;
1191 cdb[12] = (u_int8_t)((start_blk >> 56) & 0xff);
1192 cdb[13] = (u_int8_t)((start_blk >> 48) & 0xff);
1193 cdb[14] = (u_int8_t)((start_blk >> 40) & 0xff);
1194 cdb[15] = (u_int8_t)((start_blk >> 32) & 0xff);
1195 cdb[16] = (u_int8_t)((start_blk >> 24) & 0xff);
1196 cdb[17] = (u_int8_t)((start_blk >> 16) & 0xff);
1197 cdb[18] = (u_int8_t)((start_blk >> 8) & 0xff);
1198 cdb[19] = (u_int8_t)(start_blk & 0xff);
1200 /* Logical block reference tag */
1201 io_request->CDB.EEDP32.PrimaryReferenceTag = swap32(ref_tag);
1202 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1203 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1205 /* Transfer length */
1206 cdb[28] = (u_int8_t)((num_blocks >> 24) & 0xff);
1207 cdb[29] = (u_int8_t)((num_blocks >> 16) & 0xff);
1208 cdb[30] = (u_int8_t)((num_blocks >> 8) & 0xff);
1209 cdb[31] = (u_int8_t)(num_blocks & 0xff);
1211 /* set SCSI IO EEDP Flags */
1212 if (ccb_h->flags == CAM_DIR_OUT) {
1213 io_request->EEDPFlags =
1214 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1215 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1216 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1217 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1218 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1220 io_request->EEDPFlags =
1221 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1222 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1224 io_request->Control |= (0x4 << 26);
1225 io_request->EEDPBlockSize = ld_block_size;
1227 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1228 if (((cdb_len == 12) || (cdb_len == 16)) &&
1229 (start_blk <= 0xffffffff)) {
1230 if (cdb_len == 16) {
1231 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1236 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1242 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1249 /* Transfer length */
1250 cdb[8] = (u_int8_t)(num_blocks & 0xff);
1251 cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1253 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1255 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1256 /* Convert to 16 byte CDB for large LBA's */
1259 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1263 opcode = cdb[0] == READ_10 ? READ_16 : WRITE_16;
1269 opcode = cdb[0] == READ_12 ? READ_16 : WRITE_16;
1276 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1283 /* Transfer length */
1284 cdb[13] = (u_int8_t)(num_blocks & 0xff);
1285 cdb[12] = (u_int8_t)((num_blocks >> 8) & 0xff);
1286 cdb[11] = (u_int8_t)((num_blocks >> 16) & 0xff);
1287 cdb[10] = (u_int8_t)((num_blocks >> 24) & 0xff);
1289 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1291 } else if ((cdb_len == 6) && (start_blk > 0x1fffff)) {
1292 /* convert to 10 byte CDB */
1293 opcode = cdb[0] == READ_6 ? READ_10 : WRITE_10;
1296 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1300 /* Set transfer length */
1301 cdb[8] = (u_int8_t)(num_blocks & 0xff);
1302 cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1304 /* Specify 10-byte cdb */
1307 /* Fall through normal case, just load LBA here */
1308 u_int8_t val = cdb[1] & 0xE0;
1312 cdb[3] = (u_int8_t)(start_blk & 0xff);
1313 cdb[2] = (u_int8_t)((start_blk >> 8) & 0xff);
1314 cdb[1] = val | ((u_int8_t)(start_blk >> 16) & 0x1f);
1317 cdb[5] = (u_int8_t)(start_blk & 0xff);
1318 cdb[4] = (u_int8_t)((start_blk >> 8) & 0xff);
1319 cdb[3] = (u_int8_t)((start_blk >> 16) & 0xff);
1320 cdb[2] = (u_int8_t)((start_blk >> 24) & 0xff);
1323 cdb[9] = (u_int8_t)(start_blk & 0xff);
1324 cdb[8] = (u_int8_t)((start_blk >> 8) & 0xff);
1325 cdb[7] = (u_int8_t)((start_blk >> 16) & 0xff);
1326 cdb[6] = (u_int8_t)((start_blk >> 24) & 0xff);
1327 cdb[5] = (u_int8_t)((start_blk >> 32) & 0xff);
1328 cdb[4] = (u_int8_t)((start_blk >> 40) & 0xff);
1329 cdb[3] = (u_int8_t)((start_blk >> 48) & 0xff);
1330 cdb[2] = (u_int8_t)((start_blk >> 56) & 0xff);
1337 * mrsas_get_best_arm_pd: Determine the best spindle arm
1340 * lbInfo - Load balance info
1341 * io_info - IO request info
1343 * This function determines and returns the best arm by looking at the
1344 * parameters of the last PD access.
1347 mrsas_get_best_arm_pd(struct mrsas_softc *sc,
1348 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info)
1351 MR_DRV_RAID_MAP_ALL *drv_map;
1352 u_int16_t pend0, pend1, ld;
1353 u_int64_t diff0, diff1;
1354 u_int8_t bestArm, pd0, pd1, span, arm;
1355 u_int32_t arRef, span_row_size;
1357 u_int64_t block = io_info->ldStartBlock;
1358 u_int32_t count = io_info->numBlocks;
1360 span = ((io_info->span_arm & RAID_CTX_SPANARM_SPAN_MASK)
1361 >> RAID_CTX_SPANARM_SPAN_SHIFT);
1362 arm = (io_info->span_arm & RAID_CTX_SPANARM_ARM_MASK);
1364 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
1365 ld = MR_TargetIdToLdGet(io_info->ldTgtId, drv_map);
1366 raid = MR_LdRaidGet(ld, drv_map);
1367 span_row_size = sc->UnevenSpanSupport ?
1368 SPAN_ROW_SIZE(drv_map, ld, span) : raid->rowSize;
1370 arRef = MR_LdSpanArrayGet(ld, span, drv_map);
1371 pd0 = MR_ArPdGet(arRef, arm, drv_map);
1372 pd1 = MR_ArPdGet(arRef, (arm + 1) >= span_row_size ?
1373 (arm + 1 - span_row_size) : arm + 1, drv_map);
1375 /* get the pending cmds for the data and mirror arms */
1376 pend0 = mrsas_atomic_read(&lbInfo->scsi_pending_cmds[pd0]);
1377 pend1 = mrsas_atomic_read(&lbInfo->scsi_pending_cmds[pd1]);
1379 /* Determine the disk whose head is nearer to the req. block */
1380 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[pd0]);
1381 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[pd1]);
1382 bestArm = (diff0 <= diff1 ? arm : arm ^ 1);
1384 if ((bestArm == arm && pend0 > pend1 + sc->lb_pending_cmds) ||
1385 (bestArm != arm && pend1 > pend0 + sc->lb_pending_cmds))
1388 /* Update the last accessed block on the correct pd */
1389 lbInfo->last_accessed_block[bestArm == arm ? pd0 : pd1] = block + count - 1;
1390 io_info->span_arm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | bestArm;
1391 io_info->pd_after_lb = (bestArm == arm) ? pd0 : pd1;
1394 printf("AVAGO Debug R1 Load balance occur - span 0x%x arm 0x%x bestArm 0x%x "
1395 "io_info->span_arm 0x%x\n",
1396 span, arm, bestArm, io_info->span_arm);
1399 return io_info->pd_after_lb;
1403 * mrsas_get_updated_dev_handle: Get the update dev handle
1405 * sc - Adapter instance soft state
1406 * lbInfo - Load balance info
1407 * io_info - io_info pointer
1409 * This function determines and returns the updated dev handle.
1412 mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
1413 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info)
1416 u_int16_t devHandle;
1417 MR_DRV_RAID_MAP_ALL *drv_map;
1419 drv_map = sc->ld_drv_map[(sc->map_id & 1)];
1421 /* get best new arm */
1422 arm_pd = mrsas_get_best_arm_pd(sc, lbInfo, io_info);
1423 devHandle = MR_PdDevHandleGet(arm_pd, drv_map);
1424 mrsas_atomic_inc(&lbInfo->scsi_pending_cmds[arm_pd]);
1430 * MR_GetPhyParams: Calculates arm, span, and block
1431 * Inputs: Adapter soft state
1432 * Logical drive number (LD)
1433 * Stripe number(stripRow)
1434 * Reference in stripe (stripRef)
1436 * Outputs: Absolute Block number in the physical disk
1438 * This routine calculates the arm, span and block for the specified stripe and
1439 * reference in stripe.
1442 MR_GetPhyParams(struct mrsas_softc *sc, u_int32_t ld,
1444 u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
1445 RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
1447 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1448 u_int32_t pd, arRef;
1449 u_int8_t physArm, span;
1451 u_int8_t retval = TRUE;
1453 u_int64_t *pdBlock = &io_info->pdBlock;
1454 u_int16_t *pDevHandle = &io_info->devHandle;
1455 u_int32_t rowMod, armQ, arm, logArm;
1456 u_int8_t do_invader = 0;
1458 if ((sc->device_id == MRSAS_INVADER) ||
1459 (sc->device_id == MRSAS_FURY) ||
1460 (sc->device_id == MRSAS_INTRUDER) ||
1461 (sc->device_id == MRSAS_INTRUDER_24) ||
1462 (sc->device_id == MRSAS_CUTLASS_52) ||
1463 (sc->device_id == MRSAS_CUTLASS_53))
1466 row = mega_div64_32(stripRow, raid->rowDataSize);
1468 if (raid->level == 6) {
1469 /* logical arm within row */
1470 logArm = mega_mod64(stripRow, raid->rowDataSize);
1471 if (raid->rowSize == 0)
1473 rowMod = mega_mod64(row, raid->rowSize); /* get logical row mod */
1474 armQ = raid->rowSize - 1 - rowMod; /* index of Q drive */
1475 arm = armQ + 1 + logArm;/* data always logically follows Q */
1476 if (arm >= raid->rowSize) /* handle wrap condition */
1477 arm -= raid->rowSize;
1478 physArm = (u_int8_t)arm;
1480 if (raid->modFactor == 0)
1482 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow, raid->modFactor), map);
1485 if (raid->spanDepth == 1) {
1487 *pdBlock = row << raid->stripeShift;
1489 span = (u_int8_t)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
1490 if (error_code == 1)
1494 /* Get the array on which this span is present */
1495 arRef = MR_LdSpanArrayGet(ld, span, map);
1497 pd = MR_ArPdGet(arRef, physArm, map); /* Get the Pd. */
1499 if (pd != MR_PD_INVALID)
1500 /* Get dev handle from Pd */
1501 *pDevHandle = MR_PdDevHandleGet(pd, map);
1503 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
1504 if ((raid->level >= 5) && ((!do_invader) || (do_invader &&
1505 raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
1506 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
1507 else if (raid->level == 1) {
1508 /* Get Alternate Pd. */
1509 pd = MR_ArPdGet(arRef, physArm + 1, map);
1510 if (pd != MR_PD_INVALID)
1511 /* Get dev handle from Pd. */
1512 *pDevHandle = MR_PdDevHandleGet(pd, map);
1516 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
1517 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | physArm;
1518 io_info->span_arm = pRAID_Context->spanArm;
1523 * MR_GetSpanBlock: Calculates span block
1529 * Outputs: Span number Error code
1531 * This routine calculates the span from the span block info.
1534 MR_GetSpanBlock(u_int32_t ld, u_int64_t row, u_int64_t *span_blk,
1535 MR_DRV_RAID_MAP_ALL * map, int *div_error)
1537 MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
1538 MR_QUAD_ELEMENT *quad;
1539 MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1541 u_int64_t blk, debugBlk;
1543 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
1544 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
1545 quad = &pSpanBlock->block_span_info.quad[j];
1546 if (quad->diff == 0) {
1550 if (quad->logStart <= row && row <= quad->logEnd &&
1551 (mega_mod64(row - quad->logStart, quad->diff)) == 0) {
1552 if (span_blk != NULL) {
1553 blk = mega_div64_32((row - quad->logStart), quad->diff);
1555 blk = (blk + quad->offsetInSpan) << raid->stripeShift;