Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Driver for the NXP SAA7164 PCIe bridge
0004  *
0005  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
0006  */
0007 
0008 #include <linux/wait.h>
0009 
0010 #include "saa7164.h"
0011 
0012 static int saa7164_cmd_alloc_seqno(struct saa7164_dev *dev)
0013 {
0014     int i, ret = -1;
0015 
0016     mutex_lock(&dev->lock);
0017     for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
0018         if (dev->cmds[i].inuse == 0) {
0019             dev->cmds[i].inuse = 1;
0020             dev->cmds[i].signalled = 0;
0021             dev->cmds[i].timeout = 0;
0022             ret = dev->cmds[i].seqno;
0023             break;
0024         }
0025     }
0026     mutex_unlock(&dev->lock);
0027 
0028     return ret;
0029 }
0030 
0031 static void saa7164_cmd_free_seqno(struct saa7164_dev *dev, u8 seqno)
0032 {
0033     mutex_lock(&dev->lock);
0034     if ((dev->cmds[seqno].inuse == 1) &&
0035         (dev->cmds[seqno].seqno == seqno)) {
0036         dev->cmds[seqno].inuse = 0;
0037         dev->cmds[seqno].signalled = 0;
0038         dev->cmds[seqno].timeout = 0;
0039     }
0040     mutex_unlock(&dev->lock);
0041 }
0042 
0043 static void saa7164_cmd_timeout_seqno(struct saa7164_dev *dev, u8 seqno)
0044 {
0045     mutex_lock(&dev->lock);
0046     if ((dev->cmds[seqno].inuse == 1) &&
0047         (dev->cmds[seqno].seqno == seqno)) {
0048         dev->cmds[seqno].timeout = 1;
0049     }
0050     mutex_unlock(&dev->lock);
0051 }
0052 
0053 static u32 saa7164_cmd_timeout_get(struct saa7164_dev *dev, u8 seqno)
0054 {
0055     int ret = 0;
0056 
0057     mutex_lock(&dev->lock);
0058     if ((dev->cmds[seqno].inuse == 1) &&
0059         (dev->cmds[seqno].seqno == seqno)) {
0060         ret = dev->cmds[seqno].timeout;
0061     }
0062     mutex_unlock(&dev->lock);
0063 
0064     return ret;
0065 }
0066 
0067 /* Commands to the f/w get marshelled to/from this code then onto the PCI
0068  * -bus/c running buffer. */
0069 int saa7164_irq_dequeue(struct saa7164_dev *dev)
0070 {
0071     int ret = SAA_OK, i = 0;
0072     u32 timeout;
0073     wait_queue_head_t *q = NULL;
0074     u8 tmp[512];
0075     dprintk(DBGLVL_CMD, "%s()\n", __func__);
0076 
0077     /* While any outstand message on the bus exists... */
0078     do {
0079 
0080         /* Peek the msg bus */
0081         struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
0082         ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
0083         if (ret != SAA_OK)
0084             break;
0085 
0086         q = &dev->cmds[tRsp.seqno].wait;
0087         timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
0088         dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
0089         if (!timeout) {
0090             dprintk(DBGLVL_CMD,
0091                 "%s() signalled seqno(%d) (for dequeue)\n",
0092                 __func__, tRsp.seqno);
0093             dev->cmds[tRsp.seqno].signalled = 1;
0094             wake_up(q);
0095         } else {
0096             printk(KERN_ERR
0097                 "%s() found timed out command on the bus\n",
0098                     __func__);
0099 
0100             /* Clean the bus */
0101             ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
0102             printk(KERN_ERR "%s() ret = %x\n", __func__, ret);
0103             if (ret == SAA_ERR_EMPTY)
0104                 /* Someone else already fetched the response */
0105                 return SAA_OK;
0106 
0107             if (ret != SAA_OK)
0108                 return ret;
0109         }
0110 
0111         /* It's unlikely to have more than 4 or 5 pending messages,
0112          * ensure we exit at some point regardless.
0113          */
0114     } while (i++ < 32);
0115 
0116     return ret;
0117 }
0118 
0119 /* Commands to the f/w get marshelled to/from this code then onto the PCI
0120  * -bus/c running buffer. */
0121 static int saa7164_cmd_dequeue(struct saa7164_dev *dev)
0122 {
0123     int ret;
0124     u32 timeout;
0125     wait_queue_head_t *q = NULL;
0126     u8 tmp[512];
0127     dprintk(DBGLVL_CMD, "%s()\n", __func__);
0128 
0129     while (true) {
0130 
0131         struct tmComResInfo tRsp = { 0, 0, 0, 0, 0, 0 };
0132         ret = saa7164_bus_get(dev, &tRsp, NULL, 1);
0133         if (ret == SAA_ERR_EMPTY)
0134             return SAA_OK;
0135 
0136         if (ret != SAA_OK)
0137             return ret;
0138 
0139         q = &dev->cmds[tRsp.seqno].wait;
0140         timeout = saa7164_cmd_timeout_get(dev, tRsp.seqno);
0141         dprintk(DBGLVL_CMD, "%s() timeout = %d\n", __func__, timeout);
0142         if (timeout) {
0143             printk(KERN_ERR "found timed out command on the bus\n");
0144 
0145             /* Clean the bus */
0146             ret = saa7164_bus_get(dev, &tRsp, &tmp, 0);
0147             printk(KERN_ERR "ret = %x\n", ret);
0148             if (ret == SAA_ERR_EMPTY)
0149                 /* Someone else already fetched the response */
0150                 return SAA_OK;
0151 
0152             if (ret != SAA_OK)
0153                 return ret;
0154 
0155             if (tRsp.flags & PVC_CMDFLAG_CONTINUE)
0156                 printk(KERN_ERR "split response\n");
0157             else
0158                 saa7164_cmd_free_seqno(dev, tRsp.seqno);
0159 
0160             printk(KERN_ERR " timeout continue\n");
0161             continue;
0162         }
0163 
0164         dprintk(DBGLVL_CMD, "%s() signalled seqno(%d) (for dequeue)\n",
0165             __func__, tRsp.seqno);
0166         dev->cmds[tRsp.seqno].signalled = 1;
0167         wake_up(q);
0168         return SAA_OK;
0169     }
0170 }
0171 
0172 static int saa7164_cmd_set(struct saa7164_dev *dev, struct tmComResInfo *msg,
0173                void *buf)
0174 {
0175     struct tmComResBusInfo *bus = &dev->bus;
0176     u8 cmd_sent;
0177     u16 size, idx;
0178     u32 cmds;
0179     void *tmp;
0180     int ret = -1;
0181 
0182     if (!msg) {
0183         printk(KERN_ERR "%s() !msg\n", __func__);
0184         return SAA_ERR_BAD_PARAMETER;
0185     }
0186 
0187     mutex_lock(&dev->cmds[msg->id].lock);
0188 
0189     size = msg->size;
0190     cmds = size / bus->m_wMaxReqSize;
0191     if (size % bus->m_wMaxReqSize == 0)
0192         cmds -= 1;
0193 
0194     cmd_sent = 0;
0195 
0196     /* Split the request into smaller chunks */
0197     for (idx = 0; idx < cmds; idx++) {
0198 
0199         msg->flags |= SAA_CMDFLAG_CONTINUE;
0200         msg->size = bus->m_wMaxReqSize;
0201         tmp = buf + idx * bus->m_wMaxReqSize;
0202 
0203         ret = saa7164_bus_set(dev, msg, tmp);
0204         if (ret != SAA_OK) {
0205             printk(KERN_ERR "%s() set failed %d\n", __func__, ret);
0206 
0207             if (cmd_sent) {
0208                 ret = SAA_ERR_BUSY;
0209                 goto out;
0210             }
0211             ret = SAA_ERR_OVERFLOW;
0212             goto out;
0213         }
0214         cmd_sent = 1;
0215     }
0216 
0217     /* If not the last command... */
0218     if (idx != 0)
0219         msg->flags &= ~SAA_CMDFLAG_CONTINUE;
0220 
0221     msg->size = size - idx * bus->m_wMaxReqSize;
0222 
0223     ret = saa7164_bus_set(dev, msg, buf + idx * bus->m_wMaxReqSize);
0224     if (ret != SAA_OK) {
0225         printk(KERN_ERR "%s() set last failed %d\n", __func__, ret);
0226 
0227         if (cmd_sent) {
0228             ret = SAA_ERR_BUSY;
0229             goto out;
0230         }
0231         ret = SAA_ERR_OVERFLOW;
0232         goto out;
0233     }
0234     ret = SAA_OK;
0235 
0236 out:
0237     mutex_unlock(&dev->cmds[msg->id].lock);
0238     return ret;
0239 }
0240 
0241 /* Wait for a signal event, without holding a mutex. Either return TIMEOUT if
0242  * the event never occurred, or SAA_OK if it was signaled during the wait.
0243  */
0244 static int saa7164_cmd_wait(struct saa7164_dev *dev, u8 seqno)
0245 {
0246     wait_queue_head_t *q = NULL;
0247     int ret = SAA_BUS_TIMEOUT;
0248     unsigned long stamp;
0249     int r;
0250 
0251     if (saa_debug >= 4)
0252         saa7164_bus_dump(dev);
0253 
0254     dprintk(DBGLVL_CMD, "%s(seqno=%d)\n", __func__, seqno);
0255 
0256     mutex_lock(&dev->lock);
0257     if ((dev->cmds[seqno].inuse == 1) &&
0258         (dev->cmds[seqno].seqno == seqno)) {
0259         q = &dev->cmds[seqno].wait;
0260     }
0261     mutex_unlock(&dev->lock);
0262 
0263     if (q) {
0264         /* If we haven't been signalled we need to wait */
0265         if (dev->cmds[seqno].signalled == 0) {
0266             stamp = jiffies;
0267             dprintk(DBGLVL_CMD,
0268                 "%s(seqno=%d) Waiting (signalled=%d)\n",
0269                 __func__, seqno, dev->cmds[seqno].signalled);
0270 
0271             /* Wait for signalled to be flagged or timeout */
0272             /* In a highly stressed system this can easily extend
0273              * into multiple seconds before the deferred worker
0274              * is scheduled, and we're woken up via signal.
0275              * We typically are signalled in < 50ms but it can
0276              * take MUCH longer.
0277              */
0278             wait_event_timeout(*q, dev->cmds[seqno].signalled,
0279                 (HZ * waitsecs));
0280             r = time_before(jiffies, stamp + (HZ * waitsecs));
0281             if (r)
0282                 ret = SAA_OK;
0283             else
0284                 saa7164_cmd_timeout_seqno(dev, seqno);
0285 
0286             dprintk(DBGLVL_CMD, "%s(seqno=%d) Waiting res = %d (signalled=%d)\n",
0287                 __func__, seqno, r,
0288                 dev->cmds[seqno].signalled);
0289         } else
0290             ret = SAA_OK;
0291     } else
0292         printk(KERN_ERR "%s(seqno=%d) seqno is invalid\n",
0293             __func__, seqno);
0294 
0295     return ret;
0296 }
0297 
0298 void saa7164_cmd_signal(struct saa7164_dev *dev, u8 seqno)
0299 {
0300     int i;
0301     dprintk(DBGLVL_CMD, "%s()\n", __func__);
0302 
0303     mutex_lock(&dev->lock);
0304     for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
0305         if (dev->cmds[i].inuse == 1) {
0306             dprintk(DBGLVL_CMD,
0307                 "seqno %d inuse, sig = %d, t/out = %d\n",
0308                 dev->cmds[i].seqno,
0309                 dev->cmds[i].signalled,
0310                 dev->cmds[i].timeout);
0311         }
0312     }
0313 
0314     for (i = 0; i < SAA_CMD_MAX_MSG_UNITS; i++) {
0315         if ((dev->cmds[i].inuse == 1) && ((i == 0) ||
0316             (dev->cmds[i].signalled) || (dev->cmds[i].timeout))) {
0317             dprintk(DBGLVL_CMD, "%s(seqno=%d) calling wake_up\n",
0318                 __func__, i);
0319             dev->cmds[i].signalled = 1;
0320             wake_up(&dev->cmds[i].wait);
0321         }
0322     }
0323     mutex_unlock(&dev->lock);
0324 }
0325 
0326 int saa7164_cmd_send(struct saa7164_dev *dev, u8 id, enum tmComResCmd command,
0327     u16 controlselector, u16 size, void *buf)
0328 {
0329     struct tmComResInfo command_t, *pcommand_t;
0330     struct tmComResInfo response_t, *presponse_t;
0331     u8 errdata[256];
0332     u16 resp_dsize;
0333     u16 data_recd;
0334     u32 loop;
0335     int ret;
0336     int safety = 0;
0337 
0338     dprintk(DBGLVL_CMD, "%s(unitid = %s (%d) , command = 0x%x, sel = 0x%x)\n",
0339         __func__, saa7164_unitid_name(dev, id), id,
0340         command, controlselector);
0341 
0342     if ((size == 0) || (buf == NULL)) {
0343         printk(KERN_ERR "%s() Invalid param\n", __func__);
0344         return SAA_ERR_BAD_PARAMETER;
0345     }
0346 
0347     /* Prepare some basic command/response structures */
0348     memset(&command_t, 0, sizeof(command_t));
0349     memset(&response_t, 0, sizeof(response_t));
0350     pcommand_t = &command_t;
0351     presponse_t = &response_t;
0352     command_t.id = id;
0353     command_t.command = command;
0354     command_t.controlselector = controlselector;
0355     command_t.size = size;
0356 
0357     /* Allocate a unique sequence number */
0358     ret = saa7164_cmd_alloc_seqno(dev);
0359     if (ret < 0) {
0360         printk(KERN_ERR "%s() No free sequences\n", __func__);
0361         ret = SAA_ERR_NO_RESOURCES;
0362         goto out;
0363     }
0364 
0365     command_t.seqno = (u8)ret;
0366 
0367     /* Send Command */
0368     resp_dsize = size;
0369     pcommand_t->size = size;
0370 
0371     dprintk(DBGLVL_CMD, "%s() pcommand_t.seqno = %d\n",
0372         __func__, pcommand_t->seqno);
0373 
0374     dprintk(DBGLVL_CMD, "%s() pcommand_t.size = %d\n",
0375         __func__, pcommand_t->size);
0376 
0377     ret = saa7164_cmd_set(dev, pcommand_t, buf);
0378     if (ret != SAA_OK) {
0379         printk(KERN_ERR "%s() set command failed %d\n", __func__, ret);
0380 
0381         if (ret != SAA_ERR_BUSY)
0382             saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
0383         else
0384             /* Flag a timeout, because at least one
0385              * command was sent */
0386             saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
0387 
0388         goto out;
0389     }
0390 
0391     /* With split responses we have to collect the msgs piece by piece */
0392     data_recd = 0;
0393     loop = 1;
0394     while (loop) {
0395         dprintk(DBGLVL_CMD, "%s() loop\n", __func__);
0396 
0397         ret = saa7164_cmd_wait(dev, pcommand_t->seqno);
0398         dprintk(DBGLVL_CMD, "%s() loop ret = %d\n", __func__, ret);
0399 
0400         /* if power is down and this is not a power command ... */
0401 
0402         if (ret == SAA_BUS_TIMEOUT) {
0403             printk(KERN_ERR "Event timed out\n");
0404             saa7164_cmd_timeout_seqno(dev, pcommand_t->seqno);
0405             return ret;
0406         }
0407 
0408         if (ret != SAA_OK) {
0409             printk(KERN_ERR "spurious error\n");
0410             return ret;
0411         }
0412 
0413         /* Peek response */
0414         ret = saa7164_bus_get(dev, presponse_t, NULL, 1);
0415         if (ret == SAA_ERR_EMPTY) {
0416             dprintk(4, "%s() SAA_ERR_EMPTY\n", __func__);
0417             continue;
0418         }
0419         if (ret != SAA_OK) {
0420             printk(KERN_ERR "peek failed\n");
0421             return ret;
0422         }
0423 
0424         dprintk(DBGLVL_CMD, "%s() presponse_t->seqno = %d\n",
0425             __func__, presponse_t->seqno);
0426 
0427         dprintk(DBGLVL_CMD, "%s() presponse_t->flags = 0x%x\n",
0428             __func__, presponse_t->flags);
0429 
0430         dprintk(DBGLVL_CMD, "%s() presponse_t->size = %d\n",
0431             __func__, presponse_t->size);
0432 
0433         /* Check if the response was for our command */
0434         if (presponse_t->seqno != pcommand_t->seqno) {
0435 
0436             dprintk(DBGLVL_CMD,
0437                 "wrong event: seqno = %d, expected seqno = %d, will dequeue regardless\n",
0438                 presponse_t->seqno, pcommand_t->seqno);
0439 
0440             ret = saa7164_cmd_dequeue(dev);
0441             if (ret != SAA_OK) {
0442                 printk(KERN_ERR "dequeue failed, ret = %d\n",
0443                     ret);
0444                 if (safety++ > 16) {
0445                     printk(KERN_ERR
0446                     "dequeue exceeded, safety exit\n");
0447                     return SAA_ERR_BUSY;
0448                 }
0449             }
0450 
0451             continue;
0452         }
0453 
0454         if ((presponse_t->flags & PVC_RESPONSEFLAG_ERROR) != 0) {
0455 
0456             memset(&errdata[0], 0, sizeof(errdata));
0457 
0458             ret = saa7164_bus_get(dev, presponse_t, &errdata[0], 0);
0459             if (ret != SAA_OK) {
0460                 printk(KERN_ERR "get error(2)\n");
0461                 return ret;
0462             }
0463 
0464             saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
0465 
0466             dprintk(DBGLVL_CMD, "%s() errdata %02x%02x%02x%02x\n",
0467                 __func__, errdata[0], errdata[1], errdata[2],
0468                 errdata[3]);
0469 
0470             /* Map error codes */
0471             dprintk(DBGLVL_CMD, "%s() cmd, error code  = 0x%x\n",
0472                 __func__, errdata[0]);
0473 
0474             switch (errdata[0]) {
0475             case PVC_ERRORCODE_INVALID_COMMAND:
0476                 dprintk(DBGLVL_CMD, "%s() INVALID_COMMAND\n",
0477                     __func__);
0478                 ret = SAA_ERR_INVALID_COMMAND;
0479                 break;
0480             case PVC_ERRORCODE_INVALID_DATA:
0481                 dprintk(DBGLVL_CMD, "%s() INVALID_DATA\n",
0482                     __func__);
0483                 ret = SAA_ERR_BAD_PARAMETER;
0484                 break;
0485             case PVC_ERRORCODE_TIMEOUT:
0486                 dprintk(DBGLVL_CMD, "%s() TIMEOUT\n", __func__);
0487                 ret = SAA_ERR_TIMEOUT;
0488                 break;
0489             case PVC_ERRORCODE_NAK:
0490                 dprintk(DBGLVL_CMD, "%s() NAK\n", __func__);
0491                 ret = SAA_ERR_NULL_PACKET;
0492                 break;
0493             case PVC_ERRORCODE_UNKNOWN:
0494             case PVC_ERRORCODE_INVALID_CONTROL:
0495                 dprintk(DBGLVL_CMD,
0496                     "%s() UNKNOWN OR INVALID CONTROL\n",
0497                     __func__);
0498                 ret = SAA_ERR_NOT_SUPPORTED;
0499                 break;
0500             default:
0501                 dprintk(DBGLVL_CMD, "%s() UNKNOWN\n", __func__);
0502                 ret = SAA_ERR_NOT_SUPPORTED;
0503             }
0504 
0505             /* See of other commands are on the bus */
0506             if (saa7164_cmd_dequeue(dev) != SAA_OK)
0507                 printk(KERN_ERR "dequeue(2) failed\n");
0508 
0509             return ret;
0510         }
0511 
0512         /* If response is invalid */
0513         if ((presponse_t->id != pcommand_t->id) ||
0514             (presponse_t->command != pcommand_t->command) ||
0515             (presponse_t->controlselector !=
0516                 pcommand_t->controlselector) ||
0517             (((resp_dsize - data_recd) != presponse_t->size) &&
0518                 !(presponse_t->flags & PVC_CMDFLAG_CONTINUE)) ||
0519             ((resp_dsize - data_recd) < presponse_t->size)) {
0520 
0521             /* Invalid */
0522             dprintk(DBGLVL_CMD, "%s() Invalid\n", __func__);
0523             ret = saa7164_bus_get(dev, presponse_t, NULL, 0);
0524             if (ret != SAA_OK) {
0525                 printk(KERN_ERR "get failed\n");
0526                 return ret;
0527             }
0528 
0529             /* See of other commands are on the bus */
0530             if (saa7164_cmd_dequeue(dev) != SAA_OK)
0531                 printk(KERN_ERR "dequeue(3) failed\n");
0532             continue;
0533         }
0534 
0535         /* OK, now we're actually getting out correct response */
0536         ret = saa7164_bus_get(dev, presponse_t, buf + data_recd, 0);
0537         if (ret != SAA_OK) {
0538             printk(KERN_ERR "get failed\n");
0539             return ret;
0540         }
0541 
0542         data_recd = presponse_t->size + data_recd;
0543         if (resp_dsize == data_recd) {
0544             dprintk(DBGLVL_CMD, "%s() Resp recd\n", __func__);
0545             break;
0546         }
0547 
0548         /* See of other commands are on the bus */
0549         if (saa7164_cmd_dequeue(dev) != SAA_OK)
0550             printk(KERN_ERR "dequeue(3) failed\n");
0551     } /* (loop) */
0552 
0553     /* Release the sequence number allocation */
0554     saa7164_cmd_free_seqno(dev, pcommand_t->seqno);
0555 
0556     /* if powerdown signal all pending commands */
0557 
0558     dprintk(DBGLVL_CMD, "%s() Calling dequeue then exit\n", __func__);
0559 
0560     /* See of other commands are on the bus */
0561     if (saa7164_cmd_dequeue(dev) != SAA_OK)
0562         printk(KERN_ERR "dequeue(4) failed\n");
0563 
0564     ret = SAA_OK;
0565 out:
0566     return ret;
0567 }
0568