megaraid_sas_fp.c 14 KB

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