islpci_eth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Intersil Americas Inc.
  4. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/gfp.h>
  8. #include <linux/pci.h>
  9. #include <linux/delay.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/if_arp.h>
  13. #include <asm/byteorder.h>
  14. #include "prismcompat.h"
  15. #include "isl_38xx.h"
  16. #include "islpci_eth.h"
  17. #include "islpci_mgt.h"
  18. #include "oid_mgt.h"
  19. /******************************************************************************
  20. Network Interface functions
  21. ******************************************************************************/
  22. void
  23. islpci_eth_cleanup_transmit(islpci_private *priv,
  24. isl38xx_control_block *control_block)
  25. {
  26. struct sk_buff *skb;
  27. u32 index;
  28. /* compare the control block read pointer with the free pointer */
  29. while (priv->free_data_tx !=
  30. le32_to_cpu(control_block->
  31. device_curr_frag[ISL38XX_CB_TX_DATA_LQ])) {
  32. /* read the index of the first fragment to be freed */
  33. index = priv->free_data_tx % ISL38XX_CB_TX_QSIZE;
  34. /* check for holes in the arrays caused by multi fragment frames
  35. * searching for the last fragment of a frame */
  36. if (priv->pci_map_tx_address[index]) {
  37. /* entry is the last fragment of a frame
  38. * free the skb structure and unmap pci memory */
  39. skb = priv->data_low_tx[index];
  40. #if VERBOSE > SHOW_ERROR_MESSAGES
  41. DEBUG(SHOW_TRACING,
  42. "cleanup skb %p skb->data %p skb->len %u truesize %u\n",
  43. skb, skb->data, skb->len, skb->truesize);
  44. #endif
  45. pci_unmap_single(priv->pdev,
  46. priv->pci_map_tx_address[index],
  47. skb->len, PCI_DMA_TODEVICE);
  48. dev_kfree_skb_irq(skb);
  49. skb = NULL;
  50. }
  51. /* increment the free data low queue pointer */
  52. priv->free_data_tx++;
  53. }
  54. }
  55. netdev_tx_t
  56. islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
  57. {
  58. islpci_private *priv = netdev_priv(ndev);
  59. isl38xx_control_block *cb = priv->control_block;
  60. u32 index;
  61. dma_addr_t pci_map_address;
  62. int frame_size;
  63. isl38xx_fragment *fragment;
  64. int offset;
  65. struct sk_buff *newskb;
  66. int newskb_offset;
  67. unsigned long flags;
  68. unsigned char wds_mac[6];
  69. u32 curr_frag;
  70. #if VERBOSE > SHOW_ERROR_MESSAGES
  71. DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_transmit\n");
  72. #endif
  73. /* lock the driver code */
  74. spin_lock_irqsave(&priv->slock, flags);
  75. /* check whether the destination queue has enough fragments for the frame */
  76. curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ]);
  77. if (unlikely(curr_frag - priv->free_data_tx >= ISL38XX_CB_TX_QSIZE)) {
  78. printk(KERN_ERR "%s: transmit device queue full when awake\n",
  79. ndev->name);
  80. netif_stop_queue(ndev);
  81. /* trigger the device */
  82. isl38xx_w32_flush(priv->device_base, ISL38XX_DEV_INT_UPDATE,
  83. ISL38XX_DEV_INT_REG);
  84. udelay(ISL38XX_WRITEIO_DELAY);
  85. goto drop_free;
  86. }
  87. /* Check alignment and WDS frame formatting. The start of the packet should
  88. * be aligned on a 4-byte boundary. If WDS is enabled add another 6 bytes
  89. * and add WDS address information */
  90. if (likely(((long) skb->data & 0x03) | init_wds)) {
  91. /* get the number of bytes to add and re-align */
  92. offset = (4 - (long) skb->data) & 0x03;
  93. offset += init_wds ? 6 : 0;
  94. /* check whether the current skb can be used */
  95. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  96. unsigned char *src = skb->data;
  97. #if VERBOSE > SHOW_ERROR_MESSAGES
  98. DEBUG(SHOW_TRACING, "skb offset %i wds %i\n", offset,
  99. init_wds);
  100. #endif
  101. /* align the buffer on 4-byte boundary */
  102. skb_reserve(skb, (4 - (long) skb->data) & 0x03);
  103. if (init_wds) {
  104. /* wds requires an additional address field of 6 bytes */
  105. skb_put(skb, 6);
  106. #ifdef ISLPCI_ETH_DEBUG
  107. printk("islpci_eth_transmit:wds_mac\n");
  108. #endif
  109. memmove(skb->data + 6, src, skb->len);
  110. skb_copy_to_linear_data(skb, wds_mac, 6);
  111. } else {
  112. memmove(skb->data, src, skb->len);
  113. }
  114. #if VERBOSE > SHOW_ERROR_MESSAGES
  115. DEBUG(SHOW_TRACING, "memmove %p %p %i\n", skb->data,
  116. src, skb->len);
  117. #endif
  118. } else {
  119. newskb =
  120. dev_alloc_skb(init_wds ? skb->len + 6 : skb->len);
  121. if (unlikely(newskb == NULL)) {
  122. printk(KERN_ERR "%s: Cannot allocate skb\n",
  123. ndev->name);
  124. goto drop_free;
  125. }
  126. newskb_offset = (4 - (long) newskb->data) & 0x03;
  127. /* Check if newskb->data is aligned */
  128. if (newskb_offset)
  129. skb_reserve(newskb, newskb_offset);
  130. skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
  131. if (init_wds) {
  132. skb_copy_from_linear_data(skb,
  133. newskb->data + 6,
  134. skb->len);
  135. skb_copy_to_linear_data(newskb, wds_mac, 6);
  136. #ifdef ISLPCI_ETH_DEBUG
  137. printk("islpci_eth_transmit:wds_mac\n");
  138. #endif
  139. } else
  140. skb_copy_from_linear_data(skb, newskb->data,
  141. skb->len);
  142. #if VERBOSE > SHOW_ERROR_MESSAGES
  143. DEBUG(SHOW_TRACING, "memcpy %p %p %i wds %i\n",
  144. newskb->data, skb->data, skb->len, init_wds);
  145. #endif
  146. newskb->dev = skb->dev;
  147. dev_kfree_skb_irq(skb);
  148. skb = newskb;
  149. }
  150. }
  151. /* display the buffer contents for debugging */
  152. #if VERBOSE > SHOW_ERROR_MESSAGES
  153. DEBUG(SHOW_BUFFER_CONTENTS, "\ntx %p ", skb->data);
  154. display_buffer((char *) skb->data, skb->len);
  155. #endif
  156. /* map the skb buffer to pci memory for DMA operation */
  157. pci_map_address = pci_map_single(priv->pdev,
  158. (void *) skb->data, skb->len,
  159. PCI_DMA_TODEVICE);
  160. if (pci_dma_mapping_error(priv->pdev, pci_map_address)) {
  161. printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
  162. ndev->name);
  163. goto drop_free;
  164. }
  165. /* Place the fragment in the control block structure. */
  166. index = curr_frag % ISL38XX_CB_TX_QSIZE;
  167. fragment = &cb->tx_data_low[index];
  168. priv->pci_map_tx_address[index] = pci_map_address;
  169. /* store the skb address for future freeing */
  170. priv->data_low_tx[index] = skb;
  171. /* set the proper fragment start address and size information */
  172. frame_size = skb->len;
  173. fragment->size = cpu_to_le16(frame_size);
  174. fragment->flags = cpu_to_le16(0); /* set to 1 if more fragments */
  175. fragment->address = cpu_to_le32(pci_map_address);
  176. curr_frag++;
  177. /* The fragment address in the control block must have been
  178. * written before announcing the frame buffer to device. */
  179. wmb();
  180. cb->driver_curr_frag[ISL38XX_CB_TX_DATA_LQ] = cpu_to_le32(curr_frag);
  181. if (curr_frag - priv->free_data_tx + ISL38XX_MIN_QTHRESHOLD
  182. > ISL38XX_CB_TX_QSIZE) {
  183. /* stop sends from upper layers */
  184. netif_stop_queue(ndev);
  185. /* set the full flag for the transmission queue */
  186. priv->data_low_tx_full = 1;
  187. }
  188. ndev->stats.tx_packets++;
  189. ndev->stats.tx_bytes += skb->len;
  190. /* trigger the device */
  191. islpci_trigger(priv);
  192. /* unlock the driver code */
  193. spin_unlock_irqrestore(&priv->slock, flags);
  194. return NETDEV_TX_OK;
  195. drop_free:
  196. ndev->stats.tx_dropped++;
  197. spin_unlock_irqrestore(&priv->slock, flags);
  198. dev_kfree_skb(skb);
  199. return NETDEV_TX_OK;
  200. }
  201. static inline int
  202. islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
  203. {
  204. /* The card reports full 802.11 packets but with a 20 bytes
  205. * header and without the FCS. But there a is a bit that
  206. * indicates if the packet is corrupted :-) */
  207. struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
  208. if (hdr->flags & 0x01)
  209. /* This one is bad. Drop it ! */
  210. return -1;
  211. if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
  212. struct avs_80211_1_header *avs;
  213. /* extract the relevant data from the header */
  214. u32 clock = le32_to_cpu(hdr->clock);
  215. u8 rate = hdr->rate;
  216. u16 freq = le16_to_cpu(hdr->freq);
  217. u8 rssi = hdr->rssi;
  218. skb_pull(*skb, sizeof (struct rfmon_header));
  219. if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
  220. struct sk_buff *newskb = skb_copy_expand(*skb,
  221. sizeof (struct
  222. avs_80211_1_header),
  223. 0, GFP_ATOMIC);
  224. if (newskb) {
  225. dev_kfree_skb_irq(*skb);
  226. *skb = newskb;
  227. } else
  228. return -1;
  229. /* This behavior is not very subtile... */
  230. }
  231. /* make room for the new header and fill it. */
  232. avs = skb_push(*skb, sizeof(struct avs_80211_1_header));
  233. avs->version = cpu_to_be32(P80211CAPTURE_VERSION);
  234. avs->length = cpu_to_be32(sizeof (struct avs_80211_1_header));
  235. avs->mactime = cpu_to_be64(clock);
  236. avs->hosttime = cpu_to_be64(jiffies);
  237. avs->phytype = cpu_to_be32(6); /*OFDM: 6 for (g), 8 for (a) */
  238. avs->channel = cpu_to_be32(channel_of_freq(freq));
  239. avs->datarate = cpu_to_be32(rate * 5);
  240. avs->antenna = cpu_to_be32(0); /*unknown */
  241. avs->priority = cpu_to_be32(0); /*unknown */
  242. avs->ssi_type = cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
  243. avs->ssi_signal = cpu_to_be32(rssi & 0x7f);
  244. avs->ssi_noise = cpu_to_be32(priv->local_iwstatistics.qual.noise); /*better than 'undefined', I assume */
  245. avs->preamble = cpu_to_be32(0); /*unknown */
  246. avs->encoding = cpu_to_be32(0); /*unknown */
  247. } else
  248. skb_pull(*skb, sizeof (struct rfmon_header));
  249. (*skb)->protocol = htons(ETH_P_802_2);
  250. skb_reset_mac_header(*skb);
  251. (*skb)->pkt_type = PACKET_OTHERHOST;
  252. return 0;
  253. }
  254. int
  255. islpci_eth_receive(islpci_private *priv)
  256. {
  257. struct net_device *ndev = priv->ndev;
  258. isl38xx_control_block *control_block = priv->control_block;
  259. struct sk_buff *skb;
  260. u16 size;
  261. u32 index, offset;
  262. unsigned char *src;
  263. int discard = 0;
  264. #if VERBOSE > SHOW_ERROR_MESSAGES
  265. DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive\n");
  266. #endif
  267. /* the device has written an Ethernet frame in the data area
  268. * of the sk_buff without updating the structure, do it now */
  269. index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
  270. size = le16_to_cpu(control_block->rx_data_low[index].size);
  271. skb = priv->data_low_rx[index];
  272. offset = ((unsigned long)
  273. le32_to_cpu(control_block->rx_data_low[index].address) -
  274. (unsigned long) skb->data) & 3;
  275. #if VERBOSE > SHOW_ERROR_MESSAGES
  276. DEBUG(SHOW_TRACING,
  277. "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n",
  278. control_block->rx_data_low[priv->free_data_rx].address, skb->data,
  279. skb->len, offset, skb->truesize);
  280. #endif
  281. /* delete the streaming DMA mapping before processing the skb */
  282. pci_unmap_single(priv->pdev,
  283. priv->pci_map_rx_address[index],
  284. MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
  285. /* update the skb structure and align the buffer */
  286. skb_put(skb, size);
  287. if (offset) {
  288. /* shift the buffer allocation offset bytes to get the right frame */
  289. skb_pull(skb, 2);
  290. skb_put(skb, 2);
  291. }
  292. #if VERBOSE > SHOW_ERROR_MESSAGES
  293. /* display the buffer contents for debugging */
  294. DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
  295. display_buffer((char *) skb->data, skb->len);
  296. #endif
  297. /* check whether WDS is enabled and whether the data frame is a WDS frame */
  298. if (init_wds) {
  299. /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
  300. src = skb->data + 6;
  301. memmove(skb->data, src, skb->len - 6);
  302. skb_trim(skb, skb->len - 6);
  303. }
  304. #if VERBOSE > SHOW_ERROR_MESSAGES
  305. DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
  306. DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
  307. /* display the buffer contents for debugging */
  308. DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
  309. display_buffer((char *) skb->data, skb->len);
  310. #endif
  311. /* take care of monitor mode and spy monitoring. */
  312. if (unlikely(priv->iw_mode == IW_MODE_MONITOR)) {
  313. skb->dev = ndev;
  314. discard = islpci_monitor_rx(priv, &skb);
  315. } else {
  316. if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
  317. /* The packet has a rx_annex. Read it for spy monitoring, Then
  318. * remove it, while keeping the 2 leading MAC addr.
  319. */
  320. struct iw_quality wstats;
  321. struct rx_annex_header *annex =
  322. (struct rx_annex_header *) skb->data;
  323. wstats.level = annex->rfmon.rssi;
  324. /* The noise value can be a bit outdated if nobody's
  325. * reading wireless stats... */
  326. wstats.noise = priv->local_iwstatistics.qual.noise;
  327. wstats.qual = wstats.level - wstats.noise;
  328. wstats.updated = 0x07;
  329. /* Update spy records */
  330. wireless_spy_update(ndev, annex->addr2, &wstats);
  331. skb_copy_from_linear_data(skb,
  332. (skb->data +
  333. sizeof(struct rfmon_header)),
  334. 2 * ETH_ALEN);
  335. skb_pull(skb, sizeof (struct rfmon_header));
  336. }
  337. skb->protocol = eth_type_trans(skb, ndev);
  338. }
  339. skb->ip_summed = CHECKSUM_NONE;
  340. ndev->stats.rx_packets++;
  341. ndev->stats.rx_bytes += size;
  342. /* deliver the skb to the network layer */
  343. #ifdef ISLPCI_ETH_DEBUG
  344. printk
  345. ("islpci_eth_receive:netif_rx %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  346. skb->data[0], skb->data[1], skb->data[2], skb->data[3],
  347. skb->data[4], skb->data[5]);
  348. #endif
  349. if (unlikely(discard)) {
  350. dev_kfree_skb_irq(skb);
  351. skb = NULL;
  352. } else
  353. netif_rx(skb);
  354. /* increment the read index for the rx data low queue */
  355. priv->free_data_rx++;
  356. /* add one or more sk_buff structures */
  357. while (index =
  358. le32_to_cpu(control_block->
  359. driver_curr_frag[ISL38XX_CB_RX_DATA_LQ]),
  360. index - priv->free_data_rx < ISL38XX_CB_RX_QSIZE) {
  361. /* allocate an sk_buff for received data frames storage
  362. * include any required allignment operations */
  363. skb = dev_alloc_skb(MAX_FRAGMENT_SIZE_RX + 2);
  364. if (unlikely(skb == NULL)) {
  365. /* error allocating an sk_buff structure elements */
  366. DEBUG(SHOW_ERROR_MESSAGES, "Error allocating skb\n");
  367. break;
  368. }
  369. skb_reserve(skb, (4 - (long) skb->data) & 0x03);
  370. /* store the new skb structure pointer */
  371. index = index % ISL38XX_CB_RX_QSIZE;
  372. priv->data_low_rx[index] = skb;
  373. #if VERBOSE > SHOW_ERROR_MESSAGES
  374. DEBUG(SHOW_TRACING,
  375. "new alloc skb %p skb->data %p skb->len %u index %u truesize %u\n",
  376. skb, skb->data, skb->len, index, skb->truesize);
  377. #endif
  378. /* set the streaming DMA mapping for proper PCI bus operation */
  379. priv->pci_map_rx_address[index] =
  380. pci_map_single(priv->pdev, (void *) skb->data,
  381. MAX_FRAGMENT_SIZE_RX + 2,
  382. PCI_DMA_FROMDEVICE);
  383. if (pci_dma_mapping_error(priv->pdev,
  384. priv->pci_map_rx_address[index])) {
  385. /* error mapping the buffer to device accessible memory address */
  386. DEBUG(SHOW_ERROR_MESSAGES,
  387. "Error mapping DMA address\n");
  388. /* free the skbuf structure before aborting */
  389. dev_kfree_skb_irq(skb);
  390. skb = NULL;
  391. break;
  392. }
  393. /* update the fragment address */
  394. control_block->rx_data_low[index].address =
  395. cpu_to_le32((u32)priv->pci_map_rx_address[index]);
  396. wmb();
  397. /* increment the driver read pointer */
  398. le32_add_cpu(&control_block->
  399. driver_curr_frag[ISL38XX_CB_RX_DATA_LQ], 1);
  400. }
  401. /* trigger the device */
  402. islpci_trigger(priv);
  403. return 0;
  404. }
  405. void
  406. islpci_do_reset_and_wake(struct work_struct *work)
  407. {
  408. islpci_private *priv = container_of(work, islpci_private, reset_task);
  409. islpci_reset(priv, 1);
  410. priv->reset_task_pending = 0;
  411. smp_wmb();
  412. netif_wake_queue(priv->ndev);
  413. }
  414. void
  415. islpci_eth_tx_timeout(struct net_device *ndev)
  416. {
  417. islpci_private *priv = netdev_priv(ndev);
  418. /* increment the transmit error counter */
  419. ndev->stats.tx_errors++;
  420. if (!priv->reset_task_pending) {
  421. printk(KERN_WARNING
  422. "%s: tx_timeout, scheduling reset", ndev->name);
  423. netif_stop_queue(ndev);
  424. priv->reset_task_pending = 1;
  425. schedule_work(&priv->reset_task);
  426. } else {
  427. printk(KERN_WARNING
  428. "%s: tx_timeout, waiting for reset", ndev->name);
  429. }
  430. }