islpci_eth.c 15 KB

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