]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/megaraid/megaraid_sas_fp.c
[SCSI] megaraid_sas: Add support for Extended MSI-x vectors for 12Gb/s controller
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / megaraid / megaraid_sas_fp.c
CommitLineData
9c915a8c
AR
1/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
ae59057b 4 * Copyright (c) 2009-2012 LSI Corporation.
9c915a8c
AR
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * FILE: megaraid_sas_fp.c
21 *
22 * Authors: LSI Corporation
23 * Sumant Patro
24 * Varad Talamacki
25 * Manoj Jose
26 *
27 * Send feedback to: <megaraidlinux@lsi.com>
28 *
29 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
30 * ATTN: Linuxraid
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>
9c915a8c
AR
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"
36807e67 55#include "megaraid_sas.h"
9c915a8c
AR
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/* Prototypes */
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 * @param dividend : Dividend
82 * @param divisor : Divisor
83 *
84 * @return quotient
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 * This function will validate Map info data provided by FW
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* This routine calculates the arm, span and block for the specified stripe and
217* reference in stripe.
218*
219* Inputs :
220*
221* ld - Logical drive number
222* stripRow - Stripe number
223* stripRef - Reference in stripe
224*
225* Outputs :
226*
227* span - Span number
228* block - Absolute Block number in the physical disk
229*/
36807e67
AR
230u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
231 u16 stripRef, u64 *pdBlock, u16 *pDevHandle,
232 struct RAID_CONTEXT *pRAID_Context,
9c915a8c
AR
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;
21d3c710
SS
241 u8 do_invader = 0;
242
243 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER ||
244 instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
245 do_invader = 1;
9c915a8c
AR
246
247 row = mega_div64_32(stripRow, raid->rowDataSize);
248
249 if (raid->level == 6) {
250 /* logical arm within row */
251 u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
252 u32 rowMod, armQ, arm;
253
254 if (raid->rowSize == 0)
255 return FALSE;
256 /* get logical row mod */
257 rowMod = mega_mod64(row, raid->rowSize);
258 armQ = raid->rowSize-1-rowMod; /* index of Q drive */
259 arm = armQ+1+logArm; /* data always logically follows Q */
260 if (arm >= raid->rowSize) /* handle wrap condition */
261 arm -= raid->rowSize;
262 physArm = (u8)arm;
263 } else {
264 if (raid->modFactor == 0)
265 return FALSE;
266 physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
267 raid->modFactor),
268 map);
269 }
270
271 if (raid->spanDepth == 1) {
272 span = 0;
273 *pdBlock = row << raid->stripeShift;
274 } else {
275 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
276 if (error_code == 1)
277 return FALSE;
278 }
279
280 /* Get the array on which this span is present */
281 arRef = MR_LdSpanArrayGet(ld, span, map);
282 pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
283
284 if (pd != MR_PD_INVALID)
285 /* Get dev handle from Pd. */
286 *pDevHandle = MR_PdDevHandleGet(pd, map);
287 else {
288 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
36807e67 289 if ((raid->level >= 5) &&
21d3c710
SS
290 (!do_invader || (do_invader &&
291 (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
9c915a8c
AR
292 pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
293 else if (raid->level == 1) {
294 /* Get alternate Pd. */
295 pd = MR_ArPdGet(arRef, physArm + 1, map);
296 if (pd != MR_PD_INVALID)
297 /* Get dev handle from Pd */
298 *pDevHandle = MR_PdDevHandleGet(pd, map);
299 }
9c915a8c
AR
300 }
301
302 *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
303 pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
304 physArm;
305 return retval;
306}
307
308/*
309******************************************************************************
310*
311* MR_BuildRaidContext function
312*
313* This function will initiate command processing. The start/end row and strip
314* information is calculated then the lock is acquired.
315* This function will return 0 if region lock was acquired OR return num strips
316*/
317u8
36807e67
AR
318MR_BuildRaidContext(struct megasas_instance *instance,
319 struct IO_REQUEST_INFO *io_info,
9c915a8c
AR
320 struct RAID_CONTEXT *pRAID_Context,
321 struct MR_FW_RAID_MAP_ALL *map)
322{
323 struct MR_LD_RAID *raid;
324 u32 ld, stripSize, stripe_mask;
325 u64 endLba, endStrip, endRow, start_row, start_strip;
326 u64 regStart;
327 u32 regSize;
328 u8 num_strips, numRows;
329 u16 ref_in_start_stripe, ref_in_end_stripe;
330 u64 ldStartBlock;
331 u32 numBlocks, ldTgtId;
332 u8 isRead;
333 u8 retval = 0;
334
335 ldStartBlock = io_info->ldStartBlock;
336 numBlocks = io_info->numBlocks;
337 ldTgtId = io_info->ldTgtId;
338 isRead = io_info->isRead;
339
340 ld = MR_TargetIdToLdGet(ldTgtId, map);
341 raid = MR_LdRaidGet(ld, map);
342
343 stripSize = 1 << raid->stripeShift;
344 stripe_mask = stripSize-1;
345 /*
346 * calculate starting row and stripe, and number of strips and rows
347 */
348 start_strip = ldStartBlock >> raid->stripeShift;
349 ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
350 endLba = ldStartBlock + numBlocks - 1;
351 ref_in_end_stripe = (u16)(endLba & stripe_mask);
352 endStrip = endLba >> raid->stripeShift;
353 num_strips = (u8)(endStrip - start_strip + 1); /* End strip */
354 if (raid->rowDataSize == 0)
355 return FALSE;
356 start_row = mega_div64_32(start_strip, raid->rowDataSize);
357 endRow = mega_div64_32(endStrip, raid->rowDataSize);
358 numRows = (u8)(endRow - start_row + 1);
359
360 /*
361 * calculate region info.
362 */
363
364 /* assume region is at the start of the first row */
365 regStart = start_row << raid->stripeShift;
366 /* assume this IO needs the full row - we'll adjust if not true */
367 regSize = stripSize;
368
c1529fa2
AR
369 /* Check if we can send this I/O via FastPath */
370 if (raid->capability.fpCapable) {
371 if (isRead)
372 io_info->fpOkForIo = (raid->capability.fpReadCapable &&
373 ((num_strips == 1) ||
374 raid->capability.
375 fpReadAcrossStripe));
376 else
377 io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
378 ((num_strips == 1) ||
379 raid->capability.
380 fpWriteAcrossStripe));
381 } else
9c915a8c 382 io_info->fpOkForIo = FALSE;
9c915a8c
AR
383
384 if (numRows == 1) {
385 /* single-strip IOs can always lock only the data needed */
386 if (num_strips == 1) {
387 regStart += ref_in_start_stripe;
388 regSize = numBlocks;
389 }
390 /* multi-strip IOs always need to full stripe locked */
391 } else {
392 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
393 /* If the start strip is the last in the start row */
394 regStart += ref_in_start_stripe;
395 regSize = stripSize - ref_in_start_stripe;
396 /* initialize count to sectors from startref to end
397 of strip */
398 }
399
400 if (numRows > 2)
401 /* Add complete rows in the middle of the transfer */
402 regSize += (numRows-2) << raid->stripeShift;
403
404 /* if IO ends within first strip of last row */
405 if (endStrip == endRow*raid->rowDataSize)
406 regSize += ref_in_end_stripe+1;
407 else
408 regSize += stripSize;
409 }
410
411 pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
21d3c710
SS
412 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
413 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
36807e67
AR
414 pRAID_Context->regLockFlags = (isRead) ?
415 raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
416 else
417 pRAID_Context->regLockFlags = (isRead) ?
418 REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
9c915a8c
AR
419 pRAID_Context->VirtualDiskTgtId = raid->targetId;
420 pRAID_Context->regLockRowLBA = regStart;
421 pRAID_Context->regLockLength = regSize;
422 pRAID_Context->configSeqNum = raid->seqNum;
423
424 /*Get Phy Params only if FP capable, or else leave it to MR firmware
425 to do the calculation.*/
426 if (io_info->fpOkForIo) {
36807e67
AR
427 retval = MR_GetPhyParams(instance, ld, start_strip,
428 ref_in_start_stripe,
9c915a8c
AR
429 &io_info->pdBlock,
430 &io_info->devHandle, pRAID_Context,
431 map);
432 /* If IO on an invalid Pd, then FP i snot possible */
433 if (io_info->devHandle == MR_PD_INVALID)
434 io_info->fpOkForIo = FALSE;
435 return retval;
436 } else if (isRead) {
437 uint stripIdx;
438 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
36807e67
AR
439 if (!MR_GetPhyParams(instance, ld,
440 start_strip + stripIdx,
9c915a8c
AR
441 ref_in_start_stripe,
442 &io_info->pdBlock,
443 &io_info->devHandle,
444 pRAID_Context, map))
445 return TRUE;
446 }
447 }
448 return TRUE;
449}
450
451void
452mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
453 struct LD_LOAD_BALANCE_INFO *lbInfo)
454{
455 int ldCount;
456 u16 ld;
457 struct MR_LD_RAID *raid;
458
459 for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
460 ld = MR_TargetIdToLdGet(ldCount, map);
461 if (ld >= MAX_LOGICAL_DRIVES) {
462 lbInfo[ldCount].loadBalanceFlag = 0;
463 continue;
464 }
465
466 raid = MR_LdRaidGet(ld, map);
467
468 /* Two drive Optimal RAID 1 */
469 if ((raid->level == 1) && (raid->rowSize == 2) &&
470 (raid->spanDepth == 1) && raid->ldState ==
471 MR_LD_STATE_OPTIMAL) {
472 u32 pd, arRef;
473
474 lbInfo[ldCount].loadBalanceFlag = 1;
475
476 /* Get the array on which this span is present */
477 arRef = MR_LdSpanArrayGet(ld, 0, map);
478
479 /* Get the Pd */
480 pd = MR_ArPdGet(arRef, 0, map);
481 /* Get dev handle from Pd */
482 lbInfo[ldCount].raid1DevHandle[0] =
483 MR_PdDevHandleGet(pd, map);
484 /* Get the Pd */
485 pd = MR_ArPdGet(arRef, 1, map);
486
487 /* Get the dev handle from Pd */
488 lbInfo[ldCount].raid1DevHandle[1] =
489 MR_PdDevHandleGet(pd, map);
490 } else
491 lbInfo[ldCount].loadBalanceFlag = 0;
492 }
493}
494
495u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block,
496 u32 count)
497{
498 u16 pend0, pend1;
499 u64 diff0, diff1;
500 u8 bestArm;
501
502 /* get the pending cmds for the data and mirror arms */
503 pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]);
504 pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]);
505
506 /* Determine the disk whose head is nearer to the req. block */
507 diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
508 diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
509 bestArm = (diff0 <= diff1 ? 0 : 1);
510
70b47b88
SS
511 /*Make balance count from 16 to 4 to keep driver in sync with Firmware*/
512 if ((bestArm == arm && pend0 > pend1 + 4) ||
513 (bestArm != arm && pend1 > pend0 + 4))
9c915a8c
AR
514 bestArm ^= 1;
515
516 /* Update the last accessed block on the correct pd */
517 lbInfo->last_accessed_block[bestArm] = block + count - 1;
518
519 return bestArm;
520}
521
522u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
523 struct IO_REQUEST_INFO *io_info)
524{
525 u8 arm, old_arm;
526 u16 devHandle;
527
528 old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
529
530 /* get best new arm */
531 arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
532 io_info->numBlocks);
533 devHandle = lbInfo->raid1DevHandle[arm];
534 atomic_inc(&lbInfo->scsi_pending_cmds[arm]);
535
536 return devHandle;
537}