Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * FPGA Manager Core
0004  *
0005  *  Copyright (C) 2013-2015 Altera Corporation
0006  *  Copyright (C) 2017 Intel Corporation
0007  *
0008  * With code from the mailing list:
0009  * Copyright (C) 2013 Xilinx, Inc.
0010  */
0011 #include <linux/firmware.h>
0012 #include <linux/fpga/fpga-mgr.h>
0013 #include <linux/idr.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/mutex.h>
0017 #include <linux/slab.h>
0018 #include <linux/scatterlist.h>
0019 #include <linux/highmem.h>
0020 
0021 static DEFINE_IDA(fpga_mgr_ida);
0022 static struct class *fpga_mgr_class;
0023 
0024 struct fpga_mgr_devres {
0025     struct fpga_manager *mgr;
0026 };
0027 
0028 static inline void fpga_mgr_fpga_remove(struct fpga_manager *mgr)
0029 {
0030     if (mgr->mops->fpga_remove)
0031         mgr->mops->fpga_remove(mgr);
0032 }
0033 
0034 static inline enum fpga_mgr_states fpga_mgr_state(struct fpga_manager *mgr)
0035 {
0036     if (mgr->mops->state)
0037         return  mgr->mops->state(mgr);
0038     return FPGA_MGR_STATE_UNKNOWN;
0039 }
0040 
0041 static inline u64 fpga_mgr_status(struct fpga_manager *mgr)
0042 {
0043     if (mgr->mops->status)
0044         return mgr->mops->status(mgr);
0045     return 0;
0046 }
0047 
0048 static inline int fpga_mgr_write(struct fpga_manager *mgr, const char *buf, size_t count)
0049 {
0050     if (mgr->mops->write)
0051         return  mgr->mops->write(mgr, buf, count);
0052     return -EOPNOTSUPP;
0053 }
0054 
0055 /*
0056  * After all the FPGA image has been written, do the device specific steps to
0057  * finish and set the FPGA into operating mode.
0058  */
0059 static inline int fpga_mgr_write_complete(struct fpga_manager *mgr,
0060                       struct fpga_image_info *info)
0061 {
0062     int ret = 0;
0063 
0064     mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
0065     if (mgr->mops->write_complete)
0066         ret = mgr->mops->write_complete(mgr, info);
0067     if (ret) {
0068         dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
0069         mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
0070         return ret;
0071     }
0072     mgr->state = FPGA_MGR_STATE_OPERATING;
0073 
0074     return 0;
0075 }
0076 
0077 static inline int fpga_mgr_parse_header(struct fpga_manager *mgr,
0078                     struct fpga_image_info *info,
0079                     const char *buf, size_t count)
0080 {
0081     if (mgr->mops->parse_header)
0082         return mgr->mops->parse_header(mgr, info, buf, count);
0083     return 0;
0084 }
0085 
0086 static inline int fpga_mgr_write_init(struct fpga_manager *mgr,
0087                       struct fpga_image_info *info,
0088                       const char *buf, size_t count)
0089 {
0090     if (mgr->mops->write_init)
0091         return  mgr->mops->write_init(mgr, info, buf, count);
0092     return 0;
0093 }
0094 
0095 static inline int fpga_mgr_write_sg(struct fpga_manager *mgr,
0096                     struct sg_table *sgt)
0097 {
0098     if (mgr->mops->write_sg)
0099         return  mgr->mops->write_sg(mgr, sgt);
0100     return -EOPNOTSUPP;
0101 }
0102 
0103 /**
0104  * fpga_image_info_alloc - Allocate an FPGA image info struct
0105  * @dev: owning device
0106  *
0107  * Return: struct fpga_image_info or NULL
0108  */
0109 struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
0110 {
0111     struct fpga_image_info *info;
0112 
0113     get_device(dev);
0114 
0115     info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
0116     if (!info) {
0117         put_device(dev);
0118         return NULL;
0119     }
0120 
0121     info->dev = dev;
0122 
0123     return info;
0124 }
0125 EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
0126 
0127 /**
0128  * fpga_image_info_free - Free an FPGA image info struct
0129  * @info: FPGA image info struct to free
0130  */
0131 void fpga_image_info_free(struct fpga_image_info *info)
0132 {
0133     struct device *dev;
0134 
0135     if (!info)
0136         return;
0137 
0138     dev = info->dev;
0139     if (info->firmware_name)
0140         devm_kfree(dev, info->firmware_name);
0141 
0142     devm_kfree(dev, info);
0143     put_device(dev);
0144 }
0145 EXPORT_SYMBOL_GPL(fpga_image_info_free);
0146 
0147 /*
0148  * Call the low level driver's parse_header function with entire FPGA image
0149  * buffer on the input. This will set info->header_size and info->data_size.
0150  */
0151 static int fpga_mgr_parse_header_mapped(struct fpga_manager *mgr,
0152                     struct fpga_image_info *info,
0153                     const char *buf, size_t count)
0154 {
0155     int ret;
0156 
0157     mgr->state = FPGA_MGR_STATE_PARSE_HEADER;
0158     ret = fpga_mgr_parse_header(mgr, info, buf, count);
0159 
0160     if (info->header_size + info->data_size > count) {
0161         dev_err(&mgr->dev, "Bitstream data outruns FPGA image\n");
0162         ret = -EINVAL;
0163     }
0164 
0165     if (ret) {
0166         dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
0167         mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
0168     }
0169 
0170     return ret;
0171 }
0172 
0173 /*
0174  * Call the low level driver's parse_header function with first fragment of
0175  * scattered FPGA image on the input. If header fits first fragment,
0176  * parse_header will set info->header_size and info->data_size. If it is not,
0177  * parse_header will set desired size to info->header_size and -EAGAIN will be
0178  * returned.
0179  */
0180 static int fpga_mgr_parse_header_sg_first(struct fpga_manager *mgr,
0181                       struct fpga_image_info *info,
0182                       struct sg_table *sgt)
0183 {
0184     struct sg_mapping_iter miter;
0185     int ret;
0186 
0187     mgr->state = FPGA_MGR_STATE_PARSE_HEADER;
0188 
0189     sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
0190     if (sg_miter_next(&miter) &&
0191         miter.length >= info->header_size)
0192         ret = fpga_mgr_parse_header(mgr, info, miter.addr, miter.length);
0193     else
0194         ret = -EAGAIN;
0195     sg_miter_stop(&miter);
0196 
0197     if (ret && ret != -EAGAIN) {
0198         dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
0199         mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
0200     }
0201 
0202     return ret;
0203 }
0204 
0205 /*
0206  * Copy scattered FPGA image fragments to temporary buffer and call the
0207  * low level driver's parse_header function. This should be called after
0208  * fpga_mgr_parse_header_sg_first() returned -EAGAIN. In case of success,
0209  * pointer to the newly allocated image header copy will be returned and
0210  * its size will be set into *ret_size. Returned buffer needs to be freed.
0211  */
0212 static void *fpga_mgr_parse_header_sg(struct fpga_manager *mgr,
0213                       struct fpga_image_info *info,
0214                       struct sg_table *sgt, size_t *ret_size)
0215 {
0216     size_t len, new_header_size, header_size = 0;
0217     char *new_buf, *buf = NULL;
0218     int ret;
0219 
0220     do {
0221         new_header_size = info->header_size;
0222         if (new_header_size <= header_size) {
0223             dev_err(&mgr->dev, "Requested invalid header size\n");
0224             ret = -EFAULT;
0225             break;
0226         }
0227 
0228         new_buf = krealloc(buf, new_header_size, GFP_KERNEL);
0229         if (!new_buf) {
0230             ret = -ENOMEM;
0231             break;
0232         }
0233 
0234         buf = new_buf;
0235 
0236         len = sg_pcopy_to_buffer(sgt->sgl, sgt->nents,
0237                      buf + header_size,
0238                      new_header_size - header_size,
0239                      header_size);
0240         if (len != new_header_size - header_size) {
0241             ret = -EFAULT;
0242             break;
0243         }
0244 
0245         header_size = new_header_size;
0246         ret = fpga_mgr_parse_header(mgr, info, buf, header_size);
0247     } while (ret == -EAGAIN);
0248 
0249     if (ret) {
0250         dev_err(&mgr->dev, "Error while parsing FPGA image header\n");
0251         mgr->state = FPGA_MGR_STATE_PARSE_HEADER_ERR;
0252         kfree(buf);
0253         buf = ERR_PTR(ret);
0254     }
0255 
0256     *ret_size = header_size;
0257 
0258     return buf;
0259 }
0260 
0261 /*
0262  * Call the low level driver's write_init function. This will do the
0263  * device-specific things to get the FPGA into the state where it is ready to
0264  * receive an FPGA image. The low level driver gets to see at least first
0265  * info->header_size bytes in the buffer. If info->header_size is 0,
0266  * write_init will not get any bytes of image buffer.
0267  */
0268 static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
0269                    struct fpga_image_info *info,
0270                    const char *buf, size_t count)
0271 {
0272     size_t header_size = info->header_size;
0273     int ret;
0274 
0275     mgr->state = FPGA_MGR_STATE_WRITE_INIT;
0276 
0277     if (header_size > count)
0278         ret = -EINVAL;
0279     else if (!header_size)
0280         ret = fpga_mgr_write_init(mgr, info, NULL, 0);
0281     else
0282         ret = fpga_mgr_write_init(mgr, info, buf, count);
0283 
0284     if (ret) {
0285         dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
0286         mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
0287         return ret;
0288     }
0289 
0290     return 0;
0291 }
0292 
0293 static int fpga_mgr_prepare_sg(struct fpga_manager *mgr,
0294                    struct fpga_image_info *info,
0295                    struct sg_table *sgt)
0296 {
0297     struct sg_mapping_iter miter;
0298     size_t len;
0299     char *buf;
0300     int ret;
0301 
0302     /* Short path. Low level driver don't care about image header. */
0303     if (!mgr->mops->initial_header_size && !mgr->mops->parse_header)
0304         return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
0305 
0306     /*
0307      * First try to use miter to map the first fragment to access the
0308      * header, this is the typical path.
0309      */
0310     ret = fpga_mgr_parse_header_sg_first(mgr, info, sgt);
0311     /* If 0, header fits first fragment, call write_init on it */
0312     if (!ret) {
0313         sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
0314         if (sg_miter_next(&miter)) {
0315             ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
0316                               miter.length);
0317             sg_miter_stop(&miter);
0318             return ret;
0319         }
0320         sg_miter_stop(&miter);
0321     /*
0322      * If -EAGAIN, more sg buffer is needed,
0323      * otherwise an error has occurred.
0324      */
0325     } else if (ret != -EAGAIN) {
0326         return ret;
0327     }
0328 
0329     /*
0330      * Copy the fragments into temporary memory.
0331      * Copying is done inside fpga_mgr_parse_header_sg().
0332      */
0333     buf = fpga_mgr_parse_header_sg(mgr, info, sgt, &len);
0334     if (IS_ERR(buf))
0335         return PTR_ERR(buf);
0336 
0337     ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
0338 
0339     kfree(buf);
0340 
0341     return ret;
0342 }
0343 
0344 /**
0345  * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
0346  * @mgr:    fpga manager
0347  * @info:   fpga image specific information
0348  * @sgt:    scatterlist table
0349  *
0350  * Step the low level fpga manager through the device-specific steps of getting
0351  * an FPGA ready to be configured, writing the image to it, then doing whatever
0352  * post-configuration steps necessary.  This code assumes the caller got the
0353  * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
0354  * not an error code.
0355  *
0356  * This is the preferred entry point for FPGA programming, it does not require
0357  * any contiguous kernel memory.
0358  *
0359  * Return: 0 on success, negative error code otherwise.
0360  */
0361 static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
0362                 struct fpga_image_info *info,
0363                 struct sg_table *sgt)
0364 {
0365     int ret;
0366 
0367     ret = fpga_mgr_prepare_sg(mgr, info, sgt);
0368     if (ret)
0369         return ret;
0370 
0371     /* Write the FPGA image to the FPGA. */
0372     mgr->state = FPGA_MGR_STATE_WRITE;
0373     if (mgr->mops->write_sg) {
0374         ret = fpga_mgr_write_sg(mgr, sgt);
0375     } else {
0376         size_t length, count = 0, data_size = info->data_size;
0377         struct sg_mapping_iter miter;
0378 
0379         sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
0380 
0381         if (mgr->mops->skip_header &&
0382             !sg_miter_skip(&miter, info->header_size)) {
0383             ret = -EINVAL;
0384             goto out;
0385         }
0386 
0387         while (sg_miter_next(&miter)) {
0388             if (data_size)
0389                 length = min(miter.length, data_size - count);
0390             else
0391                 length = miter.length;
0392 
0393             ret = fpga_mgr_write(mgr, miter.addr, length);
0394             if (ret)
0395                 break;
0396 
0397             count += length;
0398             if (data_size && count >= data_size)
0399                 break;
0400         }
0401         sg_miter_stop(&miter);
0402     }
0403 
0404 out:
0405     if (ret) {
0406         dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
0407         mgr->state = FPGA_MGR_STATE_WRITE_ERR;
0408         return ret;
0409     }
0410 
0411     return fpga_mgr_write_complete(mgr, info);
0412 }
0413 
0414 static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
0415                     struct fpga_image_info *info,
0416                     const char *buf, size_t count)
0417 {
0418     int ret;
0419 
0420     ret = fpga_mgr_parse_header_mapped(mgr, info, buf, count);
0421     if (ret)
0422         return ret;
0423 
0424     ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
0425     if (ret)
0426         return ret;
0427 
0428     if (mgr->mops->skip_header) {
0429         buf += info->header_size;
0430         count -= info->header_size;
0431     }
0432 
0433     if (info->data_size)
0434         count = info->data_size;
0435 
0436     /*
0437      * Write the FPGA image to the FPGA.
0438      */
0439     mgr->state = FPGA_MGR_STATE_WRITE;
0440     ret = fpga_mgr_write(mgr, buf, count);
0441     if (ret) {
0442         dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
0443         mgr->state = FPGA_MGR_STATE_WRITE_ERR;
0444         return ret;
0445     }
0446 
0447     return fpga_mgr_write_complete(mgr, info);
0448 }
0449 
0450 /**
0451  * fpga_mgr_buf_load - load fpga from image in buffer
0452  * @mgr:    fpga manager
0453  * @info:   fpga image info
0454  * @buf:    buffer contain fpga image
0455  * @count:  byte count of buf
0456  *
0457  * Step the low level fpga manager through the device-specific steps of getting
0458  * an FPGA ready to be configured, writing the image to it, then doing whatever
0459  * post-configuration steps necessary.  This code assumes the caller got the
0460  * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
0461  *
0462  * Return: 0 on success, negative error code otherwise.
0463  */
0464 static int fpga_mgr_buf_load(struct fpga_manager *mgr,
0465                  struct fpga_image_info *info,
0466                  const char *buf, size_t count)
0467 {
0468     struct page **pages;
0469     struct sg_table sgt;
0470     const void *p;
0471     int nr_pages;
0472     int index;
0473     int rc;
0474 
0475     /*
0476      * This is just a fast path if the caller has already created a
0477      * contiguous kernel buffer and the driver doesn't require SG, non-SG
0478      * drivers will still work on the slow path.
0479      */
0480     if (mgr->mops->write)
0481         return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
0482 
0483     /*
0484      * Convert the linear kernel pointer into a sg_table of pages for use
0485      * by the driver.
0486      */
0487     nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
0488            (unsigned long)buf / PAGE_SIZE;
0489     pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
0490     if (!pages)
0491         return -ENOMEM;
0492 
0493     p = buf - offset_in_page(buf);
0494     for (index = 0; index < nr_pages; index++) {
0495         if (is_vmalloc_addr(p))
0496             pages[index] = vmalloc_to_page(p);
0497         else
0498             pages[index] = kmap_to_page((void *)p);
0499         if (!pages[index]) {
0500             kfree(pages);
0501             return -EFAULT;
0502         }
0503         p += PAGE_SIZE;
0504     }
0505 
0506     /*
0507      * The temporary pages list is used to code share the merging algorithm
0508      * in sg_alloc_table_from_pages
0509      */
0510     rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
0511                        count, GFP_KERNEL);
0512     kfree(pages);
0513     if (rc)
0514         return rc;
0515 
0516     rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
0517     sg_free_table(&sgt);
0518 
0519     return rc;
0520 }
0521 
0522 /**
0523  * fpga_mgr_firmware_load - request firmware and load to fpga
0524  * @mgr:    fpga manager
0525  * @info:   fpga image specific information
0526  * @image_name: name of image file on the firmware search path
0527  *
0528  * Request an FPGA image using the firmware class, then write out to the FPGA.
0529  * Update the state before each step to provide info on what step failed if
0530  * there is a failure.  This code assumes the caller got the mgr pointer
0531  * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
0532  * code.
0533  *
0534  * Return: 0 on success, negative error code otherwise.
0535  */
0536 static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
0537                   struct fpga_image_info *info,
0538                   const char *image_name)
0539 {
0540     struct device *dev = &mgr->dev;
0541     const struct firmware *fw;
0542     int ret;
0543 
0544     dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
0545 
0546     mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
0547 
0548     ret = request_firmware(&fw, image_name, dev);
0549     if (ret) {
0550         mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
0551         dev_err(dev, "Error requesting firmware %s\n", image_name);
0552         return ret;
0553     }
0554 
0555     ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
0556 
0557     release_firmware(fw);
0558 
0559     return ret;
0560 }
0561 
0562 /**
0563  * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
0564  * @mgr:    fpga manager
0565  * @info:   fpga image information.
0566  *
0567  * Load the FPGA from an image which is indicated in @info.  If successful, the
0568  * FPGA ends up in operating mode.
0569  *
0570  * Return: 0 on success, negative error code otherwise.
0571  */
0572 int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
0573 {
0574     info->header_size = mgr->mops->initial_header_size;
0575 
0576     if (info->sgt)
0577         return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
0578     if (info->buf && info->count)
0579         return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
0580     if (info->firmware_name)
0581         return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
0582     return -EINVAL;
0583 }
0584 EXPORT_SYMBOL_GPL(fpga_mgr_load);
0585 
0586 static const char * const state_str[] = {
0587     [FPGA_MGR_STATE_UNKNOWN] =      "unknown",
0588     [FPGA_MGR_STATE_POWER_OFF] =        "power off",
0589     [FPGA_MGR_STATE_POWER_UP] =     "power up",
0590     [FPGA_MGR_STATE_RESET] =        "reset",
0591 
0592     /* requesting FPGA image from firmware */
0593     [FPGA_MGR_STATE_FIRMWARE_REQ] =     "firmware request",
0594     [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] = "firmware request error",
0595 
0596     /* Parse FPGA image header */
0597     [FPGA_MGR_STATE_PARSE_HEADER] =     "parse header",
0598     [FPGA_MGR_STATE_PARSE_HEADER_ERR] = "parse header error",
0599 
0600     /* Preparing FPGA to receive image */
0601     [FPGA_MGR_STATE_WRITE_INIT] =       "write init",
0602     [FPGA_MGR_STATE_WRITE_INIT_ERR] =   "write init error",
0603 
0604     /* Writing image to FPGA */
0605     [FPGA_MGR_STATE_WRITE] =        "write",
0606     [FPGA_MGR_STATE_WRITE_ERR] =        "write error",
0607 
0608     /* Finishing configuration after image has been written */
0609     [FPGA_MGR_STATE_WRITE_COMPLETE] =   "write complete",
0610     [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =   "write complete error",
0611 
0612     /* FPGA reports to be in normal operating mode */
0613     [FPGA_MGR_STATE_OPERATING] =        "operating",
0614 };
0615 
0616 static ssize_t name_show(struct device *dev,
0617              struct device_attribute *attr, char *buf)
0618 {
0619     struct fpga_manager *mgr = to_fpga_manager(dev);
0620 
0621     return sprintf(buf, "%s\n", mgr->name);
0622 }
0623 
0624 static ssize_t state_show(struct device *dev,
0625               struct device_attribute *attr, char *buf)
0626 {
0627     struct fpga_manager *mgr = to_fpga_manager(dev);
0628 
0629     return sprintf(buf, "%s\n", state_str[mgr->state]);
0630 }
0631 
0632 static ssize_t status_show(struct device *dev,
0633                struct device_attribute *attr, char *buf)
0634 {
0635     struct fpga_manager *mgr = to_fpga_manager(dev);
0636     u64 status;
0637     int len = 0;
0638 
0639     status = fpga_mgr_status(mgr);
0640 
0641     if (status & FPGA_MGR_STATUS_OPERATION_ERR)
0642         len += sprintf(buf + len, "reconfig operation error\n");
0643     if (status & FPGA_MGR_STATUS_CRC_ERR)
0644         len += sprintf(buf + len, "reconfig CRC error\n");
0645     if (status & FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR)
0646         len += sprintf(buf + len, "reconfig incompatible image\n");
0647     if (status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
0648         len += sprintf(buf + len, "reconfig IP protocol error\n");
0649     if (status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
0650         len += sprintf(buf + len, "reconfig fifo overflow error\n");
0651 
0652     return len;
0653 }
0654 
0655 static DEVICE_ATTR_RO(name);
0656 static DEVICE_ATTR_RO(state);
0657 static DEVICE_ATTR_RO(status);
0658 
0659 static struct attribute *fpga_mgr_attrs[] = {
0660     &dev_attr_name.attr,
0661     &dev_attr_state.attr,
0662     &dev_attr_status.attr,
0663     NULL,
0664 };
0665 ATTRIBUTE_GROUPS(fpga_mgr);
0666 
0667 static struct fpga_manager *__fpga_mgr_get(struct device *dev)
0668 {
0669     struct fpga_manager *mgr;
0670 
0671     mgr = to_fpga_manager(dev);
0672 
0673     if (!try_module_get(dev->parent->driver->owner))
0674         goto err_dev;
0675 
0676     return mgr;
0677 
0678 err_dev:
0679     put_device(dev);
0680     return ERR_PTR(-ENODEV);
0681 }
0682 
0683 static int fpga_mgr_dev_match(struct device *dev, const void *data)
0684 {
0685     return dev->parent == data;
0686 }
0687 
0688 /**
0689  * fpga_mgr_get - Given a device, get a reference to an fpga mgr.
0690  * @dev:    parent device that fpga mgr was registered with
0691  *
0692  * Return: fpga manager struct or IS_ERR() condition containing error code.
0693  */
0694 struct fpga_manager *fpga_mgr_get(struct device *dev)
0695 {
0696     struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
0697                            fpga_mgr_dev_match);
0698     if (!mgr_dev)
0699         return ERR_PTR(-ENODEV);
0700 
0701     return __fpga_mgr_get(mgr_dev);
0702 }
0703 EXPORT_SYMBOL_GPL(fpga_mgr_get);
0704 
0705 /**
0706  * of_fpga_mgr_get - Given a device node, get a reference to an fpga mgr.
0707  *
0708  * @node:   device node
0709  *
0710  * Return: fpga manager struct or IS_ERR() condition containing error code.
0711  */
0712 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
0713 {
0714     struct device *dev;
0715 
0716     dev = class_find_device_by_of_node(fpga_mgr_class, node);
0717     if (!dev)
0718         return ERR_PTR(-ENODEV);
0719 
0720     return __fpga_mgr_get(dev);
0721 }
0722 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
0723 
0724 /**
0725  * fpga_mgr_put - release a reference to an fpga manager
0726  * @mgr:    fpga manager structure
0727  */
0728 void fpga_mgr_put(struct fpga_manager *mgr)
0729 {
0730     module_put(mgr->dev.parent->driver->owner);
0731     put_device(&mgr->dev);
0732 }
0733 EXPORT_SYMBOL_GPL(fpga_mgr_put);
0734 
0735 /**
0736  * fpga_mgr_lock - Lock FPGA manager for exclusive use
0737  * @mgr:    fpga manager
0738  *
0739  * Given a pointer to FPGA Manager (from fpga_mgr_get() or
0740  * of_fpga_mgr_put()) attempt to get the mutex. The user should call
0741  * fpga_mgr_lock() and verify that it returns 0 before attempting to
0742  * program the FPGA.  Likewise, the user should call fpga_mgr_unlock
0743  * when done programming the FPGA.
0744  *
0745  * Return: 0 for success or -EBUSY
0746  */
0747 int fpga_mgr_lock(struct fpga_manager *mgr)
0748 {
0749     if (!mutex_trylock(&mgr->ref_mutex)) {
0750         dev_err(&mgr->dev, "FPGA manager is in use.\n");
0751         return -EBUSY;
0752     }
0753 
0754     return 0;
0755 }
0756 EXPORT_SYMBOL_GPL(fpga_mgr_lock);
0757 
0758 /**
0759  * fpga_mgr_unlock - Unlock FPGA manager after done programming
0760  * @mgr:    fpga manager
0761  */
0762 void fpga_mgr_unlock(struct fpga_manager *mgr)
0763 {
0764     mutex_unlock(&mgr->ref_mutex);
0765 }
0766 EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
0767 
0768 /**
0769  * fpga_mgr_register_full - create and register an FPGA Manager device
0770  * @parent: fpga manager device from pdev
0771  * @info:   parameters for fpga manager
0772  *
0773  * The caller of this function is responsible for calling fpga_mgr_unregister().
0774  * Using devm_fpga_mgr_register_full() instead is recommended.
0775  *
0776  * Return: pointer to struct fpga_manager pointer or ERR_PTR()
0777  */
0778 struct fpga_manager *
0779 fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
0780 {
0781     const struct fpga_manager_ops *mops = info->mops;
0782     struct fpga_manager *mgr;
0783     int id, ret;
0784 
0785     if (!mops) {
0786         dev_err(parent, "Attempt to register without fpga_manager_ops\n");
0787         return ERR_PTR(-EINVAL);
0788     }
0789 
0790     if (!info->name || !strlen(info->name)) {
0791         dev_err(parent, "Attempt to register with no name!\n");
0792         return ERR_PTR(-EINVAL);
0793     }
0794 
0795     mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
0796     if (!mgr)
0797         return ERR_PTR(-ENOMEM);
0798 
0799     id = ida_alloc(&fpga_mgr_ida, GFP_KERNEL);
0800     if (id < 0) {
0801         ret = id;
0802         goto error_kfree;
0803     }
0804 
0805     mutex_init(&mgr->ref_mutex);
0806 
0807     mgr->name = info->name;
0808     mgr->mops = info->mops;
0809     mgr->priv = info->priv;
0810     mgr->compat_id = info->compat_id;
0811 
0812     mgr->dev.class = fpga_mgr_class;
0813     mgr->dev.groups = mops->groups;
0814     mgr->dev.parent = parent;
0815     mgr->dev.of_node = parent->of_node;
0816     mgr->dev.id = id;
0817 
0818     ret = dev_set_name(&mgr->dev, "fpga%d", id);
0819     if (ret)
0820         goto error_device;
0821 
0822     /*
0823      * Initialize framework state by requesting low level driver read state
0824      * from device.  FPGA may be in reset mode or may have been programmed
0825      * by bootloader or EEPROM.
0826      */
0827     mgr->state = fpga_mgr_state(mgr);
0828 
0829     ret = device_register(&mgr->dev);
0830     if (ret) {
0831         put_device(&mgr->dev);
0832         return ERR_PTR(ret);
0833     }
0834 
0835     return mgr;
0836 
0837 error_device:
0838     ida_free(&fpga_mgr_ida, id);
0839 error_kfree:
0840     kfree(mgr);
0841 
0842     return ERR_PTR(ret);
0843 }
0844 EXPORT_SYMBOL_GPL(fpga_mgr_register_full);
0845 
0846 /**
0847  * fpga_mgr_register - create and register an FPGA Manager device
0848  * @parent: fpga manager device from pdev
0849  * @name:   fpga manager name
0850  * @mops:   pointer to structure of fpga manager ops
0851  * @priv:   fpga manager private data
0852  *
0853  * The caller of this function is responsible for calling fpga_mgr_unregister().
0854  * Using devm_fpga_mgr_register() instead is recommended. This simple
0855  * version of the register function should be sufficient for most users. The
0856  * fpga_mgr_register_full() function is available for users that need to pass
0857  * additional, optional parameters.
0858  *
0859  * Return: pointer to struct fpga_manager pointer or ERR_PTR()
0860  */
0861 struct fpga_manager *
0862 fpga_mgr_register(struct device *parent, const char *name,
0863           const struct fpga_manager_ops *mops, void *priv)
0864 {
0865     struct fpga_manager_info info = { 0 };
0866 
0867     info.name = name;
0868     info.mops = mops;
0869     info.priv = priv;
0870 
0871     return fpga_mgr_register_full(parent, &info);
0872 }
0873 EXPORT_SYMBOL_GPL(fpga_mgr_register);
0874 
0875 /**
0876  * fpga_mgr_unregister - unregister an FPGA manager
0877  * @mgr: fpga manager struct
0878  *
0879  * This function is intended for use in an FPGA manager driver's remove function.
0880  */
0881 void fpga_mgr_unregister(struct fpga_manager *mgr)
0882 {
0883     dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
0884 
0885     /*
0886      * If the low level driver provides a method for putting fpga into
0887      * a desired state upon unregister, do it.
0888      */
0889     fpga_mgr_fpga_remove(mgr);
0890 
0891     device_unregister(&mgr->dev);
0892 }
0893 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
0894 
0895 static void devm_fpga_mgr_unregister(struct device *dev, void *res)
0896 {
0897     struct fpga_mgr_devres *dr = res;
0898 
0899     fpga_mgr_unregister(dr->mgr);
0900 }
0901 
0902 /**
0903  * devm_fpga_mgr_register_full - resource managed variant of fpga_mgr_register()
0904  * @parent: fpga manager device from pdev
0905  * @info:   parameters for fpga manager
0906  *
0907  * Return:  fpga manager pointer on success, negative error code otherwise.
0908  *
0909  * This is the devres variant of fpga_mgr_register_full() for which the unregister
0910  * function will be called automatically when the managing device is detached.
0911  */
0912 struct fpga_manager *
0913 devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info)
0914 {
0915     struct fpga_mgr_devres *dr;
0916     struct fpga_manager *mgr;
0917 
0918     dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
0919     if (!dr)
0920         return ERR_PTR(-ENOMEM);
0921 
0922     mgr = fpga_mgr_register_full(parent, info);
0923     if (IS_ERR(mgr)) {
0924         devres_free(dr);
0925         return mgr;
0926     }
0927 
0928     dr->mgr = mgr;
0929     devres_add(parent, dr);
0930 
0931     return mgr;
0932 }
0933 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register_full);
0934 
0935 /**
0936  * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
0937  * @parent: fpga manager device from pdev
0938  * @name:   fpga manager name
0939  * @mops:   pointer to structure of fpga manager ops
0940  * @priv:   fpga manager private data
0941  *
0942  * Return:  fpga manager pointer on success, negative error code otherwise.
0943  *
0944  * This is the devres variant of fpga_mgr_register() for which the
0945  * unregister function will be called automatically when the managing
0946  * device is detached.
0947  */
0948 struct fpga_manager *
0949 devm_fpga_mgr_register(struct device *parent, const char *name,
0950                const struct fpga_manager_ops *mops, void *priv)
0951 {
0952     struct fpga_manager_info info = { 0 };
0953 
0954     info.name = name;
0955     info.mops = mops;
0956     info.priv = priv;
0957 
0958     return devm_fpga_mgr_register_full(parent, &info);
0959 }
0960 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
0961 
0962 static void fpga_mgr_dev_release(struct device *dev)
0963 {
0964     struct fpga_manager *mgr = to_fpga_manager(dev);
0965 
0966     ida_free(&fpga_mgr_ida, mgr->dev.id);
0967     kfree(mgr);
0968 }
0969 
0970 static int __init fpga_mgr_class_init(void)
0971 {
0972     pr_info("FPGA manager framework\n");
0973 
0974     fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
0975     if (IS_ERR(fpga_mgr_class))
0976         return PTR_ERR(fpga_mgr_class);
0977 
0978     fpga_mgr_class->dev_groups = fpga_mgr_groups;
0979     fpga_mgr_class->dev_release = fpga_mgr_dev_release;
0980 
0981     return 0;
0982 }
0983 
0984 static void __exit fpga_mgr_class_exit(void)
0985 {
0986     class_destroy(fpga_mgr_class);
0987     ida_destroy(&fpga_mgr_ida);
0988 }
0989 
0990 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
0991 MODULE_DESCRIPTION("FPGA manager framework");
0992 MODULE_LICENSE("GPL v2");
0993 
0994 subsys_initcall(fpga_mgr_class_init);
0995 module_exit(fpga_mgr_class_exit);