islpci_eth.c 15 KB

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