megaraid_sas_fp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 "megaraid_sas.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(struct megasas_instance *instance, u32 ld, u64 stripRow,
  201. u16 stripRef, u64 *pdBlock, u16 *pDevHandle,
  202. struct RAID_CONTEXT *pRAID_Context,
  203. struct MR_FW_RAID_MAP_ALL *map)
  204. {
  205. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  206. u32 pd, arRef;
  207. u8 physArm, span;
  208. u64 row;
  209. u8 retval = TRUE;
  210. int error_code = 0;
  211. row = mega_div64_32(stripRow, raid->rowDataSize);
  212. if (raid->level == 6) {
  213. /* logical arm within row */
  214. u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
  215. u32 rowMod, armQ, arm;
  216. if (raid->rowSize == 0)
  217. return FALSE;
  218. /* get logical row mod */
  219. rowMod = mega_mod64(row, raid->rowSize);
  220. armQ = raid->rowSize-1-rowMod; /* index of Q drive */
  221. arm = armQ+1+logArm; /* data always logically follows Q */
  222. if (arm >= raid->rowSize) /* handle wrap condition */
  223. arm -= raid->rowSize;
  224. physArm = (u8)arm;
  225. } else {
  226. if (raid->modFactor == 0)
  227. return FALSE;
  228. physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
  229. raid->modFactor),
  230. map);
  231. }
  232. if (raid->spanDepth == 1) {
  233. span = 0;
  234. *pdBlock = row << raid->stripeShift;
  235. } else {
  236. span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map, &error_code);
  237. if (error_code == 1)
  238. return FALSE;
  239. }
  240. /* Get the array on which this span is present */
  241. arRef = MR_LdSpanArrayGet(ld, span, map);
  242. pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
  243. if (pd != MR_PD_INVALID)
  244. /* Get dev handle from Pd. */
  245. *pDevHandle = MR_PdDevHandleGet(pd, map);
  246. else {
  247. *pDevHandle = MR_PD_INVALID; /* set dev handle as invalid. */
  248. if ((raid->level >= 5) &&
  249. ((instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) ||
  250. (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER &&
  251. raid->regTypeReqOnRead != REGION_TYPE_UNUSED)))
  252. pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
  253. else if (raid->level == 1) {
  254. /* Get alternate Pd. */
  255. pd = MR_ArPdGet(arRef, physArm + 1, map);
  256. if (pd != MR_PD_INVALID)
  257. /* Get dev handle from Pd */
  258. *pDevHandle = MR_PdDevHandleGet(pd, map);
  259. }
  260. }
  261. *pdBlock += stripRef + MR_LdSpanPtrGet(ld, span, map)->startBlk;
  262. pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
  263. physArm;
  264. return retval;
  265. }
  266. /*
  267. ******************************************************************************
  268. *
  269. * MR_BuildRaidContext function
  270. *
  271. * This function will initiate command processing. The start/end row and strip
  272. * information is calculated then the lock is acquired.
  273. * This function will return 0 if region lock was acquired OR return num strips
  274. */
  275. u8
  276. MR_BuildRaidContext(struct megasas_instance *instance,
  277. struct IO_REQUEST_INFO *io_info,
  278. struct RAID_CONTEXT *pRAID_Context,
  279. struct MR_FW_RAID_MAP_ALL *map)
  280. {
  281. struct MR_LD_RAID *raid;
  282. u32 ld, stripSize, stripe_mask;
  283. u64 endLba, endStrip, endRow, start_row, start_strip;
  284. u64 regStart;
  285. u32 regSize;
  286. u8 num_strips, numRows;
  287. u16 ref_in_start_stripe, ref_in_end_stripe;
  288. u64 ldStartBlock;
  289. u32 numBlocks, ldTgtId;
  290. u8 isRead;
  291. u8 retval = 0;
  292. ldStartBlock = io_info->ldStartBlock;
  293. numBlocks = io_info->numBlocks;
  294. ldTgtId = io_info->ldTgtId;
  295. isRead = io_info->isRead;
  296. ld = MR_TargetIdToLdGet(ldTgtId, map);
  297. raid = MR_LdRaidGet(ld, map);
  298. stripSize = 1 << raid->stripeShift;
  299. stripe_mask = stripSize-1;
  300. /*
  301. * calculate starting row and stripe, and number of strips and rows
  302. */
  303. start_strip = ldStartBlock >> raid->stripeShift;
  304. ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
  305. endLba = ldStartBlock + numBlocks - 1;
  306. ref_in_end_stripe = (u16)(endLba & stripe_mask);
  307. endStrip = endLba >> raid->stripeShift;
  308. num_strips = (u8)(endStrip - start_strip + 1); /* End strip */
  309. if (raid->rowDataSize == 0)
  310. return FALSE;
  311. start_row = mega_div64_32(start_strip, raid->rowDataSize);
  312. endRow = mega_div64_32(endStrip, raid->rowDataSize);
  313. numRows = (u8)(endRow - start_row + 1);
  314. /*
  315. * calculate region info.
  316. */
  317. /* assume region is at the start of the first row */
  318. regStart = start_row << raid->stripeShift;
  319. /* assume this IO needs the full row - we'll adjust if not true */
  320. regSize = stripSize;
  321. /* If IO spans more than 1 strip, fp is not possible
  322. FP is not possible for writes on non-0 raid levels
  323. FP is not possible if LD is not capable */
  324. if (num_strips > 1 || (!isRead && raid->level != 0) ||
  325. !raid->capability.fpCapable) {
  326. io_info->fpOkForIo = FALSE;
  327. } else {
  328. io_info->fpOkForIo = TRUE;
  329. }
  330. if (numRows == 1) {
  331. /* single-strip IOs can always lock only the data needed */
  332. if (num_strips == 1) {
  333. regStart += ref_in_start_stripe;
  334. regSize = numBlocks;
  335. }
  336. /* multi-strip IOs always need to full stripe locked */
  337. } else {
  338. if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
  339. /* If the start strip is the last in the start row */
  340. regStart += ref_in_start_stripe;
  341. regSize = stripSize - ref_in_start_stripe;
  342. /* initialize count to sectors from startref to end
  343. of strip */
  344. }
  345. if (numRows > 2)
  346. /* Add complete rows in the middle of the transfer */
  347. regSize += (numRows-2) << raid->stripeShift;
  348. /* if IO ends within first strip of last row */
  349. if (endStrip == endRow*raid->rowDataSize)
  350. regSize += ref_in_end_stripe+1;
  351. else
  352. regSize += stripSize;
  353. }
  354. pRAID_Context->timeoutValue = map->raidMap.fpPdIoTimeoutSec;
  355. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
  356. pRAID_Context->regLockFlags = (isRead) ?
  357. raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
  358. else
  359. pRAID_Context->regLockFlags = (isRead) ?
  360. REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
  361. pRAID_Context->VirtualDiskTgtId = raid->targetId;
  362. pRAID_Context->regLockRowLBA = regStart;
  363. pRAID_Context->regLockLength = regSize;
  364. pRAID_Context->configSeqNum = raid->seqNum;
  365. /*Get Phy Params only if FP capable, or else leave it to MR firmware
  366. to do the calculation.*/
  367. if (io_info->fpOkForIo) {
  368. retval = MR_GetPhyParams(instance, ld, start_strip,
  369. ref_in_start_stripe,
  370. &io_info->pdBlock,
  371. &io_info->devHandle, pRAID_Context,
  372. map);
  373. /* If IO on an invalid Pd, then FP i snot possible */
  374. if (io_info->devHandle == MR_PD_INVALID)
  375. io_info->fpOkForIo = FALSE;
  376. return retval;
  377. } else if (isRead) {
  378. uint stripIdx;
  379. for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
  380. if (!MR_GetPhyParams(instance, ld,
  381. start_strip + stripIdx,
  382. ref_in_start_stripe,
  383. &io_info->pdBlock,
  384. &io_info->devHandle,
  385. pRAID_Context, map))
  386. return TRUE;
  387. }
  388. }
  389. return TRUE;
  390. }
  391. void
  392. mr_update_load_balance_params(struct MR_FW_RAID_MAP_ALL *map,
  393. struct LD_LOAD_BALANCE_INFO *lbInfo)
  394. {
  395. int ldCount;
  396. u16 ld;
  397. struct MR_LD_RAID *raid;
  398. for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES; ldCount++) {
  399. ld = MR_TargetIdToLdGet(ldCount, map);
  400. if (ld >= MAX_LOGICAL_DRIVES) {
  401. lbInfo[ldCount].loadBalanceFlag = 0;
  402. continue;
  403. }
  404. raid = MR_LdRaidGet(ld, map);
  405. /* Two drive Optimal RAID 1 */
  406. if ((raid->level == 1) && (raid->rowSize == 2) &&
  407. (raid->spanDepth == 1) && raid->ldState ==
  408. MR_LD_STATE_OPTIMAL) {
  409. u32 pd, arRef;
  410. lbInfo[ldCount].loadBalanceFlag = 1;
  411. /* Get the array on which this span is present */
  412. arRef = MR_LdSpanArrayGet(ld, 0, map);
  413. /* Get the Pd */
  414. pd = MR_ArPdGet(arRef, 0, map);
  415. /* Get dev handle from Pd */
  416. lbInfo[ldCount].raid1DevHandle[0] =
  417. MR_PdDevHandleGet(pd, map);
  418. /* Get the Pd */
  419. pd = MR_ArPdGet(arRef, 1, map);
  420. /* Get the dev handle from Pd */
  421. lbInfo[ldCount].raid1DevHandle[1] =
  422. MR_PdDevHandleGet(pd, map);
  423. } else
  424. lbInfo[ldCount].loadBalanceFlag = 0;
  425. }
  426. }
  427. u8 megasas_get_best_arm(struct LD_LOAD_BALANCE_INFO *lbInfo, u8 arm, u64 block,
  428. u32 count)
  429. {
  430. u16 pend0, pend1;
  431. u64 diff0, diff1;
  432. u8 bestArm;
  433. /* get the pending cmds for the data and mirror arms */
  434. pend0 = atomic_read(&lbInfo->scsi_pending_cmds[0]);
  435. pend1 = atomic_read(&lbInfo->scsi_pending_cmds[1]);
  436. /* Determine the disk whose head is nearer to the req. block */
  437. diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[0]);
  438. diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[1]);
  439. bestArm = (diff0 <= diff1 ? 0 : 1);
  440. if ((bestArm == arm && pend0 > pend1 + 16) ||
  441. (bestArm != arm && pend1 > pend0 + 16))
  442. bestArm ^= 1;
  443. /* Update the last accessed block on the correct pd */
  444. lbInfo->last_accessed_block[bestArm] = block + count - 1;
  445. return bestArm;
  446. }
  447. u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
  448. struct IO_REQUEST_INFO *io_info)
  449. {
  450. u8 arm, old_arm;
  451. u16 devHandle;
  452. old_arm = lbInfo->raid1DevHandle[0] == io_info->devHandle ? 0 : 1;
  453. /* get best new arm */
  454. arm = megasas_get_best_arm(lbInfo, old_arm, io_info->ldStartBlock,
  455. io_info->numBlocks);
  456. devHandle = lbInfo->raid1DevHandle[arm];
  457. atomic_inc(&lbInfo->scsi_pending_cmds[arm]);
  458. return devHandle;
  459. }