megaraid_sas_fp.c 14 KB

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