1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/types.h>
35#include <linux/pci.h>
36#include <linux/list.h>
37#include <linux/moduleparam.h>
38#include <linux/module.h>
39#include <linux/spinlock.h>
40#include <linux/interrupt.h>
41#include <linux/delay.h>
42#include <linux/uio.h>
43#include <linux/uaccess.h>
44#include <linux/fs.h>
45#include <linux/compat.h>
46#include <linux/blkdev.h>
47#include <linux/poll.h>
48
49#include <scsi/scsi.h>
50#include <scsi/scsi_cmnd.h>
51#include <scsi/scsi_device.h>
52#include <scsi/scsi_host.h>
53
54#include "megaraid_sas_fusion.h"
55#include "megaraid_sas.h"
56#include <asm/div64.h>
57
58#define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
59#define MR_LD_STATE_OPTIMAL 3
60#define FALSE 0
61#define TRUE 1
62
63
64void
65mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
66 struct LD_LOAD_BALANCE_INFO *lbInfo);
67
68u32 mega_mod64(u64 dividend, u32 divisor)
69{
70 u64 d;
71 u32 remainder;
72
73 if (!divisor)
74 printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
75 d = dividend;
76 remainder = do_div(d, divisor);
77 return remainder;
78}
79
80
81
82
83
84
85
86u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
87{
88 u32 remainder;
89 u64 d;
90
91 if (!divisor)
92 printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");
93
94 d = dividend;
95 remainder = do_div(d, divisor);
96
97 return d;
98}
99
100struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
101{
102 return &map->raidMap.ldSpanMap[ld].ldRaid;
103}
104
105static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
106 struct MR_FW_RAID_MAP_ALL
107 *map)
108{
109 return &map->raidMap.ldSpanMap[ld].spanBlock[0];
110}
111
112static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_FW_RAID_MAP_ALL *map)
113{
114 return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
115}
116
117static u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_FW_RAID_MAP_ALL *map)
118{
119 return map->raidMap.arMapInfo[ar].pd[arm];
120}
121
122static u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_FW_RAID_MAP_ALL *map)
123{
124 return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
125}
126
127static u16 MR_PdDevHandleGet(u32 pd, struct MR_FW_RAID_MAP_ALL *map)
128{
129 return map->raidMap.devHndlInfo[pd].curDevHdl;
130}
131
132u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
133{
134 return map->raidMap.ldSpanMap[ld].ldRaid.targetId;
135}
136
137u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map)
138{
139 return map->raidMap.ldTgtIdToLd[ldTgtId];
140}
141
142static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
143 struct MR_FW_RAID_MAP_ALL *map)
144{
145 return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
146}
147
148
149
150
151u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
152 struct LD_LOAD_BALANCE_INFO *lbInfo)
153{
154 struct MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap;
155
156 if (pFwRaidMap->totalSize !=
157 (sizeof(struct MR_FW_RAID_MAP) -sizeof(struct MR_LD_SPAN_MAP) +
158 (sizeof(struct MR_LD_SPAN_MAP) *pFwRaidMap->ldCount))) {
159 printk(KERN_ERR "megasas: map info structure size 0x%x is not matching with ld count\n",
160 (unsigned int)((sizeof(struct MR_FW_RAID_MAP) -
161 sizeof(struct MR_LD_SPAN_MAP)) +
162 (sizeof(struct MR_LD_SPAN_MAP) *
163 pFwRaidMap->ldCount)));
164 printk(KERN_ERR "megasas: span map %x, pFwRaidMap->totalSize "
165 ": %x\n", (unsigned int)sizeof(struct MR_LD_SPAN_MAP),
166 pFwRaidMap->totalSize);
167 return 0;
168 }
169
170 mr_update_load_balance_params(map, lbInfo);
171
172 return 1;
173}
174
175u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
176 struct MR_FW_RAID_MAP_ALL *map, int *div_error)
177{
178 struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
179 struct MR_QUAD_ELEMENT *quad;
180 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
181 u32 span, j;
182
183 for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
184
185 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
186 quad = &pSpanBlock->block_span_info.quad[j];
187
188 if (quad->diff == 0) {
189 *div_error = 1;
190 return span;
191 }
192 if (quad->logStart <= row && row <= quad->logEnd &&
193 (mega_mod64(row-quad->logStart, quad->diff)) == 0) {
194 if (span_blk != NULL) {
195 u64 blk, debugBlk;
196 blk =
197 mega_div64_32(
198 (row-quad->logStart),
199 quad->diff);
200 debugBlk = blk;
201
202 blk = (blk + quad->offsetInSpan) <<
203 raid->stripeShift;
204 *span_blk = blk;
205 }
206 return span;
207 }
208 }
209 }
210 return span;
211}
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
231 u16 stripRef, u64 *pdBlock, u16 *pDevHandle,
232 struct RAID_CONTEXT *pRAID_Context,
233 struct MR_FW_RAID_MAP_ALL *map)
234{
235 struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
236 u32 pd, arRef;
237 u8 physArm, span;
238 u64 row;
239 u8 retval = TRUE;
240 int error_code = 0;
241
242 row = mega_div64_32(stripRow, raid->rowDataSize);
243
244 if (raid->level == 6) {
245
246 u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
247 u32 rowMod, armQ, arm;
248
249 if (raid->rowSize == 0)
250 return FALSE;
251
252 rowMod = mega_mod64(row, raid->rowSize);
253 armQ = raid->rowSize-1-rowMod;
254 arm = armQ+1+logArm;
255 if (arm >= raid->rowSize)
256 arm -= raid->rowSize;
257 physArm = (u8)arm;
258 } else {
259 if (raid->modFactor == 0)
260 return FALSE;
261 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
262 raid->modFactor),
263 map);
264 }
265
266 if (raid->spanDepth == 1) {
267 span = 0;
268 *pdBlock = row << raid->stripeShift;
269 } else {
270 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
271 if (error_code == 1)
272 return FALSE;
273 }
274
275
276 arRef = MR_LdSpanArrayGet(ld, span, map);
277 pd = MR_ArPdGet(arRef, physArm, map);
278
279 if (pd != MR_PD_INVALID)
280
281 *pDevHandle = MR_PdDevHandleGet(pd, map);
282 else {
283 *pDevHandle = MR_PD_INVALID;
284 if ((raid->level >= 5) &&
285 ((instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) ||
286 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER &&
287 raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
288 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
289 else if (raid->level == 1) {
290
291 pd = MR_ArPdGet(arRef, physArm + 1, map);
292 if (pd != MR_PD_INVALID)
293
294 *pDevHandle = MR_PdDevHandleGet(pd, map);
295 }
296 }
297
298 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
299 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
300 physArm;
301 return retval;
302}
303
304
305
306
307
308
309
310
311
312
313u8
314MR_BuildRaidContext(struct megasas_instance *instance,
315 struct IO_REQUEST_INFO *io_info,
316 struct RAID_CONTEXT *pRAID_Context,
317 struct MR_FW_RAID_MAP_ALL *map)
318{
319 struct MR_LD_RAID *raid;
320 u32 ld, stripSize, stripe_mask;
321 u64 endLba, endStrip, endRow, start_row, start_strip;
322 u64 regStart;
323 u32 regSize;
324 u8 num_strips, numRows;
325 u16 ref_in_start_stripe, ref_in_end_stripe;
326 u64 ldStartBlock;
327 u32 numBlocks, ldTgtId;
328 u8 isRead;
329 u8 retval = 0;
330
331 ldStartBlock = io_info->ldStartBlock;
332 numBlocks = io_info->numBlocks;
333 ldTgtId = io_info->ldTgtId;
334 isRead = io_info->isRead;
335
336 ld = MR_TargetIdToLdGet(ldTgtId, map);
337 raid = MR_LdRaidGet(ld, map);
338
339 stripSize = 1 << raid->stripeShift;
340 stripe_mask = stripSize-1;
341
342
343
344 start_strip = ldStartBlock >> raid->stripeShift;
345 ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
346 endLba = ldStartBlock + numBlocks - 1;
347 ref_in_end_stripe = (u16)(endLba & stripe_mask);
348 endStrip = endLba >> raid->stripeShift;
349 num_strips = (u8)(endStrip - start_strip + 1);
350 if (raid->rowDataSize == 0)
351 return FALSE;
352 start_row = mega_div64_32(start_strip, raid->rowDataSize);
353 endRow = mega_div64_32(endStrip, raid->rowDataSize);
354 numRows = (u8)(endRow - start_row + 1);
355
356
357
358
359
360
361 regStart = start_row << raid->stripeShift;
362
363 regSize = stripSize;
364
365
366 if (raid->capability.fpCapable) {
367 if (isRead)
368 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
369 ((num_strips == 1) ||
370 raid->capability.
371 fpReadAcrossStripe));
372 else
373 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
374 ((num_strips == 1) ||
375 raid->capability.
376 fpWriteAcrossStripe));
377 } else
378 io_info->fpOkForIo = FALSE;
379
380 if (numRows == 1) {
381
382 if (num_strips == 1) {
383 regStart += ref_in_start_stripe;
384 regSize = numBlocks;
385 }
386
387 } else {
388 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
389
390 regStart += ref_in_start_stripe;
391 regSize = stripSize - ref_in_start_stripe;
392
393
394 }
395
396 if (numRows > 2)
397
398 regSize += (numRows-2) << raid->stripeShift;
399
400
401 if (endStrip == endRow*raid->rowDataSize)
402 regSize += ref_in_end_stripe+1;
403 else
404 regSize += stripSize;
405 }
406
407 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
408 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
409 pRAID_Context->regLockFlags = (isRead) ?
410 raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
411 else
412 pRAID_Context->regLockFlags = (isRead) ?
413 REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
414 pRAID_Context->VirtualDiskTgtId = raid->targetId;
415 pRAID_Context->regLockRowLBA = regStart;
416 pRAID_Context->regLockLength = regSize;
417 pRAID_Context->configSeqNum = raid->seqNum;
418
419
420
421 if (io_info->fpOkForIo) {
422 retval = MR_GetPhyParams(instance, ld, start_strip,
423 ref_in_start_stripe,
424 &io_info->pdBlock,
425 &io_info->devHandle, pRAID_Context,
426 map);
427
428 if (io_info->devHandle == MR_PD_INVALID)
429 io_info->fpOkForIo = FALSE;
430 return retval;
431 } else if (isRead) {
432 uint stripIdx;
433 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
434 if (!MR_GetPhyParams(instance, ld,
435 start_strip + stripIdx,
436 ref_in_start_stripe,
437 &io_info->pdBlock,
438 &io_info->devHandle,
439 pRAID_Context, map))
440 return TRUE;
441 }
442 }
443 return TRUE;
444}
445
446void
447mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
448 struct LD_LOAD_BALANCE_INFO *lbInfo)
449{
450 int ldCount;
451 u16 ld;
452 struct MR_LD_RAID *raid;
453
454 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
455 ld = MR_TargetIdToLdGet(ldCount, map);
456 if (ld >= MAX_LOGICAL_DRIVES) {
457 lbInfo[ldCount].loadBalanceFlag = 0;
458 continue;
459 }
460
461 raid = MR_LdRaidGet(ld, map);
462
463
464 if ((raid->level == 1) && (raid->rowSize == 2) &&
465 (raid->spanDepth == 1) && raid->ldState ==
466 MR_LD_STATE_OPTIMAL) {
467 u32 pd, arRef;
468
469 lbInfo[ldCount].loadBalanceFlag = 1;
470
471
472 arRef = MR_LdSpanArrayGet(ld, 0, map);
473
474
475 pd = MR_ArPdGet(arRef, 0, map);
476
477 lbInfo[ldCount].raid1DevHandle[0] =
478 MR_PdDevHandleGet(pd, map);
479
480 pd = MR_ArPdGet(arRef, 1, map);
481
482
483 lbInfo[ldCount].raid1DevHandle[1] =
484 MR_PdDevHandleGet(pd, map);
485 } else
486 lbInfo[ldCount].loadBalanceFlag = 0;
487 }
488}
489
490u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block,
491 u32 count)
492{
493 u16 pend0, pend1;
494 u64 diff0, diff1;
495 u8 bestArm;
496
497
498 pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]);
499 pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]);
500
501
502 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
503 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
504 bestArm = (diff0 <= diff1 ? 0 : 1);
505
506 if ((bestArm == arm && pend0 > pend1 + 16) ||
507 (bestArm != arm && pend1 > pend0 + 16))
508 bestArm ^= 1;
509
510
511 lbInfo->last_accessed_block[bestArm] = block + count - 1;
512
513 return bestArm;
514}
515
516u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
517 struct IO_REQUEST_INFO *io_info)
518{
519 u8 arm, old_arm;
520 u16 devHandle;
521
522 old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
523
524
525 arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
526 io_info->numBlocks);
527 devHandle = lbInfo->raid1DevHandle[arm];
528 atomic_inc(&lbInfo->scsi_pending_cmds[arm]);
529
530 return devHandle;
531}
532