megaraid_sas_fp.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * FILE: megaraid_sas_fp.c
  21. *
  22. * Authors: Avago Technologies
  23. * Sumant Patro
  24. * Varad Talamacki
  25. * Manoj Jose
  26. * Kashyap Desai <kashyap.desai@avagotech.com>
  27. * Sumit Saxena <sumit.saxena@avagotech.com>
  28. *
  29. * Send feedback to: megaraidlinux.pdl@avagotech.com
  30. *
  31. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  32. * San Jose, California 95131
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/pci.h>
  37. #include <linux/list.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/delay.h>
  43. #include <linux/uio.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/fs.h>
  46. #include <linux/compat.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/poll.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include "megaraid_sas_fusion.h"
  54. #include "megaraid_sas.h"
  55. #include <asm/div64.h>
  56. #define LB_PENDING_CMDS_DEFAULT 4
  57. static unsigned int lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
  58. module_param(lb_pending_cmds, int, S_IRUGO);
  59. MODULE_PARM_DESC(lb_pending_cmds, "Change raid-1 load balancing outstanding "
  60. "threshold. Valid Values are 1-128. Default: 4");
  61. #define ABS_DIFF(a, b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
  62. #define MR_LD_STATE_OPTIMAL 3
  63. #ifdef FALSE
  64. #undef FALSE
  65. #endif
  66. #define FALSE 0
  67. #ifdef TRUE
  68. #undef TRUE
  69. #endif
  70. #define TRUE 1
  71. #define SPAN_DEBUG 0
  72. #define SPAN_ROW_SIZE(map, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowSize)
  73. #define SPAN_ROW_DATA_SIZE(map_, ld, index_) (MR_LdSpanPtrGet(ld, index_, map)->spanRowDataSize)
  74. #define SPAN_INVALID 0xff
  75. /* Prototypes */
  76. static void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
  77. PLD_SPAN_INFO ldSpanInfo);
  78. static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
  79. u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
  80. struct RAID_CONTEXT *pRAID_Context, struct MR_DRV_RAID_MAP_ALL *map);
  81. static u64 get_row_from_strip(struct megasas_instance *instance, u32 ld,
  82. u64 strip, struct MR_DRV_RAID_MAP_ALL *map);
  83. u32 mega_mod64(u64 dividend, u32 divisor)
  84. {
  85. u64 d;
  86. u32 remainder;
  87. if (!divisor)
  88. printk(KERN_ERR "megasas : DIVISOR is zero, in div fn\n");
  89. d = dividend;
  90. remainder = do_div(d, divisor);
  91. return remainder;
  92. }
  93. /**
  94. * @param dividend : Dividend
  95. * @param divisor : Divisor
  96. *
  97. * @return quotient
  98. **/
  99. u64 mega_div64_32(uint64_t dividend, uint32_t divisor)
  100. {
  101. u32 remainder;
  102. u64 d;
  103. if (!divisor)
  104. printk(KERN_ERR "megasas : DIVISOR is zero in mod fn\n");
  105. d = dividend;
  106. remainder = do_div(d, divisor);
  107. return d;
  108. }
  109. struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
  110. {
  111. return &map->raidMap.ldSpanMap[ld].ldRaid;
  112. }
  113. static struct MR_SPAN_BLOCK_INFO *MR_LdSpanInfoGet(u32 ld,
  114. struct MR_DRV_RAID_MAP_ALL
  115. *map)
  116. {
  117. return &map->raidMap.ldSpanMap[ld].spanBlock[0];
  118. }
  119. static u8 MR_LdDataArmGet(u32 ld, u32 armIdx, struct MR_DRV_RAID_MAP_ALL *map)
  120. {
  121. return map->raidMap.ldSpanMap[ld].dataArmMap[armIdx];
  122. }
  123. u16 MR_ArPdGet(u32 ar, u32 arm, struct MR_DRV_RAID_MAP_ALL *map)
  124. {
  125. return le16_to_cpu(map->raidMap.arMapInfo[ar].pd[arm]);
  126. }
  127. u16 MR_LdSpanArrayGet(u32 ld, u32 span, struct MR_DRV_RAID_MAP_ALL *map)
  128. {
  129. return le16_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].span.arrayRef);
  130. }
  131. __le16 MR_PdDevHandleGet(u32 pd, struct MR_DRV_RAID_MAP_ALL *map)
  132. {
  133. return map->raidMap.devHndlInfo[pd].curDevHdl;
  134. }
  135. u16 MR_GetLDTgtId(u32 ld, struct MR_DRV_RAID_MAP_ALL *map)
  136. {
  137. return le16_to_cpu(map->raidMap.ldSpanMap[ld].ldRaid.targetId);
  138. }
  139. u8 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_DRV_RAID_MAP_ALL *map)
  140. {
  141. return map->raidMap.ldTgtIdToLd[ldTgtId];
  142. }
  143. static struct MR_LD_SPAN *MR_LdSpanPtrGet(u32 ld, u32 span,
  144. struct MR_DRV_RAID_MAP_ALL *map)
  145. {
  146. return &map->raidMap.ldSpanMap[ld].spanBlock[span].span;
  147. }
  148. /*
  149. * This function will Populate Driver Map using firmware raid map
  150. */
  151. void MR_PopulateDrvRaidMap(struct megasas_instance *instance)
  152. {
  153. struct fusion_context *fusion = instance->ctrl_context;
  154. struct MR_FW_RAID_MAP_ALL *fw_map_old = NULL;
  155. struct MR_FW_RAID_MAP *pFwRaidMap = NULL;
  156. int i;
  157. u16 ld_count;
  158. struct MR_DRV_RAID_MAP_ALL *drv_map =
  159. fusion->ld_drv_map[(instance->map_id & 1)];
  160. struct MR_DRV_RAID_MAP *pDrvRaidMap = &drv_map->raidMap;
  161. if (instance->supportmax256vd) {
  162. memcpy(fusion->ld_drv_map[instance->map_id & 1],
  163. fusion->ld_map[instance->map_id & 1],
  164. fusion->current_map_sz);
  165. /* New Raid map will not set totalSize, so keep expected value
  166. * for legacy code in ValidateMapInfo
  167. */
  168. pDrvRaidMap->totalSize =
  169. cpu_to_le32(sizeof(struct MR_FW_RAID_MAP_EXT));
  170. } else {
  171. fw_map_old = (struct MR_FW_RAID_MAP_ALL *)
  172. fusion->ld_map[(instance->map_id & 1)];
  173. pFwRaidMap = &fw_map_old->raidMap;
  174. ld_count = (u16)le32_to_cpu(pFwRaidMap->ldCount);
  175. #if VD_EXT_DEBUG
  176. for (i = 0; i < ld_count; i++) {
  177. dev_dbg(&instance->pdev->dev, "(%d) :Index 0x%x "
  178. "Target Id 0x%x Seq Num 0x%x Size 0/%llx\n",
  179. instance->unique_id, i,
  180. fw_map_old->raidMap.ldSpanMap[i].ldRaid.targetId,
  181. fw_map_old->raidMap.ldSpanMap[i].ldRaid.seqNum,
  182. fw_map_old->raidMap.ldSpanMap[i].ldRaid.size);
  183. }
  184. #endif
  185. memset(drv_map, 0, fusion->drv_map_sz);
  186. pDrvRaidMap->totalSize = pFwRaidMap->totalSize;
  187. pDrvRaidMap->ldCount = (__le16)cpu_to_le16(ld_count);
  188. pDrvRaidMap->fpPdIoTimeoutSec = pFwRaidMap->fpPdIoTimeoutSec;
  189. for (i = 0; i < MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS; i++)
  190. pDrvRaidMap->ldTgtIdToLd[i] =
  191. (u8)pFwRaidMap->ldTgtIdToLd[i];
  192. for (i = (MAX_RAIDMAP_LOGICAL_DRIVES + MAX_RAIDMAP_VIEWS);
  193. i < MAX_LOGICAL_DRIVES_EXT; i++)
  194. pDrvRaidMap->ldTgtIdToLd[i] = 0xff;
  195. for (i = 0; i < ld_count; i++) {
  196. pDrvRaidMap->ldSpanMap[i] = pFwRaidMap->ldSpanMap[i];
  197. #if VD_EXT_DEBUG
  198. dev_dbg(&instance->pdev->dev,
  199. "pFwRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
  200. "pFwRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
  201. "size 0x%x\n", i, i,
  202. pFwRaidMap->ldSpanMap[i].ldRaid.targetId,
  203. pFwRaidMap->ldSpanMap[i].ldRaid.seqNum,
  204. (u32)pFwRaidMap->ldSpanMap[i].ldRaid.rowSize);
  205. dev_dbg(&instance->pdev->dev,
  206. "pDrvRaidMap->ldSpanMap[%d].ldRaid.targetId 0x%x "
  207. "pDrvRaidMap->ldSpanMap[%d].ldRaid.seqNum 0x%x "
  208. "size 0x%x\n", i, i,
  209. pDrvRaidMap->ldSpanMap[i].ldRaid.targetId,
  210. pDrvRaidMap->ldSpanMap[i].ldRaid.seqNum,
  211. (u32)pDrvRaidMap->ldSpanMap[i].ldRaid.rowSize);
  212. dev_dbg(&instance->pdev->dev, "Driver raid map all %p "
  213. "raid map %p LD RAID MAP %p/%p\n", drv_map,
  214. pDrvRaidMap, &pFwRaidMap->ldSpanMap[i].ldRaid,
  215. &pDrvRaidMap->ldSpanMap[i].ldRaid);
  216. #endif
  217. }
  218. memcpy(pDrvRaidMap->arMapInfo, pFwRaidMap->arMapInfo,
  219. sizeof(struct MR_ARRAY_INFO) * MAX_RAIDMAP_ARRAYS);
  220. memcpy(pDrvRaidMap->devHndlInfo, pFwRaidMap->devHndlInfo,
  221. sizeof(struct MR_DEV_HANDLE_INFO) *
  222. MAX_RAIDMAP_PHYSICAL_DEVICES);
  223. }
  224. }
  225. /*
  226. * This function will validate Map info data provided by FW
  227. */
  228. u8 MR_ValidateMapInfo(struct megasas_instance *instance)
  229. {
  230. struct fusion_context *fusion;
  231. struct MR_DRV_RAID_MAP_ALL *drv_map;
  232. struct MR_DRV_RAID_MAP *pDrvRaidMap;
  233. struct LD_LOAD_BALANCE_INFO *lbInfo;
  234. PLD_SPAN_INFO ldSpanInfo;
  235. struct MR_LD_RAID *raid;
  236. u16 ldCount, num_lds;
  237. u16 ld;
  238. u32 expected_size;
  239. MR_PopulateDrvRaidMap(instance);
  240. fusion = instance->ctrl_context;
  241. drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
  242. pDrvRaidMap = &drv_map->raidMap;
  243. lbInfo = fusion->load_balance_info;
  244. ldSpanInfo = fusion->log_to_span;
  245. if (instance->supportmax256vd)
  246. expected_size = sizeof(struct MR_FW_RAID_MAP_EXT);
  247. else
  248. expected_size =
  249. (sizeof(struct MR_FW_RAID_MAP) - sizeof(struct MR_LD_SPAN_MAP) +
  250. (sizeof(struct MR_LD_SPAN_MAP) * le16_to_cpu(pDrvRaidMap->ldCount)));
  251. if (le32_to_cpu(pDrvRaidMap->totalSize) != expected_size) {
  252. dev_err(&instance->pdev->dev, "map info structure size 0x%x is not matching with ld count\n",
  253. (unsigned int) expected_size);
  254. dev_err(&instance->pdev->dev, "megasas: span map %x, pDrvRaidMap->totalSize : %x\n",
  255. (unsigned int)sizeof(struct MR_LD_SPAN_MAP),
  256. le32_to_cpu(pDrvRaidMap->totalSize));
  257. return 0;
  258. }
  259. if (instance->UnevenSpanSupport)
  260. mr_update_span_set(drv_map, ldSpanInfo);
  261. mr_update_load_balance_params(drv_map, lbInfo);
  262. num_lds = le16_to_cpu(drv_map->raidMap.ldCount);
  263. /*Convert Raid capability values to CPU arch */
  264. for (ldCount = 0; ldCount < num_lds; ldCount++) {
  265. ld = MR_TargetIdToLdGet(ldCount, drv_map);
  266. raid = MR_LdRaidGet(ld, drv_map);
  267. le32_to_cpus((u32 *)&raid->capability);
  268. }
  269. return 1;
  270. }
  271. u32 MR_GetSpanBlock(u32 ld, u64 row, u64 *span_blk,
  272. struct MR_DRV_RAID_MAP_ALL *map)
  273. {
  274. struct MR_SPAN_BLOCK_INFO *pSpanBlock = MR_LdSpanInfoGet(ld, map);
  275. struct MR_QUAD_ELEMENT *quad;
  276. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  277. u32 span, j;
  278. for (span = 0; span < raid->spanDepth; span++, pSpanBlock++) {
  279. for (j = 0; j < le32_to_cpu(pSpanBlock->block_span_info.noElements); j++) {
  280. quad = &pSpanBlock->block_span_info.quad[j];
  281. if (le32_to_cpu(quad->diff) == 0)
  282. return SPAN_INVALID;
  283. if (le64_to_cpu(quad->logStart) <= row && row <=
  284. le64_to_cpu(quad->logEnd) && (mega_mod64(row - le64_to_cpu(quad->logStart),
  285. le32_to_cpu(quad->diff))) == 0) {
  286. if (span_blk != NULL) {
  287. u64 blk, debugBlk;
  288. blk = mega_div64_32((row-le64_to_cpu(quad->logStart)), le32_to_cpu(quad->diff));
  289. debugBlk = blk;
  290. blk = (blk + le64_to_cpu(quad->offsetInSpan)) << raid->stripeShift;
  291. *span_blk = blk;
  292. }
  293. return span;
  294. }
  295. }
  296. }
  297. return SPAN_INVALID;
  298. }
  299. /*
  300. ******************************************************************************
  301. *
  302. * Function to print info about span set created in driver from FW raid map
  303. *
  304. * Inputs :
  305. * map - LD map
  306. * ldSpanInfo - ldSpanInfo per HBA instance
  307. */
  308. #if SPAN_DEBUG
  309. static int getSpanInfo(struct MR_DRV_RAID_MAP_ALL *map,
  310. PLD_SPAN_INFO ldSpanInfo)
  311. {
  312. u8 span;
  313. u32 element;
  314. struct MR_LD_RAID *raid;
  315. LD_SPAN_SET *span_set;
  316. struct MR_QUAD_ELEMENT *quad;
  317. int ldCount;
  318. u16 ld;
  319. for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
  320. ld = MR_TargetIdToLdGet(ldCount, map);
  321. if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
  322. continue;
  323. raid = MR_LdRaidGet(ld, map);
  324. dev_dbg(&instance->pdev->dev, "LD %x: span_depth=%x\n",
  325. ld, raid->spanDepth);
  326. for (span = 0; span < raid->spanDepth; span++)
  327. dev_dbg(&instance->pdev->dev, "Span=%x,"
  328. " number of quads=%x\n", span,
  329. le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  330. block_span_info.noElements));
  331. for (element = 0; element < MAX_QUAD_DEPTH; element++) {
  332. span_set = &(ldSpanInfo[ld].span_set[element]);
  333. if (span_set->span_row_data_width == 0)
  334. break;
  335. dev_dbg(&instance->pdev->dev, "Span Set %x:"
  336. "width=%x, diff=%x\n", element,
  337. (unsigned int)span_set->span_row_data_width,
  338. (unsigned int)span_set->diff);
  339. dev_dbg(&instance->pdev->dev, "logical LBA"
  340. "start=0x%08lx, end=0x%08lx\n",
  341. (long unsigned int)span_set->log_start_lba,
  342. (long unsigned int)span_set->log_end_lba);
  343. dev_dbg(&instance->pdev->dev, "span row start=0x%08lx,"
  344. " end=0x%08lx\n",
  345. (long unsigned int)span_set->span_row_start,
  346. (long unsigned int)span_set->span_row_end);
  347. dev_dbg(&instance->pdev->dev, "data row start=0x%08lx,"
  348. " end=0x%08lx\n",
  349. (long unsigned int)span_set->data_row_start,
  350. (long unsigned int)span_set->data_row_end);
  351. dev_dbg(&instance->pdev->dev, "data strip start=0x%08lx,"
  352. " end=0x%08lx\n",
  353. (long unsigned int)span_set->data_strip_start,
  354. (long unsigned int)span_set->data_strip_end);
  355. for (span = 0; span < raid->spanDepth; span++) {
  356. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  357. block_span_info.noElements) >=
  358. element + 1) {
  359. quad = &map->raidMap.ldSpanMap[ld].
  360. spanBlock[span].block_span_info.
  361. quad[element];
  362. dev_dbg(&instance->pdev->dev, "Span=%x,"
  363. "Quad=%x, diff=%x\n", span,
  364. element, le32_to_cpu(quad->diff));
  365. dev_dbg(&instance->pdev->dev,
  366. "offset_in_span=0x%08lx\n",
  367. (long unsigned int)le64_to_cpu(quad->offsetInSpan));
  368. dev_dbg(&instance->pdev->dev,
  369. "logical start=0x%08lx, end=0x%08lx\n",
  370. (long unsigned int)le64_to_cpu(quad->logStart),
  371. (long unsigned int)le64_to_cpu(quad->logEnd));
  372. }
  373. }
  374. }
  375. }
  376. return 0;
  377. }
  378. #endif
  379. /*
  380. ******************************************************************************
  381. *
  382. * This routine calculates the Span block for given row using spanset.
  383. *
  384. * Inputs :
  385. * instance - HBA instance
  386. * ld - Logical drive number
  387. * row - Row number
  388. * map - LD map
  389. *
  390. * Outputs :
  391. *
  392. * span - Span number
  393. * block - Absolute Block number in the physical disk
  394. * div_error - Devide error code.
  395. */
  396. u32 mr_spanset_get_span_block(struct megasas_instance *instance,
  397. u32 ld, u64 row, u64 *span_blk, struct MR_DRV_RAID_MAP_ALL *map)
  398. {
  399. struct fusion_context *fusion = instance->ctrl_context;
  400. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  401. LD_SPAN_SET *span_set;
  402. struct MR_QUAD_ELEMENT *quad;
  403. u32 span, info;
  404. PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
  405. for (info = 0; info < MAX_QUAD_DEPTH; info++) {
  406. span_set = &(ldSpanInfo[ld].span_set[info]);
  407. if (span_set->span_row_data_width == 0)
  408. break;
  409. if (row > span_set->data_row_end)
  410. continue;
  411. for (span = 0; span < raid->spanDepth; span++)
  412. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  413. block_span_info.noElements) >= info+1) {
  414. quad = &map->raidMap.ldSpanMap[ld].
  415. spanBlock[span].
  416. block_span_info.quad[info];
  417. if (le32_to_cpu(quad->diff) == 0)
  418. return SPAN_INVALID;
  419. if (le64_to_cpu(quad->logStart) <= row &&
  420. row <= le64_to_cpu(quad->logEnd) &&
  421. (mega_mod64(row - le64_to_cpu(quad->logStart),
  422. le32_to_cpu(quad->diff))) == 0) {
  423. if (span_blk != NULL) {
  424. u64 blk;
  425. blk = mega_div64_32
  426. ((row - le64_to_cpu(quad->logStart)),
  427. le32_to_cpu(quad->diff));
  428. blk = (blk + le64_to_cpu(quad->offsetInSpan))
  429. << raid->stripeShift;
  430. *span_blk = blk;
  431. }
  432. return span;
  433. }
  434. }
  435. }
  436. return SPAN_INVALID;
  437. }
  438. /*
  439. ******************************************************************************
  440. *
  441. * This routine calculates the row for given strip using spanset.
  442. *
  443. * Inputs :
  444. * instance - HBA instance
  445. * ld - Logical drive number
  446. * Strip - Strip
  447. * map - LD map
  448. *
  449. * Outputs :
  450. *
  451. * row - row associated with strip
  452. */
  453. static u64 get_row_from_strip(struct megasas_instance *instance,
  454. u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
  455. {
  456. struct fusion_context *fusion = instance->ctrl_context;
  457. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  458. LD_SPAN_SET *span_set;
  459. PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
  460. u32 info, strip_offset, span, span_offset;
  461. u64 span_set_Strip, span_set_Row, retval;
  462. for (info = 0; info < MAX_QUAD_DEPTH; info++) {
  463. span_set = &(ldSpanInfo[ld].span_set[info]);
  464. if (span_set->span_row_data_width == 0)
  465. break;
  466. if (strip > span_set->data_strip_end)
  467. continue;
  468. span_set_Strip = strip - span_set->data_strip_start;
  469. strip_offset = mega_mod64(span_set_Strip,
  470. span_set->span_row_data_width);
  471. span_set_Row = mega_div64_32(span_set_Strip,
  472. span_set->span_row_data_width) * span_set->diff;
  473. for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
  474. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  475. block_span_info.noElements) >= info+1) {
  476. if (strip_offset >=
  477. span_set->strip_offset[span])
  478. span_offset++;
  479. else
  480. break;
  481. }
  482. #if SPAN_DEBUG
  483. dev_info(&instance->pdev->dev, "Strip 0x%llx,"
  484. "span_set_Strip 0x%llx, span_set_Row 0x%llx"
  485. "data width 0x%llx span offset 0x%x\n", strip,
  486. (unsigned long long)span_set_Strip,
  487. (unsigned long long)span_set_Row,
  488. (unsigned long long)span_set->span_row_data_width,
  489. span_offset);
  490. dev_info(&instance->pdev->dev, "For strip 0x%llx"
  491. "row is 0x%llx\n", strip,
  492. (unsigned long long) span_set->data_row_start +
  493. (unsigned long long) span_set_Row + (span_offset - 1));
  494. #endif
  495. retval = (span_set->data_row_start + span_set_Row +
  496. (span_offset - 1));
  497. return retval;
  498. }
  499. return -1LLU;
  500. }
  501. /*
  502. ******************************************************************************
  503. *
  504. * This routine calculates the Start Strip for given row using spanset.
  505. *
  506. * Inputs :
  507. * instance - HBA instance
  508. * ld - Logical drive number
  509. * row - Row number
  510. * map - LD map
  511. *
  512. * Outputs :
  513. *
  514. * Strip - Start strip associated with row
  515. */
  516. static u64 get_strip_from_row(struct megasas_instance *instance,
  517. u32 ld, u64 row, struct MR_DRV_RAID_MAP_ALL *map)
  518. {
  519. struct fusion_context *fusion = instance->ctrl_context;
  520. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  521. LD_SPAN_SET *span_set;
  522. struct MR_QUAD_ELEMENT *quad;
  523. PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
  524. u32 span, info;
  525. u64 strip;
  526. for (info = 0; info < MAX_QUAD_DEPTH; info++) {
  527. span_set = &(ldSpanInfo[ld].span_set[info]);
  528. if (span_set->span_row_data_width == 0)
  529. break;
  530. if (row > span_set->data_row_end)
  531. continue;
  532. for (span = 0; span < raid->spanDepth; span++)
  533. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  534. block_span_info.noElements) >= info+1) {
  535. quad = &map->raidMap.ldSpanMap[ld].
  536. spanBlock[span].block_span_info.quad[info];
  537. if (le64_to_cpu(quad->logStart) <= row &&
  538. row <= le64_to_cpu(quad->logEnd) &&
  539. mega_mod64((row - le64_to_cpu(quad->logStart)),
  540. le32_to_cpu(quad->diff)) == 0) {
  541. strip = mega_div64_32
  542. (((row - span_set->data_row_start)
  543. - le64_to_cpu(quad->logStart)),
  544. le32_to_cpu(quad->diff));
  545. strip *= span_set->span_row_data_width;
  546. strip += span_set->data_strip_start;
  547. strip += span_set->strip_offset[span];
  548. return strip;
  549. }
  550. }
  551. }
  552. dev_err(&instance->pdev->dev, "get_strip_from_row"
  553. "returns invalid strip for ld=%x, row=%lx\n",
  554. ld, (long unsigned int)row);
  555. return -1;
  556. }
  557. /*
  558. ******************************************************************************
  559. *
  560. * This routine calculates the Physical Arm for given strip using spanset.
  561. *
  562. * Inputs :
  563. * instance - HBA instance
  564. * ld - Logical drive number
  565. * strip - Strip
  566. * map - LD map
  567. *
  568. * Outputs :
  569. *
  570. * Phys Arm - Phys Arm associated with strip
  571. */
  572. static u32 get_arm_from_strip(struct megasas_instance *instance,
  573. u32 ld, u64 strip, struct MR_DRV_RAID_MAP_ALL *map)
  574. {
  575. struct fusion_context *fusion = instance->ctrl_context;
  576. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  577. LD_SPAN_SET *span_set;
  578. PLD_SPAN_INFO ldSpanInfo = fusion->log_to_span;
  579. u32 info, strip_offset, span, span_offset, retval;
  580. for (info = 0 ; info < MAX_QUAD_DEPTH; info++) {
  581. span_set = &(ldSpanInfo[ld].span_set[info]);
  582. if (span_set->span_row_data_width == 0)
  583. break;
  584. if (strip > span_set->data_strip_end)
  585. continue;
  586. strip_offset = (uint)mega_mod64
  587. ((strip - span_set->data_strip_start),
  588. span_set->span_row_data_width);
  589. for (span = 0, span_offset = 0; span < raid->spanDepth; span++)
  590. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  591. block_span_info.noElements) >= info+1) {
  592. if (strip_offset >=
  593. span_set->strip_offset[span])
  594. span_offset =
  595. span_set->strip_offset[span];
  596. else
  597. break;
  598. }
  599. #if SPAN_DEBUG
  600. dev_info(&instance->pdev->dev, "get_arm_from_strip:"
  601. "for ld=0x%x strip=0x%lx arm is 0x%x\n", ld,
  602. (long unsigned int)strip, (strip_offset - span_offset));
  603. #endif
  604. retval = (strip_offset - span_offset);
  605. return retval;
  606. }
  607. dev_err(&instance->pdev->dev, "get_arm_from_strip"
  608. "returns invalid arm for ld=%x strip=%lx\n",
  609. ld, (long unsigned int)strip);
  610. return -1;
  611. }
  612. /* This Function will return Phys arm */
  613. u8 get_arm(struct megasas_instance *instance, u32 ld, u8 span, u64 stripe,
  614. struct MR_DRV_RAID_MAP_ALL *map)
  615. {
  616. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  617. /* Need to check correct default value */
  618. u32 arm = 0;
  619. switch (raid->level) {
  620. case 0:
  621. case 5:
  622. case 6:
  623. arm = mega_mod64(stripe, SPAN_ROW_SIZE(map, ld, span));
  624. break;
  625. case 1:
  626. /* start with logical arm */
  627. arm = get_arm_from_strip(instance, ld, stripe, map);
  628. if (arm != -1U)
  629. arm *= 2;
  630. break;
  631. }
  632. return arm;
  633. }
  634. /*
  635. ******************************************************************************
  636. *
  637. * This routine calculates the arm, span and block for the specified stripe and
  638. * reference in stripe using spanset
  639. *
  640. * Inputs :
  641. *
  642. * ld - Logical drive number
  643. * stripRow - Stripe number
  644. * stripRef - Reference in stripe
  645. *
  646. * Outputs :
  647. *
  648. * span - Span number
  649. * block - Absolute Block number in the physical disk
  650. */
  651. static u8 mr_spanset_get_phy_params(struct megasas_instance *instance, u32 ld,
  652. u64 stripRow, u16 stripRef, struct IO_REQUEST_INFO *io_info,
  653. struct RAID_CONTEXT *pRAID_Context,
  654. struct MR_DRV_RAID_MAP_ALL *map)
  655. {
  656. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  657. u32 pd, arRef;
  658. u8 physArm, span;
  659. u64 row;
  660. u8 retval = TRUE;
  661. u64 *pdBlock = &io_info->pdBlock;
  662. __le16 *pDevHandle = &io_info->devHandle;
  663. u32 logArm, rowMod, armQ, arm;
  664. struct fusion_context *fusion;
  665. fusion = instance->ctrl_context;
  666. /*Get row and span from io_info for Uneven Span IO.*/
  667. row = io_info->start_row;
  668. span = io_info->start_span;
  669. if (raid->level == 6) {
  670. logArm = get_arm_from_strip(instance, ld, stripRow, map);
  671. if (logArm == -1U)
  672. return FALSE;
  673. rowMod = mega_mod64(row, SPAN_ROW_SIZE(map, ld, span));
  674. armQ = SPAN_ROW_SIZE(map, ld, span) - 1 - rowMod;
  675. arm = armQ + 1 + logArm;
  676. if (arm >= SPAN_ROW_SIZE(map, ld, span))
  677. arm -= SPAN_ROW_SIZE(map, ld, span);
  678. physArm = (u8)arm;
  679. } else
  680. /* Calculate the arm */
  681. physArm = get_arm(instance, ld, span, stripRow, map);
  682. if (physArm == 0xFF)
  683. return FALSE;
  684. arRef = MR_LdSpanArrayGet(ld, span, map);
  685. pd = MR_ArPdGet(arRef, physArm, map);
  686. if (pd != MR_PD_INVALID)
  687. *pDevHandle = MR_PdDevHandleGet(pd, map);
  688. else {
  689. *pDevHandle = cpu_to_le16(MR_PD_INVALID);
  690. if ((raid->level >= 5) &&
  691. ((fusion->adapter_type == THUNDERBOLT_SERIES) ||
  692. ((fusion->adapter_type == INVADER_SERIES) &&
  693. (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
  694. pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
  695. else if (raid->level == 1) {
  696. pd = MR_ArPdGet(arRef, physArm + 1, map);
  697. if (pd != MR_PD_INVALID)
  698. *pDevHandle = MR_PdDevHandleGet(pd, map);
  699. }
  700. }
  701. *pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
  702. pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
  703. physArm;
  704. io_info->span_arm = pRAID_Context->spanArm;
  705. return retval;
  706. }
  707. /*
  708. ******************************************************************************
  709. *
  710. * This routine calculates the arm, span and block for the specified stripe and
  711. * reference in stripe.
  712. *
  713. * Inputs :
  714. *
  715. * ld - Logical drive number
  716. * stripRow - Stripe number
  717. * stripRef - Reference in stripe
  718. *
  719. * Outputs :
  720. *
  721. * span - Span number
  722. * block - Absolute Block number in the physical disk
  723. */
  724. u8 MR_GetPhyParams(struct megasas_instance *instance, u32 ld, u64 stripRow,
  725. u16 stripRef, struct IO_REQUEST_INFO *io_info,
  726. struct RAID_CONTEXT *pRAID_Context,
  727. struct MR_DRV_RAID_MAP_ALL *map)
  728. {
  729. struct MR_LD_RAID *raid = MR_LdRaidGet(ld, map);
  730. u32 pd, arRef;
  731. u8 physArm, span;
  732. u64 row;
  733. u8 retval = TRUE;
  734. u64 *pdBlock = &io_info->pdBlock;
  735. __le16 *pDevHandle = &io_info->devHandle;
  736. struct fusion_context *fusion;
  737. fusion = instance->ctrl_context;
  738. row = mega_div64_32(stripRow, raid->rowDataSize);
  739. if (raid->level == 6) {
  740. /* logical arm within row */
  741. u32 logArm = mega_mod64(stripRow, raid->rowDataSize);
  742. u32 rowMod, armQ, arm;
  743. if (raid->rowSize == 0)
  744. return FALSE;
  745. /* get logical row mod */
  746. rowMod = mega_mod64(row, raid->rowSize);
  747. armQ = raid->rowSize-1-rowMod; /* index of Q drive */
  748. arm = armQ+1+logArm; /* data always logically follows Q */
  749. if (arm >= raid->rowSize) /* handle wrap condition */
  750. arm -= raid->rowSize;
  751. physArm = (u8)arm;
  752. } else {
  753. if (raid->modFactor == 0)
  754. return FALSE;
  755. physArm = MR_LdDataArmGet(ld, mega_mod64(stripRow,
  756. raid->modFactor),
  757. map);
  758. }
  759. if (raid->spanDepth == 1) {
  760. span = 0;
  761. *pdBlock = row << raid->stripeShift;
  762. } else {
  763. span = (u8)MR_GetSpanBlock(ld, row, pdBlock, map);
  764. if (span == SPAN_INVALID)
  765. return FALSE;
  766. }
  767. /* Get the array on which this span is present */
  768. arRef = MR_LdSpanArrayGet(ld, span, map);
  769. pd = MR_ArPdGet(arRef, physArm, map); /* Get the pd */
  770. if (pd != MR_PD_INVALID)
  771. /* Get dev handle from Pd. */
  772. *pDevHandle = MR_PdDevHandleGet(pd, map);
  773. else {
  774. /* set dev handle as invalid. */
  775. *pDevHandle = cpu_to_le16(MR_PD_INVALID);
  776. if ((raid->level >= 5) &&
  777. ((fusion->adapter_type == THUNDERBOLT_SERIES) ||
  778. ((fusion->adapter_type == INVADER_SERIES) &&
  779. (raid->regTypeReqOnRead != REGION_TYPE_UNUSED))))
  780. pRAID_Context->regLockFlags = REGION_TYPE_EXCLUSIVE;
  781. else if (raid->level == 1) {
  782. /* Get alternate Pd. */
  783. pd = MR_ArPdGet(arRef, physArm + 1, map);
  784. if (pd != MR_PD_INVALID)
  785. /* Get dev handle from Pd */
  786. *pDevHandle = MR_PdDevHandleGet(pd, map);
  787. }
  788. }
  789. *pdBlock += stripRef + le64_to_cpu(MR_LdSpanPtrGet(ld, span, map)->startBlk);
  790. pRAID_Context->spanArm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) |
  791. physArm;
  792. io_info->span_arm = pRAID_Context->spanArm;
  793. return retval;
  794. }
  795. /*
  796. ******************************************************************************
  797. *
  798. * MR_BuildRaidContext function
  799. *
  800. * This function will initiate command processing. The start/end row and strip
  801. * information is calculated then the lock is acquired.
  802. * This function will return 0 if region lock was acquired OR return num strips
  803. */
  804. u8
  805. MR_BuildRaidContext(struct megasas_instance *instance,
  806. struct IO_REQUEST_INFO *io_info,
  807. struct RAID_CONTEXT *pRAID_Context,
  808. struct MR_DRV_RAID_MAP_ALL *map, u8 **raidLUN)
  809. {
  810. struct fusion_context *fusion;
  811. struct MR_LD_RAID *raid;
  812. u32 ld, stripSize, stripe_mask;
  813. u64 endLba, endStrip, endRow, start_row, start_strip;
  814. u64 regStart;
  815. u32 regSize;
  816. u8 num_strips, numRows;
  817. u16 ref_in_start_stripe, ref_in_end_stripe;
  818. u64 ldStartBlock;
  819. u32 numBlocks, ldTgtId;
  820. u8 isRead;
  821. u8 retval = 0;
  822. u8 startlba_span = SPAN_INVALID;
  823. u64 *pdBlock = &io_info->pdBlock;
  824. ldStartBlock = io_info->ldStartBlock;
  825. numBlocks = io_info->numBlocks;
  826. ldTgtId = io_info->ldTgtId;
  827. isRead = io_info->isRead;
  828. io_info->IoforUnevenSpan = 0;
  829. io_info->start_span = SPAN_INVALID;
  830. fusion = instance->ctrl_context;
  831. ld = MR_TargetIdToLdGet(ldTgtId, map);
  832. raid = MR_LdRaidGet(ld, map);
  833. /*
  834. * if rowDataSize @RAID map and spanRowDataSize @SPAN INFO are zero
  835. * return FALSE
  836. */
  837. if (raid->rowDataSize == 0) {
  838. if (MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize == 0)
  839. return FALSE;
  840. else if (instance->UnevenSpanSupport) {
  841. io_info->IoforUnevenSpan = 1;
  842. } else {
  843. dev_info(&instance->pdev->dev,
  844. "raid->rowDataSize is 0, but has SPAN[0]"
  845. "rowDataSize = 0x%0x,"
  846. "but there is _NO_ UnevenSpanSupport\n",
  847. MR_LdSpanPtrGet(ld, 0, map)->spanRowDataSize);
  848. return FALSE;
  849. }
  850. }
  851. stripSize = 1 << raid->stripeShift;
  852. stripe_mask = stripSize-1;
  853. /*
  854. * calculate starting row and stripe, and number of strips and rows
  855. */
  856. start_strip = ldStartBlock >> raid->stripeShift;
  857. ref_in_start_stripe = (u16)(ldStartBlock & stripe_mask);
  858. endLba = ldStartBlock + numBlocks - 1;
  859. ref_in_end_stripe = (u16)(endLba & stripe_mask);
  860. endStrip = endLba >> raid->stripeShift;
  861. num_strips = (u8)(endStrip - start_strip + 1); /* End strip */
  862. if (io_info->IoforUnevenSpan) {
  863. start_row = get_row_from_strip(instance, ld, start_strip, map);
  864. endRow = get_row_from_strip(instance, ld, endStrip, map);
  865. if (start_row == -1ULL || endRow == -1ULL) {
  866. dev_info(&instance->pdev->dev, "return from %s %d."
  867. "Send IO w/o region lock.\n",
  868. __func__, __LINE__);
  869. return FALSE;
  870. }
  871. if (raid->spanDepth == 1) {
  872. startlba_span = 0;
  873. *pdBlock = start_row << raid->stripeShift;
  874. } else
  875. startlba_span = (u8)mr_spanset_get_span_block(instance,
  876. ld, start_row, pdBlock, map);
  877. if (startlba_span == SPAN_INVALID) {
  878. dev_info(&instance->pdev->dev, "return from %s %d"
  879. "for row 0x%llx,start strip %llx"
  880. "endSrip %llx\n", __func__, __LINE__,
  881. (unsigned long long)start_row,
  882. (unsigned long long)start_strip,
  883. (unsigned long long)endStrip);
  884. return FALSE;
  885. }
  886. io_info->start_span = startlba_span;
  887. io_info->start_row = start_row;
  888. #if SPAN_DEBUG
  889. dev_dbg(&instance->pdev->dev, "Check Span number from %s %d"
  890. "for row 0x%llx, start strip 0x%llx end strip 0x%llx"
  891. " span 0x%x\n", __func__, __LINE__,
  892. (unsigned long long)start_row,
  893. (unsigned long long)start_strip,
  894. (unsigned long long)endStrip, startlba_span);
  895. dev_dbg(&instance->pdev->dev, "start_row 0x%llx endRow 0x%llx"
  896. "Start span 0x%x\n", (unsigned long long)start_row,
  897. (unsigned long long)endRow, startlba_span);
  898. #endif
  899. } else {
  900. start_row = mega_div64_32(start_strip, raid->rowDataSize);
  901. endRow = mega_div64_32(endStrip, raid->rowDataSize);
  902. }
  903. numRows = (u8)(endRow - start_row + 1);
  904. /*
  905. * calculate region info.
  906. */
  907. /* assume region is at the start of the first row */
  908. regStart = start_row << raid->stripeShift;
  909. /* assume this IO needs the full row - we'll adjust if not true */
  910. regSize = stripSize;
  911. /* Check if we can send this I/O via FastPath */
  912. if (raid->capability.fpCapable) {
  913. if (isRead)
  914. io_info->fpOkForIo = (raid->capability.fpReadCapable &&
  915. ((num_strips == 1) ||
  916. raid->capability.
  917. fpReadAcrossStripe));
  918. else
  919. io_info->fpOkForIo = (raid->capability.fpWriteCapable &&
  920. ((num_strips == 1) ||
  921. raid->capability.
  922. fpWriteAcrossStripe));
  923. } else
  924. io_info->fpOkForIo = FALSE;
  925. if (numRows == 1) {
  926. /* single-strip IOs can always lock only the data needed */
  927. if (num_strips == 1) {
  928. regStart += ref_in_start_stripe;
  929. regSize = numBlocks;
  930. }
  931. /* multi-strip IOs always need to full stripe locked */
  932. } else if (io_info->IoforUnevenSpan == 0) {
  933. /*
  934. * For Even span region lock optimization.
  935. * If the start strip is the last in the start row
  936. */
  937. if (start_strip == (start_row + 1) * raid->rowDataSize - 1) {
  938. regStart += ref_in_start_stripe;
  939. /* initialize count to sectors from startref to end
  940. of strip */
  941. regSize = stripSize - ref_in_start_stripe;
  942. }
  943. /* add complete rows in the middle of the transfer */
  944. if (numRows > 2)
  945. regSize += (numRows-2) << raid->stripeShift;
  946. /* if IO ends within first strip of last row*/
  947. if (endStrip == endRow*raid->rowDataSize)
  948. regSize += ref_in_end_stripe+1;
  949. else
  950. regSize += stripSize;
  951. } else {
  952. /*
  953. * For Uneven span region lock optimization.
  954. * If the start strip is the last in the start row
  955. */
  956. if (start_strip == (get_strip_from_row(instance, ld, start_row, map) +
  957. SPAN_ROW_DATA_SIZE(map, ld, startlba_span) - 1)) {
  958. regStart += ref_in_start_stripe;
  959. /* initialize count to sectors from
  960. * startRef to end of strip
  961. */
  962. regSize = stripSize - ref_in_start_stripe;
  963. }
  964. /* Add complete rows in the middle of the transfer*/
  965. if (numRows > 2)
  966. /* Add complete rows in the middle of the transfer*/
  967. regSize += (numRows-2) << raid->stripeShift;
  968. /* if IO ends within first strip of last row */
  969. if (endStrip == get_strip_from_row(instance, ld, endRow, map))
  970. regSize += ref_in_end_stripe + 1;
  971. else
  972. regSize += stripSize;
  973. }
  974. pRAID_Context->timeoutValue =
  975. cpu_to_le16(raid->fpIoTimeoutForLd ?
  976. raid->fpIoTimeoutForLd :
  977. map->raidMap.fpPdIoTimeoutSec);
  978. if (fusion->adapter_type == INVADER_SERIES)
  979. pRAID_Context->regLockFlags = (isRead) ?
  980. raid->regTypeReqOnRead : raid->regTypeReqOnWrite;
  981. else
  982. pRAID_Context->regLockFlags = (isRead) ?
  983. REGION_TYPE_SHARED_READ : raid->regTypeReqOnWrite;
  984. pRAID_Context->VirtualDiskTgtId = raid->targetId;
  985. pRAID_Context->regLockRowLBA = cpu_to_le64(regStart);
  986. pRAID_Context->regLockLength = cpu_to_le32(regSize);
  987. pRAID_Context->configSeqNum = raid->seqNum;
  988. /* save pointer to raid->LUN array */
  989. *raidLUN = raid->LUN;
  990. /*Get Phy Params only if FP capable, or else leave it to MR firmware
  991. to do the calculation.*/
  992. if (io_info->fpOkForIo) {
  993. retval = io_info->IoforUnevenSpan ?
  994. mr_spanset_get_phy_params(instance, ld,
  995. start_strip, ref_in_start_stripe,
  996. io_info, pRAID_Context, map) :
  997. MR_GetPhyParams(instance, ld, start_strip,
  998. ref_in_start_stripe, io_info,
  999. pRAID_Context, map);
  1000. /* If IO on an invalid Pd, then FP is not possible.*/
  1001. if (io_info->devHandle == cpu_to_le16(MR_PD_INVALID))
  1002. io_info->fpOkForIo = FALSE;
  1003. return retval;
  1004. } else if (isRead) {
  1005. uint stripIdx;
  1006. for (stripIdx = 0; stripIdx < num_strips; stripIdx++) {
  1007. retval = io_info->IoforUnevenSpan ?
  1008. mr_spanset_get_phy_params(instance, ld,
  1009. start_strip + stripIdx,
  1010. ref_in_start_stripe, io_info,
  1011. pRAID_Context, map) :
  1012. MR_GetPhyParams(instance, ld,
  1013. start_strip + stripIdx, ref_in_start_stripe,
  1014. io_info, pRAID_Context, map);
  1015. if (!retval)
  1016. return TRUE;
  1017. }
  1018. }
  1019. #if SPAN_DEBUG
  1020. /* Just for testing what arm we get for strip.*/
  1021. if (io_info->IoforUnevenSpan)
  1022. get_arm_from_strip(instance, ld, start_strip, map);
  1023. #endif
  1024. return TRUE;
  1025. }
  1026. /*
  1027. ******************************************************************************
  1028. *
  1029. * This routine pepare spanset info from Valid Raid map and store it into
  1030. * local copy of ldSpanInfo per instance data structure.
  1031. *
  1032. * Inputs :
  1033. * map - LD map
  1034. * ldSpanInfo - ldSpanInfo per HBA instance
  1035. *
  1036. */
  1037. void mr_update_span_set(struct MR_DRV_RAID_MAP_ALL *map,
  1038. PLD_SPAN_INFO ldSpanInfo)
  1039. {
  1040. u8 span, count;
  1041. u32 element, span_row_width;
  1042. u64 span_row;
  1043. struct MR_LD_RAID *raid;
  1044. LD_SPAN_SET *span_set, *span_set_prev;
  1045. struct MR_QUAD_ELEMENT *quad;
  1046. int ldCount;
  1047. u16 ld;
  1048. for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
  1049. ld = MR_TargetIdToLdGet(ldCount, map);
  1050. if (ld >= (MAX_LOGICAL_DRIVES_EXT - 1))
  1051. continue;
  1052. raid = MR_LdRaidGet(ld, map);
  1053. for (element = 0; element < MAX_QUAD_DEPTH; element++) {
  1054. for (span = 0; span < raid->spanDepth; span++) {
  1055. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].spanBlock[span].
  1056. block_span_info.noElements) <
  1057. element + 1)
  1058. continue;
  1059. span_set = &(ldSpanInfo[ld].span_set[element]);
  1060. quad = &map->raidMap.ldSpanMap[ld].
  1061. spanBlock[span].block_span_info.
  1062. quad[element];
  1063. span_set->diff = le32_to_cpu(quad->diff);
  1064. for (count = 0, span_row_width = 0;
  1065. count < raid->spanDepth; count++) {
  1066. if (le32_to_cpu(map->raidMap.ldSpanMap[ld].
  1067. spanBlock[count].
  1068. block_span_info.
  1069. noElements) >= element + 1) {
  1070. span_set->strip_offset[count] =
  1071. span_row_width;
  1072. span_row_width +=
  1073. MR_LdSpanPtrGet
  1074. (ld, count, map)->spanRowDataSize;
  1075. }
  1076. }
  1077. span_set->span_row_data_width = span_row_width;
  1078. span_row = mega_div64_32(((le64_to_cpu(quad->logEnd) -
  1079. le64_to_cpu(quad->logStart)) + le32_to_cpu(quad->diff)),
  1080. le32_to_cpu(quad->diff));
  1081. if (element == 0) {
  1082. span_set->log_start_lba = 0;
  1083. span_set->log_end_lba =
  1084. ((span_row << raid->stripeShift)
  1085. * span_row_width) - 1;
  1086. span_set->span_row_start = 0;
  1087. span_set->span_row_end = span_row - 1;
  1088. span_set->data_strip_start = 0;
  1089. span_set->data_strip_end =
  1090. (span_row * span_row_width) - 1;
  1091. span_set->data_row_start = 0;
  1092. span_set->data_row_end =
  1093. (span_row * le32_to_cpu(quad->diff)) - 1;
  1094. } else {
  1095. span_set_prev = &(ldSpanInfo[ld].
  1096. span_set[element - 1]);
  1097. span_set->log_start_lba =
  1098. span_set_prev->log_end_lba + 1;
  1099. span_set->log_end_lba =
  1100. span_set->log_start_lba +
  1101. ((span_row << raid->stripeShift)
  1102. * span_row_width) - 1;
  1103. span_set->span_row_start =
  1104. span_set_prev->span_row_end + 1;
  1105. span_set->span_row_end =
  1106. span_set->span_row_start + span_row - 1;
  1107. span_set->data_strip_start =
  1108. span_set_prev->data_strip_end + 1;
  1109. span_set->data_strip_end =
  1110. span_set->data_strip_start +
  1111. (span_row * span_row_width) - 1;
  1112. span_set->data_row_start =
  1113. span_set_prev->data_row_end + 1;
  1114. span_set->data_row_end =
  1115. span_set->data_row_start +
  1116. (span_row * le32_to_cpu(quad->diff)) - 1;
  1117. }
  1118. break;
  1119. }
  1120. if (span == raid->spanDepth)
  1121. break;
  1122. }
  1123. }
  1124. #if SPAN_DEBUG
  1125. getSpanInfo(map, ldSpanInfo);
  1126. #endif
  1127. }
  1128. void mr_update_load_balance_params(struct MR_DRV_RAID_MAP_ALL *drv_map,
  1129. struct LD_LOAD_BALANCE_INFO *lbInfo)
  1130. {
  1131. int ldCount;
  1132. u16 ld;
  1133. struct MR_LD_RAID *raid;
  1134. if (lb_pending_cmds > 128 || lb_pending_cmds < 1)
  1135. lb_pending_cmds = LB_PENDING_CMDS_DEFAULT;
  1136. for (ldCount = 0; ldCount < MAX_LOGICAL_DRIVES_EXT; ldCount++) {
  1137. ld = MR_TargetIdToLdGet(ldCount, drv_map);
  1138. if (ld >= MAX_LOGICAL_DRIVES_EXT - 1) {
  1139. lbInfo[ldCount].loadBalanceFlag = 0;
  1140. continue;
  1141. }
  1142. raid = MR_LdRaidGet(ld, drv_map);
  1143. if ((raid->level != 1) ||
  1144. (raid->ldState != MR_LD_STATE_OPTIMAL)) {
  1145. lbInfo[ldCount].loadBalanceFlag = 0;
  1146. continue;
  1147. }
  1148. lbInfo[ldCount].loadBalanceFlag = 1;
  1149. }
  1150. }
  1151. u8 megasas_get_best_arm_pd(struct megasas_instance *instance,
  1152. struct LD_LOAD_BALANCE_INFO *lbInfo, struct IO_REQUEST_INFO *io_info)
  1153. {
  1154. struct fusion_context *fusion;
  1155. struct MR_LD_RAID *raid;
  1156. struct MR_DRV_RAID_MAP_ALL *drv_map;
  1157. u16 pend0, pend1, ld;
  1158. u64 diff0, diff1;
  1159. u8 bestArm, pd0, pd1, span, arm;
  1160. u32 arRef, span_row_size;
  1161. u64 block = io_info->ldStartBlock;
  1162. u32 count = io_info->numBlocks;
  1163. span = ((io_info->span_arm & RAID_CTX_SPANARM_SPAN_MASK)
  1164. >> RAID_CTX_SPANARM_SPAN_SHIFT);
  1165. arm = (io_info->span_arm & RAID_CTX_SPANARM_ARM_MASK);
  1166. fusion = instance->ctrl_context;
  1167. drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
  1168. ld = MR_TargetIdToLdGet(io_info->ldTgtId, drv_map);
  1169. raid = MR_LdRaidGet(ld, drv_map);
  1170. span_row_size = instance->UnevenSpanSupport ?
  1171. SPAN_ROW_SIZE(drv_map, ld, span) : raid->rowSize;
  1172. arRef = MR_LdSpanArrayGet(ld, span, drv_map);
  1173. pd0 = MR_ArPdGet(arRef, arm, drv_map);
  1174. pd1 = MR_ArPdGet(arRef, (arm + 1) >= span_row_size ?
  1175. (arm + 1 - span_row_size) : arm + 1, drv_map);
  1176. /* get the pending cmds for the data and mirror arms */
  1177. pend0 = atomic_read(&lbInfo->scsi_pending_cmds[pd0]);
  1178. pend1 = atomic_read(&lbInfo->scsi_pending_cmds[pd1]);
  1179. /* Determine the disk whose head is nearer to the req. block */
  1180. diff0 = ABS_DIFF(block, lbInfo->last_accessed_block[pd0]);
  1181. diff1 = ABS_DIFF(block, lbInfo->last_accessed_block[pd1]);
  1182. bestArm = (diff0 <= diff1 ? arm : arm ^ 1);
  1183. if ((bestArm == arm && pend0 > pend1 + lb_pending_cmds) ||
  1184. (bestArm != arm && pend1 > pend0 + lb_pending_cmds))
  1185. bestArm ^= 1;
  1186. /* Update the last accessed block on the correct pd */
  1187. io_info->pd_after_lb = (bestArm == arm) ? pd0 : pd1;
  1188. lbInfo->last_accessed_block[io_info->pd_after_lb] = block + count - 1;
  1189. io_info->span_arm = (span << RAID_CTX_SPANARM_SPAN_SHIFT) | bestArm;
  1190. #if SPAN_DEBUG
  1191. if (arm != bestArm)
  1192. dev_dbg(&instance->pdev->dev, "LSI Debug R1 Load balance "
  1193. "occur - span 0x%x arm 0x%x bestArm 0x%x "
  1194. "io_info->span_arm 0x%x\n",
  1195. span, arm, bestArm, io_info->span_arm);
  1196. #endif
  1197. return io_info->pd_after_lb;
  1198. }
  1199. __le16 get_updated_dev_handle(struct megasas_instance *instance,
  1200. struct LD_LOAD_BALANCE_INFO *lbInfo, struct IO_REQUEST_INFO *io_info)
  1201. {
  1202. u8 arm_pd;
  1203. __le16 devHandle;
  1204. struct fusion_context *fusion;
  1205. struct MR_DRV_RAID_MAP_ALL *drv_map;
  1206. fusion = instance->ctrl_context;
  1207. drv_map = fusion->ld_drv_map[(instance->map_id & 1)];
  1208. /* get best new arm (PD ID) */
  1209. arm_pd = megasas_get_best_arm_pd(instance, lbInfo, io_info);
  1210. devHandle = MR_PdDevHandleGet(arm_pd, drv_map);
  1211. atomic_inc(&lbInfo->scsi_pending_cmds[arm_pd]);
  1212. return devHandle;
  1213. }