]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mrsas/mrsas_fp.c
Current driver does fast path read load balancing between arm and mirror disk
[FreeBSD/FreeBSD.git] / sys / dev / mrsas / mrsas_fp.c
1 /*
2  * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
3  * Support: freebsdraid@lsi.com
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
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
16  * permission.
17  *
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.
29  *
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.
33  *
34  * Send feedback to: <megaraidfbsd@lsi.com> Mail to: LSI Corporation, 1621
35  * Barber Lane, Milpitas, CA 95035 ATTN: MegaRaid FreeBSD
36  *
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <dev/mrsas/mrsas.h>
43
44 #include <cam/cam.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>
51
52
53 /*
54  * Function prototypes
55  */
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);
59 u_int8_t 
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);
63 u_int8_t 
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);
74 u_int32_t 
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);
80 void 
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);
85 static u_int16_t 
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);
89 static u_int16_t 
90 MR_ArPdGet(u_int32_t ar, u_int32_t arm,
91     MR_DRV_RAID_MAP_ALL * map);
92 static MR_LD_SPAN *
93 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span,
94     MR_DRV_RAID_MAP_ALL * map);
95 static u_int8_t 
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);
103
104
105 /*
106  * Spanset related function prototypes Added for PRL11 configuration (Uneven
107  * span support)
108  */
109 void    mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo);
110 static u_int8_t
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);
114 static u_int64_t
115 get_row_from_strip(struct mrsas_softc *sc, u_int32_t ld,
116     u_int64_t strip, MR_DRV_RAID_MAP_ALL * map);
117 static u_int32_t
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);
121 static u_int8_t
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);
124
125
126 /*
127  * Spanset related defines Added for PRL11 configuration(Uneven span support)
128  */
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
133 #define SPAN_DEBUG              0
134
135 /*
136  * Related Defines
137  */
138
139 typedef u_int64_t REGION_KEY;
140 typedef u_int32_t REGION_LEN;
141
142 #define MR_LD_STATE_OPTIMAL             3
143 #define FALSE                                   0
144 #define TRUE                                    1
145
146 #define LB_PENDING_CMDS_DEFAULT 4
147
148
149 /*
150  * Related Macros
151  */
152
153 #define ABS_DIFF(a,b)   ( ((a) > (b)) ? ((a) - (b)) : ((b) - (a)) )
154
155 #define swap32(x) \
156   ((unsigned int)( \
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) ))
161
162
163 /*
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.
166  *
167  * @param dividend:     Dividend
168  * @param divisor:      Divisor
169  * @return                      remainder
170  */
171
172 #define mega_mod64(dividend, divisor) ({ \
173 int remainder; \
174 remainder = ((u_int64_t) (dividend)) % (u_int32_t) (divisor); \
175 remainder;})
176
177 #define mega_div64_32(dividend, divisor) ({ \
178 int quotient; \
179 quotient = ((u_int64_t) (dividend)) / (u_int32_t) (divisor); \
180 quotient;})
181
182
183 /*
184  * Various RAID map access functions.  These functions access the various
185  * parts of the RAID map and returns the appropriate parameters.
186  */
187
188 MR_LD_RAID *
189 MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
190 {
191         return (&map->raidMap.ldSpanMap[ld].ldRaid);
192 }
193
194 u_int16_t
195 MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
196 {
197         return (map->raidMap.ldSpanMap[ld].ldRaid.targetId);
198 }
199
200 static u_int16_t
201 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
202 {
203         return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
204 }
205
206 static u_int8_t
207 MR_LdDataArmGet(u_int32_t ld, u_int32_t armIdx, MR_DRV_RAID_MAP_ALL * map)
208 {
209         return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
210 }
211
212 static u_int16_t
213 MR_PdDevHandleGet(u_int32_t pd, MR_DRV_RAID_MAP_ALL * map)
214 {
215         return map->raidMap.devHndlInfo[pd].curDevHdl;
216 }
217
218 static u_int16_t
219 MR_ArPdGet(u_int32_t ar, u_int32_t arm, MR_DRV_RAID_MAP_ALL * map)
220 {
221         return map->raidMap.arMapInfo[ar].pd[arm];
222 }
223
224 static MR_LD_SPAN *
225 MR_LdSpanPtrGet(u_int32_t ld, u_int32_t span, MR_DRV_RAID_MAP_ALL * map)
226 {
227         return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
228 }
229
230 static MR_SPAN_BLOCK_INFO *
231 MR_LdSpanInfoGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map)
232 {
233         return &map->raidMap.ldSpanMap[ld].spanBlock[0];
234 }
235
236 u_int16_t
237 MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
238 {
239         return map->raidMap.ldTgtIdToLd[ldTgtId];
240 }
241
242 u_int32_t
243 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map)
244 {
245         MR_LD_RAID *raid;
246         u_int32_t ld, ldBlockSize = MRSAS_SCSIBLOCKSIZE;
247
248         ld = MR_TargetIdToLdGet(ldTgtId, map);
249
250         /*
251          * Check if logical drive was removed.
252          */
253         if (ld >= MAX_LOGICAL_DRIVES)
254                 return ldBlockSize;
255
256         raid = MR_LdRaidGet(ld, map);
257         ldBlockSize = raid->logicalBlockLength;
258         if (!ldBlockSize)
259                 ldBlockSize = MRSAS_SCSIBLOCKSIZE;
260
261         return ldBlockSize;
262 }
263
264 /*
265  * This function will Populate Driver Map using firmware raid map
266  */
267 void
268 MR_PopulateDrvRaidMap(struct mrsas_softc *sc)
269 {
270         MR_FW_RAID_MAP_ALL *fw_map_old = NULL;
271         MR_FW_RAID_MAP *pFwRaidMap = NULL;
272         unsigned int i;
273
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;
276
277         if (sc->max256vdSupport) {
278                 memcpy(sc->ld_drv_map[sc->map_id & 1],
279                     sc->raidmap_mem[sc->map_id & 1],
280                     sc->current_map_sz);
281                 /*
282                  * New Raid map will not set totalSize, so keep expected
283                  * value for legacy code in ValidateMapInfo
284                  */
285                 pDrvRaidMap->totalSize = sizeof(MR_FW_RAID_MAP_EXT);
286         } else {
287                 fw_map_old = (MR_FW_RAID_MAP_ALL *) sc->raidmap_mem[(sc->map_id & 1)];
288                 pFwRaidMap = &fw_map_old->raidMap;
289
290 #if VD_EXT_DEBUG
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);
297                 }
298 #endif
299
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;
305
306                 for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++) {
307                         pDrvRaidMap->ldTgtIdToLd[i] =
308                             (u_int8_t)pFwRaidMap->ldTgtIdToLd[i];
309                 }
310
311                 for (i = 0; i < pDrvRaidMap->ldCount; i++) {
312                         pDrvRaidMap->ldSpanMap[i] =
313                             pFwRaidMap->ldSpanMap[i];
314
315 #if VD_EXT_DEBUG
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);
329 #endif
330                 }
331
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);
337         }
338 }
339
340 /*
341  * MR_ValidateMapInfo:  Validate RAID map
342  * input:                               Adapter instance soft state
343  *
344  * This function checks and validates the loaded RAID map. It returns 0 if
345  * successful, and 1 otherwise.
346  */
347 u_int8_t
348 MR_ValidateMapInfo(struct mrsas_softc *sc)
349 {
350         if (!sc) {
351                 return 1;
352         }
353         MR_PopulateDrvRaidMap(sc);
354
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;
357
358         u_int32_t expected_map_size;
359
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;
363
364         if (sc->max256vdSupport)
365                 expected_map_size = sizeof(MR_FW_RAID_MAP_EXT);
366         else
367                 expected_map_size =
368                     (sizeof(MR_FW_RAID_MAP) - sizeof(MR_LD_SPAN_MAP)) +
369                     (sizeof(MR_LD_SPAN_MAP) * pDrvRaidMap->ldCount);
370
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);
375                 return 1;
376         }
377         if (sc->UnevenSpanSupport) {
378                 printf("Updating span set\n\n");
379                 mr_update_span_set(drv_map, ldSpanInfo);
380         }
381         mrsas_update_load_balance_params(sc, drv_map, sc->load_balance_info);
382
383         return 0;
384 }
385
386 /*
387  *
388  * Function to print info about span set created in driver from FW raid map
389  *
390  * Inputs:              map
391  * ldSpanInfo:  ld map span info per HBA instance
392  *
393  *
394  */
395 #if SPAN_DEBUG
396 static int
397 getSpanInfo(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
398 {
399
400         u_int8_t span;
401         u_int32_t element;
402         MR_LD_RAID *raid;
403         LD_SPAN_SET *span_set;
404         MR_QUAD_ELEMENT *quad;
405         int ldCount;
406         u_int16_t ld;
407
408         for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
409                 ld = MR_TargetIdToLdGet(ldCount, map);
410                 if (ld >= MAX_LOGICAL_DRIVES) {
411                         continue;
412                 }
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)
422                                 break;
423
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);
439
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.
445                                             quad[element];
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);
453                                 }
454                         }
455                 }
456         }
457         return 0;
458 }
459
460 #endif
461 /*
462  *
463  * This routine calculates the Span block for given row using spanset.
464  *
465  * Inputs :     HBA instance
466  * ld:          Logical drive number
467  * row:         Row number
468  * map:         LD map
469  *
470  * Outputs :    span    - Span number block
471  *                                              - Absolute Block number in the physical disk
472  *                              div_error    - Devide error code.
473  */
474
475 u_int32_t
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)
478 {
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;
484
485         for (info = 0; info < MAX_QUAD_DEPTH; info++) {
486                 span_set = &(ldSpanInfo[ld].span_set[info]);
487
488                 if (span_set->span_row_data_width == 0)
489                         break;
490                 if (row > span_set->data_row_end)
491                         continue;
492
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].
497                                     spanBlock[span].
498                                     block_span_info.quad[info];
499                                 if (quad->diff == 0) {
500                                         *div_error = 1;
501                                         return span;
502                                 }
503                                 if (quad->logStart <= row &&
504                                     row <= quad->logEnd &&
505                                     (mega_mod64(row - quad->logStart,
506                                     quad->diff)) == 0) {
507                                         if (span_blk != NULL) {
508                                                 u_int64_t blk;
509
510                                                 blk = mega_div64_32
511                                                     ((row - quad->logStart),
512                                                     quad->diff);
513                                                 blk = (blk + quad->offsetInSpan)
514                                                     << raid->stripeShift;
515                                                 *span_blk = blk;
516                                         }
517                                         return span;
518                                 }
519                         }
520         }
521         return SPAN_INVALID;
522 }
523
524 /*
525  *
526  * This routine calculates the row for given strip using spanset.
527  *
528  * Inputs :     HBA instance
529  * ld:          Logical drive number
530  * Strip:       Strip
531  * map:         LD map
532  *
533  * Outputs :    row - row associated with strip
534  */
535
536 static u_int64_t
537 get_row_from_strip(struct mrsas_softc *sc,
538     u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
539 {
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;
545
546         for (info = 0; info < MAX_QUAD_DEPTH; info++) {
547                 span_set = &(ldSpanInfo[ld].span_set[info]);
548
549                 if (span_set->span_row_data_width == 0)
550                         break;
551                 if (strip > span_set->data_strip_end)
552                         continue;
553
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) {
562                                 if (strip_offset >=
563                                     span_set->strip_offset[span])
564                                         span_offset++;
565                                 else
566                                         break;
567                         }
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));
577         }
578         return -1LLU;
579 }
580
581
582 /*
583  *
584  * This routine calculates the Start Strip for given row using spanset.
585  *
586  * Inputs:      HBA instance
587  * ld:          Logical drive number
588  * row:         Row number
589  * map:         LD map
590  *
591  * Outputs :    Strip - Start strip associated with row
592  */
593
594 static u_int64_t
595 get_strip_from_row(struct mrsas_softc *sc,
596     u_int32_t ld, u_int64_t row, MR_DRV_RAID_MAP_ALL * map)
597 {
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;
603         u_int64_t strip;
604
605         for (info = 0; info < MAX_QUAD_DEPTH; info++) {
606                 span_set = &(ldSpanInfo[ld].span_set[info]);
607
608                 if (span_set->span_row_data_width == 0)
609                         break;
610                 if (row > span_set->data_row_end)
611                         continue;
612
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),
621                                     quad->diff) == 0) {
622                                         strip = mega_div64_32
623                                             (((row - span_set->data_row_start)
624                                             - quad->logStart),
625                                             quad->diff);
626                                         strip *= span_set->span_row_data_width;
627                                         strip += span_set->data_strip_start;
628                                         strip += span_set->strip_offset[span];
629                                         return strip;
630                                 }
631                         }
632         }
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);
635         return -1;
636 }
637
638 /*
639  * *****************************************************************************
640  *
641  *
642  * This routine calculates the Physical Arm for given strip using spanset.
643  *
644  * Inputs :     HBA instance
645  *                      Logical drive number
646  *                      Strip
647  *                      LD map
648  *
649  * Outputs :    Phys Arm - Phys Arm associated with strip
650  */
651
652 static u_int32_t
653 get_arm_from_strip(struct mrsas_softc *sc,
654     u_int32_t ld, u_int64_t strip, MR_DRV_RAID_MAP_ALL * map)
655 {
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;
660
661         for (info = 0; info < MAX_QUAD_DEPTH; info++) {
662                 span_set = &(ldSpanInfo[ld].span_set[info]);
663
664                 if (span_set->span_row_data_width == 0)
665                         break;
666                 if (strip > span_set->data_strip_end)
667                         continue;
668
669                 strip_offset = (u_int32_t)mega_mod64
670                     ((strip - span_set->data_strip_start),
671                     span_set->span_row_data_width);
672
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];
678                                 else
679                                         break;
680                         }
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);
685         }
686
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);
689
690         return -1;
691 }
692
693
694 /* This Function will return Phys arm */
695 u_int8_t
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)
698 {
699         MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
700
701         /* Need to check correct default value */
702         u_int32_t arm = 0;
703
704         switch (raid->level) {
705         case 0:
706         case 5:
707         case 6:
708                 arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
709                 break;
710         case 1:
711                 /* start with logical arm */
712                 arm = get_arm_from_strip(sc, ld, stripe, map);
713                 arm *= 2;
714                 break;
715         }
716
717         return arm;
718 }
719
720 /*
721  *
722  * This routine calculates the arm, span and block for the specified stripe and
723  * reference in stripe using spanset
724  *
725  * Inputs :
726  * sc - HBA instance
727  * ld - Logical drive number
728  * stripRow: Stripe number
729  * stripRef: Reference in stripe
730  *
731  * Outputs :    span - Span number block - Absolute Block
732  * number in the physical disk
733  */
734 static u_int8_t
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)
738 {
739         MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
740         u_int32_t pd, arRef;
741         u_int8_t physArm, span;
742         u_int64_t row;
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;
748
749         if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
750                 do_invader = 1;
751
752         /* Get row and span from io_info for Uneven Span IO. */
753         row = io_info->start_row;
754         span = io_info->start_span;
755
756
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;
765         } else
766                 /* Calculate the arm */
767                 physArm = get_arm(sc, ld, span, stripRow, map);
768
769
770         arRef = MR_LdSpanArrayGet(ld, span, map);
771         pd = MR_ArPdGet(arRef, physArm, map);
772
773         if (pd != MR_PD_INVALID)
774                 *pDevHandle = MR_PdDevHandleGet(pd, map);
775         else {
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);
784                 }
785         }
786
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;
790         return retval;
791 }
792
793 /*
794  * MR_BuildRaidContext: Set up Fast path RAID context
795  *
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.
799  */
800 u_int8_t
801 MR_BuildRaidContext(struct mrsas_softc *sc, struct IO_REQUEST_INFO *io_info,
802     RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
803 {
804         MR_LD_RAID *raid;
805         u_int32_t ld, stripSize, stripe_mask;
806         u_int64_t endLba, endStrip, endRow, start_row, start_strip;
807         REGION_KEY regStart;
808         REGION_LEN regSize;
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;
814         u_int8_t retval = 0;
815         u_int8_t startlba_span = SPAN_INVALID;
816         u_int64_t *pdBlock = &io_info->pdBlock;
817         int error_code = 0;
818
819         ldStartBlock = io_info->ldStartBlock;
820         numBlocks = io_info->numBlocks;
821         ldTgtId = io_info->ldTgtId;
822         isRead = io_info->isRead;
823
824         io_info->IoforUnevenSpan = 0;
825         io_info->start_span = SPAN_INVALID;
826
827         ld = MR_TargetIdToLdGet(ldTgtId, map);
828         raid = MR_LdRaidGet(ld, map);
829
830         if (raid->rowDataSize == 0) {
831                 if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
832                         return FALSE;
833                 else if (sc->UnevenSpanSupport) {
834                         io_info->IoforUnevenSpan = 1;
835                 } else {
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);
839                         return FALSE;
840                 }
841         }
842         stripSize = 1 << raid->stripeShift;
843         stripe_mask = stripSize - 1;
844         /*
845          * calculate starting row and stripe, and number of strips and rows
846          */
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) {
857                         startlba_span = 0;
858                         *pdBlock = start_row << raid->stripeShift;
859                 } else {
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",
864                                     __func__, __LINE__);
865                                 return FALSE;
866                         }
867                 }
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);
874                         return FALSE;
875                 }
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);
885         } else {
886                 start_row = mega_div64_32(start_strip, raid->rowDataSize);
887                 endRow = mega_div64_32(endStrip, raid->rowDataSize);
888         }
889
890         numRows = (u_int8_t)(endRow - start_row + 1);   /* get the row count */
891
892         /*
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.)
895          */
896         regStart = start_row << raid->stripeShift;
897         regSize = stripSize;
898
899         /* Check if we can send this I/O via FastPath */
900         if (raid->capability.fpCapable) {
901                 if (isRead)
902                         io_info->fpOkForIo = (raid->capability.fpReadCapable &&
903                             ((num_strips == 1) ||
904                             raid->capability.fpReadAcrossStripe));
905                 else
906                         io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
907                             ((num_strips == 1) ||
908                             raid->capability.fpWriteAcrossStripe));
909         } else
910                 io_info->fpOkForIo = FALSE;
911
912         if (numRows == 1) {
913                 if (num_strips == 1) {
914                         regStart += ref_in_start_stripe;
915                         regSize = numBlocks;
916                 }
917         } else if (io_info->IoforUnevenSpan == 0) {
918                 /*
919                  * For Even span region lock optimization. If the start strip
920                  * is the last in the start row
921                  */
922                 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
923                         regStart += ref_in_start_stripe;
924                         /*
925                          * initialize count to sectors from startRef to end
926                          * of strip
927                          */
928                         regSize = stripSize - ref_in_start_stripe;
929                 }
930                 /* add complete rows in the middle of the transfer */
931                 if (numRows > 2)
932                         regSize += (numRows - 2) << raid->stripeShift;
933
934                 /* if IO ends within first strip of last row */
935                 if (endStrip == endRow * raid->rowDataSize)
936                         regSize += ref_in_end_stripe + 1;
937                 else
938                         regSize += stripSize;
939         } else {
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;
943                         /*
944                          * initialize count to sectors from startRef to end
945                          * of strip
946                          */
947                         regSize = stripSize - ref_in_start_stripe;
948                 }
949                 /* add complete rows in the middle of the transfer */
950                 if (numRows > 2)
951                         regSize += (numRows - 2) << raid->stripeShift;
952
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;
956                 else
957                         regSize += stripSize;
958         }
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;
962         else
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;
968
969         /*
970          * Get Phy Params only if FP capable, or else leave it to MR firmware
971          * to do the calculation.
972          */
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;
982                 return retval;
983         } else if (isRead) {
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);
990                         if (!retval)
991                                 return TRUE;
992                 }
993         }
994 #if SPAN_DEBUG
995         /* Just for testing what arm we get for strip. */
996         get_arm_from_strip(sc, ld, start_strip, map);
997 #endif
998         return TRUE;
999 }
1000
1001 /*
1002  *
1003  * This routine pepare spanset info from Valid Raid map and store it into local
1004  * copy of ldSpanInfo per instance data structure.
1005  *
1006  * Inputs :     LD map
1007  *                      ldSpanInfo per HBA instance
1008  *
1009  */
1010 void
1011 mr_update_span_set(MR_DRV_RAID_MAP_ALL * map, PLD_SPAN_INFO ldSpanInfo)
1012 {
1013         u_int8_t span, count;
1014         u_int32_t element, span_row_width;
1015         u_int64_t span_row;
1016         MR_LD_RAID *raid;
1017         LD_SPAN_SET *span_set, *span_set_prev;
1018         MR_QUAD_ELEMENT *quad;
1019         int ldCount;
1020         u_int16_t ld;
1021
1022         for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
1023                 ld = MR_TargetIdToLdGet(ldCount, map);
1024                 if (ld >= MAX_LOGICAL_DRIVES)
1025                         continue;
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)
1031                                         continue;
1032                                 /* TO-DO */
1033                                 span_set = &(ldSpanInfo[ld].span_set[element]);
1034                                 quad = &map->raidMap.ldSpanMap[ld].
1035                                     spanBlock[span].block_span_info.quad[element];
1036
1037                                 span_set->diff = quad->diff;
1038
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;
1044                                                 span_row_width +=
1045                                                     MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize;
1046 #if SPAN_DEBUG
1047                                                 printf("LSI Debug span %x rowDataSize %x\n", count,
1048                                                     MR_LdSpanPtrGet(ld, count, map)->spanRowDataSize);
1049 #endif
1050                                         }
1051                                 }
1052
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);
1056
1057                                 if (element == 0) {
1058                                         span_set->log_start_lba = 0;
1059                                         span_set->log_end_lba =
1060                                             ((span_row << raid->stripeShift) * span_row_width) - 1;
1061
1062                                         span_set->span_row_start = 0;
1063                                         span_set->span_row_end = span_row - 1;
1064
1065                                         span_set->data_strip_start = 0;
1066                                         span_set->data_strip_end = (span_row * span_row_width) - 1;
1067
1068                                         span_set->data_row_start = 0;
1069                                         span_set->data_row_end = (span_row * quad->diff) - 1;
1070                                 } else {
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;
1075
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;
1079
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;
1084
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;
1088                                 }
1089                                 break;
1090                         }
1091                         if (span == raid->spanDepth)
1092                                 break;  /* no quads remain */
1093                 }
1094         }
1095 #if SPAN_DEBUG
1096         getSpanInfo(map, ldSpanInfo);   /* to get span set info */
1097 #endif
1098 }
1099
1100 /*
1101  * mrsas_update_load_balance_params:    Update load balance parmas
1102  * Inputs:
1103  * sc - driver softc instance
1104  * drv_map - driver RAID map
1105  * lbInfo - Load balance info
1106  *
1107  * This function updates the load balance parameters for the LD config of a two
1108  * drive optimal RAID-1.
1109  */
1110 void 
1111 mrsas_update_load_balance_params(struct mrsas_softc *sc,
1112         MR_DRV_RAID_MAP_ALL * drv_map, PLD_LOAD_BALANCE_INFO lbInfo)
1113 {
1114         int ldCount;
1115         u_int16_t ld;
1116         MR_LD_RAID *raid;
1117
1118         if(sc->lb_pending_cmds > 128 || sc->lb_pending_cmds < 1)
1119                         sc-> lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
1120
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;
1125                         continue;
1126                 }
1127
1128                 raid = MR_LdRaidGet(ld, drv_map);
1129                 if ((raid->level != 1) ||
1130                         (raid->ldState != MR_LD_STATE_OPTIMAL)) {
1131                         lbInfo[ldCount].loadBalanceFlag = 0;
1132                         continue;
1133                 }
1134                 lbInfo[ldCount].loadBalanceFlag = 1;
1135         }
1136 }
1137
1138
1139 /*
1140  * mrsas_set_pd_lba:    Sets PD LBA
1141  * input:                               io_request pointer
1142  *                                              CDB length
1143  *                                              io_info pointer
1144  *                                              Pointer to CCB
1145  *                                              Local RAID map pointer
1146  *                                              Start block of IO Block Size
1147  *
1148  * Used to set the PD logical block address in CDB for FP IOs.
1149  */
1150 void 
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)
1155 {
1156         MR_LD_RAID *raid;
1157         u_int32_t ld;
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);
1163
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;
1171
1172                 if (ccb_h->flags == CAM_DIR_OUT)
1173                         cdb[9] = MRSAS_SCSI_SERVICE_ACTION_READ32;
1174                 else
1175                         cdb[9] = MRSAS_SCSI_SERVICE_ACTION_WRITE32;
1176                 cdb[10] = MRSAS_RD_WR_PROTECT_CHECK_ALL;
1177
1178                 /* LBA */
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);
1187
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 */
1192
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);
1198
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;
1207                 } else {
1208                         io_request->EEDPFlags =
1209                             MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1210                             MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1211                 }
1212                 io_request->Control |= (0x4 << 26);
1213                 io_request->EEDPBlockSize = ld_block_size;
1214         } else {
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;
1220                                 flagvals = cdb[1];
1221                                 groupnum = cdb[14];
1222                                 control = cdb[15];
1223                         } else {
1224                                 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1225                                 flagvals = cdb[1];
1226                                 groupnum = cdb[10];
1227                                 control = cdb[11];
1228                         }
1229
1230                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1231
1232                         cdb[0] = opcode;
1233                         cdb[1] = flagvals;
1234                         cdb[6] = groupnum;
1235                         cdb[9] = control;
1236
1237                         /* Transfer length */
1238                         cdb[8] = (u_int8_t)(num_blocks & 0xff);
1239                         cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1240
1241                         io_request->IoFlags = 10;       /* Specify 10-byte cdb */
1242                         cdb_len = 10;
1243                 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1244                         /* Convert to 16 byte CDB for large LBA's */
1245                         switch (cdb_len) {
1246                         case 6:
1247                                 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1248                                 control = cdb[5];
1249                                 break;
1250                         case 10:
1251                                 opcode = cdb[0] == READ_10 ? READ_16 : WRITE_16;
1252                                 flagvals = cdb[1];
1253                                 groupnum = cdb[6];
1254                                 control = cdb[9];
1255                                 break;
1256                         case 12:
1257                                 opcode = cdb[0] == READ_12 ? READ_16 : WRITE_16;
1258                                 flagvals = cdb[1];
1259                                 groupnum = cdb[10];
1260                                 control = cdb[11];
1261                                 break;
1262                         }
1263
1264                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1265
1266                         cdb[0] = opcode;
1267                         cdb[1] = flagvals;
1268                         cdb[14] = groupnum;
1269                         cdb[15] = control;
1270
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);
1276
1277                         io_request->IoFlags = 16;       /* Specify 16-byte cdb */
1278                         cdb_len = 16;
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;
1282                         control = cdb[5];
1283
1284                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1285                         cdb[0] = opcode;
1286                         cdb[9] = control;
1287
1288                         /* Set transfer length */
1289                         cdb[8] = (u_int8_t)(num_blocks & 0xff);
1290                         cdb[7] = (u_int8_t)((num_blocks >> 8) & 0xff);
1291
1292                         /* Specify 10-byte cdb */
1293                         cdb_len = 10;
1294                 }
1295                 /* Fall through normal case, just load LBA here */
1296                 u_int8_t val = cdb[1] & 0xE0;
1297                 switch (cdb_len) {
1298                 case 6:
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);
1302                         break;
1303                 case 10:
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);
1308                         break;
1309                 case 12:
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);
1314                         break;
1315                 case 16:
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);
1324                         break;
1325                 }
1326         }
1327 }
1328
1329 /*
1330  * mrsas_get_best_arm_pd:       Determine the best spindle arm
1331  * Inputs:
1332  *    sc - HBA instance
1333  *    lbInfo - Load balance info
1334  *    io_info - IO request info
1335  *
1336  * This function determines and returns the best arm by looking at the
1337  * parameters of the last PD access.
1338  */
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)
1341 {
1342         MR_LD_RAID  *raid;
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;
1348
1349         u_int64_t block = io_info->ldStartBlock;
1350         u_int32_t count = io_info->numBlocks;
1351
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);
1355
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;
1361
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);
1366
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]);
1370
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);
1375
1376         if ((bestArm == arm && pend0 > pend1 + sc->lb_pending_cmds) ||
1377                         (bestArm != arm && pend1 > pend0 + sc->lb_pending_cmds))
1378                 bestArm ^= 1;
1379
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;
1384 #if SPAN_DEBUG
1385         if(arm != bestArm)
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);
1389 #endif
1390
1391         return io_info->pd_after_lb;
1392 }
1393
1394 /*
1395  * mrsas_get_updated_dev_handle:        Get the update dev handle
1396  * Inputs:
1397  *      sc - Adapter instance soft state
1398  *      lbInfo - Load balance info
1399  *      io_info - io_info pointer
1400  *
1401  * This function determines and returns the updated dev handle.
1402  */
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)
1405 {
1406         u_int8_t arm_pd;
1407         u_int16_t devHandle;
1408         MR_DRV_RAID_MAP_ALL *drv_map;
1409
1410         drv_map = sc->ld_drv_map[(sc->map_id & 1)];
1411
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]);
1416
1417         return devHandle;
1418 }
1419
1420 /*
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)
1426  *
1427  * Outputs:                     Absolute Block number in the physical disk
1428  *
1429  * This routine calculates the arm, span and block for the specified stripe and
1430  * reference in stripe.
1431  */
1432 u_int8_t
1433 MR_GetPhyParams(struct mrsas_softc *sc, u_int32_t ld,
1434     u_int64_t stripRow,
1435     u_int16_t stripRef, struct IO_REQUEST_INFO *io_info,
1436     RAID_CONTEXT * pRAID_Context, MR_DRV_RAID_MAP_ALL * map)
1437 {
1438         MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1439         u_int32_t pd, arRef;
1440         u_int8_t physArm, span;
1441         u_int64_t row;
1442         u_int8_t retval = TRUE;
1443         int error_code = 0;
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;
1448
1449         if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1450                 do_invader = 1;
1451
1452         row = mega_div64_32(stripRow, raid->rowDataSize);
1453
1454         if (raid->level == 6) {
1455                 /* logical arm within row */
1456                 logArm = mega_mod64(stripRow, raid->rowDataSize);
1457                 if (raid->rowSize == 0)
1458                         return FALSE;
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;
1465         } else {
1466                 if (raid->modFactor == 0)
1467                         return FALSE;
1468                 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow, raid->modFactor), map);
1469         }
1470
1471         if (raid->spanDepth == 1) {
1472                 span = 0;
1473                 *pdBlock = row << raid->stripeShift;
1474         } else {
1475                 span = (u_int8_t)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
1476                 if (error_code == 1)
1477                         return FALSE;
1478         }
1479
1480         /* Get the array on which this span is present */
1481         arRef = MR_LdSpanArrayGet(ld, span, map);
1482
1483         pd = MR_ArPdGet(arRef, physArm, map);   /* Get the Pd. */
1484
1485         if (pd != MR_PD_INVALID)
1486                 /* Get dev handle from Pd */
1487                 *pDevHandle = MR_PdDevHandleGet(pd, map);
1488         else {
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);
1499                 }
1500         }
1501
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;
1505         return retval;
1506 }
1507
1508 /*
1509  * MR_GetSpanBlock:     Calculates span block
1510  * Inputs:                      LD
1511  *                                      row PD
1512  *                                      span block
1513  *                                      RAID map pointer
1514  *
1515  * Outputs:                     Span number Error code
1516  *
1517  * This routine calculates the span from the span block info.
1518  */
1519 u_int32_t
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)
1522 {
1523         MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
1524         MR_QUAD_ELEMENT *quad;
1525         MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
1526         u_int32_t span, j;
1527         u_int64_t blk, debugBlk;
1528
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) {
1533                                 *div_error = 1;
1534                                 return span;
1535                         }
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);
1540                                         debugBlk = blk;
1541                                         blk = (blk + quad->offsetInSpan) << raid->stripeShift;
1542                                         *span_blk = blk;
1543                                 }
1544                                 return span;
1545                         }
1546                 }
1547         }
1548         return span;
1549 }