i2o_lan.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:46k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * drivers/i2o/i2o_lan.c
  3.  *
  4.  *  I2O LAN CLASS OSM  May 26th 2000
  5.  *
  6.  * (C) Copyright 1999, 2000  University of Helsinki,
  7.  *        Department of Computer Science
  8.  *
  9.  *  This code is still under development / test.
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version
  14.  * 2 of the License, or (at your option) any later version.
  15.  *
  16.  * Authors:  Auvo H鋕kinen <Auvo.Hakkinen@cs.Helsinki.FI>
  17.  * Fixes: Juha Siev鋘en <Juha.Sievanen@cs.Helsinki.FI>
  18.  *   Taneli V鋒鋕angas <Taneli.Vahakangas@cs.Helsinki.FI>
  19.  * Deepak Saxena <deepak@plexity.net>
  20.  *
  21.  * Tested: in FDDI environment (using SysKonnect's DDM)
  22.  * in Gigabit Eth environment (using SysKonnect's DDM)
  23.  * in Fast Ethernet environment (using Intel 82558 DDM)
  24.  *
  25.  * TODO: tests for other LAN classes (Token Ring, Fibre Channel)
  26.  */
  27. #include <linux/config.h>
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/fddidevice.h>
  33. #include <linux/trdevice.h>
  34. #include <linux/fcdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/if_arp.h>
  37. #include <linux/slab.h>
  38. #include <linux/init.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/tqueue.h>
  41. #include <asm/io.h>
  42. #include <linux/errno.h>
  43. #include <linux/i2o.h>
  44. #include "i2o_lan.h"
  45. //#define DRIVERDEBUG
  46. #ifdef DRIVERDEBUG
  47. #define dprintk(s, args...) printk(s, ## args)
  48. #else
  49. #define dprintk(s, args...)
  50. #endif
  51. /* The following module parameters are used as default values
  52.  * for per interface values located in the net_device private area.
  53.  * Private values are changed via /proc filesystem.
  54.  */
  55. static u32 max_buckets_out = I2O_LAN_MAX_BUCKETS_OUT;
  56. static u32 bucket_thresh   = I2O_LAN_BUCKET_THRESH;
  57. static u32 rx_copybreak    = I2O_LAN_RX_COPYBREAK;
  58. static u8  tx_batch_mode   = I2O_LAN_TX_BATCH_MODE;
  59. static u32 i2o_event_mask  = I2O_LAN_EVENT_MASK;
  60. #define MAX_LAN_CARDS 16
  61. static struct net_device *i2o_landevs[MAX_LAN_CARDS+1];
  62. static int unit = -1;    /* device unit number */
  63. static void i2o_lan_reply(struct i2o_handler *h, struct i2o_controller *iop, struct i2o_message *m);
  64. static void i2o_lan_send_post_reply(struct i2o_handler *h, struct i2o_controller *iop, struct i2o_message *m);
  65. static int i2o_lan_receive_post(struct net_device *dev);
  66. static void i2o_lan_receive_post_reply(struct i2o_handler *h, struct i2o_controller *iop, struct i2o_message *m);
  67. static void i2o_lan_release_buckets(struct net_device *dev, u32 *msg);
  68. static int i2o_lan_reset(struct net_device *dev);
  69. static void i2o_lan_handle_event(struct net_device *dev, u32 *msg);
  70. /* Structures to register handlers for the incoming replies. */
  71. static struct i2o_handler i2o_lan_send_handler = {
  72. i2o_lan_send_post_reply,  // For send replies
  73. NULL,
  74. NULL,
  75. NULL,
  76. "I2O LAN OSM send",
  77. -1,
  78. I2O_CLASS_LAN
  79. };
  80. static int lan_send_context;
  81. static struct i2o_handler i2o_lan_receive_handler = {
  82. i2o_lan_receive_post_reply, // For receive replies
  83. NULL,
  84. NULL,
  85. NULL,
  86. "I2O LAN OSM receive",
  87. -1,
  88. I2O_CLASS_LAN
  89. };
  90. static int lan_receive_context;
  91. static struct i2o_handler i2o_lan_handler = {
  92. i2o_lan_reply, // For other replies
  93. NULL,
  94. NULL,
  95. NULL,
  96. "I2O LAN OSM",
  97. -1,
  98. I2O_CLASS_LAN
  99. };
  100. static int lan_context;
  101. DECLARE_TASK_QUEUE(i2o_post_buckets_task);
  102. struct tq_struct run_i2o_post_buckets_task = {
  103. routine: (void (*)(void *)) run_task_queue,
  104. data: (void *) 0
  105. };
  106. /* Functions to handle message failures and transaction errors:
  107. ==============================================================*/
  108. /*
  109.  * i2o_lan_handle_failure(): Fail bit has been set since IOP's message
  110.  * layer cannot deliver the request to the target, or the target cannot
  111.  * process the request.
  112.  */
  113. static void i2o_lan_handle_failure(struct net_device *dev, u32 *msg)
  114. {
  115. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  116. struct i2o_device *i2o_dev = priv->i2o_dev;
  117. struct i2o_controller *iop = i2o_dev->controller;
  118. u32 *preserved_msg = (u32*)(iop->mem_offset + msg[7]);
  119. u32 *sgl_elem = &preserved_msg[4];
  120. struct sk_buff *skb = NULL;
  121. u8 le_flag;
  122. i2o_report_status(KERN_INFO, dev->name, msg);
  123. /* If PacketSend failed, free sk_buffs reserved by upper layers */
  124. if (msg[1] >> 24 == LAN_PACKET_SEND) {
  125. do {
  126. skb = (struct sk_buff *)(sgl_elem[1]);
  127. dev_kfree_skb_irq(skb);
  128. atomic_dec(&priv->tx_out);
  129. le_flag = *sgl_elem >> 31;
  130. sgl_elem +=3;
  131. } while (le_flag == 0); /* Last element flag not set */
  132. if (netif_queue_stopped(dev))
  133. netif_wake_queue(dev);
  134. }
  135. /* If ReceivePost failed, free sk_buffs we have reserved */
  136. if (msg[1] >> 24 == LAN_RECEIVE_POST) {
  137. do {
  138. skb = (struct sk_buff *)(sgl_elem[1]);
  139. dev_kfree_skb_irq(skb);
  140. atomic_dec(&priv->buckets_out);
  141. le_flag = *sgl_elem >> 31;
  142. sgl_elem +=3;
  143. } while (le_flag == 0); /* Last element flag not set */
  144. }
  145. /* Release the preserved msg frame by resubmitting it as a NOP */
  146. preserved_msg[0] = THREE_WORD_MSG_SIZE | SGL_OFFSET_0;
  147. preserved_msg[1] = I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0;
  148. preserved_msg[2] = 0;
  149. i2o_post_message(iop, msg[7]);
  150. }
  151. /*
  152.  * i2o_lan_handle_transaction_error(): IOP or DDM has rejected the request
  153.  * for general cause (format error, bad function code, insufficient resources,
  154.  * etc.). We get one transaction_error for each failed transaction.
  155.  */
  156. static void i2o_lan_handle_transaction_error(struct net_device *dev, u32 *msg)
  157. {
  158. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  159. struct sk_buff *skb;
  160. i2o_report_status(KERN_INFO, dev->name, msg);
  161. /* If PacketSend was rejected, free sk_buff reserved by upper layers */
  162. if (msg[1] >> 24 == LAN_PACKET_SEND) {
  163. skb = (struct sk_buff *)(msg[3]); // TransactionContext
  164. dev_kfree_skb_irq(skb);
  165. atomic_dec(&priv->tx_out);
  166. if (netif_queue_stopped(dev))
  167. netif_wake_queue(dev);
  168.   }
  169. /* If ReceivePost was rejected, free sk_buff we have reserved */
  170. if (msg[1] >> 24 == LAN_RECEIVE_POST) {
  171. skb = (struct sk_buff *)(msg[3]);
  172. dev_kfree_skb_irq(skb);
  173. atomic_dec(&priv->buckets_out);
  174. }
  175. }
  176. /*
  177.  * i2o_lan_handle_status(): Common parts of handling a not succeeded request
  178.  * (status != SUCCESS).
  179.  */
  180. static int i2o_lan_handle_status(struct net_device *dev, u32 *msg)
  181. {
  182. /* Fail bit set? */
  183. if (msg[0] & MSG_FAIL) {
  184. i2o_lan_handle_failure(dev, msg);
  185. return -1;
  186. }
  187. /* Message rejected for general cause? */
  188. if ((msg[4]>>24) == I2O_REPLY_STATUS_TRANSACTION_ERROR) {
  189. i2o_lan_handle_transaction_error(dev, msg);
  190. return -1;
  191. }
  192. /* Else have to handle it in the callback function */
  193. return 0;
  194. }
  195. /* Callback functions called from the interrupt routine:
  196. =======================================================*/
  197. /*
  198.  * i2o_lan_send_post_reply(): Callback function to handle PostSend replies.
  199.  */
  200. static void i2o_lan_send_post_reply(struct i2o_handler *h,
  201. struct i2o_controller *iop, struct i2o_message *m)
  202. {
  203. u32 *msg = (u32 *)m;
  204. u8 unit  = (u8)(msg[2]>>16); // InitiatorContext
  205. struct net_device *dev = i2o_landevs[unit];
  206. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  207. u8 trl_count  = msg[3] & 0x000000FF;
  208. if ((msg[4] >> 24) != I2O_REPLY_STATUS_SUCCESS) {
  209. if (i2o_lan_handle_status(dev, msg))
  210. return;
  211. }
  212. #ifdef DRIVERDEBUG
  213. i2o_report_status(KERN_INFO, dev->name, msg);
  214. #endif
  215. /* DDM has handled transmit request(s), free sk_buffs.
  216.  * We get similar single transaction reply also in error cases 
  217.  * (except if msg failure or transaction error).
  218.  */
  219. while (trl_count) {
  220. dev_kfree_skb_irq((struct sk_buff *)msg[4 + trl_count]);
  221. dprintk(KERN_INFO "%s: tx skb freed (trl_count=%d).n",
  222. dev->name, trl_count);
  223. atomic_dec(&priv->tx_out);
  224. trl_count--;
  225. }
  226. /* If priv->tx_out had reached tx_max_out, the queue was stopped */
  227. if (netif_queue_stopped(dev))
  228.   netif_wake_queue(dev);
  229. }
  230. /*
  231.  * i2o_lan_receive_post_reply(): Callback function to process incoming packets.
  232.  */
  233. static void i2o_lan_receive_post_reply(struct i2o_handler *h,
  234. struct i2o_controller *iop, struct i2o_message *m)
  235. {
  236. u32 *msg = (u32 *)m;
  237. u8 unit  = (u8)(msg[2]>>16); // InitiatorContext
  238. struct net_device *dev = i2o_landevs[unit];
  239. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  240. struct i2o_bucket_descriptor *bucket = (struct i2o_bucket_descriptor *)&msg[6];
  241. struct i2o_packet_info *packet;
  242. u8 trl_count = msg[3] & 0x000000FF;
  243. struct sk_buff *skb, *old_skb;
  244. unsigned long flags = 0;
  245. if ((msg[4] >> 24) != I2O_REPLY_STATUS_SUCCESS) {
  246. if (i2o_lan_handle_status(dev, msg))
  247. return;
  248. i2o_lan_release_buckets(dev, msg);
  249. return;
  250. }
  251. #ifdef DRIVERDEBUG
  252. i2o_report_status(KERN_INFO, dev->name, msg);
  253. #endif
  254. /* Else we are receiving incoming post. */
  255. while (trl_count--) {
  256. skb = (struct sk_buff *)bucket->context;
  257. packet = (struct i2o_packet_info *)bucket->packet_info;
  258. atomic_dec(&priv->buckets_out);
  259. /* Sanity checks: Any weird characteristics in bucket? */
  260. if (packet->flags & 0x0f || ! packet->flags & 0x40) {
  261. if (packet->flags & 0x01)
  262. printk(KERN_WARNING "%s: packet with errors, error code=0x%02x.n",
  263. dev->name, packet->status & 0xff);
  264. /* The following shouldn't happen, unless parameters in
  265.  * LAN_OPERATION group are changed during the run time.
  266.  */
  267.  if (packet->flags & 0x0c)
  268. printk(KERN_DEBUG "%s: multi-bucket packets not supported!n", 
  269. dev->name);
  270. if (! packet->flags & 0x40)
  271. printk(KERN_DEBUG "%s: multiple packets in a bucket not supported!n", 
  272. dev->name);
  273. dev_kfree_skb_irq(skb);
  274. bucket++;
  275. continue;
  276. }
  277. /* Copy short packet to a new skb */
  278. if (packet->len < priv->rx_copybreak) {
  279. old_skb = skb;
  280. skb = (struct sk_buff *)dev_alloc_skb(packet->len+2);
  281. if (skb == NULL) {
  282. printk(KERN_ERR "%s: Can't allocate skb.n", dev->name);
  283. return;
  284. }
  285. skb_reserve(skb, 2);
  286. memcpy(skb_put(skb, packet->len), old_skb->data, packet->len);
  287. spin_lock_irqsave(&priv->fbl_lock, flags);
  288. if (priv->i2o_fbl_tail < I2O_LAN_MAX_BUCKETS_OUT)
  289. priv->i2o_fbl[++priv->i2o_fbl_tail] = old_skb;
  290. else
  291. dev_kfree_skb_irq(old_skb);
  292. spin_unlock_irqrestore(&priv->fbl_lock, flags);
  293. } else
  294. skb_put(skb, packet->len);
  295. /* Deliver to upper layers */
  296. skb->dev = dev;
  297. skb->protocol = priv->type_trans(skb, dev);
  298. netif_rx(skb);
  299. dev->last_rx = jiffies;
  300. dprintk(KERN_INFO "%s: Incoming packet (%d bytes) delivered "
  301. "to upper level.n", dev->name, packet->len);
  302. bucket++; // to next Packet Descriptor Block
  303. }
  304. #ifdef DRIVERDEBUG
  305. if (msg[5] == 0)
  306. printk(KERN_INFO "%s: DDM out of buckets (priv->count = %d)!n",
  307.        dev->name, atomic_read(&priv->buckets_out));
  308. #endif
  309. /* If DDM has already consumed bucket_thresh buckets, post new ones */
  310. if (atomic_read(&priv->buckets_out) <= priv->max_buckets_out - priv->bucket_thresh) {
  311. run_i2o_post_buckets_task.data = (void *)dev;
  312. queue_task(&run_i2o_post_buckets_task, &tq_immediate);
  313. mark_bh(IMMEDIATE_BH);
  314. }
  315. return;
  316. }
  317. /*
  318.  * i2o_lan_reply(): Callback function to handle other incoming messages
  319.  * except SendPost and ReceivePost.
  320.  */
  321. static void i2o_lan_reply(struct i2o_handler *h, struct i2o_controller *iop,
  322.   struct i2o_message *m)
  323. {
  324. u32 *msg = (u32 *)m;
  325. u8 unit  = (u8)(msg[2]>>16); // InitiatorContext
  326. struct net_device *dev = i2o_landevs[unit];
  327. if ((msg[4] >> 24) != I2O_REPLY_STATUS_SUCCESS) {
  328. if (i2o_lan_handle_status(dev, msg))
  329. return;
  330. /* In other error cases just report and continue */
  331. i2o_report_status(KERN_INFO, dev->name, msg);
  332. }
  333. #ifdef DRIVERDEBUG
  334. i2o_report_status(KERN_INFO, dev->name, msg);
  335. #endif
  336. switch (msg[1] >> 24) {
  337. case LAN_RESET:
  338. case LAN_SUSPEND:
  339. /* default reply without payload */
  340. break;
  341. case I2O_CMD_UTIL_EVT_REGISTER: 
  342. case I2O_CMD_UTIL_EVT_ACK:
  343. i2o_lan_handle_event(dev, msg);
  344. break;
  345. case I2O_CMD_UTIL_PARAMS_SET:
  346. /* default reply, results in ReplyPayload (not examined) */
  347. switch (msg[3] >> 16) {
  348.     case 1: dprintk(KERN_INFO "%s: Reply to set MAC filter mask.n",
  349. dev->name);
  350.     break;
  351.     case 2: dprintk(KERN_INFO "%s: Reply to set MAC table.n", 
  352. dev->name);
  353.     break;
  354.     default: printk(KERN_WARNING "%s: Bad group 0x%04Xn",
  355.   dev->name,msg[3] >> 16);
  356. }
  357. break;
  358. default:
  359. printk(KERN_ERR "%s: No handler for the reply.n",
  360.         dev->name);
  361. i2o_report_status(KERN_INFO, dev->name, msg);
  362. }
  363. }
  364. /* Functions used by the above callback functions:
  365. =================================================*/
  366. /*
  367.  * i2o_lan_release_buckets(): Free unused buckets (sk_buffs).
  368.  */
  369. static void i2o_lan_release_buckets(struct net_device *dev, u32 *msg)
  370. {
  371. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  372. u8 trl_elem_size = (u8)(msg[3]>>8 & 0x000000FF);
  373. u8 trl_count = (u8)(msg[3] & 0x000000FF);
  374. u32 *pskb = &msg[6];
  375. while (trl_count--) {
  376. dprintk(KERN_DEBUG "%s: Releasing unused rx skb %p (trl_count=%d).n",
  377. dev->name, (struct sk_buff*)(*pskb),trl_count+1);
  378. dev_kfree_skb_irq((struct sk_buff *)(*pskb));
  379. pskb += 1 + trl_elem_size;
  380. atomic_dec(&priv->buckets_out);
  381. }
  382. }
  383. /*
  384.  * i2o_lan_event_reply(): Handle events.
  385.  */
  386. static void i2o_lan_handle_event(struct net_device *dev, u32 *msg)
  387. {
  388. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  389. struct i2o_device *i2o_dev = priv->i2o_dev;
  390. struct i2o_controller *iop = i2o_dev->controller;
  391. u32 max_evt_data_size =iop->status_block->inbound_frame_size-5;
  392. struct i2o_reply {
  393. u32 header[4];
  394. u32 evt_indicator;
  395. u32 data[max_evt_data_size];
  396. } *evt = (struct i2o_reply *)msg;
  397. int evt_data_len = ((msg[0]>>16) - 5) * 4; /* real size*/
  398. printk(KERN_INFO "%s: I2O event - ", dev->name);
  399. if (msg[1]>>24 == I2O_CMD_UTIL_EVT_ACK) {
  400. printk("Event acknowledgement reply.n");
  401. return;
  402. }
  403. /* Else evt->function == I2O_CMD_UTIL_EVT_REGISTER) */
  404. switch (evt->evt_indicator) {
  405. case I2O_EVT_IND_STATE_CHANGE:  {
  406. struct state_data {
  407. u16 status;
  408. u8 state;
  409. u8 data;
  410. } *evt_data = (struct state_data *)(evt->data[0]);
  411. printk("State chance 0x%08x.n", evt->data[0]);
  412. /* If the DDM is in error state, recovery may be
  413.  * possible if status = Transmit or Receive Control
  414.  * Unit Inoperable.
  415.  */
  416. if (evt_data->state==0x05 && evt_data->status==0x0003)
  417. i2o_lan_reset(dev);
  418. break;
  419. }
  420. case I2O_EVT_IND_FIELD_MODIFIED: {
  421. u16 *work16 = (u16 *)evt->data;
  422. printk("Group 0x%04x, field %d changed.n", work16[0], work16[1]);
  423. break;
  424. }
  425. case I2O_EVT_IND_VENDOR_EVT: {
  426. int i;
  427. printk("Vendor event:n");
  428. for (i = 0; i < evt_data_len / 4; i++)
  429. printk("   0x%08xn", evt->data[i]);
  430. break;
  431. }
  432. case I2O_EVT_IND_DEVICE_RESET:
  433. /* Spec 2.0 p. 6-121:
  434.  * The event of _DEVICE_RESET should also be responded
  435.  */
  436. printk("Device reset.n");
  437. if (i2o_event_ack(iop, msg) < 0)
  438. printk("%s: Event Acknowledge timeout.n", dev->name);
  439. break;
  440. #if 0
  441. case I2O_EVT_IND_EVT_MASK_MODIFIED:
  442. printk("Event mask modified, 0x%08x.n", evt->data[0]);
  443. break;
  444. case I2O_EVT_IND_GENERAL_WARNING:
  445. printk("General warning 0x%04x.n", evt->data[0]);
  446. break;
  447. case I2O_EVT_IND_CONFIGURATION_FLAG:
  448. printk("Configuration requested.n");
  449. break;
  450. case I2O_EVT_IND_CAPABILITY_CHANGE:
  451. printk("Capability change 0x%04x.n", evt->data[0]);
  452. break;
  453. case I2O_EVT_IND_DEVICE_STATE:
  454. printk("Device state changed 0x%08x.n", evt->data[0]);
  455. break;
  456. #endif
  457. case I2O_LAN_EVT_LINK_DOWN:
  458. netif_carrier_off(dev); 
  459. printk("Link to the physical device is lost.n");
  460. break;
  461. case I2O_LAN_EVT_LINK_UP:
  462. netif_carrier_on(dev); 
  463. printk("Link to the physical device is (re)established.n");
  464. break;
  465. case I2O_LAN_EVT_MEDIA_CHANGE:
  466. printk("Media change.n");
  467. break;
  468. default:
  469. printk("0x%08x. No handler.n", evt->evt_indicator);
  470. }
  471. }
  472. /*
  473.  * i2o_lan_receive_post(): Post buckets to receive packets.
  474.  */
  475. static int i2o_lan_receive_post(struct net_device *dev)
  476. {
  477. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  478. struct i2o_device *i2o_dev = priv->i2o_dev;
  479. struct i2o_controller *iop = i2o_dev->controller;
  480. struct sk_buff *skb;
  481. u32 m, *msg;
  482. u32 bucket_len = (dev->mtu + dev->hard_header_len);
  483. u32 total = priv->max_buckets_out - atomic_read(&priv->buckets_out);
  484. u32 bucket_count;
  485. u32 *sgl_elem;
  486. unsigned long flags;
  487. /* Send (total/bucket_count) separate I2O requests */
  488. while (total) {
  489. m = I2O_POST_READ32(iop);
  490. if (m == 0xFFFFFFFF)
  491. return -ETIMEDOUT;
  492. msg = (u32 *)(iop->mem_offset + m);
  493. bucket_count = (total >= priv->sgl_max) ? priv->sgl_max : total;
  494. total -= bucket_count;
  495. atomic_add(bucket_count, &priv->buckets_out);
  496. dprintk(KERN_INFO "%s: Sending %d buckets (size %d) to LAN DDM.n",
  497. dev->name, bucket_count, bucket_len);
  498. /* Fill in the header */
  499. __raw_writel(I2O_MESSAGE_SIZE(4 + 3 * bucket_count) | SGL_OFFSET_4, msg);
  500. __raw_writel(LAN_RECEIVE_POST<<24 | HOST_TID<<12 | i2o_dev->lct_data.tid, msg+1);
  501. __raw_writel(priv->unit << 16 | lan_receive_context, msg+2);
  502. __raw_writel(bucket_count, msg+3);
  503. sgl_elem = &msg[4];
  504. /* Fill in the payload - contains bucket_count SGL elements */
  505. while (bucket_count--) {
  506. spin_lock_irqsave(&priv->fbl_lock, flags);
  507. if (priv->i2o_fbl_tail >= 0)
  508. skb = priv->i2o_fbl[priv->i2o_fbl_tail--];
  509. else {
  510. skb = dev_alloc_skb(bucket_len + 2);
  511. if (skb == NULL) {
  512. spin_unlock_irqrestore(&priv->fbl_lock, flags);
  513. return -ENOMEM;
  514. }
  515. skb_reserve(skb, 2);
  516. }
  517. spin_unlock_irqrestore(&priv->fbl_lock, flags);
  518. __raw_writel(0x51000000 | bucket_len, sgl_elem);
  519. __raw_writel((u32)skb,       sgl_elem+1);
  520. __raw_writel(virt_to_bus(skb->data),  sgl_elem+2);
  521. sgl_elem += 3;
  522. }
  523. /* set LE flag and post  */
  524. __raw_writel(__raw_readl(sgl_elem-3) | 0x80000000, (sgl_elem-3));
  525. i2o_post_message(iop, m);
  526. }
  527. return 0;
  528. }
  529. /* Functions called from the network stack, and functions called by them:
  530. ========================================================================*/
  531. /*
  532.  * i2o_lan_reset(): Reset the LAN adapter into the operational state and
  533.  *  restore it to full operation.
  534.  */
  535. static int i2o_lan_reset(struct net_device *dev)
  536. {
  537. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  538. struct i2o_device *i2o_dev = priv->i2o_dev;
  539. struct i2o_controller *iop = i2o_dev->controller;
  540. u32 msg[5];
  541. dprintk(KERN_INFO "%s: LAN RESET MESSAGE.n", dev->name);
  542. msg[0] = FIVE_WORD_MSG_SIZE | SGL_OFFSET_0;
  543. msg[1] = LAN_RESET<<24 | HOST_TID<<12 | i2o_dev->lct_data.tid;
  544. msg[2] = priv->unit << 16 | lan_context; // InitiatorContext
  545. msg[3] = 0;   // TransactionContext
  546. msg[4] = 0;  // Keep posted buckets
  547. if (i2o_post_this(iop, msg, sizeof(msg)) < 0)
  548. return -ETIMEDOUT;
  549. return 0;
  550. }
  551. /*
  552.  * i2o_lan_suspend(): Put LAN adapter into a safe, non-active state.
  553.  *  IOP replies to any LAN class message with status error_no_data_transfer
  554.  * / suspended.
  555.  */
  556. static int i2o_lan_suspend(struct net_device *dev)
  557. {
  558. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  559. struct i2o_device *i2o_dev = priv->i2o_dev;
  560. struct i2o_controller *iop = i2o_dev->controller;
  561. u32 msg[5];
  562. dprintk(KERN_INFO "%s: LAN SUSPEND MESSAGE.n", dev->name);
  563. msg[0] = FIVE_WORD_MSG_SIZE | SGL_OFFSET_0;
  564. msg[1] = LAN_SUSPEND<<24 | HOST_TID<<12 | i2o_dev->lct_data.tid;
  565. msg[2] = priv->unit << 16 | lan_context; // InitiatorContext
  566. msg[3] = 0;   // TransactionContext
  567. msg[4] = 1 << 16;   // return posted buckets
  568. if (i2o_post_this(iop, msg, sizeof(msg)) < 0)
  569. return -ETIMEDOUT;
  570. return 0;
  571. }
  572. /*
  573.  * i2o_set_ddm_parameters:
  574.  * These settings are done to ensure proper initial values for DDM.
  575.  * They can be changed via proc file system or vai configuration utility.
  576.  */
  577. static void i2o_set_ddm_parameters(struct net_device *dev)
  578. {
  579. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  580. struct i2o_device *i2o_dev = priv->i2o_dev;
  581. struct i2o_controller *iop = i2o_dev->controller;
  582. u32 val;
  583. /*
  584.  * When PacketOrphanlimit is set to the maximum packet length,
  585.  * the packets will never be split into two separate buckets
  586.  */
  587. val = dev->mtu + dev->hard_header_len;
  588. if (i2o_set_scalar(iop, i2o_dev->lct_data.tid, 0x0004, 2, &val, sizeof(val)) < 0)
  589. printk(KERN_WARNING "%s: Unable to set PacketOrphanLimit.n",
  590.        dev->name);
  591. else
  592. dprintk(KERN_INFO "%s: PacketOrphanLimit set to %d.n",
  593. dev->name, val);
  594. /* When RxMaxPacketsBucket = 1, DDM puts only one packet into bucket */
  595. val = 1;
  596. if (i2o_set_scalar(iop, i2o_dev->lct_data.tid, 0x0008, 4, &val, sizeof(val)) <0)
  597. printk(KERN_WARNING "%s: Unable to set RxMaxPacketsBucket.n",
  598.        dev->name);
  599. else
  600. dprintk(KERN_INFO "%s: RxMaxPacketsBucket set to %d.n", 
  601. dev->name, val);
  602. return;
  603. }
  604. /* Functions called from the network stack:
  605. ==========================================*/
  606. /*
  607.  * i2o_lan_open(): Open the device to send/receive packets via
  608.  * the network device.
  609.  */
  610. static int i2o_lan_open(struct net_device *dev)
  611. {
  612. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  613. struct i2o_device *i2o_dev = priv->i2o_dev;
  614. struct i2o_controller *iop = i2o_dev->controller;
  615. u32 mc_addr_group[64];
  616. MOD_INC_USE_COUNT;
  617. if (i2o_claim_device(i2o_dev, &i2o_lan_handler)) {
  618. printk(KERN_WARNING "%s: Unable to claim the I2O LAN device.n", dev->name);
  619. MOD_DEC_USE_COUNT;
  620. return -EAGAIN;
  621. }
  622. dprintk(KERN_INFO "%s: I2O LAN device (tid=%d) claimed by LAN OSM.n",
  623. dev->name, i2o_dev->lct_data.tid);
  624. if (i2o_event_register(iop, i2o_dev->lct_data.tid,
  625.        priv->unit << 16 | lan_context, 0, priv->i2o_event_mask) < 0)
  626. printk(KERN_WARNING "%s: Unable to set the event mask.n", dev->name);
  627. i2o_lan_reset(dev);
  628. /* Get the max number of multicast addresses */
  629. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0001, -1,
  630.      &mc_addr_group, sizeof(mc_addr_group)) < 0 ) {
  631. printk(KERN_WARNING "%s: Unable to query LAN_MAC_ADDRESS group.n", dev->name);
  632. MOD_DEC_USE_COUNT;
  633. return -EAGAIN;
  634. }
  635. priv->max_size_mc_table = mc_addr_group[8];
  636. /* Malloc space for free bucket list to resuse reveive post buckets */
  637. priv->i2o_fbl = kmalloc(priv->max_buckets_out * sizeof(struct sk_buff *),
  638. GFP_KERNEL);
  639. if (priv->i2o_fbl == NULL) {
  640. MOD_DEC_USE_COUNT;
  641. return -ENOMEM;
  642. }
  643. priv->i2o_fbl_tail = -1;
  644. priv->send_active = 0;
  645. i2o_set_ddm_parameters(dev);
  646. i2o_lan_receive_post(dev);
  647. netif_start_queue(dev);
  648. return 0;
  649. }
  650. /*
  651.  * i2o_lan_close(): End the transfering.
  652.  */
  653. static int i2o_lan_close(struct net_device *dev)
  654. {
  655. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  656. struct i2o_device *i2o_dev = priv->i2o_dev;
  657. struct i2o_controller *iop = i2o_dev->controller;
  658. int ret = 0;
  659. netif_stop_queue(dev);
  660. i2o_lan_suspend(dev);
  661. if (i2o_event_register(iop, i2o_dev->lct_data.tid,
  662.        priv->unit << 16 | lan_context, 0, 0) < 0)
  663. printk(KERN_WARNING "%s: Unable to clear the event mask.n",
  664.        dev->name);
  665. while (priv->i2o_fbl_tail >= 0)
  666. dev_kfree_skb(priv->i2o_fbl[priv->i2o_fbl_tail--]);
  667. kfree(priv->i2o_fbl);
  668. if (i2o_release_device(i2o_dev, &i2o_lan_handler)) {
  669. printk(KERN_WARNING "%s: Unable to unclaim I2O LAN device "
  670.        "(tid=%d).n", dev->name, i2o_dev->lct_data.tid);
  671. ret = -EBUSY;
  672. }
  673. MOD_DEC_USE_COUNT;
  674. return ret;
  675. }
  676. /*
  677.  * i2o_lan_tx_timeout(): Tx timeout handler.
  678.  */
  679. static void i2o_lan_tx_timeout(struct net_device *dev)
  680. {
  681.   if (!netif_queue_stopped(dev))
  682. netif_start_queue(dev);
  683. }
  684. /*
  685.  * i2o_lan_batch_send(): Send packets in batch. 
  686.  * Both i2o_lan_sdu_send and i2o_lan_packet_send use this.
  687.  */
  688. static void i2o_lan_batch_send(struct net_device *dev)
  689. {
  690. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  691. struct i2o_controller *iop = priv->i2o_dev->controller;
  692. spin_lock_irq(&priv->tx_lock);
  693. if (priv->tx_count != 0) {
  694. dev->trans_start = jiffies;
  695. i2o_post_message(iop, priv->m);
  696. dprintk(KERN_DEBUG "%s: %d packets sent.n", dev->name, priv->tx_count);
  697. priv->tx_count = 0;
  698. }
  699. priv->send_active = 0;
  700. spin_unlock_irq(&priv->tx_lock);
  701. MOD_DEC_USE_COUNT;
  702. }
  703. #ifdef CONFIG_NET_FC
  704. /*
  705.  * i2o_lan_sdu_send(): Send a packet, MAC header added by the DDM.
  706.  * Must be supported by Fibre Channel, optional for Ethernet/802.3,
  707.  * Token Ring, FDDI
  708.  */
  709. static int i2o_lan_sdu_send(struct sk_buff *skb, struct net_device *dev)
  710. {
  711. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  712. struct i2o_device *i2o_dev = priv->i2o_dev;
  713. struct i2o_controller *iop = i2o_dev->controller;
  714. int tickssofar = jiffies - dev->trans_start;
  715. u32 m, *msg;
  716. u32 *sgl_elem;
  717. spin_lock_irq(&priv->tx_lock);
  718. priv->tx_count++;
  719. atomic_inc(&priv->tx_out);
  720. /* 
  721.  * If tx_batch_mode = 0x00 forced to immediate mode
  722.  * If tx_batch_mode = 0x01 forced to batch mode
  723.  * If tx_batch_mode = 0x10 switch automatically, current mode immediate
  724.  * If tx_batch_mode = 0x11 switch automatically, current mode batch
  725.  * If gap between two packets is > 0 ticks, switch to immediate
  726.  */
  727. if (priv->tx_batch_mode >> 1) // switch automatically
  728. priv->tx_batch_mode = tickssofar ? 0x02 : 0x03;
  729. if (priv->tx_count == 1) {
  730. m = I2O_POST_READ32(iop);
  731. if (m == 0xFFFFFFFF) {
  732. spin_unlock_irq(&priv->tx_lock);
  733. return 1;
  734. }
  735. msg = (u32 *)(iop->mem_offset + m);
  736. priv->m = m;
  737. __raw_writel(NINE_WORD_MSG_SIZE | 1<<12 | SGL_OFFSET_4, msg);
  738. __raw_writel(LAN_PACKET_SEND<<24 | HOST_TID<<12 | i2o_dev->lct_data.tid, msg+1);
  739. __raw_writel(priv->unit << 16 | lan_send_context, msg+2); // InitiatorContext
  740. __raw_writel(1 << 30 | 1 << 3, msg+3);      // TransmitControlWord
  741. __raw_writel(0xD7000000 | skb->len, msg+4);        // MAC hdr included
  742. __raw_writel((u32)skb, msg+5);        // TransactionContext
  743. __raw_writel(virt_to_bus(skb->data), msg+6);
  744. __raw_writel((u32)skb->mac.raw, msg+7);
  745. __raw_writel((u32)skb->mac.raw+4, msg+8);
  746. if ((priv->tx_batch_mode & 0x01) && !priv->send_active) {
  747. priv->send_active = 1;
  748. MOD_INC_USE_COUNT;
  749. if (schedule_task(&priv->i2o_batch_send_task) == 0)
  750. MOD_DEC_USE_COUNT;
  751. }
  752. } else {  /* Add new SGL element to the previous message frame */
  753. msg = (u32 *)(iop->mem_offset + priv->m);
  754. sgl_elem = &msg[priv->tx_count * 5 + 1];
  755. __raw_writel(I2O_MESSAGE_SIZE((__raw_readl(msg)>>16) + 5) | 1<<12 | SGL_OFFSET_4, msg);
  756. __raw_writel(__raw_readl(sgl_elem-5) & 0x7FFFFFFF, sgl_elem-5); /* clear LE flag */
  757. __raw_writel(0xD5000000 | skb->len, sgl_elem);
  758. __raw_writel((u32)skb, sgl_elem+1);
  759. __raw_writel(virt_to_bus(skb->data), sgl_elem+2);
  760. __raw_writel((u32)(skb->mac.raw), sgl_elem+3);
  761. __raw_writel((u32)(skb->mac.raw)+1, sgl_elem+4);
  762. }
  763. /* If tx not in batch mode or frame is full, send immediatelly */
  764. if (!(priv->tx_batch_mode & 0x01) || priv->tx_count == priv->sgl_max) {
  765. dev->trans_start = jiffies;
  766. i2o_post_message(iop, priv->m);
  767. dprintk(KERN_DEBUG "%s: %d packets sent.n", dev->name, priv->tx_count);
  768. priv->tx_count = 0;
  769. }
  770. /* If DDMs TxMaxPktOut reached, stop queueing layer to send more */
  771. if (atomic_read(&priv->tx_out) >= priv->tx_max_out)
  772. netif_stop_queue(dev);
  773. spin_unlock_irq(&priv->tx_lock);
  774. return 0;
  775. }
  776. #endif /* CONFIG_NET_FC */
  777. /*
  778.  * i2o_lan_packet_send(): Send a packet as is, including the MAC header.
  779.  *
  780.  * Must be supported by Ethernet/802.3, Token Ring, FDDI, optional for
  781.  * Fibre Channel
  782.  */
  783. static int i2o_lan_packet_send(struct sk_buff *skb, struct net_device *dev)
  784. {
  785. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  786. struct i2o_device *i2o_dev = priv->i2o_dev;
  787. struct i2o_controller *iop = i2o_dev->controller;
  788. int tickssofar = jiffies - dev->trans_start;
  789. u32 m, *msg;
  790. u32 *sgl_elem;
  791. spin_lock_irq(&priv->tx_lock);
  792. priv->tx_count++;
  793. atomic_inc(&priv->tx_out);
  794. /* 
  795.  * If tx_batch_mode = 0x00 forced to immediate mode
  796.  * If tx_batch_mode = 0x01 forced to batch mode
  797.  * If tx_batch_mode = 0x10 switch automatically, current mode immediate
  798.  * If tx_batch_mode = 0x11 switch automatically, current mode batch
  799.  * If gap between two packets is > 0 ticks, switch to immediate
  800.  */
  801. if (priv->tx_batch_mode >> 1) // switch automatically
  802. priv->tx_batch_mode = tickssofar ? 0x02 : 0x03;
  803. if (priv->tx_count == 1) {
  804. m = I2O_POST_READ32(iop);
  805. if (m == 0xFFFFFFFF) {
  806. spin_unlock_irq(&priv->tx_lock);
  807. return 1;
  808. }
  809. msg = (u32 *)(iop->mem_offset + m);
  810. priv->m = m;
  811. __raw_writel(SEVEN_WORD_MSG_SIZE | 1<<12 | SGL_OFFSET_4, msg);
  812. __raw_writel(LAN_PACKET_SEND<<24 | HOST_TID<<12 | i2o_dev->lct_data.tid, msg+1);
  813. __raw_writel(priv->unit << 16 | lan_send_context, msg+2); // InitiatorContext
  814. __raw_writel(1 << 30 | 1 << 3, msg+3);      // TransmitControlWord
  815. // bit 30: reply as soon as transmission attempt is complete
  816. // bit 3: Suppress CRC generation
  817. __raw_writel(0xD5000000 | skb->len, msg+4);        // MAC hdr included
  818. __raw_writel((u32)skb, msg+5);        // TransactionContext
  819. __raw_writel(virt_to_bus(skb->data), msg+6);
  820. if ((priv->tx_batch_mode & 0x01) && !priv->send_active) {
  821. priv->send_active = 1;
  822. MOD_INC_USE_COUNT;
  823. if (schedule_task(&priv->i2o_batch_send_task) == 0)
  824. MOD_DEC_USE_COUNT;
  825. }
  826. } else {  /* Add new SGL element to the previous message frame */
  827. msg = (u32 *)(iop->mem_offset + priv->m);
  828. sgl_elem = &msg[priv->tx_count * 3 + 1];
  829. __raw_writel(I2O_MESSAGE_SIZE((__raw_readl(msg)>>16) + 3) | 1<<12 | SGL_OFFSET_4, msg);
  830. __raw_writel(__raw_readl(sgl_elem-3) & 0x7FFFFFFF, sgl_elem-3); /* clear LE flag */
  831. __raw_writel(0xD5000000 | skb->len, sgl_elem);
  832. __raw_writel((u32)skb, sgl_elem+1);
  833. __raw_writel(virt_to_bus(skb->data), sgl_elem+2);
  834. }
  835. /* If tx is in immediate mode or frame is full, send now */
  836. if (!(priv->tx_batch_mode & 0x01) || priv->tx_count == priv->sgl_max) {
  837. dev->trans_start = jiffies;
  838. i2o_post_message(iop, priv->m);
  839. dprintk(KERN_DEBUG "%s: %d packets sent.n", dev->name, priv->tx_count);
  840. priv->tx_count = 0;
  841. }
  842. /* If DDMs TxMaxPktOut reached, stop queueing layer to send more */
  843. if (atomic_read(&priv->tx_out) >= priv->tx_max_out)
  844. netif_stop_queue(dev);
  845. spin_unlock_irq(&priv->tx_lock);
  846. return 0;
  847. }
  848. /*
  849.  * i2o_lan_get_stats(): Fill in the statistics.
  850.  */
  851. static struct net_device_stats *i2o_lan_get_stats(struct net_device *dev)
  852. {
  853. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  854. struct i2o_device *i2o_dev = priv->i2o_dev;
  855. struct i2o_controller *iop = i2o_dev->controller;
  856. u64 val64[16];
  857. u64 supported_group[4] = { 0, 0, 0, 0 };
  858. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0100, -1, val64,
  859.      sizeof(val64)) < 0)
  860. printk(KERN_INFO "%s: Unable to query LAN_HISTORICAL_STATS.n", dev->name);
  861. else {
  862. dprintk(KERN_DEBUG "%s: LAN_HISTORICAL_STATS queried.n", dev->name);
  863. priv->stats.tx_packets = val64[0];
  864. priv->stats.tx_bytes   = val64[1];
  865. priv->stats.rx_packets = val64[2];
  866. priv->stats.rx_bytes   = val64[3];
  867. priv->stats.tx_errors  = val64[4];
  868. priv->stats.rx_errors  = val64[5];
  869. priv->stats.rx_dropped = val64[6];
  870. }
  871. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0180, -1,
  872.      &supported_group, sizeof(supported_group)) < 0)
  873. printk(KERN_INFO "%s: Unable to query LAN_SUPPORTED_OPTIONAL_HISTORICAL_STATS.n", dev->name);
  874. if (supported_group[2]) {
  875. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0183, -1,
  876.      val64, sizeof(val64)) < 0)
  877. printk(KERN_INFO "%s: Unable to query LAN_OPTIONAL_RX_HISTORICAL_STATS.n", dev->name);
  878. else {
  879. dprintk(KERN_DEBUG "%s: LAN_OPTIONAL_RX_HISTORICAL_STATS queried.n", dev->name);
  880. priv->stats.multicast      = val64[4];
  881. priv->stats.rx_length_errors = val64[10];
  882. priv->stats.rx_crc_errors    = val64[0];
  883. }
  884. }
  885. if (i2o_dev->lct_data.sub_class == I2O_LAN_ETHERNET) {
  886. u64 supported_stats = 0;
  887. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0200, -1,
  888.      val64, sizeof(val64)) < 0)
  889. printk(KERN_INFO "%s: Unable to query LAN_802_3_HISTORICAL_STATS.n", dev->name);
  890. else {
  891. dprintk(KERN_DEBUG "%s: LAN_802_3_HISTORICAL_STATS queried.n", dev->name);
  892.   priv->stats.transmit_collision = val64[1] + val64[2];
  893. priv->stats.rx_frame_errors    = val64[0];
  894. priv->stats.tx_carrier_errors  = val64[6];
  895. }
  896. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0280, -1,
  897.      &supported_stats, sizeof(supported_stats)) < 0)
  898. printk(KERN_INFO "%s: Unable to query LAN_SUPPORTED_802_3_HISTORICAL_STATS.n", dev->name);
  899. if (supported_stats != 0) {
  900. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0281, -1,
  901.      val64, sizeof(val64)) < 0)
  902. printk(KERN_INFO "%s: Unable to query LAN_OPTIONAL_802_3_HISTORICAL_STATS.n", dev->name);
  903. else {
  904. dprintk(KERN_DEBUG "%s: LAN_OPTIONAL_802_3_HISTORICAL_STATS queried.n", dev->name);
  905. if (supported_stats & 0x1)
  906. priv->stats.rx_over_errors = val64[0];
  907. if (supported_stats & 0x4)
  908. priv->stats.tx_heartbeat_errors = val64[2];
  909. }
  910. }
  911. }
  912. #ifdef CONFIG_TR
  913. if (i2o_dev->lct_data.sub_class == I2O_LAN_TR) {
  914. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0300, -1,
  915.      val64, sizeof(val64)) < 0)
  916. printk(KERN_INFO "%s: Unable to query LAN_802_5_HISTORICAL_STATS.n", dev->name);
  917. else {
  918. struct tr_statistics *stats =
  919. (struct tr_statistics *)&priv->stats;
  920. dprintk(KERN_DEBUG "%s: LAN_802_5_HISTORICAL_STATS queried.n", dev->name);
  921. stats->line_errors = val64[0];
  922. stats->internal_errors = val64[7];
  923. stats->burst_errors = val64[4];
  924. stats->A_C_errors = val64[2];
  925. stats->abort_delimiters = val64[3];
  926. stats->lost_frames = val64[1];
  927. /* stats->recv_congest_count = ?;  FIXME ??*/
  928. stats->frame_copied_errors = val64[5];
  929. stats->frequency_errors = val64[6];
  930. stats->token_errors = val64[9];
  931. }
  932. /* Token Ring optional stats not yet defined */
  933. }
  934. #endif
  935. #ifdef CONFIG_FDDI
  936. if (i2o_dev->lct_data.sub_class == I2O_LAN_FDDI) {
  937. if (i2o_query_scalar(iop, i2o_dev->lct_data.tid, 0x0400, -1,
  938.      val64, sizeof(val64)) < 0)
  939. printk(KERN_INFO "%s: Unable to query LAN_FDDI_HISTORICAL_STATS.n", dev->name);
  940. else {
  941. dprintk(KERN_DEBUG "%s: LAN_FDDI_HISTORICAL_STATS queried.n", dev->name);
  942. priv->stats.smt_cf_state = val64[0];
  943. memcpy(priv->stats.mac_upstream_nbr, &val64[1], FDDI_K_ALEN);
  944. memcpy(priv->stats.mac_downstream_nbr, &val64[2], FDDI_K_ALEN);
  945. priv->stats.mac_error_cts = val64[3];
  946. priv->stats.mac_lost_cts  = val64[4];
  947. priv->stats.mac_rmt_state = val64[5];
  948. memcpy(priv->stats.port_lct_fail_cts, &val64[6], 8);
  949. memcpy(priv->stats.port_lem_reject_cts, &val64[7], 8);
  950. memcpy(priv->stats.port_lem_cts, &val64[8], 8);
  951. memcpy(priv->stats.port_pcm_state, &val64[9], 8);
  952. }
  953. /* FDDI optional stats not yet defined */
  954. }
  955. #endif
  956. #ifdef CONFIG_NET_FC
  957. /* Fibre Channel Statistics not yet defined in 1.53 nor 2.0 */
  958. #endif
  959. return (struct net_device_stats *)&priv->stats;
  960. }
  961. /* 
  962.  * i2o_lan_set_mc_filter(): Post a request to set multicast filter.
  963.  */
  964. int i2o_lan_set_mc_filter(struct net_device *dev, u32 filter_mask)
  965. {
  966. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  967. struct i2o_device *i2o_dev = priv->i2o_dev;
  968. struct i2o_controller *iop = i2o_dev->controller;
  969. u32 msg[10]; 
  970. msg[0] = TEN_WORD_MSG_SIZE | SGL_OFFSET_5;
  971. msg[1] = I2O_CMD_UTIL_PARAMS_SET << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid;
  972. msg[2] = priv->unit << 16 | lan_context;
  973. msg[3] = 0x0001 << 16 | 3 ; // TransactionContext: group&field
  974. msg[4] = 0;
  975. msg[5] = 0xCC000000 | 16;  // Immediate data SGL
  976. msg[6] = 1; // OperationCount
  977. msg[7] = 0x0001<<16 | I2O_PARAMS_FIELD_SET; // Group, Operation
  978. msg[8] = 3 << 16 | 1;  // FieldIndex, FieldCount 
  979. msg[9] = filter_mask; // Value
  980. return i2o_post_this(iop, msg, sizeof(msg));
  981. }
  982. /* 
  983.  * i2o_lan_set_mc_table(): Post a request to set LAN_MULTICAST_MAC_ADDRESS table.
  984.  */
  985. int i2o_lan_set_mc_table(struct net_device *dev)
  986. {
  987. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  988. struct i2o_device *i2o_dev = priv->i2o_dev;
  989. struct i2o_controller *iop = i2o_dev->controller;
  990. struct dev_mc_list *mc;
  991. u32 msg[10 + 2 * dev->mc_count]; 
  992. u8 *work8 = (u8 *)(msg + 10);
  993. msg[0] = I2O_MESSAGE_SIZE(10 + 2 * dev->mc_count) | SGL_OFFSET_5;
  994. msg[1] = I2O_CMD_UTIL_PARAMS_SET << 24 | HOST_TID << 12 | i2o_dev->lct_data.tid;
  995. msg[2] = priv->unit << 16 | lan_context; // InitiatorContext
  996. msg[3] = 0x0002 << 16 | (u16)-1; // TransactionContext
  997. msg[4] = 0; // OperationFlags
  998. msg[5] = 0xCC000000 | (16 + 8 * dev->mc_count); // Immediate data SGL
  999. msg[6] = 2; // OperationCount
  1000. msg[7] = 0x0002 << 16 | I2O_PARAMS_TABLE_CLEAR; // Group, Operation
  1001. msg[8] = 0x0002 << 16 | I2O_PARAMS_ROW_ADD;     // Group, Operation
  1002. msg[9] = dev->mc_count << 16 | (u16)-1;  // RowCount, FieldCount
  1003.         for (mc = dev->mc_list; mc ; mc = mc->next, work8 += 8) {
  1004.   memset(work8, 0, 8);
  1005.                   memcpy(work8, mc->dmi_addr, mc->dmi_addrlen); // Values
  1006. }
  1007. return i2o_post_this(iop, msg, sizeof(msg));
  1008. }
  1009. /*
  1010.  * i2o_lan_set_multicast_list(): Enable a network device to receive packets
  1011.  *      not send to the protocol address.
  1012.  */
  1013. static void i2o_lan_set_multicast_list(struct net_device *dev)
  1014. {
  1015. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  1016. u32 filter_mask;
  1017. if (dev->flags & IFF_PROMISC) {
  1018. filter_mask = 0x00000002;
  1019. dprintk(KERN_INFO "%s: Enabling promiscuous mode...n", dev->name);
  1020. } else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count > priv->max_size_mc_table) {
  1021. filter_mask = 0x00000004;
  1022. dprintk(KERN_INFO "%s: Enabling all multicast mode...n", dev->name);
  1023. } else if (dev->mc_count) {
  1024. filter_mask = 0x00000000;
  1025. dprintk(KERN_INFO "%s: Enabling multicast mode...n", dev->name);
  1026. if (i2o_lan_set_mc_table(dev) < 0)
  1027. printk(KERN_WARNING "%s: Unable to send MAC table.n", dev->name);
  1028. } else {
  1029. filter_mask = 0x00000300; // Broadcast, Multicast disabled
  1030. dprintk(KERN_INFO "%s: Enabling unicast mode...n", dev->name);
  1031. }
  1032. /* Finally copy new FilterMask to DDM */
  1033. if (i2o_lan_set_mc_filter(dev, filter_mask) < 0)
  1034. printk(KERN_WARNING "%s: Unable to send MAC FilterMask.n", dev->name);
  1035. }
  1036. /*
  1037.  * i2o_lan_change_mtu(): Change maximum transfer unit size.
  1038.  */
  1039. static int i2o_lan_change_mtu(struct net_device *dev, int new_mtu)
  1040. {
  1041. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  1042. struct i2o_device *i2o_dev = priv->i2o_dev;
  1043. u32 max_pkt_size;
  1044. if (i2o_query_scalar(i2o_dev->controller, i2o_dev->lct_data.tid,
  1045.        0x0000, 6, &max_pkt_size, 4) < 0)
  1046. return -EFAULT;
  1047. if (new_mtu < 68 || new_mtu > 9000 || new_mtu > max_pkt_size)
  1048. return -EINVAL;
  1049. dev->mtu = new_mtu;
  1050. i2o_lan_suspend(dev);    // to SUSPENDED state, return buckets
  1051. while (priv->i2o_fbl_tail >= 0) // free buffered buckets
  1052. dev_kfree_skb(priv->i2o_fbl[priv->i2o_fbl_tail--]);
  1053. i2o_lan_reset(dev); // to OPERATIONAL state
  1054. i2o_set_ddm_parameters(dev);  // reset some parameters
  1055. i2o_lan_receive_post(dev);  // post new buckets (new size)
  1056. return 0;
  1057. }
  1058. /* Functions to initialize I2O LAN OSM:
  1059. ======================================*/
  1060. /*
  1061.  * i2o_lan_register_device(): Register LAN class device to kernel.
  1062.  */
  1063. struct net_device *i2o_lan_register_device(struct i2o_device *i2o_dev)
  1064. {
  1065. struct net_device *dev = NULL;
  1066. struct i2o_lan_local *priv = NULL;
  1067. u8 hw_addr[8];
  1068. u32 tx_max_out = 0;
  1069. unsigned short (*type_trans)(struct sk_buff *, struct net_device *);
  1070. void (*unregister_dev)(struct net_device *dev);
  1071. switch (i2o_dev->lct_data.sub_class) {
  1072. case I2O_LAN_ETHERNET:
  1073. dev = init_etherdev(NULL, sizeof(struct i2o_lan_local));
  1074. if (dev == NULL)
  1075. return NULL;
  1076. type_trans = eth_type_trans;
  1077. unregister_dev = unregister_netdev;
  1078. break;
  1079. #ifdef CONFIG_ANYLAN
  1080. case I2O_LAN_100VG:
  1081. printk(KERN_ERR "i2o_lan: 100base VG not yet supported.n");
  1082. return NULL;
  1083. break;
  1084. #endif
  1085. #ifdef CONFIG_TR
  1086. case I2O_LAN_TR:
  1087. dev = init_trdev(NULL, sizeof(struct i2o_lan_local));
  1088. if (dev==NULL)
  1089. return NULL;
  1090. type_trans = tr_type_trans;
  1091. unregister_dev = unregister_trdev;
  1092. break;
  1093. #endif
  1094. #ifdef CONFIG_FDDI
  1095. case I2O_LAN_FDDI:
  1096. {
  1097. int size = sizeof(struct net_device) + sizeof(struct i2o_lan_local);
  1098. dev = (struct net_device *) kmalloc(size, GFP_KERNEL);
  1099. if (dev == NULL)
  1100. return NULL;
  1101. memset((char *)dev, 0, size);
  1102.      dev->priv = (void *)(dev + 1);
  1103. if (dev_alloc_name(dev, "fddi%d") < 0) {
  1104. printk(KERN_WARNING "i2o_lan: Too many FDDI devices.n");
  1105. kfree(dev);
  1106. return NULL;
  1107. }
  1108. type_trans = fddi_type_trans;
  1109. unregister_dev = (void *)unregister_netdevice;
  1110. fddi_setup(dev);
  1111. register_netdev(dev);
  1112. }
  1113. break;
  1114. #endif
  1115. #ifdef CONFIG_NET_FC
  1116. case I2O_LAN_FIBRE_CHANNEL:
  1117. dev = init_fcdev(NULL, sizeof(struct i2o_lan_local));
  1118. if (dev == NULL)
  1119. return NULL;
  1120. type_trans = NULL;
  1121. /* FIXME: Move fc_type_trans() from drivers/net/fc/iph5526.c to net/802/fc.c
  1122.  * and export it in include/linux/fcdevice.h
  1123.  * type_trans = fc_type_trans;
  1124.  */
  1125. unregister_dev = (void *)unregister_fcdev;
  1126. break;
  1127. #endif
  1128. case I2O_LAN_UNKNOWN:
  1129. default:
  1130. printk(KERN_ERR "i2o_lan: LAN type 0x%04x not supported.n",
  1131.        i2o_dev->lct_data.sub_class);
  1132. return NULL;
  1133. }
  1134. priv = (struct i2o_lan_local *)dev->priv;
  1135. priv->i2o_dev = i2o_dev;
  1136. priv->type_trans = type_trans;
  1137. priv->sgl_max = (i2o_dev->controller->status_block->inbound_frame_size - 4) / 3;
  1138. atomic_set(&priv->buckets_out, 0);
  1139. /* Set default values for user configurable parameters */
  1140. /* Private values are changed via /proc file system */
  1141. priv->max_buckets_out = max_buckets_out;
  1142. priv->bucket_thresh   = bucket_thresh;
  1143. priv->rx_copybreak    = rx_copybreak;
  1144. priv->tx_batch_mode   = tx_batch_mode & 0x03;
  1145. priv->i2o_event_mask  = i2o_event_mask;
  1146. priv->tx_lock       = SPIN_LOCK_UNLOCKED;
  1147. priv->fbl_lock       = SPIN_LOCK_UNLOCKED;
  1148. unit++;
  1149. i2o_landevs[unit] = dev;
  1150. priv->unit = unit;
  1151. if (i2o_query_scalar(i2o_dev->controller, i2o_dev->lct_data.tid,
  1152.      0x0001, 0, &hw_addr, sizeof(hw_addr)) < 0) {
  1153. printk(KERN_ERR "%s: Unable to query hardware address.n", dev->name);
  1154. unit--;
  1155. unregister_dev(dev);
  1156. kfree(dev);
  1157. return NULL;
  1158. }
  1159. dprintk(KERN_DEBUG "%s: hwaddr = %02X:%02X:%02X:%02X:%02X:%02Xn",
  1160.   dev->name, hw_addr[0], hw_addr[1], hw_addr[2], hw_addr[3],
  1161. hw_addr[4], hw_addr[5]);
  1162. dev->addr_len = 6;
  1163. memcpy(dev->dev_addr, hw_addr, 6);
  1164. if (i2o_query_scalar(i2o_dev->controller, i2o_dev->lct_data.tid,
  1165.      0x0007, 2, &tx_max_out, sizeof(tx_max_out)) < 0) {
  1166. printk(KERN_ERR "%s: Unable to query max TX queue.n", dev->name);
  1167. unit--;
  1168. unregister_dev(dev);
  1169. kfree(dev);
  1170. return NULL;
  1171. }
  1172. dprintk(KERN_INFO "%s: Max TX Outstanding = %d.n", dev->name, tx_max_out);
  1173. priv->tx_max_out = tx_max_out;
  1174. atomic_set(&priv->tx_out, 0);
  1175. priv->tx_count = 0;
  1176. INIT_LIST_HEAD(&priv->i2o_batch_send_task.list);
  1177. priv->i2o_batch_send_task.sync    = 0;
  1178. priv->i2o_batch_send_task.routine = (void *)i2o_lan_batch_send;
  1179. priv->i2o_batch_send_task.data    = (void *)dev;
  1180. dev->open = i2o_lan_open;
  1181. dev->stop = i2o_lan_close;
  1182. dev->get_stats = i2o_lan_get_stats;
  1183. dev->set_multicast_list = i2o_lan_set_multicast_list;
  1184. dev->tx_timeout = i2o_lan_tx_timeout;
  1185. dev->watchdog_timeo = I2O_LAN_TX_TIMEOUT;
  1186. #ifdef CONFIG_NET_FC
  1187. if (i2o_dev->lct_data.sub_class == I2O_LAN_FIBRE_CHANNEL)
  1188. dev->hard_start_xmit = i2o_lan_sdu_send;
  1189. else
  1190. #endif
  1191. dev->hard_start_xmit = i2o_lan_packet_send;
  1192. if (i2o_dev->lct_data.sub_class == I2O_LAN_ETHERNET)
  1193. dev->change_mtu = i2o_lan_change_mtu;
  1194. return dev;
  1195. }
  1196. static int __init i2o_lan_init(void)
  1197. {
  1198. struct net_device *dev;
  1199. int i;
  1200. printk(KERN_INFO "I2O LAN OSM (C) 1999 University of Helsinki.n");
  1201. /* Module params are used as global defaults for private values */
  1202. if (max_buckets_out > I2O_LAN_MAX_BUCKETS_OUT)
  1203. max_buckets_out = I2O_LAN_MAX_BUCKETS_OUT;
  1204. if (bucket_thresh > max_buckets_out)
  1205. bucket_thresh = max_buckets_out;
  1206. /* Install handlers for incoming replies */
  1207. if (i2o_install_handler(&i2o_lan_send_handler) < 0) {
  1208.   printk(KERN_ERR "i2o_lan: Unable to register I2O LAN OSM.n");
  1209. return -EINVAL;
  1210. }
  1211. lan_send_context = i2o_lan_send_handler.context;
  1212. if (i2o_install_handler(&i2o_lan_receive_handler) < 0) {
  1213.   printk(KERN_ERR "i2o_lan: Unable to register I2O LAN OSM.n");
  1214. return -EINVAL;
  1215. }
  1216. lan_receive_context = i2o_lan_receive_handler.context;
  1217. if (i2o_install_handler(&i2o_lan_handler) < 0) {
  1218.   printk(KERN_ERR "i2o_lan: Unable to register I2O LAN OSM.n");
  1219. return -EINVAL;
  1220. }
  1221. lan_context = i2o_lan_handler.context;
  1222. for(i=0; i <= MAX_LAN_CARDS; i++)
  1223. i2o_landevs[i] = NULL;
  1224. for (i=0; i < MAX_I2O_CONTROLLERS; i++) {
  1225. struct i2o_controller *iop = i2o_find_controller(i);
  1226. struct i2o_device *i2o_dev;
  1227. if (iop==NULL)
  1228. continue;
  1229. for (i2o_dev=iop->devices;i2o_dev != NULL;i2o_dev=i2o_dev->next) {
  1230. if (i2o_dev->lct_data.class_id != I2O_CLASS_LAN)
  1231. continue;
  1232. /* Make sure device not already claimed by an ISM */
  1233. if (i2o_dev->lct_data.user_tid != 0xFFF)
  1234. continue;
  1235. if (unit == MAX_LAN_CARDS) {
  1236. i2o_unlock_controller(iop);
  1237. printk(KERN_WARNING "i2o_lan: Too many I2O LAN devices.n");
  1238. return -EINVAL;
  1239. }
  1240. dev = i2o_lan_register_device(i2o_dev);
  1241.   if (dev == NULL) {
  1242. printk(KERN_ERR "i2o_lan: Unable to register I2O LAN device 0x%04x.n",
  1243.        i2o_dev->lct_data.sub_class);
  1244. continue;
  1245. }
  1246. printk(KERN_INFO "%s: I2O LAN device registered, "
  1247. "subclass = 0x%04x, unit = %d, tid = %d.n",
  1248. dev->name, i2o_dev->lct_data.sub_class,
  1249. ((struct i2o_lan_local *)dev->priv)->unit,
  1250. i2o_dev->lct_data.tid);
  1251. }
  1252. i2o_unlock_controller(iop);
  1253. }
  1254. dprintk(KERN_INFO "%d I2O LAN devices found and registered.n", unit+1);
  1255. return 0;
  1256. }
  1257. static void i2o_lan_exit(void)
  1258. {
  1259. int i;
  1260. for (i = 0; i <= unit; i++) {
  1261. struct net_device *dev = i2o_landevs[i];
  1262. struct i2o_lan_local *priv = (struct i2o_lan_local *)dev->priv;
  1263. struct i2o_device *i2o_dev = priv->i2o_dev;
  1264. switch (i2o_dev->lct_data.sub_class) {
  1265. case I2O_LAN_ETHERNET:
  1266. unregister_netdev(dev);
  1267. break;
  1268. #ifdef CONFIG_FDDI
  1269. case I2O_LAN_FDDI:
  1270. unregister_netdevice(dev);
  1271. break;
  1272. #endif
  1273. #ifdef CONFIG_TR
  1274. case I2O_LAN_TR:
  1275. unregister_trdev(dev);
  1276. break;
  1277. #endif
  1278. #ifdef CONFIG_NET_FC
  1279. case I2O_LAN_FIBRE_CHANNEL:
  1280. unregister_fcdev(dev);
  1281. break;
  1282. #endif
  1283. default:
  1284. printk(KERN_WARNING "%s: Spurious I2O LAN subclass 0x%08x.n",
  1285.        dev->name, i2o_dev->lct_data.sub_class);
  1286. }
  1287. dprintk(KERN_INFO "%s: I2O LAN device unregistered.n",
  1288. dev->name);
  1289. kfree(dev);
  1290. }
  1291. i2o_remove_handler(&i2o_lan_handler);
  1292. i2o_remove_handler(&i2o_lan_send_handler);
  1293. i2o_remove_handler(&i2o_lan_receive_handler);
  1294. }
  1295. EXPORT_NO_SYMBOLS;
  1296. MODULE_AUTHOR("University of Helsinki, Department of Computer Science");
  1297. MODULE_DESCRIPTION("I2O Lan OSM");
  1298. MODULE_LICENSE("GPL");
  1299. MODULE_PARM(max_buckets_out, "1-" __MODULE_STRING(I2O_LAN_MAX_BUCKETS_OUT) "i");
  1300. MODULE_PARM_DESC(max_buckets_out, "Total number of buckets to post (1-)");
  1301. MODULE_PARM(bucket_thresh, "1-" __MODULE_STRING(I2O_LAN_MAX_BUCKETS_OUT) "i");
  1302. MODULE_PARM_DESC(bucket_thresh, "Bucket post threshold (1-)");
  1303. MODULE_PARM(rx_copybreak, "1-" "i");
  1304. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy only small frames (1-)");
  1305. MODULE_PARM(tx_batch_mode, "0-2" "i");
  1306. MODULE_PARM_DESC(tx_batch_mode, "0=Send immediatelly, 1=Send in batches, 2=Switch automatically");
  1307. module_init(i2o_lan_init);
  1308. module_exit(i2o_lan_exit);