Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / scsi / megaraid / megaraid_sas_fp.c
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2009-2011  LSI Corporation.
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>
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 <asm/div64.h>
56
57 #define ABS_DIFF(a, b)   (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
58 #define MR_LD_STATE_OPTIMAL 3
59 #define FALSE 0
60 #define TRUE 1
61
62 /* Prototypes */
63 void
64 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
65                               struct LD_LOAD_BALANCE_INFO *lbInfo);
66
67 u32 mega_mod64(u64 dividend, u32 divisor)
68 {
69         u64 d;
70         u32 remainder;
71
72         if (!divisor)
73                 printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
74         d = dividend;
75         remainder = do_div(d, divisor);
76         return remainder;
77 }
78
79 /**
80  * @param dividend    : Dividend
81  * @param divisor    : Divisor
82  *
83  * @return quotient
84  **/
85 u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
86 {
87         u32 remainder;
88         u64 d;
89
90         if (!divisor)
91                 printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");
92
93         d = dividend;
94         remainder = do_div(d, divisor);
95
96         return d;
97 }
98
99 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
100 {
101         return &map->raidMap.ldSpanMap[ld].ldRaid;
102 }
103
104 static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
105                                                    struct MR_FW_RAID_MAP_ALL
106                                                    *map)
107 {
108         return &map->raidMap.ldSpanMap[ld].spanBlock[0];
109 }
110
111 static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_FW_RAID_MAP_ALL *map)
112 {
113         return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
114 }
115
116 static u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_FW_RAID_MAP_ALL *map)
117 {
118         return map->raidMap.arMapInfo[ar].pd[arm];
119 }
120
121 static u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_FW_RAID_MAP_ALL *map)
122 {
123         return map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef;
124 }
125
126 static u16 MR_PdDevHandleGet(u32 pd, struct MR_FW_RAID_MAP_ALL *map)
127 {
128         return map->raidMap.devHndlInfo[pd].curDevHdl;
129 }
130
131 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map)
132 {
133         return map->raidMap.ldSpanMap[ld].ldRaid.targetId;
134 }
135
136 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map)
137 {
138         return map->raidMap.ldTgtIdToLd[ldTgtId];
139 }
140
141 static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
142                                           struct MR_FW_RAID_MAP_ALL *map)
143 {
144         return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
145 }
146
147 /*
148  * This function will validate Map info data provided by FW
149  */
150 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
151                       struct LD_LOAD_BALANCE_INFO *lbInfo)
152 {
153         struct MR_FW_RAID_MAP *pFwRaidMap = &map->raidMap;
154
155         if (pFwRaidMap->totalSize !=
156             (sizeof(struct MR_FW_RAID_MAP) -sizeof(struct MR_LD_SPAN_MAP) +
157              (sizeof(struct MR_LD_SPAN_MAP) *pFwRaidMap->ldCount))) {
158                 printk(KERN_ERR "megasas: map info structure size 0x%x is not matching with ld count\n",
159                        (unsigned int)((sizeof(struct MR_FW_RAID_MAP) -
160                                        sizeof(struct MR_LD_SPAN_MAP)) +
161                                       (sizeof(struct MR_LD_SPAN_MAP) *
162                                        pFwRaidMap->ldCount)));
163                 printk(KERN_ERR "megasas: span map %x, pFwRaidMap->totalSize "
164                        ": %x\n", (unsigned int)sizeof(struct MR_LD_SPAN_MAP),
165                        pFwRaidMap->totalSize);
166                 return 0;
167         }
168
169         mr_update_load_balance_params(map, lbInfo);
170
171         return 1;
172 }
173
174 u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
175                     struct MR_FW_RAID_MAP_ALL *map, int *div_error)
176 {
177         struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
178         struct MR_QUAD_ELEMENT    *quad;
179         struct MR_LD_RAID         *raid = MR_LdRaidGet(ld, map);
180         u32                span, j;
181
182         for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
183
184                 for (j = 0; j < pSpanBlock->block_span_info.noElements; j++) {
185                         quad = &pSpanBlock->block_span_info.quad[j];
186
187                         if (quad->diff == 0) {
188                                 *div_error = 1;
189                                 return span;
190                         }
191                         if (quad->logStart <= row  &&  row <= quad->logEnd  &&
192                             (mega_mod64(row-quad->logStart, quad->diff)) == 0) {
193                                 if (span_blk != NULL) {
194                                         u64  blk, debugBlk;
195                                         blk =
196                                                 mega_div64_32(
197                                                         (row-quad->logStart),
198                                                         quad->diff);
199                                         debugBlk = blk;
200
201                                         blk = (blk + quad->offsetInSpan) <<
202                                                 raid->stripeShift;
203                                         *span_blk = blk;
204                                 }
205                                 return span;
206                         }
207                 }
208         }
209         return span;
210 }
211
212 /*
213 ******************************************************************************
214 *
215 * This routine calculates the arm, span and block for the specified stripe and
216 * reference in stripe.
217 *
218 * Inputs :
219 *
220 *    ld   - Logical drive number
221 *    stripRow        - Stripe number
222 *    stripRef    - Reference in stripe
223 *
224 * Outputs :
225 *
226 *    span          - Span number
227 *    block         - Absolute Block number in the physical disk
228 */
229 u8 MR_GetPhyParams(u32 ld, u64 stripRow, u16 stripRef, u64 *pdBlock,
230                    u16 *pDevHandle, struct RAID_CONTEXT *pRAID_Context,
231                    struct MR_FW_RAID_MAP_ALL *map)
232 {
233         struct MR_LD_RAID  *raid = MR_LdRaidGet(ld, map);
234         u32         pd, arRef;
235         u8          physArm, span;
236         u64         row;
237         u8          retval = TRUE;
238         int         error_code = 0;
239
240         row =  mega_div64_32(stripRow, raid->rowDataSize);
241
242         if (raid->level == 6) {
243                 /* logical arm within row */
244                 u32 logArm =  mega_mod64(stripRow, raid->rowDataSize);
245                 u32 rowMod, armQ, arm;
246
247                 if (raid->rowSize == 0)
248                         return FALSE;
249                 /* get logical row mod */
250                 rowMod = mega_mod64(row, raid->rowSize);
251                 armQ = raid->rowSize-1-rowMod; /* index of Q drive */
252                 arm = armQ+1+logArm; /* data always logically follows Q */
253                 if (arm >= raid->rowSize) /* handle wrap condition */
254                         arm -= raid->rowSize;
255                 physArm = (u8)arm;
256         } else  {
257                 if (raid->modFactor == 0)
258                         return FALSE;
259                 physArm = MR_LdDataArmGet(ld,  mega_mod64(stripRow,
260                                                           raid->modFactor),
261                                           map);
262         }
263
264         if (raid->spanDepth == 1) {
265                 span = 0;
266                 *pdBlock = row << raid->stripeShift;
267         } else {
268                 span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
269                 if (error_code == 1)
270                         return FALSE;
271         }
272
273         /* Get the array on which this span is present */
274         arRef       = MR_LdSpanArrayGet(ld, span, map);
275         pd          = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
276
277         if (pd != MR_PD_INVALID)
278                 /* Get dev handle from Pd. */
279                 *pDevHandle = MR_PdDevHandleGet(pd, map);
280         else {
281                 *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
282                 if (raid->level >= 5)
283                         pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
284                 else if (raid->level == 1) {
285                         /* Get alternate Pd. */
286                         pd = MR_ArPdGet(arRef, physArm + 1, map);
287                         if (pd != MR_PD_INVALID)
288                                 /* Get dev handle from Pd */
289                                 *pDevHandle = MR_PdDevHandleGet(pd, map);
290                 }
291                 retval = FALSE;
292         }
293
294         *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
295         pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
296                 physArm;
297         return retval;
298 }
299
300 /*
301 ******************************************************************************
302 *
303 * MR_BuildRaidContext function
304 *
305 * This function will initiate command processing.  The start/end row and strip
306 * information is calculated then the lock is acquired.
307 * This function will return 0 if region lock was acquired OR return num strips
308 */
309 u8
310 MR_BuildRaidContext(struct IO_REQUEST_INFO *io_info,
311                     struct RAID_CONTEXT *pRAID_Context,
312                     struct MR_FW_RAID_MAP_ALL *map)
313 {
314         struct MR_LD_RAID  *raid;
315         u32         ld, stripSize, stripe_mask;
316         u64         endLba, endStrip, endRow, start_row, start_strip;
317         u64         regStart;
318         u32         regSize;
319         u8          num_strips, numRows;
320         u16         ref_in_start_stripe, ref_in_end_stripe;
321         u64         ldStartBlock;
322         u32         numBlocks, ldTgtId;
323         u8          isRead;
324         u8          retval = 0;
325
326         ldStartBlock = io_info->ldStartBlock;
327         numBlocks = io_info->numBlocks;
328         ldTgtId = io_info->ldTgtId;
329         isRead = io_info->isRead;
330
331         ld = MR_TargetIdToLdGet(ldTgtId, map);
332         raid = MR_LdRaidGet(ld, map);
333
334         stripSize = 1 << raid->stripeShift;
335         stripe_mask = stripSize-1;
336         /*
337          * calculate starting row and stripe, and number of strips and rows
338          */
339         start_strip         = ldStartBlock >> raid->stripeShift;
340         ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
341         endLba              = ldStartBlock + numBlocks - 1;
342         ref_in_end_stripe   = (u16)(endLba & stripe_mask);
343         endStrip            = endLba >> raid->stripeShift;
344         num_strips          = (u8)(endStrip - start_strip + 1); /* End strip */
345         if (raid->rowDataSize == 0)
346                 return FALSE;
347         start_row           =  mega_div64_32(start_strip, raid->rowDataSize);
348         endRow              =  mega_div64_32(endStrip, raid->rowDataSize);
349         numRows             = (u8)(endRow - start_row + 1);
350
351         /*
352          * calculate region info.
353          */
354
355         /* assume region is at the start of the first row */
356         regStart            = start_row << raid->stripeShift;
357         /* assume this IO needs the full row - we'll adjust if not true */
358         regSize             = stripSize;
359
360         /* If IO spans more than 1 strip, fp is not possible
361            FP is not possible for writes on non-0 raid levels
362            FP is not possible if LD is not capable */
363         if (num_strips > 1 || (!isRead && raid->level != 0) ||
364             !raid->capability.fpCapable) {
365                 io_info->fpOkForIo = FALSE;
366         } else {
367                 io_info->fpOkForIo = TRUE;
368         }
369
370         if (numRows == 1) {
371                 /* single-strip IOs can always lock only the data needed */
372                 if (num_strips == 1) {
373                         regStart += ref_in_start_stripe;
374                         regSize = numBlocks;
375                 }
376                 /* multi-strip IOs always need to full stripe locked */
377         } else {
378                 if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
379                         /* If the start strip is the last in the start row */
380                         regStart += ref_in_start_stripe;
381                         regSize = stripSize - ref_in_start_stripe;
382                         /* initialize count to sectors from startref to end
383                            of strip */
384                 }
385
386                 if (numRows > 2)
387                         /* Add complete rows in the middle of the transfer */
388                         regSize += (numRows-2) << raid->stripeShift;
389
390                 /* if IO ends within first strip of last row */
391                 if (endStrip == endRow*raid->rowDataSize)
392                         regSize += ref_in_end_stripe+1;
393                 else
394                         regSize += stripSize;
395         }
396
397         pRAID_Context->timeoutValue     = map->raidMap.fpPdIoTimeoutSec;
398         pRAID_Context->regLockFlags     = (isRead) ? REGION_TYPE_SHARED_READ :
399                 raid->regTypeReqOnWrite;
400         pRAID_Context->VirtualDiskTgtId = raid->targetId;
401         pRAID_Context->regLockRowLBA    = regStart;
402         pRAID_Context->regLockLength    = regSize;
403         pRAID_Context->configSeqNum     = raid->seqNum;
404
405         /*Get Phy Params only if FP capable, or else leave it to MR firmware
406           to do the calculation.*/
407         if (io_info->fpOkForIo) {
408                 retval = MR_GetPhyParams(ld, start_strip, ref_in_start_stripe,
409                                          &io_info->pdBlock,
410                                          &io_info->devHandle, pRAID_Context,
411                                          map);
412                 /* If IO on an invalid Pd, then FP i snot possible */
413                 if (io_info->devHandle == MR_PD_INVALID)
414                         io_info->fpOkForIo = FALSE;
415                 return retval;
416         } else if (isRead) {
417                 uint stripIdx;
418                 for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
419                         if (!MR_GetPhyParams(ld, start_strip + stripIdx,
420                                              ref_in_start_stripe,
421                                              &io_info->pdBlock,
422                                              &io_info->devHandle,
423                                              pRAID_Context, map))
424                                 return TRUE;
425                 }
426         }
427         return TRUE;
428 }
429
430 void
431 mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
432                               struct LD_LOAD_BALANCE_INFO *lbInfo)
433 {
434         int ldCount;
435         u16 ld;
436         struct MR_LD_RAID *raid;
437
438         for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
439                 ld = MR_TargetIdToLdGet(ldCount, map);
440                 if (ld >= MAX_LOGICAL_DRIVES) {
441                         lbInfo[ldCount].loadBalanceFlag = 0;
442                         continue;
443                 }
444
445                 raid = MR_LdRaidGet(ld, map);
446
447                 /* Two drive Optimal RAID 1 */
448                 if ((raid->level == 1)  &&  (raid->rowSize == 2) &&
449                     (raid->spanDepth == 1) && raid->ldState ==
450                     MR_LD_STATE_OPTIMAL) {
451                         u32 pd, arRef;
452
453                         lbInfo[ldCount].loadBalanceFlag = 1;
454
455                         /* Get the array on which this span is present */
456                         arRef = MR_LdSpanArrayGet(ld, 0, map);
457
458                         /* Get the Pd */
459                         pd = MR_ArPdGet(arRef, 0, map);
460                         /* Get dev handle from Pd */
461                         lbInfo[ldCount].raid1DevHandle[0] =
462                                 MR_PdDevHandleGet(pd, map);
463                         /* Get the Pd */
464                         pd = MR_ArPdGet(arRef, 1, map);
465
466                         /* Get the dev handle from Pd */
467                         lbInfo[ldCount].raid1DevHandle[1] =
468                                 MR_PdDevHandleGet(pd, map);
469                 } else
470                         lbInfo[ldCount].loadBalanceFlag = 0;
471         }
472 }
473
474 u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block,
475                         u32 count)
476 {
477         u16     pend0, pend1;
478         u64     diff0, diff1;
479         u8      bestArm;
480
481         /* get the pending cmds for the data and mirror arms */
482         pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]);
483         pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]);
484
485         /* Determine the disk whose head is nearer to the req. block */
486         diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
487         diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
488         bestArm = (diff0 <= diff1 ? 0 : 1);
489
490         if ((bestArm == arm && pend0 > pend1 + 16)  ||
491             (bestArm != arm && pend1 > pend0 + 16))
492                 bestArm ^= 1;
493
494         /* Update the last accessed block on the correct pd */
495         lbInfo->last_accessed_block[bestArm] = block + count - 1;
496
497         return bestArm;
498 }
499
500 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
501                            struct IO_REQUEST_INFO *io_info)
502 {
503         u8 arm, old_arm;
504         u16 devHandle;
505
506         old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
507
508         /* get best new arm */
509         arm  = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
510                                     io_info->numBlocks);
511         devHandle = lbInfo->raid1DevHandle[arm];
512         atomic_inc(&lbInfo->scsi_pending_cmds[arm]);
513
514         return devHandle;
515 }