1/*	$NetBSD: rf_interdecluster.c,v 1.15 2019/02/09 03:34:00 christos Exp $	*/
2/*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Khalil Amiri
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21 *  School of Computer Science
22 *  Carnegie Mellon University
23 *  Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29/************************************************************
30 *
31 * rf_interdecluster.c -- implements interleaved declustering
32 *
33 ************************************************************/
34
35#include <sys/cdefs.h>
36__KERNEL_RCSID(0, "$NetBSD: rf_interdecluster.c,v 1.15 2019/02/09 03:34:00 christos Exp $");
37
38#include "rf_archs.h"
39
40#if RF_INCLUDE_INTERDECLUSTER > 0
41
42#include <dev/raidframe/raidframevar.h>
43
44#include "rf_raid.h"
45#include "rf_interdecluster.h"
46#include "rf_dag.h"
47#include "rf_dagutils.h"
48#include "rf_dagfuncs.h"
49#include "rf_general.h"
50#include "rf_utils.h"
51#include "rf_dagffrd.h"
52#include "rf_dagdegrd.h"
53#include "rf_dagffwr.h"
54#include "rf_dagdegwr.h"
55
56typedef struct RF_InterdeclusterConfigInfo_s {
57	RF_RowCol_t **stripeIdentifier;	/* filled in at config time and used
58					 * by IdentifyStripe */
59	RF_StripeCount_t numSparingRegions;
60	RF_StripeCount_t stripeUnitsPerSparingRegion;
61	RF_SectorNum_t mirrorStripeOffset;
62}       RF_InterdeclusterConfigInfo_t;
63
64int
65rf_ConfigureInterDecluster(
66    RF_ShutdownList_t ** listp,
67    RF_Raid_t * raidPtr,
68    RF_Config_t * cfgPtr)
69{
70	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
71	RF_StripeCount_t num_used_stripeUnitsPerDisk;
72	RF_InterdeclusterConfigInfo_t *info;
73	RF_RowCol_t i, tmp, SUs_per_region;
74
75	/* create an Interleaved Declustering configuration structure */
76	info = RF_MallocAndAdd(sizeof(*info), raidPtr->cleanupList);
77	if (info == NULL)
78		return (ENOMEM);
79	layoutPtr->layoutSpecificInfo = (void *) info;
80
81	/* fill in the config structure.  */
82	SUs_per_region = raidPtr->numCol * (raidPtr->numCol - 1);
83	info->stripeIdentifier = rf_make_2d_array(SUs_per_region, 2, raidPtr->cleanupList);
84	if (info->stripeIdentifier == NULL)
85		return (ENOMEM);
86	for (i = 0; i < SUs_per_region; i++) {
87		info->stripeIdentifier[i][0] = i / (raidPtr->numCol - 1);
88		tmp = i / raidPtr->numCol;
89		info->stripeIdentifier[i][1] = (i + 1 + tmp) % raidPtr->numCol;
90	}
91
92	/* fill in the remaining layout parameters */
93
94	/* total number of stripes should a multiple of 2*numCol: Each sparing
95	 * region consists of 2*numCol stripes: n-1 primary copy, n-1
96	 * secondary copy and 2 for spare .. */
97	num_used_stripeUnitsPerDisk = layoutPtr->stripeUnitsPerDisk - (layoutPtr->stripeUnitsPerDisk %
98	    (2 * raidPtr->numCol));
99	info->numSparingRegions = num_used_stripeUnitsPerDisk / (2 * raidPtr->numCol);
100	/* this is in fact the number of stripe units (that are primary data
101	 * copies) in the sparing region */
102	info->stripeUnitsPerSparingRegion = raidPtr->numCol * (raidPtr->numCol - 1);
103	info->mirrorStripeOffset = info->numSparingRegions * (raidPtr->numCol + 1);
104	layoutPtr->numStripe = info->numSparingRegions * info->stripeUnitsPerSparingRegion;
105	layoutPtr->numDataCol = 1;
106	layoutPtr->dataSectorsPerStripe = layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit;
107	layoutPtr->numParityCol = 1;
108
109	layoutPtr->dataStripeUnitsPerDisk = num_used_stripeUnitsPerDisk;
110
111	raidPtr->sectorsPerDisk =
112	    num_used_stripeUnitsPerDisk * layoutPtr->sectorsPerStripeUnit;
113
114	raidPtr->totalSectors =
115	    (layoutPtr->numStripe) * layoutPtr->sectorsPerStripeUnit;
116
117	layoutPtr->stripeUnitsPerDisk = raidPtr->sectorsPerDisk / layoutPtr->sectorsPerStripeUnit;
118
119	return (0);
120}
121
122int
123rf_GetDefaultNumFloatingReconBuffersInterDecluster(RF_Raid_t * raidPtr)
124{
125	return (30);
126}
127
128RF_HeadSepLimit_t
129rf_GetDefaultHeadSepLimitInterDecluster(RF_Raid_t * raidPtr)
130{
131	return (raidPtr->sectorsPerDisk);
132}
133
134RF_ReconUnitCount_t
135rf_GetNumSpareRUsInterDecluster(
136    RF_Raid_t * raidPtr)
137{
138	RF_InterdeclusterConfigInfo_t *info = (RF_InterdeclusterConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
139
140	return (2 * ((RF_ReconUnitCount_t) info->numSparingRegions));
141	/* the layout uses two stripe units per disk as spare within each
142	 * sparing region */
143}
144/* Maps to the primary copy of the data, i.e. the first mirror pair */
145void
146rf_MapSectorInterDecluster(
147    RF_Raid_t * raidPtr,
148    RF_RaidAddr_t raidSector,
149    RF_RowCol_t * col,
150    RF_SectorNum_t * diskSector,
151    int remap)
152{
153	RF_InterdeclusterConfigInfo_t *info = (RF_InterdeclusterConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
154	RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
155	RF_StripeNum_t su_offset_into_disk, mirror_su_offset_into_disk;
156	RF_StripeNum_t sparing_region_id, index_within_region;
157	int     col_before_remap;
158
159	sparing_region_id = SUID / info->stripeUnitsPerSparingRegion;
160	index_within_region = SUID % info->stripeUnitsPerSparingRegion;
161	su_offset_into_disk = index_within_region % (raidPtr->numCol - 1);
162	mirror_su_offset_into_disk = index_within_region / raidPtr->numCol;
163	col_before_remap = index_within_region / (raidPtr->numCol - 1);
164
165	if (!remap) {
166		*col = col_before_remap;
167		*diskSector = (su_offset_into_disk + ((raidPtr->numCol - 1) * sparing_region_id)) *
168		    raidPtr->Layout.sectorsPerStripeUnit;
169		*diskSector += (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
170	} else {
171		/* remap sector to spare space... */
172		*diskSector = sparing_region_id * (raidPtr->numCol + 1) * raidPtr->Layout.sectorsPerStripeUnit;
173		*diskSector += (raidPtr->numCol - 1) * raidPtr->Layout.sectorsPerStripeUnit;
174		*diskSector += (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
175		*col = (index_within_region + 1 + mirror_su_offset_into_disk) % raidPtr->numCol;
176		*col = (*col + 1) % raidPtr->numCol;
177		if (*col == col_before_remap)
178			*col = (*col + 1) % raidPtr->numCol;
179	}
180}
181/* Maps to the second copy of the mirror pair. */
182void
183rf_MapParityInterDecluster(
184    RF_Raid_t * raidPtr,
185    RF_RaidAddr_t raidSector,
186    RF_RowCol_t * col,
187    RF_SectorNum_t * diskSector,
188    int remap)
189{
190	RF_InterdeclusterConfigInfo_t *info = (RF_InterdeclusterConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
191	RF_StripeNum_t sparing_region_id, index_within_region, mirror_su_offset_into_disk;
192	RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
193	int     col_before_remap;
194
195	sparing_region_id = SUID / info->stripeUnitsPerSparingRegion;
196	index_within_region = SUID % info->stripeUnitsPerSparingRegion;
197	mirror_su_offset_into_disk = index_within_region / raidPtr->numCol;
198	col_before_remap = (index_within_region + 1 + mirror_su_offset_into_disk) % raidPtr->numCol;
199
200	if (!remap) {
201		*col = col_before_remap;
202		*diskSector = info->mirrorStripeOffset * raidPtr->Layout.sectorsPerStripeUnit;
203		*diskSector += sparing_region_id * (raidPtr->numCol - 1) * raidPtr->Layout.sectorsPerStripeUnit;
204		*diskSector += mirror_su_offset_into_disk * raidPtr->Layout.sectorsPerStripeUnit;
205		*diskSector += (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
206	} else {
207		/* remap parity to spare space ... */
208		*diskSector = sparing_region_id * (raidPtr->numCol + 1) * raidPtr->Layout.sectorsPerStripeUnit;
209		*diskSector += (raidPtr->numCol) * raidPtr->Layout.sectorsPerStripeUnit;
210		*diskSector += (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
211		*col = index_within_region / (raidPtr->numCol - 1);
212		*col = (*col + 1) % raidPtr->numCol;
213		if (*col == col_before_remap)
214			*col = (*col + 1) % raidPtr->numCol;
215	}
216}
217
218void
219rf_IdentifyStripeInterDecluster(
220    RF_Raid_t * raidPtr,
221    RF_RaidAddr_t addr,
222    RF_RowCol_t ** diskids)
223{
224	RF_InterdeclusterConfigInfo_t *info = (RF_InterdeclusterConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
225	RF_StripeNum_t SUID;
226
227	SUID = addr / raidPtr->Layout.sectorsPerStripeUnit;
228	SUID = SUID % info->stripeUnitsPerSparingRegion;
229
230	*diskids = info->stripeIdentifier[SUID];
231}
232
233void
234rf_MapSIDToPSIDInterDecluster(
235    RF_RaidLayout_t * layoutPtr,
236    RF_StripeNum_t stripeID,
237    RF_StripeNum_t * psID,
238    RF_ReconUnitNum_t * which_ru)
239{
240	*which_ru = 0;
241	*psID = stripeID;
242}
243/******************************************************************************
244 * select a graph to perform a single-stripe access
245 *
246 * Parameters:  raidPtr    - description of the physical array
247 *              type       - type of operation (read or write) requested
248 *              asmap      - logical & physical addresses for this access
249 *              createFunc - name of function to use to create the graph
250 *****************************************************************************/
251
252void
253rf_RAIDIDagSelect(
254    RF_Raid_t * raidPtr,
255    RF_IoType_t type,
256    RF_AccessStripeMap_t * asmap,
257    RF_VoidFuncPtr * createFunc)
258{
259	RF_ASSERT(RF_IO_IS_R_OR_W(type));
260
261	if (asmap->numDataFailed + asmap->numParityFailed > 1) {
262		RF_ERRORMSG("Multiple disks failed in a single group!  Aborting I/O operation.\n");
263		*createFunc = NULL;
264		return;
265	}
266	*createFunc = (type == RF_IO_TYPE_READ) ? (RF_VoidFuncPtr) rf_CreateFaultFreeReadDAG : (RF_VoidFuncPtr) rf_CreateRaidOneWriteDAG;
267	if (type == RF_IO_TYPE_READ) {
268		if (asmap->numDataFailed == 0)
269			*createFunc = (RF_VoidFuncPtr) rf_CreateMirrorPartitionReadDAG;
270		else
271			*createFunc = (RF_VoidFuncPtr) rf_CreateRaidOneDegradedReadDAG;
272	} else
273		*createFunc = (RF_VoidFuncPtr) rf_CreateRaidOneWriteDAG;
274}
275#endif /* RF_INCLUDE_INTERDECLUSTER > 0 */
276