summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Nelson2016-11-11 17:17:35 -0600
committerSam Nelson2016-11-11 17:17:35 -0600
commit6cb4eec0d3c17149e6ed5910a518132b2e6b3322 (patch)
tree5a0fd03c80c4e57f54339040b700591a99ea146a
parent696a9ec37559b671860ef393194c6032b7b6d9ef (diff)
downloadmpm-transport-6cb4eec0d3c17149e6ed5910a518132b2e6b3322.tar.gz
mpm-transport-6cb4eec0d3c17149e6ed5910a518132b2e6b3322.tar.xz
mpm-transport-6cb4eec0d3c17149e6ed5910a518132b2e6b3322.zip
transport_sharedmem:Update to work with memory sections not with same start address
The memory configuration earlier in mpm_config.json was matched up with the start address in the sysfs entry or devicetree. Now the code is changed to just update the sections based on overlapping sections in the sysfs entry or devicetree. Also cleaned up the structure elements for the sake of code clarity Signed-off-by: Sam Nelson <sam.nelson@ti.com>
-rwxr-xr-xsrc/transport/sharedmem/mpm_transport_sharedmem.c243
-rwxr-xr-xsrc/transport/sharedmem/mpm_transport_sharedmem.h15
2 files changed, 201 insertions, 57 deletions
diff --git a/src/transport/sharedmem/mpm_transport_sharedmem.c b/src/transport/sharedmem/mpm_transport_sharedmem.c
index a76a200..2cd3689 100755
--- a/src/transport/sharedmem/mpm_transport_sharedmem.c
+++ b/src/transport/sharedmem/mpm_transport_sharedmem.c
@@ -48,7 +48,7 @@
48#define MAX_PARAM_VAL_LENGTH 32 48#define MAX_PARAM_VAL_LENGTH 32
49#define HEXA_DECIMAL_BASE 16 49#define HEXA_DECIMAL_BASE 16
50 50
51#define SYSFS_ENTRY_PREFIX /sys/class/misc 51#define SYSFS_ENTRY_PREFIX "/sys/class/misc"
52 52
53static int 53static int
54mpm_transport_get_mem_details (mpm_transport_cfg_t *sp, 54mpm_transport_get_mem_details (mpm_transport_cfg_t *sp,
@@ -84,14 +84,29 @@ mpm_transport_get_mem_details (mpm_transport_cfg_t *sp,
84} 84}
85 85
86/* Function finds the mapping index based on the starting address */ 86/* Function finds the mapping index based on the starting address */
87static int mpm_transport_find_map_index(mpm_transport_sharedmem_t *td, int fd_index, uint64_t global_start_addr) { 87static int mpm_transport_find_map_index(
88 mpm_transport_sharedmem_t *td, int fd_index, uint64_t global_start_addr,
89 uint32_t length, int start_block)
90{
88 91
89 int k; 92 int k;
93#ifdef DEBUG
94 mpm_printf(1, "DEBUG: fd_index %d, global_start_addr 0x%x, length 0x%x num_mem_blocks %d start_block %d\n",
95 fd_index, (uint32_t)global_start_addr, length,
96 td->fd_mem.fd_mem_entry[fd_index].num_mem_blocks, start_block);
97#endif
98 /* Find the mem_block region that is within the range of configured region */
90 99
91 for (k = 0; k < td->fd_mem_block.num_mem_blocks[fd_index]; k++) { 100 for (k = start_block; k < td->fd_mem.fd_mem_entry[fd_index].num_mem_blocks; k++) {
92 if(((uint32_t)global_start_addr >= td->fd_mem_block.mem_block[fd_index][k].base) && 101#ifdef DEBUG
93 ((uint32_t)global_start_addr < (td->fd_mem_block.mem_block[fd_index][k].base 102 mpm_printf(1, "DEBUG: k %d, base addr 0x%x: length 0x%x\n",
94 + td->fd_mem_block.mem_block[fd_index][k].length))) { 103 k, td->fd_mem.fd_mem_entry[fd_index].mem_block[k].base,
104 td->fd_mem.fd_mem_entry[fd_index].mem_block[k].length);
105#endif
106 if((global_start_addr <= td->fd_mem.fd_mem_entry[fd_index].mem_block[k].base)
107 &&
108 ((global_start_addr + length)
109 >= td->fd_mem.fd_mem_entry[fd_index].mem_block[k].base)) {
95#ifdef DEBUG 110#ifdef DEBUG
96 mpm_printf(1, "DEBUG: Success map_index %d, global_start_addr 0x%x: fd_index %d\n", 111 mpm_printf(1, "DEBUG: Success map_index %d, global_start_addr 0x%x: fd_index %d\n",
97 k, (uint32_t)global_start_addr, fd_index); 112 k, (uint32_t)global_start_addr, fd_index);
@@ -102,6 +117,93 @@ static int mpm_transport_find_map_index(mpm_transport_sharedmem_t *td, int fd_in
102 return -1; 117 return -1;
103 118
104} 119}
120static int update_mem_entries(mpm_transport_cfg_t *sp,
121 mpm_transport_sharedmem_t *td, int section_index,
122 int fd_index, int map_index)
123{
124 int map_index2;
125#ifdef DEBUG
126 mpm_printf(1, "DEBUG: section_index:%d, map_index %d, original config: Global addr: 0x%x, length 0x%x, : device Base 0x%x, length 0x%x\n",
127 section_index, map_index, (uint32_t)sp->mmap[section_index].global_addr,
128 (uint32_t)sp->mmap[section_index].length,
129 (uint32_t)td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].base,
130 (uint32_t)td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].length);
131#endif
132
133 map_index2 = map_index;
134 do {
135 /* Also check for additional sections within the range
136 and add as additional entries */
137 /* Now find the matching map section */
138 map_index2 = mpm_transport_find_map_index(td, fd_index,
139 sp->mmap[section_index].global_addr,
140 sp->mmap[section_index].length, map_index2+1);
141 if (map_index2 < 0)
142 break;
143 if (sp->num_mmap >= (MPM_MAX_MMAPS-1)) {
144 mpm_printf(1, "num mmaps exceeded%d\n");
145 return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX;
146
147 }
148 sp->mmap[sp->num_mmap].local_addr = sp->mmap[section_index].local_addr
149 + (td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].base
150 - sp->mmap[section_index].global_addr
151 );
152 if ((td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].base
153 + td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].length)
154 > (sp->mmap[section_index].global_addr + sp->mmap[section_index].length)) {
155 sp->mmap[sp->num_mmap].length
156 = (sp->mmap[section_index].global_addr + sp->mmap[section_index].length)
157 - td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].base;
158 } else {
159 sp->mmap[sp->num_mmap].length
160 = td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].length;
161 }
162
163 sp->mmap[sp->num_mmap].global_addr
164 = td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index2].base;
165 strcpy(sp->mmap[sp->num_mmap].devicename, sp->mmap[section_index].devicename);
166#ifdef DEBUG
167 mpm_printf(1, "DEBUG: local_addr 0x%x global addr 0x%x length 0x%x \n",
168 (uint32_t)sp->mmap[sp->num_mmap].local_addr,
169 (uint32_t)sp->mmap[sp->num_mmap].global_addr,
170 (uint32_t)sp->mmap[sp->num_mmap].length);
171#endif
172 sp->num_mmap++;
173#ifdef DEBUG
174 mpm_printf(1, "DEBUG: Added one more entry %d: \n", sp->num_mmap);
175#endif
176 } while (1);
177
178 /* Need to also update the configured addresses to restrict to what
179 is available in sysfs or device tree entry */
180 /* Override local address with update based on global address */
181 sp->mmap[section_index].local_addr = sp->mmap[section_index].local_addr
182 + (td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].base
183 - sp->mmap[section_index].global_addr);
184 if ((td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].base
185 + td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].length)
186 > (sp->mmap[section_index].global_addr + sp->mmap[section_index].length)) {
187 sp->mmap[section_index].length
188 = (sp->mmap[section_index].global_addr + sp->mmap[section_index].length)
189 - td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].base;
190 } else {
191 /* Override length to length in sysfs or device tree entry */
192 sp->mmap[section_index].length
193 = td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].length;
194 }
195 /* Override global address with available address */
196 sp->mmap[section_index].global_addr
197 = td->fd_mem.fd_mem_entry[fd_index].mem_block[map_index].base;
198
199#ifdef DEBUG
200 mpm_printf(1, "DEBUG: Updating: local_addr 0x%x global addr 0x%x length 0x%x \n",
201 (uint32_t)sp->mmap[section_index].local_addr,
202 (uint32_t)sp->mmap[section_index].global_addr,
203 (uint32_t)sp->mmap[section_index].length);
204#endif
205 return 0;
206}
105 207
106int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *ocfg) 208int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *ocfg)
107{ 209{
@@ -114,7 +216,6 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *
114 struct stat statbuf; 216 struct stat statbuf;
115 char *scratch; 217 char *scratch;
116 char *devname; 218 char *devname;
117 int fd_index;
118 int map_index; 219 int map_index;
119 int ret; 220 int ret;
120 221
@@ -123,45 +224,59 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *
123 mpm_printf(1, "can't allocate memory for transport data \n"); 224 mpm_printf(1, "can't allocate memory for transport data \n");
124 return MPM_TRANSPORT_SHM_OPEN_ERR_CALLOC; 225 return MPM_TRANSPORT_SHM_OPEN_ERR_CALLOC;
125 } 226 }
126 td->fd_mem_block.num_fds = 0; 227 td->fd_mem.num_fds = 0;
127 for (i = 0; i < sp->num_mmap; i++) { 228 for (i = 0; i < sp->num_mmap; i++) {
128 /* check if fd for same device name is already opened */ 229 /* check if fd for same device name is already opened */
129 for (j = 0; j < td->fd_mem_block.num_fds; j++) { 230 for (j = 0; j < td->fd_mem.num_fds; j++) {
130#ifdef DEBUG 231#ifdef DEBUG
131 mpm_printf(1, "DEBUG: string compare i:%d, j:%d, %s : %s\n", 232 mpm_printf(1, "DEBUG: string compare i:%d, j:%d, %s : %s\n",
132 i, j, sp->mmap[i].devicename, sp->mmap[j].devicename); 233 i, j, sp->mmap[i].devicename,
234 td->fd_mem.fd_mem_entry[j].devicename);
133#endif 235#endif
134 if (!strncmp(sp->mmap[i].devicename, sp->mmap[j].devicename, MAX_DEVICE_NAME_LEN)) { 236 if (!strncmp(sp->mmap[i].devicename,
135 td->fd_index[i] = td->fd_index[j]; 237 td->fd_mem.fd_mem_entry[j].devicename,
136 fd_index = td->fd_index[i]; 238 MAX_DEVICE_NAME_LEN)) {
137 map_index = mpm_transport_find_map_index(td, fd_index, sp->mmap[i].global_addr); 239 td->fd_index[i] = j;
240 map_index =
241 mpm_transport_find_map_index(
242 td, j,
243 sp->mmap[i].global_addr,
244 sp->mmap[i].length, 0);
138 if ( map_index < 0 ) { 245 if ( map_index < 0 ) {
139 mpm_printf(1, "Could not find map index for address %llx error map_index %d\n", 246 mpm_printf(1, "Could not find map index for address %llx error map_index %d\n",
140 sp->mmap[i].global_addr, map_index); 247 sp->mmap[i].global_addr, map_index);
141 return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX; 248 return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX;
142 249
143 } 250 }
251 if (((uint32_t)sp->mmap[i].global_addr
252 != (uint32_t)td->fd_mem.fd_mem_entry[j].mem_block[map_index].base)
253 || ((uint32_t)sp->mmap[i].length != (uint32_t)td->fd_mem.fd_mem_entry[j].mem_block[map_index].length)) {
254 ret = update_mem_entries(sp, td, i, j, map_index);
255 if (ret < 0)
256 return ret;
257 }
144 td->map_index[i] = map_index; 258 td->map_index[i] = map_index;
145 break; 259 break;
146 } 260 }
147 } 261 }
148 if (j < i) { 262 if (j < td->fd_mem.num_fds) {
149 continue; 263 continue;
150 } 264 }
151 td->fd_mem_block.fd[td->fd_mem_block.num_fds] = open(sp->mmap[i].devicename, (O_RDWR | O_SYNC)); 265 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].fd =
152 if (td->fd_mem_block.fd[td->fd_mem_block.num_fds] < 0) { 266 open(sp->mmap[i].devicename, (O_RDWR | O_SYNC));
267 if (td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].fd < 0) {
153 mpm_printf(1, "Failed to open %s :Error (%s)", 268 mpm_printf(1, "Failed to open %s :Error (%s)",
154 sp->mmap[i].devicename, strerror(errno)); 269 sp->mmap[i].devicename, strerror(errno));
155 free(td); 270 free(td);
156 return MPM_TRANSPORT_SHM_OPEN_ERR_DEV_OPEN; 271 return MPM_TRANSPORT_SHM_OPEN_ERR_DEV_OPEN;
157 } 272 }
158 td->fd_index[i] = td->fd_mem_block.num_fds; 273 td->fd_index[i] = td->fd_mem.num_fds;
159 devname = strrchr(sp->mmap[i].devicename, '/')+1; 274 devname = strrchr(sp->mmap[i].devicename, '/')+1;
160 275
161 k = 0; 276 k = 0;
162 /* Check first if there are sysfs entries for the memory */ 277 /* Check first if there are sysfs entries for the memory */
163 snprintf(filename, MAX_FILE_NAME_LENGTH, 278 snprintf(filename, MAX_FILE_NAME_LENGTH,
164 "SYSFS_ENTRY_PREFIX""/%s/memory%d/addr", devname, k); 279 SYSFS_ENTRY_PREFIX"/%s/memory%d/addr", devname, k);
165 280
166 if ((fd = open (filename, O_RDONLY)) != -1) { 281 if ((fd = open (filename, O_RDONLY)) != -1) {
167 282
@@ -175,41 +290,52 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *
175 } 290 }
176 close(fd); 291 close(fd);
177 /* And populate mem_block base information */ 292 /* And populate mem_block base information */
178 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base 293 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].base
179 = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE); 294 = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE);
180#ifdef DEBUG 295#ifdef DEBUG
181 mpm_printf(1, "Recording addr 0x%x\n", 296 mpm_printf(1, "Recording addr 0x%x, ",
182 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base); 297 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].base);
183#endif 298#endif
184 snprintf(filename, MAX_FILE_NAME_LENGTH, 299 snprintf(filename, MAX_FILE_NAME_LENGTH,
185 "SYSFS_ENTRY_PREFIX""/%s/memory%d/size", 300 SYSFS_ENTRY_PREFIX"/%s/memory%d/size",
186 devname, k); 301 devname, k);
187 fd = open(filename, O_RDONLY); 302 fd = open(filename, O_RDONLY);
188 if ( fd == -1 ) { 303 if ( fd == -1 ) {
189 mpm_printf(1, "Failed to open \"%s\" err=%s\n", 304 mpm_printf(1, "\nFailed to open \"%s\" err=%s\n",
190 filename, strerror(errno)); 305 filename, strerror(errno));
191 return MPM_TRANSPORT_SHM_OPEN_ERR_MEM_FILE_OPEN; 306 return MPM_TRANSPORT_SHM_OPEN_ERR_MEM_FILE_OPEN;
192 } 307 }
193 308
194 ret = read(fd, hexstring, MAX_PARAM_VAL_LENGTH); 309 ret = read(fd, hexstring, MAX_PARAM_VAL_LENGTH);
195 if ( ret <= 0 ) { 310 if ( ret <= 0 ) {
196 mpm_printf (1, "Failed to read %d bytes from %s: ret %d\n", 311 mpm_printf (1, "\nFailed to read %d bytes from %s: ret %d\n",
197 (int) MAX_PARAM_VAL_LENGTH, filename, ret); 312 (int) MAX_PARAM_VAL_LENGTH, filename, ret);
198 return MPM_TRANSPORT_SHM_OPEN_ERR_READ; 313 return MPM_TRANSPORT_SHM_OPEN_ERR_READ;
199 } 314 }
200 close(fd); 315 close(fd);
201 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length 316 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].length
202 = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE); 317 = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE);
203#ifdef DEBUG 318#ifdef DEBUG
204 mpm_printf(1, "Recording length 0x%x\n", 319 mpm_printf(1, "Recording length 0x%x\n",
205 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length); 320 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].length);
206#endif 321#endif
207 k++; 322 k++;
323 if ( k >= MPM_MAX_MEM_ENTRIES) {
324 mpm_printf(1, "\nNumber of mem entries: %d exceed max allowed %d\n",
325 k, MPM_MAX_MEM_ENTRIES);
326 return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX;
327 }
208 snprintf(filename, MAX_FILE_NAME_LENGTH, 328 snprintf(filename, MAX_FILE_NAME_LENGTH,
209 "SYSFS_ENTRY_PREFIX""/%s/memory%d/addr", 329 SYSFS_ENTRY_PREFIX"/%s/memory%d/addr",
210 devname, k); 330 devname, k);
211 } while ((fd = open(filename, O_RDONLY)) != -1 ); 331 } while ((fd = open(filename, O_RDONLY)) != -1 );
332 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks = k;
333
212 } else { 334 } else {
335#ifdef DEBUG
336 mpm_printf(1, "\nFailed to open \"%s\" err=%s\n",
337 filename, strerror(errno));
338#endif
213 /* Check device tree and record the index of the mmap */ 339 /* Check device tree and record the index of the mmap */
214 snprintf(filename, MAX_FILE_NAME_LENGTH, "/proc/device-tree/soc/%s/reg", devname); 340 snprintf(filename, MAX_FILE_NAME_LENGTH, "/proc/device-tree/soc/%s/reg", devname);
215 341
@@ -226,12 +352,14 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *
226 return MPM_TRANSPORT_SHM_OPEN_ERR_FIND_FILE_SIZE; 352 return MPM_TRANSPORT_SHM_OPEN_ERR_FIND_FILE_SIZE;
227 } 353 }
228 fileSize = statbuf.st_size; 354 fileSize = statbuf.st_size;
229 td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] = fileSize / sizeof(mem_block_t); 355 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks =
230 scratch = malloc (sizeof(mem_block_t) * td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); 356 fileSize / sizeof(mem_block_t);
231 if (!td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] || !scratch) 357 scratch =
358 malloc (sizeof(mem_block_t) * td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks);
359 if (!td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks || !scratch)
232 { 360 {
233 mpm_printf (1, "Failed to malloc size blocks for %d\n", 361 mpm_printf (1, "Failed to malloc size blocks for %d\n",
234 td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); 362 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks);
235 return MPM_TRANSPORT_SHM_OPEN_ERR_MALLOC; 363 return MPM_TRANSPORT_SHM_OPEN_ERR_MALLOC;
236 } 364 }
237 if ( read (fd, scratch, fileSize) != fileSize) 365 if ( read (fd, scratch, fileSize) != fileSize)
@@ -241,47 +369,58 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *
241 return MPM_TRANSPORT_SHM_OPEN_ERR_READ; 369 return MPM_TRANSPORT_SHM_OPEN_ERR_READ;
242 } 370 }
243 close (fd); 371 close (fd);
244 if ( td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] > MPM_MAX_MEM_ENTRIES) { 372 if ( td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks > MPM_MAX_MEM_ENTRIES) {
245 mpm_printf (1, "Number of blocks %d exceeds max %d\n", 373 mpm_printf (1, "Number of blocks %d exceeds max %d\n",
246 td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds], MPM_MAX_MEM_ENTRIES ); 374 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks, MPM_MAX_MEM_ENTRIES );
247 return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX; 375 return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX;
248 } 376 }
249 for (k = 0; k < td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]; k++) 377 for (k = 0; k < td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].num_mem_blocks; k++)
250 { 378 {
251 /* Swizzle from big endian in proc */ 379 /* Swizzle from big endian in proc */
252 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base = 380 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].base =
253 (scratch[k*8 + 0] << 24) | 381 (scratch[k*8 + 0] << 24) |
254 (scratch[k*8 + 1] << 16) | 382 (scratch[k*8 + 1] << 16) |
255 (scratch[k*8 + 2] << 8) | 383 (scratch[k*8 + 2] << 8) |
256 (scratch[k*8 + 3] ); 384 (scratch[k*8 + 3] );
257 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length = 385 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].length =
258 (scratch[k*8 + 4] << 24) | 386 (scratch[k*8 + 4] << 24) |
259 (scratch[k*8 + 5] << 16) | 387 (scratch[k*8 + 5] << 16) |
260 (scratch[k*8 + 6] << 8) | 388 (scratch[k*8 + 6] << 8) |
261 (scratch[k*8 + 7] ); 389 (scratch[k*8 + 7] );
262#ifdef DEBUG 390#ifdef DEBUG
263 mpm_printf(1, " DEBUG: Index %d: Base %x, length %x\n", k, 391 mpm_printf(1, " DEBUG: Index %d: Base %x, length %x\n", k,
264 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base, 392 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].base,
265 td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length); 393 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[k].length);
266#endif 394#endif
267 } 395 }
268 free (scratch); 396 free (scratch);
269 } 397 }
270 /* Now find the matching map section */ 398 /* Now find the matching map section */
271 map_index = mpm_transport_find_map_index(td, td->fd_mem_block.num_fds, 399 map_index = mpm_transport_find_map_index(td, td->fd_mem.num_fds,
272 sp->mmap[i].global_addr); 400 sp->mmap[i].global_addr, sp->mmap[i].length, 0);
273 if ( map_index < 0 ) { 401 if ( map_index < 0 ) {
274 mpm_printf(1, "Could not find map index for address 0x%llx error: map_index %d\n", 402 mpm_printf(1, "Could not find map index for address 0x%llx error: map_index %d\n",
275 sp->mmap[i].global_addr, map_index); 403 sp->mmap[i].global_addr, map_index);
276 return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX; 404 return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX;
277 } 405 }
406 if ((sp->mmap[i].global_addr
407 != (td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[map_index].base))
408 || (sp->mmap[i].length
409 != td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].mem_block[map_index].length)) {
410 ret = update_mem_entries(sp, td, i, td->fd_mem.num_fds, map_index);
411 if (ret < 0)
412 return ret;
413 }
414
278 td->map_index[i] = map_index; 415 td->map_index[i] = map_index;
416 td->fd_mem.fd_mem_entry[td->fd_mem.num_fds].devicename =
417 sp->mmap[i].devicename;
279 418
280 td->fd_mem_block.num_fds++; 419 td->fd_mem.num_fds++;
281 if ( td->fd_mem_block.num_fds >= MPM_MAX_NUM_FDS ) { 420 if ( td->fd_mem.num_fds >= MPM_MAX_NUM_FDS ) {
282 mpm_printf (1, "Number of fds %d exceeds max fds %d\n", 421 mpm_printf (1, "Number of fds %d exceeds max fds %d\n",
283 td->fd_mem_block.num_fds, MPM_MAX_NUM_FDS ); 422 td->fd_mem.num_fds, MPM_MAX_NUM_FDS );
284 return MPM_TRANSPORT_SHM_OPEN_ERR_EXCEED_NUM_FDS; 423 return MPM_TRANSPORT_SHM_OPEN_ERR_EXCEED_NUM_FDS;
285 } 424 }
286 } 425 }
287 426
@@ -324,7 +463,7 @@ void *mpm_transport_sharedmem_mmap(mpm_transport_cfg_t *sp, uint32_t addr, uint3
324 463
325 td->mmap_user[user_index].addr = mmap(NULL, mmap_length, 464 td->mmap_user[user_index].addr = mmap(NULL, mmap_length,
326 mcfg->mmap_prot, mcfg->mmap_flags, 465 mcfg->mmap_prot, mcfg->mmap_flags,
327 td->fd_mem_block.fd[td->fd_index[index]], 466 td->fd_mem.fd_mem_entry[td->fd_index[index]].fd,
328 ((pg_offset) + (td->map_index[index] * page_size))); 467 ((pg_offset) + (td->map_index[index] * page_size)));
329 if (td->mmap_user[user_index].addr == MAP_FAILED) { 468 if (td->mmap_user[user_index].addr == MAP_FAILED) {
330 mpm_printf(1, "can't mmap for the address 0x%x with length" 469 mpm_printf(1, "can't mmap for the address 0x%x with length"
@@ -389,7 +528,7 @@ static void *mpm_transport_sharedmem_rw_map (mpm_transport_cfg_t *sp, uint32_t a
389 base_address, *index, offset, base_correction, mmap_length); 528 base_address, *index, offset, base_correction, mmap_length);
390 if (!td->mmap_rw[*index].addr) { 529 if (!td->mmap_rw[*index].addr) {
391 td->mmap_rw[*index].addr = mmap(NULL, mmap_length, (PROT_READ|PROT_WRITE), 530 td->mmap_rw[*index].addr = mmap(NULL, mmap_length, (PROT_READ|PROT_WRITE),
392 MAP_SHARED, td->fd_mem_block.fd[td->fd_index[*index]], 531 MAP_SHARED, td->fd_mem.fd_mem_entry[td->fd_index[*index]].fd,
393 (td->map_index[*index] * page_size)); 532 (td->map_index[*index] * page_size));
394 if (td->mmap_rw[*index].addr == MAP_FAILED) { 533 if (td->mmap_rw[*index].addr == MAP_FAILED) {
395 td->mmap_rw[*index].addr = 0; 534 td->mmap_rw[*index].addr = 0;
@@ -468,11 +607,11 @@ void mpm_transport_sharedmem_close(mpm_transport_cfg_t *sp)
468 td->mmap_rw[i].addr = 0; 607 td->mmap_rw[i].addr = 0;
469 } 608 }
470 } 609 }
471 for (i = 0; i < td->fd_mem_block.num_fds; i++) { 610 for (i = 0; i < td->fd_mem.num_fds; i++) {
472 if (td->fd_mem_block.fd[i]) { 611 if (td->fd_mem.fd_mem_entry[i].fd) {
473 fsync(td->fd_mem_block.fd[i]); 612 fsync(td->fd_mem.fd_mem_entry[i].fd);
474 close(td->fd_mem_block.fd[i]); 613 close(td->fd_mem.fd_mem_entry[i].fd);
475 td->fd_mem_block.fd[i] = 0; 614 td->fd_mem.fd_mem_entry[i].fd = 0;
476 } 615 }
477 } 616 }
478 617
diff --git a/src/transport/sharedmem/mpm_transport_sharedmem.h b/src/transport/sharedmem/mpm_transport_sharedmem.h
index 485c971..cac2f88 100755
--- a/src/transport/sharedmem/mpm_transport_sharedmem.h
+++ b/src/transport/sharedmem/mpm_transport_sharedmem.h
@@ -58,16 +58,21 @@ typedef struct {
58} mem_block_t; 58} mem_block_t;
59 59
60typedef struct { 60typedef struct {
61 int fd;
62 char *devicename;
63 int num_mem_blocks;
64 mem_block_t mem_block[MPM_MAX_MEM_ENTRIES];
65} fd_mem_entry_t;
66
67typedef struct {
61 int num_fds; 68 int num_fds;
62 int num_mem_blocks[MPM_MAX_NUM_FDS]; 69 fd_mem_entry_t fd_mem_entry[MPM_MAX_NUM_FDS];
63 int fd[MPM_MAX_NUM_FDS]; 70} fd_mem_t;
64 mem_block_t mem_block[MPM_MAX_NUM_FDS][MPM_MAX_MEM_ENTRIES];
65} fd_mem_block_t;
66 71
67typedef struct mpm_transport_sharedmem_tag { 72typedef struct mpm_transport_sharedmem_tag {
68 int fd_index[MPM_MAX_MMAPS]; 73 int fd_index[MPM_MAX_MMAPS];
69 int map_index[MPM_MAX_MMAPS]; 74 int map_index[MPM_MAX_MMAPS];
70 fd_mem_block_t fd_mem_block; 75 fd_mem_t fd_mem;
71 mmap_data_t mmap_rw[MPM_MAX_MMAPS]; 76 mmap_data_t mmap_rw[MPM_MAX_MMAPS];
72 pthread_mutex_t mutex_rw[MPM_MAX_MMAPS]; 77 pthread_mutex_t mutex_rw[MPM_MAX_MMAPS];
73 mmap_data_t mmap_user[MPM_MAX_USER_MMAPS]; 78 mmap_data_t mmap_user[MPM_MAX_USER_MMAPS];