islpci_eth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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/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 "prismcompat.h"
  25. #include "isl_38xx.h"
  26. #include "islpci_eth.h"
  27. #include "islpci_mgt.h"
  28. #include "oid_mgt.h"
  29. /******************************************************************************
  30. Network Interface functions
  31. ******************************************************************************/
  32. void
  33. islpci_eth_cleanup_transmit(islpci_private *priv,
  34. isl38xx_control_block *control_block)
  35. {
  36. struct sk_buff *skb;
  37. u32 index;
  38. /* compare the control block read pointer with the free pointer */
  39. while (priv->free_data_tx !=
  40. le32_to_cpu(control_block->
  41. device_curr_frag[ISL38XX_CB_TX_DATA_LQ])) {
  42. /* read the index of the first fragment to be freed */
  43. index = priv->free_data_tx % ISL38XX_CB_TX_QSIZE;
  44. /* check for holes in the arrays caused by multi fragment frames
  45. * searching for the last fragment of a frame */
  46. if (priv->pci_map_tx_address[index] != (dma_addr_t) NULL) {
  47. /* entry is the last fragment of a frame
  48. * free the skb structure and unmap pci memory */
  49. skb = priv->data_low_tx[index];
  50. #if VERBOSE > SHOW_ERROR_MESSAGES
  51. DEBUG(SHOW_TRACING,
  52. "cleanup skb %p skb->data %p skb->len %u truesize %u\n ",
  53. skb, skb->data, skb->len, skb->truesize);
  54. #endif
  55. pci_unmap_single(priv->pdev,
  56. priv->pci_map_tx_address[index],
  57. skb->len, PCI_DMA_TODEVICE);
  58. dev_kfree_skb_irq(skb);
  59. skb = NULL;
  60. }
  61. /* increment the free data low queue pointer */
  62. priv->free_data_tx++;
  63. }
  64. }
  65. int
  66. islpci_eth_transmit(struct sk_buff *skb, struct net_device *ndev)
  67. {
  68. islpci_private *priv = netdev_priv(ndev);
  69. isl38xx_control_block *cb = priv->control_block;
  70. u32 index;
  71. dma_addr_t pci_map_address;
  72. int frame_size;
  73. isl38xx_fragment *fragment;
  74. int offset;
  75. struct sk_buff *newskb;
  76. int newskb_offset;
  77. unsigned long flags;
  78. unsigned char wds_mac[6];
  79. u32 curr_frag;
  80. int err = 0;
  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. err = -EBUSY;
  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-allign */
  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. memcpy(skb->data, 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. err = -ENOMEM;
  137. goto drop_free;
  138. }
  139. newskb_offset = (4 - (long) newskb->data) & 0x03;
  140. /* Check if newskb->data is aligned */
  141. if (newskb_offset)
  142. skb_reserve(newskb, newskb_offset);
  143. skb_put(newskb, init_wds ? skb->len + 6 : skb->len);
  144. if (init_wds) {
  145. memcpy(newskb->data + 6, skb->data, skb->len);
  146. memcpy(newskb->data, wds_mac, 6);
  147. #ifdef ISLPCI_ETH_DEBUG
  148. printk("islpci_eth_transmit:wds_mac\n");
  149. #endif
  150. } else
  151. memcpy(newskb->data, skb->data, skb->len);
  152. #if VERBOSE > SHOW_ERROR_MESSAGES
  153. DEBUG(SHOW_TRACING, "memcpy %p %p %i wds %i\n",
  154. newskb->data, skb->data, skb->len, init_wds);
  155. #endif
  156. newskb->dev = skb->dev;
  157. dev_kfree_skb_irq(skb);
  158. skb = newskb;
  159. }
  160. }
  161. /* display the buffer contents for debugging */
  162. #if VERBOSE > SHOW_ERROR_MESSAGES
  163. DEBUG(SHOW_BUFFER_CONTENTS, "\ntx %p ", skb->data);
  164. display_buffer((char *) skb->data, skb->len);
  165. #endif
  166. /* map the skb buffer to pci memory for DMA operation */
  167. pci_map_address = pci_map_single(priv->pdev,
  168. (void *) skb->data, skb->len,
  169. PCI_DMA_TODEVICE);
  170. if (unlikely(pci_map_address == 0)) {
  171. printk(KERN_WARNING "%s: cannot map buffer to PCI\n",
  172. ndev->name);
  173. err = -EIO;
  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. /* set the transmission time */
  200. ndev->trans_start = jiffies;
  201. priv->statistics.tx_packets++;
  202. priv->statistics.tx_bytes += skb->len;
  203. /* trigger the device */
  204. islpci_trigger(priv);
  205. /* unlock the driver code */
  206. spin_unlock_irqrestore(&priv->slock, flags);
  207. return 0;
  208. drop_free:
  209. priv->statistics.tx_dropped++;
  210. spin_unlock_irqrestore(&priv->slock, flags);
  211. dev_kfree_skb(skb);
  212. return err;
  213. }
  214. static inline int
  215. islpci_monitor_rx(islpci_private *priv, struct sk_buff **skb)
  216. {
  217. /* The card reports full 802.11 packets but with a 20 bytes
  218. * header and without the FCS. But there a is a bit that
  219. * indicates if the packet is corrupted :-) */
  220. struct rfmon_header *hdr = (struct rfmon_header *) (*skb)->data;
  221. if (hdr->flags & 0x01)
  222. /* This one is bad. Drop it ! */
  223. return -1;
  224. if (priv->ndev->type == ARPHRD_IEEE80211_PRISM) {
  225. struct avs_80211_1_header *avs;
  226. /* extract the relevant data from the header */
  227. u32 clock = le32_to_cpu(hdr->clock);
  228. u8 rate = hdr->rate;
  229. u16 freq = le16_to_cpu(hdr->freq);
  230. u8 rssi = hdr->rssi;
  231. skb_pull(*skb, sizeof (struct rfmon_header));
  232. if (skb_headroom(*skb) < sizeof (struct avs_80211_1_header)) {
  233. struct sk_buff *newskb = skb_copy_expand(*skb,
  234. sizeof (struct
  235. avs_80211_1_header),
  236. 0, GFP_ATOMIC);
  237. if (newskb) {
  238. dev_kfree_skb_irq(*skb);
  239. *skb = newskb;
  240. } else
  241. return -1;
  242. /* This behavior is not very subtile... */
  243. }
  244. /* make room for the new header and fill it. */
  245. avs =
  246. (struct avs_80211_1_header *) skb_push(*skb,
  247. sizeof (struct
  248. avs_80211_1_header));
  249. avs->version = cpu_to_be32(P80211CAPTURE_VERSION);
  250. avs->length = cpu_to_be32(sizeof (struct avs_80211_1_header));
  251. avs->mactime = cpu_to_be64(le64_to_cpu(clock));
  252. avs->hosttime = cpu_to_be64(jiffies);
  253. avs->phytype = cpu_to_be32(6); /*OFDM: 6 for (g), 8 for (a) */
  254. avs->channel = cpu_to_be32(channel_of_freq(freq));
  255. avs->datarate = cpu_to_be32(rate * 5);
  256. avs->antenna = cpu_to_be32(0); /*unknown */
  257. avs->priority = cpu_to_be32(0); /*unknown */
  258. avs->ssi_type = cpu_to_be32(3); /*2: dBm, 3: raw RSSI */
  259. avs->ssi_signal = cpu_to_be32(rssi & 0x7f);
  260. avs->ssi_noise = cpu_to_be32(priv->local_iwstatistics.qual.noise); /*better than 'undefined', I assume */
  261. avs->preamble = cpu_to_be32(0); /*unknown */
  262. avs->encoding = cpu_to_be32(0); /*unknown */
  263. } else
  264. skb_pull(*skb, sizeof (struct rfmon_header));
  265. (*skb)->protocol = htons(ETH_P_802_2);
  266. (*skb)->mac.raw = (*skb)->data;
  267. (*skb)->pkt_type = PACKET_OTHERHOST;
  268. return 0;
  269. }
  270. int
  271. islpci_eth_receive(islpci_private *priv)
  272. {
  273. struct net_device *ndev = priv->ndev;
  274. isl38xx_control_block *control_block = priv->control_block;
  275. struct sk_buff *skb;
  276. u16 size;
  277. u32 index, offset;
  278. unsigned char *src;
  279. int discard = 0;
  280. #if VERBOSE > SHOW_ERROR_MESSAGES
  281. DEBUG(SHOW_FUNCTION_CALLS, "islpci_eth_receive \n");
  282. #endif
  283. /* the device has written an Ethernet frame in the data area
  284. * of the sk_buff without updating the structure, do it now */
  285. index = priv->free_data_rx % ISL38XX_CB_RX_QSIZE;
  286. size = le16_to_cpu(control_block->rx_data_low[index].size);
  287. skb = priv->data_low_rx[index];
  288. offset = ((unsigned long)
  289. le32_to_cpu(control_block->rx_data_low[index].address) -
  290. (unsigned long) skb->data) & 3;
  291. #if VERBOSE > SHOW_ERROR_MESSAGES
  292. DEBUG(SHOW_TRACING,
  293. "frq->addr %x skb->data %p skb->len %u offset %u truesize %u\n ",
  294. control_block->rx_data_low[priv->free_data_rx].address, skb->data,
  295. skb->len, offset, skb->truesize);
  296. #endif
  297. /* delete the streaming DMA mapping before processing the skb */
  298. pci_unmap_single(priv->pdev,
  299. priv->pci_map_rx_address[index],
  300. MAX_FRAGMENT_SIZE_RX + 2, PCI_DMA_FROMDEVICE);
  301. /* update the skb structure and allign the buffer */
  302. skb_put(skb, size);
  303. if (offset) {
  304. /* shift the buffer allocation offset bytes to get the right frame */
  305. skb_pull(skb, 2);
  306. skb_put(skb, 2);
  307. }
  308. #if VERBOSE > SHOW_ERROR_MESSAGES
  309. /* display the buffer contents for debugging */
  310. DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
  311. display_buffer((char *) skb->data, skb->len);
  312. #endif
  313. /* check whether WDS is enabled and whether the data frame is a WDS frame */
  314. if (init_wds) {
  315. /* WDS enabled, check for the wds address on the first 6 bytes of the buffer */
  316. src = skb->data + 6;
  317. memmove(skb->data, src, skb->len - 6);
  318. skb_trim(skb, skb->len - 6);
  319. }
  320. #if VERBOSE > SHOW_ERROR_MESSAGES
  321. DEBUG(SHOW_TRACING, "Fragment size %i in skb at %p\n", size, skb);
  322. DEBUG(SHOW_TRACING, "Skb data at %p, length %i\n", skb->data, skb->len);
  323. /* display the buffer contents for debugging */
  324. DEBUG(SHOW_BUFFER_CONTENTS, "\nrx %p ", skb->data);
  325. display_buffer((char *) skb->data, skb->len);
  326. #endif
  327. /* do some additional sk_buff and network layer parameters */
  328. skb->dev = ndev;
  329. /* take care of monitor mode and spy monitoring. */
  330. if (unlikely(priv->iw_mode == IW_MODE_MONITOR))
  331. discard = islpci_monitor_rx(priv, &skb);
  332. else {
  333. if (unlikely(skb->data[2 * ETH_ALEN] == 0)) {
  334. /* The packet has a rx_annex. Read it for spy monitoring, Then
  335. * remove it, while keeping the 2 leading MAC addr.
  336. */
  337. struct iw_quality wstats;
  338. struct rx_annex_header *annex =
  339. (struct rx_annex_header *) skb->data;
  340. wstats.level = annex->rfmon.rssi;
  341. /* The noise value can be a bit outdated if nobody's
  342. * reading wireless stats... */
  343. wstats.noise = priv->local_iwstatistics.qual.noise;
  344. wstats.qual = wstats.level - wstats.noise;
  345. wstats.updated = 0x07;
  346. /* Update spy records */
  347. wireless_spy_update(ndev, annex->addr2, &wstats);
  348. memcpy(skb->data + sizeof (struct rfmon_header),
  349. skb->data, 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. priv->statistics.rx_packets++;
  356. priv->statistics.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] == (dma_addr_t) NULL)) {
  399. /* error mapping the buffer to device accessable 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. add_le32p((u32 *) &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. struct net_device_stats *statistics = &priv->statistics;
  433. /* increment the transmit error counter */
  434. statistics->tx_errors++;
  435. if (!priv->reset_task_pending) {
  436. printk(KERN_WARNING
  437. "%s: tx_timeout, scheduling reset", ndev->name);
  438. netif_stop_queue(ndev);
  439. priv->reset_task_pending = 1;
  440. schedule_work(&priv->reset_task);
  441. } else {
  442. printk(KERN_WARNING
  443. "%s: tx_timeout, waiting for reset", ndev->name);
  444. }
  445. }