Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * DMA traffic test driver
0004  *
0005  * Copyright (C) 2020, Intel Corporation
0006  * Authors: Isaac Hazan <isaac.hazan@intel.com>
0007  *      Mika Westerberg <mika.westerberg@linux.intel.com>
0008  */
0009 
0010 #include <linux/completion.h>
0011 #include <linux/debugfs.h>
0012 #include <linux/module.h>
0013 #include <linux/sizes.h>
0014 #include <linux/thunderbolt.h>
0015 
0016 #define DMA_TEST_TX_RING_SIZE       64
0017 #define DMA_TEST_RX_RING_SIZE       256
0018 #define DMA_TEST_FRAME_SIZE     SZ_4K
0019 #define DMA_TEST_DATA_PATTERN       0x0123456789abcdefLL
0020 #define DMA_TEST_MAX_PACKETS        1000
0021 
0022 enum dma_test_frame_pdf {
0023     DMA_TEST_PDF_FRAME_START = 1,
0024     DMA_TEST_PDF_FRAME_END,
0025 };
0026 
0027 struct dma_test_frame {
0028     struct dma_test *dma_test;
0029     void *data;
0030     struct ring_frame frame;
0031 };
0032 
0033 enum dma_test_test_error {
0034     DMA_TEST_NO_ERROR,
0035     DMA_TEST_INTERRUPTED,
0036     DMA_TEST_BUFFER_ERROR,
0037     DMA_TEST_DMA_ERROR,
0038     DMA_TEST_CONFIG_ERROR,
0039     DMA_TEST_SPEED_ERROR,
0040     DMA_TEST_WIDTH_ERROR,
0041     DMA_TEST_BONDING_ERROR,
0042     DMA_TEST_PACKET_ERROR,
0043 };
0044 
0045 static const char * const dma_test_error_names[] = {
0046     [DMA_TEST_NO_ERROR] = "no errors",
0047     [DMA_TEST_INTERRUPTED] = "interrupted by signal",
0048     [DMA_TEST_BUFFER_ERROR] = "no memory for packet buffers",
0049     [DMA_TEST_DMA_ERROR] = "DMA ring setup failed",
0050     [DMA_TEST_CONFIG_ERROR] = "configuration is not valid",
0051     [DMA_TEST_SPEED_ERROR] = "unexpected link speed",
0052     [DMA_TEST_WIDTH_ERROR] = "unexpected link width",
0053     [DMA_TEST_BONDING_ERROR] = "lane bonding configuration error",
0054     [DMA_TEST_PACKET_ERROR] = "packet check failed",
0055 };
0056 
0057 enum dma_test_result {
0058     DMA_TEST_NOT_RUN,
0059     DMA_TEST_SUCCESS,
0060     DMA_TEST_FAIL,
0061 };
0062 
0063 static const char * const dma_test_result_names[] = {
0064     [DMA_TEST_NOT_RUN] = "not run",
0065     [DMA_TEST_SUCCESS] = "success",
0066     [DMA_TEST_FAIL] = "failed",
0067 };
0068 
0069 /**
0070  * struct dma_test - DMA test device driver private data
0071  * @svc: XDomain service the driver is bound to
0072  * @xd: XDomain the service belongs to
0073  * @rx_ring: Software ring holding RX frames
0074  * @rx_hopid: HopID used for receiving frames
0075  * @tx_ring: Software ring holding TX frames
0076  * @tx_hopid: HopID used for sending fames
0077  * @packets_to_send: Number of packets to send
0078  * @packets_to_receive: Number of packets to receive
0079  * @packets_sent: Actual number of packets sent
0080  * @packets_received: Actual number of packets received
0081  * @link_speed: Expected link speed (Gb/s), %0 to use whatever is negotiated
0082  * @link_width: Expected link width (Gb/s), %0 to use whatever is negotiated
0083  * @crc_errors: Number of CRC errors during the test run
0084  * @buffer_overflow_errors: Number of buffer overflow errors during the test
0085  *              run
0086  * @result: Result of the last run
0087  * @error_code: Error code of the last run
0088  * @complete: Used to wait for the Rx to complete
0089  * @lock: Lock serializing access to this structure
0090  * @debugfs_dir: dentry of this dma_test
0091  */
0092 struct dma_test {
0093     const struct tb_service *svc;
0094     struct tb_xdomain *xd;
0095     struct tb_ring *rx_ring;
0096     int rx_hopid;
0097     struct tb_ring *tx_ring;
0098     int tx_hopid;
0099     unsigned int packets_to_send;
0100     unsigned int packets_to_receive;
0101     unsigned int packets_sent;
0102     unsigned int packets_received;
0103     unsigned int link_speed;
0104     unsigned int link_width;
0105     unsigned int crc_errors;
0106     unsigned int buffer_overflow_errors;
0107     enum dma_test_result result;
0108     enum dma_test_test_error error_code;
0109     struct completion complete;
0110     struct mutex lock;
0111     struct dentry *debugfs_dir;
0112 };
0113 
0114 /* DMA test property directory UUID: 3188cd10-6523-4a5a-a682-fdca07a248d8 */
0115 static const uuid_t dma_test_dir_uuid =
0116     UUID_INIT(0x3188cd10, 0x6523, 0x4a5a,
0117           0xa6, 0x82, 0xfd, 0xca, 0x07, 0xa2, 0x48, 0xd8);
0118 
0119 static struct tb_property_dir *dma_test_dir;
0120 static void *dma_test_pattern;
0121 
0122 static void dma_test_free_rings(struct dma_test *dt)
0123 {
0124     if (dt->rx_ring) {
0125         tb_xdomain_release_in_hopid(dt->xd, dt->rx_hopid);
0126         tb_ring_free(dt->rx_ring);
0127         dt->rx_ring = NULL;
0128     }
0129     if (dt->tx_ring) {
0130         tb_xdomain_release_out_hopid(dt->xd, dt->tx_hopid);
0131         tb_ring_free(dt->tx_ring);
0132         dt->tx_ring = NULL;
0133     }
0134 }
0135 
0136 static int dma_test_start_rings(struct dma_test *dt)
0137 {
0138     unsigned int flags = RING_FLAG_FRAME;
0139     struct tb_xdomain *xd = dt->xd;
0140     int ret, e2e_tx_hop = 0;
0141     struct tb_ring *ring;
0142 
0143     /*
0144      * If we are both sender and receiver (traffic goes over a
0145      * special loopback dongle) enable E2E flow control. This avoids
0146      * losing packets.
0147      */
0148     if (dt->packets_to_send && dt->packets_to_receive)
0149         flags |= RING_FLAG_E2E;
0150 
0151     if (dt->packets_to_send) {
0152         ring = tb_ring_alloc_tx(xd->tb->nhi, -1, DMA_TEST_TX_RING_SIZE,
0153                     flags);
0154         if (!ring)
0155             return -ENOMEM;
0156 
0157         dt->tx_ring = ring;
0158         e2e_tx_hop = ring->hop;
0159 
0160         ret = tb_xdomain_alloc_out_hopid(xd, -1);
0161         if (ret < 0) {
0162             dma_test_free_rings(dt);
0163             return ret;
0164         }
0165 
0166         dt->tx_hopid = ret;
0167     }
0168 
0169     if (dt->packets_to_receive) {
0170         u16 sof_mask, eof_mask;
0171 
0172         sof_mask = BIT(DMA_TEST_PDF_FRAME_START);
0173         eof_mask = BIT(DMA_TEST_PDF_FRAME_END);
0174 
0175         ring = tb_ring_alloc_rx(xd->tb->nhi, -1, DMA_TEST_RX_RING_SIZE,
0176                     flags, e2e_tx_hop, sof_mask, eof_mask,
0177                     NULL, NULL);
0178         if (!ring) {
0179             dma_test_free_rings(dt);
0180             return -ENOMEM;
0181         }
0182 
0183         dt->rx_ring = ring;
0184 
0185         ret = tb_xdomain_alloc_in_hopid(xd, -1);
0186         if (ret < 0) {
0187             dma_test_free_rings(dt);
0188             return ret;
0189         }
0190 
0191         dt->rx_hopid = ret;
0192     }
0193 
0194     ret = tb_xdomain_enable_paths(dt->xd, dt->tx_hopid,
0195                       dt->tx_ring ? dt->tx_ring->hop : 0,
0196                       dt->rx_hopid,
0197                       dt->rx_ring ? dt->rx_ring->hop : 0);
0198     if (ret) {
0199         dma_test_free_rings(dt);
0200         return ret;
0201     }
0202 
0203     if (dt->tx_ring)
0204         tb_ring_start(dt->tx_ring);
0205     if (dt->rx_ring)
0206         tb_ring_start(dt->rx_ring);
0207 
0208     return 0;
0209 }
0210 
0211 static void dma_test_stop_rings(struct dma_test *dt)
0212 {
0213     int ret;
0214 
0215     if (dt->rx_ring)
0216         tb_ring_stop(dt->rx_ring);
0217     if (dt->tx_ring)
0218         tb_ring_stop(dt->tx_ring);
0219 
0220     ret = tb_xdomain_disable_paths(dt->xd, dt->tx_hopid,
0221                        dt->tx_ring ? dt->tx_ring->hop : 0,
0222                        dt->rx_hopid,
0223                        dt->rx_ring ? dt->rx_ring->hop : 0);
0224     if (ret)
0225         dev_warn(&dt->svc->dev, "failed to disable DMA paths\n");
0226 
0227     dma_test_free_rings(dt);
0228 }
0229 
0230 static void dma_test_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
0231                  bool canceled)
0232 {
0233     struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
0234     struct dma_test *dt = tf->dma_test;
0235     struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
0236 
0237     dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
0238              DMA_FROM_DEVICE);
0239     kfree(tf->data);
0240 
0241     if (canceled) {
0242         kfree(tf);
0243         return;
0244     }
0245 
0246     dt->packets_received++;
0247     dev_dbg(&dt->svc->dev, "packet %u/%u received\n", dt->packets_received,
0248         dt->packets_to_receive);
0249 
0250     if (tf->frame.flags & RING_DESC_CRC_ERROR)
0251         dt->crc_errors++;
0252     if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN)
0253         dt->buffer_overflow_errors++;
0254 
0255     kfree(tf);
0256 
0257     if (dt->packets_received == dt->packets_to_receive)
0258         complete(&dt->complete);
0259 }
0260 
0261 static int dma_test_submit_rx(struct dma_test *dt, size_t npackets)
0262 {
0263     struct device *dma_dev = tb_ring_dma_device(dt->rx_ring);
0264     int i;
0265 
0266     for (i = 0; i < npackets; i++) {
0267         struct dma_test_frame *tf;
0268         dma_addr_t dma_addr;
0269 
0270         tf = kzalloc(sizeof(*tf), GFP_KERNEL);
0271         if (!tf)
0272             return -ENOMEM;
0273 
0274         tf->data = kzalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
0275         if (!tf->data) {
0276             kfree(tf);
0277             return -ENOMEM;
0278         }
0279 
0280         dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
0281                       DMA_FROM_DEVICE);
0282         if (dma_mapping_error(dma_dev, dma_addr)) {
0283             kfree(tf->data);
0284             kfree(tf);
0285             return -ENOMEM;
0286         }
0287 
0288         tf->frame.buffer_phy = dma_addr;
0289         tf->frame.callback = dma_test_rx_callback;
0290         tf->dma_test = dt;
0291         INIT_LIST_HEAD(&tf->frame.list);
0292 
0293         tb_ring_rx(dt->rx_ring, &tf->frame);
0294     }
0295 
0296     return 0;
0297 }
0298 
0299 static void dma_test_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
0300                  bool canceled)
0301 {
0302     struct dma_test_frame *tf = container_of(frame, typeof(*tf), frame);
0303     struct dma_test *dt = tf->dma_test;
0304     struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
0305 
0306     dma_unmap_single(dma_dev, tf->frame.buffer_phy, DMA_TEST_FRAME_SIZE,
0307              DMA_TO_DEVICE);
0308     kfree(tf->data);
0309     kfree(tf);
0310 }
0311 
0312 static int dma_test_submit_tx(struct dma_test *dt, size_t npackets)
0313 {
0314     struct device *dma_dev = tb_ring_dma_device(dt->tx_ring);
0315     int i;
0316 
0317     for (i = 0; i < npackets; i++) {
0318         struct dma_test_frame *tf;
0319         dma_addr_t dma_addr;
0320 
0321         tf = kzalloc(sizeof(*tf), GFP_KERNEL);
0322         if (!tf)
0323             return -ENOMEM;
0324 
0325         tf->frame.size = 0; /* means 4096 */
0326         tf->dma_test = dt;
0327 
0328         tf->data = kmemdup(dma_test_pattern, DMA_TEST_FRAME_SIZE, GFP_KERNEL);
0329         if (!tf->data) {
0330             kfree(tf);
0331             return -ENOMEM;
0332         }
0333 
0334         dma_addr = dma_map_single(dma_dev, tf->data, DMA_TEST_FRAME_SIZE,
0335                       DMA_TO_DEVICE);
0336         if (dma_mapping_error(dma_dev, dma_addr)) {
0337             kfree(tf->data);
0338             kfree(tf);
0339             return -ENOMEM;
0340         }
0341 
0342         tf->frame.buffer_phy = dma_addr;
0343         tf->frame.callback = dma_test_tx_callback;
0344         tf->frame.sof = DMA_TEST_PDF_FRAME_START;
0345         tf->frame.eof = DMA_TEST_PDF_FRAME_END;
0346         INIT_LIST_HEAD(&tf->frame.list);
0347 
0348         dt->packets_sent++;
0349         dev_dbg(&dt->svc->dev, "packet %u/%u sent\n", dt->packets_sent,
0350             dt->packets_to_send);
0351 
0352         tb_ring_tx(dt->tx_ring, &tf->frame);
0353     }
0354 
0355     return 0;
0356 }
0357 
0358 #define DMA_TEST_DEBUGFS_ATTR(__fops, __get, __validate, __set) \
0359 static int __fops ## _show(void *data, u64 *val)        \
0360 {                               \
0361     struct tb_service *svc = data;              \
0362     struct dma_test *dt = tb_service_get_drvdata(svc);  \
0363     int ret;                        \
0364                                 \
0365     ret = mutex_lock_interruptible(&dt->lock);      \
0366     if (ret)                        \
0367         return ret;                 \
0368     __get(dt, val);                     \
0369     mutex_unlock(&dt->lock);                \
0370     return 0;                       \
0371 }                               \
0372 static int __fops ## _store(void *data, u64 val)        \
0373 {                               \
0374     struct tb_service *svc = data;              \
0375     struct dma_test *dt = tb_service_get_drvdata(svc);  \
0376     int ret;                        \
0377                                 \
0378     ret = __validate(val);                  \
0379     if (ret)                        \
0380         return ret;                 \
0381     ret = mutex_lock_interruptible(&dt->lock);      \
0382     if (ret)                        \
0383         return ret;                 \
0384     __set(dt, val);                     \
0385     mutex_unlock(&dt->lock);                \
0386     return 0;                       \
0387 }                               \
0388 DEFINE_DEBUGFS_ATTRIBUTE(__fops ## _fops, __fops ## _show,  \
0389              __fops ## _store, "%llu\n")
0390 
0391 static void lanes_get(const struct dma_test *dt, u64 *val)
0392 {
0393     *val = dt->link_width;
0394 }
0395 
0396 static int lanes_validate(u64 val)
0397 {
0398     return val > 2 ? -EINVAL : 0;
0399 }
0400 
0401 static void lanes_set(struct dma_test *dt, u64 val)
0402 {
0403     dt->link_width = val;
0404 }
0405 DMA_TEST_DEBUGFS_ATTR(lanes, lanes_get, lanes_validate, lanes_set);
0406 
0407 static void speed_get(const struct dma_test *dt, u64 *val)
0408 {
0409     *val = dt->link_speed;
0410 }
0411 
0412 static int speed_validate(u64 val)
0413 {
0414     switch (val) {
0415     case 20:
0416     case 10:
0417     case 0:
0418         return 0;
0419     default:
0420         return -EINVAL;
0421     }
0422 }
0423 
0424 static void speed_set(struct dma_test *dt, u64 val)
0425 {
0426     dt->link_speed = val;
0427 }
0428 DMA_TEST_DEBUGFS_ATTR(speed, speed_get, speed_validate, speed_set);
0429 
0430 static void packets_to_receive_get(const struct dma_test *dt, u64 *val)
0431 {
0432     *val = dt->packets_to_receive;
0433 }
0434 
0435 static int packets_to_receive_validate(u64 val)
0436 {
0437     return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
0438 }
0439 
0440 static void packets_to_receive_set(struct dma_test *dt, u64 val)
0441 {
0442     dt->packets_to_receive = val;
0443 }
0444 DMA_TEST_DEBUGFS_ATTR(packets_to_receive, packets_to_receive_get,
0445               packets_to_receive_validate, packets_to_receive_set);
0446 
0447 static void packets_to_send_get(const struct dma_test *dt, u64 *val)
0448 {
0449     *val = dt->packets_to_send;
0450 }
0451 
0452 static int packets_to_send_validate(u64 val)
0453 {
0454     return val > DMA_TEST_MAX_PACKETS ? -EINVAL : 0;
0455 }
0456 
0457 static void packets_to_send_set(struct dma_test *dt, u64 val)
0458 {
0459     dt->packets_to_send = val;
0460 }
0461 DMA_TEST_DEBUGFS_ATTR(packets_to_send, packets_to_send_get,
0462               packets_to_send_validate, packets_to_send_set);
0463 
0464 static int dma_test_set_bonding(struct dma_test *dt)
0465 {
0466     switch (dt->link_width) {
0467     case 2:
0468         return tb_xdomain_lane_bonding_enable(dt->xd);
0469     case 1:
0470         tb_xdomain_lane_bonding_disable(dt->xd);
0471         fallthrough;
0472     default:
0473         return 0;
0474     }
0475 }
0476 
0477 static bool dma_test_validate_config(struct dma_test *dt)
0478 {
0479     if (!dt->packets_to_send && !dt->packets_to_receive)
0480         return false;
0481     if (dt->packets_to_send && dt->packets_to_receive &&
0482         dt->packets_to_send != dt->packets_to_receive)
0483         return false;
0484     return true;
0485 }
0486 
0487 static void dma_test_check_errors(struct dma_test *dt, int ret)
0488 {
0489     if (!dt->error_code) {
0490         if (dt->link_speed && dt->xd->link_speed != dt->link_speed) {
0491             dt->error_code = DMA_TEST_SPEED_ERROR;
0492         } else if (dt->link_width &&
0493                dt->xd->link_width != dt->link_width) {
0494             dt->error_code = DMA_TEST_WIDTH_ERROR;
0495         } else if (dt->packets_to_send != dt->packets_sent ||
0496              dt->packets_to_receive != dt->packets_received ||
0497              dt->crc_errors || dt->buffer_overflow_errors) {
0498             dt->error_code = DMA_TEST_PACKET_ERROR;
0499         } else {
0500             return;
0501         }
0502     }
0503 
0504     dt->result = DMA_TEST_FAIL;
0505 }
0506 
0507 static int test_store(void *data, u64 val)
0508 {
0509     struct tb_service *svc = data;
0510     struct dma_test *dt = tb_service_get_drvdata(svc);
0511     int ret;
0512 
0513     if (val != 1)
0514         return -EINVAL;
0515 
0516     ret = mutex_lock_interruptible(&dt->lock);
0517     if (ret)
0518         return ret;
0519 
0520     dt->packets_sent = 0;
0521     dt->packets_received = 0;
0522     dt->crc_errors = 0;
0523     dt->buffer_overflow_errors = 0;
0524     dt->result = DMA_TEST_SUCCESS;
0525     dt->error_code = DMA_TEST_NO_ERROR;
0526 
0527     dev_dbg(&svc->dev, "DMA test starting\n");
0528     if (dt->link_speed)
0529         dev_dbg(&svc->dev, "link_speed: %u Gb/s\n", dt->link_speed);
0530     if (dt->link_width)
0531         dev_dbg(&svc->dev, "link_width: %u\n", dt->link_width);
0532     dev_dbg(&svc->dev, "packets_to_send: %u\n", dt->packets_to_send);
0533     dev_dbg(&svc->dev, "packets_to_receive: %u\n", dt->packets_to_receive);
0534 
0535     if (!dma_test_validate_config(dt)) {
0536         dev_err(&svc->dev, "invalid test configuration\n");
0537         dt->error_code = DMA_TEST_CONFIG_ERROR;
0538         goto out_unlock;
0539     }
0540 
0541     ret = dma_test_set_bonding(dt);
0542     if (ret) {
0543         dev_err(&svc->dev, "failed to set lanes\n");
0544         dt->error_code = DMA_TEST_BONDING_ERROR;
0545         goto out_unlock;
0546     }
0547 
0548     ret = dma_test_start_rings(dt);
0549     if (ret) {
0550         dev_err(&svc->dev, "failed to enable DMA rings\n");
0551         dt->error_code = DMA_TEST_DMA_ERROR;
0552         goto out_unlock;
0553     }
0554 
0555     if (dt->packets_to_receive) {
0556         reinit_completion(&dt->complete);
0557         ret = dma_test_submit_rx(dt, dt->packets_to_receive);
0558         if (ret) {
0559             dev_err(&svc->dev, "failed to submit receive buffers\n");
0560             dt->error_code = DMA_TEST_BUFFER_ERROR;
0561             goto out_stop;
0562         }
0563     }
0564 
0565     if (dt->packets_to_send) {
0566         ret = dma_test_submit_tx(dt, dt->packets_to_send);
0567         if (ret) {
0568             dev_err(&svc->dev, "failed to submit transmit buffers\n");
0569             dt->error_code = DMA_TEST_BUFFER_ERROR;
0570             goto out_stop;
0571         }
0572     }
0573 
0574     if (dt->packets_to_receive) {
0575         ret = wait_for_completion_interruptible(&dt->complete);
0576         if (ret) {
0577             dt->error_code = DMA_TEST_INTERRUPTED;
0578             goto out_stop;
0579         }
0580     }
0581 
0582 out_stop:
0583     dma_test_stop_rings(dt);
0584 out_unlock:
0585     dma_test_check_errors(dt, ret);
0586     mutex_unlock(&dt->lock);
0587 
0588     dev_dbg(&svc->dev, "DMA test %s\n", dma_test_result_names[dt->result]);
0589     return ret;
0590 }
0591 DEFINE_DEBUGFS_ATTRIBUTE(test_fops, NULL, test_store, "%llu\n");
0592 
0593 static int status_show(struct seq_file *s, void *not_used)
0594 {
0595     struct tb_service *svc = s->private;
0596     struct dma_test *dt = tb_service_get_drvdata(svc);
0597     int ret;
0598 
0599     ret = mutex_lock_interruptible(&dt->lock);
0600     if (ret)
0601         return ret;
0602 
0603     seq_printf(s, "result: %s\n", dma_test_result_names[dt->result]);
0604     if (dt->result == DMA_TEST_NOT_RUN)
0605         goto out_unlock;
0606 
0607     seq_printf(s, "packets received: %u\n", dt->packets_received);
0608     seq_printf(s, "packets sent: %u\n", dt->packets_sent);
0609     seq_printf(s, "CRC errors: %u\n", dt->crc_errors);
0610     seq_printf(s, "buffer overflow errors: %u\n",
0611            dt->buffer_overflow_errors);
0612     seq_printf(s, "error: %s\n", dma_test_error_names[dt->error_code]);
0613 
0614 out_unlock:
0615     mutex_unlock(&dt->lock);
0616     return 0;
0617 }
0618 DEFINE_SHOW_ATTRIBUTE(status);
0619 
0620 static void dma_test_debugfs_init(struct tb_service *svc)
0621 {
0622     struct dma_test *dt = tb_service_get_drvdata(svc);
0623 
0624     dt->debugfs_dir = debugfs_create_dir("dma_test", svc->debugfs_dir);
0625 
0626     debugfs_create_file("lanes", 0600, dt->debugfs_dir, svc, &lanes_fops);
0627     debugfs_create_file("speed", 0600, dt->debugfs_dir, svc, &speed_fops);
0628     debugfs_create_file("packets_to_receive", 0600, dt->debugfs_dir, svc,
0629                 &packets_to_receive_fops);
0630     debugfs_create_file("packets_to_send", 0600, dt->debugfs_dir, svc,
0631                 &packets_to_send_fops);
0632     debugfs_create_file("status", 0400, dt->debugfs_dir, svc, &status_fops);
0633     debugfs_create_file("test", 0200, dt->debugfs_dir, svc, &test_fops);
0634 }
0635 
0636 static int dma_test_probe(struct tb_service *svc, const struct tb_service_id *id)
0637 {
0638     struct tb_xdomain *xd = tb_service_parent(svc);
0639     struct dma_test *dt;
0640 
0641     dt = devm_kzalloc(&svc->dev, sizeof(*dt), GFP_KERNEL);
0642     if (!dt)
0643         return -ENOMEM;
0644 
0645     dt->svc = svc;
0646     dt->xd = xd;
0647     mutex_init(&dt->lock);
0648     init_completion(&dt->complete);
0649 
0650     tb_service_set_drvdata(svc, dt);
0651     dma_test_debugfs_init(svc);
0652 
0653     return 0;
0654 }
0655 
0656 static void dma_test_remove(struct tb_service *svc)
0657 {
0658     struct dma_test *dt = tb_service_get_drvdata(svc);
0659 
0660     mutex_lock(&dt->lock);
0661     debugfs_remove_recursive(dt->debugfs_dir);
0662     mutex_unlock(&dt->lock);
0663 }
0664 
0665 static int __maybe_unused dma_test_suspend(struct device *dev)
0666 {
0667     /*
0668      * No need to do anything special here. If userspace is writing
0669      * to the test attribute when suspend started, it comes out from
0670      * wait_for_completion_interruptible() with -ERESTARTSYS and the
0671      * DMA test fails tearing down the rings. Once userspace is
0672      * thawed the kernel restarts the write syscall effectively
0673      * re-running the test.
0674      */
0675     return 0;
0676 }
0677 
0678 static int __maybe_unused dma_test_resume(struct device *dev)
0679 {
0680     return 0;
0681 }
0682 
0683 static const struct dev_pm_ops dma_test_pm_ops = {
0684     SET_SYSTEM_SLEEP_PM_OPS(dma_test_suspend, dma_test_resume)
0685 };
0686 
0687 static const struct tb_service_id dma_test_ids[] = {
0688     { TB_SERVICE("dma_test", 1) },
0689     { },
0690 };
0691 MODULE_DEVICE_TABLE(tbsvc, dma_test_ids);
0692 
0693 static struct tb_service_driver dma_test_driver = {
0694     .driver = {
0695         .owner = THIS_MODULE,
0696         .name = "thunderbolt_dma_test",
0697         .pm = &dma_test_pm_ops,
0698     },
0699     .probe = dma_test_probe,
0700     .remove = dma_test_remove,
0701     .id_table = dma_test_ids,
0702 };
0703 
0704 static int __init dma_test_init(void)
0705 {
0706     u64 data_value = DMA_TEST_DATA_PATTERN;
0707     int i, ret;
0708 
0709     dma_test_pattern = kmalloc(DMA_TEST_FRAME_SIZE, GFP_KERNEL);
0710     if (!dma_test_pattern)
0711         return -ENOMEM;
0712 
0713     for (i = 0; i < DMA_TEST_FRAME_SIZE / sizeof(data_value); i++)
0714         ((u32 *)dma_test_pattern)[i] = data_value++;
0715 
0716     dma_test_dir = tb_property_create_dir(&dma_test_dir_uuid);
0717     if (!dma_test_dir) {
0718         ret = -ENOMEM;
0719         goto err_free_pattern;
0720     }
0721 
0722     tb_property_add_immediate(dma_test_dir, "prtcid", 1);
0723     tb_property_add_immediate(dma_test_dir, "prtcvers", 1);
0724     tb_property_add_immediate(dma_test_dir, "prtcrevs", 0);
0725     tb_property_add_immediate(dma_test_dir, "prtcstns", 0);
0726 
0727     ret = tb_register_property_dir("dma_test", dma_test_dir);
0728     if (ret)
0729         goto err_free_dir;
0730 
0731     ret = tb_register_service_driver(&dma_test_driver);
0732     if (ret)
0733         goto err_unregister_dir;
0734 
0735     return 0;
0736 
0737 err_unregister_dir:
0738     tb_unregister_property_dir("dma_test", dma_test_dir);
0739 err_free_dir:
0740     tb_property_free_dir(dma_test_dir);
0741 err_free_pattern:
0742     kfree(dma_test_pattern);
0743 
0744     return ret;
0745 }
0746 module_init(dma_test_init);
0747 
0748 static void __exit dma_test_exit(void)
0749 {
0750     tb_unregister_service_driver(&dma_test_driver);
0751     tb_unregister_property_dir("dma_test", dma_test_dir);
0752     tb_property_free_dir(dma_test_dir);
0753     kfree(dma_test_pattern);
0754 }
0755 module_exit(dma_test_exit);
0756 
0757 MODULE_AUTHOR("Isaac Hazan <isaac.hazan@intel.com>");
0758 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
0759 MODULE_DESCRIPTION("DMA traffic test driver");
0760 MODULE_LICENSE("GPL v2");