aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIra W. Snyder <iws@ovro.caltech.edu>2012-07-19 11:54:42 -0400
committerMarc Kleine-Budde <mkl@pengutronix.de>2012-07-20 11:49:04 -0400
commit83702f69272e4591a91a27eb58eade1bcd361dae (patch)
tree7e0c4a6c702c3fd5e11b553099998b6cba21bd44
parent88b587039c1ad4e7a981bea3269eeb02a1a2a14b (diff)
can: janz-ican3: fix support for CAN_RAW_RECV_OWN_MSGS
The Janz VMOD-ICAN3 firmware does not support any sort of TX-done notification or interrupt. The driver previously used the hardware loopback to attempt to work around this deficiency, but this caused all sockets to receive all messages, even if CAN_RAW_RECV_OWN_MSGS is off. Using the new function ican3_cmp_echo_skb(), we can drop the loopback messages and return the original skbs. This fixes the issues with CAN_RAW_RECV_OWN_MSGS. A private skb queue is used to store the echo skbs. This avoids the need for any index management. Due to a lack of TX-error interrupts, bus errors are permanently enabled, and are used as a TX-error notification. This is used to drop an echo skb when transmission fails. Bus error packets are not generated if the user has not enabled bus error reporting. Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r--drivers/net/can/janz-ican3.c203
1 files changed, 157 insertions, 46 deletions
diff --git a/drivers/net/can/janz-ican3.c b/drivers/net/can/janz-ican3.c
index 4a5a8fb53a2f..47f8f6b4fef9 100644
--- a/drivers/net/can/janz-ican3.c
+++ b/drivers/net/can/janz-ican3.c
@@ -220,6 +220,9 @@ struct ican3_dev {
220 /* old and new style host interface */ 220 /* old and new style host interface */
221 unsigned int iftype; 221 unsigned int iftype;
222 222
223 /* queue for echo packets */
224 struct sk_buff_head echoq;
225
223 /* 226 /*
224 * Any function which changes the current DPM page must hold this 227 * Any function which changes the current DPM page must hold this
225 * lock while it is performing data accesses. This ensures that the 228 * lock while it is performing data accesses. This ensures that the
@@ -925,7 +928,7 @@ static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
925 struct net_device *dev = mod->ndev; 928 struct net_device *dev = mod->ndev;
926 struct net_device_stats *stats = &dev->stats; 929 struct net_device_stats *stats = &dev->stats;
927 enum can_state state = mod->can.state; 930 enum can_state state = mod->can.state;
928 u8 status, isrc, rxerr, txerr; 931 u8 isrc, ecc, status, rxerr, txerr;
929 struct can_frame *cf; 932 struct can_frame *cf;
930 struct sk_buff *skb; 933 struct sk_buff *skb;
931 934
@@ -941,15 +944,43 @@ static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
941 return -EINVAL; 944 return -EINVAL;
942 } 945 }
943 946
944 skb = alloc_can_err_skb(dev, &cf);
945 if (skb == NULL)
946 return -ENOMEM;
947
948 isrc = msg->data[0]; 947 isrc = msg->data[0];
948 ecc = msg->data[2];
949 status = msg->data[3]; 949 status = msg->data[3];
950 rxerr = msg->data[4]; 950 rxerr = msg->data[4];
951 txerr = msg->data[5]; 951 txerr = msg->data[5];
952 952
953 /*
954 * This hardware lacks any support other than bus error messages to
955 * determine if packet transmission has failed.
956 *
957 * When TX errors happen, one echo skb needs to be dropped from the
958 * front of the queue.
959 *
960 * A small bit of code is duplicated here and below, to avoid error
961 * skb allocation when it will just be freed immediately.
962 */
963 if (isrc == CEVTIND_BEI) {
964 int ret;
965 dev_dbg(mod->dev, "bus error interrupt\n");
966
967 /* TX error */
968 if (!(ecc & ECC_DIR)) {
969 kfree_skb(skb_dequeue(&mod->echoq));
970 stats->tx_errors++;
971 } else {
972 stats->rx_errors++;
973 }
974
975 /* bus error reporting is off, return immediately */
976 if (!(mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
977 return 0;
978 }
979
980 skb = alloc_can_err_skb(dev, &cf);
981 if (skb == NULL)
982 return -ENOMEM;
983
953 /* data overrun interrupt */ 984 /* data overrun interrupt */
954 if (isrc == CEVTIND_DOI || isrc == CEVTIND_LOST) { 985 if (isrc == CEVTIND_DOI || isrc == CEVTIND_LOST) {
955 dev_dbg(mod->dev, "data overrun interrupt\n"); 986 dev_dbg(mod->dev, "data overrun interrupt\n");
@@ -978,9 +1009,6 @@ static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
978 1009
979 /* bus error interrupt */ 1010 /* bus error interrupt */
980 if (isrc == CEVTIND_BEI) { 1011 if (isrc == CEVTIND_BEI) {
981 u8 ecc = msg->data[2];
982
983 dev_dbg(mod->dev, "bus error interrupt\n");
984 mod->can.can_stats.bus_error++; 1012 mod->can.can_stats.bus_error++;
985 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 1013 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
986 1014
@@ -1000,12 +1028,8 @@ static int ican3_handle_cevtind(struct ican3_dev *mod, struct ican3_msg *msg)
1000 break; 1028 break;
1001 } 1029 }
1002 1030
1003 if (!(ecc & ECC_DIR)) { 1031 if (!(ecc & ECC_DIR))
1004 cf->data[2] |= CAN_ERR_PROT_TX; 1032 cf->data[2] |= CAN_ERR_PROT_TX;
1005 stats->tx_errors++;
1006 } else {
1007 stats->rx_errors++;
1008 }
1009 1033
1010 cf->data[6] = txerr; 1034 cf->data[6] = txerr;
1011 cf->data[7] = rxerr; 1035 cf->data[7] = rxerr;
@@ -1090,6 +1114,88 @@ static void ican3_handle_message(struct ican3_dev *mod, struct ican3_msg *msg)
1090} 1114}
1091 1115
1092/* 1116/*
1117 * The ican3 needs to store all echo skbs, and therefore cannot
1118 * use the generic infrastructure for this.
1119 */
1120static void ican3_put_echo_skb(struct ican3_dev *mod, struct sk_buff *skb)
1121{
1122 struct sock *srcsk = skb->sk;
1123
1124 if (atomic_read(&skb->users) != 1) {
1125 struct sk_buff *old_skb = skb;
1126
1127 skb = skb_clone(old_skb, GFP_ATOMIC);
1128 kfree_skb(old_skb);
1129 if (!skb)
1130 return;
1131 } else {
1132 skb_orphan(skb);
1133 }
1134
1135 skb->sk = srcsk;
1136
1137 /* save this skb for tx interrupt echo handling */
1138 skb_queue_tail(&mod->echoq, skb);
1139}
1140
1141static unsigned int ican3_get_echo_skb(struct ican3_dev *mod)
1142{
1143 struct sk_buff *skb = skb_dequeue(&mod->echoq);
1144 struct can_frame *cf;
1145 u8 dlc;
1146
1147 /* this should never trigger unless there is a driver bug */
1148 if (!skb) {
1149 netdev_err(mod->ndev, "BUG: echo skb not occupied\n");
1150 return 0;
1151 }
1152
1153 cf = (struct can_frame *)skb->data;
1154 dlc = cf->can_dlc;
1155
1156 /* check flag whether this packet has to be looped back */
1157 if (skb->pkt_type != PACKET_LOOPBACK) {
1158 kfree_skb(skb);
1159 return dlc;
1160 }
1161
1162 skb->protocol = htons(ETH_P_CAN);
1163 skb->pkt_type = PACKET_BROADCAST;
1164 skb->ip_summed = CHECKSUM_UNNECESSARY;
1165 skb->dev = mod->ndev;
1166 netif_receive_skb(skb);
1167 return dlc;
1168}
1169
1170/*
1171 * Compare an skb with an existing echo skb
1172 *
1173 * This function will be used on devices which have a hardware loopback.
1174 * On these devices, this function can be used to compare a received skb
1175 * with the saved echo skbs so that the hardware echo skb can be dropped.
1176 *
1177 * Returns true if the skb's are identical, false otherwise.
1178 */
1179static bool ican3_echo_skb_matches(struct ican3_dev *mod, struct sk_buff *skb)
1180{
1181 struct can_frame *cf = (struct can_frame *)skb->data;
1182 struct sk_buff *echo_skb = skb_peek(&mod->echoq);
1183 struct can_frame *echo_cf;
1184
1185 if (!echo_skb)
1186 return false;
1187
1188 echo_cf = (struct can_frame *)echo_skb->data;
1189 if (cf->can_id != echo_cf->can_id)
1190 return false;
1191
1192 if (cf->can_dlc != echo_cf->can_dlc)
1193 return false;
1194
1195 return memcmp(cf->data, echo_cf->data, cf->can_dlc) == 0;
1196}
1197
1198/*
1093 * Check that there is room in the TX ring to transmit another skb 1199 * Check that there is room in the TX ring to transmit another skb
1094 * 1200 *
1095 * LOCKING: must hold mod->lock 1201 * LOCKING: must hold mod->lock
@@ -1099,6 +1205,10 @@ static bool ican3_txok(struct ican3_dev *mod)
1099 struct ican3_fast_desc __iomem *desc; 1205 struct ican3_fast_desc __iomem *desc;
1100 u8 control; 1206 u8 control;
1101 1207
1208 /* check that we have echo queue space */
1209 if (skb_queue_len(&mod->echoq) >= ICAN3_TX_BUFFERS)
1210 return false;
1211
1102 /* copy the control bits of the descriptor */ 1212 /* copy the control bits of the descriptor */
1103 ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16)); 1213 ican3_set_page(mod, mod->fasttx_start + (mod->fasttx_num / 16));
1104 desc = mod->dpm + ((mod->fasttx_num % 16) * sizeof(*desc)); 1214 desc = mod->dpm + ((mod->fasttx_num % 16) * sizeof(*desc));
@@ -1149,10 +1259,27 @@ static int ican3_recv_skb(struct ican3_dev *mod)
1149 /* convert the ICAN3 frame into Linux CAN format */ 1259 /* convert the ICAN3 frame into Linux CAN format */
1150 ican3_to_can_frame(mod, &desc, cf); 1260 ican3_to_can_frame(mod, &desc, cf);
1151 1261
1152 /* receive the skb, update statistics */ 1262 /*
1153 netif_receive_skb(skb); 1263 * If this is an ECHO frame received from the hardware loopback
1264 * feature, use the skb saved in the ECHO stack instead. This allows
1265 * the Linux CAN core to support CAN_RAW_RECV_OWN_MSGS correctly.
1266 *
1267 * Since this is a confirmation of a successfully transmitted packet
1268 * sent from this host, update the transmit statistics.
1269 *
1270 * Also, the netdevice queue needs to be allowed to send packets again.
1271 */
1272 if (ican3_echo_skb_matches(mod, skb)) {
1273 stats->tx_packets++;
1274 stats->tx_bytes += ican3_get_echo_skb(mod);
1275 kfree_skb(skb);
1276 goto err_noalloc;
1277 }
1278
1279 /* update statistics, receive the skb */
1154 stats->rx_packets++; 1280 stats->rx_packets++;
1155 stats->rx_bytes += cf->can_dlc; 1281 stats->rx_bytes += cf->can_dlc;
1282 netif_receive_skb(skb);
1156 1283
1157err_noalloc: 1284err_noalloc:
1158 /* toggle the valid bit and return the descriptor to the ring */ 1285 /* toggle the valid bit and return the descriptor to the ring */
@@ -1175,13 +1302,13 @@ err_noalloc:
1175static int ican3_napi(struct napi_struct *napi, int budget) 1302static int ican3_napi(struct napi_struct *napi, int budget)
1176{ 1303{
1177 struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi); 1304 struct ican3_dev *mod = container_of(napi, struct ican3_dev, napi);
1178 struct ican3_msg msg;
1179 unsigned long flags; 1305 unsigned long flags;
1180 int received = 0; 1306 int received = 0;
1181 int ret; 1307 int ret;
1182 1308
1183 /* process all communication messages */ 1309 /* process all communication messages */
1184 while (true) { 1310 while (true) {
1311 struct ican3_msg msg;
1185 ret = ican3_recv_msg(mod, &msg); 1312 ret = ican3_recv_msg(mod, &msg);
1186 if (ret) 1313 if (ret)
1187 break; 1314 break;
@@ -1353,7 +1480,6 @@ static int __devinit ican3_startup_module(struct ican3_dev *mod)
1353static int ican3_open(struct net_device *ndev) 1480static int ican3_open(struct net_device *ndev)
1354{ 1481{
1355 struct ican3_dev *mod = netdev_priv(ndev); 1482 struct ican3_dev *mod = netdev_priv(ndev);
1356 u8 quota;
1357 int ret; 1483 int ret;
1358 1484
1359 /* open the CAN layer */ 1485 /* open the CAN layer */
@@ -1363,19 +1489,6 @@ static int ican3_open(struct net_device *ndev)
1363 return ret; 1489 return ret;
1364 } 1490 }
1365 1491
1366 /* set the bus error generation state appropriately */
1367 if (mod->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
1368 quota = ICAN3_BUSERR_QUOTA_MAX;
1369 else
1370 quota = 0;
1371
1372 ret = ican3_set_buserror(mod, quota);
1373 if (ret) {
1374 dev_err(mod->dev, "unable to set bus-error\n");
1375 close_candev(ndev);
1376 return ret;
1377 }
1378
1379 /* bring the bus online */ 1492 /* bring the bus online */
1380 ret = ican3_set_bus_state(mod, true); 1493 ret = ican3_set_bus_state(mod, true);
1381 if (ret) { 1494 if (ret) {
@@ -1407,6 +1520,9 @@ static int ican3_stop(struct net_device *ndev)
1407 return ret; 1520 return ret;
1408 } 1521 }
1409 1522
1523 /* drop all outstanding echo skbs */
1524 skb_queue_purge(&mod->echoq);
1525
1410 /* close the CAN layer */ 1526 /* close the CAN layer */
1411 close_candev(ndev); 1527 close_candev(ndev);
1412 return 0; 1528 return 0;
@@ -1415,7 +1531,6 @@ static int ican3_stop(struct net_device *ndev)
1415static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev) 1531static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1416{ 1532{
1417 struct ican3_dev *mod = netdev_priv(ndev); 1533 struct ican3_dev *mod = netdev_priv(ndev);
1418 struct net_device_stats *stats = &ndev->stats;
1419 struct can_frame *cf = (struct can_frame *)skb->data; 1534 struct can_frame *cf = (struct can_frame *)skb->data;
1420 struct ican3_fast_desc desc; 1535 struct ican3_fast_desc desc;
1421 void __iomem *desc_addr; 1536 void __iomem *desc_addr;
@@ -1428,8 +1543,7 @@ static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1428 1543
1429 /* check that we can actually transmit */ 1544 /* check that we can actually transmit */
1430 if (!ican3_txok(mod)) { 1545 if (!ican3_txok(mod)) {
1431 dev_err(mod->dev, "no free descriptors, stopping queue\n"); 1546 dev_err(mod->dev, "BUG: no free descriptors\n");
1432 netif_stop_queue(ndev);
1433 spin_unlock_irqrestore(&mod->lock, flags); 1547 spin_unlock_irqrestore(&mod->lock, flags);
1434 return NETDEV_TX_BUSY; 1548 return NETDEV_TX_BUSY;
1435 } 1549 }
@@ -1444,6 +1558,14 @@ static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1444 can_frame_to_ican3(mod, cf, &desc); 1558 can_frame_to_ican3(mod, cf, &desc);
1445 1559
1446 /* 1560 /*
1561 * This hardware doesn't have TX-done notifications, so we'll try and
1562 * emulate it the best we can using ECHO skbs. Add the skb to the ECHO
1563 * stack. Upon packet reception, check if the ECHO skb and received
1564 * skb match, and use that to wake the queue.
1565 */
1566 ican3_put_echo_skb(mod, skb);
1567
1568 /*
1447 * the programming manual says that you must set the IVALID bit, then 1569 * the programming manual says that you must set the IVALID bit, then
1448 * interrupt, then set the valid bit. Quite weird, but it seems to be 1570 * interrupt, then set the valid bit. Quite weird, but it seems to be
1449 * required for this to work 1571 * required for this to work
@@ -1461,19 +1583,7 @@ static int ican3_xmit(struct sk_buff *skb, struct net_device *ndev)
1461 mod->fasttx_num = (desc.control & DESC_WRAP) ? 0 1583 mod->fasttx_num = (desc.control & DESC_WRAP) ? 0
1462 : (mod->fasttx_num + 1); 1584 : (mod->fasttx_num + 1);
1463 1585
1464 /* update statistics */ 1586 /* if there is no free descriptor space, stop the transmit queue */
1465 stats->tx_packets++;
1466 stats->tx_bytes += cf->can_dlc;
1467 kfree_skb(skb);
1468
1469 /*
1470 * This hardware doesn't have TX-done notifications, so we'll try and
1471 * emulate it the best we can using ECHO skbs. Get the next TX
1472 * descriptor, and see if we have room to send. If not, stop the queue.
1473 * It will be woken when the ECHO skb for the current packet is recv'd.
1474 */
1475
1476 /* copy the control bits of the descriptor */
1477 if (!ican3_txok(mod)) 1587 if (!ican3_txok(mod))
1478 netif_stop_queue(ndev); 1588 netif_stop_queue(ndev);
1479 1589
@@ -1669,6 +1779,7 @@ static int __devinit ican3_probe(struct platform_device *pdev)
1669 mod->dev = &pdev->dev; 1779 mod->dev = &pdev->dev;
1670 mod->num = pdata->modno; 1780 mod->num = pdata->modno;
1671 netif_napi_add(ndev, &mod->napi, ican3_napi, ICAN3_RX_BUFFERS); 1781 netif_napi_add(ndev, &mod->napi, ican3_napi, ICAN3_RX_BUFFERS);
1782 skb_queue_head_init(&mod->echoq);
1672 spin_lock_init(&mod->lock); 1783 spin_lock_init(&mod->lock);
1673 init_completion(&mod->termination_comp); 1784 init_completion(&mod->termination_comp);
1674 init_completion(&mod->buserror_comp); 1785 init_completion(&mod->buserror_comp);