Skip to content

Commit 43a7b34

Browse files
committed
Track the attach state and error if we try to map a per-PASID MMIO area
Signed-off-by: Alastair D'Silva <alastair@d-silva.org>
1 parent 0add783 commit 43a7b34

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/afu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ static void afu_init(ocxl_afu *afu)
259259

260260
afu->tracing = false;
261261

262+
afu->attached = false;
263+
262264
#ifdef _ARCH_PPC64
263265
afu->ppc64_amr = 0;
264266
#endif
@@ -682,6 +684,8 @@ ocxl_err ocxl_afu_attach(ocxl_afu_h afu, __attribute__((unused)) uint64_t flags)
682684
return rc;
683685
}
684686

687+
my_afu->attached = true;
688+
685689
return OCXL_OK;
686690
}
687691

@@ -736,6 +740,7 @@ ocxl_err ocxl_afu_close(ocxl_afu_h afu)
736740

737741
close(my_afu->fd);
738742
my_afu->fd = -1;
743+
my_afu->attached = false;
739744

740745
if (my_afu->device_path) {
741746
free(my_afu->device_path);

src/libocxl_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ struct ocxl_afu {
171171
bool tracing;
172172
pthread_mutex_t trace_mutex;
173173

174+
bool attached;
175+
174176
#ifdef _ARCH_PPC64
175177
uint64_t ppc64_amr;
176178
#endif

src/mmio.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static ocxl_err global_mmio_map(ocxl_afu *afu, size_t size, int prot, uint64_t f
182182
* contents of this area are specific each AFU. The size can be discovered with
183183
* ocxl_mmio_size().
184184
*
185-
* @pre the AFU has been opened
185+
* @pre the AFU has been opened and attached
186186
*
187187
* @param afu the AFU to operate on
188188
* @param size the size of the MMIO region to map (or 0 to map the full region)
@@ -193,7 +193,7 @@ static ocxl_err global_mmio_map(ocxl_afu *afu, size_t size, int prot, uint64_t f
193193
*
194194
* @retval OCXL_OK on success
195195
* @retval OCXL_NO_MEM if the map failed
196-
* @retval OCXL_NO_CONTEXT if the AFU has not been opened
196+
* @retval OCXL_NO_CONTEXT if the AFU has not been opened/attached
197197
* @retval OCXL_INVALID_ARGS if the flags are not valid
198198
*/
199199
static ocxl_err mmio_map(ocxl_afu *afu, size_t size, int prot, uint64_t flags, off_t offset, ocxl_mmio_h *region)
@@ -206,12 +206,17 @@ static ocxl_err mmio_map(ocxl_afu *afu, size_t size, int prot, uint64_t flags, o
206206

207207
if (afu->fd < 0) {
208208
ocxl_err rc = OCXL_NO_CONTEXT;
209-
errmsg(afu, rc, "Could not map per-PASID MMIO on AFU '%s' as it has not been opened",
210-
afu->identifier.afu_name);
209+
errmsg(afu, rc, "Could not map per-PASID MMIO as the AFU has not been opened");
210+
return rc;
211+
}
212+
213+
if (!afu->attached) {
214+
ocxl_err rc = OCXL_NO_CONTEXT;
215+
errmsg(afu, rc, "Could not map per-PASID MMIO as the AFU has not been attached");
211216
return rc;
212217
}
213218

214-
void *addr = mmap(NULL, afu->per_pasid_mmio.length, prot, MAP_SHARED, afu->fd, offset);
219+
void *addr = mmap(NULL, size, prot, MAP_SHARED, afu->fd, offset);
215220
if (addr == MAP_FAILED) {
216221
ocxl_err rc = OCXL_NO_MEM;
217222
errmsg(afu, rc, "Could not map per-PASID MMIO: %d: %s", errno, strerror(errno));

0 commit comments

Comments
 (0)