islpci_eth.c 16 KB

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